blob: 782a838630bb7770e05a70cb870d90ae3c34d9b6 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Zongker28ce47c2011-10-28 10:33:05 -070019#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include "mincrypt/rsa.h"
22#include "mincrypt/sha.h"
Doug Zongkerbac7fba2013-04-10 11:32:17 -070023#include "mincrypt/sha256.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <string.h>
Doug Zongker54e2e862009-08-17 13:21:04 -070026#include <stdio.h>
27#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Doug Zongker211aebc2011-10-28 15:13:10 -070029extern RecoveryUI* ui;
30
Doug Zongker54e2e862009-08-17 13:21:04 -070031// Look for an RSA signature embedded in the .ZIP file comment given
32// the path to the zip. Verify it matches one of the given public
33// keys.
34//
35// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
36// or no key matches the signature).
37
Doug Zongkerbac7fba2013-04-10 11:32:17 -070038int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys) {
Doug Zongker211aebc2011-10-28 15:13:10 -070039 ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -070040
41 FILE* f = fopen(path, "rb");
42 if (f == NULL) {
43 LOGE("failed to open %s (%s)\n", path, strerror(errno));
44 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045 }
46
Doug Zongker54e2e862009-08-17 13:21:04 -070047 // An archive with a whole-file signature will end in six bytes:
48 //
Doug Zongker73ae31c2009-12-09 17:01:45 -080049 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -070050 //
51 // (As far as the ZIP format is concerned, these are part of the
52 // archive comment.) We start by reading this footer, this tells
53 // us how far back from the end we have to start reading to find
54 // the whole comment.
55
56#define FOOTER_SIZE 6
57
58 if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) {
59 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
60 fclose(f);
61 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062 }
63
Doug Zongker54e2e862009-08-17 13:21:04 -070064 unsigned char footer[FOOTER_SIZE];
65 if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) {
66 LOGE("failed to read footer from %s (%s)\n", path, strerror(errno));
67 fclose(f);
68 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 }
70
Doug Zongker54e2e862009-08-17 13:21:04 -070071 if (footer[2] != 0xff || footer[3] != 0xff) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -070072 LOGE("footer is wrong\n");
Doug Zongker54e2e862009-08-17 13:21:04 -070073 fclose(f);
74 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 }
76
Doug Zongker28ce47c2011-10-28 10:33:05 -070077 size_t comment_size = footer[4] + (footer[5] << 8);
78 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -070079 LOGI("comment is %d bytes; signature %d bytes from end\n",
80 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081
Doug Zongker54e2e862009-08-17 13:21:04 -070082 if (signature_start - FOOTER_SIZE < RSANUMBYTES) {
83 // "signature" block isn't big enough to contain an RSA block.
84 LOGE("signature is too short\n");
85 fclose(f);
86 return VERIFY_FAILURE;
87 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088
Doug Zongker54e2e862009-08-17 13:21:04 -070089#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090
Doug Zongker54e2e862009-08-17 13:21:04 -070091 // The end-of-central-directory record is 22 bytes plus any
92 // comment length.
93 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094
Doug Zongker54e2e862009-08-17 13:21:04 -070095 if (fseek(f, -eocd_size, SEEK_END) != 0) {
96 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
97 fclose(f);
98 return VERIFY_FAILURE;
99 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100
Doug Zongker54e2e862009-08-17 13:21:04 -0700101 // Determine how much of the file is covered by the signature.
102 // This is everything except the signature data and length, which
103 // includes all of the EOCD except for the comment length field (2
104 // bytes) and the comment data.
105 size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106
Doug Zongker28ce47c2011-10-28 10:33:05 -0700107 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700108 if (eocd == NULL) {
109 LOGE("malloc for EOCD record failed\n");
110 fclose(f);
111 return VERIFY_FAILURE;
112 }
113 if (fread(eocd, 1, eocd_size, f) != eocd_size) {
114 LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno));
115 fclose(f);
116 return VERIFY_FAILURE;
117 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118
Doug Zongker54e2e862009-08-17 13:21:04 -0700119 // If this is really is the EOCD record, it will begin with the
120 // magic number $50 $4b $05 $06.
121 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
122 eocd[2] != 0x05 || eocd[3] != 0x06) {
123 LOGE("signature length doesn't match EOCD marker\n");
124 fclose(f);
125 return VERIFY_FAILURE;
126 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800127
Doug Zongker28ce47c2011-10-28 10:33:05 -0700128 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700129 for (i = 4; i < eocd_size-3; ++i) {
130 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800131 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700132 // if the sequence $50 $4b $05 $06 appears anywhere after
133 // the real one, minzip will find the later (wrong) one,
134 // which could be exploitable. Fail verification if
135 // this sequence occurs anywhere after the real one.
136 LOGE("EOCD marker occurs after start of EOCD\n");
137 fclose(f);
138 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 }
140 }
141
Doug Zongker54e2e862009-08-17 13:21:04 -0700142#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700144 bool need_sha1 = false;
145 bool need_sha256 = false;
146 for (i = 0; i < numKeys; ++i) {
147 switch (pKeys[i].hash_len) {
148 case SHA_DIGEST_SIZE: need_sha1 = true; break;
149 case SHA256_DIGEST_SIZE: need_sha256 = true; break;
150 }
151 }
152
153 SHA_CTX sha1_ctx;
154 SHA256_CTX sha256_ctx;
155 SHA_init(&sha1_ctx);
156 SHA256_init(&sha256_ctx);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700157 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700158 if (buffer == NULL) {
159 LOGE("failed to alloc memory for sha1 buffer\n");
160 fclose(f);
161 return VERIFY_FAILURE;
162 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
Doug Zongker54e2e862009-08-17 13:21:04 -0700164 double frac = -1.0;
165 size_t so_far = 0;
166 fseek(f, 0, SEEK_SET);
167 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700168 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700169 if (signed_len - so_far < size) size = signed_len - so_far;
170 if (fread(buffer, 1, size, f) != size) {
171 LOGE("failed to read data from %s (%s)\n", path, strerror(errno));
172 fclose(f);
173 return VERIFY_FAILURE;
174 }
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700175 if (need_sha1) SHA_update(&sha1_ctx, buffer, size);
176 if (need_sha256) SHA256_update(&sha256_ctx, buffer, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700177 so_far += size;
178 double f = so_far / (double)signed_len;
179 if (f > frac + 0.02 || size == so_far) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700180 ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700181 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 }
183 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700184 fclose(f);
185 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700187 const uint8_t* sha1 = SHA_final(&sha1_ctx);
188 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
189
Doug Zongker54e2e862009-08-17 13:21:04 -0700190 for (i = 0; i < numKeys; ++i) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700191 const uint8_t* hash;
192 switch (pKeys[i].hash_len) {
193 case SHA_DIGEST_SIZE: hash = sha1; break;
194 case SHA256_DIGEST_SIZE: hash = sha256; break;
195 default: continue;
196 }
197
Doug Zongker73ae31c2009-12-09 17:01:45 -0800198 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700199 // the signing tool appends after the signature itself.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700200 if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES,
201 RSANUMBYTES, hash, pKeys[i].hash_len)) {
Doug Zongker47626332011-03-15 12:11:08 -0700202 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700203 free(eocd);
204 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700205 } else {
206 LOGI("failed to verify against key %d\n", i);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207 }
208 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700209 free(eocd);
210 LOGE("failed to verify whole-file signature\n");
211 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212}
Doug Zongker6c249f72012-11-02 15:04:05 -0700213
214// Reads a file containing one or more public keys as produced by
215// DumpPublicKey: this is an RSAPublicKey struct as it would appear
216// as a C source literal, eg:
217//
218// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
219//
220// For key versions newer than the original 2048-bit e=3 keys
221// supported by Android, the string is preceded by a version
222// identifier, eg:
223//
224// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
225//
226// (Note that the braces and commas in this example are actual
227// characters the parser expects to find in the file; the ellipses
228// indicate more numbers omitted from this example.)
229//
230// The file may contain multiple keys in this format, separated by
231// commas. The last key must not be followed by a comma.
232//
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700233// A Certificate is a pair of an RSAPublicKey and a particular hash
234// (we support SHA-1 and SHA-256; we store the hash length to signify
235// which is being used). The hash used is implied by the version number.
236//
237// 1: 2048-bit RSA key with e=3 and SHA-1 hash
238// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
239// 3: 2048-bit RSA key with e=3 and SHA-256 hash
240// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
241//
Doug Zongker6c249f72012-11-02 15:04:05 -0700242// Returns NULL if the file failed to parse, or if it contain zero keys.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700243Certificate*
Doug Zongker6c249f72012-11-02 15:04:05 -0700244load_keys(const char* filename, int* numKeys) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700245 Certificate* out = NULL;
Doug Zongker6c249f72012-11-02 15:04:05 -0700246 *numKeys = 0;
247
248 FILE* f = fopen(filename, "r");
249 if (f == NULL) {
250 LOGE("opening %s: %s\n", filename, strerror(errno));
251 goto exit;
252 }
253
254 {
255 int i;
256 bool done = false;
257 while (!done) {
258 ++*numKeys;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700259 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
260 Certificate* cert = out + (*numKeys - 1);
261 cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
Doug Zongker6c249f72012-11-02 15:04:05 -0700262
263 char start_char;
264 if (fscanf(f, " %c", &start_char) != 1) goto exit;
265 if (start_char == '{') {
266 // a version 1 key has no version specifier.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700267 cert->public_key->exponent = 3;
268 cert->hash_len = SHA_DIGEST_SIZE;
Doug Zongker6c249f72012-11-02 15:04:05 -0700269 } else if (start_char == 'v') {
270 int version;
271 if (fscanf(f, "%d {", &version) != 1) goto exit;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700272 switch (version) {
273 case 2:
274 cert->public_key->exponent = 65537;
275 cert->hash_len = SHA_DIGEST_SIZE;
276 break;
277 case 3:
278 cert->public_key->exponent = 3;
279 cert->hash_len = SHA256_DIGEST_SIZE;
280 break;
281 case 4:
282 cert->public_key->exponent = 65537;
283 cert->hash_len = SHA256_DIGEST_SIZE;
284 break;
285 default:
286 goto exit;
Doug Zongker6c249f72012-11-02 15:04:05 -0700287 }
288 }
289
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700290 RSAPublicKey* key = cert->public_key;
Doug Zongker6c249f72012-11-02 15:04:05 -0700291 if (fscanf(f, " %i , 0x%x , { %u",
292 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
293 goto exit;
294 }
295 if (key->len != RSANUMWORDS) {
296 LOGE("key length (%d) does not match expected size\n", key->len);
297 goto exit;
298 }
299 for (i = 1; i < key->len; ++i) {
300 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
301 }
302 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
303 for (i = 1; i < key->len; ++i) {
304 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
305 }
306 fscanf(f, " } } ");
307
308 // if the line ends in a comma, this file has more keys.
309 switch (fgetc(f)) {
310 case ',':
311 // more keys to come.
312 break;
313
314 case EOF:
315 done = true;
316 break;
317
318 default:
319 LOGE("unexpected character between keys\n");
320 goto exit;
321 }
322
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700323 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
Doug Zongker6c249f72012-11-02 15:04:05 -0700324 }
325 }
326
327 fclose(f);
328 return out;
329
330exit:
331 if (f) fclose(f);
332 free(out);
333 *numKeys = 0;
334 return NULL;
335}