Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "asn1_decoder.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 18 | #include "../common.h" |
| 19 | //#include "ui.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 20 | #include "verifier.h" |
| 21 | |
| 22 | #include "mincrypt/dsa_sig.h" |
| 23 | #include "mincrypt/p256.h" |
| 24 | #include "mincrypt/p256_ecdsa.h" |
| 25 | #include "mincrypt/rsa.h" |
| 26 | #include "mincrypt/sha.h" |
| 27 | #include "mincrypt/sha256.h" |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <malloc.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | //extern RecoveryUI* ui; |
| 35 | |
| 36 | #define PUBLIC_KEYS_FILE "/res/keys" |
| 37 | |
| 38 | /* |
| 39 | * Simple version of PKCS#7 SignedData extraction. This extracts the |
| 40 | * signature OCTET STRING to be used for signature verification. |
| 41 | * |
| 42 | * For full details, see http://www.ietf.org/rfc/rfc3852.txt |
| 43 | * |
| 44 | * The PKCS#7 structure looks like: |
| 45 | * |
| 46 | * SEQUENCE (ContentInfo) |
| 47 | * OID (ContentType) |
| 48 | * [0] (content) |
| 49 | * SEQUENCE (SignedData) |
| 50 | * INTEGER (version CMSVersion) |
| 51 | * SET (DigestAlgorithmIdentifiers) |
| 52 | * SEQUENCE (EncapsulatedContentInfo) |
| 53 | * [0] (CertificateSet OPTIONAL) |
| 54 | * [1] (RevocationInfoChoices OPTIONAL) |
| 55 | * SET (SignerInfos) |
| 56 | * SEQUENCE (SignerInfo) |
| 57 | * INTEGER (CMSVersion) |
| 58 | * SEQUENCE (SignerIdentifier) |
| 59 | * SEQUENCE (DigestAlgorithmIdentifier) |
| 60 | * SEQUENCE (SignatureAlgorithmIdentifier) |
| 61 | * OCTET STRING (SignatureValue) |
| 62 | */ |
| 63 | static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der, |
| 64 | size_t* sig_der_length) { |
| 65 | asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len); |
| 66 | if (ctx == NULL) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx); |
| 71 | if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) { |
| 72 | asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq); |
| 73 | if (signed_data_app != NULL) { |
| 74 | asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app); |
| 75 | if (signed_data_seq != NULL |
| 76 | && asn1_sequence_next(signed_data_seq) |
| 77 | && asn1_sequence_next(signed_data_seq) |
| 78 | && asn1_sequence_next(signed_data_seq) |
| 79 | && asn1_constructed_skip_all(signed_data_seq)) { |
| 80 | asn1_context_t *sig_set = asn1_set_get(signed_data_seq); |
| 81 | if (sig_set != NULL) { |
| 82 | asn1_context_t* sig_seq = asn1_sequence_get(sig_set); |
| 83 | if (sig_seq != NULL |
| 84 | && asn1_sequence_next(sig_seq) |
| 85 | && asn1_sequence_next(sig_seq) |
| 86 | && asn1_sequence_next(sig_seq) |
| 87 | && asn1_sequence_next(sig_seq)) { |
| 88 | uint8_t* sig_der_ptr; |
| 89 | if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) { |
| 90 | *sig_der = (uint8_t*) malloc(*sig_der_length); |
| 91 | if (*sig_der != NULL) { |
| 92 | memcpy(*sig_der, sig_der_ptr, *sig_der_length); |
| 93 | } |
| 94 | } |
| 95 | asn1_context_free(sig_seq); |
| 96 | } |
| 97 | asn1_context_free(sig_set); |
| 98 | } |
| 99 | asn1_context_free(signed_data_seq); |
| 100 | } |
| 101 | asn1_context_free(signed_data_app); |
| 102 | } |
| 103 | asn1_context_free(pkcs7_seq); |
| 104 | } |
| 105 | asn1_context_free(ctx); |
| 106 | |
| 107 | return *sig_der != NULL; |
| 108 | } |
| 109 | |
| 110 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 111 | // the path to the zip. Verify it matches one of the given public |
| 112 | // keys. |
| 113 | // |
| 114 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 115 | // or no key matches the signature). |
| 116 | int verify_file(unsigned char* addr, size_t length) { |
| 117 | //ui->SetProgress(0.0); |
| 118 | |
| 119 | int numKeys; |
| 120 | Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 121 | if (pKeys == NULL) { |
| 122 | LOGE("Failed to load keys\n"); |
| 123 | return INSTALL_CORRUPT; |
| 124 | } |
| 125 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 126 | |
| 127 | // An archive with a whole-file signature will end in six bytes: |
| 128 | // |
| 129 | // (2-byte signature start) $ff $ff (2-byte comment size) |
| 130 | // |
| 131 | // (As far as the ZIP format is concerned, these are part of the |
| 132 | // archive comment.) We start by reading this footer, this tells |
| 133 | // us how far back from the end we have to start reading to find |
| 134 | // the whole comment. |
| 135 | |
| 136 | #define FOOTER_SIZE 6 |
| 137 | |
| 138 | if (length < FOOTER_SIZE) { |
| 139 | LOGE("not big enough to contain footer\n"); |
| 140 | return VERIFY_FAILURE; |
| 141 | } |
| 142 | |
| 143 | unsigned char* footer = addr + length - FOOTER_SIZE; |
| 144 | |
| 145 | if (footer[2] != 0xff || footer[3] != 0xff) { |
| 146 | LOGE("footer is wrong\n"); |
| 147 | return VERIFY_FAILURE; |
| 148 | } |
| 149 | |
| 150 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 151 | size_t signature_start = footer[0] + (footer[1] << 8); |
| 152 | LOGI("comment is %zu bytes; signature %zu bytes from end\n", |
| 153 | comment_size, signature_start); |
| 154 | |
| 155 | if (signature_start <= FOOTER_SIZE) { |
| 156 | LOGE("Signature start is in the footer"); |
| 157 | return VERIFY_FAILURE; |
| 158 | } |
| 159 | |
| 160 | #define EOCD_HEADER_SIZE 22 |
| 161 | |
| 162 | // The end-of-central-directory record is 22 bytes plus any |
| 163 | // comment length. |
| 164 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
| 165 | |
| 166 | if (length < eocd_size) { |
| 167 | LOGE("not big enough to contain EOCD\n"); |
| 168 | return VERIFY_FAILURE; |
| 169 | } |
| 170 | |
| 171 | // Determine how much of the file is covered by the signature. |
| 172 | // This is everything except the signature data and length, which |
| 173 | // includes all of the EOCD except for the comment length field (2 |
| 174 | // bytes) and the comment data. |
| 175 | size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2; |
| 176 | |
| 177 | unsigned char* eocd = addr + length - eocd_size; |
| 178 | |
| 179 | // If this is really is the EOCD record, it will begin with the |
| 180 | // magic number $50 $4b $05 $06. |
| 181 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 182 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 183 | LOGE("signature length doesn't match EOCD marker\n"); |
| 184 | return VERIFY_FAILURE; |
| 185 | } |
| 186 | |
| 187 | size_t i; |
| 188 | for (i = 4; i < eocd_size-3; ++i) { |
| 189 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
| 190 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
| 191 | // if the sequence $50 $4b $05 $06 appears anywhere after |
| 192 | // the real one, minzip will find the later (wrong) one, |
| 193 | // which could be exploitable. Fail verification if |
| 194 | // this sequence occurs anywhere after the real one. |
| 195 | LOGE("EOCD marker occurs after start of EOCD\n"); |
| 196 | return VERIFY_FAILURE; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #define BUFFER_SIZE 4096 |
| 201 | |
| 202 | bool need_sha1 = false; |
| 203 | bool need_sha256 = false; |
| 204 | for (i = 0; i < numKeys; ++i) { |
| 205 | switch (pKeys[i].hash_len) { |
| 206 | case SHA_DIGEST_SIZE: need_sha1 = true; break; |
| 207 | case SHA256_DIGEST_SIZE: need_sha256 = true; break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | SHA_CTX sha1_ctx; |
| 212 | SHA256_CTX sha256_ctx; |
| 213 | SHA_init(&sha1_ctx); |
| 214 | SHA256_init(&sha256_ctx); |
| 215 | |
| 216 | double frac = -1.0; |
| 217 | size_t so_far = 0; |
| 218 | while (so_far < signed_len) { |
| 219 | size_t size = signed_len - so_far; |
| 220 | if (size > BUFFER_SIZE) size = BUFFER_SIZE; |
| 221 | |
| 222 | if (need_sha1) SHA_update(&sha1_ctx, addr + so_far, size); |
| 223 | if (need_sha256) SHA256_update(&sha256_ctx, addr + so_far, size); |
| 224 | so_far += size; |
| 225 | |
| 226 | double f = so_far / (double)signed_len; |
| 227 | if (f > frac + 0.02 || size == so_far) { |
| 228 | //ui->SetProgress(f); |
| 229 | frac = f; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | const uint8_t* sha1 = SHA_final(&sha1_ctx); |
| 234 | const uint8_t* sha256 = SHA256_final(&sha256_ctx); |
| 235 | |
| 236 | uint8_t* sig_der = NULL; |
| 237 | size_t sig_der_length = 0; |
| 238 | |
| 239 | size_t signature_size = signature_start - FOOTER_SIZE; |
| 240 | if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der, |
| 241 | &sig_der_length)) { |
| 242 | LOGE("Could not find signature DER block\n"); |
| 243 | return VERIFY_FAILURE; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Check to make sure at least one of the keys matches the signature. Since |
| 248 | * any key can match, we need to try each before determining a verification |
| 249 | * failure has happened. |
| 250 | */ |
| 251 | for (i = 0; i < numKeys; ++i) { |
| 252 | const uint8_t* hash; |
| 253 | switch (pKeys[i].hash_len) { |
| 254 | case SHA_DIGEST_SIZE: hash = sha1; break; |
| 255 | case SHA256_DIGEST_SIZE: hash = sha256; break; |
| 256 | default: continue; |
| 257 | } |
| 258 | |
| 259 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
| 260 | // the signing tool appends after the signature itself. |
| 261 | if (pKeys[i].key_type == Certificate::RSA) { |
| 262 | if (sig_der_length < RSANUMBYTES) { |
| 263 | // "signature" block isn't big enough to contain an RSA block. |
| 264 | LOGI("signature is too short for RSA key %zu\n", i); |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES, |
| 269 | hash, pKeys[i].hash_len)) { |
| 270 | LOGI("failed to verify against RSA key %zu\n", i); |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | LOGI("whole-file signature verified against RSA key %zu\n", i); |
| 275 | free(sig_der); |
| 276 | return VERIFY_SUCCESS; |
| 277 | } else if (pKeys[i].key_type == Certificate::EC |
| 278 | && pKeys[i].hash_len == SHA256_DIGEST_SIZE) { |
| 279 | p256_int r, s; |
| 280 | if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) { |
| 281 | LOGI("Not a DSA signature block for EC key %zu\n", i); |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | p256_int p256_hash; |
| 286 | p256_from_bin(hash, &p256_hash); |
| 287 | if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y), |
| 288 | &p256_hash, &r, &s)) { |
| 289 | LOGI("failed to verify against EC key %zu\n", i); |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | LOGI("whole-file signature verified against EC key %zu\n", i); |
| 294 | free(sig_der); |
| 295 | return VERIFY_SUCCESS; |
| 296 | } else { |
| 297 | LOGI("Unknown key type %d\n", pKeys[i].key_type); |
| 298 | } |
| 299 | LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES); |
| 300 | } |
| 301 | free(sig_der); |
| 302 | LOGE("failed to verify whole-file signature\n"); |
| 303 | return VERIFY_FAILURE; |
| 304 | } |
| 305 | |
| 306 | // Reads a file containing one or more public keys as produced by |
| 307 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 308 | // as a C source literal, eg: |
| 309 | // |
| 310 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 311 | // |
| 312 | // For key versions newer than the original 2048-bit e=3 keys |
| 313 | // supported by Android, the string is preceded by a version |
| 314 | // identifier, eg: |
| 315 | // |
| 316 | // "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 317 | // |
| 318 | // (Note that the braces and commas in this example are actual |
| 319 | // characters the parser expects to find in the file; the ellipses |
| 320 | // indicate more numbers omitted from this example.) |
| 321 | // |
| 322 | // The file may contain multiple keys in this format, separated by |
| 323 | // commas. The last key must not be followed by a comma. |
| 324 | // |
| 325 | // A Certificate is a pair of an RSAPublicKey and a particular hash |
| 326 | // (we support SHA-1 and SHA-256; we store the hash length to signify |
| 327 | // which is being used). The hash used is implied by the version number. |
| 328 | // |
| 329 | // 1: 2048-bit RSA key with e=3 and SHA-1 hash |
| 330 | // 2: 2048-bit RSA key with e=65537 and SHA-1 hash |
| 331 | // 3: 2048-bit RSA key with e=3 and SHA-256 hash |
| 332 | // 4: 2048-bit RSA key with e=65537 and SHA-256 hash |
| 333 | // 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash |
| 334 | // |
| 335 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
| 336 | Certificate* |
| 337 | load_keys(const char* filename, int* numKeys) { |
| 338 | Certificate* out = NULL; |
| 339 | *numKeys = 0; |
| 340 | |
| 341 | FILE* f = fopen(filename, "r"); |
| 342 | if (f == NULL) { |
| 343 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 344 | goto exit; |
| 345 | } |
| 346 | |
| 347 | { |
| 348 | int i; |
| 349 | bool done = false; |
| 350 | while (!done) { |
| 351 | ++*numKeys; |
| 352 | out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); |
| 353 | Certificate* cert = out + (*numKeys - 1); |
| 354 | memset(cert, '\0', sizeof(Certificate)); |
| 355 | |
| 356 | char start_char; |
| 357 | if (fscanf(f, " %c", &start_char) != 1) goto exit; |
| 358 | if (start_char == '{') { |
| 359 | // a version 1 key has no version specifier. |
| 360 | cert->key_type = Certificate::RSA; |
| 361 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 362 | cert->rsa->exponent = 3; |
| 363 | cert->hash_len = SHA_DIGEST_SIZE; |
| 364 | } else if (start_char == 'v') { |
| 365 | int version; |
| 366 | if (fscanf(f, "%d {", &version) != 1) goto exit; |
| 367 | switch (version) { |
| 368 | case 2: |
| 369 | cert->key_type = Certificate::RSA; |
| 370 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 371 | cert->rsa->exponent = 65537; |
| 372 | cert->hash_len = SHA_DIGEST_SIZE; |
| 373 | break; |
| 374 | case 3: |
| 375 | cert->key_type = Certificate::RSA; |
| 376 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 377 | cert->rsa->exponent = 3; |
| 378 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 379 | break; |
| 380 | case 4: |
| 381 | cert->key_type = Certificate::RSA; |
| 382 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 383 | cert->rsa->exponent = 65537; |
| 384 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 385 | break; |
| 386 | case 5: |
| 387 | cert->key_type = Certificate::EC; |
| 388 | cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey)); |
| 389 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 390 | break; |
| 391 | default: |
| 392 | goto exit; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (cert->key_type == Certificate::RSA) { |
| 397 | RSAPublicKey* key = cert->rsa; |
| 398 | if (fscanf(f, " %i , 0x%x , { %u", |
| 399 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 400 | goto exit; |
| 401 | } |
| 402 | if (key->len != RSANUMWORDS) { |
| 403 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 404 | goto exit; |
| 405 | } |
| 406 | for (i = 1; i < key->len; ++i) { |
| 407 | if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; |
| 408 | } |
| 409 | if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; |
| 410 | for (i = 1; i < key->len; ++i) { |
| 411 | if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; |
| 412 | } |
| 413 | fscanf(f, " } } "); |
| 414 | |
| 415 | LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len); |
| 416 | } else if (cert->key_type == Certificate::EC) { |
| 417 | ECPublicKey* key = cert->ec; |
| 418 | int key_len; |
| 419 | unsigned int byte; |
| 420 | uint8_t x_bytes[P256_NBYTES]; |
| 421 | uint8_t y_bytes[P256_NBYTES]; |
| 422 | if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit; |
| 423 | if (key_len != P256_NBYTES) { |
| 424 | LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES); |
| 425 | goto exit; |
| 426 | } |
| 427 | x_bytes[P256_NBYTES - 1] = byte; |
| 428 | for (i = P256_NBYTES - 2; i >= 0; --i) { |
| 429 | if (fscanf(f, " , %u", &byte) != 1) goto exit; |
| 430 | x_bytes[i] = byte; |
| 431 | } |
| 432 | if (fscanf(f, " } , { %u", &byte) != 1) goto exit; |
| 433 | y_bytes[P256_NBYTES - 1] = byte; |
| 434 | for (i = P256_NBYTES - 2; i >= 0; --i) { |
| 435 | if (fscanf(f, " , %u", &byte) != 1) goto exit; |
| 436 | y_bytes[i] = byte; |
| 437 | } |
| 438 | fscanf(f, " } } "); |
| 439 | p256_from_bin(x_bytes, &key->x); |
| 440 | p256_from_bin(y_bytes, &key->y); |
| 441 | } else { |
| 442 | LOGE("Unknown key type %d\n", cert->key_type); |
| 443 | goto exit; |
| 444 | } |
| 445 | |
| 446 | // if the line ends in a comma, this file has more keys. |
| 447 | switch (fgetc(f)) { |
| 448 | case ',': |
| 449 | // more keys to come. |
| 450 | break; |
| 451 | |
| 452 | case EOF: |
| 453 | done = true; |
| 454 | break; |
| 455 | |
| 456 | default: |
| 457 | LOGE("unexpected character between keys\n"); |
| 458 | goto exit; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | fclose(f); |
| 464 | return out; |
| 465 | |
| 466 | exit: |
| 467 | if (f) fclose(f); |
| 468 | free(out); |
| 469 | *numKeys = 0; |
| 470 | return NULL; |
| 471 | } |