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

Side by Side Diff: net/ssl/client_cert_store_mac.cc

Issue 2910893002: Improved support for loading client certificates on smart cards on macOS
Patch Set: Improved support for loading client certificates on smart cards on macOS Created 3 years, 6 months 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
« no previous file with comments | « net/ssl/client_cert_store_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "net/ssl/client_cert_store_mac.h" 5 #include "net/ssl/client_cert_store_mac.h"
6 6
7 #include <CommonCrypto/CommonDigest.h> 7 #include <CommonCrypto/CommonDigest.h>
8 #include <CoreFoundation/CFArray.h> 8 #include <CoreFoundation/CFArray.h>
9 #include <CoreServices/CoreServices.h> 9 #include <CoreServices/CoreServices.h>
10 #include <Security/SecBase.h> 10 #include <Security/SecBase.h>
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 ScopedCFTypeRef<SecIdentitySearchRef> scoped_search(search); 277 ScopedCFTypeRef<SecIdentitySearchRef> scoped_search(search);
278 while (!err) { 278 while (!err) {
279 SecIdentityRef identity = NULL; 279 SecIdentityRef identity = NULL;
280 { 280 {
281 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); 281 base::AutoLock lock(crypto::GetMacSecurityServicesLock());
282 err = SecIdentitySearchCopyNext(search, &identity); 282 err = SecIdentitySearchCopyNext(search, &identity);
283 } 283 }
284 if (err) 284 if (err)
285 break; 285 break;
286 ScopedCFTypeRef<SecIdentityRef> scoped_identity(identity); 286 ScopedCFTypeRef<SecIdentityRef> scoped_identity(identity);
287 287 AddIdentity(regular_certs, preferred_cert, preferred_identity.get(),
288 SecCertificateRef cert_handle; 288 identity);
289 err = SecIdentityCopyCertificate(identity, &cert_handle);
290 if (err != noErr)
291 continue;
292 ScopedCFTypeRef<SecCertificateRef> scoped_cert_handle(cert_handle);
293
294 if (!SupportsSSLClientAuth(cert_handle))
295 continue;
296
297 scoped_refptr<X509Certificate> cert(
298 x509_util::CreateX509CertificateFromSecCertificate(
299 cert_handle, std::vector<SecCertificateRef>()));
300 if (!cert)
301 continue;
302
303 if (preferred_identity && CFEqual(preferred_identity, identity)) {
304 // Only one certificate should match.
305 DCHECK(!preferred_cert.get());
306 preferred_cert = cert;
307 } else {
308 regular_certs.push_back(cert);
309 }
310 } 289 }
311 290
312 if (err != errSecItemNotFound) { 291 if (err != errSecItemNotFound) {
313 OSSTATUS_LOG(ERROR, err) << "SecIdentitySearch error"; 292 OSSTATUS_LOG(ERROR, err) << "SecIdentitySearch error";
314 callback.Run(CertificateList()); 293 callback.Run(CertificateList());
315 return; 294 return;
316 } 295 }
317 296
297 // macOS provides two ways to search for identities. SecIdentitySearchCreate()
298 // is deprecated, as it relies on CSSM_KEYUSE_SIGN (part of the deprecated
299 // CDSM/CSSA implementation), but is necessary to return some certificates
300 // that would otherwise not be returned by SecItemCopyMatching(), which is the
301 // non-deprecated way. However, SecIdentitySearchCreate() will not return all
302 // items, particularly smart-card based identities, so it's necessary to call
303 // both functions.
304 const void* keys[] = {
305 kSecClass, kSecMatchLimit, kSecReturnRef, kSecAttrCanSign,
306 };
307 const void* values[] = {
308 kSecClassIdentity, kSecMatchLimitAll, kCFBooleanTrue, kCFBooleanTrue,
309 };
310 ScopedCFTypeRef<CFDictionaryRef> query(CFDictionaryCreate(
311 kCFAllocatorDefault, keys, values, sizeof(values) / sizeof(values[0]),
mattm 2017/06/05 23:34:28 arraysize(values)
agaynor 2017/06/06 22:38:49 Done.
312 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
313 ScopedCFTypeRef<CFArrayRef> result;
314 err = SecItemCopyMatching(
mattm 2017/06/05 23:34:27 put this in a base::AutoLock lock(crypto::GetMacSe
agaynor 2017/06/06 22:38:49 Done.
315 query, reinterpret_cast<CFTypeRef*>(result.InitializeInto()));
316 if (!err) {
317 for (CFIndex i = 0; i < CFArrayGetCount(result); i++) {
318 CFTypeRef item = CFArrayGetValueAtIndex(result, i);
319 AddIdentity(regular_certs, preferred_cert, preferred_identity,
320 (SecIdentityRef)item);
mattm 2017/06/05 23:34:27 C style casts aren't allowed, should use reinterpr
agaynor 2017/06/06 22:38:49 Done. Although I had to also use a const_cast, whi
321 }
322 }
323
318 CertificateList selected_certs; 324 CertificateList selected_certs;
319 GetClientCertsImpl(preferred_cert, regular_certs, request, true, 325 GetClientCertsImpl(preferred_cert, regular_certs, request, true,
320 &selected_certs); 326 &selected_certs);
321 callback.Run(std::move(selected_certs)); 327 callback.Run(std::move(selected_certs));
322 } 328 }
323 329
330 void ClientCertStoreMac::AddIdentity(
mattm 2017/06/05 23:34:28 It appears this could be an anonymous function ins
agaynor 2017/06/06 22:38:49 Done.
mattm 2017/06/07 00:24:07 Also move it into the "namespace {}" block above.
agaynor 2017/06/07 21:19:47 Done.
331 CertificateList& regular_certs,
332 scoped_refptr<X509Certificate>& preferred_cert,
mattm 2017/06/05 23:34:27 non-const reference parameters aren't allowed, the
agaynor 2017/06/06 22:38:49 Done.
333 SecIdentityRef preferred_identity,
334 SecIdentityRef identity) {
mattm 2017/06/05 23:34:27 Input parameters should come before output params.
agaynor 2017/06/06 22:38:49 Done.
335 OSStatus err;
336 ScopedCFTypeRef<SecCertificateRef> cert_handle;
337 err = SecIdentityCopyCertificate(identity, cert_handle.InitializeInto());
338 if (err != noErr)
339 return;
340
341 if (!SupportsSSLClientAuth(cert_handle))
342 return;
343
344 scoped_refptr<X509Certificate> cert(
345 x509_util::CreateX509CertificateFromSecCertificate(
346 cert_handle, std::vector<SecCertificateRef>()));
347 if (!cert)
348 return;
349
350 if (preferred_identity && CFEqual(preferred_identity, identity)) {
351 // Only one certificate should match.
352 DCHECK(!preferred_cert.get());
353 preferred_cert = cert;
354 } else {
355 regular_certs.push_back(cert);
356 }
357 }
358
324 bool ClientCertStoreMac::SelectClientCertsForTesting( 359 bool ClientCertStoreMac::SelectClientCertsForTesting(
325 const CertificateList& input_certs, 360 const CertificateList& input_certs,
326 const SSLCertRequestInfo& request, 361 const SSLCertRequestInfo& request,
327 CertificateList* selected_certs) { 362 CertificateList* selected_certs) {
328 GetClientCertsImpl(NULL, input_certs, request, false, selected_certs); 363 GetClientCertsImpl(NULL, input_certs, request, false, selected_certs);
329 return true; 364 return true;
330 } 365 }
331 366
332 bool ClientCertStoreMac::SelectClientCertsGivenPreferredForTesting( 367 bool ClientCertStoreMac::SelectClientCertsGivenPreferredForTesting(
333 const scoped_refptr<X509Certificate>& preferred_cert, 368 const scoped_refptr<X509Certificate>& preferred_cert,
334 const CertificateList& regular_certs, 369 const CertificateList& regular_certs,
335 const SSLCertRequestInfo& request, 370 const SSLCertRequestInfo& request,
336 CertificateList* selected_certs) { 371 CertificateList* selected_certs) {
337 GetClientCertsImpl( 372 GetClientCertsImpl(
338 preferred_cert, regular_certs, request, false, selected_certs); 373 preferred_cert, regular_certs, request, false, selected_certs);
339 return true; 374 return true;
340 } 375 }
341 376
342 #pragma clang diagnostic pop // "-Wdeprecated-declarations" 377 #pragma clang diagnostic pop // "-Wdeprecated-declarations"
343 378
344 } // namespace net 379 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/client_cert_store_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698