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