The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 17 | #include "verifier.h" |
| 18 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 24 | #include <algorithm> |
Tao Bao | d7bf82e | 2017-03-18 09:24:11 -0700 | [diff] [blame] | 25 | #include <functional> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 26 | #include <memory> |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 27 | #include <vector> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 28 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
David Benjamin | a86392e | 2016-04-15 20:22:09 -0400 | [diff] [blame] | 30 | #include <openssl/bn.h> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 31 | #include <openssl/ecdsa.h> |
| 32 | #include <openssl/obj_mac.h> |
| 33 | |
| 34 | #include "asn1_decoder.h" |
Tao Bao | e179276 | 2016-04-19 22:31:01 -0700 | [diff] [blame] | 35 | #include "print_sha1.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 37 | static constexpr size_t MiB = 1024 * 1024; |
| 38 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 39 | /* |
| 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 64 | static 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 Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 68 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 69 | 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 76 | std::unique_ptr<asn1_context> signed_data_app(pkcs7_seq->asn1_constructed_get()); |
| 77 | if (signed_data_app == nullptr) { |
| 78 | return false; |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 79 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 80 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 81 | 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 Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 115 | /* |
| 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 123 | int verify_file(const unsigned char* addr, size_t length, const std::vector<Certificate>& keys, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 124 | const std::function<void(float)>& set_progress) { |
| 125 | if (set_progress) { |
| 126 | set_progress(0.0); |
| 127 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 128 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 129 | // 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 Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 136 | |
| 137 | #define FOOTER_SIZE 6 |
| 138 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 139 | if (length < FOOTER_SIZE) { |
| 140 | LOG(ERROR) << "not big enough to contain footer"; |
| 141 | return VERIFY_FAILURE; |
| 142 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 143 | |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 144 | const unsigned char* footer = addr + length - FOOTER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 146 | if (footer[2] != 0xff || footer[3] != 0xff) { |
| 147 | LOG(ERROR) << "footer is wrong"; |
| 148 | return VERIFY_FAILURE; |
| 149 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 150 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 151 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 156 | if (signature_start <= FOOTER_SIZE) { |
| 157 | LOG(ERROR) << "Signature start is in the footer"; |
| 158 | return VERIFY_FAILURE; |
| 159 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 160 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 161 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 162 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 163 | // 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 165 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 166 | if (length < eocd_size) { |
| 167 | LOG(ERROR) << "not big enough to contain EOCD"; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 168 | return VERIFY_FAILURE; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 169 | } |
| 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 176 | const unsigned char* eocd = addr + length - eocd_size; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 177 | |
| 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 185 | if (eocd[i] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 186 | // 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 234 | const uint8_t* signature = eocd + eocd_size - signature_start; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 235 | size_t signature_size = signature_start - FOOTER_SIZE; |
| 236 | |
| 237 | LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: " |
| 238 | << signature_size << "): " << print_hex(signature, signature_size); |
| 239 | |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 240 | std::vector<uint8_t> sig_der; |
| 241 | if (!read_pkcs7(signature, signature_size, &sig_der)) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 242 | 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 Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 268 | if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der.data(), sig_der.size(), |
| 269 | key.rsa.get())) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 270 | 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 Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 275 | return VERIFY_SUCCESS; |
| 276 | } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) { |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 277 | if (!ECDSA_verify(0, hash, key.hash_len, sig_der.data(), sig_der.size(), key.ec.get())) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 278 | 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 Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 283 | return VERIFY_SUCCESS; |
| 284 | } else { |
| 285 | LOG(INFO) << "Unknown key type " << key.key_type; |
| 286 | } |
| 287 | i++; |
| 288 | } |
| 289 | |
| 290 | if (need_sha1) { |
| 291 | LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH); |
| 292 | } |
| 293 | if (need_sha256) { |
| 294 | LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH); |
| 295 | } |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 296 | LOG(ERROR) << "failed to verify whole-file signature"; |
| 297 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 298 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 299 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 300 | std::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 Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 317 | LOG(ERROR) << "key length (" << key_len_words << ") too large"; |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 318 | 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 | |
| 372 | struct BNDeleter { |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 373 | void operator()(BIGNUM* bn) const { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 374 | BN_free(bn); |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | std::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 Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 438 | // 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 Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 457 | // 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 Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 465 | // 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 466 | // |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 467 | // 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. |
| 470 | bool 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 Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 473 | PLOG(ERROR) << "error opening " << filename; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 474 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 477 | while (true) { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 478 | certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 479 | Certificate& cert = certs.back(); |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 480 | uint32_t exponent = 0; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 481 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 482 | 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 Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 486 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 487 | exponent = 3; |
| 488 | cert.hash_len = SHA_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 489 | } 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 Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 494 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 495 | exponent = 65537; |
| 496 | cert.hash_len = SHA_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 497 | break; |
| 498 | case 3: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 499 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 500 | exponent = 3; |
| 501 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 502 | break; |
| 503 | case 4: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 504 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 505 | exponent = 65537; |
| 506 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 507 | break; |
| 508 | case 5: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 509 | cert.key_type = Certificate::KEY_TYPE_EC; |
| 510 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 511 | break; |
| 512 | default: |
| 513 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 514 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 515 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 516 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 517 | 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 Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 521 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 522 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 523 | LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len; |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 524 | } 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 Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 528 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 529 | } else { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 530 | LOG(ERROR) << "Unknown key type " << cert.key_type; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 531 | 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 Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 542 | LOG(ERROR) << "unexpected character between keys"; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 543 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 547 | return true; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 548 | } |