Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/internal/verify_name_match.h" | 5 #include "net/cert/internal/verify_name_match.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_util.h" | |
| 7 #include "net/der/input.h" | 13 #include "net/der/input.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 15 |
| 10 namespace net { | 16 namespace net { |
| 11 | 17 namespace { |
| 12 TEST(VerifyNameMatchTest, Simple) { | 18 |
| 13 // TODO(mattm): Use valid Names. | 19 der::Input InputFromString(const std::string& s) { |
| 14 const uint8_t hello[] = {'h', 'e', 'l', 'l', 'o'}; | 20 return der::Input(reinterpret_cast<const uint8_t*>(s.data()), s.size()); |
| 15 const uint8_t aello[] = {'a', 'e', 'l', 'l', 'o'}; | 21 } |
| 16 const uint8_t hello1[] = {'h', 'e', 'l', 'l', 'o', '1'}; | 22 |
| 17 EXPECT_TRUE(VerifyNameMatch(der::Input(hello), der::Input(hello))); | 23 std::string LoadTestData(const std::string& prefix, |
| 18 EXPECT_FALSE(VerifyNameMatch(der::Input(aello), der::Input(hello))); | 24 const std::string& value_type, |
| 19 EXPECT_FALSE(VerifyNameMatch(der::Input(hello), der::Input(hello1))); | 25 const std::string& suffix) { |
| 20 EXPECT_FALSE(VerifyNameMatch(der::Input(hello1), der::Input(hello))); | 26 base::FilePath src_root; |
| 27 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | |
| 28 std::string filename = prefix + "-" + value_type + "-" + suffix + ".der"; | |
| 29 base::FilePath filepath = | |
| 30 src_root.Append(FILE_PATH_LITERAL( | |
| 31 "net/data/verify_name_match_unittest/names")) | |
| 32 .AppendASCII(filename); | |
| 33 std::string result; | |
| 34 bool success = base::ReadFileToString(filepath, &result); | |
| 35 EXPECT_TRUE(success); | |
| 36 return result; | |
| 37 } | |
| 38 | |
| 39 // All string types. | |
| 40 static const char* kValueTypes[] = {"PRINTABLESTRING", | |
| 41 "T61STRING", | |
| 42 "UTF8", | |
| 43 "BMPSTRING", | |
| 44 "UNIVERSALSTRING"}; | |
| 45 // String types that can encode the Unicode Basic Multilingual Plane. | |
| 46 static const char* kUnicodeBMPValueTypes[] = {"UTF8", | |
| 47 "BMPSTRING", | |
| 48 "UNIVERSALSTRING"}; | |
| 49 // String types that can encode the Unicode Supplementary Planes. | |
| 50 static const char* kUnicodeSupplementaryValueTypes[] = {"UTF8", | |
| 51 "UNIVERSALSTRING"}; | |
| 52 | |
| 53 static const char* kMangleTypes[] = {"unmangled", | |
| 54 "case_swap", | |
| 55 "extra_whitespace"}; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 class VerifyNameMatchSimpleTest | |
| 60 : public ::testing::TestWithParam< | |
| 61 ::testing::tuple<const char*, const char*>> { | |
| 62 public: | |
| 63 std::string value_type() const { return ::testing::get<0>(GetParam()); } | |
| 64 std::string suffix() const { return ::testing::get<1>(GetParam()); } | |
| 65 }; | |
| 66 | |
| 67 // Compare each input against itself, verifies that all input data is parsed | |
| 68 // successfully. | |
| 69 TEST_P(VerifyNameMatchSimpleTest, ExactEquality) { | |
| 70 std::string der = LoadTestData("ascii", value_type(), suffix()); | |
| 71 EXPECT_TRUE(VerifyNameMatch(InputFromString(der), InputFromString(der))); | |
| 72 | |
| 73 std::string der_extra_attr = | |
| 74 LoadTestData("ascii", value_type(), suffix() + "-extra_attr"); | |
| 75 EXPECT_TRUE(VerifyNameMatch(InputFromString(der_extra_attr), | |
| 76 InputFromString(der_extra_attr))); | |
| 77 | |
| 78 std::string der_extra_rdn = | |
| 79 LoadTestData("ascii", value_type(), suffix() + "-extra_rdn"); | |
| 80 EXPECT_TRUE(VerifyNameMatch(InputFromString(der_extra_rdn), | |
| 81 InputFromString(der_extra_rdn))); | |
| 82 } | |
| 83 | |
| 84 // Ensure that a Name does not match another Name which is exactly the same but | |
| 85 // with an extra attribute in one Relative Distinguished Name. | |
| 86 TEST_P(VerifyNameMatchSimpleTest, ExtraAttrDoesNotMatch) { | |
| 87 std::string der = LoadTestData("ascii", value_type(), suffix()); | |
| 88 std::string der_extra_attr = | |
| 89 LoadTestData("ascii", value_type(), suffix() + "-extra_attr"); | |
| 90 EXPECT_FALSE( | |
| 91 VerifyNameMatch(InputFromString(der), InputFromString(der_extra_attr))); | |
| 92 EXPECT_FALSE( | |
| 93 VerifyNameMatch(InputFromString(der_extra_attr), InputFromString(der))); | |
| 94 } | |
| 95 | |
| 96 // Ensure that a Name does not match another Name which is exactly the same but | |
| 97 // with an extra Relative Distinguished Name. | |
| 98 TEST_P(VerifyNameMatchSimpleTest, ExtraRdnDoesNotMatch) { | |
| 99 std::string der = LoadTestData("ascii", value_type(), suffix()); | |
| 100 std::string der_extra_rdn = | |
| 101 LoadTestData("ascii", value_type(), suffix() + "-extra_rdn"); | |
| 102 EXPECT_FALSE( | |
| 103 VerifyNameMatch(InputFromString(der), InputFromString(der_extra_rdn))); | |
| 104 EXPECT_FALSE( | |
| 105 VerifyNameMatch(InputFromString(der_extra_rdn), InputFromString(der))); | |
| 106 } | |
| 107 | |
| 108 INSTANTIATE_TEST_CASE_P(InstantiationName, | |
| 109 VerifyNameMatchSimpleTest, | |
| 110 ::testing::Combine(::testing::ValuesIn(kValueTypes), | |
| 111 ::testing::ValuesIn(kMangleTypes))); | |
| 112 | |
| 113 class VerifyNameMatchNormalizationTest | |
| 114 : public ::testing::TestWithParam<::testing::tuple<bool, const char*>> { | |
|
Ryan Sleevi
2015/06/20 00:15:45
nit: Seems like it'd be less writing on lines 147-
mattm
2015/06/22 23:42:10
I gave it a try, but it only made those lines a li
Ryan Sleevi
2015/06/23 07:47:54
You shouldn't need a constructor - I would presume
| |
| 115 public: | |
| 116 bool expected_result() const { return ::testing::get<0>(GetParam()); } | |
| 117 std::string value_type() const { return ::testing::get<1>(GetParam()); } | |
| 118 }; | |
| 119 | |
| 120 // Verify matching is case insensitive (for the types which currently support | |
| 121 // normalization). | |
| 122 TEST_P(VerifyNameMatchNormalizationTest, CaseInsensitivity) { | |
| 123 std::string normal = LoadTestData("ascii", value_type(), "unmangled"); | |
| 124 std::string case_swap = LoadTestData("ascii", value_type(), "case_swap"); | |
| 125 EXPECT_EQ(expected_result(), VerifyNameMatch(InputFromString(normal), | |
| 126 InputFromString(case_swap))); | |
| 127 EXPECT_EQ(expected_result(), VerifyNameMatch(InputFromString(case_swap), | |
| 128 InputFromString(normal))); | |
| 129 } | |
| 130 | |
| 131 // Verify matching folds whitespace (for the types which currently support | |
| 132 // normalization). | |
| 133 TEST_P(VerifyNameMatchNormalizationTest, CollapseWhitespace) { | |
| 134 std::string normal = LoadTestData("ascii", value_type(), "unmangled"); | |
| 135 std::string whitespace = | |
| 136 LoadTestData("ascii", value_type(), "extra_whitespace"); | |
| 137 EXPECT_EQ(expected_result(), VerifyNameMatch(InputFromString(normal), | |
| 138 InputFromString(whitespace))); | |
| 139 EXPECT_EQ(expected_result(), VerifyNameMatch(InputFromString(whitespace), | |
| 140 InputFromString(normal))); | |
| 141 } | |
| 142 | |
| 143 INSTANTIATE_TEST_CASE_P( | |
| 144 InstantiationName, | |
| 145 VerifyNameMatchNormalizationTest, | |
| 146 ::testing::Values( | |
| 147 ::testing::make_tuple(true, | |
| 148 static_cast<const char*>("PRINTABLESTRING")), | |
| 149 ::testing::make_tuple(false, static_cast<const char*>("T61STRING")), | |
| 150 ::testing::make_tuple(true, static_cast<const char*>("UTF8")), | |
| 151 ::testing::make_tuple(true, static_cast<const char*>("BMPSTRING")), | |
| 152 ::testing::make_tuple(true, | |
| 153 static_cast<const char*>("UNIVERSALSTRING")))); | |
| 154 | |
| 155 class VerifyNameMatchDifferingTypesTest | |
| 156 : public ::testing::TestWithParam< | |
| 157 ::testing::tuple<const char*, const char*>> { | |
| 158 public: | |
| 159 std::string value_type_1() const { return ::testing::get<0>(GetParam()); } | |
| 160 std::string value_type_2() const { return ::testing::get<1>(GetParam()); } | |
| 161 }; | |
| 162 | |
| 163 TEST_P(VerifyNameMatchDifferingTypesTest, NormalizableTypesAreEqual) { | |
| 164 std::string der_1 = LoadTestData("ascii", value_type_1(), "unmangled"); | |
| 165 std::string der_2 = LoadTestData("ascii", value_type_2(), "unmangled"); | |
| 166 if (value_type_1() == value_type_2()) { | |
| 167 EXPECT_TRUE( | |
| 168 VerifyNameMatch(InputFromString(der_1), InputFromString(der_2))); | |
| 169 } else if ((value_type_1() == "PRINTABLESTRING" || value_type_1() == "UTF8" || | |
| 170 value_type_1() == "BMPSTRING" || | |
| 171 value_type_1() == "UNIVERSALSTRING") && | |
| 172 (value_type_2() == "PRINTABLESTRING" || value_type_2() == "UTF8" || | |
| 173 value_type_2() == "BMPSTRING" || | |
| 174 value_type_2() == "UNIVERSALSTRING")) { | |
| 175 EXPECT_TRUE( | |
| 176 VerifyNameMatch(InputFromString(der_1), InputFromString(der_2))); | |
| 177 } else { | |
| 178 EXPECT_FALSE( | |
| 179 VerifyNameMatch(InputFromString(der_1), InputFromString(der_2))); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 INSTANTIATE_TEST_CASE_P(InstantiationName, | |
| 184 VerifyNameMatchDifferingTypesTest, | |
| 185 ::testing::Combine(::testing::ValuesIn(kValueTypes), | |
| 186 ::testing::ValuesIn(kValueTypes))); | |
| 187 | |
| 188 class VerifyNameMatchUnicodeConversionTest | |
| 189 : public ::testing::TestWithParam< | |
| 190 ::testing::tuple<const char*, | |
| 191 ::testing::tuple<const char*, const char*>>> { | |
| 192 public: | |
| 193 std::string prefix() const { return ::testing::get<0>(GetParam()); } | |
| 194 std::string value_type_1() const { | |
| 195 return ::testing::get<0>(::testing::get<1>(GetParam())); | |
| 196 } | |
| 197 std::string value_type_2() const { | |
| 198 return ::testing::get<1>(::testing::get<1>(GetParam())); | |
| 199 } | |
| 200 }; | |
| 201 | |
| 202 TEST_P(VerifyNameMatchUnicodeConversionTest, UnicodeConversionsAreEqual) { | |
| 203 std::string der_1 = LoadTestData(prefix(), value_type_1(), "unmangled"); | |
| 204 std::string der_2 = LoadTestData(prefix(), value_type_2(), "unmangled"); | |
| 205 EXPECT_TRUE(VerifyNameMatch(InputFromString(der_1), InputFromString(der_2))); | |
| 206 } | |
| 207 | |
| 208 INSTANTIATE_TEST_CASE_P( | |
| 209 BMPConversion, | |
| 210 VerifyNameMatchUnicodeConversionTest, | |
| 211 ::testing::Combine( | |
| 212 ::testing::Values("unicode_bmp"), | |
| 213 ::testing::Combine(::testing::ValuesIn(kUnicodeBMPValueTypes), | |
| 214 ::testing::ValuesIn(kUnicodeBMPValueTypes)))); | |
| 215 | |
| 216 INSTANTIATE_TEST_CASE_P( | |
| 217 SMPConversion, | |
| 218 VerifyNameMatchUnicodeConversionTest, | |
| 219 ::testing::Combine( | |
| 220 ::testing::Values("unicode_supplementary"), | |
| 221 ::testing::Combine( | |
| 222 ::testing::ValuesIn(kUnicodeSupplementaryValueTypes), | |
| 223 ::testing::ValuesIn(kUnicodeSupplementaryValueTypes)))); | |
| 224 | |
| 225 // Matching should fail if a PrintableString contains invalid characters. | |
| 226 TEST(VerifyNameMatchInvalidDataTest, FailOnInvalidPrintableStringChars) { | |
| 227 std::string der = LoadTestData("ascii", "PRINTABLESTRING", "unmangled"); | |
| 228 // Find a known location inside a PrintableString in the DER-encoded data. | |
| 229 size_t replace_location = der.find("0123456789"); | |
| 230 ASSERT_NE(std::string::npos, replace_location); | |
| 231 for (int c = 0; c < 256; ++c) { | |
| 232 SCOPED_TRACE(base::IntToString(c)); | |
| 233 if (IsAsciiAlpha(c) || IsAsciiDigit(c)) | |
| 234 continue; | |
| 235 switch (c) { | |
| 236 case ' ': | |
| 237 case '\'': | |
| 238 case '(': | |
| 239 case ')': | |
| 240 case '*': | |
| 241 case '+': | |
| 242 case ',': | |
| 243 case '-': | |
| 244 case '.': | |
| 245 case '/': | |
| 246 case ':': | |
| 247 case '=': | |
| 248 case '?': | |
| 249 continue; | |
| 250 } | |
| 251 der.replace(replace_location, 1, 1, c); | |
| 252 // Verification should fail due to the invalid character. | |
| 253 EXPECT_FALSE(VerifyNameMatch(InputFromString(der), InputFromString(der))); | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 // Matching should fail if an IA5String contains invalid characters. | |
| 258 TEST(VerifyNameMatchInvalidDataTest, FailOnInvalidIA5StringChars) { | |
| 259 std::string der = LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1"); | |
| 260 // Find a known location inside an IA5String in the DER-encoded data. | |
| 261 size_t replace_location = der.find("eXaMple"); | |
| 262 ASSERT_NE(std::string::npos, replace_location); | |
| 263 for (int c = 0; c < 256; ++c) { | |
| 264 SCOPED_TRACE(base::IntToString(c)); | |
| 265 der.replace(replace_location, 1, 1, c); | |
| 266 bool expected_result = (c <= 127); | |
| 267 EXPECT_EQ(expected_result, | |
| 268 VerifyNameMatch(InputFromString(der), InputFromString(der))); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueExtraData) { | |
| 273 std::string invalid = | |
| 274 LoadTestData("invalid", "AttributeTypeAndValue", "extradata"); | |
| 275 // Verification should fail due to extra element in AttributeTypeAndValue | |
| 276 // sequence. | |
| 277 EXPECT_FALSE( | |
| 278 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 279 } | |
| 280 | |
| 281 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueShort) { | |
| 282 std::string invalid = | |
| 283 LoadTestData("invalid", "AttributeTypeAndValue", "onlyOneElement"); | |
| 284 // Verification should fail due to AttributeTypeAndValue sequence having only | |
| 285 // one element. | |
| 286 EXPECT_FALSE( | |
| 287 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 288 } | |
| 289 | |
| 290 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueEmpty) { | |
| 291 std::string invalid = | |
| 292 LoadTestData("invalid", "AttributeTypeAndValue", "empty"); | |
| 293 // Verification should fail due to empty AttributeTypeAndValue sequence. | |
| 294 EXPECT_FALSE( | |
| 295 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 296 } | |
| 297 | |
| 298 TEST(VerifyNameMatchInvalidDataTest, FailOnBadAttributeType) { | |
| 299 std::string invalid = | |
| 300 LoadTestData("invalid", "AttributeTypeAndValue", "badAttributeType"); | |
| 301 // Verification should fail due to Attribute Type not being an OID. | |
| 302 EXPECT_FALSE( | |
| 303 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 304 } | |
| 305 | |
| 306 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueNotSequence) { | |
| 307 std::string invalid = | |
| 308 LoadTestData("invalid", "AttributeTypeAndValue", "setNotSequence"); | |
| 309 // Verification should fail due to AttributeTypeAndValue being a Set instead | |
| 310 // of a Sequence. | |
| 311 EXPECT_FALSE( | |
| 312 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 313 } | |
| 314 | |
| 315 TEST(VerifyNameMatchInvalidDataTest, FailOnRdnNotSet) { | |
| 316 std::string invalid = LoadTestData("invalid", "RDN", "sequenceInsteadOfSet"); | |
| 317 // Verification should fail due to RDN being a Sequence instead of a Set. | |
| 318 EXPECT_FALSE( | |
| 319 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 320 } | |
| 321 | |
| 322 TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyRdn) { | |
| 323 std::string invalid = LoadTestData("invalid", "RDN", "empty"); | |
| 324 // Verification should fail due to RDN having zero AttributeTypeAndValues. | |
| 325 EXPECT_FALSE( | |
| 326 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 327 } | |
| 328 | |
| 329 TEST(VerifyNameMatchInvalidDataTest, FailOnNameNotSequence) { | |
| 330 std::string invalid = LoadTestData("invalid", "Name", "setInsteadOfSequence"); | |
| 331 // Verification should fail due to Name being a Set instead of a Sequence. | |
| 332 EXPECT_FALSE( | |
| 333 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 334 } | |
| 335 | |
| 336 TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyName) { | |
| 337 std::string invalid = LoadTestData("invalid", "Name", "empty"); | |
| 338 // Verification should fail due to Name having zero RDNs. | |
| 339 EXPECT_FALSE( | |
| 340 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 341 } | |
| 342 | |
| 343 TEST(VerifyNameMatchInvalidDataTest, FailOnTrailingData) { | |
| 344 std::string invalid = | |
| 345 LoadTestData("ascii", "PRINTABLESTRING", "unmangled") + "X"; | |
| 346 // Verification should fail due to the appended character. | |
| 347 EXPECT_FALSE( | |
| 348 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 349 } | |
| 350 | |
| 351 // Matching should fail if a BMPString contains surrogates. | |
| 352 TEST(VerifyNameMatchInvalidDataTest, FailOnBmpStringSurrogates) { | |
| 353 std::string normal = LoadTestData("unicode_bmp", "BMPSTRING", "unmangled"); | |
| 354 // Find a known location inside a BMPSTRING in the DER-encoded data. | |
| 355 size_t replace_location = normal.find("\x67\x71\x4e\xac"); | |
| 356 ASSERT_NE(std::string::npos, replace_location); | |
| 357 // Replace with U+1D400 MATHEMATICAL BOLD CAPITAL A, which requires surrogates | |
| 358 // to represent. | |
| 359 std::string invalid = normal.replace(replace_location, 4, "\xd8\x35\xdc\x00"); | |
| 360 // Verification should fail due to the invalid codepoints. | |
| 361 EXPECT_FALSE( | |
| 362 VerifyNameMatch(InputFromString(invalid), InputFromString(invalid))); | |
| 363 } | |
| 364 | |
| 365 // Matching should succeed when the RDNs are sorted differently but are still | |
| 366 // equal after normalizing. | |
| 367 TEST(VerifyNameMatchRDNSorting, Simple) { | |
| 368 std::string a = LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_1"); | |
| 369 std::string b = LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_2"); | |
| 370 EXPECT_TRUE(VerifyNameMatch(InputFromString(a), InputFromString(b))); | |
| 371 EXPECT_TRUE(VerifyNameMatch(InputFromString(b), InputFromString(a))); | |
| 372 } | |
| 373 | |
| 374 // Matching should succeed when the RDNs are sorted differently but are still | |
| 375 // equal after normalizing, even in malformed RDNs that contain multiple | |
| 376 // elements with the same type. | |
| 377 TEST(VerifyNameMatchRDNSorting, DuplicateTypes) { | |
| 378 std::string a = LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1"); | |
| 379 std::string b = LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_2"); | |
| 380 EXPECT_TRUE(VerifyNameMatch(InputFromString(a), InputFromString(b))); | |
| 381 EXPECT_TRUE(VerifyNameMatch(InputFromString(b), InputFromString(a))); | |
| 21 } | 382 } |
| 22 | 383 |
| 23 } // namespace net | 384 } // namespace net |
| OLD | NEW |