Ensure low-memory renderers retry failed loads correctly.
[chromium-blink-merge.git] / chromeos / cert_loader.cc
blob0940f525d4ceb08ab00a5ff0a528365c8dbadaa0
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
3 // found in the LICENSE file.
5 #include "chromeos/cert_loader.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/task_runner_util.h"
13 #include "base/threading/worker_pool.h"
14 #include "crypto/nss_util.h"
15 #include "crypto/scoped_nss_types.h"
16 #include "net/cert/nss_cert_database.h"
17 #include "net/cert/nss_cert_database_chromeos.h"
18 #include "net/cert/x509_certificate.h"
20 namespace chromeos {
22 static CertLoader* g_cert_loader = NULL;
23 static bool g_force_hardware_backed_for_test = false;
25 // static
26 void CertLoader::Initialize() {
27 CHECK(!g_cert_loader);
28 g_cert_loader = new CertLoader();
31 // static
32 void CertLoader::Shutdown() {
33 CHECK(g_cert_loader);
34 delete g_cert_loader;
35 g_cert_loader = NULL;
38 // static
39 CertLoader* CertLoader::Get() {
40 CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()";
41 return g_cert_loader;
44 // static
45 bool CertLoader::IsInitialized() {
46 return g_cert_loader;
49 CertLoader::CertLoader()
50 : certificates_loaded_(false),
51 certificates_update_required_(false),
52 certificates_update_running_(false),
53 database_(NULL),
54 cert_list_(new net::CertificateList),
55 weak_factory_(this) {
58 CertLoader::~CertLoader() {
59 net::CertDatabase::GetInstance()->RemoveObserver(this);
62 void CertLoader::StartWithNSSDB(net::NSSCertDatabase* database) {
63 CHECK(!database_);
64 database_ = database;
66 // Start observing cert database for changes.
67 // Observing net::CertDatabase is preferred over observing |database_|
68 // directly, as |database_| observers receive only events generated directly
69 // by |database_|, so they may miss a few relevant ones.
70 // TODO(tbarzic): Once singleton NSSCertDatabase is removed, investigate if
71 // it would be OK to observe |database_| directly; or change NSSCertDatabase
72 // to send notification on all relevant changes.
73 net::CertDatabase::GetInstance()->AddObserver(this);
75 LoadCertificates();
78 void CertLoader::AddObserver(CertLoader::Observer* observer) {
79 observers_.AddObserver(observer);
82 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
83 observers_.RemoveObserver(observer);
86 // static
87 bool CertLoader::IsCertificateHardwareBacked(const net::X509Certificate* cert) {
88 if (g_force_hardware_backed_for_test)
89 return true;
90 PK11SlotInfo* slot = cert->os_cert_handle()->slot;
91 return slot && PK11_IsHW(slot);
94 bool CertLoader::CertificatesLoading() const {
95 return database_ && !certificates_loaded_;
98 // static
99 void CertLoader::ForceHardwareBackedForTesting() {
100 g_force_hardware_backed_for_test = true;
103 // static
105 // For background see this discussion on dev-tech-crypto.lists.mozilla.org:
106 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX
108 // NOTE: This function relies on the convention that the same PKCS#11 ID
109 // is shared between a certificate and its associated private and public
110 // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(),
111 // but that always returns NULL on Chrome OS for me.
112 std::string CertLoader::GetPkcs11IdAndSlotForCert(
113 const net::X509Certificate& cert,
114 int* slot_id) {
115 DCHECK(slot_id);
117 CERTCertificateStr* cert_handle = cert.os_cert_handle();
118 SECKEYPrivateKey *priv_key =
119 PK11_FindKeyByAnyCert(cert_handle, NULL /* wincx */);
120 if (!priv_key)
121 return std::string();
123 *slot_id = static_cast<int>(PK11_GetSlotID(priv_key->pkcs11Slot));
125 // Get the CKA_ID attribute for a key.
126 SECItem* sec_item = PK11_GetLowLevelKeyIDForPrivateKey(priv_key);
127 std::string pkcs11_id;
128 if (sec_item) {
129 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
130 SECITEM_FreeItem(sec_item, PR_TRUE);
132 SECKEY_DestroyPrivateKey(priv_key);
134 return pkcs11_id;
137 void CertLoader::LoadCertificates() {
138 CHECK(thread_checker_.CalledOnValidThread());
139 VLOG(1) << "LoadCertificates: " << certificates_update_running_;
141 if (certificates_update_running_) {
142 certificates_update_required_ = true;
143 return;
146 certificates_update_running_ = true;
147 certificates_update_required_ = false;
149 database_->ListCerts(
150 base::Bind(&CertLoader::UpdateCertificates, weak_factory_.GetWeakPtr()));
153 void CertLoader::UpdateCertificates(
154 scoped_ptr<net::CertificateList> cert_list) {
155 CHECK(thread_checker_.CalledOnValidThread());
156 DCHECK(certificates_update_running_);
157 VLOG(1) << "UpdateCertificates: " << cert_list->size();
159 // Ignore any existing certificates.
160 cert_list_ = cert_list.Pass();
162 bool initial_load = !certificates_loaded_;
163 certificates_loaded_ = true;
164 NotifyCertificatesLoaded(initial_load);
166 certificates_update_running_ = false;
167 if (certificates_update_required_)
168 LoadCertificates();
171 void CertLoader::NotifyCertificatesLoaded(bool initial_load) {
172 FOR_EACH_OBSERVER(Observer, observers_,
173 OnCertificatesLoaded(*cert_list_, initial_load));
176 void CertLoader::OnCACertChanged(const net::X509Certificate* cert) {
177 // This is triggered when a CA certificate is modified.
178 VLOG(1) << "OnCACertChanged";
179 LoadCertificates();
182 void CertLoader::OnCertAdded(const net::X509Certificate* cert) {
183 // This is triggered when a client certificate is added.
184 VLOG(1) << "OnCertAdded";
185 LoadCertificates();
188 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
189 VLOG(1) << "OnCertRemoved";
190 LoadCertificates();
193 } // namespace chromeos