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 | 5e53501 | 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 | 5e53501 | 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 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 36 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 37 | protected: |
| 38 | void SetUp() override { |
| 39 | std::vector<std::string> args = GetParam(); |
| 40 | std::string package = from_testdata_base(args[0]); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 41 | if (!memmap.MapFile(package)) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 42 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 45 | for (auto it = ++args.cbegin(); it != args.cend(); ++it) { |
| 46 | std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt"); |
| 47 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 48 | } |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | MemMapping memmap; |
| 52 | std::vector<Certificate> certs; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class VerifierSuccessTest : public VerifierTest { |
| 56 | }; |
| 57 | |
| 58 | class VerifierFailureTest : public VerifierTest { |
| 59 | }; |
| 60 | |
Tao Bao | 3116ce4 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 61 | TEST(VerifierTest, load_keys_multiple_keys) { |
| 62 | std::string testkey_v4; |
| 63 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4)); |
| 64 | |
| 65 | std::string testkey_v3; |
| 66 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 67 | |
| 68 | std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4; |
| 69 | TemporaryFile key_file1; |
| 70 | ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path)); |
| 71 | std::vector<Certificate> certs; |
| 72 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 73 | ASSERT_EQ(3U, certs.size()); |
| 74 | } |
| 75 | |
| 76 | TEST(VerifierTest, load_keys_invalid_keys) { |
| 77 | std::vector<Certificate> certs; |
| 78 | ASSERT_FALSE(load_keys("/doesntexist", certs)); |
| 79 | |
| 80 | // Empty file. |
| 81 | TemporaryFile key_file1; |
| 82 | ASSERT_FALSE(load_keys(key_file1.path, certs)); |
| 83 | |
| 84 | // Invalid contents. |
| 85 | ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path)); |
| 86 | ASSERT_FALSE(load_keys(key_file1.path, certs)); |
| 87 | |
| 88 | std::string testkey_v4; |
| 89 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4)); |
| 90 | |
| 91 | // Invalid key version: "v4 ..." => "v6 ...". |
| 92 | std::string invalid_key2(testkey_v4); |
| 93 | invalid_key2[1] = '6'; |
| 94 | TemporaryFile key_file2; |
| 95 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path)); |
| 96 | ASSERT_FALSE(load_keys(key_file2.path, certs)); |
| 97 | |
| 98 | // Invalid key content: inserted extra bytes ",2209831334". |
| 99 | std::string invalid_key3(testkey_v4); |
| 100 | invalid_key3.insert(invalid_key2.size() - 2, ",2209831334"); |
| 101 | TemporaryFile key_file3; |
| 102 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path)); |
| 103 | ASSERT_FALSE(load_keys(key_file3.path, certs)); |
| 104 | |
| 105 | // Invalid key: the last key must not end with an extra ','. |
| 106 | std::string invalid_key4 = testkey_v4 + ","; |
| 107 | TemporaryFile key_file4; |
| 108 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path)); |
| 109 | ASSERT_FALSE(load_keys(key_file4.path, certs)); |
| 110 | |
| 111 | // Invalid key separator. |
| 112 | std::string invalid_key5 = testkey_v4 + ";" + testkey_v4; |
| 113 | TemporaryFile key_file5; |
| 114 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path)); |
| 115 | ASSERT_FALSE(load_keys(key_file5.path, certs)); |
| 116 | } |
| 117 | |
Tao Bao | 217d9f9 | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 118 | TEST(VerifierTest, BadPackage_AlteredFooter) { |
| 119 | std::string testkey_v3; |
| 120 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 121 | TemporaryFile key_file1; |
| 122 | ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path)); |
| 123 | std::vector<Certificate> certs; |
| 124 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 125 | |
| 126 | std::string package; |
| 127 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package)); |
| 128 | ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6)); |
| 129 | |
| 130 | // Alter the footer. |
| 131 | package[package.size() - 5] = '\x05'; |
| 132 | ASSERT_EQ(VERIFY_FAILURE, |
| 133 | verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(), |
| 134 | certs)); |
| 135 | } |
| 136 | |
| 137 | TEST(VerifierTest, BadPackage_AlteredContent) { |
| 138 | std::string testkey_v3; |
| 139 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 140 | TemporaryFile key_file1; |
| 141 | ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path)); |
| 142 | std::vector<Certificate> certs; |
| 143 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 144 | |
| 145 | std::string package; |
| 146 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package)); |
| 147 | ASSERT_GT(package.size(), static_cast<size_t>(100)); |
| 148 | |
| 149 | // Alter the content. |
| 150 | std::string altered1(package); |
| 151 | altered1[50] += 1; |
| 152 | ASSERT_EQ(VERIFY_FAILURE, |
| 153 | verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(), |
| 154 | certs)); |
| 155 | |
| 156 | std::string altered2(package); |
| 157 | altered2[10] += 1; |
| 158 | ASSERT_EQ(VERIFY_FAILURE, |
| 159 | verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(), |
| 160 | certs)); |
| 161 | } |
| 162 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 163 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 164 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST_P(VerifierFailureTest, VerifyFailure) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 168 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 172 | ::testing::Values( |
| 173 | std::vector<std::string>({"otasigned_v1.zip", "v1"}), |
| 174 | std::vector<std::string>({"otasigned_v2.zip", "v2"}), |
| 175 | std::vector<std::string>({"otasigned_v3.zip", "v3"}), |
| 176 | std::vector<std::string>({"otasigned_v4.zip", "v4"}), |
| 177 | std::vector<std::string>({"otasigned_v5.zip", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 178 | |
| 179 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 180 | ::testing::Values( |
| 181 | std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}), |
| 182 | std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}), |
| 183 | std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}), |
| 184 | std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}), |
| 185 | std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 186 | |
| 187 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 188 | ::testing::Values( |
| 189 | std::vector<std::string>({"otasigned_v1.zip", "v2"}), |
| 190 | std::vector<std::string>({"otasigned_v2.zip", "v1"}), |
| 191 | std::vector<std::string>({"otasigned_v3.zip", "v5"}), |
| 192 | std::vector<std::string>({"otasigned_v4.zip", "v5"}), |
| 193 | std::vector<std::string>({"otasigned_v5.zip", "v3"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 194 | |
| 195 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 196 | ::testing::Values( |
| 197 | std::vector<std::string>({"otasigned_v1.zip", "v3"}), |
| 198 | std::vector<std::string>({"otasigned_v2.zip", "v4"}), |
| 199 | std::vector<std::string>({"otasigned_v3.zip", "v1"}), |
| 200 | std::vector<std::string>({"otasigned_v4.zip", "v2"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 201 | |
| 202 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 203 | ::testing::Values( |
| 204 | std::vector<std::string>({"random.zip", "v1"}), |
Tao Bao | 217d9f9 | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 205 | std::vector<std::string>({"fake-eocd.zip", "v1"}))); |