| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "chromeos/network/onc/onc_certificate_importer.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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // The PEM block header used for DER certificates | 27 // The PEM block header used for DER certificates |
| 28 const char kCertificateHeader[] = "CERTIFICATE"; | 28 const char kCertificateHeader[] = "CERTIFICATE"; |
| 29 // This is an older PEM marker for DER certificates. | 29 // This is an older PEM marker for DER certificates. |
| 30 const char kX509CertificateHeader[] = "X509 CERTIFICATE"; | 30 const char kX509CertificateHeader[] = "X509 CERTIFICATE"; |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 namespace chromeos { | 34 namespace chromeos { |
| 35 namespace onc { | 35 namespace onc { |
| 36 | 36 |
| 37 CertificateImporter::CertificateImporter( | 37 CertificateImporter::CertificateImporter(bool allow_web_trust) |
| 38 ONCSource onc_source, | 38 : allow_web_trust_(allow_web_trust) { |
| 39 bool allow_web_trust_from_policy) | |
| 40 : onc_source_(onc_source), | |
| 41 allow_web_trust_from_policy_(allow_web_trust_from_policy) { | |
| 42 } | 39 } |
| 43 | 40 |
| 44 CertificateImporter::ParseResult CertificateImporter::ParseAndStoreCertificates( | 41 CertificateImporter::ParseResult CertificateImporter::ParseAndStoreCertificates( |
| 45 const base::ListValue& certificates) { | 42 const base::ListValue& certificates) { |
| 46 size_t successful_imports = 0; | 43 size_t successful_imports = 0; |
| 47 for (size_t i = 0; i < certificates.GetSize(); ++i) { | 44 for (size_t i = 0; i < certificates.GetSize(); ++i) { |
| 48 const base::DictionaryValue* certificate = NULL; | 45 const base::DictionaryValue* certificate = NULL; |
| 49 if (!certificates.GetDictionary(i, &certificate)) { | 46 certificates.GetDictionary(i, &certificate); |
| 50 ONC_LOG_ERROR("Certificate data malformed"); | 47 DCHECK(certificate != NULL); |
| 51 continue; | |
| 52 } | |
| 53 | 48 |
| 54 VLOG(2) << "Parsing certificate at index " << i << ": " << *certificate; | 49 VLOG(2) << "Parsing certificate at index " << i << ": " << *certificate; |
| 55 | 50 |
| 56 if (!ParseAndStoreCertificate(*certificate)) { | 51 if (!ParseAndStoreCertificate(*certificate)) { |
| 57 ONC_LOG_ERROR( | 52 ONC_LOG_ERROR( |
| 58 base::StringPrintf("Cannot parse certificate at index %zu", i)); | 53 base::StringPrintf("Cannot parse certificate at index %zu", i)); |
| 59 } else { | 54 } else { |
| 60 VLOG(2) << "Successfully imported certificate at index " << i; | 55 VLOG(2) << "Successfully imported certificate at index " << i; |
| 61 ++successful_imports; | 56 ++successful_imports; |
| 62 } | 57 } |
| 63 } | 58 } |
| 64 | 59 |
| 65 if (successful_imports == certificates.GetSize()) | 60 if (successful_imports == certificates.GetSize()) { |
| 66 return IMPORT_OK; | 61 return IMPORT_OK; |
| 67 else if (successful_imports == 0) | 62 } else if (successful_imports == 0) { |
| 68 return IMPORT_FAILED; | 63 return IMPORT_FAILED; |
| 69 else | 64 } else { |
| 70 return IMPORT_INCOMPLETE; | 65 return IMPORT_INCOMPLETE; |
| 66 } |
| 71 } | 67 } |
| 72 | 68 |
| 73 bool CertificateImporter::ParseAndStoreCertificate( | 69 bool CertificateImporter::ParseAndStoreCertificate( |
| 74 const base::DictionaryValue& certificate) { | 70 const base::DictionaryValue& certificate) { |
| 75 // Get out the attributes of the given certificate. | 71 // Get out the attributes of the given certificate. |
| 76 std::string guid; | 72 std::string guid; |
| 77 if (!certificate.GetString(certificate::kGUID, &guid) || guid.empty()) { | 73 certificate.GetString(certificate::kGUID, &guid); |
| 78 ONC_LOG_ERROR("Certificate missing GUID identifier"); | 74 DCHECK(!guid.empty()); |
| 79 return false; | |
| 80 } | |
| 81 | 75 |
| 82 bool remove = false; | 76 bool remove = false; |
| 83 if (certificate.GetBoolean(kRemove, &remove) && remove) { | 77 if (certificate.GetBoolean(kRemove, &remove) && remove) { |
| 84 if (!DeleteCertAndKeyByNickname(guid)) { | 78 if (!DeleteCertAndKeyByNickname(guid)) { |
| 85 ONC_LOG_ERROR("Unable to delete certificate"); | 79 ONC_LOG_ERROR("Unable to delete certificate"); |
| 86 return false; | 80 return false; |
| 87 } else { | 81 } else { |
| 88 return true; | 82 return true; |
| 89 } | 83 } |
| 90 } | 84 } |
| 91 | 85 |
| 92 // Not removing, so let's get the data we need to add this certificate. | 86 // Not removing, so let's get the data we need to add this certificate. |
| 93 std::string cert_type; | 87 std::string cert_type; |
| 94 certificate.GetString(certificate::kType, &cert_type); | 88 certificate.GetString(certificate::kType, &cert_type); |
| 95 if (cert_type == certificate::kServer || cert_type == certificate::kAuthority) | 89 if (cert_type == certificate::kServer || |
| 90 cert_type == certificate::kAuthority) { |
| 96 return ParseServerOrCaCertificate(cert_type, guid, certificate); | 91 return ParseServerOrCaCertificate(cert_type, guid, certificate); |
| 92 } else if (cert_type == certificate::kClient) { |
| 93 return ParseClientCertificate(guid, certificate); |
| 94 } |
| 97 | 95 |
| 98 if (cert_type == certificate::kClient) | 96 NOTREACHED(); |
| 99 return ParseClientCertificate(guid, certificate); | |
| 100 | |
| 101 ONC_LOG_ERROR("Certificate of unknown type: " + cert_type); | |
| 102 return false; | 97 return false; |
| 103 } | 98 } |
| 104 | 99 |
| 105 // static | 100 // static |
| 106 void CertificateImporter::ListCertsWithNickname(const std::string& label, | 101 void CertificateImporter::ListCertsWithNickname(const std::string& label, |
| 107 net::CertificateList* result) { | 102 net::CertificateList* result) { |
| 108 net::CertificateList all_certs; | 103 net::CertificateList all_certs; |
| 109 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); | 104 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); |
| 110 result->clear(); | 105 result->clear(); |
| 111 for (net::CertificateList::iterator iter = all_certs.begin(); | 106 for (net::CertificateList::iterator iter = all_certs.begin(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(iter->get())) | 152 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(iter->get())) |
| 158 result = false; | 153 result = false; |
| 159 } | 154 } |
| 160 return result; | 155 return result; |
| 161 } | 156 } |
| 162 | 157 |
| 163 bool CertificateImporter::ParseServerOrCaCertificate( | 158 bool CertificateImporter::ParseServerOrCaCertificate( |
| 164 const std::string& cert_type, | 159 const std::string& cert_type, |
| 165 const std::string& guid, | 160 const std::string& guid, |
| 166 const base::DictionaryValue& certificate) { | 161 const base::DictionaryValue& certificate) { |
| 167 // Device policy can't import certificates. | |
| 168 if (onc_source_ == ONC_SOURCE_DEVICE_POLICY) { | |
| 169 // This isn't a parsing error. | |
| 170 ONC_LOG_WARNING("Refusing to import certificate from device policy."); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 bool web_trust = false; | 162 bool web_trust = false; |
| 175 const base::ListValue* trust_list = NULL; | 163 const base::ListValue* trust_list = NULL; |
| 176 if (certificate.GetList(certificate::kTrust, &trust_list)) { | 164 if (certificate.GetList(certificate::kTrust, &trust_list)) { |
| 177 for (size_t i = 0; i < trust_list->GetSize(); ++i) { | 165 for (size_t i = 0; i < trust_list->GetSize(); ++i) { |
| 178 std::string trust_type; | 166 std::string trust_type; |
| 179 if (!trust_list->GetString(i, &trust_type)) { | 167 if (!trust_list->GetString(i, &trust_type)) |
| 180 ONC_LOG_ERROR("Certificate trust is invalid"); | 168 NOTREACHED(); |
| 181 return false; | 169 |
| 182 } | |
| 183 if (trust_type == certificate::kWeb) { | 170 if (trust_type == certificate::kWeb) { |
| 184 // "Web" implies that the certificate is to be trusted for SSL | 171 // "Web" implies that the certificate is to be trusted for SSL |
| 185 // identification. | 172 // identification. |
| 186 web_trust = true; | 173 web_trust = true; |
| 187 } else { | 174 } else { |
| 188 ONC_LOG_ERROR("Certificate contains unknown trust type " + trust_type); | 175 ONC_LOG_ERROR("Certificate contains unknown trust type " + trust_type); |
| 189 return false; | 176 return false; |
| 190 } | 177 } |
| 191 } | 178 } |
| 192 } | 179 } |
| 193 | 180 |
| 194 // Web trust is only granted to certificates imported for a managed user | 181 if (web_trust && !allow_web_trust_) { |
| 195 // on a managed device. | |
| 196 if (onc_source_ == ONC_SOURCE_USER_POLICY && | |
| 197 web_trust && !allow_web_trust_from_policy_) { | |
| 198 LOG(WARNING) << "Web trust not granted for certificate: " << guid; | 182 LOG(WARNING) << "Web trust not granted for certificate: " << guid; |
| 199 web_trust = false; | 183 web_trust = false; |
| 200 } | 184 } |
| 201 | 185 |
| 202 std::string x509_data; | 186 std::string x509_data; |
| 203 if (!certificate.GetString(certificate::kX509, &x509_data) || | 187 if (!certificate.GetString(certificate::kX509, &x509_data) || |
| 204 x509_data.empty()) { | 188 x509_data.empty()) { |
| 205 ONC_LOG_ERROR( | 189 ONC_LOG_ERROR( |
| 206 "Certificate missing appropriate certificate data for type: " + | 190 "Certificate missing appropriate certificate data for type: " + |
| 207 cert_type); | 191 cert_type); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 return false; | 272 return false; |
| 289 } | 273 } |
| 290 | 274 |
| 291 net::CertificateList cert_list; | 275 net::CertificateList cert_list; |
| 292 cert_list.push_back(x509_cert); | 276 cert_list.push_back(x509_cert); |
| 293 net::NSSCertDatabase::ImportCertFailureList failures; | 277 net::NSSCertDatabase::ImportCertFailureList failures; |
| 294 bool success = false; | 278 bool success = false; |
| 295 net::NSSCertDatabase::TrustBits trust = web_trust ? | 279 net::NSSCertDatabase::TrustBits trust = web_trust ? |
| 296 net::NSSCertDatabase::TRUSTED_SSL : | 280 net::NSSCertDatabase::TRUSTED_SSL : |
| 297 net::NSSCertDatabase::TRUST_DEFAULT; | 281 net::NSSCertDatabase::TRUST_DEFAULT; |
| 298 if (cert_type == certificate::kServer) | 282 if (cert_type == certificate::kServer) { |
| 299 success = cert_database->ImportServerCert(cert_list, trust, &failures); | 283 success = cert_database->ImportServerCert(cert_list, trust, &failures); |
| 300 else // Authority cert | 284 } else { // Authority cert |
| 301 success = cert_database->ImportCACerts(cert_list, trust, &failures); | 285 success = cert_database->ImportCACerts(cert_list, trust, &failures); |
| 286 } |
| 302 | 287 |
| 303 if (!failures.empty()) { | 288 if (!failures.empty()) { |
| 304 ONC_LOG_ERROR("Error (" + net::ErrorToString(failures[0].net_error) + | 289 ONC_LOG_ERROR("Error (" + net::ErrorToString(failures[0].net_error) + |
| 305 ") importing " + cert_type + " certificate"); | 290 ") importing " + cert_type + " certificate"); |
| 306 return false; | 291 return false; |
| 307 } | 292 } |
| 308 if (!success) { | 293 if (!success) { |
| 309 ONC_LOG_ERROR("Unknown error importing " + cert_type + " certificate."); | 294 ONC_LOG_ERROR("Unknown error importing " + cert_type + " certificate."); |
| 310 return false; | 295 return false; |
| 311 } | 296 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); | 350 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); |
| 366 SECKEY_DestroyPrivateKey(private_key); | 351 SECKEY_DestroyPrivateKey(private_key); |
| 367 } else { | 352 } else { |
| 368 ONC_LOG_WARNING("Unable to find private key for certificate."); | 353 ONC_LOG_WARNING("Unable to find private key for certificate."); |
| 369 } | 354 } |
| 370 return true; | 355 return true; |
| 371 } | 356 } |
| 372 | 357 |
| 373 } // namespace onc | 358 } // namespace onc |
| 374 } // namespace chromeos | 359 } // namespace chromeos |
| OLD | NEW |