| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2014 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 | // | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/payload_verifier.h" | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 18 |  | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 19 | #include <utility> | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 20 | #include <vector> | 
|  | 21 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 22 | #include <base/logging.h> | 
|  | 23 | #include <openssl/pem.h> | 
|  | 24 |  | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 25 | #include "update_engine/common/constants.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/hash_calculator.h" | 
|  | 27 | #include "update_engine/common/utils.h" | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 28 | #include "update_engine/payload_consumer/certificate_parser_interface.h" | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 29 | #include "update_engine/update_metadata.pb.h" | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | using std::string; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | namespace chromeos_update_engine { | 
|  | 34 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 35 | namespace { | 
|  | 36 |  | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 37 | // The ASN.1 DigestInfo prefix for encoding SHA256 digest. The complete 51-byte | 
|  | 38 | // DigestInfo consists of 19-byte SHA256_DIGEST_INFO_PREFIX and 32-byte SHA256 | 
|  | 39 | // digest. | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 40 | // | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 41 | // SEQUENCE(2+49) { | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 42 | //   SEQUENCE(2+13) { | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 43 | //     OBJECT(2+9) id-sha256 | 
|  | 44 | //     NULL(2+0) | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 45 | //   } | 
|  | 46 | //   OCTET STRING(2+32) <actual signature bytes...> | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 47 | // } | 
|  | 48 | const uint8_t kSHA256DigestInfoPrefix[] = { | 
|  | 49 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, | 
|  | 50 | 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 51 | }; | 
|  | 52 |  | 
|  | 53 | }  // namespace | 
|  | 54 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 55 | std::unique_ptr<PayloadVerifier> PayloadVerifier::CreateInstance( | 
|  | 56 | const std::string& pem_public_key) { | 
|  | 57 | std::unique_ptr<BIO, decltype(&BIO_free)> bp( | 
|  | 58 | BIO_new_mem_buf(pem_public_key.data(), pem_public_key.size()), BIO_free); | 
|  | 59 | if (!bp) { | 
|  | 60 | LOG(ERROR) << "Failed to read " << pem_public_key << " into buffer."; | 
|  | 61 | return nullptr; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | auto pub_key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>( | 
|  | 65 | PEM_read_bio_PUBKEY(bp.get(), nullptr, nullptr, nullptr), EVP_PKEY_free); | 
|  | 66 | if (!pub_key) { | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 67 | LOG(ERROR) << "Failed to parse the public key in: " << pem_public_key; | 
|  | 68 | return nullptr; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> keys; | 
|  | 72 | keys.emplace_back(std::move(pub_key)); | 
|  | 73 | return std::unique_ptr<PayloadVerifier>(new PayloadVerifier(std::move(keys))); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | std::unique_ptr<PayloadVerifier> PayloadVerifier::CreateInstanceFromZipPath( | 
|  | 77 | const std::string& certificate_zip_path) { | 
|  | 78 | auto parser = CreateCertificateParser(); | 
|  | 79 | if (!parser) { | 
|  | 80 | LOG(ERROR) << "Failed to create certificate parser from " | 
|  | 81 | << certificate_zip_path; | 
|  | 82 | return nullptr; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> public_keys; | 
|  | 86 | if (!parser->ReadPublicKeysFromCertificates(certificate_zip_path, | 
|  | 87 | &public_keys) || | 
|  | 88 | public_keys.empty()) { | 
|  | 89 | LOG(ERROR) << "Failed to parse public keys in: " << certificate_zip_path; | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 90 | return nullptr; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | return std::unique_ptr<PayloadVerifier>( | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 94 | new PayloadVerifier(std::move(public_keys))); | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | bool PayloadVerifier::VerifySignature( | 
|  | 98 | const string& signature_proto, const brillo::Blob& sha256_hash_data) const { | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 99 | TEST_AND_RETURN_FALSE(!public_keys_.empty()); | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 100 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 101 | Signatures signatures; | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 102 | LOG(INFO) << "signature blob size = " << signature_proto.size(); | 
|  | 103 | TEST_AND_RETURN_FALSE(signatures.ParseFromString(signature_proto)); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 104 |  | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 105 | if (!signatures.signatures_size()) { | 
|  | 106 | LOG(ERROR) << "No signatures stored in the blob."; | 
|  | 107 | return false; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 108 | } | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 109 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 110 | std::vector<brillo::Blob> tested_hashes; | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 111 | // Tries every signature in the signature blob. | 
|  | 112 | for (int i = 0; i < signatures.signatures_size(); i++) { | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 113 | const Signatures::Signature& signature = signatures.signatures(i); | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 114 | brillo::Blob sig_data; | 
|  | 115 | if (signature.has_unpadded_signature_size()) { | 
|  | 116 | TEST_AND_RETURN_FALSE(signature.unpadded_signature_size() <= | 
|  | 117 | signature.data().size()); | 
|  | 118 | LOG(INFO) << "Truncating the signature to its unpadded size: " | 
|  | 119 | << signature.unpadded_signature_size() << "."; | 
|  | 120 | sig_data.assign( | 
|  | 121 | signature.data().begin(), | 
|  | 122 | signature.data().begin() + signature.unpadded_signature_size()); | 
|  | 123 | } else { | 
|  | 124 | sig_data.assign(signature.data().begin(), signature.data().end()); | 
|  | 125 | } | 
|  | 126 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 127 | brillo::Blob sig_hash_data; | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 128 | if (VerifyRawSignature(sig_data, sha256_hash_data, &sig_hash_data)) { | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 129 | LOG(INFO) << "Verified correct signature " << i + 1 << " out of " | 
|  | 130 | << signatures.signatures_size() << " signatures."; | 
|  | 131 | return true; | 
|  | 132 | } | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 133 | if (!sig_hash_data.empty()) { | 
|  | 134 | tested_hashes.push_back(sig_hash_data); | 
|  | 135 | } | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 136 | } | 
|  | 137 | LOG(ERROR) << "None of the " << signatures.signatures_size() | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 138 | << " signatures is correct. Expected hash before padding:"; | 
|  | 139 | utils::HexDumpVector(sha256_hash_data); | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 140 | LOG(ERROR) << "But found RSA decrypted hashes:"; | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 141 | for (const auto& sig_hash_data : tested_hashes) { | 
|  | 142 | utils::HexDumpVector(sig_hash_data); | 
|  | 143 | } | 
|  | 144 | return false; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 147 | bool PayloadVerifier::VerifyRawSignature( | 
|  | 148 | const brillo::Blob& sig_data, | 
|  | 149 | const brillo::Blob& sha256_hash_data, | 
|  | 150 | brillo::Blob* decrypted_sig_data) const { | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 151 | TEST_AND_RETURN_FALSE(!public_keys_.empty()); | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 152 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 153 | for (const auto& public_key : public_keys_) { | 
|  | 154 | int key_type = EVP_PKEY_id(public_key.get()); | 
|  | 155 | if (key_type == EVP_PKEY_RSA) { | 
|  | 156 | brillo::Blob sig_hash_data; | 
|  | 157 | if (!GetRawHashFromSignature( | 
|  | 158 | sig_data, public_key.get(), &sig_hash_data)) { | 
|  | 159 | LOG(WARNING) | 
|  | 160 | << "Failed to get the raw hash with RSA key. Trying other keys."; | 
|  | 161 | continue; | 
|  | 162 | } | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 163 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 164 | if (decrypted_sig_data != nullptr) { | 
|  | 165 | *decrypted_sig_data = sig_hash_data; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | brillo::Blob padded_hash_data = sha256_hash_data; | 
|  | 169 | TEST_AND_RETURN_FALSE( | 
|  | 170 | PadRSASHA256Hash(&padded_hash_data, sig_hash_data.size())); | 
|  | 171 |  | 
|  | 172 | if (padded_hash_data == sig_hash_data) { | 
|  | 173 | return true; | 
|  | 174 | } | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 177 | if (key_type == EVP_PKEY_EC) { | 
|  | 178 | EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(public_key.get()); | 
|  | 179 | TEST_AND_RETURN_FALSE(ec_key != nullptr); | 
|  | 180 | if (ECDSA_verify(0, | 
|  | 181 | sha256_hash_data.data(), | 
|  | 182 | sha256_hash_data.size(), | 
|  | 183 | sig_data.data(), | 
|  | 184 | sig_data.size(), | 
|  | 185 | ec_key) == 1) { | 
|  | 186 | return true; | 
|  | 187 | } | 
|  | 188 | } | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 189 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "Unsupported key type " << key_type; | 
|  | 191 | return false; | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 192 | } | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 193 | LOG(INFO) << "Failed to verify the signature with " << public_keys_.size() | 
|  | 194 | << " keys."; | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 195 | return false; | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | bool PayloadVerifier::GetRawHashFromSignature( | 
|  | 199 | const brillo::Blob& sig_data, | 
|  | 200 | const EVP_PKEY* public_key, | 
|  | 201 | brillo::Blob* out_hash_data) const { | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 202 | // The code below executes the equivalent of: | 
|  | 203 | // | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 204 | // openssl rsautl -verify -pubin -inkey <(echo pem_public_key) | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 205 | //   -in |sig_data| -out |out_hash_data| | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 206 | RSA* rsa = EVP_PKEY_get0_RSA(public_key); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 207 |  | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 208 | TEST_AND_RETURN_FALSE(rsa != nullptr); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 209 | unsigned int keysize = RSA_size(rsa); | 
|  | 210 | if (sig_data.size() > 2 * keysize) { | 
|  | 211 | LOG(ERROR) << "Signature size is too big for public key size."; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 212 | return false; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | // Decrypts the signature. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 216 | brillo::Blob hash_data(keysize); | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 217 | int decrypt_size = RSA_public_decrypt( | 
|  | 218 | sig_data.size(), sig_data.data(), hash_data.data(), rsa, RSA_NO_PADDING); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 219 | TEST_AND_RETURN_FALSE(decrypt_size > 0 && | 
|  | 220 | decrypt_size <= static_cast<int>(hash_data.size())); | 
|  | 221 | hash_data.resize(decrypt_size); | 
|  | 222 | out_hash_data->swap(hash_data); | 
|  | 223 | return true; | 
|  | 224 | } | 
|  | 225 |  | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 226 | bool PayloadVerifier::PadRSASHA256Hash(brillo::Blob* hash, size_t rsa_size) { | 
|  | 227 | TEST_AND_RETURN_FALSE(hash->size() == kSHA256Size); | 
|  | 228 | TEST_AND_RETURN_FALSE(rsa_size == 256 || rsa_size == 512); | 
|  | 229 |  | 
|  | 230 | // The following is a standard PKCS1-v1_5 padding for SHA256 signatures, as | 
|  | 231 | // defined in RFC3447 section 9.2. It is prepended to the actual signature | 
|  | 232 | // (32 bytes) to form a sequence of 256|512 bytes (2048|4096 bits) that is | 
|  | 233 | // amenable to RSA signing. The padded hash will look as follows: | 
|  | 234 | // | 
|  | 235 | //    0x00 0x01 0xff ... 0xff 0x00  ASN1HEADER  SHA256HASH | 
|  | 236 | //   |-----------205|461----------||----19----||----32----| | 
|  | 237 | size_t padding_string_size = | 
|  | 238 | rsa_size - hash->size() - sizeof(kSHA256DigestInfoPrefix) - 3; | 
|  | 239 | brillo::Blob padded_result = brillo::CombineBlobs({ | 
|  | 240 | {0x00, 0x01}, | 
|  | 241 | brillo::Blob(padding_string_size, 0xff), | 
|  | 242 | {0x00}, | 
|  | 243 | brillo::Blob(kSHA256DigestInfoPrefix, | 
|  | 244 | kSHA256DigestInfoPrefix + sizeof(kSHA256DigestInfoPrefix)), | 
|  | 245 | *hash, | 
|  | 246 | }); | 
|  | 247 |  | 
|  | 248 | *hash = std::move(padded_result); | 
|  | 249 | TEST_AND_RETURN_FALSE(hash->size() == rsa_size); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 250 | return true; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | }  // namespace chromeos_update_engine |