Chromium Code Reviews| 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, | |
|
Ryan Sleevi
2014/07/17 22:18:52
const scoped_refptr<>&
pneubeck (no reviews)
2014/07/18 08:42:42
Done.
| |
| 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 } | |
|
Ryan Sleevi
2014/07/17 22:18:52
Not sure why you do this check. It's not required
pneubeck (no reviews)
2014/07/18 08:42:42
Removed.
| |
| 35 | |
| 36 std::string nickname = cert->GetDefaultNickname(USER_CERT); | |
| 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()); | |
| 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. | |
|
Ryan Sleevi
2014/07/17 22:18:52
This doesn't sound right, especially since NSS doe
pneubeck (no reviews)
2014/07/18 08:42:42
I never get used to the spares (or rather not exis
| |
| 66 ImportSensitiveKeyFromFile( | |
| 67 GetTestCertsDirectory(), "client_1.pk8", slot_.get()); | |
| 68 ImportSensitiveKeyFromFile( | |
| 69 GetTestCertsDirectory(), "client_2.pk8", slot_.get()); | |
|
Ryan Sleevi
2014/07/17 22:18:51
One way to reduce refactoring would be to defer al
pneubeck (no reviews)
2014/07/18 08:42:42
Done.
| |
| 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 }; | |
|
Ryan Sleevi
2014/07/17 22:18:52
More documentation is needed - either in this clas
| |
| 93 | |
| 94 // This tests whether the filtering functionality is correctly delegated to the | |
| 95 // base class ClientCertStoreNSS. | |
|
Ryan Sleevi
2014/07/17 22:18:52
Eh? This doesn't make sense, considering that you'
pneubeck (no reviews)
2014/07/18 08:42:42
I don't see why it didn't make sense.
But I made t
| |
| 96 INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS, | |
| 97 ClientCertStoreTest, | |
| 98 ClientCertStoreChromeOSTestDelegate); | |
| 99 | |
| 19 class ClientCertStoreChromeOSTest : public ::testing::Test { | 100 class ClientCertStoreChromeOSTest : public ::testing::Test { |
| 20 public: | 101 public: |
| 21 scoped_refptr<X509Certificate> ImportCertForUser( | 102 scoped_refptr<X509Certificate> ImportCertForUser( |
| 22 const std::string& username_hash, | 103 const std::string& username_hash, |
| 23 const std::string& filename, | 104 const std::string& cert_filename, |
| 24 const std::string& password) { | 105 const std::string& key_filename) { |
| 25 crypto::ScopedPK11Slot slot( | 106 crypto::ScopedPK11Slot slot( |
| 26 crypto::GetPublicSlotForChromeOSUser(username_hash)); | 107 crypto::GetPublicSlotForChromeOSUser(username_hash)); |
| 27 EXPECT_TRUE(slot.get()); | 108 if (!slot) { |
| 28 if (!slot.get()) | 109 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; | 110 return NULL; |
| 38 } | 111 } |
| 39 | 112 |
| 40 scoped_refptr<net::CryptoModule> module( | 113 if (!ImportSensitiveKeyFromFile( |
| 41 net::CryptoModule::CreateFromHandle(slot.get())); | 114 GetTestCertsDirectory(), key_filename, slot.get())) { |
| 42 int rv = NSSCertDatabase::GetInstance()->ImportFromPKCS12( | 115 LOG(ERROR) << "Could not import private key for user " << username_hash; |
| 43 module.get(), p12_data, base::UTF8ToUTF16(password), false, &cert_list); | 116 return NULL; |
| 117 } | |
| 44 | 118 |
| 45 EXPECT_EQ(0, rv); | 119 scoped_refptr<X509Certificate> cert( |
| 46 EXPECT_EQ(1U, cert_list.size()); | 120 ImportCertFromFile(GetTestCertsDirectory(), cert_filename)); |
| 47 if (rv || cert_list.size() != 1) | 121 |
| 122 if (!cert) { | |
| 123 LOG(ERROR) << "Failed to parse cert from file " << cert_filename; | |
| 124 return NULL; | |
| 125 } | |
| 126 | |
| 127 if (!ImportClientCertToSlot(cert, slot.get())) | |
| 48 return NULL; | 128 return NULL; |
| 49 | 129 |
| 50 return cert_list[0]; | 130 return cert; |
|
Ryan Sleevi
2014/07/17 22:18:52
|cert| may (and almost certainly will) refer to th
pneubeck (no reviews)
2014/07/18 08:42:42
Added a comment.
| |
| 51 } | 131 } |
| 52 }; | 132 }; |
| 53 | 133 |
| 54 // TODO(mattm): Do better testing of cert_authorities matching below. Update | 134 TEST_F(ClientCertStoreChromeOSTest, |
| 55 // net/data/ssl/scripts/generate-client-certificates.sh so that it actually | 135 IfRequestCertsBeforeNSSDBInitializedThenRequestWaitsForInitAndSucceeds) { |
|
Ryan Sleevi
2014/07/17 22:18:52
I actually meant just include a more meaningful co
pneubeck (no reviews)
2014/07/18 08:42:42
I blame TotT for that...
| |
| 56 // saves the .p12 files, and regenerate them. | |
| 57 | |
| 58 TEST_F(ClientCertStoreChromeOSTest, WaitForNSSInit) { | |
| 59 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); | 136 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); |
| 60 ASSERT_TRUE(user.constructed_successfully()); | 137 ASSERT_TRUE(user.constructed_successfully()); |
| 61 ClientCertStoreChromeOS store( | 138 ClientCertStoreChromeOS store( |
| 62 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); | 139 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); |
| 63 scoped_refptr<X509Certificate> cert_1( | 140 scoped_refptr<X509Certificate> cert_1( |
| 64 ImportCertForUser(user.username_hash(), "client.p12", "12345")); | 141 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8")); |
| 65 scoped_refptr<X509Certificate> cert_2( | 142 ASSERT_TRUE(cert_1); |
| 66 ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", "")); | |
| 67 | 143 |
| 68 std::vector<std::string> authority_1( | 144 // 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()); | 145 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
| 76 | 146 |
| 77 base::RunLoop run_loop_1; | 147 base::RunLoop run_loop; |
| 78 base::RunLoop run_loop_all; | |
| 79 store.GetClientCerts( | 148 store.GetClientCerts( |
| 80 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 149 *request_all, &request_all->client_certs, run_loop.QuitClosure()); |
| 81 store.GetClientCerts( | |
| 82 *request_all, &request_all->client_certs, run_loop_all.QuitClosure()); | |
| 83 | 150 |
| 84 // Callbacks won't be run until nss_util init finishes for the user. | 151 { |
| 152 base::RunLoop run_loop_inner; | |
| 153 run_loop_inner.RunUntilIdle(); | |
| 154 // GetClientCerts should wait for the initialization of the user's DB to | |
| 155 // finish. | |
| 156 ASSERT_EQ(0u, request_all->client_certs.size()); | |
| 157 } | |
| 158 // This should trigger the GetClientCerts operation to finish and to call | |
| 159 // back. | |
| 85 user.FinishInit(); | 160 user.FinishInit(); |
| 86 | 161 |
| 87 run_loop_1.Run(); | 162 run_loop.Run(); |
| 88 run_loop_all.Run(); | |
| 89 | 163 |
| 90 ASSERT_EQ(0u, request_1->client_certs.size()); | 164 ASSERT_EQ(1u, request_all->client_certs.size()); |
| 91 ASSERT_EQ(2u, request_all->client_certs.size()); | |
| 92 } | 165 } |
| 93 | 166 |
| 94 TEST_F(ClientCertStoreChromeOSTest, NSSAlreadyInitialized) { | 167 TEST_F(ClientCertStoreChromeOSTest, |
| 168 IfRequestCertsAfterNSSDBInitializedThenRequestSucceeds) { | |
| 95 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); | 169 crypto::ScopedTestNSSChromeOSUser user("scopeduser"); |
| 96 ASSERT_TRUE(user.constructed_successfully()); | 170 ASSERT_TRUE(user.constructed_successfully()); |
| 97 user.FinishInit(); | 171 user.FinishInit(); |
| 98 | 172 |
| 99 ClientCertStoreChromeOS store( | 173 ClientCertStoreChromeOS store( |
| 100 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); | 174 user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory()); |
| 101 scoped_refptr<X509Certificate> cert_1( | 175 scoped_refptr<X509Certificate> cert_1( |
| 102 ImportCertForUser(user.username_hash(), "client.p12", "12345")); | 176 ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8")); |
| 103 scoped_refptr<X509Certificate> cert_2( | 177 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 | 178 |
| 113 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); | 179 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
| 114 | 180 |
| 115 base::RunLoop run_loop_1; | 181 base::RunLoop run_loop; |
| 116 base::RunLoop run_loop_all; | |
| 117 store.GetClientCerts( | 182 store.GetClientCerts( |
| 118 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 183 *request_all, &request_all->client_certs, run_loop.QuitClosure()); |
| 119 store.GetClientCerts( | |
| 120 *request_all, &request_all->client_certs, run_loop_all.QuitClosure()); | |
| 121 | 184 |
| 122 run_loop_1.Run(); | 185 run_loop.Run(); |
| 123 run_loop_all.Run(); | |
| 124 | 186 |
| 125 ASSERT_EQ(0u, request_1->client_certs.size()); | 187 ASSERT_EQ(1u, request_all->client_certs.size()); |
| 126 ASSERT_EQ(2u, request_all->client_certs.size()); | |
| 127 } | 188 } |
| 128 | 189 |
| 129 TEST_F(ClientCertStoreChromeOSTest, TwoUsers) { | 190 // This verifies that a request in the context of User1 doesn't see certificates |
| 191 // of User2, and the other way round. We check both directions, to ensure that | |
| 192 // the behavior doesn't depend on initialization order of the DBs, for example. | |
| 193 TEST_F(ClientCertStoreChromeOSTest, | |
| 194 IfTwoNSSDBsExistThenCertRequestsReturnOnlyCertsOfTheGivenDB) { | |
| 130 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1"); | 195 crypto::ScopedTestNSSChromeOSUser user1("scopeduser1"); |
| 131 ASSERT_TRUE(user1.constructed_successfully()); | 196 ASSERT_TRUE(user1.constructed_successfully()); |
| 132 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2"); | 197 crypto::ScopedTestNSSChromeOSUser user2("scopeduser2"); |
| 133 ASSERT_TRUE(user2.constructed_successfully()); | 198 ASSERT_TRUE(user2.constructed_successfully()); |
| 199 | |
| 200 user1.FinishInit(); | |
| 201 user2.FinishInit(); | |
| 202 | |
| 134 ClientCertStoreChromeOS store1( | 203 ClientCertStoreChromeOS store1( |
| 135 user1.username_hash(), | 204 user1.username_hash(), |
| 136 ClientCertStoreChromeOS::PasswordDelegateFactory()); | 205 ClientCertStoreChromeOS::PasswordDelegateFactory()); |
| 137 ClientCertStoreChromeOS store2( | 206 ClientCertStoreChromeOS store2( |
| 138 user2.username_hash(), | 207 user2.username_hash(), |
| 139 ClientCertStoreChromeOS::PasswordDelegateFactory()); | 208 ClientCertStoreChromeOS::PasswordDelegateFactory()); |
| 209 | |
| 140 scoped_refptr<X509Certificate> cert_1( | 210 scoped_refptr<X509Certificate> cert_1( |
| 141 ImportCertForUser(user1.username_hash(), "client.p12", "12345")); | 211 ImportCertForUser(user1.username_hash(), "client_1.pem", "client_1.pk8")); |
| 142 scoped_refptr<X509Certificate> cert_2(ImportCertForUser( | 212 ASSERT_TRUE(cert_1); |
| 143 user2.username_hash(), "websocket_client_cert.p12", "")); | 213 scoped_refptr<X509Certificate> cert_2( |
| 214 ImportCertForUser(user2.username_hash(), "client_2.pem", "client_2.pk8")); | |
| 215 ASSERT_TRUE(cert_2); | |
| 144 | 216 |
| 145 scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo()); | 217 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
| 146 scoped_refptr<SSLCertRequestInfo> request_2(new SSLCertRequestInfo()); | |
| 147 | 218 |
| 148 base::RunLoop run_loop_1; | 219 base::RunLoop run_loop_1; |
| 149 base::RunLoop run_loop_2; | 220 base::RunLoop run_loop_2; |
| 221 | |
| 222 CertificateList selected_certs1, selected_certs2; | |
| 150 store1.GetClientCerts( | 223 store1.GetClientCerts( |
| 151 *request_1, &request_1->client_certs, run_loop_1.QuitClosure()); | 224 *request_all, &selected_certs1, run_loop_1.QuitClosure()); |
| 152 store2.GetClientCerts( | 225 store2.GetClientCerts( |
| 153 *request_2, &request_2->client_certs, run_loop_2.QuitClosure()); | 226 *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 | 227 |
| 159 run_loop_1.Run(); | 228 run_loop_1.Run(); |
| 160 run_loop_2.Run(); | 229 run_loop_2.Run(); |
| 161 | 230 |
| 162 ASSERT_EQ(1u, request_1->client_certs.size()); | 231 // store1 should only return certs of user1, namely cert_1. |
| 163 EXPECT_TRUE(cert_1->Equals(request_1->client_certs[0])); | 232 ASSERT_EQ(1u, selected_certs1.size()); |
| 164 // TODO(mattm): Request for second user will have zero results due to | 233 EXPECT_TRUE(cert_1->Equals(selected_certs1[0])); |
| 165 // crbug.com/315285. Update the test once that is fixed. | 234 |
| 235 // store2 should only return certs of user2, namely cert_2. | |
| 236 ASSERT_EQ(1u, selected_certs2.size()); | |
| 237 EXPECT_TRUE(cert_2->Equals(selected_certs2[0])); | |
| 166 } | 238 } |
| 167 | 239 |
| 168 } // namespace net | 240 } // namespace net |
| OLD | NEW |