blob: bf7071d13c43c40dd47dfa2417117585a5833d62 [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 Zongker30362a62013-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
Doug Zongker211aebc2011-10-28 15:13:10 -070034extern RecoveryUI* ui;
35
Kenny Root7a4adb52013-10-09 10:14:35 -070036/*
37 * Simple version of PKCS#7 SignedData extraction. This extracts the
38 * signature OCTET STRING to be used for signature verification.
39 *
40 * For full details, see http://www.ietf.org/rfc/rfc3852.txt
41 *
42 * The PKCS#7 structure looks like:
43 *
44 * SEQUENCE (ContentInfo)
45 * OID (ContentType)
46 * [0] (content)
47 * SEQUENCE (SignedData)
48 * INTEGER (version CMSVersion)
49 * SET (DigestAlgorithmIdentifiers)
50 * SEQUENCE (EncapsulatedContentInfo)
51 * [0] (CertificateSet OPTIONAL)
52 * [1] (RevocationInfoChoices OPTIONAL)
53 * SET (SignerInfos)
54 * SEQUENCE (SignerInfo)
55 * INTEGER (CMSVersion)
56 * SEQUENCE (SignerIdentifier)
57 * SEQUENCE (DigestAlgorithmIdentifier)
58 * SEQUENCE (SignatureAlgorithmIdentifier)
59 * OCTET STRING (SignatureValue)
60 */
61static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der,
62 size_t* sig_der_length) {
63 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
64 if (ctx == NULL) {
65 return false;
66 }
67
68 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
69 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
70 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
71 if (signed_data_app != NULL) {
72 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
73 if (signed_data_seq != NULL
74 && asn1_sequence_next(signed_data_seq)
75 && asn1_sequence_next(signed_data_seq)
76 && asn1_sequence_next(signed_data_seq)
77 && asn1_constructed_skip_all(signed_data_seq)) {
78 asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
79 if (sig_set != NULL) {
80 asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
81 if (sig_seq != NULL
82 && asn1_sequence_next(sig_seq)
83 && asn1_sequence_next(sig_seq)
84 && asn1_sequence_next(sig_seq)
85 && asn1_sequence_next(sig_seq)) {
86 uint8_t* sig_der_ptr;
87 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) {
88 *sig_der = (uint8_t*) malloc(*sig_der_length);
89 if (*sig_der != NULL) {
90 memcpy(*sig_der, sig_der_ptr, *sig_der_length);
91 }
92 }
93 asn1_context_free(sig_seq);
94 }
95 asn1_context_free(sig_set);
96 }
97 asn1_context_free(signed_data_seq);
98 }
99 asn1_context_free(signed_data_app);
100 }
101 asn1_context_free(pkcs7_seq);
102 }
103 asn1_context_free(ctx);
104
105 return *sig_der != NULL;
106}
107
Doug Zongker54e2e862009-08-17 13:21:04 -0700108// Look for an RSA signature embedded in the .ZIP file comment given
109// the path to the zip. Verify it matches one of the given public
110// keys.
111//
112// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
113// or no key matches the signature).
114
Doug Zongker99916f02014-01-13 14:16:58 -0800115int verify_file(unsigned char* addr, size_t length,
116 const Certificate* pKeys, unsigned int numKeys) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700117 ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -0700118
Doug Zongker54e2e862009-08-17 13:21:04 -0700119 // An archive with a whole-file signature will end in six bytes:
120 //
Doug Zongker73ae31c2009-12-09 17:01:45 -0800121 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -0700122 //
123 // (As far as the ZIP format is concerned, these are part of the
124 // archive comment.) We start by reading this footer, this tells
125 // us how far back from the end we have to start reading to find
126 // the whole comment.
127
128#define FOOTER_SIZE 6
129
Doug Zongker99916f02014-01-13 14:16:58 -0800130 if (length < FOOTER_SIZE) {
131 LOGE("not big enough to contain footer\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700132 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 }
134
Doug Zongker99916f02014-01-13 14:16:58 -0800135 unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136
Doug Zongker54e2e862009-08-17 13:21:04 -0700137 if (footer[2] != 0xff || footer[3] != 0xff) {
Doug Zongker30362a62013-04-10 11:32:17 -0700138 LOGE("footer is wrong\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700139 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 }
141
Doug Zongker28ce47c2011-10-28 10:33:05 -0700142 size_t comment_size = footer[4] + (footer[5] << 8);
143 size_t signature_start = footer[0] + (footer[1] << 8);
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700144 LOGI("comment is %zu bytes; signature %zu bytes from end\n",
Doug Zongker54e2e862009-08-17 13:21:04 -0700145 comment_size, signature_start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146
Tianjie Xu54ea1362016-12-16 16:24:09 -0800147 if (signature_start > comment_size) {
148 LOGE("signature start: %zu is larger than comment size: %zu\n", signature_start,
149 comment_size);
150 return VERIFY_FAILURE;
151 }
152
Kenny Root7a4adb52013-10-09 10:14:35 -0700153 if (signature_start <= FOOTER_SIZE) {
154 LOGE("Signature start is in the footer");
Doug Zongker54e2e862009-08-17 13:21:04 -0700155 return VERIFY_FAILURE;
156 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
Doug Zongker54e2e862009-08-17 13:21:04 -0700158#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159
Doug Zongker54e2e862009-08-17 13:21:04 -0700160 // The end-of-central-directory record is 22 bytes plus any
161 // comment length.
162 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
Doug Zongker99916f02014-01-13 14:16:58 -0800164 if (length < eocd_size) {
165 LOGE("not big enough to contain EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700166 return VERIFY_FAILURE;
167 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Doug Zongker54e2e862009-08-17 13:21:04 -0700169 // Determine how much of the file is covered by the signature.
170 // This is everything except the signature data and length, which
171 // includes all of the EOCD except for the comment length field (2
172 // bytes) and the comment data.
Doug Zongker99916f02014-01-13 14:16:58 -0800173 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
Doug Zongker99916f02014-01-13 14:16:58 -0800175 unsigned char* eocd = addr + length - eocd_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176
Doug Zongker54e2e862009-08-17 13:21:04 -0700177 // If this is really is the EOCD record, it will begin with the
178 // magic number $50 $4b $05 $06.
179 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
180 eocd[2] != 0x05 || eocd[3] != 0x06) {
181 LOGE("signature length doesn't match EOCD marker\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700182 return VERIFY_FAILURE;
183 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184
Doug Zongker28ce47c2011-10-28 10:33:05 -0700185 size_t i;
Doug Zongker54e2e862009-08-17 13:21:04 -0700186 for (i = 4; i < eocd_size-3; ++i) {
187 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800188 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700189 // if the sequence $50 $4b $05 $06 appears anywhere after
190 // the real one, minzip will find the later (wrong) one,
191 // which could be exploitable. Fail verification if
192 // this sequence occurs anywhere after the real one.
193 LOGE("EOCD marker occurs after start of EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700194 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 }
196 }
197
Doug Zongker54e2e862009-08-17 13:21:04 -0700198#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199
Doug Zongker30362a62013-04-10 11:32:17 -0700200 bool need_sha1 = false;
201 bool need_sha256 = false;
202 for (i = 0; i < numKeys; ++i) {
203 switch (pKeys[i].hash_len) {
204 case SHA_DIGEST_SIZE: need_sha1 = true; break;
205 case SHA256_DIGEST_SIZE: need_sha256 = true; break;
206 }
207 }
208
209 SHA_CTX sha1_ctx;
210 SHA256_CTX sha256_ctx;
211 SHA_init(&sha1_ctx);
212 SHA256_init(&sha256_ctx);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Doug Zongker54e2e862009-08-17 13:21:04 -0700214 double frac = -1.0;
215 size_t so_far = 0;
Doug Zongker54e2e862009-08-17 13:21:04 -0700216 while (so_far < signed_len) {
Doug Zongker99916f02014-01-13 14:16:58 -0800217 size_t size = signed_len - so_far;
218 if (size > BUFFER_SIZE) size = BUFFER_SIZE;
219
220 if (need_sha1) SHA_update(&sha1_ctx, addr + so_far, size);
221 if (need_sha256) SHA256_update(&sha256_ctx, addr + so_far, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700222 so_far += size;
Doug Zongker99916f02014-01-13 14:16:58 -0800223
Doug Zongker54e2e862009-08-17 13:21:04 -0700224 double f = so_far / (double)signed_len;
225 if (f > frac + 0.02 || size == so_far) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700226 ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700227 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228 }
229 }
230
Doug Zongker30362a62013-04-10 11:32:17 -0700231 const uint8_t* sha1 = SHA_final(&sha1_ctx);
232 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
233
Kenny Root7a4adb52013-10-09 10:14:35 -0700234 uint8_t* sig_der = NULL;
235 size_t sig_der_length = 0;
236
237 size_t signature_size = signature_start - FOOTER_SIZE;
238 if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der,
239 &sig_der_length)) {
240 LOGE("Could not find signature DER block\n");
Kenny Root7a4adb52013-10-09 10:14:35 -0700241 return VERIFY_FAILURE;
242 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700243
244 /*
245 * Check to make sure at least one of the keys matches the signature. Since
246 * any key can match, we need to try each before determining a verification
247 * failure has happened.
248 */
Doug Zongker54e2e862009-08-17 13:21:04 -0700249 for (i = 0; i < numKeys; ++i) {
Doug Zongker30362a62013-04-10 11:32:17 -0700250 const uint8_t* hash;
251 switch (pKeys[i].hash_len) {
252 case SHA_DIGEST_SIZE: hash = sha1; break;
253 case SHA256_DIGEST_SIZE: hash = sha256; break;
254 default: continue;
255 }
256
Doug Zongker73ae31c2009-12-09 17:01:45 -0800257 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700258 // the signing tool appends after the signature itself.
Kenny Root7a4adb52013-10-09 10:14:35 -0700259 if (pKeys[i].key_type == Certificate::RSA) {
260 if (sig_der_length < RSANUMBYTES) {
261 // "signature" block isn't big enough to contain an RSA block.
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700262 LOGI("signature is too short for RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700263 continue;
264 }
265
266 if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES,
267 hash, pKeys[i].hash_len)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700268 LOGI("failed to verify against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700269 continue;
270 }
271
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700272 LOGI("whole-file signature verified against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700273 free(sig_der);
274 return VERIFY_SUCCESS;
275 } else if (pKeys[i].key_type == Certificate::EC
276 && pKeys[i].hash_len == SHA256_DIGEST_SIZE) {
277 p256_int r, s;
278 if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700279 LOGI("Not a DSA signature block for EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700280 continue;
281 }
282
283 p256_int p256_hash;
284 p256_from_bin(hash, &p256_hash);
285 if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y),
286 &p256_hash, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700287 LOGI("failed to verify against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700288 continue;
289 }
290
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700291 LOGI("whole-file signature verified against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700292 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700293 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700294 } else {
Kenny Root7a4adb52013-10-09 10:14:35 -0700295 LOGI("Unknown key type %d\n", pKeys[i].key_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 }
297 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700298 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700299 LOGE("failed to verify whole-file signature\n");
300 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301}
Doug Zongker6c249f72012-11-02 15:04:05 -0700302
303// Reads a file containing one or more public keys as produced by
304// DumpPublicKey: this is an RSAPublicKey struct as it would appear
305// as a C source literal, eg:
306//
307// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
308//
309// For key versions newer than the original 2048-bit e=3 keys
310// supported by Android, the string is preceded by a version
311// identifier, eg:
312//
313// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
314//
315// (Note that the braces and commas in this example are actual
316// characters the parser expects to find in the file; the ellipses
317// indicate more numbers omitted from this example.)
318//
319// The file may contain multiple keys in this format, separated by
320// commas. The last key must not be followed by a comma.
321//
Doug Zongker30362a62013-04-10 11:32:17 -0700322// A Certificate is a pair of an RSAPublicKey and a particular hash
323// (we support SHA-1 and SHA-256; we store the hash length to signify
324// which is being used). The hash used is implied by the version number.
325//
326// 1: 2048-bit RSA key with e=3 and SHA-1 hash
327// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
328// 3: 2048-bit RSA key with e=3 and SHA-256 hash
329// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700330// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700331//
Doug Zongker6c249f72012-11-02 15:04:05 -0700332// Returns NULL if the file failed to parse, or if it contain zero keys.
Doug Zongker30362a62013-04-10 11:32:17 -0700333Certificate*
Doug Zongker6c249f72012-11-02 15:04:05 -0700334load_keys(const char* filename, int* numKeys) {
Doug Zongker30362a62013-04-10 11:32:17 -0700335 Certificate* out = NULL;
Doug Zongker6c249f72012-11-02 15:04:05 -0700336 *numKeys = 0;
337
338 FILE* f = fopen(filename, "r");
339 if (f == NULL) {
340 LOGE("opening %s: %s\n", filename, strerror(errno));
341 goto exit;
342 }
343
344 {
345 int i;
346 bool done = false;
347 while (!done) {
348 ++*numKeys;
Doug Zongker30362a62013-04-10 11:32:17 -0700349 out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate));
350 Certificate* cert = out + (*numKeys - 1);
Kenny Root7a4adb52013-10-09 10:14:35 -0700351 memset(cert, '\0', sizeof(Certificate));
Doug Zongker6c249f72012-11-02 15:04:05 -0700352
353 char start_char;
354 if (fscanf(f, " %c", &start_char) != 1) goto exit;
355 if (start_char == '{') {
356 // a version 1 key has no version specifier.
Kenny Root7a4adb52013-10-09 10:14:35 -0700357 cert->key_type = Certificate::RSA;
358 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
359 cert->rsa->exponent = 3;
Doug Zongker30362a62013-04-10 11:32:17 -0700360 cert->hash_len = SHA_DIGEST_SIZE;
Doug Zongker6c249f72012-11-02 15:04:05 -0700361 } else if (start_char == 'v') {
362 int version;
363 if (fscanf(f, "%d {", &version) != 1) goto exit;
Doug Zongker30362a62013-04-10 11:32:17 -0700364 switch (version) {
365 case 2:
Kenny Root7a4adb52013-10-09 10:14:35 -0700366 cert->key_type = Certificate::RSA;
367 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
368 cert->rsa->exponent = 65537;
Doug Zongker30362a62013-04-10 11:32:17 -0700369 cert->hash_len = SHA_DIGEST_SIZE;
370 break;
371 case 3:
Kenny Root7a4adb52013-10-09 10:14:35 -0700372 cert->key_type = Certificate::RSA;
373 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
374 cert->rsa->exponent = 3;
Doug Zongker30362a62013-04-10 11:32:17 -0700375 cert->hash_len = SHA256_DIGEST_SIZE;
376 break;
377 case 4:
Kenny Root7a4adb52013-10-09 10:14:35 -0700378 cert->key_type = Certificate::RSA;
379 cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey));
380 cert->rsa->exponent = 65537;
381 cert->hash_len = SHA256_DIGEST_SIZE;
382 break;
383 case 5:
384 cert->key_type = Certificate::EC;
385 cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey));
Doug Zongker30362a62013-04-10 11:32:17 -0700386 cert->hash_len = SHA256_DIGEST_SIZE;
387 break;
388 default:
389 goto exit;
Doug Zongker6c249f72012-11-02 15:04:05 -0700390 }
391 }
392
Kenny Root7a4adb52013-10-09 10:14:35 -0700393 if (cert->key_type == Certificate::RSA) {
394 RSAPublicKey* key = cert->rsa;
395 if (fscanf(f, " %i , 0x%x , { %u",
396 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
397 goto exit;
398 }
399 if (key->len != RSANUMWORDS) {
400 LOGE("key length (%d) does not match expected size\n", key->len);
401 goto exit;
402 }
403 for (i = 1; i < key->len; ++i) {
404 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
405 }
406 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
407 for (i = 1; i < key->len; ++i) {
408 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
409 }
410 fscanf(f, " } } ");
411
412 LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len);
413 } else if (cert->key_type == Certificate::EC) {
414 ECPublicKey* key = cert->ec;
415 int key_len;
416 unsigned int byte;
417 uint8_t x_bytes[P256_NBYTES];
418 uint8_t y_bytes[P256_NBYTES];
419 if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit;
420 if (key_len != P256_NBYTES) {
421 LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES);
422 goto exit;
423 }
424 x_bytes[P256_NBYTES - 1] = byte;
425 for (i = P256_NBYTES - 2; i >= 0; --i) {
426 if (fscanf(f, " , %u", &byte) != 1) goto exit;
427 x_bytes[i] = byte;
428 }
429 if (fscanf(f, " } , { %u", &byte) != 1) goto exit;
430 y_bytes[P256_NBYTES - 1] = byte;
431 for (i = P256_NBYTES - 2; i >= 0; --i) {
432 if (fscanf(f, " , %u", &byte) != 1) goto exit;
433 y_bytes[i] = byte;
434 }
435 fscanf(f, " } } ");
436 p256_from_bin(x_bytes, &key->x);
437 p256_from_bin(y_bytes, &key->y);
438 } else {
439 LOGE("Unknown key type %d\n", cert->key_type);
Doug Zongker6c249f72012-11-02 15:04:05 -0700440 goto exit;
441 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700442
443 // if the line ends in a comma, this file has more keys.
444 switch (fgetc(f)) {
445 case ',':
446 // more keys to come.
447 break;
448
449 case EOF:
450 done = true;
451 break;
452
453 default:
454 LOGE("unexpected character between keys\n");
455 goto exit;
456 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700457 }
458 }
459
460 fclose(f);
461 return out;
462
463exit:
464 if (f) fclose(f);
465 free(out);
466 *numKeys = 0;
467 return NULL;
468}