blob: 5a25b657dcb10df13331de9c43b8d4fb41a76b3b [file] [log] [blame]
Kenny Root70e3a862012-02-15 17:20:23 -08001/*
2 * Copyright (C) 2012 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#include <errno.h>
17#include <string.h>
18#include <stdint.h>
19
Kenny Root07438c82012-11-02 15:41:02 -070020#include <keystore/keystore.h>
Kenny Root822c3a92012-03-23 16:34:39 -070021
Kenny Root70e3a862012-02-15 17:20:23 -080022#include <hardware/hardware.h>
23#include <hardware/keymaster.h>
24
25#include <openssl/evp.h>
26#include <openssl/bio.h>
27#include <openssl/rsa.h>
28#include <openssl/err.h>
29#include <openssl/x509.h>
30
Kenny Root26cfc082013-09-11 14:38:56 -070031#include <UniquePtr.h>
Kenny Root70e3a862012-02-15 17:20:23 -080032
33// For debugging
Shawn Willden1406b8a2014-06-12 11:39:48 -060034// #define LOG_NDEBUG 0
Kenny Root70e3a862012-02-15 17:20:23 -080035
36#define LOG_TAG "OpenSSLKeyMaster"
37#include <cutils/log.h>
38
39struct BIGNUM_Delete {
40 void operator()(BIGNUM* p) const {
41 BN_free(p);
42 }
43};
44typedef UniquePtr<BIGNUM, BIGNUM_Delete> Unique_BIGNUM;
45
46struct EVP_PKEY_Delete {
47 void operator()(EVP_PKEY* p) const {
48 EVP_PKEY_free(p);
49 }
50};
51typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
52
53struct PKCS8_PRIV_KEY_INFO_Delete {
54 void operator()(PKCS8_PRIV_KEY_INFO* p) const {
55 PKCS8_PRIV_KEY_INFO_free(p);
56 }
57};
58typedef UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO;
59
Kenny Root60711792013-08-16 14:02:41 -070060struct DSA_Delete {
61 void operator()(DSA* p) const {
62 DSA_free(p);
63 }
64};
65typedef UniquePtr<DSA, DSA_Delete> Unique_DSA;
66
67struct EC_KEY_Delete {
68 void operator()(EC_KEY* p) const {
69 EC_KEY_free(p);
70 }
71};
72typedef UniquePtr<EC_KEY, EC_KEY_Delete> Unique_EC_KEY;
73
Kenny Root70e3a862012-02-15 17:20:23 -080074struct RSA_Delete {
75 void operator()(RSA* p) const {
76 RSA_free(p);
77 }
78};
79typedef UniquePtr<RSA, RSA_Delete> Unique_RSA;
80
Shawn Willden8d0531e2014-06-17 11:45:07 -060081struct Malloc_Free {
82 void operator()(void* p) const {
83 free(p);
84 }
85};
86
Kenny Root70e3a862012-02-15 17:20:23 -080087typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
88
89/**
Shawn Willden1406b8a2014-06-12 11:39:48 -060090 * Many OpenSSL APIs take ownership of an argument on success but
91 * don't free the argument on failure. This means we need to tell our
92 * scoped pointers when we've transferred ownership, without
93 * triggering a warning by not using the result of release().
Kenny Root70e3a862012-02-15 17:20:23 -080094 */
Shawn Willden2cd28fa2014-06-13 11:51:08 -060095template <typename T, typename Delete_T>
96inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) {
97 T* val __attribute__((unused)) = p.release();
98}
Kenny Root70e3a862012-02-15 17:20:23 -080099
100/*
101 * Checks this thread's OpenSSL error queue and logs if
102 * necessary.
103 */
104static void logOpenSSLError(const char* location) {
105 int error = ERR_get_error();
106
107 if (error != 0) {
108 char message[256];
109 ERR_error_string_n(error, message, sizeof(message));
110 ALOGE("OpenSSL error in %s %d: %s", location, error, message);
111 }
112
113 ERR_clear_error();
114 ERR_remove_state(0);
115}
116
117static int wrap_key(EVP_PKEY* pkey, int type, uint8_t** keyBlob, size_t* keyBlobLength) {
Kenny Root60711792013-08-16 14:02:41 -0700118 /*
Shawn Willden1406b8a2014-06-12 11:39:48 -0600119 * Find the length of each size. Public key is not needed anymore
120 * but must be kept for alignment purposes.
Kenny Root60711792013-08-16 14:02:41 -0700121 */
122 int publicLen = 0;
Kenny Root70e3a862012-02-15 17:20:23 -0800123 int privateLen = i2d_PrivateKey(pkey, NULL);
124
Kenny Root60711792013-08-16 14:02:41 -0700125 if (privateLen <= 0) {
126 ALOGE("private key size was too big");
Kenny Root70e3a862012-02-15 17:20:23 -0800127 return -1;
128 }
129
130 /* int type + int size + private key data + int size + public key data */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600131 *keyBlobLength = get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + privateLen +
132 sizeof(privateLen) + publicLen;
Kenny Root70e3a862012-02-15 17:20:23 -0800133
Shawn Willden8d0531e2014-06-17 11:45:07 -0600134 // derData will be returned to the caller, so allocate it with malloc.
135 UniquePtr<unsigned char, Malloc_Free> derData(
136 static_cast<unsigned char*>(malloc(*keyBlobLength)));
Kenny Root70e3a862012-02-15 17:20:23 -0800137 if (derData.get() == NULL) {
138 ALOGE("could not allocate memory for key blob");
139 return -1;
140 }
141 unsigned char* p = derData.get();
142
Kenny Root822c3a92012-03-23 16:34:39 -0700143 /* Write the magic value for software keys. */
144 p = add_softkey_header(p, *keyBlobLength);
145
Kenny Root70e3a862012-02-15 17:20:23 -0800146 /* Write key type to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600147 for (int i = sizeof(type) - 1; i >= 0; i--) {
148 *p++ = (type >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800149 }
150
151 /* Write public key to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600152 for (int i = sizeof(publicLen) - 1; i >= 0; i--) {
153 *p++ = (publicLen >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800154 }
Kenny Root70e3a862012-02-15 17:20:23 -0800155
156 /* Write private key to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600157 for (int i = sizeof(privateLen) - 1; i >= 0; i--) {
158 *p++ = (privateLen >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800159 }
160 if (i2d_PrivateKey(pkey, &p) != privateLen) {
161 logOpenSSLError("wrap_key");
162 return -1;
163 }
164
165 *keyBlob = derData.release();
166
167 return 0;
168}
169
170static EVP_PKEY* unwrap_key(const uint8_t* keyBlob, const size_t keyBlobLength) {
171 long publicLen = 0;
172 long privateLen = 0;
173 const uint8_t* p = keyBlob;
Shawn Willden1406b8a2014-06-12 11:39:48 -0600174 const uint8_t* const end = keyBlob + keyBlobLength;
Kenny Root70e3a862012-02-15 17:20:23 -0800175
176 if (keyBlob == NULL) {
177 ALOGE("supplied key blob was NULL");
178 return NULL;
179 }
180
Shawn Willden1406b8a2014-06-12 11:39:48 -0600181 int type = 0;
182 if (keyBlobLength < (get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + 1 +
183 sizeof(privateLen) + 1)) {
Kenny Root70e3a862012-02-15 17:20:23 -0800184 ALOGE("key blob appears to be truncated");
185 return NULL;
186 }
187
Kenny Root822c3a92012-03-23 16:34:39 -0700188 if (!is_softkey(p, keyBlobLength)) {
189 ALOGE("cannot read key; it was not made by this keymaster");
190 return NULL;
191 }
192 p += get_softkey_header_size();
193
Shawn Willden1406b8a2014-06-12 11:39:48 -0600194 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800195 type = (type << 8) | *p++;
196 }
197
Shawn Willden1406b8a2014-06-12 11:39:48 -0600198 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800199 publicLen = (publicLen << 8) | *p++;
200 }
201 if (p + publicLen > end) {
Matteo Franchin6489e022013-12-02 14:46:29 +0000202 ALOGE("public key length encoding error: size=%ld, end=%td", publicLen, end - p);
Kenny Root70e3a862012-02-15 17:20:23 -0800203 return NULL;
204 }
Kenny Root70e3a862012-02-15 17:20:23 -0800205
Kenny Root60711792013-08-16 14:02:41 -0700206 p += publicLen;
Kenny Root70e3a862012-02-15 17:20:23 -0800207 if (end - p < 2) {
208 ALOGE("private key truncated");
209 return NULL;
210 }
Shawn Willden1406b8a2014-06-12 11:39:48 -0600211 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800212 privateLen = (privateLen << 8) | *p++;
213 }
214 if (p + privateLen > end) {
Matteo Franchin6489e022013-12-02 14:46:29 +0000215 ALOGE("private key length encoding error: size=%ld, end=%td", privateLen, end - p);
Kenny Root70e3a862012-02-15 17:20:23 -0800216 return NULL;
217 }
Kenny Root60711792013-08-16 14:02:41 -0700218
219 Unique_EVP_PKEY pkey(EVP_PKEY_new());
220 if (pkey.get() == NULL) {
221 logOpenSSLError("unwrap_key");
222 return NULL;
223 }
224 EVP_PKEY* tmp = pkey.get();
225
226 if (d2i_PrivateKey(type, &tmp, &p, privateLen) == NULL) {
227 logOpenSSLError("unwrap_key");
228 return NULL;
229 }
Kenny Root70e3a862012-02-15 17:20:23 -0800230
231 return pkey.release();
232}
233
Shawn Willden1406b8a2014-06-12 11:39:48 -0600234static int generate_dsa_keypair(EVP_PKEY* pkey, const keymaster_dsa_keygen_params_t* dsa_params) {
Kenny Root60711792013-08-16 14:02:41 -0700235 if (dsa_params->key_size < 512) {
236 ALOGI("Requested DSA key size is too small (<512)");
237 return -1;
238 }
239
240 Unique_DSA dsa(DSA_new());
241
Shawn Willden1406b8a2014-06-12 11:39:48 -0600242 if (dsa_params->generator_len == 0 || dsa_params->prime_p_len == 0 ||
243 dsa_params->prime_q_len == 0 || dsa_params->generator == NULL ||
244 dsa_params->prime_p == NULL || dsa_params->prime_q == NULL) {
Kenny Root60711792013-08-16 14:02:41 -0700245 if (DSA_generate_parameters_ex(dsa.get(), dsa_params->key_size, NULL, 0, NULL, NULL,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600246 NULL) != 1) {
Kenny Root60711792013-08-16 14:02:41 -0700247 logOpenSSLError("generate_dsa_keypair");
248 return -1;
249 }
250 } else {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600251 dsa->g = BN_bin2bn(dsa_params->generator, dsa_params->generator_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700252 if (dsa->g == NULL) {
253 logOpenSSLError("generate_dsa_keypair");
254 return -1;
255 }
256
Shawn Willden1406b8a2014-06-12 11:39:48 -0600257 dsa->p = BN_bin2bn(dsa_params->prime_p, dsa_params->prime_p_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700258 if (dsa->p == NULL) {
259 logOpenSSLError("generate_dsa_keypair");
260 return -1;
261 }
262
Shawn Willden1406b8a2014-06-12 11:39:48 -0600263 dsa->q = BN_bin2bn(dsa_params->prime_q, dsa_params->prime_q_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700264 if (dsa->q == NULL) {
265 logOpenSSLError("generate_dsa_keypair");
266 return -1;
267 }
268 }
269
270 if (DSA_generate_key(dsa.get()) != 1) {
271 logOpenSSLError("generate_dsa_keypair");
272 return -1;
273 }
274
275 if (EVP_PKEY_assign_DSA(pkey, dsa.get()) == 0) {
276 logOpenSSLError("generate_dsa_keypair");
277 return -1;
278 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600279 release_because_ownership_transferred(dsa);
Kenny Root60711792013-08-16 14:02:41 -0700280
281 return 0;
282}
283
Shawn Willden1406b8a2014-06-12 11:39:48 -0600284static int generate_ec_keypair(EVP_PKEY* pkey, const keymaster_ec_keygen_params_t* ec_params) {
Kenny Root60711792013-08-16 14:02:41 -0700285 EC_GROUP* group;
286 switch (ec_params->field_size) {
287 case 192:
288 group = EC_GROUP_new_by_curve_name(NID_X9_62_prime192v1);
289 break;
290 case 224:
291 group = EC_GROUP_new_by_curve_name(NID_secp224r1);
292 break;
293 case 256:
294 group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
295 break;
296 case 384:
297 group = EC_GROUP_new_by_curve_name(NID_secp384r1);
298 break;
299 case 521:
300 group = EC_GROUP_new_by_curve_name(NID_secp521r1);
301 break;
302 default:
303 group = NULL;
304 break;
305 }
306
307 if (group == NULL) {
308 logOpenSSLError("generate_ec_keypair");
309 return -1;
310 }
311
312 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
313 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
314
315 /* initialize EC key */
316 Unique_EC_KEY eckey(EC_KEY_new());
317 if (eckey.get() == NULL) {
318 logOpenSSLError("generate_ec_keypair");
319 return -1;
320 }
321
322 if (EC_KEY_set_group(eckey.get(), group) != 1) {
323 logOpenSSLError("generate_ec_keypair");
324 return -1;
325 }
326
Shawn Willden1406b8a2014-06-12 11:39:48 -0600327 if (EC_KEY_generate_key(eckey.get()) != 1 || EC_KEY_check_key(eckey.get()) < 0) {
Kenny Root60711792013-08-16 14:02:41 -0700328 logOpenSSLError("generate_ec_keypair");
329 return -1;
330 }
331
332 if (EVP_PKEY_assign_EC_KEY(pkey, eckey.get()) == 0) {
333 logOpenSSLError("generate_ec_keypair");
334 return -1;
335 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600336 release_because_ownership_transferred(eckey);
Kenny Root60711792013-08-16 14:02:41 -0700337
338 return 0;
339}
340
Shawn Willden1406b8a2014-06-12 11:39:48 -0600341static int generate_rsa_keypair(EVP_PKEY* pkey, const keymaster_rsa_keygen_params_t* rsa_params) {
Kenny Root60711792013-08-16 14:02:41 -0700342 Unique_BIGNUM bn(BN_new());
343 if (bn.get() == NULL) {
344 logOpenSSLError("generate_rsa_keypair");
345 return -1;
346 }
347
348 if (BN_set_word(bn.get(), rsa_params->public_exponent) == 0) {
349 logOpenSSLError("generate_rsa_keypair");
350 return -1;
351 }
352
353 /* initialize RSA */
354 Unique_RSA rsa(RSA_new());
355 if (rsa.get() == NULL) {
356 logOpenSSLError("generate_rsa_keypair");
357 return -1;
358 }
359
Shawn Willden1406b8a2014-06-12 11:39:48 -0600360 if (!RSA_generate_key_ex(rsa.get(), rsa_params->modulus_size, bn.get(), NULL) ||
361 RSA_check_key(rsa.get()) < 0) {
Kenny Root60711792013-08-16 14:02:41 -0700362 logOpenSSLError("generate_rsa_keypair");
363 return -1;
364 }
365
366 if (EVP_PKEY_assign_RSA(pkey, rsa.get()) == 0) {
367 logOpenSSLError("generate_rsa_keypair");
368 return -1;
369 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600370 release_because_ownership_transferred(rsa);
Kenny Root60711792013-08-16 14:02:41 -0700371
372 return 0;
373}
374
Shawn Willden1406b8a2014-06-12 11:39:48 -0600375__attribute__((visibility("default"))) int openssl_generate_keypair(
376 const keymaster_device_t*, const keymaster_keypair_t key_type, const void* key_params,
377 uint8_t** keyBlob, size_t* keyBlobLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800378 Unique_EVP_PKEY pkey(EVP_PKEY_new());
379 if (pkey.get() == NULL) {
380 logOpenSSLError("openssl_generate_keypair");
381 return -1;
382 }
383
Kenny Root60711792013-08-16 14:02:41 -0700384 if (key_params == NULL) {
385 ALOGW("key_params == null");
386 return -1;
387 } else if (key_type == TYPE_DSA) {
388 const keymaster_dsa_keygen_params_t* dsa_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600389 (const keymaster_dsa_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700390 generate_dsa_keypair(pkey.get(), dsa_params);
391 } else if (key_type == TYPE_EC) {
392 const keymaster_ec_keygen_params_t* ec_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600393 (const keymaster_ec_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700394 generate_ec_keypair(pkey.get(), ec_params);
395 } else if (key_type == TYPE_RSA) {
396 const keymaster_rsa_keygen_params_t* rsa_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600397 (const keymaster_rsa_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700398 generate_rsa_keypair(pkey.get(), rsa_params);
399 } else {
400 ALOGW("Unsupported key type %d", key_type);
Kenny Root70e3a862012-02-15 17:20:23 -0800401 return -1;
402 }
Kenny Root70e3a862012-02-15 17:20:23 -0800403
Kenny Root60711792013-08-16 14:02:41 -0700404 if (wrap_key(pkey.get(), EVP_PKEY_type(pkey->type), keyBlob, keyBlobLength)) {
Kenny Root70e3a862012-02-15 17:20:23 -0800405 return -1;
406 }
407
408 return 0;
409}
410
Shawn Willden1406b8a2014-06-12 11:39:48 -0600411__attribute__((visibility("default"))) int openssl_import_keypair(const keymaster_device_t*,
412 const uint8_t* key,
413 const size_t key_length,
414 uint8_t** key_blob,
415 size_t* key_blob_length) {
Kenny Root70e3a862012-02-15 17:20:23 -0800416 if (key == NULL) {
417 ALOGW("input key == NULL");
418 return -1;
419 } else if (key_blob == NULL || key_blob_length == NULL) {
420 ALOGW("output key blob or length == NULL");
421 return -1;
422 }
423
424 Unique_PKCS8_PRIV_KEY_INFO pkcs8(d2i_PKCS8_PRIV_KEY_INFO(NULL, &key, key_length));
425 if (pkcs8.get() == NULL) {
426 logOpenSSLError("openssl_import_keypair");
427 return -1;
428 }
429
430 /* assign to EVP */
431 Unique_EVP_PKEY pkey(EVP_PKCS82PKEY(pkcs8.get()));
432 if (pkey.get() == NULL) {
433 logOpenSSLError("openssl_import_keypair");
434 return -1;
435 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600436 release_because_ownership_transferred(pkcs8);
Kenny Root70e3a862012-02-15 17:20:23 -0800437
438 if (wrap_key(pkey.get(), EVP_PKEY_type(pkey->type), key_blob, key_blob_length)) {
439 return -1;
440 }
441
442 return 0;
443}
444
Shawn Willden1406b8a2014-06-12 11:39:48 -0600445__attribute__((visibility("default"))) int openssl_get_keypair_public(
446 const struct keymaster_device*, const uint8_t* key_blob, const size_t key_blob_length,
447 uint8_t** x509_data, size_t* x509_data_length) {
Kenny Root70e3a862012-02-15 17:20:23 -0800448
449 if (x509_data == NULL || x509_data_length == NULL) {
450 ALOGW("output public key buffer == NULL");
451 return -1;
452 }
453
454 Unique_EVP_PKEY pkey(unwrap_key(key_blob, key_blob_length));
455 if (pkey.get() == NULL) {
456 return -1;
457 }
458
459 int len = i2d_PUBKEY(pkey.get(), NULL);
460 if (len <= 0) {
461 logOpenSSLError("openssl_get_keypair_public");
462 return -1;
463 }
464
Shawn Willden8d0531e2014-06-17 11:45:07 -0600465 UniquePtr<uint8_t, Malloc_Free> key(static_cast<uint8_t*>(malloc(len)));
Kenny Root70e3a862012-02-15 17:20:23 -0800466 if (key.get() == NULL) {
467 ALOGE("Could not allocate memory for public key data");
468 return -1;
469 }
470
471 unsigned char* tmp = reinterpret_cast<unsigned char*>(key.get());
472 if (i2d_PUBKEY(pkey.get(), &tmp) != len) {
473 logOpenSSLError("openssl_get_keypair_public");
474 return -1;
475 }
476
477 ALOGV("Length of x509 data is %d", len);
478 *x509_data_length = len;
479 *x509_data = key.release();
480
481 return 0;
482}
483
Kenny Root60711792013-08-16 14:02:41 -0700484static int sign_dsa(EVP_PKEY* pkey, keymaster_dsa_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600485 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700486 if (sign_params->digest_type != DIGEST_NONE) {
487 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
488 return -1;
489 }
490
491 Unique_DSA dsa(EVP_PKEY_get1_DSA(pkey));
492 if (dsa.get() == NULL) {
493 logOpenSSLError("openssl_sign_dsa");
494 return -1;
495 }
496
497 unsigned int dsaSize = DSA_size(dsa.get());
Shawn Willden8d0531e2014-06-17 11:45:07 -0600498 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
Kenny Root60711792013-08-16 14:02:41 -0700499 if (signedDataPtr.get() == NULL) {
500 logOpenSSLError("openssl_sign_dsa");
501 return -1;
502 }
503
504 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
505 if (DSA_sign(0, data, dataLength, tmp, &dsaSize, dsa.get()) <= 0) {
506 logOpenSSLError("openssl_sign_dsa");
507 return -1;
508 }
509
510 *signedDataLength = dsaSize;
511 *signedData = signedDataPtr.release();
512
513 return 0;
514}
515
516static int sign_ec(EVP_PKEY* pkey, keymaster_ec_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600517 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700518 if (sign_params->digest_type != DIGEST_NONE) {
519 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
520 return -1;
521 }
522
523 Unique_EC_KEY eckey(EVP_PKEY_get1_EC_KEY(pkey));
524 if (eckey.get() == NULL) {
525 logOpenSSLError("openssl_sign_ec");
526 return -1;
527 }
528
529 unsigned int ecdsaSize = ECDSA_size(eckey.get());
Shawn Willden8d0531e2014-06-17 11:45:07 -0600530 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
Kenny Root60711792013-08-16 14:02:41 -0700531 if (signedDataPtr.get() == NULL) {
532 logOpenSSLError("openssl_sign_ec");
533 return -1;
534 }
535
536 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
537 if (ECDSA_sign(0, data, dataLength, tmp, &ecdsaSize, eckey.get()) <= 0) {
538 logOpenSSLError("openssl_sign_ec");
539 return -1;
540 }
541
542 *signedDataLength = ecdsaSize;
543 *signedData = signedDataPtr.release();
544
545 return 0;
546}
547
Kenny Root60711792013-08-16 14:02:41 -0700548static int sign_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600549 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700550 if (sign_params->digest_type != DIGEST_NONE) {
551 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
552 return -1;
553 } else if (sign_params->padding_type != PADDING_NONE) {
554 ALOGW("Cannot handle padding type %d", sign_params->padding_type);
555 return -1;
556 }
557
558 Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey));
559 if (rsa.get() == NULL) {
560 logOpenSSLError("openssl_sign_rsa");
561 return -1;
562 }
563
Shawn Willden8d0531e2014-06-17 11:45:07 -0600564 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
Kenny Root60711792013-08-16 14:02:41 -0700565 if (signedDataPtr.get() == NULL) {
566 logOpenSSLError("openssl_sign_rsa");
567 return -1;
568 }
569
570 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
571 if (RSA_private_encrypt(dataLength, data, tmp, rsa.get(), RSA_NO_PADDING) <= 0) {
572 logOpenSSLError("openssl_sign_rsa");
573 return -1;
574 }
575
576 *signedDataLength = dataLength;
577 *signedData = signedDataPtr.release();
578
579 return 0;
580}
581
Shawn Willden1406b8a2014-06-12 11:39:48 -0600582__attribute__((visibility("default"))) int openssl_sign_data(
583 const keymaster_device_t*, const void* params, const uint8_t* keyBlob,
584 const size_t keyBlobLength, const uint8_t* data, const size_t dataLength, uint8_t** signedData,
585 size_t* signedDataLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800586 if (data == NULL) {
587 ALOGW("input data to sign == NULL");
588 return -1;
589 } else if (signedData == NULL || signedDataLength == NULL) {
590 ALOGW("output signature buffer == NULL");
591 return -1;
592 }
593
594 Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
595 if (pkey.get() == NULL) {
596 return -1;
597 }
598
Kenny Root60711792013-08-16 14:02:41 -0700599 int type = EVP_PKEY_type(pkey->type);
600 if (type == EVP_PKEY_DSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600601 const keymaster_dsa_sign_params_t* sign_params =
602 reinterpret_cast<const keymaster_dsa_sign_params_t*>(params);
603 return sign_dsa(pkey.get(), const_cast<keymaster_dsa_sign_params_t*>(sign_params), data,
604 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700605 } else if (type == EVP_PKEY_EC) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600606 const keymaster_ec_sign_params_t* sign_params =
607 reinterpret_cast<const keymaster_ec_sign_params_t*>(params);
608 return sign_ec(pkey.get(), const_cast<keymaster_ec_sign_params_t*>(sign_params), data,
609 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700610 } else if (type == EVP_PKEY_RSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600611 const keymaster_rsa_sign_params_t* sign_params =
612 reinterpret_cast<const keymaster_rsa_sign_params_t*>(params);
613 return sign_rsa(pkey.get(), const_cast<keymaster_rsa_sign_params_t*>(sign_params), data,
614 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700615 } else {
616 ALOGW("Unsupported key type");
Kenny Root70e3a862012-02-15 17:20:23 -0800617 return -1;
618 }
Kenny Root60711792013-08-16 14:02:41 -0700619}
Kenny Root70e3a862012-02-15 17:20:23 -0800620
Kenny Root60711792013-08-16 14:02:41 -0700621static int verify_dsa(EVP_PKEY* pkey, keymaster_dsa_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600622 const uint8_t* signedData, const size_t signedDataLength,
623 const uint8_t* signature, const size_t signatureLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800624 if (sign_params->digest_type != DIGEST_NONE) {
625 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
626 return -1;
Kenny Root60711792013-08-16 14:02:41 -0700627 }
628
629 Unique_DSA dsa(EVP_PKEY_get1_DSA(pkey));
630 if (dsa.get() == NULL) {
631 logOpenSSLError("openssl_verify_dsa");
Kenny Root70e3a862012-02-15 17:20:23 -0800632 return -1;
633 }
634
Kenny Root60711792013-08-16 14:02:41 -0700635 if (DSA_verify(0, signedData, signedDataLength, signature, signatureLength, dsa.get()) <= 0) {
636 logOpenSSLError("openssl_verify_dsa");
Kenny Root70e3a862012-02-15 17:20:23 -0800637 return -1;
638 }
639
Kenny Root70e3a862012-02-15 17:20:23 -0800640 return 0;
641}
642
Kenny Root60711792013-08-16 14:02:41 -0700643static int verify_ec(EVP_PKEY* pkey, keymaster_ec_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600644 const uint8_t* signedData, const size_t signedDataLength,
645 const uint8_t* signature, const size_t signatureLength) {
Kenny Root60711792013-08-16 14:02:41 -0700646 if (sign_params->digest_type != DIGEST_NONE) {
647 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
Kenny Root70e3a862012-02-15 17:20:23 -0800648 return -1;
649 }
650
Kenny Root60711792013-08-16 14:02:41 -0700651 Unique_EC_KEY eckey(EVP_PKEY_get1_EC_KEY(pkey));
652 if (eckey.get() == NULL) {
653 logOpenSSLError("openssl_verify_ec");
Kenny Root70e3a862012-02-15 17:20:23 -0800654 return -1;
655 }
656
Shawn Willden1406b8a2014-06-12 11:39:48 -0600657 if (ECDSA_verify(0, signedData, signedDataLength, signature, signatureLength, eckey.get()) <=
658 0) {
Kenny Root60711792013-08-16 14:02:41 -0700659 logOpenSSLError("openssl_verify_ec");
Kenny Root70e3a862012-02-15 17:20:23 -0800660 return -1;
661 }
662
Kenny Root60711792013-08-16 14:02:41 -0700663 return 0;
664}
665
666static int verify_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600667 const uint8_t* signedData, const size_t signedDataLength,
668 const uint8_t* signature, const size_t signatureLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800669 if (sign_params->digest_type != DIGEST_NONE) {
670 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
671 return -1;
672 } else if (sign_params->padding_type != PADDING_NONE) {
673 ALOGW("Cannot handle padding type %d", sign_params->padding_type);
674 return -1;
675 } else if (signatureLength != signedDataLength) {
676 ALOGW("signed data length must be signature length");
677 return -1;
678 }
679
Kenny Root60711792013-08-16 14:02:41 -0700680 Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey));
Kenny Root70e3a862012-02-15 17:20:23 -0800681 if (rsa.get() == NULL) {
682 logOpenSSLError("openssl_verify_data");
683 return -1;
684 }
685
Shawn Willden8d0531e2014-06-17 11:45:07 -0600686 UniquePtr<uint8_t[]> dataPtr(new uint8_t[signedDataLength]);
Kenny Root70e3a862012-02-15 17:20:23 -0800687 if (dataPtr.get() == NULL) {
688 logOpenSSLError("openssl_verify_data");
689 return -1;
690 }
691
692 unsigned char* tmp = reinterpret_cast<unsigned char*>(dataPtr.get());
693 if (!RSA_public_decrypt(signatureLength, signature, tmp, rsa.get(), RSA_NO_PADDING)) {
694 logOpenSSLError("openssl_verify_data");
695 return -1;
696 }
697
698 int result = 0;
699 for (size_t i = 0; i < signedDataLength; i++) {
700 result |= tmp[i] ^ signedData[i];
701 }
702
703 return result == 0 ? 0 : -1;
704}
705
Shawn Willden1406b8a2014-06-12 11:39:48 -0600706__attribute__((visibility("default"))) int openssl_verify_data(
707 const keymaster_device_t*, const void* params, const uint8_t* keyBlob,
708 const size_t keyBlobLength, const uint8_t* signedData, const size_t signedDataLength,
709 const uint8_t* signature, const size_t signatureLength) {
Kenny Root60711792013-08-16 14:02:41 -0700710
711 if (signedData == NULL || signature == NULL) {
712 ALOGW("data or signature buffers == NULL");
713 return -1;
714 }
715
716 Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
717 if (pkey.get() == NULL) {
718 return -1;
719 }
720
721 int type = EVP_PKEY_type(pkey->type);
Kenny Rootb4d2e022013-09-04 13:56:03 -0700722 if (type == EVP_PKEY_DSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600723 const keymaster_dsa_sign_params_t* sign_params =
724 reinterpret_cast<const keymaster_dsa_sign_params_t*>(params);
725 return verify_dsa(pkey.get(), const_cast<keymaster_dsa_sign_params_t*>(sign_params),
726 signedData, signedDataLength, signature, signatureLength);
Kenny Rootb4d2e022013-09-04 13:56:03 -0700727 } else if (type == EVP_PKEY_RSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600728 const keymaster_rsa_sign_params_t* sign_params =
729 reinterpret_cast<const keymaster_rsa_sign_params_t*>(params);
730 return verify_rsa(pkey.get(), const_cast<keymaster_rsa_sign_params_t*>(sign_params),
731 signedData, signedDataLength, signature, signatureLength);
Kenny Root60711792013-08-16 14:02:41 -0700732 } else if (type == EVP_PKEY_EC) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600733 const keymaster_ec_sign_params_t* sign_params =
734 reinterpret_cast<const keymaster_ec_sign_params_t*>(params);
735 return verify_ec(pkey.get(), const_cast<keymaster_ec_sign_params_t*>(sign_params),
736 signedData, signedDataLength, signature, signatureLength);
Kenny Root60711792013-08-16 14:02:41 -0700737 } else {
738 ALOGW("Unsupported key type %d", type);
739 return -1;
740 }
741}