blob: a93e8d18bfaaa7efba355d2ff664282640c72c81 [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"
23
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024#include <string.h>
Doug Zongker54e2e862009-08-17 13:21:04 -070025#include <stdio.h>
26#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027
Dees_Troy2673cec2013-04-02 20:22:16 +000028//extern RecoveryUI* ui;
29
30#define PUBLIC_KEYS_FILE "/res/keys"
31
Doug Zongker54e2e862009-08-17 13:21:04 -070032// 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_Troy2673cec2013-04-02 20:22:16 +000038int verify_file(const char* path) {
39 //ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -070040
Dees_Troy2673cec2013-04-02 20:22:16 +000041 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_Troy1669f892013-09-04 18:35:08 +000047 /*
Dees_Troy2673cec2013-04-02 20:22:16 +000048 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_Troy1669f892013-09-04 18:35:08 +000057 printf("\n\n");*/
Doug Zongker54e2e862009-08-17 13:21:04 -070058
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 Projectc24a8e62009-03-03 19:28:42 -080063 }
64
Doug Zongker54e2e862009-08-17 13:21:04 -070065 // An archive with a whole-file signature will end in six bytes:
66 //
Doug Zongker73ae31c2009-12-09 17:01:45 -080067 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -070068 //
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 Projectc24a8e62009-03-03 19:28:42 -080080 }
81
Doug Zongker54e2e862009-08-17 13:21:04 -070082 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 Projectc24a8e62009-03-03 19:28:42 -080087 }
88
Doug Zongker54e2e862009-08-17 13:21:04 -070089 if (footer[2] != 0xff || footer[3] != 0xff) {
90 fclose(f);
91 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 }
93
Doug Zongker28ce47c2011-10-28 10:33:05 -070094 size_t comment_size = footer[4] + (footer[5] << 8);
95 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -070096 LOGI("comment is %d bytes; signature %d bytes from end\n",
97 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098
Doug Zongker54e2e862009-08-17 13:21:04 -070099 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 Projectc24a8e62009-03-03 19:28:42 -0800105
Doug Zongker54e2e862009-08-17 13:21:04 -0700106#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107
Doug Zongker54e2e862009-08-17 13:21:04 -0700108 // 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 Projectc24a8e62009-03-03 19:28:42 -0800111
Doug Zongker54e2e862009-08-17 13:21:04 -0700112 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 Projectc24a8e62009-03-03 19:28:42 -0800117
Doug Zongker54e2e862009-08-17 13:21:04 -0700118 // 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 Projectc24a8e62009-03-03 19:28:42 -0800123
Doug Zongker28ce47c2011-10-28 10:33:05 -0700124 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700125 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 Projectc24a8e62009-03-03 19:28:42 -0800135
Doug Zongker54e2e862009-08-17 13:21:04 -0700136 // 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 Projectc24a8e62009-03-03 19:28:42 -0800144
Doug Zongker28ce47c2011-10-28 10:33:05 -0700145 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700146 for (i = 4; i < eocd_size-3; ++i) {
147 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800148 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700149 // 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 Projectc24a8e62009-03-03 19:28:42 -0800156 }
157 }
158
Doug Zongker54e2e862009-08-17 13:21:04 -0700159#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160
Doug Zongker54e2e862009-08-17 13:21:04 -0700161 SHA_CTX ctx;
162 SHA_init(&ctx);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700163 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700164 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 Projectc24a8e62009-03-03 19:28:42 -0800169
Doug Zongker54e2e862009-08-17 13:21:04 -0700170 double frac = -1.0;
171 size_t so_far = 0;
172 fseek(f, 0, SEEK_SET);
173 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700174 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700175 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_Troy2673cec2013-04-02 20:22:16 +0000185 //ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700186 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187 }
188 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700189 fclose(f);
190 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191
Doug Zongker54e2e862009-08-17 13:21:04 -0700192 const uint8_t* sha1 = SHA_final(&ctx);
193 for (i = 0; i < numKeys; ++i) {
Doug Zongker73ae31c2009-12-09 17:01:45 -0800194 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700195 // the signing tool appends after the signature itself.
Dees_Troy2673cec2013-04-02 20:22:16 +0000196 int dees = RSA_verify(loadedKeys+i, eocd + eocd_size - 6 - RSANUMBYTES,
197 RSANUMBYTES, sha1);
198 if (dees) {
Doug Zongker47626332011-03-15 12:11:08 -0700199 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700200 free(eocd);
201 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700202 } else {
203 LOGI("failed to verify against key %d\n", i);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000205 LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i, returned %i\n", i, eocd_size, RSANUMBYTES, dees);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700207 free(eocd);
208 LOGE("failed to verify whole-file signature\n");
209 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210}
Doug Zongker6c249f72012-11-02 15:04:05 -0700211
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.
232RSAPublicKey*
233load_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_Troy1669f892013-09-04 18:35:08 +0000251#ifdef HAS_EXPONENT
Doug Zongker6c249f72012-11-02 15:04:05 -0700252 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_Troy1669f892013-09-04 18:35:08 +0000268#else
269 if (fscanf(f, " { %i , 0x%x , { %u",
270#endif
Doug Zongker6c249f72012-11-02 15:04:05 -0700271 &(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_Troy1669f892013-09-04 18:35:08 +0000301#ifdef HAS_EXPONENT
Doug Zongker6c249f72012-11-02 15:04:05 -0700302 LOGI("read key e=%d\n", key->exponent);
Dees_Troy1669f892013-09-04 18:35:08 +0000303#endif
Doug Zongker6c249f72012-11-02 15:04:05 -0700304 }
305 }
306
307 fclose(f);
308 return out;
309
310exit:
311 if (f) fclose(f);
312 free(out);
313 *numKeys = 0;
314 return NULL;
315}