blob: e7b1daf73cb5fb30038479b5dee5a83bc5bf3d89 [file] [log] [blame]
[email protected]0fd776c42010-09-29 21:59:171// Copyright (c) 2010 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.
4
[email protected]df8e899b2011-02-22 22:58:225#include "content/browser/certificate_manager_model.h"
[email protected]0fd776c42010-09-29 21:59:176
[email protected]0fd776c42010-09-29 21:59:177#include "base/i18n/time_formatting.h"
8#include "base/logging.h"
9#include "base/utf_string_conversions.h"
[email protected]4c4f7cd2011-03-05 02:20:4410#include "chrome/browser/ui/crypto_module_password_dialog.h"
[email protected]b1c2a5542010-10-08 12:44:4011#include "chrome/common/net/x509_certificate_model.h"
[email protected]4c4f7cd2011-03-05 02:20:4412#include "net/base/crypto_module.h"
[email protected]06dc3202010-10-06 21:18:0713#include "net/base/net_errors.h"
[email protected]0fd776c42010-09-29 21:59:1714#include "net/base/x509_certificate.h"
15
[email protected]06dc3202010-10-06 21:18:0716CertificateManagerModel::CertificateManagerModel(Observer* observer)
17 : observer_(observer) {
[email protected]0fd776c42010-09-29 21:59:1718}
19
20CertificateManagerModel::~CertificateManagerModel() {
21}
22
23void CertificateManagerModel::Refresh() {
[email protected]06dc3202010-10-06 21:18:0724 VLOG(1) << "refresh started";
[email protected]4c4f7cd2011-03-05 02:20:4425 net::CryptoModuleList modules;
26 cert_db_.ListModules(&modules, false);
27 VLOG(1) << "refresh waiting for unlocking...";
28 browser::UnlockSlotsIfNecessary(
29 modules,
30 browser::kCryptoModulePasswordListCerts,
31 "", // unused.
32 NewCallback(this,
33 &CertificateManagerModel::RefreshSlotsUnlocked));
34}
35
36void CertificateManagerModel::RefreshSlotsUnlocked() {
37 VLOG(1) << "refresh listing certs...";
[email protected]0fd776c42010-09-29 21:59:1738 cert_db_.ListCerts(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0739 observer_->CertificatesRefreshed();
40 VLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1741}
42
43void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
44 net::CertType filter_type,
45 CertificateManagerModel::OrgGroupingMap* map) const {
46 for (net::CertificateList::const_iterator i = cert_list_.begin();
47 i != cert_list_.end(); ++i) {
48 net::X509Certificate* cert = i->get();
[email protected]b1c2a5542010-10-08 12:44:4049 net::CertType type =
50 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:1751 if (type != filter_type)
52 continue;
53
54 std::string org;
55 if (!cert->subject().organization_names.empty())
56 org = cert->subject().organization_names[0];
57 if (org.empty())
58 org = cert->subject().GetDisplayName();
59
60 (*map)[org].push_back(cert);
61 }
62}
63
64string16 CertificateManagerModel::GetColumnText(
65 const net::X509Certificate& cert,
66 Column column) const {
67 string16 rv;
68 switch (column) {
69 case COL_SUBJECT_NAME:
[email protected]b1c2a5542010-10-08 12:44:4070 rv = UTF8ToUTF16(
71 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1772 break;
73 case COL_CERTIFICATE_STORE:
[email protected]b1c2a5542010-10-08 12:44:4074 rv = UTF8ToUTF16(
75 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1776 break;
77 case COL_SERIAL_NUMBER:
[email protected]b1c2a5542010-10-08 12:44:4078 rv = ASCIIToUTF16(
79 x509_certificate_model::GetSerialNumberHexified(
80 cert.os_cert_handle(), ""));
[email protected]0fd776c42010-09-29 21:59:1781 break;
82 case COL_EXPIRES_ON:
[email protected]1b6dc3e2010-12-22 15:08:0883 if (!cert.valid_expiry().is_null())
84 rv = base::TimeFormatShortDateNumeric(cert.valid_expiry());
[email protected]0fd776c42010-09-29 21:59:1785 break;
[email protected]0fd776c42010-09-29 21:59:1786 default:
87 NOTREACHED();
88 }
89 return rv;
90}
[email protected]06dc3202010-10-06 21:18:0791
[email protected]88b9db72011-01-13 01:48:4392int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module,
93 const std::string& data,
[email protected]06dc3202010-10-06 21:18:0794 const string16& password) {
[email protected]88b9db72011-01-13 01:48:4395 int result = cert_db_.ImportFromPKCS12(module, data, password);
[email protected]06dc3202010-10-06 21:18:0796 if (result == net::OK)
97 Refresh();
98 return result;
99}
100
[email protected]2feacc342010-10-12 22:52:52101bool CertificateManagerModel::ImportCACerts(
102 const net::CertificateList& certificates,
103 unsigned int trust_bits,
104 net::CertDatabase::ImportCertFailureList* not_imported) {
105 bool result = cert_db_.ImportCACerts(certificates, trust_bits, not_imported);
106 if (result && not_imported->size() != certificates.size())
107 Refresh();
108 return result;
[email protected]72a8d0d72010-10-08 00:36:57109}
110
[email protected]7a3a9652010-10-13 01:21:13111bool CertificateManagerModel::ImportServerCert(
112 const net::CertificateList& certificates,
113 net::CertDatabase::ImportCertFailureList* not_imported) {
114 bool result = cert_db_.ImportServerCert(certificates, not_imported);
115 if (result && not_imported->size() != certificates.size())
116 Refresh();
117 return result;
118}
119
[email protected]72a8d0d72010-10-08 00:36:57120bool CertificateManagerModel::SetCertTrust(const net::X509Certificate* cert,
121 net::CertType type,
122 unsigned int trust_bits) {
123 return cert_db_.SetCertTrust(cert, type, trust_bits);
124}
125
[email protected]06dc3202010-10-06 21:18:07126bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
127 bool result = cert_db_.DeleteCertAndKey(cert);
128 if (result)
129 Refresh();
130 return result;
131}