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