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]); |
| 43 | if (sysMapFile(package.c_str(), &memmap) != 0) { |
| 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 | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 120 | TEST(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 Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 135 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 136 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_P(VerifierFailureTest, VerifyFailure) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 140 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 144 | ::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 Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 150 | |
| 151 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 152 | ::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 Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 158 | |
| 159 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 160 | ::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 Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 166 | |
| 167 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 168 | ::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 Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 173 | |
| 174 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 175 | ::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 Bao | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 179 | std::vector<std::string>({"alter-footer.zip", "v1"}))); |