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