aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Root2013-10-09 12:14:35 -0500
committerKenny Root2013-10-10 16:19:19 -0500
commit7a4adb5268ae71260c86788ccdeb7a699c80ee0a (patch)
treeda4a744e642f5014c3a1085097590bd4a5997f1c /verifier.cpp
parent58c60900ac3682fab08f64373bdd1020713b48f7 (diff)
downloadplatform-bootable-recovery-7a4adb5268ae71260c86788ccdeb7a699c80ee0a.tar.gz
platform-bootable-recovery-7a4adb5268ae71260c86788ccdeb7a699c80ee0a.tar.xz
platform-bootable-recovery-7a4adb5268ae71260c86788ccdeb7a699c80ee0a.zip
Add support for ECDSA signatures
This adds support for key version 5 which is an EC key using the NIST P-256 curve parameters. OTAs may be signed with these keys using the ECDSA signature algorithm with SHA-256. Change-Id: Id88672a3deb70681c78d5ea0d739e10f839e4567
Diffstat (limited to 'verifier.cpp')
-rw-r--r--verifier.cpp230
1 files changed, 198 insertions, 32 deletions
diff --git a/verifier.cpp b/verifier.cpp
index 782a8386..0930fbd1 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -14,10 +14,14 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include "asn1_decoder.h"
17#include "common.h" 18#include "common.h"
18#include "verifier.h"
19#include "ui.h" 19#include "ui.h"
20#include "verifier.h"
20 21
22#include "mincrypt/dsa_sig.h"
23#include "mincrypt/p256.h"
24#include "mincrypt/p256_ecdsa.h"
21#include "mincrypt/rsa.h" 25#include "mincrypt/rsa.h"
22#include "mincrypt/sha.h" 26#include "mincrypt/sha.h"
23#include "mincrypt/sha256.h" 27#include "mincrypt/sha256.h"
@@ -28,6 +32,78 @@
28 32
29extern RecoveryUI* ui; 33extern RecoveryUI* ui;
30 34
35/*
36 * Simple version of PKCS#7 SignedData extraction. This extracts the
37 * signature OCTET STRING to be used for signature verification.
38 *
39 * For full details, see http://www.ietf.org/rfc/rfc3852.txt
40 *
41 * The PKCS#7 structure looks like:
42 *
43 * SEQUENCE (ContentInfo)
44 * OID (ContentType)
45 * [0] (content)
46 * SEQUENCE (SignedData)
47 * INTEGER (version CMSVersion)
48 * SET (DigestAlgorithmIdentifiers)
49 * SEQUENCE (EncapsulatedContentInfo)
50 * [0] (CertificateSet OPTIONAL)
51 * [1] (RevocationInfoChoices OPTIONAL)
52 * SET (SignerInfos)
53 * SEQUENCE (SignerInfo)
54 * INTEGER (CMSVersion)
55 * SEQUENCE (SignerIdentifier)
56 * SEQUENCE (DigestAlgorithmIdentifier)
57 * SEQUENCE (SignatureAlgorithmIdentifier)
58 * OCTET STRING (SignatureValue)
59 */
60static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der,
61 size_t* sig_der_length) {
62 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
63 if (ctx == NULL) {
64 return false;
65 }
66
67 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
68 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
69 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
70 if (signed_data_app != NULL) {
71 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
72 if (signed_data_seq != NULL
73 && asn1_sequence_next(signed_data_seq)
74 && asn1_sequence_next(signed_data_seq)
75 && asn1_sequence_next(signed_data_seq)
76 && asn1_constructed_skip_all(signed_data_seq)) {
77 asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
78 if (sig_set != NULL) {
79 asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
80 if (sig_seq != NULL
81 && asn1_sequence_next(sig_seq)
82 && asn1_sequence_next(sig_seq)
83 && asn1_sequence_next(sig_seq)
84 && asn1_sequence_next(sig_seq)) {
85 uint8_t* sig_der_ptr;
86 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) {
87 *sig_der = (uint8_t*) malloc(*sig_der_length);
88 if (*sig_der != NULL) {
89 memcpy(*sig_der, sig_der_ptr, *sig_der_length);
90 }
91 }
92 asn1_context_free(sig_seq);
93 }
94 asn1_context_free(sig_set);
95 }
96 asn1_context_free(signed_data_seq);
97 }
98 asn1_context_free(signed_data_app);
99 }
100 asn1_context_free(pkcs7_seq);
101 }
102 asn1_context_free(ctx);
103
104 return *sig_der != NULL;
105}
106
31// Look for an RSA signature embedded in the .ZIP file comment given 107// Look for an RSA signature embedded in the .ZIP file comment given
32// the path to the zip. Verify it matches one of the given public 108// the path to the zip. Verify it matches one of the given public
33// keys. 109// keys.
@@ -79,9 +155,8 @@ int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys
79 LOGI("comment is %d bytes; signature %d bytes from end\n", 155 LOGI("comment is %d bytes; signature %d bytes from end\n",
80 comment_size, signature_start); 156 comment_size, signature_start);
81 157
82 if (signature_start - FOOTER_SIZE < RSANUMBYTES) { 158 if (signature_start <= FOOTER_SIZE) {
83 // "signature" block isn't big enough to contain an RSA block. 159 LOGE("Signature start is in the footer");
84 LOGE("signature is too short\n");
85 fclose(f); 160 fclose(f);
86 return VERIFY_FAILURE; 161 return VERIFY_FAILURE;
87 } 162 }
@@ -187,6 +262,23 @@ int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys
187 const uint8_t* sha1 = SHA_final(&sha1_ctx); 262 const uint8_t* sha1 = SHA_final(&sha1_ctx);
188 const uint8_t* sha256 = SHA256_final(&sha256_ctx); 263 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
189 264
265 uint8_t* sig_der = NULL;
266 size_t sig_der_length = 0;
267
268 size_t signature_size = signature_start - FOOTER_SIZE;
269 if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der,
270 &sig_der_length)) {
271 LOGE("Could not find signature DER block\n");
272 free(eocd);
273 return VERIFY_FAILURE;
274 }
275 free(eocd);
276
277 /*
278 * Check to make sure at least one of the keys matches the signature. Since
279 * any key can match, we need to try each before determining a verification
280 * failure has happened.
281 */
190 for (i = 0; i < numKeys; ++i) { 282 for (i = 0; i < numKeys; ++i) {
191 const uint8_t* hash; 283 const uint8_t* hash;
192 switch (pKeys[i].hash_len) { 284 switch (pKeys[i].hash_len) {
@@ -197,16 +289,46 @@ int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys
197 289
198 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that 290 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
199 // the signing tool appends after the signature itself. 291 // the signing tool appends after the signature itself.
200 if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES, 292 if (pKeys[i].key_type == Certificate::RSA) {
201 RSANUMBYTES, hash, pKeys[i].hash_len)) { 293 if (sig_der_length < RSANUMBYTES) {
202 LOGI("whole-file signature verified against key %d\n", i); 294 // "signature" block isn't big enough to contain an RSA block.
203 free(eocd); 295 LOGI("signature is too short for RSA key %d\n", i);
296 continue;
297 }
298
299 if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES,
300 hash, pKeys[i].hash_len)) {
301 LOGI("failed to verify against RSA key %d\n", i);
302 continue;
303 }
304
305 LOGI("whole-file signature verified against RSA key %d\n", i);
306 free(sig_der);
307 return VERIFY_SUCCESS;
308 } else if (pKeys[i].key_type == Certificate::EC
309 && pKeys[i].hash_len == SHA256_DIGEST_SIZE) {
310 p256_int r, s;
311 if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) {
312 LOGI("Not a DSA signature block for EC key %d\n", i);
313 continue;
314 }
315
316 p256_int p256_hash;
317 p256_from_bin(hash, &p256_hash);
318 if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y),
319 &p256_hash, &r, &s)) {
320 LOGI("failed to verify against EC key %d\n", i);
321 continue;
322 }
323
324 LOGI("whole-file signature verified against EC key %d\n", i);
325 free(sig_der);
204 return VERIFY_SUCCESS; 326 return VERIFY_SUCCESS;
205 } else { 327 } else {
206 LOGI("failed to verify against key %d\n", i); 328 LOGI("Unknown key type %d\n", pKeys[i].key_type);
207 } 329 }
208 } 330 }
209 free(eocd); 331 free(sig_der);
210 LOGE("failed to verify whole-file signature\n"); 332 LOGE("failed to verify whole-file signature\n");
211 return VERIFY_FAILURE; 333 return VERIFY_FAILURE;
212} 334}
@@ -238,6 +360,7 @@ int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys
238// 2: 2048-bit RSA key with e=65537 and SHA-1 hash 360// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
239// 3: 2048-bit RSA key with e=3 and SHA-256 hash 361// 3: 2048-bit RSA key with e=3 and SHA-256 hash
240// 4: 2048-bit RSA key with e=65537 and SHA-256 hash 362// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
363// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
241// 364//
242// Returns NULL if the file failed to parse, or if it contain zero keys. 365// Returns NULL if the file failed to parse, or if it contain zero keys.
243Certificate* 366Certificate*
@@ -258,28 +381,41 @@ load_keys(const char* filename, int* numKeys) {
258 ++*numKeys; 381 ++*numKeys;
259 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); 382 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
260 Certificate* cert = out + (*numKeys - 1); 383 Certificate* cert = out + (*numKeys - 1);
261 cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); 384 memset(cert, '\0', sizeof(Certificate));
262 385
263 char start_char; 386 char start_char;
264 if (fscanf(f, " %c", &start_char) != 1) goto exit; 387 if (fscanf(f, " %c", &start_char) != 1) goto exit;
265 if (start_char == '{') { 388 if (start_char == '{') {
266 // a version 1 key has no version specifier. 389 // a version 1 key has no version specifier.
267 cert->public_key->exponent = 3; 390 cert->key_type = Certificate::RSA;
391 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
392 cert->rsa->exponent = 3;
268 cert->hash_len = SHA_DIGEST_SIZE; 393 cert->hash_len = SHA_DIGEST_SIZE;
269 } else if (start_char == 'v') { 394 } else if (start_char == 'v') {
270 int version; 395 int version;
271 if (fscanf(f, "%d {", &version) != 1) goto exit; 396 if (fscanf(f, "%d {", &version) != 1) goto exit;
272 switch (version) { 397 switch (version) {
273 case 2: 398 case 2:
274 cert->public_key->exponent = 65537; 399 cert->key_type = Certificate::RSA;
400 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
401 cert->rsa->exponent = 65537;
275 cert->hash_len = SHA_DIGEST_SIZE; 402 cert->hash_len = SHA_DIGEST_SIZE;
276 break; 403 break;
277 case 3: 404 case 3:
278 cert->public_key->exponent = 3; 405 cert->key_type = Certificate::RSA;
406 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
407 cert->rsa->exponent = 3;
279 cert->hash_len = SHA256_DIGEST_SIZE; 408 cert->hash_len = SHA256_DIGEST_SIZE;
280 break; 409 break;
281 case 4: 410 case 4:
282 cert->public_key->exponent = 65537; 411 cert->key_type = Certificate::RSA;
412 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
413 cert->rsa->exponent = 65537;
414 cert->hash_len = SHA256_DIGEST_SIZE;
415 break;
416 case 5:
417 cert->key_type = Certificate::EC;
418 cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey));
283 cert->hash_len = SHA256_DIGEST_SIZE; 419 cert->hash_len = SHA256_DIGEST_SIZE;
284 break; 420 break;
285 default: 421 default:
@@ -287,23 +423,55 @@ load_keys(const char* filename, int* numKeys) {
287 } 423 }
288 } 424 }
289 425
290 RSAPublicKey* key = cert->public_key; 426 if (cert->key_type == Certificate::RSA) {
291 if (fscanf(f, " %i , 0x%x , { %u", 427 RSAPublicKey* key = cert->rsa;
292 &(key->len), &(key->n0inv), &(key->n[0])) != 3) { 428 if (fscanf(f, " %i , 0x%x , { %u",
293 goto exit; 429 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
294 } 430 goto exit;
295 if (key->len != RSANUMWORDS) { 431 }
296 LOGE("key length (%d) does not match expected size\n", key->len); 432 if (key->len != RSANUMWORDS) {
433 LOGE("key length (%d) does not match expected size\n", key->len);
434 goto exit;
435 }
436 for (i = 1; i < key->len; ++i) {
437 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
438 }
439 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
440 for (i = 1; i < key->len; ++i) {
441 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
442 }
443 fscanf(f, " } } ");
444
445 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
446 } else if (cert->key_type == Certificate::EC) {
447 ECPublicKey* key = cert->ec;
448 int key_len;
449 unsigned int byte;
450 uint8_t x_bytes[P256_NBYTES];
451 uint8_t y_bytes[P256_NBYTES];
452 if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit;
453 if (key_len != P256_NBYTES) {
454 LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES);
455 goto exit;
456 }
457 x_bytes[P256_NBYTES - 1] = byte;
458 for (i = P256_NBYTES - 2; i >= 0; --i) {
459 if (fscanf(f, " , %u", &byte) != 1) goto exit;
460 x_bytes[i] = byte;
461 }
462 if (fscanf(f, " } , { %u", &byte) != 1) goto exit;
463 y_bytes[P256_NBYTES - 1] = byte;
464 for (i = P256_NBYTES - 2; i >= 0; --i) {
465 if (fscanf(f, " , %u", &byte) != 1) goto exit;
466 y_bytes[i] = byte;
467 }
468 fscanf(f, " } } ");
469 p256_from_bin(x_bytes, &key->x);
470 p256_from_bin(y_bytes, &key->y);
471 } else {
472 LOGE("Unknown key type %d\n", cert->key_type);
297 goto exit; 473 goto exit;
298 } 474 }
299 for (i = 1; i < key->len; ++i) {
300 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
301 }
302 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
303 for (i = 1; i < key->len; ++i) {
304 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
305 }
306 fscanf(f, " } } ");
307 475
308 // if the line ends in a comma, this file has more keys. 476 // if the line ends in a comma, this file has more keys.
309 switch (fgetc(f)) { 477 switch (fgetc(f)) {
@@ -319,8 +487,6 @@ load_keys(const char* filename, int* numKeys) {
319 LOGE("unexpected character between keys\n"); 487 LOGE("unexpected character between keys\n");
320 goto exit; 488 goto exit;
321 } 489 }
322
323 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
324 } 490 }
325 } 491 }
326 492