blob: 3beaa6e02db46020102d61addc36bcecceb717e6 [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
Tao Baoac9d94d2016-11-03 11:37:15 -070017#include "verifier.h"
18
Doug Zongker54e2e862009-08-17 13:21:04 -070019#include <errno.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080020#include <stdio.h>
Tao Baoac9d94d2016-11-03 11:37:15 -070021#include <stdlib.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023
Elliott Hughes8febafa2016-04-13 16:39:56 -070024#include <algorithm>
Tao Baod7bf82e2017-03-18 09:24:11 -070025#include <functional>
Elliott Hughes8febafa2016-04-13 16:39:56 -070026#include <memory>
27
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070028#include <android-base/logging.h>
David Benjamina86392e2016-04-15 20:22:09 -040029#include <openssl/bn.h>
Elliott Hughes8febafa2016-04-13 16:39:56 -070030#include <openssl/ecdsa.h>
31#include <openssl/obj_mac.h>
32
33#include "asn1_decoder.h"
Tao Baoe1792762016-04-19 22:31:01 -070034#include "print_sha1.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070035
Elliott Hughes8febafa2016-04-13 16:39:56 -070036static constexpr size_t MiB = 1024 * 1024;
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
Tao Bao5e535012017-03-16 17:37:38 -0700110/*
111 * Looks for an RSA signature embedded in the .ZIP file comment given the path to the zip. Verifies
112 * that it matches one of the given public keys. A callback function can be optionally provided for
113 * posting the progress.
114 *
115 * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the
116 * signature).
117 */
118int verify_file(unsigned char* addr, size_t length, const std::vector<Certificate>& keys,
119 const std::function<void(float)>& set_progress) {
120 if (set_progress) {
121 set_progress(0.0);
122 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700123
Tao Bao5e535012017-03-16 17:37:38 -0700124 // An archive with a whole-file signature will end in six bytes:
125 //
126 // (2-byte signature start) $ff $ff (2-byte comment size)
127 //
128 // (As far as the ZIP format is concerned, these are part of the archive comment.) We start by
129 // reading this footer, this tells us how far back from the end we have to start reading to find
130 // the whole comment.
Doug Zongker54e2e862009-08-17 13:21:04 -0700131
132#define FOOTER_SIZE 6
133
Tao Bao5e535012017-03-16 17:37:38 -0700134 if (length < FOOTER_SIZE) {
135 LOG(ERROR) << "not big enough to contain footer";
136 return VERIFY_FAILURE;
137 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138
Tao Bao5e535012017-03-16 17:37:38 -0700139 unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140
Tao Bao5e535012017-03-16 17:37:38 -0700141 if (footer[2] != 0xff || footer[3] != 0xff) {
142 LOG(ERROR) << "footer is wrong";
143 return VERIFY_FAILURE;
144 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
Tao Bao5e535012017-03-16 17:37:38 -0700146 size_t comment_size = footer[4] + (footer[5] << 8);
147 size_t signature_start = footer[0] + (footer[1] << 8);
148 LOG(INFO) << "comment is " << comment_size << " bytes; signature is " << signature_start
149 << " bytes from end";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
Tao Bao5e535012017-03-16 17:37:38 -0700151 if (signature_start <= FOOTER_SIZE) {
152 LOG(ERROR) << "Signature start is in the footer";
153 return VERIFY_FAILURE;
154 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155
Doug Zongker54e2e862009-08-17 13:21:04 -0700156#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
Tao Bao5e535012017-03-16 17:37:38 -0700158 // The end-of-central-directory record is 22 bytes plus any comment length.
159 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160
Tao Bao5e535012017-03-16 17:37:38 -0700161 if (length < eocd_size) {
162 LOG(ERROR) << "not big enough to contain EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700163 return VERIFY_FAILURE;
Tao Bao5e535012017-03-16 17:37:38 -0700164 }
165
166 // Determine how much of the file is covered by the signature. This is everything except the
167 // signature data and length, which includes all of the EOCD except for the comment length field
168 // (2 bytes) and the comment data.
169 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
170
171 unsigned char* eocd = addr + length - eocd_size;
172
173 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
174 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
175 LOG(ERROR) << "signature length doesn't match EOCD marker";
176 return VERIFY_FAILURE;
177 }
178
179 for (size_t i = 4; i < eocd_size-3; ++i) {
180 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
181 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
182 // find the later (wrong) one, which could be exploitable. Fail the verification if this
183 // sequence occurs anywhere after the real one.
184 LOG(ERROR) << "EOCD marker occurs after start of EOCD";
185 return VERIFY_FAILURE;
186 }
187 }
188
189 bool need_sha1 = false;
190 bool need_sha256 = false;
191 for (const auto& key : keys) {
192 switch (key.hash_len) {
193 case SHA_DIGEST_LENGTH: need_sha1 = true; break;
194 case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
195 }
196 }
197
198 SHA_CTX sha1_ctx;
199 SHA256_CTX sha256_ctx;
200 SHA1_Init(&sha1_ctx);
201 SHA256_Init(&sha256_ctx);
202
203 double frac = -1.0;
204 size_t so_far = 0;
205 while (so_far < signed_len) {
206 // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
207 // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
208 // http://b/28135231.
209 size_t size = std::min(signed_len - so_far, 16 * MiB);
210
211 if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
212 if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
213 so_far += size;
214
215 if (set_progress) {
216 double f = so_far / (double)signed_len;
217 if (f > frac + 0.02 || size == so_far) {
218 set_progress(f);
219 frac = f;
220 }
221 }
222 }
223
224 uint8_t sha1[SHA_DIGEST_LENGTH];
225 SHA1_Final(sha1, &sha1_ctx);
226 uint8_t sha256[SHA256_DIGEST_LENGTH];
227 SHA256_Final(sha256, &sha256_ctx);
228
229 uint8_t* sig_der = nullptr;
230 size_t sig_der_length = 0;
231
232 uint8_t* signature = eocd + eocd_size - signature_start;
233 size_t signature_size = signature_start - FOOTER_SIZE;
234
235 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
236 << signature_size << "): " << print_hex(signature, signature_size);
237
238 if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) {
239 LOG(ERROR) << "Could not find signature DER block";
240 return VERIFY_FAILURE;
241 }
242
243 // Check to make sure at least one of the keys matches the signature. Since any key can match,
244 // we need to try each before determining a verification failure has happened.
245 size_t i = 0;
246 for (const auto& key : keys) {
247 const uint8_t* hash;
248 int hash_nid;
249 switch (key.hash_len) {
250 case SHA_DIGEST_LENGTH:
251 hash = sha1;
252 hash_nid = NID_sha1;
253 break;
254 case SHA256_DIGEST_LENGTH:
255 hash = sha256;
256 hash_nid = NID_sha256;
257 break;
258 default:
259 continue;
260 }
261
262 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
263 // after the signature itself.
264 if (key.key_type == Certificate::KEY_TYPE_RSA) {
265 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der, sig_der_length, key.rsa.get())) {
266 LOG(INFO) << "failed to verify against RSA key " << i;
267 continue;
268 }
269
270 LOG(INFO) << "whole-file signature verified against RSA key " << i;
271 free(sig_der);
272 return VERIFY_SUCCESS;
273 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
274 if (!ECDSA_verify(0, hash, key.hash_len, sig_der, sig_der_length, key.ec.get())) {
275 LOG(INFO) << "failed to verify against EC key " << i;
276 continue;
277 }
278
279 LOG(INFO) << "whole-file signature verified against EC key " << i;
280 free(sig_der);
281 return VERIFY_SUCCESS;
282 } else {
283 LOG(INFO) << "Unknown key type " << key.key_type;
284 }
285 i++;
286 }
287
288 if (need_sha1) {
289 LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH);
290 }
291 if (need_sha256) {
292 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
293 }
294 free(sig_der);
295 LOG(ERROR) << "failed to verify whole-file signature";
296 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297}
Doug Zongker6c249f72012-11-02 15:04:05 -0700298
Elliott Hughes8febafa2016-04-13 16:39:56 -0700299std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
300 // Read key length in words and n0inv. n0inv is a precomputed montgomery
301 // parameter derived from the modulus and can be used to speed up
302 // verification. n0inv is 32 bits wide here, assuming the verification logic
303 // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
304 // internally, in which case we don't have a valid n0inv. Thus, we just
305 // ignore the montgomery parameters and have BoringSSL recompute them
306 // internally. If/When the speedup from using the montgomery parameters
307 // becomes relevant, we can add more sophisticated code here to obtain a
308 // 64-bit n0inv and initialize the montgomery parameters in the key object.
309 uint32_t key_len_words = 0;
310 uint32_t n0inv = 0;
311 if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
312 return nullptr;
313 }
314
315 if (key_len_words > 8192 / 32) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700316 LOG(ERROR) << "key length (" << key_len_words << ") too large";
Elliott Hughes8febafa2016-04-13 16:39:56 -0700317 return nullptr;
318 }
319
320 // Read the modulus.
321 std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
322 if (fscanf(file, " , { %u", &modulus[0]) != 1) {
323 return nullptr;
324 }
325 for (uint32_t i = 1; i < key_len_words; ++i) {
326 if (fscanf(file, " , %u", &modulus[i]) != 1) {
327 return nullptr;
328 }
329 }
330
331 // Cconvert from little-endian array of little-endian words to big-endian
332 // byte array suitable as input for BN_bin2bn.
333 std::reverse((uint8_t*)modulus.get(),
334 (uint8_t*)(modulus.get() + key_len_words));
335
336 // The next sequence of values is the montgomery parameter R^2. Since we
337 // generally don't have a valid |n0inv|, we ignore this (see comment above).
338 uint32_t rr_value;
339 if (fscanf(file, " } , { %u", &rr_value) != 1) {
340 return nullptr;
341 }
342 for (uint32_t i = 1; i < key_len_words; ++i) {
343 if (fscanf(file, " , %u", &rr_value) != 1) {
344 return nullptr;
345 }
346 }
347 if (fscanf(file, " } } ") != 0) {
348 return nullptr;
349 }
350
351 // Initialize the key.
352 std::unique_ptr<RSA, RSADeleter> key(RSA_new());
353 if (!key) {
354 return nullptr;
355 }
356
357 key->n = BN_bin2bn((uint8_t*)modulus.get(),
358 key_len_words * sizeof(uint32_t), NULL);
359 if (!key->n) {
360 return nullptr;
361 }
362
363 key->e = BN_new();
364 if (!key->e || !BN_set_word(key->e, exponent)) {
365 return nullptr;
366 }
367
368 return key;
369}
370
371struct BNDeleter {
372 void operator()(BIGNUM* bn) {
373 BN_free(bn);
374 }
375};
376
377std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
378 uint32_t key_len_bytes = 0;
379 if (fscanf(file, " %i", &key_len_bytes) != 1) {
380 return nullptr;
381 }
382
383 std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
384 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
385 if (!group) {
386 return nullptr;
387 }
388
389 // Verify that |key_len| matches the group order.
390 if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
391 return nullptr;
392 }
393
394 // Read the public key coordinates. Note that the byte order in the file is
395 // little-endian, so we convert to big-endian here.
396 std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
397 std::unique_ptr<BIGNUM, BNDeleter> point[2];
398 for (int i = 0; i < 2; ++i) {
399 unsigned int byte = 0;
400 if (fscanf(file, " , { %u", &byte) != 1) {
401 return nullptr;
402 }
403 bytes[key_len_bytes - 1] = byte;
404
405 for (size_t i = 1; i < key_len_bytes; ++i) {
406 if (fscanf(file, " , %u", &byte) != 1) {
407 return nullptr;
408 }
409 bytes[key_len_bytes - i - 1] = byte;
410 }
411
412 point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
413 if (!point[i]) {
414 return nullptr;
415 }
416
417 if (fscanf(file, " }") != 0) {
418 return nullptr;
419 }
420 }
421
422 if (fscanf(file, " } ") != 0) {
423 return nullptr;
424 }
425
426 // Create and initialize the key.
427 std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
428 if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
429 !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
430 point[1].get())) {
431 return nullptr;
432 }
433
434 return key;
435}
436
Doug Zongker6c249f72012-11-02 15:04:05 -0700437// Reads a file containing one or more public keys as produced by
438// DumpPublicKey: this is an RSAPublicKey struct as it would appear
439// as a C source literal, eg:
440//
441// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
442//
443// For key versions newer than the original 2048-bit e=3 keys
444// supported by Android, the string is preceded by a version
445// identifier, eg:
446//
447// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
448//
449// (Note that the braces and commas in this example are actual
450// characters the parser expects to find in the file; the ellipses
451// indicate more numbers omitted from this example.)
452//
453// The file may contain multiple keys in this format, separated by
454// commas. The last key must not be followed by a comma.
455//
Doug Zongker30362a62013-04-10 11:32:17 -0700456// A Certificate is a pair of an RSAPublicKey and a particular hash
457// (we support SHA-1 and SHA-256; we store the hash length to signify
458// which is being used). The hash used is implied by the version number.
459//
460// 1: 2048-bit RSA key with e=3 and SHA-1 hash
461// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
462// 3: 2048-bit RSA key with e=3 and SHA-256 hash
463// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700464// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700465//
Tao Bao71e3e092016-02-02 14:02:27 -0800466// Returns true on success, and appends the found keys (at least one) to certs.
467// Otherwise returns false if the file failed to parse, or if it contains zero
468// keys. The contents in certs would be unspecified on failure.
469bool load_keys(const char* filename, std::vector<Certificate>& certs) {
470 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
471 if (!f) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700472 PLOG(ERROR) << "error opening " << filename;
Tao Bao71e3e092016-02-02 14:02:27 -0800473 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700474 }
475
Tao Bao71e3e092016-02-02 14:02:27 -0800476 while (true) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700477 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
Tao Bao71e3e092016-02-02 14:02:27 -0800478 Certificate& cert = certs.back();
Elliott Hughes8febafa2016-04-13 16:39:56 -0700479 uint32_t exponent = 0;
Doug Zongker6c249f72012-11-02 15:04:05 -0700480
Tao Bao71e3e092016-02-02 14:02:27 -0800481 char start_char;
482 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
483 if (start_char == '{') {
484 // a version 1 key has no version specifier.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700485 cert.key_type = Certificate::KEY_TYPE_RSA;
486 exponent = 3;
487 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800488 } else if (start_char == 'v') {
489 int version;
490 if (fscanf(f.get(), "%d {", &version) != 1) return false;
491 switch (version) {
492 case 2:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700493 cert.key_type = Certificate::KEY_TYPE_RSA;
494 exponent = 65537;
495 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800496 break;
497 case 3:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700498 cert.key_type = Certificate::KEY_TYPE_RSA;
499 exponent = 3;
500 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800501 break;
502 case 4:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700503 cert.key_type = Certificate::KEY_TYPE_RSA;
504 exponent = 65537;
505 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800506 break;
507 case 5:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700508 cert.key_type = Certificate::KEY_TYPE_EC;
509 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800510 break;
511 default:
512 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700513 }
Tao Bao71e3e092016-02-02 14:02:27 -0800514 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700515
Elliott Hughes8febafa2016-04-13 16:39:56 -0700516 if (cert.key_type == Certificate::KEY_TYPE_RSA) {
517 cert.rsa = parse_rsa_key(f.get(), exponent);
518 if (!cert.rsa) {
519 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700520 }
Tao Bao71e3e092016-02-02 14:02:27 -0800521
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700522 LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700523 } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
524 cert.ec = parse_ec_key(f.get());
525 if (!cert.ec) {
526 return false;
Tao Bao71e3e092016-02-02 14:02:27 -0800527 }
Tao Bao71e3e092016-02-02 14:02:27 -0800528 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700529 LOG(ERROR) << "Unknown key type " << cert.key_type;
Tao Bao71e3e092016-02-02 14:02:27 -0800530 return false;
531 }
532
533 // if the line ends in a comma, this file has more keys.
534 int ch = fgetc(f.get());
535 if (ch == ',') {
536 // more keys to come.
537 continue;
538 } else if (ch == EOF) {
539 break;
540 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700541 LOG(ERROR) << "unexpected character between keys";
Tao Bao71e3e092016-02-02 14:02:27 -0800542 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700543 }
544 }
545
Tao Bao71e3e092016-02-02 14:02:27 -0800546 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700547}