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

Side by Side Diff: chromeos/cert_loader.cc

Issue 135193007: Use user specific NSSDatabase in CertLoader. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: return of slow task runner 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "chromeos/cert_loader.h" 5 #include "chromeos/cert_loader.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/sequenced_task_runner.h"
11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
12 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
13 #include "base/threading/worker_pool.h" 14 #include "base/threading/worker_pool.h"
14 #include "crypto/nss_util.h" 15 #include "crypto/nss_util.h"
15 #include "net/cert/nss_cert_database.h" 16 #include "net/cert/nss_cert_database.h"
17 #include "net/cert/nss_cert_database_chromeos.h"
16 #include "net/cert/x509_certificate.h" 18 #include "net/cert/x509_certificate.h"
17 19
18 namespace chromeos { 20 namespace chromeos {
19 21
20 namespace { 22 namespace {
21 23
22 // Loads certificates from |cert_database| into |cert_list|. 24 // Loads certificates from |cert_database| into |cert_list|.
23 void LoadNSSCertificates(net::NSSCertDatabase* cert_database, 25 void LoadNSSCertificates(net::NSSCertDatabase* cert_database,
24 net::CertificateList* cert_list) { 26 net::CertificateList* cert_list) {
25 cert_database->ListCerts(cert_list); 27 cert_database->ListCerts(cert_list);
(...skipping 25 matching lines...) Expand all
51 // static 53 // static
52 bool CertLoader::IsInitialized() { 54 bool CertLoader::IsInitialized() {
53 return g_cert_loader; 55 return g_cert_loader;
54 } 56 }
55 57
56 CertLoader::CertLoader() 58 CertLoader::CertLoader()
57 : certificates_requested_(false), 59 : certificates_requested_(false),
58 certificates_loaded_(false), 60 certificates_loaded_(false),
59 certificates_update_required_(false), 61 certificates_update_required_(false),
60 certificates_update_running_(false), 62 certificates_update_running_(false),
63 database_(NULL),
61 tpm_token_slot_id_(-1), 64 tpm_token_slot_id_(-1),
65 is_hardware_backed_(false),
66 hardware_backed_for_test_(false),
62 weak_factory_(this) { 67 weak_factory_(this) {
63 if (TPMTokenLoader::IsInitialized())
64 TPMTokenLoader::Get()->AddObserver(this);
65 }
66
67 void CertLoader::SetSlowTaskRunnerForTest(
68 const scoped_refptr<base::TaskRunner>& task_runner) {
69 slow_task_runner_for_test_ = task_runner;
70 } 68 }
71 69
72 CertLoader::~CertLoader() { 70 CertLoader::~CertLoader() {
73 net::CertDatabase::GetInstance()->RemoveObserver(this); 71 net::CertDatabase::GetInstance()->RemoveObserver(this);
74 if (TPMTokenLoader::IsInitialized())
75 TPMTokenLoader::Get()->RemoveObserver(this);
76 }
77
78 void CertLoader::AddObserver(CertLoader::Observer* observer) {
79 observers_.AddObserver(observer);
80 }
81
82 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
83 observers_.RemoveObserver(observer);
84 }
85
86 bool CertLoader::IsHardwareBacked() const {
87 return !tpm_token_name_.empty();
88 }
89
90 bool CertLoader::CertificatesLoading() const {
91 return certificates_requested_ && !certificates_loaded_;
92 } 72 }
93 73
94 // This is copied from chrome/common/net/x509_certificate_model_nss.cc. 74 // This is copied from chrome/common/net/x509_certificate_model_nss.cc.
95 // For background see this discussion on dev-tech-crypto.lists.mozilla.org: 75 // For background see this discussion on dev-tech-crypto.lists.mozilla.org:
96 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX 76 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX
97 // 77 //
98 // NOTE: This function relies on the convention that the same PKCS#11 ID 78 // NOTE: This function relies on the convention that the same PKCS#11 ID
99 // is shared between a certificate and its associated private and public 79 // is shared between a certificate and its associated private and public
100 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(), 80 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(),
101 // but that always returns NULL on Chrome OS for me. 81 // but that always returns NULL on Chrome OS for me.
(...skipping 11 matching lines...) Expand all
113 std::string pkcs11_id; 93 std::string pkcs11_id;
114 if (sec_item) { 94 if (sec_item) {
115 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); 95 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
116 SECITEM_FreeItem(sec_item, PR_TRUE); 96 SECITEM_FreeItem(sec_item, PR_TRUE);
117 } 97 }
118 SECKEY_DestroyPrivateKey(priv_key); 98 SECKEY_DestroyPrivateKey(priv_key);
119 99
120 return pkcs11_id; 100 return pkcs11_id;
121 } 101 }
122 102
123 void CertLoader::RequestCertificates() { 103 void CertLoader::SetSlowTaskRunnerForTest(
pneubeck (no reviews) 2014/01/24 13:18:02 nit: If you already reorder the definitions, could
tbarzic 2014/01/25 00:26:27 Done.
104 const scoped_refptr<base::TaskRunner>& task_runner) {
105 slow_task_runner_for_test_ = task_runner;
106 }
107
108 void CertLoader::AddObserver(CertLoader::Observer* observer) {
109 observers_.AddObserver(observer);
110 }
111
112 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
113 observers_.RemoveObserver(observer);
114 }
115
116 bool CertLoader::IsCertificateHardwareBacked(
117 const net::X509Certificate* cert) const {
118 if (!database_ || !cert)
pneubeck (no reviews) 2014/01/24 13:18:02 DCHECK(cert) (or let it crash in IsHardwareBacked
tbarzic 2014/01/25 00:26:27 Done.
119 return false;
120 return database_->IsHardwareBacked(cert);
121 }
122
123 bool CertLoader::CertificatesLoading() const {
124 return certificates_requested_ && !certificates_loaded_;
125 }
126
127 void CertLoader::StartWithNSSDB(net::NSSCertDatabase* database) {
124 if (certificates_requested_) 128 if (certificates_requested_)
pneubeck (no reviews) 2014/01/24 13:18:02 this shouldn't be called more than once anymore, r
tbarzic 2014/01/25 00:26:27 Done.
125 return; 129 return;
126 certificates_requested_ = true; 130 certificates_requested_ = true;
127 131
128 DCHECK(!certificates_loaded_ && !certificates_update_running_); 132 CHECK(database);
pneubeck (no reviews) 2014/01/24 13:18:02 nit: not necessary, will crash anyways below.
tbarzic 2014/01/25 00:26:27 Done.
133 CHECK(!database_);
134
135 database_ = database;
136 tpm_token_slot_id_ =
pneubeck (no reviews) 2014/01/24 13:18:02 Has to be cached? Why not calculate every time?
tbarzic 2014/01/25 00:26:27 Done.
137 static_cast<int>(PK11_GetSlotID(database_->GetPrivateSlot().get()));
138
139 is_hardware_backed_ = hardware_backed_for_test_ ||
pneubeck (no reviews) 2014/01/24 13:18:02 hardware_backed_for_test_ can only switch is_hardw
tbarzic 2014/01/25 00:26:27 Done.
140 PK11_IsHW(database_->GetPrivateSlot().get());
141
142 // Start observing cert database for changes.
129 net::CertDatabase::GetInstance()->AddObserver(this); 143 net::CertDatabase::GetInstance()->AddObserver(this);
144
130 LoadCertificates(); 145 LoadCertificates();
131 } 146 }
132 147
133 void CertLoader::LoadCertificates() { 148 void CertLoader::LoadCertificates() {
134 CHECK(thread_checker_.CalledOnValidThread()); 149 CHECK(thread_checker_.CalledOnValidThread());
135 VLOG(1) << "LoadCertificates: " << certificates_update_running_; 150 VLOG(1) << "LoadCertificates: " << certificates_update_running_;
136 151
137 if (certificates_update_running_) { 152 if (certificates_update_running_) {
138 certificates_update_required_ = true; 153 certificates_update_required_ = true;
139 return; 154 return;
140 } 155 }
141 156
142 net::CertificateList* cert_list = new net::CertificateList; 157 net::CertificateList* cert_list = new net::CertificateList;
143 certificates_update_running_ = true; 158 certificates_update_running_ = true;
144 certificates_update_required_ = false; 159 certificates_update_required_ = false;
145 160
146 base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); 161 base::TaskRunner* task_runner = slow_task_runner_for_test_.get();
147 if (!task_runner) 162 if (!task_runner)
148 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */); 163 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */);
149 task_runner->PostTaskAndReply( 164 task_runner->PostTaskAndReply(
150 FROM_HERE, 165 FROM_HERE,
151 base::Bind(LoadNSSCertificates, 166 base::Bind(LoadNSSCertificates,
152 net::NSSCertDatabase::GetInstance(), 167 // Create a copy of the database so it can be used on the
168 // worker pool.
pneubeck (no reviews) 2014/01/24 13:18:02 if you plan to change this, please add a TODO
tbarzic 2014/01/25 00:26:27 Done.
169 base::Owned(new net::NSSCertDatabaseChromeOS(
170 database_->GetPublicSlot(),
171 database_->GetPrivateSlot())),
153 cert_list), 172 cert_list),
154 base::Bind(&CertLoader::UpdateCertificates, 173 base::Bind(&CertLoader::UpdateCertificates,
155 weak_factory_.GetWeakPtr(), 174 weak_factory_.GetWeakPtr(),
156 base::Owned(cert_list))); 175 base::Owned(cert_list)));
157 } 176 }
158 177
159 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { 178 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) {
160 CHECK(thread_checker_.CalledOnValidThread()); 179 CHECK(thread_checker_.CalledOnValidThread());
161 DCHECK(certificates_update_running_); 180 DCHECK(certificates_update_running_);
162 VLOG(1) << "UpdateCertificates: " << cert_list->size(); 181 VLOG(1) << "UpdateCertificates: " << cert_list->size();
(...skipping 25 matching lines...) Expand all
188 // This is triggered when a client certificate is added. 207 // This is triggered when a client certificate is added.
189 VLOG(1) << "OnCertAdded"; 208 VLOG(1) << "OnCertAdded";
190 LoadCertificates(); 209 LoadCertificates();
191 } 210 }
192 211
193 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { 212 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
194 VLOG(1) << "OnCertRemoved"; 213 VLOG(1) << "OnCertRemoved";
195 LoadCertificates(); 214 LoadCertificates();
196 } 215 }
197 216
198 void CertLoader::OnTPMTokenReady(const std::string& tpm_user_pin,
199 const std::string& tpm_token_name,
200 int tpm_token_slot_id) {
201 tpm_user_pin_ = tpm_user_pin;
202 tpm_token_name_ = tpm_token_name;
203 tpm_token_slot_id_ = tpm_token_slot_id;
204
205 VLOG(1) << "TPM token ready.";
206 RequestCertificates();
207 }
208
209 } // namespace chromeos 217 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698