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
3 // found in the LICENSE file.
11 #include "base/bind.h"
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/lazy_instance.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/message_loop/message_loop_proxy.h"
17 #include "base/path_service.h"
18 #include "base/run_loop.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "crypto/nss_util.h"
23 #include "crypto/nss_util_internal.h"
24 #include "crypto/scoped_nss_types.h"
25 #include "net/base/crypto_module.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/test_data_directory.h"
28 #include "net/cert/cert_status_flags.h"
29 #include "net/cert/cert_verify_proc_nss.h"
30 #include "net/cert/cert_verify_result.h"
31 #include "net/cert/nss_cert_database.h"
32 #include "net/cert/x509_certificate.h"
33 #include "net/test/cert_test_util.h"
34 #include "net/third_party/mozilla_security_manager/nsNSSCertificateDB.h"
35 #include "testing/gtest/include/gtest/gtest.h"
37 // In NSS 3.13, CERTDB_VALID_PEER was renamed CERTDB_TERMINAL_RECORD. So we use
38 // the new name of the macro.
39 #if !defined(CERTDB_TERMINAL_RECORD)
40 #define CERTDB_TERMINAL_RECORD CERTDB_VALID_PEER
43 using base::ASCIIToUTF16
;
49 void SwapCertList(CertificateList
* destination
,
50 scoped_ptr
<CertificateList
> source
) {
51 ASSERT_TRUE(destination
);
52 destination
->swap(*source
);
57 class CertDatabaseNSSTest
: public testing::Test
{
59 virtual void SetUp() {
60 ASSERT_TRUE(test_nssdb_
.is_open());
61 cert_db_
= NSSCertDatabase::GetInstance();
62 slot_
= cert_db_
->GetPublicModule();
64 // Test db should be empty at start of test.
65 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
68 virtual void TearDown() {
69 // Don't try to cleanup if the setup failed.
70 ASSERT_TRUE(slot_
->os_module_handle());
72 EXPECT_TRUE(CleanupSlotContents());
74 // Run the message loop to process any observer callbacks (e.g. for the
75 // ClientSocketFactory singleton) so that the scoped ref ptrs created in
76 // NSSCertDatabase::NotifyObservers* get released.
77 base::MessageLoop::current()->RunUntilIdle();
79 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
83 static std::string
ReadTestFile(const std::string
& name
) {
85 base::FilePath cert_path
= GetTestCertsDirectory().AppendASCII(name
);
86 EXPECT_TRUE(base::ReadFileToString(cert_path
, &result
));
90 static bool ReadCertIntoList(const std::string
& name
,
91 CertificateList
* certs
) {
92 scoped_refptr
<X509Certificate
> cert(
93 ImportCertFromFile(GetTestCertsDirectory(), name
));
97 certs
->push_back(cert
);
101 static CertificateList
ListCertsInSlot(PK11SlotInfo
* slot
) {
102 CertificateList result
;
103 CERTCertList
* cert_list
= PK11_ListCertsInSlot(slot
);
104 for (CERTCertListNode
* node
= CERT_LIST_HEAD(cert_list
);
105 !CERT_LIST_END(node
, cert_list
);
106 node
= CERT_LIST_NEXT(node
)) {
107 result
.push_back(X509Certificate::CreateFromHandle(
108 node
->cert
, X509Certificate::OSCertHandles()));
110 CERT_DestroyCertList(cert_list
);
112 // Sort the result so that test comparisons can be deterministic.
113 std::sort(result
.begin(), result
.end(), X509Certificate::LessThan());
117 scoped_refptr
<CryptoModule
> slot_
;
118 NSSCertDatabase
* cert_db_
;
119 const CertificateList empty_cert_list_
;
122 bool CleanupSlotContents() {
124 CertificateList certs
= ListCertsInSlot(slot_
->os_module_handle());
125 CERTCertTrust default_trust
= {0};
126 for (size_t i
= 0; i
< certs
.size(); ++i
) {
127 // Reset cert trust values to defaults before deleting. Otherwise NSS
128 // somehow seems to remember the trust which can break following tests.
129 SECStatus srv
= CERT_ChangeCertTrust(
130 CERT_GetDefaultCertDB(), certs
[i
]->os_cert_handle(), &default_trust
);
131 if (srv
!= SECSuccess
)
134 if (!cert_db_
->DeleteCertAndKey(certs
[i
].get()))
140 crypto::ScopedTestNSSDB test_nssdb_
;
143 TEST_F(CertDatabaseNSSTest
, ListCertsSync
) {
144 // This test isn't terribly useful, though it will at least let valgrind test
146 CertificateList certs
;
147 cert_db_
->ListCertsSync(&certs
);
148 // The test DB is empty, but let's assume there will always be something in
150 EXPECT_LT(0U, certs
.size());
153 TEST_F(CertDatabaseNSSTest
, ListCerts
) {
154 // This test isn't terribly useful, though it will at least let valgrind test
156 CertificateList certs
;
157 cert_db_
->SetSlowTaskRunnerForTest(base::MessageLoopProxy::current());
158 cert_db_
->ListCerts(base::Bind(&SwapCertList
, base::Unretained(&certs
)));
159 EXPECT_EQ(0U, certs
.size());
161 base::RunLoop().RunUntilIdle();
163 // The test DB is empty, but let's assume there will always be something in
165 EXPECT_LT(0U, certs
.size());
168 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12WrongPassword
) {
169 std::string pkcs12_data
= ReadTestFile("client.p12");
171 EXPECT_EQ(ERR_PKCS12_IMPORT_BAD_PASSWORD
,
172 cert_db_
->ImportFromPKCS12(slot_
.get(),
175 true, // is_extractable
178 // Test db should still be empty.
179 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
182 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12AsExtractableAndExportAgain
) {
183 std::string pkcs12_data
= ReadTestFile("client.p12");
186 cert_db_
->ImportFromPKCS12(slot_
.get(),
188 ASCIIToUTF16("12345"),
189 true, // is_extractable
192 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
193 ASSERT_EQ(1U, cert_list
.size());
194 scoped_refptr
<X509Certificate
> cert(cert_list
[0]);
196 EXPECT_EQ("testusercert",
197 cert
->subject().common_name
);
199 // TODO(mattm): move export test to separate test case?
200 std::string exported_data
;
201 EXPECT_EQ(1, cert_db_
->ExportToPKCS12(cert_list
, ASCIIToUTF16("exportpw"),
203 ASSERT_LT(0U, exported_data
.size());
204 // TODO(mattm): further verification of exported data?
207 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12Twice
) {
208 std::string pkcs12_data
= ReadTestFile("client.p12");
211 cert_db_
->ImportFromPKCS12(slot_
.get(),
213 ASCIIToUTF16("12345"),
214 true, // is_extractable
216 EXPECT_EQ(1U, ListCertsInSlot(slot_
->os_module_handle()).size());
218 // NSS has a SEC_ERROR_PKCS12_DUPLICATE_DATA error, but it doesn't look like
219 // it's ever used. This test verifies that.
221 cert_db_
->ImportFromPKCS12(slot_
.get(),
223 ASCIIToUTF16("12345"),
224 true, // is_extractable
226 EXPECT_EQ(1U, ListCertsInSlot(slot_
->os_module_handle()).size());
229 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12AsUnextractableAndExportAgain
) {
230 std::string pkcs12_data
= ReadTestFile("client.p12");
233 cert_db_
->ImportFromPKCS12(slot_
.get(),
235 ASCIIToUTF16("12345"),
236 false, // is_extractable
239 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
240 ASSERT_EQ(1U, cert_list
.size());
241 scoped_refptr
<X509Certificate
> cert(cert_list
[0]);
243 EXPECT_EQ("testusercert",
244 cert
->subject().common_name
);
246 std::string exported_data
;
247 EXPECT_EQ(0, cert_db_
->ExportToPKCS12(cert_list
, ASCIIToUTF16("exportpw"),
251 // Importing a PKCS#12 file with a certificate but no corresponding
252 // private key should not mark an existing private key as unextractable.
253 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12OnlyMarkIncludedKey
) {
254 std::string pkcs12_data
= ReadTestFile("client.p12");
256 cert_db_
->ImportFromPKCS12(slot_
.get(),
258 ASCIIToUTF16("12345"),
259 true, // is_extractable
262 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
263 ASSERT_EQ(1U, cert_list
.size());
265 // Now import a PKCS#12 file with just a certificate but no private key.
266 pkcs12_data
= ReadTestFile("client-nokey.p12");
268 cert_db_
->ImportFromPKCS12(slot_
.get(),
270 ASCIIToUTF16("12345"),
271 false, // is_extractable
274 cert_list
= ListCertsInSlot(slot_
->os_module_handle());
275 ASSERT_EQ(1U, cert_list
.size());
277 // Make sure the imported private key is still extractable.
278 std::string exported_data
;
279 EXPECT_EQ(1, cert_db_
->ExportToPKCS12(cert_list
, ASCIIToUTF16("exportpw"),
281 ASSERT_LT(0U, exported_data
.size());
284 TEST_F(CertDatabaseNSSTest
, ImportFromPKCS12InvalidFile
) {
285 std::string pkcs12_data
= "Foobarbaz";
287 EXPECT_EQ(ERR_PKCS12_IMPORT_INVALID_FILE
,
288 cert_db_
->ImportFromPKCS12(slot_
.get(),
291 true, // is_extractable
294 // Test db should still be empty.
295 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
298 TEST_F(CertDatabaseNSSTest
, ImportCACert_SSLTrust
) {
299 CertificateList certs
= CreateCertificateListFromFile(
300 GetTestCertsDirectory(), "root_ca_cert.pem",
301 X509Certificate::FORMAT_AUTO
);
302 ASSERT_EQ(1U, certs
.size());
303 EXPECT_FALSE(certs
[0]->os_cert_handle()->isperm
);
306 NSSCertDatabase::ImportCertFailureList failed
;
307 EXPECT_TRUE(cert_db_
->ImportCACerts(certs
, NSSCertDatabase::TRUSTED_SSL
,
310 EXPECT_EQ(0U, failed
.size());
312 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
313 ASSERT_EQ(1U, cert_list
.size());
314 scoped_refptr
<X509Certificate
> cert(cert_list
[0]);
315 EXPECT_EQ("Test Root CA", cert
->subject().common_name
);
317 EXPECT_EQ(NSSCertDatabase::TRUSTED_SSL
,
318 cert_db_
->GetCertTrust(cert
.get(), CA_CERT
));
320 EXPECT_EQ(unsigned(CERTDB_VALID_CA
| CERTDB_TRUSTED_CA
|
321 CERTDB_TRUSTED_CLIENT_CA
),
322 cert
->os_cert_handle()->trust
->sslFlags
);
323 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
324 cert
->os_cert_handle()->trust
->emailFlags
);
325 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
326 cert
->os_cert_handle()->trust
->objectSigningFlags
);
329 TEST_F(CertDatabaseNSSTest
, ImportCACert_EmailTrust
) {
330 CertificateList certs
= CreateCertificateListFromFile(
331 GetTestCertsDirectory(), "root_ca_cert.pem",
332 X509Certificate::FORMAT_AUTO
);
333 ASSERT_EQ(1U, certs
.size());
334 EXPECT_FALSE(certs
[0]->os_cert_handle()->isperm
);
337 NSSCertDatabase::ImportCertFailureList failed
;
338 EXPECT_TRUE(cert_db_
->ImportCACerts(certs
, NSSCertDatabase::TRUSTED_EMAIL
,
341 EXPECT_EQ(0U, failed
.size());
343 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
344 ASSERT_EQ(1U, cert_list
.size());
345 scoped_refptr
<X509Certificate
> cert(cert_list
[0]);
346 EXPECT_EQ("Test Root CA", cert
->subject().common_name
);
348 EXPECT_EQ(NSSCertDatabase::TRUSTED_EMAIL
,
349 cert_db_
->GetCertTrust(cert
.get(), CA_CERT
));
351 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
352 cert
->os_cert_handle()->trust
->sslFlags
);
353 EXPECT_EQ(unsigned(CERTDB_VALID_CA
| CERTDB_TRUSTED_CA
|
354 CERTDB_TRUSTED_CLIENT_CA
),
355 cert
->os_cert_handle()->trust
->emailFlags
);
356 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
357 cert
->os_cert_handle()->trust
->objectSigningFlags
);
360 TEST_F(CertDatabaseNSSTest
, ImportCACert_ObjSignTrust
) {
361 CertificateList certs
= CreateCertificateListFromFile(
362 GetTestCertsDirectory(), "root_ca_cert.pem",
363 X509Certificate::FORMAT_AUTO
);
364 ASSERT_EQ(1U, certs
.size());
365 EXPECT_FALSE(certs
[0]->os_cert_handle()->isperm
);
368 NSSCertDatabase::ImportCertFailureList failed
;
369 EXPECT_TRUE(cert_db_
->ImportCACerts(certs
, NSSCertDatabase::TRUSTED_OBJ_SIGN
,
372 EXPECT_EQ(0U, failed
.size());
374 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
375 ASSERT_EQ(1U, cert_list
.size());
376 scoped_refptr
<X509Certificate
> cert(cert_list
[0]);
377 EXPECT_EQ("Test Root CA", cert
->subject().common_name
);
379 EXPECT_EQ(NSSCertDatabase::TRUSTED_OBJ_SIGN
,
380 cert_db_
->GetCertTrust(cert
.get(), CA_CERT
));
382 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
383 cert
->os_cert_handle()->trust
->sslFlags
);
384 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
385 cert
->os_cert_handle()->trust
->emailFlags
);
386 EXPECT_EQ(unsigned(CERTDB_VALID_CA
| CERTDB_TRUSTED_CA
|
387 CERTDB_TRUSTED_CLIENT_CA
),
388 cert
->os_cert_handle()->trust
->objectSigningFlags
);
391 TEST_F(CertDatabaseNSSTest
, ImportCA_NotCACert
) {
392 CertificateList certs
= CreateCertificateListFromFile(
393 GetTestCertsDirectory(), "ok_cert.pem",
394 X509Certificate::FORMAT_AUTO
);
395 ASSERT_EQ(1U, certs
.size());
396 EXPECT_FALSE(certs
[0]->os_cert_handle()->isperm
);
399 NSSCertDatabase::ImportCertFailureList failed
;
400 EXPECT_TRUE(cert_db_
->ImportCACerts(certs
, NSSCertDatabase::TRUSTED_SSL
,
402 ASSERT_EQ(1U, failed
.size());
403 // Note: this compares pointers directly. It's okay in this case because
404 // ImportCACerts returns the same pointers that were passed in. In the
405 // general case IsSameOSCert should be used.
406 EXPECT_EQ(certs
[0], failed
[0].certificate
);
407 EXPECT_EQ(ERR_IMPORT_CA_CERT_NOT_CA
, failed
[0].net_error
);
409 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
412 TEST_F(CertDatabaseNSSTest
, ImportCACertHierarchy
) {
413 CertificateList certs
;
414 ASSERT_TRUE(ReadCertIntoList("dod_root_ca_2_cert.der", &certs
));
415 ASSERT_TRUE(ReadCertIntoList("dod_ca_17_cert.der", &certs
));
416 ASSERT_TRUE(ReadCertIntoList("www_us_army_mil_cert.der", &certs
));
419 NSSCertDatabase::ImportCertFailureList failed
;
420 // Have to specify email trust for the cert verification of the child cert to
422 // http://mxr.mozilla.org/mozilla/source/security/nss/lib/certhigh/certvfy.c#752
423 // "XXX This choice of trustType seems arbitrary.")
424 EXPECT_TRUE(cert_db_
->ImportCACerts(
425 certs
, NSSCertDatabase::TRUSTED_SSL
| NSSCertDatabase::TRUSTED_EMAIL
,
428 ASSERT_EQ(2U, failed
.size());
429 EXPECT_EQ("DOD CA-17", failed
[0].certificate
->subject().common_name
);
430 EXPECT_EQ(ERR_FAILED
, failed
[0].net_error
); // The certificate expired.
431 EXPECT_EQ("www.us.army.mil", failed
[1].certificate
->subject().common_name
);
432 EXPECT_EQ(ERR_IMPORT_CA_CERT_NOT_CA
, failed
[1].net_error
);
434 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
435 ASSERT_EQ(1U, cert_list
.size());
436 EXPECT_EQ("DoD Root CA 2", cert_list
[0]->subject().common_name
);
439 TEST_F(CertDatabaseNSSTest
, ImportCACertHierarchyDupeRoot
) {
440 CertificateList certs
;
441 ASSERT_TRUE(ReadCertIntoList("dod_root_ca_2_cert.der", &certs
));
443 // First import just the root.
444 NSSCertDatabase::ImportCertFailureList failed
;
445 EXPECT_TRUE(cert_db_
->ImportCACerts(
446 certs
, NSSCertDatabase::TRUSTED_SSL
| NSSCertDatabase::TRUSTED_EMAIL
,
449 EXPECT_EQ(0U, failed
.size());
450 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
451 ASSERT_EQ(1U, cert_list
.size());
452 EXPECT_EQ("DoD Root CA 2", cert_list
[0]->subject().common_name
);
454 ASSERT_TRUE(ReadCertIntoList("dod_ca_17_cert.der", &certs
));
455 ASSERT_TRUE(ReadCertIntoList("www_us_army_mil_cert.der", &certs
));
457 // Now import with the other certs in the list too. Even though the root is
458 // already present, we should still import the rest.
460 EXPECT_TRUE(cert_db_
->ImportCACerts(
461 certs
, NSSCertDatabase::TRUSTED_SSL
| NSSCertDatabase::TRUSTED_EMAIL
,
464 ASSERT_EQ(3U, failed
.size());
465 EXPECT_EQ("DoD Root CA 2", failed
[0].certificate
->subject().common_name
);
466 EXPECT_EQ(ERR_IMPORT_CERT_ALREADY_EXISTS
, failed
[0].net_error
);
467 EXPECT_EQ("DOD CA-17", failed
[1].certificate
->subject().common_name
);
468 EXPECT_EQ(ERR_FAILED
, failed
[1].net_error
); // The certificate expired.
469 EXPECT_EQ("www.us.army.mil", failed
[2].certificate
->subject().common_name
);
470 EXPECT_EQ(ERR_IMPORT_CA_CERT_NOT_CA
, failed
[2].net_error
);
472 cert_list
= ListCertsInSlot(slot_
->os_module_handle());
473 ASSERT_EQ(1U, cert_list
.size());
474 EXPECT_EQ("DoD Root CA 2", cert_list
[0]->subject().common_name
);
477 TEST_F(CertDatabaseNSSTest
, ImportCACertHierarchyUntrusted
) {
478 CertificateList certs
;
479 ASSERT_TRUE(ReadCertIntoList("dod_root_ca_2_cert.der", &certs
));
480 ASSERT_TRUE(ReadCertIntoList("dod_ca_17_cert.der", &certs
));
483 NSSCertDatabase::ImportCertFailureList failed
;
484 EXPECT_TRUE(cert_db_
->ImportCACerts(certs
, NSSCertDatabase::TRUST_DEFAULT
,
487 ASSERT_EQ(1U, failed
.size());
488 EXPECT_EQ("DOD CA-17", failed
[0].certificate
->subject().common_name
);
489 // TODO(mattm): should check for net error equivalent of
490 // SEC_ERROR_UNTRUSTED_ISSUER
491 EXPECT_EQ(ERR_FAILED
, failed
[0].net_error
);
493 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
494 ASSERT_EQ(1U, cert_list
.size());
495 EXPECT_EQ("DoD Root CA 2", cert_list
[0]->subject().common_name
);
498 TEST_F(CertDatabaseNSSTest
, ImportCACertHierarchyTree
) {
499 CertificateList certs
;
500 ASSERT_TRUE(ReadCertIntoList("dod_root_ca_2_cert.der", &certs
));
501 ASSERT_TRUE(ReadCertIntoList("dod_ca_13_cert.der", &certs
));
502 ASSERT_TRUE(ReadCertIntoList("dod_ca_17_cert.der", &certs
));
505 NSSCertDatabase::ImportCertFailureList failed
;
506 EXPECT_TRUE(cert_db_
->ImportCACerts(
507 certs
, NSSCertDatabase::TRUSTED_SSL
| NSSCertDatabase::TRUSTED_EMAIL
,
510 EXPECT_EQ(2U, failed
.size());
511 EXPECT_EQ("DOD CA-13", failed
[0].certificate
->subject().common_name
);
512 EXPECT_EQ(ERR_FAILED
, failed
[0].net_error
); // The certificate expired.
513 EXPECT_EQ("DOD CA-17", failed
[1].certificate
->subject().common_name
);
514 EXPECT_EQ(ERR_FAILED
, failed
[1].net_error
); // The certificate expired.
516 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
517 ASSERT_EQ(1U, cert_list
.size());
518 EXPECT_EQ("DoD Root CA 2", cert_list
[0]->subject().common_name
);
521 TEST_F(CertDatabaseNSSTest
, ImportCACertNotHierarchy
) {
522 CertificateList certs
= CreateCertificateListFromFile(
523 GetTestCertsDirectory(), "root_ca_cert.pem",
524 X509Certificate::FORMAT_AUTO
);
525 ASSERT_EQ(1U, certs
.size());
526 ASSERT_TRUE(ReadCertIntoList("dod_ca_13_cert.der", &certs
));
527 ASSERT_TRUE(ReadCertIntoList("dod_ca_17_cert.der", &certs
));
530 NSSCertDatabase::ImportCertFailureList failed
;
531 EXPECT_TRUE(cert_db_
->ImportCACerts(
532 certs
, NSSCertDatabase::TRUSTED_SSL
| NSSCertDatabase::TRUSTED_EMAIL
|
533 NSSCertDatabase::TRUSTED_OBJ_SIGN
, &failed
));
535 ASSERT_EQ(2U, failed
.size());
536 // TODO(mattm): should check for net error equivalent of
537 // SEC_ERROR_UNKNOWN_ISSUER
538 EXPECT_EQ("DOD CA-13", failed
[0].certificate
->subject().common_name
);
539 EXPECT_EQ(ERR_FAILED
, failed
[0].net_error
);
540 EXPECT_EQ("DOD CA-17", failed
[1].certificate
->subject().common_name
);
541 EXPECT_EQ(ERR_FAILED
, failed
[1].net_error
);
543 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
544 ASSERT_EQ(1U, cert_list
.size());
545 EXPECT_EQ("Test Root CA", cert_list
[0]->subject().common_name
);
548 // http://crbug.com/108009 - Disabled, as google.chain.pem is an expired
550 TEST_F(CertDatabaseNSSTest
, DISABLED_ImportServerCert
) {
551 // Need to import intermediate cert for the verify of google cert, otherwise
552 // it will try to fetch it automatically with cert_pi_useAIACertFetch, which
553 // will cause OCSPCreateSession on the main thread, which is not allowed.
554 CertificateList certs
= CreateCertificateListFromFile(
555 GetTestCertsDirectory(), "google.chain.pem",
556 X509Certificate::FORMAT_AUTO
);
557 ASSERT_EQ(2U, certs
.size());
559 NSSCertDatabase::ImportCertFailureList failed
;
560 EXPECT_TRUE(cert_db_
->ImportServerCert(certs
, NSSCertDatabase::TRUST_DEFAULT
,
563 EXPECT_EQ(0U, failed
.size());
565 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
566 ASSERT_EQ(2U, cert_list
.size());
567 scoped_refptr
<X509Certificate
> goog_cert(cert_list
[0]);
568 scoped_refptr
<X509Certificate
> thawte_cert(cert_list
[1]);
569 EXPECT_EQ("www.google.com", goog_cert
->subject().common_name
);
570 EXPECT_EQ("Thawte SGC CA", thawte_cert
->subject().common_name
);
572 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
573 cert_db_
->GetCertTrust(goog_cert
.get(), SERVER_CERT
));
575 EXPECT_EQ(0U, goog_cert
->os_cert_handle()->trust
->sslFlags
);
577 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
579 CertVerifyResult verify_result
;
580 int error
= verify_proc
->Verify(goog_cert
.get(),
586 EXPECT_EQ(OK
, error
);
587 EXPECT_EQ(0U, verify_result
.cert_status
);
590 TEST_F(CertDatabaseNSSTest
, ImportServerCert_SelfSigned
) {
591 CertificateList certs
;
592 ASSERT_TRUE(ReadCertIntoList("punycodetest.der", &certs
));
594 NSSCertDatabase::ImportCertFailureList failed
;
595 EXPECT_TRUE(cert_db_
->ImportServerCert(certs
, NSSCertDatabase::TRUST_DEFAULT
,
598 EXPECT_EQ(0U, failed
.size());
600 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
601 ASSERT_EQ(1U, cert_list
.size());
602 scoped_refptr
<X509Certificate
> puny_cert(cert_list
[0]);
604 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
605 cert_db_
->GetCertTrust(puny_cert
.get(), SERVER_CERT
));
606 EXPECT_EQ(0U, puny_cert
->os_cert_handle()->trust
->sslFlags
);
608 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
610 CertVerifyResult verify_result
;
611 int error
= verify_proc
->Verify(puny_cert
.get(),
612 "xn--wgv71a119e.com",
617 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID
, error
);
618 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID
, verify_result
.cert_status
);
621 TEST_F(CertDatabaseNSSTest
, ImportServerCert_SelfSigned_Trusted
) {
622 CertificateList certs
;
623 ASSERT_TRUE(ReadCertIntoList("punycodetest.der", &certs
));
625 NSSCertDatabase::ImportCertFailureList failed
;
626 EXPECT_TRUE(cert_db_
->ImportServerCert(certs
, NSSCertDatabase::TRUSTED_SSL
,
629 EXPECT_EQ(0U, failed
.size());
631 CertificateList cert_list
= ListCertsInSlot(slot_
->os_module_handle());
632 ASSERT_EQ(1U, cert_list
.size());
633 scoped_refptr
<X509Certificate
> puny_cert(cert_list
[0]);
635 EXPECT_EQ(NSSCertDatabase::TRUSTED_SSL
,
636 cert_db_
->GetCertTrust(puny_cert
.get(), SERVER_CERT
));
637 EXPECT_EQ(unsigned(CERTDB_TRUSTED
| CERTDB_TERMINAL_RECORD
),
638 puny_cert
->os_cert_handle()->trust
->sslFlags
);
640 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
642 CertVerifyResult verify_result
;
643 int error
= verify_proc
->Verify(puny_cert
.get(),
644 "xn--wgv71a119e.com",
649 EXPECT_EQ(OK
, error
);
650 EXPECT_EQ(0U, verify_result
.cert_status
);
653 TEST_F(CertDatabaseNSSTest
, ImportCaAndServerCert
) {
654 CertificateList ca_certs
= CreateCertificateListFromFile(
655 GetTestCertsDirectory(), "root_ca_cert.pem",
656 X509Certificate::FORMAT_AUTO
);
657 ASSERT_EQ(1U, ca_certs
.size());
659 // Import CA cert and trust it.
660 NSSCertDatabase::ImportCertFailureList failed
;
661 EXPECT_TRUE(cert_db_
->ImportCACerts(ca_certs
, NSSCertDatabase::TRUSTED_SSL
,
663 EXPECT_EQ(0U, failed
.size());
665 CertificateList certs
= CreateCertificateListFromFile(
666 GetTestCertsDirectory(), "ok_cert.pem",
667 X509Certificate::FORMAT_AUTO
);
668 ASSERT_EQ(1U, certs
.size());
670 // Import server cert with default trust.
671 EXPECT_TRUE(cert_db_
->ImportServerCert(certs
, NSSCertDatabase::TRUST_DEFAULT
,
673 EXPECT_EQ(0U, failed
.size());
675 // Server cert should verify.
676 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
678 CertVerifyResult verify_result
;
679 int error
= verify_proc
->Verify(certs
[0].get(),
685 EXPECT_EQ(OK
, error
);
686 EXPECT_EQ(0U, verify_result
.cert_status
);
689 TEST_F(CertDatabaseNSSTest
, ImportCaAndServerCert_DistrustServer
) {
690 CertificateList ca_certs
= CreateCertificateListFromFile(
691 GetTestCertsDirectory(), "root_ca_cert.pem",
692 X509Certificate::FORMAT_AUTO
);
693 ASSERT_EQ(1U, ca_certs
.size());
695 // Import CA cert and trust it.
696 NSSCertDatabase::ImportCertFailureList failed
;
697 EXPECT_TRUE(cert_db_
->ImportCACerts(ca_certs
, NSSCertDatabase::TRUSTED_SSL
,
699 EXPECT_EQ(0U, failed
.size());
701 CertificateList certs
= CreateCertificateListFromFile(
702 GetTestCertsDirectory(), "ok_cert.pem",
703 X509Certificate::FORMAT_AUTO
);
704 ASSERT_EQ(1U, certs
.size());
706 // Import server cert without inheriting trust from issuer (explicit
708 EXPECT_TRUE(cert_db_
->ImportServerCert(
709 certs
, NSSCertDatabase::DISTRUSTED_SSL
, &failed
));
710 EXPECT_EQ(0U, failed
.size());
711 EXPECT_EQ(NSSCertDatabase::DISTRUSTED_SSL
,
712 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
714 EXPECT_EQ(unsigned(CERTDB_TERMINAL_RECORD
),
715 certs
[0]->os_cert_handle()->trust
->sslFlags
);
717 // Server cert should fail to verify.
718 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
720 CertVerifyResult verify_result
;
721 int error
= verify_proc
->Verify(certs
[0].get(),
727 EXPECT_EQ(ERR_CERT_REVOKED
, error
);
728 EXPECT_EQ(CERT_STATUS_REVOKED
, verify_result
.cert_status
);
731 TEST_F(CertDatabaseNSSTest
, TrustIntermediateCa
) {
732 CertificateList ca_certs
= CreateCertificateListFromFile(
733 GetTestCertsDirectory(), "2048-rsa-root.pem",
734 X509Certificate::FORMAT_AUTO
);
735 ASSERT_EQ(1U, ca_certs
.size());
737 // Import Root CA cert and distrust it.
738 NSSCertDatabase::ImportCertFailureList failed
;
739 EXPECT_TRUE(cert_db_
->ImportCACerts(ca_certs
, NSSCertDatabase::DISTRUSTED_SSL
,
741 EXPECT_EQ(0U, failed
.size());
743 CertificateList intermediate_certs
= CreateCertificateListFromFile(
744 GetTestCertsDirectory(), "2048-rsa-intermediate.pem",
745 X509Certificate::FORMAT_AUTO
);
746 ASSERT_EQ(1U, intermediate_certs
.size());
748 // Import Intermediate CA cert and trust it.
749 EXPECT_TRUE(cert_db_
->ImportCACerts(intermediate_certs
,
750 NSSCertDatabase::TRUSTED_SSL
, &failed
));
751 EXPECT_EQ(0U, failed
.size());
753 CertificateList certs
= CreateCertificateListFromFile(
754 GetTestCertsDirectory(), "2048-rsa-ee-by-2048-rsa-intermediate.pem",
755 X509Certificate::FORMAT_AUTO
);
756 ASSERT_EQ(1U, certs
.size());
758 // Import server cert with default trust.
759 EXPECT_TRUE(cert_db_
->ImportServerCert(
760 certs
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
761 EXPECT_EQ(0U, failed
.size());
762 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
763 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
765 // Server cert should verify.
766 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
768 CertVerifyResult verify_result
;
769 int error
= verify_proc
->Verify(certs
[0].get(),
775 EXPECT_EQ(OK
, error
);
776 EXPECT_EQ(0U, verify_result
.cert_status
);
778 // Trust the root cert and distrust the intermediate.
779 EXPECT_TRUE(cert_db_
->SetCertTrust(
780 ca_certs
[0].get(), CA_CERT
, NSSCertDatabase::TRUSTED_SSL
));
781 EXPECT_TRUE(cert_db_
->SetCertTrust(
782 intermediate_certs
[0].get(), CA_CERT
, NSSCertDatabase::DISTRUSTED_SSL
));
784 unsigned(CERTDB_VALID_CA
| CERTDB_TRUSTED_CA
| CERTDB_TRUSTED_CLIENT_CA
),
785 ca_certs
[0]->os_cert_handle()->trust
->sslFlags
);
786 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
787 ca_certs
[0]->os_cert_handle()->trust
->emailFlags
);
788 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
789 ca_certs
[0]->os_cert_handle()->trust
->objectSigningFlags
);
790 EXPECT_EQ(unsigned(CERTDB_TERMINAL_RECORD
),
791 intermediate_certs
[0]->os_cert_handle()->trust
->sslFlags
);
792 EXPECT_EQ(unsigned(CERTDB_VALID_CA
),
793 intermediate_certs
[0]->os_cert_handle()->trust
->emailFlags
);
795 unsigned(CERTDB_VALID_CA
),
796 intermediate_certs
[0]->os_cert_handle()->trust
->objectSigningFlags
);
798 // Server cert should fail to verify.
799 CertVerifyResult verify_result2
;
800 error
= verify_proc
->Verify(certs
[0].get(),
806 EXPECT_EQ(ERR_CERT_REVOKED
, error
);
807 EXPECT_EQ(CERT_STATUS_REVOKED
, verify_result2
.cert_status
);
810 TEST_F(CertDatabaseNSSTest
, TrustIntermediateCa2
) {
811 if (NSS_VersionCheck("3.14.2") && !NSS_VersionCheck("3.15")) {
812 // See http://bugzil.la/863947 for details.
813 LOG(INFO
) << "Skipping test for NSS 3.14.2 - NSS 3.15";
817 NSSCertDatabase::ImportCertFailureList failed
;
819 CertificateList intermediate_certs
= CreateCertificateListFromFile(
820 GetTestCertsDirectory(), "2048-rsa-intermediate.pem",
821 X509Certificate::FORMAT_AUTO
);
822 ASSERT_EQ(1U, intermediate_certs
.size());
824 // Import Intermediate CA cert and trust it.
825 EXPECT_TRUE(cert_db_
->ImportCACerts(intermediate_certs
,
826 NSSCertDatabase::TRUSTED_SSL
, &failed
));
827 EXPECT_EQ(0U, failed
.size());
829 CertificateList certs
= CreateCertificateListFromFile(
830 GetTestCertsDirectory(), "2048-rsa-ee-by-2048-rsa-intermediate.pem",
831 X509Certificate::FORMAT_AUTO
);
832 ASSERT_EQ(1U, certs
.size());
834 // Import server cert with default trust.
835 EXPECT_TRUE(cert_db_
->ImportServerCert(
836 certs
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
837 EXPECT_EQ(0U, failed
.size());
838 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
839 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
841 // Server cert should verify.
842 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
844 CertVerifyResult verify_result
;
845 int error
= verify_proc
->Verify(certs
[0].get(),
851 EXPECT_EQ(OK
, error
);
852 EXPECT_EQ(0U, verify_result
.cert_status
);
854 // Without explicit trust of the intermediate, verification should fail.
855 EXPECT_TRUE(cert_db_
->SetCertTrust(
856 intermediate_certs
[0].get(), CA_CERT
, NSSCertDatabase::TRUST_DEFAULT
));
858 // Server cert should fail to verify.
859 CertVerifyResult verify_result2
;
860 error
= verify_proc
->Verify(certs
[0].get(),
866 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID
, error
);
867 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID
, verify_result2
.cert_status
);
870 TEST_F(CertDatabaseNSSTest
, TrustIntermediateCa3
) {
871 if (NSS_VersionCheck("3.14.2") && !NSS_VersionCheck("3.15")) {
872 // See http://bugzil.la/863947 for details.
873 LOG(INFO
) << "Skipping test for NSS 3.14.2 - NSS 3.15";
877 NSSCertDatabase::ImportCertFailureList failed
;
879 CertificateList ca_certs
= CreateCertificateListFromFile(
880 GetTestCertsDirectory(), "2048-rsa-root.pem",
881 X509Certificate::FORMAT_AUTO
);
882 ASSERT_EQ(1U, ca_certs
.size());
884 // Import Root CA cert and default trust it.
885 EXPECT_TRUE(cert_db_
->ImportCACerts(ca_certs
, NSSCertDatabase::TRUST_DEFAULT
,
887 EXPECT_EQ(0U, failed
.size());
889 CertificateList intermediate_certs
= CreateCertificateListFromFile(
890 GetTestCertsDirectory(), "2048-rsa-intermediate.pem",
891 X509Certificate::FORMAT_AUTO
);
892 ASSERT_EQ(1U, intermediate_certs
.size());
894 // Import Intermediate CA cert and trust it.
895 EXPECT_TRUE(cert_db_
->ImportCACerts(intermediate_certs
,
896 NSSCertDatabase::TRUSTED_SSL
, &failed
));
897 EXPECT_EQ(0U, failed
.size());
899 CertificateList certs
= CreateCertificateListFromFile(
900 GetTestCertsDirectory(), "2048-rsa-ee-by-2048-rsa-intermediate.pem",
901 X509Certificate::FORMAT_AUTO
);
902 ASSERT_EQ(1U, certs
.size());
904 // Import server cert with default trust.
905 EXPECT_TRUE(cert_db_
->ImportServerCert(
906 certs
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
907 EXPECT_EQ(0U, failed
.size());
908 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
909 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
911 // Server cert should verify.
912 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
914 CertVerifyResult verify_result
;
915 int error
= verify_proc
->Verify(certs
[0].get(),
921 EXPECT_EQ(OK
, error
);
922 EXPECT_EQ(0U, verify_result
.cert_status
);
924 // Without explicit trust of the intermediate, verification should fail.
925 EXPECT_TRUE(cert_db_
->SetCertTrust(
926 intermediate_certs
[0].get(), CA_CERT
, NSSCertDatabase::TRUST_DEFAULT
));
928 // Server cert should fail to verify.
929 CertVerifyResult verify_result2
;
930 error
= verify_proc
->Verify(certs
[0].get(),
936 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID
, error
);
937 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID
, verify_result2
.cert_status
);
940 TEST_F(CertDatabaseNSSTest
, TrustIntermediateCa4
) {
941 NSSCertDatabase::ImportCertFailureList failed
;
943 CertificateList ca_certs
= CreateCertificateListFromFile(
944 GetTestCertsDirectory(), "2048-rsa-root.pem",
945 X509Certificate::FORMAT_AUTO
);
946 ASSERT_EQ(1U, ca_certs
.size());
948 // Import Root CA cert and trust it.
949 EXPECT_TRUE(cert_db_
->ImportCACerts(ca_certs
, NSSCertDatabase::TRUSTED_SSL
,
951 EXPECT_EQ(0U, failed
.size());
953 CertificateList intermediate_certs
= CreateCertificateListFromFile(
954 GetTestCertsDirectory(), "2048-rsa-intermediate.pem",
955 X509Certificate::FORMAT_AUTO
);
956 ASSERT_EQ(1U, intermediate_certs
.size());
958 // Import Intermediate CA cert and distrust it.
959 EXPECT_TRUE(cert_db_
->ImportCACerts(
960 intermediate_certs
, NSSCertDatabase::DISTRUSTED_SSL
, &failed
));
961 EXPECT_EQ(0U, failed
.size());
963 CertificateList certs
= CreateCertificateListFromFile(
964 GetTestCertsDirectory(), "2048-rsa-ee-by-2048-rsa-intermediate.pem",
965 X509Certificate::FORMAT_AUTO
);
966 ASSERT_EQ(1U, certs
.size());
968 // Import server cert with default trust.
969 EXPECT_TRUE(cert_db_
->ImportServerCert(
970 certs
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
971 EXPECT_EQ(0U, failed
.size());
972 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
973 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
975 // Server cert should not verify.
976 scoped_refptr
<CertVerifyProc
> verify_proc(new CertVerifyProcNSS());
978 CertVerifyResult verify_result
;
979 int error
= verify_proc
->Verify(certs
[0].get(),
985 EXPECT_EQ(ERR_CERT_REVOKED
, error
);
986 EXPECT_EQ(CERT_STATUS_REVOKED
, verify_result
.cert_status
);
988 // Without explicit distrust of the intermediate, verification should succeed.
989 EXPECT_TRUE(cert_db_
->SetCertTrust(
990 intermediate_certs
[0].get(), CA_CERT
, NSSCertDatabase::TRUST_DEFAULT
));
992 // Server cert should verify.
993 CertVerifyResult verify_result2
;
994 error
= verify_proc
->Verify(certs
[0].get(),
1000 EXPECT_EQ(OK
, error
);
1001 EXPECT_EQ(0U, verify_result2
.cert_status
);
1004 // Importing two certificates with the same issuer and subject common name,
1005 // but overall distinct subject names, should succeed and generate a unique
1006 // nickname for the second certificate.
1007 TEST_F(CertDatabaseNSSTest
, ImportDuplicateCommonName
) {
1008 CertificateList certs
=
1009 CreateCertificateListFromFile(GetTestCertsDirectory(),
1010 "duplicate_cn_1.pem",
1011 X509Certificate::FORMAT_AUTO
);
1012 ASSERT_EQ(1U, certs
.size());
1014 EXPECT_EQ(0U, ListCertsInSlot(slot_
->os_module_handle()).size());
1016 // Import server cert with default trust.
1017 NSSCertDatabase::ImportCertFailureList failed
;
1018 EXPECT_TRUE(cert_db_
->ImportServerCert(
1019 certs
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
1020 EXPECT_EQ(0U, failed
.size());
1021 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
1022 cert_db_
->GetCertTrust(certs
[0].get(), SERVER_CERT
));
1024 CertificateList new_certs
= ListCertsInSlot(slot_
->os_module_handle());
1025 ASSERT_EQ(1U, new_certs
.size());
1027 // Now attempt to import a different certificate with the same common name.
1028 CertificateList certs2
=
1029 CreateCertificateListFromFile(GetTestCertsDirectory(),
1030 "duplicate_cn_2.pem",
1031 X509Certificate::FORMAT_AUTO
);
1032 ASSERT_EQ(1U, certs2
.size());
1034 // Import server cert with default trust.
1035 EXPECT_TRUE(cert_db_
->ImportServerCert(
1036 certs2
, NSSCertDatabase::TRUST_DEFAULT
, &failed
));
1037 EXPECT_EQ(0U, failed
.size());
1038 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT
,
1039 cert_db_
->GetCertTrust(certs2
[0].get(), SERVER_CERT
));
1041 new_certs
= ListCertsInSlot(slot_
->os_module_handle());
1042 ASSERT_EQ(2U, new_certs
.size());
1043 EXPECT_STRNE(new_certs
[0]->os_cert_handle()->nickname
,
1044 new_certs
[1]->os_cert_handle()->nickname
);