blob: 00e13aa7dbaf8baa66eff7dfe6712f736de13d20 [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
Tao Bao5e535012017-03-16 17:37:38 -070024#include <functional>
Elliott Hughes8febafa2016-04-13 16:39:56 -070025#include <algorithm>
26#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"
34#include "common.h"
Tao Baoe1792762016-04-19 22:31:01 -070035#include "print_sha1.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070036#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070037
Elliott Hughes8febafa2016-04-13 16:39:56 -070038static constexpr size_t MiB = 1024 * 1024;
39
Kenny Root7a4adb52013-10-09 10:14:35 -070040/*
41 * Simple version of PKCS#7 SignedData extraction. This extracts the
42 * signature OCTET STRING to be used for signature verification.
43 *
44 * For full details, see http://www.ietf.org/rfc/rfc3852.txt
45 *
46 * The PKCS#7 structure looks like:
47 *
48 * SEQUENCE (ContentInfo)
49 * OID (ContentType)
50 * [0] (content)
51 * SEQUENCE (SignedData)
52 * INTEGER (version CMSVersion)
53 * SET (DigestAlgorithmIdentifiers)
54 * SEQUENCE (EncapsulatedContentInfo)
55 * [0] (CertificateSet OPTIONAL)
56 * [1] (RevocationInfoChoices OPTIONAL)
57 * SET (SignerInfos)
58 * SEQUENCE (SignerInfo)
59 * INTEGER (CMSVersion)
60 * SEQUENCE (SignerIdentifier)
61 * SEQUENCE (DigestAlgorithmIdentifier)
62 * SEQUENCE (SignatureAlgorithmIdentifier)
63 * OCTET STRING (SignatureValue)
64 */
65static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der,
66 size_t* sig_der_length) {
67 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
68 if (ctx == NULL) {
69 return false;
70 }
71
72 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
73 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
74 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
75 if (signed_data_app != NULL) {
76 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
77 if (signed_data_seq != NULL
78 && asn1_sequence_next(signed_data_seq)
79 && asn1_sequence_next(signed_data_seq)
80 && asn1_sequence_next(signed_data_seq)
81 && asn1_constructed_skip_all(signed_data_seq)) {
82 asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
83 if (sig_set != NULL) {
84 asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
85 if (sig_seq != NULL
86 && asn1_sequence_next(sig_seq)
87 && asn1_sequence_next(sig_seq)
88 && asn1_sequence_next(sig_seq)
89 && asn1_sequence_next(sig_seq)) {
90 uint8_t* sig_der_ptr;
91 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) {
92 *sig_der = (uint8_t*) malloc(*sig_der_length);
93 if (*sig_der != NULL) {
94 memcpy(*sig_der, sig_der_ptr, *sig_der_length);
95 }
96 }
97 asn1_context_free(sig_seq);
98 }
99 asn1_context_free(sig_set);
100 }
101 asn1_context_free(signed_data_seq);
102 }
103 asn1_context_free(signed_data_app);
104 }
105 asn1_context_free(pkcs7_seq);
106 }
107 asn1_context_free(ctx);
108
109 return *sig_der != NULL;
110}
111
Tao Bao5e535012017-03-16 17:37:38 -0700112/*
113 * Looks for an RSA signature embedded in the .ZIP file comment given the path to the zip. Verifies
114 * that it matches one of the given public keys. A callback function can be optionally provided for
115 * posting the progress.
116 *
117 * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the
118 * signature).
119 */
120int verify_file(unsigned char* addr, size_t length, const std::vector<Certificate>& keys,
121 const std::function<void(float)>& set_progress) {
122 if (set_progress) {
123 set_progress(0.0);
124 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700125
Tao Bao5e535012017-03-16 17:37:38 -0700126 // An archive with a whole-file signature will end in six bytes:
127 //
128 // (2-byte signature start) $ff $ff (2-byte comment size)
129 //
130 // (As far as the ZIP format is concerned, these are part of the archive comment.) We start by
131 // reading this footer, this tells us how far back from the end we have to start reading to find
132 // the whole comment.
Doug Zongker54e2e862009-08-17 13:21:04 -0700133
134#define FOOTER_SIZE 6
135
Tao Bao5e535012017-03-16 17:37:38 -0700136 if (length < FOOTER_SIZE) {
137 LOG(ERROR) << "not big enough to contain footer";
138 return VERIFY_FAILURE;
139 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140
Tao Bao5e535012017-03-16 17:37:38 -0700141 unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142
Tao Bao5e535012017-03-16 17:37:38 -0700143 if (footer[2] != 0xff || footer[3] != 0xff) {
144 LOG(ERROR) << "footer is wrong";
145 return VERIFY_FAILURE;
146 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147
Tao Bao5e535012017-03-16 17:37:38 -0700148 size_t comment_size = footer[4] + (footer[5] << 8);
149 size_t signature_start = footer[0] + (footer[1] << 8);
150 LOG(INFO) << "comment is " << comment_size << " bytes; signature is " << signature_start
151 << " bytes from end";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152
Tao Bao5e535012017-03-16 17:37:38 -0700153 if (signature_start <= FOOTER_SIZE) {
154 LOG(ERROR) << "Signature start is in the footer";
155 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
Tao Bao5e535012017-03-16 17:37:38 -0700160 // The end-of-central-directory record is 22 bytes plus any comment length.
161 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162
Tao Bao5e535012017-03-16 17:37:38 -0700163 if (length < eocd_size) {
164 LOG(ERROR) << "not big enough to contain EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700165 return VERIFY_FAILURE;
Tao Bao5e535012017-03-16 17:37:38 -0700166 }
167
168 // Determine how much of the file is covered by the signature. This is everything except the
169 // signature data and length, which includes all of the EOCD except for the comment length field
170 // (2 bytes) and the comment data.
171 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
172
173 unsigned char* eocd = addr + length - eocd_size;
174
175 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
176 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
177 LOG(ERROR) << "signature length doesn't match EOCD marker";
178 return VERIFY_FAILURE;
179 }
180
181 for (size_t i = 4; i < eocd_size-3; ++i) {
182 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
183 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
184 // find the later (wrong) one, which could be exploitable. Fail the verification if this
185 // sequence occurs anywhere after the real one.
186 LOG(ERROR) << "EOCD marker occurs after start of EOCD";
187 return VERIFY_FAILURE;
188 }
189 }
190
191 bool need_sha1 = false;
192 bool need_sha256 = false;
193 for (const auto& key : keys) {
194 switch (key.hash_len) {
195 case SHA_DIGEST_LENGTH: need_sha1 = true; break;
196 case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
197 }
198 }
199
200 SHA_CTX sha1_ctx;
201 SHA256_CTX sha256_ctx;
202 SHA1_Init(&sha1_ctx);
203 SHA256_Init(&sha256_ctx);
204
205 double frac = -1.0;
206 size_t so_far = 0;
207 while (so_far < signed_len) {
208 // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
209 // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
210 // http://b/28135231.
211 size_t size = std::min(signed_len - so_far, 16 * MiB);
212
213 if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
214 if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
215 so_far += size;
216
217 if (set_progress) {
218 double f = so_far / (double)signed_len;
219 if (f > frac + 0.02 || size == so_far) {
220 set_progress(f);
221 frac = f;
222 }
223 }
224 }
225
226 uint8_t sha1[SHA_DIGEST_LENGTH];
227 SHA1_Final(sha1, &sha1_ctx);
228 uint8_t sha256[SHA256_DIGEST_LENGTH];
229 SHA256_Final(sha256, &sha256_ctx);
230
231 uint8_t* sig_der = nullptr;
232 size_t sig_der_length = 0;
233
234 uint8_t* signature = eocd + eocd_size - signature_start;
235 size_t signature_size = signature_start - FOOTER_SIZE;
236
237 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
238 << signature_size << "): " << print_hex(signature, signature_size);
239
240 if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) {
241 LOG(ERROR) << "Could not find signature DER block";
242 return VERIFY_FAILURE;
243 }
244
245 // Check to make sure at least one of the keys matches the signature. Since any key can match,
246 // we need to try each before determining a verification failure has happened.
247 size_t i = 0;
248 for (const auto& key : keys) {
249 const uint8_t* hash;
250 int hash_nid;
251 switch (key.hash_len) {
252 case SHA_DIGEST_LENGTH:
253 hash = sha1;
254 hash_nid = NID_sha1;
255 break;
256 case SHA256_DIGEST_LENGTH:
257 hash = sha256;
258 hash_nid = NID_sha256;
259 break;
260 default:
261 continue;
262 }
263
264 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
265 // after the signature itself.
266 if (key.key_type == Certificate::KEY_TYPE_RSA) {
267 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der, sig_der_length, key.rsa.get())) {
268 LOG(INFO) << "failed to verify against RSA key " << i;
269 continue;
270 }
271
272 LOG(INFO) << "whole-file signature verified against RSA key " << i;
273 free(sig_der);
274 return VERIFY_SUCCESS;
275 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
276 if (!ECDSA_verify(0, hash, key.hash_len, sig_der, sig_der_length, key.ec.get())) {
277 LOG(INFO) << "failed to verify against EC key " << i;
278 continue;
279 }
280
281 LOG(INFO) << "whole-file signature verified against EC key " << i;
282 free(sig_der);
283 return VERIFY_SUCCESS;
284 } else {
285 LOG(INFO) << "Unknown key type " << key.key_type;
286 }
287 i++;
288 }
289
290 if (need_sha1) {
291 LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH);
292 }
293 if (need_sha256) {
294 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
295 }
296 free(sig_der);
297 LOG(ERROR) << "failed to verify whole-file signature";
298 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299}
Doug Zongker6c249f72012-11-02 15:04:05 -0700300
Elliott Hughes8febafa2016-04-13 16:39:56 -0700301std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
302 // Read key length in words and n0inv. n0inv is a precomputed montgomery
303 // parameter derived from the modulus and can be used to speed up
304 // verification. n0inv is 32 bits wide here, assuming the verification logic
305 // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
306 // internally, in which case we don't have a valid n0inv. Thus, we just
307 // ignore the montgomery parameters and have BoringSSL recompute them
308 // internally. If/When the speedup from using the montgomery parameters
309 // becomes relevant, we can add more sophisticated code here to obtain a
310 // 64-bit n0inv and initialize the montgomery parameters in the key object.
311 uint32_t key_len_words = 0;
312 uint32_t n0inv = 0;
313 if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
314 return nullptr;
315 }
316
317 if (key_len_words > 8192 / 32) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700318 LOG(ERROR) << "key length (" << key_len_words << ") too large";
Elliott Hughes8febafa2016-04-13 16:39:56 -0700319 return nullptr;
320 }
321
322 // Read the modulus.
323 std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
324 if (fscanf(file, " , { %u", &modulus[0]) != 1) {
325 return nullptr;
326 }
327 for (uint32_t i = 1; i < key_len_words; ++i) {
328 if (fscanf(file, " , %u", &modulus[i]) != 1) {
329 return nullptr;
330 }
331 }
332
333 // Cconvert from little-endian array of little-endian words to big-endian
334 // byte array suitable as input for BN_bin2bn.
335 std::reverse((uint8_t*)modulus.get(),
336 (uint8_t*)(modulus.get() + key_len_words));
337
338 // The next sequence of values is the montgomery parameter R^2. Since we
339 // generally don't have a valid |n0inv|, we ignore this (see comment above).
340 uint32_t rr_value;
341 if (fscanf(file, " } , { %u", &rr_value) != 1) {
342 return nullptr;
343 }
344 for (uint32_t i = 1; i < key_len_words; ++i) {
345 if (fscanf(file, " , %u", &rr_value) != 1) {
346 return nullptr;
347 }
348 }
349 if (fscanf(file, " } } ") != 0) {
350 return nullptr;
351 }
352
353 // Initialize the key.
354 std::unique_ptr<RSA, RSADeleter> key(RSA_new());
355 if (!key) {
356 return nullptr;
357 }
358
359 key->n = BN_bin2bn((uint8_t*)modulus.get(),
360 key_len_words * sizeof(uint32_t), NULL);
361 if (!key->n) {
362 return nullptr;
363 }
364
365 key->e = BN_new();
366 if (!key->e || !BN_set_word(key->e, exponent)) {
367 return nullptr;
368 }
369
370 return key;
371}
372
373struct BNDeleter {
374 void operator()(BIGNUM* bn) {
375 BN_free(bn);
376 }
377};
378
379std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
380 uint32_t key_len_bytes = 0;
381 if (fscanf(file, " %i", &key_len_bytes) != 1) {
382 return nullptr;
383 }
384
385 std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
386 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
387 if (!group) {
388 return nullptr;
389 }
390
391 // Verify that |key_len| matches the group order.
392 if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
393 return nullptr;
394 }
395
396 // Read the public key coordinates. Note that the byte order in the file is
397 // little-endian, so we convert to big-endian here.
398 std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
399 std::unique_ptr<BIGNUM, BNDeleter> point[2];
400 for (int i = 0; i < 2; ++i) {
401 unsigned int byte = 0;
402 if (fscanf(file, " , { %u", &byte) != 1) {
403 return nullptr;
404 }
405 bytes[key_len_bytes - 1] = byte;
406
407 for (size_t i = 1; i < key_len_bytes; ++i) {
408 if (fscanf(file, " , %u", &byte) != 1) {
409 return nullptr;
410 }
411 bytes[key_len_bytes - i - 1] = byte;
412 }
413
414 point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
415 if (!point[i]) {
416 return nullptr;
417 }
418
419 if (fscanf(file, " }") != 0) {
420 return nullptr;
421 }
422 }
423
424 if (fscanf(file, " } ") != 0) {
425 return nullptr;
426 }
427
428 // Create and initialize the key.
429 std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
430 if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
431 !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
432 point[1].get())) {
433 return nullptr;
434 }
435
436 return key;
437}
438
Doug Zongker6c249f72012-11-02 15:04:05 -0700439// Reads a file containing one or more public keys as produced by
440// DumpPublicKey: this is an RSAPublicKey struct as it would appear
441// as a C source literal, eg:
442//
443// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
444//
445// For key versions newer than the original 2048-bit e=3 keys
446// supported by Android, the string is preceded by a version
447// identifier, eg:
448//
449// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
450//
451// (Note that the braces and commas in this example are actual
452// characters the parser expects to find in the file; the ellipses
453// indicate more numbers omitted from this example.)
454//
455// The file may contain multiple keys in this format, separated by
456// commas. The last key must not be followed by a comma.
457//
Doug Zongker30362a62013-04-10 11:32:17 -0700458// A Certificate is a pair of an RSAPublicKey and a particular hash
459// (we support SHA-1 and SHA-256; we store the hash length to signify
460// which is being used). The hash used is implied by the version number.
461//
462// 1: 2048-bit RSA key with e=3 and SHA-1 hash
463// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
464// 3: 2048-bit RSA key with e=3 and SHA-256 hash
465// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700466// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700467//
Tao Bao71e3e092016-02-02 14:02:27 -0800468// Returns true on success, and appends the found keys (at least one) to certs.
469// Otherwise returns false if the file failed to parse, or if it contains zero
470// keys. The contents in certs would be unspecified on failure.
471bool load_keys(const char* filename, std::vector<Certificate>& certs) {
472 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
473 if (!f) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700474 PLOG(ERROR) << "error opening " << filename;
Tao Bao71e3e092016-02-02 14:02:27 -0800475 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700476 }
477
Tao Bao71e3e092016-02-02 14:02:27 -0800478 while (true) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700479 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
Tao Bao71e3e092016-02-02 14:02:27 -0800480 Certificate& cert = certs.back();
Elliott Hughes8febafa2016-04-13 16:39:56 -0700481 uint32_t exponent = 0;
Doug Zongker6c249f72012-11-02 15:04:05 -0700482
Tao Bao71e3e092016-02-02 14:02:27 -0800483 char start_char;
484 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
485 if (start_char == '{') {
486 // a version 1 key has no version specifier.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700487 cert.key_type = Certificate::KEY_TYPE_RSA;
488 exponent = 3;
489 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800490 } else if (start_char == 'v') {
491 int version;
492 if (fscanf(f.get(), "%d {", &version) != 1) return false;
493 switch (version) {
494 case 2:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700495 cert.key_type = Certificate::KEY_TYPE_RSA;
496 exponent = 65537;
497 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800498 break;
499 case 3:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700500 cert.key_type = Certificate::KEY_TYPE_RSA;
501 exponent = 3;
502 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800503 break;
504 case 4:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700505 cert.key_type = Certificate::KEY_TYPE_RSA;
506 exponent = 65537;
507 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800508 break;
509 case 5:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700510 cert.key_type = Certificate::KEY_TYPE_EC;
511 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800512 break;
513 default:
514 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700515 }
Tao Bao71e3e092016-02-02 14:02:27 -0800516 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700517
Elliott Hughes8febafa2016-04-13 16:39:56 -0700518 if (cert.key_type == Certificate::KEY_TYPE_RSA) {
519 cert.rsa = parse_rsa_key(f.get(), exponent);
520 if (!cert.rsa) {
521 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700522 }
Tao Bao71e3e092016-02-02 14:02:27 -0800523
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700524 LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700525 } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
526 cert.ec = parse_ec_key(f.get());
527 if (!cert.ec) {
528 return false;
Tao Bao71e3e092016-02-02 14:02:27 -0800529 }
Tao Bao71e3e092016-02-02 14:02:27 -0800530 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700531 LOG(ERROR) << "Unknown key type " << cert.key_type;
Tao Bao71e3e092016-02-02 14:02:27 -0800532 return false;
533 }
534
535 // if the line ends in a comma, this file has more keys.
536 int ch = fgetc(f.get());
537 if (ch == ',') {
538 // more keys to come.
539 continue;
540 } else if (ch == EOF) {
541 break;
542 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700543 LOG(ERROR) << "unexpected character between keys";
Tao Bao71e3e092016-02-02 14:02:27 -0800544 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700545 }
546 }
547
Tao Bao71e3e092016-02-02 14:02:27 -0800548 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700549}