1 // 2 // Copyright (C) 2015 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_TRUNKS_CLIENT_TEST_H_ 18 #define TRUNKS_TRUNKS_CLIENT_TEST_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "trunks/scoped_key_handle.h" 24 #include "trunks/tpm_generated.h" 25 #include "trunks/trunks_factory.h" 26 27 namespace trunks { 28 29 // This class is used to perform integration tests on the TPM. Each public 30 // method defines a different test to perform. 31 // NOTE: All these tests require that the TPM be owned, and SRKs exist. 32 // Example usage: 33 // TrunksClientTest test(factory); 34 // CHECK(test.RNGTest()); 35 // CHECK(test.SimplePolicyTest()); 36 class TrunksClientTest { 37 public: 38 // Does not take ownership of factory. 39 explicit TrunksClientTest(const TrunksFactory& factory); 40 virtual ~TrunksClientTest(); 41 42 // This test verifies that the Random Number Generator on the TPM is working 43 // correctly. 44 bool RNGTest(); 45 46 // This test verifies that we can create an unrestricted RSA signing key and 47 // use it to sign arbitrary data. 48 bool SignTest(); 49 50 // This test verfifies that we can create an unrestricted RSA decryption key 51 // and use it to encrypt and decrypt arbitrary data. 52 bool DecryptTest(); 53 54 // This test verifies that we can import a RSA key into the TPM and use it 55 // to encrypt and decrypt some data. 56 bool ImportTest(); 57 58 // This test verifies that we can change a key's authorization data and 59 // still use it to encrypt/decrypt data. 60 bool AuthChangeTest(); 61 62 // This test verifies that we can create a key and then confirm that it 63 // was created by the TPM. 64 bool VerifyKeyCreationTest(); 65 66 // This test verifies that we can seal a secret to the TPM and access 67 // it later. 68 bool SealedDataTest(); 69 70 // This test performs a simple PCR extension and then reads the value in the 71 // PCR to verify if it is correct. 72 // NOTE: PCR banks need to be configured for this test to succeed. Normally 73 // this is done by the platform firmware. 74 bool PCRTest(); 75 76 // This test sets up a PolicySession with the PolicyAuthValue assertion. 77 // This policy is then used to create a key and use it to sign/verify and 78 // encrypt/decrypt. 79 bool PolicyAuthValueTest(); 80 81 // This test sets up a PolicySession that is based on the current PCR value 82 // and a CommandCode for signing. The key created this way is restricted to 83 // be only used for signing, and only if the PCR remains unchanged. The key 84 // is then used to sign arbitrary data, and the signature verified. 85 bool PolicyAndTest(); 86 87 // This test performs a complex assertion using PolicyOR. 88 // We create an unrestricted key, and restricts it to signing 89 // and decryption using Policy Sessions. 90 bool PolicyOrTest(); 91 92 // This test verfies that we can create, write, read, lock and delete 93 // NV spaces in the TPM. 94 // NOTE: This test needs the |owner_password| to work. 95 bool NvramTest(const std::string& owner_password); 96 97 // This test uses many key handles simultaneously. 98 bool ManyKeysTest(); 99 100 // This test uses many sessions simultaneously. 101 bool ManySessionsTest(); 102 103 private: 104 // This method verifies that plaintext == decrypt(encrypt(plaintext)) using 105 // a given key. 106 // TODO(usanghi): Remove |session| argument once we can support multiple 107 // sessions. 108 bool PerformRSAEncrpytAndDecrpyt(TPM_HANDLE key_handle, 109 const std::string& key_authorization, 110 HmacSession* session); 111 112 // Generates an RSA key pair in software. The |modulus| and |prime_factor| 113 // must not be NULL and will be populated with values that can be imported 114 // into the TPM. The |public_key| may be NULL, but if it is not, will be 115 // populated with a value that can be used with VerifyRSASignature. 116 void GenerateRSAKeyPair(std::string* modulus, 117 std::string* prime_factor, 118 std::string* public_key); 119 120 // Verifies an RSA-SSA-SHA256 |signature| over the given |data|. The 121 // |public_key| is as produced by GenerateRSAKeyPair(). Returns true on 122 // success. 123 bool VerifyRSASignature(const std::string& public_key, 124 const std::string& data, 125 const std::string& signature); 126 127 // Loads an arbitrary RSA signing key and provides the |key_handle| and the 128 // |public_key|. Returns true on success. 129 bool LoadSigningKey(ScopedKeyHandle* key_handle, std::string* public_key); 130 131 // Signs arbitrary data with |key_handle| authorized by |delegate| and 132 // verifies the signature with |public_key|. Returns true on success. 133 bool SignAndVerify(const ScopedKeyHandle& key_handle, 134 const std::string& public_key, 135 AuthorizationDelegate* delegate); 136 137 // Factory for instantiation of Tpm classes 138 const TrunksFactory& factory_; 139 140 DISALLOW_COPY_AND_ASSIGN(TrunksClientTest); 141 }; 142 143 } // namespace trunks 144 145 #endif // TRUNKS_TRUNKS_CLIENT_TEST_H_ 146