[email protected] | 1d77c3e | 2011-06-08 16:34:47 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 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] | 4f24296 | 2011-05-13 22:25:22 | [diff] [blame] | 5 | #include "chrome/browser/certificate_manager_model.h" |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 6 | |
[email protected] | 2b62324 | 2011-05-10 00:37:40 | [diff] [blame] | 7 | #include "base/callback_old.h" |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 8 | #include "base/i18n/time_formatting.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/utf_string_conversions.h" |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 11 | #include "chrome/browser/ui/crypto_module_password_dialog.h" |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 12 | #include "chrome/common/net/x509_certificate_model.h" |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 13 | #include "net/base/crypto_module.h" |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 15 | #include "net/base/x509_certificate.h" |
| 16 | |
[email protected] | 1d77c3e | 2011-06-08 16:34:47 | [diff] [blame^] | 17 | #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] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 25 | CertificateManagerModel::CertificateManagerModel(Observer* observer) |
| 26 | : observer_(observer) { |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | CertificateManagerModel::~CertificateManagerModel() { |
| 30 | } |
| 31 | |
| 32 | void CertificateManagerModel::Refresh() { |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 33 | VLOG(1) << "refresh started"; |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 34 | net::CryptoModuleList modules; |
| 35 | cert_db_.ListModules(&modules, false); |
| 36 | VLOG(1) << "refresh waiting for unlocking..."; |
| 37 | browser::UnlockSlotsIfNecessary( |
| 38 | modules, |
| 39 | browser::kCryptoModulePasswordListCerts, |
| 40 | "", // unused. |
| 41 | NewCallback(this, |
| 42 | &CertificateManagerModel::RefreshSlotsUnlocked)); |
| 43 | } |
| 44 | |
| 45 | void CertificateManagerModel::RefreshSlotsUnlocked() { |
| 46 | VLOG(1) << "refresh listing certs..."; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 47 | cert_db_.ListCerts(&cert_list_); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 48 | observer_->CertificatesRefreshed(); |
| 49 | VLOG(1) << "refresh finished"; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void CertificateManagerModel::FilterAndBuildOrgGroupingMap( |
| 53 | net::CertType filter_type, |
| 54 | CertificateManagerModel::OrgGroupingMap* map) const { |
| 55 | for (net::CertificateList::const_iterator i = cert_list_.begin(); |
| 56 | i != cert_list_.end(); ++i) { |
| 57 | net::X509Certificate* cert = i->get(); |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 58 | net::CertType type = |
| 59 | x509_certificate_model::GetType(cert->os_cert_handle()); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 60 | if (type != filter_type) |
| 61 | continue; |
| 62 | |
| 63 | std::string org; |
| 64 | if (!cert->subject().organization_names.empty()) |
| 65 | org = cert->subject().organization_names[0]; |
| 66 | if (org.empty()) |
| 67 | org = cert->subject().GetDisplayName(); |
| 68 | |
| 69 | (*map)[org].push_back(cert); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | string16 CertificateManagerModel::GetColumnText( |
| 74 | const net::X509Certificate& cert, |
| 75 | Column column) const { |
| 76 | string16 rv; |
| 77 | switch (column) { |
| 78 | case COL_SUBJECT_NAME: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 79 | rv = UTF8ToUTF16( |
| 80 | x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle())); |
[email protected] | 1d77c3e | 2011-06-08 16:34:47 | [diff] [blame^] | 81 | |
| 82 | #if defined(OS_CHROMEOS) |
| 83 | // TODO(xiyuan): Put this into a column when we have js tree-table. |
| 84 | if (crypto::IsTPMTokenReady() && |
| 85 | cert.os_cert_handle()->slot == |
| 86 | cert_db().GetPrivateModule()->os_module_handle()) { |
| 87 | rv = l10n_util::GetStringFUTF16( |
| 88 | IDS_CERT_MANAGER_HARDWARE_BACKED_KEY_FORMAT, |
| 89 | rv, |
| 90 | l10n_util::GetStringUTF16(IDS_CERT_MANAGER_HARDWARE_BACKED)); |
| 91 | } |
| 92 | #endif |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 93 | break; |
| 94 | case COL_CERTIFICATE_STORE: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 95 | rv = UTF8ToUTF16( |
| 96 | x509_certificate_model::GetTokenName(cert.os_cert_handle())); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 97 | break; |
| 98 | case COL_SERIAL_NUMBER: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 99 | rv = ASCIIToUTF16( |
| 100 | x509_certificate_model::GetSerialNumberHexified( |
| 101 | cert.os_cert_handle(), "")); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 102 | break; |
| 103 | case COL_EXPIRES_ON: |
[email protected] | 1b6dc3e | 2010-12-22 15:08:08 | [diff] [blame] | 104 | if (!cert.valid_expiry().is_null()) |
| 105 | rv = base::TimeFormatShortDateNumeric(cert.valid_expiry()); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 106 | break; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 107 | default: |
| 108 | NOTREACHED(); |
| 109 | } |
| 110 | return rv; |
| 111 | } |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 112 | |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 113 | int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module, |
| 114 | const std::string& data, |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 115 | const string16& password) { |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 116 | int result = cert_db_.ImportFromPKCS12(module, data, password); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 117 | if (result == net::OK) |
| 118 | Refresh(); |
| 119 | return result; |
| 120 | } |
| 121 | |
[email protected] | 2feacc34 | 2010-10-12 22:52:52 | [diff] [blame] | 122 | bool CertificateManagerModel::ImportCACerts( |
| 123 | const net::CertificateList& certificates, |
| 124 | unsigned int trust_bits, |
| 125 | net::CertDatabase::ImportCertFailureList* not_imported) { |
| 126 | bool result = cert_db_.ImportCACerts(certificates, trust_bits, not_imported); |
| 127 | if (result && not_imported->size() != certificates.size()) |
| 128 | Refresh(); |
| 129 | return result; |
[email protected] | 72a8d0d7 | 2010-10-08 00:36:57 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 7a3a965 | 2010-10-13 01:21:13 | [diff] [blame] | 132 | bool CertificateManagerModel::ImportServerCert( |
| 133 | const net::CertificateList& certificates, |
| 134 | net::CertDatabase::ImportCertFailureList* not_imported) { |
| 135 | bool result = cert_db_.ImportServerCert(certificates, not_imported); |
| 136 | if (result && not_imported->size() != certificates.size()) |
| 137 | Refresh(); |
| 138 | return result; |
| 139 | } |
| 140 | |
[email protected] | 72a8d0d7 | 2010-10-08 00:36:57 | [diff] [blame] | 141 | bool CertificateManagerModel::SetCertTrust(const net::X509Certificate* cert, |
| 142 | net::CertType type, |
| 143 | unsigned int trust_bits) { |
| 144 | return cert_db_.SetCertTrust(cert, type, trust_bits); |
| 145 | } |
| 146 | |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 147 | bool CertificateManagerModel::Delete(net::X509Certificate* cert) { |
| 148 | bool result = cert_db_.DeleteCertAndKey(cert); |
| 149 | if (result) |
| 150 | Refresh(); |
| 151 | return result; |
| 152 | } |