Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/cert/internal/name_constraints.h" | |
| 6 | |
| 7 #include "net/cert/internal/test_helpers.h" | |
| 8 #include "net/der/parser.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace net { | |
| 12 namespace { | |
| 13 | |
| 14 der::Input SequenceValueFromString(const std::string* s) { | |
|
eroman
2015/08/26 19:56:44
This is the same function used in verify_name_matc
mattm
2015/08/29 01:37:19
Done.
| |
| 15 der::Parser parser(InputFromString(s)); | |
| 16 der::Input data; | |
| 17 if (!parser.ReadTag(der::kSequence, &data)) { | |
| 18 EXPECT_TRUE(false); | |
| 19 return der::Input(); | |
| 20 } | |
| 21 if (parser.HasMore()) { | |
| 22 EXPECT_TRUE(false); | |
| 23 return der::Input(); | |
| 24 } | |
| 25 return data; | |
| 26 } | |
| 27 | |
| 28 ::testing::AssertionResult LoadTestData(const std::string& token, | |
| 29 const std::string& basename, | |
| 30 std::string* result) { | |
| 31 std::string path = "net/data/name_constraints_unittest/name_constraints/" + | |
| 32 basename + ".pem"; | |
| 33 | |
| 34 const PemBlockMapping mappings[] = { | |
| 35 {token.c_str(), result}, | |
|
eroman
2015/08/26 19:56:44
This reads funny. Can you either change the |toke|
mattm
2015/08/29 01:37:19
Done.
| |
| 36 }; | |
| 37 | |
| 38 return ReadTestDataFromPemFile(path, mappings); | |
| 39 } | |
| 40 | |
| 41 ::testing::AssertionResult LoadTestName(const std::string& basename, | |
| 42 std::string* result) { | |
| 43 return LoadTestData("NAME", basename, result); | |
| 44 } | |
| 45 | |
| 46 ::testing::AssertionResult LoadTestNameConstraint(const std::string& basename, | |
| 47 std::string* result) { | |
| 48 return LoadTestData("NAME CONSTRAINTS", basename, result); | |
| 49 } | |
| 50 | |
| 51 ::testing::AssertionResult LoadTestSubjectAltName(const std::string& basename, | |
| 52 std::string* result) { | |
| 53 return LoadTestData("SUBJECT ALTERNATIVE NAME", basename, result); | |
| 54 } | |
| 55 | |
| 56 const int kOtherNameFlag = 1 << 0; | |
| 57 const int kRfc822NameFlag = 1 << 1; | |
| 58 const int kDnsNameFlag = 1 << 2; | |
| 59 const int kX400AddressFlag = 1 << 3; | |
| 60 const int kDirectoryNameFlag = 1 << 4; | |
| 61 const int kEdiPartyNameFlag = 1 << 5; | |
| 62 const int kUniformResourceIdentifierFlag = 1 << 6; | |
| 63 const int kIpAddressFlag = 1 << 7; | |
| 64 const int kRegisteredIdFlag = 1 << 8; | |
| 65 const int kAllFlags = (1 << 9) - 1; | |
| 66 ::testing::AssertionResult CheckNotPresent( | |
|
eroman
2015/08/26 19:56:44
nit: newline
mattm
2015/08/29 01:37:19
Done.
| |
| 67 const NameConstraints& name_constraints, | |
| 68 int flag) { | |
| 69 if (flag & kOtherNameFlag && !name_constraints.IsPermittedOtherName()) | |
| 70 return ::testing::AssertionFailure() << "contained otherNames"; | |
| 71 if (flag & kRfc822NameFlag && !name_constraints.IsPermittedRFC822Name()) | |
| 72 return ::testing::AssertionFailure() << "contained rfc822Names"; | |
| 73 if (flag & kDnsNameFlag) { | |
| 74 if (!name_constraints.IsPermittedDNSName("a") || | |
| 75 !name_constraints.IsPermittedDNSName("b")) | |
| 76 return ::testing::AssertionFailure() << "contained dNSNames"; | |
| 77 } | |
| 78 if (flag & kX400AddressFlag && !name_constraints.IsPermittedX400Address()) | |
| 79 return ::testing::AssertionFailure() << "contained x400Addresses"; | |
| 80 if (flag & kDirectoryNameFlag) { | |
| 81 std::string name_us; | |
| 82 ::testing::AssertionResult r(::testing::AssertionFailure()); | |
| 83 if (!(r = LoadTestName("name-us", &name_us))) | |
| 84 return r; | |
| 85 std::string name_jp; | |
| 86 if (!(r = LoadTestName("name-jp", &name_jp))) | |
| 87 return r; | |
| 88 std::string name_de; | |
| 89 if (!(r = LoadTestName("name-de", &name_de))) | |
| 90 return r; | |
| 91 std::string name_ca; | |
| 92 if (!(r = LoadTestName("name-ca", &name_ca))) | |
| 93 return r; | |
| 94 if (!name_constraints.IsPermittedDirectoryName( | |
| 95 SequenceValueFromString(&name_us)) || | |
| 96 !name_constraints.IsPermittedDirectoryName( | |
| 97 SequenceValueFromString(&name_jp)) || | |
| 98 !name_constraints.IsPermittedDirectoryName( | |
| 99 SequenceValueFromString(&name_de)) || | |
| 100 !name_constraints.IsPermittedDirectoryName( | |
| 101 SequenceValueFromString(&name_ca))) { | |
| 102 return ::testing::AssertionFailure() << "contained directoryNames"; | |
| 103 } | |
| 104 } | |
| 105 if (flag & kEdiPartyNameFlag && !name_constraints.IsPermittedEdiPartyName()) | |
| 106 return ::testing::AssertionFailure() << "contained ediPartyNames"; | |
| 107 if (flag & kUniformResourceIdentifierFlag && | |
| 108 !name_constraints.IsPermittedURI()) { | |
| 109 return ::testing::AssertionFailure() | |
| 110 << "contained uniformResourceIdentifiers"; | |
| 111 } | |
| 112 if (flag & kIpAddressFlag) { | |
| 113 const uint8_t ip4a[] = {192, 168, 0, 1}; | |
| 114 const uint8_t ip4b[] = {255, 255, 255, 255}; | |
| 115 // clang-format off | |
| 116 const uint8_t ip6a[] = {1, 2, 3, 4, 5, 6, 7, 8, | |
| 117 9, 10, 11, 12, 13, 14, 15, 16}; | |
| 118 const uint8_t ip6b[] = {255, 255, 255, 255, 255, 255, 255, 255, | |
| 119 255, 255, 255, 255, 255, 255, 255, 255}; | |
| 120 // clang-format on | |
| 121 if (!name_constraints.IsPermittedIP( | |
| 122 IPAddressNumber(ip4a, ip4a + arraysize(ip4a))) || | |
| 123 !name_constraints.IsPermittedIP( | |
| 124 IPAddressNumber(ip4b, ip4b + arraysize(ip4b))) || | |
| 125 !name_constraints.IsPermittedIP( | |
| 126 IPAddressNumber(ip6a, ip6a + arraysize(ip6a))) || | |
| 127 !name_constraints.IsPermittedIP( | |
| 128 IPAddressNumber(ip6b, ip6b + arraysize(ip6b)))) { | |
| 129 return ::testing::AssertionFailure() << "contained iPAddresses"; | |
| 130 } | |
| 131 } | |
| 132 if (flag & kRegisteredIdFlag && !name_constraints.IsPermittedRegisteredId()) | |
| 133 return ::testing::AssertionFailure() << "contained registeredIDs"; | |
| 134 | |
| 135 return ::testing::AssertionSuccess(); | |
| 136 } | |
| 137 | |
| 138 } // namespace | |
| 139 | |
| 140 class ParseNameConstraints | |
| 141 : public ::testing::TestWithParam<::testing::tuple<bool>> { | |
| 142 public: | |
| 143 bool is_critical() const { return ::testing::get<0>(GetParam()); } | |
| 144 }; | |
| 145 | |
| 146 // Run the tests with the name constraints marked critical and non-critical. For | |
| 147 // supported name types, the results should be the same for both. | |
| 148 INSTANTIATE_TEST_CASE_P(InstantiationName, | |
| 149 ParseNameConstraints, | |
| 150 ::testing::Values(true, false)); | |
| 151 | |
| 152 TEST_P(ParseNameConstraints, DNSNames) { | |
| 153 std::string a; | |
| 154 ASSERT_TRUE(LoadTestNameConstraint("dnsname", &a)); | |
| 155 | |
| 156 NameConstraints name_constraints; | |
| 157 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 158 | |
| 159 EXPECT_TRUE(name_constraints.IsPermittedDNSName("permitted.example.com")); | |
| 160 EXPECT_TRUE(name_constraints.IsPermittedDNSName("permitted.example.com.")); | |
| 161 EXPECT_TRUE(name_constraints.IsPermittedDNSName("a.permitted.example.com")); | |
| 162 EXPECT_FALSE(name_constraints.IsPermittedDNSName("apermitted.example.com")); | |
| 163 EXPECT_FALSE(name_constraints.IsPermittedDNSName("apermitted.example.com.")); | |
| 164 EXPECT_TRUE(name_constraints.IsPermittedDNSName("alsopermitted.example.com")); | |
| 165 EXPECT_FALSE( | |
| 166 name_constraints.IsPermittedDNSName("excluded.permitted.example.com")); | |
| 167 EXPECT_FALSE( | |
| 168 name_constraints.IsPermittedDNSName("a.excluded.permitted.example.com")); | |
| 169 EXPECT_FALSE(name_constraints.IsPermittedDNSName( | |
| 170 "stillnotpermitted.excluded.permitted.example.com")); | |
| 171 EXPECT_FALSE(name_constraints.IsPermittedDNSName( | |
| 172 "a.stillnotpermitted.excluded.permitted.example.com")); | |
| 173 EXPECT_FALSE( | |
| 174 name_constraints.IsPermittedDNSName("extraneousexclusion.example.com")); | |
| 175 EXPECT_FALSE( | |
| 176 name_constraints.IsPermittedDNSName("a.extraneousexclusion.example.com")); | |
| 177 EXPECT_FALSE(name_constraints.IsPermittedDNSName("other.example.com")); | |
| 178 EXPECT_FALSE(name_constraints.IsPermittedDNSName("other.com")); | |
| 179 | |
| 180 // Wildcard names: | |
| 181 // Pattern could match excluded.permitted.example.com, thus should not be | |
| 182 // allowed. | |
| 183 EXPECT_FALSE(name_constraints.IsPermittedDNSName("*.permitted.example.com")); | |
| 184 // Entirely within excluded name, obviously not allowed. | |
| 185 EXPECT_FALSE( | |
| 186 name_constraints.IsPermittedDNSName("*.excluded.permitted.example.com")); | |
| 187 // Within permitted.example.com and cannot match any exclusion, thus these are | |
| 188 // allowed. | |
| 189 EXPECT_TRUE( | |
| 190 name_constraints.IsPermittedDNSName("*.foo.permitted.example.com")); | |
| 191 EXPECT_TRUE( | |
| 192 name_constraints.IsPermittedDNSName("*.alsopermitted.example.com")); | |
| 193 // Matches permitted.example2.com, but also matches other .example2.com names | |
| 194 // which are not in either permitted or excluded, so not allowed. | |
| 195 EXPECT_FALSE(name_constraints.IsPermittedDNSName("*.example2.com")); | |
| 196 // Partial wildcards are not supported, so these name are permitted even if | |
| 197 // it seems like they shouldn't be. It's fine, since certificate verification | |
| 198 // won't treat them as wildcard names either. | |
| 199 EXPECT_TRUE( | |
| 200 name_constraints.IsPermittedDNSName("*xcluded.permitted.example.com")); | |
| 201 EXPECT_TRUE( | |
| 202 name_constraints.IsPermittedDNSName("exclude*.permitted.example.com")); | |
| 203 EXPECT_TRUE( | |
| 204 name_constraints.IsPermittedDNSName("excl*ded.permitted.example.com")); | |
| 205 // Garbage wildcard data. | |
| 206 EXPECT_FALSE(name_constraints.IsPermittedDNSName("*.")); | |
| 207 EXPECT_FALSE(name_constraints.IsPermittedDNSName("*.*")); | |
| 208 EXPECT_FALSE(name_constraints.IsPermittedDNSName(".*")); | |
| 209 EXPECT_FALSE(name_constraints.IsPermittedDNSName("*")); | |
| 210 // Matches SAN with trailing dot. | |
| 211 EXPECT_TRUE(name_constraints.IsPermittedDNSName("permitted.example3.com")); | |
| 212 EXPECT_TRUE(name_constraints.IsPermittedDNSName("permitted.example3.com.")); | |
| 213 EXPECT_TRUE(name_constraints.IsPermittedDNSName("a.permitted.example3.com")); | |
| 214 EXPECT_TRUE(name_constraints.IsPermittedDNSName("a.permitted.example3.com.")); | |
| 215 | |
| 216 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags & ~kDnsNameFlag)); | |
| 217 | |
| 218 std::string san; | |
| 219 ASSERT_TRUE(LoadTestSubjectAltName("san-permitted", &san)); | |
| 220 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 221 InputFromString(&san), true)); | |
| 222 | |
| 223 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-dnsname", &san)); | |
| 224 EXPECT_FALSE(name_constraints.IsPermittedCert(der::Input(), | |
| 225 InputFromString(&san), true)); | |
| 226 | |
| 227 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-directoryname", &san)); | |
| 228 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 229 InputFromString(&san), true)); | |
| 230 | |
| 231 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-ipaddress", &san)); | |
| 232 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 233 InputFromString(&san), true)); | |
| 234 } | |
| 235 | |
| 236 TEST_P(ParseNameConstraints, DNSNames_ExcludeOnly) { | |
|
eroman
2015/08/26 19:56:44
Underscores in Gtest names are discouraged/banned:
mattm
2015/08/29 01:37:19
Done.
| |
| 237 std::string a; | |
| 238 ASSERT_TRUE(LoadTestNameConstraint("dnsname-excluded", &a)); | |
| 239 | |
| 240 NameConstraints name_constraints; | |
| 241 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 242 | |
| 243 // Only "excluded.permitted.example.com" is excluded, but since no dNSNames | |
| 244 // are permitted, everything is excluded. | |
| 245 EXPECT_FALSE(name_constraints.IsPermittedDNSName("")); | |
| 246 EXPECT_FALSE(name_constraints.IsPermittedDNSName("foo.com")); | |
| 247 EXPECT_FALSE(name_constraints.IsPermittedDNSName("permitted.example.com")); | |
| 248 EXPECT_FALSE( | |
| 249 name_constraints.IsPermittedDNSName("excluded.permitted.example.com")); | |
| 250 EXPECT_FALSE( | |
| 251 name_constraints.IsPermittedDNSName("a.excluded.permitted.example.com")); | |
| 252 } | |
| 253 | |
| 254 TEST_P(ParseNameConstraints, DNSNames_ExcludeAll) { | |
| 255 std::string a; | |
| 256 ASSERT_TRUE(LoadTestNameConstraint("dnsname-excludeall", &a)); | |
| 257 | |
| 258 NameConstraints name_constraints; | |
| 259 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 260 | |
| 261 // "permitted.example.com" is in the permitted section, but since "" is | |
| 262 // excluded, nothing is permitted. | |
| 263 EXPECT_FALSE(name_constraints.IsPermittedDNSName("")); | |
| 264 EXPECT_FALSE(name_constraints.IsPermittedDNSName("foo.com")); | |
| 265 EXPECT_FALSE(name_constraints.IsPermittedDNSName("permitted.example.com")); | |
| 266 EXPECT_FALSE( | |
| 267 name_constraints.IsPermittedDNSName("foo.permitted.example.com")); | |
| 268 } | |
| 269 | |
| 270 TEST_P(ParseNameConstraints, DirectoryNames) { | |
| 271 std::string constraints_der; | |
| 272 ASSERT_TRUE(LoadTestNameConstraint("directoryname", &constraints_der)); | |
|
eroman
2015/08/26 19:56:44
nit: I find it more readable to include the ".pem"
mattm
2015/08/29 01:37:19
Done.
| |
| 273 | |
| 274 std::string name_us; | |
| 275 ASSERT_TRUE(LoadTestName("name-us", &name_us)); | |
| 276 std::string name_us_ca; | |
| 277 ASSERT_TRUE(LoadTestName("name-us-california", &name_us_ca)); | |
| 278 std::string name_us_ca_mountain_view; | |
| 279 ASSERT_TRUE(LoadTestName("name-us-california-mountain_view", | |
| 280 &name_us_ca_mountain_view)); | |
| 281 std::string name_us_az; | |
| 282 ASSERT_TRUE(LoadTestName("name-us-arizona", &name_us_az)); | |
| 283 std::string name_jp; | |
| 284 ASSERT_TRUE(LoadTestName("name-jp", &name_jp)); | |
| 285 std::string name_jp_tokyo; | |
| 286 ASSERT_TRUE(LoadTestName("name-jp-tokyo", &name_jp_tokyo)); | |
| 287 std::string name_de; | |
| 288 ASSERT_TRUE(LoadTestName("name-de", &name_de)); | |
| 289 std::string name_ca; | |
| 290 ASSERT_TRUE(LoadTestName("name-ca", &name_ca)); | |
| 291 | |
| 292 NameConstraints name_constraints; | |
| 293 EXPECT_TRUE( | |
| 294 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 295 | |
| 296 // Not in any permitted subtree. | |
| 297 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 298 SequenceValueFromString(&name_ca))); | |
| 299 // Within the permitted C=US subtree. | |
| 300 EXPECT_TRUE(name_constraints.IsPermittedDirectoryName( | |
| 301 SequenceValueFromString(&name_us))); | |
| 302 // Within the permitted C=US subtree. | |
| 303 EXPECT_TRUE(name_constraints.IsPermittedDirectoryName( | |
| 304 SequenceValueFromString(&name_us_az))); | |
| 305 // Within the permitted C=US subtree, however the excluded C=US,ST=California | |
| 306 // subtree takes priority. | |
| 307 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 308 SequenceValueFromString(&name_us_ca))); | |
| 309 // Within the permitted C=US subtree as well as the permitted | |
| 310 // C=US,ST=California,L=Mountain View subtree, however the excluded | |
| 311 // C=US,ST=California subtree still takes priority. | |
| 312 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 313 SequenceValueFromString(&name_us_ca_mountain_view))); | |
| 314 // Not in any permitted subtree, and also inside the extraneous excluded C=DE | |
| 315 // subtree. | |
| 316 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 317 SequenceValueFromString(&name_de))); | |
| 318 // Not in any permitted subtree. | |
| 319 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 320 SequenceValueFromString(&name_jp))); | |
| 321 // Within the permitted C=JP,ST=Tokyo subtree. | |
| 322 EXPECT_TRUE(name_constraints.IsPermittedDirectoryName( | |
| 323 SequenceValueFromString(&name_jp_tokyo))); | |
| 324 | |
| 325 EXPECT_TRUE( | |
| 326 CheckNotPresent(name_constraints, kAllFlags & ~kDirectoryNameFlag)); | |
| 327 | |
| 328 // Within the permitted C=US subtree. | |
| 329 EXPECT_TRUE( | |
| 330 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us), | |
| 331 der::Input(), false /* is_leaf_cert */)); | |
| 332 EXPECT_TRUE( | |
| 333 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us), | |
| 334 der::Input(), true /* is_leaf_cert */)); | |
| 335 // Within the permitted C=US subtree, however the excluded C=US,ST=California | |
| 336 // subtree takes priority. | |
| 337 EXPECT_FALSE( | |
| 338 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_ca), | |
| 339 der::Input(), false /* is_leaf_cert */)); | |
| 340 EXPECT_FALSE( | |
| 341 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_ca), | |
| 342 der::Input(), true /* is_leaf_cert */)); | |
| 343 | |
| 344 std::string san; | |
| 345 ASSERT_TRUE(LoadTestSubjectAltName("san-permitted", &san)); | |
| 346 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 347 InputFromString(&san), true)); | |
| 348 | |
| 349 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-dnsname", &san)); | |
| 350 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 351 InputFromString(&san), true)); | |
| 352 | |
| 353 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-directoryname", &san)); | |
| 354 EXPECT_FALSE(name_constraints.IsPermittedCert(der::Input(), | |
| 355 InputFromString(&san), true)); | |
| 356 | |
| 357 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-ipaddress", &san)); | |
| 358 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 359 InputFromString(&san), true)); | |
| 360 } | |
| 361 | |
| 362 TEST_P(ParseNameConstraints, DirectoryNames_ExcludeOnly) { | |
| 363 std::string constraints_der; | |
| 364 ASSERT_TRUE( | |
| 365 LoadTestNameConstraint("directoryname-excluded", &constraints_der)); | |
| 366 NameConstraints name_constraints; | |
| 367 EXPECT_TRUE( | |
| 368 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 369 | |
| 370 std::string name_empty; | |
| 371 ASSERT_TRUE(LoadTestName("name-empty", &name_empty)); | |
| 372 std::string name_us; | |
| 373 ASSERT_TRUE(LoadTestName("name-us", &name_us)); | |
| 374 std::string name_us_ca; | |
| 375 ASSERT_TRUE(LoadTestName("name-us-california", &name_us_ca)); | |
| 376 std::string name_us_ca_mountain_view; | |
| 377 ASSERT_TRUE(LoadTestName("name-us-california-mountain_view", | |
| 378 &name_us_ca_mountain_view)); | |
| 379 | |
| 380 // Only "C=US,ST=California" is excluded, but since no directoryNames are | |
| 381 // permitted, everything is excluded. | |
| 382 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 383 SequenceValueFromString(&name_empty))); | |
| 384 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 385 SequenceValueFromString(&name_us))); | |
| 386 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 387 SequenceValueFromString(&name_us_ca))); | |
| 388 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 389 SequenceValueFromString(&name_us_ca_mountain_view))); | |
| 390 } | |
| 391 | |
| 392 TEST_P(ParseNameConstraints, DirectoryNames_ExcludeAll) { | |
| 393 std::string constraints_der; | |
| 394 ASSERT_TRUE( | |
| 395 LoadTestNameConstraint("directoryname-excluded", &constraints_der)); | |
| 396 NameConstraints name_constraints; | |
| 397 EXPECT_TRUE( | |
| 398 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 399 | |
| 400 std::string name_empty; | |
| 401 ASSERT_TRUE(LoadTestName("name-empty", &name_empty)); | |
| 402 std::string name_us; | |
| 403 ASSERT_TRUE(LoadTestName("name-us", &name_us)); | |
| 404 std::string name_us_ca; | |
| 405 ASSERT_TRUE(LoadTestName("name-us-california", &name_us_ca)); | |
| 406 std::string name_us_ca_mountain_view; | |
| 407 ASSERT_TRUE(LoadTestName("name-us-california-mountain_view", | |
| 408 &name_us_ca_mountain_view)); | |
| 409 std::string name_jp; | |
| 410 ASSERT_TRUE(LoadTestName("name-jp", &name_jp)); | |
| 411 | |
| 412 // "C=US" is in the permitted section, but since an empty | |
| 413 // directoryName is excluded, nothing is permitted. | |
| 414 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 415 SequenceValueFromString(&name_empty))); | |
| 416 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 417 SequenceValueFromString(&name_us))); | |
| 418 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 419 SequenceValueFromString(&name_us_ca))); | |
| 420 EXPECT_FALSE(name_constraints.IsPermittedDirectoryName( | |
| 421 SequenceValueFromString(&name_jp))); | |
| 422 } | |
| 423 | |
| 424 TEST_P(ParseNameConstraints, IPAdresses) { | |
| 425 std::string a; | |
| 426 ASSERT_TRUE(LoadTestNameConstraint("ipaddress", &a)); | |
| 427 | |
| 428 NameConstraints name_constraints; | |
| 429 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 430 | |
| 431 // IPv4 tests: | |
| 432 { | |
| 433 // Not in any permitted range. | |
| 434 const uint8_t ip4[] = {192, 169, 0, 1}; | |
| 435 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 436 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 437 } | |
| 438 { | |
| 439 // Within the permitted 192.168.0.0/255.255.0.0 range. | |
| 440 const uint8_t ip4[] = {192, 168, 0, 1}; | |
| 441 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 442 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 443 } | |
| 444 { | |
| 445 // Within the permitted 192.168.0.0/255.255.0.0 range, however the | |
| 446 // excluded 192.168.5.0/255.255.255.0 takes priority. | |
| 447 const uint8_t ip4[] = {192, 168, 5, 1}; | |
| 448 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 449 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 450 } | |
| 451 { | |
| 452 // Within the permitted 192.168.0.0/255.255.0.0 range as well as the | |
| 453 // permitted 192.168.5.32/255.255.255.96 range, however the excluded | |
| 454 // 192.168.5.0/255.255.255.0 still takes priority. | |
| 455 const uint8_t ip4[] = {192, 168, 5, 33}; | |
| 456 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 457 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 458 } | |
| 459 { | |
| 460 // Not in any permitted range. (Just outside the 192.167.5.32/255.255.255.96 | |
| 461 // range.) | |
| 462 const uint8_t ip4[] = {192, 167, 5, 31}; | |
| 463 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 464 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 465 } | |
| 466 { | |
| 467 // Within the permitted 192.167.5.32/255.255.255.96 range. | |
| 468 const uint8_t ip4[] = {192, 167, 5, 32}; | |
| 469 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 470 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 471 } | |
| 472 { | |
| 473 // Within the permitted 192.167.5.32/255.255.255.96 range. | |
| 474 const uint8_t ip4[] = {192, 167, 5, 63}; | |
| 475 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 476 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 477 } | |
| 478 { | |
| 479 // Not in any permitted range. (Just outside the 192.167.5.32/255.255.255.96 | |
| 480 // range.) | |
| 481 const uint8_t ip4[] = {192, 167, 5, 64}; | |
| 482 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 483 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 484 } | |
| 485 { | |
| 486 // Not in any permitted range, and also inside the extraneous excluded | |
| 487 // 192.166.5.32/255.255.255.96 range. | |
| 488 const uint8_t ip4[] = {192, 166, 5, 32}; | |
| 489 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 490 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 491 } | |
| 492 | |
| 493 // IPv6 tests: | |
| 494 { | |
| 495 // Not in any permitted range. | |
| 496 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 0, 0, 0, 1}; | |
| 497 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 498 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 499 } | |
| 500 { | |
| 501 // Within the permitted | |
| 502 // 102:304:506:708:90a:b0c::/ffff:ffff:ffff:ffff:ffff:ffff:: range. | |
| 503 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 1}; | |
| 504 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 505 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 506 } | |
| 507 { | |
| 508 // Within the permitted | |
| 509 // 102:304:506:708:90a:b0c::/ffff:ffff:ffff:ffff:ffff:ffff:: range, however | |
| 510 // the excluded | |
| 511 // 102:304:506:708:90a:b0c:500:0/ffff:ffff:ffff:ffff:ffff:ffff:ff00:0 takes | |
| 512 // priority. | |
| 513 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 5, 0, 0, 1}; | |
| 514 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 515 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 516 } | |
| 517 { | |
| 518 // Within the permitted | |
| 519 // 102:304:506:708:90a:b0c::/ffff:ffff:ffff:ffff:ffff:ffff:: range as well | |
| 520 // as the permitted | |
| 521 // 102:304:506:708:90a:b0c:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0, | |
| 522 // however the excluded | |
| 523 // 102:304:506:708:90a:b0c:500:0/ffff:ffff:ffff:ffff:ffff:ffff:ff00:0 takes | |
| 524 // priority. | |
| 525 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 5, 33, 0, 1}; | |
| 526 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 527 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 528 } | |
| 529 { | |
| 530 // Not in any permitted range. (Just outside the | |
| 531 // 102:304:506:708:90a:b0b:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0 | |
| 532 // range.) | |
| 533 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, | |
| 534 9, 10, 11, 11, 5, 31, 255, 255}; | |
| 535 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 536 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 537 } | |
| 538 { | |
| 539 // Within the permitted | |
| 540 // 102:304:506:708:90a:b0b:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0 range. | |
| 541 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 5, 32, 0, 0}; | |
| 542 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 543 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 544 } | |
| 545 { | |
| 546 // Within the permitted | |
| 547 // 102:304:506:708:90a:b0b:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0 range. | |
| 548 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, | |
| 549 9, 10, 11, 11, 5, 63, 255, 255}; | |
| 550 EXPECT_TRUE(name_constraints.IsPermittedIP( | |
| 551 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 552 } | |
| 553 { | |
| 554 // Not in any permitted range. (Just outside the | |
| 555 // 102:304:506:708:90a:b0b:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0 | |
| 556 // range.) | |
| 557 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 5, 64, 0, 0}; | |
| 558 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 559 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 560 } | |
| 561 { | |
| 562 // Not in any permitted range, and also inside the extraneous excluded | |
| 563 // 102:304:506:708:90a:b0a:520:0/ffff:ffff:ffff:ffff:ffff:ffff:ff60:0 range. | |
| 564 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 5, 33, 0, 1}; | |
| 565 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 566 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 567 } | |
| 568 | |
| 569 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags & ~kIpAddressFlag)); | |
| 570 | |
| 571 std::string san; | |
| 572 ASSERT_TRUE(LoadTestSubjectAltName("san-permitted", &san)); | |
| 573 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 574 InputFromString(&san), true)); | |
| 575 | |
| 576 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-dnsname", &san)); | |
| 577 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 578 InputFromString(&san), true)); | |
| 579 | |
| 580 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-directoryname", &san)); | |
| 581 EXPECT_TRUE(name_constraints.IsPermittedCert(der::Input(), | |
| 582 InputFromString(&san), true)); | |
| 583 | |
| 584 ASSERT_TRUE(LoadTestSubjectAltName("san-excluded-ipaddress", &san)); | |
| 585 EXPECT_FALSE(name_constraints.IsPermittedCert(der::Input(), | |
| 586 InputFromString(&san), true)); | |
| 587 } | |
| 588 | |
| 589 TEST_P(ParseNameConstraints, IPAdresses_ExcludeOnly) { | |
| 590 std::string a; | |
| 591 ASSERT_TRUE(LoadTestNameConstraint("ipaddress-excluded", &a)); | |
| 592 | |
| 593 NameConstraints name_constraints; | |
| 594 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 595 | |
| 596 // Only 192.168.5.0/255.255.255.0 is excluded, but since no iPAddresses | |
| 597 // are permitted, everything is excluded. | |
| 598 { | |
| 599 const uint8_t ip4[] = {192, 168, 0, 1}; | |
| 600 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 601 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 602 } | |
| 603 { | |
| 604 const uint8_t ip4[] = {192, 168, 5, 1}; | |
| 605 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 606 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 607 } | |
| 608 { | |
| 609 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 0, 0, 0, 1}; | |
| 610 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 611 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 TEST_P(ParseNameConstraints, IPAdresses_ExcludeAll) { | |
| 616 std::string a; | |
| 617 ASSERT_TRUE(LoadTestNameConstraint("ipaddress-excludeall", &a)); | |
| 618 | |
| 619 NameConstraints name_constraints; | |
| 620 EXPECT_TRUE(name_constraints.Parse(InputFromString(&a), is_critical())); | |
| 621 | |
| 622 // 192.168.0.0/255.255.0.0 and | |
| 623 // 102:304:506:708:90a:b0c::/ffff:ffff:ffff:ffff:ffff:ffff:: are permitted, | |
| 624 // but since 0.0.0.0/0 and ::/0 are excluded nothing is permitted. | |
| 625 { | |
| 626 const uint8_t ip4[] = {192, 168, 0, 1}; | |
| 627 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 628 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 629 } | |
| 630 { | |
| 631 const uint8_t ip4[] = {1, 1, 1, 1}; | |
| 632 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 633 IPAddressNumber(ip4, ip4 + arraysize(ip4)))); | |
| 634 } | |
| 635 { | |
| 636 const uint8_t ip6[] = {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; | |
| 637 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 638 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 639 } | |
| 640 { | |
| 641 const uint8_t ip6[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 0, 0, 0, 1}; | |
| 642 EXPECT_FALSE(name_constraints.IsPermittedIP( | |
| 643 IPAddressNumber(ip6, ip6 + arraysize(ip6)))); | |
| 644 } | |
| 645 } | |
| 646 | |
| 647 TEST_P(ParseNameConstraints, otherNamesInPermitted) { | |
| 648 std::string constraints_der; | |
| 649 ASSERT_TRUE(LoadTestNameConstraint("othername-permitted", &constraints_der)); | |
| 650 NameConstraints name_constraints; | |
| 651 EXPECT_TRUE( | |
| 652 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 653 | |
| 654 if (is_critical()) { | |
| 655 EXPECT_FALSE(name_constraints.IsPermittedOtherName()); | |
| 656 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags & ~kOtherNameFlag)); | |
| 657 } else { | |
| 658 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 659 } | |
| 660 } | |
| 661 | |
| 662 TEST_P(ParseNameConstraints, otherNamesInExcluded) { | |
| 663 std::string constraints_der; | |
| 664 ASSERT_TRUE(LoadTestNameConstraint("othername-excluded", &constraints_der)); | |
| 665 NameConstraints name_constraints; | |
| 666 EXPECT_TRUE( | |
| 667 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 668 | |
| 669 if (is_critical()) { | |
| 670 EXPECT_FALSE(name_constraints.IsPermittedOtherName()); | |
| 671 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags & ~kOtherNameFlag)); | |
| 672 } else { | |
| 673 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 674 } | |
| 675 } | |
| 676 | |
| 677 TEST_P(ParseNameConstraints, Rfc822NamesInPermitted) { | |
| 678 std::string constraints_der; | |
| 679 ASSERT_TRUE(LoadTestNameConstraint("rfc822name-permitted", &constraints_der)); | |
| 680 NameConstraints name_constraints; | |
| 681 EXPECT_TRUE( | |
| 682 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 683 | |
| 684 if (is_critical()) { | |
| 685 EXPECT_FALSE(name_constraints.IsPermittedRFC822Name()); | |
| 686 EXPECT_TRUE( | |
| 687 CheckNotPresent(name_constraints, kAllFlags & ~kRfc822NameFlag)); | |
| 688 } else { | |
| 689 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 690 } | |
| 691 } | |
| 692 | |
| 693 TEST_P(ParseNameConstraints, Rfc822NamesInExcluded) { | |
| 694 std::string constraints_der; | |
| 695 ASSERT_TRUE(LoadTestNameConstraint("rfc822name-excluded", &constraints_der)); | |
| 696 NameConstraints name_constraints; | |
| 697 EXPECT_TRUE( | |
| 698 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 699 | |
| 700 if (is_critical()) { | |
| 701 EXPECT_FALSE(name_constraints.IsPermittedRFC822Name()); | |
| 702 EXPECT_TRUE( | |
| 703 CheckNotPresent(name_constraints, kAllFlags & ~kRfc822NameFlag)); | |
| 704 } else { | |
| 705 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 706 } | |
| 707 } | |
| 708 | |
| 709 TEST_P(ParseNameConstraints, x400AddresssInPermitted) { | |
| 710 std::string constraints_der; | |
| 711 ASSERT_TRUE( | |
| 712 LoadTestNameConstraint("x400address-permitted", &constraints_der)); | |
| 713 NameConstraints name_constraints; | |
| 714 EXPECT_TRUE( | |
| 715 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 716 | |
| 717 if (is_critical()) { | |
| 718 EXPECT_FALSE(name_constraints.IsPermittedX400Address()); | |
| 719 EXPECT_TRUE( | |
| 720 CheckNotPresent(name_constraints, kAllFlags & ~kX400AddressFlag)); | |
| 721 } else { | |
| 722 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 723 } | |
| 724 } | |
| 725 | |
| 726 TEST_P(ParseNameConstraints, x400AddresssInExcluded) { | |
| 727 std::string constraints_der; | |
| 728 ASSERT_TRUE(LoadTestNameConstraint("x400address-excluded", &constraints_der)); | |
| 729 NameConstraints name_constraints; | |
| 730 EXPECT_TRUE( | |
| 731 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 732 | |
| 733 if (is_critical()) { | |
| 734 EXPECT_FALSE(name_constraints.IsPermittedX400Address()); | |
| 735 EXPECT_TRUE( | |
| 736 CheckNotPresent(name_constraints, kAllFlags & ~kX400AddressFlag)); | |
| 737 } else { | |
| 738 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 739 } | |
| 740 } | |
| 741 | |
| 742 TEST_P(ParseNameConstraints, ediPartyNamesInPermitted) { | |
| 743 std::string constraints_der; | |
| 744 ASSERT_TRUE( | |
| 745 LoadTestNameConstraint("edipartyname-permitted", &constraints_der)); | |
| 746 NameConstraints name_constraints; | |
| 747 EXPECT_TRUE( | |
| 748 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 749 | |
| 750 if (is_critical()) { | |
| 751 EXPECT_FALSE(name_constraints.IsPermittedEdiPartyName()); | |
| 752 EXPECT_TRUE( | |
| 753 CheckNotPresent(name_constraints, kAllFlags & ~kEdiPartyNameFlag)); | |
| 754 } else { | |
| 755 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 756 } | |
| 757 } | |
| 758 | |
| 759 TEST_P(ParseNameConstraints, ediPartyNamesInExcluded) { | |
| 760 std::string constraints_der; | |
| 761 ASSERT_TRUE( | |
| 762 LoadTestNameConstraint("edipartyname-excluded", &constraints_der)); | |
| 763 NameConstraints name_constraints; | |
| 764 EXPECT_TRUE( | |
| 765 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 766 | |
| 767 if (is_critical()) { | |
| 768 EXPECT_FALSE(name_constraints.IsPermittedEdiPartyName()); | |
| 769 EXPECT_TRUE( | |
| 770 CheckNotPresent(name_constraints, kAllFlags & ~kEdiPartyNameFlag)); | |
| 771 } else { | |
| 772 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 773 } | |
| 774 } | |
| 775 | |
| 776 TEST_P(ParseNameConstraints, registeredIDsInPermitted) { | |
| 777 std::string constraints_der; | |
| 778 ASSERT_TRUE( | |
| 779 LoadTestNameConstraint("registeredid-permitted", &constraints_der)); | |
| 780 NameConstraints name_constraints; | |
| 781 EXPECT_TRUE( | |
| 782 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 783 | |
| 784 if (is_critical()) { | |
| 785 EXPECT_FALSE(name_constraints.IsPermittedRegisteredId()); | |
| 786 EXPECT_TRUE( | |
| 787 CheckNotPresent(name_constraints, kAllFlags & ~kRegisteredIdFlag)); | |
| 788 } else { | |
| 789 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 790 } | |
| 791 } | |
| 792 | |
| 793 TEST_P(ParseNameConstraints, registeredIDsInExcluded) { | |
| 794 std::string constraints_der; | |
| 795 ASSERT_TRUE( | |
| 796 LoadTestNameConstraint("registeredid-excluded", &constraints_der)); | |
| 797 NameConstraints name_constraints; | |
| 798 EXPECT_TRUE( | |
| 799 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 800 | |
| 801 if (is_critical()) { | |
| 802 EXPECT_FALSE(name_constraints.IsPermittedRegisteredId()); | |
| 803 EXPECT_TRUE( | |
| 804 CheckNotPresent(name_constraints, kAllFlags & ~kRegisteredIdFlag)); | |
| 805 } else { | |
| 806 EXPECT_TRUE(CheckNotPresent(name_constraints, kAllFlags)); | |
| 807 } | |
| 808 } | |
| 809 | |
| 810 TEST_P(ParseNameConstraints, | |
| 811 failsOnGeneralSubtreeWithMinimumZeroEncodedUnnecessarily) { | |
| 812 std::string constraints_der; | |
| 813 ASSERT_TRUE(LoadTestNameConstraint("dnsname-with_min_0", &constraints_der)); | |
| 814 NameConstraints name_constraints; | |
| 815 // The value should not be in the DER encoding if it is the default. But this | |
| 816 // could be changed to allowed if there are buggy encoders out there that | |
| 817 // include it anyway. | |
| 818 EXPECT_FALSE( | |
| 819 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 820 } | |
| 821 | |
| 822 TEST_P(ParseNameConstraints, failsOnGeneralSubtreeWithMinimum) { | |
| 823 std::string constraints_der; | |
| 824 ASSERT_TRUE(LoadTestNameConstraint("dnsname-with_min_1", &constraints_der)); | |
| 825 NameConstraints name_constraints; | |
| 826 // Could handle this more gracefully (see TODO in ParseGeneralSubtrees). | |
| 827 EXPECT_FALSE( | |
| 828 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 829 } | |
| 830 | |
| 831 TEST_P(ParseNameConstraints, | |
| 832 failsOnGeneralSubtreeWithMinimumZeroEncodedUnnecessarilyAndMaximum) { | |
| 833 std::string constraints_der; | |
| 834 ASSERT_TRUE( | |
| 835 LoadTestNameConstraint("dnsname-with_min_0_and_max", &constraints_der)); | |
| 836 NameConstraints name_constraints; | |
| 837 // Could handle this more gracefully (see TODO in ParseGeneralSubtrees). | |
| 838 EXPECT_FALSE( | |
| 839 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 840 } | |
| 841 | |
| 842 TEST_P(ParseNameConstraints, failsOnGeneralSubtreeWithMinimumAndMaximum) { | |
| 843 std::string constraints_der; | |
| 844 ASSERT_TRUE( | |
| 845 LoadTestNameConstraint("dnsname-with_min_1_and_max", &constraints_der)); | |
| 846 NameConstraints name_constraints; | |
| 847 // Could handle this more gracefully (see TODO in ParseGeneralSubtrees). | |
| 848 EXPECT_FALSE( | |
| 849 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 850 } | |
| 851 | |
| 852 TEST_P(ParseNameConstraints, failsOnGeneralSubtreeWithMaximum) { | |
| 853 std::string constraints_der; | |
| 854 ASSERT_TRUE(LoadTestNameConstraint("dnsname-with_max", &constraints_der)); | |
| 855 NameConstraints name_constraints; | |
| 856 // Could handle this more gracefully (see TODO in ParseGeneralSubtrees). | |
| 857 EXPECT_FALSE( | |
| 858 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 859 } | |
| 860 | |
| 861 TEST_P(ParseNameConstraints, failsOnEmptyExtensionValue) { | |
| 862 std::string constraints_der = ""; | |
| 863 NameConstraints name_constraints; | |
| 864 EXPECT_FALSE( | |
| 865 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 866 } | |
| 867 | |
| 868 TEST_P(ParseNameConstraints, failsOnEmptyPermittedAndExcluded) { | |
| 869 std::string constraints_der; | |
| 870 ASSERT_TRUE(LoadTestNameConstraint("invalid-no_subtrees", &constraints_der)); | |
| 871 NameConstraints name_constraints; | |
| 872 EXPECT_FALSE( | |
| 873 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 874 } | |
| 875 | |
| 876 TEST_P(ParseNameConstraints, IsPermittedCert_SubjectEmailAddress_ok) { | |
| 877 std::string constraints_der; | |
| 878 ASSERT_TRUE(LoadTestNameConstraint("directoryname", &constraints_der)); | |
| 879 NameConstraints name_constraints; | |
| 880 EXPECT_TRUE( | |
| 881 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 882 | |
| 883 std::string name_us_arizona_email; | |
| 884 ASSERT_TRUE(LoadTestName("name-us-arizona-email", &name_us_arizona_email)); | |
| 885 | |
| 886 // Name constraints don't contain rfc822Name, so emailAddress in subject is | |
| 887 // allowed regardless. | |
| 888 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 889 SequenceValueFromString(&name_us_arizona_email), der::Input(), | |
| 890 true /* is_leaf_cert */)); | |
| 891 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 892 SequenceValueFromString(&name_us_arizona_email), der::Input(), | |
| 893 false /* is_leaf_cert */)); | |
| 894 } | |
| 895 | |
| 896 TEST_P(ParseNameConstraints, IsPermittedCert_SubjectEmailAddress_notok) { | |
| 897 std::string constraints_der; | |
| 898 ASSERT_TRUE(LoadTestNameConstraint("rfc822name-permitted", &constraints_der)); | |
| 899 NameConstraints name_constraints; | |
| 900 EXPECT_TRUE( | |
| 901 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 902 | |
| 903 std::string name_us_arizona_email; | |
| 904 ASSERT_TRUE(LoadTestName("name-us-arizona-email", &name_us_arizona_email)); | |
| 905 | |
| 906 // Name constraints contain rfc822Name, so emailAddress in subject is not | |
| 907 // allowed if the constraints were critical. | |
| 908 EXPECT_EQ(!is_critical(), name_constraints.IsPermittedCert( | |
| 909 SequenceValueFromString(&name_us_arizona_email), | |
| 910 der::Input(), true /* is_leaf_cert */)); | |
| 911 EXPECT_EQ(!is_critical(), name_constraints.IsPermittedCert( | |
| 912 SequenceValueFromString(&name_us_arizona_email), | |
| 913 der::Input(), false /* is_leaf_cert */)); | |
| 914 } | |
| 915 | |
| 916 TEST_P(ParseNameConstraints, IsPermittedCert_SubjectDnsNames) { | |
| 917 std::string constraints_der; | |
| 918 ASSERT_TRUE(LoadTestNameConstraint("dnsname", &constraints_der)); | |
| 919 NameConstraints name_constraints; | |
| 920 EXPECT_TRUE( | |
| 921 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 922 | |
| 923 ASSERT_TRUE(LoadTestNameConstraint("directoryname", &constraints_der)); | |
| 924 // This is a bit of a hack, but Parse can be called multiple times to load | |
| 925 // different constraints into the same object (doesn't do a proper merge, but | |
| 926 // good enough for this test). | |
| 927 EXPECT_TRUE( | |
| 928 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 929 | |
| 930 std::string name_us_az; | |
| 931 ASSERT_TRUE(LoadTestName("name-us-arizona", &name_us_az)); | |
| 932 std::string name_us_az_foocom; | |
| 933 ASSERT_TRUE(LoadTestName("name-us-arizona-foo.com", &name_us_az_foocom)); | |
| 934 | |
| 935 // foo.com is not within the permitted dNSName constraints: | |
| 936 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 937 SequenceValueFromString(&name_us_az_foocom), der::Input(), | |
| 938 true /* is_leaf_cert */)); | |
| 939 // commonName check is only done for leaf certs, so this is allowed: | |
| 940 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 941 SequenceValueFromString(&name_us_az_foocom), der::Input(), | |
| 942 false /* is_leaf_cert */)); | |
| 943 | |
| 944 std::string name_us_az_permitted; | |
| 945 ASSERT_TRUE(LoadTestName("name-us-arizona-permitted.example.com", | |
| 946 &name_us_az_permitted)); | |
| 947 // permitted.example.com is within the permitted dNSName constraints and the | |
| 948 // permitted C=US directoryName constraint: | |
| 949 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 950 SequenceValueFromString(&name_us_az_permitted), der::Input(), | |
| 951 true /* is_leaf_cert */)); | |
| 952 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 953 SequenceValueFromString(&name_us_az_permitted), der::Input(), | |
| 954 false /* is_leaf_cert */)); | |
| 955 | |
| 956 std::string name_us_ca_permitted; | |
| 957 ASSERT_TRUE(LoadTestName("name-us-california-permitted.example.com", | |
| 958 &name_us_ca_permitted)); | |
| 959 // permitted.example.com is within the permitted dNSName constraints but the | |
| 960 // subject is within the excluded C=US,ST=California directoryName, so not | |
| 961 // allowed anyway: | |
| 962 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 963 SequenceValueFromString(&name_us_ca_permitted), der::Input(), | |
| 964 true /* is_leaf_cert */)); | |
| 965 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 966 SequenceValueFromString(&name_us_ca_permitted), der::Input(), | |
| 967 false /* is_leaf_cert */)); | |
| 968 | |
| 969 // As a leaf cert with no SubjectAltName, the subject is expected to have a | |
| 970 // host in commonName, this subject has no commonName field, so fails: | |
| 971 EXPECT_FALSE( | |
| 972 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_az), | |
| 973 der::Input(), true /* is_leaf_cert */)); | |
| 974 // Within the permitted C=US subtree, not a leaf cert, so does not need | |
| 975 // a host in commonName. | |
| 976 EXPECT_TRUE( | |
| 977 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_az), | |
| 978 der::Input(), false /* is_leaf_cert */)); | |
| 979 } | |
| 980 | |
| 981 TEST_P(ParseNameConstraints, IsPermittedCert_SubjectIpAddresses) { | |
| 982 std::string constraints_der; | |
| 983 ASSERT_TRUE(LoadTestNameConstraint("ipaddress", &constraints_der)); | |
| 984 NameConstraints name_constraints; | |
| 985 EXPECT_TRUE( | |
| 986 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 987 | |
| 988 ASSERT_TRUE(LoadTestNameConstraint("directoryname", &constraints_der)); | |
| 989 // This is a bit of a hack, but Parse can be called multiple times to load | |
| 990 // different constraints into the same object (doesn't do a proper merge, but | |
| 991 // good enough for this test). | |
| 992 EXPECT_TRUE( | |
| 993 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 994 ASSERT_TRUE(LoadTestNameConstraint("dnsname", &constraints_der)); | |
| 995 EXPECT_TRUE( | |
| 996 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 997 | |
| 998 std::string name_us_az; | |
| 999 ASSERT_TRUE(LoadTestName("name-us-arizona", &name_us_az)); | |
| 1000 std::string name_us_az_1_1_1_1; | |
| 1001 ASSERT_TRUE(LoadTestName("name-us-arizona-1.1.1.1", &name_us_az_1_1_1_1)); | |
| 1002 | |
| 1003 // 1.1.1.1 is not within the permitted iPAddress constraints: | |
| 1004 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 1005 SequenceValueFromString(&name_us_az_1_1_1_1), der::Input(), | |
| 1006 true /* is_leaf_cert */)); | |
| 1007 // commonName check is only done for leaf certs, so this is allowed: | |
| 1008 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1009 SequenceValueFromString(&name_us_az_1_1_1_1), der::Input(), | |
| 1010 false /* is_leaf_cert */)); | |
| 1011 | |
| 1012 std::string name_us_az_192_168_1_1; | |
| 1013 ASSERT_TRUE( | |
| 1014 LoadTestName("name-us-arizona-192.168.1.1", &name_us_az_192_168_1_1)); | |
| 1015 // 192.168.1.1 is within the permitted iPAddress constraints and the | |
| 1016 // permitted C=US directoryName constraint: | |
| 1017 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1018 SequenceValueFromString(&name_us_az_192_168_1_1), der::Input(), | |
| 1019 true /* is_leaf_cert */)); | |
| 1020 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1021 SequenceValueFromString(&name_us_az_192_168_1_1), der::Input(), | |
| 1022 false /* is_leaf_cert */)); | |
| 1023 | |
| 1024 std::string name_us_ca_192_168_1_1; | |
| 1025 ASSERT_TRUE( | |
| 1026 LoadTestName("name-us-california-192.168.1.1", &name_us_ca_192_168_1_1)); | |
| 1027 // 192.168.1.1 is within the permitted iPAddress constraints but the | |
| 1028 // subject is within the excluded C=US,ST=California directoryName, so not | |
| 1029 // allowed anyway: | |
| 1030 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 1031 SequenceValueFromString(&name_us_ca_192_168_1_1), der::Input(), | |
| 1032 true /* is_leaf_cert */)); | |
| 1033 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 1034 SequenceValueFromString(&name_us_ca_192_168_1_1), der::Input(), | |
| 1035 false /* is_leaf_cert */)); | |
| 1036 | |
| 1037 std::string name_us_az_ipv6; | |
| 1038 ASSERT_TRUE(LoadTestName("name-us-arizona-ipv6", &name_us_az_ipv6)); | |
| 1039 // 102:304:506:708:90a:b0c::1 is within the permitted iPAddress constraints | |
| 1040 // and the permitted C=US directoryName constraint, but Chrome only handles | |
| 1041 // IPv4 addresses in commonName so the address will be checked against dNSName | |
| 1042 // constraints instead, which will not match any permitted names. | |
| 1043 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 1044 SequenceValueFromString(&name_us_az_ipv6), der::Input(), | |
| 1045 true /* is_leaf_cert */)); | |
| 1046 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1047 SequenceValueFromString(&name_us_az_ipv6), der::Input(), | |
| 1048 false /* is_leaf_cert */)); | |
| 1049 | |
| 1050 // As a leaf cert with no SubjectAltName, the subject is expected to have a | |
| 1051 // host in commonName, this subject has no commonName field, so fails: | |
| 1052 EXPECT_FALSE( | |
| 1053 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_az), | |
| 1054 der::Input(), true /* is_leaf_cert */)); | |
| 1055 // Within the permitted C=US subtree, not a leaf cert, so does not need | |
| 1056 // a host in commonName. | |
| 1057 EXPECT_TRUE( | |
| 1058 name_constraints.IsPermittedCert(SequenceValueFromString(&name_us_az), | |
| 1059 der::Input(), false /* is_leaf_cert */)); | |
| 1060 } | |
| 1061 | |
| 1062 TEST_P(ParseNameConstraints, IsPermittedCert_Subject_commonName_encodings) { | |
| 1063 std::string constraints_der; | |
| 1064 ASSERT_TRUE(LoadTestNameConstraint("ipaddress", &constraints_der)); | |
| 1065 NameConstraints name_constraints; | |
| 1066 EXPECT_TRUE( | |
| 1067 name_constraints.Parse(InputFromString(&constraints_der), is_critical())); | |
| 1068 | |
| 1069 // CommonName encoded as BMPString: should work since we convert to UTF8 | |
| 1070 // before parsing. | |
| 1071 std::string name_us_az_192_168_1_1_bmp; | |
| 1072 ASSERT_TRUE(LoadTestName("name-us-arizona-192.168.1.1-bmp", | |
| 1073 &name_us_az_192_168_1_1_bmp)); | |
| 1074 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1075 SequenceValueFromString(&name_us_az_192_168_1_1_bmp), der::Input(), | |
| 1076 true /* is_leaf_cert */)); | |
| 1077 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1078 SequenceValueFromString(&name_us_az_192_168_1_1_bmp), der::Input(), | |
| 1079 false /* is_leaf_cert */)); | |
| 1080 | |
| 1081 // CommonName encoded as TeletexString: should not work, TeletexString is not | |
| 1082 // supported. | |
| 1083 std::string name_us_az_192_168_1_1_t61; | |
| 1084 ASSERT_TRUE(LoadTestName("name-us-arizona-192.168.1.1-t61", | |
| 1085 &name_us_az_192_168_1_1_t61)); | |
| 1086 EXPECT_FALSE(name_constraints.IsPermittedCert( | |
| 1087 SequenceValueFromString(&name_us_az_192_168_1_1_t61), der::Input(), | |
| 1088 true /* is_leaf_cert */)); | |
| 1089 // If it's not a leaf cert, commonName is not checked, so this is allowed. | |
| 1090 EXPECT_TRUE(name_constraints.IsPermittedCert( | |
| 1091 SequenceValueFromString(&name_us_az_192_168_1_1_t61), der::Input(), | |
| 1092 false /* is_leaf_cert */)); | |
| 1093 } | |
| 1094 | |
| 1095 } // namespace net | |
| OLD | NEW |