1 /*
2  * Copyright (c) 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 SYSTEM_SECURITY_CREDENTIAL_H_
18 #define SYSTEM_SECURITY_CREDENTIAL_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <android/security/identity/BnCredential.h>
24 
25 #include <android/hardware/identity/IIdentityCredentialStore.h>
26 
27 #include "CredentialData.h"
28 
29 namespace android {
30 namespace security {
31 namespace identity {
32 
33 using ::android::sp;
34 using ::android::binder::Status;
35 using ::std::string;
36 using ::std::vector;
37 
38 using ::android::hardware::identity::CipherSuite;
39 using ::android::hardware::identity::HardwareInformation;
40 using ::android::hardware::identity::IIdentityCredential;
41 using ::android::hardware::identity::IIdentityCredentialStore;
42 using ::android::hardware::identity::RequestDataItem;
43 using ::android::hardware::identity::RequestNamespace;
44 
45 class Credential : public BnCredential {
46   public:
47     Credential(CipherSuite cipherSuite, const string& dataPath, const string& credentialName,
48                uid_t callingUid, HardwareInformation hwInfo,
49                sp<IIdentityCredentialStore> halStoreBinder, int halApiVersion);
50     ~Credential();
51 
52     Status ensureOrReplaceHalBinder();
53     void writableCredentialPersonalized();
54 
55     // ICredential overrides
56     Status createEphemeralKeyPair(vector<uint8_t>* _aidl_return) override;
57 
58     Status setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) override;
59 
60     Status deleteCredential(vector<uint8_t>* _aidl_return) override;
61 
62     Status deleteWithChallenge(const vector<uint8_t>& challenge,
63                                vector<uint8_t>* _aidl_return) override;
64 
65     Status proveOwnership(const vector<uint8_t>& challenge, vector<uint8_t>* _aidl_return) override;
66 
67     Status getCredentialKeyCertificateChain(vector<uint8_t>* _aidl_return) override;
68 
69     Status selectAuthKey(bool allowUsingExhaustedKeys, bool allowUsingExpiredKeys,
70                          int64_t* _aidl_return) override;
71 
72     Status getEntries(const vector<uint8_t>& requestMessage,
73                       const vector<RequestNamespaceParcel>& requestNamespaces,
74                       const vector<uint8_t>& sessionTranscript,
75                       const vector<uint8_t>& readerSignature, bool allowUsingExhaustedKeys,
76                       bool allowUsingExpiredKeys, GetEntriesResultParcel* _aidl_return) override;
77 
78     Status setAvailableAuthenticationKeys(int32_t keyCount, int32_t maxUsesPerKey) override;
79     Status getAuthKeysNeedingCertification(vector<AuthKeyParcel>* _aidl_return) override;
80     Status storeStaticAuthenticationData(const AuthKeyParcel& authenticationKey,
81                                          const vector<uint8_t>& staticAuthData) override;
82     Status
83     storeStaticAuthenticationDataWithExpiration(const AuthKeyParcel& authenticationKey,
84                                                 int64_t expirationDateMillisSinceEpoch,
85                                                 const vector<uint8_t>& staticAuthData) override;
86     Status getAuthenticationDataUsageCount(vector<int32_t>* _aidl_return) override;
87 
88     Status update(sp<IWritableCredential>* _aidl_return) override;
89 
90   private:
91     CipherSuite cipherSuite_;
92     string dataPath_;
93     string credentialName_;
94     uid_t callingUid_;
95     HardwareInformation hwInfo_;
96     sp<IIdentityCredentialStore> halStoreBinder_;
97 
98     uint64_t selectedChallenge_ = 0;
99 
100     sp<IIdentityCredential> halBinder_;
101     int halApiVersion_;
102 
103     bool ensureChallenge();
104 
105     ssize_t
106     calcExpectedDeviceNameSpacesSize(const vector<uint8_t>& requestMessage,
107                                      const vector<RequestNamespaceParcel>& requestNamespaces,
108                                      uint32_t authorizedAcps);
109 };
110 
111 }  // namespace identity
112 }  // namespace security
113 }  // namespace android
114 
115 #endif  // SYSTEM_SECURITY_IDENTITY_CREDENTIAL_H_
116