blob: c460cbe6fff23780db1e4408febe5d2ea43750d2 [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>
Tao Bao7b22c922017-03-16 17:37:38 -070029#include <android-base/test_utils.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>
Jed Estep43291862016-02-03 17:02:09 -080032
Jed Estepb8a693b2016-03-09 17:51:34 -080033#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070034#include "otautil/sysutil.h"
Jed Estep43291862016-02-03 17:02:09 -080035#include "verifier.h"
36
Tao Bao056e2da2017-03-26 23:25:11 -070037using namespace std::string_literals;
38
Tianjie Xu82566982018-10-10 15:44:17 -070039static void LoadKeyFromFile(const std::string& file_name, Certificate* cert) {
40 std::string testkey_string;
41 ASSERT_TRUE(android::base::ReadFileToString(file_name, &testkey_string));
42 ASSERT_TRUE(LoadCertificateFromBuffer(
43 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), cert));
44}
45
46static void VerifyPackageWithCertificate(const std::string& name, Certificate&& cert) {
47 std::string package = from_testdata_base(name);
48 MemMapping memmap;
49 if (!memmap.MapFile(package)) {
50 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
51 }
52
53 std::vector<Certificate> certs;
54 certs.emplace_back(std::move(cert));
55 ASSERT_EQ(VERIFY_SUCCESS, verify_file(memmap.addr, memmap.length, certs));
56}
57
58TEST(VerifierTest, LoadCertificateFromBuffer_failure) {
59 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
60 std::string testkey_string;
61 ASSERT_TRUE(
62 android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string));
63 ASSERT_FALSE(LoadCertificateFromBuffer(
64 std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert));
65}
66
67TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) {
68 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
69 LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert);
70
71 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
72 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
73 ASSERT_EQ(nullptr, cert.ec);
74
75 VerifyPackageWithCertificate("otasigned_v1.zip", std::move(cert));
76}
77
78TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) {
79 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
80 LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert);
81
82 ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len);
83 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
84 ASSERT_EQ(nullptr, cert.ec);
85
86 VerifyPackageWithCertificate("otasigned_v2.zip", std::move(cert));
87}
88
89TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) {
90 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
91 LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert);
92
93 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
94 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
95 ASSERT_EQ(nullptr, cert.ec);
96
97 VerifyPackageWithCertificate("otasigned_v3.zip", std::move(cert));
98}
99
100TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) {
101 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
102 LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert);
103
104 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
105 ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type);
106 ASSERT_EQ(nullptr, cert.ec);
107
108 VerifyPackageWithCertificate("otasigned_v4.zip", std::move(cert));
109}
110
111TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) {
112 Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
113 LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert);
114
115 ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len);
116 ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type);
117 ASSERT_EQ(nullptr, cert.rsa);
118
119 VerifyPackageWithCertificate("otasigned_v5.zip", std::move(cert));
120}
121
Jed Estep43291862016-02-03 17:02:09 -0800122class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -0700123 protected:
124 void SetUp() override {
125 std::vector<std::string> args = GetParam();
126 std::string package = from_testdata_base(args[0]);
Tao Baob656a152017-04-18 23:54:29 -0700127 if (!memmap.MapFile(package)) {
Tao Bao7b22c922017-03-16 17:37:38 -0700128 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -0800129 }
130
Tao Bao7b22c922017-03-16 17:37:38 -0700131 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
132 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
133 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -0800134 }
Tao Bao7b22c922017-03-16 17:37:38 -0700135 }
136
137 MemMapping memmap;
138 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -0800139};
140
141class VerifierSuccessTest : public VerifierTest {
142};
143
144class VerifierFailureTest : public VerifierTest {
145};
146
Tao Bao3116ce42017-03-16 17:37:38 -0700147TEST(VerifierTest, load_keys_multiple_keys) {
148 std::string testkey_v4;
149 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
150
151 std::string testkey_v3;
152 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
153
154 std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4;
155 TemporaryFile key_file1;
156 ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path));
157 std::vector<Certificate> certs;
158 ASSERT_TRUE(load_keys(key_file1.path, certs));
159 ASSERT_EQ(3U, certs.size());
160}
161
162TEST(VerifierTest, load_keys_invalid_keys) {
163 std::vector<Certificate> certs;
164 ASSERT_FALSE(load_keys("/doesntexist", certs));
165
166 // Empty file.
167 TemporaryFile key_file1;
168 ASSERT_FALSE(load_keys(key_file1.path, certs));
169
170 // Invalid contents.
171 ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path));
172 ASSERT_FALSE(load_keys(key_file1.path, certs));
173
174 std::string testkey_v4;
175 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
176
177 // Invalid key version: "v4 ..." => "v6 ...".
178 std::string invalid_key2(testkey_v4);
179 invalid_key2[1] = '6';
180 TemporaryFile key_file2;
181 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path));
182 ASSERT_FALSE(load_keys(key_file2.path, certs));
183
184 // Invalid key content: inserted extra bytes ",2209831334".
185 std::string invalid_key3(testkey_v4);
186 invalid_key3.insert(invalid_key2.size() - 2, ",2209831334");
187 TemporaryFile key_file3;
188 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path));
189 ASSERT_FALSE(load_keys(key_file3.path, certs));
190
191 // Invalid key: the last key must not end with an extra ','.
192 std::string invalid_key4 = testkey_v4 + ",";
193 TemporaryFile key_file4;
194 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path));
195 ASSERT_FALSE(load_keys(key_file4.path, certs));
196
197 // Invalid key separator.
198 std::string invalid_key5 = testkey_v4 + ";" + testkey_v4;
199 TemporaryFile key_file5;
200 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path));
201 ASSERT_FALSE(load_keys(key_file5.path, certs));
202}
203
Tao Bao7e61c6a2017-03-20 16:57:25 -0700204TEST(VerifierTest, BadPackage_AlteredFooter) {
205 std::string testkey_v3;
206 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
207 TemporaryFile key_file1;
208 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
209 std::vector<Certificate> certs;
210 ASSERT_TRUE(load_keys(key_file1.path, certs));
211
212 std::string package;
213 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
214 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
215
216 // Alter the footer.
217 package[package.size() - 5] = '\x05';
218 ASSERT_EQ(VERIFY_FAILURE,
219 verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
220 certs));
221}
222
223TEST(VerifierTest, BadPackage_AlteredContent) {
224 std::string testkey_v3;
225 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
226 TemporaryFile key_file1;
227 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
228 std::vector<Certificate> certs;
229 ASSERT_TRUE(load_keys(key_file1.path, certs));
230
231 std::string package;
232 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
233 ASSERT_GT(package.size(), static_cast<size_t>(100));
234
235 // Alter the content.
236 std::string altered1(package);
237 altered1[50] += 1;
238 ASSERT_EQ(VERIFY_FAILURE,
239 verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
240 certs));
241
242 std::string altered2(package);
243 altered2[10] += 1;
244 ASSERT_EQ(VERIFY_FAILURE,
245 verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
246 certs));
247}
248
Tao Bao056e2da2017-03-26 23:25:11 -0700249TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
250 std::string testkey_v3;
251 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
252
253 TemporaryFile key_file;
254 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
255 std::vector<Certificate> certs;
256 ASSERT_TRUE(load_keys(key_file.path, certs));
257
258 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
259 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
260 ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
261 package.size(), certs));
262}
263
Jed Estep43291862016-02-03 17:02:09 -0800264TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700265 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800266}
267
268TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700269 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800270}
271
272INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700273 ::testing::Values(
274 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
275 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
276 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
277 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
278 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800279
280INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700281 ::testing::Values(
282 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
283 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
284 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
285 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
286 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800287
288INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700289 ::testing::Values(
290 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
291 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
292 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
293 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
294 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800295
296INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700297 ::testing::Values(
298 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
299 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
300 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
301 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800302
303INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700304 ::testing::Values(
305 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao7e61c6a2017-03-20 16:57:25 -0700306 std::vector<std::string>({"fake-eocd.zip", "v1"})));