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 <string> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
13 #include "crypto/nss_util_internal.h" | 14 #include "crypto/nss_util_internal.h" |
14 #include "net/cert/nss_cert_database.h" | 15 #include "crypto/rsa_private_key.h" |
16 #include "net/base/test_data_directory.h" | |
17 #include "net/cert/cert_type.h" | |
18 #include "net/cert/x509_certificate.h" | |
15 #include "net/ssl/client_cert_store_unittest-inl.h" | 19 #include "net/ssl/client_cert_store_unittest-inl.h" |
20 #include "net/test/cert_test_util.h" | |
16 | 21 |
17 namespace net { | 22 namespace net { |
18 | 23 |
24 namespace { | |
25 | |
26 bool ImportClientCertToSlot(scoped_refptr<X509Certificate> cert, | |
27 PK11SlotInfo* slot) { | |
28 CK_OBJECT_HANDLE key; | |
29 crypto::ScopedPK11Slot key_slot( | |
30 PK11_KeyForCertExists(cert->os_cert_handle(), &key, NULL)); | |
31 if (slot != key_slot.get()) { | |
32 LOG(ERROR) << "Did not find key in the right slot"; | |
33 return false; | |
34 } | |
35 | |
36 std::string nickname = cert->GetDefaultNickname(USER_CERT); | |
pneubeck (no reviews)
2014/07/17 13:40:50
Changed the nickname from filename (because it's n
| |
37 { | |
38 crypto::AutoNSSWriteLock lock; | |
39 SECStatus rv = PK11_ImportCert( | |
40 slot, cert->os_cert_handle(), key, nickname.c_str(), PR_FALSE); | |
41 if (rv != SECSuccess) { | |
42 LOG(ERROR) << "Could not import cert"; | |
43 return false; | |
44 } | |
45 } | |
46 return true; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 class ClientCertStoreChromeOSTestDelegate { | |
52 public: | |
53 ClientCertStoreChromeOSTestDelegate() | |
54 : user_("scopeduser"), | |
55 store_(user_.username_hash(), | |
56 ClientCertStoreChromeOS::PasswordDelegateFactory()) { | |
57 CHECK(user_.constructed_successfully()); | |
pneubeck (no reviews)
2014/07/17 13:40:50
very likely you don't like these CHECKs, although
Ryan Sleevi
2014/07/17 18:42:20
This is my preference, in part because I almost ex
pneubeck (no reviews)
2014/07/17 21:45:10
I can do that tomorrow or in a follow up. Will mea
| |
58 user_.FinishInit(); | |
59 | |
60 slot_ = crypto::GetPublicSlotForChromeOSUser(user_.username_hash()); | |
61 CHECK(slot_); | |
62 | |
63 // Client certs can only be imported to |slot_| if the respective private | |
64 // key is present. Import the private keys for all client certificates that | |
65 // are used during the test. | |
66 ImportSensitiveKeyFromFile( | |
67 GetTestCertsDirectory(), "client_1.pk8", slot_.get()); | |
68 ImportSensitiveKeyFromFile( | |
69 GetTestCertsDirectory(), "client_2.pk8", slot_.get()); | |
70 } | |
71 | |
72 bool SelectClientCerts(const CertificateList& input_certs, | |
73 const SSLCertRequestInfo& cert_request_info, | |
74 CertificateList* selected_certs) { | |
75 for (CertificateList::const_iterator it = input_certs.begin(); | |
76 it != input_certs.end(); | |
77 ++it) { | |
78 if (!ImportClientCertToSlot(*it, slot_.get())) | |
79 return false; | |
80 } | |
81 base::RunLoop run_loop; | |
82 store_.GetClientCerts( | |
83 cert_request_info, selected_certs, run_loop.QuitClosure()); | |
84 run_loop.Run(); | |
85 return true; | |
86 } | |
87 | |
88 private: | |
89 crypto::ScopedTestNSSChromeOSUser user_; | |
90 ClientCertStoreChromeOS store_; | |
91 crypto::ScopedPK11Slot slot_; | |
92 }; | |
93 | |
94 // This tests whether the filtering functionality is correctly delegated to the | |
95 // base class ClientCertStoreNSS. | |
96 INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS, | |
97 ClientCertStoreTest, | |
98 ClientCertStoreChromeOSTestDelegate); | |
19 class ClientCertStoreChromeOSTest : public ::testing::Test { | 99 class ClientCertStoreChromeOSTest : public ::testing::Test { |
20 public: | 100 public: |
21 scoped_refptr<X509Certificate> ImportCertForUser( | 101 scoped_refptr<X509Certificate> ImportCertForUser( |
22 const std::string& username_hash, | 102 const std::string& username_hash, |
23 const std::string& filename, | 103 const std::string& cert_filename, |
24 const std::string& password) { | 104 const std::string& key_filename) { |
25 crypto::ScopedPK11Slot slot( | 105 crypto::ScopedPK11Slot slot( |
26 crypto::GetPublicSlotForChromeOSUser(username_hash)); | 106 crypto::GetPublicSlotForChromeOSUser(username_hash)); |
27 EXPECT_TRUE(slot.get()); | 107 if (!slot) { |
28 if (!slot.get()) | 108 LOG(ERROR) << "No slot for user " << username_hash; |
29 return NULL; | |
30 | |
31 net::CertificateList cert_list; | |
32 | |
33 base::FilePath p12_path = GetTestCertsDirectory().AppendASCII(filename); | |
34 std::string p12_data; | |
35 if (!base::ReadFileToString(p12_path, &p12_data)) { | |
36 EXPECT_TRUE(false); | |
37 return NULL; | 109 return NULL; |
38 } | 110 } |
39 | 111 |
40 scoped_refptr<net::CryptoModule> module( | 112 if (!ImportSensitiveKeyFromFile( |
41 net::CryptoModule::CreateFromHandle(slot.get())); | 113 GetTestCertsDirectory(), key_filename, slot.get())) { |
42 int rv = NSSCertDatabase::GetInstance()->ImportFromPKCS12( | 114 LOG(ERROR) << "Could not import private key for user " << username_hash; |
43 module.get(), p12_data, base::UTF8ToUTF16(password), false, &cert_list); | 115 return NULL; |
116 } | |
44 | 117 |
45 EXPECT_EQ(0, rv); | 118 scoped_refptr<X509Certificate> cert( |
46 EXPECT_EQ(1U, cert_list.size()); | 119 ImportCertFromFile(GetTestCertsDirectory(), cert_filename)); |
47 if (rv || cert_list.size() != 1) | 120 |
121 if (!cert) { | |
122 LOG(ERROR) << "Failed to parse cert from file " << cert_filename; | |
123 return NULL; | |
124 } | |
125 | |
126 if (!ImportClientCertToSlot(cert, slot.get())) | |
48 return NULL; | 127 return NULL; |
49 | 128 |
50 return cert_list[0]; | 129 return cert; |
51 } | 130 } |
52 }; | 131 }; |
53 | 132 |
54 // TODO(mattm): Do better testing of cert_authorities matching below. Update | 133 TEST_F(ClientCertStoreChromeOSTest, |
55 // net/data/ssl/scripts/generate-client-certificates.sh so that it actually | 134 IfRequestCertsBeforeNSSDBInitializedThenRequestWaitsForInitAndSucceeds) { |
56 // saves the .p12 files, and regenerate them. | |
57 | |
58 TEST_F(ClientCertStoreChromeOSTest, WaitForNSSInit) { | |
59 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); | 135 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); |
60 ASSERT_TRUE(user.constructed_successfully()); | 136 ASSERT_TRUE(user.constructed_successfully()); |
61 ClientCertStoreChromeOS store( | 137 ClientCertStoreChromeOS store( |
62 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); | 138 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); |
63 scoped_refptr<X509Certificate> cert_1( | 139 scoped_refptr<X509Certificate> cert_1( |
64 ImportCertForUser(user.username_hash(), "client.p12", "12345")); | 140 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8")); |
65 scoped_refptr<X509Certificate> cert_2( | 141 ASSERT_TRUE(cert_1); |
66 ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", "")); | |
67 | 142 |
68 std::vector<std::string> authority_1( | 143 // Request any client certificate, which is expected to match client_1. |
69 1, | |
70 std::string(reinterpret_cast<const char*>(kAuthority1DN), | |
71 sizeof(kAuthority1DN))); | |
72 scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo()); | |
73 request_1->cert_authorities = authority_1; | |
74 | |
75 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); | 144 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
76 | 145 |
77 base::RunLoop run_loop_1; | 146 base::RunLoop run_loop; |
78 base::RunLoop run_loop_all; | |
79 store.GetClientCerts( | 147 store.GetClientCerts( |
80 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 148 *request_all, &request_all->client_certs, run_loop.QuitClosure()); |
81 store.GetClientCerts( | |
82 *request_all, &request_all->client_certs, run_loop_all.QuitClosure()); | |
83 | 149 |
84 // Callbacks won't be run until nss_util init finishes for the user. | 150 { |
151 base::RunLoop run_loop_inner; | |
152 run_loop_inner.RunUntilIdle(); | |
153 // GetClientCerts should wait for the initialization of the user's DB to | |
154 // finish. | |
155 ASSERT_EQ(0u, request_all->client_certs.size()); | |
156 } | |
157 // This should trigger the GetClientCerts operation to finish and to call | |
158 // back. | |
85 user.FinishInit(); | 159 user.FinishInit(); |
86 | 160 |
87 run_loop_1.Run(); | 161 run_loop.Run(); |
88 run_loop_all.Run(); | |
89 | 162 |
90 ASSERT_EQ(0u, request_1->client_certs.size()); | 163 ASSERT_EQ(1u, request_all->client_certs.size()); |
91 ASSERT_EQ(2u, request_all->client_certs.size()); | |
92 } | 164 } |
93 | 165 |
94 TEST_F(ClientCertStoreChromeOSTest, NSSAlreadyInitialized) { | 166 TEST_F(ClientCertStoreChromeOSTest, |
167 IfRequestCertsAfterNSSDBInitializedThenRequestSucceeds) { | |
95 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); | 168 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); |
96 ASSERT_TRUE(user.constructed_successfully()); | 169 ASSERT_TRUE(user.constructed_successfully()); |
97 user.FinishInit(); | 170 user.FinishInit(); |
98 | 171 |
99 ClientCertStoreChromeOS store( | 172 ClientCertStoreChromeOS store( |
100 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); | 173 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); |
101 scoped_refptr<X509Certificate> cert_1( | 174 scoped_refptr<X509Certificate> cert_1( |
102 ImportCertForUser(user.username_hash(), "client.p12", "12345")); | 175 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8")); |
103 scoped_refptr<X509Certificate> cert_2( | 176 ASSERT_TRUE(cert_1); |
104 ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", "")); | |
105 | |
106 std::vector<std::string> authority_1( | |
107 1, | |
108 std::string(reinterpret_cast<const char*>(kAuthority1DN), | |
109 sizeof(kAuthority1DN))); | |
110 scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo()); | |
111 request_1->cert_authorities = authority_1; | |
112 | 177 |
113 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); | 178 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
114 | 179 |
115 base::RunLoop run_loop_1; | 180 base::RunLoop run_loop; |
116 base::RunLoop run_loop_all; | |
117 store.GetClientCerts( | 181 store.GetClientCerts( |
118 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 182 *request_all, &request_all->client_certs, run_loop.QuitClosure()); |
119 store.GetClientCerts( | |
120 *request_all, &request_all->client_certs, run_loop_all.QuitClosure()); | |
121 | 183 |
122 run_loop_1.Run(); | 184 run_loop.Run(); |
123 run_loop_all.Run(); | |
124 | 185 |
125 ASSERT_EQ(0u, request_1->client_certs.size()); | 186 ASSERT_EQ(1u, request_all->client_certs.size()); |
126 ASSERT_EQ(2u, request_all->client_certs.size()); | |
127 } | 187 } |
128 | 188 |
129 TEST_F(ClientCertStoreChromeOSTest, TwoUsers) { | 189 // This verifies that a request in the context of User1 doesn't see certificates |
190 // of User2, and the other way round. We check both directions, to ensure that | |
191 // the behavior doesn't depend on initialization order of the DBs, for example. | |
192 TEST_F(ClientCertStoreChromeOSTest, | |
193 IfTwoNSSDBsExistThenCertRequestsReturnOnlyCertsOfTheGivenDB) { | |
130 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1"); | 194 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1"); |
131 ASSERT_TRUE(user1.constructed_successfully()); | 195 ASSERT_TRUE(user1.constructed_successfully()); |
132 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2"); | 196 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2"); |
133 ASSERT_TRUE(user2.constructed_successfully()); | 197 ASSERT_TRUE(user2.constructed_successfully()); |
198 | |
199 user1.FinishInit(); | |
200 user2.FinishInit(); | |
201 | |
134 ClientCertStoreChromeOS store1( | 202 ClientCertStoreChromeOS store1( |
135 user1.username_hash(), | 203 user1.username_hash(), |
136 ClientCertStoreChromeOS::PasswordDelegateFactory()); | 204 ClientCertStoreChromeOS::PasswordDelegateFactory()); |
137 ClientCertStoreChromeOS store2( | 205 ClientCertStoreChromeOS store2( |
138 user2.username_hash(), | 206 user2.username_hash(), |
139 ClientCertStoreChromeOS::PasswordDelegateFactory()); | 207 ClientCertStoreChromeOS::PasswordDelegateFactory()); |
208 | |
140 scoped_refptr<X509Certificate> cert_1( | 209 scoped_refptr<X509Certificate> cert_1( |
141 ImportCertForUser(user1.username_hash(), "client.p12", "12345")); | 210 ImportCertForUser(user1.username_hash(), "client_1.pem", "client_1.pk8")); |
142 scoped_refptr<X509Certificate> cert_2(ImportCertForUser( | 211 ASSERT_TRUE(cert_1); |
143 user2.username_hash(), "websocket_client_cert.p12", "")); | 212 scoped_refptr<X509Certificate> cert_2( |
213 ImportCertForUser(user2.username_hash(), "client_2.pem", "client_2.pk8")); | |
214 ASSERT_TRUE(cert_2); | |
144 | 215 |
145 scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo()); | 216 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
146 scoped_refptr<SSLCertRequestInfo> request_2(new SSLCertRequestInfo()); | |
147 | 217 |
148 base::RunLoop run_loop_1; | 218 base::RunLoop run_loop_1; |
149 base::RunLoop run_loop_2; | 219 base::RunLoop run_loop_2; |
220 | |
221 CertificateList selected_certs1, selected_certs2; | |
150 store1.GetClientCerts( | 222 store1.GetClientCerts( |
151 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 223 *request_all, &selected_certs1, run_loop_1.QuitClosure()); |
152 store2.GetClientCerts( | 224 store2.GetClientCerts( |
153 *request_2, &request_2->client_certs, run_loop_2.QuitClosure()); | 225 *request_all, &selected_certs2, run_loop_2.QuitClosure()); |
154 | |
155 // Callbacks won't be run until nss_util init finishes for the user. | |
156 user1.FinishInit(); | |
157 user2.FinishInit(); | |
158 | 226 |
159 run_loop_1.Run(); | 227 run_loop_1.Run(); |
160 run_loop_2.Run(); | 228 run_loop_2.Run(); |
161 | 229 |
162 ASSERT_EQ(1u, request_1->client_certs.size()); | 230 // store1 should only return certs of user1, namely cert_1. |
163 EXPECT_TRUE(cert_1->Equals(request_1->client_certs[0])); | 231 ASSERT_EQ(1u, selected_certs1.size()); |
164 // TODO(mattm): Request for second user will have zero results due to | 232 EXPECT_TRUE(cert_1->Equals(selected_certs1[0])); |
165 // crbug.com/315285. Update the test once that is fixed. | 233 |
234 // store2 should only return certs of user2, namely cert_2. | |
235 ASSERT_EQ(1u, selected_certs2.size()); | |
236 EXPECT_TRUE(cert_2->Equals(selected_certs2[0])); | |
166 } | 237 } |
167 | 238 |
168 } // namespace net | 239 } // namespace net |
OLD | NEW |