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

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: . 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/message_loop/message_loop_proxy.h"
12 #include "base/sequenced_task_runner.h"
11 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
12 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
13 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
14 #include "crypto/nss_util.h" 16 #include "crypto/nss_util.h"
15 #include "net/cert/nss_cert_database.h" 17 #include "crypto/nss_util_internal.h"
18 #include "net/cert/nss_cert_database_chromeos.h"
16 #include "net/cert/x509_certificate.h" 19 #include "net/cert/x509_certificate.h"
17 20
18 namespace chromeos { 21 namespace chromeos {
19 22
20 namespace { 23 namespace {
21 24
25 typedef base::Callback<void(scoped_ptr<net::NSSCertDatabaseChromeOS> db)>
26 DatabaseCreatedCallback;
27
28 void RelayToMessageLoop(
29 const scoped_refptr<base::MessageLoopProxy>& message_loop_proxy,
30 const DatabaseCreatedCallback& callback,
31 scoped_ptr<net::NSSCertDatabaseChromeOS> database) {
32 message_loop_proxy->PostTask(FROM_HERE,
33 base::Bind(callback, base::Passed(&database)));
34 }
35
36 void DidGetPrivateSlot(const DatabaseCreatedCallback& callback,
37 const std::string& userhash,
38 crypto::ScopedPK11Slot private_slot) {
39 scoped_ptr<net::NSSCertDatabaseChromeOS> database(
40 new net::NSSCertDatabaseChromeOS(
41 crypto::GetPublicSlotForChromeOSUser(userhash),
42 private_slot.Pass()));
43 callback.Run(database.Pass());
44 }
45
46 void CreateDBForUser(const std::string& userhash,
47 const DatabaseCreatedCallback& callback) {
48 CHECK(crypto::GetPublicSlotForChromeOSUser(userhash))
49 << "Trying to create user's NSS db instance before the NSS has been "
50 << "initialized for the user";
51
52 base::Callback<void(crypto::ScopedPK11Slot private_slot)> on_private_slot =
53 base::Bind(&DidGetPrivateSlot, callback, userhash);
54
55 // Note that callback passed to |crypto::GetPrivateSlotForChromeOSUser| is
56 // called only if the function returns NULL.
57 crypto::ScopedPK11Slot private_slot(
58 crypto::GetPrivateSlotForChromeOSUser(userhash, on_private_slot));
59
60 if (private_slot)
61 on_private_slot.Run(private_slot.Pass());
62 }
63
22 // Loads certificates from |cert_database| into |cert_list|. 64 // Loads certificates from |cert_database| into |cert_list|.
23 void LoadNSSCertificates(net::NSSCertDatabase* cert_database, 65 void LoadNSSCertificates(net::NSSCertDatabaseChromeOS* cert_database,
24 net::CertificateList* cert_list) { 66 net::CertificateList* cert_list) {
25 cert_database->ListCerts(cert_list); 67 cert_database->ListCerts(cert_list);
26 } 68 }
27 69
28 } // namespace 70 } // namespace
29 71
30 static CertLoader* g_cert_loader = NULL; 72 static CertLoader* g_cert_loader = NULL;
31 73
32 // static 74 // static
33 void CertLoader::Initialize() { 75 void CertLoader::Initialize() {
(...skipping 18 matching lines...) Expand all
52 bool CertLoader::IsInitialized() { 94 bool CertLoader::IsInitialized() {
53 return g_cert_loader; 95 return g_cert_loader;
54 } 96 }
55 97
56 CertLoader::CertLoader() 98 CertLoader::CertLoader()
57 : certificates_requested_(false), 99 : certificates_requested_(false),
58 certificates_loaded_(false), 100 certificates_loaded_(false),
59 certificates_update_required_(false), 101 certificates_update_required_(false),
60 certificates_update_running_(false), 102 certificates_update_running_(false),
61 tpm_token_slot_id_(-1), 103 tpm_token_slot_id_(-1),
104 is_hardware_backed_(false),
105 hardware_backed_for_test_(false),
62 weak_factory_(this) { 106 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 } 107 }
71 108
72 CertLoader::~CertLoader() { 109 CertLoader::~CertLoader() {
73 net::CertDatabase::GetInstance()->RemoveObserver(this); 110 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 } 111 }
93 112
94 // This is copied from chrome/common/net/x509_certificate_model_nss.cc. 113 // 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: 114 // For background see this discussion on dev-tech-crypto.lists.mozilla.org:
96 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX 115 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX
97 // 116 //
98 // NOTE: This function relies on the convention that the same PKCS#11 ID 117 // 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 118 // is shared between a certificate and its associated private and public
100 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(), 119 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(),
101 // but that always returns NULL on Chrome OS for me. 120 // but that always returns NULL on Chrome OS for me.
(...skipping 11 matching lines...) Expand all
113 std::string pkcs11_id; 132 std::string pkcs11_id;
114 if (sec_item) { 133 if (sec_item) {
115 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); 134 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
116 SECITEM_FreeItem(sec_item, PR_TRUE); 135 SECITEM_FreeItem(sec_item, PR_TRUE);
117 } 136 }
118 SECKEY_DestroyPrivateKey(priv_key); 137 SECKEY_DestroyPrivateKey(priv_key);
119 138
120 return pkcs11_id; 139 return pkcs11_id;
121 } 140 }
122 141
142 void CertLoader::StartWithUser(const std::string& userhash) {
143 VLOG(1) << "Start CertLoader with user " << userhash;
144
145 CHECK(userhash_.empty());
146 CHECK(!userhash.empty());
147 userhash_ = userhash;
148 RequestCertificates();
149 }
150
151 void CertLoader::SetCryptoTaskRunner(
152 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner) {
153 crypto_task_runner_ = crypto_task_runner;
154 }
155
156 void CertLoader::SetSlowTaskRunnerForTest(
157 const scoped_refptr<base::TaskRunner>& task_runner) {
158 slow_task_runner_for_test_ = task_runner;
159 }
160
161 void CertLoader::AddObserver(CertLoader::Observer* observer) {
162 observers_.AddObserver(observer);
163 }
164
165 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
166 observers_.RemoveObserver(observer);
167 }
168
169 bool CertLoader::IsCertificateInPrivateSlot(
170 const net::X509Certificate& cert) const {
171 if (!certificates_loaded_)
172 return false;
173 if (!cert.os_cert_handle() || !cert.os_cert_handle()->slot)
174 return false;
175 int cert_slot = static_cast<int>(PK11_GetSlotID(cert.os_cert_handle()->slot));
176 return cert_slot == tpm_token_slot_id_;
177 }
178
179 bool CertLoader::CertificatesLoading() const {
180 return certificates_requested_ && !certificates_loaded_;
181 }
182
123 void CertLoader::RequestCertificates() { 183 void CertLoader::RequestCertificates() {
124 if (certificates_requested_) 184 if (certificates_requested_)
125 return; 185 return;
126 certificates_requested_ = true; 186 certificates_requested_ = true;
127 187
188 CHECK(crypto_task_runner_) << "Crypto task runner not set.";
128 DCHECK(!certificates_loaded_ && !certificates_update_running_); 189 DCHECK(!certificates_loaded_ && !certificates_update_running_);
190
191 DatabaseCreatedCallback on_db_ready =
192 base::Bind(&RelayToMessageLoop,
193 base::MessageLoopProxy::current(),
194 base::Bind(&CertLoader::OnDatabaseReady,
195 weak_factory_.GetWeakPtr()));
196 crypto_task_runner_->PostTask(FROM_HERE,
197 base::Bind(&CreateDBForUser, userhash_, on_db_ready));
198 }
199
200 void CertLoader::OnDatabaseReady(
201 scoped_ptr<net::NSSCertDatabaseChromeOS> database) {
202 database_ = database.Pass();
203 tpm_token_slot_id_ =
204 static_cast<int>(PK11_GetSlotID(database_->GetPrivateSlot().get()));
205
206 int software_slot_id =
207 static_cast<int>(PK11_GetSlotID(database_->GetPublicSlot().get()));
208 is_hardware_backed_ = hardware_backed_for_test_ ||
209 tpm_token_slot_id_ != software_slot_id;
210
211 // Start observing cert database for changes.
129 net::CertDatabase::GetInstance()->AddObserver(this); 212 net::CertDatabase::GetInstance()->AddObserver(this);
213
130 LoadCertificates(); 214 LoadCertificates();
131 } 215 }
132 216
133 void CertLoader::LoadCertificates() { 217 void CertLoader::LoadCertificates() {
134 CHECK(thread_checker_.CalledOnValidThread()); 218 CHECK(thread_checker_.CalledOnValidThread());
135 VLOG(1) << "LoadCertificates: " << certificates_update_running_; 219 VLOG(1) << "LoadCertificates: " << certificates_update_running_;
136 220
137 if (certificates_update_running_) { 221 if (certificates_update_running_) {
138 certificates_update_required_ = true; 222 certificates_update_required_ = true;
139 return; 223 return;
140 } 224 }
141 225
142 net::CertificateList* cert_list = new net::CertificateList; 226 net::CertificateList* cert_list = new net::CertificateList;
143 certificates_update_running_ = true; 227 certificates_update_running_ = true;
144 certificates_update_required_ = false; 228 certificates_update_required_ = false;
145 229
146 base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); 230 base::TaskRunner* task_runner = slow_task_runner_for_test_.get();
147 if (!task_runner) 231 if (!task_runner)
148 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */); 232 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */);
149 task_runner->PostTaskAndReply( 233 task_runner->PostTaskAndReply(
150 FROM_HERE, 234 FROM_HERE,
151 base::Bind(LoadNSSCertificates, 235 base::Bind(LoadNSSCertificates,
152 net::NSSCertDatabase::GetInstance(), 236 // Create a copy of the database so it can be used on the
237 // worker pool.
238 base::Owned(new net::NSSCertDatabaseChromeOS(
mattm 2014/01/23 01:45:55 hmm.. I think it would be preferable to have the N
tbarzic 2014/01/23 04:45:28 I'm not sure that by itself would help here. Unles
mattm 2014/01/23 19:46:52 Yeah, what I meant was NSSCertDatabase would inter
tbarzic 2014/01/23 19:52:03 OK, then I'll keep creating new instance NSSCertDa
239 database_->GetPublicSlot(),
240 database_->GetPrivateSlot())),
153 cert_list), 241 cert_list),
154 base::Bind(&CertLoader::UpdateCertificates, 242 base::Bind(&CertLoader::UpdateCertificates,
155 weak_factory_.GetWeakPtr(), 243 weak_factory_.GetWeakPtr(),
156 base::Owned(cert_list))); 244 base::Owned(cert_list)));
157 } 245 }
158 246
159 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { 247 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) {
160 CHECK(thread_checker_.CalledOnValidThread()); 248 CHECK(thread_checker_.CalledOnValidThread());
161 DCHECK(certificates_update_running_); 249 DCHECK(certificates_update_running_);
162 VLOG(1) << "UpdateCertificates: " << cert_list->size(); 250 VLOG(1) << "UpdateCertificates: " << cert_list->size();
(...skipping 25 matching lines...) Expand all
188 // This is triggered when a client certificate is added. 276 // This is triggered when a client certificate is added.
189 VLOG(1) << "OnCertAdded"; 277 VLOG(1) << "OnCertAdded";
190 LoadCertificates(); 278 LoadCertificates();
191 } 279 }
192 280
193 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { 281 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
194 VLOG(1) << "OnCertRemoved"; 282 VLOG(1) << "OnCertRemoved";
195 LoadCertificates(); 283 LoadCertificates();
196 } 284 }
197 285
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 286 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698