blob: 9fcaa0b730cbeb3a49ac1f57aeaf5b3853a57d63 [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"
Jed Estep43291862016-02-03 17:02:09 -080039#include "verifier.h"
40
Tao Bao056e2da2017-03-26 23:25:11 -070041using namespace std::string_literals;
42
Tianjie Xu82566982018-10-10 15:44:17 -070043static void LoadKeyFromFile(const std::string& file_name, Certificate* cert) {
44 std::string testkey_string;
45 ASSERT_TRUE(android::base::ReadFileToString(file_name, &testkey_string));
46 ASSERT_TRUE(LoadCertificateFromBuffer(
47 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), cert));
48}
49
Tianjie Xu0dd96852018-10-15 11:44:14 -070050static void VerifyPackageWithCertificates(const std::string& name,
51 const std::vector<Certificate>& certs) {
Tianjie Xu82566982018-10-10 15:44:17 -070052 std::string package = from_testdata_base(name);
53 MemMapping memmap;
54 if (!memmap.MapFile(package)) {
55 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
56 }
57
Tianjie Xu0dd96852018-10-15 11:44:14 -070058 ASSERT_EQ(VERIFY_SUCCESS, verify_file(memmap.addr, memmap.length, certs));
59}
60
61static void VerifyPackageWithSingleCertificate(const std::string& name, Certificate&& cert) {
Tianjie Xu82566982018-10-10 15:44:17 -070062 std::vector<Certificate> certs;
63 certs.emplace_back(std::move(cert));
Tianjie Xu0dd96852018-10-15 11:44:14 -070064 VerifyPackageWithCertificates(name, certs);
65}
66
67static void BuildCertificateArchive(const std::vector<std::string>& file_names, int fd) {
68 FILE* zip_file_ptr = fdopen(fd, "wb");
69 ZipWriter zip_writer(zip_file_ptr);
70
71 for (const auto& name : file_names) {
72 std::string content;
73 ASSERT_TRUE(android::base::ReadFileToString(name, &content));
74
75 // Makes sure the zip entry name has the correct suffix.
76 std::string entry_name = name;
77 if (!android::base::EndsWith(entry_name, "x509.pem")) {
78 entry_name += "x509.pem";
79 }
80 ASSERT_EQ(0, zip_writer.StartEntry(entry_name.c_str(), ZipWriter::kCompress));
81 ASSERT_EQ(0, zip_writer.WriteBytes(content.data(), content.size()));
82 ASSERT_EQ(0, zip_writer.FinishEntry());
83 }
84
85 ASSERT_EQ(0, zip_writer.Finish());
86 ASSERT_EQ(0, fclose(zip_file_ptr));
Tianjie Xu82566982018-10-10 15:44:17 -070087}
88
89TEST(VerifierTest, LoadCertificateFromBuffer_failure) {
90 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
91 std::string testkey_string;
92 ASSERT_TRUE(
93 android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string));
94 ASSERT_FALSE(LoadCertificateFromBuffer(
95 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert));
96}
97
98TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) {
99 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
100 LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert);
101
102 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
103 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
104 ASSERT_EQ(nullptr, cert.ec);
105
Tianjie Xu0dd96852018-10-15 11:44:14 -0700106 VerifyPackageWithSingleCertificate("otasigned_v1.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700107}
108
109TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) {
110 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
111 LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert);
112
113 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
114 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
115 ASSERT_EQ(nullptr, cert.ec);
116
Tianjie Xu0dd96852018-10-15 11:44:14 -0700117 VerifyPackageWithSingleCertificate("otasigned_v2.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700118}
119
120TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) {
121 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
122 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert);
123
124 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
125 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
126 ASSERT_EQ(nullptr, cert.ec);
127
Tianjie Xu0dd96852018-10-15 11:44:14 -0700128 VerifyPackageWithSingleCertificate("otasigned_v3.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700129}
130
131TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) {
132 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
133 LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert);
134
135 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
136 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
137 ASSERT_EQ(nullptr, cert.ec);
138
Tianjie Xu0dd96852018-10-15 11:44:14 -0700139 VerifyPackageWithSingleCertificate("otasigned_v4.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700140}
141
142TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) {
143 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
144 LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert);
145
146 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
147 ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type);
148 ASSERT_EQ(nullptr, cert.rsa);
149
Tianjie Xu0dd96852018-10-15 11:44:14 -0700150 VerifyPackageWithSingleCertificate("otasigned_v5.zip", std::move(cert));
151}
152
Tianjie Xub5110de2018-10-23 23:31:43 -0700153TEST(VerifierTest, LoadCertificateFromBuffer_check_rsa_keys) {
154 std::unique_ptr<RSA, RSADeleter> rsa(RSA_new());
155 std::unique_ptr<BIGNUM, decltype(&BN_free)> exponent(BN_new(), BN_free);
156 BN_set_word(exponent.get(), 3);
157 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
158 ASSERT_TRUE(CheckRSAKey(rsa));
159
160 // Exponent is expected to be 3 or 65537
161 BN_set_word(exponent.get(), 17);
162 RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
163 ASSERT_FALSE(CheckRSAKey(rsa));
164
165 // Modulus is expected to be 2048.
166 BN_set_word(exponent.get(), 3);
167 RSA_generate_key_ex(rsa.get(), 1024, exponent.get(), nullptr);
168 ASSERT_FALSE(CheckRSAKey(rsa));
169}
170
171TEST(VerifierTest, LoadCertificateFromBuffer_check_ec_keys) {
172 std::unique_ptr<EC_KEY, ECKEYDeleter> ec(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
173 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
174 ASSERT_TRUE(CheckECKey(ec));
175
176 // Expects 256-bit EC key with curve NIST P-256
177 ec.reset(EC_KEY_new_by_curve_name(NID_secp224r1));
178 ASSERT_EQ(1, EC_KEY_generate_key(ec.get()));
179 ASSERT_FALSE(CheckECKey(ec));
180}
181
Tianjie Xu0dd96852018-10-15 11:44:14 -0700182TEST(VerifierTest, LoadKeysFromZipfile_empty_archive) {
183 TemporaryFile otacerts;
184 BuildCertificateArchive({}, otacerts.release());
185 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
186 ASSERT_TRUE(certs.empty());
187}
188
189TEST(VerifierTest, LoadKeysFromZipfile_single_key) {
190 TemporaryFile otacerts;
191 BuildCertificateArchive({ from_testdata_base("testkey_v1.x509.pem") }, otacerts.release());
192 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
193 ASSERT_EQ(1, certs.size());
194
195 VerifyPackageWithCertificates("otasigned_v1.zip", certs);
196}
197
198TEST(VerifierTest, LoadKeysFromZipfile_corrupted_key) {
199 TemporaryFile corrupted_key;
200 std::string content;
201 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v1.x509.pem"), &content));
202 content = "random-contents" + content;
203 ASSERT_TRUE(android::base::WriteStringToFd(content, corrupted_key.release()));
204
205 TemporaryFile otacerts;
206 BuildCertificateArchive({ from_testdata_base("testkey_v2.x509.pem"), corrupted_key.path },
207 otacerts.release());
208 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
209 ASSERT_EQ(0, certs.size());
210}
211
212TEST(VerifierTest, LoadKeysFromZipfile_multiple_key) {
213 TemporaryFile otacerts;
214 BuildCertificateArchive(
215 {
216 from_testdata_base("testkey_v3.x509.pem"),
217 from_testdata_base("testkey_v4.x509.pem"),
218 from_testdata_base("testkey_v5.x509.pem"),
219
220 },
221 otacerts.release());
222 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
223 ASSERT_EQ(3, certs.size());
224
225 VerifyPackageWithCertificates("otasigned_v3.zip", certs);
226 VerifyPackageWithCertificates("otasigned_v4.zip", certs);
227 VerifyPackageWithCertificates("otasigned_v5.zip", certs);
Tianjie Xu82566982018-10-10 15:44:17 -0700228}
229
Jed Estep43291862016-02-03 17:02:09 -0800230class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -0700231 protected:
232 void SetUp() override {
233 std::vector<std::string> args = GetParam();
234 std::string package = from_testdata_base(args[0]);
Tao Baob656a152017-04-18 23:54:29 -0700235 if (!memmap.MapFile(package)) {
Tao Bao7b22c922017-03-16 17:37:38 -0700236 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -0800237 }
238
Tao Bao7b22c922017-03-16 17:37:38 -0700239 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
Tianjie Xucbe93e62018-10-19 17:23:21 -0700240 std::string public_key_file = from_testdata_base("testkey_" + *it + ".x509.pem");
241 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
242 LoadKeyFromFile(public_key_file, &certs.back());
Jed Estep43291862016-02-03 17:02:09 -0800243 }
Tao Bao7b22c922017-03-16 17:37:38 -0700244 }
245
246 MemMapping memmap;
247 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -0800248};
249
250class VerifierSuccessTest : public VerifierTest {
251};
252
253class VerifierFailureTest : public VerifierTest {
254};
255
Tao Bao7e61c6a2017-03-20 16:57:25 -0700256TEST(VerifierTest, BadPackage_AlteredFooter) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700257 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700258 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
259 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700260
261 std::string package;
262 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
263 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
264
265 // Alter the footer.
266 package[package.size() - 5] = '\x05';
267 ASSERT_EQ(VERIFY_FAILURE,
268 verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
269 certs));
270}
271
272TEST(VerifierTest, BadPackage_AlteredContent) {
Tao Bao7e61c6a2017-03-20 16:57:25 -0700273 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700274 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
275 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao7e61c6a2017-03-20 16:57:25 -0700276
277 std::string package;
278 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
279 ASSERT_GT(package.size(), static_cast<size_t>(100));
280
281 // Alter the content.
282 std::string altered1(package);
283 altered1[50] += 1;
284 ASSERT_EQ(VERIFY_FAILURE,
285 verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
286 certs));
287
288 std::string altered2(package);
289 altered2[10] += 1;
290 ASSERT_EQ(VERIFY_FAILURE,
291 verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
292 certs));
293}
294
Tao Bao056e2da2017-03-26 23:25:11 -0700295TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
Tao Bao056e2da2017-03-26 23:25:11 -0700296 std::vector<Certificate> certs;
Tianjie Xucbe93e62018-10-19 17:23:21 -0700297 certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
298 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &certs.back());
Tao Bao056e2da2017-03-26 23:25:11 -0700299
300 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
301 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
302 ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
303 package.size(), certs));
304}
305
Jed Estep43291862016-02-03 17:02:09 -0800306TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700307 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800308}
309
310TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700311 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800312}
313
314INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700315 ::testing::Values(
316 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
317 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
318 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
319 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
320 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800321
322INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700323 ::testing::Values(
324 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
325 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
326 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
327 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
328 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800329
330INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700331 ::testing::Values(
332 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
333 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
334 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
335 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
336 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800337
338INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700339 ::testing::Values(
340 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
341 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
342 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
343 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800344
345INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700346 ::testing::Values(
347 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao7e61c6a2017-03-20 16:57:25 -0700348 std::vector<std::string>({"fake-eocd.zip", "v1"})));