blob: aecd3a3494495546711e9a0245fd62013b4e014c [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]6e7845ae2013-03-29 21:48:1115#include "net/cert/x509_certificate.h"
[email protected]0fd776c42010-09-29 21:59:1716
[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]007b3f82013-04-09 08:46:4541 std::string(), // 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]007b3f82013-04-09 08:46:4598 rv = ASCIIToUTF16(x509_certificate_model::GetSerialNumberHexified(
99 cert.os_cert_handle(), std::string()));
[email protected]0fd776c42010-09-29 21:59:17100 break;
101 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:08102 if (!cert.valid_expiry().is_null())
103 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:17104 break;
[email protected]0fd776c42010-09-29 21:59:17105 default:
106 NOTREACHED();
107 }
108 return rv;
109}
[email protected]06dc3202010-10-06 21:18:07110
[email protected]88b9db72011-01-13 01:48:43111int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
112 const std::string& data,
[email protected]6a18d072011-06-29 00:25:40113 const string16& password,
114 bool is_extractable) {
[email protected]7fda9a402012-09-10 14:11:07115 int result = cert_db_->ImportFromPKCS12(module, data, password,
116 is_extractable, NULL);
[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,
[email protected]7fda9a402012-09-10 14:11:07124 net::NSSCertDatabase::TrustBits trust_bits,
125 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
126 bool result = cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
[email protected]2feacc342010-10-12 22:52:52127 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,
[email protected]7fda9a402012-09-10 14:11:07134 net::NSSCertDatabase::TrustBits trust_bits,
135 net::NSSCertDatabase::ImportCertFailureList* not_imported) {
136 bool result = cert_db_->ImportServerCert(certificates, trust_bits,
137 not_imported);
[email protected]7a3a9652010-10-13 01:21:13138 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,
[email protected]7fda9a402012-09-10 14:11:07146 net::NSSCertDatabase::TrustBits trust_bits) {
147 return cert_db_->SetCertTrust(cert, type, trust_bits);
[email protected]72a8d0d72010-10-08 00:36:57148}
149
[email protected]06dc3202010-10-06 21:18:07150bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
[email protected]7fda9a402012-09-10 14:11:07151 bool result = cert_db_->DeleteCertAndKey(cert);
[email protected]06dc3202010-10-06 21:18:07152 if (result)
153 Refresh();
154 return result;
155}
[email protected]e0ad0892012-05-22 19:16:59156
157bool CertificateManagerModel::IsHardwareBacked(
158 const net::X509Certificate* cert) const {
159#if defined(OS_CHROMEOS)
160 return crypto::IsTPMTokenReady() &&
161 cert->os_cert_handle()->slot ==
[email protected]7fda9a402012-09-10 14:11:07162 cert_db_->GetPrivateModule()->os_module_handle();
[email protected]e0ad0892012-05-22 19:16:59163#else
164 return false;
165#endif
166}