blob: d79451c2f11dd8636af933f6e4cd7d73829f5aa4 [file] [log] [blame]
Shawn Willdenac398062015-05-20 16:36:24 -06001/*
2 * Copyright 2015 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#include "keymaster0_engine.h"
18
19#include <assert.h>
20
21#include <memory>
22
23#define LOG_TAG "Keymaster0Engine"
24#include <cutils/log.h>
25
26#include "keymaster/android_keymaster_utils.h"
27
Shawn Willden9a1cd6d2015-07-08 17:12:16 -060028#include <openssl/bn.h>
Shawn Willdenac398062015-05-20 16:36:24 -060029#include <openssl/ec_key.h>
30#include <openssl/ecdsa.h>
31
32#include "openssl_utils.h"
33
34using std::shared_ptr;
35using std::unique_ptr;
36
37namespace keymaster {
38
39// int Keymaster0Engine::rsa_index_ = -1;
40// int Keymaster0Engine::ec_key_index_ = -1;
41Keymaster0Engine* Keymaster0Engine::instance_ = nullptr;
42const RSA_METHOD Keymaster0Engine::rsa_method_ = {
Shawn Willden24bdfc22015-05-26 13:12:24 -060043 .common =
44 {
45 0, // references
46 1 // is_static
47 },
Shawn Willdenac398062015-05-20 16:36:24 -060048 .app_data = nullptr,
49 .init = nullptr,
50 .finish = nullptr,
51 .size = nullptr,
52 .sign = nullptr,
53 .verify = nullptr,
54
55 .encrypt = nullptr,
56 .sign_raw = nullptr,
57 .decrypt = nullptr,
58 .verify_raw = nullptr,
59
60 .private_transform = Keymaster0Engine::rsa_private_transform,
61
62 .mod_exp = nullptr,
63 .bn_mod_exp = BN_mod_exp_mont,
64
Shawn Willden24bdfc22015-05-26 13:12:24 -060065 .flags = RSA_FLAG_OPAQUE,
Shawn Willdenac398062015-05-20 16:36:24 -060066
67 .keygen = nullptr,
Adam Langley607b0e02015-09-02 15:33:52 -070068#if defined(BORINGSSL_201509)
69 .multi_prime_keygen = nullptr,
70#endif
Shawn Willdenac398062015-05-20 16:36:24 -060071 .supports_digest = nullptr,
72};
73
Shawn Willden24bdfc22015-05-26 13:12:24 -060074const ECDSA_METHOD Keymaster0Engine::ecdsa_method_ = {
75 .common =
76 {
77 0, // references
78 1 // is_static
79 },
80 .app_data = nullptr,
81 .init = nullptr,
82 .finish = nullptr,
83 .group_order_size = nullptr,
84 .sign = Keymaster0Engine::ecdsa_sign,
85 .verify = nullptr,
86 .flags = ECDSA_FLAG_OPAQUE,
87};
88
Shawn Willdenac398062015-05-20 16:36:24 -060089Keymaster0Engine::Keymaster0Engine(const keymaster0_device_t* keymaster0_device)
Shawn Willden24bdfc22015-05-26 13:12:24 -060090 : keymaster0_device_(keymaster0_device), engine_(ENGINE_new()), supports_ec_(false) {
Shawn Willdenac398062015-05-20 16:36:24 -060091 assert(!instance_);
92 instance_ = this;
93
94 rsa_index_ = RSA_get_ex_new_index(0 /* argl */, NULL /* argp */, NULL /* new_func */,
95 keyblob_dup, keyblob_free);
Shawn Willden24bdfc22015-05-26 13:12:24 -060096 ec_key_index_ = EC_KEY_get_ex_new_index(0 /* argl */, NULL /* argp */, NULL /* new_func */,
97 keyblob_dup, keyblob_free);
98
Shawn Willdenac398062015-05-20 16:36:24 -060099 ENGINE_set_RSA_method(engine_, &rsa_method_, sizeof(rsa_method_));
Shawn Willden24bdfc22015-05-26 13:12:24 -0600100
101 if ((keymaster0_device_->flags & KEYMASTER_SUPPORTS_EC) != 0) {
102 supports_ec_ = true;
103 ENGINE_set_ECDSA_method(engine_, &ecdsa_method_, sizeof(ecdsa_method_));
104 }
Shawn Willdenac398062015-05-20 16:36:24 -0600105}
106
107Keymaster0Engine::~Keymaster0Engine() {
108 if (keymaster0_device_)
109 keymaster0_device_->common.close(
110 reinterpret_cast<hw_device_t*>(const_cast<keymaster0_device_t*>(keymaster0_device_)));
111 ENGINE_free(engine_);
112 instance_ = nullptr;
113}
114
115bool Keymaster0Engine::GenerateRsaKey(uint64_t public_exponent, uint32_t public_modulus,
116 KeymasterKeyBlob* key_material) const {
117 assert(key_material);
118 keymaster_rsa_keygen_params_t params;
119 params.public_exponent = public_exponent;
120 params.modulus_size = public_modulus;
121
122 uint8_t* key_blob = 0;
123 if (keymaster0_device_->generate_keypair(keymaster0_device_, TYPE_RSA, &params, &key_blob,
124 &key_material->key_material_size) < 0) {
125 ALOGE("Error generating RSA key pair with keymaster0 device");
126 return false;
127 }
128 unique_ptr<uint8_t, Malloc_Delete> key_blob_deleter(key_blob);
129 key_material->key_material = dup_buffer(key_blob, key_material->key_material_size);
130 return true;
131}
132
Shawn Willden24bdfc22015-05-26 13:12:24 -0600133bool Keymaster0Engine::GenerateEcKey(uint32_t key_size, KeymasterKeyBlob* key_material) const {
134 assert(key_material);
135 keymaster_ec_keygen_params_t params;
136 params.field_size = key_size;
137
138 uint8_t* key_blob = 0;
139 if (keymaster0_device_->generate_keypair(keymaster0_device_, TYPE_EC, &params, &key_blob,
140 &key_material->key_material_size) < 0) {
141 ALOGE("Error generating EC key pair with keymaster0 device");
142 return false;
143 }
144 unique_ptr<uint8_t, Malloc_Delete> key_blob_deleter(key_blob);
145 key_material->key_material = dup_buffer(key_blob, key_material->key_material_size);
146 return true;
147}
148
149bool Keymaster0Engine::ImportKey(keymaster_key_format_t key_format,
150 const KeymasterKeyBlob& to_import,
151 KeymasterKeyBlob* imported_key) const {
Shawn Willdenac398062015-05-20 16:36:24 -0600152 assert(imported_key);
153 if (key_format != KM_KEY_FORMAT_PKCS8)
154 return false;
155
156 uint8_t* key_blob = 0;
157 if (keymaster0_device_->import_keypair(keymaster0_device_, to_import.key_material,
158 to_import.key_material_size, &key_blob,
159 &imported_key->key_material_size) < 0) {
Shawn Willden24bdfc22015-05-26 13:12:24 -0600160 ALOGW("Error importing keypair with keymaster0 device");
Shawn Willdenac398062015-05-20 16:36:24 -0600161 return false;
162 }
163 unique_ptr<uint8_t, Malloc_Delete> key_blob_deleter(key_blob);
164 imported_key->key_material = dup_buffer(key_blob, imported_key->key_material_size);
165 return true;
166}
167
168static keymaster_key_blob_t* duplicate_blob(const uint8_t* key_data, size_t key_data_size) {
Shawn Willden661b2b12015-06-20 09:16:30 -0600169 unique_ptr<uint8_t[]> key_material_copy(dup_buffer(key_data, key_data_size));
Shawn Willdenac398062015-05-20 16:36:24 -0600170 if (!key_material_copy)
171 return nullptr;
172
Shawn Willden9a1cd6d2015-07-08 17:12:16 -0600173 unique_ptr<keymaster_key_blob_t> blob_copy(new (std::nothrow) keymaster_key_blob_t);
Shawn Willden661b2b12015-06-20 09:16:30 -0600174 if (!blob_copy.get())
175 return nullptr;
Shawn Willdenac398062015-05-20 16:36:24 -0600176 blob_copy->key_material_size = key_data_size;
177 blob_copy->key_material = key_material_copy.release();
178 return blob_copy.release();
179}
180
181inline keymaster_key_blob_t* duplicate_blob(const keymaster_key_blob_t& blob) {
182 return duplicate_blob(blob.key_material, blob.key_material_size);
183}
184
185RSA* Keymaster0Engine::BlobToRsaKey(const KeymasterKeyBlob& blob) const {
186 // Create new RSA key (with engine methods) and insert blob
187 unique_ptr<RSA, RSA_Delete> rsa(RSA_new_method(engine_));
188 if (!rsa)
189 return nullptr;
190
191 keymaster_key_blob_t* blob_copy = duplicate_blob(blob);
192 if (!blob_copy->key_material || !RSA_set_ex_data(rsa.get(), rsa_index_, blob_copy))
193 return nullptr;
194
195 // Copy public key into new RSA key
196 unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(GetKeymaster0PublicKey(blob));
197 if (!pkey)
198 return nullptr;
199 unique_ptr<RSA, RSA_Delete> public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
200 if (!public_rsa)
201 return nullptr;
202 rsa->n = BN_dup(public_rsa->n);
203 rsa->e = BN_dup(public_rsa->e);
204 if (!rsa->n || !rsa->e)
205 return nullptr;
206
207 return rsa.release();
208}
209
Shawn Willden24bdfc22015-05-26 13:12:24 -0600210EC_KEY* Keymaster0Engine::BlobToEcKey(const KeymasterKeyBlob& blob) const {
211 // Create new EC key (with engine methods) and insert blob
212 unique_ptr<EC_KEY, EC_Delete> ec_key(EC_KEY_new_method(engine_));
213 if (!ec_key)
214 return nullptr;
215
216 keymaster_key_blob_t* blob_copy = duplicate_blob(blob);
217 if (!blob_copy->key_material || !EC_KEY_set_ex_data(ec_key.get(), ec_key_index_, blob_copy))
218 return nullptr;
219
220 // Copy public key into new EC key
221 unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(GetKeymaster0PublicKey(blob));
222 if (!pkey)
223 return nullptr;
224
225 unique_ptr<EC_KEY, EC_Delete> public_ec_key(EVP_PKEY_get1_EC_KEY(pkey.get()));
226 if (!public_ec_key)
227 return nullptr;
228
229 if (!EC_KEY_set_group(ec_key.get(), EC_KEY_get0_group(public_ec_key.get())) ||
230 !EC_KEY_set_public_key(ec_key.get(), EC_KEY_get0_public_key(public_ec_key.get())))
231 return nullptr;
232
233 return ec_key.release();
234}
235
236const keymaster_key_blob_t* Keymaster0Engine::RsaKeyToBlob(const RSA* rsa) const {
Shawn Willdenac398062015-05-20 16:36:24 -0600237 return reinterpret_cast<keymaster_key_blob_t*>(RSA_get_ex_data(rsa, rsa_index_));
238}
239
Shawn Willden24bdfc22015-05-26 13:12:24 -0600240const keymaster_key_blob_t* Keymaster0Engine::EcKeyToBlob(const EC_KEY* ec_key) const {
241 return reinterpret_cast<keymaster_key_blob_t*>(EC_KEY_get_ex_data(ec_key, ec_key_index_));
242}
243
Shawn Willdenac398062015-05-20 16:36:24 -0600244/* static */
245int Keymaster0Engine::keyblob_dup(CRYPTO_EX_DATA* /* to */, const CRYPTO_EX_DATA* /* from */,
246 void** from_d, int /* index */, long /* argl */,
247 void* /* argp */) {
248 keymaster_key_blob_t* blob = reinterpret_cast<keymaster_key_blob_t*>(*from_d);
Shawn Willden24bdfc22015-05-26 13:12:24 -0600249 if (!blob)
250 return 1;
Shawn Willdenac398062015-05-20 16:36:24 -0600251 *from_d = duplicate_blob(*blob);
252 if (*from_d)
253 return 1;
254 return 0;
255}
256
257/* static */
258void Keymaster0Engine::keyblob_free(void* /* parent */, void* ptr, CRYPTO_EX_DATA* /* data */,
259 int /* index*/, long /* argl */, void* /* argp */) {
260 keymaster_key_blob_t* blob = reinterpret_cast<keymaster_key_blob_t*>(ptr);
261 if (blob) {
262 delete[] blob->key_material;
263 delete blob;
264 }
265}
266
267/* static */
268int Keymaster0Engine::rsa_private_transform(RSA* rsa, uint8_t* out, const uint8_t* in, size_t len) {
269 ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned)len);
270
271 assert(instance_);
272 return instance_->RsaPrivateTransform(rsa, out, in, len);
273}
274
Shawn Willden24bdfc22015-05-26 13:12:24 -0600275/* static */
276int Keymaster0Engine::ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
277 unsigned int* sig_len, EC_KEY* ec_key) {
278 ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned)digest_len, ec_key);
279 assert(instance_);
280 return instance_->EcdsaSign(digest, digest_len, sig, sig_len, ec_key);
281}
282
Shawn Willdenac398062015-05-20 16:36:24 -0600283bool Keymaster0Engine::Keymaster0Sign(const void* signing_params, const keymaster_key_blob_t& blob,
284 const uint8_t* data, const size_t data_length,
285 unique_ptr<uint8_t[], Malloc_Delete>* signature,
286 size_t* signature_length) const {
287 uint8_t* signed_data;
288 int err = keymaster0_device_->sign_data(keymaster0_device_, signing_params, blob.key_material,
289 blob.key_material_size, data, data_length, &signed_data,
290 signature_length);
291 if (err < 0) {
292 ALOGE("Keymaster0 signing failed with error %d", err);
293 return false;
294 }
295
296 signature->reset(signed_data);
297 return true;
298}
299
300EVP_PKEY* Keymaster0Engine::GetKeymaster0PublicKey(const KeymasterKeyBlob& blob) const {
301 uint8_t* pub_key_data;
302 size_t pub_key_data_length;
303 int err = keymaster0_device_->get_keypair_public(keymaster0_device_, blob.key_material,
304 blob.key_material_size, &pub_key_data,
305 &pub_key_data_length);
306 if (err < 0) {
307 ALOGE("Error %d extracting public key", err);
308 return nullptr;
309 }
310 unique_ptr<uint8_t, Malloc_Delete> pub_key(pub_key_data);
311
312 const uint8_t* p = pub_key_data;
313 return d2i_PUBKEY(nullptr /* allocate new struct */, &p, pub_key_data_length);
314}
315
316int Keymaster0Engine::RsaPrivateTransform(RSA* rsa, uint8_t* out, const uint8_t* in,
317 size_t len) const {
Shawn Willden24bdfc22015-05-26 13:12:24 -0600318 const keymaster_key_blob_t* key_blob = RsaKeyToBlob(rsa);
Shawn Willdenac398062015-05-20 16:36:24 -0600319 if (key_blob == NULL) {
320 ALOGE("key had no key_blob!");
321 return 0;
322 }
323
324 keymaster_rsa_sign_params_t sign_params = {DIGEST_NONE, PADDING_NONE};
325 unique_ptr<uint8_t[], Malloc_Delete> signature;
326 size_t signature_length;
327 if (!Keymaster0Sign(&sign_params, *key_blob, in, len, &signature, &signature_length))
328 return 0;
329 Eraser eraser(signature.get(), signature_length);
330
331 if (signature_length > len) {
332 /* The result of the RSA operation can never be larger than the size of
333 * the modulus so we assume that the result has extra zeros on the
334 * left. This provides attackers with an oracle, but there's nothing
335 * that we can do about it here. */
336 memcpy(out, signature.get() + signature_length - len, len);
337 } else if (signature_length < len) {
338 /* If the keymaster0 implementation returns a short value we assume that
339 * it's because it removed leading zeros from the left side. This is
340 * bad because it provides attackers with an oracle but we cannot do
341 * anything about a broken keymaster0 implementation here. */
342 memset(out, 0, len);
343 memcpy(out + len - signature_length, signature.get(), signature_length);
344 } else {
345 memcpy(out, signature.get(), len);
346 }
347
348 ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
349 return 1;
350}
351
Shawn Willden9a1cd6d2015-07-08 17:12:16 -0600352static size_t ec_group_size_bits(EC_KEY* ec_key) {
353 const EC_GROUP* group = EC_KEY_get0_group(ec_key);
354 unique_ptr<BN_CTX, BN_CTX_Delete> bn_ctx(BN_CTX_new());
355 unique_ptr<BIGNUM, BIGNUM_Delete> order(BN_new());
356 if (!EC_GROUP_get_order(group, order.get(), bn_ctx.get())) {
357 ALOGE("Failed to get EC group order");
358 return 0;
359 }
360 return BN_num_bits(order.get());
361}
362
Shawn Willden24bdfc22015-05-26 13:12:24 -0600363int Keymaster0Engine::EcdsaSign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
364 unsigned int* sig_len, EC_KEY* ec_key) const {
365 const keymaster_key_blob_t* key_blob = EcKeyToBlob(ec_key);
366 if (key_blob == NULL) {
367 ALOGE("key had no key_blob!");
368 return 0;
369 }
370
Shawn Willden9a1cd6d2015-07-08 17:12:16 -0600371 // Truncate digest if it's too long
372 size_t max_input_len = (ec_group_size_bits(ec_key) + 7) / 8;
373 if (digest_len > max_input_len)
374 digest_len = max_input_len;
375
Shawn Willden24bdfc22015-05-26 13:12:24 -0600376 keymaster_ec_sign_params_t sign_params = {DIGEST_NONE};
377 unique_ptr<uint8_t[], Malloc_Delete> signature;
378 size_t signature_length;
379 if (!Keymaster0Sign(&sign_params, *key_blob, digest, digest_len, &signature, &signature_length))
380 return 0;
381 Eraser eraser(signature.get(), signature_length);
382
383 if (signature_length == 0) {
384 ALOGW("No valid signature returned");
385 return 0;
386 } else if (signature_length > ECDSA_size(ec_key)) {
387 ALOGW("Signature is too large");
388 return 0;
389 } else {
390 memcpy(sig, signature.get(), signature_length);
391 *sig_len = signature_length;
392 }
393
394 ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len, ec_key);
395 return 1;
396}
397
Shawn Willdenac398062015-05-20 16:36:24 -0600398} // namespace keymaster