blob: 82739f3394742d5055f9a9191986434467434b59 [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
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.
46static RSAPublicKey*
47load_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
101exit:
102 if (f) fclose(f);
103 free(out);
104 *numKeys = 0;
105 return NULL;
106}
Doug Zongker211aebc2011-10-28 15:13:10 -0700107
Doug Zongker54e2e862009-08-17 13:21:04 -0700108// 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_Troy2673cec2013-04-02 20:22:16 +0000114int verify_file(const char* path) {
115 //ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -0700116
Dees_Troy2673cec2013-04-02 20:22:16 +0000117 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 Zongker54e2e862009-08-17 13:21:04 -0700133
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 Projectc24a8e62009-03-03 19:28:42 -0800138 }
139
Doug Zongker54e2e862009-08-17 13:21:04 -0700140 // An archive with a whole-file signature will end in six bytes:
141 //
Doug Zongker73ae31c2009-12-09 17:01:45 -0800142 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -0700143 //
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 Projectc24a8e62009-03-03 19:28:42 -0800155 }
156
Doug Zongker54e2e862009-08-17 13:21:04 -0700157 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 Projectc24a8e62009-03-03 19:28:42 -0800162 }
163
Doug Zongker54e2e862009-08-17 13:21:04 -0700164 if (footer[2] != 0xff || footer[3] != 0xff) {
165 fclose(f);
166 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167 }
168
Doug Zongker28ce47c2011-10-28 10:33:05 -0700169 size_t comment_size = footer[4] + (footer[5] << 8);
170 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -0700171 LOGI("comment is %d bytes; signature %d bytes from end\n",
172 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173
Doug Zongker54e2e862009-08-17 13:21:04 -0700174 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 Projectc24a8e62009-03-03 19:28:42 -0800180
Doug Zongker54e2e862009-08-17 13:21:04 -0700181#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182
Doug Zongker54e2e862009-08-17 13:21:04 -0700183 // 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 Projectc24a8e62009-03-03 19:28:42 -0800186
Doug Zongker54e2e862009-08-17 13:21:04 -0700187 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 Projectc24a8e62009-03-03 19:28:42 -0800192
Doug Zongker54e2e862009-08-17 13:21:04 -0700193 // 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 Projectc24a8e62009-03-03 19:28:42 -0800198
Doug Zongker28ce47c2011-10-28 10:33:05 -0700199 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700200 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 Projectc24a8e62009-03-03 19:28:42 -0800210
Doug Zongker54e2e862009-08-17 13:21:04 -0700211 // 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 Projectc24a8e62009-03-03 19:28:42 -0800219
Doug Zongker28ce47c2011-10-28 10:33:05 -0700220 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700221 for (i = 4; i < eocd_size-3; ++i) {
222 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800223 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700224 // 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 Projectc24a8e62009-03-03 19:28:42 -0800231 }
232 }
233
Doug Zongker54e2e862009-08-17 13:21:04 -0700234#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235
Doug Zongker54e2e862009-08-17 13:21:04 -0700236 SHA_CTX ctx;
237 SHA_init(&ctx);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700238 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700239 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 Projectc24a8e62009-03-03 19:28:42 -0800244
Doug Zongker54e2e862009-08-17 13:21:04 -0700245 double frac = -1.0;
246 size_t so_far = 0;
247 fseek(f, 0, SEEK_SET);
248 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700249 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700250 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_Troy2673cec2013-04-02 20:22:16 +0000260 //ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700261 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 }
263 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700264 fclose(f);
265 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266
Doug Zongker54e2e862009-08-17 13:21:04 -0700267 const uint8_t* sha1 = SHA_final(&ctx);
268 for (i = 0; i < numKeys; ++i) {
Doug Zongker73ae31c2009-12-09 17:01:45 -0800269 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700270 // the signing tool appends after the signature itself.
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 int dees = RSA_verify(loadedKeys+i, eocd + eocd_size - 6 - RSANUMBYTES,
272 RSANUMBYTES, sha1);
273 if (dees) {
Doug Zongker47626332011-03-15 12:11:08 -0700274 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700275 free(eocd);
276 return VERIFY_SUCCESS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000278 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 -0800279 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700280 free(eocd);
281 LOGE("failed to verify whole-file signature\n");
282 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283}