blob: 58ca72393e639741dbb837a015e2a7b648b8a4fc [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 Zongker54e2e862009-08-17 13:21:04 -070028// Look for an RSA signature embedded in the .ZIP file comment given
29// the path to the zip. Verify it matches one of the given public
30// keys.
31//
32// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
33// or no key matches the signature).
34
35int verify_file(const char* path, const RSAPublicKey *pKeys, unsigned int numKeys) {
36 ui_set_progress(0.0);
37
38 FILE* f = fopen(path, "rb");
39 if (f == NULL) {
40 LOGE("failed to open %s (%s)\n", path, strerror(errno));
41 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042 }
43
Doug Zongker54e2e862009-08-17 13:21:04 -070044 // An archive with a whole-file signature will end in six bytes:
45 //
Doug Zongker73ae31c2009-12-09 17:01:45 -080046 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -070047 //
48 // (As far as the ZIP format is concerned, these are part of the
49 // archive comment.) We start by reading this footer, this tells
50 // us how far back from the end we have to start reading to find
51 // the whole comment.
52
53#define FOOTER_SIZE 6
54
55 if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) {
56 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
57 fclose(f);
58 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059 }
60
Doug Zongker54e2e862009-08-17 13:21:04 -070061 unsigned char footer[FOOTER_SIZE];
62 if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) {
63 LOGE("failed to read footer from %s (%s)\n", path, strerror(errno));
64 fclose(f);
65 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066 }
67
Doug Zongker54e2e862009-08-17 13:21:04 -070068 if (footer[2] != 0xff || footer[3] != 0xff) {
69 fclose(f);
70 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071 }
72
Doug Zongker28ce47c2011-10-28 10:33:05 -070073 size_t comment_size = footer[4] + (footer[5] << 8);
74 size_t signature_start = footer[0] + (footer[1] << 8);
Doug Zongker54e2e862009-08-17 13:21:04 -070075 LOGI("comment is %d bytes; signature %d bytes from end\n",
76 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077
Doug Zongker54e2e862009-08-17 13:21:04 -070078 if (signature_start - FOOTER_SIZE < RSANUMBYTES) {
79 // "signature" block isn't big enough to contain an RSA block.
80 LOGE("signature is too short\n");
81 fclose(f);
82 return VERIFY_FAILURE;
83 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084
Doug Zongker54e2e862009-08-17 13:21:04 -070085#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086
Doug Zongker54e2e862009-08-17 13:21:04 -070087 // The end-of-central-directory record is 22 bytes plus any
88 // comment length.
89 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090
Doug Zongker54e2e862009-08-17 13:21:04 -070091 if (fseek(f, -eocd_size, SEEK_END) != 0) {
92 LOGE("failed to seek in %s (%s)\n", path, strerror(errno));
93 fclose(f);
94 return VERIFY_FAILURE;
95 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096
Doug Zongker54e2e862009-08-17 13:21:04 -070097 // Determine how much of the file is covered by the signature.
98 // This is everything except the signature data and length, which
99 // includes all of the EOCD except for the comment length field (2
100 // bytes) and the comment data.
101 size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102
Doug Zongker28ce47c2011-10-28 10:33:05 -0700103 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700104 if (eocd == NULL) {
105 LOGE("malloc for EOCD record failed\n");
106 fclose(f);
107 return VERIFY_FAILURE;
108 }
109 if (fread(eocd, 1, eocd_size, f) != eocd_size) {
110 LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno));
111 fclose(f);
112 return VERIFY_FAILURE;
113 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114
Doug Zongker54e2e862009-08-17 13:21:04 -0700115 // If this is really is the EOCD record, it will begin with the
116 // magic number $50 $4b $05 $06.
117 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
118 eocd[2] != 0x05 || eocd[3] != 0x06) {
119 LOGE("signature length doesn't match EOCD marker\n");
120 fclose(f);
121 return VERIFY_FAILURE;
122 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123
Doug Zongker28ce47c2011-10-28 10:33:05 -0700124 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700125 for (i = 4; i < eocd_size-3; ++i) {
126 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800127 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700128 // if the sequence $50 $4b $05 $06 appears anywhere after
129 // the real one, minzip will find the later (wrong) one,
130 // which could be exploitable. Fail verification if
131 // this sequence occurs anywhere after the real one.
132 LOGE("EOCD marker occurs after start of EOCD\n");
133 fclose(f);
134 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 }
136 }
137
Doug Zongker54e2e862009-08-17 13:21:04 -0700138#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139
Doug Zongker54e2e862009-08-17 13:21:04 -0700140 SHA_CTX ctx;
141 SHA_init(&ctx);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700142 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700143 if (buffer == NULL) {
144 LOGE("failed to alloc memory for sha1 buffer\n");
145 fclose(f);
146 return VERIFY_FAILURE;
147 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148
Doug Zongker54e2e862009-08-17 13:21:04 -0700149 double frac = -1.0;
150 size_t so_far = 0;
151 fseek(f, 0, SEEK_SET);
152 while (so_far < signed_len) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700153 size_t size = BUFFER_SIZE;
Doug Zongker54e2e862009-08-17 13:21:04 -0700154 if (signed_len - so_far < size) size = signed_len - so_far;
155 if (fread(buffer, 1, size, f) != size) {
156 LOGE("failed to read data from %s (%s)\n", path, strerror(errno));
157 fclose(f);
158 return VERIFY_FAILURE;
159 }
160 SHA_update(&ctx, buffer, size);
161 so_far += size;
162 double f = so_far / (double)signed_len;
163 if (f > frac + 0.02 || size == so_far) {
164 ui_set_progress(f);
165 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166 }
167 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700168 fclose(f);
169 free(buffer);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170
Doug Zongker54e2e862009-08-17 13:21:04 -0700171 const uint8_t* sha1 = SHA_final(&ctx);
172 for (i = 0; i < numKeys; ++i) {
Doug Zongker73ae31c2009-12-09 17:01:45 -0800173 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700174 // the signing tool appends after the signature itself.
175 if (RSA_verify(pKeys+i, eocd + eocd_size - 6 - RSANUMBYTES,
176 RSANUMBYTES, sha1)) {
Doug Zongker47626332011-03-15 12:11:08 -0700177 LOGI("whole-file signature verified against key %d\n", i);
Doug Zongker54e2e862009-08-17 13:21:04 -0700178 free(eocd);
179 return VERIFY_SUCCESS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180 }
181 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700182 free(eocd);
183 LOGE("failed to verify whole-file signature\n");
184 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800185}