blob: e9d540cdb4ca711332df25d8dfc24e80f6d47cf3 [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>
Tao Bao76fdb242017-03-20 17:09:13 -070027#include <vector>
Elliott Hughes8febafa2016-04-13 16:39:56 -070028
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070029#include <android-base/logging.h>
David Benjamina86392e2016-04-15 20:22:09 -040030#include <openssl/bn.h>
Elliott Hughes8febafa2016-04-13 16:39:56 -070031#include <openssl/ecdsa.h>
32#include <openssl/obj_mac.h>
33
34#include "asn1_decoder.h"
Tao Baoe1792762016-04-19 22:31:01 -070035#include "print_sha1.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070036
Elliott Hughes8febafa2016-04-13 16:39:56 -070037static constexpr size_t MiB = 1024 * 1024;
38
Kenny Root7a4adb52013-10-09 10:14:35 -070039/*
40 * Simple version of PKCS#7 SignedData extraction. This extracts the
41 * signature OCTET STRING to be used for signature verification.
42 *
43 * For full details, see http://www.ietf.org/rfc/rfc3852.txt
44 *
45 * The PKCS#7 structure looks like:
46 *
47 * SEQUENCE (ContentInfo)
48 * OID (ContentType)
49 * [0] (content)
50 * SEQUENCE (SignedData)
51 * INTEGER (version CMSVersion)
52 * SET (DigestAlgorithmIdentifiers)
53 * SEQUENCE (EncapsulatedContentInfo)
54 * [0] (CertificateSet OPTIONAL)
55 * [1] (RevocationInfoChoices OPTIONAL)
56 * SET (SignerInfos)
57 * SEQUENCE (SignerInfo)
58 * INTEGER (CMSVersion)
59 * SEQUENCE (SignerIdentifier)
60 * SEQUENCE (DigestAlgorithmIdentifier)
61 * SEQUENCE (SignatureAlgorithmIdentifier)
62 * OCTET STRING (SignatureValue)
63 */
Tao Bao76fdb242017-03-20 17:09:13 -070064static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len,
65 std::vector<uint8_t>* sig_der) {
66 CHECK(sig_der != nullptr);
67 sig_der->clear();
Kenny Root7a4adb52013-10-09 10:14:35 -070068
Tao Bao861c53c2017-03-20 17:09:13 -070069 asn1_context ctx(pkcs7_der, pkcs7_der_len);
70
71 std::unique_ptr<asn1_context> pkcs7_seq(ctx.asn1_sequence_get());
72 if (pkcs7_seq == nullptr || !pkcs7_seq->asn1_sequence_next()) {
Tao Bao76fdb242017-03-20 17:09:13 -070073 return false;
74 }
75
Tao Bao861c53c2017-03-20 17:09:13 -070076 std::unique_ptr<asn1_context> signed_data_app(pkcs7_seq->asn1_constructed_get());
77 if (signed_data_app == nullptr) {
78 return false;
Tao Bao76fdb242017-03-20 17:09:13 -070079 }
Kenny Root7a4adb52013-10-09 10:14:35 -070080
Tao Bao861c53c2017-03-20 17:09:13 -070081 std::unique_ptr<asn1_context> signed_data_seq(signed_data_app->asn1_sequence_get());
82 if (signed_data_seq == nullptr ||
83 !signed_data_seq->asn1_sequence_next() ||
84 !signed_data_seq->asn1_sequence_next() ||
85 !signed_data_seq->asn1_sequence_next() ||
86 !signed_data_seq->asn1_constructed_skip_all()) {
87 return false;
88 }
89
90 std::unique_ptr<asn1_context> sig_set(signed_data_seq->asn1_set_get());
91 if (sig_set == nullptr) {
92 return false;
93 }
94
95 std::unique_ptr<asn1_context> sig_seq(sig_set->asn1_sequence_get());
96 if (sig_seq == nullptr ||
97 !sig_seq->asn1_sequence_next() ||
98 !sig_seq->asn1_sequence_next() ||
99 !sig_seq->asn1_sequence_next() ||
100 !sig_seq->asn1_sequence_next()) {
101 return false;
102 }
103
104 const uint8_t* sig_der_ptr;
105 size_t sig_der_length;
106 if (!sig_seq->asn1_octet_string_get(&sig_der_ptr, &sig_der_length)) {
107 return false;
108 }
109
110 sig_der->resize(sig_der_length);
111 std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin());
112 return true;
Kenny Root7a4adb52013-10-09 10:14:35 -0700113}
114
Tao Bao5e535012017-03-16 17:37:38 -0700115/*
116 * Looks for an RSA signature embedded in the .ZIP file comment given the path to the zip. Verifies
117 * that it matches one of the given public keys. A callback function can be optionally provided for
118 * posting the progress.
119 *
120 * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the
121 * signature).
122 */
Tao Bao76fdb242017-03-20 17:09:13 -0700123int verify_file(const unsigned char* addr, size_t length, const std::vector<Certificate>& keys,
Tao Bao5e535012017-03-16 17:37:38 -0700124 const std::function<void(float)>& set_progress) {
125 if (set_progress) {
126 set_progress(0.0);
127 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700128
Tao Bao5e535012017-03-16 17:37:38 -0700129 // An archive with a whole-file signature will end in six bytes:
130 //
131 // (2-byte signature start) $ff $ff (2-byte comment size)
132 //
133 // (As far as the ZIP format is concerned, these are part of the archive comment.) We start by
134 // reading this footer, this tells us how far back from the end we have to start reading to find
135 // the whole comment.
Doug Zongker54e2e862009-08-17 13:21:04 -0700136
137#define FOOTER_SIZE 6
138
Tao Bao5e535012017-03-16 17:37:38 -0700139 if (length < FOOTER_SIZE) {
140 LOG(ERROR) << "not big enough to contain footer";
141 return VERIFY_FAILURE;
142 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143
Tao Bao76fdb242017-03-20 17:09:13 -0700144 const unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
Tao Bao5e535012017-03-16 17:37:38 -0700146 if (footer[2] != 0xff || footer[3] != 0xff) {
147 LOG(ERROR) << "footer is wrong";
148 return VERIFY_FAILURE;
149 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
Tao Bao5e535012017-03-16 17:37:38 -0700151 size_t comment_size = footer[4] + (footer[5] << 8);
152 size_t signature_start = footer[0] + (footer[1] << 8);
153 LOG(INFO) << "comment is " << comment_size << " bytes; signature is " << signature_start
154 << " bytes from end";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155
Tao Bao5e535012017-03-16 17:37:38 -0700156 if (signature_start <= FOOTER_SIZE) {
157 LOG(ERROR) << "Signature start is in the footer";
158 return VERIFY_FAILURE;
159 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160
Doug Zongker54e2e862009-08-17 13:21:04 -0700161#define EOCD_HEADER_SIZE 22
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162
Tao Bao5e535012017-03-16 17:37:38 -0700163 // The end-of-central-directory record is 22 bytes plus any comment length.
164 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
Tao Bao5e535012017-03-16 17:37:38 -0700166 if (length < eocd_size) {
167 LOG(ERROR) << "not big enough to contain EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700168 return VERIFY_FAILURE;
Tao Bao5e535012017-03-16 17:37:38 -0700169 }
170
171 // Determine how much of the file is covered by the signature. This is everything except the
172 // signature data and length, which includes all of the EOCD except for the comment length field
173 // (2 bytes) and the comment data.
174 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
175
Tao Bao76fdb242017-03-20 17:09:13 -0700176 const unsigned char* eocd = addr + length - eocd_size;
Tao Bao5e535012017-03-16 17:37:38 -0700177
178 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
179 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
180 LOG(ERROR) << "signature length doesn't match EOCD marker";
181 return VERIFY_FAILURE;
182 }
183
184 for (size_t i = 4; i < eocd_size-3; ++i) {
Tao Bao76fdb242017-03-20 17:09:13 -0700185 if (eocd[i] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Tao Bao5e535012017-03-16 17:37:38 -0700186 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
187 // find the later (wrong) one, which could be exploitable. Fail the verification if this
188 // sequence occurs anywhere after the real one.
189 LOG(ERROR) << "EOCD marker occurs after start of EOCD";
190 return VERIFY_FAILURE;
191 }
192 }
193
194 bool need_sha1 = false;
195 bool need_sha256 = false;
196 for (const auto& key : keys) {
197 switch (key.hash_len) {
198 case SHA_DIGEST_LENGTH: need_sha1 = true; break;
199 case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
200 }
201 }
202
203 SHA_CTX sha1_ctx;
204 SHA256_CTX sha256_ctx;
205 SHA1_Init(&sha1_ctx);
206 SHA256_Init(&sha256_ctx);
207
208 double frac = -1.0;
209 size_t so_far = 0;
210 while (so_far < signed_len) {
211 // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
212 // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
213 // http://b/28135231.
214 size_t size = std::min(signed_len - so_far, 16 * MiB);
215
216 if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
217 if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
218 so_far += size;
219
220 if (set_progress) {
221 double f = so_far / (double)signed_len;
222 if (f > frac + 0.02 || size == so_far) {
223 set_progress(f);
224 frac = f;
225 }
226 }
227 }
228
229 uint8_t sha1[SHA_DIGEST_LENGTH];
230 SHA1_Final(sha1, &sha1_ctx);
231 uint8_t sha256[SHA256_DIGEST_LENGTH];
232 SHA256_Final(sha256, &sha256_ctx);
233
Tao Bao76fdb242017-03-20 17:09:13 -0700234 const uint8_t* signature = eocd + eocd_size - signature_start;
Tao Bao5e535012017-03-16 17:37:38 -0700235 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
Tao Bao76fdb242017-03-20 17:09:13 -0700240 std::vector<uint8_t> sig_der;
241 if (!read_pkcs7(signature, signature_size, &sig_der)) {
Tao Bao5e535012017-03-16 17:37:38 -0700242 LOG(ERROR) << "Could not find signature DER block";
243 return VERIFY_FAILURE;
244 }
245
246 // Check to make sure at least one of the keys matches the signature. Since any key can match,
247 // we need to try each before determining a verification failure has happened.
248 size_t i = 0;
249 for (const auto& key : keys) {
250 const uint8_t* hash;
251 int hash_nid;
252 switch (key.hash_len) {
253 case SHA_DIGEST_LENGTH:
254 hash = sha1;
255 hash_nid = NID_sha1;
256 break;
257 case SHA256_DIGEST_LENGTH:
258 hash = sha256;
259 hash_nid = NID_sha256;
260 break;
261 default:
262 continue;
263 }
264
265 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
266 // after the signature itself.
267 if (key.key_type == Certificate::KEY_TYPE_RSA) {
Tao Bao76fdb242017-03-20 17:09:13 -0700268 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der.data(), sig_der.size(),
269 key.rsa.get())) {
Tao Bao5e535012017-03-16 17:37:38 -0700270 LOG(INFO) << "failed to verify against RSA key " << i;
271 continue;
272 }
273
274 LOG(INFO) << "whole-file signature verified against RSA key " << i;
Tao Bao5e535012017-03-16 17:37:38 -0700275 return VERIFY_SUCCESS;
276 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
Tao Bao76fdb242017-03-20 17:09:13 -0700277 if (!ECDSA_verify(0, hash, key.hash_len, sig_der.data(), sig_der.size(), key.ec.get())) {
Tao Bao5e535012017-03-16 17:37:38 -0700278 LOG(INFO) << "failed to verify against EC key " << i;
279 continue;
280 }
281
282 LOG(INFO) << "whole-file signature verified against EC key " << i;
Tao Bao5e535012017-03-16 17:37:38 -0700283 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 }
Tao Bao5e535012017-03-16 17:37:38 -0700296 LOG(ERROR) << "failed to verify whole-file signature";
297 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298}
Doug Zongker6c249f72012-11-02 15:04:05 -0700299
Elliott Hughes8febafa2016-04-13 16:39:56 -0700300std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
301 // Read key length in words and n0inv. n0inv is a precomputed montgomery
302 // parameter derived from the modulus and can be used to speed up
303 // verification. n0inv is 32 bits wide here, assuming the verification logic
304 // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
305 // internally, in which case we don't have a valid n0inv. Thus, we just
306 // ignore the montgomery parameters and have BoringSSL recompute them
307 // internally. If/When the speedup from using the montgomery parameters
308 // becomes relevant, we can add more sophisticated code here to obtain a
309 // 64-bit n0inv and initialize the montgomery parameters in the key object.
310 uint32_t key_len_words = 0;
311 uint32_t n0inv = 0;
312 if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
313 return nullptr;
314 }
315
316 if (key_len_words > 8192 / 32) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700317 LOG(ERROR) << "key length (" << key_len_words << ") too large";
Elliott Hughes8febafa2016-04-13 16:39:56 -0700318 return nullptr;
319 }
320
321 // Read the modulus.
322 std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
323 if (fscanf(file, " , { %u", &modulus[0]) != 1) {
324 return nullptr;
325 }
326 for (uint32_t i = 1; i < key_len_words; ++i) {
327 if (fscanf(file, " , %u", &modulus[i]) != 1) {
328 return nullptr;
329 }
330 }
331
332 // Cconvert from little-endian array of little-endian words to big-endian
333 // byte array suitable as input for BN_bin2bn.
334 std::reverse((uint8_t*)modulus.get(),
335 (uint8_t*)(modulus.get() + key_len_words));
336
337 // The next sequence of values is the montgomery parameter R^2. Since we
338 // generally don't have a valid |n0inv|, we ignore this (see comment above).
339 uint32_t rr_value;
340 if (fscanf(file, " } , { %u", &rr_value) != 1) {
341 return nullptr;
342 }
343 for (uint32_t i = 1; i < key_len_words; ++i) {
344 if (fscanf(file, " , %u", &rr_value) != 1) {
345 return nullptr;
346 }
347 }
348 if (fscanf(file, " } } ") != 0) {
349 return nullptr;
350 }
351
352 // Initialize the key.
353 std::unique_ptr<RSA, RSADeleter> key(RSA_new());
354 if (!key) {
355 return nullptr;
356 }
357
358 key->n = BN_bin2bn((uint8_t*)modulus.get(),
359 key_len_words * sizeof(uint32_t), NULL);
360 if (!key->n) {
361 return nullptr;
362 }
363
364 key->e = BN_new();
365 if (!key->e || !BN_set_word(key->e, exponent)) {
366 return nullptr;
367 }
368
369 return key;
370}
371
372struct BNDeleter {
373 void operator()(BIGNUM* bn) {
374 BN_free(bn);
375 }
376};
377
378std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
379 uint32_t key_len_bytes = 0;
380 if (fscanf(file, " %i", &key_len_bytes) != 1) {
381 return nullptr;
382 }
383
384 std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
385 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
386 if (!group) {
387 return nullptr;
388 }
389
390 // Verify that |key_len| matches the group order.
391 if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
392 return nullptr;
393 }
394
395 // Read the public key coordinates. Note that the byte order in the file is
396 // little-endian, so we convert to big-endian here.
397 std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
398 std::unique_ptr<BIGNUM, BNDeleter> point[2];
399 for (int i = 0; i < 2; ++i) {
400 unsigned int byte = 0;
401 if (fscanf(file, " , { %u", &byte) != 1) {
402 return nullptr;
403 }
404 bytes[key_len_bytes - 1] = byte;
405
406 for (size_t i = 1; i < key_len_bytes; ++i) {
407 if (fscanf(file, " , %u", &byte) != 1) {
408 return nullptr;
409 }
410 bytes[key_len_bytes - i - 1] = byte;
411 }
412
413 point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
414 if (!point[i]) {
415 return nullptr;
416 }
417
418 if (fscanf(file, " }") != 0) {
419 return nullptr;
420 }
421 }
422
423 if (fscanf(file, " } ") != 0) {
424 return nullptr;
425 }
426
427 // Create and initialize the key.
428 std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
429 if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
430 !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
431 point[1].get())) {
432 return nullptr;
433 }
434
435 return key;
436}
437
Doug Zongker6c249f72012-11-02 15:04:05 -0700438// Reads a file containing one or more public keys as produced by
439// DumpPublicKey: this is an RSAPublicKey struct as it would appear
440// as a C source literal, eg:
441//
442// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
443//
444// For key versions newer than the original 2048-bit e=3 keys
445// supported by Android, the string is preceded by a version
446// identifier, eg:
447//
448// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
449//
450// (Note that the braces and commas in this example are actual
451// characters the parser expects to find in the file; the ellipses
452// indicate more numbers omitted from this example.)
453//
454// The file may contain multiple keys in this format, separated by
455// commas. The last key must not be followed by a comma.
456//
Doug Zongker30362a62013-04-10 11:32:17 -0700457// A Certificate is a pair of an RSAPublicKey and a particular hash
458// (we support SHA-1 and SHA-256; we store the hash length to signify
459// which is being used). The hash used is implied by the version number.
460//
461// 1: 2048-bit RSA key with e=3 and SHA-1 hash
462// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
463// 3: 2048-bit RSA key with e=3 and SHA-256 hash
464// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700465// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700466//
Tao Bao71e3e092016-02-02 14:02:27 -0800467// Returns true on success, and appends the found keys (at least one) to certs.
468// Otherwise returns false if the file failed to parse, or if it contains zero
469// keys. The contents in certs would be unspecified on failure.
470bool load_keys(const char* filename, std::vector<Certificate>& certs) {
471 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
472 if (!f) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700473 PLOG(ERROR) << "error opening " << filename;
Tao Bao71e3e092016-02-02 14:02:27 -0800474 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700475 }
476
Tao Bao71e3e092016-02-02 14:02:27 -0800477 while (true) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700478 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
Tao Bao71e3e092016-02-02 14:02:27 -0800479 Certificate& cert = certs.back();
Elliott Hughes8febafa2016-04-13 16:39:56 -0700480 uint32_t exponent = 0;
Doug Zongker6c249f72012-11-02 15:04:05 -0700481
Tao Bao71e3e092016-02-02 14:02:27 -0800482 char start_char;
483 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
484 if (start_char == '{') {
485 // a version 1 key has no version specifier.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700486 cert.key_type = Certificate::KEY_TYPE_RSA;
487 exponent = 3;
488 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800489 } else if (start_char == 'v') {
490 int version;
491 if (fscanf(f.get(), "%d {", &version) != 1) return false;
492 switch (version) {
493 case 2:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700494 cert.key_type = Certificate::KEY_TYPE_RSA;
495 exponent = 65537;
496 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800497 break;
498 case 3:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700499 cert.key_type = Certificate::KEY_TYPE_RSA;
500 exponent = 3;
501 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800502 break;
503 case 4:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700504 cert.key_type = Certificate::KEY_TYPE_RSA;
505 exponent = 65537;
506 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800507 break;
508 case 5:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700509 cert.key_type = Certificate::KEY_TYPE_EC;
510 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800511 break;
512 default:
513 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700514 }
Tao Bao71e3e092016-02-02 14:02:27 -0800515 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700516
Elliott Hughes8febafa2016-04-13 16:39:56 -0700517 if (cert.key_type == Certificate::KEY_TYPE_RSA) {
518 cert.rsa = parse_rsa_key(f.get(), exponent);
519 if (!cert.rsa) {
520 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700521 }
Tao Bao71e3e092016-02-02 14:02:27 -0800522
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700523 LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700524 } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
525 cert.ec = parse_ec_key(f.get());
526 if (!cert.ec) {
527 return false;
Tao Bao71e3e092016-02-02 14:02:27 -0800528 }
Tao Bao71e3e092016-02-02 14:02:27 -0800529 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700530 LOG(ERROR) << "Unknown key type " << cert.key_type;
Tao Bao71e3e092016-02-02 14:02:27 -0800531 return false;
532 }
533
534 // if the line ends in a comma, this file has more keys.
535 int ch = fgetc(f.get());
536 if (ch == ',') {
537 // more keys to come.
538 continue;
539 } else if (ch == EOF) {
540 break;
541 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700542 LOG(ERROR) << "unexpected character between keys";
Tao Bao71e3e092016-02-02 14:02:27 -0800543 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700544 }
545 }
546
Tao Bao71e3e092016-02-02 14:02:27 -0800547 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700548}