blob: 4832cdb47480bd509f6bf412136061d8a52e9827 [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]99e5e9522013-12-16 13:05:2711#include "chrome/browser/ui/crypto_module_password_dialog_nss.h"
[email protected]b1c2a5542010-10-08 12:44:4012#include "chrome/common/net/x509_certificate_model.h"
[email protected]e764bea2013-11-20 05:07:0113#include "grit/generated_resources.h"
[email protected]4c4f7cd2011-03-05 02:20:4414#include "net/base/crypto_module.h"
[email protected]06dc3202010-10-06 21:18:0715#include "net/base/net_errors.h"
[email protected]6e7845ae2013-03-29 21:48:1116#include "net/cert/x509_certificate.h"
[email protected]1d77c3e2011-06-08 16:34:4717#include "ui/base/l10n/l10n_util.h"
[email protected]1d77c3e2011-06-08 16:34:4718
[email protected]06dc3202010-10-06 21:18:0719CertificateManagerModel::CertificateManagerModel(Observer* observer)
[email protected]7fda9a402012-09-10 14:11:0720 : cert_db_(net::NSSCertDatabase::GetInstance()),
21 observer_(observer) {
[email protected]0fd776c42010-09-29 21:59:1722}
23
24CertificateManagerModel::~CertificateManagerModel() {
25}
26
27void CertificateManagerModel::Refresh() {
[email protected]3f1719b2013-11-14 00:34:5428 DVLOG(1) << "refresh started";
[email protected]4c4f7cd2011-03-05 02:20:4429 net::CryptoModuleList modules;
[email protected]7fda9a402012-09-10 14:11:0730 cert_db_->ListModules(&modules, false);
[email protected]3f1719b2013-11-14 00:34:5431 DVLOG(1) << "refresh waiting for unlocking...";
[email protected]6246ac52012-09-24 01:55:2932 chrome::UnlockSlotsIfNecessary(
[email protected]4c4f7cd2011-03-05 02:20:4433 modules,
[email protected]6246ac52012-09-24 01:55:2934 chrome::kCryptoModulePasswordListCerts,
[email protected]791879c2013-12-17 07:22:4135 net::HostPortPair(), // unused.
[email protected]3f1719b2013-11-14 00:34:5436 NULL, // TODO(mattm): supply parent window.
[email protected]289838c2011-09-29 22:12:2737 base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked,
38 base::Unretained(this)));
[email protected]4c4f7cd2011-03-05 02:20:4439}
40
41void CertificateManagerModel::RefreshSlotsUnlocked() {
[email protected]3f1719b2013-11-14 00:34:5442 DVLOG(1) << "refresh listing certs...";
[email protected]7fda9a402012-09-10 14:11:0743 cert_db_->ListCerts(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0744 observer_->CertificatesRefreshed();
[email protected]3f1719b2013-11-14 00:34:5445 DVLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1746}
47
48void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
49 net::CertType filter_type,
50 CertificateManagerModel::OrgGroupingMap* map) const {
51 for (net::CertificateList::const_iterator i = cert_list_.begin();
52 i != cert_list_.end(); ++i) {
53 net::X509Certificate* cert = i->get();
[email protected]b1c2a5542010-10-08 12:44:4054 net::CertType type =
55 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:1756 if (type != filter_type)
57 continue;
58
59 std::string org;
60 if (!cert->subject().organization_names.empty())
61 org = cert->subject().organization_names[0];
62 if (org.empty())
63 org = cert->subject().GetDisplayName();
64
65 (*map)[org].push_back(cert);
66 }
67}
68
[email protected]96920152013-12-04 21:00:1669base::string16 CertificateManagerModel::GetColumnText(
[email protected]0fd776c42010-09-29 21:59:1770 const net::X509Certificate& cert,
71 Column column) const {
[email protected]96920152013-12-04 21:00:1672 base::string16 rv;
[email protected]0fd776c42010-09-29 21:59:1773 switch (column) {
74 case COL_SUBJECT_NAME:
[email protected]b1c2a5542010-10-08 12:44:4075 rv = UTF8ToUTF16(
76 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]1d77c3e2011-06-08 16:34:4777
[email protected]1d77c3e2011-06-08 16:34:4778 // TODO(xiyuan): Put this into a column when we have js tree-table.
[email protected]e0ad0892012-05-22 19:16:5979 if (IsHardwareBacked(&cert)) {
[email protected]1d77c3e2011-06-08 16:34:4780 rv = l10n_util::GetStringFUTF16(
81 IDS_CERT_MANAGER_HARDWARE_BACKED_KEY_FORMAT,
82 rv,
83 l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED));
84 }
[email protected]0fd776c42010-09-29 21:59:1785 break;
86 case COL_CERTIFICATE_STORE:
[email protected]b1c2a5542010-10-08 12:44:4087 rv = UTF8ToUTF16(
88 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1789 break;
90 case COL_SERIAL_NUMBER:
[email protected]007b3f82013-04-09 08:46:4591 rv = ASCIIToUTF16(x509_certificate_model::GetSerialNumberHexified(
92 cert.os_cert_handle(), std::string()));
[email protected]0fd776c42010-09-29 21:59:1793 break;
94 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:0895 if (!cert.valid_expiry().is_null())
96 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:1797 break;
[email protected]0fd776c42010-09-29 21:59:1798 default:
99 NOTREACHED();
100 }
101 return rv;
102}
[email protected]06dc3202010-10-06 21:18:07103
[email protected]88b9db72011-01-13 01:48:43104int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
105 const std::string& data,
[email protected]96920152013-12-04 21:00:16106 const base::string16& password,
[email protected]6a18d072011-06-29 00:25:40107 bool is_extractable) {
[email protected]7fda9a402012-09-10 14:11:07108 int result = cert_db_->ImportFromPKCS12(module, data, password,
109 is_extractable, NULL);
[email protected]06dc3202010-10-06 21:18:07110 if (result == net::OK)
111 Refresh();
112 return result;
113}
114
[email protected]2feacc342010-10-12 22:52:52115bool CertificateManagerModel::ImportCACerts(
116 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07117 net::NSSCertDatabase::TrustBits trust_bits,
118 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
119 bool result = cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
[email protected]2feacc342010-10-12 22:52:52120 if (result && not_imported->size() != certificates.size())
121 Refresh();
122 return result;
[email protected]72a8d0d72010-10-08 00:36:57123}
124
[email protected]7a3a9652010-10-13 01:21:13125bool CertificateManagerModel::ImportServerCert(
126 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07127 net::NSSCertDatabase::TrustBits trust_bits,
128 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
129 bool result = cert_db_->ImportServerCert(certificates, trust_bits,
130 not_imported);
[email protected]7a3a9652010-10-13 01:21:13131 if (result && not_imported->size() != certificates.size())
132 Refresh();
133 return result;
134}
135
[email protected]c79b784d12011-09-20 18:44:54136bool CertificateManagerModel::SetCertTrust(
137 const net::X509Certificate* cert,
138 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07139 net::NSSCertDatabase::TrustBits trust_bits) {
140 return cert_db_->SetCertTrust(cert, type, trust_bits);
[email protected]72a8d0d72010-10-08 00:36:57141}
142
[email protected]06dc3202010-10-06 21:18:07143bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
[email protected]7fda9a402012-09-10 14:11:07144 bool result = cert_db_->DeleteCertAndKey(cert);
[email protected]06dc3202010-10-06 21:18:07145 if (result)
146 Refresh();
147 return result;
148}
[email protected]e0ad0892012-05-22 19:16:59149
150bool CertificateManagerModel::IsHardwareBacked(
151 const net::X509Certificate* cert) const {
[email protected]e764bea2013-11-20 05:07:01152 return cert_db_->IsHardwareBacked(cert);
[email protected]e0ad0892012-05-22 19:16:59153}