Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [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 "common.h" |
| 18 | #include "verifierold.h" |
| 19 | #include "ui.h" |
| 20 | |
| 21 | #include "mincrypt/rsa.h" |
| 22 | #include "mincrypt/sha.h" |
| 23 | #include "mincrypt/sha256.h" |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | //extern RecoveryUI* ui; |
| 30 | |
| 31 | #define PUBLIC_KEYS_FILE "/res/keys" |
| 32 | |
| 33 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 34 | // the path to the zip. Verify it matches one of the given public |
| 35 | // keys. |
| 36 | // |
| 37 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 38 | // or no key matches the signature). |
| 39 | int verify_file(const char* path) { |
| 40 | //ui->SetProgress(0.0); |
| 41 | |
| 42 | int numKeys; |
| 43 | Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 44 | if (pKeys == NULL) { |
| 45 | LOGE("Failed to load keys\n"); |
| 46 | return INSTALL_CORRUPT; |
| 47 | } |
| 48 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 49 | |
| 50 | FILE* f = fopen(path, "rb"); |
| 51 | if (f == NULL) { |
| 52 | LOGE("failed to open %s (%s)\n", path, strerror(errno)); |
| 53 | return VERIFY_FAILURE; |
| 54 | } |
| 55 | |
| 56 | // An archive with a whole-file signature will end in six bytes: |
| 57 | // |
| 58 | // (2-byte signature start) $ff $ff (2-byte comment size) |
| 59 | // |
| 60 | // (As far as the ZIP format is concerned, these are part of the |
| 61 | // archive comment.) We start by reading this footer, this tells |
| 62 | // us how far back from the end we have to start reading to find |
| 63 | // the whole comment. |
| 64 | |
| 65 | #define FOOTER_SIZE 6 |
| 66 | |
| 67 | if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) { |
| 68 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 69 | fclose(f); |
| 70 | return VERIFY_FAILURE; |
| 71 | } |
| 72 | |
| 73 | unsigned char footer[FOOTER_SIZE]; |
| 74 | if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) { |
| 75 | LOGE("failed to read footer from %s (%s)\n", path, strerror(errno)); |
| 76 | fclose(f); |
| 77 | return VERIFY_FAILURE; |
| 78 | } |
| 79 | |
| 80 | if (footer[2] != 0xff || footer[3] != 0xff) { |
| 81 | LOGE("footer is wrong\n"); |
| 82 | fclose(f); |
| 83 | return VERIFY_FAILURE; |
| 84 | } |
| 85 | |
| 86 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 87 | size_t signature_start = footer[0] + (footer[1] << 8); |
| 88 | LOGI("comment is %d bytes; signature %d bytes from end\n", |
| 89 | comment_size, signature_start); |
| 90 | |
| 91 | if (signature_start - FOOTER_SIZE < RSANUMBYTES) { |
| 92 | // "signature" block isn't big enough to contain an RSA block. |
| 93 | LOGE("signature is too short\n"); |
| 94 | fclose(f); |
| 95 | return VERIFY_FAILURE; |
| 96 | } |
| 97 | |
| 98 | #define EOCD_HEADER_SIZE 22 |
| 99 | |
| 100 | // The end-of-central-directory record is 22 bytes plus any |
| 101 | // comment length. |
| 102 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
| 103 | |
| 104 | if (fseek(f, -eocd_size, SEEK_END) != 0) { |
| 105 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 106 | fclose(f); |
| 107 | return VERIFY_FAILURE; |
| 108 | } |
| 109 | |
| 110 | // Determine how much of the file is covered by the signature. |
| 111 | // This is everything except the signature data and length, which |
| 112 | // includes all of the EOCD except for the comment length field (2 |
| 113 | // bytes) and the comment data. |
| 114 | size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2; |
| 115 | |
| 116 | unsigned char* eocd = (unsigned char*)malloc(eocd_size); |
| 117 | if (eocd == NULL) { |
| 118 | LOGE("malloc for EOCD record failed\n"); |
| 119 | fclose(f); |
| 120 | return VERIFY_FAILURE; |
| 121 | } |
| 122 | if (fread(eocd, 1, eocd_size, f) != eocd_size) { |
| 123 | LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno)); |
| 124 | fclose(f); |
| 125 | return VERIFY_FAILURE; |
| 126 | } |
| 127 | |
| 128 | // If this is really is the EOCD record, it will begin with the |
| 129 | // magic number $50 $4b $05 $06. |
| 130 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 131 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 132 | LOGE("signature length doesn't match EOCD marker\n"); |
| 133 | fclose(f); |
| 134 | return VERIFY_FAILURE; |
| 135 | } |
| 136 | |
| 137 | size_t i; |
| 138 | for (i = 4; i < eocd_size-3; ++i) { |
| 139 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
| 140 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
| 141 | // if the sequence $50 $4b $05 $06 appears anywhere after |
| 142 | // the real one, minzip will find the later (wrong) one, |
| 143 | // which could be exploitable. Fail verification if |
| 144 | // this sequence occurs anywhere after the real one. |
| 145 | LOGE("EOCD marker occurs after start of EOCD\n"); |
| 146 | fclose(f); |
| 147 | return VERIFY_FAILURE; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #define BUFFER_SIZE 4096 |
| 152 | |
| 153 | bool need_sha1 = false; |
| 154 | bool need_sha256 = false; |
| 155 | for (i = 0; i < numKeys; ++i) { |
| 156 | switch (pKeys[i].hash_len) { |
| 157 | case SHA_DIGEST_SIZE: need_sha1 = true; break; |
| 158 | case SHA256_DIGEST_SIZE: need_sha256 = true; break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | SHA_CTX sha1_ctx; |
| 163 | SHA256_CTX sha256_ctx; |
| 164 | SHA_init(&sha1_ctx); |
| 165 | SHA256_init(&sha256_ctx); |
| 166 | unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE); |
| 167 | if (buffer == NULL) { |
| 168 | LOGE("failed to alloc memory for sha1 buffer\n"); |
| 169 | fclose(f); |
| 170 | return VERIFY_FAILURE; |
| 171 | } |
| 172 | |
| 173 | double frac = -1.0; |
| 174 | size_t so_far = 0; |
| 175 | fseek(f, 0, SEEK_SET); |
| 176 | while (so_far < signed_len) { |
| 177 | size_t size = BUFFER_SIZE; |
| 178 | if (signed_len - so_far < size) size = signed_len - so_far; |
| 179 | if (fread(buffer, 1, size, f) != size) { |
| 180 | LOGE("failed to read data from %s (%s)\n", path, strerror(errno)); |
| 181 | fclose(f); |
| 182 | return VERIFY_FAILURE; |
| 183 | } |
| 184 | if (need_sha1) SHA_update(&sha1_ctx, buffer, size); |
| 185 | if (need_sha256) SHA256_update(&sha256_ctx, buffer, size); |
| 186 | so_far += size; |
| 187 | double f = so_far / (double)signed_len; |
| 188 | if (f > frac + 0.02 || size == so_far) { |
| 189 | //ui->SetProgress(f); |
| 190 | frac = f; |
| 191 | } |
| 192 | } |
| 193 | fclose(f); |
| 194 | free(buffer); |
| 195 | |
| 196 | const uint8_t* sha1 = SHA_final(&sha1_ctx); |
| 197 | const uint8_t* sha256 = SHA256_final(&sha256_ctx); |
| 198 | |
| 199 | for (i = 0; i < numKeys; ++i) { |
| 200 | const uint8_t* hash; |
| 201 | switch (pKeys[i].hash_len) { |
| 202 | case SHA_DIGEST_SIZE: hash = sha1; break; |
| 203 | case SHA256_DIGEST_SIZE: hash = sha256; break; |
| 204 | default: continue; |
| 205 | } |
| 206 | |
| 207 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
| 208 | // the signing tool appends after the signature itself. |
| 209 | if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES, |
| 210 | RSANUMBYTES, hash, pKeys[i].hash_len)) { |
| 211 | LOGI("whole-file signature verified against key %d\n", i); |
| 212 | free(eocd); |
| 213 | return VERIFY_SUCCESS; |
| 214 | } else { |
| 215 | LOGI("failed to verify against key %d\n", i); |
| 216 | } |
| 217 | LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES); |
| 218 | } |
| 219 | free(eocd); |
| 220 | LOGE("failed to verify whole-file signature\n"); |
| 221 | return VERIFY_FAILURE; |
| 222 | } |
| 223 | |
| 224 | // Reads a file containing one or more public keys as produced by |
| 225 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 226 | // as a C source literal, eg: |
| 227 | // |
| 228 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 229 | // |
| 230 | // For key versions newer than the original 2048-bit e=3 keys |
| 231 | // supported by Android, the string is preceded by a version |
| 232 | // identifier, eg: |
| 233 | // |
| 234 | // "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 235 | // |
| 236 | // (Note that the braces and commas in this example are actual |
| 237 | // characters the parser expects to find in the file; the ellipses |
| 238 | // indicate more numbers omitted from this example.) |
| 239 | // |
| 240 | // The file may contain multiple keys in this format, separated by |
| 241 | // commas. The last key must not be followed by a comma. |
| 242 | // |
| 243 | // A Certificate is a pair of an RSAPublicKey and a particular hash |
| 244 | // (we support SHA-1 and SHA-256; we store the hash length to signify |
| 245 | // which is being used). The hash used is implied by the version number. |
| 246 | // |
| 247 | // 1: 2048-bit RSA key with e=3 and SHA-1 hash |
| 248 | // 2: 2048-bit RSA key with e=65537 and SHA-1 hash |
| 249 | // 3: 2048-bit RSA key with e=3 and SHA-256 hash |
| 250 | // 4: 2048-bit RSA key with e=65537 and SHA-256 hash |
| 251 | // |
| 252 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
| 253 | Certificate* |
| 254 | load_keys(const char* filename, int* numKeys) { |
| 255 | Certificate* out = NULL; |
| 256 | *numKeys = 0; |
| 257 | |
| 258 | FILE* f = fopen(filename, "r"); |
| 259 | if (f == NULL) { |
| 260 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 261 | goto exit; |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | int i; |
| 266 | bool done = false; |
| 267 | while (!done) { |
| 268 | ++*numKeys; |
| 269 | out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); |
| 270 | Certificate* cert = out + (*numKeys - 1); |
| 271 | cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 272 | |
| 273 | char start_char; |
| 274 | if (fscanf(f, " %c", &start_char) != 1) goto exit; |
| 275 | if (start_char == '{') { |
| 276 | // a version 1 key has no version specifier. |
| 277 | cert->public_key->exponent = 3; |
| 278 | cert->hash_len = SHA_DIGEST_SIZE; |
| 279 | } else if (start_char == 'v') { |
| 280 | int version; |
| 281 | if (fscanf(f, "%d {", &version) != 1) goto exit; |
| 282 | switch (version) { |
| 283 | case 2: |
| 284 | cert->public_key->exponent = 65537; |
| 285 | cert->hash_len = SHA_DIGEST_SIZE; |
| 286 | break; |
| 287 | case 3: |
| 288 | cert->public_key->exponent = 3; |
| 289 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 290 | break; |
| 291 | case 4: |
| 292 | cert->public_key->exponent = 65537; |
| 293 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 294 | break; |
| 295 | default: |
| 296 | goto exit; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | RSAPublicKey* key = cert->public_key; |
| 301 | if (fscanf(f, " %i , 0x%x , { %u", |
| 302 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 303 | goto exit; |
| 304 | } |
| 305 | if (key->len != RSANUMWORDS) { |
| 306 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 307 | goto exit; |
| 308 | } |
| 309 | for (i = 1; i < key->len; ++i) { |
| 310 | if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; |
| 311 | } |
| 312 | if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; |
| 313 | for (i = 1; i < key->len; ++i) { |
| 314 | if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; |
| 315 | } |
| 316 | fscanf(f, " } } "); |
| 317 | |
| 318 | // if the line ends in a comma, this file has more keys. |
| 319 | switch (fgetc(f)) { |
| 320 | case ',': |
| 321 | // more keys to come. |
| 322 | break; |
| 323 | |
| 324 | case EOF: |
| 325 | done = true; |
| 326 | break; |
| 327 | |
| 328 | default: |
| 329 | LOGE("unexpected character between keys\n"); |
| 330 | goto exit; |
| 331 | } |
| 332 | LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | fclose(f); |
| 337 | return out; |
| 338 | |
| 339 | exit: |
| 340 | if (f) fclose(f); |
| 341 | free(out); |
| 342 | *numKeys = 0; |
| 343 | return NULL; |
| 344 | } |