blob: 228182160acb8d368f5fd95974f8081486890191 [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
30#include <cppbor/cppbor.h>
31
David Zeuthen630de2a2020-05-11 14:04:54 -040032#include "IdentityCredentialStore.h"
33#include "SecureHardwareProxy.h"
34
David Zeuthen81603152020-02-11 22:04:24 -050035namespace aidl::android::hardware::identity {
36
37using ::aidl::android::hardware::keymaster::HardwareAuthToken;
David Zeuthena8ed82c2020-05-08 10:03:28 -040038using ::aidl::android::hardware::keymaster::VerificationToken;
David Zeuthen630de2a2020-05-11 14:04:54 -040039using ::android::sp;
40using ::android::hardware::identity::SecureHardwarePresentationProxy;
David Zeuthen81603152020-02-11 22:04:24 -050041using ::std::map;
David Zeuthen28edb102020-04-28 18:54:55 -040042using ::std::set;
David Zeuthen81603152020-02-11 22:04:24 -050043using ::std::string;
44using ::std::vector;
45
David Zeuthen81603152020-02-11 22:04:24 -050046class IdentityCredential : public BnIdentityCredential {
47 public:
David Zeuthen630de2a2020-05-11 14:04:54 -040048 IdentityCredential(sp<SecureHardwarePresentationProxy> hwProxy,
49 const vector<uint8_t>& credentialData)
50 : hwProxy_(hwProxy),
51 credentialData_(credentialData),
David Zeuthen28edb102020-04-28 18:54:55 -040052 numStartRetrievalCalls_(0),
David Zeuthen28edb102020-04-28 18:54:55 -040053 expectedDeviceNameSpacesSize_(0) {}
David Zeuthen81603152020-02-11 22:04:24 -050054
55 // Parses and decrypts credentialData_, return a status code from
56 // IIdentityCredentialStore. Must be called right after construction.
57 int initialize();
58
59 // Methods from IIdentityCredential follow.
Jooyung Han17be89b2020-02-21 21:17:06 +090060 ndk::ScopedAStatus deleteCredential(vector<uint8_t>* outProofOfDeletionSignature) override;
61 ndk::ScopedAStatus createEphemeralKeyPair(vector<uint8_t>* outKeyPair) override;
62 ndk::ScopedAStatus setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override;
David Zeuthen81603152020-02-11 22:04:24 -050063 ndk::ScopedAStatus createAuthChallenge(int64_t* outChallenge) override;
David Zeuthen28edb102020-04-28 18:54:55 -040064 ndk::ScopedAStatus setRequestedNamespaces(
65 const vector<RequestNamespace>& requestNamespaces) override;
David Zeuthena8ed82c2020-05-08 10:03:28 -040066 ndk::ScopedAStatus setVerificationToken(const VerificationToken& verificationToken) override;
David Zeuthen81603152020-02-11 22:04:24 -050067 ndk::ScopedAStatus startRetrieval(
68 const vector<SecureAccessControlProfile>& accessControlProfiles,
Jooyung Han17be89b2020-02-21 21:17:06 +090069 const HardwareAuthToken& authToken, const vector<uint8_t>& itemsRequest,
70 const vector<uint8_t>& signingKeyBlob, const vector<uint8_t>& sessionTranscript,
71 const vector<uint8_t>& readerSignature, const vector<int32_t>& requestCounts) override;
David Zeuthen81603152020-02-11 22:04:24 -050072 ndk::ScopedAStatus startRetrieveEntryValue(
73 const string& nameSpace, const string& name, int32_t entrySize,
74 const vector<int32_t>& accessControlProfileIds) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090075 ndk::ScopedAStatus retrieveEntryValue(const vector<uint8_t>& encryptedContent,
76 vector<uint8_t>* outContent) override;
77 ndk::ScopedAStatus finishRetrieval(vector<uint8_t>* outMac,
78 vector<uint8_t>* outDeviceNameSpaces) override;
79 ndk::ScopedAStatus generateSigningKeyPair(vector<uint8_t>* outSigningKeyBlob,
David Zeuthen81603152020-02-11 22:04:24 -050080 Certificate* outSigningKeyCertificate) override;
81
82 private:
83 // Set by constructor
David Zeuthen630de2a2020-05-11 14:04:54 -040084 sp<SecureHardwarePresentationProxy> hwProxy_;
David Zeuthen81603152020-02-11 22:04:24 -050085 vector<uint8_t> credentialData_;
86 int numStartRetrievalCalls_;
87
88 // Set by initialize()
89 string docType_;
90 bool testCredential_;
David Zeuthen81603152020-02-11 22:04:24 -050091
92 // Set by createEphemeralKeyPair()
93 vector<uint8_t> ephemeralPublicKey_;
94
95 // Set by setReaderEphemeralPublicKey()
96 vector<uint8_t> readerPublicKey_;
97
David Zeuthen28edb102020-04-28 18:54:55 -040098 // Set by setRequestedNamespaces()
99 vector<RequestNamespace> requestNamespaces_;
100
David Zeuthena8ed82c2020-05-08 10:03:28 -0400101 // Set by setVerificationToken().
102 VerificationToken verificationToken_;
103
David Zeuthen81603152020-02-11 22:04:24 -0500104 // Set at startRetrieval() time.
David Zeuthene35797f2020-02-27 14:25:54 -0500105 vector<uint8_t> signingKeyBlob_;
David Zeuthen81603152020-02-11 22:04:24 -0500106 vector<uint8_t> sessionTranscript_;
David Zeuthen81603152020-02-11 22:04:24 -0500107 vector<uint8_t> itemsRequest_;
108 vector<int32_t> requestCountsRemaining_;
David Zeuthen28edb102020-04-28 18:54:55 -0400109 map<string, set<string>> requestedNameSpacesAndNames_;
David Zeuthen81603152020-02-11 22:04:24 -0500110 cppbor::Map deviceNameSpacesMap_;
111 cppbor::Map currentNameSpaceDeviceNameSpacesMap_;
112
David Zeuthen28edb102020-04-28 18:54:55 -0400113 // Calculated at startRetrieval() time.
114 size_t expectedDeviceNameSpacesSize_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400115 vector<unsigned int> expectedNumEntriesPerNamespace_;
David Zeuthen28edb102020-04-28 18:54:55 -0400116
David Zeuthen81603152020-02-11 22:04:24 -0500117 // Set at startRetrieveEntryValue() time.
118 string currentNameSpace_;
119 string currentName_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400120 vector<int32_t> currentAccessControlProfileIds_;
David Zeuthen81603152020-02-11 22:04:24 -0500121 size_t entryRemainingBytes_;
122 vector<uint8_t> entryValue_;
David Zeuthen28edb102020-04-28 18:54:55 -0400123
David Zeuthen630de2a2020-05-11 14:04:54 -0400124 void calcDeviceNameSpacesSize(uint32_t accessControlProfileMask);
David Zeuthen81603152020-02-11 22:04:24 -0500125};
126
127} // namespace aidl::android::hardware::identity
128
129#endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H