[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 1 | // 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] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 7 | #include "base/i18n/time_formatting.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/utf_string_conversions.h" |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 10 | #include "chrome/common/net/x509_certificate_model.h" |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 11 | #include "net/base/net_errors.h" |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 12 | #include "net/base/x509_certificate.h" |
| 13 | |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 14 | CertificateManagerModel::CertificateManagerModel(Observer* observer) |
| 15 | : observer_(observer) { |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | CertificateManagerModel::~CertificateManagerModel() { |
| 19 | } |
| 20 | |
| 21 | void CertificateManagerModel::Refresh() { |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 22 | VLOG(1) << "refresh started"; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 23 | cert_db_.ListCerts(&cert_list_); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 24 | observer_->CertificatesRefreshed(); |
| 25 | VLOG(1) << "refresh finished"; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void 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] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 34 | net::CertType type = |
| 35 | x509_certificate_model::GetType(cert->os_cert_handle()); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 36 | 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 | |
| 49 | string16 CertificateManagerModel::GetColumnText( |
| 50 | const net::X509Certificate& cert, |
| 51 | Column column) const { |
| 52 | string16 rv; |
| 53 | switch (column) { |
| 54 | case COL_SUBJECT_NAME: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 55 | rv = UTF8ToUTF16( |
| 56 | x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle())); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 57 | break; |
| 58 | case COL_CERTIFICATE_STORE: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 59 | rv = UTF8ToUTF16( |
| 60 | x509_certificate_model::GetTokenName(cert.os_cert_handle())); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 61 | break; |
| 62 | case COL_SERIAL_NUMBER: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 63 | rv = ASCIIToUTF16( |
| 64 | x509_certificate_model::GetSerialNumberHexified( |
| 65 | cert.os_cert_handle(), "")); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 66 | 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] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame^] | 74 | rv = UTF8ToUTF16( |
| 75 | x509_certificate_model::GetEmailAddress(cert.os_cert_handle())); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 76 | break; |
| 77 | default: |
| 78 | NOTREACHED(); |
| 79 | } |
| 80 | return rv; |
| 81 | } |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 82 | |
| 83 | int 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 | |
| 91 | int 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] | 72a8d0d7 | 2010-10-08 00:36:57 | [diff] [blame] | 97 | unsigned int CertificateManagerModel::GetCertTrust( |
| 98 | const net::X509Certificate* cert, net::CertType type) const { |
| 99 | return cert_db_.GetCertTrust(cert, type); |
| 100 | } |
| 101 | |
| 102 | bool 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] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 108 | bool CertificateManagerModel::Delete(net::X509Certificate* cert) { |
| 109 | bool result = cert_db_.DeleteCertAndKey(cert); |
| 110 | if (result) |
| 111 | Refresh(); |
| 112 | return result; |
| 113 | } |