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

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/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 "crypto/nss_util.h" 14 #include "crypto/nss_util.h"
15 #include "net/cert/nss_cert_database.h" 15 #include "net/cert/nss_cert_database.h"
16 #include "net/cert/x509_certificate.h" 16 #include "net/cert/x509_certificate.h"
17 17
18 namespace chromeos { 18 namespace chromeos {
19 19
20 namespace { 20 namespace {
21 21
22 // Loads certificates from |cert_database| into |cert_list|. 22 // Loads certificates from |cert_database| into |cert_list|.
23 void LoadNSSCertificates(net::NSSCertDatabase* cert_database, 23 void LoadNSSCertificates(net::NSSCertDatabase* cert_database,
(...skipping 27 matching lines...) Expand all
51 // static 51 // static
52 bool CertLoader::IsInitialized() { 52 bool CertLoader::IsInitialized() {
53 return g_cert_loader; 53 return g_cert_loader;
54 } 54 }
55 55
56 CertLoader::CertLoader() 56 CertLoader::CertLoader()
57 : certificates_requested_(false), 57 : certificates_requested_(false),
58 certificates_loaded_(false), 58 certificates_loaded_(false),
59 certificates_update_required_(false), 59 certificates_update_required_(false),
60 certificates_update_running_(false), 60 certificates_update_running_(false),
61 database_(NULL),
61 tpm_token_slot_id_(-1), 62 tpm_token_slot_id_(-1),
63 is_hardware_backed_(false),
64 hardware_backed_for_test_(false),
62 weak_factory_(this) { 65 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 } 66 }
71 67
72 CertLoader::~CertLoader() { 68 CertLoader::~CertLoader() {
73 net::CertDatabase::GetInstance()->RemoveObserver(this); 69 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 } 70 }
93 71
94 // This is copied from chrome/common/net/x509_certificate_model_nss.cc. 72 // 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: 73 // For background see this discussion on dev-tech-crypto.lists.mozilla.org:
96 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX 74 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX
97 // 75 //
98 // NOTE: This function relies on the convention that the same PKCS#11 ID 76 // 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 77 // is shared between a certificate and its associated private and public
100 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(), 78 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(),
101 // but that always returns NULL on Chrome OS for me. 79 // but that always returns NULL on Chrome OS for me.
(...skipping 11 matching lines...) Expand all
113 std::string pkcs11_id; 91 std::string pkcs11_id;
114 if (sec_item) { 92 if (sec_item) {
115 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); 93 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
116 SECITEM_FreeItem(sec_item, PR_TRUE); 94 SECITEM_FreeItem(sec_item, PR_TRUE);
117 } 95 }
118 SECKEY_DestroyPrivateKey(priv_key); 96 SECKEY_DestroyPrivateKey(priv_key);
119 97
120 return pkcs11_id; 98 return pkcs11_id;
121 } 99 }
122 100
123 void CertLoader::RequestCertificates() { 101 void CertLoader::SetCryptoTaskRunner(
102 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner) {
103 crypto_task_runner_ = crypto_task_runner;
104 }
105
106 void CertLoader::AddObserver(CertLoader::Observer* observer) {
107 observers_.AddObserver(observer);
108 }
109
110 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
111 observers_.RemoveObserver(observer);
112 }
113
114 bool CertLoader::IsCertificateHardwareBacked(
115 const net::X509Certificate* cert) const {
116 if (!database_ || !cert)
117 return false;
118 return database_->IsHardwareBacked(cert);
119 }
120
121 bool CertLoader::CertificatesLoading() const {
122 return certificates_requested_ && !certificates_loaded_;
123 }
124
125 void CertLoader::StartWithNSSDB(net::NSSCertDatabase* database) {
124 if (certificates_requested_) 126 if (certificates_requested_)
125 return; 127 return;
126 certificates_requested_ = true; 128 certificates_requested_ = true;
127 129
128 DCHECK(!certificates_loaded_ && !certificates_update_running_); 130 CHECK(database);
131 CHECK(!database_);
132 CHECK(crypto_task_runner_) << "Crypto task runner not set.";
133
134 database_ = database;
135 tpm_token_slot_id_ =
136 static_cast<int>(PK11_GetSlotID(database_->GetPrivateSlot().get()));
137
138 is_hardware_backed_ = hardware_backed_for_test_ ||
139 PK11_IsHW(database_->GetPrivateSlot().get());
140
141 // Start observing cert database for changes.
129 net::CertDatabase::GetInstance()->AddObserver(this); 142 net::CertDatabase::GetInstance()->AddObserver(this);
143
130 LoadCertificates(); 144 LoadCertificates();
131 } 145 }
132 146
133 void CertLoader::LoadCertificates() { 147 void CertLoader::LoadCertificates() {
134 CHECK(thread_checker_.CalledOnValidThread()); 148 CHECK(thread_checker_.CalledOnValidThread());
135 VLOG(1) << "LoadCertificates: " << certificates_update_running_; 149 VLOG(1) << "LoadCertificates: " << certificates_update_running_;
136 150
137 if (certificates_update_running_) { 151 if (certificates_update_running_) {
138 certificates_update_required_ = true; 152 certificates_update_required_ = true;
139 return; 153 return;
140 } 154 }
141 155
142 net::CertificateList* cert_list = new net::CertificateList; 156 net::CertificateList* cert_list = new net::CertificateList;
143 certificates_update_running_ = true; 157 certificates_update_running_ = true;
144 certificates_update_required_ = false; 158 certificates_update_required_ = false;
145 159
146 base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); 160 crypto_task_runner_->PostTaskAndReply(
147 if (!task_runner)
148 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */);
149 task_runner->PostTaskAndReply(
150 FROM_HERE, 161 FROM_HERE,
151 base::Bind(LoadNSSCertificates, 162 base::Bind(LoadNSSCertificates, database_, cert_list),
152 net::NSSCertDatabase::GetInstance(),
153 cert_list),
154 base::Bind(&CertLoader::UpdateCertificates, 163 base::Bind(&CertLoader::UpdateCertificates,
155 weak_factory_.GetWeakPtr(), 164 weak_factory_.GetWeakPtr(),
156 base::Owned(cert_list))); 165 base::Owned(cert_list)));
157 } 166 }
158 167
159 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { 168 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) {
160 CHECK(thread_checker_.CalledOnValidThread()); 169 CHECK(thread_checker_.CalledOnValidThread());
161 DCHECK(certificates_update_running_); 170 DCHECK(certificates_update_running_);
162 VLOG(1) << "UpdateCertificates: " << cert_list->size(); 171 VLOG(1) << "UpdateCertificates: " << cert_list->size();
163 172
(...skipping 24 matching lines...) Expand all
188 // This is triggered when a client certificate is added. 197 // This is triggered when a client certificate is added.
189 VLOG(1) << "OnCertAdded"; 198 VLOG(1) << "OnCertAdded";
190 LoadCertificates(); 199 LoadCertificates();
191 } 200 }
192 201
193 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { 202 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
194 VLOG(1) << "OnCertRemoved"; 203 VLOG(1) << "OnCertRemoved";
195 LoadCertificates(); 204 LoadCertificates();
196 } 205 }
197 206
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 207 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698