blob: df98c7a1212017842d3fef4e5df6f67eefbb893b [file] [log] [blame]
David Zeuthen630de2a2020-05-11 14:04:54 -04001/*
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
24namespace android::hardware::identity {
25
26// This implementation uses libEmbeddedIC in-process.
27//
28class FakeSecureHardwareProvisioningProxy : public SecureHardwareProvisioningProxy {
29 public:
David Zeuthen1eb12b22021-09-11 13:59:43 -040030 FakeSecureHardwareProvisioningProxy() = default;
David Zeuthen630de2a2020-05-11 14:04:54 -040031 virtual ~FakeSecureHardwareProvisioningProxy();
32
33 bool initialize(bool testCredential) override;
34
David Zeuthen1eb12b22021-09-11 13:59:43 -040035 bool initializeForUpdate(bool testCredential, const string& docType,
36 const vector<uint8_t>& encryptedCredentialKeys) override;
David Zeuthen49f2d252020-10-16 11:27:24 -040037
David Zeuthen630de2a2020-05-11 14:04:54 -040038 bool shutdown() override;
39
David Zeuthen1eb12b22021-09-11 13:59:43 -040040 optional<uint32_t> getId() override;
41
David Zeuthen630de2a2020-05-11 14:04:54 -040042 // Returns public key certificate.
43 optional<vector<uint8_t>> createCredentialKey(const vector<uint8_t>& challenge,
44 const vector<uint8_t>& applicationId) override;
45
David Zeuthen1eb12b22021-09-11 13:59:43 -040046 bool startPersonalization(int accessControlProfileCount, const vector<int>& entryCounts,
David Zeuthen630de2a2020-05-11 14:04:54 -040047 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 Zeuthen1eb12b22021-09-11 13:59:43 -040072 // 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//
92class 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 Zeuthen630de2a2020-05-11 14:04:54 -0400129};
130
131// This implementation uses libEmbeddedIC in-process.
132//
133class FakeSecureHardwarePresentationProxy : public SecureHardwarePresentationProxy {
134 public:
David Zeuthen1eb12b22021-09-11 13:59:43 -0400135 FakeSecureHardwarePresentationProxy() = default;
David Zeuthen630de2a2020-05-11 14:04:54 -0400136 virtual ~FakeSecureHardwarePresentationProxy();
137
David Zeuthen1eb12b22021-09-11 13:59:43 -0400138 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 Zeuthen630de2a2020-05-11 14:04:54 -0400144
145 // Returns publicKeyCert (1st component) and signingKeyBlob (2nd component)
David Zeuthen1eb12b22021-09-11 13:59:43 -0400146 optional<pair<vector<uint8_t>, vector<uint8_t>>> generateSigningKeyPair(const string& docType,
David Zeuthen630de2a2020-05-11 14:04:54 -0400147 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 Zeuthen49f2d252020-10-16 11:27:24 -0400190 const vector<uint8_t>& challenge,
191 bool includeChallenge,
David Zeuthen630de2a2020-05-11 14:04:54 -0400192 size_t proofOfDeletionCborSize) override;
193
David Zeuthen49f2d252020-10-16 11:27:24 -0400194 optional<vector<uint8_t>> proveOwnership(const string& docType, bool testCredential,
195 const vector<uint8_t>& challenge,
196 size_t proofOfOwnershipCborSize) override;
197
David Zeuthen630de2a2020-05-11 14:04:54 -0400198 protected:
David Zeuthen1eb12b22021-09-11 13:59:43 -0400199 // 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 Zeuthen630de2a2020-05-11 14:04:54 -0400215};
216
217// Factory implementation.
218//
219class FakeSecureHardwareProxyFactory : public SecureHardwareProxyFactory {
220 public:
221 FakeSecureHardwareProxyFactory() {}
222 virtual ~FakeSecureHardwareProxyFactory() {}
223
224 sp<SecureHardwareProvisioningProxy> createProvisioningProxy() override {
225 return new FakeSecureHardwareProvisioningProxy();
226 }
227
David Zeuthen1eb12b22021-09-11 13:59:43 -0400228 sp<SecureHardwareSessionProxy> createSessionProxy() override {
229 return new FakeSecureHardwareSessionProxy();
230 }
231
David Zeuthen630de2a2020-05-11 14:04:54 -0400232 sp<SecureHardwarePresentationProxy> createPresentationProxy() override {
233 return new FakeSecureHardwarePresentationProxy();
234 }
235};
236
237} // namespace android::hardware::identity
238
239#endif // ANDROID_HARDWARE_IDENTITY_FAKESECUREHARDWAREPROXY_H