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