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 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 18 | #include <malloc.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <memory> |
| 24 | |
David Benjamin | a86392e | 2016-04-15 20:22:09 -0400 | [diff] [blame] | 25 | #include <openssl/bn.h> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 26 | #include <openssl/ecdsa.h> |
| 27 | #include <openssl/obj_mac.h> |
| 28 | |
| 29 | #include "asn1_decoder.h" |
| 30 | #include "common.h" |
| 31 | #include "ui.h" |
| 32 | #include "verifier.h" |
| 33 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 34 | extern RecoveryUI* ui; |
| 35 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 36 | static constexpr size_t MiB = 1024 * 1024; |
| 37 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 38 | /* |
| 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 | */ |
| 63 | static 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 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 110 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 111 | // the path to the zip. Verify it matches one of the given public |
| 112 | // keys. |
| 113 | // |
| 114 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 115 | // or no key matches the signature). |
| 116 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 117 | int verify_file(unsigned char* addr, size_t length, |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 118 | const std::vector<Certificate>& keys) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 119 | ui->SetProgress(0.0); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 120 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 121 | // An archive with a whole-file signature will end in six bytes: |
| 122 | // |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 123 | // (2-byte signature start) $ff $ff (2-byte comment size) |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 124 | // |
| 125 | // (As far as the ZIP format is concerned, these are part of the |
| 126 | // archive comment.) We start by reading this footer, this tells |
| 127 | // us how far back from the end we have to start reading to find |
| 128 | // the whole comment. |
| 129 | |
| 130 | #define FOOTER_SIZE 6 |
| 131 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 132 | if (length < FOOTER_SIZE) { |
| 133 | LOGE("not big enough to contain footer\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 134 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 137 | unsigned char* footer = addr + length - FOOTER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 139 | if (footer[2] != 0xff || footer[3] != 0xff) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 140 | LOGE("footer is wrong\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 141 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 144 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 145 | size_t signature_start = footer[0] + (footer[1] << 8); |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 146 | LOGI("comment is %zu bytes; signature %zu bytes from end\n", |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 147 | comment_size, signature_start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 149 | if (signature_start <= FOOTER_SIZE) { |
| 150 | LOGE("Signature start is in the footer"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 151 | return VERIFY_FAILURE; |
| 152 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 154 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 156 | // The end-of-central-directory record is 22 bytes plus any |
| 157 | // comment length. |
| 158 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 159 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 160 | if (length < eocd_size) { |
| 161 | LOGE("not big enough to contain EOCD\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 162 | return VERIFY_FAILURE; |
| 163 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 164 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 165 | // Determine how much of the file is covered by the signature. |
| 166 | // This is everything except the signature data and length, which |
| 167 | // includes all of the EOCD except for the comment length field (2 |
| 168 | // bytes) and the comment data. |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 169 | size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 171 | unsigned char* eocd = addr + length - eocd_size; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 172 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 173 | // If this is really is the EOCD record, it will begin with the |
| 174 | // magic number $50 $4b $05 $06. |
| 175 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 176 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 177 | LOGE("signature length doesn't match EOCD marker\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 178 | return VERIFY_FAILURE; |
| 179 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 180 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 181 | for (size_t i = 4; i < eocd_size-3; ++i) { |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 182 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
Doug Zongker | c652e41 | 2009-12-08 15:30:09 -0800 | [diff] [blame] | 183 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 184 | // if the sequence $50 $4b $05 $06 appears anywhere after |
| 185 | // the real one, minzip will find the later (wrong) one, |
| 186 | // which could be exploitable. Fail verification if |
| 187 | // this sequence occurs anywhere after the real one. |
| 188 | LOGE("EOCD marker occurs after start of EOCD\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 189 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 193 | bool need_sha1 = false; |
| 194 | bool need_sha256 = false; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 195 | for (const auto& key : keys) { |
| 196 | switch (key.hash_len) { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 197 | case SHA_DIGEST_LENGTH: need_sha1 = true; break; |
| 198 | case SHA256_DIGEST_LENGTH: need_sha256 = true; break; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | SHA_CTX sha1_ctx; |
| 203 | SHA256_CTX sha256_ctx; |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 204 | SHA1_Init(&sha1_ctx); |
| 205 | SHA256_Init(&sha256_ctx); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 206 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 207 | double frac = -1.0; |
| 208 | size_t so_far = 0; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 209 | while (so_far < signed_len) { |
Elliott Hughes | dd895d0 | 2016-04-19 15:24:38 -0700 | [diff] [blame] | 210 | // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a |
| 211 | // 1196MiB full OTA and 60% for an 89MiB incremental OTA. |
| 212 | // http://b/28135231. |
| 213 | size_t size = std::min(signed_len - so_far, 16 * MiB); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 214 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 215 | if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size); |
| 216 | if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 217 | so_far += size; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 218 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 219 | double f = so_far / (double)signed_len; |
| 220 | if (f > frac + 0.02 || size == so_far) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 221 | ui->SetProgress(f); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 222 | frac = f; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 226 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
| 227 | SHA1_Final(sha1, &sha1_ctx); |
| 228 | uint8_t sha256[SHA256_DIGEST_LENGTH]; |
| 229 | SHA256_Final(sha256, &sha256_ctx); |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 230 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 231 | uint8_t* sig_der = nullptr; |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 232 | size_t sig_der_length = 0; |
| 233 | |
| 234 | size_t signature_size = signature_start - FOOTER_SIZE; |
| 235 | if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der, |
| 236 | &sig_der_length)) { |
| 237 | LOGE("Could not find signature DER block\n"); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 238 | return VERIFY_FAILURE; |
| 239 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * Check to make sure at least one of the keys matches the signature. Since |
| 243 | * any key can match, we need to try each before determining a verification |
| 244 | * failure has happened. |
| 245 | */ |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 246 | size_t i = 0; |
| 247 | for (const auto& key : keys) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 248 | const uint8_t* hash; |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 249 | int hash_nid; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 250 | switch (key.hash_len) { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 251 | case SHA_DIGEST_LENGTH: |
| 252 | hash = sha1; |
| 253 | hash_nid = NID_sha1; |
| 254 | break; |
| 255 | case SHA256_DIGEST_LENGTH: |
| 256 | hash = sha256; |
| 257 | hash_nid = NID_sha256; |
| 258 | break; |
| 259 | default: |
| 260 | continue; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 263 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 264 | // the signing tool appends after the signature itself. |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 265 | if (key.key_type == Certificate::KEY_TYPE_RSA) { |
| 266 | if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der, |
| 267 | sig_der_length, key.rsa.get())) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 268 | LOGI("failed to verify against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 269 | continue; |
| 270 | } |
| 271 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 272 | LOGI("whole-file signature verified against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 273 | free(sig_der); |
| 274 | return VERIFY_SUCCESS; |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 275 | } else if (key.key_type == Certificate::KEY_TYPE_EC |
| 276 | && key.hash_len == SHA256_DIGEST_LENGTH) { |
| 277 | if (!ECDSA_verify(0, hash, key.hash_len, sig_der, |
| 278 | sig_der_length, key.ec.get())) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 279 | LOGI("failed to verify against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 283 | LOGI("whole-file signature verified against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 284 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 285 | return VERIFY_SUCCESS; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 286 | } else { |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 287 | LOGI("Unknown key type %d\n", key.key_type); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 288 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 289 | i++; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 290 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 291 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 292 | LOGE("failed to verify whole-file signature\n"); |
| 293 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 294 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 295 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 296 | std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) { |
| 297 | // Read key length in words and n0inv. n0inv is a precomputed montgomery |
| 298 | // parameter derived from the modulus and can be used to speed up |
| 299 | // verification. n0inv is 32 bits wide here, assuming the verification logic |
| 300 | // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits |
| 301 | // internally, in which case we don't have a valid n0inv. Thus, we just |
| 302 | // ignore the montgomery parameters and have BoringSSL recompute them |
| 303 | // internally. If/When the speedup from using the montgomery parameters |
| 304 | // becomes relevant, we can add more sophisticated code here to obtain a |
| 305 | // 64-bit n0inv and initialize the montgomery parameters in the key object. |
| 306 | uint32_t key_len_words = 0; |
| 307 | uint32_t n0inv = 0; |
| 308 | if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
| 312 | if (key_len_words > 8192 / 32) { |
| 313 | LOGE("key length (%d) too large\n", key_len_words); |
| 314 | return nullptr; |
| 315 | } |
| 316 | |
| 317 | // Read the modulus. |
| 318 | std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]); |
| 319 | if (fscanf(file, " , { %u", &modulus[0]) != 1) { |
| 320 | return nullptr; |
| 321 | } |
| 322 | for (uint32_t i = 1; i < key_len_words; ++i) { |
| 323 | if (fscanf(file, " , %u", &modulus[i]) != 1) { |
| 324 | return nullptr; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // Cconvert from little-endian array of little-endian words to big-endian |
| 329 | // byte array suitable as input for BN_bin2bn. |
| 330 | std::reverse((uint8_t*)modulus.get(), |
| 331 | (uint8_t*)(modulus.get() + key_len_words)); |
| 332 | |
| 333 | // The next sequence of values is the montgomery parameter R^2. Since we |
| 334 | // generally don't have a valid |n0inv|, we ignore this (see comment above). |
| 335 | uint32_t rr_value; |
| 336 | if (fscanf(file, " } , { %u", &rr_value) != 1) { |
| 337 | return nullptr; |
| 338 | } |
| 339 | for (uint32_t i = 1; i < key_len_words; ++i) { |
| 340 | if (fscanf(file, " , %u", &rr_value) != 1) { |
| 341 | return nullptr; |
| 342 | } |
| 343 | } |
| 344 | if (fscanf(file, " } } ") != 0) { |
| 345 | return nullptr; |
| 346 | } |
| 347 | |
| 348 | // Initialize the key. |
| 349 | std::unique_ptr<RSA, RSADeleter> key(RSA_new()); |
| 350 | if (!key) { |
| 351 | return nullptr; |
| 352 | } |
| 353 | |
| 354 | key->n = BN_bin2bn((uint8_t*)modulus.get(), |
| 355 | key_len_words * sizeof(uint32_t), NULL); |
| 356 | if (!key->n) { |
| 357 | return nullptr; |
| 358 | } |
| 359 | |
| 360 | key->e = BN_new(); |
| 361 | if (!key->e || !BN_set_word(key->e, exponent)) { |
| 362 | return nullptr; |
| 363 | } |
| 364 | |
| 365 | return key; |
| 366 | } |
| 367 | |
| 368 | struct BNDeleter { |
| 369 | void operator()(BIGNUM* bn) { |
| 370 | BN_free(bn); |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) { |
| 375 | uint32_t key_len_bytes = 0; |
| 376 | if (fscanf(file, " %i", &key_len_bytes) != 1) { |
| 377 | return nullptr; |
| 378 | } |
| 379 | |
| 380 | std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group( |
| 381 | EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free); |
| 382 | if (!group) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | |
| 386 | // Verify that |key_len| matches the group order. |
| 387 | if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) { |
| 388 | return nullptr; |
| 389 | } |
| 390 | |
| 391 | // Read the public key coordinates. Note that the byte order in the file is |
| 392 | // little-endian, so we convert to big-endian here. |
| 393 | std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]); |
| 394 | std::unique_ptr<BIGNUM, BNDeleter> point[2]; |
| 395 | for (int i = 0; i < 2; ++i) { |
| 396 | unsigned int byte = 0; |
| 397 | if (fscanf(file, " , { %u", &byte) != 1) { |
| 398 | return nullptr; |
| 399 | } |
| 400 | bytes[key_len_bytes - 1] = byte; |
| 401 | |
| 402 | for (size_t i = 1; i < key_len_bytes; ++i) { |
| 403 | if (fscanf(file, " , %u", &byte) != 1) { |
| 404 | return nullptr; |
| 405 | } |
| 406 | bytes[key_len_bytes - i - 1] = byte; |
| 407 | } |
| 408 | |
| 409 | point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr)); |
| 410 | if (!point[i]) { |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
| 414 | if (fscanf(file, " }") != 0) { |
| 415 | return nullptr; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (fscanf(file, " } ") != 0) { |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | // Create and initialize the key. |
| 424 | std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new()); |
| 425 | if (!key || !EC_KEY_set_group(key.get(), group.get()) || |
| 426 | !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(), |
| 427 | point[1].get())) { |
| 428 | return nullptr; |
| 429 | } |
| 430 | |
| 431 | return key; |
| 432 | } |
| 433 | |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 434 | // Reads a file containing one or more public keys as produced by |
| 435 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 436 | // as a C source literal, eg: |
| 437 | // |
| 438 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 439 | // |
| 440 | // For key versions newer than the original 2048-bit e=3 keys |
| 441 | // supported by Android, the string is preceded by a version |
| 442 | // identifier, eg: |
| 443 | // |
| 444 | // "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 445 | // |
| 446 | // (Note that the braces and commas in this example are actual |
| 447 | // characters the parser expects to find in the file; the ellipses |
| 448 | // indicate more numbers omitted from this example.) |
| 449 | // |
| 450 | // The file may contain multiple keys in this format, separated by |
| 451 | // commas. The last key must not be followed by a comma. |
| 452 | // |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 453 | // A Certificate is a pair of an RSAPublicKey and a particular hash |
| 454 | // (we support SHA-1 and SHA-256; we store the hash length to signify |
| 455 | // which is being used). The hash used is implied by the version number. |
| 456 | // |
| 457 | // 1: 2048-bit RSA key with e=3 and SHA-1 hash |
| 458 | // 2: 2048-bit RSA key with e=65537 and SHA-1 hash |
| 459 | // 3: 2048-bit RSA key with e=3 and SHA-256 hash |
| 460 | // 4: 2048-bit RSA key with e=65537 and SHA-256 hash |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 461 | // 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] | 462 | // |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 463 | // Returns true on success, and appends the found keys (at least one) to certs. |
| 464 | // Otherwise returns false if the file failed to parse, or if it contains zero |
| 465 | // keys. The contents in certs would be unspecified on failure. |
| 466 | bool load_keys(const char* filename, std::vector<Certificate>& certs) { |
| 467 | std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose); |
| 468 | if (!f) { |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 469 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 470 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 473 | while (true) { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 474 | certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 475 | Certificate& cert = certs.back(); |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 476 | uint32_t exponent = 0; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 477 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 478 | char start_char; |
| 479 | if (fscanf(f.get(), " %c", &start_char) != 1) return false; |
| 480 | if (start_char == '{') { |
| 481 | // a version 1 key has no version specifier. |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 482 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 483 | exponent = 3; |
| 484 | cert.hash_len = SHA_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 485 | } else if (start_char == 'v') { |
| 486 | int version; |
| 487 | if (fscanf(f.get(), "%d {", &version) != 1) return false; |
| 488 | switch (version) { |
| 489 | case 2: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 490 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 491 | exponent = 65537; |
| 492 | cert.hash_len = SHA_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 493 | break; |
| 494 | case 3: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 495 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 496 | exponent = 3; |
| 497 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 498 | break; |
| 499 | case 4: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 500 | cert.key_type = Certificate::KEY_TYPE_RSA; |
| 501 | exponent = 65537; |
| 502 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 503 | break; |
| 504 | case 5: |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 505 | cert.key_type = Certificate::KEY_TYPE_EC; |
| 506 | cert.hash_len = SHA256_DIGEST_LENGTH; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 507 | break; |
| 508 | default: |
| 509 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 510 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 511 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 512 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 513 | if (cert.key_type == Certificate::KEY_TYPE_RSA) { |
| 514 | cert.rsa = parse_rsa_key(f.get(), exponent); |
| 515 | if (!cert.rsa) { |
| 516 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 517 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 518 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 519 | LOGI("read key e=%d hash=%d\n", exponent, cert.hash_len); |
| 520 | } else if (cert.key_type == Certificate::KEY_TYPE_EC) { |
| 521 | cert.ec = parse_ec_key(f.get()); |
| 522 | if (!cert.ec) { |
| 523 | return false; |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 524 | } |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 525 | } else { |
| 526 | LOGE("Unknown key type %d\n", cert.key_type); |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | // if the line ends in a comma, this file has more keys. |
| 531 | int ch = fgetc(f.get()); |
| 532 | if (ch == ',') { |
| 533 | // more keys to come. |
| 534 | continue; |
| 535 | } else if (ch == EOF) { |
| 536 | break; |
| 537 | } else { |
| 538 | LOGE("unexpected character between keys\n"); |
| 539 | return false; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 543 | return true; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 544 | } |