blob: 401bd7e3ed2d911e7647e76eb6d71108d0817f61 [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
Doug Zongker54e2e862009-08-17 13:21:04 -070017#include <errno.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080018#include <malloc.h>
19#include <stdio.h>
20#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021
Elliott Hughes8febafa2016-04-13 16:39:56 -070022#include <algorithm>
23#include <memory>
24
Tianjie Xu74778142016-08-05 18:00:04 -070025#include <android-base/logging.h>
David Benjamina86392e2016-04-15 20:22:09 -040026#include <openssl/bn.h>
Elliott Hughes8febafa2016-04-13 16:39:56 -070027#include <openssl/ecdsa.h>
28#include <openssl/obj_mac.h>
29
30#include "asn1_decoder.h"
31#include "common.h"
Tao Baoe1792762016-04-19 22:31:01 -070032#include "print_sha1.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070033#include "ui.h"
34#include "verifier.h"
35
Doug Zongker211aebc2011-10-28 15:13:10 -070036extern RecoveryUI* ui;
37
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
Doug Zongker54e2e862009-08-17 13:21:04 -0700112// Look for an RSA signature embedded in the .ZIP file comment given
113// the path to the zip. Verify it matches one of the given public
114// keys.
115//
116// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
117// or no key matches the signature).
118
Doug Zongker99916f02014-01-13 14:16:58 -0800119int verify_file(unsigned char* addr, size_t length,
Tao Bao71e3e092016-02-02 14:02:27 -0800120 const std::vector<Certificate>& keys) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700121 ui->SetProgress(0.0);
Doug Zongker54e2e862009-08-17 13:21:04 -0700122
Doug Zongker54e2e862009-08-17 13:21:04 -0700123 // An archive with a whole-file signature will end in six bytes:
124 //
Doug Zongker73ae31c2009-12-09 17:01:45 -0800125 // (2-byte signature start) $ff $ff (2-byte comment size)
Doug Zongker54e2e862009-08-17 13:21:04 -0700126 //
127 // (As far as the ZIP format is concerned, these are part of the
128 // archive comment.) We start by reading this footer, this tells
129 // us how far back from the end we have to start reading to find
130 // the whole comment.
131
132#define FOOTER_SIZE 6
133
Doug Zongker99916f02014-01-13 14:16:58 -0800134 if (length < FOOTER_SIZE) {
Tianjie Xu74778142016-08-05 18:00:04 -0700135 LOG(ERROR) << "not big enough to contain footer";
Doug Zongker54e2e862009-08-17 13:21:04 -0700136 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 }
138
Doug Zongker99916f02014-01-13 14:16:58 -0800139 unsigned char* footer = addr + length - FOOTER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140
Doug Zongker54e2e862009-08-17 13:21:04 -0700141 if (footer[2] != 0xff || footer[3] != 0xff) {
Tianjie Xu74778142016-08-05 18:00:04 -0700142 LOG(ERROR) << "footer is wrong";
Doug Zongker54e2e862009-08-17 13:21:04 -0700143 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 }
145
Doug Zongker28ce47c2011-10-28 10:33:05 -0700146 size_t comment_size = footer[4] + (footer[5] << 8);
147 size_t signature_start = footer[0] + (footer[1] << 8);
Tianjie Xu74778142016-08-05 18:00:04 -0700148 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
Kenny Root7a4adb52013-10-09 10:14:35 -0700151 if (signature_start <= FOOTER_SIZE) {
Tianjie Xu74778142016-08-05 18:00:04 -0700152 LOG(ERROR) << "Signature start is in the footer";
Doug Zongker54e2e862009-08-17 13:21:04 -0700153 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
Doug Zongker54e2e862009-08-17 13:21:04 -0700158 // The end-of-central-directory record is 22 bytes plus any
159 // comment length.
160 size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161
Doug Zongker99916f02014-01-13 14:16:58 -0800162 if (length < eocd_size) {
Tianjie Xu74778142016-08-05 18:00:04 -0700163 LOG(ERROR) << "not big enough to contain EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700164 return VERIFY_FAILURE;
165 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166
Doug Zongker54e2e862009-08-17 13:21:04 -0700167 // Determine how much of the file is covered by the signature.
168 // This is everything except the signature data and length, which
169 // includes all of the EOCD except for the comment length field (2
170 // bytes) and the comment data.
Doug Zongker99916f02014-01-13 14:16:58 -0800171 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172
Doug Zongker99916f02014-01-13 14:16:58 -0800173 unsigned char* eocd = addr + length - eocd_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
Doug Zongker54e2e862009-08-17 13:21:04 -0700175 // If this is really is the EOCD record, it will begin with the
176 // magic number $50 $4b $05 $06.
177 if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
178 eocd[2] != 0x05 || eocd[3] != 0x06) {
Tianjie Xu74778142016-08-05 18:00:04 -0700179 LOG(ERROR) << "signature length doesn't match EOCD marker";
Doug Zongker54e2e862009-08-17 13:21:04 -0700180 return VERIFY_FAILURE;
181 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182
Tao Bao71e3e092016-02-02 14:02:27 -0800183 for (size_t i = 4; i < eocd_size-3; ++i) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700184 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
Doug Zongkerc652e412009-12-08 15:30:09 -0800185 eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
Doug Zongker54e2e862009-08-17 13:21:04 -0700186 // if the sequence $50 $4b $05 $06 appears anywhere after
187 // the real one, minzip will find the later (wrong) one,
188 // which could be exploitable. Fail verification if
189 // this sequence occurs anywhere after the real one.
Tianjie Xu74778142016-08-05 18:00:04 -0700190 LOG(ERROR) << "EOCD marker occurs after start of EOCD";
Doug Zongker54e2e862009-08-17 13:21:04 -0700191 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192 }
193 }
194
Doug Zongker30362a62013-04-10 11:32:17 -0700195 bool need_sha1 = false;
196 bool need_sha256 = false;
Tao Bao71e3e092016-02-02 14:02:27 -0800197 for (const auto& key : keys) {
198 switch (key.hash_len) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700199 case SHA_DIGEST_LENGTH: need_sha1 = true; break;
200 case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
Doug Zongker30362a62013-04-10 11:32:17 -0700201 }
202 }
203
204 SHA_CTX sha1_ctx;
205 SHA256_CTX sha256_ctx;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700206 SHA1_Init(&sha1_ctx);
207 SHA256_Init(&sha256_ctx);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208
Doug Zongker54e2e862009-08-17 13:21:04 -0700209 double frac = -1.0;
210 size_t so_far = 0;
Doug Zongker54e2e862009-08-17 13:21:04 -0700211 while (so_far < signed_len) {
Elliott Hughesdd895d02016-04-19 15:24:38 -0700212 // 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);
Doug Zongker99916f02014-01-13 14:16:58 -0800216
Elliott Hughes8febafa2016-04-13 16:39:56 -0700217 if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
218 if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
Doug Zongker54e2e862009-08-17 13:21:04 -0700219 so_far += size;
Doug Zongker99916f02014-01-13 14:16:58 -0800220
Doug Zongker54e2e862009-08-17 13:21:04 -0700221 double f = so_far / (double)signed_len;
222 if (f > frac + 0.02 || size == so_far) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700223 ui->SetProgress(f);
Doug Zongker54e2e862009-08-17 13:21:04 -0700224 frac = f;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 }
226 }
227
Elliott Hughes8febafa2016-04-13 16:39:56 -0700228 uint8_t sha1[SHA_DIGEST_LENGTH];
229 SHA1_Final(sha1, &sha1_ctx);
230 uint8_t sha256[SHA256_DIGEST_LENGTH];
231 SHA256_Final(sha256, &sha256_ctx);
Doug Zongker30362a62013-04-10 11:32:17 -0700232
Tao Bao71e3e092016-02-02 14:02:27 -0800233 uint8_t* sig_der = nullptr;
Kenny Root7a4adb52013-10-09 10:14:35 -0700234 size_t sig_der_length = 0;
235
Tao Baoe1792762016-04-19 22:31:01 -0700236 uint8_t* signature = eocd + eocd_size - signature_start;
Kenny Root7a4adb52013-10-09 10:14:35 -0700237 size_t signature_size = signature_start - FOOTER_SIZE;
Tao Baoe1792762016-04-19 22:31:01 -0700238
Tianjie Xu74778142016-08-05 18:00:04 -0700239 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
240 << signature_size << "): " << print_hex(signature, signature_size);
Tao Baoe1792762016-04-19 22:31:01 -0700241
242 if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) {
Tianjie Xu74778142016-08-05 18:00:04 -0700243 LOG(ERROR) << "Could not find signature DER block";
Kenny Root7a4adb52013-10-09 10:14:35 -0700244 return VERIFY_FAILURE;
245 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700246
247 /*
248 * Check to make sure at least one of the keys matches the signature. Since
249 * any key can match, we need to try each before determining a verification
250 * failure has happened.
251 */
Tao Bao71e3e092016-02-02 14:02:27 -0800252 size_t i = 0;
253 for (const auto& key : keys) {
Doug Zongker30362a62013-04-10 11:32:17 -0700254 const uint8_t* hash;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700255 int hash_nid;
Tao Bao71e3e092016-02-02 14:02:27 -0800256 switch (key.hash_len) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700257 case SHA_DIGEST_LENGTH:
258 hash = sha1;
259 hash_nid = NID_sha1;
260 break;
261 case SHA256_DIGEST_LENGTH:
262 hash = sha256;
263 hash_nid = NID_sha256;
264 break;
265 default:
266 continue;
Doug Zongker30362a62013-04-10 11:32:17 -0700267 }
268
Doug Zongker73ae31c2009-12-09 17:01:45 -0800269 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
Doug Zongker54e2e862009-08-17 13:21:04 -0700270 // the signing tool appends after the signature itself.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700271 if (key.key_type == Certificate::KEY_TYPE_RSA) {
272 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der,
273 sig_der_length, key.rsa.get())) {
Tianjie Xu74778142016-08-05 18:00:04 -0700274 LOG(INFO) << "failed to verify against RSA key " << i;
Kenny Root7a4adb52013-10-09 10:14:35 -0700275 continue;
276 }
277
Tianjie Xu74778142016-08-05 18:00:04 -0700278 LOG(INFO) << "whole-file signature verified against RSA key " << i;
Kenny Root7a4adb52013-10-09 10:14:35 -0700279 free(sig_der);
280 return VERIFY_SUCCESS;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700281 } else if (key.key_type == Certificate::KEY_TYPE_EC
282 && key.hash_len == SHA256_DIGEST_LENGTH) {
283 if (!ECDSA_verify(0, hash, key.hash_len, sig_der,
284 sig_der_length, key.ec.get())) {
Tianjie Xu74778142016-08-05 18:00:04 -0700285 LOG(INFO) << "failed to verify against EC key " << i;
Kenny Root7a4adb52013-10-09 10:14:35 -0700286 continue;
287 }
288
Tianjie Xu74778142016-08-05 18:00:04 -0700289 LOG(INFO) << "whole-file signature verified against EC key " << i;
Kenny Root7a4adb52013-10-09 10:14:35 -0700290 free(sig_der);
Doug Zongker54e2e862009-08-17 13:21:04 -0700291 return VERIFY_SUCCESS;
Doug Zongker6c249f72012-11-02 15:04:05 -0700292 } else {
Tianjie Xu74778142016-08-05 18:00:04 -0700293 LOG(INFO) << "Unknown key type " << key.key_type;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 }
Tao Bao71e3e092016-02-02 14:02:27 -0800295 i++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 }
Tao Baoe1792762016-04-19 22:31:01 -0700297
298 if (need_sha1) {
Tianjie Xu74778142016-08-05 18:00:04 -0700299 LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH);
Tao Baoe1792762016-04-19 22:31:01 -0700300 }
301 if (need_sha256) {
Tianjie Xu74778142016-08-05 18:00:04 -0700302 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
Tao Baoe1792762016-04-19 22:31:01 -0700303 }
Kenny Root7a4adb52013-10-09 10:14:35 -0700304 free(sig_der);
Tianjie Xu74778142016-08-05 18:00:04 -0700305 LOG(ERROR) << "failed to verify whole-file signature";
Doug Zongker54e2e862009-08-17 13:21:04 -0700306 return VERIFY_FAILURE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307}
Doug Zongker6c249f72012-11-02 15:04:05 -0700308
Elliott Hughes8febafa2016-04-13 16:39:56 -0700309std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
310 // Read key length in words and n0inv. n0inv is a precomputed montgomery
311 // parameter derived from the modulus and can be used to speed up
312 // verification. n0inv is 32 bits wide here, assuming the verification logic
313 // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
314 // internally, in which case we don't have a valid n0inv. Thus, we just
315 // ignore the montgomery parameters and have BoringSSL recompute them
316 // internally. If/When the speedup from using the montgomery parameters
317 // becomes relevant, we can add more sophisticated code here to obtain a
318 // 64-bit n0inv and initialize the montgomery parameters in the key object.
319 uint32_t key_len_words = 0;
320 uint32_t n0inv = 0;
321 if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
322 return nullptr;
323 }
324
325 if (key_len_words > 8192 / 32) {
Tianjie Xu74778142016-08-05 18:00:04 -0700326 LOG(ERROR) << "key length (" << key_len_words << ") too large";
Elliott Hughes8febafa2016-04-13 16:39:56 -0700327 return nullptr;
328 }
329
330 // Read the modulus.
331 std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
332 if (fscanf(file, " , { %u", &modulus[0]) != 1) {
333 return nullptr;
334 }
335 for (uint32_t i = 1; i < key_len_words; ++i) {
336 if (fscanf(file, " , %u", &modulus[i]) != 1) {
337 return nullptr;
338 }
339 }
340
341 // Cconvert from little-endian array of little-endian words to big-endian
342 // byte array suitable as input for BN_bin2bn.
343 std::reverse((uint8_t*)modulus.get(),
344 (uint8_t*)(modulus.get() + key_len_words));
345
346 // The next sequence of values is the montgomery parameter R^2. Since we
347 // generally don't have a valid |n0inv|, we ignore this (see comment above).
348 uint32_t rr_value;
349 if (fscanf(file, " } , { %u", &rr_value) != 1) {
350 return nullptr;
351 }
352 for (uint32_t i = 1; i < key_len_words; ++i) {
353 if (fscanf(file, " , %u", &rr_value) != 1) {
354 return nullptr;
355 }
356 }
357 if (fscanf(file, " } } ") != 0) {
358 return nullptr;
359 }
360
361 // Initialize the key.
362 std::unique_ptr<RSA, RSADeleter> key(RSA_new());
363 if (!key) {
364 return nullptr;
365 }
366
367 key->n = BN_bin2bn((uint8_t*)modulus.get(),
368 key_len_words * sizeof(uint32_t), NULL);
369 if (!key->n) {
370 return nullptr;
371 }
372
373 key->e = BN_new();
374 if (!key->e || !BN_set_word(key->e, exponent)) {
375 return nullptr;
376 }
377
378 return key;
379}
380
381struct BNDeleter {
382 void operator()(BIGNUM* bn) {
383 BN_free(bn);
384 }
385};
386
387std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
388 uint32_t key_len_bytes = 0;
389 if (fscanf(file, " %i", &key_len_bytes) != 1) {
390 return nullptr;
391 }
392
393 std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
394 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
395 if (!group) {
396 return nullptr;
397 }
398
399 // Verify that |key_len| matches the group order.
400 if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
401 return nullptr;
402 }
403
404 // Read the public key coordinates. Note that the byte order in the file is
405 // little-endian, so we convert to big-endian here.
406 std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
407 std::unique_ptr<BIGNUM, BNDeleter> point[2];
408 for (int i = 0; i < 2; ++i) {
409 unsigned int byte = 0;
410 if (fscanf(file, " , { %u", &byte) != 1) {
411 return nullptr;
412 }
413 bytes[key_len_bytes - 1] = byte;
414
415 for (size_t i = 1; i < key_len_bytes; ++i) {
416 if (fscanf(file, " , %u", &byte) != 1) {
417 return nullptr;
418 }
419 bytes[key_len_bytes - i - 1] = byte;
420 }
421
422 point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
423 if (!point[i]) {
424 return nullptr;
425 }
426
427 if (fscanf(file, " }") != 0) {
428 return nullptr;
429 }
430 }
431
432 if (fscanf(file, " } ") != 0) {
433 return nullptr;
434 }
435
436 // Create and initialize the key.
437 std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
438 if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
439 !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
440 point[1].get())) {
441 return nullptr;
442 }
443
444 return key;
445}
446
Doug Zongker6c249f72012-11-02 15:04:05 -0700447// Reads a file containing one or more public keys as produced by
448// DumpPublicKey: this is an RSAPublicKey struct as it would appear
449// as a C source literal, eg:
450//
451// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
452//
453// For key versions newer than the original 2048-bit e=3 keys
454// supported by Android, the string is preceded by a version
455// identifier, eg:
456//
457// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
458//
459// (Note that the braces and commas in this example are actual
460// characters the parser expects to find in the file; the ellipses
461// indicate more numbers omitted from this example.)
462//
463// The file may contain multiple keys in this format, separated by
464// commas. The last key must not be followed by a comma.
465//
Doug Zongker30362a62013-04-10 11:32:17 -0700466// A Certificate is a pair of an RSAPublicKey and a particular hash
467// (we support SHA-1 and SHA-256; we store the hash length to signify
468// which is being used). The hash used is implied by the version number.
469//
470// 1: 2048-bit RSA key with e=3 and SHA-1 hash
471// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
472// 3: 2048-bit RSA key with e=3 and SHA-256 hash
473// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
Kenny Root7a4adb52013-10-09 10:14:35 -0700474// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
Doug Zongker30362a62013-04-10 11:32:17 -0700475//
Tao Bao71e3e092016-02-02 14:02:27 -0800476// Returns true on success, and appends the found keys (at least one) to certs.
477// Otherwise returns false if the file failed to parse, or if it contains zero
478// keys. The contents in certs would be unspecified on failure.
479bool load_keys(const char* filename, std::vector<Certificate>& certs) {
480 std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
481 if (!f) {
Tianjie Xu74778142016-08-05 18:00:04 -0700482 PLOG(ERROR) << "error opening " << filename;
Tao Bao71e3e092016-02-02 14:02:27 -0800483 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700484 }
485
Tao Bao71e3e092016-02-02 14:02:27 -0800486 while (true) {
Elliott Hughes8febafa2016-04-13 16:39:56 -0700487 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
Tao Bao71e3e092016-02-02 14:02:27 -0800488 Certificate& cert = certs.back();
Elliott Hughes8febafa2016-04-13 16:39:56 -0700489 uint32_t exponent = 0;
Doug Zongker6c249f72012-11-02 15:04:05 -0700490
Tao Bao71e3e092016-02-02 14:02:27 -0800491 char start_char;
492 if (fscanf(f.get(), " %c", &start_char) != 1) return false;
493 if (start_char == '{') {
494 // a version 1 key has no version specifier.
Elliott Hughes8febafa2016-04-13 16:39:56 -0700495 cert.key_type = Certificate::KEY_TYPE_RSA;
496 exponent = 3;
497 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800498 } else if (start_char == 'v') {
499 int version;
500 if (fscanf(f.get(), "%d {", &version) != 1) return false;
501 switch (version) {
502 case 2:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700503 cert.key_type = Certificate::KEY_TYPE_RSA;
504 exponent = 65537;
505 cert.hash_len = SHA_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800506 break;
507 case 3:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700508 cert.key_type = Certificate::KEY_TYPE_RSA;
509 exponent = 3;
510 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800511 break;
512 case 4:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700513 cert.key_type = Certificate::KEY_TYPE_RSA;
514 exponent = 65537;
515 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800516 break;
517 case 5:
Elliott Hughes8febafa2016-04-13 16:39:56 -0700518 cert.key_type = Certificate::KEY_TYPE_EC;
519 cert.hash_len = SHA256_DIGEST_LENGTH;
Tao Bao71e3e092016-02-02 14:02:27 -0800520 break;
521 default:
522 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700523 }
Tao Bao71e3e092016-02-02 14:02:27 -0800524 }
Doug Zongker6c249f72012-11-02 15:04:05 -0700525
Elliott Hughes8febafa2016-04-13 16:39:56 -0700526 if (cert.key_type == Certificate::KEY_TYPE_RSA) {
527 cert.rsa = parse_rsa_key(f.get(), exponent);
528 if (!cert.rsa) {
529 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700530 }
Tao Bao71e3e092016-02-02 14:02:27 -0800531
Tianjie Xu74778142016-08-05 18:00:04 -0700532 LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
Elliott Hughes8febafa2016-04-13 16:39:56 -0700533 } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
534 cert.ec = parse_ec_key(f.get());
535 if (!cert.ec) {
536 return false;
Tao Bao71e3e092016-02-02 14:02:27 -0800537 }
Tao Bao71e3e092016-02-02 14:02:27 -0800538 } else {
Tianjie Xu74778142016-08-05 18:00:04 -0700539 LOG(ERROR) << "Unknown key type " << cert.key_type;
Tao Bao71e3e092016-02-02 14:02:27 -0800540 return false;
541 }
542
543 // if the line ends in a comma, this file has more keys.
544 int ch = fgetc(f.get());
545 if (ch == ',') {
546 // more keys to come.
547 continue;
548 } else if (ch == EOF) {
549 break;
550 } else {
Tianjie Xu74778142016-08-05 18:00:04 -0700551 LOG(ERROR) << "unexpected character between keys";
Tao Bao71e3e092016-02-02 14:02:27 -0800552 return false;
Doug Zongker6c249f72012-11-02 15:04:05 -0700553 }
554 }
555
Tao Bao71e3e092016-02-02 14:02:27 -0800556 return true;
Doug Zongker6c249f72012-11-02 15:04:05 -0700557}