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

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

Issue 394013005: Remove NSSCertDatabase from ClientCertStoreChromeOS unittest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « net/ssl/client_cert_store_chromeos.cc ('k') | net/test/cert_test_util.h » ('j') | 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_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(const scoped_refptr<X509Certificate>& cert,
27 PK11SlotInfo* slot) {
28 std::string nickname = cert->GetDefaultNickname(USER_CERT);
29 {
30 crypto::AutoNSSWriteLock lock;
31 SECStatus rv = PK11_ImportCert(slot,
32 cert->os_cert_handle(),
33 CK_INVALID_HANDLE,
34 nickname.c_str(),
35 PR_FALSE);
36 if (rv != SECSuccess) {
37 LOG(ERROR) << "Could not import cert";
38 return false;
39 }
40 }
41 return true;
42 }
43
44 } // namespace
45
46 // Define a delegate to be used for instantiating the parameterized test set
47 // ClientCertStoreTest.
48 class ClientCertStoreChromeOSTestDelegate {
49 public:
50 ClientCertStoreChromeOSTestDelegate()
51 : user_("scopeduser"),
52 store_(user_.username_hash(),
53 ClientCertStoreChromeOS::PasswordDelegateFactory()) {
54 // Defer futher initialization and checks to SelectClientCerts, because the
55 // constructor doesn't allow us to return an initialization result. Could be
56 // cleaned up by adding an Init() function.
57 }
58
59 // Called by the ClientCertStoreTest tests.
60 // |inpurt_certs| contains certificates to select from. Because
61 // ClientCertStoreChromeOS filters also for the right slot, we have to import
62 // the certs at first.
63 // Since the certs are imported, the store can be tested by using its public
64 // interface (GetClientCerts), which will read the certs from NSS.
65 bool SelectClientCerts(const CertificateList& input_certs,
66 const SSLCertRequestInfo& cert_request_info,
67 CertificateList* selected_certs) {
68 if (!user_.constructed_successfully()) {
69 LOG(ERROR) << "Scoped test user DB could not be constructed.";
70 return false;
71 }
72 user_.FinishInit();
73
74 crypto::ScopedPK11Slot slot(
75 crypto::GetPublicSlotForChromeOSUser(user_.username_hash()));
76 if (!slot) {
77 LOG(ERROR) << "Could not get the user's public slot";
78 return false;
79 }
80
81 // Only user certs are considered for the cert request, which means that the
82 // private key must be known to NSS. Import all private keys for certs that
83 // are used througout the test.
84 if (!ImportSensitiveKeyFromFile(
85 GetTestCertsDirectory(), "client_1.pk8", slot.get()) ||
86 !ImportSensitiveKeyFromFile(
87 GetTestCertsDirectory(), "client_2.pk8", slot.get())) {
88 return false;
89 }
90
91 for (CertificateList::const_iterator it = input_certs.begin();
92 it != input_certs.end();
93 ++it) {
94 if (!ImportClientCertToSlot(*it, slot.get()))
95 return false;
96 }
97 base::RunLoop run_loop;
98 store_.GetClientCerts(
99 cert_request_info, selected_certs, run_loop.QuitClosure());
100 run_loop.Run();
101 return true;
102 }
103
104 private:
105 crypto::ScopedTestNSSChromeOSUser user_;
106 ClientCertStoreChromeOS store_;
107 };
108
109 // ClientCertStoreChromeOS derives from ClientCertStoreNSS and delegates the
110 // filtering by issuer to that base class.
111 // To verify that this delegation is functional, run the same filtering tests as
112 // for the other implementations. These tests are defined in
113 // client_cert_store_unittest-inl.h and are instantiated for each platform.
114 INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS,
115 ClientCertStoreTest,
116 ClientCertStoreChromeOSTestDelegate);
117
19 class ClientCertStoreChromeOSTest : public ::testing::Test { 118 class ClientCertStoreChromeOSTest : public ::testing::Test {
20 public: 119 public:
21 scoped_refptr<X509Certificate> ImportCertForUser( 120 scoped_refptr<X509Certificate> ImportCertForUser(
22 const std::string& username_hash, 121 const std::string& username_hash,
23 const std::string& filename, 122 const std::string& cert_filename,
24 const std::string& password) { 123 const std::string& key_filename) {
25 crypto::ScopedPK11Slot slot( 124 crypto::ScopedPK11Slot slot(
26 crypto::GetPublicSlotForChromeOSUser(username_hash)); 125 crypto::GetPublicSlotForChromeOSUser(username_hash));
27 EXPECT_TRUE(slot.get()); 126 if (!slot) {
28 if (!slot.get()) 127 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; 128 return NULL;
38 } 129 }
39 130
40 scoped_refptr<net::CryptoModule> module( 131 if (!ImportSensitiveKeyFromFile(
41 net::CryptoModule::CreateFromHandle(slot.get())); 132 GetTestCertsDirectory(), key_filename, slot.get())) {
42 int rv = NSSCertDatabase::GetInstance()->ImportFromPKCS12( 133 LOG(ERROR) << "Could not import private key for user " << username_hash;
43 module.get(), p12_data, base::UTF8ToUTF16(password), false, &cert_list); 134 return NULL;
135 }
44 136
45 EXPECT_EQ(0, rv); 137 scoped_refptr<X509Certificate> cert(
46 EXPECT_EQ(1U, cert_list.size()); 138 ImportCertFromFile(GetTestCertsDirectory(), cert_filename));
47 if (rv || cert_list.size() != 1) 139
140 if (!cert) {
141 LOG(ERROR) << "Failed to parse cert from file " << cert_filename;
142 return NULL;
143 }
144
145 if (!ImportClientCertToSlot(cert, slot.get()))
48 return NULL; 146 return NULL;
49 147
50 return cert_list[0]; 148 // |cert| continues to point to the original X509Certificate before the
149 // import to |slot|. However this should not make a difference for this
150 // test.
151 return cert;
51 } 152 }
52 }; 153 };
53 154
54 // TODO(mattm): Do better testing of cert_authorities matching below. Update 155 // Ensure that cert requests, that are started before the user's NSS DB is
55 // net/data/ssl/scripts/generate-client-certificates.sh so that it actually 156 // initialized, will wait for the initialization and succeed afterwards.
56 // saves the .p12 files, and regenerate them. 157 TEST_F(ClientCertStoreChromeOSTest, RequestWaitsForNSSInitAndSucceeds) {
57
58 TEST_F(ClientCertStoreChromeOSTest, WaitForNSSInit) {
59 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); 158 crypto::ScopedTestNSSChromeOSUser user("scopeduser");
60 ASSERT_TRUE(user.constructed_successfully()); 159 ASSERT_TRUE(user.constructed_successfully());
61 ClientCertStoreChromeOS store( 160 ClientCertStoreChromeOS store(
62 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); 161 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory());
63 scoped_refptr<X509Certificate> cert_1( 162 scoped_refptr<X509Certificate> cert_1(
64 ImportCertForUser(user.username_hash(), "client.p12", "12345")); 163 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8"));
65 scoped_refptr<X509Certificate> cert_2( 164 ASSERT_TRUE(cert_1);
66 ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", ""));
67 165
68 std::vector<std::string> authority_1( 166 // 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()); 167 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());
76 168
77 base::RunLoop run_loop_1; 169 base::RunLoop run_loop;
78 base::RunLoop run_loop_all;
79 store.GetClientCerts( 170 store.GetClientCerts(
80 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); 171 *request_all, &request_all->client_certs, run_loop.QuitClosure());
81 store.GetClientCerts(
82 *request_all, &request_all->client_certs, run_loop_all.QuitClosure());
83 172
84 // Callbacks won't be run until nss_util init finishes for the user. 173 {
174 base::RunLoop run_loop_inner;
175 run_loop_inner.RunUntilIdle();
176 // GetClientCerts should wait for the initialization of the user's DB to
177 // finish.
178 ASSERT_EQ(0u, request_all->client_certs.size());
179 }
180 // This should trigger the GetClientCerts operation to finish and to call
181 // back.
85 user.FinishInit(); 182 user.FinishInit();
86 183
87 run_loop_1.Run(); 184 run_loop.Run();
88 run_loop_all.Run();
89 185
90 ASSERT_EQ(0u, request_1->client_certs.size()); 186 ASSERT_EQ(1u, request_all->client_certs.size());
91 ASSERT_EQ(2u, request_all->client_certs.size());
92 } 187 }
93 188
94 TEST_F(ClientCertStoreChromeOSTest, NSSAlreadyInitialized) { 189 // Ensure that cert requests, that are started after the user's NSS DB was
190 // initialized, will succeed.
191 TEST_F(ClientCertStoreChromeOSTest, RequestsAfterNSSInitSucceed) {
95 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); 192 crypto::ScopedTestNSSChromeOSUser user("scopeduser");
96 ASSERT_TRUE(user.constructed_successfully()); 193 ASSERT_TRUE(user.constructed_successfully());
97 user.FinishInit(); 194 user.FinishInit();
98 195
99 ClientCertStoreChromeOS store( 196 ClientCertStoreChromeOS store(
100 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); 197 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory());
101 scoped_refptr<X509Certificate> cert_1( 198 scoped_refptr<X509Certificate> cert_1(
102 ImportCertForUser(user.username_hash(), "client.p12", "12345")); 199 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8"));
103 scoped_refptr<X509Certificate> cert_2( 200 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 201
113 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); 202 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());
114 203
115 base::RunLoop run_loop_1; 204 base::RunLoop run_loop;
116 base::RunLoop run_loop_all;
117 store.GetClientCerts( 205 store.GetClientCerts(
118 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); 206 *request_all, &request_all->client_certs, run_loop.QuitClosure());
119 store.GetClientCerts( 207 run_loop.Run();
120 *request_all, &request_all->client_certs, run_loop_all.QuitClosure());
121 208
122 run_loop_1.Run(); 209 ASSERT_EQ(1u, request_all->client_certs.size());
123 run_loop_all.Run();
124
125 ASSERT_EQ(0u, request_1->client_certs.size());
126 ASSERT_EQ(2u, request_all->client_certs.size());
127 } 210 }
128 211
129 TEST_F(ClientCertStoreChromeOSTest, TwoUsers) { 212 // This verifies that a request in the context of User1 doesn't see certificates
213 // of User2, and the other way round. We check both directions, to ensure that
214 // the behavior doesn't depend on initialization order of the DBs, for example.
215 TEST_F(ClientCertStoreChromeOSTest, RequestDoesCrossReadSecondDB) {
130 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1"); 216 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1");
131 ASSERT_TRUE(user1.constructed_successfully()); 217 ASSERT_TRUE(user1.constructed_successfully());
132 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2"); 218 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2");
133 ASSERT_TRUE(user2.constructed_successfully()); 219 ASSERT_TRUE(user2.constructed_successfully());
220
221 user1.FinishInit();
222 user2.FinishInit();
223
134 ClientCertStoreChromeOS store1( 224 ClientCertStoreChromeOS store1(
135 user1.username_hash(), 225 user1.username_hash(),
136 ClientCertStoreChromeOS::PasswordDelegateFactory()); 226 ClientCertStoreChromeOS::PasswordDelegateFactory());
137 ClientCertStoreChromeOS store2( 227 ClientCertStoreChromeOS store2(
138 user2.username_hash(), 228 user2.username_hash(),
139 ClientCertStoreChromeOS::PasswordDelegateFactory()); 229 ClientCertStoreChromeOS::PasswordDelegateFactory());
230
140 scoped_refptr<X509Certificate> cert_1( 231 scoped_refptr<X509Certificate> cert_1(
141 ImportCertForUser(user1.username_hash(), "client.p12", "12345")); 232 ImportCertForUser(user1.username_hash(), "client_1.pem", "client_1.pk8"));
142 scoped_refptr<X509Certificate> cert_2(ImportCertForUser( 233 ASSERT_TRUE(cert_1);
143 user2.username_hash(), "websocket_client_cert.p12", "")); 234 scoped_refptr<X509Certificate> cert_2(
235 ImportCertForUser(user2.username_hash(), "client_2.pem", "client_2.pk8"));
236 ASSERT_TRUE(cert_2);
144 237
145 scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo()); 238 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());
146 scoped_refptr<SSLCertRequestInfo> request_2(new SSLCertRequestInfo());
147 239
148 base::RunLoop run_loop_1; 240 base::RunLoop run_loop_1;
149 base::RunLoop run_loop_2; 241 base::RunLoop run_loop_2;
242
243 CertificateList selected_certs1, selected_certs2;
150 store1.GetClientCerts( 244 store1.GetClientCerts(
151 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); 245 *request_all, &selected_certs1, run_loop_1.QuitClosure());
152 store2.GetClientCerts( 246 store2.GetClientCerts(
153 *request_2, &request_2->client_certs, run_loop_2.QuitClosure()); 247 *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 248
159 run_loop_1.Run(); 249 run_loop_1.Run();
160 run_loop_2.Run(); 250 run_loop_2.Run();
161 251
162 ASSERT_EQ(1u, request_1->client_certs.size()); 252 // store1 should only return certs of user1, namely cert_1.
163 EXPECT_TRUE(cert_1->Equals(request_1->client_certs[0])); 253 ASSERT_EQ(1u, selected_certs1.size());
164 // TODO(mattm): Request for second user will have zero results due to 254 EXPECT_TRUE(cert_1->Equals(selected_certs1[0]));
165 // crbug.com/315285. Update the test once that is fixed. 255
256 // store2 should only return certs of user2, namely cert_2.
257 ASSERT_EQ(1u, selected_certs2.size());
258 EXPECT_TRUE(cert_2->Equals(selected_certs2[0]));
166 } 259 }
167 260
168 } // namespace net 261 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/client_cert_store_chromeos.cc ('k') | net/test/cert_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698