David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | #ifndef ANDROID_HARDWARE_IDENTITY_FAKESECUREHARDWAREPROXY_H |
| 18 | #define ANDROID_HARDWARE_IDENTITY_FAKESECUREHARDWAREPROXY_H |
| 19 | |
| 20 | #include <libeic/libeic.h> |
| 21 | |
| 22 | #include "SecureHardwareProxy.h" |
| 23 | |
| 24 | namespace android::hardware::identity { |
| 25 | |
| 26 | // This implementation uses libEmbeddedIC in-process. |
| 27 | // |
| 28 | class FakeSecureHardwareProvisioningProxy : public SecureHardwareProvisioningProxy { |
| 29 | public: |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 30 | FakeSecureHardwareProvisioningProxy() = default; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 31 | virtual ~FakeSecureHardwareProvisioningProxy(); |
| 32 | |
| 33 | bool initialize(bool testCredential) override; |
| 34 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 35 | bool initializeForUpdate(bool testCredential, const string& docType, |
| 36 | const vector<uint8_t>& encryptedCredentialKeys) override; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 37 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 38 | bool shutdown() override; |
| 39 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 40 | optional<uint32_t> getId() override; |
| 41 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 42 | // Returns public key certificate. |
| 43 | optional<vector<uint8_t>> createCredentialKey(const vector<uint8_t>& challenge, |
| 44 | const vector<uint8_t>& applicationId) override; |
| 45 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 46 | bool startPersonalization(int accessControlProfileCount, const vector<int>& entryCounts, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 47 | const string& docType, |
| 48 | size_t expectedProofOfProvisioningSize) override; |
| 49 | |
| 50 | // Returns MAC (28 bytes). |
| 51 | optional<vector<uint8_t>> addAccessControlProfile(int id, |
| 52 | const vector<uint8_t>& readerCertificate, |
| 53 | bool userAuthenticationRequired, |
| 54 | uint64_t timeoutMillis, |
| 55 | uint64_t secureUserId) override; |
| 56 | |
| 57 | bool beginAddEntry(const vector<int>& accessControlProfileIds, const string& nameSpace, |
| 58 | const string& name, uint64_t entrySize) override; |
| 59 | |
| 60 | // Returns encryptedContent. |
| 61 | optional<vector<uint8_t>> addEntryValue(const vector<int>& accessControlProfileIds, |
| 62 | const string& nameSpace, const string& name, |
| 63 | const vector<uint8_t>& content) override; |
| 64 | |
| 65 | // Returns signatureOfToBeSigned (EIC_ECDSA_P256_SIGNATURE_SIZE bytes). |
| 66 | optional<vector<uint8_t>> finishAddingEntries() override; |
| 67 | |
| 68 | // Returns encryptedCredentialKeys (80 bytes). |
| 69 | optional<vector<uint8_t>> finishGetCredentialData(const string& docType) override; |
| 70 | |
| 71 | protected: |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 72 | // See docs for id_. |
| 73 | // |
| 74 | bool validateId(const string& callerName); |
| 75 | |
| 76 | // We use a singleton libeic object, shared by all proxy instances. This is to |
| 77 | // properly simulate a situation where libeic is used on constrained hardware |
| 78 | // with only enough RAM for a single instance of the libeic object. |
| 79 | // |
| 80 | static EicProvisioning ctx_; |
| 81 | |
| 82 | // On the HAL side we keep track of the ID that was assigned to the libeic object |
| 83 | // created in secure hardware. For every call into libeic we validate that this |
| 84 | // identifier matches what is on the secure side. This is what the validateId() |
| 85 | // method does. |
| 86 | // |
| 87 | uint32_t id_ = 0; |
| 88 | }; |
| 89 | |
| 90 | // This implementation uses libEmbeddedIC in-process. |
| 91 | // |
| 92 | class FakeSecureHardwareSessionProxy : public SecureHardwareSessionProxy { |
| 93 | public: |
| 94 | FakeSecureHardwareSessionProxy() = default; |
| 95 | virtual ~FakeSecureHardwareSessionProxy(); |
| 96 | |
| 97 | bool initialize() override; |
| 98 | |
| 99 | bool shutdown() override; |
| 100 | |
| 101 | optional<uint32_t> getId() override; |
| 102 | |
| 103 | optional<uint64_t> getAuthChallenge() override; |
| 104 | |
| 105 | // Returns private key |
| 106 | optional<vector<uint8_t>> getEphemeralKeyPair() override; |
| 107 | |
| 108 | bool setReaderEphemeralPublicKey(const vector<uint8_t>& readerEphemeralPublicKey) override; |
| 109 | |
| 110 | bool setSessionTranscript(const vector<uint8_t>& sessionTranscript) override; |
| 111 | |
| 112 | protected: |
| 113 | // See docs for id_. |
| 114 | // |
| 115 | bool validateId(const string& callerName); |
| 116 | |
| 117 | // We use a singleton libeic object, shared by all proxy instances. This is to |
| 118 | // properly simulate a situation where libeic is used on constrained hardware |
| 119 | // with only enough RAM for a single instance of the libeic object. |
| 120 | // |
| 121 | static EicSession ctx_; |
| 122 | |
| 123 | // On the HAL side we keep track of the ID that was assigned to the libeic object |
| 124 | // created in secure hardware. For every call into libeic we validate that this |
| 125 | // identifier matches what is on the secure side. This is what the validateId() |
| 126 | // method does. |
| 127 | // |
| 128 | uint32_t id_ = 0; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // This implementation uses libEmbeddedIC in-process. |
| 132 | // |
| 133 | class FakeSecureHardwarePresentationProxy : public SecureHardwarePresentationProxy { |
| 134 | public: |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 135 | FakeSecureHardwarePresentationProxy() = default; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 136 | virtual ~FakeSecureHardwarePresentationProxy(); |
| 137 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 138 | bool initialize(uint32_t sessionId, bool testCredential, const string& docType, |
| 139 | const vector<uint8_t>& encryptedCredentialKeys) override; |
| 140 | |
| 141 | bool shutdown() override; |
| 142 | |
| 143 | optional<uint32_t> getId() override; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 144 | |
| 145 | // Returns publicKeyCert (1st component) and signingKeyBlob (2nd component) |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 146 | optional<pair<vector<uint8_t>, vector<uint8_t>>> generateSigningKeyPair(const string& docType, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 147 | time_t now) override; |
| 148 | |
| 149 | // Returns private key |
| 150 | optional<vector<uint8_t>> createEphemeralKeyPair() override; |
| 151 | |
| 152 | optional<uint64_t> createAuthChallenge() override; |
| 153 | |
| 154 | bool startRetrieveEntries() override; |
| 155 | |
| 156 | bool setAuthToken(uint64_t challenge, uint64_t secureUserId, uint64_t authenticatorId, |
| 157 | int hardwareAuthenticatorType, uint64_t timeStamp, const vector<uint8_t>& mac, |
| 158 | uint64_t verificationTokenChallenge, uint64_t verificationTokenTimestamp, |
| 159 | int verificationTokenSecurityLevel, |
| 160 | const vector<uint8_t>& verificationTokenMac) override; |
| 161 | |
| 162 | bool pushReaderCert(const vector<uint8_t>& certX509) override; |
| 163 | |
| 164 | optional<bool> validateAccessControlProfile(int id, const vector<uint8_t>& readerCertificate, |
| 165 | bool userAuthenticationRequired, int timeoutMillis, |
| 166 | uint64_t secureUserId, |
| 167 | const vector<uint8_t>& mac) override; |
| 168 | |
| 169 | bool validateRequestMessage(const vector<uint8_t>& sessionTranscript, |
| 170 | const vector<uint8_t>& requestMessage, int coseSignAlg, |
| 171 | const vector<uint8_t>& readerSignatureOfToBeSigned) override; |
| 172 | |
| 173 | bool calcMacKey(const vector<uint8_t>& sessionTranscript, |
| 174 | const vector<uint8_t>& readerEphemeralPublicKey, |
| 175 | const vector<uint8_t>& signingKeyBlob, const string& docType, |
| 176 | unsigned int numNamespacesWithValues, |
| 177 | size_t expectedProofOfProvisioningSize) override; |
| 178 | |
| 179 | AccessCheckResult startRetrieveEntryValue( |
| 180 | const string& nameSpace, const string& name, unsigned int newNamespaceNumEntries, |
| 181 | int32_t entrySize, const vector<int32_t>& accessControlProfileIds) override; |
| 182 | |
| 183 | optional<vector<uint8_t>> retrieveEntryValue( |
| 184 | const vector<uint8_t>& encryptedContent, const string& nameSpace, const string& name, |
| 185 | const vector<int32_t>& accessControlProfileIds) override; |
| 186 | |
| 187 | optional<vector<uint8_t>> finishRetrieval() override; |
| 188 | |
| 189 | optional<vector<uint8_t>> deleteCredential(const string& docType, |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 190 | const vector<uint8_t>& challenge, |
| 191 | bool includeChallenge, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 192 | size_t proofOfDeletionCborSize) override; |
| 193 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 194 | optional<vector<uint8_t>> proveOwnership(const string& docType, bool testCredential, |
| 195 | const vector<uint8_t>& challenge, |
| 196 | size_t proofOfOwnershipCborSize) override; |
| 197 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 198 | protected: |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 199 | // See docs for id_. |
| 200 | // |
| 201 | bool validateId(const string& callerName); |
| 202 | |
| 203 | // We use a singleton libeic object, shared by all proxy instances. This is to |
| 204 | // properly simulate a situation where libeic is used on constrained hardware |
| 205 | // with only enough RAM for a single instance of the libeic object. |
| 206 | // |
| 207 | static EicPresentation ctx_; |
| 208 | |
| 209 | // On the HAL side we keep track of the ID that was assigned to the libeic object |
| 210 | // created in secure hardware. For every call into libeic we validate that this |
| 211 | // identifier matches what is on the secure side. This is what the validateId() |
| 212 | // method does. |
| 213 | // |
| 214 | uint32_t id_ = 0; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | // Factory implementation. |
| 218 | // |
| 219 | class FakeSecureHardwareProxyFactory : public SecureHardwareProxyFactory { |
| 220 | public: |
| 221 | FakeSecureHardwareProxyFactory() {} |
| 222 | virtual ~FakeSecureHardwareProxyFactory() {} |
| 223 | |
| 224 | sp<SecureHardwareProvisioningProxy> createProvisioningProxy() override { |
| 225 | return new FakeSecureHardwareProvisioningProxy(); |
| 226 | } |
| 227 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 228 | sp<SecureHardwareSessionProxy> createSessionProxy() override { |
| 229 | return new FakeSecureHardwareSessionProxy(); |
| 230 | } |
| 231 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 232 | sp<SecureHardwarePresentationProxy> createPresentationProxy() override { |
| 233 | return new FakeSecureHardwarePresentationProxy(); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | } // namespace android::hardware::identity |
| 238 | |
| 239 | #endif // ANDROID_HARDWARE_IDENTITY_FAKESECUREHARDWAREPROXY_H |