David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019, 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_IDENTITYCREDENTIAL_H |
| 18 | #define ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H |
| 19 | |
| 20 | #include <aidl/android/hardware/identity/BnIdentityCredential.h> |
| 21 | #include <aidl/android/hardware/keymaster/HardwareAuthToken.h> |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 22 | #include <aidl/android/hardware/keymaster/VerificationToken.h> |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 23 | #include <android/hardware/identity/support/IdentityCredentialSupport.h> |
| 24 | |
| 25 | #include <map> |
| 26 | #include <set> |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
Max Bires | a3c7f4c | 2021-04-09 08:56:40 -0700 | [diff] [blame] | 30 | #include <cppbor.h> |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 31 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 32 | #include "IdentityCredentialStore.h" |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 33 | #include "PresentationSession.h" |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 34 | #include "SecureHardwareProxy.h" |
| 35 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 36 | namespace aidl::android::hardware::identity { |
| 37 | |
| 38 | using ::aidl::android::hardware::keymaster::HardwareAuthToken; |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 39 | using ::aidl::android::hardware::keymaster::VerificationToken; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 40 | using ::android::sp; |
| 41 | using ::android::hardware::identity::SecureHardwarePresentationProxy; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 42 | using ::std::map; |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 43 | using ::std::set; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 44 | using ::std::string; |
| 45 | using ::std::vector; |
| 46 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 47 | class IdentityCredential : public BnIdentityCredential { |
| 48 | public: |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 49 | IdentityCredential(sp<SecureHardwareProxyFactory> hwProxyFactory, |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 50 | const vector<uint8_t>& credentialData, |
| 51 | std::shared_ptr<PresentationSession> session) |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 52 | : hwProxyFactory_(hwProxyFactory), |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 53 | credentialData_(credentialData), |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 54 | session_(std::move(session)), |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 55 | numStartRetrievalCalls_(0), |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 56 | expectedDeviceNameSpacesSize_(0) {} |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 57 | |
| 58 | // Parses and decrypts credentialData_, return a status code from |
| 59 | // IIdentityCredentialStore. Must be called right after construction. |
| 60 | int initialize(); |
| 61 | |
| 62 | // Methods from IIdentityCredential follow. |
Jooyung Han | 17be89b | 2020-02-21 21:17:06 +0900 | [diff] [blame] | 63 | ndk::ScopedAStatus deleteCredential(vector<uint8_t>* outProofOfDeletionSignature) override; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 64 | ndk::ScopedAStatus deleteCredentialWithChallenge( |
| 65 | const vector<uint8_t>& challenge, |
| 66 | vector<uint8_t>* outProofOfDeletionSignature) override; |
| 67 | ndk::ScopedAStatus proveOwnership(const vector<uint8_t>& challenge, |
| 68 | vector<uint8_t>* outProofOfOwnershipSignature) override; |
Jooyung Han | 17be89b | 2020-02-21 21:17:06 +0900 | [diff] [blame] | 69 | ndk::ScopedAStatus createEphemeralKeyPair(vector<uint8_t>* outKeyPair) override; |
| 70 | ndk::ScopedAStatus setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 71 | ndk::ScopedAStatus createAuthChallenge(int64_t* outChallenge) override; |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 72 | ndk::ScopedAStatus setRequestedNamespaces( |
| 73 | const vector<RequestNamespace>& requestNamespaces) override; |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 74 | ndk::ScopedAStatus setVerificationToken(const VerificationToken& verificationToken) override; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 75 | ndk::ScopedAStatus startRetrieval( |
| 76 | const vector<SecureAccessControlProfile>& accessControlProfiles, |
Jooyung Han | 17be89b | 2020-02-21 21:17:06 +0900 | [diff] [blame] | 77 | const HardwareAuthToken& authToken, const vector<uint8_t>& itemsRequest, |
| 78 | const vector<uint8_t>& signingKeyBlob, const vector<uint8_t>& sessionTranscript, |
| 79 | const vector<uint8_t>& readerSignature, const vector<int32_t>& requestCounts) override; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 80 | ndk::ScopedAStatus startRetrieveEntryValue( |
| 81 | const string& nameSpace, const string& name, int32_t entrySize, |
| 82 | const vector<int32_t>& accessControlProfileIds) override; |
Jooyung Han | 17be89b | 2020-02-21 21:17:06 +0900 | [diff] [blame] | 83 | ndk::ScopedAStatus retrieveEntryValue(const vector<uint8_t>& encryptedContent, |
| 84 | vector<uint8_t>* outContent) override; |
| 85 | ndk::ScopedAStatus finishRetrieval(vector<uint8_t>* outMac, |
| 86 | vector<uint8_t>* outDeviceNameSpaces) override; |
| 87 | ndk::ScopedAStatus generateSigningKeyPair(vector<uint8_t>* outSigningKeyBlob, |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 88 | Certificate* outSigningKeyCertificate) override; |
| 89 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 90 | ndk::ScopedAStatus updateCredential( |
| 91 | shared_ptr<IWritableIdentityCredential>* outWritableCredential) override; |
| 92 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 93 | private: |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 94 | ndk::ScopedAStatus deleteCredentialCommon(const vector<uint8_t>& challenge, |
| 95 | bool includeChallenge, |
| 96 | vector<uint8_t>* outProofOfDeletionSignature); |
| 97 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 98 | // Creates and initializes hwProxy_. |
| 99 | ndk::ScopedAStatus ensureHwProxy(); |
| 100 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 101 | // Set by constructor |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 102 | sp<SecureHardwareProxyFactory> hwProxyFactory_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 103 | vector<uint8_t> credentialData_; |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 104 | shared_ptr<PresentationSession> session_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 105 | int numStartRetrievalCalls_; |
| 106 | |
| 107 | // Set by initialize() |
| 108 | string docType_; |
| 109 | bool testCredential_; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 110 | vector<uint8_t> encryptedCredentialKeys_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 111 | |
David Zeuthen | 1eb12b2 | 2021-09-11 13:59:43 -0400 | [diff] [blame] | 112 | // Set by ensureHwProxy() |
| 113 | sp<SecureHardwarePresentationProxy> hwProxy_; |
| 114 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 115 | // Set by createEphemeralKeyPair() |
| 116 | vector<uint8_t> ephemeralPublicKey_; |
| 117 | |
| 118 | // Set by setReaderEphemeralPublicKey() |
| 119 | vector<uint8_t> readerPublicKey_; |
| 120 | |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 121 | // Set by setRequestedNamespaces() |
| 122 | vector<RequestNamespace> requestNamespaces_; |
| 123 | |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 124 | // Set by setVerificationToken(). |
| 125 | VerificationToken verificationToken_; |
| 126 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 127 | // Set at startRetrieval() time. |
David Zeuthen | e35797f | 2020-02-27 14:25:54 -0500 | [diff] [blame] | 128 | vector<uint8_t> signingKeyBlob_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 129 | vector<uint8_t> sessionTranscript_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 130 | vector<uint8_t> itemsRequest_; |
| 131 | vector<int32_t> requestCountsRemaining_; |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 132 | map<string, set<string>> requestedNameSpacesAndNames_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 133 | cppbor::Map deviceNameSpacesMap_; |
| 134 | cppbor::Map currentNameSpaceDeviceNameSpacesMap_; |
| 135 | |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 136 | // Calculated at startRetrieval() time. |
| 137 | size_t expectedDeviceNameSpacesSize_; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 138 | vector<unsigned int> expectedNumEntriesPerNamespace_; |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 139 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 140 | // Set at startRetrieveEntryValue() time. |
| 141 | string currentNameSpace_; |
| 142 | string currentName_; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 143 | vector<int32_t> currentAccessControlProfileIds_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 144 | size_t entryRemainingBytes_; |
| 145 | vector<uint8_t> entryValue_; |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 146 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 147 | void calcDeviceNameSpacesSize(uint32_t accessControlProfileMask); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace aidl::android::hardware::identity |
| 151 | |
| 152 | #endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H |