blob: c904cd03874d38aa833ca723410fed6a56c02714 [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>
Tianjie Xu82566982018-10-10 15:44:17 -070030#include <android-base/unique_fd.h>
Tao Bao17054c02018-05-03 22:41:23 -070031#include <gtest/gtest.h>
Tianjie Xub5110de2018-10-23 23:31:43 -070032#include <openssl/bn.h>
33#include <openssl/ec.h>
34#include <openssl/nid.h>
Tianjie Xu0dd96852018-10-15 11:44:14 -070035#include <ziparchive/zip_writer.h>
Jed Estep43291862016-02-03 17:02:09 -080036
Jed Estepb8a693b2016-03-09 17:51:34 -080037#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070038#include "otautil/sysutil.h"
xunchangf07ed2e2019-02-25 14:14:01 -080039#include "package.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
xunchangf07ed2e2019-02-25 14:14:01 -080051static void VerifyFile(const std::string& content, const std::vector<Certificate>& keys,
52 int expected) {
53 auto package =
54 Package::CreateMemoryPackage(std::vector<uint8_t>(content.begin(), content.end()), nullptr);
55 ASSERT_NE(nullptr, package);
56
57 ASSERT_EQ(expected, verify_file(package.get(), keys));
58}
59
Tianjie Xu0dd96852018-10-15 11:44:14 -070060static void VerifyPackageWithCertificates(const std::string& name,
61 const std::vector<Certificate>& certs) {
xunchangf07ed2e2019-02-25 14:14:01 -080062 std::string path = from_testdata_base(name);
63 auto package = Package::CreateMemoryPackage(path, nullptr);
64 ASSERT_NE(nullptr, package);
Tianjie Xu82566982018-10-10 15:44:17 -070065
xunchangf07ed2e2019-02-25 14:14:01 -080066 ASSERT_EQ(VERIFY_SUCCESS, verify_file(package.get(), certs));
Tianjie Xu0dd96852018-10-15 11:44:14 -070067}
68
69static void VerifyPackageWithSingleCertificate(const std::string& name, Certificate&& cert) {
Tianjie Xu82566982018-10-10 15:44:17 -070070 std::vector<Certificate> certs;
71 certs.emplace_back(std::move(cert));
Tianjie Xu0dd96852018-10-15 11:44:14 -070072 VerifyPackageWithCertificates(name, certs);
73}
74
75static void BuildCertificateArchive(const std::vector<std::string>& file_names, int fd) {
76 FILE* zip_file_ptr = fdopen(fd, "wb");
77 ZipWriter zip_writer(zip_file_ptr);
78
79 for (const auto& name : file_names) {
80 std::string content;
81 ASSERT_TRUE(android::base::ReadFileToString(name, &content));
82
83 // Makes sure the zip entry name has the correct suffix.
84 std::string entry_name = name;
85 if (!android::base::EndsWith(entry_name, "x509.pem")) {
86 entry_name += "x509.pem";
87 }
88 ASSERT_EQ(0, zip_writer.StartEntry(entry_name.c_str(), ZipWriter::kCompress));
89 ASSERT_EQ(0, zip_writer.WriteBytes(content.data(), content.size()));
90 ASSERT_EQ(0, zip_writer.FinishEntry());
91 }
92
93 ASSERT_EQ(0, zip_writer.Finish());
94 ASSERT_EQ(0, fclose(zip_file_ptr));
Tianjie Xu82566982018-10-10 15:44:17 -070095}
96
97TEST(VerifierTest, LoadCertificateFromBuffer_failure) {
98 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
99 std::string testkey_string;
100 ASSERT_TRUE(
101 android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string));
102 ASSERT_FALSE(LoadCertificateFromBuffer(
103 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert));
104}
105
106TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) {
107 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
108 LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert);
109
110 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
111 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
112 ASSERT_EQ(nullptr, cert.ec);
113
Tianjie Xu0dd96852018-10-15 11:44:14 -0700114 VerifyPackageWithSingleCertificate("otasigned_v1.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700115}
116
117TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) {
118 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
119 LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert);
120
121 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
122 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
123 ASSERT_EQ(nullptr, cert.ec);
124
Tianjie Xu0dd96852018-10-15 11:44:14 -0700125 VerifyPackageWithSingleCertificate("otasigned_v2.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700126}
127
128TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) {
129 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
130 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert);
131
132 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
133 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
134 ASSERT_EQ(nullptr, cert.ec);
135
Tianjie Xu0dd96852018-10-15 11:44:14 -0700136 VerifyPackageWithSingleCertificate("otasigned_v3.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700137}
138
139TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) {
140 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
141 LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert);
142
143 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
144 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
145 ASSERT_EQ(nullptr, cert.ec);
146
Tianjie Xu0dd96852018-10-15 11:44:14 -0700147 VerifyPackageWithSingleCertificate("otasigned_v4.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700148}
149
150TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) {
151 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
152 LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert);
153
154 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
155 ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type);
156 ASSERT_EQ(nullptr, cert.rsa);
157
Tianjie Xu0dd96852018-10-15 11:44:14 -0700158 VerifyPackageWithSingleCertificate("otasigned_v5.zip", std::move(cert));
159}
160
Tianjie Xub5110de2018-10-23 23:31:43 -0700161TEST(VerifierTest, LoadCertificateFromBuffer_check_rsa_keys) {
162 std::unique_ptr<RSA, RSADeleter> rsa(RSA_new());
163 std::unique_ptr<BIGNUM, decltype(&BN_free)> exponent(BN_new(), BN_free);
164 BN_set_word(exponent.get(), 3);
165 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
166 ASSERT_TRUE(CheckRSAKey(rsa));
167
168 // Exponent is expected to be 3 or 65537
169 BN_set_word(exponent.get(), 17);
170 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
171 ASSERT_FALSE(CheckRSAKey(rsa));
172
173 // Modulus is expected to be 2048.
174 BN_set_word(exponent.get(), 3);
175 RSA_generate_key_ex(rsa.get(), 1024, exponent.get(), nullptr);
176 ASSERT_FALSE(CheckRSAKey(rsa));
177}
178
179TEST(VerifierTest, LoadCertificateFromBuffer_check_ec_keys) {
180 std::unique_ptr<EC_KEY, ECKEYDeleter> ec(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
181 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
182 ASSERT_TRUE(CheckECKey(ec));
183
184 // Expects 256-bit EC key with curve NIST P-256
185 ec.reset(EC_KEY_new_by_curve_name(NID_secp224r1));
186 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
187 ASSERT_FALSE(CheckECKey(ec));
188}
189
Tianjie Xu0dd96852018-10-15 11:44:14 -0700190TEST(VerifierTest, LoadKeysFromZipfile_empty_archive) {
191 TemporaryFile otacerts;
192 BuildCertificateArchive({}, otacerts.release());
193 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
194 ASSERT_TRUE(certs.empty());
195}
196
197TEST(VerifierTest, LoadKeysFromZipfile_single_key) {
198 TemporaryFile otacerts;
199 BuildCertificateArchive({ from_testdata_base("testkey_v1.x509.pem") }, otacerts.release());
200 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
201 ASSERT_EQ(1, certs.size());
202
203 VerifyPackageWithCertificates("otasigned_v1.zip", certs);
204}
205
206TEST(VerifierTest, LoadKeysFromZipfile_corrupted_key) {
207 TemporaryFile corrupted_key;
208 std::string content;
209 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v1.x509.pem"), &content));
210 content = "random-contents" + content;
211 ASSERT_TRUE(android::base::WriteStringToFd(content, corrupted_key.release()));
212
213 TemporaryFile otacerts;
214 BuildCertificateArchive({ from_testdata_base("testkey_v2.x509.pem"), corrupted_key.path },
215 otacerts.release());
216 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
217 ASSERT_EQ(0, certs.size());
218}
219
220TEST(VerifierTest, LoadKeysFromZipfile_multiple_key) {
221 TemporaryFile otacerts;
222 BuildCertificateArchive(
223 {
224 from_testdata_base("testkey_v3.x509.pem"),
225 from_testdata_base("testkey_v4.x509.pem"),
226 from_testdata_base("testkey_v5.x509.pem"),
227
228 },
229 otacerts.release());
230 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
231 ASSERT_EQ(3, certs.size());
232
233 VerifyPackageWithCertificates("otasigned_v3.zip", certs);
234 VerifyPackageWithCertificates("otasigned_v4.zip", certs);
235 VerifyPackageWithCertificates("otasigned_v5.zip", certs);
Tianjie Xu82566982018-10-10 15:44:17 -0700236}
237
Jed Estep43291862016-02-03 17:02:09 -0800238class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -0700239 protected:
240 void SetUp() override {
241 std::vector<std::string> args = GetParam();
xunchangf07ed2e2019-02-25 14:14:01 -0800242 std::string path = from_testdata_base(args[0]);
xunchang37304f32019-03-12 12:40:14 -0700243 memory_package_ = Package::CreateMemoryPackage(path, nullptr);
244 ASSERT_NE(nullptr, memory_package_);
245 file_package_ = Package::CreateFilePackage(path, nullptr);
246 ASSERT_NE(nullptr, file_package_);
Jed Estep43291862016-02-03 17:02:09 -0800247
Tao Bao7b22c922017-03-16 17:37:38 -0700248 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
Tianjie Xucbe93e62018-10-19 17:23:21 -0700249 std::string public_key_file = from_testdata_base("testkey_" + *it + ".x509.pem");
xunchangf07ed2e2019-02-25 14:14:01 -0800250 certs_.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
251 LoadKeyFromFile(public_key_file, &certs_.back());
Jed Estep43291862016-02-03 17:02:09 -0800252 }
Tao Bao7b22c922017-03-16 17:37:38 -0700253 }
254
xunchang37304f32019-03-12 12:40:14 -0700255 std::unique_ptr<Package> memory_package_;
256 std::unique_ptr<Package> file_package_;
xunchangf07ed2e2019-02-25 14:14:01 -0800257 std::vector<Certificate> certs_;
Jed Estep43291862016-02-03 17:02:09 -0800258};
259
260class VerifierSuccessTest : public VerifierTest {
261};
262
263class VerifierFailureTest : public VerifierTest {
264};
265
Tao Bao7e61c6a2017-03-20 16:57:25 -0700266TEST(VerifierTest, BadPackage_AlteredFooter) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700267 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700268 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
269 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700270
271 std::string package;
272 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
273 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
274
275 // Alter the footer.
276 package[package.size() - 5] = '\x05';
xunchangf07ed2e2019-02-25 14:14:01 -0800277 VerifyFile(package, certs, VERIFY_FAILURE);
Tao Bao7e61c6a2017-03-20 16:57:25 -0700278}
279
280TEST(VerifierTest, BadPackage_AlteredContent) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700281 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700282 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
283 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700284
285 std::string package;
286 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
287 ASSERT_GT(package.size(), static_cast<size_t>(100));
288
289 // Alter the content.
290 std::string altered1(package);
291 altered1[50] += 1;
xunchangf07ed2e2019-02-25 14:14:01 -0800292 VerifyFile(altered1, certs, VERIFY_FAILURE);
Tao Bao7e61c6a2017-03-20 16:57:25 -0700293
294 std::string altered2(package);
295 altered2[10] += 1;
xunchangf07ed2e2019-02-25 14:14:01 -0800296 VerifyFile(altered2, certs, VERIFY_FAILURE);
Tao Bao7e61c6a2017-03-20 16:57:25 -0700297}
298
Tao Bao056e2da2017-03-26 23:25:11 -0700299TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
Tao Bao056e2da2017-03-26 23:25:11 -0700300 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700301 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
302 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao056e2da2017-03-26 23:25:11 -0700303
304 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
305 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
xunchangf07ed2e2019-02-25 14:14:01 -0800306 VerifyFile(package, certs, VERIFY_FAILURE);
Tao Bao056e2da2017-03-26 23:25:11 -0700307}
308
Jed Estep43291862016-02-03 17:02:09 -0800309TEST_P(VerifierSuccessTest, VerifySucceed) {
xunchang37304f32019-03-12 12:40:14 -0700310 ASSERT_EQ(VERIFY_SUCCESS, verify_file(memory_package_.get(), certs_));
311 ASSERT_EQ(VERIFY_SUCCESS, verify_file(file_package_.get(), certs_));
Jed Estep43291862016-02-03 17:02:09 -0800312}
313
314TEST_P(VerifierFailureTest, VerifyFailure) {
xunchang37304f32019-03-12 12:40:14 -0700315 ASSERT_EQ(VERIFY_FAILURE, verify_file(memory_package_.get(), certs_));
316 ASSERT_EQ(VERIFY_FAILURE, verify_file(file_package_.get(), certs_));
Jed Estep43291862016-02-03 17:02:09 -0800317}
318
319INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700320 ::testing::Values(
321 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
322 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
323 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
324 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
325 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800326
327INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700328 ::testing::Values(
329 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
330 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
331 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
332 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
333 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800334
335INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700336 ::testing::Values(
337 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
338 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
339 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
340 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
341 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800342
343INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700344 ::testing::Values(
345 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
346 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
347 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
348 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800349
350INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700351 ::testing::Values(
352 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao7e61c6a2017-03-20 16:57:25 -0700353 std::vector<std::string>({"fake-eocd.zip", "v1"})));