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_chromeos.h" | 5 #include "net/ssl/client_cert_store_chromeos.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <algorithm> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "crypto/nss_crypto_module_delegate.h" | 11 #include "base/bind_helpers.h" |
11 #include "crypto/nss_util_internal.h" | 12 #include "base/callback.h" |
12 | 13 |
13 namespace net { | 14 namespace net { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 typedef base::Callback<void(crypto::ScopedPK11Slot system_slot, | 18 class CertNotAllowedPredicate { |
18 crypto::ScopedPK11Slot private_slot)> | 19 public: |
19 GetSystemAndPrivateSlotCallback; | 20 explicit CertNotAllowedPredicate( |
| 21 const ClientCertStoreChromeOS::CertFilter& filter) |
| 22 : filter_(filter) {} |
| 23 bool operator()(const scoped_refptr<X509Certificate>& cert) const { |
| 24 return !filter_.IsCertAllowed(cert); |
| 25 } |
20 | 26 |
21 // Gets the private slot for the user with the username hash |username_hash| and | 27 private: |
22 // calls |callback| with both |system_slot| and the obtained private slot. | 28 const ClientCertStoreChromeOS::CertFilter& filter_; |
23 void GetPrivateSlotAndCallBack(const std::string& username_hash, | 29 }; |
24 const GetSystemAndPrivateSlotCallback& callback, | |
25 crypto::ScopedPK11Slot system_slot) { | |
26 base::Callback<void(crypto::ScopedPK11Slot)> wrapped_callback = | |
27 base::Bind(callback, base::Passed(&system_slot)); | |
28 | |
29 crypto::ScopedPK11Slot slot( | |
30 crypto::GetPrivateSlotForChromeOSUser(username_hash, wrapped_callback)); | |
31 if (slot) | |
32 wrapped_callback.Run(slot.Pass()); | |
33 } | |
34 | |
35 // Gets the system slot, then the private slot for the user with the username | |
36 // hash |username_hash|, and finally calls |callback| with both slots. | |
37 void GetSystemAndPrivateSlot(const std::string& username_hash, | |
38 const GetSystemAndPrivateSlotCallback& callback) { | |
39 crypto::ScopedPK11Slot system_slot(crypto::GetSystemNSSKeySlot( | |
40 base::Bind(&GetPrivateSlotAndCallBack, username_hash, callback))); | |
41 if (system_slot) | |
42 GetPrivateSlotAndCallBack(username_hash, callback, system_slot.Pass()); | |
43 } | |
44 | 30 |
45 } // namespace | 31 } // namespace |
46 | 32 |
47 ClientCertStoreChromeOS::ClientCertStoreChromeOS( | 33 ClientCertStoreChromeOS::ClientCertStoreChromeOS( |
48 bool use_system_slot, | 34 scoped_ptr<CertFilter> cert_filter, |
49 const std::string& username_hash, | |
50 const PasswordDelegateFactory& password_delegate_factory) | 35 const PasswordDelegateFactory& password_delegate_factory) |
51 : ClientCertStoreNSS(password_delegate_factory), | 36 : ClientCertStoreNSS(password_delegate_factory), |
52 use_system_slot_(use_system_slot), | 37 cert_filter_(cert_filter.Pass()) { |
53 username_hash_(username_hash) { | |
54 } | 38 } |
55 | 39 |
56 ClientCertStoreChromeOS::~ClientCertStoreChromeOS() {} | 40 ClientCertStoreChromeOS::~ClientCertStoreChromeOS() {} |
57 | 41 |
58 void ClientCertStoreChromeOS::GetClientCerts( | 42 void ClientCertStoreChromeOS::GetClientCerts( |
59 const SSLCertRequestInfo& cert_request_info, | 43 const SSLCertRequestInfo& cert_request_info, |
60 CertificateList* selected_certs, | 44 CertificateList* selected_certs, |
61 const base::Closure& callback) { | 45 const base::Closure& callback) { |
62 GetSystemAndPrivateSlotCallback bound_callback = | 46 base::Closure bound_callback = |
63 base::Bind(&ClientCertStoreChromeOS::DidGetSystemAndPrivateSlot, | 47 base::Bind(&ClientCertStoreChromeOS::CertFilterInitialized, |
64 // Caller is responsible for keeping the ClientCertStore alive | 48 // Caller is responsible for keeping the ClientCertStore alive |
65 // until the callback is run. | 49 // until the callback is run. |
66 base::Unretained(this), | 50 base::Unretained(this), |
67 &cert_request_info, | 51 &cert_request_info, |
68 selected_certs, | 52 selected_certs, |
69 callback); | 53 callback); |
70 | 54 |
71 if (use_system_slot_) { | 55 if (cert_filter_->Init(bound_callback)) |
72 GetSystemAndPrivateSlot(username_hash_, bound_callback); | 56 bound_callback.Run(); |
73 } else { | |
74 // Skip getting the system slot. | |
75 GetPrivateSlotAndCallBack( | |
76 username_hash_, bound_callback, crypto::ScopedPK11Slot()); | |
77 } | |
78 } | 57 } |
79 | 58 |
80 void ClientCertStoreChromeOS::GetClientCertsImpl( | 59 void ClientCertStoreChromeOS::GetClientCertsImpl( |
81 CERTCertList* cert_list, | 60 CERTCertList* cert_list, |
82 const SSLCertRequestInfo& request, | 61 const SSLCertRequestInfo& request, |
83 bool query_nssdb, | 62 bool query_nssdb, |
84 CertificateList* selected_certs) { | 63 CertificateList* selected_certs) { |
85 ClientCertStoreNSS::GetClientCertsImpl( | 64 ClientCertStoreNSS::GetClientCertsImpl( |
86 cert_list, request, query_nssdb, selected_certs); | 65 cert_list, request, query_nssdb, selected_certs); |
87 | 66 |
88 size_t pre_size = selected_certs->size(); | 67 size_t pre_size = selected_certs->size(); |
89 selected_certs->erase( | 68 selected_certs->erase(std::remove_if(selected_certs->begin(), |
90 std::remove_if( | 69 selected_certs->end(), |
91 selected_certs->begin(), | 70 CertNotAllowedPredicate(*cert_filter_)), |
92 selected_certs->end(), | 71 selected_certs->end()); |
93 NSSProfileFilterChromeOS::CertNotAllowedForProfilePredicate( | |
94 profile_filter_)), | |
95 selected_certs->end()); | |
96 DVLOG(1) << "filtered " << pre_size - selected_certs->size() << " of " | 72 DVLOG(1) << "filtered " << pre_size - selected_certs->size() << " of " |
97 << pre_size << " certs"; | 73 << pre_size << " certs"; |
98 } | 74 } |
99 | 75 |
100 void ClientCertStoreChromeOS::DidGetSystemAndPrivateSlot( | 76 void ClientCertStoreChromeOS::CertFilterInitialized( |
101 const SSLCertRequestInfo* request, | 77 const SSLCertRequestInfo* request, |
102 CertificateList* selected_certs, | 78 CertificateList* selected_certs, |
103 const base::Closure& callback, | 79 const base::Closure& callback) { |
104 crypto::ScopedPK11Slot system_slot, | |
105 crypto::ScopedPK11Slot private_slot) { | |
106 profile_filter_.Init(crypto::GetPublicSlotForChromeOSUser(username_hash_), | |
107 private_slot.Pass(), | |
108 system_slot.Pass()); | |
109 ClientCertStoreNSS::GetClientCerts(*request, selected_certs, callback); | 80 ClientCertStoreNSS::GetClientCerts(*request, selected_certs, callback); |
110 } | 81 } |
111 | 82 |
112 } // namespace net | 83 } // namespace net |
OLD | NEW |