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" |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 23 | #include "mincrypt/sha256.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <string.h> |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <errno.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 29 | //extern RecoveryUI* ui; |
| 30 | |
| 31 | #define PUBLIC_KEYS_FILE "/res/keys" |
| 32 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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). |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | int verify_file(const char* path) { |
| 40 | //ui->SetProgress(0.0); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 41 | |
Dees Troy | bb4c0cb | 2013-11-02 20:25:14 +0000 | [diff] [blame] | 42 | int numKeys; |
| 43 | Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 44 | if (pKeys == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 45 | LOGE("Failed to load keys\n"); |
Dees Troy | bb4c0cb | 2013-11-02 20:25:14 +0000 | [diff] [blame] | 46 | return INSTALL_CORRUPT; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 47 | } |
Dees Troy | bb4c0cb | 2013-11-02 20:25:14 +0000 | [diff] [blame] | 48 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 56 | // An archive with a whole-file signature will end in six bytes: |
| 57 | // |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 58 | // (2-byte signature start) $ff $ff (2-byte comment size) |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 80 | if (footer[2] != 0xff || footer[3] != 0xff) { |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 81 | LOGE("footer is wrong\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 82 | fclose(f); |
| 83 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 86 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 87 | size_t signature_start = footer[0] + (footer[1] << 8); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 88 | LOGI("comment is %d bytes; signature %d bytes from end\n", |
| 89 | comment_size, signature_start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 97 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 98 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 99 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 103 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 115 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 116 | unsigned char* eocd = (unsigned char*)malloc(eocd_size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 127 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 137 | size_t i; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 138 | for (i = 4; i < eocd_size-3; ++i) { |
| 139 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
Doug Zongker | c652e41 | 2009-12-08 15:30:09 -0800 | [diff] [blame] | 140 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 151 | #define BUFFER_SIZE 4096 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 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); |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 166 | unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 167 | if (buffer == NULL) { |
| 168 | LOGE("failed to alloc memory for sha1 buffer\n"); |
| 169 | fclose(f); |
| 170 | return VERIFY_FAILURE; |
| 171 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 172 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 173 | double frac = -1.0; |
| 174 | size_t so_far = 0; |
| 175 | fseek(f, 0, SEEK_SET); |
| 176 | while (so_far < signed_len) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 177 | size_t size = BUFFER_SIZE; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 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 | } |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 184 | if (need_sha1) SHA_update(&sha1_ctx, buffer, size); |
| 185 | if (need_sha256) SHA256_update(&sha256_ctx, buffer, size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 186 | so_far += size; |
| 187 | double f = so_far / (double)signed_len; |
| 188 | if (f > frac + 0.02 || size == so_far) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 189 | //ui->SetProgress(f); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 190 | frac = f; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 193 | fclose(f); |
| 194 | free(buffer); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 195 | |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 196 | const uint8_t* sha1 = SHA_final(&sha1_ctx); |
| 197 | const uint8_t* sha256 = SHA256_final(&sha256_ctx); |
| 198 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 199 | for (i = 0; i < numKeys; ++i) { |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 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 | |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 207 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 208 | // the signing tool appends after the signature itself. |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 209 | if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES, |
| 210 | RSANUMBYTES, hash, pKeys[i].hash_len)) { |
Doug Zongker | 4762633 | 2011-03-15 12:11:08 -0700 | [diff] [blame] | 211 | LOGI("whole-file signature verified against key %d\n", i); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 212 | free(eocd); |
| 213 | return VERIFY_SUCCESS; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 214 | } else { |
| 215 | LOGI("failed to verify against key %d\n", i); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 216 | } |
Dees Troy | bb4c0cb | 2013-11-02 20:25:14 +0000 | [diff] [blame] | 217 | LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 218 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 219 | free(eocd); |
| 220 | LOGE("failed to verify whole-file signature\n"); |
| 221 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 222 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 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 | // |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 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 | // |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 252 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 253 | Certificate* |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 254 | load_keys(const char* filename, int* numKeys) { |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 255 | Certificate* out = NULL; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 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; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 269 | out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); |
| 270 | Certificate* cert = out + (*numKeys - 1); |
| 271 | cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 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. |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 277 | cert->public_key->exponent = 3; |
| 278 | cert->hash_len = SHA_DIGEST_SIZE; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 279 | } else if (start_char == 'v') { |
| 280 | int version; |
| 281 | if (fscanf(f, "%d {", &version) != 1) goto exit; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 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; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 300 | RSAPublicKey* key = cert->public_key; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 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 | } |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 332 | LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 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 | } |