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