1 // Copyright (c) 2011 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 "chrome/browser/certificate_manager_model.h"
8 #include "base/i18n/time_formatting.h"
9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/ui/crypto_module_password_dialog.h"
12 #include "chrome/common/net/x509_certificate_model.h"
13 #include "net/base/crypto_module.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/x509_certificate.h"
17 #if defined(OS_CHROMEOS)
20 #include "crypto/nss_util.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
25 CertificateManagerModel::CertificateManagerModel(Observer
* observer
)
26 : observer_(observer
) {
29 CertificateManagerModel::~CertificateManagerModel() {
32 void CertificateManagerModel::Refresh() {
33 VLOG(1) << "refresh started";
34 net::CryptoModuleList modules
;
35 cert_db_
.ListModules(&modules
, false);
36 VLOG(1) << "refresh waiting for unlocking...";
37 browser::UnlockSlotsIfNecessary(
39 browser::kCryptoModulePasswordListCerts
,
41 base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked
,
42 base::Unretained(this)));
45 void CertificateManagerModel::RefreshSlotsUnlocked() {
46 VLOG(1) << "refresh listing certs...";
47 cert_db_
.ListCerts(&cert_list_
);
48 observer_
->CertificatesRefreshed();
49 VLOG(1) << "refresh finished";
52 void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
53 net::CertType filter_type
,
54 CertificateManagerModel::OrgGroupingMap
* map
) const {
55 for (net::CertificateList::const_iterator i
= cert_list_
.begin();
56 i
!= cert_list_
.end(); ++i
) {
57 net::X509Certificate
* cert
= i
->get();
59 x509_certificate_model::GetType(cert
->os_cert_handle());
60 if (type
!= filter_type
)
64 if (!cert
->subject().organization_names
.empty())
65 org
= cert
->subject().organization_names
[0];
67 org
= cert
->subject().GetDisplayName();
69 (*map
)[org
].push_back(cert
);
73 string16
CertificateManagerModel::GetColumnText(
74 const net::X509Certificate
& cert
,
75 Column column
) const {
78 case COL_SUBJECT_NAME
:
80 x509_certificate_model::GetCertNameOrNickname(cert
.os_cert_handle()));
82 #if defined(OS_CHROMEOS)
83 // TODO(xiyuan): Put this into a column when we have js tree-table.
84 if (crypto::IsTPMTokenReady() &&
85 cert
.os_cert_handle()->slot
==
86 cert_db().GetPrivateModule()->os_module_handle()) {
87 rv
= l10n_util::GetStringFUTF16(
88 IDS_CERT_MANAGER_HARDWARE_BACKED_KEY_FORMAT
,
90 l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED
));
94 case COL_CERTIFICATE_STORE
:
96 x509_certificate_model::GetTokenName(cert
.os_cert_handle()));
98 case COL_SERIAL_NUMBER
:
100 x509_certificate_model::GetSerialNumberHexified(
101 cert
.os_cert_handle(), ""));
104 if (!cert
.valid_expiry().is_null())
105 rv
= base::TimeFormatShortDateNumeric(cert
.valid_expiry());
113 int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule
* module
,
114 const std::string
& data
,
115 const string16
& password
,
116 bool is_extractable
) {
117 int result
= cert_db_
.ImportFromPKCS12(module
, data
, password
,
118 is_extractable
, NULL
);
119 if (result
== net::OK
)
124 bool CertificateManagerModel::ImportCACerts(
125 const net::CertificateList
& certificates
,
126 net::CertDatabase::TrustBits trust_bits
,
127 net::CertDatabase::ImportCertFailureList
* not_imported
) {
128 bool result
= cert_db_
.ImportCACerts(certificates
, trust_bits
, not_imported
);
129 if (result
&& not_imported
->size() != certificates
.size())
134 bool CertificateManagerModel::ImportServerCert(
135 const net::CertificateList
& certificates
,
136 net::CertDatabase::ImportCertFailureList
* not_imported
) {
137 bool result
= cert_db_
.ImportServerCert(certificates
, not_imported
);
138 if (result
&& not_imported
->size() != certificates
.size())
143 bool CertificateManagerModel::SetCertTrust(
144 const net::X509Certificate
* cert
,
146 net::CertDatabase::TrustBits trust_bits
) {
147 return cert_db_
.SetCertTrust(cert
, type
, trust_bits
);
150 bool CertificateManagerModel::Delete(net::X509Certificate
* cert
) {
151 bool result
= cert_db_
.DeleteCertAndKey(cert
);