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