[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 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. |
| 4 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 5 | #include "chromeos/cert_loader.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/location.h" |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 11 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 13 | #include "base/task_runner_util.h" |
| 14 | #include "base/threading/worker_pool.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 15 | #include "crypto/nss_util.h" |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 16 | #include "crypto/scoped_nss_types.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 17 | #include "net/cert/nss_cert_database.h" |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 18 | #include "net/cert/nss_cert_database_chromeos.h" |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 19 | #include "net/cert/x509_certificate.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 20 | |
| 21 | namespace chromeos { |
| 22 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 23 | static CertLoader* g_cert_loader = NULL; |
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 24 | static bool g_force_hardware_backed_for_test = false; |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 25 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 26 | // static |
| 27 | void CertLoader::Initialize() { |
| 28 | CHECK(!g_cert_loader); |
| 29 | g_cert_loader = new CertLoader(); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // static |
| 33 | void CertLoader::Shutdown() { |
| 34 | CHECK(g_cert_loader); |
| 35 | delete g_cert_loader; |
| 36 | g_cert_loader = NULL; |
| 37 | } |
| 38 | |
| 39 | // static |
| 40 | CertLoader* CertLoader::Get() { |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 41 | CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()"; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 42 | return g_cert_loader; |
| 43 | } |
| 44 | |
| 45 | // static |
| 46 | bool CertLoader::IsInitialized() { |
| 47 | return g_cert_loader; |
| 48 | } |
| 49 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 50 | CertLoader::CertLoader() |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 51 | : certificates_loaded_(false), |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 52 | certificates_update_required_(false), |
| 53 | certificates_update_running_(false), |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 54 | database_(NULL), |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 55 | cert_list_(new net::CertificateList), |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 56 | weak_factory_(this) { |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | CertLoader::~CertLoader() { |
| 60 | net::CertDatabase::GetInstance()->RemoveObserver(this); |
| 61 | } |
| 62 | |
| 63 | void CertLoader::StartWithNSSDB(net::NSSCertDatabase* database) { |
| 64 | CHECK(!database_); |
| 65 | database_ = database; |
| 66 | |
| 67 | // Start observing cert database for changes. |
| 68 | // Observing net::CertDatabase is preferred over observing |database_| |
| 69 | // directly, as |database_| observers receive only events generated directly |
| 70 | // by |database_|, so they may miss a few relevant ones. |
| 71 | // TODO(tbarzic): Once singleton NSSCertDatabase is removed, investigate if |
| 72 | // it would be OK to observe |database_| directly; or change NSSCertDatabase |
| 73 | // to send notification on all relevant changes. |
| 74 | net::CertDatabase::GetInstance()->AddObserver(this); |
| 75 | |
| 76 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 79 | void CertLoader::AddObserver(CertLoader::Observer* observer) { |
| 80 | observers_.AddObserver(observer); |
| 81 | } |
| 82 | |
| 83 | void CertLoader::RemoveObserver(CertLoader::Observer* observer) { |
| 84 | observers_.RemoveObserver(observer); |
| 85 | } |
| 86 | |
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 87 | // static |
| 88 | bool CertLoader::IsCertificateHardwareBacked(const net::X509Certificate* cert) { |
| 89 | if (g_force_hardware_backed_for_test) |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 90 | return true; |
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 91 | PK11SlotInfo* slot = cert->os_cert_handle()->slot; |
| 92 | return slot && PK11_IsHW(slot); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 95 | bool CertLoader::CertificatesLoading() const { |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 96 | return database_ && !certificates_loaded_; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 6084b9b | 2014-04-07 23:54:07 | [diff] [blame] | 99 | // static |
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 100 | void CertLoader::ForceHardwareBackedForTesting() { |
| 101 | g_force_hardware_backed_for_test = true; |
| 102 | } |
| 103 | |
| 104 | // static |
[email protected] | 6084b9b | 2014-04-07 23:54:07 | [diff] [blame] | 105 | // |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 106 | // For background see this discussion on dev-tech-crypto.lists.mozilla.org: |
| 107 | // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX |
| 108 | // |
| 109 | // NOTE: This function relies on the convention that the same PKCS#11 ID |
| 110 | // is shared between a certificate and its associated private and public |
| 111 | // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(), |
| 112 | // but that always returns NULL on Chrome OS for me. |
[email protected] | 5fffe15 | 2014-07-30 19:40:09 | [diff] [blame] | 113 | std::string CertLoader::GetPkcs11IdAndSlotForCert( |
| 114 | const net::X509Certificate& cert, |
| 115 | int* slot_id) { |
| 116 | DCHECK(slot_id); |
| 117 | |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 118 | CERTCertificateStr* cert_handle = cert.os_cert_handle(); |
| 119 | SECKEYPrivateKey *priv_key = |
| 120 | PK11_FindKeyByAnyCert(cert_handle, NULL /* wincx */); |
| 121 | if (!priv_key) |
| 122 | return std::string(); |
| 123 | |
[email protected] | 5fffe15 | 2014-07-30 19:40:09 | [diff] [blame] | 124 | *slot_id = static_cast<int>(PK11_GetSlotID(priv_key->pkcs11Slot)); |
| 125 | |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 126 | // Get the CKA_ID attribute for a key. |
| 127 | SECItem* sec_item = PK11_GetLowLevelKeyIDForPrivateKey(priv_key); |
| 128 | std::string pkcs11_id; |
| 129 | if (sec_item) { |
| 130 | pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); |
| 131 | SECITEM_FreeItem(sec_item, PR_TRUE); |
| 132 | } |
| 133 | SECKEY_DestroyPrivateKey(priv_key); |
| 134 | |
| 135 | return pkcs11_id; |
| 136 | } |
| 137 | |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 138 | void CertLoader::LoadCertificates() { |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 139 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 140 | VLOG(1) << "LoadCertificates: " << certificates_update_running_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 141 | |
| 142 | if (certificates_update_running_) { |
| 143 | certificates_update_required_ = true; |
| 144 | return; |
| 145 | } |
| 146 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 147 | certificates_update_running_ = true; |
[email protected] | 8e80c288 | 2013-05-23 10:43:40 | [diff] [blame] | 148 | certificates_update_required_ = false; |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 149 | |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 150 | database_->ListCerts( |
| 151 | base::Bind(&CertLoader::UpdateCertificates, weak_factory_.GetWeakPtr())); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 154 | void CertLoader::UpdateCertificates( |
| 155 | scoped_ptr<net::CertificateList> cert_list) { |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 156 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 157 | DCHECK(certificates_update_running_); |
| 158 | VLOG(1) << "UpdateCertificates: " << cert_list->size(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 159 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 160 | // Ignore any existing certificates. |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 161 | cert_list_ = cert_list.Pass(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 162 | |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 163 | bool initial_load = !certificates_loaded_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 164 | certificates_loaded_ = true; |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 165 | NotifyCertificatesLoaded(initial_load); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 166 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 167 | certificates_update_running_ = false; |
| 168 | if (certificates_update_required_) |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 169 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void CertLoader::NotifyCertificatesLoaded(bool initial_load) { |
| 173 | FOR_EACH_OBSERVER(Observer, observers_, |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 174 | OnCertificatesLoaded(*cert_list_, initial_load)); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | c157b2e2 | 2013-10-31 01:38:33 | [diff] [blame] | 177 | void CertLoader::OnCACertChanged(const net::X509Certificate* cert) { |
| 178 | // This is triggered when a CA certificate is modified. |
| 179 | VLOG(1) << "OnCACertChanged"; |
| 180 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void CertLoader::OnCertAdded(const net::X509Certificate* cert) { |
[email protected] | c157b2e2 | 2013-10-31 01:38:33 | [diff] [blame] | 184 | // This is triggered when a client certificate is added. |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 185 | VLOG(1) << "OnCertAdded"; |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 186 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 190 | VLOG(1) << "OnCertRemoved"; |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 191 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 194 | } // namespace chromeos |