OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/network/onc/onc_certificate_importer_impl.h" | 5 #include "chromeos/network/onc/onc_certificate_importer_impl.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <keyhi.h> | 8 #include <keyhi.h> |
9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chromeos/network/network_event_log.h" | 14 #include "chromeos/network/network_event_log.h" |
15 #include "chromeos/network/onc/onc_utils.h" | 15 #include "chromeos/network/onc/onc_utils.h" |
16 #include "components/onc/onc_constants.h" | 16 #include "components/onc/onc_constants.h" |
17 #include "net/base/crypto_module.h" | 17 #include "net/base/crypto_module.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 #include "net/cert/nss_cert_database.h" | 19 #include "net/cert/nss_cert_database.h" |
20 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
21 | 21 |
22 #define ONC_LOG_WARNING(message) \ | 22 #define ONC_LOG_WARNING(message) \ |
23 NET_LOG_DEBUG("ONC Certificate Import Warning", message) | 23 NET_LOG_DEBUG("ONC Certificate Import Warning", message) |
24 #define ONC_LOG_ERROR(message) \ | 24 #define ONC_LOG_ERROR(message) \ |
25 NET_LOG_ERROR("ONC Certificate Import Error", message) | 25 NET_LOG_ERROR("ONC Certificate Import Error", message) |
26 | 26 |
27 namespace chromeos { | 27 namespace chromeos { |
28 namespace onc { | 28 namespace onc { |
29 | 29 |
30 CertificateImporterImpl::CertificateImporterImpl() { | 30 CertificateImporterImpl::CertificateImporterImpl( |
| 31 net::NSSCertDatabase* target_nssdb) |
| 32 : target_nssdb_(target_nssdb) { |
| 33 CHECK(target_nssdb); |
31 } | 34 } |
32 | 35 |
33 bool CertificateImporterImpl::ImportCertificates( | 36 bool CertificateImporterImpl::ImportCertificates( |
34 const base::ListValue& certificates, | 37 const base::ListValue& certificates, |
35 ::onc::ONCSource source, | 38 ::onc::ONCSource source, |
36 net::CertificateList* onc_trusted_certificates) { | 39 net::CertificateList* onc_trusted_certificates) { |
37 VLOG(2) << "ONC file has " << certificates.GetSize() << " certificates"; | 40 VLOG(2) << "ONC file has " << certificates.GetSize() << " certificates"; |
38 | 41 |
39 // Web trust is only granted to certificates imported by the user. | 42 // Web trust is only granted to certificates imported by the user. |
40 bool allow_trust_imports = source == ::onc::ONC_SOURCE_USER_IMPORT; | 43 bool allow_trust_imports = source == ::onc::ONC_SOURCE_USER_IMPORT; |
41 if (!ParseAndStoreCertificates( | 44 if (!ParseAndStoreCertificates(allow_trust_imports, |
42 allow_trust_imports, certificates, onc_trusted_certificates, NULL)) { | 45 certificates, |
| 46 onc_trusted_certificates, |
| 47 NULL)) { |
43 LOG(ERROR) << "Cannot parse some of the certificates in the ONC from " | 48 LOG(ERROR) << "Cannot parse some of the certificates in the ONC from " |
44 << onc::GetSourceAsString(source); | 49 << onc::GetSourceAsString(source); |
45 return false; | 50 return false; |
46 } | 51 } |
47 return true; | 52 return true; |
48 } | 53 } |
49 | 54 |
50 bool CertificateImporterImpl::ParseAndStoreCertificates( | 55 bool CertificateImporterImpl::ParseAndStoreCertificates( |
51 bool allow_trust_imports, | 56 bool allow_trust_imports, |
52 const base::ListValue& certificates, | 57 const base::ListValue& certificates, |
(...skipping 15 matching lines...) Expand all Loading... |
68 ONC_LOG_ERROR( | 73 ONC_LOG_ERROR( |
69 base::StringPrintf("Cannot parse certificate at index %zu", i)); | 74 base::StringPrintf("Cannot parse certificate at index %zu", i)); |
70 } else { | 75 } else { |
71 VLOG(2) << "Successfully imported certificate at index " << i; | 76 VLOG(2) << "Successfully imported certificate at index " << i; |
72 } | 77 } |
73 } | 78 } |
74 return success; | 79 return success; |
75 } | 80 } |
76 | 81 |
77 // static | 82 // static |
78 void CertificateImporterImpl::ListCertsWithNickname(const std::string& label, | 83 void CertificateImporterImpl::ListCertsWithNickname( |
79 net::CertificateList* result) { | 84 const std::string& label, |
| 85 net::CertificateList* result, |
| 86 net::NSSCertDatabase* target_nssdb) { |
80 net::CertificateList all_certs; | 87 net::CertificateList all_certs; |
81 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); | 88 target_nssdb->ListCerts(&all_certs); |
82 result->clear(); | 89 result->clear(); |
83 for (net::CertificateList::iterator iter = all_certs.begin(); | 90 for (net::CertificateList::iterator iter = all_certs.begin(); |
84 iter != all_certs.end(); ++iter) { | 91 iter != all_certs.end(); ++iter) { |
85 if (iter->get()->os_cert_handle()->nickname) { | 92 if (iter->get()->os_cert_handle()->nickname) { |
86 // Separate the nickname stored in the certificate at the colon, since | 93 // Separate the nickname stored in the certificate at the colon, since |
87 // NSS likes to store it as token:nickname. | 94 // NSS likes to store it as token:nickname. |
88 const char* delimiter = | 95 const char* delimiter = |
89 ::strchr(iter->get()->os_cert_handle()->nickname, ':'); | 96 ::strchr(iter->get()->os_cert_handle()->nickname, ':'); |
90 if (delimiter) { | 97 if (delimiter) { |
91 ++delimiter; // move past the colon. | 98 ++delimiter; // move past the colon. |
(...skipping 15 matching lines...) Expand all Loading... |
107 if (private_key_nickname && std::string(label) == private_key_nickname) | 114 if (private_key_nickname && std::string(label) == private_key_nickname) |
108 result->push_back(*iter); | 115 result->push_back(*iter); |
109 PORT_Free(private_key_nickname); | 116 PORT_Free(private_key_nickname); |
110 SECKEY_DestroyPrivateKey(private_key); | 117 SECKEY_DestroyPrivateKey(private_key); |
111 } | 118 } |
112 } | 119 } |
113 } | 120 } |
114 | 121 |
115 // static | 122 // static |
116 bool CertificateImporterImpl::DeleteCertAndKeyByNickname( | 123 bool CertificateImporterImpl::DeleteCertAndKeyByNickname( |
117 const std::string& label) { | 124 const std::string& label, |
| 125 net::NSSCertDatabase* target_nssdb) { |
118 net::CertificateList cert_list; | 126 net::CertificateList cert_list; |
119 ListCertsWithNickname(label, &cert_list); | 127 ListCertsWithNickname(label, &cert_list, target_nssdb); |
120 bool result = true; | 128 bool result = true; |
121 for (net::CertificateList::iterator iter = cert_list.begin(); | 129 for (net::CertificateList::iterator iter = cert_list.begin(); |
122 iter != cert_list.end(); ++iter) { | 130 iter != cert_list.end(); ++iter) { |
123 // If we fail, we try and delete the rest still. | 131 // If we fail, we try and delete the rest still. |
124 // TODO(gspencer): this isn't very "transactional". If we fail on some, but | 132 // TODO(gspencer): this isn't very "transactional". If we fail on some, but |
125 // not all, then it's possible to leave things in a weird state. | 133 // not all, then it's possible to leave things in a weird state. |
126 // Luckily there should only be one cert with a particular | 134 // Luckily there should only be one cert with a particular |
127 // label, and the cert not being found is one of the few reasons the | 135 // label, and the cert not being found is one of the few reasons the |
128 // delete could fail, but still... The other choice is to return | 136 // delete could fail, but still... The other choice is to return |
129 // failure immediately, but that doesn't seem to do what is intended. | 137 // failure immediately, but that doesn't seem to do what is intended. |
130 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(iter->get())) | 138 if (!target_nssdb->DeleteCertAndKey(iter->get())) |
131 result = false; | 139 result = false; |
132 } | 140 } |
133 return result; | 141 return result; |
134 } | 142 } |
135 | 143 |
136 bool CertificateImporterImpl::ParseAndStoreCertificate( | 144 bool CertificateImporterImpl::ParseAndStoreCertificate( |
137 bool allow_trust_imports, | 145 bool allow_trust_imports, |
138 const base::DictionaryValue& certificate, | 146 const base::DictionaryValue& certificate, |
139 net::CertificateList* onc_trusted_certificates, | 147 net::CertificateList* onc_trusted_certificates, |
140 CertsByGUID* imported_server_and_ca_certs) { | 148 CertsByGUID* imported_server_and_ca_certs) { |
141 // Get out the attributes of the given certificate. | 149 // Get out the attributes of the given certificate. |
142 std::string guid; | 150 std::string guid; |
143 certificate.GetStringWithoutPathExpansion(::onc::certificate::kGUID, &guid); | 151 certificate.GetStringWithoutPathExpansion(::onc::certificate::kGUID, &guid); |
144 DCHECK(!guid.empty()); | 152 DCHECK(!guid.empty()); |
145 | 153 |
146 bool remove = false; | 154 bool remove = false; |
147 if (certificate.GetBooleanWithoutPathExpansion(::onc::kRemove, &remove) && | 155 if (certificate.GetBooleanWithoutPathExpansion(::onc::kRemove, &remove) && |
148 remove) { | 156 remove) { |
149 if (!DeleteCertAndKeyByNickname(guid)) { | 157 if (!DeleteCertAndKeyByNickname(guid, target_nssdb_)) { |
150 ONC_LOG_ERROR("Unable to delete certificate"); | 158 ONC_LOG_ERROR("Unable to delete certificate"); |
151 return false; | 159 return false; |
152 } else { | 160 } else { |
153 return true; | 161 return true; |
154 } | 162 } |
155 } | 163 } |
156 | 164 |
157 // Not removing, so let's get the data we need to add this certificate. | 165 // Not removing, so let's get the data we need to add this certificate. |
158 std::string cert_type; | 166 std::string cert_type; |
159 certificate.GetStringWithoutPathExpansion(::onc::certificate::kType, | 167 certificate.GetStringWithoutPathExpansion(::onc::certificate::kType, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 if (!x509_cert.get()) { | 235 if (!x509_cert.get()) { |
228 ONC_LOG_ERROR("Unable to create certificate from PEM encoding, type: " + | 236 ONC_LOG_ERROR("Unable to create certificate from PEM encoding, type: " + |
229 cert_type); | 237 cert_type); |
230 return false; | 238 return false; |
231 } | 239 } |
232 | 240 |
233 net::NSSCertDatabase::TrustBits trust = (import_with_ssl_trust ? | 241 net::NSSCertDatabase::TrustBits trust = (import_with_ssl_trust ? |
234 net::NSSCertDatabase::TRUSTED_SSL : | 242 net::NSSCertDatabase::TRUSTED_SSL : |
235 net::NSSCertDatabase::TRUST_DEFAULT); | 243 net::NSSCertDatabase::TRUST_DEFAULT); |
236 | 244 |
237 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); | |
238 if (x509_cert->os_cert_handle()->isperm) { | 245 if (x509_cert->os_cert_handle()->isperm) { |
239 net::CertType net_cert_type = | 246 net::CertType net_cert_type = |
240 cert_type == ::onc::certificate::kServer ? net::SERVER_CERT | 247 cert_type == ::onc::certificate::kServer ? net::SERVER_CERT |
241 : net::CA_CERT; | 248 : net::CA_CERT; |
242 VLOG(1) << "Certificate is already installed."; | 249 VLOG(1) << "Certificate is already installed."; |
243 net::NSSCertDatabase::TrustBits missing_trust_bits = | 250 net::NSSCertDatabase::TrustBits missing_trust_bits = |
244 trust & ~cert_database->GetCertTrust(x509_cert.get(), net_cert_type); | 251 trust & ~target_nssdb_->GetCertTrust(x509_cert.get(), net_cert_type); |
245 if (missing_trust_bits) { | 252 if (missing_trust_bits) { |
246 std::string error_reason; | 253 std::string error_reason; |
247 bool success = false; | 254 bool success = false; |
248 if (cert_database->IsReadOnly(x509_cert.get())) { | 255 if (target_nssdb_->IsReadOnly(x509_cert.get())) { |
249 error_reason = " Certificate is stored read-only."; | 256 error_reason = " Certificate is stored read-only."; |
250 } else { | 257 } else { |
251 success = cert_database->SetCertTrust(x509_cert.get(), | 258 success = target_nssdb_->SetCertTrust(x509_cert.get(), |
252 net_cert_type, | 259 net_cert_type, |
253 trust); | 260 trust); |
254 } | 261 } |
255 if (!success) { | 262 if (!success) { |
256 ONC_LOG_ERROR("Certificate of type " + cert_type + | 263 ONC_LOG_ERROR("Certificate of type " + cert_type + |
257 " was already present, but trust couldn't be set." + | 264 " was already present, but trust couldn't be set." + |
258 error_reason); | 265 error_reason); |
259 } | 266 } |
260 } | 267 } |
261 } else { | 268 } else { |
262 net::CertificateList cert_list; | 269 net::CertificateList cert_list; |
263 cert_list.push_back(x509_cert); | 270 cert_list.push_back(x509_cert); |
264 net::NSSCertDatabase::ImportCertFailureList failures; | 271 net::NSSCertDatabase::ImportCertFailureList failures; |
265 bool success = false; | 272 bool success = false; |
266 if (cert_type == ::onc::certificate::kServer) | 273 if (cert_type == ::onc::certificate::kServer) |
267 success = cert_database->ImportServerCert(cert_list, trust, &failures); | 274 success = target_nssdb_->ImportServerCert(cert_list, trust, &failures); |
268 else // Authority cert | 275 else // Authority cert |
269 success = cert_database->ImportCACerts(cert_list, trust, &failures); | 276 success = target_nssdb_->ImportCACerts(cert_list, trust, &failures); |
270 | 277 |
271 if (!failures.empty()) { | 278 if (!failures.empty()) { |
272 ONC_LOG_ERROR( | 279 ONC_LOG_ERROR( |
273 base::StringPrintf("Error ( %s ) importing %s certificate", | 280 base::StringPrintf("Error ( %s ) importing %s certificate", |
274 net::ErrorToString(failures[0].net_error), | 281 net::ErrorToString(failures[0].net_error), |
275 cert_type.c_str())); | 282 cert_type.c_str())); |
276 return false; | 283 return false; |
277 } | 284 } |
278 | 285 |
279 if (!success) { | 286 if (!success) { |
(...skipping 23 matching lines...) Expand all Loading... |
303 } | 310 } |
304 | 311 |
305 std::string decoded_pkcs12; | 312 std::string decoded_pkcs12; |
306 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) { | 313 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) { |
307 ONC_LOG_ERROR( | 314 ONC_LOG_ERROR( |
308 "Unable to base64 decode PKCS#12 data: \"" + pkcs12_data + "\"."); | 315 "Unable to base64 decode PKCS#12 data: \"" + pkcs12_data + "\"."); |
309 return false; | 316 return false; |
310 } | 317 } |
311 | 318 |
312 // Since this has a private key, always use the private module. | 319 // Since this has a private key, always use the private module. |
313 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); | 320 scoped_refptr<net::CryptoModule> module(net::CryptoModule::CreateFromHandle( |
314 scoped_refptr<net::CryptoModule> module(cert_database->GetPrivateModule()); | 321 target_nssdb_->GetPrivateSlot().get())); |
315 net::CertificateList imported_certs; | 322 net::CertificateList imported_certs; |
316 | 323 |
317 int import_result = cert_database->ImportFromPKCS12( | 324 int import_result = target_nssdb_->ImportFromPKCS12( |
318 module.get(), decoded_pkcs12, base::string16(), false, &imported_certs); | 325 module.get(), decoded_pkcs12, base::string16(), false, &imported_certs); |
319 if (import_result != net::OK) { | 326 if (import_result != net::OK) { |
320 ONC_LOG_ERROR( | 327 ONC_LOG_ERROR( |
321 base::StringPrintf("Unable to import client certificate (error %s)", | 328 base::StringPrintf("Unable to import client certificate (error %s)", |
322 net::ErrorToString(import_result))); | 329 net::ErrorToString(import_result))); |
323 return false; | 330 return false; |
324 } | 331 } |
325 | 332 |
326 if (imported_certs.size() == 0) { | 333 if (imported_certs.size() == 0) { |
327 ONC_LOG_WARNING("PKCS12 data contains no importable certificates."); | 334 ONC_LOG_WARNING("PKCS12 data contains no importable certificates."); |
(...skipping 17 matching lines...) Expand all Loading... |
345 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); | 352 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); |
346 SECKEY_DestroyPrivateKey(private_key); | 353 SECKEY_DestroyPrivateKey(private_key); |
347 } else { | 354 } else { |
348 ONC_LOG_WARNING("Unable to find private key for certificate."); | 355 ONC_LOG_WARNING("Unable to find private key for certificate."); |
349 } | 356 } |
350 return true; | 357 return true; |
351 } | 358 } |
352 | 359 |
353 } // namespace onc | 360 } // namespace onc |
354 } // namespace chromeos | 361 } // namespace chromeos |
OLD | NEW |