blob: 96f6f3f466753f2c34edfc9c5299630dc5a6afd0 [file] [log] [blame]
[email protected]e0ad0892012-05-22 19:16:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0fd776c42010-09-29 21:59:172// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4f242962011-05-13 22:25:225#include "chrome/browser/certificate_manager_model.h"
[email protected]0fd776c42010-09-29 21:59:176
[email protected]289838c2011-09-29 22:12:277#include "base/bind.h"
[email protected]0fd776c42010-09-29 21:59:178#include "base/i18n/time_formatting.h"
9#include "base/logging.h"
[email protected]135cb802013-06-09 16:44:2010#include "base/strings/utf_string_conversions.h"
[email protected]3065a1f2014-01-22 08:56:3511#include "chrome/browser/net/nss_context.h"
[email protected]99e5e9522013-12-16 13:05:2712#include "chrome/browser/ui/crypto_module_password_dialog_nss.h"
[email protected]b1c2a5542010-10-08 12:44:4013#include "chrome/common/net/x509_certificate_model.h"
[email protected]3065a1f2014-01-22 08:56:3514#include "content/public/browser/browser_context.h"
15#include "content/public/browser/browser_thread.h"
16#include "content/public/browser/resource_context.h"
17#include "crypto/nss_util.h"
[email protected]e764bea2013-11-20 05:07:0118#include "grit/generated_resources.h"
[email protected]4c4f7cd2011-03-05 02:20:4419#include "net/base/crypto_module.h"
[email protected]06dc3202010-10-06 21:18:0720#include "net/base/net_errors.h"
[email protected]6e7845ae2013-03-29 21:48:1121#include "net/cert/x509_certificate.h"
[email protected]1d77c3e2011-06-08 16:34:4722#include "ui/base/l10n/l10n_util.h"
[email protected]1d77c3e2011-06-08 16:34:4723
[email protected]3065a1f2014-01-22 08:56:3524using content::BrowserThread;
25
26// CertificateManagerModel is created on the UI thread. It needs a
27// NSSCertDatabase handle (and on ChromeOS it needs to get the TPM status) which
28// needs to be done on the IO thread.
29//
30// The initialization flow is roughly:
31//
32// UI thread IO Thread
33//
34// CertificateManagerModel::Create
35// \--------------------------------------v
36// CertificateManagerModel::GetCertDBOnIOThread
37// |
38// GetNSSCertDatabaseForResourceContext
39// |
40// CertificateManagerModel::DidGetCertDBOnIOThread
41// |
42// crypto::IsTPMTokenEnabledForNSS
43// v--------------------------------------/
44// CertificateManagerModel::DidGetCertDBOnUIThread
45// |
46// new CertificateManagerModel
47// |
48// callback
49
50// static
51void CertificateManagerModel::Create(
52 content::BrowserContext* browser_context,
53 CertificateManagerModel::Observer* observer,
54 const CreationCallback& callback) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
56 BrowserThread::PostTask(
57 BrowserThread::IO,
58 FROM_HERE,
59 base::Bind(&CertificateManagerModel::GetCertDBOnIOThread,
60 browser_context->GetResourceContext(),
61 observer,
62 callback));
63}
64
65CertificateManagerModel::CertificateManagerModel(
66 net::NSSCertDatabase* nss_cert_database,
67 bool is_tpm_available,
68 Observer* observer)
69 : cert_db_(nss_cert_database),
70 is_tpm_available_(is_tpm_available),
[email protected]7fda9a402012-09-10 14:11:0771 observer_(observer) {
[email protected]3065a1f2014-01-22 08:56:3572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0fd776c42010-09-29 21:59:1773}
74
75CertificateManagerModel::~CertificateManagerModel() {
76}
77
78void CertificateManagerModel::Refresh() {
[email protected]3f1719b2013-11-14 00:34:5479 DVLOG(1) << "refresh started";
[email protected]4c4f7cd2011-03-05 02:20:4480 net::CryptoModuleList modules;
[email protected]7fda9a402012-09-10 14:11:0781 cert_db_->ListModules(&modules, false);
[email protected]3f1719b2013-11-14 00:34:5482 DVLOG(1) << "refresh waiting for unlocking...";
[email protected]6246ac52012-09-24 01:55:2983 chrome::UnlockSlotsIfNecessary(
[email protected]4c4f7cd2011-03-05 02:20:4484 modules,
[email protected]6246ac52012-09-24 01:55:2985 chrome::kCryptoModulePasswordListCerts,
[email protected]791879c2013-12-17 07:22:4186 net::HostPortPair(), // unused.
[email protected]3f1719b2013-11-14 00:34:5487 NULL, // TODO(mattm): supply parent window.
[email protected]289838c2011-09-29 22:12:2788 base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked,
89 base::Unretained(this)));
[email protected]4c4f7cd2011-03-05 02:20:4490}
91
92void CertificateManagerModel::RefreshSlotsUnlocked() {
[email protected]3f1719b2013-11-14 00:34:5493 DVLOG(1) << "refresh listing certs...";
[email protected]9e818932014-02-06 10:24:1194 // TODO(tbarzic): Use async |ListCerts|.
95 cert_db_->ListCertsSync(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0796 observer_->CertificatesRefreshed();
[email protected]3f1719b2013-11-14 00:34:5497 DVLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1798}
99
100void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
101 net::CertType filter_type,
102 CertificateManagerModel::OrgGroupingMap* map) const {
103 for (net::CertificateList::const_iterator i = cert_list_.begin();
104 i != cert_list_.end(); ++i) {
105 net::X509Certificate* cert = i->get();
[email protected]b1c2a5542010-10-08 12:44:40106 net::CertType type =
107 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:17108 if (type != filter_type)
109 continue;
110
111 std::string org;
112 if (!cert->subject().organization_names.empty())
113 org = cert->subject().organization_names[0];
114 if (org.empty())
115 org = cert->subject().GetDisplayName();
116
117 (*map)[org].push_back(cert);
118 }
119}
120
[email protected]96920152013-12-04 21:00:16121base::string16 CertificateManagerModel::GetColumnText(
[email protected]0fd776c42010-09-29 21:59:17122 const net::X509Certificate& cert,
123 Column column) const {
[email protected]96920152013-12-04 21:00:16124 base::string16 rv;
[email protected]0fd776c42010-09-29 21:59:17125 switch (column) {
126 case COL_SUBJECT_NAME:
[email protected]cc2a2a22013-12-24 23:12:15127 rv = base::UTF8ToUTF16(
[email protected]b1c2a5542010-10-08 12:44:40128 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]1d77c3e2011-06-08 16:34:47129
[email protected]1d77c3e2011-06-08 16:34:47130 // TODO(xiyuan): Put this into a column when we have js tree-table.
[email protected]e0ad0892012-05-22 19:16:59131 if (IsHardwareBacked(&cert)) {
[email protected]1d77c3e2011-06-08 16:34:47132 rv = l10n_util::GetStringFUTF16(
133 IDS_CERT_MANAGER_HARDWARE_BACKED_KEY_FORMAT,
134 rv,
135 l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED));
136 }
[email protected]0fd776c42010-09-29 21:59:17137 break;
138 case COL_CERTIFICATE_STORE:
[email protected]cc2a2a22013-12-24 23:12:15139 rv = base::UTF8ToUTF16(
[email protected]b1c2a5542010-10-08 12:44:40140 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:17141 break;
142 case COL_SERIAL_NUMBER:
[email protected]cc2a2a22013-12-24 23:12:15143 rv = base::ASCIIToUTF16(x509_certificate_model::GetSerialNumberHexified(
[email protected]007b3f82013-04-09 08:46:45144 cert.os_cert_handle(), std::string()));
[email protected]0fd776c42010-09-29 21:59:17145 break;
146 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:08147 if (!cert.valid_expiry().is_null())
148 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:17149 break;
[email protected]0fd776c42010-09-29 21:59:17150 default:
151 NOTREACHED();
152 }
153 return rv;
154}
[email protected]06dc3202010-10-06 21:18:07155
[email protected]88b9db72011-01-13 01:48:43156int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
157 const std::string& data,
[email protected]96920152013-12-04 21:00:16158 const base::string16& password,
[email protected]6a18d072011-06-29 00:25:40159 bool is_extractable) {
[email protected]7fda9a402012-09-10 14:11:07160 int result = cert_db_->ImportFromPKCS12(module, data, password,
161 is_extractable, NULL);
[email protected]06dc3202010-10-06 21:18:07162 if (result == net::OK)
163 Refresh();
164 return result;
165}
166
[email protected]2feacc342010-10-12 22:52:52167bool CertificateManagerModel::ImportCACerts(
168 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07169 net::NSSCertDatabase::TrustBits trust_bits,
170 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
171 bool result = cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
[email protected]2feacc342010-10-12 22:52:52172 if (result && not_imported->size() != certificates.size())
173 Refresh();
174 return result;
[email protected]72a8d0d72010-10-08 00:36:57175}
176
[email protected]7a3a9652010-10-13 01:21:13177bool CertificateManagerModel::ImportServerCert(
178 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07179 net::NSSCertDatabase::TrustBits trust_bits,
180 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
181 bool result = cert_db_->ImportServerCert(certificates, trust_bits,
182 not_imported);
[email protected]7a3a9652010-10-13 01:21:13183 if (result && not_imported->size() != certificates.size())
184 Refresh();
185 return result;
186}
187
[email protected]c79b784d12011-09-20 18:44:54188bool CertificateManagerModel::SetCertTrust(
189 const net::X509Certificate* cert,
190 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07191 net::NSSCertDatabase::TrustBits trust_bits) {
192 return cert_db_->SetCertTrust(cert, type, trust_bits);
[email protected]72a8d0d72010-10-08 00:36:57193}
194
[email protected]06dc3202010-10-06 21:18:07195bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
[email protected]7fda9a402012-09-10 14:11:07196 bool result = cert_db_->DeleteCertAndKey(cert);
[email protected]06dc3202010-10-06 21:18:07197 if (result)
198 Refresh();
199 return result;
200}
[email protected]e0ad0892012-05-22 19:16:59201
202bool CertificateManagerModel::IsHardwareBacked(
203 const net::X509Certificate* cert) const {
[email protected]e764bea2013-11-20 05:07:01204 return cert_db_->IsHardwareBacked(cert);
[email protected]e0ad0892012-05-22 19:16:59205}
[email protected]3065a1f2014-01-22 08:56:35206
207// static
208void CertificateManagerModel::DidGetCertDBOnUIThread(
209 net::NSSCertDatabase* cert_db,
210 bool is_tpm_available,
211 CertificateManagerModel::Observer* observer,
212 const CreationCallback& callback) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214
215 scoped_ptr<CertificateManagerModel> model(
216 new CertificateManagerModel(cert_db, is_tpm_available, observer));
217 callback.Run(model.Pass());
218}
219
220// static
221void CertificateManagerModel::DidGetCertDBOnIOThread(
222 CertificateManagerModel::Observer* observer,
223 const CreationCallback& callback,
224 net::NSSCertDatabase* cert_db) {
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
226
227 bool is_tpm_available = false;
228#if defined(OS_CHROMEOS)
229 is_tpm_available = crypto::IsTPMTokenEnabledForNSS();
230#endif
231 BrowserThread::PostTask(
232 BrowserThread::UI,
233 FROM_HERE,
234 base::Bind(&CertificateManagerModel::DidGetCertDBOnUIThread,
235 cert_db,
236 is_tpm_available,
237 observer,
238 callback));
239}
240
241// static
242void CertificateManagerModel::GetCertDBOnIOThread(
243 content::ResourceContext* context,
244 CertificateManagerModel::Observer* observer,
245 const CreationCallback& callback) {
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
247 net::NSSCertDatabase* cert_db = GetNSSCertDatabaseForResourceContext(
248 context,
249 base::Bind(&CertificateManagerModel::DidGetCertDBOnIOThread,
250 observer,
251 callback));
252 if (cert_db)
253 DidGetCertDBOnIOThread(observer, callback, cert_db);
254}