blob: 36ad4300d139f5aa5e805bae3ff08a5bfa8c2ea3 [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_WRITABLEIDENTITYCREDENTIAL_H
18#define ANDROID_HARDWARE_IDENTITY_WRITABLEIDENTITYCREDENTIAL_H
19
20#include <aidl/android/hardware/identity/BnWritableIdentityCredential.h>
21#include <android/hardware/identity/support/IdentityCredentialSupport.h>
22
23#include <cppbor.h>
Selene Huang92b61d62020-03-04 02:24:16 -080024#include <set>
David Zeuthen81603152020-02-11 22:04:24 -050025
David Zeuthen630de2a2020-05-11 14:04:54 -040026#include "IdentityCredentialStore.h"
27#include "SecureHardwareProxy.h"
28
David Zeuthen81603152020-02-11 22:04:24 -050029namespace aidl::android::hardware::identity {
30
David Zeuthen630de2a2020-05-11 14:04:54 -040031using ::android::sp;
32using ::android::hardware::identity::SecureHardwareProvisioningProxy;
Selene Huang92b61d62020-03-04 02:24:16 -080033using ::std::set;
David Zeuthen81603152020-02-11 22:04:24 -050034using ::std::string;
35using ::std::vector;
36
37class WritableIdentityCredential : public BnWritableIdentityCredential {
38 public:
David Zeuthen49f2d252020-10-16 11:27:24 -040039 // For a new credential, call initialize() right after construction.
40 //
41 // For an updated credential, call initializeForUpdate() right after construction.
42 //
David Zeuthen630de2a2020-05-11 14:04:54 -040043 WritableIdentityCredential(sp<SecureHardwareProvisioningProxy> hwProxy, const string& docType,
Seth Moorebe321132022-01-25 22:44:24 +000044 bool testCredential)
45 : hwProxy_(hwProxy), docType_(docType), testCredential_(testCredential) {}
David Zeuthen630de2a2020-05-11 14:04:54 -040046
47 ~WritableIdentityCredential();
David Zeuthen81603152020-02-11 22:04:24 -050048
David Zeuthen49f2d252020-10-16 11:27:24 -040049 // Creates the Credential Key. Returns false on failure.
David Zeuthen81603152020-02-11 22:04:24 -050050 bool initialize();
51
David Zeuthen49f2d252020-10-16 11:27:24 -040052 // Used when updating a credential. Returns false on failure.
53 bool initializeForUpdate(const vector<uint8_t>& encryptedCredentialKeys);
54
David Zeuthen81603152020-02-11 22:04:24 -050055 // Methods from IWritableIdentityCredential follow.
Jooyung Han17be89b2020-02-21 21:17:06 +090056 ndk::ScopedAStatus getAttestationCertificate(const vector<uint8_t>& attestationApplicationId,
57 const vector<uint8_t>& attestationChallenge,
David Zeuthen81603152020-02-11 22:04:24 -050058 vector<Certificate>* outCertificateChain) override;
59
David Zeuthen28edb102020-04-28 18:54:55 -040060 ndk::ScopedAStatus setExpectedProofOfProvisioningSize(
61 int32_t expectedProofOfProvisioningSize) override;
62
David Zeuthen81603152020-02-11 22:04:24 -050063 ndk::ScopedAStatus startPersonalization(int32_t accessControlProfileCount,
64 const vector<int32_t>& entryCounts) override;
65
66 ndk::ScopedAStatus addAccessControlProfile(
67 int32_t id, const Certificate& readerCertificate, bool userAuthenticationRequired,
68 int64_t timeoutMillis, int64_t secureUserId,
69 SecureAccessControlProfile* outSecureAccessControlProfile) override;
70
71 ndk::ScopedAStatus beginAddEntry(const vector<int32_t>& accessControlProfileIds,
72 const string& nameSpace, const string& name,
73 int32_t entrySize) override;
Jooyung Han17be89b2020-02-21 21:17:06 +090074 ndk::ScopedAStatus addEntryValue(const vector<uint8_t>& content,
75 vector<uint8_t>* outEncryptedContent) override;
David Zeuthen81603152020-02-11 22:04:24 -050076
77 ndk::ScopedAStatus finishAddingEntries(
Jooyung Han17be89b2020-02-21 21:17:06 +090078 vector<uint8_t>* outCredentialData,
79 vector<uint8_t>* outProofOfProvisioningSignature) override;
David Zeuthen81603152020-02-11 22:04:24 -050080
David Zeuthen28edb102020-04-28 18:54:55 -040081 private:
David Zeuthen630de2a2020-05-11 14:04:54 -040082 // Set by constructor.
83 sp<SecureHardwareProvisioningProxy> hwProxy_;
David Zeuthen81603152020-02-11 22:04:24 -050084 string docType_;
85 bool testCredential_;
86
Selene Huang459cb802020-01-08 22:59:02 -080087 // This is set in initialize().
Selene Huang92b61d62020-03-04 02:24:16 -080088 bool startPersonalizationCalled_;
89 bool firstEntry_;
Selene Huang459cb802020-01-08 22:59:02 -080090
David Zeuthen630de2a2020-05-11 14:04:54 -040091 // This is set in getAttestationCertificate().
92 bool getAttestationCertificateAlreadyCalled_ = false;
David Zeuthen81603152020-02-11 22:04:24 -050093
94 // These fields are initialized during startPersonalization()
95 size_t numAccessControlProfileRemaining_;
96 vector<int32_t> remainingEntryCounts_;
97 cppbor::Array signedDataAccessControlProfiles_;
98 cppbor::Map signedDataNamespaces_;
99 cppbor::Array signedDataCurrentNamespace_;
David Zeuthen28edb102020-04-28 18:54:55 -0400100 size_t expectedProofOfProvisioningSize_;
David Zeuthen81603152020-02-11 22:04:24 -0500101
Selene Huang92b61d62020-03-04 02:24:16 -0800102 // This field is initialized in addAccessControlProfile
103 set<int32_t> accessControlProfileIds_;
104
David Zeuthen81603152020-02-11 22:04:24 -0500105 // These fields are initialized during beginAddEntry()
106 size_t entryRemainingBytes_;
David Zeuthen81603152020-02-11 22:04:24 -0500107 string entryNameSpace_;
108 string entryName_;
109 vector<int32_t> entryAccessControlProfileIds_;
110 vector<uint8_t> entryBytes_;
Selene Huang92b61d62020-03-04 02:24:16 -0800111 set<string> allNameSpaces_;
David Zeuthen81603152020-02-11 22:04:24 -0500112};
113
114} // namespace aidl::android::hardware::identity
115
116#endif // ANDROID_HARDWARE_IDENTITY_WRITABLEIDENTITYCREDENTIAL_H