Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: net/cert/x509_certificate_unittest.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/x509_certificate.h" 5 #include "net/cert/x509_certificate.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/pickle.h" 10 #include "base/pickle.h"
(...skipping 28 matching lines...) Expand all
39 // 39 //
40 // For fingerprint 40 // For fingerprint
41 // $ openssl x509 -inform DER -fingerprint -noout < /tmp/host.der 41 // $ openssl x509 -inform DER -fingerprint -noout < /tmp/host.der
42 42
43 // For valid_start, valid_expiry 43 // For valid_start, valid_expiry
44 // $ openssl x509 -inform DER -text -noout < /tmp/host.der | 44 // $ openssl x509 -inform DER -text -noout < /tmp/host.der |
45 // grep -A 2 Validity 45 // grep -A 2 Validity
46 // $ date +%s -d '<date str>' 46 // $ date +%s -d '<date str>'
47 47
48 // Google's cert. 48 // Google's cert.
49 uint8 google_fingerprint[] = { 49 uint8 google_fingerprint[] = {0xab, 0xbe, 0x5e, 0xb4, 0x93, 0x88, 0x4e,
50 0xab, 0xbe, 0x5e, 0xb4, 0x93, 0x88, 0x4e, 0xe4, 0x60, 0xc6, 0xef, 0xf8, 50 0xe4, 0x60, 0xc6, 0xef, 0xf8, 0xea, 0xd4,
51 0xea, 0xd4, 0xb1, 0x55, 0x4b, 0xc9, 0x59, 0x3c 51 0xb1, 0x55, 0x4b, 0xc9, 0x59, 0x3c};
52 };
53 52
54 // webkit.org's cert. 53 // webkit.org's cert.
55 uint8 webkit_fingerprint[] = { 54 uint8 webkit_fingerprint[] = {0xa1, 0x4a, 0x94, 0x46, 0x22, 0x8e, 0x70,
56 0xa1, 0x4a, 0x94, 0x46, 0x22, 0x8e, 0x70, 0x66, 0x2b, 0x94, 0xf9, 0xf8, 55 0x66, 0x2b, 0x94, 0xf9, 0xf8, 0x57, 0x83,
57 0x57, 0x83, 0x2d, 0xa2, 0xff, 0xbc, 0x84, 0xc2 56 0x2d, 0xa2, 0xff, 0xbc, 0x84, 0xc2};
58 };
59 57
60 // thawte.com's cert (it's EV-licious!). 58 // thawte.com's cert (it's EV-licious!).
61 uint8 thawte_fingerprint[] = { 59 uint8 thawte_fingerprint[] = {0x85, 0x04, 0x2d, 0xfd, 0x2b, 0x0e, 0xc6,
62 0x85, 0x04, 0x2d, 0xfd, 0x2b, 0x0e, 0xc6, 0xc8, 0xaf, 0x2d, 0x77, 0xd6, 60 0xc8, 0xaf, 0x2d, 0x77, 0xd6, 0xa1, 0x3a,
63 0xa1, 0x3a, 0x64, 0x04, 0x27, 0x90, 0x97, 0x37 61 0x64, 0x04, 0x27, 0x90, 0x97, 0x37};
64 };
65 62
66 // A certificate for https://www.unosoft.hu/, whose AIA extension contains 63 // A certificate for https://www.unosoft.hu/, whose AIA extension contains
67 // an LDAP URL without a host name. 64 // an LDAP URL without a host name.
68 uint8 unosoft_hu_fingerprint[] = { 65 uint8 unosoft_hu_fingerprint[] = {0x32, 0xff, 0xe3, 0xbe, 0x2c, 0x3b, 0xc7,
69 0x32, 0xff, 0xe3, 0xbe, 0x2c, 0x3b, 0xc7, 0xca, 0xbf, 0x2d, 0x64, 0xbd, 66 0xca, 0xbf, 0x2d, 0x64, 0xbd, 0x25, 0x66,
70 0x25, 0x66, 0xf2, 0xec, 0x8b, 0x0f, 0xbf, 0xd8 67 0xf2, 0xec, 0x8b, 0x0f, 0xbf, 0xd8};
71 };
72 68
73 // The fingerprint of the Google certificate used in the parsing tests, 69 // The fingerprint of the Google certificate used in the parsing tests,
74 // which is newer than the one included in the x509_certificate_data.h 70 // which is newer than the one included in the x509_certificate_data.h
75 uint8 google_parse_fingerprint[] = { 71 uint8 google_parse_fingerprint[] = {0x40, 0x50, 0x62, 0xe5, 0xbe, 0xfd, 0xe4,
76 0x40, 0x50, 0x62, 0xe5, 0xbe, 0xfd, 0xe4, 0xaf, 0x97, 0xe9, 0x38, 0x2a, 72 0xaf, 0x97, 0xe9, 0x38, 0x2a, 0xf1, 0x6c,
77 0xf1, 0x6c, 0xc8, 0x7c, 0x8f, 0xb7, 0xc4, 0xe2 73 0xc8, 0x7c, 0x8f, 0xb7, 0xc4, 0xe2};
78 };
79 74
80 // The fingerprint for the Thawte SGC certificate 75 // The fingerprint for the Thawte SGC certificate
81 uint8 thawte_parse_fingerprint[] = { 76 uint8 thawte_parse_fingerprint[] = {0xec, 0x07, 0x10, 0x03, 0xd8, 0xf5, 0xa3,
82 0xec, 0x07, 0x10, 0x03, 0xd8, 0xf5, 0xa3, 0x7f, 0x42, 0xc4, 0x55, 0x7f, 77 0x7f, 0x42, 0xc4, 0x55, 0x7f, 0x65, 0x6a,
83 0x65, 0x6a, 0xae, 0x86, 0x65, 0xfa, 0x4b, 0x02 78 0xae, 0x86, 0x65, 0xfa, 0x4b, 0x02};
84 };
85 79
86 // Dec 18 00:00:00 2009 GMT 80 // Dec 18 00:00:00 2009 GMT
87 const double kGoogleParseValidFrom = 1261094400; 81 const double kGoogleParseValidFrom = 1261094400;
88 // Dec 18 23:59:59 2011 GMT 82 // Dec 18 23:59:59 2011 GMT
89 const double kGoogleParseValidTo = 1324252799; 83 const double kGoogleParseValidTo = 1324252799;
90 84
91 void CheckGoogleCert(const scoped_refptr<X509Certificate>& google_cert, 85 void CheckGoogleCert(const scoped_refptr<X509Certificate>& google_cert,
92 uint8* expected_fingerprint, 86 uint8* expected_fingerprint,
93 double valid_from, double valid_to) { 87 double valid_from,
88 double valid_to) {
94 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_cert); 89 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_cert);
95 90
96 const CertPrincipal& subject = google_cert->subject(); 91 const CertPrincipal& subject = google_cert->subject();
97 EXPECT_EQ("www.google.com", subject.common_name); 92 EXPECT_EQ("www.google.com", subject.common_name);
98 EXPECT_EQ("Mountain View", subject.locality_name); 93 EXPECT_EQ("Mountain View", subject.locality_name);
99 EXPECT_EQ("California", subject.state_or_province_name); 94 EXPECT_EQ("California", subject.state_or_province_name);
100 EXPECT_EQ("US", subject.country_name); 95 EXPECT_EQ("US", subject.country_name);
101 EXPECT_EQ(0U, subject.street_addresses.size()); 96 EXPECT_EQ(0U, subject.street_addresses.size());
102 ASSERT_EQ(1U, subject.organization_names.size()); 97 ASSERT_EQ(1U, subject.organization_names.size());
103 EXPECT_EQ("Google Inc", subject.organization_names[0]); 98 EXPECT_EQ("Google Inc", subject.organization_names[0]);
(...skipping 22 matching lines...) Expand all
126 for (size_t i = 0; i < 20; ++i) 121 for (size_t i = 0; i < 20; ++i)
127 EXPECT_EQ(expected_fingerprint[i], fingerprint.data[i]); 122 EXPECT_EQ(expected_fingerprint[i], fingerprint.data[i]);
128 123
129 std::vector<std::string> dns_names; 124 std::vector<std::string> dns_names;
130 google_cert->GetDNSNames(&dns_names); 125 google_cert->GetDNSNames(&dns_names);
131 ASSERT_EQ(1U, dns_names.size()); 126 ASSERT_EQ(1U, dns_names.size());
132 EXPECT_EQ("www.google.com", dns_names[0]); 127 EXPECT_EQ("www.google.com", dns_names[0]);
133 } 128 }
134 129
135 TEST(X509CertificateTest, GoogleCertParsing) { 130 TEST(X509CertificateTest, GoogleCertParsing) {
136 scoped_refptr<X509Certificate> google_cert( 131 scoped_refptr<X509Certificate> google_cert(X509Certificate::CreateFromBytes(
137 X509Certificate::CreateFromBytes( 132 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
138 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
139 133
140 CheckGoogleCert(google_cert, google_fingerprint, 134 CheckGoogleCert(google_cert,
135 google_fingerprint,
141 1238192407, // Mar 27 22:20:07 2009 GMT 136 1238192407, // Mar 27 22:20:07 2009 GMT
142 1269728407); // Mar 27 22:20:07 2010 GMT 137 1269728407); // Mar 27 22:20:07 2010 GMT
143 } 138 }
144 139
145 TEST(X509CertificateTest, WebkitCertParsing) { 140 TEST(X509CertificateTest, WebkitCertParsing) {
146 scoped_refptr<X509Certificate> webkit_cert(X509Certificate::CreateFromBytes( 141 scoped_refptr<X509Certificate> webkit_cert(X509Certificate::CreateFromBytes(
147 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); 142 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
148 143
149 ASSERT_NE(static_cast<X509Certificate*>(NULL), webkit_cert); 144 ASSERT_NE(static_cast<X509Certificate*>(NULL), webkit_cert);
150 145
(...skipping 11 matching lines...) Expand all
162 const CertPrincipal& issuer = webkit_cert->issuer(); 157 const CertPrincipal& issuer = webkit_cert->issuer();
163 EXPECT_EQ("Go Daddy Secure Certification Authority", issuer.common_name); 158 EXPECT_EQ("Go Daddy Secure Certification Authority", issuer.common_name);
164 EXPECT_EQ("Scottsdale", issuer.locality_name); 159 EXPECT_EQ("Scottsdale", issuer.locality_name);
165 EXPECT_EQ("Arizona", issuer.state_or_province_name); 160 EXPECT_EQ("Arizona", issuer.state_or_province_name);
166 EXPECT_EQ("US", issuer.country_name); 161 EXPECT_EQ("US", issuer.country_name);
167 EXPECT_EQ(0U, issuer.street_addresses.size()); 162 EXPECT_EQ(0U, issuer.street_addresses.size());
168 ASSERT_EQ(1U, issuer.organization_names.size()); 163 ASSERT_EQ(1U, issuer.organization_names.size());
169 EXPECT_EQ("GoDaddy.com, Inc.", issuer.organization_names[0]); 164 EXPECT_EQ("GoDaddy.com, Inc.", issuer.organization_names[0]);
170 ASSERT_EQ(1U, issuer.organization_unit_names.size()); 165 ASSERT_EQ(1U, issuer.organization_unit_names.size());
171 EXPECT_EQ("http://certificates.godaddy.com/repository", 166 EXPECT_EQ("http://certificates.godaddy.com/repository",
172 issuer.organization_unit_names[0]); 167 issuer.organization_unit_names[0]);
173 EXPECT_EQ(0U, issuer.domain_components.size()); 168 EXPECT_EQ(0U, issuer.domain_components.size());
174 169
175 // Use DoubleT because its epoch is the same on all platforms 170 // Use DoubleT because its epoch is the same on all platforms
176 const Time& valid_start = webkit_cert->valid_start(); 171 const Time& valid_start = webkit_cert->valid_start();
177 EXPECT_EQ(1205883319, valid_start.ToDoubleT()); // Mar 18 23:35:19 2008 GMT 172 EXPECT_EQ(1205883319, valid_start.ToDoubleT()); // Mar 18 23:35:19 2008 GMT
178 173
179 const Time& valid_expiry = webkit_cert->valid_expiry(); 174 const Time& valid_expiry = webkit_cert->valid_expiry();
180 EXPECT_EQ(1300491319, valid_expiry.ToDoubleT()); // Mar 18 23:35:19 2011 GMT 175 EXPECT_EQ(1300491319, valid_expiry.ToDoubleT()); // Mar 18 23:35:19 2011 GMT
181 176
182 const SHA1HashValue& fingerprint = webkit_cert->fingerprint(); 177 const SHA1HashValue& fingerprint = webkit_cert->fingerprint();
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 EXPECT_EQ("1600 Amphitheatre Parkway", subject.street_addresses[0]); 284 EXPECT_EQ("1600 Amphitheatre Parkway", subject.street_addresses[0]);
290 ASSERT_EQ(1U, subject.organization_names.size()); 285 ASSERT_EQ(1U, subject.organization_names.size());
291 EXPECT_EQ("Chromium = \"net_unittests\"", subject.organization_names[0]); 286 EXPECT_EQ("Chromium = \"net_unittests\"", subject.organization_names[0]);
292 ASSERT_EQ(2U, subject.organization_unit_names.size()); 287 ASSERT_EQ(2U, subject.organization_unit_names.size());
293 EXPECT_EQ("net_unittests", subject.organization_unit_names[0]); 288 EXPECT_EQ("net_unittests", subject.organization_unit_names[0]);
294 EXPECT_EQ("Chromium", subject.organization_unit_names[1]); 289 EXPECT_EQ("Chromium", subject.organization_unit_names[1]);
295 EXPECT_EQ(0U, subject.domain_components.size()); 290 EXPECT_EQ(0U, subject.domain_components.size());
296 } 291 }
297 292
298 TEST(X509CertificateTest, SerialNumbers) { 293 TEST(X509CertificateTest, SerialNumbers) {
299 scoped_refptr<X509Certificate> google_cert( 294 scoped_refptr<X509Certificate> google_cert(X509Certificate::CreateFromBytes(
300 X509Certificate::CreateFromBytes( 295 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
301 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
302 296
303 static const uint8 google_serial[16] = { 297 static const uint8 google_serial[16] = {
304 0x01,0x2a,0x39,0x76,0x0d,0x3f,0x4f,0xc9, 298 0x01, 0x2a, 0x39, 0x76, 0x0d, 0x3f, 0x4f, 0xc9,
305 0x0b,0xe7,0xbd,0x2b,0xcf,0x95,0x2e,0x7a, 299 0x0b, 0xe7, 0xbd, 0x2b, 0xcf, 0x95, 0x2e, 0x7a,
306 }; 300 };
307 301
308 ASSERT_EQ(sizeof(google_serial), google_cert->serial_number().size()); 302 ASSERT_EQ(sizeof(google_serial), google_cert->serial_number().size());
309 EXPECT_TRUE(memcmp(google_cert->serial_number().data(), google_serial, 303 EXPECT_TRUE(memcmp(google_cert->serial_number().data(),
304 google_serial,
310 sizeof(google_serial)) == 0); 305 sizeof(google_serial)) == 0);
311 306
312 // We also want to check a serial number where the first byte is >= 0x80 in 307 // We also want to check a serial number where the first byte is >= 0x80 in
313 // case the underlying library tries to pad it. 308 // case the underlying library tries to pad it.
314 scoped_refptr<X509Certificate> paypal_null_cert( 309 scoped_refptr<X509Certificate> paypal_null_cert(
315 X509Certificate::CreateFromBytes( 310 X509Certificate::CreateFromBytes(
316 reinterpret_cast<const char*>(paypal_null_der), 311 reinterpret_cast<const char*>(paypal_null_der),
317 sizeof(paypal_null_der))); 312 sizeof(paypal_null_der)));
318 313
319 static const uint8 paypal_null_serial[3] = {0x00, 0xf0, 0x9b}; 314 static const uint8 paypal_null_serial[3] = {0x00, 0xf0, 0x9b};
320 ASSERT_EQ(sizeof(paypal_null_serial), 315 ASSERT_EQ(sizeof(paypal_null_serial),
321 paypal_null_cert->serial_number().size()); 316 paypal_null_cert->serial_number().size());
322 EXPECT_TRUE(memcmp(paypal_null_cert->serial_number().data(), 317 EXPECT_TRUE(memcmp(paypal_null_cert->serial_number().data(),
323 paypal_null_serial, sizeof(paypal_null_serial)) == 0); 318 paypal_null_serial,
319 sizeof(paypal_null_serial)) == 0);
324 } 320 }
325 321
326 TEST(X509CertificateTest, CAFingerprints) { 322 TEST(X509CertificateTest, CAFingerprints) {
327 base::FilePath certs_dir = GetTestCertsDirectory(); 323 base::FilePath certs_dir = GetTestCertsDirectory();
328 324
329 scoped_refptr<X509Certificate> server_cert = 325 scoped_refptr<X509Certificate> server_cert =
330 ImportCertFromFile(certs_dir, "salesforce_com_test.pem"); 326 ImportCertFromFile(certs_dir, "salesforce_com_test.pem");
331 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); 327 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert);
332 328
333 scoped_refptr<X509Certificate> intermediate_cert1 = 329 scoped_refptr<X509Certificate> intermediate_cert1 =
(...skipping 16 matching lines...) Expand all
350 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), 346 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(),
351 intermediates); 347 intermediates);
352 348
353 // No intermediate CA certicates. 349 // No intermediate CA certicates.
354 intermediates.clear(); 350 intermediates.clear();
355 scoped_refptr<X509Certificate> cert_chain3 = 351 scoped_refptr<X509Certificate> cert_chain3 =
356 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), 352 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(),
357 intermediates); 353 intermediates);
358 354
359 static const uint8 cert_chain1_ca_fingerprint[20] = { 355 static const uint8 cert_chain1_ca_fingerprint[20] = {
360 0xc2, 0xf0, 0x08, 0x7d, 0x01, 0xe6, 0x86, 0x05, 0x3a, 0x4d, 356 0xc2, 0xf0, 0x08, 0x7d, 0x01, 0xe6, 0x86, 0x05, 0x3a, 0x4d,
361 0x63, 0x3e, 0x7e, 0x70, 0xd4, 0xef, 0x65, 0xc2, 0xcc, 0x4f 357 0x63, 0x3e, 0x7e, 0x70, 0xd4, 0xef, 0x65, 0xc2, 0xcc, 0x4f};
362 };
363 static const uint8 cert_chain2_ca_fingerprint[20] = { 358 static const uint8 cert_chain2_ca_fingerprint[20] = {
364 0xd5, 0x59, 0xa5, 0x86, 0x66, 0x9b, 0x08, 0xf4, 0x6a, 0x30, 359 0xd5, 0x59, 0xa5, 0x86, 0x66, 0x9b, 0x08, 0xf4, 0x6a, 0x30,
365 0xa1, 0x33, 0xf8, 0xa9, 0xed, 0x3d, 0x03, 0x8e, 0x2e, 0xa8 360 0xa1, 0x33, 0xf8, 0xa9, 0xed, 0x3d, 0x03, 0x8e, 0x2e, 0xa8};
366 };
367 // The SHA-1 hash of nothing. 361 // The SHA-1 hash of nothing.
368 static const uint8 cert_chain3_ca_fingerprint[20] = { 362 static const uint8 cert_chain3_ca_fingerprint[20] = {
369 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 363 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
370 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 364 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09};
371 };
372 EXPECT_TRUE(memcmp(cert_chain1->ca_fingerprint().data, 365 EXPECT_TRUE(memcmp(cert_chain1->ca_fingerprint().data,
373 cert_chain1_ca_fingerprint, 20) == 0); 366 cert_chain1_ca_fingerprint,
367 20) == 0);
374 EXPECT_TRUE(memcmp(cert_chain2->ca_fingerprint().data, 368 EXPECT_TRUE(memcmp(cert_chain2->ca_fingerprint().data,
375 cert_chain2_ca_fingerprint, 20) == 0); 369 cert_chain2_ca_fingerprint,
370 20) == 0);
376 EXPECT_TRUE(memcmp(cert_chain3->ca_fingerprint().data, 371 EXPECT_TRUE(memcmp(cert_chain3->ca_fingerprint().data,
377 cert_chain3_ca_fingerprint, 20) == 0); 372 cert_chain3_ca_fingerprint,
373 20) == 0);
378 } 374 }
379 375
380 TEST(X509CertificateTest, ParseSubjectAltNames) { 376 TEST(X509CertificateTest, ParseSubjectAltNames) {
381 base::FilePath certs_dir = GetTestCertsDirectory(); 377 base::FilePath certs_dir = GetTestCertsDirectory();
382 378
383 scoped_refptr<X509Certificate> san_cert = 379 scoped_refptr<X509Certificate> san_cert =
384 ImportCertFromFile(certs_dir, "subjectAltName_sanity_check.pem"); 380 ImportCertFromFile(certs_dir, "subjectAltName_sanity_check.pem");
385 ASSERT_NE(static_cast<X509Certificate*>(NULL), san_cert); 381 ASSERT_NE(static_cast<X509Certificate*>(NULL), san_cert);
386 382
387 std::vector<std::string> dns_names; 383 std::vector<std::string> dns_names;
388 std::vector<std::string> ip_addresses; 384 std::vector<std::string> ip_addresses;
389 san_cert->GetSubjectAltName(&dns_names, &ip_addresses); 385 san_cert->GetSubjectAltName(&dns_names, &ip_addresses);
390 386
391 // Ensure that DNS names are correctly parsed. 387 // Ensure that DNS names are correctly parsed.
392 ASSERT_EQ(1U, dns_names.size()); 388 ASSERT_EQ(1U, dns_names.size());
393 EXPECT_EQ("test.example", dns_names[0]); 389 EXPECT_EQ("test.example", dns_names[0]);
394 390
395 // Ensure that both IPv4 and IPv6 addresses are correctly parsed. 391 // Ensure that both IPv4 and IPv6 addresses are correctly parsed.
396 ASSERT_EQ(2U, ip_addresses.size()); 392 ASSERT_EQ(2U, ip_addresses.size());
397 393
398 static const uint8 kIPv4Address[] = { 394 static const uint8 kIPv4Address[] = {0x7F, 0x00, 0x00, 0x02};
399 0x7F, 0x00, 0x00, 0x02
400 };
401 ASSERT_EQ(arraysize(kIPv4Address), ip_addresses[0].size()); 395 ASSERT_EQ(arraysize(kIPv4Address), ip_addresses[0].size());
402 EXPECT_EQ(0, memcmp(ip_addresses[0].data(), kIPv4Address, 396 EXPECT_EQ(
403 arraysize(kIPv4Address))); 397 0, memcmp(ip_addresses[0].data(), kIPv4Address, arraysize(kIPv4Address)));
404 398
405 static const uint8 kIPv6Address[] = { 399 static const uint8 kIPv6Address[] = {0xFE, 0x80, 0x00, 0x00, 0x00, 0x00,
406 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 401 0x00, 0x00, 0x00, 0x01};
408 };
409 ASSERT_EQ(arraysize(kIPv6Address), ip_addresses[1].size()); 402 ASSERT_EQ(arraysize(kIPv6Address), ip_addresses[1].size());
410 EXPECT_EQ(0, memcmp(ip_addresses[1].data(), kIPv6Address, 403 EXPECT_EQ(
411 arraysize(kIPv6Address))); 404 0, memcmp(ip_addresses[1].data(), kIPv6Address, arraysize(kIPv6Address)));
412 405
413 // Ensure the subjectAltName dirName has not influenced the handling of 406 // Ensure the subjectAltName dirName has not influenced the handling of
414 // the subject commonName. 407 // the subject commonName.
415 EXPECT_EQ("127.0.0.1", san_cert->subject().common_name); 408 EXPECT_EQ("127.0.0.1", san_cert->subject().common_name);
416 } 409 }
417 410
418 TEST(X509CertificateTest, ExtractSPKIFromDERCert) { 411 TEST(X509CertificateTest, ExtractSPKIFromDERCert) {
419 base::FilePath certs_dir = GetTestCertsDirectory(); 412 base::FilePath certs_dir = GetTestCertsDirectory();
420 scoped_refptr<X509Certificate> cert = 413 scoped_refptr<X509Certificate> cert =
421 ImportCertFromFile(certs_dir, "nist.der"); 414 ImportCertFromFile(certs_dir, "nist.der");
422 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); 415 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert);
423 416
424 std::string derBytes; 417 std::string derBytes;
425 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), 418 EXPECT_TRUE(
426 &derBytes)); 419 X509Certificate::GetDEREncoded(cert->os_cert_handle(), &derBytes));
427 420
428 base::StringPiece spkiBytes; 421 base::StringPiece spkiBytes;
429 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes)); 422 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes));
430 423
431 uint8 hash[base::kSHA1Length]; 424 uint8 hash[base::kSHA1Length];
432 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spkiBytes.data()), 425 base::SHA1HashBytes(
433 spkiBytes.size(), hash); 426 reinterpret_cast<const uint8*>(spkiBytes.data()), spkiBytes.size(), hash);
434 427
435 EXPECT_EQ(0, memcmp(hash, kNistSPKIHash, sizeof(hash))); 428 EXPECT_EQ(0, memcmp(hash, kNistSPKIHash, sizeof(hash)));
436 } 429 }
437 430
438 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) { 431 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) {
439 base::FilePath certs_dir = GetTestCertsDirectory(); 432 base::FilePath certs_dir = GetTestCertsDirectory();
440 scoped_refptr<X509Certificate> cert = 433 scoped_refptr<X509Certificate> cert =
441 ImportCertFromFile(certs_dir, "nist.der"); 434 ImportCertFromFile(certs_dir, "nist.der");
442 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); 435 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert);
443 436
444 std::string derBytes; 437 std::string derBytes;
445 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), 438 EXPECT_TRUE(
446 &derBytes)); 439 X509Certificate::GetDEREncoded(cert->os_cert_handle(), &derBytes));
447 440
448 std::vector<base::StringPiece> crl_urls; 441 std::vector<base::StringPiece> crl_urls;
449 EXPECT_TRUE(asn1::ExtractCRLURLsFromDERCert(derBytes, &crl_urls)); 442 EXPECT_TRUE(asn1::ExtractCRLURLsFromDERCert(derBytes, &crl_urls));
450 443
451 EXPECT_EQ(1u, crl_urls.size()); 444 EXPECT_EQ(1u, crl_urls.size());
452 if (crl_urls.size() > 0) { 445 if (crl_urls.size() > 0) {
453 EXPECT_EQ("http://SVRSecure-G3-crl.verisign.com/SVRSecureG3.crl", 446 EXPECT_EQ("http://SVRSecure-G3-crl.verisign.com/SVRSecureG3.crl",
454 crl_urls[0].as_string()); 447 crl_urls[0].as_string());
455 } 448 }
456 } 449 }
(...skipping 28 matching lines...) Expand all
485 478
486 // Add the same certificate, but this time with an intermediate. This 479 // Add the same certificate, but this time with an intermediate. This
487 // should result in the intermediate being cached. Note that this is not 480 // should result in the intermediate being cached. Note that this is not
488 // a legitimate chain, but is suitable for testing. 481 // a legitimate chain, but is suitable for testing.
489 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 482 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
490 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 483 reinterpret_cast<const char*>(google_der), sizeof(google_der));
491 thawte_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 484 thawte_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
492 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)); 485 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der));
493 X509Certificate::OSCertHandles intermediates; 486 X509Certificate::OSCertHandles intermediates;
494 intermediates.push_back(thawte_cert_handle); 487 intermediates.push_back(thawte_cert_handle);
495 scoped_refptr<X509Certificate> cert3(X509Certificate::CreateFromHandle( 488 scoped_refptr<X509Certificate> cert3(
496 google_cert_handle, intermediates)); 489 X509Certificate::CreateFromHandle(google_cert_handle, intermediates));
497 X509Certificate::FreeOSCertHandle(google_cert_handle); 490 X509Certificate::FreeOSCertHandle(google_cert_handle);
498 X509Certificate::FreeOSCertHandle(thawte_cert_handle); 491 X509Certificate::FreeOSCertHandle(thawte_cert_handle);
499 492
500 // Test that the new certificate, even with intermediates, results in the 493 // Test that the new certificate, even with intermediates, results in the
501 // same underlying handle being used. 494 // same underlying handle being used.
502 EXPECT_EQ(cert1->os_cert_handle(), cert3->os_cert_handle()); 495 EXPECT_EQ(cert1->os_cert_handle(), cert3->os_cert_handle());
503 // Though they use the same OS handle, the intermediates should be different. 496 // Though they use the same OS handle, the intermediates should be different.
504 EXPECT_NE(cert1->GetIntermediateCertificates().size(), 497 EXPECT_NE(cert1->GetIntermediateCertificates().size(),
505 cert3->GetIntermediateCertificates().size()); 498 cert3->GetIntermediateCertificates().size());
506 } 499 }
507 500
508 TEST(X509CertificateTest, Pickle) { 501 TEST(X509CertificateTest, Pickle) {
509 X509Certificate::OSCertHandle google_cert_handle = 502 X509Certificate::OSCertHandle google_cert_handle =
510 X509Certificate::CreateOSCertHandleFromBytes( 503 X509Certificate::CreateOSCertHandleFromBytes(
511 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 504 reinterpret_cast<const char*>(google_der), sizeof(google_der));
512 X509Certificate::OSCertHandle thawte_cert_handle = 505 X509Certificate::OSCertHandle thawte_cert_handle =
513 X509Certificate::CreateOSCertHandleFromBytes( 506 X509Certificate::CreateOSCertHandleFromBytes(
514 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)); 507 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der));
515 508
516 X509Certificate::OSCertHandles intermediates; 509 X509Certificate::OSCertHandles intermediates;
517 intermediates.push_back(thawte_cert_handle); 510 intermediates.push_back(thawte_cert_handle);
518 scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromHandle( 511 scoped_refptr<X509Certificate> cert =
519 google_cert_handle, intermediates); 512 X509Certificate::CreateFromHandle(google_cert_handle, intermediates);
520 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert.get()); 513 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert.get());
521 514
522 X509Certificate::FreeOSCertHandle(google_cert_handle); 515 X509Certificate::FreeOSCertHandle(google_cert_handle);
523 X509Certificate::FreeOSCertHandle(thawte_cert_handle); 516 X509Certificate::FreeOSCertHandle(thawte_cert_handle);
524 517
525 Pickle pickle; 518 Pickle pickle;
526 cert->Persist(&pickle); 519 cert->Persist(&pickle);
527 520
528 PickleIterator iter(pickle); 521 PickleIterator iter(pickle);
529 scoped_refptr<X509Certificate> cert_from_pickle = 522 scoped_refptr<X509Certificate> cert_from_pickle =
(...skipping 29 matching lines...) Expand all
559 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID)); 552 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID));
560 EXPECT_FALSE(policy.HasAllowedCert()); 553 EXPECT_FALSE(policy.HasAllowedCert());
561 EXPECT_FALSE(policy.HasDeniedCert()); 554 EXPECT_FALSE(policy.HasDeniedCert());
562 555
563 // Test adding one certificate with one error. 556 // Test adding one certificate with one error.
564 policy.Allow(google_cert.get(), CERT_STATUS_DATE_INVALID); 557 policy.Allow(google_cert.get(), CERT_STATUS_DATE_INVALID);
565 EXPECT_EQ(CertPolicy::ALLOWED, 558 EXPECT_EQ(CertPolicy::ALLOWED,
566 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID)); 559 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID));
567 EXPECT_EQ(CertPolicy::UNKNOWN, 560 EXPECT_EQ(CertPolicy::UNKNOWN,
568 policy.Check(google_cert.get(), CERT_STATUS_COMMON_NAME_INVALID)); 561 policy.Check(google_cert.get(), CERT_STATUS_COMMON_NAME_INVALID));
569 EXPECT_EQ(CertPolicy::UNKNOWN, 562 EXPECT_EQ(
570 policy.Check(google_cert.get(), 563 CertPolicy::UNKNOWN,
571 CERT_STATUS_DATE_INVALID | CERT_STATUS_COMMON_NAME_INVALID)); 564 policy.Check(google_cert.get(),
565 CERT_STATUS_DATE_INVALID | CERT_STATUS_COMMON_NAME_INVALID));
572 EXPECT_EQ(CertPolicy::UNKNOWN, 566 EXPECT_EQ(CertPolicy::UNKNOWN,
573 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID)); 567 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID));
574 EXPECT_TRUE(policy.HasAllowedCert()); 568 EXPECT_TRUE(policy.HasAllowedCert());
575 EXPECT_FALSE(policy.HasDeniedCert()); 569 EXPECT_FALSE(policy.HasDeniedCert());
576 570
577 // Test saving the same certificate with a new error. 571 // Test saving the same certificate with a new error.
578 policy.Allow(google_cert.get(), CERT_STATUS_AUTHORITY_INVALID); 572 policy.Allow(google_cert.get(), CERT_STATUS_AUTHORITY_INVALID);
579 EXPECT_EQ(CertPolicy::UNKNOWN, 573 EXPECT_EQ(CertPolicy::UNKNOWN,
580 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID)); 574 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID));
581 EXPECT_EQ(CertPolicy::ALLOWED, 575 EXPECT_EQ(CertPolicy::ALLOWED,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 EXPECT_TRUE(policy.HasDeniedCert()); 621 EXPECT_TRUE(policy.HasDeniedCert());
628 622
629 // Test denying an overlapping certificate. 623 // Test denying an overlapping certificate.
630 policy.Allow(google_cert.get(), 624 policy.Allow(google_cert.get(),
631 CERT_STATUS_COMMON_NAME_INVALID | CERT_STATUS_DATE_INVALID); 625 CERT_STATUS_COMMON_NAME_INVALID | CERT_STATUS_DATE_INVALID);
632 policy.Deny(google_cert.get(), CERT_STATUS_DATE_INVALID); 626 policy.Deny(google_cert.get(), CERT_STATUS_DATE_INVALID);
633 EXPECT_EQ(CertPolicy::DENIED, 627 EXPECT_EQ(CertPolicy::DENIED,
634 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID)); 628 policy.Check(google_cert.get(), CERT_STATUS_DATE_INVALID));
635 EXPECT_EQ(CertPolicy::UNKNOWN, 629 EXPECT_EQ(CertPolicy::UNKNOWN,
636 policy.Check(google_cert.get(), CERT_STATUS_COMMON_NAME_INVALID)); 630 policy.Check(google_cert.get(), CERT_STATUS_COMMON_NAME_INVALID));
637 EXPECT_EQ(CertPolicy::DENIED, 631 EXPECT_EQ(
638 policy.Check(google_cert.get(), 632 CertPolicy::DENIED,
639 CERT_STATUS_COMMON_NAME_INVALID | CERT_STATUS_DATE_INVALID)); 633 policy.Check(google_cert.get(),
634 CERT_STATUS_COMMON_NAME_INVALID | CERT_STATUS_DATE_INVALID));
640 635
641 // Test denying an overlapping certificate (other direction). 636 // Test denying an overlapping certificate (other direction).
642 policy.Allow(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID); 637 policy.Allow(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID);
643 policy.Deny(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID); 638 policy.Deny(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID);
644 policy.Deny(webkit_cert.get(), CERT_STATUS_DATE_INVALID); 639 policy.Deny(webkit_cert.get(), CERT_STATUS_DATE_INVALID);
645 EXPECT_EQ(CertPolicy::DENIED, 640 EXPECT_EQ(CertPolicy::DENIED,
646 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID)); 641 policy.Check(webkit_cert.get(), CERT_STATUS_COMMON_NAME_INVALID));
647 EXPECT_EQ(CertPolicy::DENIED, 642 EXPECT_EQ(CertPolicy::DENIED,
648 policy.Check(webkit_cert.get(), CERT_STATUS_DATE_INVALID)); 643 policy.Check(webkit_cert.get(), CERT_STATUS_DATE_INVALID));
649 } 644 }
650 645
651 TEST(X509CertificateTest, IntermediateCertificates) { 646 TEST(X509CertificateTest, IntermediateCertificates) {
652 scoped_refptr<X509Certificate> webkit_cert( 647 scoped_refptr<X509Certificate> webkit_cert(X509Certificate::CreateFromBytes(
653 X509Certificate::CreateFromBytes( 648 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
654 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
655 649
656 scoped_refptr<X509Certificate> thawte_cert( 650 scoped_refptr<X509Certificate> thawte_cert(X509Certificate::CreateFromBytes(
657 X509Certificate::CreateFromBytes( 651 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)));
658 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)));
659 652
660 X509Certificate::OSCertHandle google_handle; 653 X509Certificate::OSCertHandle google_handle;
661 // Create object with no intermediates: 654 // Create object with no intermediates:
662 google_handle = X509Certificate::CreateOSCertHandleFromBytes( 655 google_handle = X509Certificate::CreateOSCertHandleFromBytes(
663 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 656 reinterpret_cast<const char*>(google_der), sizeof(google_der));
664 X509Certificate::OSCertHandles intermediates1; 657 X509Certificate::OSCertHandles intermediates1;
665 scoped_refptr<X509Certificate> cert1; 658 scoped_refptr<X509Certificate> cert1;
666 cert1 = X509Certificate::CreateFromHandle(google_handle, intermediates1); 659 cert1 = X509Certificate::CreateFromHandle(google_handle, intermediates1);
667 EXPECT_EQ(0u, cert1->GetIntermediateCertificates().size()); 660 EXPECT_EQ(0u, cert1->GetIntermediateCertificates().size());
668 661
(...skipping 18 matching lines...) Expand all
687 } 680 }
688 681
689 TEST(X509CertificateTest, IsIssuedByEncoded) { 682 TEST(X509CertificateTest, IsIssuedByEncoded) {
690 base::FilePath certs_dir = GetTestCertsDirectory(); 683 base::FilePath certs_dir = GetTestCertsDirectory();
691 684
692 // Test a client certificate from MIT. 685 // Test a client certificate from MIT.
693 scoped_refptr<X509Certificate> mit_davidben_cert( 686 scoped_refptr<X509Certificate> mit_davidben_cert(
694 ImportCertFromFile(certs_dir, "mit.davidben.der")); 687 ImportCertFromFile(certs_dir, "mit.davidben.der"));
695 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert); 688 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert);
696 689
697 std::string mit_issuer(reinterpret_cast<const char*>(MITDN), 690 std::string mit_issuer(reinterpret_cast<const char*>(MITDN), sizeof(MITDN));
698 sizeof(MITDN));
699 691
700 // Test a certificate from Google, issued by Thawte 692 // Test a certificate from Google, issued by Thawte
701 scoped_refptr<X509Certificate> google_cert( 693 scoped_refptr<X509Certificate> google_cert(
702 ImportCertFromFile(certs_dir, "google.single.der")); 694 ImportCertFromFile(certs_dir, "google.single.der"));
703 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_cert); 695 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_cert);
704 696
705 std::string thawte_issuer(reinterpret_cast<const char*>(ThawteDN), 697 std::string thawte_issuer(reinterpret_cast<const char*>(ThawteDN),
706 sizeof(ThawteDN)); 698 sizeof(ThawteDN));
707 699
708 // Check that the David Ben certificate is issued by MIT, but not 700 // Check that the David Ben certificate is issued by MIT, but not
(...skipping 14 matching lines...) Expand all
723 // Check that they both pass when given a list of the two issuers. 715 // Check that they both pass when given a list of the two issuers.
724 issuers.clear(); 716 issuers.clear();
725 issuers.push_back(mit_issuer); 717 issuers.push_back(mit_issuer);
726 issuers.push_back(thawte_issuer); 718 issuers.push_back(thawte_issuer);
727 EXPECT_TRUE(mit_davidben_cert->IsIssuedByEncoded(issuers)); 719 EXPECT_TRUE(mit_davidben_cert->IsIssuedByEncoded(issuers));
728 EXPECT_TRUE(google_cert->IsIssuedByEncoded(issuers)); 720 EXPECT_TRUE(google_cert->IsIssuedByEncoded(issuers));
729 } 721 }
730 722
731 TEST(X509CertificateTest, IsIssuedByEncodedWithIntermediates) { 723 TEST(X509CertificateTest, IsIssuedByEncodedWithIntermediates) {
732 static const unsigned char kPolicyRootDN[] = { 724 static const unsigned char kPolicyRootDN[] = {
733 0x30, 0x1e, 0x31, 0x1c, 0x30, 0x1a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 725 0x30, 0x1e, 0x31, 0x1c, 0x30, 0x1a, 0x06, 0x03, 0x55, 0x04, 0x03,
734 0x13, 0x50, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x20, 0x54, 0x65, 0x73, 0x74, 726 0x0c, 0x13, 0x50, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x20, 0x54, 0x65,
735 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41 727 0x73, 0x74, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41};
736 };
737 static const unsigned char kPolicyIntermediateDN[] = { 728 static const unsigned char kPolicyIntermediateDN[] = {
738 0x30, 0x26, 0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 729 0x30, 0x26, 0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04,
739 0x1b, 0x50, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x20, 0x54, 0x65, 0x73, 0x74, 730 0x03, 0x0c, 0x1b, 0x50, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x20,
740 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 731 0x54, 0x65, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72,
741 0x65, 0x20, 0x43, 0x41 732 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41};
742 };
743 733
744 base::FilePath certs_dir = GetTestCertsDirectory(); 734 base::FilePath certs_dir = GetTestCertsDirectory();
745 735
746 CertificateList policy_chain = CreateCertificateListFromFile( 736 CertificateList policy_chain = CreateCertificateListFromFile(
747 certs_dir, "explicit-policy-chain.pem", X509Certificate::FORMAT_AUTO); 737 certs_dir, "explicit-policy-chain.pem", X509Certificate::FORMAT_AUTO);
748 ASSERT_EQ(3u, policy_chain.size()); 738 ASSERT_EQ(3u, policy_chain.size());
749 739
750 // The intermediate CA certificate's policyConstraints extension has a 740 // The intermediate CA certificate's policyConstraints extension has a
751 // requireExplicitPolicy field with SkipCerts=0. 741 // requireExplicitPolicy field with SkipCerts=0.
752 std::string policy_intermediate_dn( 742 std::string policy_intermediate_dn(
753 reinterpret_cast<const char*>(kPolicyIntermediateDN), 743 reinterpret_cast<const char*>(kPolicyIntermediateDN),
754 sizeof(kPolicyIntermediateDN)); 744 sizeof(kPolicyIntermediateDN));
755 std::string policy_root_dn(reinterpret_cast<const char*>(kPolicyRootDN), 745 std::string policy_root_dn(reinterpret_cast<const char*>(kPolicyRootDN),
756 sizeof(kPolicyRootDN)); 746 sizeof(kPolicyRootDN));
757 747
758 X509Certificate::OSCertHandles intermediates; 748 X509Certificate::OSCertHandles intermediates;
759 intermediates.push_back(policy_chain[1]->os_cert_handle()); 749 intermediates.push_back(policy_chain[1]->os_cert_handle());
760 scoped_refptr<X509Certificate> cert_chain = 750 scoped_refptr<X509Certificate> cert_chain = X509Certificate::CreateFromHandle(
761 X509Certificate::CreateFromHandle(policy_chain[0]->os_cert_handle(), 751 policy_chain[0]->os_cert_handle(), intermediates);
762 intermediates);
763 752
764 std::vector<std::string> issuers; 753 std::vector<std::string> issuers;
765 754
766 // Check that the chain is issued by the intermediate. 755 // Check that the chain is issued by the intermediate.
767 issuers.clear(); 756 issuers.clear();
768 issuers.push_back(policy_intermediate_dn); 757 issuers.push_back(policy_intermediate_dn);
769 EXPECT_TRUE(cert_chain->IsIssuedByEncoded(issuers)); 758 EXPECT_TRUE(cert_chain->IsIssuedByEncoded(issuers));
770 759
771 // Check that the chain is also issued by the root. 760 // Check that the chain is also issued by the root.
772 issuers.clear(); 761 issuers.clear();
(...skipping 20 matching lines...) Expand all
793 782
794 #if defined(USE_NSS) 783 #if defined(USE_NSS)
795 TEST(X509CertificateTest, GetDefaultNickname) { 784 TEST(X509CertificateTest, GetDefaultNickname) {
796 base::FilePath certs_dir = GetTestCertsDirectory(); 785 base::FilePath certs_dir = GetTestCertsDirectory();
797 786
798 scoped_refptr<X509Certificate> test_cert( 787 scoped_refptr<X509Certificate> test_cert(
799 ImportCertFromFile(certs_dir, "no_subject_common_name_cert.pem")); 788 ImportCertFromFile(certs_dir, "no_subject_common_name_cert.pem"));
800 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert); 789 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert);
801 790
802 std::string nickname = test_cert->GetDefaultNickname(USER_CERT); 791 std::string nickname = test_cert->GetDefaultNickname(USER_CERT);
803 EXPECT_EQ("wtc@google.com's COMODO Client Authentication and " 792 EXPECT_EQ(
804 "Secure Email CA ID", nickname); 793 "wtc@google.com's COMODO Client Authentication and "
794 "Secure Email CA ID",
795 nickname);
805 } 796 }
806 #endif 797 #endif
807 798
808 const struct CertificateFormatTestData { 799 const struct CertificateFormatTestData {
809 const char* file_name; 800 const char* file_name;
810 X509Certificate::Format format; 801 X509Certificate::Format format;
811 uint8* chain_fingerprints[3]; 802 uint8* chain_fingerprints[3];
812 } kFormatTestData[] = { 803 } kFormatTestData[] = {
813 // DER Parsing - single certificate, DER encoded 804 // DER Parsing - single certificate, DER encoded
814 { "google.single.der", X509Certificate::FORMAT_SINGLE_CERTIFICATE, 805 {"google.single.der",
815 { google_parse_fingerprint, 806 X509Certificate::FORMAT_SINGLE_CERTIFICATE,
816 NULL, } }, 807 {
817 // DER parsing - single certificate, PEM encoded 808 google_parse_fingerprint, NULL,
818 { "google.single.pem", X509Certificate::FORMAT_SINGLE_CERTIFICATE, 809 }},
819 { google_parse_fingerprint, 810 // DER parsing - single certificate, PEM encoded
820 NULL, } }, 811 {"google.single.pem",
821 // PEM parsing - single certificate, PEM encoded with a PEB of 812 X509Certificate::FORMAT_SINGLE_CERTIFICATE,
822 // "CERTIFICATE" 813 {
823 { "google.single.pem", X509Certificate::FORMAT_PEM_CERT_SEQUENCE, 814 google_parse_fingerprint, NULL,
824 { google_parse_fingerprint, 815 }},
825 NULL, } }, 816 // PEM parsing - single certificate, PEM encoded with a PEB of
826 // PEM parsing - sequence of certificates, PEM encoded with a PEB of 817 // "CERTIFICATE"
827 // "CERTIFICATE" 818 {"google.single.pem",
828 { "google.chain.pem", X509Certificate::FORMAT_PEM_CERT_SEQUENCE, 819 X509Certificate::FORMAT_PEM_CERT_SEQUENCE,
829 { google_parse_fingerprint, 820 {
830 thawte_parse_fingerprint, 821 google_parse_fingerprint, NULL,
831 NULL, } }, 822 }},
832 // PKCS#7 parsing - "degenerate" SignedData collection of certificates, DER 823 // PEM parsing - sequence of certificates, PEM encoded with a PEB of
833 // encoding 824 // "CERTIFICATE"
834 { "google.binary.p7b", X509Certificate::FORMAT_PKCS7, 825 {"google.chain.pem",
835 { google_parse_fingerprint, 826 X509Certificate::FORMAT_PEM_CERT_SEQUENCE,
836 thawte_parse_fingerprint, 827 {
837 NULL, } }, 828 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
838 // PKCS#7 parsing - "degenerate" SignedData collection of certificates, PEM 829 }},
839 // encoded with a PEM PEB of "CERTIFICATE" 830 // PKCS#7 parsing - "degenerate" SignedData collection of certificates,
840 { "google.pem_cert.p7b", X509Certificate::FORMAT_PKCS7, 831 // DER
841 { google_parse_fingerprint, 832 // encoding
842 thawte_parse_fingerprint, 833 {"google.binary.p7b",
843 NULL, } }, 834 X509Certificate::FORMAT_PKCS7,
844 // PKCS#7 parsing - "degenerate" SignedData collection of certificates, PEM 835 {
845 // encoded with a PEM PEB of "PKCS7" 836 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
846 { "google.pem_pkcs7.p7b", X509Certificate::FORMAT_PKCS7, 837 }},
847 { google_parse_fingerprint, 838 // PKCS#7 parsing - "degenerate" SignedData collection of certificates,
848 thawte_parse_fingerprint, 839 // PEM
849 NULL, } }, 840 // encoded with a PEM PEB of "CERTIFICATE"
850 // All of the above, this time using auto-detection 841 {"google.pem_cert.p7b",
851 { "google.single.der", X509Certificate::FORMAT_AUTO, 842 X509Certificate::FORMAT_PKCS7,
852 { google_parse_fingerprint, 843 {
853 NULL, } }, 844 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
854 { "google.single.pem", X509Certificate::FORMAT_AUTO, 845 }},
855 { google_parse_fingerprint, 846 // PKCS#7 parsing - "degenerate" SignedData collection of certificates,
856 NULL, } }, 847 // PEM
857 { "google.chain.pem", X509Certificate::FORMAT_AUTO, 848 // encoded with a PEM PEB of "PKCS7"
858 { google_parse_fingerprint, 849 {"google.pem_pkcs7.p7b",
859 thawte_parse_fingerprint, 850 X509Certificate::FORMAT_PKCS7,
860 NULL, } }, 851 {
861 { "google.binary.p7b", X509Certificate::FORMAT_AUTO, 852 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
862 { google_parse_fingerprint, 853 }},
863 thawte_parse_fingerprint, 854 // All of the above, this time using auto-detection
864 NULL, } }, 855 {"google.single.der",
865 { "google.pem_cert.p7b", X509Certificate::FORMAT_AUTO, 856 X509Certificate::FORMAT_AUTO,
866 { google_parse_fingerprint, 857 {
867 thawte_parse_fingerprint, 858 google_parse_fingerprint, NULL,
868 NULL, } }, 859 }},
869 { "google.pem_pkcs7.p7b", X509Certificate::FORMAT_AUTO, 860 {"google.single.pem",
870 { google_parse_fingerprint, 861 X509Certificate::FORMAT_AUTO,
871 thawte_parse_fingerprint, 862 {
872 NULL, } }, 863 google_parse_fingerprint, NULL,
864 }},
865 {"google.chain.pem",
866 X509Certificate::FORMAT_AUTO,
867 {
868 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
869 }},
870 {"google.binary.p7b",
871 X509Certificate::FORMAT_AUTO,
872 {
873 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
874 }},
875 {"google.pem_cert.p7b",
876 X509Certificate::FORMAT_AUTO,
877 {
878 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
879 }},
880 {"google.pem_pkcs7.p7b",
881 X509Certificate::FORMAT_AUTO,
882 {
883 google_parse_fingerprint, thawte_parse_fingerprint, NULL,
884 }},
873 }; 885 };
874 886
875 class X509CertificateParseTest 887 class X509CertificateParseTest
876 : public testing::TestWithParam<CertificateFormatTestData> { 888 : public testing::TestWithParam<CertificateFormatTestData> {
877 public: 889 public:
878 virtual ~X509CertificateParseTest() {} 890 virtual ~X509CertificateParseTest() {}
879 virtual void SetUp() { 891 virtual void SetUp() { test_data_ = GetParam(); }
880 test_data_ = GetParam();
881 }
882 virtual void TearDown() {} 892 virtual void TearDown() {}
883 893
884 protected: 894 protected:
885 CertificateFormatTestData test_data_; 895 CertificateFormatTestData test_data_;
886 }; 896 };
887 897
888 TEST_P(X509CertificateParseTest, CanParseFormat) { 898 TEST_P(X509CertificateParseTest, CanParseFormat) {
889 base::FilePath certs_dir = GetTestCertsDirectory(); 899 base::FilePath certs_dir = GetTestCertsDirectory();
890 CertificateList certs = CreateCertificateListFromFile( 900 CertificateList certs = CreateCertificateListFromFile(
891 certs_dir, test_data_.file_name, test_data_.format); 901 certs_dir, test_data_.file_name, test_data_.format);
892 ASSERT_FALSE(certs.empty()); 902 ASSERT_FALSE(certs.empty());
893 ASSERT_LE(certs.size(), arraysize(test_data_.chain_fingerprints)); 903 ASSERT_LE(certs.size(), arraysize(test_data_.chain_fingerprints));
894 CheckGoogleCert(certs.front(), google_parse_fingerprint, 904 CheckGoogleCert(certs.front(),
895 kGoogleParseValidFrom, kGoogleParseValidTo); 905 google_parse_fingerprint,
906 kGoogleParseValidFrom,
907 kGoogleParseValidTo);
896 908
897 size_t i; 909 size_t i;
898 for (i = 0; i < arraysize(test_data_.chain_fingerprints); ++i) { 910 for (i = 0; i < arraysize(test_data_.chain_fingerprints); ++i) {
899 if (test_data_.chain_fingerprints[i] == NULL) { 911 if (test_data_.chain_fingerprints[i] == NULL) {
900 // No more test certificates expected - make sure no more were 912 // No more test certificates expected - make sure no more were
901 // returned before marking this test a success. 913 // returned before marking this test a success.
902 EXPECT_EQ(i, certs.size()); 914 EXPECT_EQ(i, certs.size());
903 break; 915 break;
904 } 916 }
905 917
906 // A cert is expected - make sure that one was parsed. 918 // A cert is expected - make sure that one was parsed.
907 ASSERT_LT(i, certs.size()); 919 ASSERT_LT(i, certs.size());
908 920
909 // Compare the parsed certificate with the expected certificate, by 921 // Compare the parsed certificate with the expected certificate, by
910 // comparing fingerprints. 922 // comparing fingerprints.
911 const X509Certificate* cert = certs[i].get(); 923 const X509Certificate* cert = certs[i].get();
912 const SHA1HashValue& actual_fingerprint = cert->fingerprint(); 924 const SHA1HashValue& actual_fingerprint = cert->fingerprint();
913 uint8* expected_fingerprint = test_data_.chain_fingerprints[i]; 925 uint8* expected_fingerprint = test_data_.chain_fingerprints[i];
914 926
915 for (size_t j = 0; j < 20; ++j) 927 for (size_t j = 0; j < 20; ++j)
916 EXPECT_EQ(expected_fingerprint[j], actual_fingerprint.data[j]); 928 EXPECT_EQ(expected_fingerprint[j], actual_fingerprint.data[j]);
917 } 929 }
918 } 930 }
919 931
920 INSTANTIATE_TEST_CASE_P(, X509CertificateParseTest, 932 INSTANTIATE_TEST_CASE_P(,
933 X509CertificateParseTest,
921 testing::ValuesIn(kFormatTestData)); 934 testing::ValuesIn(kFormatTestData));
922 935
923 struct CertificateNameVerifyTestData { 936 struct CertificateNameVerifyTestData {
924 // true iff we expect hostname to match an entry in cert_names. 937 // true iff we expect hostname to match an entry in cert_names.
925 bool expected; 938 bool expected;
926 // The hostname to match. 939 // The hostname to match.
927 const char* hostname; 940 const char* hostname;
928 // Common name, may be used if |dns_names| or |ip_addrs| are empty. 941 // Common name, may be used if |dns_names| or |ip_addrs| are empty.
929 const char* common_name; 942 const char* common_name;
930 // Comma separated list of certificate names to match against. Any occurrence 943 // Comma separated list of certificate names to match against. Any occurrence
931 // of '#' will be replaced with a null character before processing. 944 // of '#' will be replaced with a null character before processing.
932 const char* dns_names; 945 const char* dns_names;
933 // Comma separated list of certificate IP Addresses to match against. Each 946 // Comma separated list of certificate IP Addresses to match against. Each
934 // address is x prefixed 16 byte hex code for v6 or dotted-decimals for v4. 947 // address is x prefixed 16 byte hex code for v6 or dotted-decimals for v4.
935 const char* ip_addrs; 948 const char* ip_addrs;
936 }; 949 };
937 950
938 // GTest 'magic' pretty-printer, so that if/when a test fails, it knows how 951 // GTest 'magic' pretty-printer, so that if/when a test fails, it knows how
939 // to output the parameter that was passed. Without this, it will simply 952 // to output the parameter that was passed. Without this, it will simply
940 // attempt to print out the first twenty bytes of the object, which depending 953 // attempt to print out the first twenty bytes of the object, which depending
941 // on platform and alignment, may result in an invalid read. 954 // on platform and alignment, may result in an invalid read.
942 void PrintTo(const CertificateNameVerifyTestData& data, std::ostream* os) { 955 void PrintTo(const CertificateNameVerifyTestData& data, std::ostream* os) {
943 ASSERT_TRUE(data.hostname && data.common_name); 956 ASSERT_TRUE(data.hostname && data.common_name);
944 // Using StringPiece to allow for optional fields being NULL. 957 // Using StringPiece to allow for optional fields being NULL.
945 *os << " expected: " << data.expected 958 *os << " expected: " << data.expected << "; hostname: " << data.hostname
946 << "; hostname: " << data.hostname
947 << "; common_name: " << data.common_name 959 << "; common_name: " << data.common_name
948 << "; dns_names: " << base::StringPiece(data.dns_names) 960 << "; dns_names: " << base::StringPiece(data.dns_names)
949 << "; ip_addrs: " << base::StringPiece(data.ip_addrs); 961 << "; ip_addrs: " << base::StringPiece(data.ip_addrs);
950 } 962 }
951 963
952 const CertificateNameVerifyTestData kNameVerifyTestData[] = { 964 const CertificateNameVerifyTestData kNameVerifyTestData[] = {
953 { true, "foo.com", "foo.com" }, 965 {true, "foo.com", "foo.com"},
954 { true, "f", "f" }, 966 {true, "f", "f"},
955 { false, "h", "i" }, 967 {false, "h", "i"},
956 { true, "bar.foo.com", "*.foo.com" }, 968 {true, "bar.foo.com", "*.foo.com"},
957 { true, "www.test.fr", "common.name", 969 {true, "www.test.fr", "common.name",
958 "*.test.com,*.test.co.uk,*.test.de,*.test.fr" }, 970 "*.test.com,*.test.co.uk,*.test.de,*.test.fr"},
959 { true, "wwW.tESt.fr", "common.name", 971 {true, "wwW.tESt.fr", "common.name", ",*.*,*.test.de,*.test.FR,www"},
960 ",*.*,*.test.de,*.test.FR,www" }, 972 {false, "f.uk", ".uk"},
961 { false, "f.uk", ".uk" }, 973 {false, "w.bar.foo.com", "?.bar.foo.com"},
962 { false, "w.bar.foo.com", "?.bar.foo.com" }, 974 {false, "www.foo.com", "(www|ftp).foo.com"},
963 { false, "www.foo.com", "(www|ftp).foo.com" }, 975 {false, "www.foo.com", "www.foo.com#"}, // # = null char.
964 { false, "www.foo.com", "www.foo.com#" }, // # = null char. 976 {false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#"},
965 { false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#" }, 977 {false, "www.house.example", "ww.house.example"},
966 { false, "www.house.example", "ww.house.example" }, 978 {false, "test.org", "", "www.test.org,*.test.org,*.org"},
967 { false, "test.org", "", "www.test.org,*.test.org,*.org" }, 979 {false, "w.bar.foo.com", "w*.bar.foo.com"},
968 { false, "w.bar.foo.com", "w*.bar.foo.com" }, 980 {false, "www.bar.foo.com", "ww*ww.bar.foo.com"},
969 { false, "www.bar.foo.com", "ww*ww.bar.foo.com" }, 981 {false, "wwww.bar.foo.com", "ww*ww.bar.foo.com"},
970 { false, "wwww.bar.foo.com", "ww*ww.bar.foo.com" }, 982 {true, "wwww.bar.foo.com", "w*w.bar.foo.com"},
971 { true, "wwww.bar.foo.com", "w*w.bar.foo.com" }, 983 {false, "wwww.bar.foo.com", "w*w.bar.foo.c0m"},
972 { false, "wwww.bar.foo.com", "w*w.bar.foo.c0m" }, 984 {true, "WALLY.bar.foo.com", "wa*.bar.foo.com"},
973 { true, "WALLY.bar.foo.com", "wa*.bar.foo.com" }, 985 {true, "wally.bar.foo.com", "*Ly.bar.foo.com"},
974 { true, "wally.bar.foo.com", "*Ly.bar.foo.com" }, 986 {true, "ww%57.foo.com", "", "www.foo.com"},
975 { true, "ww%57.foo.com", "", "www.foo.com" }, 987 {true, "www&.foo.com", "www%26.foo.com"},
976 { true, "www&.foo.com", "www%26.foo.com" },
977 // Common name must not be used if subject alternative name was provided. 988 // Common name must not be used if subject alternative name was provided.
978 { false, "www.test.co.jp", "www.test.co.jp", 989 {false, "www.test.co.jp", "www.test.co.jp",
979 "*.test.de,*.jp,www.test.co.uk,www.*.co.jp" }, 990 "*.test.de,*.jp,www.test.co.uk,www.*.co.jp"},
980 { false, "www.bar.foo.com", "www.bar.foo.com", 991 {false, "www.bar.foo.com", "www.bar.foo.com",
981 "*.foo.com,*.*.foo.com,*.*.bar.foo.com,*..bar.foo.com," }, 992 "*.foo.com,*.*.foo.com,*.*.bar.foo.com,*..bar.foo.com,"},
982 { false, "www.bath.org", "www.bath.org", "", "20.30.40.50" }, 993 {false, "www.bath.org", "www.bath.org", "", "20.30.40.50"},
983 { false, "66.77.88.99", "www.bath.org", "www.bath.org" }, 994 {false, "66.77.88.99", "www.bath.org", "www.bath.org"},
984 // IDN tests 995 // IDN tests
985 { true, "xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br" }, 996 {true, "xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br"},
986 { true, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br" }, 997 {true, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br"},
987 { false, "xn--poema-9qae5a.com.br", "", "*.xn--poema-9qae5a.com.br," 998 {false, "xn--poema-9qae5a.com.br", "",
988 "xn--poema-*.com.br," 999 "*.xn--poema-9qae5a.com.br,"
989 "xn--*-9qae5a.com.br," 1000 "xn--poema-*.com.br,"
990 "*--poema-9qae5a.com.br" }, 1001 "xn--*-9qae5a.com.br,"
1002 "*--poema-9qae5a.com.br"},
991 // The following are adapted from the examples quoted from 1003 // The following are adapted from the examples quoted from
992 // http://tools.ietf.org/html/rfc6125#section-6.4.3 1004 // http://tools.ietf.org/html/rfc6125#section-6.4.3
993 // (e.g., *.example.com would match foo.example.com but 1005 // (e.g., *.example.com would match foo.example.com but
994 // not bar.foo.example.com or example.com). 1006 // not bar.foo.example.com or example.com).
995 { true, "foo.example.com", "*.example.com" }, 1007 {true, "foo.example.com", "*.example.com"},
996 { false, "bar.foo.example.com", "*.example.com" }, 1008 {false, "bar.foo.example.com", "*.example.com"},
997 { false, "example.com", "*.example.com" }, 1009 {false, "example.com", "*.example.com"},
998 // (e.g., baz*.example.net and *baz.example.net and b*z.example.net would 1010 // (e.g., baz*.example.net and *baz.example.net and b*z.example.net would
999 // be taken to match baz1.example.net and foobaz.example.net and 1011 // be taken to match baz1.example.net and foobaz.example.net and
1000 // buzz.example.net, respectively 1012 // buzz.example.net, respectively
1001 { true, "baz1.example.net", "baz*.example.net" }, 1013 {true, "baz1.example.net", "baz*.example.net"},
1002 { true, "foobaz.example.net", "*baz.example.net" }, 1014 {true, "foobaz.example.net", "*baz.example.net"},
1003 { true, "buzz.example.net", "b*z.example.net" }, 1015 {true, "buzz.example.net", "b*z.example.net"},
1004 // Wildcards should not be valid for public registry controlled domains, 1016 // Wildcards should not be valid for public registry controlled domains,
1005 // and unknown/unrecognized domains, at least three domain components must 1017 // and unknown/unrecognized domains, at least three domain components must
1006 // be present. 1018 // be present.
1007 { true, "www.test.example", "*.test.example" }, 1019 {true, "www.test.example", "*.test.example"},
1008 { true, "test.example.co.uk", "*.example.co.uk" }, 1020 {true, "test.example.co.uk", "*.example.co.uk"},
1009 { false, "test.example", "*.exmaple" }, 1021 {false, "test.example", "*.exmaple"},
1010 { false, "example.co.uk", "*.co.uk" }, 1022 {false, "example.co.uk", "*.co.uk"},
1011 { false, "foo.com", "*.com" }, 1023 {false, "foo.com", "*.com"},
1012 { false, "foo.us", "*.us" }, 1024 {false, "foo.us", "*.us"},
1013 { false, "foo", "*" }, 1025 {false, "foo", "*"},
1014 // IDN variants of wildcards and registry controlled domains. 1026 // IDN variants of wildcards and registry controlled domains.
1015 { true, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br" }, 1027 {true, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br"},
1016 { true, "test.example.xn--mgbaam7a8h", "*.example.xn--mgbaam7a8h" }, 1028 {true, "test.example.xn--mgbaam7a8h", "*.example.xn--mgbaam7a8h"},
1017 { false, "xn--poema-9qae5a.com.br", "*.com.br" }, 1029 {false, "xn--poema-9qae5a.com.br", "*.com.br"},
1018 { false, "example.xn--mgbaam7a8h", "*.xn--mgbaam7a8h" }, 1030 {false, "example.xn--mgbaam7a8h", "*.xn--mgbaam7a8h"},
1019 // Wildcards should be permissible for 'private' registry controlled 1031 // Wildcards should be permissible for 'private' registry controlled
1020 // domains. 1032 // domains.
1021 { true, "www.appspot.com", "*.appspot.com" }, 1033 {true, "www.appspot.com", "*.appspot.com"},
1022 { true, "foo.s3.amazonaws.com", "*.s3.amazonaws.com" }, 1034 {true, "foo.s3.amazonaws.com", "*.s3.amazonaws.com"},
1023 // Multiple wildcards are not valid. 1035 // Multiple wildcards are not valid.
1024 { false, "foo.example.com", "*.*.com" }, 1036 {false, "foo.example.com", "*.*.com"},
1025 { false, "foo.bar.example.com", "*.bar.*.com" }, 1037 {false, "foo.bar.example.com", "*.bar.*.com"},
1026 // Absolute vs relative DNS name tests. Although not explicitly specified 1038 // Absolute vs relative DNS name tests. Although not explicitly specified
1027 // in RFC 6125, absolute reference names (those ending in a .) should 1039 // in RFC 6125, absolute reference names (those ending in a .) should
1028 // match either absolute or relative presented names. 1040 // match either absolute or relative presented names.
1029 { true, "foo.com", "foo.com." }, 1041 {true, "foo.com", "foo.com."},
1030 { true, "foo.com.", "foo.com" }, 1042 {true, "foo.com.", "foo.com"},
1031 { true, "foo.com.", "foo.com." }, 1043 {true, "foo.com.", "foo.com."},
1032 { true, "f", "f." }, 1044 {true, "f", "f."},
1033 { true, "f.", "f" }, 1045 {true, "f.", "f"},
1034 { true, "f.", "f." }, 1046 {true, "f.", "f."},
1035 { true, "www-3.bar.foo.com", "*.bar.foo.com." }, 1047 {true, "www-3.bar.foo.com", "*.bar.foo.com."},
1036 { true, "www-3.bar.foo.com.", "*.bar.foo.com" }, 1048 {true, "www-3.bar.foo.com.", "*.bar.foo.com"},
1037 { true, "www-3.bar.foo.com.", "*.bar.foo.com." }, 1049 {true, "www-3.bar.foo.com.", "*.bar.foo.com."},
1038 { false, ".", "." }, 1050 {false, ".", "."},
1039 { false, "example.com", "*.com." }, 1051 {false, "example.com", "*.com."},
1040 { false, "example.com.", "*.com" }, 1052 {false, "example.com.", "*.com"},
1041 { false, "example.com.", "*.com." }, 1053 {false, "example.com.", "*.com."},
1042 { false, "foo.", "*." }, 1054 {false, "foo.", "*."},
1043 { false, "foo", "*." }, 1055 {false, "foo", "*."},
1044 { false, "foo.co.uk", "*.co.uk." }, 1056 {false, "foo.co.uk", "*.co.uk."},
1045 { false, "foo.co.uk.", "*.co.uk." }, 1057 {false, "foo.co.uk.", "*.co.uk."},
1046 // IP addresses in common name; IPv4 only. 1058 // IP addresses in common name; IPv4 only.
1047 { true, "127.0.0.1", "127.0.0.1" }, 1059 {true, "127.0.0.1", "127.0.0.1"},
1048 { true, "192.168.1.1", "192.168.1.1" }, 1060 {true, "192.168.1.1", "192.168.1.1"},
1049 { true, "676768", "0.10.83.160" }, 1061 {true, "676768", "0.10.83.160"},
1050 { true, "1.2.3", "1.2.0.3" }, 1062 {true, "1.2.3", "1.2.0.3"},
1051 { false, "192.169.1.1", "192.168.1.1" }, 1063 {false, "192.169.1.1", "192.168.1.1"},
1052 { false, "12.19.1.1", "12.19.1.1/255.255.255.0" }, 1064 {false, "12.19.1.1", "12.19.1.1/255.255.255.0"},
1053 { false, "FEDC:ba98:7654:3210:FEDC:BA98:7654:3210", 1065 {false, "FEDC:ba98:7654:3210:FEDC:BA98:7654:3210",
1054 "FEDC:BA98:7654:3210:FEDC:ba98:7654:3210" }, 1066 "FEDC:BA98:7654:3210:FEDC:ba98:7654:3210"},
1055 { false, "1111:2222:3333:4444:5555:6666:7777:8888", 1067 {false, "1111:2222:3333:4444:5555:6666:7777:8888",
1056 "1111:2222:3333:4444:5555:6666:7777:8888" }, 1068 "1111:2222:3333:4444:5555:6666:7777:8888"},
1057 { false, "::192.9.5.5", "[::192.9.5.5]" }, 1069 {false, "::192.9.5.5", "[::192.9.5.5]"},
1058 // No wildcard matching in valid IP addresses 1070 // No wildcard matching in valid IP addresses
1059 { false, "::192.9.5.5", "*.9.5.5" }, 1071 {false, "::192.9.5.5", "*.9.5.5"},
1060 { false, "2010:836B:4179::836B:4179", "*:836B:4179::836B:4179" }, 1072 {false, "2010:836B:4179::836B:4179", "*:836B:4179::836B:4179"},
1061 { false, "192.168.1.11", "*.168.1.11" }, 1073 {false, "192.168.1.11", "*.168.1.11"},
1062 { false, "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", "*.]" }, 1074 {false, "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", "*.]"},
1063 // IP addresses in subject alternative name (common name ignored) 1075 // IP addresses in subject alternative name (common name ignored)
1064 { true, "10.1.2.3", "", "", "10.1.2.3" }, 1076 {true, "10.1.2.3", "", "", "10.1.2.3"},
1065 { true, "14.15", "", "", "14.0.0.15" }, 1077 {true, "14.15", "", "", "14.0.0.15"},
1066 { false, "10.1.2.7", "10.1.2.7", "", "10.1.2.6,10.1.2.8" }, 1078 {false, "10.1.2.7", "10.1.2.7", "", "10.1.2.6,10.1.2.8"},
1067 { false, "10.1.2.8", "10.20.2.8", "foo" }, 1079 {false, "10.1.2.8", "10.20.2.8", "foo"},
1068 { true, "::4.5.6.7", "", "", "x00000000000000000000000004050607" }, 1080 {true, "::4.5.6.7", "", "", "x00000000000000000000000004050607"},
1069 { false, "::6.7.8.9", "::6.7.8.9", "::6.7.8.9", 1081 {false, "::6.7.8.9", "::6.7.8.9", "::6.7.8.9",
1070 "x00000000000000000000000006070808,x0000000000000000000000000607080a," 1082 "x00000000000000000000000006070808,x0000000000000000000000000607080a,"
1071 "xff000000000000000000000006070809,6.7.8.9" }, 1083 "xff000000000000000000000006070809,6.7.8.9"},
1072 { true, "FE80::200:f8ff:fe21:67cf", "no.common.name", "", 1084 {true, "FE80::200:f8ff:fe21:67cf", "no.common.name", "",
1073 "x00000000000000000000000006070808,xfe800000000000000200f8fffe2167cf," 1085 "x00000000000000000000000006070808,xfe800000000000000200f8fffe2167cf,"
1074 "xff0000000000000000000000060708ff,10.0.0.1" }, 1086 "xff0000000000000000000000060708ff,10.0.0.1"},
1075 // Numeric only hostnames (none of these are considered valid IP addresses). 1087 // Numeric only hostnames (none of these are considered valid IP addresses).
1076 { false, "12345.6", "12345.6" }, 1088 {false, "12345.6", "12345.6"},
1077 { false, "121.2.3.512", "", "1*1.2.3.512,*1.2.3.512,1*.2.3.512,*.2.3.512", 1089 {false, "121.2.3.512", "", "1*1.2.3.512,*1.2.3.512,1*.2.3.512,*.2.3.512",
1078 "121.2.3.0"}, 1090 "121.2.3.0"},
1079 { false, "1.2.3.4.5.6", "*.2.3.4.5.6" }, 1091 {false, "1.2.3.4.5.6", "*.2.3.4.5.6"},
1080 { true, "1.2.3.4.5", "", "1.2.3.4.5" }, 1092 {true, "1.2.3.4.5", "", "1.2.3.4.5"},
1081 // Invalid host names. 1093 // Invalid host names.
1082 { false, "junk)(£)$*!@~#", "junk)(£)$*!@~#" }, 1094 {false, "junk)(£)$*!@~#", "junk)(£)$*!@~#"},
1083 { false, "www.*.com", "www.*.com" }, 1095 {false, "www.*.com", "www.*.com"},
1084 { false, "w$w.f.com", "w$w.f.com" }, 1096 {false, "w$w.f.com", "w$w.f.com"},
1085 { false, "nocolonallowed:example", "", "nocolonallowed:example" }, 1097 {false, "nocolonallowed:example", "", "nocolonallowed:example"},
1086 { false, "www-1.[::FFFF:129.144.52.38]", "*.[::FFFF:129.144.52.38]" }, 1098 {false, "www-1.[::FFFF:129.144.52.38]", "*.[::FFFF:129.144.52.38]"},
1087 { false, "[::4.5.6.9]", "", "", "x00000000000000000000000004050609" }, 1099 {false, "[::4.5.6.9]", "", "", "x00000000000000000000000004050609"},
1088 }; 1100 };
1089 1101
1090 class X509CertificateNameVerifyTest 1102 class X509CertificateNameVerifyTest
1091 : public testing::TestWithParam<CertificateNameVerifyTestData> { 1103 : public testing::TestWithParam<CertificateNameVerifyTestData> {};
1092 };
1093 1104
1094 TEST_P(X509CertificateNameVerifyTest, VerifyHostname) { 1105 TEST_P(X509CertificateNameVerifyTest, VerifyHostname) {
1095 CertificateNameVerifyTestData test_data = GetParam(); 1106 CertificateNameVerifyTestData test_data = GetParam();
1096 1107
1097 std::string common_name(test_data.common_name); 1108 std::string common_name(test_data.common_name);
1098 ASSERT_EQ(std::string::npos, common_name.find(',')); 1109 ASSERT_EQ(std::string::npos, common_name.find(','));
1099 std::replace(common_name.begin(), common_name.end(), '#', '\0'); 1110 std::replace(common_name.begin(), common_name.end(), '#', '\0');
1100 1111
1101 std::vector<std::string> dns_names, ip_addressses; 1112 std::vector<std::string> dns_names, ip_addressses;
1102 if (test_data.dns_names) { 1113 if (test_data.dns_names) {
1103 // Build up the certificate DNS names list. 1114 // Build up the certificate DNS names list.
1104 std::string dns_name_line(test_data.dns_names); 1115 std::string dns_name_line(test_data.dns_names);
1105 std::replace(dns_name_line.begin(), dns_name_line.end(), '#', '\0'); 1116 std::replace(dns_name_line.begin(), dns_name_line.end(), '#', '\0');
1106 base::SplitString(dns_name_line, ',', &dns_names); 1117 base::SplitString(dns_name_line, ',', &dns_names);
1107 } 1118 }
1108 1119
1109 if (test_data.ip_addrs) { 1120 if (test_data.ip_addrs) {
1110 // Build up the certificate IP address list. 1121 // Build up the certificate IP address list.
1111 std::string ip_addrs_line(test_data.ip_addrs); 1122 std::string ip_addrs_line(test_data.ip_addrs);
1112 std::vector<std::string> ip_addressses_ascii; 1123 std::vector<std::string> ip_addressses_ascii;
1113 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii); 1124 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii);
1114 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) { 1125 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) {
1115 std::string& addr_ascii = ip_addressses_ascii[i]; 1126 std::string& addr_ascii = ip_addressses_ascii[i];
1116 ASSERT_NE(0U, addr_ascii.length()); 1127 ASSERT_NE(0U, addr_ascii.length());
1117 if (addr_ascii[0] == 'x') { // Hex encoded address 1128 if (addr_ascii[0] == 'x') { // Hex encoded address
1118 addr_ascii.erase(0, 1); 1129 addr_ascii.erase(0, 1);
1119 std::vector<uint8> bytes; 1130 std::vector<uint8> bytes;
1120 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes)) 1131 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes))
1121 << "Could not parse hex address " << addr_ascii << " i = " << i; 1132 << "Could not parse hex address " << addr_ascii << " i = " << i;
1122 ip_addressses.push_back(std::string(reinterpret_cast<char*>(&bytes[0]), 1133 ip_addressses.push_back(
1123 bytes.size())); 1134 std::string(reinterpret_cast<char*>(&bytes[0]), bytes.size()));
1124 ASSERT_EQ(16U, ip_addressses.back().size()) << i; 1135 ASSERT_EQ(16U, ip_addressses.back().size()) << i;
1125 } else { // Decimal groups 1136 } else { // Decimal groups
1126 std::vector<std::string> decimals_ascii; 1137 std::vector<std::string> decimals_ascii;
1127 base::SplitString(addr_ascii, '.', &decimals_ascii); 1138 base::SplitString(addr_ascii, '.', &decimals_ascii);
1128 EXPECT_EQ(4U, decimals_ascii.size()) << i; 1139 EXPECT_EQ(4U, decimals_ascii.size()) << i;
1129 std::string addr_bytes; 1140 std::string addr_bytes;
1130 for (size_t j = 0; j < decimals_ascii.size(); ++j) { 1141 for (size_t j = 0; j < decimals_ascii.size(); ++j) {
1131 int decimal_value; 1142 int decimal_value;
1132 EXPECT_TRUE(base::StringToInt(decimals_ascii[j], &decimal_value)); 1143 EXPECT_TRUE(base::StringToInt(decimals_ascii[j], &decimal_value));
1133 EXPECT_GE(decimal_value, 0); 1144 EXPECT_GE(decimal_value, 0);
1134 EXPECT_LE(decimal_value, 255); 1145 EXPECT_LE(decimal_value, 255);
1135 addr_bytes.push_back(static_cast<char>(decimal_value)); 1146 addr_bytes.push_back(static_cast<char>(decimal_value));
1136 } 1147 }
1137 ip_addressses.push_back(addr_bytes); 1148 ip_addressses.push_back(addr_bytes);
1138 ASSERT_EQ(4U, ip_addressses.back().size()) << i; 1149 ASSERT_EQ(4U, ip_addressses.back().size()) << i;
1139 } 1150 }
1140 } 1151 }
1141 } 1152 }
1142 1153
1143 bool unused = false; 1154 bool unused = false;
1144 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname( 1155 EXPECT_EQ(
1145 test_data.hostname, common_name, dns_names, ip_addressses, &unused)); 1156 test_data.expected,
1157 X509Certificate::VerifyHostname(
1158 test_data.hostname, common_name, dns_names, ip_addressses, &unused));
1146 } 1159 }
1147 1160
1148 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest, 1161 INSTANTIATE_TEST_CASE_P(,
1162 X509CertificateNameVerifyTest,
1149 testing::ValuesIn(kNameVerifyTestData)); 1163 testing::ValuesIn(kNameVerifyTestData));
1150 1164
1151 const struct PublicKeyInfoTestData { 1165 const struct PublicKeyInfoTestData {
1152 const char* cert_file; 1166 const char* cert_file;
1153 size_t expected_bits; 1167 size_t expected_bits;
1154 X509Certificate::PublicKeyType expected_type; 1168 X509Certificate::PublicKeyType expected_type;
1155 } kPublicKeyInfoTestData[] = { 1169 } kPublicKeyInfoTestData[] = {
1156 { "768-rsa-ee-by-768-rsa-intermediate.pem", 768, 1170 {"768-rsa-ee-by-768-rsa-intermediate.pem", 768,
1157 X509Certificate::kPublicKeyTypeRSA }, 1171 X509Certificate::kPublicKeyTypeRSA},
1158 { "1024-rsa-ee-by-768-rsa-intermediate.pem", 1024, 1172 {"1024-rsa-ee-by-768-rsa-intermediate.pem", 1024,
1159 X509Certificate::kPublicKeyTypeRSA }, 1173 X509Certificate::kPublicKeyTypeRSA},
1160 { "prime256v1-ecdsa-ee-by-1024-rsa-intermediate.pem", 256, 1174 {"prime256v1-ecdsa-ee-by-1024-rsa-intermediate.pem", 256,
1161 X509Certificate::kPublicKeyTypeECDSA }, 1175 X509Certificate::kPublicKeyTypeECDSA},
1162 }; 1176 };
1163 1177
1164 class X509CertificatePublicKeyInfoTest 1178 class X509CertificatePublicKeyInfoTest
1165 : public testing::TestWithParam<PublicKeyInfoTestData> { 1179 : public testing::TestWithParam<PublicKeyInfoTestData> {};
1166 };
1167 1180
1168 TEST_P(X509CertificatePublicKeyInfoTest, GetPublicKeyInfo) { 1181 TEST_P(X509CertificatePublicKeyInfoTest, GetPublicKeyInfo) {
1169 PublicKeyInfoTestData data = GetParam(); 1182 PublicKeyInfoTestData data = GetParam();
1170 1183
1171 #if defined(OS_WIN) 1184 #if defined(OS_WIN)
1172 if (base::win::GetVersion() < base::win::VERSION_VISTA && 1185 if (base::win::GetVersion() < base::win::VERSION_VISTA &&
1173 data.expected_type == X509Certificate::kPublicKeyTypeECDSA) { 1186 data.expected_type == X509Certificate::kPublicKeyTypeECDSA) {
1174 // ECC is only supported on Vista+. Skip the test. 1187 // ECC is only supported on Vista+. Skip the test.
1175 return; 1188 return;
1176 } 1189 }
1177 #endif 1190 #endif
1178 1191
1179 scoped_refptr<X509Certificate> cert( 1192 scoped_refptr<X509Certificate> cert(
1180 ImportCertFromFile(GetTestCertsDirectory(), data.cert_file)); 1193 ImportCertFromFile(GetTestCertsDirectory(), data.cert_file));
1181 ASSERT_TRUE(cert.get()); 1194 ASSERT_TRUE(cert.get());
1182 1195
1183 size_t actual_bits = 0; 1196 size_t actual_bits = 0;
1184 X509Certificate::PublicKeyType actual_type = 1197 X509Certificate::PublicKeyType actual_type =
1185 X509Certificate::kPublicKeyTypeUnknown; 1198 X509Certificate::kPublicKeyTypeUnknown;
1186 1199
1187 X509Certificate::GetPublicKeyInfo(cert->os_cert_handle(), &actual_bits, 1200 X509Certificate::GetPublicKeyInfo(
1188 &actual_type); 1201 cert->os_cert_handle(), &actual_bits, &actual_type);
1189 1202
1190 EXPECT_EQ(data.expected_bits, actual_bits); 1203 EXPECT_EQ(data.expected_bits, actual_bits);
1191 EXPECT_EQ(data.expected_type, actual_type); 1204 EXPECT_EQ(data.expected_type, actual_type);
1192 } 1205 }
1193 1206
1194 INSTANTIATE_TEST_CASE_P(, X509CertificatePublicKeyInfoTest, 1207 INSTANTIATE_TEST_CASE_P(,
1208 X509CertificatePublicKeyInfoTest,
1195 testing::ValuesIn(kPublicKeyInfoTestData)); 1209 testing::ValuesIn(kPublicKeyInfoTestData));
1196 1210
1197 } // namespace net 1211 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698