blob: 2b9ba9d35f22b5d412fe6948c6038191e3974f1c [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
Kenny Root7a4adb52013-10-09 10:14:35 -070017#include "asn1_decoder.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include "common.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070019#include "ui.h"
Kenny Root7a4adb52013-10-09 10:14:35 -070020#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021
Kenny Root7a4adb52013-10-09 10:14:35 -070022#include "mincrypt/dsa_sig.h"
23#include "mincrypt/p256.h"
24#include "mincrypt/p256_ecdsa.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include "mincrypt/rsa.h"
26#include "mincrypt/sha.h"
Doug Zongkerbac7fba2013-04-10 11:32:17 -070027#include "mincrypt/sha256.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Doug Zongker54e2e862009-08-17 13:21:04 -070029#include <errno.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080030#include <malloc.h>
31#include <stdio.h>
32#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Dees_Troy2673cec2013-04-02 20:22:16 +000034//extern RecoveryUI* ui;
35
36#define PUBLIC_KEYS_FILE "/res/keys"
37
Kenny Root7a4adb52013-10-09 10:14:35 -070038/*
39 * Simple version of PKCS#7 SignedData extraction. This extracts the
40 * signature OCTET STRING to be used for signature verification.
41 *
42 * For full details, see http://www.ietf.org/rfc/rfc3852.txt
43 *
44 * The PKCS#7 structure looks like:
45 *
46 * SEQUENCE (ContentInfo)
47 * OID (ContentType)
48 * [0] (content)
49 * SEQUENCE (SignedData)
50 * INTEGER (version CMSVersion)
51 * SET (DigestAlgorithmIdentifiers)
52 * SEQUENCE (EncapsulatedContentInfo)
53 * [0] (CertificateSet OPTIONAL)
54 * [1] (RevocationInfoChoices OPTIONAL)
55 * SET (SignerInfos)
56 * SEQUENCE (SignerInfo)
57 * INTEGER (CMSVersion)
58 * SEQUENCE (SignerIdentifier)
59 * SEQUENCE (DigestAlgorithmIdentifier)
60 * SEQUENCE (SignatureAlgorithmIdentifier)
61 * OCTET STRING (SignatureValue)
62 */
63static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der,
64 size_t* sig_der_length) {
65 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
66 if (ctx == NULL) {
67 return false;
68 }
69
70 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
71 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
72 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
73 if (signed_data_app != NULL) {
74 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
75 if (signed_data_seq != NULL
76 && asn1_sequence_next(signed_data_seq)
77 && asn1_sequence_next(signed_data_seq)
78 && asn1_sequence_next(signed_data_seq)
79 && asn1_constructed_skip_all(signed_data_seq)) {
80 asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
81 if (sig_set != NULL) {
82 asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
83 if (sig_seq != NULL
84 && asn1_sequence_next(sig_seq)
85 && asn1_sequence_next(sig_seq)
86 && asn1_sequence_next(sig_seq)
87 && asn1_sequence_next(sig_seq)) {
88 uint8_t* sig_der_ptr;
89 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) {
90 *sig_der = (uint8_t*) malloc(*sig_der_length);
91 if (*sig_der != NULL) {
92 memcpy(*sig_der, sig_der_ptr, *sig_der_length);
93 }
94 }
95 asn1_context_free(sig_seq);
96 }
97 asn1_context_free(sig_set);
98 }
99 asn1_context_free(signed_data_seq);
100 }
101 asn1_context_free(signed_data_app);
102 }
103 asn1_context_free(pkcs7_seq);
104 }
105 asn1_context_free(ctx);
106
107 return *sig_der != NULL;
108}
109
Doug Zongker54e2e862009-08-17 13:21:04 -0700110// Look for an RSA signature embedded in the .ZIP file comment given
111// the path to the zip. Verify it matches one of the given public
112// keys.
113//
114// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
115// or no key matches the signature).
Ethan Yonkera1674162014-11-06 08:35:10 -0600116int verify_file(unsigned char* addr, size_t length) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000117 //ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -0700118
Dees Troybb4c0cb2013-11-02 20:25:14 +0000119 int numKeys;
120 Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
121 if (pKeys == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000122 LOGE("Failed to load keys\n");
Dees Troybb4c0cb2013-11-02 20:25:14 +0000123 return INSTALL_CORRUPT;
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 }
Dees Troybb4c0cb2013-11-02 20:25:14 +0000125 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
Doug Zongker54e2e862009-08-17 13:21:04 -0700126
Doug Zongker54e2e862009-08-17 13:21:04 -0700127 // An archive with a whole-file signature will end in six bytes:
128 //
Doug Zongker73ae31c2009-12-09 17:01:45 -0800129 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -0700130 //
131 // (As far as the ZIP format is concerned, these are part of the
132 // archive comment.) We start by reading this footer, this tells
133 // us how far back from the end we have to start reading to find
134 // the whole comment.
135
136#define FOOTER_SIZE 6
137
Doug Zongker99916f02014-01-13 14:16:58 -0800138 if (length < FOOTER_SIZE) {
139 LOGE("not big enough to contain footer\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700140 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 }
142
Doug Zongker99916f02014-01-13 14:16:58 -0800143 unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144
Doug Zongker54e2e862009-08-17 13:21:04 -0700145 if (footer[2] != 0xff || footer[3] != 0xff) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700146 LOGE("footer is wrong\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700147 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 }
149
Doug Zongker28ce47c2011-10-28 10:33:05 -0700150 size_t comment_size = footer[4] + (footer[5] << 8);
151 size_t signature_start = footer[0] + (footer[1] << 8);
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700152 LOGI("comment is %zu bytes; signature %zu bytes from end\n",
Doug Zongker54e2e862009-08-17 13:21:04 -0700153 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154
Tianjie Xu8ca3ae42016-12-16 16:24:09 -0800155 if (signature_start > comment_size) {
156 LOGE("signature start: %zu is larger than comment size: %zu\n", signature_start,
157 comment_size);
158 return VERIFY_FAILURE;
159 }
160
Kenny Root7a4adb52013-10-09 10:14:35 -0700161 if (signature_start <= FOOTER_SIZE) {
162 LOGE("Signature start is in the footer");
Doug Zongker54e2e862009-08-17 13:21:04 -0700163 return VERIFY_FAILURE;
164 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
Doug Zongker54e2e862009-08-17 13:21:04 -0700166#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167
Doug Zongker54e2e862009-08-17 13:21:04 -0700168 // The end-of-central-directory record is 22 bytes plus any
169 // comment length.
170 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171
Doug Zongker99916f02014-01-13 14:16:58 -0800172 if (length < eocd_size) {
173 LOGE("not big enough to contain EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700174 return VERIFY_FAILURE;
175 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176
Doug Zongker54e2e862009-08-17 13:21:04 -0700177 // Determine how much of the file is covered by the signature.
178 // This is everything except the signature data and length, which
179 // includes all of the EOCD except for the comment length field (2
180 // bytes) and the comment data.
Doug Zongker99916f02014-01-13 14:16:58 -0800181 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182
Doug Zongker99916f02014-01-13 14:16:58 -0800183 unsigned char* eocd = addr + length - eocd_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184
Doug Zongker54e2e862009-08-17 13:21:04 -0700185 // If this is really is the EOCD record, it will begin with the
186 // magic number $50 $4b $05 $06.
187 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
188 eocd[2] != 0x05 || eocd[3] != 0x06) {
189 LOGE("signature length doesn't match EOCD marker\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700190 return VERIFY_FAILURE;
191 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192
Doug Zongker28ce47c2011-10-28 10:33:05 -0700193 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700194 for (i = 4; i < eocd_size-3; ++i) {
195 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800196 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700197 // if the sequence $50 $4b $05 $06 appears anywhere after
198 // the real one, minzip will find the later (wrong) one,
199 // which could be exploitable. Fail verification if
200 // this sequence occurs anywhere after the real one.
201 LOGE("EOCD marker occurs after start of EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700202 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 }
204 }
205
Doug Zongker54e2e862009-08-17 13:21:04 -0700206#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700208 bool need_sha1 = false;
209 bool need_sha256 = false;
210 for (i = 0; i < numKeys; ++i) {
211 switch (pKeys[i].hash_len) {
212 case SHA_DIGEST_SIZE: need_sha1 = true; break;
213 case SHA256_DIGEST_SIZE: need_sha256 = true; break;
214 }
215 }
216
217 SHA_CTX sha1_ctx;
218 SHA256_CTX sha256_ctx;
219 SHA_init(&sha1_ctx);
220 SHA256_init(&sha256_ctx);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221
Doug Zongker54e2e862009-08-17 13:21:04 -0700222 double frac = -1.0;
223 size_t so_far = 0;
Doug Zongker54e2e862009-08-17 13:21:04 -0700224 while (so_far < signed_len) {
Doug Zongker99916f02014-01-13 14:16:58 -0800225 size_t size = signed_len - so_far;
226 if (size > BUFFER_SIZE) size = BUFFER_SIZE;
227
228 if (need_sha1) SHA_update(&sha1_ctx, addr + so_far, size);
229 if (need_sha256) SHA256_update(&sha256_ctx, addr + so_far, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700230 so_far += size;
Doug Zongker99916f02014-01-13 14:16:58 -0800231
Doug Zongker54e2e862009-08-17 13:21:04 -0700232 double f = so_far / (double)signed_len;
233 if (f > frac + 0.02 || size == so_far) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 //ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700235 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236 }
237 }
238
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700239 const uint8_t* sha1 = SHA_final(&sha1_ctx);
240 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
241
Kenny Root7a4adb52013-10-09 10:14:35 -0700242 uint8_t* sig_der = NULL;
243 size_t sig_der_length = 0;
244
245 size_t signature_size = signature_start - FOOTER_SIZE;
246 if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der,
247 &sig_der_length)) {
248 LOGE("Could not find signature DER block\n");
Kenny Root7a4adb52013-10-09 10:14:35 -0700249 return VERIFY_FAILURE;
250 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700251
252 /*
253 * Check to make sure at least one of the keys matches the signature. Since
254 * any key can match, we need to try each before determining a verification
255 * failure has happened.
256 */
Doug Zongker54e2e862009-08-17 13:21:04 -0700257 for (i = 0; i < numKeys; ++i) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700258 const uint8_t* hash;
259 switch (pKeys[i].hash_len) {
260 case SHA_DIGEST_SIZE: hash = sha1; break;
261 case SHA256_DIGEST_SIZE: hash = sha256; break;
262 default: continue;
263 }
264
Doug Zongker73ae31c2009-12-09 17:01:45 -0800265 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700266 // the signing tool appends after the signature itself.
Kenny Root7a4adb52013-10-09 10:14:35 -0700267 if (pKeys[i].key_type == Certificate::RSA) {
268 if (sig_der_length < RSANUMBYTES) {
269 // "signature" block isn't big enough to contain an RSA block.
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700270 LOGI("signature is too short for RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700271 continue;
272 }
273
274 if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES,
275 hash, pKeys[i].hash_len)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700276 LOGI("failed to verify against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700277 continue;
278 }
279
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700280 LOGI("whole-file signature verified against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700281 free(sig_der);
282 return VERIFY_SUCCESS;
283 } else if (pKeys[i].key_type == Certificate::EC
284 && pKeys[i].hash_len == SHA256_DIGEST_SIZE) {
285 p256_int r, s;
286 if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700287 LOGI("Not a DSA signature block for EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700288 continue;
289 }
290
291 p256_int p256_hash;
292 p256_from_bin(hash, &p256_hash);
293 if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y),
294 &p256_hash, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700295 LOGI("failed to verify against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700296 continue;
297 }
298
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700299 LOGI("whole-file signature verified against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700300 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700301 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700302 } else {
Kenny Root7a4adb52013-10-09 10:14:35 -0700303 LOGI("Unknown key type %d\n", pKeys[i].key_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304 }
Dees Troybb4c0cb2013-11-02 20:25:14 +0000305 LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700307 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700308 LOGE("failed to verify whole-file signature\n");
309 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310}
Doug Zongker6c249f72012-11-02 15:04:05 -0700311
312// Reads a file containing one or more public keys as produced by
313// DumpPublicKey: this is an RSAPublicKey struct as it would appear
314// as a C source literal, eg:
315//
316// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
317//
318// For key versions newer than the original 2048-bit e=3 keys
319// supported by Android, the string is preceded by a version
320// identifier, eg:
321//
322// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
323//
324// (Note that the braces and commas in this example are actual
325// characters the parser expects to find in the file; the ellipses
326// indicate more numbers omitted from this example.)
327//
328// The file may contain multiple keys in this format, separated by
329// commas. The last key must not be followed by a comma.
330//
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700331// A Certificate is a pair of an RSAPublicKey and a particular hash
332// (we support SHA-1 and SHA-256; we store the hash length to signify
333// which is being used). The hash used is implied by the version number.
334//
335// 1: 2048-bit RSA key with e=3 and SHA-1 hash
336// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
337// 3: 2048-bit RSA key with e=3 and SHA-256 hash
338// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700339// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700340//
Doug Zongker6c249f72012-11-02 15:04:05 -0700341// Returns NULL if the file failed to parse, or if it contain zero keys.
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700342Certificate*
Doug Zongker6c249f72012-11-02 15:04:05 -0700343load_keys(const char* filename, int* numKeys) {
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700344 Certificate* out = NULL;
Doug Zongker6c249f72012-11-02 15:04:05 -0700345 *numKeys = 0;
346
347 FILE* f = fopen(filename, "r");
348 if (f == NULL) {
349 LOGE("opening %s: %s\n", filename, strerror(errno));
350 goto exit;
351 }
352
353 {
354 int i;
355 bool done = false;
356 while (!done) {
357 ++*numKeys;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700358 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
359 Certificate* cert = out + (*numKeys - 1);
Kenny Root7a4adb52013-10-09 10:14:35 -0700360 memset(cert, '\0', sizeof(Certificate));
Doug Zongker6c249f72012-11-02 15:04:05 -0700361
362 char start_char;
363 if (fscanf(f, " %c", &start_char) != 1) goto exit;
364 if (start_char == '{') {
365 // a version 1 key has no version specifier.
Kenny Root7a4adb52013-10-09 10:14:35 -0700366 cert->key_type = Certificate::RSA;
367 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
368 cert->rsa->exponent = 3;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700369 cert->hash_len = SHA_DIGEST_SIZE;
Doug Zongker6c249f72012-11-02 15:04:05 -0700370 } else if (start_char == 'v') {
371 int version;
372 if (fscanf(f, "%d {", &version) != 1) goto exit;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700373 switch (version) {
374 case 2:
Kenny Root7a4adb52013-10-09 10:14:35 -0700375 cert->key_type = Certificate::RSA;
376 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
377 cert->rsa->exponent = 65537;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700378 cert->hash_len = SHA_DIGEST_SIZE;
379 break;
380 case 3:
Kenny Root7a4adb52013-10-09 10:14:35 -0700381 cert->key_type = Certificate::RSA;
382 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
383 cert->rsa->exponent = 3;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700384 cert->hash_len = SHA256_DIGEST_SIZE;
385 break;
386 case 4:
Kenny Root7a4adb52013-10-09 10:14:35 -0700387 cert->key_type = Certificate::RSA;
388 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
389 cert->rsa->exponent = 65537;
390 cert->hash_len = SHA256_DIGEST_SIZE;
391 break;
392 case 5:
393 cert->key_type = Certificate::EC;
394 cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey));
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700395 cert->hash_len = SHA256_DIGEST_SIZE;
396 break;
397 default:
398 goto exit;
Doug Zongker6c249f72012-11-02 15:04:05 -0700399 }
400 }
401
Kenny Root7a4adb52013-10-09 10:14:35 -0700402 if (cert->key_type == Certificate::RSA) {
403 RSAPublicKey* key = cert->rsa;
404 if (fscanf(f, " %i , 0x%x , { %u",
405 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
406 goto exit;
407 }
408 if (key->len != RSANUMWORDS) {
409 LOGE("key length (%d) does not match expected size\n", key->len);
410 goto exit;
411 }
412 for (i = 1; i < key->len; ++i) {
413 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
414 }
415 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
416 for (i = 1; i < key->len; ++i) {
417 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
418 }
419 fscanf(f, " } } ");
420
421 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
422 } else if (cert->key_type == Certificate::EC) {
423 ECPublicKey* key = cert->ec;
424 int key_len;
425 unsigned int byte;
426 uint8_t x_bytes[P256_NBYTES];
427 uint8_t y_bytes[P256_NBYTES];
428 if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit;
429 if (key_len != P256_NBYTES) {
430 LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES);
431 goto exit;
432 }
433 x_bytes[P256_NBYTES - 1] = byte;
434 for (i = P256_NBYTES - 2; i >= 0; --i) {
435 if (fscanf(f, " , %u", &byte) != 1) goto exit;
436 x_bytes[i] = byte;
437 }
438 if (fscanf(f, " } , { %u", &byte) != 1) goto exit;
439 y_bytes[P256_NBYTES - 1] = byte;
440 for (i = P256_NBYTES - 2; i >= 0; --i) {
441 if (fscanf(f, " , %u", &byte) != 1) goto exit;
442 y_bytes[i] = byte;
443 }
444 fscanf(f, " } } ");
445 p256_from_bin(x_bytes, &key->x);
446 p256_from_bin(y_bytes, &key->y);
447 } else {
448 LOGE("Unknown key type %d\n", cert->key_type);
Doug Zongker6c249f72012-11-02 15:04:05 -0700449 goto exit;
450 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700451
452 // if the line ends in a comma, this file has more keys.
453 switch (fgetc(f)) {
454 case ',':
455 // more keys to come.
456 break;
457
458 case EOF:
459 done = true;
460 break;
461
462 default:
463 LOGE("unexpected character between keys\n");
464 goto exit;
465 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700466 }
467 }
468
469 fclose(f);
470 return out;
471
472exit:
473 if (f) fclose(f);
474 free(out);
475 *numKeys = 0;
476 return NULL;
477}