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