Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chromeos / cert_loader.cc
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
5 #include "chromeos/cert_loader.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/task_runner_util.h"
14 #include "base/threading/worker_pool.h"
15 #include "crypto/nss_util.h"
16 #include "crypto/scoped_nss_types.h"
17 #include "net/cert/nss_cert_database.h"
18 #include "net/cert/nss_cert_database_chromeos.h"
19 #include "net/cert/x509_certificate.h"
20
21 namespace chromeos {
22
23 static CertLoader* g_cert_loader = NULL;
24
25 // static
26 void CertLoader::Initialize() {
27   CHECK(!g_cert_loader);
28   g_cert_loader = new CertLoader();
29 }
30
31 // static
32 void CertLoader::Shutdown() {
33   CHECK(g_cert_loader);
34   delete g_cert_loader;
35   g_cert_loader = NULL;
36 }
37
38 // static
39 CertLoader* CertLoader::Get() {
40   CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()";
41   return g_cert_loader;
42 }
43
44 // static
45 bool CertLoader::IsInitialized() {
46   return g_cert_loader;
47 }
48
49 CertLoader::CertLoader()
50     : certificates_loaded_(false),
51       certificates_update_required_(false),
52       certificates_update_running_(false),
53       database_(NULL),
54       force_hardware_backed_for_test_(false),
55       cert_list_(new net::CertificateList),
56       weak_factory_(this) {
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();
77 }
78
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
87 int CertLoader::TPMTokenSlotID() const {
88   if (!database_)
89     return -1;
90   crypto::ScopedPK11Slot slot(database_->GetPrivateSlot());
91   if (!slot)
92     return -1;
93   return static_cast<int>(PK11_GetSlotID(slot.get()));
94 }
95
96 bool CertLoader::IsHardwareBacked() const {
97   if (force_hardware_backed_for_test_)
98     return true;
99   if (!database_)
100     return false;
101   crypto::ScopedPK11Slot slot(database_->GetPrivateSlot());
102   if (!slot)
103     return false;
104   return PK11_IsHW(slot.get());
105 }
106
107 bool CertLoader::IsCertificateHardwareBacked(
108     const net::X509Certificate* cert) const {
109   if (!database_)
110     return false;
111   return database_->IsHardwareBacked(cert);
112 }
113
114 bool CertLoader::CertificatesLoading() const {
115   return database_ && !certificates_loaded_;
116 }
117
118 // This is copied from chrome/common/net/x509_certificate_model_nss.cc.
119 // For background see this discussion on dev-tech-crypto.lists.mozilla.org:
120 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX
121 //
122 // NOTE: This function relies on the convention that the same PKCS#11 ID
123 // is shared between a certificate and its associated private and public
124 // keys.  I tried to implement this with PK11_GetLowLevelKeyIDForCert(),
125 // but that always returns NULL on Chrome OS for me.
126
127 // static
128 std::string CertLoader::GetPkcs11IdForCert(const net::X509Certificate& cert) {
129   CERTCertificateStr* cert_handle = cert.os_cert_handle();
130   SECKEYPrivateKey *priv_key =
131       PK11_FindKeyByAnyCert(cert_handle, NULL /* wincx */);
132   if (!priv_key)
133     return std::string();
134
135   // Get the CKA_ID attribute for a key.
136   SECItem* sec_item = PK11_GetLowLevelKeyIDForPrivateKey(priv_key);
137   std::string pkcs11_id;
138   if (sec_item) {
139     pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
140     SECITEM_FreeItem(sec_item, PR_TRUE);
141   }
142   SECKEY_DestroyPrivateKey(priv_key);
143
144   return pkcs11_id;
145 }
146
147 void CertLoader::LoadCertificates() {
148   CHECK(thread_checker_.CalledOnValidThread());
149   VLOG(1) << "LoadCertificates: " << certificates_update_running_;
150
151   if (certificates_update_running_) {
152     certificates_update_required_ = true;
153     return;
154   }
155
156   certificates_update_running_ = true;
157   certificates_update_required_ = false;
158
159   database_->ListCerts(
160       base::Bind(&CertLoader::UpdateCertificates, weak_factory_.GetWeakPtr()));
161 }
162
163 void CertLoader::UpdateCertificates(
164     scoped_ptr<net::CertificateList> cert_list) {
165   CHECK(thread_checker_.CalledOnValidThread());
166   DCHECK(certificates_update_running_);
167   VLOG(1) << "UpdateCertificates: " << cert_list->size();
168
169   // Ignore any existing certificates.
170   cert_list_ = cert_list.Pass();
171
172   bool initial_load = !certificates_loaded_;
173   certificates_loaded_ = true;
174   NotifyCertificatesLoaded(initial_load);
175
176   certificates_update_running_ = false;
177   if (certificates_update_required_)
178     LoadCertificates();
179 }
180
181 void CertLoader::NotifyCertificatesLoaded(bool initial_load) {
182   FOR_EACH_OBSERVER(Observer, observers_,
183                     OnCertificatesLoaded(*cert_list_, initial_load));
184 }
185
186 void CertLoader::OnCACertChanged(const net::X509Certificate* cert) {
187   // This is triggered when a CA certificate is modified.
188   VLOG(1) << "OnCACertChanged";
189   LoadCertificates();
190 }
191
192 void CertLoader::OnCertAdded(const net::X509Certificate* cert) {
193   // This is triggered when a client certificate is added.
194   VLOG(1) << "OnCertAdded";
195   LoadCertificates();
196 }
197
198 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
199   VLOG(1) << "OnCertRemoved";
200   LoadCertificates();
201 }
202
203 }  // namespace chromeos