blob: ccc0742d2f1ae837c5d7115d53f88b8042cc2268 [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
Dees_Troy2673cec2013-04-02 20:22:16 +000029//extern RecoveryUI* ui;
30
31#define PUBLIC_KEYS_FILE "/res/keys"
32
Doug Zongker54e2e862009-08-17 13:21:04 -070033// 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_Troy2673cec2013-04-02 20:22:16 +000039int verify_file(const char* path) {
40 //ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -070041
Dees Troybb4c0cb2013-11-02 20:25:14 +000042 int numKeys;
43 Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
44 if (pKeys == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000045 LOGE("Failed to load keys\n");
Dees Troybb4c0cb2013-11-02 20:25:14 +000046 return INSTALL_CORRUPT;
Dees_Troy2673cec2013-04-02 20:22:16 +000047 }
Dees Troybb4c0cb2013-11-02 20:25:14 +000048 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
Doug Zongker54e2e862009-08-17 13:21:04 -070049
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 Projectc24a8e62009-03-03 19:28:42 -080054 }
55
Doug Zongker54e2e862009-08-17 13:21:04 -070056 // An archive with a whole-file signature will end in six bytes:
57 //
Doug Zongker73ae31c2009-12-09 17:01:45 -080058 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -070059 //
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 Projectc24a8e62009-03-03 19:28:42 -080071 }
72
Doug Zongker54e2e862009-08-17 13:21:04 -070073 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 Projectc24a8e62009-03-03 19:28:42 -080078 }
79
Doug Zongker54e2e862009-08-17 13:21:04 -070080 if (footer[2] != 0xff || footer[3] != 0xff) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -070081 LOGE("footer is wrong\n");
Doug Zongker54e2e862009-08-17 13:21:04 -070082 fclose(f);
83 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 }
85
Doug Zongker28ce47c2011-10-28 10:33:05 -070086 size_t comment_size = footer[4] + (footer[5] << 8);
87 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -070088 LOGI("comment is %d bytes; signature %d bytes from end\n",
89 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090
Doug Zongker54e2e862009-08-17 13:21:04 -070091 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 Projectc24a8e62009-03-03 19:28:42 -080097
Doug Zongker54e2e862009-08-17 13:21:04 -070098#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099
Doug Zongker54e2e862009-08-17 13:21:04 -0700100 // 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 Projectc24a8e62009-03-03 19:28:42 -0800103
Doug Zongker54e2e862009-08-17 13:21:04 -0700104 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 Projectc24a8e62009-03-03 19:28:42 -0800109
Doug Zongker54e2e862009-08-17 13:21:04 -0700110 // 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 Projectc24a8e62009-03-03 19:28:42 -0800115
Doug Zongker28ce47c2011-10-28 10:33:05 -0700116 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700117 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 Projectc24a8e62009-03-03 19:28:42 -0800127
Doug Zongker54e2e862009-08-17 13:21:04 -0700128 // 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 Projectc24a8e62009-03-03 19:28:42 -0800136
Doug Zongker28ce47c2011-10-28 10:33:05 -0700137 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700138 for (i = 4; i < eocd_size-3; ++i) {
139 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800140 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700141 // 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 Projectc24a8e62009-03-03 19:28:42 -0800148 }
149 }
150
Doug Zongker54e2e862009-08-17 13:21:04 -0700151#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700153 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 Zongker28ce47c2011-10-28 10:33:05 -0700166 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700167 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 Projectc24a8e62009-03-03 19:28:42 -0800172
Doug Zongker54e2e862009-08-17 13:21:04 -0700173 double frac = -1.0;
174 size_t so_far = 0;
175 fseek(f, 0, SEEK_SET);
176 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700177 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700178 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 Zongkerbac7fba2013-04-10 11:32:17 -0700184 if (need_sha1) SHA_update(&sha1_ctx, buffer, size);
185 if (need_sha256) SHA256_update(&sha256_ctx, buffer, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700186 so_far += size;
187 double f = so_far / (double)signed_len;
188 if (f > frac + 0.02 || size == so_far) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000189 //ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700190 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191 }
192 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700193 fclose(f);
194 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700196 const uint8_t* sha1 = SHA_final(&sha1_ctx);
197 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
198
Doug Zongker54e2e862009-08-17 13:21:04 -0700199 for (i = 0; i < numKeys; ++i) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700200 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 Zongker73ae31c2009-12-09 17:01:45 -0800207 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700208 // the signing tool appends after the signature itself.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700209 if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES,
210 RSANUMBYTES, hash, pKeys[i].hash_len)) {
Doug Zongker47626332011-03-15 12:11:08 -0700211 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700212 free(eocd);
213 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700214 } else {
215 LOGI("failed to verify against key %d\n", i);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
Dees Troybb4c0cb2013-11-02 20:25:14 +0000217 LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700219 free(eocd);
220 LOGE("failed to verify whole-file signature\n");
221 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222}
Doug Zongker6c249f72012-11-02 15:04:05 -0700223
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 Zongkerbac7fba2013-04-10 11:32:17 -0700243// 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 Zongker6c249f72012-11-02 15:04:05 -0700252// Returns NULL if the file failed to parse, or if it contain zero keys.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700253Certificate*
Doug Zongker6c249f72012-11-02 15:04:05 -0700254load_keys(const char* filename, int* numKeys) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700255 Certificate* out = NULL;
Doug Zongker6c249f72012-11-02 15:04:05 -0700256 *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 Zongkerbac7fba2013-04-10 11:32:17 -0700269 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
270 Certificate* cert = out + (*numKeys - 1);
271 cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
Doug Zongker6c249f72012-11-02 15:04:05 -0700272
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 Zongkerbac7fba2013-04-10 11:32:17 -0700277 cert->public_key->exponent = 3;
278 cert->hash_len = SHA_DIGEST_SIZE;
Doug Zongker6c249f72012-11-02 15:04:05 -0700279 } else if (start_char == 'v') {
280 int version;
281 if (fscanf(f, "%d {", &version) != 1) goto exit;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700282 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 Zongker6c249f72012-11-02 15:04:05 -0700297 }
298 }
299
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700300 RSAPublicKey* key = cert->public_key;
Doug Zongker6c249f72012-11-02 15:04:05 -0700301 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 Zongkerbac7fba2013-04-10 11:32:17 -0700332 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
Doug Zongker6c249f72012-11-02 15:04:05 -0700333 }
334 }
335
336 fclose(f);
337 return out;
338
339exit:
340 if (f) fclose(f);
341 free(out);
342 *numKeys = 0;
343 return NULL;
344}