blob: 61ceadc9c02bf6922ddc0bdc01e33fabee4ccfc3 [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>
19#include <gtest/gtest.h>
20#include <stdio.h>
21#include <stdlib.h>
Jed Estep43291862016-02-03 17:02:09 -080022#include <sys/stat.h>
Mattias Nissler452df6d2016-04-04 16:17:01 +020023#include <sys/types.h>
Jed Estep43291862016-02-03 17:02:09 -080024
Jed Estep43291862016-02-03 17:02:09 -080025#include <string>
26#include <vector>
27
Tao Bao7b22c922017-03-16 17:37:38 -070028#include <android-base/file.h>
Jed Estep43291862016-02-03 17:02:09 -080029#include <android-base/stringprintf.h>
Tao Bao7b22c922017-03-16 17:37:38 -070030#include <android-base/test_utils.h>
Jed Estep43291862016-02-03 17:02:09 -080031
Jed Estepb8a693b2016-03-09 17:51:34 -080032#include "common/test_constants.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070033#include "otautil/SysUtil.h"
Jed Estep43291862016-02-03 17:02:09 -080034#include "verifier.h"
35
Tao Bao056e2da2017-03-26 23:25:11 -070036using namespace std::string_literals;
37
Jed Estep43291862016-02-03 17:02:09 -080038class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao7b22c922017-03-16 17:37:38 -070039 protected:
40 void SetUp() override {
41 std::vector<std::string> args = GetParam();
42 std::string package = from_testdata_base(args[0]);
43 if (sysMapFile(package.c_str(), &memmap) != 0) {
44 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -080045 }
46
Tao Bao7b22c922017-03-16 17:37:38 -070047 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
48 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
49 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -080050 }
Tao Bao7b22c922017-03-16 17:37:38 -070051 }
52
53 MemMapping memmap;
54 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -080055};
56
57class VerifierSuccessTest : public VerifierTest {
58};
59
60class VerifierFailureTest : public VerifierTest {
61};
62
Tao Bao3116ce42017-03-16 17:37:38 -070063TEST(VerifierTest, load_keys_multiple_keys) {
64 std::string testkey_v4;
65 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
66
67 std::string testkey_v3;
68 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
69
70 std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4;
71 TemporaryFile key_file1;
72 ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path));
73 std::vector<Certificate> certs;
74 ASSERT_TRUE(load_keys(key_file1.path, certs));
75 ASSERT_EQ(3U, certs.size());
76}
77
78TEST(VerifierTest, load_keys_invalid_keys) {
79 std::vector<Certificate> certs;
80 ASSERT_FALSE(load_keys("/doesntexist", certs));
81
82 // Empty file.
83 TemporaryFile key_file1;
84 ASSERT_FALSE(load_keys(key_file1.path, certs));
85
86 // Invalid contents.
87 ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path));
88 ASSERT_FALSE(load_keys(key_file1.path, certs));
89
90 std::string testkey_v4;
91 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
92
93 // Invalid key version: "v4 ..." => "v6 ...".
94 std::string invalid_key2(testkey_v4);
95 invalid_key2[1] = '6';
96 TemporaryFile key_file2;
97 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path));
98 ASSERT_FALSE(load_keys(key_file2.path, certs));
99
100 // Invalid key content: inserted extra bytes ",2209831334".
101 std::string invalid_key3(testkey_v4);
102 invalid_key3.insert(invalid_key2.size() - 2, ",2209831334");
103 TemporaryFile key_file3;
104 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path));
105 ASSERT_FALSE(load_keys(key_file3.path, certs));
106
107 // Invalid key: the last key must not end with an extra ','.
108 std::string invalid_key4 = testkey_v4 + ",";
109 TemporaryFile key_file4;
110 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path));
111 ASSERT_FALSE(load_keys(key_file4.path, certs));
112
113 // Invalid key separator.
114 std::string invalid_key5 = testkey_v4 + ";" + testkey_v4;
115 TemporaryFile key_file5;
116 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path));
117 ASSERT_FALSE(load_keys(key_file5.path, certs));
118}
119
Tao Bao056e2da2017-03-26 23:25:11 -0700120TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
121 std::string testkey_v3;
122 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
123
124 TemporaryFile key_file;
125 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
126 std::vector<Certificate> certs;
127 ASSERT_TRUE(load_keys(key_file.path, certs));
128
129 // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
130 std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
131 ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
132 package.size(), certs));
133}
134
Tao Bao217d9f92017-03-20 16:57:25 -0700135TEST(VerifierTest, BadPackage_AlteredFooter) {
136 std::string testkey_v3;
137 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
138 TemporaryFile key_file1;
139 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
140 std::vector<Certificate> certs;
141 ASSERT_TRUE(load_keys(key_file1.path, certs));
142
143 std::string package;
144 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
145 ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
146
147 // Alter the footer.
148 package[package.size() - 5] = '\x05';
149 ASSERT_EQ(VERIFY_FAILURE,
150 verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
151 certs));
152}
153
154TEST(VerifierTest, BadPackage_AlteredContent) {
155 std::string testkey_v3;
156 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
157 TemporaryFile key_file1;
158 ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
159 std::vector<Certificate> certs;
160 ASSERT_TRUE(load_keys(key_file1.path, certs));
161
162 std::string package;
163 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
164 ASSERT_GT(package.size(), static_cast<size_t>(100));
165
166 // Alter the content.
167 std::string altered1(package);
168 altered1[50] += 1;
169 ASSERT_EQ(VERIFY_FAILURE,
170 verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
171 certs));
172
173 std::string altered2(package);
174 altered2[10] += 1;
175 ASSERT_EQ(VERIFY_FAILURE,
176 verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
177 certs));
178}
179
Jed Estep43291862016-02-03 17:02:09 -0800180TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700181 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800182}
183
184TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700185 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800186}
187
188INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700189 ::testing::Values(
190 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
191 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
192 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
193 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
194 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800195
196INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700197 ::testing::Values(
198 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
199 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
200 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
201 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
202 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800203
204INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700205 ::testing::Values(
206 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
207 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
208 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
209 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
210 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800211
212INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700213 ::testing::Values(
214 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
215 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
216 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
217 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800218
219INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700220 ::testing::Values(
221 std::vector<std::string>({"random.zip", "v1"}),
Tao Bao217d9f92017-03-20 16:57:25 -0700222 std::vector<std::string>({"fake-eocd.zip", "v1"})));