• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
19 #include "ui.h"
20 
21 #include "mincrypt/rsa.h"
22 #include "mincrypt/sha.h"
23 #include "mincrypt/sha256.h"
24 
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 
29 extern RecoveryUI* ui;
30 
31 // 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 
verify_file(const char * path,const Certificate * pKeys,unsigned int numKeys)38 int verify_file(const char* path, const Certificate* pKeys, unsigned int numKeys) {
39     ui->SetProgress(0.0);
40 
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;
45     }
46 
47     // An archive with a whole-file signature will end in six bytes:
48     //
49     //   (2-byte signature start) $ff $ff (2-byte comment size)
50     //
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;
62     }
63 
64     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;
69     }
70 
71     if (footer[2] != 0xff || footer[3] != 0xff) {
72         LOGE("footer is wrong\n");
73         fclose(f);
74         return VERIFY_FAILURE;
75     }
76 
77     size_t comment_size = footer[4] + (footer[5] << 8);
78     size_t signature_start = footer[0] + (footer[1] << 8);
79     LOGI("comment is %d bytes; signature %d bytes from end\n",
80          comment_size, signature_start);
81 
82     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     }
88 
89 #define EOCD_HEADER_SIZE 22
90 
91     // The end-of-central-directory record is 22 bytes plus any
92     // comment length.
93     size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
94 
95     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     }
100 
101     // 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;
106 
107     unsigned char* eocd = (unsigned char*)malloc(eocd_size);
108     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     }
118 
119     // 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     }
127 
128     size_t i;
129     for (i = 4; i < eocd_size-3; ++i) {
130         if (eocd[i  ] == 0x50 && eocd[i+1] == 0x4b &&
131             eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
132             // 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;
139         }
140     }
141 
142 #define BUFFER_SIZE 4096
143 
144     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);
157     unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
158     if (buffer == NULL) {
159         LOGE("failed to alloc memory for sha1 buffer\n");
160         fclose(f);
161         return VERIFY_FAILURE;
162     }
163 
164     double frac = -1.0;
165     size_t so_far = 0;
166     fseek(f, 0, SEEK_SET);
167     while (so_far < signed_len) {
168         size_t size = BUFFER_SIZE;
169         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         }
175         if (need_sha1) SHA_update(&sha1_ctx, buffer, size);
176         if (need_sha256) SHA256_update(&sha256_ctx, buffer, size);
177         so_far += size;
178         double f = so_far / (double)signed_len;
179         if (f > frac + 0.02 || size == so_far) {
180             ui->SetProgress(f);
181             frac = f;
182         }
183     }
184     fclose(f);
185     free(buffer);
186 
187     const uint8_t* sha1 = SHA_final(&sha1_ctx);
188     const uint8_t* sha256 = SHA256_final(&sha256_ctx);
189 
190     for (i = 0; i < numKeys; ++i) {
191         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 
198         // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
199         // the signing tool appends after the signature itself.
200         if (RSA_verify(pKeys[i].public_key, eocd + eocd_size - 6 - RSANUMBYTES,
201                        RSANUMBYTES, hash, pKeys[i].hash_len)) {
202             LOGI("whole-file signature verified against key %d\n", i);
203             free(eocd);
204             return VERIFY_SUCCESS;
205         } else {
206             LOGI("failed to verify against key %d\n", i);
207         }
208     }
209     free(eocd);
210     LOGE("failed to verify whole-file signature\n");
211     return VERIFY_FAILURE;
212 }
213 
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 //
233 // 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 //
242 // Returns NULL if the file failed to parse, or if it contain zero keys.
243 Certificate*
load_keys(const char * filename,int * numKeys)244 load_keys(const char* filename, int* numKeys) {
245     Certificate* out = NULL;
246     *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;
259             out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
260             Certificate* cert = out + (*numKeys - 1);
261             cert->public_key = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
262 
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.
267                 cert->public_key->exponent = 3;
268                 cert->hash_len = SHA_DIGEST_SIZE;
269             } else if (start_char == 'v') {
270                 int version;
271                 if (fscanf(f, "%d {", &version) != 1) goto exit;
272                 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;
287                 }
288             }
289 
290             RSAPublicKey* key = cert->public_key;
291             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 
323             LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
324         }
325     }
326 
327     fclose(f);
328     return out;
329 
330 exit:
331     if (f) fclose(f);
332     free(out);
333     *numKeys = 0;
334     return NULL;
335 }
336