-
Notifications
You must be signed in to change notification settings - Fork 0
/
keymaster1_engine.cpp
427 lines (358 loc) · 15.6 KB
/
keymaster1_engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "keymaster1_engine.h"
#include <assert.h>
#include <algorithm>
#include <memory>
#define LOG_TAG "Keymaster1Engine"
#include <cutils/log.h>
#include "keymaster/android_keymaster_utils.h"
#include <openssl/bn.h>
#include <openssl/ec_key.h>
#include <openssl/ecdsa.h>
#include "openssl_err.h"
#include "openssl_utils.h"
using std::shared_ptr;
using std::unique_ptr;
namespace keymaster {
Keymaster1Engine* Keymaster1Engine::instance_ = nullptr;
Keymaster1Engine::Keymaster1Engine(const keymaster1_device_t* keymaster1_device)
: keymaster1_device_(keymaster1_device), engine_(ENGINE_new()),
rsa_index_(RSA_get_ex_new_index(0 /* argl */, NULL /* argp */, NULL /* new_func */,
Keymaster1Engine::duplicate_key_data,
Keymaster1Engine::free_key_data)),
ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */, NULL /* argp */, NULL /* new_func */,
Keymaster1Engine::duplicate_key_data,
Keymaster1Engine::free_key_data)),
rsa_method_(BuildRsaMethod()), ecdsa_method_(BuildEcdsaMethod()) {
assert(rsa_index_ != -1);
assert(ec_key_index_ != -1);
assert(keymaster1_device);
assert(!instance_);
instance_ = this;
ENGINE_set_RSA_method(engine_.get(), &rsa_method_, sizeof(rsa_method_));
ENGINE_set_ECDSA_method(engine_.get(), &ecdsa_method_, sizeof(ecdsa_method_));
}
Keymaster1Engine::~Keymaster1Engine() {
keymaster1_device_->common.close(
reinterpret_cast<hw_device_t*>(const_cast<keymaster1_device_t*>(keymaster1_device_)));
instance_ = nullptr;
}
static void ConvertCharacteristics(keymaster_key_characteristics_t* characteristics,
AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) {
unique_ptr<keymaster_key_characteristics_t, Characteristics_Delete> characteristics_deleter(
characteristics);
if (hw_enforced)
hw_enforced->Reinitialize(characteristics->hw_enforced);
if (sw_enforced)
sw_enforced->Reinitialize(characteristics->sw_enforced);
}
keymaster_error_t Keymaster1Engine::GenerateKey(const AuthorizationSet& key_description,
KeymasterKeyBlob* key_blob,
AuthorizationSet* hw_enforced,
AuthorizationSet* sw_enforced) const {
assert(key_blob);
keymaster_key_characteristics_t* characteristics;
keymaster_key_blob_t blob;
keymaster_error_t error = keymaster1_device_->generate_key(keymaster1_device_, &key_description,
&blob, &characteristics);
if (error != KM_ERROR_OK)
return error;
unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
key_blob->key_material_size = blob.key_material_size;
ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
return error;
}
keymaster_error_t Keymaster1Engine::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) const {
assert(output_key_blob);
keymaster_key_characteristics_t* characteristics;
const keymaster_blob_t input_key = {input_key_material.key_material,
input_key_material.key_material_size};
keymaster_key_blob_t blob;
keymaster_error_t error = keymaster1_device_->import_key(keymaster1_device_, &key_description,
input_key_material_format, &input_key,
&blob, &characteristics);
if (error != KM_ERROR_OK)
return error;
unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
output_key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
output_key_blob->key_material_size = blob.key_material_size;
ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
return error;
}
keymaster_error_t Keymaster1Engine::DeleteKey(const KeymasterKeyBlob& blob) const {
if (!keymaster1_device_->delete_key)
return KM_ERROR_OK;
return keymaster1_device_->delete_key(keymaster1_device_, &blob);
}
keymaster_error_t Keymaster1Engine::DeleteAllKeys() const {
if (!keymaster1_device_->delete_all_keys)
return KM_ERROR_OK;
return keymaster1_device_->delete_all_keys(keymaster1_device_);
}
RSA* Keymaster1Engine::BuildRsaKey(const KeymasterKeyBlob& blob,
const AuthorizationSet& additional_params,
keymaster_error_t* error) const {
// Create new RSA key (with engine methods) and add metadata
unique_ptr<RSA, RSA_Delete> rsa(RSA_new_method(engine_.get()));
if (!rsa) {
*error = TranslateLastOpenSslError();
return nullptr;
}
KeyData* key_data = new KeyData(blob, additional_params);
if (!RSA_set_ex_data(rsa.get(), rsa_index_, key_data)) {
*error = TranslateLastOpenSslError();
delete key_data;
return nullptr;
}
// Copy public key into new RSA key
unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
GetKeymaster1PublicKey(key_data->key_material, key_data->begin_params, error));
if (!pkey) {
*error = TranslateLastOpenSslError();
return nullptr;
}
unique_ptr<RSA, RSA_Delete> public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
if (!public_rsa) {
*error = TranslateLastOpenSslError();
return nullptr;
}
rsa->n = BN_dup(public_rsa->n);
rsa->e = BN_dup(public_rsa->e);
if (!rsa->n || !rsa->e) {
*error = TranslateLastOpenSslError();
return nullptr;
}
*error = KM_ERROR_OK;
return rsa.release();
}
EC_KEY* Keymaster1Engine::BuildEcKey(const KeymasterKeyBlob& blob,
const AuthorizationSet& additional_params,
keymaster_error_t* error) const {
// Create new EC key (with engine methods) and insert blob
unique_ptr<EC_KEY, EC_KEY_Delete> ec_key(EC_KEY_new_method(engine_.get()));
if (!ec_key) {
*error = TranslateLastOpenSslError();
return nullptr;
}
KeyData* key_data = new KeyData(blob, additional_params);
if (!EC_KEY_set_ex_data(ec_key.get(), ec_key_index_, key_data)) {
*error = TranslateLastOpenSslError();
delete key_data;
return nullptr;
}
// Copy public key into new EC key
unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
GetKeymaster1PublicKey(blob, additional_params, error));
if (!pkey) {
*error = TranslateLastOpenSslError();
return nullptr;
}
unique_ptr<EC_KEY, EC_KEY_Delete> public_ec_key(EVP_PKEY_get1_EC_KEY(pkey.get()));
if (!public_ec_key) {
*error = TranslateLastOpenSslError();
return nullptr;
}
if (!EC_KEY_set_group(ec_key.get(), EC_KEY_get0_group(public_ec_key.get())) ||
!EC_KEY_set_public_key(ec_key.get(), EC_KEY_get0_public_key(public_ec_key.get()))) {
*error = TranslateLastOpenSslError();
return nullptr;
}
*error = KM_ERROR_OK;
return ec_key.release();
}
Keymaster1Engine::KeyData* Keymaster1Engine::GetData(EVP_PKEY* key) const {
switch (EVP_PKEY_type(key->type)) {
case EVP_PKEY_RSA: {
unique_ptr<RSA, RSA_Delete> rsa(EVP_PKEY_get1_RSA(key));
return GetData(rsa.get());
}
case EVP_PKEY_EC: {
unique_ptr<EC_KEY, EC_KEY_Delete> ec_key(EVP_PKEY_get1_EC_KEY(key));
return GetData(ec_key.get());
}
default:
return nullptr;
};
}
Keymaster1Engine::KeyData* Keymaster1Engine::GetData(const RSA* rsa) const {
if (!rsa)
return nullptr;
return reinterpret_cast<KeyData*>(RSA_get_ex_data(rsa, rsa_index_));
}
Keymaster1Engine::KeyData* Keymaster1Engine::GetData(const EC_KEY* ec_key) const {
if (!ec_key)
return nullptr;
return reinterpret_cast<KeyData*>(EC_KEY_get_ex_data(ec_key, ec_key_index_));
}
/* static */
int Keymaster1Engine::duplicate_key_data(CRYPTO_EX_DATA* /* to */, const CRYPTO_EX_DATA* /* from */,
void** from_d, int /* index */, long /* argl */,
void* /* argp */) {
KeyData* data = reinterpret_cast<KeyData*>(*from_d);
if (!data)
return 1;
// Default copy ctor is good.
*from_d = new KeyData(*data);
if (*from_d)
return 1;
return 0;
}
/* static */
void Keymaster1Engine::free_key_data(void* /* parent */, void* ptr, CRYPTO_EX_DATA* /* data */,
int /* index*/, long /* argl */, void* /* argp */) {
delete reinterpret_cast<KeyData*>(ptr);
}
keymaster_error_t Keymaster1Engine::Keymaster1Finish(const KeyData* key_data,
const keymaster_blob_t& input,
keymaster_blob_t* output) {
if (key_data->op_handle == 0)
return KM_ERROR_UNKNOWN_ERROR;
size_t input_consumed;
// Note: devices are required to consume all input in a single update call for undigested
// signing operations and encryption operations. No need to loop here.
keymaster_error_t error =
device()->update(device(), key_data->op_handle, &key_data->finish_params, &input,
&input_consumed, nullptr /* out_params */, nullptr /* output */);
if (error != KM_ERROR_OK)
return error;
return device()->finish(device(), key_data->op_handle, &key_data->finish_params,
nullptr /* signature */, nullptr /* out_params */, output);
}
/* static */
int Keymaster1Engine::rsa_sign_raw(RSA* rsa, size_t* out_len, uint8_t* out, size_t max_out,
const uint8_t* in, size_t in_len, int padding) {
KeyData* key_data = instance_->GetData(rsa);
if (!key_data)
return 0;
if (padding != key_data->expected_openssl_padding) {
LOG_E("Expected sign_raw with padding %d but got padding %d",
key_data->expected_openssl_padding, padding);
return KM_ERROR_UNKNOWN_ERROR;
}
keymaster_blob_t input = {in, in_len};
keymaster_blob_t output;
key_data->error = instance_->Keymaster1Finish(key_data, input, &output);
if (key_data->error != KM_ERROR_OK)
return 0;
unique_ptr<uint8_t, Malloc_Delete> output_deleter(const_cast<uint8_t*>(output.data));
*out_len = std::min(output.data_length, max_out);
memcpy(out, output.data, *out_len);
return 1;
}
/* static */
int Keymaster1Engine::rsa_decrypt(RSA* rsa, size_t* out_len, uint8_t* out, size_t max_out,
const uint8_t* in, size_t in_len, int padding) {
KeyData* key_data = instance_->GetData(rsa);
if (!key_data)
return 0;
if (padding != key_data->expected_openssl_padding) {
LOG_E("Expected sign_raw with padding %d but got padding %d",
key_data->expected_openssl_padding, padding);
return KM_ERROR_UNKNOWN_ERROR;
}
keymaster_blob_t input = {in, in_len};
keymaster_blob_t output;
key_data->error = instance_->Keymaster1Finish(key_data, input, &output);
if (key_data->error != KM_ERROR_OK)
return 0;
unique_ptr<uint8_t, Malloc_Delete> output_deleter(const_cast<uint8_t*>(output.data));
*out_len = std::min(output.data_length, max_out);
memcpy(out, output.data, *out_len);
return 1;
}
/* static */
int Keymaster1Engine::ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
unsigned int* sig_len, EC_KEY* ec_key) {
KeyData* key_data = instance_->GetData(ec_key);
if (!key_data)
return 0;
// Truncate digest if it's too long
size_t max_input_len = (ec_group_size_bits(ec_key) + 7) / 8;
if (digest_len > max_input_len)
digest_len = max_input_len;
keymaster_blob_t input = {digest, digest_len};
keymaster_blob_t output;
key_data->error = instance_->Keymaster1Finish(key_data, input, &output);
if (key_data->error != KM_ERROR_OK)
return 0;
unique_ptr<uint8_t, Malloc_Delete> output_deleter(const_cast<uint8_t*>(output.data));
*sig_len = std::min(output.data_length, ECDSA_size(ec_key));
memcpy(sig, output.data, *sig_len);
return 1;
}
EVP_PKEY* Keymaster1Engine::GetKeymaster1PublicKey(const KeymasterKeyBlob& blob,
const AuthorizationSet& additional_params,
keymaster_error_t* error) const {
keymaster_blob_t client_id = {nullptr, 0};
keymaster_blob_t app_data = {nullptr, 0};
keymaster_blob_t* client_id_ptr = nullptr;
keymaster_blob_t* app_data_ptr = nullptr;
if (additional_params.GetTagValue(TAG_APPLICATION_ID, &client_id))
client_id_ptr = &client_id;
if (additional_params.GetTagValue(TAG_APPLICATION_DATA, &app_data))
app_data_ptr = &app_data;
keymaster_blob_t export_data = {nullptr, 0};
*error = keymaster1_device_->export_key(keymaster1_device_, KM_KEY_FORMAT_X509, &blob,
client_id_ptr, app_data_ptr, &export_data);
if (*error != KM_ERROR_OK)
return nullptr;
unique_ptr<uint8_t, Malloc_Delete> pub_key(const_cast<uint8_t*>(export_data.data));
const uint8_t* p = export_data.data;
return d2i_PUBKEY(nullptr /* allocate new struct */, &p, export_data.data_length);
}
RSA_METHOD Keymaster1Engine::BuildRsaMethod() {
RSA_METHOD method;
method.common.references = 0;
method.common.is_static = 1;
method.app_data = nullptr;
method.init = nullptr;
method.finish = nullptr;
method.size = nullptr;
method.sign = nullptr;
method.verify = nullptr;
method.encrypt = nullptr;
method.sign_raw = Keymaster1Engine::rsa_sign_raw;
method.decrypt = Keymaster1Engine::rsa_decrypt;
method.verify_raw = nullptr;
method.private_transform = nullptr;
method.mod_exp = nullptr;
method.bn_mod_exp = BN_mod_exp_mont;
method.flags = RSA_FLAG_OPAQUE;
method.keygen = nullptr;
method.supports_digest = nullptr;
return method;
}
ECDSA_METHOD Keymaster1Engine::BuildEcdsaMethod() {
ECDSA_METHOD method;
method.common.references = 0;
method.common.is_static = 1;
method.app_data = nullptr;
method.init = nullptr;
method.finish = nullptr;
method.group_order_size = nullptr;
method.sign = Keymaster1Engine::ecdsa_sign;
method.verify = nullptr;
method.flags = ECDSA_FLAG_OPAQUE;
return method;
}
} // namespace keymaster