blob: 9a2d60c66acbe6e05a5a5263415c41be4cbafe91 [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,
Tao Bao71e3e092016-02-02 14:02:27 -0800116 const std::vector<Certificate>& keys) {
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
Kenny Root7a4adb52013-10-09 10:14:35 -0700147 if (signature_start <= FOOTER_SIZE) {
148 LOGE("Signature start is in the footer");
Doug Zongker54e2e862009-08-17 13:21:04 -0700149 return VERIFY_FAILURE;
150 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151
Doug Zongker54e2e862009-08-17 13:21:04 -0700152#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153
Doug Zongker54e2e862009-08-17 13:21:04 -0700154 // The end-of-central-directory record is 22 bytes plus any
155 // comment length.
156 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
Doug Zongker99916f02014-01-13 14:16:58 -0800158 if (length < eocd_size) {
159 LOGE("not big enough to contain EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700160 return VERIFY_FAILURE;
161 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162
Doug Zongker54e2e862009-08-17 13:21:04 -0700163 // Determine how much of the file is covered by the signature.
164 // This is everything except the signature data and length, which
165 // includes all of the EOCD except for the comment length field (2
166 // bytes) and the comment data.
Doug Zongker99916f02014-01-13 14:16:58 -0800167 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Doug Zongker99916f02014-01-13 14:16:58 -0800169 unsigned char* eocd = addr + length - eocd_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170
Doug Zongker54e2e862009-08-17 13:21:04 -0700171 // If this is really is the EOCD record, it will begin with the
172 // magic number $50 $4b $05 $06.
173 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
174 eocd[2] != 0x05 || eocd[3] != 0x06) {
175 LOGE("signature length doesn't match EOCD marker\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700176 return VERIFY_FAILURE;
177 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800178
Tao Bao71e3e092016-02-02 14:02:27 -0800179 for (size_t i = 4; i < eocd_size-3; ++i) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700180 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800181 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700182 // if the sequence $50 $4b $05 $06 appears anywhere after
183 // the real one, minzip will find the later (wrong) one,
184 // which could be exploitable. Fail verification if
185 // this sequence occurs anywhere after the real one.
186 LOGE("EOCD marker occurs after start of EOCD\n");
Doug Zongker54e2e862009-08-17 13:21:04 -0700187 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 }
189 }
190
Doug Zongker54e2e862009-08-17 13:21:04 -0700191#define BUFFER_SIZE 4096
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192
Doug Zongker30362a62013-04-10 11:32:17 -0700193 bool need_sha1 = false;
194 bool need_sha256 = false;
Tao Bao71e3e092016-02-02 14:02:27 -0800195 for (const auto& key : keys) {
196 switch (key.hash_len) {
Doug Zongker30362a62013-04-10 11:32:17 -0700197 case SHA_DIGEST_SIZE: need_sha1 = true; break;
198 case SHA256_DIGEST_SIZE: need_sha256 = true; break;
199 }
200 }
201
202 SHA_CTX sha1_ctx;
203 SHA256_CTX sha256_ctx;
204 SHA_init(&sha1_ctx);
205 SHA256_init(&sha256_ctx);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206
Doug Zongker54e2e862009-08-17 13:21:04 -0700207 double frac = -1.0;
208 size_t so_far = 0;
Doug Zongker54e2e862009-08-17 13:21:04 -0700209 while (so_far < signed_len) {
Doug Zongker99916f02014-01-13 14:16:58 -0800210 size_t size = signed_len - so_far;
211 if (size > BUFFER_SIZE) size = BUFFER_SIZE;
212
213 if (need_sha1) SHA_update(&sha1_ctx, addr + so_far, size);
214 if (need_sha256) SHA256_update(&sha256_ctx, addr + so_far, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700215 so_far += size;
Doug Zongker99916f02014-01-13 14:16:58 -0800216
Doug Zongker54e2e862009-08-17 13:21:04 -0700217 double f = so_far / (double)signed_len;
218 if (f > frac + 0.02 || size == so_far) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700219 ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700220 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 }
222 }
223
Doug Zongker30362a62013-04-10 11:32:17 -0700224 const uint8_t* sha1 = SHA_final(&sha1_ctx);
225 const uint8_t* sha256 = SHA256_final(&sha256_ctx);
226
Tao Bao71e3e092016-02-02 14:02:27 -0800227 uint8_t* sig_der = nullptr;
Kenny Root7a4adb52013-10-09 10:14:35 -0700228 size_t sig_der_length = 0;
229
230 size_t signature_size = signature_start - FOOTER_SIZE;
231 if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der,
232 &sig_der_length)) {
233 LOGE("Could not find signature DER block\n");
Kenny Root7a4adb52013-10-09 10:14:35 -0700234 return VERIFY_FAILURE;
235 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700236
237 /*
238 * Check to make sure at least one of the keys matches the signature. Since
239 * any key can match, we need to try each before determining a verification
240 * failure has happened.
241 */
Tao Bao71e3e092016-02-02 14:02:27 -0800242 size_t i = 0;
243 for (const auto& key : keys) {
Doug Zongker30362a62013-04-10 11:32:17 -0700244 const uint8_t* hash;
Tao Bao71e3e092016-02-02 14:02:27 -0800245 switch (key.hash_len) {
Doug Zongker30362a62013-04-10 11:32:17 -0700246 case SHA_DIGEST_SIZE: hash = sha1; break;
247 case SHA256_DIGEST_SIZE: hash = sha256; break;
248 default: continue;
249 }
250
Doug Zongker73ae31c2009-12-09 17:01:45 -0800251 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700252 // the signing tool appends after the signature itself.
Tao Bao71e3e092016-02-02 14:02:27 -0800253 if (key.key_type == Certificate::RSA) {
Kenny Root7a4adb52013-10-09 10:14:35 -0700254 if (sig_der_length < RSANUMBYTES) {
255 // "signature" block isn't big enough to contain an RSA block.
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700256 LOGI("signature is too short for RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700257 continue;
258 }
259
Tao Bao71e3e092016-02-02 14:02:27 -0800260 if (!RSA_verify(key.rsa.get(), sig_der, RSANUMBYTES,
261 hash, key.hash_len)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700262 LOGI("failed to verify against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700263 continue;
264 }
265
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700266 LOGI("whole-file signature verified against RSA key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700267 free(sig_der);
268 return VERIFY_SUCCESS;
Tao Bao71e3e092016-02-02 14:02:27 -0800269 } else if (key.key_type == Certificate::EC
270 && key.hash_len == SHA256_DIGEST_SIZE) {
Kenny Root7a4adb52013-10-09 10:14:35 -0700271 p256_int r, s;
272 if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700273 LOGI("Not a DSA signature block for EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700274 continue;
275 }
276
277 p256_int p256_hash;
278 p256_from_bin(hash, &p256_hash);
Tao Bao71e3e092016-02-02 14:02:27 -0800279 if (!p256_ecdsa_verify(&(key.ec->x), &(key.ec->y),
Kenny Root7a4adb52013-10-09 10:14:35 -0700280 &p256_hash, &r, &s)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700281 LOGI("failed to verify against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700282 continue;
283 }
284
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700285 LOGI("whole-file signature verified against EC key %zu\n", i);
Kenny Root7a4adb52013-10-09 10:14:35 -0700286 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700287 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700288 } else {
Tao Bao71e3e092016-02-02 14:02:27 -0800289 LOGI("Unknown key type %d\n", key.key_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290 }
Tao Bao71e3e092016-02-02 14:02:27 -0800291 i++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700293 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700294 LOGE("failed to verify whole-file signature\n");
295 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296}
Doug Zongker6c249f72012-11-02 15:04:05 -0700297
298// Reads a file containing one or more public keys as produced by
299// DumpPublicKey: this is an RSAPublicKey struct as it would appear
300// as a C source literal, eg:
301//
302// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
303//
304// For key versions newer than the original 2048-bit e=3 keys
305// supported by Android, the string is preceded by a version
306// identifier, eg:
307//
308// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
309//
310// (Note that the braces and commas in this example are actual
311// characters the parser expects to find in the file; the ellipses
312// indicate more numbers omitted from this example.)
313//
314// The file may contain multiple keys in this format, separated by
315// commas. The last key must not be followed by a comma.
316//
Doug Zongker30362a62013-04-10 11:32:17 -0700317// A Certificate is a pair of an RSAPublicKey and a particular hash
318// (we support SHA-1 and SHA-256; we store the hash length to signify
319// which is being used). The hash used is implied by the version number.
320//
321// 1: 2048-bit RSA key with e=3 and SHA-1 hash
322// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
323// 3: 2048-bit RSA key with e=3 and SHA-256 hash
324// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700325// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700326//
Tao Bao71e3e092016-02-02 14:02:27 -0800327// Returns true on success, and appends the found keys (at least one) to certs.
328// Otherwise returns false if the file failed to parse, or if it contains zero
329// keys. The contents in certs would be unspecified on failure.
330bool load_keys(const char* filename, std::vector<Certificate>& certs) {
331 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
332 if (!f) {
Doug Zongker6c249f72012-11-02 15:04:05 -0700333 LOGE("opening %s: %s\n", filename, strerror(errno));
Tao Bao71e3e092016-02-02 14:02:27 -0800334 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700335 }
336
Tao Bao71e3e092016-02-02 14:02:27 -0800337 while (true) {
338 certs.emplace_back(0, Certificate::RSA, nullptr, nullptr);
339 Certificate& cert = certs.back();
Doug Zongker6c249f72012-11-02 15:04:05 -0700340
Tao Bao71e3e092016-02-02 14:02:27 -0800341 char start_char;
342 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
343 if (start_char == '{') {
344 // a version 1 key has no version specifier.
345 cert.key_type = Certificate::RSA;
346 cert.rsa = std::unique_ptr<RSAPublicKey>(new RSAPublicKey);
347 cert.rsa->exponent = 3;
348 cert.hash_len = SHA_DIGEST_SIZE;
349 } else if (start_char == 'v') {
350 int version;
351 if (fscanf(f.get(), "%d {", &version) != 1) return false;
352 switch (version) {
353 case 2:
354 cert.key_type = Certificate::RSA;
355 cert.rsa = std::unique_ptr<RSAPublicKey>(new RSAPublicKey);
356 cert.rsa->exponent = 65537;
357 cert.hash_len = SHA_DIGEST_SIZE;
358 break;
359 case 3:
360 cert.key_type = Certificate::RSA;
361 cert.rsa = std::unique_ptr<RSAPublicKey>(new RSAPublicKey);
362 cert.rsa->exponent = 3;
363 cert.hash_len = SHA256_DIGEST_SIZE;
364 break;
365 case 4:
366 cert.key_type = Certificate::RSA;
367 cert.rsa = std::unique_ptr<RSAPublicKey>(new RSAPublicKey);
368 cert.rsa->exponent = 65537;
369 cert.hash_len = SHA256_DIGEST_SIZE;
370 break;
371 case 5:
372 cert.key_type = Certificate::EC;
373 cert.ec = std::unique_ptr<ECPublicKey>(new ECPublicKey);
374 cert.hash_len = SHA256_DIGEST_SIZE;
375 break;
376 default:
377 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700378 }
Tao Bao71e3e092016-02-02 14:02:27 -0800379 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700380
Tao Bao71e3e092016-02-02 14:02:27 -0800381 if (cert.key_type == Certificate::RSA) {
382 RSAPublicKey* key = cert.rsa.get();
383 if (fscanf(f.get(), " %i , 0x%x , { %u", &(key->len), &(key->n0inv),
384 &(key->n[0])) != 3) {
385 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700386 }
Tao Bao71e3e092016-02-02 14:02:27 -0800387 if (key->len != RSANUMWORDS) {
388 LOGE("key length (%d) does not match expected size\n", key->len);
389 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700390 }
Tao Bao71e3e092016-02-02 14:02:27 -0800391 for (int i = 1; i < key->len; ++i) {
392 if (fscanf(f.get(), " , %u", &(key->n[i])) != 1) return false;
393 }
394 if (fscanf(f.get(), " } , { %u", &(key->rr[0])) != 1) return false;
395 for (int i = 1; i < key->len; ++i) {
396 if (fscanf(f.get(), " , %u", &(key->rr[i])) != 1) return false;
397 }
398 fscanf(f.get(), " } } ");
399
400 LOGI("read key e=%d hash=%d\n", key->exponent, cert.hash_len);
401 } else if (cert.key_type == Certificate::EC) {
402 ECPublicKey* key = cert.ec.get();
403 int key_len;
404 unsigned int byte;
405 uint8_t x_bytes[P256_NBYTES];
406 uint8_t y_bytes[P256_NBYTES];
407 if (fscanf(f.get(), " %i , { %u", &key_len, &byte) != 2) return false;
408 if (key_len != P256_NBYTES) {
409 LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES);
410 return false;
411 }
412 x_bytes[P256_NBYTES - 1] = byte;
413 for (int i = P256_NBYTES - 2; i >= 0; --i) {
414 if (fscanf(f.get(), " , %u", &byte) != 1) return false;
415 x_bytes[i] = byte;
416 }
417 if (fscanf(f.get(), " } , { %u", &byte) != 1) return false;
418 y_bytes[P256_NBYTES - 1] = byte;
419 for (int i = P256_NBYTES - 2; i >= 0; --i) {
420 if (fscanf(f.get(), " , %u", &byte) != 1) return false;
421 y_bytes[i] = byte;
422 }
423 fscanf(f.get(), " } } ");
424 p256_from_bin(x_bytes, &key->x);
425 p256_from_bin(y_bytes, &key->y);
426 } else {
427 LOGE("Unknown key type %d\n", cert.key_type);
428 return false;
429 }
430
431 // if the line ends in a comma, this file has more keys.
432 int ch = fgetc(f.get());
433 if (ch == ',') {
434 // more keys to come.
435 continue;
436 } else if (ch == EOF) {
437 break;
438 } else {
439 LOGE("unexpected character between keys\n");
440 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700441 }
442 }
443
Tao Bao71e3e092016-02-02 14:02:27 -0800444 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700445}