blob: e722ad8b0e78fc120320f611e48321134cde5a23 [file] [log] [blame]
[email protected]1d77c3e2011-06-08 16:34:471// Copyright (c) 2011 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"
10#include "base/utf_string_conversions.h"
[email protected]4c4f7cd2011-03-05 02:20:4411#include "chrome/browser/ui/crypto_module_password_dialog.h"
[email protected]b1c2a5542010-10-08 12:44:4012#include "chrome/common/net/x509_certificate_model.h"
[email protected]4c4f7cd2011-03-05 02:20:4413#include "net/base/crypto_module.h"
[email protected]06dc3202010-10-06 21:18:0714#include "net/base/net_errors.h"
[email protected]0fd776c42010-09-29 21:59:1715#include "net/base/x509_certificate.h"
16
[email protected]1d77c3e2011-06-08 16:34:4717#if defined(OS_CHROMEOS)
18#include <cert.h>
19
20#include "crypto/nss_util.h"
21#include "grit/generated_resources.h"
22#include "ui/base/l10n/l10n_util.h"
23#endif
24
[email protected]06dc3202010-10-06 21:18:0725CertificateManagerModel::CertificateManagerModel(Observer* observer)
26 : observer_(observer) {
[email protected]0fd776c42010-09-29 21:59:1727}
28
29CertificateManagerModel::~CertificateManagerModel() {
30}
31
32void CertificateManagerModel::Refresh() {
[email protected]06dc3202010-10-06 21:18:0733 VLOG(1) << "refresh started";
[email protected]4c4f7cd2011-03-05 02:20:4434 net::CryptoModuleList modules;
35 cert_db_.ListModules(&modules, false);
36 VLOG(1) << "refresh waiting for unlocking...";
37 browser::UnlockSlotsIfNecessary(
38 modules,
39 browser::kCryptoModulePasswordListCerts,
40 "", // unused.
[email protected]289838c2011-09-29 22:12:2741 base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked,
42 base::Unretained(this)));
[email protected]4c4f7cd2011-03-05 02:20:4443}
44
45void CertificateManagerModel::RefreshSlotsUnlocked() {
46 VLOG(1) << "refresh listing certs...";
[email protected]0fd776c42010-09-29 21:59:1747 cert_db_.ListCerts(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0748 observer_->CertificatesRefreshed();
49 VLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1750}
51
52void 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();
[email protected]b1c2a5542010-10-08 12:44:4058 net::CertType type =
59 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:1760 if (type != filter_type)
61 continue;
62
63 std::string org;
64 if (!cert->subject().organization_names.empty())
65 org = cert->subject().organization_names[0];
66 if (org.empty())
67 org = cert->subject().GetDisplayName();
68
69 (*map)[org].push_back(cert);
70 }
71}
72
73string16 CertificateManagerModel::GetColumnText(
74 const net::X509Certificate& cert,
75 Column column) const {
76 string16 rv;
77 switch (column) {
78 case COL_SUBJECT_NAME:
[email protected]b1c2a5542010-10-08 12:44:4079 rv = UTF8ToUTF16(
80 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]1d77c3e2011-06-08 16:34:4781
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,
89 rv,
90 l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED));
91 }
92#endif
[email protected]0fd776c42010-09-29 21:59:1793 break;
94 case COL_CERTIFICATE_STORE:
[email protected]b1c2a5542010-10-08 12:44:4095 rv = UTF8ToUTF16(
96 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1797 break;
98 case COL_SERIAL_NUMBER:
[email protected]b1c2a5542010-10-08 12:44:4099 rv = ASCIIToUTF16(
100 x509_certificate_model::GetSerialNumberHexified(
101 cert.os_cert_handle(), ""));
[email protected]0fd776c42010-09-29 21:59:17102 break;
103 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:08104 if (!cert.valid_expiry().is_null())
105 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:17106 break;
[email protected]0fd776c42010-09-29 21:59:17107 default:
108 NOTREACHED();
109 }
110 return rv;
111}
[email protected]06dc3202010-10-06 21:18:07112
[email protected]88b9db72011-01-13 01:48:43113int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
114 const std::string& data,
[email protected]6a18d072011-06-29 00:25:40115 const string16& password,
116 bool is_extractable) {
117 int result = cert_db_.ImportFromPKCS12(module, data, password,
118 is_extractable);
[email protected]06dc3202010-10-06 21:18:07119 if (result == net::OK)
120 Refresh();
121 return result;
122}
123
[email protected]2feacc342010-10-12 22:52:52124bool CertificateManagerModel::ImportCACerts(
125 const net::CertificateList& certificates,
[email protected]c79b784d12011-09-20 18:44:54126 net::CertDatabase::TrustBits trust_bits,
[email protected]2feacc342010-10-12 22:52:52127 net::CertDatabase::ImportCertFailureList* not_imported) {
128 bool result = cert_db_.ImportCACerts(certificates, trust_bits, not_imported);
129 if (result && not_imported->size() != certificates.size())
130 Refresh();
131 return result;
[email protected]72a8d0d72010-10-08 00:36:57132}
133
[email protected]7a3a9652010-10-13 01:21:13134bool 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())
139 Refresh();
140 return result;
141}
142
[email protected]c79b784d12011-09-20 18:44:54143bool CertificateManagerModel::SetCertTrust(
144 const net::X509Certificate* cert,
145 net::CertType type,
146 net::CertDatabase::TrustBits trust_bits) {
[email protected]72a8d0d72010-10-08 00:36:57147 return cert_db_.SetCertTrust(cert, type, trust_bits);
148}
149
[email protected]06dc3202010-10-06 21:18:07150bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
151 bool result = cert_db_.DeleteCertAndKey(cert);
152 if (result)
153 Refresh();
154 return result;
155}