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 |
Ryan Sleevi
2014/01/30 04:16:30
unnecessary newline
tbarzic
2014/02/03 23:42:05
Done.
| |
45 | 49 |
46 NSSCertDatabase::ImportCertFailure::ImportCertFailure( | 50 NSSCertDatabase::ImportCertFailure::ImportCertFailure( |
47 const scoped_refptr<X509Certificate>& cert, | 51 const scoped_refptr<X509Certificate>& cert, |
48 int err) | 52 int err) |
49 : certificate(cert), net_error(err) {} | 53 : certificate(cert), net_error(err) {} |
50 | 54 |
51 NSSCertDatabase::ImportCertFailure::~ImportCertFailure() {} | 55 NSSCertDatabase::ImportCertFailure::~ImportCertFailure() {} |
52 | 56 |
53 // static | 57 // static |
54 NSSCertDatabase* NSSCertDatabase::GetInstance() { | 58 NSSCertDatabase* NSSCertDatabase::GetInstance() { |
55 // TODO(mattm): Remove this ifdef guard once the linux impl of | 59 // TODO(mattm): Remove this ifdef guard once the linux impl of |
56 // GetNSSCertDatabaseForResourceContext does not call GetInstance. | 60 // GetNSSCertDatabaseForResourceContext does not call GetInstance. |
57 #if defined(OS_CHROMEOS) | 61 #if defined(OS_CHROMEOS) |
58 LOG(ERROR) << "NSSCertDatabase::GetInstance() is deprecated." | 62 LOG(ERROR) << "NSSCertDatabase::GetInstance() is deprecated." |
59 << "See http://crbug.com/329735."; | 63 << "See http://crbug.com/329735."; |
60 #endif | 64 #endif |
61 return &g_nss_cert_database.Get(); | 65 return &g_nss_cert_database.Get(); |
62 } | 66 } |
63 | 67 |
64 NSSCertDatabase::NSSCertDatabase() | 68 NSSCertDatabase::NSSCertDatabase() |
65 : observer_list_(new ObserverListThreadSafe<Observer>) { | 69 : observer_list_(new ObserverListThreadSafe<Observer>) { |
66 // This also makes sure that NSS has been initialized. | 70 // This also makes sure that NSS has been initialized. |
67 CertDatabase::GetInstance()->ObserveNSSCertDatabase(this); | 71 CertDatabase::GetInstance()->ObserveNSSCertDatabase(this); |
68 | 72 |
69 psm::EnsurePKCS12Init(); | 73 psm::EnsurePKCS12Init(); |
70 } | 74 } |
71 | 75 |
72 NSSCertDatabase::~NSSCertDatabase() {} | 76 NSSCertDatabase::~NSSCertDatabase() {} |
73 | 77 |
74 void NSSCertDatabase::ListCerts(CertificateList* certs) { | 78 void NSSCertDatabase::ListCertsSync(CertificateList* certs) { |
75 certs->clear(); | 79 ListCertsImpl(certs); |
80 } | |
76 | 81 |
77 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL); | 82 void NSSCertDatabase::ListCerts( |
78 CERTCertListNode* node; | 83 const base::Callback<void(scoped_ptr<CertificateList> certs)>& callback) { |
79 for (node = CERT_LIST_HEAD(cert_list); | 84 scoped_ptr<CertificateList> certs(new CertificateList()); |
80 !CERT_LIST_END(node, cert_list); | 85 CertificateList* raw_certs = certs.get(); |
81 node = CERT_LIST_NEXT(node)) { | 86 |
82 certs->push_back(X509Certificate::CreateFromHandle( | 87 GetSlowTaskRunner()->PostTaskAndReply( |
83 node->cert, X509Certificate::OSCertHandles())); | 88 FROM_HERE, |
84 } | 89 base::Bind(&NSSCertDatabase::ListCertsImpl, |
85 CERT_DestroyCertList(cert_list); | 90 base::Unretained(raw_certs)), |
Ryan Sleevi
2014/01/30 04:16:30
base::Unretained(certs.get()) ?
tbarzic
2014/02/03 23:42:05
that would return NULL due to base::Passed bellow
| |
91 base::Bind(callback, base::Passed(&certs))); | |
86 } | 92 } |
87 | 93 |
88 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const { | 94 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const { |
89 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()); | 95 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()); |
90 } | 96 } |
91 | 97 |
92 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const { | 98 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const { |
93 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()); | 99 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()); |
94 } | 100 } |
95 | 101 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 } | 349 } |
344 | 350 |
345 void NSSCertDatabase::AddObserver(Observer* observer) { | 351 void NSSCertDatabase::AddObserver(Observer* observer) { |
346 observer_list_->AddObserver(observer); | 352 observer_list_->AddObserver(observer); |
347 } | 353 } |
348 | 354 |
349 void NSSCertDatabase::RemoveObserver(Observer* observer) { | 355 void NSSCertDatabase::RemoveObserver(Observer* observer) { |
350 observer_list_->RemoveObserver(observer); | 356 observer_list_->RemoveObserver(observer); |
351 } | 357 } |
352 | 358 |
359 void NSSCertDatabase::SetSlowTaskRunnerForTest( | |
360 const scoped_refptr<base::TaskRunner>& task_runner) { | |
361 slow_task_runner_for_test_ = task_runner; | |
362 } | |
363 | |
364 // static | |
365 void NSSCertDatabase::ListCertsImpl(CertificateList* certs) { | |
366 certs->clear(); | |
367 | |
368 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL); | |
369 CERTCertListNode* node; | |
370 for (node = CERT_LIST_HEAD(cert_list); | |
371 !CERT_LIST_END(node, cert_list); | |
372 node = CERT_LIST_NEXT(node)) { | |
373 certs->push_back(X509Certificate::CreateFromHandle( | |
374 node->cert, X509Certificate::OSCertHandles())); | |
375 } | |
376 CERT_DestroyCertList(cert_list); | |
377 } | |
378 | |
379 scoped_refptr<base::TaskRunner> NSSCertDatabase::GetSlowTaskRunner() const { | |
380 if (slow_task_runner_for_test_) | |
381 return slow_task_runner_for_test_; | |
382 return base::WorkerPool::GetTaskRunner(true /*task is slow*/); | |
383 } | |
384 | |
353 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { | 385 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { |
354 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert)); | 386 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert)); |
355 } | 387 } |
356 | 388 |
357 void NSSCertDatabase::NotifyObserversOfCertRemoved( | 389 void NSSCertDatabase::NotifyObserversOfCertRemoved( |
358 const X509Certificate* cert) { | 390 const X509Certificate* cert) { |
359 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert)); | 391 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert)); |
360 } | 392 } |
361 | 393 |
362 void NSSCertDatabase::NotifyObserversOfCACertChanged( | 394 void NSSCertDatabase::NotifyObserversOfCACertChanged( |
363 const X509Certificate* cert) { | 395 const X509Certificate* cert) { |
364 observer_list_->Notify( | 396 observer_list_->Notify( |
365 &Observer::OnCACertChanged, make_scoped_refptr(cert)); | 397 &Observer::OnCACertChanged, make_scoped_refptr(cert)); |
366 } | 398 } |
367 | 399 |
368 } // namespace net | 400 } // namespace net |
OLD | NEW |