blob: c53547cce6c6f54f4a6f6bf1389670284b302b7a [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
5#include "chrome/browser/certificate_manager_model.h"
6
[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]b1c2a5542010-10-08 12:44:4010#include "chrome/common/net/x509_certificate_model.h"
[email protected]06dc3202010-10-06 21:18:0711#include "net/base/net_errors.h"
[email protected]0fd776c42010-09-29 21:59:1712#include "net/base/x509_certificate.h"
13
[email protected]06dc3202010-10-06 21:18:0714CertificateManagerModel::CertificateManagerModel(Observer* observer)
15 : observer_(observer) {
[email protected]0fd776c42010-09-29 21:59:1716}
17
18CertificateManagerModel::~CertificateManagerModel() {
19}
20
21void CertificateManagerModel::Refresh() {
[email protected]06dc3202010-10-06 21:18:0722 VLOG(1) << "refresh started";
[email protected]0fd776c42010-09-29 21:59:1723 cert_db_.ListCerts(&cert_list_);
[email protected]06dc3202010-10-06 21:18:0724 observer_->CertificatesRefreshed();
25 VLOG(1) << "refresh finished";
[email protected]0fd776c42010-09-29 21:59:1726}
27
28void CertificateManagerModel::FilterAndBuildOrgGroupingMap(
29 net::CertType filter_type,
30 CertificateManagerModel::OrgGroupingMap* map) const {
31 for (net::CertificateList::const_iterator i = cert_list_.begin();
32 i != cert_list_.end(); ++i) {
33 net::X509Certificate* cert = i->get();
[email protected]b1c2a5542010-10-08 12:44:4034 net::CertType type =
35 x509_certificate_model::GetType(cert->os_cert_handle());
[email protected]0fd776c42010-09-29 21:59:1736 if (type != filter_type)
37 continue;
38
39 std::string org;
40 if (!cert->subject().organization_names.empty())
41 org = cert->subject().organization_names[0];
42 if (org.empty())
43 org = cert->subject().GetDisplayName();
44
45 (*map)[org].push_back(cert);
46 }
47}
48
49string16 CertificateManagerModel::GetColumnText(
50 const net::X509Certificate& cert,
51 Column column) const {
52 string16 rv;
53 switch (column) {
54 case COL_SUBJECT_NAME:
[email protected]b1c2a5542010-10-08 12:44:4055 rv = UTF8ToUTF16(
56 x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1757 break;
58 case COL_CERTIFICATE_STORE:
[email protected]b1c2a5542010-10-08 12:44:4059 rv = UTF8ToUTF16(
60 x509_certificate_model::GetTokenName(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1761 break;
62 case COL_SERIAL_NUMBER:
[email protected]b1c2a5542010-10-08 12:44:4063 rv = ASCIIToUTF16(
64 x509_certificate_model::GetSerialNumberHexified(
65 cert.os_cert_handle(), ""));
[email protected]0fd776c42010-09-29 21:59:1766 break;
67 case COL_EXPIRES_ON:
68 if (!cert.valid_expiry().is_null()) {
69 rv = WideToUTF16Hack(
70 base::TimeFormatShortDateNumeric(cert.valid_expiry()));
71 }
72 break;
73 case COL_EMAIL_ADDRESS:
[email protected]b1c2a5542010-10-08 12:44:4074 rv = UTF8ToUTF16(
75 x509_certificate_model::GetEmailAddress(cert.os_cert_handle()));
[email protected]0fd776c42010-09-29 21:59:1776 break;
77 default:
78 NOTREACHED();
79 }
80 return rv;
81}
[email protected]06dc3202010-10-06 21:18:0782
83int CertificateManagerModel::ImportFromPKCS12(const std::string& data,
84 const string16& password) {
85 int result = cert_db_.ImportFromPKCS12(data, password);
86 if (result == net::OK)
87 Refresh();
88 return result;
89}
90
91int CertificateManagerModel::ExportToPKCS12(const net::CertificateList& certs,
92 const string16& password,
93 std::string* output) const {
94 return cert_db_.ExportToPKCS12(certs, password, output);
95}
96
[email protected]72a8d0d72010-10-08 00:36:5797unsigned int CertificateManagerModel::GetCertTrust(
98 const net::X509Certificate* cert, net::CertType type) const {
99 return cert_db_.GetCertTrust(cert, type);
100}
101
102bool CertificateManagerModel::SetCertTrust(const net::X509Certificate* cert,
103 net::CertType type,
104 unsigned int trust_bits) {
105 return cert_db_.SetCertTrust(cert, type, trust_bits);
106}
107
[email protected]06dc3202010-10-06 21:18:07108bool CertificateManagerModel::Delete(net::X509Certificate* cert) {
109 bool result = cert_db_.DeleteCertAndKey(cert);
110 if (result)
111 Refresh();
112 return result;
113}