1 /*
2  ** Copyright 2020, 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 #pragma once
18 
19 #include "Keymaster.h"
20 
21 namespace android::hardware::keymaster::V4_1::support {
22 
23 using android::sp;
24 
25 /**
26  * This class can wrap either a V4_0 or V4_1 IKeymasterDevice.
27  */
28 class Keymaster4 : public Keymaster {
29   public:
30     // This definition is used for device enumeration; enumerating 4.0 devices will also
31     // enumerate 4.1. devices.
32     using WrappedIKeymasterDevice = V4_0::IKeymasterDevice;
33 
Keymaster4(sp<V4_0::IKeymasterDevice> km4_0_dev,const hidl_string & instanceName)34     Keymaster4(sp<V4_0::IKeymasterDevice> km4_0_dev, const hidl_string& instanceName)
35         : Keymaster(V4_1::IKeymasterDevice::descriptor, instanceName),
36           haveVersion_(false),
37           km4_0_dev_(km4_0_dev),
38           km4_1_dev_(V4_1::IKeymasterDevice::castFrom(km4_0_dev)) {}
39 
halVersion()40     const VersionResult& halVersion() const override {
41         const_cast<Keymaster4*>(this)->getVersionIfNeeded();
42         return version_;
43     }
44 
45     /**********************************
46      * V4_0::IKeymasterDevice methods *
47      *********************************/
48 
getHardwareInfo(getHardwareInfo_cb _hidl_cb)49     Return<void> getHardwareInfo(getHardwareInfo_cb _hidl_cb) override {
50         return km4_0_dev_->getHardwareInfo(_hidl_cb);
51     }
52 
getHmacSharingParameters(getHmacSharingParameters_cb _hidl_cb)53     Return<void> getHmacSharingParameters(getHmacSharingParameters_cb _hidl_cb) override {
54         return km4_0_dev_->getHmacSharingParameters(_hidl_cb);
55     }
56 
computeSharedHmac(const hidl_vec<HmacSharingParameters> & params,computeSharedHmac_cb _hidl_cb)57     Return<void> computeSharedHmac(const hidl_vec<HmacSharingParameters>& params,
58                                    computeSharedHmac_cb _hidl_cb) override {
59         return km4_0_dev_->computeSharedHmac(params, _hidl_cb);
60     }
61 
verifyAuthorization(uint64_t operationHandle,const hidl_vec<KeyParameter> & params,const HardwareAuthToken & authToken,verifyAuthorization_cb _hidl_cb)62     Return<void> verifyAuthorization(uint64_t operationHandle, const hidl_vec<KeyParameter>& params,
63                                      const HardwareAuthToken& authToken,
64                                      verifyAuthorization_cb _hidl_cb) override {
65         return km4_0_dev_->verifyAuthorization(operationHandle, params, authToken, _hidl_cb);
66     }
67 
addRngEntropy(const hidl_vec<uint8_t> & data)68     Return<V4_0::ErrorCode> addRngEntropy(const hidl_vec<uint8_t>& data) override {
69         return km4_0_dev_->addRngEntropy(data);
70     }
71 
generateKey(const hidl_vec<KeyParameter> & keyParams,generateKey_cb _hidl_cb)72     Return<void> generateKey(const hidl_vec<KeyParameter>& keyParams,
73                              generateKey_cb _hidl_cb) override {
74         return km4_0_dev_->generateKey(keyParams, _hidl_cb);
75     }
76 
getKeyCharacteristics(const hidl_vec<uint8_t> & keyBlob,const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,getKeyCharacteristics_cb _hidl_cb)77     Return<void> getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
78                                        const hidl_vec<uint8_t>& clientId,
79                                        const hidl_vec<uint8_t>& appData,
80                                        getKeyCharacteristics_cb _hidl_cb) override {
81         return km4_0_dev_->getKeyCharacteristics(keyBlob, clientId, appData, _hidl_cb);
82     }
83 
importKey(const hidl_vec<KeyParameter> & params,KeyFormat keyFormat,const hidl_vec<uint8_t> & keyData,importKey_cb _hidl_cb)84     Return<void> importKey(const hidl_vec<KeyParameter>& params, KeyFormat keyFormat,
85                            const hidl_vec<uint8_t>& keyData, importKey_cb _hidl_cb) override {
86         return km4_0_dev_->importKey(params, keyFormat, keyData, _hidl_cb);
87     }
88 
importWrappedKey(const hidl_vec<uint8_t> & wrappedKeyData,const hidl_vec<uint8_t> & wrappingKeyBlob,const hidl_vec<uint8_t> & maskingKey,const hidl_vec<KeyParameter> & unwrappingParams,uint64_t passwordSid,uint64_t biometricSid,importWrappedKey_cb _hidl_cb)89     Return<void> importWrappedKey(const hidl_vec<uint8_t>& wrappedKeyData,
90                                   const hidl_vec<uint8_t>& wrappingKeyBlob,
91                                   const hidl_vec<uint8_t>& maskingKey,
92                                   const hidl_vec<KeyParameter>& unwrappingParams,
93                                   uint64_t passwordSid, uint64_t biometricSid,
94                                   importWrappedKey_cb _hidl_cb) {
95         return km4_0_dev_->importWrappedKey(wrappedKeyData, wrappingKeyBlob, maskingKey,
96                                             unwrappingParams, passwordSid, biometricSid, _hidl_cb);
97     }
98 
exportKey(KeyFormat exportFormat,const hidl_vec<uint8_t> & keyBlob,const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,exportKey_cb _hidl_cb)99     Return<void> exportKey(KeyFormat exportFormat, const hidl_vec<uint8_t>& keyBlob,
100                            const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
101                            exportKey_cb _hidl_cb) override {
102         return km4_0_dev_->exportKey(exportFormat, keyBlob, clientId, appData, _hidl_cb);
103     }
104 
attestKey(const hidl_vec<uint8_t> & keyToAttest,const hidl_vec<KeyParameter> & attestParams,attestKey_cb _hidl_cb)105     Return<void> attestKey(const hidl_vec<uint8_t>& keyToAttest,
106                            const hidl_vec<KeyParameter>& attestParams,
107                            attestKey_cb _hidl_cb) override {
108         return km4_0_dev_->attestKey(keyToAttest, attestParams, _hidl_cb);
109     }
110 
upgradeKey(const hidl_vec<uint8_t> & keyBlobToUpgrade,const hidl_vec<KeyParameter> & upgradeParams,upgradeKey_cb _hidl_cb)111     Return<void> upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
112                             const hidl_vec<KeyParameter>& upgradeParams,
113                             upgradeKey_cb _hidl_cb) override {
114         return km4_0_dev_->upgradeKey(keyBlobToUpgrade, upgradeParams, _hidl_cb);
115     }
116 
deleteKey(const hidl_vec<uint8_t> & keyBlob)117     Return<V4_0::ErrorCode> deleteKey(const hidl_vec<uint8_t>& keyBlob) override {
118         return km4_0_dev_->deleteKey(keyBlob);
119     }
120 
deleteAllKeys()121     Return<V4_0::ErrorCode> deleteAllKeys() override { return km4_0_dev_->deleteAllKeys(); }
122 
destroyAttestationIds()123     Return<V4_0::ErrorCode> destroyAttestationIds() override {
124         return km4_0_dev_->destroyAttestationIds();
125     }
126 
begin(KeyPurpose purpose,const hidl_vec<uint8_t> & key,const hidl_vec<KeyParameter> & inParams,const HardwareAuthToken & authToken,begin_cb _hidl_cb)127     Return<void> begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
128                        const hidl_vec<KeyParameter>& inParams, const HardwareAuthToken& authToken,
129                        begin_cb _hidl_cb) override {
130         return km4_0_dev_->begin(purpose, key, inParams, authToken, _hidl_cb);
131     }
132 
update(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,const HardwareAuthToken & authToken,const VerificationToken & verificationToken,update_cb _hidl_cb)133     Return<void> update(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
134                         const hidl_vec<uint8_t>& input, const HardwareAuthToken& authToken,
135                         const VerificationToken& verificationToken, update_cb _hidl_cb) override {
136         return km4_0_dev_->update(operationHandle, inParams, input, authToken, verificationToken,
137                                   _hidl_cb);
138     }
139 
finish(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,const hidl_vec<uint8_t> & signature,const HardwareAuthToken & authToken,const VerificationToken & verificationToken,finish_cb _hidl_cb)140     Return<void> finish(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
141                         const hidl_vec<uint8_t>& input, const hidl_vec<uint8_t>& signature,
142                         const HardwareAuthToken& authToken,
143                         const VerificationToken& verificationToken, finish_cb _hidl_cb) override {
144         return km4_0_dev_->finish(operationHandle, inParams, input, signature, authToken,
145                                   verificationToken, _hidl_cb);
146     }
147 
abort(uint64_t operationHandle)148     Return<V4_0::ErrorCode> abort(uint64_t operationHandle) override {
149         return km4_0_dev_->abort(operationHandle);
150     }
151 
152     /**********************************
153      * V4_1::IKeymasterDevice methods *
154      *********************************/
155 
deviceLocked(bool passwordOnly,const VerificationToken & verificationToken)156     Return<ErrorCode> deviceLocked(bool passwordOnly,
157                                    const VerificationToken& verificationToken) override {
158         if (km4_1_dev_) return km4_1_dev_->deviceLocked(passwordOnly, verificationToken);
159         return ErrorCode::UNIMPLEMENTED;
160     }
161 
earlyBootEnded()162     Return<ErrorCode> earlyBootEnded() override {
163         if (km4_1_dev_) return km4_1_dev_->earlyBootEnded();
164         return ErrorCode::UNIMPLEMENTED;
165     }
166 
167   private:
168     void getVersionIfNeeded();
169 
170     bool haveVersion_;
171     VersionResult version_;
172     sp<V4_0::IKeymasterDevice> km4_0_dev_;
173     sp<V4_1::IKeymasterDevice> km4_1_dev_;
174 };  // namespace android::hardware::keymaster::V4_1::support
175 
176 }  // namespace android::hardware::keymaster::V4_1::support
177