1// 2// Copyright (C) 2014 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 17#ifndef TRUNKS_TPM_UTILITY_IMPL_H_ 18#define TRUNKS_TPM_UTILITY_IMPL_H_ 19 20#include "trunks/tpm_utility.h" 21 22#include <map> 23#include <string> 24 25#include <base/macros.h> 26#include <base/memory/scoped_ptr.h> 27#include <gtest/gtest_prod.h> 28 29#include "trunks/trunks_export.h" 30 31namespace trunks { 32 33class AuthorizationDelegate; 34class TrunksFactory; 35 36// A default implementation of TpmUtility. 37class TRUNKS_EXPORT TpmUtilityImpl : public TpmUtility { 38 public: 39 explicit TpmUtilityImpl(const TrunksFactory& factory); 40 ~TpmUtilityImpl() override; 41 42 // TpmUtility methods. 43 TPM_RC Startup() override; 44 TPM_RC Clear() override; 45 void Shutdown() override; 46 TPM_RC InitializeTpm() override; 47 TPM_RC AllocatePCR(const std::string& platform_password) override; 48 TPM_RC TakeOwnership(const std::string& owner_password, 49 const std::string& endorsement_password, 50 const std::string& lockout_password) override; 51 TPM_RC StirRandom(const std::string& entropy_data, 52 AuthorizationDelegate* delegate) override; 53 TPM_RC GenerateRandom(size_t num_bytes, 54 AuthorizationDelegate* delegate, 55 std::string* random_data) override; 56 TPM_RC ExtendPCR(int pcr_index, 57 const std::string& extend_data, 58 AuthorizationDelegate* delegate) override; 59 TPM_RC ReadPCR(int pcr_index, std::string* pcr_value) override; 60 TPM_RC AsymmetricEncrypt(TPM_HANDLE key_handle, 61 TPM_ALG_ID scheme, 62 TPM_ALG_ID hash_alg, 63 const std::string& plaintext, 64 AuthorizationDelegate* delegate, 65 std::string* ciphertext) override; 66 TPM_RC AsymmetricDecrypt(TPM_HANDLE key_handle, 67 TPM_ALG_ID scheme, 68 TPM_ALG_ID hash_alg, 69 const std::string& ciphertext, 70 AuthorizationDelegate* delegate, 71 std::string* plaintext) override; 72 TPM_RC Sign(TPM_HANDLE key_handle, 73 TPM_ALG_ID scheme, 74 TPM_ALG_ID hash_alg, 75 const std::string& plaintext, 76 AuthorizationDelegate* delegate, 77 std::string* signature) override; 78 TPM_RC Verify(TPM_HANDLE key_handle, 79 TPM_ALG_ID scheme, 80 TPM_ALG_ID hash_alg, 81 const std::string& plaintext, 82 const std::string& signature, 83 AuthorizationDelegate* delegate) override; 84 TPM_RC CertifyCreation(TPM_HANDLE key_handle, 85 const std::string& creation_blob) override; 86 TPM_RC ChangeKeyAuthorizationData(TPM_HANDLE key_handle, 87 const std::string& new_password, 88 AuthorizationDelegate* delegate, 89 std::string* key_blob) override; 90 TPM_RC ImportRSAKey(AsymmetricKeyUsage key_type, 91 const std::string& modulus, 92 uint32_t public_exponent, 93 const std::string& prime_factor, 94 const std::string& password, 95 AuthorizationDelegate* delegate, 96 std::string* key_blob) override; 97 TPM_RC CreateRSAKeyPair(AsymmetricKeyUsage key_type, 98 int modulus_bits, 99 uint32_t public_exponent, 100 const std::string& password, 101 const std::string& policy_digest, 102 bool use_only_policy_authorization, 103 int creation_pcr_index, 104 AuthorizationDelegate* delegate, 105 std::string* key_blob, 106 std::string* creation_blob) override; 107 TPM_RC LoadKey(const std::string& key_blob, 108 AuthorizationDelegate* delegate, 109 TPM_HANDLE* key_handle) override; 110 TPM_RC GetKeyName(TPM_HANDLE handle, std::string* name) override; 111 TPM_RC GetKeyPublicArea(TPM_HANDLE handle, 112 TPMT_PUBLIC* public_data) override; 113 TPM_RC SealData(const std::string& data_to_seal, 114 const std::string& policy_digest, 115 AuthorizationDelegate* delegate, 116 std::string* sealed_data) override; 117 TPM_RC UnsealData(const std::string& sealed_data, 118 AuthorizationDelegate* delegate, 119 std::string* unsealed_data) override; 120 TPM_RC StartSession(HmacSession* session) override; 121 TPM_RC GetPolicyDigestForPcrValue(int pcr_index, 122 const std::string& pcr_value, 123 std::string* policy_digest) override; 124 TPM_RC DefineNVSpace(uint32_t index, 125 size_t num_bytes, 126 AuthorizationDelegate* delegate) override; 127 TPM_RC DestroyNVSpace(uint32_t index, 128 AuthorizationDelegate* delegate) override; 129 TPM_RC LockNVSpace(uint32_t index, AuthorizationDelegate* delegate) override; 130 TPM_RC WriteNVSpace(uint32_t index, 131 uint32_t offset, 132 const std::string& nvram_data, 133 AuthorizationDelegate* delegate) override; 134 TPM_RC ReadNVSpace(uint32_t index, 135 uint32_t offset, 136 size_t num_bytes, 137 std::string* nvram_data, 138 AuthorizationDelegate* delegate) override; 139 TPM_RC GetNVSpaceName(uint32_t index, std::string* name) override; 140 TPM_RC GetNVSpacePublicArea(uint32_t index, 141 TPMS_NV_PUBLIC* public_data) override; 142 143 private: 144 friend class TpmUtilityTest; 145 146 const TrunksFactory& factory_; 147 std::map<uint32_t, TPMS_NV_PUBLIC> nvram_public_area_map_; 148 149 // This method sets a known owner password in the TPM_RH_OWNER hierarchy. 150 TPM_RC SetKnownOwnerPassword(const std::string& known_owner_password); 151 152 // Synchronously derives storage root keys for RSA and ECC and persists the 153 // keys in the TPM. This operation must be authorized by the |owner_password| 154 // and, on success, KRSAStorageRootKey and kECCStorageRootKey can be used 155 // with an empty authorization value until the TPM is cleared. 156 TPM_RC CreateStorageRootKeys(const std::string& owner_password); 157 158 // This method creates an RSA decryption key to be used for salting sessions. 159 // This method also makes the salting key permanent under the storage 160 // hierarchy. 161 TPM_RC CreateSaltingKey(const std::string& owner_password); 162 163 // This method returns a partially filled TPMT_PUBLIC strucutre, 164 // which can then be modified by other methods to create the public 165 // template for a key. It takes a valid |key_type| tp construct the 166 // parameters. 167 TPMT_PUBLIC CreateDefaultPublicArea(TPM_ALG_ID key_alg); 168 169 // Sets TPM |hierarchy| authorization to |password| using |authorization|. 170 TPM_RC SetHierarchyAuthorization(TPMI_RH_HIERARCHY_AUTH hierarchy, 171 const std::string& password, 172 AuthorizationDelegate* authorization); 173 174 // Disables the TPM platform hierarchy until the next startup. This requires 175 // platform |authorization|. 176 TPM_RC DisablePlatformHierarchy(AuthorizationDelegate* authorization); 177 178 // Given a public area, this method computes the object name. Following 179 // TPM2.0 Specification Part 1 section 16, 180 // object_name = HashAlg || Hash(public_area); 181 TPM_RC ComputeKeyName(const TPMT_PUBLIC& public_area, 182 std::string* object_name); 183 184 // Given a public area, this method computers the NVSpace's name. 185 // It follows TPM2.0 Specification Part 1 section 16, 186 // nv_name = HashAlg || Hash(nv_public_area); 187 TPM_RC ComputeNVSpaceName(const TPMS_NV_PUBLIC& nv_public_area, 188 std::string* nv_name); 189 190 // This encrypts the |sensitive_data| struct according to the specification 191 // defined in TPM2.0 spec Part 1: Figure 19. 192 TPM_RC EncryptPrivateData(const TPMT_SENSITIVE& sensitive_area, 193 const TPMT_PUBLIC& public_area, 194 TPM2B_PRIVATE* encrypted_private_data, 195 TPM2B_DATA* encryption_key); 196 197 // Looks for a given persistent |key_handle| and outputs whether or not it 198 // |exists|. Returns TPM_RC_SUCCESS on success. 199 TPM_RC DoesPersistentKeyExist(TPMI_DH_PERSISTENT key_handle, bool* exists); 200 201 DISALLOW_COPY_AND_ASSIGN(TpmUtilityImpl); 202}; 203 204} // namespace trunks 205 206#endif // TRUNKS_TPM_UTILITY_IMPL_H_ 207