Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: chromeos/network/onc/onc_certificate_importer.cc

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

Powered by Google App Engine
This is Rietveld 408576698