blob: 22ccd85f42c933050ba4c21e3b47a8ab5d847ffa [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]2b623242011-05-10 00:37:407#include "base/callback_old.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.
41 NewCallback(this,
42 &CertificateManagerModel::RefreshSlotsUnlocked));
43}
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]06dc3202010-10-06 21:18:07115 const string16& password) {
[email protected]88b9db72011-01-13 01:48:43116 int result = cert_db_.ImportFromPKCS12(module, data, password);
[email protected]06dc3202010-10-06 21:18:07117 if (result == net::OK)
118 Refresh();
119 return result;
120}
121
[email protected]2feacc342010-10-12 22:52:52122bool CertificateManagerModel::ImportCACerts(
123 const net::CertificateList& certificates,
124 unsigned int trust_bits,
125 net::CertDatabase::ImportCertFailureList* not_imported) {
126 bool result = cert_db_.ImportCACerts(certificates, trust_bits, not_imported);
127 if (result && not_imported->size() != certificates.size())
128 Refresh();
129 return result;
[email protected]72a8d0d72010-10-08 00:36:57130}
131
[email protected]7a3a9652010-10-13 01:21:13132bool CertificateManagerModel::ImportServerCert(
133 const net::CertificateList& certificates,
134 net::CertDatabase::ImportCertFailureList* not_imported) {
135 bool result = cert_db_.ImportServerCert(certificates, not_imported);
136 if (result && not_imported->size() != certificates.size())
137 Refresh();
138 return result;
139}
140
[email protected]72a8d0d72010-10-08 00:36:57141bool CertificateManagerModel::SetCertTrust(const net::X509Certificate* cert,
142 net::CertType type,
143 unsigned int trust_bits) {
144 return cert_db_.SetCertTrust(cert, type, trust_bits);
145}
146
[email protected]06dc3202010-10-06 21:18:07147bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
148 bool result = cert_db_.DeleteCertAndKey(cert);
149 if (result)
150 Refresh();
151 return result;
152}