Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 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> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 23 | #include <sys/types.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 24 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 30 | #include <android-base/test_utils.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 31 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 32 | #include "common/test_constants.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 33 | #include "otautil/SysUtil.h" |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 34 | #include "verifier.h" |
| 35 | |
Tao Bao | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 36 | using namespace std::string_literals; |
| 37 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 38 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 39 | protected: |
| 40 | void SetUp() override { |
| 41 | std::vector<std::string> args = GetParam(); |
| 42 | std::string package = from_testdata_base(args[0]); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 43 | if (!memmap.MapFile(package)) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 44 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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)); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 50 | } |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | MemMapping memmap; |
| 54 | std::vector<Certificate> certs; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | class VerifierSuccessTest : public VerifierTest { |
| 58 | }; |
| 59 | |
| 60 | class VerifierFailureTest : public VerifierTest { |
| 61 | }; |
| 62 | |
Tao Bao | 3116ce4 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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 | |
| 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 | |
Tao Bao | 7e61c6a | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 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 | |
| 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 | |
Tao Bao | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 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 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 180 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 181 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_P(VerifierFailureTest, VerifyFailure) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 185 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 195 | |
| 196 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 203 | |
| 204 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 211 | |
| 212 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 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"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 218 | |
| 219 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 220 | ::testing::Values( |
| 221 | std::vector<std::string>({"random.zip", "v1"}), |
Tao Bao | 7e61c6a | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 222 | std::vector<std::string>({"fake-eocd.zip", "v1"}))); |