blob: 9913b86869c37154f2ee5ffab78e4025ff06d1fa [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 Zeuthen49f2d252020-10-16 11:27:24 -040048 IdentityCredential(sp<SecureHardwareProxyFactory> hwProxyFactory,
49 sp<SecureHardwarePresentationProxy> hwProxy,
David Zeuthen630de2a2020-05-11 14:04:54 -040050 const vector<uint8_t>& credentialData)
David Zeuthen49f2d252020-10-16 11:27:24 -040051 : hwProxyFactory_(hwProxyFactory),
52 hwProxy_(hwProxy),
David Zeuthen630de2a2020-05-11 14:04:54 -040053 credentialData_(credentialData),
David Zeuthen28edb102020-04-28 18:54:55 -040054 numStartRetrievalCalls_(0),
David Zeuthen28edb102020-04-28 18:54:55 -040055 expectedDeviceNameSpacesSize_(0) {}
David Zeuthen81603152020-02-11 22:04:24 -050056
57 // Parses and decrypts credentialData_, return a status code from
58 // IIdentityCredentialStore. Must be called right after construction.
59 int initialize();
60
61 // Methods from IIdentityCredential follow.
Jooyung Han17be89b2020-02-21 21:17:06 +090062 ndk::ScopedAStatus deleteCredential(vector<uint8_t>* outProofOfDeletionSignature) override;
David Zeuthen49f2d252020-10-16 11:27:24 -040063 ndk::ScopedAStatus deleteCredentialWithChallenge(
64 const vector<uint8_t>& challenge,
65 vector<uint8_t>* outProofOfDeletionSignature) override;
66 ndk::ScopedAStatus proveOwnership(const vector<uint8_t>& challenge,
67 vector<uint8_t>* outProofOfOwnershipSignature) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090068 ndk::ScopedAStatus createEphemeralKeyPair(vector<uint8_t>* outKeyPair) override;
69 ndk::ScopedAStatus setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override;
David Zeuthen81603152020-02-11 22:04:24 -050070 ndk::ScopedAStatus createAuthChallenge(int64_t* outChallenge) override;
David Zeuthen28edb102020-04-28 18:54:55 -040071 ndk::ScopedAStatus setRequestedNamespaces(
72 const vector<RequestNamespace>& requestNamespaces) override;
David Zeuthena8ed82c2020-05-08 10:03:28 -040073 ndk::ScopedAStatus setVerificationToken(const VerificationToken& verificationToken) override;
David Zeuthen81603152020-02-11 22:04:24 -050074 ndk::ScopedAStatus startRetrieval(
75 const vector<SecureAccessControlProfile>& accessControlProfiles,
Jooyung Han17be89b2020-02-21 21:17:06 +090076 const HardwareAuthToken& authToken, const vector<uint8_t>& itemsRequest,
77 const vector<uint8_t>& signingKeyBlob, const vector<uint8_t>& sessionTranscript,
78 const vector<uint8_t>& readerSignature, const vector<int32_t>& requestCounts) override;
David Zeuthen81603152020-02-11 22:04:24 -050079 ndk::ScopedAStatus startRetrieveEntryValue(
80 const string& nameSpace, const string& name, int32_t entrySize,
81 const vector<int32_t>& accessControlProfileIds) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090082 ndk::ScopedAStatus retrieveEntryValue(const vector<uint8_t>& encryptedContent,
83 vector<uint8_t>* outContent) override;
84 ndk::ScopedAStatus finishRetrieval(vector<uint8_t>* outMac,
85 vector<uint8_t>* outDeviceNameSpaces) override;
86 ndk::ScopedAStatus generateSigningKeyPair(vector<uint8_t>* outSigningKeyBlob,
David Zeuthen81603152020-02-11 22:04:24 -050087 Certificate* outSigningKeyCertificate) override;
88
David Zeuthen49f2d252020-10-16 11:27:24 -040089 ndk::ScopedAStatus updateCredential(
90 shared_ptr<IWritableIdentityCredential>* outWritableCredential) override;
91
David Zeuthen81603152020-02-11 22:04:24 -050092 private:
David Zeuthen49f2d252020-10-16 11:27:24 -040093 ndk::ScopedAStatus deleteCredentialCommon(const vector<uint8_t>& challenge,
94 bool includeChallenge,
95 vector<uint8_t>* outProofOfDeletionSignature);
96
David Zeuthen81603152020-02-11 22:04:24 -050097 // Set by constructor
David Zeuthen49f2d252020-10-16 11:27:24 -040098 sp<SecureHardwareProxyFactory> hwProxyFactory_;
David Zeuthen630de2a2020-05-11 14:04:54 -040099 sp<SecureHardwarePresentationProxy> hwProxy_;
David Zeuthen81603152020-02-11 22:04:24 -0500100 vector<uint8_t> credentialData_;
101 int numStartRetrievalCalls_;
102
103 // Set by initialize()
104 string docType_;
105 bool testCredential_;
David Zeuthen49f2d252020-10-16 11:27:24 -0400106 vector<uint8_t> encryptedCredentialKeys_;
David Zeuthen81603152020-02-11 22:04:24 -0500107
108 // Set by createEphemeralKeyPair()
109 vector<uint8_t> ephemeralPublicKey_;
110
111 // Set by setReaderEphemeralPublicKey()
112 vector<uint8_t> readerPublicKey_;
113
David Zeuthen28edb102020-04-28 18:54:55 -0400114 // Set by setRequestedNamespaces()
115 vector<RequestNamespace> requestNamespaces_;
116
David Zeuthena8ed82c2020-05-08 10:03:28 -0400117 // Set by setVerificationToken().
118 VerificationToken verificationToken_;
119
David Zeuthen81603152020-02-11 22:04:24 -0500120 // Set at startRetrieval() time.
David Zeuthene35797f2020-02-27 14:25:54 -0500121 vector<uint8_t> signingKeyBlob_;
David Zeuthen81603152020-02-11 22:04:24 -0500122 vector<uint8_t> sessionTranscript_;
David Zeuthen81603152020-02-11 22:04:24 -0500123 vector<uint8_t> itemsRequest_;
124 vector<int32_t> requestCountsRemaining_;
David Zeuthen28edb102020-04-28 18:54:55 -0400125 map<string, set<string>> requestedNameSpacesAndNames_;
David Zeuthen81603152020-02-11 22:04:24 -0500126 cppbor::Map deviceNameSpacesMap_;
127 cppbor::Map currentNameSpaceDeviceNameSpacesMap_;
128
David Zeuthen28edb102020-04-28 18:54:55 -0400129 // Calculated at startRetrieval() time.
130 size_t expectedDeviceNameSpacesSize_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400131 vector<unsigned int> expectedNumEntriesPerNamespace_;
David Zeuthen28edb102020-04-28 18:54:55 -0400132
David Zeuthen81603152020-02-11 22:04:24 -0500133 // Set at startRetrieveEntryValue() time.
134 string currentNameSpace_;
135 string currentName_;
David Zeuthen630de2a2020-05-11 14:04:54 -0400136 vector<int32_t> currentAccessControlProfileIds_;
David Zeuthen81603152020-02-11 22:04:24 -0500137 size_t entryRemainingBytes_;
138 vector<uint8_t> entryValue_;
David Zeuthen28edb102020-04-28 18:54:55 -0400139
David Zeuthen630de2a2020-05-11 14:04:54 -0400140 void calcDeviceNameSpacesSize(uint32_t accessControlProfileMask);
David Zeuthen81603152020-02-11 22:04:24 -0500141};
142
143} // namespace aidl::android::hardware::identity
144
145#endif // ANDROID_HARDWARE_IDENTITY_IDENTITYCREDENTIAL_H