OLD | NEW |
---|---|
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/nss_cert_database.h" | 5 #include "net/cert/nss_cert_database.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <certdb.h> | 8 #include <certdb.h> |
9 #include <keyhi.h> | 9 #include <keyhi.h> |
10 #include <pk11pub.h> | 10 #include <pk11pub.h> |
11 #include <secmod.h> | 11 #include <secmod.h> |
12 | 12 |
13 #include "base/bind.h" | |
14 #include "base/callback.h" | |
13 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 16 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
16 #include "base/observer_list_threadsafe.h" | 18 #include "base/observer_list_threadsafe.h" |
19 #include "base/task_runner.h" | |
20 #include "base/threading/worker_pool.h" | |
17 #include "crypto/nss_util.h" | 21 #include "crypto/nss_util.h" |
18 #include "crypto/nss_util_internal.h" | 22 #include "crypto/nss_util_internal.h" |
19 #include "crypto/scoped_nss_types.h" | 23 #include "crypto/scoped_nss_types.h" |
20 #include "net/base/crypto_module.h" | 24 #include "net/base/crypto_module.h" |
21 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
22 #include "net/cert/cert_database.h" | 26 #include "net/cert/cert_database.h" |
23 #include "net/cert/x509_certificate.h" | 27 #include "net/cert/x509_certificate.h" |
24 #include "net/third_party/mozilla_security_manager/nsNSSCertificateDB.h" | 28 #include "net/third_party/mozilla_security_manager/nsNSSCertificateDB.h" |
25 #include "net/third_party/mozilla_security_manager/nsPKCS12Blob.h" | 29 #include "net/third_party/mozilla_security_manager/nsPKCS12Blob.h" |
26 | 30 |
27 // In NSS 3.13, CERTDB_VALID_PEER was renamed CERTDB_TERMINAL_RECORD. So we use | 31 // In NSS 3.13, CERTDB_VALID_PEER was renamed CERTDB_TERMINAL_RECORD. So we use |
28 // the new name of the macro. | 32 // the new name of the macro. |
29 #if !defined(CERTDB_TERMINAL_RECORD) | 33 #if !defined(CERTDB_TERMINAL_RECORD) |
30 #define CERTDB_TERMINAL_RECORD CERTDB_VALID_PEER | 34 #define CERTDB_TERMINAL_RECORD CERTDB_VALID_PEER |
31 #endif | 35 #endif |
32 | 36 |
33 // PSM = Mozilla's Personal Security Manager. | 37 // PSM = Mozilla's Personal Security Manager. |
34 namespace psm = mozilla_security_manager; | 38 namespace psm = mozilla_security_manager; |
35 | 39 |
36 namespace net { | 40 namespace net { |
37 | 41 |
38 namespace { | 42 namespace { |
39 | 43 |
40 base::LazyInstance<NSSCertDatabase>::Leaky | 44 base::LazyInstance<NSSCertDatabase>::Leaky |
41 g_nss_cert_database = LAZY_INSTANCE_INITIALIZER; | 45 g_nss_cert_database = LAZY_INSTANCE_INITIALIZER; |
42 | 46 |
43 } // namespace | 47 } // namespace |
44 | 48 |
45 | |
46 NSSCertDatabase::ImportCertFailure::ImportCertFailure( | 49 NSSCertDatabase::ImportCertFailure::ImportCertFailure( |
47 const scoped_refptr<X509Certificate>& cert, | 50 const scoped_refptr<X509Certificate>& cert, |
48 int err) | 51 int err) |
49 : certificate(cert), net_error(err) {} | 52 : certificate(cert), net_error(err) {} |
50 | 53 |
51 NSSCertDatabase::ImportCertFailure::~ImportCertFailure() {} | 54 NSSCertDatabase::ImportCertFailure::~ImportCertFailure() {} |
52 | 55 |
53 // static | 56 // static |
54 NSSCertDatabase* NSSCertDatabase::GetInstance() { | 57 NSSCertDatabase* NSSCertDatabase::GetInstance() { |
55 // TODO(mattm): Remove this ifdef guard once the linux impl of | 58 // TODO(mattm): Remove this ifdef guard once the linux impl of |
56 // GetNSSCertDatabaseForResourceContext does not call GetInstance. | 59 // GetNSSCertDatabaseForResourceContext does not call GetInstance. |
57 #if defined(OS_CHROMEOS) | 60 #if defined(OS_CHROMEOS) |
58 LOG(ERROR) << "NSSCertDatabase::GetInstance() is deprecated." | 61 LOG(ERROR) << "NSSCertDatabase::GetInstance() is deprecated." |
59 << "See http://crbug.com/329735."; | 62 << "See http://crbug.com/329735."; |
60 #endif | 63 #endif |
61 return &g_nss_cert_database.Get(); | 64 return &g_nss_cert_database.Get(); |
62 } | 65 } |
63 | 66 |
64 NSSCertDatabase::NSSCertDatabase() | 67 NSSCertDatabase::NSSCertDatabase() |
65 : observer_list_(new ObserverListThreadSafe<Observer>) { | 68 : observer_list_(new ObserverListThreadSafe<Observer>) { |
66 // This also makes sure that NSS has been initialized. | 69 // This also makes sure that NSS has been initialized. |
67 CertDatabase::GetInstance()->ObserveNSSCertDatabase(this); | 70 CertDatabase::GetInstance()->ObserveNSSCertDatabase(this); |
68 | 71 |
69 psm::EnsurePKCS12Init(); | 72 psm::EnsurePKCS12Init(); |
70 } | 73 } |
71 | 74 |
72 NSSCertDatabase::~NSSCertDatabase() {} | 75 NSSCertDatabase::~NSSCertDatabase() {} |
73 | 76 |
74 void NSSCertDatabase::ListCerts(CertificateList* certs) { | 77 void NSSCertDatabase::ListCertsSync(CertificateList* certs) { |
75 certs->clear(); | 78 ListCertsImpl(certs); |
79 } | |
76 | 80 |
77 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL); | 81 void NSSCertDatabase::ListCerts( |
78 CERTCertListNode* node; | 82 const base::Callback<void(scoped_ptr<CertificateList> certs)>& callback) { |
79 for (node = CERT_LIST_HEAD(cert_list); | 83 scoped_ptr<CertificateList> certs(new CertificateList()); |
80 !CERT_LIST_END(node, cert_list); | 84 CertificateList* raw_certs = certs.get(); |
81 node = CERT_LIST_NEXT(node)) { | 85 |
82 certs->push_back(X509Certificate::CreateFromHandle( | 86 GetSlowTaskRunner()->PostTaskAndReply( |
83 node->cert, X509Certificate::OSCertHandles())); | 87 FROM_HERE, |
84 } | 88 base::Bind(&NSSCertDatabase::ListCertsImpl, |
85 CERT_DestroyCertList(cert_list); | 89 base::Unretained(raw_certs)), |
90 base::Bind(callback, base::Passed(&certs))); | |
86 } | 91 } |
87 | 92 |
88 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const { | 93 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const { |
89 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()); | 94 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()); |
90 } | 95 } |
91 | 96 |
92 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const { | 97 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const { |
93 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()); | 98 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()); |
94 } | 99 } |
95 | 100 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 } | 348 } |
344 | 349 |
345 void NSSCertDatabase::AddObserver(Observer* observer) { | 350 void NSSCertDatabase::AddObserver(Observer* observer) { |
346 observer_list_->AddObserver(observer); | 351 observer_list_->AddObserver(observer); |
347 } | 352 } |
348 | 353 |
349 void NSSCertDatabase::RemoveObserver(Observer* observer) { | 354 void NSSCertDatabase::RemoveObserver(Observer* observer) { |
350 observer_list_->RemoveObserver(observer); | 355 observer_list_->RemoveObserver(observer); |
351 } | 356 } |
352 | 357 |
358 // static | |
359 void NSSCertDatabase::ListCertsImpl(CertificateList* certs) { | |
360 CHECK(certs); | |
Ryan Sleevi
2014/01/29 02:24:20
unnecessary check - certs->clear() will error if c
tbarzic
2014/01/29 20:50:52
Done.
| |
361 certs->clear(); | |
362 | |
363 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL); | |
364 CERTCertListNode* node; | |
365 for (node = CERT_LIST_HEAD(cert_list); | |
366 !CERT_LIST_END(node, cert_list); | |
367 node = CERT_LIST_NEXT(node)) { | |
368 certs->push_back(X509Certificate::CreateFromHandle( | |
369 node->cert, X509Certificate::OSCertHandles())); | |
370 } | |
371 CERT_DestroyCertList(cert_list); | |
372 } | |
373 | |
374 scoped_refptr<base::TaskRunner> NSSCertDatabase::GetSlowTaskRunner() const { | |
375 if (slow_task_runner_for_test_) | |
376 return slow_task_runner_for_test_; | |
377 return base::WorkerPool::GetTaskRunner(true /*task is slow*/); | |
378 } | |
379 | |
353 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { | 380 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { |
354 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert)); | 381 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert)); |
355 } | 382 } |
356 | 383 |
357 void NSSCertDatabase::NotifyObserversOfCertRemoved( | 384 void NSSCertDatabase::NotifyObserversOfCertRemoved( |
358 const X509Certificate* cert) { | 385 const X509Certificate* cert) { |
359 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert)); | 386 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert)); |
360 } | 387 } |
361 | 388 |
362 void NSSCertDatabase::NotifyObserversOfCACertChanged( | 389 void NSSCertDatabase::NotifyObserversOfCACertChanged( |
363 const X509Certificate* cert) { | 390 const X509Certificate* cert) { |
364 observer_list_->Notify( | 391 observer_list_->Notify( |
365 &Observer::OnCACertChanged, make_scoped_refptr(cert)); | 392 &Observer::OnCACertChanged, make_scoped_refptr(cert)); |
366 } | 393 } |
367 | 394 |
395 void NSSCertDatabase::SetSlowTaskRunnerForTest( | |
396 const scoped_refptr<base::TaskRunner>& task_runner) { | |
397 slow_task_runner_for_test_ = task_runner; | |
398 } | |
399 | |
368 } // namespace net | 400 } // namespace net |
OLD | NEW |