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; |
71 } | 66 } |
72 | 67 |
73 bool CertificateImporter::ParseAndStoreCertificate( | 68 bool CertificateImporter::ParseAndStoreCertificate( |
74 const base::DictionaryValue& certificate) { | 69 const base::DictionaryValue& certificate) { |
75 // Get out the attributes of the given certificate. | 70 // Get out the attributes of the given certificate. |
76 std::string guid; | 71 std::string guid; |
77 if (!certificate.GetString(kGUID, &guid) || guid.empty()) { | 72 certificate.GetString(kGUID, &guid); |
78 ONC_LOG_ERROR("Certificate missing GUID identifier"); | 73 DCHECK(!guid.empty()); |
79 return false; | |
80 } | |
81 | 74 |
82 bool remove = false; | 75 bool remove = false; |
83 if (certificate.GetBoolean(kRemove, &remove) && remove) { | 76 if (certificate.GetBoolean(kRemove, &remove) && remove) { |
84 if (!DeleteCertAndKeyByNickname(guid)) { | 77 if (!DeleteCertAndKeyByNickname(guid)) { |
85 ONC_LOG_ERROR("Unable to delete certificate"); | 78 ONC_LOG_ERROR("Unable to delete certificate"); |
86 return false; | 79 return false; |
87 } else { | 80 } else { |
88 return true; | 81 return true; |
89 } | 82 } |
90 } | 83 } |
91 | 84 |
92 // Not removing, so let's get the data we need to add this certificate. | 85 // Not removing, so let's get the data we need to add this certificate. |
93 std::string cert_type; | 86 std::string cert_type; |
94 certificate.GetString(certificate::kType, &cert_type); | 87 certificate.GetString(certificate::kType, &cert_type); |
95 if (cert_type == certificate::kServer || cert_type == certificate::kAuthority) | 88 if (cert_type == certificate::kServer || cert_type == certificate::kAuthority) |
96 return ParseServerOrCaCertificate(cert_type, guid, certificate); | 89 return ParseServerOrCaCertificate(cert_type, guid, certificate); |
97 | 90 else if (cert_type == certificate::kClient) |
Greg Spencer (Chromium)
2013/01/18 17:39:36
Entire if clause should have braces..
if (...) {
pneubeck (no reviews)
2013/01/24 19:36:11
Done.
| |
98 if (cert_type == certificate::kClient) | |
99 return ParseClientCertificate(guid, certificate); | 91 return ParseClientCertificate(guid, certificate); |
100 | 92 |
101 ONC_LOG_ERROR("Certificate of unknown type: " + cert_type); | 93 NOTREACHED(); |
102 return false; | 94 return false; |
103 } | 95 } |
104 | 96 |
105 // static | 97 // static |
106 void CertificateImporter::ListCertsWithNickname(const std::string& label, | 98 void CertificateImporter::ListCertsWithNickname(const std::string& label, |
107 net::CertificateList* result) { | 99 net::CertificateList* result) { |
108 net::CertificateList all_certs; | 100 net::CertificateList all_certs; |
109 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); | 101 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); |
110 result->clear(); | 102 result->clear(); |
111 for (net::CertificateList::iterator iter = all_certs.begin(); | 103 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())) | 149 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(iter->get())) |
158 result = false; | 150 result = false; |
159 } | 151 } |
160 return result; | 152 return result; |
161 } | 153 } |
162 | 154 |
163 bool CertificateImporter::ParseServerOrCaCertificate( | 155 bool CertificateImporter::ParseServerOrCaCertificate( |
164 const std::string& cert_type, | 156 const std::string& cert_type, |
165 const std::string& guid, | 157 const std::string& guid, |
166 const base::DictionaryValue& certificate) { | 158 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; | 159 bool web_trust = false; |
175 const base::ListValue* trust_list = NULL; | 160 const base::ListValue* trust_list = NULL; |
176 if (certificate.GetList(certificate::kTrust, &trust_list)) { | 161 if (certificate.GetList(certificate::kTrust, &trust_list)) { |
177 for (size_t i = 0; i < trust_list->GetSize(); ++i) { | 162 for (size_t i = 0; i < trust_list->GetSize(); ++i) { |
178 std::string trust_type; | 163 std::string trust_type; |
179 if (!trust_list->GetString(i, &trust_type)) { | 164 if (!trust_list->GetString(i, &trust_type)) |
180 ONC_LOG_ERROR("Certificate trust is invalid"); | 165 NOTREACHED(); |
181 return false; | 166 |
182 } | |
183 if (trust_type == certificate::kWeb) { | 167 if (trust_type == certificate::kWeb) { |
184 // "Web" implies that the certificate is to be trusted for SSL | 168 // "Web" implies that the certificate is to be trusted for SSL |
185 // identification. | 169 // identification. |
186 web_trust = true; | 170 web_trust = true; |
187 } else { | 171 } else { |
188 ONC_LOG_ERROR("Certificate contains unknown trust type " + trust_type); | 172 ONC_LOG_ERROR("Certificate contains unknown trust type " + trust_type); |
189 return false; | 173 return false; |
190 } | 174 } |
191 } | 175 } |
192 } | 176 } |
193 | 177 |
194 // Web trust is only granted to certificates imported for a managed user | 178 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; | 179 LOG(WARNING) << "Web trust not granted for certificate: " << guid; |
199 web_trust = false; | 180 web_trust = false; |
200 } | 181 } |
201 | 182 |
202 std::string x509_data; | 183 std::string x509_data; |
203 if (!certificate.GetString(certificate::kX509, &x509_data) || | 184 if (!certificate.GetString(certificate::kX509, &x509_data) || |
204 x509_data.empty()) { | 185 x509_data.empty()) { |
205 ONC_LOG_ERROR( | 186 ONC_LOG_ERROR( |
206 "Certificate missing appropriate certificate data for type: " + | 187 "Certificate missing appropriate certificate data for type: " + |
207 cert_type); | 188 cert_type); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); | 346 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); |
366 SECKEY_DestroyPrivateKey(private_key); | 347 SECKEY_DestroyPrivateKey(private_key); |
367 } else { | 348 } else { |
368 ONC_LOG_WARNING("Unable to find private key for certificate."); | 349 ONC_LOG_WARNING("Unable to find private key for certificate."); |
369 } | 350 } |
370 return true; | 351 return true; |
371 } | 352 } |
372 | 353 |
373 } // namespace onc | 354 } // namespace onc |
374 } // namespace chromeos | 355 } // namespace chromeos |
OLD | NEW |