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 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 17 | #include "asn1_decoder.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | #include "common.h" |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 19 | #include "ui.h" |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 20 | #include "verifier.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 22 | #include "mincrypt/dsa_sig.h" |
| 23 | #include "mincrypt/p256.h" |
| 24 | #include "mincrypt/p256_ecdsa.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include "mincrypt/rsa.h" |
| 26 | #include "mincrypt/sha.h" |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 27 | #include "mincrypt/sha256.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 29 | #include <errno.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 30 | #include <malloc.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 34 | extern RecoveryUI* ui; |
| 35 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Simple version of PKCS#7 SignedData extraction. This extracts the |
| 38 | * signature OCTET STRING to be used for signature verification. |
| 39 | * |
| 40 | * For full details, see http://www.ietf.org/rfc/rfc3852.txt |
| 41 | * |
| 42 | * The PKCS#7 structure looks like: |
| 43 | * |
| 44 | * SEQUENCE (ContentInfo) |
| 45 | * OID (ContentType) |
| 46 | * [0] (content) |
| 47 | * SEQUENCE (SignedData) |
| 48 | * INTEGER (version CMSVersion) |
| 49 | * SET (DigestAlgorithmIdentifiers) |
| 50 | * SEQUENCE (EncapsulatedContentInfo) |
| 51 | * [0] (CertificateSet OPTIONAL) |
| 52 | * [1] (RevocationInfoChoices OPTIONAL) |
| 53 | * SET (SignerInfos) |
| 54 | * SEQUENCE (SignerInfo) |
| 55 | * INTEGER (CMSVersion) |
| 56 | * SEQUENCE (SignerIdentifier) |
| 57 | * SEQUENCE (DigestAlgorithmIdentifier) |
| 58 | * SEQUENCE (SignatureAlgorithmIdentifier) |
| 59 | * OCTET STRING (SignatureValue) |
| 60 | */ |
| 61 | static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der, |
| 62 | size_t* sig_der_length) { |
| 63 | asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len); |
| 64 | if (ctx == NULL) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx); |
| 69 | if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) { |
| 70 | asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq); |
| 71 | if (signed_data_app != NULL) { |
| 72 | asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app); |
| 73 | if (signed_data_seq != NULL |
| 74 | && asn1_sequence_next(signed_data_seq) |
| 75 | && asn1_sequence_next(signed_data_seq) |
| 76 | && asn1_sequence_next(signed_data_seq) |
| 77 | && asn1_constructed_skip_all(signed_data_seq)) { |
| 78 | asn1_context_t *sig_set = asn1_set_get(signed_data_seq); |
| 79 | if (sig_set != NULL) { |
| 80 | asn1_context_t* sig_seq = asn1_sequence_get(sig_set); |
| 81 | if (sig_seq != NULL |
| 82 | && asn1_sequence_next(sig_seq) |
| 83 | && asn1_sequence_next(sig_seq) |
| 84 | && asn1_sequence_next(sig_seq) |
| 85 | && asn1_sequence_next(sig_seq)) { |
| 86 | uint8_t* sig_der_ptr; |
| 87 | if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) { |
| 88 | *sig_der = (uint8_t*) malloc(*sig_der_length); |
| 89 | if (*sig_der != NULL) { |
| 90 | memcpy(*sig_der, sig_der_ptr, *sig_der_length); |
| 91 | } |
| 92 | } |
| 93 | asn1_context_free(sig_seq); |
| 94 | } |
| 95 | asn1_context_free(sig_set); |
| 96 | } |
| 97 | asn1_context_free(signed_data_seq); |
| 98 | } |
| 99 | asn1_context_free(signed_data_app); |
| 100 | } |
| 101 | asn1_context_free(pkcs7_seq); |
| 102 | } |
| 103 | asn1_context_free(ctx); |
| 104 | |
| 105 | return *sig_der != NULL; |
| 106 | } |
| 107 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 108 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 109 | // the path to the zip. Verify it matches one of the given public |
| 110 | // keys. |
| 111 | // |
| 112 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 113 | // or no key matches the signature). |
| 114 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 115 | int verify_file(unsigned char* addr, size_t length, |
| 116 | const Certificate* pKeys, unsigned int numKeys) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 117 | ui->SetProgress(0.0); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 118 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 119 | // An archive with a whole-file signature will end in six bytes: |
| 120 | // |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 121 | // (2-byte signature start) $ff $ff (2-byte comment size) |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 122 | // |
| 123 | // (As far as the ZIP format is concerned, these are part of the |
| 124 | // archive comment.) We start by reading this footer, this tells |
| 125 | // us how far back from the end we have to start reading to find |
| 126 | // the whole comment. |
| 127 | |
| 128 | #define FOOTER_SIZE 6 |
| 129 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 130 | if (length < FOOTER_SIZE) { |
| 131 | LOGE("not big enough to contain footer\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 132 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 135 | unsigned char* footer = addr + length - FOOTER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 137 | if (footer[2] != 0xff || footer[3] != 0xff) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 138 | LOGE("footer is wrong\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 139 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 142 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 143 | size_t signature_start = footer[0] + (footer[1] << 8); |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 144 | LOGI("comment is %zu bytes; signature %zu bytes from end\n", |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 145 | comment_size, signature_start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 146 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 147 | if (signature_start <= FOOTER_SIZE) { |
| 148 | LOGE("Signature start is in the footer"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 149 | return VERIFY_FAILURE; |
| 150 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 151 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 152 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 154 | // The end-of-central-directory record is 22 bytes plus any |
| 155 | // comment length. |
| 156 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 157 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 158 | if (length < eocd_size) { |
| 159 | LOGE("not big enough to contain EOCD\n"); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 160 | return VERIFY_FAILURE; |
| 161 | } |
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 | // Determine how much of the file is covered by the signature. |
| 164 | // This is everything except the signature data and length, which |
| 165 | // includes all of the EOCD except for the comment length field (2 |
| 166 | // bytes) and the comment data. |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 167 | 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] | 168 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 169 | unsigned char* eocd = addr + length - eocd_size; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 171 | // If this is really is the EOCD record, it will begin with the |
| 172 | // magic number $50 $4b $05 $06. |
| 173 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 174 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 175 | LOGE("signature length doesn't match EOCD marker\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 | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 179 | size_t i; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 180 | for (i = 4; i < eocd_size-3; ++i) { |
| 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 | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 192 | #define BUFFER_SIZE 4096 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 193 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 194 | bool need_sha1 = false; |
| 195 | bool need_sha256 = false; |
| 196 | for (i = 0; i < numKeys; ++i) { |
| 197 | switch (pKeys[i].hash_len) { |
| 198 | case SHA_DIGEST_SIZE: need_sha1 = true; break; |
| 199 | case SHA256_DIGEST_SIZE: need_sha256 = true; break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | SHA_CTX sha1_ctx; |
| 204 | SHA256_CTX sha256_ctx; |
| 205 | SHA_init(&sha1_ctx); |
| 206 | SHA256_init(&sha256_ctx); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 207 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 208 | double frac = -1.0; |
| 209 | size_t so_far = 0; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 210 | while (so_far < signed_len) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 211 | size_t size = signed_len - so_far; |
| 212 | if (size > BUFFER_SIZE) size = BUFFER_SIZE; |
| 213 | |
| 214 | if (need_sha1) SHA_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 | |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 225 | const uint8_t* sha1 = SHA_final(&sha1_ctx); |
| 226 | const uint8_t* sha256 = SHA256_final(&sha256_ctx); |
| 227 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 228 | uint8_t* sig_der = NULL; |
| 229 | size_t sig_der_length = 0; |
| 230 | |
| 231 | size_t signature_size = signature_start - FOOTER_SIZE; |
| 232 | if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der, |
| 233 | &sig_der_length)) { |
| 234 | LOGE("Could not find signature DER block\n"); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 235 | return VERIFY_FAILURE; |
| 236 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Check to make sure at least one of the keys matches the signature. Since |
| 240 | * any key can match, we need to try each before determining a verification |
| 241 | * failure has happened. |
| 242 | */ |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 243 | for (i = 0; i < numKeys; ++i) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 244 | const uint8_t* hash; |
| 245 | switch (pKeys[i].hash_len) { |
| 246 | case SHA_DIGEST_SIZE: hash = sha1; break; |
| 247 | case SHA256_DIGEST_SIZE: hash = sha256; break; |
| 248 | default: continue; |
| 249 | } |
| 250 | |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 251 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 252 | // the signing tool appends after the signature itself. |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 253 | if (pKeys[i].key_type == Certificate::RSA) { |
| 254 | if (sig_der_length < RSANUMBYTES) { |
| 255 | // "signature" block isn't big enough to contain an RSA block. |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 256 | LOGI("signature is too short for RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 257 | continue; |
| 258 | } |
| 259 | |
| 260 | if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES, |
| 261 | hash, pKeys[i].hash_len)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 262 | LOGI("failed to verify against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 266 | LOGI("whole-file signature verified against RSA key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 267 | free(sig_der); |
| 268 | return VERIFY_SUCCESS; |
| 269 | } else if (pKeys[i].key_type == Certificate::EC |
| 270 | && pKeys[i].hash_len == SHA256_DIGEST_SIZE) { |
| 271 | p256_int r, s; |
| 272 | if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 273 | LOGI("Not a DSA signature block for EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 274 | continue; |
| 275 | } |
| 276 | |
| 277 | p256_int p256_hash; |
| 278 | p256_from_bin(hash, &p256_hash); |
| 279 | if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y), |
| 280 | &p256_hash, &r, &s)) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 281 | LOGI("failed to verify against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 282 | continue; |
| 283 | } |
| 284 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 285 | LOGI("whole-file signature verified against EC key %zu\n", i); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 286 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 287 | return VERIFY_SUCCESS; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 288 | } else { |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 289 | LOGI("Unknown key type %d\n", pKeys[i].key_type); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 292 | free(sig_der); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 293 | LOGE("failed to verify whole-file signature\n"); |
| 294 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 295 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 296 | |
| 297 | // Reads a file containing one or more public keys as produced by |
| 298 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 299 | // as a C source literal, eg: |
| 300 | // |
| 301 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 302 | // |
| 303 | // For key versions newer than the original 2048-bit e=3 keys |
| 304 | // supported by Android, the string is preceded by a version |
| 305 | // identifier, eg: |
| 306 | // |
| 307 | // "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 308 | // |
| 309 | // (Note that the braces and commas in this example are actual |
| 310 | // characters the parser expects to find in the file; the ellipses |
| 311 | // indicate more numbers omitted from this example.) |
| 312 | // |
| 313 | // The file may contain multiple keys in this format, separated by |
| 314 | // commas. The last key must not be followed by a comma. |
| 315 | // |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 316 | // A Certificate is a pair of an RSAPublicKey and a particular hash |
| 317 | // (we support SHA-1 and SHA-256; we store the hash length to signify |
| 318 | // which is being used). The hash used is implied by the version number. |
| 319 | // |
| 320 | // 1: 2048-bit RSA key with e=3 and SHA-1 hash |
| 321 | // 2: 2048-bit RSA key with e=65537 and SHA-1 hash |
| 322 | // 3: 2048-bit RSA key with e=3 and SHA-256 hash |
| 323 | // 4: 2048-bit RSA key with e=65537 and SHA-256 hash |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 324 | // 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] | 325 | // |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 326 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 327 | Certificate* |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 328 | load_keys(const char* filename, int* numKeys) { |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 329 | Certificate* out = NULL; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 330 | *numKeys = 0; |
| 331 | |
| 332 | FILE* f = fopen(filename, "r"); |
| 333 | if (f == NULL) { |
| 334 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 335 | goto exit; |
| 336 | } |
| 337 | |
| 338 | { |
| 339 | int i; |
| 340 | bool done = false; |
| 341 | while (!done) { |
| 342 | ++*numKeys; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 343 | out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); |
| 344 | Certificate* cert = out + (*numKeys - 1); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 345 | memset(cert, '\0', sizeof(Certificate)); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 346 | |
| 347 | char start_char; |
| 348 | if (fscanf(f, " %c", &start_char) != 1) goto exit; |
| 349 | if (start_char == '{') { |
| 350 | // a version 1 key has no version specifier. |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 351 | cert->key_type = Certificate::RSA; |
| 352 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 353 | cert->rsa->exponent = 3; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 354 | cert->hash_len = SHA_DIGEST_SIZE; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 355 | } else if (start_char == 'v') { |
| 356 | int version; |
| 357 | if (fscanf(f, "%d {", &version) != 1) goto exit; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 358 | switch (version) { |
| 359 | case 2: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 360 | cert->key_type = Certificate::RSA; |
| 361 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 362 | cert->rsa->exponent = 65537; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 363 | cert->hash_len = SHA_DIGEST_SIZE; |
| 364 | break; |
| 365 | case 3: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 366 | cert->key_type = Certificate::RSA; |
| 367 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 368 | cert->rsa->exponent = 3; |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 369 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 370 | break; |
| 371 | case 4: |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 372 | cert->key_type = Certificate::RSA; |
| 373 | cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); |
| 374 | cert->rsa->exponent = 65537; |
| 375 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 376 | break; |
| 377 | case 5: |
| 378 | cert->key_type = Certificate::EC; |
| 379 | cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey)); |
Doug Zongker | 30362a6 | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 380 | cert->hash_len = SHA256_DIGEST_SIZE; |
| 381 | break; |
| 382 | default: |
| 383 | goto exit; |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 387 | if (cert->key_type == Certificate::RSA) { |
| 388 | RSAPublicKey* key = cert->rsa; |
| 389 | if (fscanf(f, " %i , 0x%x , { %u", |
| 390 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 391 | goto exit; |
| 392 | } |
| 393 | if (key->len != RSANUMWORDS) { |
| 394 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 395 | goto exit; |
| 396 | } |
| 397 | for (i = 1; i < key->len; ++i) { |
| 398 | if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; |
| 399 | } |
| 400 | if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; |
| 401 | for (i = 1; i < key->len; ++i) { |
| 402 | if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; |
| 403 | } |
| 404 | fscanf(f, " } } "); |
| 405 | |
| 406 | LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len); |
| 407 | } else if (cert->key_type == Certificate::EC) { |
| 408 | ECPublicKey* key = cert->ec; |
| 409 | int key_len; |
| 410 | unsigned int byte; |
| 411 | uint8_t x_bytes[P256_NBYTES]; |
| 412 | uint8_t y_bytes[P256_NBYTES]; |
| 413 | if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit; |
| 414 | if (key_len != P256_NBYTES) { |
| 415 | LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES); |
| 416 | goto exit; |
| 417 | } |
| 418 | x_bytes[P256_NBYTES - 1] = byte; |
| 419 | for (i = P256_NBYTES - 2; i >= 0; --i) { |
| 420 | if (fscanf(f, " , %u", &byte) != 1) goto exit; |
| 421 | x_bytes[i] = byte; |
| 422 | } |
| 423 | if (fscanf(f, " } , { %u", &byte) != 1) goto exit; |
| 424 | y_bytes[P256_NBYTES - 1] = byte; |
| 425 | for (i = P256_NBYTES - 2; i >= 0; --i) { |
| 426 | if (fscanf(f, " , %u", &byte) != 1) goto exit; |
| 427 | y_bytes[i] = byte; |
| 428 | } |
| 429 | fscanf(f, " } } "); |
| 430 | p256_from_bin(x_bytes, &key->x); |
| 431 | p256_from_bin(y_bytes, &key->y); |
| 432 | } else { |
| 433 | LOGE("Unknown key type %d\n", cert->key_type); |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 434 | goto exit; |
| 435 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 436 | |
| 437 | // if the line ends in a comma, this file has more keys. |
| 438 | switch (fgetc(f)) { |
| 439 | case ',': |
| 440 | // more keys to come. |
| 441 | break; |
| 442 | |
| 443 | case EOF: |
| 444 | done = true; |
| 445 | break; |
| 446 | |
| 447 | default: |
| 448 | LOGE("unexpected character between keys\n"); |
| 449 | goto exit; |
| 450 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | fclose(f); |
| 455 | return out; |
| 456 | |
| 457 | exit: |
| 458 | if (f) fclose(f); |
| 459 | free(out); |
| 460 | *numKeys = 0; |
| 461 | return NULL; |
| 462 | } |