aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoug Zongker2011-10-28 12:33:05 -0500
committerDoug Zongker2011-10-31 12:24:09 -0500
commit28ce47cfa6919518a8c298088bf08b03bb374c0f (patch)
tree0c429bcc8f4a1935f3be97d87ebdc2f4434ee887 /verifier.cpp
parentd0181b8fcdac761afec252151a8bfda116916e96 (diff)
downloadplatform-bootable-recovery-28ce47cfa6919518a8c298088bf08b03bb374c0f.tar.gz
platform-bootable-recovery-28ce47cfa6919518a8c298088bf08b03bb374c0f.tar.xz
platform-bootable-recovery-28ce47cfa6919518a8c298088bf08b03bb374c0f.zip
turn recovery into a C++ binary
Change-Id: I423a23581048d451d53eef46e5f5eac485b77555
Diffstat (limited to 'verifier.cpp')
-rw-r--r--verifier.cpp185
1 files changed, 185 insertions, 0 deletions
diff --git a/verifier.cpp b/verifier.cpp
new file mode 100644
index 00000000..58ca7239
--- /dev/null
+++ b/verifier.cpp
@@ -0,0 +1,185 @@
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
24#include <string.h>
25#include <stdio.h>
26#include <errno.h>
27
28// 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;
42 }
43
44 // An archive with a whole-file signature will end in six bytes:
45 //
46 // (2-byte signature start) $ff $ff (2-byte comment size)
47 //
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;
59 }
60
61 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;
66 }
67
68 if (footer[2] != 0xff || footer[3] != 0xff) {
69 fclose(f);
70 return VERIFY_FAILURE;
71 }
72
73 size_t comment_size = footer[4] + (footer[5] << 8);
74 size_t signature_start = footer[0] + (footer[1] << 8);
75 LOGI("comment is %d bytes; signature %d bytes from end\n",
76 comment_size, signature_start);
77
78 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 }
84
85#define EOCD_HEADER_SIZE 22
86
87 // The end-of-central-directory record is 22 bytes plus any
88 // comment length.
89 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
90
91 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 }
96
97 // 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;
102
103 unsigned char* eocd = (unsigned char*)malloc(eocd_size);
104 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 }
114
115 // 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 }
123
124 size_t i;
125 for (i = 4; i < eocd_size-3; ++i) {
126 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
127 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
128 // 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;
135 }
136 }
137
138#define BUFFER_SIZE 4096
139
140 SHA_CTX ctx;
141 SHA_init(&ctx);
142 unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE);
143 if (buffer == NULL) {
144 LOGE("failed to alloc memory for sha1 buffer\n");
145 fclose(f);
146 return VERIFY_FAILURE;
147 }
148
149 double frac = -1.0;
150 size_t so_far = 0;
151 fseek(f, 0, SEEK_SET);
152 while (so_far < signed_len) {
153 size_t size = BUFFER_SIZE;
154 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;
166 }
167 }
168 fclose(f);
169 free(buffer);
170
171 const uint8_t* sha1 = SHA_final(&ctx);
172 for (i = 0; i < numKeys; ++i) {
173 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
174 // the signing tool appends after the signature itself.
175 if (RSA_verify(pKeys+i, eocd + eocd_size - 6 - RSANUMBYTES,
176 RSANUMBYTES, sha1)) {
177 LOGI("whole-file signature verified against key %d\n", i);
178 free(eocd);
179 return VERIFY_SUCCESS;
180 }
181 }
182 free(eocd);
183 LOGE("failed to verify whole-file signature\n");
184 return VERIFY_FAILURE;
185}