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