blob: 2935fb80a7a5aa10c15f2f1e048c687294cfe7a3 [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>
David Zeuthena8ed82c2020-05-08 10:03:28 -040022#include <aidl/android/hardware/keymaster/VerificationToken.h>
David Zeuthen81603152020-02-11 22:04:24 -050023#include <android/hardware/identity/support/IdentityCredentialSupport.h>
24
25#include <map>
26#include <set>
27#include <string>
28#include <vector>
29
Max Biresa3c7f4c2021-04-09 08:56:40 -070030#include <cppbor.h>
David Zeuthen81603152020-02-11 22:04:24 -050031
David Zeuthen630de2a2020-05-11 14:04:54 -040032#include "IdentityCredentialStore.h"
David Zeuthen1eb12b22021-09-11 13:59:43 -040033#include "PresentationSession.h"
David Zeuthen630de2a2020-05-11 14:04:54 -040034#include "SecureHardwareProxy.h"
35
David Zeuthen81603152020-02-11 22:04:24 -050036namespace aidl::android::hardware::identity {
37
38using ::aidl::android::hardware::keymaster::HardwareAuthToken;
David Zeuthena8ed82c2020-05-08 10:03:28 -040039using ::aidl::android::hardware::keymaster::VerificationToken;
David Zeuthen630de2a2020-05-11 14:04:54 -040040using ::android::sp;
41using ::android::hardware::identity::SecureHardwarePresentationProxy;
David Zeuthen81603152020-02-11 22:04:24 -050042using ::std::map;
David Zeuthen28edb102020-04-28 18:54:55 -040043using ::std::set;
David Zeuthen81603152020-02-11 22:04:24 -050044using ::std::string;
45using ::std::vector;
46
David Zeuthen81603152020-02-11 22:04:24 -050047class IdentityCredential : public BnIdentityCredential {
48 public:
David Zeuthen49f2d252020-10-16 11:27:24 -040049 IdentityCredential(sp<SecureHardwareProxyFactory> hwProxyFactory,
David Zeuthen1eb12b22021-09-11 13:59:43 -040050 const vector<uint8_t>& credentialData,
51 std::shared_ptr<PresentationSession> session)
David Zeuthen49f2d252020-10-16 11:27:24 -040052 : hwProxyFactory_(hwProxyFactory),
David Zeuthen630de2a2020-05-11 14:04:54 -040053 credentialData_(credentialData),
David Zeuthen1eb12b22021-09-11 13:59:43 -040054 session_(std::move(session)),
David Zeuthen28edb102020-04-28 18:54:55 -040055 numStartRetrievalCalls_(0),
David Zeuthen28edb102020-04-28 18:54:55 -040056 expectedDeviceNameSpacesSize_(0) {}
David Zeuthen81603152020-02-11 22:04:24 -050057
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 Han17be89b2020-02-21 21:17:06 +090063 ndk::ScopedAStatus deleteCredential(vector<uint8_t>* outProofOfDeletionSignature) override;
David Zeuthen49f2d252020-10-16 11:27:24 -040064 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 Han17be89b2020-02-21 21:17:06 +090069 ndk::ScopedAStatus createEphemeralKeyPair(vector<uint8_t>* outKeyPair) override;
70 ndk::ScopedAStatus setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override;
David Zeuthen81603152020-02-11 22:04:24 -050071 ndk::ScopedAStatus createAuthChallenge(int64_t* outChallenge) override;
David Zeuthen28edb102020-04-28 18:54:55 -040072 ndk::ScopedAStatus setRequestedNamespaces(
73 const vector<RequestNamespace>& requestNamespaces) override;
David Zeuthena8ed82c2020-05-08 10:03:28 -040074 ndk::ScopedAStatus setVerificationToken(const VerificationToken& verificationToken) override;
David Zeuthen81603152020-02-11 22:04:24 -050075 ndk::ScopedAStatus startRetrieval(
76 const vector<SecureAccessControlProfile>& accessControlProfiles,
Jooyung Han17be89b2020-02-21 21:17:06 +090077 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 Zeuthen81603152020-02-11 22:04:24 -050080 ndk::ScopedAStatus startRetrieveEntryValue(
81 const string& nameSpace, const string& name, int32_t entrySize,
82 const vector<int32_t>& accessControlProfileIds) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090083 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 Zeuthen81603152020-02-11 22:04:24 -050088 Certificate* outSigningKeyCertificate) override;
89
David Zeuthen49f2d252020-10-16 11:27:24 -040090 ndk::ScopedAStatus updateCredential(
91 shared_ptr<IWritableIdentityCredential>* outWritableCredential) override;
92
David Zeuthen81603152020-02-11 22:04:24 -050093 private:
David Zeuthen49f2d252020-10-16 11:27:24 -040094 ndk::ScopedAStatus deleteCredentialCommon(const vector<uint8_t>& challenge,
95 bool includeChallenge,
96 vector<uint8_t>* outProofOfDeletionSignature);
97
David Zeuthen1eb12b22021-09-11 13:59:43 -040098 // Creates and initializes hwProxy_.
99 ndk::ScopedAStatus ensureHwProxy();
100
David Zeuthen81603152020-02-11 22:04:24 -0500101 // Set by constructor
David Zeuthen49f2d252020-10-16 11:27:24 -0400102 sp<SecureHardwareProxyFactory> hwProxyFactory_;
David Zeuthen81603152020-02-11 22:04:24 -0500103 vector<uint8_t> credentialData_;
David Zeuthen1eb12b22021-09-11 13:59:43 -0400104 shared_ptr<PresentationSession> session_;
David Zeuthen81603152020-02-11 22:04:24 -0500105 int numStartRetrievalCalls_;
106
107 // Set by initialize()
108 string docType_;
109 bool testCredential_;
David Zeuthen49f2d252020-10-16 11:27:24 -0400110 vector<uint8_t> encryptedCredentialKeys_;
David Zeuthen81603152020-02-11 22:04:24 -0500111
David Zeuthen1eb12b22021-09-11 13:59:43 -0400112 // Set by ensureHwProxy()
113 sp<SecureHardwarePresentationProxy> hwProxy_;
114
David Zeuthen81603152020-02-11 22:04:24 -0500115 // Set by createEphemeralKeyPair()
116 vector<uint8_t> ephemeralPublicKey_;
117
118 // Set by setReaderEphemeralPublicKey()
119 vector<uint8_t> readerPublicKey_;
120
David Zeuthen28edb102020-04-28 18:54:55 -0400121 // Set by setRequestedNamespaces()
122 vector<RequestNamespace> requestNamespaces_;
123
David Zeuthena8ed82c2020-05-08 10:03:28 -0400124 // Set by setVerificationToken().
125 VerificationToken verificationToken_;
126
David Zeuthen81603152020-02-11 22:04:24 -0500127 // Set at startRetrieval() time.
David Zeuthene35797f2020-02-27 14:25:54 -0500128 vector<uint8_t> signingKeyBlob_;
David Zeuthen81603152020-02-11 22:04:24 -0500129 vector<uint8_t> sessionTranscript_;
David Zeuthen81603152020-02-11 22:04:24 -0500130 vector<uint8_t> itemsRequest_;
131 vector<int32_t> requestCountsRemaining_;
David Zeuthen28edb102020-04-28 18:54:55 -0400132 map<string, set<string>> requestedNameSpacesAndNames_;
David Zeuthen81603152020-02-11 22:04:24 -0500133 cppbor::Map deviceNameSpacesMap_;
134 cppbor::Map currentNameSpaceDeviceNameSpacesMap_;
135
David Zeuthen28edb102020-04-28 18:54:55 -0400136 // Calculated at startRetrieval() time.
137 size_t expectedDeviceNameSpacesSize_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400138 vector<unsigned int> expectedNumEntriesPerNamespace_;
David Zeuthen28edb102020-04-28 18:54:55 -0400139
David Zeuthen81603152020-02-11 22:04:24 -0500140 // Set at startRetrieveEntryValue() time.
141 string currentNameSpace_;
142 string currentName_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400143 vector<int32_t> currentAccessControlProfileIds_;
David Zeuthen81603152020-02-11 22:04:24 -0500144 size_t entryRemainingBytes_;
145 vector<uint8_t> entryValue_;
David Zeuthen28edb102020-04-28 18:54:55 -0400146
David Zeuthen630de2a2020-05-11 14:04:54 -0400147 void calcDeviceNameSpacesSize(uint32_t accessControlProfileMask);
David Zeuthen81603152020-02-11 22:04:24 -0500148};
149
150} // namespace aidl::android::hardware::identity
151
152#endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H