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 "chrome/browser/chromeos/network_settings/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 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "chrome/browser/chromeos/cros/onc_constants.h" | 13 #include "base/values.h" |
14 #include "grit/generated_resources.h" | 14 #include "chromeos/network/network_event_log.h" |
15 #include "chromeos/network/onc/onc_constants.h" | |
15 #include "net/base/crypto_module.h" | 16 #include "net/base/crypto_module.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "net/base/nss_cert_database.h" | 18 #include "net/base/nss_cert_database.h" |
18 #include "net/base/pem_tokenizer.h" | 19 #include "net/base/pem_tokenizer.h" |
19 #include "net/base/x509_certificate.h" | 20 #include "net/base/x509_certificate.h" |
20 #include "ui/base/l10n/l10n_util.h" | |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // The PEM block header used for DER certificates | 24 // The PEM block header used for DER certificates |
25 const char kCertificateHeader[] = "CERTIFICATE"; | 25 const char kCertificateHeader[] = "CERTIFICATE"; |
26 // This is an older PEM marker for DER certificates. | 26 // This is an older PEM marker for DER certificates. |
27 const char kX509CertificateHeader[] = "X509 CERTIFICATE"; | 27 const char kX509CertificateHeader[] = "X509 CERTIFICATE"; |
28 | 28 |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 namespace chromeos { | 31 namespace chromeos { |
32 namespace onc { | 32 namespace onc { |
33 | 33 |
34 CertificateImporter::CertificateImporter( | 34 CertificateImporter::CertificateImporter( |
35 NetworkUIData::ONCSource onc_source, | 35 ONCSource onc_source, |
36 bool allow_web_trust_from_policy) | 36 bool allow_web_trust_from_policy) |
37 : onc_source_(onc_source), | 37 : onc_source_(onc_source), |
38 allow_web_trust_from_policy_(allow_web_trust_from_policy) { | 38 allow_web_trust_from_policy_(allow_web_trust_from_policy) { |
39 } | 39 } |
40 | 40 |
41 bool CertificateImporter::ParseAndStoreCertificates( | 41 CertificateImporter::ParseResult CertificateImporter::ParseAndStoreCertificates( |
42 const base::ListValue& certificates, std::string* error) { | 42 const base::ListValue& certificates) { |
43 error_.clear(); | |
44 for (size_t i = 0; i < certificates.GetSize(); ++i) { | 43 for (size_t i = 0; i < certificates.GetSize(); ++i) { |
45 const base::DictionaryValue* certificate = NULL; | 44 const base::DictionaryValue* certificate = NULL; |
46 if (!certificates.GetDictionary(i, &certificate)) { | 45 if (!certificates.GetDictionary(i, &certificate)) { |
47 if (error) { | 46 NET_LOG_ERROR("Certificate data malformed"); |
48 *error = l10n_util::GetStringUTF8( | 47 return i > 0 ? IMPORT_INCOMPLETE : IMPORT_FAILED; |
49 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MALFORMED); | |
50 } | |
51 return false; | |
52 } | 48 } |
53 | 49 |
54 if (VLOG_IS_ON(2)) | 50 if (VLOG_IS_ON(2)) |
55 VLOG(2) << "Parsing certificate at index " << i << ": " << *certificate; | 51 VLOG(2) << "Parsing certificate at index " << i << ": " << *certificate; |
56 | 52 |
57 if (ParseAndStoreCertificate(*certificate)) { | 53 if (!ParseAndStoreCertificate(*certificate)) { |
58 VLOG(2) << "Successfully imported certificate at index " << i; | 54 NET_LOG_ERROR( |
59 continue; | 55 base::StringPrintf("Cannot parse certificate at index %zu", i)); |
56 return i > 0 ? IMPORT_INCOMPLETE : IMPORT_FAILED; | |
60 } | 57 } |
61 | 58 |
62 LOG(WARNING) << "Cannot parse certificate at index " << i << ": " << error_; | 59 VLOG(2) << "Successfully imported certificate at index " << i; |
63 if (error) | |
64 *error = error_; | |
65 return false; | |
66 } | 60 } |
67 return true; | 61 return IMPORT_OK; |
68 } | 62 } |
69 | 63 |
70 bool CertificateImporter::ParseAndStoreCertificate( | 64 bool CertificateImporter::ParseAndStoreCertificate( |
71 const base::DictionaryValue& certificate) { | 65 const base::DictionaryValue& certificate) { |
72 | |
73 // Get out the attributes of the given certificate. | 66 // Get out the attributes of the given certificate. |
74 std::string guid; | 67 std::string guid; |
75 if (!certificate.GetString(kGUID, &guid) || guid.empty()) { | 68 if (!certificate.GetString(kGUID, &guid) || guid.empty()) { |
76 LOG(WARNING) << "Certificate missing GUID identifier"; | 69 NET_LOG_ERROR("Certificate missing GUID identifier"); |
77 error_ = l10n_util::GetStringUTF8( | |
78 IDS_NETWORK_CONFIG_ERROR_CERT_GUID_MISSING); | |
79 return false; | 70 return false; |
80 } | 71 } |
81 | 72 |
82 bool remove = false; | 73 bool remove = false; |
83 if (certificate.GetBoolean(kRemove, &remove) && remove) { | 74 if (certificate.GetBoolean(kRemove, &remove) && remove) { |
84 if (!DeleteCertAndKeyByNickname(guid)) { | 75 if (!DeleteCertAndKeyByNickname(guid)) { |
85 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_CERT_DELETE); | 76 NET_LOG_WARNING("Unable to delete certificate"); |
pneubeck (no reviews)
2012/12/10 09:33:10
to match the "return false", this should perhaps b
| |
86 return false; | 77 return false; |
87 } else { | 78 } else { |
88 return true; | 79 return true; |
89 } | 80 } |
90 } | 81 } |
91 | 82 |
92 // Not removing, so let's get the data we need to add this certificate. | 83 // Not removing, so let's get the data we need to add this certificate. |
93 std::string cert_type; | 84 std::string cert_type; |
94 certificate.GetString(certificate::kType, &cert_type); | 85 certificate.GetString(certificate::kType, &cert_type); |
95 if (cert_type == certificate::kServer || cert_type == certificate::kAuthority) | 86 if (cert_type == certificate::kServer || cert_type == certificate::kAuthority) |
96 return ParseServerOrCaCertificate(cert_type, guid, certificate); | 87 return ParseServerOrCaCertificate(cert_type, guid, certificate); |
97 | 88 |
98 if (cert_type == certificate::kClient) | 89 if (cert_type == certificate::kClient) |
99 return ParseClientCertificate(guid, certificate); | 90 return ParseClientCertificate(guid, certificate); |
100 | 91 |
101 LOG(WARNING) << "ONC File: certificate of unknown type: " << cert_type; | 92 NET_LOG_ERROR("Certificate of unknown type: " + cert_type); |
102 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_CERT_TYPE_MISSING); | |
103 return false; | 93 return false; |
104 } | 94 } |
105 | 95 |
106 // static | 96 // static |
107 void CertificateImporter::ListCertsWithNickname(const std::string& label, | 97 void CertificateImporter::ListCertsWithNickname(const std::string& label, |
108 net::CertificateList* result) { | 98 net::CertificateList* result) { |
109 net::CertificateList all_certs; | 99 net::CertificateList all_certs; |
110 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); | 100 net::NSSCertDatabase::GetInstance()->ListCerts(&all_certs); |
111 result->clear(); | 101 result->clear(); |
112 for (net::CertificateList::iterator iter = all_certs.begin(); | 102 for (net::CertificateList::iterator iter = all_certs.begin(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 result = false; | 149 result = false; |
160 } | 150 } |
161 return result; | 151 return result; |
162 } | 152 } |
163 | 153 |
164 bool CertificateImporter::ParseServerOrCaCertificate( | 154 bool CertificateImporter::ParseServerOrCaCertificate( |
165 const std::string& cert_type, | 155 const std::string& cert_type, |
166 const std::string& guid, | 156 const std::string& guid, |
167 const base::DictionaryValue& certificate) { | 157 const base::DictionaryValue& certificate) { |
168 // Device policy can't import certificates. | 158 // Device policy can't import certificates. |
169 if (onc_source_ == NetworkUIData::ONC_SOURCE_DEVICE_POLICY) { | 159 if (onc_source_ == ONC_SOURCE_DEVICE_POLICY) { |
170 LOG(WARNING) << "Refusing to import certificate from device policy"; | 160 // This isn't a parsing error. |
171 // This isn't a parsing error, so just return NULL here. | 161 NET_LOG_WARNING("Refusing to import certificate from device policy."); |
172 return true; | 162 return true; |
173 } | 163 } |
174 | 164 |
175 bool web_trust = false; | 165 bool web_trust = false; |
176 const base::ListValue* trust_list = NULL; | 166 const base::ListValue* trust_list = NULL; |
177 if (certificate.GetList(certificate::kTrust, &trust_list)) { | 167 if (certificate.GetList(certificate::kTrust, &trust_list)) { |
178 for (size_t i = 0; i < trust_list->GetSize(); ++i) { | 168 for (size_t i = 0; i < trust_list->GetSize(); ++i) { |
179 std::string trust_type; | 169 std::string trust_type; |
180 if (!trust_list->GetString(i, &trust_type)) { | 170 if (!trust_list->GetString(i, &trust_type)) { |
181 LOG(WARNING) << "ONC File: certificate trust is invalid"; | 171 NET_LOG_ERROR("Certificate trust is invalid"); |
182 error_ = l10n_util::GetStringUTF8( | |
183 IDS_NETWORK_CONFIG_ERROR_CERT_TRUST_INVALID); | |
184 return false; | 172 return false; |
185 } | 173 } |
186 if (trust_type == certificate::kWeb) { | 174 if (trust_type == certificate::kWeb) { |
187 // "Web" implies that the certificate is to be trusted for SSL | 175 // "Web" implies that the certificate is to be trusted for SSL |
188 // identification. | 176 // identification. |
189 web_trust = true; | 177 web_trust = true; |
190 } else { | 178 } else { |
191 LOG(WARNING) << "ONC File: certificate contains unknown " | 179 NET_LOG_ERROR("Certificate contains unknown trust type " + trust_type); |
192 << "trust type: " << trust_type; | |
193 error_ = l10n_util::GetStringUTF8( | |
194 IDS_NETWORK_CONFIG_ERROR_CERT_TRUST_UNKNOWN); | |
195 return false; | 180 return false; |
196 } | 181 } |
197 } | 182 } |
198 } | 183 } |
199 | 184 |
200 // Web trust is only granted to certificates imported for a managed user | 185 // Web trust is only granted to certificates imported for a managed user |
201 // on a managed device. | 186 // on a managed device. |
202 if (onc_source_ == NetworkUIData::ONC_SOURCE_USER_POLICY && | 187 if (onc_source_ == ONC_SOURCE_USER_POLICY && |
203 web_trust && !allow_web_trust_from_policy_) { | 188 web_trust && !allow_web_trust_from_policy_) { |
204 LOG(WARNING) << "Web trust not granted for certificate: " << guid; | 189 LOG(WARNING) << "Web trust not granted for certificate: " << guid; |
pneubeck (no reviews)
2012/12/10 09:33:10
NET_LOG_
| |
205 web_trust = false; | 190 web_trust = false; |
206 } | 191 } |
207 | 192 |
208 std::string x509_data; | 193 std::string x509_data; |
209 if (!certificate.GetString(certificate::kX509, &x509_data) || | 194 if (!certificate.GetString(certificate::kX509, &x509_data) || |
210 x509_data.empty()) { | 195 x509_data.empty()) { |
211 LOG(WARNING) << "ONC File: certificate missing appropriate " | 196 NET_LOG_ERROR( |
212 << "certificate data for type: " << cert_type; | 197 "Certificate missing appropriate certificate data for type: " + |
213 error_ = l10n_util::GetStringUTF8( | 198 cert_type); |
214 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MISSING); | |
215 return false; | 199 return false; |
216 } | 200 } |
217 | 201 |
218 // Parse PEM certificate, and get the decoded data for use in creating | 202 // Parse PEM certificate, and get the decoded data for use in creating |
219 // certificate below. | 203 // certificate below. |
220 std::vector<std::string> pem_headers; | 204 std::vector<std::string> pem_headers; |
221 pem_headers.push_back(kCertificateHeader); | 205 pem_headers.push_back(kCertificateHeader); |
222 pem_headers.push_back(kX509CertificateHeader); | 206 pem_headers.push_back(kX509CertificateHeader); |
223 | 207 |
224 net::PEMTokenizer pem_tokenizer(x509_data, pem_headers); | 208 net::PEMTokenizer pem_tokenizer(x509_data, pem_headers); |
225 std::string decoded_x509; | 209 std::string decoded_x509; |
226 if (!pem_tokenizer.GetNext()) { | 210 if (!pem_tokenizer.GetNext()) { |
227 // If we failed to read the data as a PEM file, then let's just try plain | 211 // If we failed to read the data as a PEM file, then let's just try plain |
228 // base64 decode: some versions of Spigots didn't apply the PEM marker | 212 // base64 decode: some versions of Spigots didn't apply the PEM marker |
229 // strings. For this to work, there has to be no white space, and it has to | 213 // strings. For this to work, there has to be no white space, and it has to |
230 // only contain the base64-encoded data. | 214 // only contain the base64-encoded data. |
231 if (!base::Base64Decode(x509_data, &decoded_x509)) { | 215 if (!base::Base64Decode(x509_data, &decoded_x509)) { |
232 LOG(WARNING) << "Unable to base64 decode X509 data: \"" | 216 NET_LOG_ERROR("Unable to base64 decode X509 data: " + x509_data); |
233 << x509_data << "\"."; | |
234 error_ = l10n_util::GetStringUTF8( | |
235 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MALFORMED); | |
236 return false; | 217 return false; |
237 } | 218 } |
238 } else { | 219 } else { |
239 decoded_x509 = pem_tokenizer.data(); | 220 decoded_x509 = pem_tokenizer.data(); |
240 } | 221 } |
241 | 222 |
242 scoped_refptr<net::X509Certificate> x509_cert = | 223 scoped_refptr<net::X509Certificate> x509_cert = |
243 net::X509Certificate::CreateFromBytesWithNickname( | 224 net::X509Certificate::CreateFromBytesWithNickname( |
244 decoded_x509.data(), | 225 decoded_x509.data(), |
245 decoded_x509.size(), | 226 decoded_x509.size(), |
246 guid.c_str()); | 227 guid.c_str()); |
247 if (!x509_cert.get()) { | 228 if (!x509_cert.get()) { |
248 LOG(WARNING) << "Unable to create X509 certificate from bytes."; | 229 NET_LOG_ERROR("Unable to create X509 certificate from bytes."); |
249 error_ = l10n_util::GetStringUTF8( | |
250 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MALFORMED); | |
251 return false; | 230 return false; |
252 } | 231 } |
253 | 232 |
254 // Due to a mismatch regarding cert identity between NSS (cert identity is | 233 // Due to a mismatch regarding cert identity between NSS (cert identity is |
255 // determined by the raw bytes) and ONC (cert identity is determined by | 234 // determined by the raw bytes) and ONC (cert identity is determined by |
256 // GUIDs), we have to special-case a number of situations here: | 235 // GUIDs), we have to special-case a number of situations here: |
257 // | 236 // |
258 // a) The cert bits we're trying to insert are already present in the NSS cert | 237 // a) The cert bits we're trying to insert are already present in the NSS cert |
259 // store. This is indicated by the isperm bit in CERTCertificateStr. Since | 238 // store. This is indicated by the isperm bit in CERTCertificateStr. Since |
260 // we might have to update the nick name, we just delete the existing cert | 239 // we might have to update the nick name, we just delete the existing cert |
261 // and reimport the cert bits. | 240 // and reimport the cert bits. |
262 // b) NSS gives us an actual temporary certificate. In this case, there is no | 241 // b) NSS gives us an actual temporary certificate. In this case, there is no |
263 // identical certificate known to NSS, so we can safely import the | 242 // identical certificate known to NSS, so we can safely import the |
264 // certificate. The GUID being imported may still be on a different | 243 // certificate. The GUID being imported may still be on a different |
265 // certificate, and we could jump through hoops to reimport the existing | 244 // certificate, and we could jump through hoops to reimport the existing |
266 // certificate with a different nickname. However, that would mean lots of | 245 // certificate with a different nickname. However, that would mean lots of |
267 // effort for a case that's pretty much illegal (reusing GUIDs contradicts | 246 // effort for a case that's pretty much illegal (reusing GUIDs contradicts |
268 // the intention of GUIDs), so we just report an error. | 247 // the intention of GUIDs), so we just report an error. |
269 // | 248 // |
270 // TODO(mnissler, gspencer): We should probably switch to a mode where we | 249 // TODO(mnissler, gspencer): We should probably switch to a mode where we |
271 // keep our own database for mapping GUIDs to certs in order to enable several | 250 // keep our own database for mapping GUIDs to certs in order to enable several |
272 // GUIDs to map to the same cert. See http://crosbug.com/26073. | 251 // GUIDs to map to the same cert. See http://crosbug.com/26073. |
273 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); | 252 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); |
274 if (x509_cert->os_cert_handle()->isperm) { | 253 if (x509_cert->os_cert_handle()->isperm) { |
275 if (!cert_database->DeleteCertAndKey(x509_cert.get())) { | 254 if (!cert_database->DeleteCertAndKey(x509_cert.get())) { |
276 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_CERT_DELETE); | 255 NET_LOG_ERROR("Unable to delete X509 certificate."); |
277 return false; | 256 return false; |
278 } | 257 } |
279 | 258 |
280 // Reload the cert here to get an actual temporary cert instance. | 259 // Reload the cert here to get an actual temporary cert instance. |
281 x509_cert = | 260 x509_cert = |
282 net::X509Certificate::CreateFromBytesWithNickname( | 261 net::X509Certificate::CreateFromBytesWithNickname( |
283 decoded_x509.data(), | 262 decoded_x509.data(), |
284 decoded_x509.size(), | 263 decoded_x509.size(), |
285 guid.c_str()); | 264 guid.c_str()); |
286 if (!x509_cert.get()) { | 265 if (!x509_cert.get()) { |
287 LOG(WARNING) << "Unable to create X509 certificate from bytes."; | 266 NET_LOG_ERROR("Unable to create X509 certificate from bytes."); |
288 error_ = l10n_util::GetStringUTF8( | |
289 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MALFORMED); | |
290 return false; | 267 return false; |
291 } | 268 } |
292 DCHECK(!x509_cert->os_cert_handle()->isperm); | 269 DCHECK(!x509_cert->os_cert_handle()->isperm); |
293 DCHECK(x509_cert->os_cert_handle()->istemp); | 270 DCHECK(x509_cert->os_cert_handle()->istemp); |
294 } | 271 } |
295 | 272 |
296 // Make sure the GUID is not already taken. Note that for the reimport case we | 273 // Make sure the GUID is not already taken. Note that for the reimport case we |
297 // have removed the existing cert above. | 274 // have removed the existing cert above. |
298 net::CertificateList certs; | 275 net::CertificateList certs; |
299 ListCertsWithNickname(guid, &certs); | 276 ListCertsWithNickname(guid, &certs); |
300 if (!certs.empty()) { | 277 if (!certs.empty()) { |
301 LOG(WARNING) << "Cert GUID is already in use: " << guid; | 278 NET_LOG_ERROR("Certificate GUID is already in use: " + guid); |
302 error_ = l10n_util::GetStringUTF8( | |
303 IDS_NETWORK_CONFIG_ERROR_CERT_GUID_COLLISION); | |
304 return false; | 279 return false; |
305 } | 280 } |
306 | 281 |
307 net::CertificateList cert_list; | 282 net::CertificateList cert_list; |
308 cert_list.push_back(x509_cert); | 283 cert_list.push_back(x509_cert); |
309 net::NSSCertDatabase::ImportCertFailureList failures; | 284 net::NSSCertDatabase::ImportCertFailureList failures; |
310 bool success = false; | 285 bool success = false; |
311 net::NSSCertDatabase::TrustBits trust = web_trust ? | 286 net::NSSCertDatabase::TrustBits trust = web_trust ? |
312 net::NSSCertDatabase::TRUSTED_SSL : | 287 net::NSSCertDatabase::TRUSTED_SSL : |
313 net::NSSCertDatabase::TRUST_DEFAULT; | 288 net::NSSCertDatabase::TRUST_DEFAULT; |
314 if (cert_type == certificate::kServer) | 289 if (cert_type == certificate::kServer) |
315 success = cert_database->ImportServerCert(cert_list, trust, &failures); | 290 success = cert_database->ImportServerCert(cert_list, trust, &failures); |
316 else // Authority cert | 291 else // Authority cert |
317 success = cert_database->ImportCACerts(cert_list, trust, &failures); | 292 success = cert_database->ImportCACerts(cert_list, trust, &failures); |
318 | 293 |
319 if (!failures.empty()) { | 294 if (!failures.empty()) { |
320 LOG(WARNING) << "ONC File: Error (" | 295 NET_LOG_ERROR("Error (" + net::ErrorToString(failures[0].net_error) + |
321 << net::ErrorToString(failures[0].net_error) | 296 ") importing " + cert_type + " certificate"); |
322 << ") importing " << cert_type << " certificate"; | |
323 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_CERT_IMPORT); | |
324 return false; | 297 return false; |
325 } | 298 } |
326 if (!success) { | 299 if (!success) { |
327 LOG(WARNING) << "ONC File: Unknown error importing " << cert_type | 300 NET_LOG_ERROR("Unknown error importing " + cert_type + " certificate."); |
328 << " certificate"; | |
329 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_UNKNOWN); | |
330 return false; | 301 return false; |
331 } | 302 } |
332 | 303 |
333 return true; | 304 return true; |
334 } | 305 } |
335 | 306 |
336 bool CertificateImporter::ParseClientCertificate( | 307 bool CertificateImporter::ParseClientCertificate( |
337 const std::string& guid, | 308 const std::string& guid, |
338 const base::DictionaryValue& certificate) { | 309 const base::DictionaryValue& certificate) { |
339 std::string pkcs12_data; | 310 std::string pkcs12_data; |
340 if (!certificate.GetString(certificate::kPKCS12, &pkcs12_data) || | 311 if (!certificate.GetString(certificate::kPKCS12, &pkcs12_data) || |
341 pkcs12_data.empty()) { | 312 pkcs12_data.empty()) { |
342 LOG(WARNING) << "ONC File: PKCS12 data is missing for Client " | 313 NET_LOG_ERROR("PKCS12 data is missing for client certificate."); |
343 << "certificate"; | |
344 error_ = l10n_util::GetStringUTF8( | |
345 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MISSING); | |
346 return false; | 314 return false; |
347 } | 315 } |
348 | 316 |
349 std::string decoded_pkcs12; | 317 std::string decoded_pkcs12; |
350 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) { | 318 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) { |
351 LOG(WARNING) << "Unable to base64 decode PKCS#12 data: \"" | 319 NET_LOG_ERROR( |
352 << pkcs12_data << "\"."; | 320 "Unable to base64 decode PKCS#12 data: \"" + pkcs12_data + "\"."); |
353 error_ = l10n_util::GetStringUTF8( | |
354 IDS_NETWORK_CONFIG_ERROR_CERT_DATA_MALFORMED); | |
355 return false; | 321 return false; |
356 } | 322 } |
357 | 323 |
358 // Since this has a private key, always use the private module. | 324 // Since this has a private key, always use the private module. |
359 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); | 325 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); |
360 scoped_refptr<net::CryptoModule> module(cert_database->GetPrivateModule()); | 326 scoped_refptr<net::CryptoModule> module(cert_database->GetPrivateModule()); |
361 net::CertificateList imported_certs; | 327 net::CertificateList imported_certs; |
362 | 328 |
363 int result = cert_database->ImportFromPKCS12( | 329 int import_result = cert_database->ImportFromPKCS12( |
364 module.get(), decoded_pkcs12, string16(), false, &imported_certs); | 330 module.get(), decoded_pkcs12, string16(), false, &imported_certs); |
365 if (result != net::OK) { | 331 if (import_result != net::OK) { |
366 LOG(WARNING) << "ONC File: Unable to import Client certificate" | 332 NET_LOG_ERROR("Unable to import client certificate (error " + |
367 << " (error " << net::ErrorToString(result) << ")."; | 333 net::ErrorToString(import_result) + ")."); |
368 error_ = l10n_util::GetStringUTF8(IDS_NETWORK_CONFIG_ERROR_CERT_IMPORT); | |
369 return false; | 334 return false; |
370 } | 335 } |
371 | 336 |
372 if (imported_certs.size() == 0) { | 337 if (imported_certs.size() == 0) { |
373 LOG(WARNING) << "ONC File: PKCS12 data contains no importable certificates"; | 338 NET_LOG_WARNING("PKCS12 data contains no importable certificates."); |
374 return true; | 339 return true; |
375 } | 340 } |
376 | 341 |
377 if (imported_certs.size() != 1) { | 342 if (imported_certs.size() != 1) { |
378 LOG(WARNING) << "ONC File: PKCS12 data contains more than one certificate." | 343 NET_LOG_WARNING("ONC File: PKCS12 data contains more than one certificate. " |
379 << "Only the first one will be imported."; | 344 "Only the first one will be imported."); |
380 } | 345 } |
381 | 346 |
382 scoped_refptr<net::X509Certificate> cert_result = imported_certs[0]; | 347 scoped_refptr<net::X509Certificate> cert_result = imported_certs[0]; |
383 | 348 |
384 // Find the private key associated with this certificate, and set the | 349 // Find the private key associated with this certificate, and set the |
385 // nickname on it. | 350 // nickname on it. |
386 SECKEYPrivateKey* private_key = PK11_FindPrivateKeyFromCert( | 351 SECKEYPrivateKey* private_key = PK11_FindPrivateKeyFromCert( |
387 cert_result->os_cert_handle()->slot, | 352 cert_result->os_cert_handle()->slot, |
388 cert_result->os_cert_handle(), | 353 cert_result->os_cert_handle(), |
389 NULL); // wincx | 354 NULL); // wincx |
390 if (private_key) { | 355 if (private_key) { |
391 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); | 356 PK11_SetPrivateKeyNickname(private_key, const_cast<char*>(guid.c_str())); |
392 SECKEY_DestroyPrivateKey(private_key); | 357 SECKEY_DestroyPrivateKey(private_key); |
393 } else { | 358 } else { |
394 LOG(WARNING) << "ONC File: Unable to find private key for cert"; | 359 NET_LOG_WARNING("Unable to find private key for certificate."); |
395 } | 360 } |
396 return true; | 361 return true; |
397 } | 362 } |
398 | 363 |
399 } // chromeos | 364 } // chromeos |
400 } // onc | 365 } // onc |
OLD | NEW |