1 // 2 // Copyright (C) 2020 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #pragma once 17 18 #include <cstddef> 19 20 #include "keymaster/serializable.h" 21 #include "tss2/tss2_mu.h" 22 #include "tss2/tss2_rc.h" 23 #include "tss2/tss2_tpm2_types.h" 24 25 #include <android-base/logging.h> 26 27 /** 28 * An implementation of a keymaster::Serializable type that refers to a TPM type 29 * by an unmanaged pointer. When the TpmSerializable serializes or deserializes 30 * data, it loads it from and saves it to the pointed at instance. 31 * 32 * The serialization format is the same as the one used in the command protocol 33 * for TPM messages. 34 * 35 * This is a template class, specialized in the corresponding implementation 36 * file for the TPM types necessary to serialize as part of larger Keymaster 37 * serializable types. 38 */ 39 template<typename Type> 40 class TpmSerializable : public keymaster::Serializable { 41 public: 42 TpmSerializable(Type*); 43 44 size_t SerializedSize() const override; 45 uint8_t* Serialize(uint8_t* buf, const uint8_t* end) const override; 46 bool Deserialize(const uint8_t** buf_ptr, const uint8_t* end) override; 47 private: 48 Type* instance_; 49 }; 50 51 using SerializeTpmKeyPrivate = TpmSerializable<TPM2B_PRIVATE>; 52 using SerializeTpmKeyPublic = TpmSerializable<TPM2B_PUBLIC>; 53