blob: 5864310b0717b796bc0fb1d39da349e7d0239857 [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
Shawn Willden18a00e12014-06-17 10:50:03 -060074struct EC_GROUP_Delete {
75 void operator()(EC_GROUP* p) const {
76 EC_GROUP_free(p);
77 }
78};
79typedef UniquePtr<EC_GROUP, EC_GROUP_Delete> Unique_EC_GROUP;
80
Kenny Root70e3a862012-02-15 17:20:23 -080081struct RSA_Delete {
82 void operator()(RSA* p) const {
83 RSA_free(p);
84 }
85};
86typedef UniquePtr<RSA, RSA_Delete> Unique_RSA;
87
Shawn Willden8d0531e2014-06-17 11:45:07 -060088struct Malloc_Free {
89 void operator()(void* p) const {
90 free(p);
91 }
92};
93
Kenny Root70e3a862012-02-15 17:20:23 -080094typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
95
96/**
Shawn Willden1406b8a2014-06-12 11:39:48 -060097 * Many OpenSSL APIs take ownership of an argument on success but
98 * don't free the argument on failure. This means we need to tell our
99 * scoped pointers when we've transferred ownership, without
100 * triggering a warning by not using the result of release().
Kenny Root70e3a862012-02-15 17:20:23 -0800101 */
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600102template <typename T, typename Delete_T>
103inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) {
104 T* val __attribute__((unused)) = p.release();
105}
Kenny Root70e3a862012-02-15 17:20:23 -0800106
107/*
108 * Checks this thread's OpenSSL error queue and logs if
109 * necessary.
110 */
111static void logOpenSSLError(const char* location) {
112 int error = ERR_get_error();
113
114 if (error != 0) {
115 char message[256];
116 ERR_error_string_n(error, message, sizeof(message));
117 ALOGE("OpenSSL error in %s %d: %s", location, error, message);
118 }
119
120 ERR_clear_error();
Adam Langley53d13c52014-09-23 17:40:53 -0700121 ERR_remove_thread_state(NULL);
Kenny Root70e3a862012-02-15 17:20:23 -0800122}
123
124static int wrap_key(EVP_PKEY* pkey, int type, uint8_t** keyBlob, size_t* keyBlobLength) {
Kenny Root60711792013-08-16 14:02:41 -0700125 /*
Shawn Willden1406b8a2014-06-12 11:39:48 -0600126 * Find the length of each size. Public key is not needed anymore
127 * but must be kept for alignment purposes.
Kenny Root60711792013-08-16 14:02:41 -0700128 */
129 int publicLen = 0;
Kenny Root70e3a862012-02-15 17:20:23 -0800130 int privateLen = i2d_PrivateKey(pkey, NULL);
131
Kenny Root60711792013-08-16 14:02:41 -0700132 if (privateLen <= 0) {
133 ALOGE("private key size was too big");
Kenny Root70e3a862012-02-15 17:20:23 -0800134 return -1;
135 }
136
137 /* int type + int size + private key data + int size + public key data */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600138 *keyBlobLength = get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + privateLen +
139 sizeof(privateLen) + publicLen;
Kenny Root70e3a862012-02-15 17:20:23 -0800140
Shawn Willden8d0531e2014-06-17 11:45:07 -0600141 // derData will be returned to the caller, so allocate it with malloc.
142 UniquePtr<unsigned char, Malloc_Free> derData(
143 static_cast<unsigned char*>(malloc(*keyBlobLength)));
Kenny Root70e3a862012-02-15 17:20:23 -0800144 if (derData.get() == NULL) {
145 ALOGE("could not allocate memory for key blob");
146 return -1;
147 }
148 unsigned char* p = derData.get();
149
Kenny Root822c3a92012-03-23 16:34:39 -0700150 /* Write the magic value for software keys. */
151 p = add_softkey_header(p, *keyBlobLength);
152
Kenny Root70e3a862012-02-15 17:20:23 -0800153 /* Write key type to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600154 for (int i = sizeof(type) - 1; i >= 0; i--) {
155 *p++ = (type >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800156 }
157
158 /* Write public key to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600159 for (int i = sizeof(publicLen) - 1; i >= 0; i--) {
160 *p++ = (publicLen >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800161 }
Kenny Root70e3a862012-02-15 17:20:23 -0800162
163 /* Write private key to allocated buffer */
Shawn Willden1406b8a2014-06-12 11:39:48 -0600164 for (int i = sizeof(privateLen) - 1; i >= 0; i--) {
165 *p++ = (privateLen >> (8 * i)) & 0xFF;
Kenny Root70e3a862012-02-15 17:20:23 -0800166 }
167 if (i2d_PrivateKey(pkey, &p) != privateLen) {
168 logOpenSSLError("wrap_key");
169 return -1;
170 }
171
172 *keyBlob = derData.release();
173
174 return 0;
175}
176
177static EVP_PKEY* unwrap_key(const uint8_t* keyBlob, const size_t keyBlobLength) {
178 long publicLen = 0;
179 long privateLen = 0;
180 const uint8_t* p = keyBlob;
Shawn Willden1406b8a2014-06-12 11:39:48 -0600181 const uint8_t* const end = keyBlob + keyBlobLength;
Kenny Root70e3a862012-02-15 17:20:23 -0800182
183 if (keyBlob == NULL) {
184 ALOGE("supplied key blob was NULL");
185 return NULL;
186 }
187
Shawn Willden1406b8a2014-06-12 11:39:48 -0600188 int type = 0;
189 if (keyBlobLength < (get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + 1 +
190 sizeof(privateLen) + 1)) {
Kenny Root70e3a862012-02-15 17:20:23 -0800191 ALOGE("key blob appears to be truncated");
192 return NULL;
193 }
194
Kenny Root822c3a92012-03-23 16:34:39 -0700195 if (!is_softkey(p, keyBlobLength)) {
196 ALOGE("cannot read key; it was not made by this keymaster");
197 return NULL;
198 }
199 p += get_softkey_header_size();
200
Shawn Willden1406b8a2014-06-12 11:39:48 -0600201 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800202 type = (type << 8) | *p++;
203 }
204
Shawn Willden1406b8a2014-06-12 11:39:48 -0600205 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800206 publicLen = (publicLen << 8) | *p++;
207 }
208 if (p + publicLen > end) {
Matteo Franchin6489e022013-12-02 14:46:29 +0000209 ALOGE("public key length encoding error: size=%ld, end=%td", publicLen, end - p);
Kenny Root70e3a862012-02-15 17:20:23 -0800210 return NULL;
211 }
Kenny Root70e3a862012-02-15 17:20:23 -0800212
Kenny Root60711792013-08-16 14:02:41 -0700213 p += publicLen;
Kenny Root70e3a862012-02-15 17:20:23 -0800214 if (end - p < 2) {
215 ALOGE("private key truncated");
216 return NULL;
217 }
Shawn Willden1406b8a2014-06-12 11:39:48 -0600218 for (size_t i = 0; i < sizeof(type); i++) {
Kenny Root70e3a862012-02-15 17:20:23 -0800219 privateLen = (privateLen << 8) | *p++;
220 }
221 if (p + privateLen > end) {
Matteo Franchin6489e022013-12-02 14:46:29 +0000222 ALOGE("private key length encoding error: size=%ld, end=%td", privateLen, end - p);
Kenny Root70e3a862012-02-15 17:20:23 -0800223 return NULL;
224 }
Kenny Root60711792013-08-16 14:02:41 -0700225
226 Unique_EVP_PKEY pkey(EVP_PKEY_new());
227 if (pkey.get() == NULL) {
228 logOpenSSLError("unwrap_key");
229 return NULL;
230 }
231 EVP_PKEY* tmp = pkey.get();
232
233 if (d2i_PrivateKey(type, &tmp, &p, privateLen) == NULL) {
234 logOpenSSLError("unwrap_key");
235 return NULL;
236 }
Kenny Root70e3a862012-02-15 17:20:23 -0800237
238 return pkey.release();
239}
240
Shawn Willden1406b8a2014-06-12 11:39:48 -0600241static int generate_dsa_keypair(EVP_PKEY* pkey, const keymaster_dsa_keygen_params_t* dsa_params) {
Kenny Root60711792013-08-16 14:02:41 -0700242 if (dsa_params->key_size < 512) {
243 ALOGI("Requested DSA key size is too small (<512)");
244 return -1;
245 }
246
247 Unique_DSA dsa(DSA_new());
248
Shawn Willden1406b8a2014-06-12 11:39:48 -0600249 if (dsa_params->generator_len == 0 || dsa_params->prime_p_len == 0 ||
250 dsa_params->prime_q_len == 0 || dsa_params->generator == NULL ||
251 dsa_params->prime_p == NULL || dsa_params->prime_q == NULL) {
Kenny Root60711792013-08-16 14:02:41 -0700252 if (DSA_generate_parameters_ex(dsa.get(), dsa_params->key_size, NULL, 0, NULL, NULL,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600253 NULL) != 1) {
Kenny Root60711792013-08-16 14:02:41 -0700254 logOpenSSLError("generate_dsa_keypair");
255 return -1;
256 }
257 } else {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600258 dsa->g = BN_bin2bn(dsa_params->generator, dsa_params->generator_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700259 if (dsa->g == NULL) {
260 logOpenSSLError("generate_dsa_keypair");
261 return -1;
262 }
263
Shawn Willden1406b8a2014-06-12 11:39:48 -0600264 dsa->p = BN_bin2bn(dsa_params->prime_p, dsa_params->prime_p_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700265 if (dsa->p == NULL) {
266 logOpenSSLError("generate_dsa_keypair");
267 return -1;
268 }
269
Shawn Willden1406b8a2014-06-12 11:39:48 -0600270 dsa->q = BN_bin2bn(dsa_params->prime_q, dsa_params->prime_q_len, NULL);
Kenny Root60711792013-08-16 14:02:41 -0700271 if (dsa->q == NULL) {
272 logOpenSSLError("generate_dsa_keypair");
273 return -1;
274 }
275 }
276
277 if (DSA_generate_key(dsa.get()) != 1) {
278 logOpenSSLError("generate_dsa_keypair");
279 return -1;
280 }
281
282 if (EVP_PKEY_assign_DSA(pkey, dsa.get()) == 0) {
283 logOpenSSLError("generate_dsa_keypair");
284 return -1;
285 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600286 release_because_ownership_transferred(dsa);
Kenny Root60711792013-08-16 14:02:41 -0700287
288 return 0;
289}
290
Shawn Willden1406b8a2014-06-12 11:39:48 -0600291static int generate_ec_keypair(EVP_PKEY* pkey, const keymaster_ec_keygen_params_t* ec_params) {
Shawn Willden18a00e12014-06-17 10:50:03 -0600292 Unique_EC_GROUP group;
Kenny Root60711792013-08-16 14:02:41 -0700293 switch (ec_params->field_size) {
Kenny Root60711792013-08-16 14:02:41 -0700294 case 224:
Shawn Willden18a00e12014-06-17 10:50:03 -0600295 group.reset(EC_GROUP_new_by_curve_name(NID_secp224r1));
Kenny Root60711792013-08-16 14:02:41 -0700296 break;
297 case 256:
Shawn Willden18a00e12014-06-17 10:50:03 -0600298 group.reset(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Kenny Root60711792013-08-16 14:02:41 -0700299 break;
300 case 384:
Shawn Willden18a00e12014-06-17 10:50:03 -0600301 group.reset(EC_GROUP_new_by_curve_name(NID_secp384r1));
Kenny Root60711792013-08-16 14:02:41 -0700302 break;
303 case 521:
Shawn Willden18a00e12014-06-17 10:50:03 -0600304 group.reset(EC_GROUP_new_by_curve_name(NID_secp521r1));
Kenny Root60711792013-08-16 14:02:41 -0700305 break;
306 default:
Kenny Root60711792013-08-16 14:02:41 -0700307 break;
308 }
309
Shawn Willden18a00e12014-06-17 10:50:03 -0600310 if (group.get() == NULL) {
Kenny Root60711792013-08-16 14:02:41 -0700311 logOpenSSLError("generate_ec_keypair");
312 return -1;
313 }
314
Adam Langley53d13c52014-09-23 17:40:53 -0700315#if !defined(OPENSSL_IS_BORINGSSL)
Adam Langleyb2747fe2014-12-11 17:19:31 -0800316 EC_GROUP_set_point_conversion_form(group.get(), POINT_CONVERSION_UNCOMPRESSED);
Shawn Willden18a00e12014-06-17 10:50:03 -0600317 EC_GROUP_set_asn1_flag(group.get(), OPENSSL_EC_NAMED_CURVE);
Adam Langley53d13c52014-09-23 17:40:53 -0700318#endif
Kenny Root60711792013-08-16 14:02:41 -0700319
320 /* initialize EC key */
321 Unique_EC_KEY eckey(EC_KEY_new());
322 if (eckey.get() == NULL) {
323 logOpenSSLError("generate_ec_keypair");
324 return -1;
325 }
326
Shawn Willden18a00e12014-06-17 10:50:03 -0600327 if (EC_KEY_set_group(eckey.get(), group.get()) != 1) {
Kenny Root60711792013-08-16 14:02:41 -0700328 logOpenSSLError("generate_ec_keypair");
329 return -1;
330 }
331
Shawn Willden1406b8a2014-06-12 11:39:48 -0600332 if (EC_KEY_generate_key(eckey.get()) != 1 || EC_KEY_check_key(eckey.get()) < 0) {
Kenny Root60711792013-08-16 14:02:41 -0700333 logOpenSSLError("generate_ec_keypair");
334 return -1;
335 }
336
337 if (EVP_PKEY_assign_EC_KEY(pkey, eckey.get()) == 0) {
338 logOpenSSLError("generate_ec_keypair");
339 return -1;
340 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600341 release_because_ownership_transferred(eckey);
Kenny Root60711792013-08-16 14:02:41 -0700342
343 return 0;
344}
345
Shawn Willden1406b8a2014-06-12 11:39:48 -0600346static int generate_rsa_keypair(EVP_PKEY* pkey, const keymaster_rsa_keygen_params_t* rsa_params) {
Kenny Root60711792013-08-16 14:02:41 -0700347 Unique_BIGNUM bn(BN_new());
348 if (bn.get() == NULL) {
349 logOpenSSLError("generate_rsa_keypair");
350 return -1;
351 }
352
353 if (BN_set_word(bn.get(), rsa_params->public_exponent) == 0) {
354 logOpenSSLError("generate_rsa_keypair");
355 return -1;
356 }
357
358 /* initialize RSA */
359 Unique_RSA rsa(RSA_new());
360 if (rsa.get() == NULL) {
361 logOpenSSLError("generate_rsa_keypair");
362 return -1;
363 }
364
Shawn Willden1406b8a2014-06-12 11:39:48 -0600365 if (!RSA_generate_key_ex(rsa.get(), rsa_params->modulus_size, bn.get(), NULL) ||
366 RSA_check_key(rsa.get()) < 0) {
Kenny Root60711792013-08-16 14:02:41 -0700367 logOpenSSLError("generate_rsa_keypair");
368 return -1;
369 }
370
371 if (EVP_PKEY_assign_RSA(pkey, rsa.get()) == 0) {
372 logOpenSSLError("generate_rsa_keypair");
373 return -1;
374 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600375 release_because_ownership_transferred(rsa);
Kenny Root60711792013-08-16 14:02:41 -0700376
377 return 0;
378}
379
Shawn Willden1406b8a2014-06-12 11:39:48 -0600380__attribute__((visibility("default"))) int openssl_generate_keypair(
381 const keymaster_device_t*, const keymaster_keypair_t key_type, const void* key_params,
382 uint8_t** keyBlob, size_t* keyBlobLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800383 Unique_EVP_PKEY pkey(EVP_PKEY_new());
384 if (pkey.get() == NULL) {
385 logOpenSSLError("openssl_generate_keypair");
386 return -1;
387 }
388
Kenny Root60711792013-08-16 14:02:41 -0700389 if (key_params == NULL) {
390 ALOGW("key_params == null");
391 return -1;
392 } else if (key_type == TYPE_DSA) {
393 const keymaster_dsa_keygen_params_t* dsa_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600394 (const keymaster_dsa_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700395 generate_dsa_keypair(pkey.get(), dsa_params);
396 } else if (key_type == TYPE_EC) {
397 const keymaster_ec_keygen_params_t* ec_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600398 (const keymaster_ec_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700399 generate_ec_keypair(pkey.get(), ec_params);
400 } else if (key_type == TYPE_RSA) {
401 const keymaster_rsa_keygen_params_t* rsa_params =
Shawn Willden1406b8a2014-06-12 11:39:48 -0600402 (const keymaster_rsa_keygen_params_t*)key_params;
Kenny Root60711792013-08-16 14:02:41 -0700403 generate_rsa_keypair(pkey.get(), rsa_params);
404 } else {
405 ALOGW("Unsupported key type %d", key_type);
Kenny Root70e3a862012-02-15 17:20:23 -0800406 return -1;
407 }
Kenny Root70e3a862012-02-15 17:20:23 -0800408
Kenny Root60711792013-08-16 14:02:41 -0700409 if (wrap_key(pkey.get(), EVP_PKEY_type(pkey->type), keyBlob, keyBlobLength)) {
Kenny Root70e3a862012-02-15 17:20:23 -0800410 return -1;
411 }
412
413 return 0;
414}
415
Shawn Willden1406b8a2014-06-12 11:39:48 -0600416__attribute__((visibility("default"))) int openssl_import_keypair(const keymaster_device_t*,
417 const uint8_t* key,
418 const size_t key_length,
419 uint8_t** key_blob,
420 size_t* key_blob_length) {
Kenny Root70e3a862012-02-15 17:20:23 -0800421 if (key == NULL) {
422 ALOGW("input key == NULL");
423 return -1;
424 } else if (key_blob == NULL || key_blob_length == NULL) {
425 ALOGW("output key blob or length == NULL");
426 return -1;
427 }
428
429 Unique_PKCS8_PRIV_KEY_INFO pkcs8(d2i_PKCS8_PRIV_KEY_INFO(NULL, &key, key_length));
430 if (pkcs8.get() == NULL) {
431 logOpenSSLError("openssl_import_keypair");
432 return -1;
433 }
434
435 /* assign to EVP */
436 Unique_EVP_PKEY pkey(EVP_PKCS82PKEY(pkcs8.get()));
437 if (pkey.get() == NULL) {
438 logOpenSSLError("openssl_import_keypair");
439 return -1;
440 }
Shawn Willden2cd28fa2014-06-13 11:51:08 -0600441 release_because_ownership_transferred(pkcs8);
Kenny Root70e3a862012-02-15 17:20:23 -0800442
443 if (wrap_key(pkey.get(), EVP_PKEY_type(pkey->type), key_blob, key_blob_length)) {
444 return -1;
445 }
446
447 return 0;
448}
449
Shawn Willden1406b8a2014-06-12 11:39:48 -0600450__attribute__((visibility("default"))) int openssl_get_keypair_public(
451 const struct keymaster_device*, const uint8_t* key_blob, const size_t key_blob_length,
452 uint8_t** x509_data, size_t* x509_data_length) {
Kenny Root70e3a862012-02-15 17:20:23 -0800453
454 if (x509_data == NULL || x509_data_length == NULL) {
455 ALOGW("output public key buffer == NULL");
456 return -1;
457 }
458
459 Unique_EVP_PKEY pkey(unwrap_key(key_blob, key_blob_length));
460 if (pkey.get() == NULL) {
461 return -1;
462 }
463
464 int len = i2d_PUBKEY(pkey.get(), NULL);
465 if (len <= 0) {
466 logOpenSSLError("openssl_get_keypair_public");
467 return -1;
468 }
469
Shawn Willden8d0531e2014-06-17 11:45:07 -0600470 UniquePtr<uint8_t, Malloc_Free> key(static_cast<uint8_t*>(malloc(len)));
Kenny Root70e3a862012-02-15 17:20:23 -0800471 if (key.get() == NULL) {
472 ALOGE("Could not allocate memory for public key data");
473 return -1;
474 }
475
476 unsigned char* tmp = reinterpret_cast<unsigned char*>(key.get());
477 if (i2d_PUBKEY(pkey.get(), &tmp) != len) {
478 logOpenSSLError("openssl_get_keypair_public");
479 return -1;
480 }
481
482 ALOGV("Length of x509 data is %d", len);
483 *x509_data_length = len;
484 *x509_data = key.release();
485
486 return 0;
487}
488
Kenny Root60711792013-08-16 14:02:41 -0700489static int sign_dsa(EVP_PKEY* pkey, keymaster_dsa_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600490 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700491 if (sign_params->digest_type != DIGEST_NONE) {
492 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
493 return -1;
494 }
495
496 Unique_DSA dsa(EVP_PKEY_get1_DSA(pkey));
497 if (dsa.get() == NULL) {
498 logOpenSSLError("openssl_sign_dsa");
499 return -1;
500 }
501
502 unsigned int dsaSize = DSA_size(dsa.get());
Shawn Willden8d0531e2014-06-17 11:45:07 -0600503 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
Kenny Root60711792013-08-16 14:02:41 -0700504 if (signedDataPtr.get() == NULL) {
505 logOpenSSLError("openssl_sign_dsa");
506 return -1;
507 }
508
509 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
510 if (DSA_sign(0, data, dataLength, tmp, &dsaSize, dsa.get()) <= 0) {
511 logOpenSSLError("openssl_sign_dsa");
512 return -1;
513 }
514
515 *signedDataLength = dsaSize;
516 *signedData = signedDataPtr.release();
517
518 return 0;
519}
520
521static int sign_ec(EVP_PKEY* pkey, keymaster_ec_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600522 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700523 if (sign_params->digest_type != DIGEST_NONE) {
524 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
525 return -1;
526 }
527
528 Unique_EC_KEY eckey(EVP_PKEY_get1_EC_KEY(pkey));
529 if (eckey.get() == NULL) {
530 logOpenSSLError("openssl_sign_ec");
531 return -1;
532 }
533
534 unsigned int ecdsaSize = ECDSA_size(eckey.get());
Shawn Willden8d0531e2014-06-17 11:45:07 -0600535 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
Kenny Root60711792013-08-16 14:02:41 -0700536 if (signedDataPtr.get() == NULL) {
537 logOpenSSLError("openssl_sign_ec");
538 return -1;
539 }
540
541 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
542 if (ECDSA_sign(0, data, dataLength, tmp, &ecdsaSize, eckey.get()) <= 0) {
543 logOpenSSLError("openssl_sign_ec");
544 return -1;
545 }
546
547 *signedDataLength = ecdsaSize;
548 *signedData = signedDataPtr.release();
549
550 return 0;
551}
552
Kenny Root60711792013-08-16 14:02:41 -0700553static int sign_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params, const uint8_t* data,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600554 const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
Kenny Root60711792013-08-16 14:02:41 -0700555 if (sign_params->digest_type != DIGEST_NONE) {
556 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
557 return -1;
558 } else if (sign_params->padding_type != PADDING_NONE) {
559 ALOGW("Cannot handle padding type %d", sign_params->padding_type);
560 return -1;
561 }
562
563 Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey));
564 if (rsa.get() == NULL) {
565 logOpenSSLError("openssl_sign_rsa");
566 return -1;
567 }
568
Shawn Willden8d0531e2014-06-17 11:45:07 -0600569 UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
Kenny Root60711792013-08-16 14:02:41 -0700570 if (signedDataPtr.get() == NULL) {
571 logOpenSSLError("openssl_sign_rsa");
572 return -1;
573 }
574
575 unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
576 if (RSA_private_encrypt(dataLength, data, tmp, rsa.get(), RSA_NO_PADDING) <= 0) {
577 logOpenSSLError("openssl_sign_rsa");
578 return -1;
579 }
580
581 *signedDataLength = dataLength;
582 *signedData = signedDataPtr.release();
583
584 return 0;
585}
586
Shawn Willden1406b8a2014-06-12 11:39:48 -0600587__attribute__((visibility("default"))) int openssl_sign_data(
588 const keymaster_device_t*, const void* params, const uint8_t* keyBlob,
589 const size_t keyBlobLength, const uint8_t* data, const size_t dataLength, uint8_t** signedData,
590 size_t* signedDataLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800591 if (data == NULL) {
592 ALOGW("input data to sign == NULL");
593 return -1;
594 } else if (signedData == NULL || signedDataLength == NULL) {
595 ALOGW("output signature buffer == NULL");
596 return -1;
597 }
598
599 Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
600 if (pkey.get() == NULL) {
601 return -1;
602 }
603
Kenny Root60711792013-08-16 14:02:41 -0700604 int type = EVP_PKEY_type(pkey->type);
605 if (type == EVP_PKEY_DSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600606 const keymaster_dsa_sign_params_t* sign_params =
607 reinterpret_cast<const keymaster_dsa_sign_params_t*>(params);
608 return sign_dsa(pkey.get(), const_cast<keymaster_dsa_sign_params_t*>(sign_params), data,
609 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700610 } else if (type == EVP_PKEY_EC) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600611 const keymaster_ec_sign_params_t* sign_params =
612 reinterpret_cast<const keymaster_ec_sign_params_t*>(params);
613 return sign_ec(pkey.get(), const_cast<keymaster_ec_sign_params_t*>(sign_params), data,
614 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700615 } else if (type == EVP_PKEY_RSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600616 const keymaster_rsa_sign_params_t* sign_params =
617 reinterpret_cast<const keymaster_rsa_sign_params_t*>(params);
618 return sign_rsa(pkey.get(), const_cast<keymaster_rsa_sign_params_t*>(sign_params), data,
619 dataLength, signedData, signedDataLength);
Kenny Root60711792013-08-16 14:02:41 -0700620 } else {
621 ALOGW("Unsupported key type");
Kenny Root70e3a862012-02-15 17:20:23 -0800622 return -1;
623 }
Kenny Root60711792013-08-16 14:02:41 -0700624}
Kenny Root70e3a862012-02-15 17:20:23 -0800625
Kenny Root60711792013-08-16 14:02:41 -0700626static int verify_dsa(EVP_PKEY* pkey, keymaster_dsa_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600627 const uint8_t* signedData, const size_t signedDataLength,
628 const uint8_t* signature, const size_t signatureLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800629 if (sign_params->digest_type != DIGEST_NONE) {
630 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
631 return -1;
Kenny Root60711792013-08-16 14:02:41 -0700632 }
633
634 Unique_DSA dsa(EVP_PKEY_get1_DSA(pkey));
635 if (dsa.get() == NULL) {
636 logOpenSSLError("openssl_verify_dsa");
Kenny Root70e3a862012-02-15 17:20:23 -0800637 return -1;
638 }
639
Kenny Root60711792013-08-16 14:02:41 -0700640 if (DSA_verify(0, signedData, signedDataLength, signature, signatureLength, dsa.get()) <= 0) {
641 logOpenSSLError("openssl_verify_dsa");
Kenny Root70e3a862012-02-15 17:20:23 -0800642 return -1;
643 }
644
Kenny Root70e3a862012-02-15 17:20:23 -0800645 return 0;
646}
647
Kenny Root60711792013-08-16 14:02:41 -0700648static int verify_ec(EVP_PKEY* pkey, keymaster_ec_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600649 const uint8_t* signedData, const size_t signedDataLength,
650 const uint8_t* signature, const size_t signatureLength) {
Kenny Root60711792013-08-16 14:02:41 -0700651 if (sign_params->digest_type != DIGEST_NONE) {
652 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
Kenny Root70e3a862012-02-15 17:20:23 -0800653 return -1;
654 }
655
Kenny Root60711792013-08-16 14:02:41 -0700656 Unique_EC_KEY eckey(EVP_PKEY_get1_EC_KEY(pkey));
657 if (eckey.get() == NULL) {
658 logOpenSSLError("openssl_verify_ec");
Kenny Root70e3a862012-02-15 17:20:23 -0800659 return -1;
660 }
661
Shawn Willden1406b8a2014-06-12 11:39:48 -0600662 if (ECDSA_verify(0, signedData, signedDataLength, signature, signatureLength, eckey.get()) <=
663 0) {
Kenny Root60711792013-08-16 14:02:41 -0700664 logOpenSSLError("openssl_verify_ec");
Kenny Root70e3a862012-02-15 17:20:23 -0800665 return -1;
666 }
667
Kenny Root60711792013-08-16 14:02:41 -0700668 return 0;
669}
670
671static int verify_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params,
Shawn Willden1406b8a2014-06-12 11:39:48 -0600672 const uint8_t* signedData, const size_t signedDataLength,
673 const uint8_t* signature, const size_t signatureLength) {
Kenny Root70e3a862012-02-15 17:20:23 -0800674 if (sign_params->digest_type != DIGEST_NONE) {
675 ALOGW("Cannot handle digest type %d", sign_params->digest_type);
676 return -1;
677 } else if (sign_params->padding_type != PADDING_NONE) {
678 ALOGW("Cannot handle padding type %d", sign_params->padding_type);
679 return -1;
680 } else if (signatureLength != signedDataLength) {
681 ALOGW("signed data length must be signature length");
682 return -1;
683 }
684
Kenny Root60711792013-08-16 14:02:41 -0700685 Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey));
Kenny Root70e3a862012-02-15 17:20:23 -0800686 if (rsa.get() == NULL) {
687 logOpenSSLError("openssl_verify_data");
688 return -1;
689 }
690
Shawn Willden8d0531e2014-06-17 11:45:07 -0600691 UniquePtr<uint8_t[]> dataPtr(new uint8_t[signedDataLength]);
Kenny Root70e3a862012-02-15 17:20:23 -0800692 if (dataPtr.get() == NULL) {
693 logOpenSSLError("openssl_verify_data");
694 return -1;
695 }
696
697 unsigned char* tmp = reinterpret_cast<unsigned char*>(dataPtr.get());
698 if (!RSA_public_decrypt(signatureLength, signature, tmp, rsa.get(), RSA_NO_PADDING)) {
699 logOpenSSLError("openssl_verify_data");
700 return -1;
701 }
702
703 int result = 0;
704 for (size_t i = 0; i < signedDataLength; i++) {
705 result |= tmp[i] ^ signedData[i];
706 }
707
708 return result == 0 ? 0 : -1;
709}
710
Shawn Willden1406b8a2014-06-12 11:39:48 -0600711__attribute__((visibility("default"))) int openssl_verify_data(
712 const keymaster_device_t*, const void* params, const uint8_t* keyBlob,
713 const size_t keyBlobLength, const uint8_t* signedData, const size_t signedDataLength,
714 const uint8_t* signature, const size_t signatureLength) {
Kenny Root60711792013-08-16 14:02:41 -0700715
716 if (signedData == NULL || signature == NULL) {
717 ALOGW("data or signature buffers == NULL");
718 return -1;
719 }
720
721 Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
722 if (pkey.get() == NULL) {
723 return -1;
724 }
725
726 int type = EVP_PKEY_type(pkey->type);
Kenny Rootb4d2e022013-09-04 13:56:03 -0700727 if (type == EVP_PKEY_DSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600728 const keymaster_dsa_sign_params_t* sign_params =
729 reinterpret_cast<const keymaster_dsa_sign_params_t*>(params);
730 return verify_dsa(pkey.get(), const_cast<keymaster_dsa_sign_params_t*>(sign_params),
731 signedData, signedDataLength, signature, signatureLength);
Kenny Rootb4d2e022013-09-04 13:56:03 -0700732 } else if (type == EVP_PKEY_RSA) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600733 const keymaster_rsa_sign_params_t* sign_params =
734 reinterpret_cast<const keymaster_rsa_sign_params_t*>(params);
735 return verify_rsa(pkey.get(), const_cast<keymaster_rsa_sign_params_t*>(sign_params),
736 signedData, signedDataLength, signature, signatureLength);
Kenny Root60711792013-08-16 14:02:41 -0700737 } else if (type == EVP_PKEY_EC) {
Shawn Willden1406b8a2014-06-12 11:39:48 -0600738 const keymaster_ec_sign_params_t* sign_params =
739 reinterpret_cast<const keymaster_ec_sign_params_t*>(params);
740 return verify_ec(pkey.get(), const_cast<keymaster_ec_sign_params_t*>(sign_params),
741 signedData, signedDataLength, signature, signatureLength);
Kenny Root60711792013-08-16 14:02:41 -0700742 } else {
743 ALOGW("Unsupported key type %d", type);
744 return -1;
745 }
746}