The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [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 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 17 | #include "asn1_decoder.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | #include "common.h" |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 19 | #include "ui.h" |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 20 | #include "verifier.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 22 | #include "mincrypt/dsa_sig.h" |
| 23 | #include "mincrypt/p256.h" |
| 24 | #include "mincrypt/p256_ecdsa.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include "mincrypt/rsa.h" |
| 26 | #include "mincrypt/sha.h" |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 27 | #include "mincrypt/sha256.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | #include <string.h> |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <errno.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 33 | extern RecoveryUI* ui; |
| 34 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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 | */ |
| 60 | static 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 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 107 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 108 | // the path to the zip. Verify it matches one of the given public |
| 109 | // keys. |
| 110 | // |
| 111 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 112 | // or no key matches the signature). |
| 113 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 114 | int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 115 | ui->SetProgress(0.0); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 116 | |
| 117 | FILE* f = fopen(path, "rb"); |
| 118 | if (f == NULL) { |
| 119 | LOGE("failed to open %s (%s)\n", path, strerror(errno)); |
| 120 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 123 | // An archive with a whole-file signature will end in six bytes: |
| 124 | // |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 125 | // (2-byte signature start) $ff $ff (2-byte comment size) |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 126 | // |
| 127 | // (As far as the ZIP format is concerned, these are part of the |
| 128 | // archive comment.) We start by reading this footer, this tells |
| 129 | // us how far back from the end we have to start reading to find |
| 130 | // the whole comment. |
| 131 | |
| 132 | #define FOOTER_SIZE 6 |
| 133 | |
| 134 | if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) { |
| 135 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 136 | fclose(f); |
| 137 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 140 | unsigned char footer[FOOTER_SIZE]; |
| 141 | if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) { |
| 142 | LOGE("failed to read footer from %s (%s)\n", path, strerror(errno)); |
| 143 | fclose(f); |
| 144 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 147 | if (footer[2] != 0xff || footer[3] != 0xff) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 148 | LOGE("footer is wrong\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 149 | fclose(f); |
| 150 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 153 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 154 | size_t signature_start = footer[0] + (footer[1] << 8); |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 155 | LOGI("comment is %zu bytes; signature %zu bytes from end\n", |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 156 | comment_size, signature_start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 157 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 158 | if (signature_start <= FOOTER_SIZE) { |
| 159 | LOGE("Signature start is in the footer"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 160 | fclose(f); |
| 161 | return VERIFY_FAILURE; |
| 162 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 163 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 164 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 165 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 166 | // The end-of-central-directory record is 22 bytes plus any |
| 167 | // comment length. |
| 168 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 169 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 170 | if (fseek(f, -eocd_size, SEEK_END) != 0) { |
| 171 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 172 | fclose(f); |
| 173 | return VERIFY_FAILURE; |
| 174 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 176 | // Determine how much of the file is covered by the signature. |
| 177 | // This is everything except the signature data and length, which |
| 178 | // includes all of the EOCD except for the comment length field (2 |
| 179 | // bytes) and the comment data. |
| 180 | size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 182 | unsigned char* eocd = (unsigned char*)malloc(eocd_size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 183 | if (eocd == NULL) { |
| 184 | LOGE("malloc for EOCD record failed\n"); |
| 185 | fclose(f); |
| 186 | return VERIFY_FAILURE; |
| 187 | } |
| 188 | if (fread(eocd, 1, eocd_size, f) != eocd_size) { |
| 189 | LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno)); |
| 190 | fclose(f); |
| 191 | return VERIFY_FAILURE; |
| 192 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 193 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 194 | // If this is really is the EOCD record, it will begin with the |
| 195 | // magic number $50 $4b $05 $06. |
| 196 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 197 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 198 | LOGE("signature length doesn't match EOCD marker\n"); |
| 199 | fclose(f); |
| 200 | return VERIFY_FAILURE; |
| 201 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 202 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 203 | size_t i; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 204 | for (i = 4; i < eocd_size-3; ++i) { |
| 205 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
Doug Zongker | c652e41 | 2009-12-08 15:30:09 -0800 | [diff] [blame] | 206 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 207 | // if the sequence $50 $4b $05 $06 appears anywhere after |
| 208 | // the real one, minzip will find the later (wrong) one, |
| 209 | // which could be exploitable. Fail verification if |
| 210 | // this sequence occurs anywhere after the real one. |
| 211 | LOGE("EOCD marker occurs after start of EOCD\n"); |
| 212 | fclose(f); |
| 213 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 217 | #define BUFFER_SIZE 4096 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 218 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 219 | bool need_sha1 = false; |
| 220 | bool need_sha256 = false; |
| 221 | for (i = 0; i < numKeys; ++i) { |
| 222 | switch (pKeys[i].hash_len) { |
| 223 | case SHA_DIGEST_SIZE: need_sha1 = true; break; |
| 224 | case SHA256_DIGEST_SIZE: need_sha256 = true; break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | SHA_CTX sha1_ctx; |
| 229 | SHA256_CTX sha256_ctx; |
| 230 | SHA_init(&sha1_ctx); |
| 231 | SHA256_init(&sha256_ctx); |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 232 | unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 233 | if (buffer == NULL) { |
| 234 | LOGE("failed to alloc memory for sha1 buffer\n"); |
| 235 | fclose(f); |
| 236 | return VERIFY_FAILURE; |
| 237 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 238 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 239 | double frac = -1.0; |
| 240 | size_t so_far = 0; |
| 241 | fseek(f, 0, SEEK_SET); |
| 242 | while (so_far < signed_len) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 243 | size_t size = BUFFER_SIZE; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 244 | if (signed_len - so_far < size) size = signed_len - so_far; |
| 245 | if (fread(buffer, 1, size, f) != size) { |
| 246 | LOGE("failed to read data from %s (%s)\n", path, strerror(errno)); |
| 247 | fclose(f); |
| 248 | return VERIFY_FAILURE; |
| 249 | } |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 250 | if (need_sha1) SHA_update(&sha1_ctx, buffer, size); |
| 251 | if (need_sha256) SHA256_update(&sha256_ctx, buffer, size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 252 | so_far += size; |
| 253 | double f = so_far / (double)signed_len; |
| 254 | if (f > frac + 0.02 || size == so_far) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 255 | ui->SetProgress(f); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 256 | frac = f; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 259 | fclose(f); |
| 260 | free(buffer); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 261 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 262 | const uint8_t* sha1 = SHA_final(&sha1_ctx); |
| 263 | const uint8_t* sha256 = SHA256_final(&sha256_ctx); |
| 264 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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 | */ |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 282 | for (i = 0; i < numKeys; ++i) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 283 | const uint8_t* hash; |
| 284 | switch (pKeys[i].hash_len) { |
| 285 | case SHA_DIGEST_SIZE: hash = sha1; break; |
| 286 | case SHA256_DIGEST_SIZE: hash = sha256; break; |
| 287 | default: continue; |
| 288 | } |
| 289 | |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 290 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 291 | // the signing tool appends after the signature itself. |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 292 | if (pKeys[i].key_type == Certificate::RSA) { |
| 293 | if (sig_der_length < RSANUMBYTES) { |
| 294 | // "signature" block isn't big enough to contain an RSA block. |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 295 | LOGI("signature is too short for RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 296 | continue; |
| 297 | } |
| 298 | |
| 299 | if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES, |
| 300 | hash, pKeys[i].hash_len)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 301 | LOGI("failed to verify against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 302 | continue; |
| 303 | } |
| 304 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 305 | LOGI("whole-file signature verified against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 312 | LOGI("Not a DSA signature block for EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 320 | LOGI("failed to verify against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 321 | continue; |
| 322 | } |
| 323 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 324 | LOGI("whole-file signature verified against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 325 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 326 | return VERIFY_SUCCESS; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 327 | } else { |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 328 | LOGI("Unknown key type %d\n", pKeys[i].key_type); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 331 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 332 | LOGE("failed to verify whole-file signature\n"); |
| 333 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 334 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 335 | |
| 336 | // Reads a file containing one or more public keys as produced by |
| 337 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 338 | // as a C source literal, eg: |
| 339 | // |
| 340 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 341 | // |
| 342 | // For key versions newer than the original 2048-bit e=3 keys |
| 343 | // supported by Android, the string is preceded by a version |
| 344 | // identifier, eg: |
| 345 | // |
| 346 | // "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 347 | // |
| 348 | // (Note that the braces and commas in this example are actual |
| 349 | // characters the parser expects to find in the file; the ellipses |
| 350 | // indicate more numbers omitted from this example.) |
| 351 | // |
| 352 | // The file may contain multiple keys in this format, separated by |
| 353 | // commas. The last key must not be followed by a comma. |
| 354 | // |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 355 | // A Certificate is a pair of an RSAPublicKey and a particular hash |
| 356 | // (we support SHA-1 and SHA-256; we store the hash length to signify |
| 357 | // which is being used). The hash used is implied by the version number. |
| 358 | // |
| 359 | // 1: 2048-bit RSA key with e=3 and SHA-1 hash |
| 360 | // 2: 2048-bit RSA key with e=65537 and SHA-1 hash |
| 361 | // 3: 2048-bit RSA key with e=3 and SHA-256 hash |
| 362 | // 4: 2048-bit RSA key with e=65537 and SHA-256 hash |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 363 | // 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 364 | // |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 365 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 366 | Certificate* |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 367 | load_keys(const char* filename, int* numKeys) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 368 | Certificate* out = NULL; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 369 | *numKeys = 0; |
| 370 | |
| 371 | FILE* f = fopen(filename, "r"); |
| 372 | if (f == NULL) { |
| 373 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 374 | goto exit; |
| 375 | } |
| 376 | |
| 377 | { |
| 378 | int i; |
| 379 | bool done = false; |
| 380 | while (!done) { |
| 381 | ++*numKeys; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 382 | out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); |
| 383 | Certificate* cert = out + (*numKeys - 1); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 384 | memset(cert, '\0', sizeof(Certificate)); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 385 | |
| 386 | char start_char; |
| 387 | if (fscanf(f, " %c", &start_char) != 1) goto exit; |
| 388 | if (start_char == '{') { |
| 389 | // a version 1 key has no version specifier. |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 390 | cert->key_type = Certificate::RSA; |
| 391 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 392 | cert->rsa->exponent = 3; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 393 | cert->hash_len = SHA_DIGEST_SIZE; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 394 | } else if (start_char == 'v') { |
| 395 | int version; |
| 396 | if (fscanf(f, "%d {", &version) != 1) goto exit; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 397 | switch (version) { |
| 398 | case 2: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 399 | cert->key_type = Certificate::RSA; |
| 400 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 401 | cert->rsa->exponent = 65537; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 402 | cert->hash_len = SHA_DIGEST_SIZE; |
| 403 | break; |
| 404 | case 3: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 405 | cert->key_type = Certificate::RSA; |
| 406 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 407 | cert->rsa->exponent = 3; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 408 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 409 | break; |
| 410 | case 4: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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)); |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 419 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 420 | break; |
| 421 | default: |
| 422 | goto exit; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 426 | if (cert->key_type == Certificate::RSA) { |
| 427 | RSAPublicKey* key = cert->rsa; |
| 428 | if (fscanf(f, " %i , 0x%x , { %u", |
| 429 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 430 | goto exit; |
| 431 | } |
| 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); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 473 | goto exit; |
| 474 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 475 | |
| 476 | // if the line ends in a comma, this file has more keys. |
| 477 | switch (fgetc(f)) { |
| 478 | case ',': |
| 479 | // more keys to come. |
| 480 | break; |
| 481 | |
| 482 | case EOF: |
| 483 | done = true; |
| 484 | break; |
| 485 | |
| 486 | default: |
| 487 | LOGE("unexpected character between keys\n"); |
| 488 | goto exit; |
| 489 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
| 493 | fclose(f); |
| 494 | return out; |
| 495 | |
| 496 | exit: |
| 497 | if (f) fclose(f); |
| 498 | free(out); |
| 499 | *numKeys = 0; |
| 500 | return NULL; |
| 501 | } |