blob: 480f3c96cdcaa8691cd1688fbb210b5c5bbcca72 [file] [log] [blame]
Jed Estep43291862016-02-03 17:02:09 -08001/*
2 * Copyright (C) 2009 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 agree 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 <errno.h>
18#include <fcntl.h>
Jed Estep43291862016-02-03 17:02:09 -080019#include <stdio.h>
20#include <stdlib.h>
Jed Estep43291862016-02-03 17:02:09 -080021#include <sys/stat.h>
Mattias Nissler452df6d2016-04-04 16:17:01 +020022#include <sys/types.h>
Jed Estep43291862016-02-03 17:02:09 -080023
Jed Estep43291862016-02-03 17:02:09 -080024#include <string>
25#include <vector>
26
Tao Bao7b22c922017-03-16 17:37:38 -070027#include <android-base/file.h>
Jed Estep43291862016-02-03 17:02:09 -080028#include <android-base/stringprintf.h>
Tianjie Xu0dd96852018-10-15 11:44:14 -070029#include <android-base/strings.h>
Tao Bao7b22c922017-03-16 17:37:38 -070030#include <android-base/test_utils.h>
Tianjie Xu82566982018-10-10 15:44:17 -070031#include <android-base/unique_fd.h>
Tao Bao17054c02018-05-03 22:41:23 -070032#include <gtest/gtest.h>
Tianjie Xub5110de2018-10-23 23:31:43 -070033#include <openssl/bn.h>
34#include <openssl/ec.h>
35#include <openssl/nid.h>
Tianjie Xu0dd96852018-10-15 11:44:14 -070036#include <ziparchive/zip_writer.h>
Jed Estep43291862016-02-03 17:02:09 -080037
Jed Estepb8a693b2016-03-09 17:51:34 -080038#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070039#include "otautil/sysutil.h"
Jed Estep43291862016-02-03 17:02:09 -080040#include "verifier.h"
41
Tao Bao056e2da2017-03-26 23:25:11 -070042using namespace std::string_literals;
43
Tianjie Xu82566982018-10-10 15:44:17 -070044static void LoadKeyFromFile(const std::string& file_name, Certificate* cert) {
45 std::string testkey_string;
46 ASSERT_TRUE(android::base::ReadFileToString(file_name, &testkey_string));
47 ASSERT_TRUE(LoadCertificateFromBuffer(
48 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), cert));
49}
50
Tianjie Xu0dd96852018-10-15 11:44:14 -070051static void VerifyPackageWithCertificates(const std::string& name,
52 const std::vector<Certificate>& certs) {
Tianjie Xu82566982018-10-10 15:44:17 -070053 std::string package = from_testdata_base(name);
54 MemMapping memmap;
55 if (!memmap.MapFile(package)) {
56 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
57 }
58
Tianjie Xu0dd96852018-10-15 11:44:14 -070059 ASSERT_EQ(VERIFY_SUCCESS, verify_file(memmap.addr, memmap.length, certs));
60}
61
62static void VerifyPackageWithSingleCertificate(const std::string& name, Certificate&& cert) {
Tianjie Xu82566982018-10-10 15:44:17 -070063 std::vector<Certificate> certs;
64 certs.emplace_back(std::move(cert));
Tianjie Xu0dd96852018-10-15 11:44:14 -070065 VerifyPackageWithCertificates(name, certs);
66}
67
68static void BuildCertificateArchive(const std::vector<std::string>& file_names, int fd) {
69 FILE* zip_file_ptr = fdopen(fd, "wb");
70 ZipWriter zip_writer(zip_file_ptr);
71
72 for (const auto& name : file_names) {
73 std::string content;
74 ASSERT_TRUE(android::base::ReadFileToString(name, &content));
75
76 // Makes sure the zip entry name has the correct suffix.
77 std::string entry_name = name;
78 if (!android::base::EndsWith(entry_name, "x509.pem")) {
79 entry_name += "x509.pem";
80 }
81 ASSERT_EQ(0, zip_writer.StartEntry(entry_name.c_str(), ZipWriter::kCompress));
82 ASSERT_EQ(0, zip_writer.WriteBytes(content.data(), content.size()));
83 ASSERT_EQ(0, zip_writer.FinishEntry());
84 }
85
86 ASSERT_EQ(0, zip_writer.Finish());
87 ASSERT_EQ(0, fclose(zip_file_ptr));
Tianjie Xu82566982018-10-10 15:44:17 -070088}
89
90TEST(VerifierTest, LoadCertificateFromBuffer_failure) {
91 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
92 std::string testkey_string;
93 ASSERT_TRUE(
94 android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string));
95 ASSERT_FALSE(LoadCertificateFromBuffer(
96 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert));
97}
98
99TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) {
100 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
101 LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert);
102
103 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
104 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
105 ASSERT_EQ(nullptr, cert.ec);
106
Tianjie Xu0dd96852018-10-15 11:44:14 -0700107 VerifyPackageWithSingleCertificate("otasigned_v1.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700108}
109
110TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) {
111 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
112 LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert);
113
114 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
115 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
116 ASSERT_EQ(nullptr, cert.ec);
117
Tianjie Xu0dd96852018-10-15 11:44:14 -0700118 VerifyPackageWithSingleCertificate("otasigned_v2.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700119}
120
121TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) {
122 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
123 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert);
124
125 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
126 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
127 ASSERT_EQ(nullptr, cert.ec);
128
Tianjie Xu0dd96852018-10-15 11:44:14 -0700129 VerifyPackageWithSingleCertificate("otasigned_v3.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700130}
131
132TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) {
133 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
134 LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert);
135
136 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
137 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
138 ASSERT_EQ(nullptr, cert.ec);
139
Tianjie Xu0dd96852018-10-15 11:44:14 -0700140 VerifyPackageWithSingleCertificate("otasigned_v4.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700141}
142
143TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) {
144 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
145 LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert);
146
147 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
148 ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type);
149 ASSERT_EQ(nullptr, cert.rsa);
150
Tianjie Xu0dd96852018-10-15 11:44:14 -0700151 VerifyPackageWithSingleCertificate("otasigned_v5.zip", std::move(cert));
152}
153
Tianjie Xub5110de2018-10-23 23:31:43 -0700154TEST(VerifierTest, LoadCertificateFromBuffer_check_rsa_keys) {
155 std::unique_ptr<RSA, RSADeleter> rsa(RSA_new());
156 std::unique_ptr<BIGNUM, decltype(&BN_free)> exponent(BN_new(), BN_free);
157 BN_set_word(exponent.get(), 3);
158 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
159 ASSERT_TRUE(CheckRSAKey(rsa));
160
161 // Exponent is expected to be 3 or 65537
162 BN_set_word(exponent.get(), 17);
163 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
164 ASSERT_FALSE(CheckRSAKey(rsa));
165
166 // Modulus is expected to be 2048.
167 BN_set_word(exponent.get(), 3);
168 RSA_generate_key_ex(rsa.get(), 1024, exponent.get(), nullptr);
169 ASSERT_FALSE(CheckRSAKey(rsa));
170}
171
172TEST(VerifierTest, LoadCertificateFromBuffer_check_ec_keys) {
173 std::unique_ptr<EC_KEY, ECKEYDeleter> ec(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
174 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
175 ASSERT_TRUE(CheckECKey(ec));
176
177 // Expects 256-bit EC key with curve NIST P-256
178 ec.reset(EC_KEY_new_by_curve_name(NID_secp224r1));
179 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
180 ASSERT_FALSE(CheckECKey(ec));
181}
182
Tianjie Xu0dd96852018-10-15 11:44:14 -0700183TEST(VerifierTest, LoadKeysFromZipfile_empty_archive) {
184 TemporaryFile otacerts;
185 BuildCertificateArchive({}, otacerts.release());
186 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
187 ASSERT_TRUE(certs.empty());
188}
189
190TEST(VerifierTest, LoadKeysFromZipfile_single_key) {
191 TemporaryFile otacerts;
192 BuildCertificateArchive({ from_testdata_base("testkey_v1.x509.pem") }, otacerts.release());
193 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
194 ASSERT_EQ(1, certs.size());
195
196 VerifyPackageWithCertificates("otasigned_v1.zip", certs);
197}
198
199TEST(VerifierTest, LoadKeysFromZipfile_corrupted_key) {
200 TemporaryFile corrupted_key;
201 std::string content;
202 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v1.x509.pem"), &content));
203 content = "random-contents" + content;
204 ASSERT_TRUE(android::base::WriteStringToFd(content, corrupted_key.release()));
205
206 TemporaryFile otacerts;
207 BuildCertificateArchive({ from_testdata_base("testkey_v2.x509.pem"), corrupted_key.path },
208 otacerts.release());
209 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
210 ASSERT_EQ(0, certs.size());
211}
212
213TEST(VerifierTest, LoadKeysFromZipfile_multiple_key) {
214 TemporaryFile otacerts;
215 BuildCertificateArchive(
216 {
217 from_testdata_base("testkey_v3.x509.pem"),
218 from_testdata_base("testkey_v4.x509.pem"),
219 from_testdata_base("testkey_v5.x509.pem"),
220
221 },
222 otacerts.release());
223 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
224 ASSERT_EQ(3, certs.size());
225
226 VerifyPackageWithCertificates("otasigned_v3.zip", certs);
227 VerifyPackageWithCertificates("otasigned_v4.zip", certs);
228 VerifyPackageWithCertificates("otasigned_v5.zip", certs);
Tianjie Xu82566982018-10-10 15:44:17 -0700229}
230
Jed Estep43291862016-02-03 17:02:09 -0800231class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -0700232 protected:
233 void SetUp() override {
234 std::vector<std::string> args = GetParam();
235 std::string package = from_testdata_base(args[0]);
Tao Baob656a152017-04-18 23:54:29 -0700236 if (!memmap.MapFile(package)) {
Tao Bao7b22c922017-03-16 17:37:38 -0700237 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -0800238 }
239
Tao Bao7b22c922017-03-16 17:37:38 -0700240 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
Tianjie Xucbe93e62018-10-19 17:23:21 -0700241 std::string public_key_file = from_testdata_base("testkey_" + *it + ".x509.pem");
242 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
243 LoadKeyFromFile(public_key_file, &certs.back());
Jed Estep43291862016-02-03 17:02:09 -0800244 }
Tao Bao7b22c922017-03-16 17:37:38 -0700245 }
246
247 MemMapping memmap;
248 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -0800249};
250
251class VerifierSuccessTest : public VerifierTest {
252};
253
254class VerifierFailureTest : public VerifierTest {
255};
256
Tao Bao7e61c6a2017-03-20 16:57:25 -0700257TEST(VerifierTest, BadPackage_AlteredFooter) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700258 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700259 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
260 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700261
262 std::string package;
263 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
264 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
265
266 // Alter the footer.
267 package[package.size() - 5] = '\x05';
268 ASSERT_EQ(VERIFY_FAILURE,
269 verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
270 certs));
271}
272
273TEST(VerifierTest, BadPackage_AlteredContent) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700274 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700275 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
276 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700277
278 std::string package;
279 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
280 ASSERT_GT(package.size(), static_cast<size_t>(100));
281
282 // Alter the content.
283 std::string altered1(package);
284 altered1[50] += 1;
285 ASSERT_EQ(VERIFY_FAILURE,
286 verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
287 certs));
288
289 std::string altered2(package);
290 altered2[10] += 1;
291 ASSERT_EQ(VERIFY_FAILURE,
292 verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
293 certs));
294}
295
Tao Bao056e2da2017-03-26 23:25:11 -0700296TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
Tao Bao056e2da2017-03-26 23:25:11 -0700297 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700298 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
299 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao056e2da2017-03-26 23:25:11 -0700300
301 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
302 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
303 ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
304 package.size(), certs));
305}
306
Jed Estep43291862016-02-03 17:02:09 -0800307TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700308 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800309}
310
311TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700312 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800313}
314
315INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700316 ::testing::Values(
317 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
318 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
319 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
320 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
321 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800322
323INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700324 ::testing::Values(
325 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
326 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
327 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
328 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
329 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800330
331INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700332 ::testing::Values(
333 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
334 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
335 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
336 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
337 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800338
339INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700340 ::testing::Values(
341 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
342 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
343 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
344 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800345
346INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700347 ::testing::Values(
348 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao7e61c6a2017-03-20 16:57:25 -0700349 std::vector<std::string>({"fake-eocd.zip", "v1"})));