David Zeuthen | c75ac31 | 2019-10-28 13:16:45 -0400 | [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 <android/hardware/identity/1.0/IIdentityCredential.h> |
| 21 | |
| 22 | #include <android/hardware/identity/support/IdentityCredentialSupport.h> |
| 23 | |
| 24 | #include <map> |
| 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <cppbor/cppbor.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace identity { |
| 34 | namespace implementation { |
| 35 | |
| 36 | using ::std::map; |
| 37 | using ::std::string; |
| 38 | using ::std::vector; |
| 39 | |
| 40 | using ::android::hardware::hidl_string; |
| 41 | using ::android::hardware::hidl_vec; |
| 42 | using ::android::hardware::Return; |
| 43 | using ::android::hardware::Void; |
| 44 | using ::android::hardware::identity::V1_0::IIdentityCredential; |
| 45 | using ::android::hardware::identity::V1_0::Result; |
| 46 | using ::android::hardware::identity::V1_0::ResultCode; |
| 47 | using ::android::hardware::identity::V1_0::SecureAccessControlProfile; |
| 48 | using ::android::hardware::keymaster::V4_0::HardwareAuthToken; |
| 49 | |
| 50 | using MapStringToVectorOfStrings = map<string, vector<string>>; |
| 51 | |
| 52 | class IdentityCredential : public IIdentityCredential { |
| 53 | public: |
| 54 | IdentityCredential(const hidl_vec<uint8_t>& credentialData) |
| 55 | : credentialData_(credentialData), numStartRetrievalCalls_(0), authChallenge_(0) {} |
| 56 | |
| 57 | // Parses and decrypts credentialData_, return false on failure. Must be |
| 58 | // called right after construction. |
| 59 | ResultCode initialize(); |
| 60 | |
| 61 | // Methods from ::android::hardware::identity::IIdentityCredential follow. |
| 62 | |
| 63 | Return<void> deleteCredential(deleteCredential_cb _hidl_cb) override; |
| 64 | Return<void> createEphemeralKeyPair(createEphemeralKeyPair_cb _hidl_cb) override; |
| 65 | |
| 66 | Return<void> setReaderEphemeralPublicKey(const hidl_vec<uint8_t>& publicKey, |
| 67 | setReaderEphemeralPublicKey_cb _hidl_cb) override; |
| 68 | |
| 69 | Return<void> createAuthChallenge(createAuthChallenge_cb _hidl_cb) override; |
| 70 | |
| 71 | Return<void> startRetrieval(const hidl_vec<SecureAccessControlProfile>& accessControlProfiles, |
| 72 | const HardwareAuthToken& authToken, |
| 73 | const hidl_vec<uint8_t>& itemsRequest, |
| 74 | const hidl_vec<uint8_t>& sessionTranscript, |
| 75 | const hidl_vec<uint8_t>& readerSignature, |
| 76 | const hidl_vec<uint16_t>& requestCounts, |
| 77 | startRetrieval_cb _hidl_cb) override; |
| 78 | Return<void> startRetrieveEntryValue(const hidl_string& nameSpace, const hidl_string& name, |
| 79 | uint32_t entrySize, |
| 80 | const hidl_vec<uint16_t>& accessControlProfileIds, |
| 81 | startRetrieveEntryValue_cb _hidl_cb) override; |
| 82 | Return<void> retrieveEntryValue(const hidl_vec<uint8_t>& encryptedContent, |
| 83 | retrieveEntryValue_cb _hidl_cb) override; |
| 84 | Return<void> finishRetrieval(const hidl_vec<uint8_t>& signingKeyBlob, |
| 85 | finishRetrieval_cb _hidl_cb) override; |
| 86 | |
| 87 | Return<void> generateSigningKeyPair(generateSigningKeyPair_cb _hidl_cb) override; |
| 88 | |
| 89 | private: |
| 90 | // Set by constructor |
| 91 | vector<uint8_t> credentialData_; |
| 92 | int numStartRetrievalCalls_; |
| 93 | |
| 94 | // Set by initialize() |
| 95 | string docType_; |
| 96 | bool testCredential_; |
| 97 | vector<uint8_t> storageKey_; |
| 98 | vector<uint8_t> credentialPrivKey_; |
| 99 | |
| 100 | // Set by createEphemeralKeyPair() |
| 101 | vector<uint8_t> ephemeralPublicKey_; |
| 102 | |
| 103 | // Set by setReaderEphemeralPublicKey() |
| 104 | vector<uint8_t> readerPublicKey_; |
| 105 | |
| 106 | // Set by createAuthChallenge() |
| 107 | uint64_t authChallenge_; |
| 108 | |
| 109 | // Set at startRetrieval() time. |
| 110 | map<uint16_t, ResultCode> profileIdToAccessCheckResult_; |
| 111 | vector<uint8_t> sessionTranscript_; |
| 112 | std::unique_ptr<cppbor::Item> sessionTranscriptItem_; |
| 113 | vector<uint8_t> itemsRequest_; |
| 114 | vector<uint16_t> requestCountsRemaining_; |
| 115 | MapStringToVectorOfStrings requestedNameSpacesAndNames_; |
| 116 | cppbor::Map deviceNameSpacesMap_; |
| 117 | cppbor::Map currentNameSpaceDeviceNameSpacesMap_; |
| 118 | |
| 119 | // Set at startRetrieveEntryValue() time. |
| 120 | string currentNameSpace_; |
| 121 | string currentName_; |
| 122 | size_t entryRemainingBytes_; |
| 123 | vector<uint8_t> entryValue_; |
| 124 | vector<uint8_t> entryAdditionalData_; |
| 125 | }; |
| 126 | |
| 127 | } // namespace implementation |
| 128 | } // namespace identity |
| 129 | } // namespace hardware |
| 130 | } // namespace android |
| 131 | |
| 132 | #endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H |