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> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 21 | #include <sys/stat.h> |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 22 | #include <sys/types.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 23 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 29 | #include <android-base/test_utils.h> |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 30 | #include <android-base/unique_fd.h> |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 32 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 33 | #include "common/test_constants.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 34 | #include "otautil/sysutil.h" |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 35 | #include "verifier.h" |
| 36 | |
Tao Bao | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 37 | using namespace std::string_literals; |
| 38 | |
Tianjie Xu | 8256698 | 2018-10-10 15:44:17 -0700 | [diff] [blame] | 39 | static void LoadKeyFromFile(const std::string& file_name, Certificate* cert) { |
| 40 | std::string testkey_string; |
| 41 | ASSERT_TRUE(android::base::ReadFileToString(file_name, &testkey_string)); |
| 42 | ASSERT_TRUE(LoadCertificateFromBuffer( |
| 43 | std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), cert)); |
| 44 | } |
| 45 | |
| 46 | static void VerifyPackageWithCertificate(const std::string& name, Certificate&& cert) { |
| 47 | std::string package = from_testdata_base(name); |
| 48 | MemMapping memmap; |
| 49 | if (!memmap.MapFile(package)) { |
| 50 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
| 51 | } |
| 52 | |
| 53 | std::vector<Certificate> certs; |
| 54 | certs.emplace_back(std::move(cert)); |
| 55 | ASSERT_EQ(VERIFY_SUCCESS, verify_file(memmap.addr, memmap.length, certs)); |
| 56 | } |
| 57 | |
| 58 | TEST(VerifierTest, LoadCertificateFromBuffer_failure) { |
| 59 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 60 | std::string testkey_string; |
| 61 | ASSERT_TRUE( |
| 62 | android::base::ReadFileToString(from_testdata_base("testkey_v1.txt"), &testkey_string)); |
| 63 | ASSERT_FALSE(LoadCertificateFromBuffer( |
| 64 | std::vector<uint8_t>(testkey_string.begin(), testkey_string.end()), &cert)); |
| 65 | } |
| 66 | |
| 67 | TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent3) { |
| 68 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 69 | LoadKeyFromFile(from_testdata_base("testkey_v1.x509.pem"), &cert); |
| 70 | |
| 71 | ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len); |
| 72 | ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type); |
| 73 | ASSERT_EQ(nullptr, cert.ec); |
| 74 | |
| 75 | VerifyPackageWithCertificate("otasigned_v1.zip", std::move(cert)); |
| 76 | } |
| 77 | |
| 78 | TEST(VerifierTest, LoadCertificateFromBuffer_sha1_exponent65537) { |
| 79 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 80 | LoadKeyFromFile(from_testdata_base("testkey_v2.x509.pem"), &cert); |
| 81 | |
| 82 | ASSERT_EQ(SHA_DIGEST_LENGTH, cert.hash_len); |
| 83 | ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type); |
| 84 | ASSERT_EQ(nullptr, cert.ec); |
| 85 | |
| 86 | VerifyPackageWithCertificate("otasigned_v2.zip", std::move(cert)); |
| 87 | } |
| 88 | |
| 89 | TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent3) { |
| 90 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 91 | LoadKeyFromFile(from_testdata_base("testkey_v3.x509.pem"), &cert); |
| 92 | |
| 93 | ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len); |
| 94 | ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type); |
| 95 | ASSERT_EQ(nullptr, cert.ec); |
| 96 | |
| 97 | VerifyPackageWithCertificate("otasigned_v3.zip", std::move(cert)); |
| 98 | } |
| 99 | |
| 100 | TEST(VerifierTest, LoadCertificateFromBuffer_sha256_exponent65537) { |
| 101 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 102 | LoadKeyFromFile(from_testdata_base("testkey_v4.x509.pem"), &cert); |
| 103 | |
| 104 | ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len); |
| 105 | ASSERT_EQ(Certificate::KEY_TYPE_RSA, cert.key_type); |
| 106 | ASSERT_EQ(nullptr, cert.ec); |
| 107 | |
| 108 | VerifyPackageWithCertificate("otasigned_v4.zip", std::move(cert)); |
| 109 | } |
| 110 | |
| 111 | TEST(VerifierTest, LoadCertificateFromBuffer_sha256_ec256bits) { |
| 112 | Certificate cert(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr); |
| 113 | LoadKeyFromFile(from_testdata_base("testkey_v5.x509.pem"), &cert); |
| 114 | |
| 115 | ASSERT_EQ(SHA256_DIGEST_LENGTH, cert.hash_len); |
| 116 | ASSERT_EQ(Certificate::KEY_TYPE_EC, cert.key_type); |
| 117 | ASSERT_EQ(nullptr, cert.rsa); |
| 118 | |
| 119 | VerifyPackageWithCertificate("otasigned_v5.zip", std::move(cert)); |
| 120 | } |
| 121 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 122 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 123 | protected: |
| 124 | void SetUp() override { |
| 125 | std::vector<std::string> args = GetParam(); |
| 126 | std::string package = from_testdata_base(args[0]); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 127 | if (!memmap.MapFile(package)) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 128 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 131 | for (auto it = ++args.cbegin(); it != args.cend(); ++it) { |
| 132 | std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt"); |
| 133 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 134 | } |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | MemMapping memmap; |
| 138 | std::vector<Certificate> certs; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | class VerifierSuccessTest : public VerifierTest { |
| 142 | }; |
| 143 | |
| 144 | class VerifierFailureTest : public VerifierTest { |
| 145 | }; |
| 146 | |
Tao Bao | 3116ce4 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 147 | TEST(VerifierTest, load_keys_multiple_keys) { |
| 148 | std::string testkey_v4; |
| 149 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4)); |
| 150 | |
| 151 | std::string testkey_v3; |
| 152 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 153 | |
| 154 | std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4; |
| 155 | TemporaryFile key_file1; |
| 156 | ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path)); |
| 157 | std::vector<Certificate> certs; |
| 158 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 159 | ASSERT_EQ(3U, certs.size()); |
| 160 | } |
| 161 | |
| 162 | TEST(VerifierTest, load_keys_invalid_keys) { |
| 163 | std::vector<Certificate> certs; |
| 164 | ASSERT_FALSE(load_keys("/doesntexist", certs)); |
| 165 | |
| 166 | // Empty file. |
| 167 | TemporaryFile key_file1; |
| 168 | ASSERT_FALSE(load_keys(key_file1.path, certs)); |
| 169 | |
| 170 | // Invalid contents. |
| 171 | ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path)); |
| 172 | ASSERT_FALSE(load_keys(key_file1.path, certs)); |
| 173 | |
| 174 | std::string testkey_v4; |
| 175 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4)); |
| 176 | |
| 177 | // Invalid key version: "v4 ..." => "v6 ...". |
| 178 | std::string invalid_key2(testkey_v4); |
| 179 | invalid_key2[1] = '6'; |
| 180 | TemporaryFile key_file2; |
| 181 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path)); |
| 182 | ASSERT_FALSE(load_keys(key_file2.path, certs)); |
| 183 | |
| 184 | // Invalid key content: inserted extra bytes ",2209831334". |
| 185 | std::string invalid_key3(testkey_v4); |
| 186 | invalid_key3.insert(invalid_key2.size() - 2, ",2209831334"); |
| 187 | TemporaryFile key_file3; |
| 188 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path)); |
| 189 | ASSERT_FALSE(load_keys(key_file3.path, certs)); |
| 190 | |
| 191 | // Invalid key: the last key must not end with an extra ','. |
| 192 | std::string invalid_key4 = testkey_v4 + ","; |
| 193 | TemporaryFile key_file4; |
| 194 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path)); |
| 195 | ASSERT_FALSE(load_keys(key_file4.path, certs)); |
| 196 | |
| 197 | // Invalid key separator. |
| 198 | std::string invalid_key5 = testkey_v4 + ";" + testkey_v4; |
| 199 | TemporaryFile key_file5; |
| 200 | ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path)); |
| 201 | ASSERT_FALSE(load_keys(key_file5.path, certs)); |
| 202 | } |
| 203 | |
Tao Bao | 7e61c6a | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 204 | TEST(VerifierTest, BadPackage_AlteredFooter) { |
| 205 | std::string testkey_v3; |
| 206 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 207 | TemporaryFile key_file1; |
| 208 | ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path)); |
| 209 | std::vector<Certificate> certs; |
| 210 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 211 | |
| 212 | std::string package; |
| 213 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package)); |
| 214 | ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6)); |
| 215 | |
| 216 | // Alter the footer. |
| 217 | package[package.size() - 5] = '\x05'; |
| 218 | ASSERT_EQ(VERIFY_FAILURE, |
| 219 | verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(), |
| 220 | certs)); |
| 221 | } |
| 222 | |
| 223 | TEST(VerifierTest, BadPackage_AlteredContent) { |
| 224 | std::string testkey_v3; |
| 225 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 226 | TemporaryFile key_file1; |
| 227 | ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path)); |
| 228 | std::vector<Certificate> certs; |
| 229 | ASSERT_TRUE(load_keys(key_file1.path, certs)); |
| 230 | |
| 231 | std::string package; |
| 232 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package)); |
| 233 | ASSERT_GT(package.size(), static_cast<size_t>(100)); |
| 234 | |
| 235 | // Alter the content. |
| 236 | std::string altered1(package); |
| 237 | altered1[50] += 1; |
| 238 | ASSERT_EQ(VERIFY_FAILURE, |
| 239 | verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(), |
| 240 | certs)); |
| 241 | |
| 242 | std::string altered2(package); |
| 243 | altered2[10] += 1; |
| 244 | ASSERT_EQ(VERIFY_FAILURE, |
| 245 | verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(), |
| 246 | certs)); |
| 247 | } |
| 248 | |
Tao Bao | 056e2da | 2017-03-26 23:25:11 -0700 | [diff] [blame] | 249 | TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) { |
| 250 | std::string testkey_v3; |
| 251 | ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3)); |
| 252 | |
| 253 | TemporaryFile key_file; |
| 254 | ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path)); |
| 255 | std::vector<Certificate> certs; |
| 256 | ASSERT_TRUE(load_keys(key_file.path, certs)); |
| 257 | |
| 258 | // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369). |
| 259 | std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s; |
| 260 | ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()), |
| 261 | package.size(), certs)); |
| 262 | } |
| 263 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 264 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 265 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TEST_P(VerifierFailureTest, VerifyFailure) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 269 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 273 | ::testing::Values( |
| 274 | std::vector<std::string>({"otasigned_v1.zip", "v1"}), |
| 275 | std::vector<std::string>({"otasigned_v2.zip", "v2"}), |
| 276 | std::vector<std::string>({"otasigned_v3.zip", "v3"}), |
| 277 | std::vector<std::string>({"otasigned_v4.zip", "v4"}), |
| 278 | std::vector<std::string>({"otasigned_v5.zip", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 279 | |
| 280 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 281 | ::testing::Values( |
| 282 | std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}), |
| 283 | std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}), |
| 284 | std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}), |
| 285 | std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}), |
| 286 | std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 287 | |
| 288 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 289 | ::testing::Values( |
| 290 | std::vector<std::string>({"otasigned_v1.zip", "v2"}), |
| 291 | std::vector<std::string>({"otasigned_v2.zip", "v1"}), |
| 292 | std::vector<std::string>({"otasigned_v3.zip", "v5"}), |
| 293 | std::vector<std::string>({"otasigned_v4.zip", "v5"}), |
| 294 | std::vector<std::string>({"otasigned_v5.zip", "v3"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 295 | |
| 296 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 297 | ::testing::Values( |
| 298 | std::vector<std::string>({"otasigned_v1.zip", "v3"}), |
| 299 | std::vector<std::string>({"otasigned_v2.zip", "v4"}), |
| 300 | std::vector<std::string>({"otasigned_v3.zip", "v1"}), |
| 301 | std::vector<std::string>({"otasigned_v4.zip", "v2"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 302 | |
| 303 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 304 | ::testing::Values( |
| 305 | std::vector<std::string>({"random.zip", "v1"}), |
Tao Bao | 7e61c6a | 2017-03-20 16:57:25 -0700 | [diff] [blame] | 306 | std::vector<std::string>({"fake-eocd.zip", "v1"}))); |