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

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

Issue 144423007: Make NSSCertDatabase::ListCerts work async on a worker thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa Created 6 years, 11 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/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 certs->clear();
76 79
77 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL); 80 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL);
78 CERTCertListNode* node; 81 CERTCertListNode* node;
79 for (node = CERT_LIST_HEAD(cert_list); 82 for (node = CERT_LIST_HEAD(cert_list);
80 !CERT_LIST_END(node, cert_list); 83 !CERT_LIST_END(node, cert_list);
81 node = CERT_LIST_NEXT(node)) { 84 node = CERT_LIST_NEXT(node)) {
82 certs->push_back(X509Certificate::CreateFromHandle( 85 certs->push_back(X509Certificate::CreateFromHandle(
83 node->cert, X509Certificate::OSCertHandles())); 86 node->cert, X509Certificate::OSCertHandles()));
84 } 87 }
85 CERT_DestroyCertList(cert_list); 88 CERT_DestroyCertList(cert_list);
86 } 89 }
87 90
91 void NSSCertDatabase::ListCerts(
92 const base::Callback<void(scoped_ptr<CertificateList> certs)>& callback) {
93 scoped_ptr<CertificateList> certs(new CertificateList());
94 CertificateList* raw_certs = certs.get();
95
96 scoped_ptr<NSSDatabaseFilter::CertNotAllowedPredicate> predicate;
97
98 scoped_refptr<NSSDatabaseFilter> database_filter = GetDatabaseFilter();
99 if (database_filter)
100 predicate.reset(new NSSDatabaseFilter::CertNotAllowedPredicate(
101 database_filter));
102
103 GetSlowTaskRunner()->PostTaskAndReply(
104 FROM_HERE,
105 base::Bind(&NSSCertDatabase::ListAndFilterCerts,
106 base::Passed(&predicate),
107 base::Unretained(raw_certs)),
108 base::Bind(callback, base::Passed(&certs)));
109 }
110
88 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const { 111 crypto::ScopedPK11Slot NSSCertDatabase::GetPublicSlot() const {
89 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()); 112 return crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot());
90 } 113 }
91 114
92 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const { 115 crypto::ScopedPK11Slot NSSCertDatabase::GetPrivateSlot() const {
93 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()); 116 return crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot());
94 } 117 }
95 118
96 CryptoModule* NSSCertDatabase::GetPublicModule() const { 119 CryptoModule* NSSCertDatabase::GetPublicModule() const {
97 crypto::ScopedPK11Slot slot(GetPublicSlot()); 120 crypto::ScopedPK11Slot slot(GetPublicSlot());
98 return CryptoModule::CreateFromHandle(slot.get()); 121 return CryptoModule::CreateFromHandle(slot.get());
99 } 122 }
100 123
101 CryptoModule* NSSCertDatabase::GetPrivateModule() const { 124 CryptoModule* NSSCertDatabase::GetPrivateModule() const {
102 crypto::ScopedPK11Slot slot(GetPrivateSlot()); 125 crypto::ScopedPK11Slot slot(GetPrivateSlot());
103 return CryptoModule::CreateFromHandle(slot.get()); 126 return CryptoModule::CreateFromHandle(slot.get());
104 } 127 }
105 128
106 void NSSCertDatabase::ListModules(CryptoModuleList* modules, 129 void NSSCertDatabase::ListModulesSync(CryptoModuleList* modules,
107 bool need_rw) const { 130 bool need_rw) const {
108 modules->clear(); 131 modules->clear();
109 132
110 // The wincx arg is unused since we don't call PK11_SetIsLoggedInFunc. 133 // The wincx arg is unused since we don't call PK11_SetIsLoggedInFunc.
111 crypto::ScopedPK11SlotList slot_list( 134 crypto::ScopedPK11SlotList slot_list(
112 PK11_GetAllTokens(CKM_INVALID_MECHANISM, 135 PK11_GetAllTokens(CKM_INVALID_MECHANISM,
113 need_rw ? PR_TRUE : PR_FALSE, // needRW 136 need_rw ? PR_TRUE : PR_FALSE, // needRW
114 PR_TRUE, // loadCerts (unused) 137 PR_TRUE, // loadCerts (unused)
115 NULL)); // wincx 138 NULL)); // wincx
116 if (!slot_list) { 139 if (!slot_list) {
117 LOG(ERROR) << "PK11_GetAllTokens failed: " << PORT_GetError(); 140 LOG(ERROR) << "PK11_GetAllTokens failed: " << PORT_GetError();
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 } 366 }
344 367
345 void NSSCertDatabase::AddObserver(Observer* observer) { 368 void NSSCertDatabase::AddObserver(Observer* observer) {
346 observer_list_->AddObserver(observer); 369 observer_list_->AddObserver(observer);
347 } 370 }
348 371
349 void NSSCertDatabase::RemoveObserver(Observer* observer) { 372 void NSSCertDatabase::RemoveObserver(Observer* observer) {
350 observer_list_->RemoveObserver(observer); 373 observer_list_->RemoveObserver(observer);
351 } 374 }
352 375
376 void NSSCertDatabase::SetSlowTaskRunnerForTest(
377 const scoped_refptr<base::TaskRunner>& task_runner) {
378 slow_task_runner_for_test_ = task_runner;
379 }
380
381 // static
382 void NSSCertDatabase::ListAndFilterCerts(
383 scoped_ptr<NSSDatabaseFilter::CertNotAllowedPredicate> predicate,
384 CertificateList* certs) {
385 CHECK(certs);
386 certs->clear();
387
388 CERTCertList* cert_list = PK11_ListCerts(PK11CertListUnique, NULL);
389 CERTCertListNode* node;
390 for (node = CERT_LIST_HEAD(cert_list);
391 !CERT_LIST_END(node, cert_list);
392 node = CERT_LIST_NEXT(node)) {
393 certs->push_back(X509Certificate::CreateFromHandle(
394 node->cert, X509Certificate::OSCertHandles()));
395 }
396 CERT_DestroyCertList(cert_list);
397
398 // No certificate filter was set.
399 if (!predicate)
400 return;
401
402 size_t pre_size = certs->size();
403 certs->erase(std::remove_if(certs->begin(), certs->end(), *predicate),
404 certs->end());
405 DVLOG(1) << "filtered " << pre_size - certs->size() << " of " << pre_size
406 << " certs";
407 }
408
353 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { 409 void NSSCertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) {
354 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert)); 410 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert));
355 } 411 }
356 412
357 void NSSCertDatabase::NotifyObserversOfCertRemoved( 413 void NSSCertDatabase::NotifyObserversOfCertRemoved(
358 const X509Certificate* cert) { 414 const X509Certificate* cert) {
359 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert)); 415 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert));
360 } 416 }
361 417
362 void NSSCertDatabase::NotifyObserversOfCACertChanged( 418 void NSSCertDatabase::NotifyObserversOfCACertChanged(
363 const X509Certificate* cert) { 419 const X509Certificate* cert) {
364 observer_list_->Notify( 420 observer_list_->Notify(
365 &Observer::OnCACertChanged, make_scoped_refptr(cert)); 421 &Observer::OnCACertChanged, make_scoped_refptr(cert));
366 } 422 }
367 423
424 scoped_refptr<NSSDatabaseFilter> NSSCertDatabase::GetDatabaseFilter() const {
425 return NULL;
426 }
427
428 scoped_refptr<base::TaskRunner> NSSCertDatabase::GetSlowTaskRunner() const {
429 if (slow_task_runner_for_test_)
430 return slow_task_runner_for_test_;
431 return base::WorkerPool::GetTaskRunner(true /*task is slow*/);
432 }
433
368 } // namespace net 434 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698