[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 289838c | 2011-09-29 22:12:27 | [diff] [blame] | 7 | #include "base/bind.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) |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 26 | : cert_db_(net::NSSCertDatabase::GetInstance()), |
| 27 | observer_(observer) { |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | CertificateManagerModel::~CertificateManagerModel() { |
| 31 | } |
| 32 | |
| 33 | void CertificateManagerModel::Refresh() { |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 34 | VLOG(1) << "refresh started"; |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 35 | net::CryptoModuleList modules; |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 36 | cert_db_->ListModules(&modules, false); |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 37 | VLOG(1) << "refresh waiting for unlocking..."; |
[email protected] | 6246ac5 | 2012-09-24 01:55:29 | [diff] [blame^] | 38 | chrome::UnlockSlotsIfNecessary( |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 39 | modules, |
[email protected] | 6246ac5 | 2012-09-24 01:55:29 | [diff] [blame^] | 40 | chrome::kCryptoModulePasswordListCerts, |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 41 | "", // unused. |
[email protected] | 289838c | 2011-09-29 22:12:27 | [diff] [blame] | 42 | base::Bind(&CertificateManagerModel::RefreshSlotsUnlocked, |
| 43 | base::Unretained(this))); |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void CertificateManagerModel::RefreshSlotsUnlocked() { |
| 47 | VLOG(1) << "refresh listing certs..."; |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 48 | cert_db_->ListCerts(&cert_list_); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 49 | observer_->CertificatesRefreshed(); |
| 50 | VLOG(1) << "refresh finished"; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void 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] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 59 | net::CertType type = |
| 60 | x509_certificate_model::GetType(cert->os_cert_handle()); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 61 | 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 | |
| 74 | string16 CertificateManagerModel::GetColumnText( |
| 75 | const net::X509Certificate& cert, |
| 76 | Column column) const { |
| 77 | string16 rv; |
| 78 | switch (column) { |
| 79 | case COL_SUBJECT_NAME: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 80 | rv = UTF8ToUTF16( |
| 81 | x509_certificate_model::GetCertNameOrNickname(cert.os_cert_handle())); |
[email protected] | 1d77c3e | 2011-06-08 16:34:47 | [diff] [blame] | 82 | |
| 83 | #if defined(OS_CHROMEOS) |
| 84 | // TODO(xiyuan): Put this into a column when we have js tree-table. |
[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 85 | if (IsHardwareBacked(&cert)) { |
[email protected] | 1d77c3e | 2011-06-08 16:34:47 | [diff] [blame] | 86 | 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] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 92 | break; |
| 93 | case COL_CERTIFICATE_STORE: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 94 | rv = UTF8ToUTF16( |
| 95 | x509_certificate_model::GetTokenName(cert.os_cert_handle())); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 96 | break; |
| 97 | case COL_SERIAL_NUMBER: |
[email protected] | b1c2a554 | 2010-10-08 12:44:40 | [diff] [blame] | 98 | rv = ASCIIToUTF16( |
| 99 | x509_certificate_model::GetSerialNumberHexified( |
| 100 | cert.os_cert_handle(), "")); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 101 | break; |
| 102 | case COL_EXPIRES_ON: |
[email protected] | 1b6dc3e | 2010-12-22 15:08:08 | [diff] [blame] | 103 | if (!cert.valid_expiry().is_null()) |
| 104 | rv = base::TimeFormatShortDateNumeric(cert.valid_expiry()); |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 105 | break; |
[email protected] | 0fd776c4 | 2010-09-29 21:59:17 | [diff] [blame] | 106 | default: |
| 107 | NOTREACHED(); |
| 108 | } |
| 109 | return rv; |
| 110 | } |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 111 | |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 112 | int CertificateManagerModel::ImportFromPKCS12(net::CryptoModule* module, |
| 113 | const std::string& data, |
[email protected] | 6a18d07 | 2011-06-29 00:25:40 | [diff] [blame] | 114 | const string16& password, |
| 115 | bool is_extractable) { |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 116 | int result = cert_db_->ImportFromPKCS12(module, data, password, |
| 117 | is_extractable, NULL); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 118 | if (result == net::OK) |
| 119 | Refresh(); |
| 120 | return result; |
| 121 | } |
| 122 | |
[email protected] | 2feacc34 | 2010-10-12 22:52:52 | [diff] [blame] | 123 | bool CertificateManagerModel::ImportCACerts( |
| 124 | const net::CertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 125 | net::NSSCertDatabase::TrustBits trust_bits, |
| 126 | net::NSSCertDatabase::ImportCertFailureList* not_imported) { |
| 127 | bool result = cert_db_->ImportCACerts(certificates, trust_bits, not_imported); |
[email protected] | 2feacc34 | 2010-10-12 22:52:52 | [diff] [blame] | 128 | if (result && not_imported->size() != certificates.size()) |
| 129 | Refresh(); |
| 130 | return result; |
[email protected] | 72a8d0d7 | 2010-10-08 00:36:57 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 7a3a965 | 2010-10-13 01:21:13 | [diff] [blame] | 133 | bool CertificateManagerModel::ImportServerCert( |
| 134 | const net::CertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 135 | net::NSSCertDatabase::TrustBits trust_bits, |
| 136 | net::NSSCertDatabase::ImportCertFailureList* not_imported) { |
| 137 | bool result = cert_db_->ImportServerCert(certificates, trust_bits, |
| 138 | not_imported); |
[email protected] | 7a3a965 | 2010-10-13 01:21:13 | [diff] [blame] | 139 | if (result && not_imported->size() != certificates.size()) |
| 140 | Refresh(); |
| 141 | return result; |
| 142 | } |
| 143 | |
[email protected] | c79b784d1 | 2011-09-20 18:44:54 | [diff] [blame] | 144 | bool CertificateManagerModel::SetCertTrust( |
| 145 | const net::X509Certificate* cert, |
| 146 | net::CertType type, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 147 | net::NSSCertDatabase::TrustBits trust_bits) { |
| 148 | return cert_db_->SetCertTrust(cert, type, trust_bits); |
[email protected] | 72a8d0d7 | 2010-10-08 00:36:57 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 151 | bool CertificateManagerModel::Delete(net::X509Certificate* cert) { |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 152 | bool result = cert_db_->DeleteCertAndKey(cert); |
[email protected] | 06dc320 | 2010-10-06 21:18:07 | [diff] [blame] | 153 | if (result) |
| 154 | Refresh(); |
| 155 | return result; |
| 156 | } |
[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 157 | |
| 158 | bool CertificateManagerModel::IsHardwareBacked( |
| 159 | const net::X509Certificate* cert) const { |
| 160 | #if defined(OS_CHROMEOS) |
| 161 | return crypto::IsTPMTokenReady() && |
| 162 | cert->os_cert_handle()->slot == |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 163 | cert_db_->GetPrivateModule()->os_module_handle(); |
[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 164 | #else |
| 165 | return false; |
| 166 | #endif |
| 167 | } |