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