blob: 41768659a6ffb383397476e4168c0622feda2787 [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"
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)
[email protected]7fda9a402012-09-10 14:11:0726 : cert_db_(net::NSSCertDatabase::GetInstance()),
27 observer_(observer) {
[email protected]0fd776c42010-09-29 21:59:1728}
29
30CertificateManagerModel::~CertificateManagerModel() {
31}
32
33void CertificateManagerModel::Refresh() {
[email protected]06dc3202010-10-06 21:18:0734 VLOG(1) << "refresh started";
[email protected]4c4f7cd2011-03-05 02:20:4435 net::CryptoModuleList modules;
[email protected]7fda9a402012-09-10 14:11:0736 cert_db_->ListModules(&modules, false);
[email protected]4c4f7cd2011-03-05 02:20:4437 VLOG(1) << "refresh waiting for unlocking...";
[email protected]6246ac52012-09-24 01:55:2938 chrome::UnlockSlotsIfNecessary(
[email protected]4c4f7cd2011-03-05 02:20:4439 modules,
[email protected]6246ac52012-09-24 01:55:2940 chrome::kCryptoModulePasswordListCerts,
[email protected]4c4f7cd2011-03-05 02:20:4441 "", // unused.
[email protected]289838c2011-09-29 22:12:2742 base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked,
43 base::Unretained(this)));
[email protected]4c4f7cd2011-03-05 02:20:4444}
45
46void CertificateManagerModel::RefreshSlotsUnlocked() {
47 VLOG(1) << "refresh listing certs...";
[email protected]7fda9a402012-09-10 14:11:0748 cert_db_->ListCerts(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0749 observer_->CertificatesRefreshed();
50 VLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1751}
52
53void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
54 net::CertType filter_type,
55 CertificateManagerModel::OrgGroupingMap* map) const {
56 for (net::CertificateList::const_iterator i = cert_list_.begin();
57 i != cert_list_.end(); ++i) {
58 net::X509Certificate* cert = i->get();
[email protected]b1c2a5542010-10-08 12:44:4059 net::CertType type =
60 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:1761 if (type != filter_type)
62 continue;
63
64 std::string org;
65 if (!cert->subject().organization_names.empty())
66 org = cert->subject().organization_names[0];
67 if (org.empty())
68 org = cert->subject().GetDisplayName();
69
70 (*map)[org].push_back(cert);
71 }
72}
73
74string16 CertificateManagerModel::GetColumnText(
75 const net::X509Certificate& cert,
76 Column column) const {
77 string16 rv;
78 switch (column) {
79 case COL_SUBJECT_NAME:
[email protected]b1c2a5542010-10-08 12:44:4080 rv = UTF8ToUTF16(
81 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]1d77c3e2011-06-08 16:34:4782
83#if defined(OS_CHROMEOS)
84 // TODO(xiyuan): Put this into a column when we have js tree-table.
[email protected]e0ad0892012-05-22 19:16:5985 if (IsHardwareBacked(&cert)) {
[email protected]1d77c3e2011-06-08 16:34:4786 rv = l10n_util::GetStringFUTF16(
87 IDS_CERT_MANAGER_HARDWARE_BACKED_KEY_FORMAT,
88 rv,
89 l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED));
90 }
91#endif
[email protected]0fd776c42010-09-29 21:59:1792 break;
93 case COL_CERTIFICATE_STORE:
[email protected]b1c2a5542010-10-08 12:44:4094 rv = UTF8ToUTF16(
95 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1796 break;
97 case COL_SERIAL_NUMBER:
[email protected]b1c2a5542010-10-08 12:44:4098 rv = ASCIIToUTF16(
99 x509_certificate_model::GetSerialNumberHexified(
100 cert.os_cert_handle(), ""));
[email protected]0fd776c42010-09-29 21:59:17101 break;
102 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:08103 if (!cert.valid_expiry().is_null())
104 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:17105 break;
[email protected]0fd776c42010-09-29 21:59:17106 default:
107 NOTREACHED();
108 }
109 return rv;
110}
[email protected]06dc3202010-10-06 21:18:07111
[email protected]88b9db72011-01-13 01:48:43112int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
113 const std::string& data,
[email protected]6a18d072011-06-29 00:25:40114 const string16& password,
115 bool is_extractable) {
[email protected]7fda9a402012-09-10 14:11:07116 int result = cert_db_->ImportFromPKCS12(module, data, password,
117 is_extractable, NULL);
[email protected]06dc3202010-10-06 21:18:07118 if (result == net::OK)
119 Refresh();
120 return result;
121}
122
[email protected]2feacc342010-10-12 22:52:52123bool CertificateManagerModel::ImportCACerts(
124 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07125 net::NSSCertDatabase::TrustBits trust_bits,
126 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
127 bool result = cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
[email protected]2feacc342010-10-12 22:52:52128 if (result && not_imported->size() != certificates.size())
129 Refresh();
130 return result;
[email protected]72a8d0d72010-10-08 00:36:57131}
132
[email protected]7a3a9652010-10-13 01:21:13133bool CertificateManagerModel::ImportServerCert(
134 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07135 net::NSSCertDatabase::TrustBits trust_bits,
136 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
137 bool result = cert_db_->ImportServerCert(certificates, trust_bits,
138 not_imported);
[email protected]7a3a9652010-10-13 01:21:13139 if (result && not_imported->size() != certificates.size())
140 Refresh();
141 return result;
142}
143
[email protected]c79b784d12011-09-20 18:44:54144bool CertificateManagerModel::SetCertTrust(
145 const net::X509Certificate* cert,
146 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07147 net::NSSCertDatabase::TrustBits trust_bits) {
148 return cert_db_->SetCertTrust(cert, type, trust_bits);
[email protected]72a8d0d72010-10-08 00:36:57149}
150
[email protected]06dc3202010-10-06 21:18:07151bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
[email protected]7fda9a402012-09-10 14:11:07152 bool result = cert_db_->DeleteCertAndKey(cert);
[email protected]06dc3202010-10-06 21:18:07153 if (result)
154 Refresh();
155 return result;
156}
[email protected]e0ad0892012-05-22 19:16:59157
158bool CertificateManagerModel::IsHardwareBacked(
159 const net::X509Certificate* cert) const {
160#if defined(OS_CHROMEOS)
161 return crypto::IsTPMTokenReady() &&
162 cert->os_cert_handle()->slot ==
[email protected]7fda9a402012-09-10 14:11:07163 cert_db_->GetPrivateModule()->os_module_handle();
[email protected]e0ad0892012-05-22 19:16:59164#else
165 return false;
166#endif
167}