OLD | NEW |
---|---|
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> |
11 #include <Security/Security.h> | 11 #include <Security/Security.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <string> | 14 #include <string> |
15 #include <utility> | |
16 #include <vector> | |
15 | 17 |
16 #include "base/callback.h" | 18 #include "base/callback.h" |
17 #include "base/logging.h" | 19 #include "base/logging.h" |
18 #include "base/mac/mac_logging.h" | 20 #include "base/mac/mac_logging.h" |
19 #include "base/mac/scoped_cftyperef.h" | 21 #include "base/mac/scoped_cftyperef.h" |
20 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
21 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
22 #include "crypto/mac_security_services_lock.h" | 24 #include "crypto/mac_security_services_lock.h" |
23 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
24 #include "net/cert/x509_util.h" | 26 #include "net/cert/x509_util.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 } | 232 } |
231 sort(sort_begin, sort_end, x509_util::ClientCertSorter()); | 233 sort(sort_begin, sort_end, x509_util::ClientCertSorter()); |
232 } | 234 } |
233 | 235 |
234 } // namespace | 236 } // namespace |
235 | 237 |
236 ClientCertStoreMac::ClientCertStoreMac() {} | 238 ClientCertStoreMac::ClientCertStoreMac() {} |
237 | 239 |
238 ClientCertStoreMac::~ClientCertStoreMac() {} | 240 ClientCertStoreMac::~ClientCertStoreMac() {} |
239 | 241 |
242 // Given an |identity|, identifies its corresponding certificate, and either | |
243 // adds it to |regular_certs| or assigns it to |preferred_cert|, if the | |
244 // |identity| matches the |preferred_identity|. | |
245 void AddIdentity(SecIdentityRef identity, | |
246 SecIdentityRef preferred_identity, | |
247 CertificateList* regular_certs, | |
248 scoped_refptr<X509Certificate>* preferred_cert); | |
249 | |
240 void ClientCertStoreMac::GetClientCerts( | 250 void ClientCertStoreMac::GetClientCerts( |
241 const SSLCertRequestInfo& request, | 251 const SSLCertRequestInfo& request, |
242 const ClientCertListCallback& callback) { | 252 const ClientCertListCallback& callback) { |
243 std::string server_domain = request.host_and_port.host(); | 253 std::string server_domain = request.host_and_port.host(); |
244 | 254 |
245 ScopedCFTypeRef<SecIdentityRef> preferred_identity; | 255 ScopedCFTypeRef<SecIdentityRef> preferred_identity; |
246 if (!server_domain.empty()) { | 256 if (!server_domain.empty()) { |
247 // See if there's an identity preference for this domain: | 257 // See if there's an identity preference for this domain: |
248 ScopedCFTypeRef<CFStringRef> domain_str( | 258 ScopedCFTypeRef<CFStringRef> domain_str( |
249 base::SysUTF8ToCFStringRef("https://" + server_domain)); | 259 base::SysUTF8ToCFStringRef("https://" + server_domain)); |
(...skipping 27 matching lines...) Expand all Loading... | |
277 ScopedCFTypeRef<SecIdentitySearchRef> scoped_search(search); | 287 ScopedCFTypeRef<SecIdentitySearchRef> scoped_search(search); |
278 while (!err) { | 288 while (!err) { |
279 SecIdentityRef identity = NULL; | 289 SecIdentityRef identity = NULL; |
280 { | 290 { |
281 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); | 291 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); |
282 err = SecIdentitySearchCopyNext(search, &identity); | 292 err = SecIdentitySearchCopyNext(search, &identity); |
283 } | 293 } |
284 if (err) | 294 if (err) |
285 break; | 295 break; |
286 ScopedCFTypeRef<SecIdentityRef> scoped_identity(identity); | 296 ScopedCFTypeRef<SecIdentityRef> scoped_identity(identity); |
287 | 297 AddIdentity(identity, preferred_identity, ®ular_certs, &preferred_cert); |
288 SecCertificateRef cert_handle; | |
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 } | 298 } |
311 | 299 |
312 if (err != errSecItemNotFound) { | 300 if (err != errSecItemNotFound) { |
313 OSSTATUS_LOG(ERROR, err) << "SecIdentitySearch error"; | 301 OSSTATUS_LOG(ERROR, err) << "SecIdentitySearch error"; |
314 callback.Run(CertificateList()); | 302 callback.Run(CertificateList()); |
315 return; | 303 return; |
316 } | 304 } |
317 | 305 |
306 // macOS provides two ways to search for identities. SecIdentitySearchCreate() | |
awong
2017/06/07 00:35:24
macOS -> MacOS
codesearch shows it's about 8x mo
mattm
2017/06/07 01:07:56
Well, Apple did change the official name to "macOS
awong
2017/06/07 19:40:14
garagh. okay, ignore my comment then.
| |
307 // is deprecated, as it relies on CSSM_KEYUSE_SIGN (part of the deprecated | |
308 // CDSM/CSSA implementation), but is necessary to return some certificates | |
309 // that would otherwise not be returned by SecItemCopyMatching(), which is the | |
310 // non-deprecated way. However, SecIdentitySearchCreate() will not return all | |
311 // items, particularly smart-card based identities, so it's necessary to call | |
312 // both functions. | |
313 const void* keys[] = { | |
awong
2017/06/07 00:35:24
Should these really be stack allocated? Make them
agaynor
2017/06/07 21:19:47
Done.
| |
314 kSecClass, kSecMatchLimit, kSecReturnRef, kSecAttrCanSign, | |
315 }; | |
316 const void* values[] = { | |
317 kSecClassIdentity, kSecMatchLimitAll, kCFBooleanTrue, kCFBooleanTrue, | |
318 }; | |
319 ScopedCFTypeRef<CFDictionaryRef> query(CFDictionaryCreate( | |
320 kCFAllocatorDefault, keys, values, arraysize(values), | |
321 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); | |
322 ScopedCFTypeRef<CFArrayRef> result; | |
323 { | |
324 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); | |
325 err = SecItemCopyMatching( | |
326 query, reinterpret_cast<CFTypeRef*>(result.InitializeInto())); | |
327 } | |
328 if (!err) { | |
329 for (CFIndex i = 0; i < CFArrayGetCount(result); i++) { | |
330 void* item = const_cast<void*>(CFArrayGetValueAtIndex(result, i)); | |
331 AddIdentity(reinterpret_cast<SecIdentityRef>(item), preferred_identity, | |
332 ®ular_certs, &preferred_cert); | |
333 } | |
334 } | |
335 | |
318 CertificateList selected_certs; | 336 CertificateList selected_certs; |
319 GetClientCertsImpl(preferred_cert, regular_certs, request, true, | 337 GetClientCertsImpl(preferred_cert, regular_certs, request, true, |
320 &selected_certs); | 338 &selected_certs); |
321 callback.Run(std::move(selected_certs)); | 339 callback.Run(std::move(selected_certs)); |
322 } | 340 } |
323 | 341 |
342 void AddIdentity(SecIdentityRef identity, | |
343 SecIdentityRef preferred_identity, | |
344 CertificateList* regular_certs, | |
345 scoped_refptr<X509Certificate>* preferred_cert) { | |
346 OSStatus err; | |
347 ScopedCFTypeRef<SecCertificateRef> cert_handle; | |
348 err = SecIdentityCopyCertificate(identity, cert_handle.InitializeInto()); | |
349 if (err != noErr) | |
350 return; | |
351 | |
352 if (!SupportsSSLClientAuth(cert_handle)) | |
353 return; | |
354 | |
355 scoped_refptr<X509Certificate> cert( | |
356 x509_util::CreateX509CertificateFromSecCertificate( | |
357 cert_handle, std::vector<SecCertificateRef>())); | |
358 if (!cert) | |
359 return; | |
360 | |
361 if (preferred_identity && CFEqual(preferred_identity, identity)) { | |
362 // Only one certificate should match. | |
363 DCHECK(!preferred_cert->get()); | |
awong
2017/06/07 00:35:24
If 2 match in production, and this just uses the l
mattm
2017/06/07 01:07:56
It's which one will be listed first in the client
| |
364 *preferred_cert = cert; | |
365 } else { | |
366 regular_certs->push_back(cert); | |
367 } | |
368 } | |
369 | |
324 bool ClientCertStoreMac::SelectClientCertsForTesting( | 370 bool ClientCertStoreMac::SelectClientCertsForTesting( |
325 const CertificateList& input_certs, | 371 const CertificateList& input_certs, |
326 const SSLCertRequestInfo& request, | 372 const SSLCertRequestInfo& request, |
327 CertificateList* selected_certs) { | 373 CertificateList* selected_certs) { |
328 GetClientCertsImpl(NULL, input_certs, request, false, selected_certs); | 374 GetClientCertsImpl(NULL, input_certs, request, false, selected_certs); |
329 return true; | 375 return true; |
330 } | 376 } |
331 | 377 |
332 bool ClientCertStoreMac::SelectClientCertsGivenPreferredForTesting( | 378 bool ClientCertStoreMac::SelectClientCertsGivenPreferredForTesting( |
333 const scoped_refptr<X509Certificate>& preferred_cert, | 379 const scoped_refptr<X509Certificate>& preferred_cert, |
334 const CertificateList& regular_certs, | 380 const CertificateList& regular_certs, |
335 const SSLCertRequestInfo& request, | 381 const SSLCertRequestInfo& request, |
336 CertificateList* selected_certs) { | 382 CertificateList* selected_certs) { |
337 GetClientCertsImpl( | 383 GetClientCertsImpl( |
338 preferred_cert, regular_certs, request, false, selected_certs); | 384 preferred_cert, regular_certs, request, false, selected_certs); |
339 return true; | 385 return true; |
340 } | 386 } |
341 | 387 |
342 #pragma clang diagnostic pop // "-Wdeprecated-declarations" | 388 #pragma clang diagnostic pop // "-Wdeprecated-declarations" |
343 | 389 |
344 } // namespace net | 390 } // namespace net |
OLD | NEW |