1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 #include "keymaster_passthrough_operation.h"
18 #include <keymaster/legacy_support/keymaster_passthrough_engine.h>
19 #include <keymaster/legacy_support/keymaster_passthrough_key.h>
20 
21 #include <hardware/keymaster1.h>
22 #include <hardware/keymaster2.h>
23 
24 #include <assert.h>
25 
26 #include <algorithm>
27 #include <memory>
28 #include <type_traits>
29 
30 #define LOG_TAG "Keymaster2Engine"
31 #include <android/log.h>
32 
33 using std::unique_ptr;
34 
35 namespace keymaster {
36 
37 template <typename KeymasterDeviceType>
38 class TKeymasterPassthroughEngine : public KeymasterPassthroughEngine {
39     using opfactory_t = KeymasterPassthroughOperationFactory<KeymasterDeviceType>;
40 
41   public:
42     /**
43      * The engine takes ownership of the device, and will close it during destruction.
44      */
TKeymasterPassthroughEngine(const KeymasterDeviceType * km_device)45     explicit TKeymasterPassthroughEngine(const KeymasterDeviceType* km_device)
46         : km_device_(km_device) {
47         rsa_encrypt_op_factory_.reset(
48             new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, km_device_));
49         rsa_decrypt_op_factory_.reset(
50             new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_DECRYPT, km_device_));
51         rsa_sign_op_factory_.reset(new (std::nothrow)
52                                        opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, km_device_));
53         rsa_verify_op_factory_.reset(
54             new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_VERIFY, km_device_));
55         ec_encrypt_op_factory_.reset(
56             new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_ENCRYPT, km_device_));
57         ec_decrypt_op_factory_.reset(
58             new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_DECRYPT, km_device_));
59         ec_sign_op_factory_.reset(new (std::nothrow)
60                                       opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_SIGN, km_device_));
61         ec_verify_op_factory_.reset(
62             new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY, km_device_));
63         ec_derive_op_factory_.reset(
64             new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_DERIVE_KEY, km_device_));
65         aes_encrypt_op_factory_.reset(
66             new (std::nothrow) opfactory_t(KM_ALGORITHM_AES, KM_PURPOSE_ENCRYPT, km_device_));
67         aes_decrypt_op_factory_.reset(
68             new (std::nothrow) opfactory_t(KM_ALGORITHM_AES, KM_PURPOSE_DECRYPT, km_device_));
69         triple_des_encrypt_op_factory_.reset(new (std::nothrow) opfactory_t(
70             KM_ALGORITHM_TRIPLE_DES, KM_PURPOSE_ENCRYPT, km_device_));
71         triple_des_decrypt_op_factory_.reset(new (std::nothrow) opfactory_t(
72             KM_ALGORITHM_TRIPLE_DES, KM_PURPOSE_DECRYPT, km_device_));
73         hmac_sign_op_factory_.reset(
74             new (std::nothrow) opfactory_t(KM_ALGORITHM_HMAC, KM_PURPOSE_SIGN, km_device_));
75         hmac_verify_op_factory_.reset(
76             new (std::nothrow) opfactory_t(KM_ALGORITHM_HMAC, KM_PURPOSE_VERIFY, km_device_));
77     }
~TKeymasterPassthroughEngine()78     virtual ~TKeymasterPassthroughEngine() {
79         // QUIRK: we only take ownership if this is a KM2 device.
80         //        For KM1 the Keymaster1Engine takes ownership
81         if (std::is_same<KeymasterDeviceType, keymaster2_device_t>::value)
82             km_device_->common.close(
83                 reinterpret_cast<hw_device_t*>(const_cast<KeymasterDeviceType*>(km_device_)));
84     }
85 
86     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
87                                   KeymasterKeyBlob* key_material, AuthorizationSet* hw_enforced,
88                                   AuthorizationSet* sw_enforced) const override;
89 
90     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
91                                 keymaster_key_format_t input_key_material_format,
92                                 const KeymasterKeyBlob& input_key_material,
93                                 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
94                                 AuthorizationSet* sw_enforced) const override;
ExportKey(keymaster_key_format_t format,const KeymasterKeyBlob & blob,const KeymasterBlob & client_id,const KeymasterBlob & app_data,KeymasterBlob * export_data) const95     keymaster_error_t ExportKey(keymaster_key_format_t format, const KeymasterKeyBlob& blob,
96                                 const KeymasterBlob& client_id, const KeymasterBlob& app_data,
97                                 KeymasterBlob* export_data) const override {
98         keymaster_blob_t my_export_data = {};
99         keymaster_error_t error = km_device_->export_key(km_device_, format, &blob, &client_id,
100                                                          &app_data, &my_export_data);
101         if (error != KM_ERROR_OK) return error;
102         *export_data = KeymasterBlob(my_export_data.data, my_export_data.data_length);
103         free(const_cast<uint8_t*>(my_export_data.data));
104         if (export_data->data == nullptr) {
105             return KM_ERROR_MEMORY_ALLOCATION_FAILED;
106         }
107         return error;
108     }
DeleteKey(const KeymasterKeyBlob & blob) const109     keymaster_error_t DeleteKey(const KeymasterKeyBlob& blob) const override {
110         return km_device_->delete_key(km_device_, &blob);
111     }
DeleteAllKeys() const112     keymaster_error_t DeleteAllKeys() const override {
113         return km_device_->delete_all_keys(km_device_);
114     }
GetOperationFactory(keymaster_purpose_t purpose,keymaster_algorithm_t algorithm) const115     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose,
116                                           keymaster_algorithm_t algorithm) const override {
117         switch (algorithm) {
118         case KM_ALGORITHM_RSA:
119             switch (purpose) {
120             case KM_PURPOSE_ENCRYPT:
121                 return rsa_encrypt_op_factory_.get();
122             case KM_PURPOSE_DECRYPT:
123                 return rsa_decrypt_op_factory_.get();
124             case KM_PURPOSE_SIGN:
125                 return rsa_sign_op_factory_.get();
126             case KM_PURPOSE_VERIFY:
127                 return rsa_verify_op_factory_.get();
128             default:
129                 return nullptr;
130             }
131         case KM_ALGORITHM_EC:
132             switch (purpose) {
133             case KM_PURPOSE_ENCRYPT:
134                 return ec_encrypt_op_factory_.get();
135             case KM_PURPOSE_DECRYPT:
136                 return ec_decrypt_op_factory_.get();
137             case KM_PURPOSE_SIGN:
138                 return ec_sign_op_factory_.get();
139             case KM_PURPOSE_VERIFY:
140                 return ec_verify_op_factory_.get();
141             case KM_PURPOSE_DERIVE_KEY:
142                 return ec_derive_op_factory_.get();
143             default:
144                 return nullptr;
145             }
146         case KM_ALGORITHM_AES:
147             switch (purpose) {
148             case KM_PURPOSE_ENCRYPT:
149                 return aes_encrypt_op_factory_.get();
150             case KM_PURPOSE_DECRYPT:
151                 return aes_decrypt_op_factory_.get();
152             default:
153                 return nullptr;
154             }
155         case KM_ALGORITHM_TRIPLE_DES:
156             switch (purpose) {
157             case KM_PURPOSE_ENCRYPT:
158                 return triple_des_encrypt_op_factory_.get();
159             case KM_PURPOSE_DECRYPT:
160                 return triple_des_decrypt_op_factory_.get();
161             default:
162                 return nullptr;
163             }
164         case KM_ALGORITHM_HMAC:
165             switch (purpose) {
166             case KM_PURPOSE_SIGN:
167                 return hmac_sign_op_factory_.get();
168             case KM_PURPOSE_VERIFY:
169                 return hmac_verify_op_factory_.get();
170             default:
171                 return nullptr;
172             }
173         }
174     }
175 
device() const176     const KeymasterDeviceType* device() const { return km_device_; }
177 
178   private:
179     TKeymasterPassthroughEngine(const KeymasterPassthroughEngine&) = delete;  // Uncopyable
180     void operator=(const KeymasterPassthroughEngine&) = delete;               // Unassignable
181 
182     const KeymasterDeviceType* const km_device_;
183     std::unique_ptr<opfactory_t> rsa_encrypt_op_factory_;
184     std::unique_ptr<opfactory_t> rsa_decrypt_op_factory_;
185     std::unique_ptr<opfactory_t> rsa_sign_op_factory_;
186     std::unique_ptr<opfactory_t> rsa_verify_op_factory_;
187     std::unique_ptr<opfactory_t> ec_encrypt_op_factory_;
188     std::unique_ptr<opfactory_t> ec_decrypt_op_factory_;
189     std::unique_ptr<opfactory_t> ec_sign_op_factory_;
190     std::unique_ptr<opfactory_t> ec_verify_op_factory_;
191     std::unique_ptr<opfactory_t> ec_derive_op_factory_;
192     std::unique_ptr<opfactory_t> aes_encrypt_op_factory_;
193     std::unique_ptr<opfactory_t> aes_decrypt_op_factory_;
194     std::unique_ptr<opfactory_t> triple_des_encrypt_op_factory_;
195     std::unique_ptr<opfactory_t> triple_des_decrypt_op_factory_;
196     std::unique_ptr<opfactory_t> hmac_sign_op_factory_;
197     std::unique_ptr<opfactory_t> hmac_verify_op_factory_;
198 };
199 
ConvertCharacteristics(const keymaster_key_characteristics_t & characteristics,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced)200 static void ConvertCharacteristics(const keymaster_key_characteristics_t& characteristics,
201                                    AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) {
202     if (hw_enforced) hw_enforced->Reinitialize(characteristics.hw_enforced);
203     if (sw_enforced) sw_enforced->Reinitialize(characteristics.sw_enforced);
204 }
205 
206 template <>
GenerateKey(const AuthorizationSet & key_description,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const207 keymaster_error_t TKeymasterPassthroughEngine<keymaster1_device_t>::GenerateKey(
208     const AuthorizationSet& key_description, KeymasterKeyBlob* key_blob,
209     AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
210     assert(key_blob);
211 
212     keymaster_key_characteristics_t* characteristics = nullptr;
213     keymaster_key_blob_t blob = {};
214     keymaster_error_t error =
215         km_device_->generate_key(km_device_, &key_description, &blob, &characteristics);
216     if (error != KM_ERROR_OK) return error;
217     unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
218     key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
219     key_blob->key_material_size = blob.key_material_size;
220 
221     ConvertCharacteristics(*characteristics, hw_enforced, sw_enforced);
222     keymaster_free_characteristics(characteristics);
223     free(characteristics);
224     return error;
225 }
226 template <>
GenerateKey(const AuthorizationSet & key_description,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const227 keymaster_error_t TKeymasterPassthroughEngine<keymaster2_device_t>::GenerateKey(
228     const AuthorizationSet& key_description, KeymasterKeyBlob* key_blob,
229     AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
230     assert(key_blob);
231 
232     keymaster_key_characteristics_t characteristics = {};
233     keymaster_key_blob_t blob = {};
234     keymaster_error_t error =
235         km_device_->generate_key(km_device_, &key_description, &blob, &characteristics);
236     if (error != KM_ERROR_OK) return error;
237     unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
238     key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
239     key_blob->key_material_size = blob.key_material_size;
240 
241     ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
242     keymaster_free_characteristics(&characteristics);
243     return error;
244 }
245 
246 template <>
ImportKey(const AuthorizationSet & key_description,keymaster_key_format_t input_key_material_format,const KeymasterKeyBlob & input_key_material,KeymasterKeyBlob * output_key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const247 keymaster_error_t TKeymasterPassthroughEngine<keymaster1_device_t>::ImportKey(
248     const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
249     const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
250     AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
251     assert(output_key_blob);
252 
253     keymaster_key_characteristics_t* characteristics = {};
254     const keymaster_blob_t input_key = {input_key_material.key_material,
255                                         input_key_material.key_material_size};
256     keymaster_key_blob_t blob = {};
257     keymaster_error_t error =
258         km_device_->import_key(km_device_, &key_description, input_key_material_format, &input_key,
259                                &blob, &characteristics);
260     if (error != KM_ERROR_OK) return error;
261     unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
262 
263     *output_key_blob = KeymasterKeyBlob(blob);
264 
265     ConvertCharacteristics(*characteristics, hw_enforced, sw_enforced);
266     keymaster_free_characteristics(characteristics);
267     free(characteristics);
268     return error;
269 }
270 
271 template <>
ImportKey(const AuthorizationSet & key_description,keymaster_key_format_t input_key_material_format,const KeymasterKeyBlob & input_key_material,KeymasterKeyBlob * output_key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const272 keymaster_error_t TKeymasterPassthroughEngine<keymaster2_device_t>::ImportKey(
273     const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
274     const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
275     AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
276     assert(output_key_blob);
277 
278     keymaster_key_characteristics_t characteristics = {};
279     const keymaster_blob_t input_key = {input_key_material.key_material,
280                                         input_key_material.key_material_size};
281     keymaster_key_blob_t blob = {};
282     keymaster_error_t error =
283         km_device_->import_key(km_device_, &key_description, input_key_material_format, &input_key,
284                                &blob, &characteristics);
285     if (error != KM_ERROR_OK) return error;
286     unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
287     // TODO why duplicate the blob if we have ownership here anyway?
288     output_key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
289     output_key_blob->key_material_size = blob.key_material_size;
290 
291     ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
292     keymaster_free_characteristics(&characteristics);
293     return error;
294 }
295 
296 typedef UniquePtr<KeymasterPassthroughEngine> engine_ptr_t;
297 
createInstance(const keymaster1_device_t * dev)298 engine_ptr_t KeymasterPassthroughEngine::createInstance(const keymaster1_device_t* dev) {
299     return engine_ptr_t(new (std::nothrow) TKeymasterPassthroughEngine<keymaster1_device_t>(dev));
300 }
createInstance(const keymaster2_device_t * dev)301 engine_ptr_t KeymasterPassthroughEngine::createInstance(const keymaster2_device_t* dev) {
302     return engine_ptr_t(new (std::nothrow) TKeymasterPassthroughEngine<keymaster2_device_t>(dev));
303 }
304 
305 }  // namespace keymaster
306