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 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 17 | #include "install/verifier.h" |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 24 | #include <algorithm> |
Tao Bao | d7bf82e | 2017-03-18 09:24:11 -0700 | [diff] [blame] | 25 | #include <functional> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 26 | #include <memory> |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 27 | #include <vector> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 28 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 30 | #include <openssl/bio.h> |
David Benjamin | a86392e | 2016-04-15 20:22:09 -0400 | [diff] [blame] | 31 | #include <openssl/bn.h> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 32 | #include <openssl/ecdsa.h> |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 33 | #include <openssl/evp.h> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 34 | #include <openssl/obj_mac.h> |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 35 | #include <openssl/pem.h> |
| 36 | #include <openssl/rsa.h> |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 37 | #include <ziparchive/zip_archive.h> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 38 | |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 39 | #include "otautil/print_sha1.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 40 | #include "private/asn1_decoder.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 41 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Simple version of PKCS#7 SignedData extraction. This extracts the |
| 44 | * signature OCTET STRING to be used for signature verification. |
| 45 | * |
| 46 | * For full details, see http://www.ietf.org/rfc/rfc3852.txt |
| 47 | * |
| 48 | * The PKCS#7 structure looks like: |
| 49 | * |
| 50 | * SEQUENCE (ContentInfo) |
| 51 | * OID (ContentType) |
| 52 | * [0] (content) |
| 53 | * SEQUENCE (SignedData) |
| 54 | * INTEGER (version CMSVersion) |
| 55 | * SET (DigestAlgorithmIdentifiers) |
| 56 | * SEQUENCE (EncapsulatedContentInfo) |
| 57 | * [0] (CertificateSet OPTIONAL) |
| 58 | * [1] (RevocationInfoChoices OPTIONAL) |
| 59 | * SET (SignerInfos) |
| 60 | * SEQUENCE (SignerInfo) |
| 61 | * INTEGER (CMSVersion) |
| 62 | * SEQUENCE (SignerIdentifier) |
| 63 | * SEQUENCE (DigestAlgorithmIdentifier) |
| 64 | * SEQUENCE (SignatureAlgorithmIdentifier) |
| 65 | * OCTET STRING (SignatureValue) |
| 66 | */ |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 67 | static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len, |
| 68 | std::vector<uint8_t>* sig_der) { |
| 69 | CHECK(sig_der != nullptr); |
| 70 | sig_der->clear(); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 71 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 72 | asn1_context ctx(pkcs7_der, pkcs7_der_len); |
| 73 | |
| 74 | std::unique_ptr<asn1_context> pkcs7_seq(ctx.asn1_sequence_get()); |
| 75 | if (pkcs7_seq == nullptr || !pkcs7_seq->asn1_sequence_next()) { |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 79 | std::unique_ptr<asn1_context> signed_data_app(pkcs7_seq->asn1_constructed_get()); |
| 80 | if (signed_data_app == nullptr) { |
| 81 | return false; |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 82 | } |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 83 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 84 | std::unique_ptr<asn1_context> signed_data_seq(signed_data_app->asn1_sequence_get()); |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 85 | if (signed_data_seq == nullptr || !signed_data_seq->asn1_sequence_next() || |
| 86 | !signed_data_seq->asn1_sequence_next() || !signed_data_seq->asn1_sequence_next() || |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 87 | !signed_data_seq->asn1_constructed_skip_all()) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<asn1_context> sig_set(signed_data_seq->asn1_set_get()); |
| 92 | if (sig_set == nullptr) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | std::unique_ptr<asn1_context> sig_seq(sig_set->asn1_sequence_get()); |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 97 | if (sig_seq == nullptr || !sig_seq->asn1_sequence_next() || !sig_seq->asn1_sequence_next() || |
| 98 | !sig_seq->asn1_sequence_next() || !sig_seq->asn1_sequence_next()) { |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
| 102 | const uint8_t* sig_der_ptr; |
| 103 | size_t sig_der_length; |
| 104 | if (!sig_seq->asn1_octet_string_get(&sig_der_ptr, &sig_der_length)) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | sig_der->resize(sig_der_length); |
| 109 | std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin()); |
| 110 | return true; |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 111 | } |
| 112 | |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 113 | int verify_file(VerifierInterface* package, const std::vector<Certificate>& keys) { |
| 114 | CHECK(package); |
| 115 | package->SetProgress(0.0); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 116 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 117 | // An archive with a whole-file signature will end in six bytes: |
| 118 | // |
| 119 | // (2-byte signature start) $ff $ff (2-byte comment size) |
| 120 | // |
| 121 | // (As far as the ZIP format is concerned, these are part of the archive comment.) We start by |
| 122 | // reading this footer, this tells us how far back from the end we have to start reading to find |
| 123 | // the whole comment. |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 124 | |
| 125 | #define FOOTER_SIZE 6 |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 126 | uint64_t length = package->GetPackageSize(); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 127 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 128 | if (length < FOOTER_SIZE) { |
| 129 | LOG(ERROR) << "not big enough to contain footer"; |
| 130 | return VERIFY_FAILURE; |
| 131 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 132 | |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 133 | uint8_t footer[FOOTER_SIZE]; |
| 134 | if (!package->ReadFullyAtOffset(footer, FOOTER_SIZE, length - FOOTER_SIZE)) { |
| 135 | LOG(ERROR) << "Failed to read footer"; |
| 136 | return VERIFY_FAILURE; |
| 137 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 139 | if (footer[2] != 0xff || footer[3] != 0xff) { |
| 140 | LOG(ERROR) << "footer is wrong"; |
| 141 | return VERIFY_FAILURE; |
| 142 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 143 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 144 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 145 | size_t signature_start = footer[0] + (footer[1] << 8); |
| 146 | LOG(INFO) << "comment is " << comment_size << " bytes; signature is " << signature_start |
| 147 | << " bytes from end"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | |
Tao Bao | 553c7bd | 2017-03-18 07:33:26 -0700 | [diff] [blame] | 149 | if (signature_start > comment_size) { |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 150 | LOG(ERROR) << "signature start: " << signature_start |
| 151 | << " is larger than comment size: " << comment_size; |
Tao Bao | 553c7bd | 2017-03-18 07:33:26 -0700 | [diff] [blame] | 152 | return VERIFY_FAILURE; |
| 153 | } |
Tianjie Xu | 54ea136 | 2016-12-16 16:24:09 -0800 | [diff] [blame] | 154 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 155 | if (signature_start <= FOOTER_SIZE) { |
| 156 | LOG(ERROR) << "Signature start is in the footer"; |
| 157 | return VERIFY_FAILURE; |
| 158 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 159 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 160 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 161 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 162 | // The end-of-central-directory record is 22 bytes plus any 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 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 165 | if (length < eocd_size) { |
| 166 | LOG(ERROR) << "not big enough to contain EOCD"; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 167 | return VERIFY_FAILURE; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Determine how much of the file is covered by the signature. This is everything except the |
| 171 | // signature data and length, which includes all of the EOCD except for the comment length field |
| 172 | // (2 bytes) and the comment data. |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 173 | uint64_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 174 | |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 175 | uint8_t eocd[eocd_size]; |
| 176 | if (!package->ReadFullyAtOffset(eocd, eocd_size, length - eocd_size)) { |
| 177 | LOG(ERROR) << "Failed to read EOCD of " << eocd_size << " bytes"; |
| 178 | return VERIFY_FAILURE; |
| 179 | } |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 180 | |
| 181 | // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06. |
| 182 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 183 | LOG(ERROR) << "signature length doesn't match EOCD marker"; |
| 184 | return VERIFY_FAILURE; |
| 185 | } |
| 186 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 187 | for (size_t i = 4; i < eocd_size - 3; ++i) { |
| 188 | if (eocd[i] == 0x50 && eocd[i + 1] == 0x4b && eocd[i + 2] == 0x05 && eocd[i + 3] == 0x06) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 189 | // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will |
| 190 | // find the later (wrong) one, which could be exploitable. Fail the verification if this |
| 191 | // sequence occurs anywhere after the real one. |
| 192 | LOG(ERROR) << "EOCD marker occurs after start of EOCD"; |
| 193 | return VERIFY_FAILURE; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | bool need_sha1 = false; |
| 198 | bool need_sha256 = false; |
| 199 | for (const auto& key : keys) { |
| 200 | switch (key.hash_len) { |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 201 | case SHA_DIGEST_LENGTH: |
| 202 | need_sha1 = true; |
| 203 | break; |
| 204 | case SHA256_DIGEST_LENGTH: |
| 205 | need_sha256 = true; |
| 206 | break; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | SHA_CTX sha1_ctx; |
| 211 | SHA256_CTX sha256_ctx; |
| 212 | SHA1_Init(&sha1_ctx); |
| 213 | SHA256_Init(&sha256_ctx); |
| 214 | |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 215 | std::vector<HasherUpdateCallback> hashers; |
| 216 | if (need_sha1) { |
| 217 | hashers.emplace_back( |
| 218 | std::bind(&SHA1_Update, &sha1_ctx, std::placeholders::_1, std::placeholders::_2)); |
| 219 | } |
| 220 | if (need_sha256) { |
| 221 | hashers.emplace_back( |
| 222 | std::bind(&SHA256_Update, &sha256_ctx, std::placeholders::_1, std::placeholders::_2)); |
| 223 | } |
| 224 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 225 | double frac = -1.0; |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 226 | uint64_t so_far = 0; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 227 | while (so_far < signed_len) { |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 228 | // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a 1196MiB full OTA and |
| 229 | // 60% for an 89MiB incremental OTA. http://b/28135231. |
| 230 | uint64_t read_size = std::min<uint64_t>(signed_len - so_far, 16 * MiB); |
| 231 | package->UpdateHashAtOffset(hashers, so_far, read_size); |
| 232 | so_far += read_size; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 233 | |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 234 | double f = so_far / static_cast<double>(signed_len); |
| 235 | if (f > frac + 0.02 || read_size == so_far) { |
| 236 | package->SetProgress(f); |
| 237 | frac = f; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
| 242 | SHA1_Final(sha1, &sha1_ctx); |
| 243 | uint8_t sha256[SHA256_DIGEST_LENGTH]; |
| 244 | SHA256_Final(sha256, &sha256_ctx); |
| 245 | |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 246 | const uint8_t* signature = eocd + eocd_size - signature_start; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 247 | size_t signature_size = signature_start - FOOTER_SIZE; |
| 248 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 249 | LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) |
| 250 | << ", length: " << signature_size << "): " << print_hex(signature, signature_size); |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 251 | |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 252 | std::vector<uint8_t> sig_der; |
| 253 | if (!read_pkcs7(signature, signature_size, &sig_der)) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 254 | LOG(ERROR) << "Could not find signature DER block"; |
| 255 | return VERIFY_FAILURE; |
| 256 | } |
| 257 | |
| 258 | // Check to make sure at least one of the keys matches the signature. Since any key can match, |
| 259 | // we need to try each before determining a verification failure has happened. |
| 260 | size_t i = 0; |
| 261 | for (const auto& key : keys) { |
| 262 | const uint8_t* hash; |
| 263 | int hash_nid; |
| 264 | switch (key.hash_len) { |
| 265 | case SHA_DIGEST_LENGTH: |
| 266 | hash = sha1; |
| 267 | hash_nid = NID_sha1; |
| 268 | break; |
| 269 | case SHA256_DIGEST_LENGTH: |
| 270 | hash = sha256; |
| 271 | hash_nid = NID_sha256; |
| 272 | break; |
| 273 | default: |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends |
| 278 | // after the signature itself. |
| 279 | if (key.key_type == Certificate::KEY_TYPE_RSA) { |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 280 | if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der.data(), sig_der.size(), |
| 281 | key.rsa.get())) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 282 | LOG(INFO) << "failed to verify against RSA key " << i; |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | LOG(INFO) << "whole-file signature verified against RSA key " << i; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 287 | return VERIFY_SUCCESS; |
| 288 | } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) { |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 289 | if (!ECDSA_verify(0, hash, key.hash_len, sig_der.data(), sig_der.size(), key.ec.get())) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 290 | LOG(INFO) << "failed to verify against EC key " << i; |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | LOG(INFO) << "whole-file signature verified against EC key " << i; |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 295 | return VERIFY_SUCCESS; |
| 296 | } else { |
| 297 | LOG(INFO) << "Unknown key type " << key.key_type; |
| 298 | } |
| 299 | i++; |
| 300 | } |
| 301 | |
| 302 | if (need_sha1) { |
| 303 | LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH); |
| 304 | } |
| 305 | if (need_sha256) { |
| 306 | LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH); |
| 307 | } |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 308 | LOG(ERROR) << "failed to verify whole-file signature"; |
| 309 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 310 | } |
Doug Zongker | 6c249f7 | 2012-11-02 15:04:05 -0700 | [diff] [blame] | 311 | |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 312 | static std::vector<Certificate> IterateZipEntriesAndSearchForKeys(const ZipArchiveHandle& handle) { |
| 313 | void* cookie; |
Elliott Hughes | 143a03f | 2019-05-07 14:59:09 -0700 | [diff] [blame] | 314 | int32_t iter_status = StartIteration(handle, &cookie, "", "x509.pem"); |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 315 | if (iter_status != 0) { |
| 316 | LOG(ERROR) << "Failed to iterate over entries in the certificate zipfile: " |
| 317 | << ErrorCodeString(iter_status); |
| 318 | return {}; |
| 319 | } |
| 320 | |
| 321 | std::vector<Certificate> result; |
| 322 | |
Elliott Hughes | 39ac1c0 | 2019-06-12 12:20:37 -0700 | [diff] [blame] | 323 | std::string_view name; |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 324 | ZipEntry entry; |
| 325 | while ((iter_status = Next(cookie, &entry, &name)) == 0) { |
| 326 | std::vector<uint8_t> pem_content(entry.uncompressed_length); |
| 327 | if (int32_t extract_status = |
| 328 | ExtractToMemory(handle, &entry, pem_content.data(), pem_content.size()); |
| 329 | extract_status != 0) { |
Elliott Hughes | 88d8001 | 2019-05-22 18:52:29 -0700 | [diff] [blame] | 330 | LOG(ERROR) << "Failed to extract " << name; |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 331 | return {}; |
| 332 | } |
| 333 | |
| 334 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 335 | // Aborts the parsing if we fail to load one of the key file. |
| 336 | if (!LoadCertificateFromBuffer(pem_content, &cert)) { |
Elliott Hughes | 88d8001 | 2019-05-22 18:52:29 -0700 | [diff] [blame] | 337 | LOG(ERROR) << "Failed to load keys from " << name; |
Tianjie Xu | 0dd9685 | 2018-10-15 11:44:14 -0700 | [diff] [blame] | 338 | return {}; |
| 339 | } |
| 340 | |
| 341 | result.emplace_back(std::move(cert)); |
| 342 | } |
| 343 | |
| 344 | if (iter_status != -1) { |
| 345 | LOG(ERROR) << "Error while iterating over zip entries: " << ErrorCodeString(iter_status); |
| 346 | return {}; |
| 347 | } |
| 348 | |
| 349 | return result; |
| 350 | } |
| 351 | |
| 352 | std::vector<Certificate> LoadKeysFromZipfile(const std::string& zip_name) { |
| 353 | ZipArchiveHandle handle; |
| 354 | if (int32_t open_status = OpenArchive(zip_name.c_str(), &handle); open_status != 0) { |
| 355 | LOG(ERROR) << "Failed to open " << zip_name << ": " << ErrorCodeString(open_status); |
| 356 | return {}; |
| 357 | } |
| 358 | |
| 359 | std::vector<Certificate> result = IterateZipEntriesAndSearchForKeys(handle); |
| 360 | CloseArchive(handle); |
| 361 | return result; |
| 362 | } |
| 363 | |
Tianjie Xu | b5110de | 2018-10-23 23:31:43 -0700 | [diff] [blame] | 364 | bool CheckRSAKey(const std::unique_ptr<RSA, RSADeleter>& rsa) { |
| 365 | if (!rsa) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | const BIGNUM* out_n; |
| 370 | const BIGNUM* out_e; |
| 371 | RSA_get0_key(rsa.get(), &out_n, &out_e, nullptr /* private exponent */); |
| 372 | auto modulus_bits = BN_num_bits(out_n); |
xunchang | 908ad77 | 2019-03-26 09:54:34 -0700 | [diff] [blame] | 373 | if (modulus_bits != 2048 && modulus_bits != 4096) { |
| 374 | LOG(ERROR) << "Modulus should be 2048 or 4096 bits long, actual: " << modulus_bits; |
Tianjie Xu | b5110de | 2018-10-23 23:31:43 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
| 378 | BN_ULONG exponent = BN_get_word(out_e); |
| 379 | if (exponent != 3 && exponent != 65537) { |
| 380 | LOG(ERROR) << "Public exponent should be 3 or 65537, actual: " << exponent; |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | bool CheckECKey(const std::unique_ptr<EC_KEY, ECKEYDeleter>& ec_key) { |
| 388 | if (!ec_key) { |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key.get()); |
| 393 | if (!ec_group) { |
| 394 | LOG(ERROR) << "Failed to get the ec_group from the ec_key"; |
| 395 | return false; |
| 396 | } |
| 397 | auto degree = EC_GROUP_get_degree(ec_group); |
| 398 | if (degree != 256) { |
| 399 | LOG(ERROR) << "Field size of the ec key should be 256 bits long, actual: " << degree; |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | return true; |
| 404 | } |
| 405 | |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 406 | bool LoadCertificateFromBuffer(const std::vector<uint8_t>& pem_content, Certificate* cert) { |
| 407 | std::unique_ptr<BIO, decltype(&BIO_free)> content( |
| 408 | BIO_new_mem_buf(pem_content.data(), pem_content.size()), BIO_free); |
| 409 | |
| 410 | std::unique_ptr<X509, decltype(&X509_free)> x509( |
| 411 | PEM_read_bio_X509(content.get(), nullptr, nullptr, nullptr), X509_free); |
| 412 | if (!x509) { |
| 413 | LOG(ERROR) << "Failed to read x509 certificate"; |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | int nid = X509_get_signature_nid(x509.get()); |
| 418 | switch (nid) { |
| 419 | // SignApk has historically accepted md5WithRSA certificates, but treated them as |
| 420 | // sha1WithRSA anyway. Continue to do so for backwards compatibility. |
| 421 | case NID_md5WithRSA: |
| 422 | case NID_md5WithRSAEncryption: |
| 423 | case NID_sha1WithRSA: |
| 424 | case NID_sha1WithRSAEncryption: |
| 425 | cert->hash_len = SHA_DIGEST_LENGTH; |
| 426 | break; |
| 427 | case NID_sha256WithRSAEncryption: |
| 428 | case NID_ecdsa_with_SHA256: |
| 429 | cert->hash_len = SHA256_DIGEST_LENGTH; |
| 430 | break; |
| 431 | default: |
| 432 | LOG(ERROR) << "Unrecognized signature nid " << OBJ_nid2ln(nid); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> public_key(X509_get_pubkey(x509.get()), |
| 437 | EVP_PKEY_free); |
| 438 | if (!public_key) { |
| 439 | LOG(ERROR) << "Failed to extract the public key from x509 certificate"; |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | int key_type = EVP_PKEY_id(public_key.get()); |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 444 | if (key_type == EVP_PKEY_RSA) { |
| 445 | cert->key_type = Certificate::KEY_TYPE_RSA; |
| 446 | cert->ec.reset(); |
| 447 | cert->rsa.reset(EVP_PKEY_get1_RSA(public_key.get())); |
Tianjie Xu | b5110de | 2018-10-23 23:31:43 -0700 | [diff] [blame] | 448 | if (!cert->rsa || !CheckRSAKey(cert->rsa)) { |
| 449 | LOG(ERROR) << "Failed to validate the rsa key info from public key"; |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | } else if (key_type == EVP_PKEY_EC) { |
| 453 | cert->key_type = Certificate::KEY_TYPE_EC; |
| 454 | cert->rsa.reset(); |
| 455 | cert->ec.reset(EVP_PKEY_get1_EC_KEY(public_key.get())); |
Tianjie Xu | b5110de | 2018-10-23 23:31:43 -0700 | [diff] [blame] | 456 | if (!cert->ec || !CheckECKey(cert->ec)) { |
| 457 | LOG(ERROR) << "Failed to validate the ec key info from the public key"; |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | } else { |
| 461 | LOG(ERROR) << "Unrecognized public key type " << OBJ_nid2ln(key_type); |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | return true; |
| 466 | } |