• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #include <string>
26 #include <vector>
27 
28 #include <android-base/file.h>
29 #include <android-base/stringprintf.h>
30 #include <android-base/test_utils.h>
31 
32 #include "common/test_constants.h"
33 #include "otautil/SysUtil.h"
34 #include "verifier.h"
35 
36 using namespace std::string_literals;
37 
38 class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
39  protected:
SetUp()40   void SetUp() override {
41     std::vector<std::string> args = GetParam();
42     std::string package = from_testdata_base(args[0]);
43     if (!memmap.MapFile(package)) {
44       FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
45     }
46 
47     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));
50     }
51   }
52 
53   MemMapping memmap;
54   std::vector<Certificate> certs;
55 };
56 
57 class VerifierSuccessTest : public VerifierTest {
58 };
59 
60 class VerifierFailureTest : public VerifierTest {
61 };
62 
TEST(VerifierTest,load_keys_multiple_keys)63 TEST(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 
TEST(VerifierTest,load_keys_invalid_keys)78 TEST(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 
TEST(VerifierTest,BadPackage_AlteredFooter)120 TEST(VerifierTest, BadPackage_AlteredFooter) {
121   std::string testkey_v3;
122   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
123   TemporaryFile key_file1;
124   ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
125   std::vector<Certificate> certs;
126   ASSERT_TRUE(load_keys(key_file1.path, certs));
127 
128   std::string package;
129   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
130   ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
131 
132   // Alter the footer.
133   package[package.size() - 5] = '\x05';
134   ASSERT_EQ(VERIFY_FAILURE,
135             verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
136                         certs));
137 }
138 
TEST(VerifierTest,BadPackage_AlteredContent)139 TEST(VerifierTest, BadPackage_AlteredContent) {
140   std::string testkey_v3;
141   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
142   TemporaryFile key_file1;
143   ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
144   std::vector<Certificate> certs;
145   ASSERT_TRUE(load_keys(key_file1.path, certs));
146 
147   std::string package;
148   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
149   ASSERT_GT(package.size(), static_cast<size_t>(100));
150 
151   // Alter the content.
152   std::string altered1(package);
153   altered1[50] += 1;
154   ASSERT_EQ(VERIFY_FAILURE,
155             verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
156                         certs));
157 
158   std::string altered2(package);
159   altered2[10] += 1;
160   ASSERT_EQ(VERIFY_FAILURE,
161             verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
162                         certs));
163 }
164 
TEST(VerifierTest,BadPackage_SignatureStartOutOfBounds)165 TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
166   std::string testkey_v3;
167   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
168 
169   TemporaryFile key_file;
170   ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
171   std::vector<Certificate> certs;
172   ASSERT_TRUE(load_keys(key_file.path, certs));
173 
174   // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
175   std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
176   ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
177                                         package.size(), certs));
178 }
179 
TEST_P(VerifierSuccessTest,VerifySucceed)180 TEST_P(VerifierSuccessTest, VerifySucceed) {
181   ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
182 }
183 
TEST_P(VerifierFailureTest,VerifyFailure)184 TEST_P(VerifierFailureTest, VerifyFailure) {
185   ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
186 }
187 
188 INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
189     ::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"})));
195 
196 INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
197     ::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"})));
203 
204 INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
205     ::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"})));
211 
212 INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
213     ::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"})));
218 
219 INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
220     ::testing::Values(
221       std::vector<std::string>({"random.zip", "v1"}),
222       std::vector<std::string>({"fake-eocd.zip", "v1"})));
223