Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2019 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 | |
| 17 | #include "update_engine/payload_consumer/certificate_parser_android.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <utility> |
| 21 | |
| 22 | #include <base/logging.h> |
| 23 | #include <openssl/bio.h> |
| 24 | #include <openssl/pem.h> |
| 25 | #include <ziparchive/zip_archive.h> |
| 26 | |
| 27 | #include "update_engine/payload_consumer/certificate_parser_interface.h" |
| 28 | |
| 29 | namespace { |
| 30 | bool IterateZipEntriesAndSearchForKeys( |
| 31 | const ZipArchiveHandle& handle, std::vector<std::vector<uint8_t>>* result) { |
| 32 | void* cookie; |
| 33 | int32_t iter_status = StartIteration(handle, &cookie, "", "x509.pem"); |
| 34 | if (iter_status != 0) { |
| 35 | LOG(ERROR) << "Failed to iterate over entries in the certificate zipfile: " |
| 36 | << ErrorCodeString(iter_status); |
| 37 | return false; |
| 38 | } |
| 39 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 40 | |
| 41 | std::vector<std::vector<uint8_t>> pem_keys; |
| 42 | std::string_view name; |
| 43 | ZipEntry entry; |
| 44 | while ((iter_status = Next(cookie, &entry, &name)) == 0) { |
| 45 | std::vector<uint8_t> pem_content(entry.uncompressed_length); |
| 46 | if (int32_t extract_status = ExtractToMemory( |
| 47 | handle, &entry, pem_content.data(), pem_content.size()); |
| 48 | extract_status != 0) { |
| 49 | LOG(ERROR) << "Failed to extract " << name << ": " |
| 50 | << ErrorCodeString(extract_status); |
| 51 | return false; |
| 52 | } |
| 53 | pem_keys.push_back(pem_content); |
| 54 | } |
| 55 | |
| 56 | if (iter_status != -1) { |
| 57 | LOG(ERROR) << "Error while iterating over zip entries: " |
| 58 | << ErrorCodeString(iter_status); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | *result = std::move(pem_keys); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | namespace chromeos_update_engine { |
| 69 | bool CertificateParserAndroid::ReadPublicKeysFromCertificates( |
| 70 | const std::string& path, |
| 71 | std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>>* |
| 72 | out_public_keys) { |
| 73 | out_public_keys->clear(); |
| 74 | |
wuhaitao3 | f3e8a2c | 2023-08-24 17:02:23 +0800 | [diff] [blame] | 75 | ZipArchiveHandle raw_handle; |
| 76 | int32_t open_status = OpenArchive(path.c_str(), &raw_handle); |
| 77 | std::unique_ptr<ZipArchive, decltype(&CloseArchive)> handle(raw_handle, CloseArchive); |
| 78 | if (open_status != 0) { |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 79 | LOG(ERROR) << "Failed to open " << path << ": " |
| 80 | << ErrorCodeString(open_status); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | std::vector<std::vector<uint8_t>> pem_certs; |
wuhaitao3 | f3e8a2c | 2023-08-24 17:02:23 +0800 | [diff] [blame] | 85 | if (!IterateZipEntriesAndSearchForKeys(handle.get(), &pem_certs)) { |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 86 | return false; |
| 87 | } |
wuhaitao3 | f3e8a2c | 2023-08-24 17:02:23 +0800 | [diff] [blame] | 88 | handle.reset(); |
| 89 | raw_handle = nullptr; |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 90 | // Convert the certificates into public keys. Stop and return false if we |
| 91 | // encounter an error. |
| 92 | std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> result; |
| 93 | for (const auto& cert : pem_certs) { |
| 94 | std::unique_ptr<BIO, decltype(&BIO_free)> input( |
| 95 | BIO_new_mem_buf(cert.data(), cert.size()), BIO_free); |
| 96 | |
| 97 | std::unique_ptr<X509, decltype(&X509_free)> x509( |
| 98 | PEM_read_bio_X509(input.get(), nullptr, nullptr, nullptr), X509_free); |
| 99 | if (!x509) { |
| 100 | LOG(ERROR) << "Failed to read x509 certificate"; |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> public_key( |
| 105 | X509_get_pubkey(x509.get()), EVP_PKEY_free); |
| 106 | if (!public_key) { |
| 107 | LOG(ERROR) << "Failed to extract the public key from x509 certificate"; |
| 108 | return false; |
| 109 | } |
| 110 | result.push_back(std::move(public_key)); |
| 111 | } |
| 112 | |
| 113 | *out_public_keys = std::move(result); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | std::unique_ptr<CertificateParserInterface> CreateCertificateParser() { |
| 118 | return std::make_unique<CertificateParserAndroid>(); |
| 119 | } |
| 120 | |
| 121 | } // namespace chromeos_update_engine |