blob: a8bad88f8f3ed66401e94e31f80aa4f065592d3b [file] [log] [blame]
David Zeuthen81603152020-02-11 22:04:24 -05001/*
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>
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
31namespace aidl::android::hardware::identity {
32
33using ::aidl::android::hardware::keymaster::HardwareAuthToken;
34using ::std::map;
David Zeuthen28edb102020-04-28 18:54:55 -040035using ::std::set;
David Zeuthen81603152020-02-11 22:04:24 -050036using ::std::string;
37using ::std::vector;
38
David Zeuthen81603152020-02-11 22:04:24 -050039class IdentityCredential : public BnIdentityCredential {
40 public:
41 IdentityCredential(const vector<uint8_t>& credentialData)
David Zeuthen28edb102020-04-28 18:54:55 -040042 : credentialData_(credentialData),
43 numStartRetrievalCalls_(0),
44 authChallenge_(0),
45 expectedDeviceNameSpacesSize_(0) {}
David Zeuthen81603152020-02-11 22:04:24 -050046
47 // Parses and decrypts credentialData_, return a status code from
48 // IIdentityCredentialStore. Must be called right after construction.
49 int initialize();
50
51 // Methods from IIdentityCredential follow.
Jooyung Han17be89b2020-02-21 21:17:06 +090052 ndk::ScopedAStatus deleteCredential(vector<uint8_t>* outProofOfDeletionSignature) override;
53 ndk::ScopedAStatus createEphemeralKeyPair(vector<uint8_t>* outKeyPair) override;
54 ndk::ScopedAStatus setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override;
David Zeuthen81603152020-02-11 22:04:24 -050055 ndk::ScopedAStatus createAuthChallenge(int64_t* outChallenge) override;
David Zeuthen28edb102020-04-28 18:54:55 -040056 ndk::ScopedAStatus setRequestedNamespaces(
57 const vector<RequestNamespace>& requestNamespaces) override;
David Zeuthen81603152020-02-11 22:04:24 -050058 ndk::ScopedAStatus startRetrieval(
59 const vector<SecureAccessControlProfile>& accessControlProfiles,
Jooyung Han17be89b2020-02-21 21:17:06 +090060 const HardwareAuthToken& authToken, const vector<uint8_t>& itemsRequest,
61 const vector<uint8_t>& signingKeyBlob, const vector<uint8_t>& sessionTranscript,
62 const vector<uint8_t>& readerSignature, const vector<int32_t>& requestCounts) override;
David Zeuthen81603152020-02-11 22:04:24 -050063 ndk::ScopedAStatus startRetrieveEntryValue(
64 const string& nameSpace, const string& name, int32_t entrySize,
65 const vector<int32_t>& accessControlProfileIds) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090066 ndk::ScopedAStatus retrieveEntryValue(const vector<uint8_t>& encryptedContent,
67 vector<uint8_t>* outContent) override;
68 ndk::ScopedAStatus finishRetrieval(vector<uint8_t>* outMac,
69 vector<uint8_t>* outDeviceNameSpaces) override;
70 ndk::ScopedAStatus generateSigningKeyPair(vector<uint8_t>* outSigningKeyBlob,
David Zeuthen81603152020-02-11 22:04:24 -050071 Certificate* outSigningKeyCertificate) override;
72
73 private:
74 // Set by constructor
75 vector<uint8_t> credentialData_;
76 int numStartRetrievalCalls_;
77
78 // Set by initialize()
79 string docType_;
80 bool testCredential_;
81 vector<uint8_t> storageKey_;
82 vector<uint8_t> credentialPrivKey_;
83
84 // Set by createEphemeralKeyPair()
85 vector<uint8_t> ephemeralPublicKey_;
86
87 // Set by setReaderEphemeralPublicKey()
88 vector<uint8_t> readerPublicKey_;
89
90 // Set by createAuthChallenge()
91 uint64_t authChallenge_;
92
David Zeuthen28edb102020-04-28 18:54:55 -040093 // Set by setRequestedNamespaces()
94 vector<RequestNamespace> requestNamespaces_;
95
David Zeuthen81603152020-02-11 22:04:24 -050096 // Set at startRetrieval() time.
97 map<int32_t, int> profileIdToAccessCheckResult_;
David Zeuthene35797f2020-02-27 14:25:54 -050098 vector<uint8_t> signingKeyBlob_;
David Zeuthen81603152020-02-11 22:04:24 -050099 vector<uint8_t> sessionTranscript_;
100 std::unique_ptr<cppbor::Item> sessionTranscriptItem_;
101 vector<uint8_t> itemsRequest_;
102 vector<int32_t> requestCountsRemaining_;
David Zeuthen28edb102020-04-28 18:54:55 -0400103 map<string, set<string>> requestedNameSpacesAndNames_;
David Zeuthen81603152020-02-11 22:04:24 -0500104 cppbor::Map deviceNameSpacesMap_;
105 cppbor::Map currentNameSpaceDeviceNameSpacesMap_;
106
David Zeuthen28edb102020-04-28 18:54:55 -0400107 // Calculated at startRetrieval() time.
108 size_t expectedDeviceNameSpacesSize_;
109
David Zeuthen81603152020-02-11 22:04:24 -0500110 // Set at startRetrieveEntryValue() time.
111 string currentNameSpace_;
112 string currentName_;
113 size_t entryRemainingBytes_;
114 vector<uint8_t> entryValue_;
115 vector<uint8_t> entryAdditionalData_;
David Zeuthen28edb102020-04-28 18:54:55 -0400116
117 size_t calcDeviceNameSpacesSize();
David Zeuthen81603152020-02-11 22:04:24 -0500118};
119
120} // namespace aidl::android::hardware::identity
121
122#endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H