blob: 582c498fba9f529266bf28552e9cc96c1632be00 [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 Bao553c7bd2017-03-18 07:33:26 -0700153 if (signature_start > comment_size) {
154 LOG(ERROR) << "signature start: " << signature_start << " is larger than comment size: "
155 << comment_size;
156 return VERIFY_FAILURE;
157 }
Tianjie Xu54ea1362016-12-16 16:24:09 -0800158
Tao Bao5e535012017-03-16 17:37:38 -0700159 if (signature_start <= FOOTER_SIZE) {
160 LOG(ERROR) << "Signature start is in the footer";
161 return VERIFY_FAILURE;
162 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
Doug Zongker54e2e862009-08-17 13:21:04 -0700164#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
Tao Bao5e535012017-03-16 17:37:38 -0700166 // The end-of-central-directory record is 22 bytes plus any comment length.
167 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Tao Bao5e535012017-03-16 17:37:38 -0700169 if (length < eocd_size) {
170 LOG(ERROR) << "not big enough to contain EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700171 return VERIFY_FAILURE;
Tao Bao5e535012017-03-16 17:37:38 -0700172 }
173
174 // Determine how much of the file is covered by the signature. This is everything except the
175 // signature data and length, which includes all of the EOCD except for the comment length field
176 // (2 bytes) and the comment data.
177 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
178
179 unsigned char* eocd = addr + length - eocd_size;
180
181 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
182 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
183 LOG(ERROR) << "signature length doesn't match EOCD marker";
184 return VERIFY_FAILURE;
185 }
186
187 for (size_t i = 4; i < eocd_size-3; ++i) {
188 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
189 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
190 // find the later (wrong) one, which could be exploitable. Fail the verification if this
191 // sequence occurs anywhere after the real one.
192 LOG(ERROR) << "EOCD marker occurs after start of EOCD";
193 return VERIFY_FAILURE;
194 }
195 }
196
197 bool need_sha1 = false;
198 bool need_sha256 = false;
199 for (const auto& key : keys) {
200 switch (key.hash_len) {
201 case SHA_DIGEST_LENGTH: need_sha1 = true; break;
202 case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
203 }
204 }
205
206 SHA_CTX sha1_ctx;
207 SHA256_CTX sha256_ctx;
208 SHA1_Init(&sha1_ctx);
209 SHA256_Init(&sha256_ctx);
210
211 double frac = -1.0;
212 size_t so_far = 0;
213 while (so_far < signed_len) {
214 // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
215 // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
216 // http://b/28135231.
217 size_t size = std::min(signed_len - so_far, 16 * MiB);
218
219 if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
220 if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
221 so_far += size;
222
223 if (set_progress) {
224 double f = so_far / (double)signed_len;
225 if (f > frac + 0.02 || size == so_far) {
226 set_progress(f);
227 frac = f;
228 }
229 }
230 }
231
232 uint8_t sha1[SHA_DIGEST_LENGTH];
233 SHA1_Final(sha1, &sha1_ctx);
234 uint8_t sha256[SHA256_DIGEST_LENGTH];
235 SHA256_Final(sha256, &sha256_ctx);
236
237 uint8_t* sig_der = nullptr;
238 size_t sig_der_length = 0;
239
240 uint8_t* signature = eocd + eocd_size - signature_start;
241 size_t signature_size = signature_start - FOOTER_SIZE;
242
243 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
244 << signature_size << "): " << print_hex(signature, signature_size);
245
246 if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) {
247 LOG(ERROR) << "Could not find signature DER block";
248 return VERIFY_FAILURE;
249 }
250
251 // Check to make sure at least one of the keys matches the signature. Since any key can match,
252 // we need to try each before determining a verification failure has happened.
253 size_t i = 0;
254 for (const auto& key : keys) {
255 const uint8_t* hash;
256 int hash_nid;
257 switch (key.hash_len) {
258 case SHA_DIGEST_LENGTH:
259 hash = sha1;
260 hash_nid = NID_sha1;
261 break;
262 case SHA256_DIGEST_LENGTH:
263 hash = sha256;
264 hash_nid = NID_sha256;
265 break;
266 default:
267 continue;
268 }
269
270 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
271 // after the signature itself.
272 if (key.key_type == Certificate::KEY_TYPE_RSA) {
273 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der, sig_der_length, key.rsa.get())) {
274 LOG(INFO) << "failed to verify against RSA key " << i;
275 continue;
276 }
277
278 LOG(INFO) << "whole-file signature verified against RSA key " << i;
279 free(sig_der);
280 return VERIFY_SUCCESS;
281 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
282 if (!ECDSA_verify(0, hash, key.hash_len, sig_der, sig_der_length, key.ec.get())) {
283 LOG(INFO) << "failed to verify against EC key " << i;
284 continue;
285 }
286
287 LOG(INFO) << "whole-file signature verified against EC key " << i;
288 free(sig_der);
289 return VERIFY_SUCCESS;
290 } else {
291 LOG(INFO) << "Unknown key type " << key.key_type;
292 }
293 i++;
294 }
295
296 if (need_sha1) {
297 LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH);
298 }
299 if (need_sha256) {
300 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
301 }
302 free(sig_der);
303 LOG(ERROR) << "failed to verify whole-file signature";
304 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305}
Doug Zongker6c249f72012-11-02 15:04:05 -0700306
Elliott Hughes8febafa2016-04-13 16:39:56 -0700307std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
308 // Read key length in words and n0inv. n0inv is a precomputed montgomery
309 // parameter derived from the modulus and can be used to speed up
310 // verification. n0inv is 32 bits wide here, assuming the verification logic
311 // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
312 // internally, in which case we don't have a valid n0inv. Thus, we just
313 // ignore the montgomery parameters and have BoringSSL recompute them
314 // internally. If/When the speedup from using the montgomery parameters
315 // becomes relevant, we can add more sophisticated code here to obtain a
316 // 64-bit n0inv and initialize the montgomery parameters in the key object.
317 uint32_t key_len_words = 0;
318 uint32_t n0inv = 0;
319 if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
320 return nullptr;
321 }
322
323 if (key_len_words > 8192 / 32) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700324 LOG(ERROR) << "key length (" << key_len_words << ") too large";
Elliott Hughes8febafa2016-04-13 16:39:56 -0700325 return nullptr;
326 }
327
328 // Read the modulus.
329 std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
330 if (fscanf(file, " , { %u", &modulus[0]) != 1) {
331 return nullptr;
332 }
333 for (uint32_t i = 1; i < key_len_words; ++i) {
334 if (fscanf(file, " , %u", &modulus[i]) != 1) {
335 return nullptr;
336 }
337 }
338
339 // Cconvert from little-endian array of little-endian words to big-endian
340 // byte array suitable as input for BN_bin2bn.
341 std::reverse((uint8_t*)modulus.get(),
342 (uint8_t*)(modulus.get() + key_len_words));
343
344 // The next sequence of values is the montgomery parameter R^2. Since we
345 // generally don't have a valid |n0inv|, we ignore this (see comment above).
346 uint32_t rr_value;
347 if (fscanf(file, " } , { %u", &rr_value) != 1) {
348 return nullptr;
349 }
350 for (uint32_t i = 1; i < key_len_words; ++i) {
351 if (fscanf(file, " , %u", &rr_value) != 1) {
352 return nullptr;
353 }
354 }
355 if (fscanf(file, " } } ") != 0) {
356 return nullptr;
357 }
358
359 // Initialize the key.
360 std::unique_ptr<RSA, RSADeleter> key(RSA_new());
361 if (!key) {
362 return nullptr;
363 }
364
365 key->n = BN_bin2bn((uint8_t*)modulus.get(),
366 key_len_words * sizeof(uint32_t), NULL);
367 if (!key->n) {
368 return nullptr;
369 }
370
371 key->e = BN_new();
372 if (!key->e || !BN_set_word(key->e, exponent)) {
373 return nullptr;
374 }
375
376 return key;
377}
378
379struct BNDeleter {
380 void operator()(BIGNUM* bn) {
381 BN_free(bn);
382 }
383};
384
385std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
386 uint32_t key_len_bytes = 0;
387 if (fscanf(file, " %i", &key_len_bytes) != 1) {
388 return nullptr;
389 }
390
391 std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
392 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
393 if (!group) {
394 return nullptr;
395 }
396
397 // Verify that |key_len| matches the group order.
398 if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
399 return nullptr;
400 }
401
402 // Read the public key coordinates. Note that the byte order in the file is
403 // little-endian, so we convert to big-endian here.
404 std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
405 std::unique_ptr<BIGNUM, BNDeleter> point[2];
406 for (int i = 0; i < 2; ++i) {
407 unsigned int byte = 0;
408 if (fscanf(file, " , { %u", &byte) != 1) {
409 return nullptr;
410 }
411 bytes[key_len_bytes - 1] = byte;
412
413 for (size_t i = 1; i < key_len_bytes; ++i) {
414 if (fscanf(file, " , %u", &byte) != 1) {
415 return nullptr;
416 }
417 bytes[key_len_bytes - i - 1] = byte;
418 }
419
420 point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
421 if (!point[i]) {
422 return nullptr;
423 }
424
425 if (fscanf(file, " }") != 0) {
426 return nullptr;
427 }
428 }
429
430 if (fscanf(file, " } ") != 0) {
431 return nullptr;
432 }
433
434 // Create and initialize the key.
435 std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
436 if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
437 !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
438 point[1].get())) {
439 return nullptr;
440 }
441
442 return key;
443}
444
Doug Zongker6c249f72012-11-02 15:04:05 -0700445// Reads a file containing one or more public keys as produced by
446// DumpPublicKey: this is an RSAPublicKey struct as it would appear
447// as a C source literal, eg:
448//
449// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
450//
451// For key versions newer than the original 2048-bit e=3 keys
452// supported by Android, the string is preceded by a version
453// identifier, eg:
454//
455// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
456//
457// (Note that the braces and commas in this example are actual
458// characters the parser expects to find in the file; the ellipses
459// indicate more numbers omitted from this example.)
460//
461// The file may contain multiple keys in this format, separated by
462// commas. The last key must not be followed by a comma.
463//
Doug Zongker30362a62013-04-10 11:32:17 -0700464// A Certificate is a pair of an RSAPublicKey and a particular hash
465// (we support SHA-1 and SHA-256; we store the hash length to signify
466// which is being used). The hash used is implied by the version number.
467//
468// 1: 2048-bit RSA key with e=3 and SHA-1 hash
469// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
470// 3: 2048-bit RSA key with e=3 and SHA-256 hash
471// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700472// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700473//
Tao Bao71e3e092016-02-02 14:02:27 -0800474// Returns true on success, and appends the found keys (at least one) to certs.
475// Otherwise returns false if the file failed to parse, or if it contains zero
476// keys. The contents in certs would be unspecified on failure.
477bool load_keys(const char* filename, std::vector<Certificate>& certs) {
478 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
479 if (!f) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700480 PLOG(ERROR) << "error opening " << filename;
Tao Bao71e3e092016-02-02 14:02:27 -0800481 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700482 }
483
Tao Bao71e3e092016-02-02 14:02:27 -0800484 while (true) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700485 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
Tao Bao71e3e092016-02-02 14:02:27 -0800486 Certificate& cert = certs.back();
Elliott Hughes8febafa2016-04-13 16:39:56 -0700487 uint32_t exponent = 0;
Doug Zongker6c249f72012-11-02 15:04:05 -0700488
Tao Bao71e3e092016-02-02 14:02:27 -0800489 char start_char;
490 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
491 if (start_char == '{') {
492 // a version 1 key has no version specifier.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700493 cert.key_type = Certificate::KEY_TYPE_RSA;
494 exponent = 3;
495 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800496 } else if (start_char == 'v') {
497 int version;
498 if (fscanf(f.get(), "%d {", &version) != 1) return false;
499 switch (version) {
500 case 2:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700501 cert.key_type = Certificate::KEY_TYPE_RSA;
502 exponent = 65537;
503 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800504 break;
505 case 3:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700506 cert.key_type = Certificate::KEY_TYPE_RSA;
507 exponent = 3;
508 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800509 break;
510 case 4:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700511 cert.key_type = Certificate::KEY_TYPE_RSA;
512 exponent = 65537;
513 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800514 break;
515 case 5:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700516 cert.key_type = Certificate::KEY_TYPE_EC;
517 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800518 break;
519 default:
520 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700521 }
Tao Bao71e3e092016-02-02 14:02:27 -0800522 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700523
Elliott Hughes8febafa2016-04-13 16:39:56 -0700524 if (cert.key_type == Certificate::KEY_TYPE_RSA) {
525 cert.rsa = parse_rsa_key(f.get(), exponent);
526 if (!cert.rsa) {
527 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700528 }
Tao Bao71e3e092016-02-02 14:02:27 -0800529
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700530 LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700531 } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
532 cert.ec = parse_ec_key(f.get());
533 if (!cert.ec) {
534 return false;
Tao Bao71e3e092016-02-02 14:02:27 -0800535 }
Tao Bao71e3e092016-02-02 14:02:27 -0800536 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700537 LOG(ERROR) << "Unknown key type " << cert.key_type;
Tao Bao71e3e092016-02-02 14:02:27 -0800538 return false;
539 }
540
541 // if the line ends in a comma, this file has more keys.
542 int ch = fgetc(f.get());
543 if (ch == ',') {
544 // more keys to come.
545 continue;
546 } else if (ch == EOF) {
547 break;
548 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700549 LOG(ERROR) << "unexpected character between keys";
Tao Bao71e3e092016-02-02 14:02:27 -0800550 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700551 }
552 }
553
Tao Bao71e3e092016-02-02 14:02:27 -0800554 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700555}