blob: 1c5a41d1b1a653ec0327fcc0e0f175b4b1d1817a [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
Doug Zongker211aebc2011-10-28 15:13:10 -070028extern RecoveryUI* ui;
29
Doug Zongker54e2e862009-08-17 13:21:04 -070030// Look for an RSA signature embedded in the .ZIP file comment given
31// the path to the zip. Verify it matches one of the given public
32// keys.
33//
34// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
35// or no key matches the signature).
36
37int verify_file(const char* path, const RSAPublicKey *pKeys, unsigned int numKeys) {
Doug Zongker211aebc2011-10-28 15:13:10 -070038 ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -070039
40 FILE* f = fopen(path, "rb");
41 if (f == NULL) {
42 LOGE("failed to open %s (%s)\n", path, strerror(errno));
43 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044 }
45
Doug Zongker54e2e862009-08-17 13:21:04 -070046 // An archive with a whole-file signature will end in six bytes:
47 //
Doug Zongker73ae31c2009-12-09 17:01:45 -080048 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -070049 //
50 // (As far as the ZIP format is concerned, these are part of the
51 // archive comment.) We start by reading this footer, this tells
52 // us how far back from the end we have to start reading to find
53 // the whole comment.
54
55#define FOOTER_SIZE 6
56
57 if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) {
58 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
59 fclose(f);
60 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 }
62
Doug Zongker54e2e862009-08-17 13:21:04 -070063 unsigned char footer[FOOTER_SIZE];
64 if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) {
65 LOGE("failed to read footer from %s (%s)\n", path, strerror(errno));
66 fclose(f);
67 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068 }
69
Doug Zongker54e2e862009-08-17 13:21:04 -070070 if (footer[2] != 0xff || footer[3] != 0xff) {
71 fclose(f);
72 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 }
74
Doug Zongker28ce47c2011-10-28 10:33:05 -070075 size_t comment_size = footer[4] + (footer[5] << 8);
76 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -070077 LOGI("comment is %d bytes; signature %d bytes from end\n",
78 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079
Doug Zongker54e2e862009-08-17 13:21:04 -070080 if (signature_start - FOOTER_SIZE < RSANUMBYTES) {
81 // "signature" block isn't big enough to contain an RSA block.
82 LOGE("signature is too short\n");
83 fclose(f);
84 return VERIFY_FAILURE;
85 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086
Doug Zongker54e2e862009-08-17 13:21:04 -070087#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088
Doug Zongker54e2e862009-08-17 13:21:04 -070089 // The end-of-central-directory record is 22 bytes plus any
90 // comment length.
91 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092
Doug Zongker54e2e862009-08-17 13:21:04 -070093 if (fseek(f, -eocd_size, SEEK_END) != 0) {
94 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
95 fclose(f);
96 return VERIFY_FAILURE;
97 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098
Doug Zongker54e2e862009-08-17 13:21:04 -070099 // Determine how much of the file is covered by the signature.
100 // This is everything except the signature data and length, which
101 // includes all of the EOCD except for the comment length field (2
102 // bytes) and the comment data.
103 size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104
Doug Zongker28ce47c2011-10-28 10:33:05 -0700105 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700106 if (eocd == NULL) {
107 LOGE("malloc for EOCD record failed\n");
108 fclose(f);
109 return VERIFY_FAILURE;
110 }
111 if (fread(eocd, 1, eocd_size, f) != eocd_size) {
112 LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno));
113 fclose(f);
114 return VERIFY_FAILURE;
115 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116
Doug Zongker54e2e862009-08-17 13:21:04 -0700117 // If this is really is the EOCD record, it will begin with the
118 // magic number $50 $4b $05 $06.
119 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
120 eocd[2] != 0x05 || eocd[3] != 0x06) {
121 LOGE("signature length doesn't match EOCD marker\n");
122 fclose(f);
123 return VERIFY_FAILURE;
124 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125
Doug Zongker28ce47c2011-10-28 10:33:05 -0700126 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700127 for (i = 4; i < eocd_size-3; ++i) {
128 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800129 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700130 // if the sequence $50 $4b $05 $06 appears anywhere after
131 // the real one, minzip will find the later (wrong) one,
132 // which could be exploitable. Fail verification if
133 // this sequence occurs anywhere after the real one.
134 LOGE("EOCD marker occurs after start of EOCD\n");
135 fclose(f);
136 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 }
138 }
139
Doug Zongker54e2e862009-08-17 13:21:04 -0700140#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141
Doug Zongker54e2e862009-08-17 13:21:04 -0700142 SHA_CTX ctx;
143 SHA_init(&ctx);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700144 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700145 if (buffer == NULL) {
146 LOGE("failed to alloc memory for sha1 buffer\n");
147 fclose(f);
148 return VERIFY_FAILURE;
149 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
Doug Zongker54e2e862009-08-17 13:21:04 -0700151 double frac = -1.0;
152 size_t so_far = 0;
153 fseek(f, 0, SEEK_SET);
154 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700155 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700156 if (signed_len - so_far < size) size = signed_len - so_far;
157 if (fread(buffer, 1, size, f) != size) {
158 LOGE("failed to read data from %s (%s)\n", path, strerror(errno));
159 fclose(f);
160 return VERIFY_FAILURE;
161 }
162 SHA_update(&ctx, buffer, size);
163 so_far += size;
164 double f = so_far / (double)signed_len;
165 if (f > frac + 0.02 || size == so_far) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700166 ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700167 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168 }
169 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700170 fclose(f);
171 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172
Doug Zongker54e2e862009-08-17 13:21:04 -0700173 const uint8_t* sha1 = SHA_final(&ctx);
174 for (i = 0; i < numKeys; ++i) {
Doug Zongker73ae31c2009-12-09 17:01:45 -0800175 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700176 // the signing tool appends after the signature itself.
177 if (RSA_verify(pKeys+i, eocd + eocd_size - 6 - RSANUMBYTES,
178 RSANUMBYTES, sha1)) {
Doug Zongker47626332011-03-15 12:11:08 -0700179 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700180 free(eocd);
181 return VERIFY_SUCCESS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 }
183 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700184 free(eocd);
185 LOGE("failed to verify whole-file signature\n");
186 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187}