blob: 4c064871498481f0c58b64e4475efeff20750619 [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
Jed Estep43291862016-02-03 17:02:09 -0800135TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao7b22c922017-03-16 17:37:38 -0700136 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800137}
138
139TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao7b22c922017-03-16 17:37:38 -0700140 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800141}
142
143INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700144 ::testing::Values(
145 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
146 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
147 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
148 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
149 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800150
151INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700152 ::testing::Values(
153 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
154 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
155 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
156 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
157 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800158
159INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700160 ::testing::Values(
161 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
162 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
163 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
164 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
165 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800166
167INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700168 ::testing::Values(
169 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
170 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
171 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
172 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800173
174INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao7b22c922017-03-16 17:37:38 -0700175 ::testing::Values(
176 std::vector<std::string>({"random.zip", "v1"}),
177 std::vector<std::string>({"fake-eocd.zip", "v1"}),
178 std::vector<std::string>({"alter-metadata.zip", "v1"}),
Tao Bao056e2da2017-03-26 23:25:11 -0700179 std::vector<std::string>({"alter-footer.zip", "v1"})));