blob: d110c37e066a88f02c8b162fe770f0cb029d01d8 [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 Xu0dd96852018-10-15 11:44:14 -070033#include <ziparchive/zip_writer.h>
Jed Estep43291862016-02-03 17:02:09 -080034
Jed Estepb8a693b2016-03-09 17:51:34 -080035#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070036#include "otautil/sysutil.h"
Jed Estep43291862016-02-03 17:02:09 -080037#include "verifier.h"
38
Tao Bao056e2da2017-03-26 23:25:11 -070039using namespace std::string_literals;
40
Tianjie Xu82566982018-10-10 15:44:17 -070041static void LoadKeyFromFile(const std::string& file_name, Certificate* cert) {
42 std::string testkey_string;
43 ASSERT_TRUE(android::base::ReadFileToString(file_name, &testkey_string));
44 ASSERT_TRUE(LoadCertificateFromBuffer(
45 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), cert));
46}
47
Tianjie Xu0dd96852018-10-15 11:44:14 -070048static void VerifyPackageWithCertificates(const std::string& name,
49 const std::vector<Certificate>& certs) {
Tianjie Xu82566982018-10-10 15:44:17 -070050 std::string package = from_testdata_base(name);
51 MemMapping memmap;
52 if (!memmap.MapFile(package)) {
53 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
54 }
55
Tianjie Xu0dd96852018-10-15 11:44:14 -070056 ASSERT_EQ(VERIFY_SUCCESS, verify_file(memmap.addr, memmap.length, certs));
57}
58
59static void VerifyPackageWithSingleCertificate(const std::string& name, Certificate&& cert) {
Tianjie Xu82566982018-10-10 15:44:17 -070060 std::vector<Certificate> certs;
61 certs.emplace_back(std::move(cert));
Tianjie Xu0dd96852018-10-15 11:44:14 -070062 VerifyPackageWithCertificates(name, certs);
63}
64
65static void BuildCertificateArchive(const std::vector<std::string>& file_names, int fd) {
66 FILE* zip_file_ptr = fdopen(fd, "wb");
67 ZipWriter zip_writer(zip_file_ptr);
68
69 for (const auto& name : file_names) {
70 std::string content;
71 ASSERT_TRUE(android::base::ReadFileToString(name, &content));
72
73 // Makes sure the zip entry name has the correct suffix.
74 std::string entry_name = name;
75 if (!android::base::EndsWith(entry_name, "x509.pem")) {
76 entry_name += "x509.pem";
77 }
78 ASSERT_EQ(0, zip_writer.StartEntry(entry_name.c_str(), ZipWriter::kCompress));
79 ASSERT_EQ(0, zip_writer.WriteBytes(content.data(), content.size()));
80 ASSERT_EQ(0, zip_writer.FinishEntry());
81 }
82
83 ASSERT_EQ(0, zip_writer.Finish());
84 ASSERT_EQ(0, fclose(zip_file_ptr));
Tianjie Xu82566982018-10-10 15:44:17 -070085}
86
87TEST(VerifierTest, LoadCertificateFromBuffer_failure) {
88 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
89 std::string testkey_string;
90 ASSERT_TRUE(
91 android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string));
92 ASSERT_FALSE(LoadCertificateFromBuffer(
93 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert));
94}
95
96TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) {
97 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
98 LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert);
99
100 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
101 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
102 ASSERT_EQ(nullptr, cert.ec);
103
Tianjie Xu0dd96852018-10-15 11:44:14 -0700104 VerifyPackageWithSingleCertificate("otasigned_v1.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700105}
106
107TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) {
108 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
109 LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert);
110
111 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
112 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
113 ASSERT_EQ(nullptr, cert.ec);
114
Tianjie Xu0dd96852018-10-15 11:44:14 -0700115 VerifyPackageWithSingleCertificate("otasigned_v2.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700116}
117
118TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) {
119 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
120 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert);
121
122 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
123 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
124 ASSERT_EQ(nullptr, cert.ec);
125
Tianjie Xu0dd96852018-10-15 11:44:14 -0700126 VerifyPackageWithSingleCertificate("otasigned_v3.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700127}
128
129TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) {
130 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
131 LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert);
132
133 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
134 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
135 ASSERT_EQ(nullptr, cert.ec);
136
Tianjie Xu0dd96852018-10-15 11:44:14 -0700137 VerifyPackageWithSingleCertificate("otasigned_v4.zip", std::move(cert));
Tianjie Xu82566982018-10-10 15:44:17 -0700138}
139
140TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) {
141 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
142 LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert);
143
144 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
145 ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type);
146 ASSERT_EQ(nullptr, cert.rsa);
147
Tianjie Xu0dd96852018-10-15 11:44:14 -0700148 VerifyPackageWithSingleCertificate("otasigned_v5.zip", std::move(cert));
149}
150
151TEST(VerifierTest, LoadKeysFromZipfile_empty_archive) {
152 TemporaryFile otacerts;
153 BuildCertificateArchive({}, otacerts.release());
154 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
155 ASSERT_TRUE(certs.empty());
156}
157
158TEST(VerifierTest, LoadKeysFromZipfile_single_key) {
159 TemporaryFile otacerts;
160 BuildCertificateArchive({ from_testdata_base("testkey_v1.x509.pem") }, otacerts.release());
161 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
162 ASSERT_EQ(1, certs.size());
163
164 VerifyPackageWithCertificates("otasigned_v1.zip", certs);
165}
166
167TEST(VerifierTest, LoadKeysFromZipfile_corrupted_key) {
168 TemporaryFile corrupted_key;
169 std::string content;
170 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v1.x509.pem"), &content));
171 content = "random-contents" + content;
172 ASSERT_TRUE(android::base::WriteStringToFd(content, corrupted_key.release()));
173
174 TemporaryFile otacerts;
175 BuildCertificateArchive({ from_testdata_base("testkey_v2.x509.pem"), corrupted_key.path },
176 otacerts.release());
177 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
178 ASSERT_EQ(0, certs.size());
179}
180
181TEST(VerifierTest, LoadKeysFromZipfile_multiple_key) {
182 TemporaryFile otacerts;
183 BuildCertificateArchive(
184 {
185 from_testdata_base("testkey_v3.x509.pem"),
186 from_testdata_base("testkey_v4.x509.pem"),
187 from_testdata_base("testkey_v5.x509.pem"),
188
189 },
190 otacerts.release());
191 std::vector<Certificate> certs = LoadKeysFromZipfile(otacerts.path);
192 ASSERT_EQ(3, certs.size());
193
194 VerifyPackageWithCertificates("otasigned_v3.zip", certs);
195 VerifyPackageWithCertificates("otasigned_v4.zip", certs);
196 VerifyPackageWithCertificates("otasigned_v5.zip", certs);
Tianjie Xu82566982018-10-10 15:44:17 -0700197}
198
Jed Estep43291862016-02-03 17:02:09 -0800199class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -0700200 protected:
201 void SetUp() override {
202 std::vector<std::string> args = GetParam();
203 std::string package = from_testdata_base(args[0]);
Tao Baob656a152017-04-18 23:54:29 -0700204 if (!memmap.MapFile(package)) {
Tao Bao7b22c922017-03-16 17:37:38 -0700205 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -0800206 }
207
Tao Bao7b22c922017-03-16 17:37:38 -0700208 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
209 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
210 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -0800211 }
Tao Bao7b22c922017-03-16 17:37:38 -0700212 }
213
214 MemMapping memmap;
215 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -0800216};
217
218class VerifierSuccessTest : public VerifierTest {
219};
220
221class VerifierFailureTest : public VerifierTest {
222};
223
Tao Bao3116ce42017-03-16 17:37:38 -0700224TEST(VerifierTest, load_keys_multiple_keys) {
225 std::string testkey_v4;
226 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
227
228 std::string testkey_v3;
229 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
230
231 std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4;
232 TemporaryFile key_file1;
233 ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path));
234 std::vector<Certificate> certs;
235 ASSERT_TRUE(load_keys(key_file1.path, certs));
236 ASSERT_EQ(3U, certs.size());
237}
238
239TEST(VerifierTest, load_keys_invalid_keys) {
240 std::vector<Certificate> certs;
241 ASSERT_FALSE(load_keys("/doesntexist", certs));
242
243 // Empty file.
244 TemporaryFile key_file1;
245 ASSERT_FALSE(load_keys(key_file1.path, certs));
246
247 // Invalid contents.
248 ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path));
249 ASSERT_FALSE(load_keys(key_file1.path, certs));
250
251 std::string testkey_v4;
252 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
253
254 // Invalid key version: "v4 ..." => "v6 ...".
255 std::string invalid_key2(testkey_v4);
256 invalid_key2[1] = '6';
257 TemporaryFile key_file2;
258 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path));
259 ASSERT_FALSE(load_keys(key_file2.path, certs));
260
261 // Invalid key content: inserted extra bytes ",2209831334".
262 std::string invalid_key3(testkey_v4);
263 invalid_key3.insert(invalid_key2.size() - 2, ",2209831334");
264 TemporaryFile key_file3;
265 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path));
266 ASSERT_FALSE(load_keys(key_file3.path, certs));
267
268 // Invalid key: the last key must not end with an extra ','.
269 std::string invalid_key4 = testkey_v4 + ",";
270 TemporaryFile key_file4;
271 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path));
272 ASSERT_FALSE(load_keys(key_file4.path, certs));
273
274 // Invalid key separator.
275 std::string invalid_key5 = testkey_v4 + ";" + testkey_v4;
276 TemporaryFile key_file5;
277 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path));
278 ASSERT_FALSE(load_keys(key_file5.path, certs));
279}
280
Tao Bao7e61c6a2017-03-20 16:57:25 -0700281TEST(VerifierTest, BadPackage_AlteredFooter) {
282 std::string testkey_v3;
283 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
284 TemporaryFile key_file1;
285 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
286 std::vector<Certificate> certs;
287 ASSERT_TRUE(load_keys(key_file1.path, certs));
288
289 std::string package;
290 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
291 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
292
293 // Alter the footer.
294 package[package.size() - 5] = '\x05';
295 ASSERT_EQ(VERIFY_FAILURE,
296 verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
297 certs));
298}
299
300TEST(VerifierTest, BadPackage_AlteredContent) {
301 std::string testkey_v3;
302 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
303 TemporaryFile key_file1;
304 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
305 std::vector<Certificate> certs;
306 ASSERT_TRUE(load_keys(key_file1.path, certs));
307
308 std::string package;
309 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
310 ASSERT_GT(package.size(), static_cast<size_t>(100));
311
312 // Alter the content.
313 std::string altered1(package);
314 altered1[50] += 1;
315 ASSERT_EQ(VERIFY_FAILURE,
316 verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
317 certs));
318
319 std::string altered2(package);
320 altered2[10] += 1;
321 ASSERT_EQ(VERIFY_FAILURE,
322 verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
323 certs));
324}
325
Tao Bao056e2da2017-03-26 23:25:11 -0700326TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
327 std::string testkey_v3;
328 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
329
330 TemporaryFile key_file;
331 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
332 std::vector<Certificate> certs;
333 ASSERT_TRUE(load_keys(key_file.path, certs));
334
335 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
336 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
337 ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
338 package.size(), certs));
339}
340
Jed Estep43291862016-02-03 17:02:09 -0800341TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700342 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800343}
344
345TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700346 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800347}
348
349INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700350 ::testing::Values(
351 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
352 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
353 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
354 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
355 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800356
357INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700358 ::testing::Values(
359 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
360 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
361 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
362 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
363 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800364
365INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700366 ::testing::Values(
367 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
368 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
369 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
370 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
371 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800372
373INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700374 ::testing::Values(
375 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
376 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
377 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
378 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800379
380INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700381 ::testing::Values(
382 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao7e61c6a2017-03-20 16:57:25 -0700383 std::vector<std::string>({"fake-eocd.zip", "v1"})));