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

Side by Side Diff: chromeos/network/network_cert_migrator_unittest.cc

Issue 845533002: Remove CACert NSS nickname to PEM migration. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
« no previous file with comments | « chromeos/network/network_cert_migrator.cc ('k') | chromeos/network/network_state.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 "chromeos/network/network_cert_migrator.h" 5 #include "chromeos/network/network_cert_migrator.h"
6 6
7 #include <cert.h> 7 #include <cert.h>
8 #include <pk11pub.h>
9 #include <string>
8 10
9 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "chromeos/cert_loader.h" 14 #include "chromeos/cert_loader.h"
14 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/shill_profile_client.h" 16 #include "chromeos/dbus/shill_profile_client.h"
16 #include "chromeos/dbus/shill_service_client.h" 17 #include "chromeos/dbus/shill_service_client.h"
17 #include "chromeos/network/network_state_handler.h" 18 #include "chromeos/network/network_state_handler.h"
18 #include "chromeos/tpm/tpm_token_loader.h" 19 #include "crypto/scoped_nss_types.h"
19 #include "crypto/nss_util_internal.h" 20 #include "crypto/scoped_test_nss_db.h"
20 #include "crypto/scoped_test_nss_chromeos_user.h"
21 #include "net/base/crypto_module.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/test_data_directory.h" 21 #include "net/base/test_data_directory.h"
24 #include "net/cert/nss_cert_database_chromeos.h" 22 #include "net/cert/nss_cert_database_chromeos.h"
25 #include "net/cert/x509_certificate.h" 23 #include "net/cert/x509_certificate.h"
26 #include "net/test/cert_test_util.h" 24 #include "net/test/cert_test_util.h"
27 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/cros_system_api/dbus/service_constants.h" 26 #include "third_party/cros_system_api/dbus/service_constants.h"
29 27
30 // http://crbug.com/418369
31 #ifdef NDEBUG
32
33 namespace chromeos { 28 namespace chromeos {
34 29
35 namespace { 30 namespace {
36 31
37 const char* kWifiStub = "wifi_stub"; 32 const char* kWifiStub = "wifi_stub";
38 const char* kEthernetEapStub = "ethernet_eap_stub"; 33 const char* kEthernetEapStub = "ethernet_eap_stub";
39 const char* kVPNStub = "vpn_stub"; 34 const char* kVPNStub = "vpn_stub";
40 const char* kNSSNickname = "nss_nickname";
41 const char* kFakePEM = "pem";
42 const char* kProfile = "/profile/profile1"; 35 const char* kProfile = "/profile/profile1";
43 36
44 } // namespace 37 } // namespace
45 38
46 class NetworkCertMigratorTest : public testing::Test { 39 class NetworkCertMigratorTest : public testing::Test {
47 public: 40 public:
48 NetworkCertMigratorTest() : service_test_(NULL), 41 NetworkCertMigratorTest() : service_test_(nullptr) {}
49 user_("user_hash") { 42 ~NetworkCertMigratorTest() override {}
50 }
51 virtual ~NetworkCertMigratorTest() {}
52 43
53 virtual void SetUp() override { 44 void SetUp() override {
54 // Initialize NSS db for the user. 45 ASSERT_TRUE(test_nssdb_.is_open());
55 ASSERT_TRUE(user_.constructed_successfully()); 46 // Use the same DB for public and private slot.
56 user_.FinishInit(); 47 test_nsscertdb_.reset(new net::NSSCertDatabaseChromeOS(
57 test_nssdb_.reset(new net::NSSCertDatabaseChromeOS( 48 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())),
58 crypto::GetPublicSlotForChromeOSUser(user_.username_hash()), 49 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot()))));
59 crypto::GetPrivateSlotForChromeOSUser( 50 test_nsscertdb_->SetSlowTaskRunnerForTest(
60 user_.username_hash(), 51 message_loop_.message_loop_proxy());
61 base::Callback<void(crypto::ScopedPK11Slot)>())));
62 test_nssdb_->SetSlowTaskRunnerForTest(message_loop_.message_loop_proxy());
63 52
64 DBusThreadManager::Initialize(); 53 DBusThreadManager::Initialize();
65 service_test_ = 54 service_test_ =
66 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 55 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
67 DBusThreadManager::Get() 56 DBusThreadManager::Get()
68 ->GetShillProfileClient() 57 ->GetShillProfileClient()
69 ->GetTestInterface() 58 ->GetTestInterface()
70 ->AddProfile(kProfile, "" /* userhash */); 59 ->AddProfile(kProfile, "" /* userhash */);
71 base::RunLoop().RunUntilIdle(); 60 base::RunLoop().RunUntilIdle();
72 service_test_->ClearServices(); 61 service_test_->ClearServices();
73 base::RunLoop().RunUntilIdle(); 62 base::RunLoop().RunUntilIdle();
74 63
75 CertLoader::Initialize(); 64 CertLoader::Initialize();
76 CertLoader* cert_loader_ = CertLoader::Get(); 65 CertLoader* cert_loader_ = CertLoader::Get();
77 cert_loader_->StartWithNSSDB(test_nssdb_.get()); 66 cert_loader_->StartWithNSSDB(test_nsscertdb_.get());
78 } 67 }
79 68
80 virtual void TearDown() override { 69 void TearDown() override {
81 network_cert_migrator_.reset(); 70 network_cert_migrator_.reset();
82 network_state_handler_.reset(); 71 network_state_handler_.reset();
83 CertLoader::Shutdown(); 72 CertLoader::Shutdown();
84 DBusThreadManager::Shutdown(); 73 DBusThreadManager::Shutdown();
85 CleanupTestCert();
86 } 74 }
87 75
88 protected: 76 protected:
89 void SetupTestCACert() {
90 scoped_refptr<net::X509Certificate> cert_wo_nickname =
91 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(),
92 "eku-test-root.pem",
93 net::X509Certificate::FORMAT_AUTO)
94 .back();
95 net::X509Certificate::GetPEMEncoded(cert_wo_nickname->os_cert_handle(),
96 &test_ca_cert_pem_);
97 std::string der_encoded;
98 net::X509Certificate::GetDEREncoded(cert_wo_nickname->os_cert_handle(),
99 &der_encoded);
100 cert_wo_nickname = NULL;
101
102 test_ca_cert_ = net::X509Certificate::CreateFromBytesWithNickname(
103 der_encoded.data(), der_encoded.size(), kNSSNickname);
104 net::CertificateList cert_list;
105 cert_list.push_back(test_ca_cert_);
106 net::NSSCertDatabase::ImportCertFailureList failures;
107 EXPECT_TRUE(test_nssdb_->ImportCACerts(
108 cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures));
109 ASSERT_TRUE(failures.empty()) << net::ErrorToString(failures[0].net_error);
110 }
111
112 void SetupTestClientCert() { 77 void SetupTestClientCert() {
113 std::string pkcs12_data; 78 test_client_cert_ = net::ImportClientCertAndKeyFromFile(
114 ASSERT_TRUE(base::ReadFileToString( 79 net::GetTestCertsDirectory(), "client_1.pem", "client_1.pk8",
115 net::GetTestCertsDirectory().Append("websocket_client_cert.p12"), 80 test_nssdb_.slot());
116 &pkcs12_data)); 81 ASSERT_TRUE(test_client_cert_.get());
117
118 net::CertificateList client_cert_list;
119 scoped_refptr<net::CryptoModule> module(net::CryptoModule::CreateFromHandle(
120 test_nssdb_->GetPrivateSlot().get()));
121 ASSERT_EQ(net::OK,
122 test_nssdb_->ImportFromPKCS12(module.get(),
123 pkcs12_data,
124 base::string16(),
125 false,
126 &client_cert_list));
127 ASSERT_TRUE(!client_cert_list.empty());
128 test_client_cert_ = client_cert_list[0];
129 82
130 int slot_id = -1; 83 int slot_id = -1;
131 test_client_cert_pkcs11_id_ = CertLoader::GetPkcs11IdAndSlotForCert( 84 test_client_cert_pkcs11_id_ = CertLoader::GetPkcs11IdAndSlotForCert(
132 *test_client_cert_, &slot_id); 85 *test_client_cert_, &slot_id);
133 ASSERT_FALSE(test_client_cert_pkcs11_id_.empty()); 86 ASSERT_FALSE(test_client_cert_pkcs11_id_.empty());
134 ASSERT_NE(-1, slot_id); 87 ASSERT_NE(-1, slot_id);
135 test_client_cert_slot_id_ = base::IntToString(slot_id); 88 test_client_cert_slot_id_ = base::IntToString(slot_id);
136 } 89 }
137 90
138 void SetupNetworkHandlers() { 91 void SetupNetworkHandlers() {
(...skipping 11 matching lines...) Expand all
150 type, 103 type,
151 state, 104 state,
152 true /* add_to_visible */); 105 true /* add_to_visible */);
153 106
154 // Ensure that the service appears as 'configured', i.e. is associated to a 107 // Ensure that the service appears as 'configured', i.e. is associated to a
155 // Shill profile. 108 // Shill profile.
156 service_test_->SetServiceProperty( 109 service_test_->SetServiceProperty(
157 network_id, shill::kProfileProperty, base::StringValue(kProfile)); 110 network_id, shill::kProfileProperty, base::StringValue(kProfile));
158 } 111 }
159 112
160 void SetupWifiWithNss() {
161 AddService(kWifiStub, shill::kTypeWifi, shill::kStateOnline);
162 service_test_->SetServiceProperty(kWifiStub,
163 shill::kEapCaCertNssProperty,
164 base::StringValue(kNSSNickname));
165 }
166
167 void SetupNetworkWithEapCertId(bool wifi, const std::string& cert_id) { 113 void SetupNetworkWithEapCertId(bool wifi, const std::string& cert_id) {
168 std::string type = wifi ? shill::kTypeWifi: shill::kTypeEthernetEap; 114 std::string type = wifi ? shill::kTypeWifi: shill::kTypeEthernetEap;
169 std::string name = wifi ? kWifiStub : kEthernetEapStub; 115 std::string name = wifi ? kWifiStub : kEthernetEapStub;
170 AddService(name, type, shill::kStateOnline); 116 AddService(name, type, shill::kStateOnline);
171 service_test_->SetServiceProperty( 117 service_test_->SetServiceProperty(
172 name, shill::kEapCertIdProperty, base::StringValue(cert_id)); 118 name, shill::kEapCertIdProperty, base::StringValue(cert_id));
173 service_test_->SetServiceProperty( 119 service_test_->SetServiceProperty(
174 name, shill::kEapKeyIdProperty, base::StringValue(cert_id)); 120 name, shill::kEapKeyIdProperty, base::StringValue(cert_id));
175 121
176 if (wifi) { 122 if (wifi) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 161
216 void GetVpnCertId(bool open_vpn, 162 void GetVpnCertId(bool open_vpn,
217 std::string* slot_id, 163 std::string* slot_id,
218 std::string* pkcs11_id) { 164 std::string* pkcs11_id) {
219 slot_id->clear(); 165 slot_id->clear();
220 pkcs11_id->clear(); 166 pkcs11_id->clear();
221 167
222 const base::DictionaryValue* properties = 168 const base::DictionaryValue* properties =
223 service_test_->GetServiceProperties(kVPNStub); 169 service_test_->GetServiceProperties(kVPNStub);
224 ASSERT_TRUE(properties); 170 ASSERT_TRUE(properties);
225 const base::DictionaryValue* provider = NULL; 171 const base::DictionaryValue* provider = nullptr;
226 properties->GetDictionaryWithoutPathExpansion(shill::kProviderProperty, 172 properties->GetDictionaryWithoutPathExpansion(shill::kProviderProperty,
227 &provider); 173 &provider);
228 if (!provider) 174 if (!provider)
229 return; 175 return;
230 if (open_vpn) { 176 if (open_vpn) {
231 provider->GetStringWithoutPathExpansion( 177 provider->GetStringWithoutPathExpansion(
232 shill::kOpenVPNClientCertIdProperty, pkcs11_id); 178 shill::kOpenVPNClientCertIdProperty, pkcs11_id);
233 } else { 179 } else {
234 provider->GetStringWithoutPathExpansion( 180 provider->GetStringWithoutPathExpansion(
235 shill::kL2tpIpsecClientCertSlotProperty, slot_id); 181 shill::kL2tpIpsecClientCertSlotProperty, slot_id);
236 provider->GetStringWithoutPathExpansion( 182 provider->GetStringWithoutPathExpansion(
237 shill::kL2tpIpsecClientCertIdProperty, pkcs11_id); 183 shill::kL2tpIpsecClientCertIdProperty, pkcs11_id);
238 } 184 }
239 } 185 }
240 186
241 void GetEapCACertProperties(std::string* nss_nickname, std::string* ca_pem) {
242 nss_nickname->clear();
243 ca_pem->clear();
244 const base::DictionaryValue* properties =
245 service_test_->GetServiceProperties(kWifiStub);
246 properties->GetStringWithoutPathExpansion(shill::kEapCaCertNssProperty,
247 nss_nickname);
248 const base::ListValue* ca_pems = NULL;
249 properties->GetListWithoutPathExpansion(shill::kEapCaCertPemProperty,
250 &ca_pems);
251 if (ca_pems && !ca_pems->empty())
252 ca_pems->GetString(0, ca_pem);
253 }
254
255 void SetupVpnWithNss(bool open_vpn) {
256 AddService(kVPNStub, shill::kTypeVPN, shill::kStateIdle);
257 base::DictionaryValue provider;
258 const char* nss_property = open_vpn ? shill::kOpenVPNCaCertNSSProperty
259 : shill::kL2tpIpsecCaCertNssProperty;
260 provider.SetStringWithoutPathExpansion(nss_property, kNSSNickname);
261 service_test_->SetServiceProperty(
262 kVPNStub, shill::kProviderProperty, provider);
263 }
264
265 void GetVpnCACertProperties(bool open_vpn,
266 std::string* nss_nickname,
267 std::string* ca_pem) {
268 nss_nickname->clear();
269 ca_pem->clear();
270 const base::DictionaryValue* properties =
271 service_test_->GetServiceProperties(kVPNStub);
272 const base::DictionaryValue* provider = NULL;
273 properties->GetDictionaryWithoutPathExpansion(shill::kProviderProperty,
274 &provider);
275 if (!provider)
276 return;
277 const char* nss_property = open_vpn ? shill::kOpenVPNCaCertNSSProperty
278 : shill::kL2tpIpsecCaCertNssProperty;
279 provider->GetStringWithoutPathExpansion(nss_property, nss_nickname);
280 const base::ListValue* ca_pems = NULL;
281 const char* pem_property = open_vpn ? shill::kOpenVPNCaCertPemProperty
282 : shill::kL2tpIpsecCaCertPemProperty;
283 provider->GetListWithoutPathExpansion(pem_property, &ca_pems);
284 if (ca_pems && !ca_pems->empty())
285 ca_pems->GetString(0, ca_pem);
286 }
287
288 ShillServiceClient::TestInterface* service_test_; 187 ShillServiceClient::TestInterface* service_test_;
289 scoped_refptr<net::X509Certificate> test_ca_cert_;
290 scoped_refptr<net::X509Certificate> test_client_cert_; 188 scoped_refptr<net::X509Certificate> test_client_cert_;
291 std::string test_client_cert_pkcs11_id_; 189 std::string test_client_cert_pkcs11_id_;
292 std::string test_client_cert_slot_id_; 190 std::string test_client_cert_slot_id_;
293 std::string test_ca_cert_pem_;
294 base::MessageLoop message_loop_; 191 base::MessageLoop message_loop_;
295 192
296 private: 193 private:
297 void CleanupTestCert() {
298 if (test_ca_cert_.get())
299 ASSERT_TRUE(test_nssdb_->DeleteCertAndKey(test_ca_cert_.get()));
300
301 if (test_client_cert_.get())
302 ASSERT_TRUE(test_nssdb_->DeleteCertAndKey(test_client_cert_.get()));
303 }
304
305 scoped_ptr<NetworkStateHandler> network_state_handler_; 194 scoped_ptr<NetworkStateHandler> network_state_handler_;
306 scoped_ptr<NetworkCertMigrator> network_cert_migrator_; 195 scoped_ptr<NetworkCertMigrator> network_cert_migrator_;
307 crypto::ScopedTestNSSChromeOSUser user_; 196 crypto::ScopedTestNSSDB test_nssdb_;
308 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; 197 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nsscertdb_;
309 198
310 DISALLOW_COPY_AND_ASSIGN(NetworkCertMigratorTest); 199 DISALLOW_COPY_AND_ASSIGN(NetworkCertMigratorTest);
311 }; 200 };
312 201
313 TEST_F(NetworkCertMigratorTest, MigrateNssOnInitialization) { 202 TEST_F(NetworkCertMigratorTest, MigrateOnInitialization) {
314 // Add a new network for migration before the handlers are initialized. 203 SetupTestClientCert();
315 SetupWifiWithNss(); 204 // Add a network for migration before the handlers are initialized.
316 SetupTestCACert(); 205 SetupNetworkWithEapCertId(true /* wifi */,
317 SetupNetworkHandlers(); 206 "123:" + test_client_cert_pkcs11_id_);
318
319 base::RunLoop().RunUntilIdle();
320 std::string nss_nickname, ca_pem;
321 GetEapCACertProperties(&nss_nickname, &ca_pem);
322 EXPECT_TRUE(nss_nickname.empty());
323 EXPECT_EQ(test_ca_cert_pem_, ca_pem);
324 }
325
326 TEST_F(NetworkCertMigratorTest, MigrateNssOnNetworkAppearance) {
327 SetupTestCACert();
328 SetupNetworkHandlers(); 207 SetupNetworkHandlers();
329 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
330 209
331 // Add a new network for migration after the handlers are initialized. 210 std::string cert_id;
332 SetupWifiWithNss(); 211 GetEapCertId(true /* wifi */, &cert_id);
333 212 std::string expected_cert_id =
334 base::RunLoop().RunUntilIdle(); 213 test_client_cert_slot_id_ + ":" + test_client_cert_pkcs11_id_;
335 std::string nss_nickname, ca_pem; 214 EXPECT_EQ(expected_cert_id, cert_id);
336 GetEapCACertProperties(&nss_nickname, &ca_pem);
337 EXPECT_TRUE(nss_nickname.empty());
338 EXPECT_EQ(test_ca_cert_pem_, ca_pem);
339 }
340
341 TEST_F(NetworkCertMigratorTest, DoNotMigrateNssIfPemSet) {
342 // Add a new network with an already set PEM property.
343 SetupWifiWithNss();
344 base::ListValue ca_pems;
345 ca_pems.AppendString(kFakePEM);
346 service_test_->SetServiceProperty(
347 kWifiStub, shill::kEapCaCertPemProperty, ca_pems);
348
349 SetupTestCACert();
350 SetupNetworkHandlers();
351 base::RunLoop().RunUntilIdle();
352
353 std::string nss_nickname, ca_pem;
354 GetEapCACertProperties(&nss_nickname, &ca_pem);
355 EXPECT_TRUE(nss_nickname.empty());
356 EXPECT_EQ(kFakePEM, ca_pem);
357 }
358
359 TEST_F(NetworkCertMigratorTest, MigrateNssOpenVpn) {
360 // Add a new network for migration before the handlers are initialized.
361 SetupVpnWithNss(true /* OpenVPN */);
362
363 SetupTestCACert();
364 SetupNetworkHandlers();
365
366 base::RunLoop().RunUntilIdle();
367 std::string nss_nickname, ca_pem;
368 GetVpnCACertProperties(true /* OpenVPN */, &nss_nickname, &ca_pem);
369 EXPECT_TRUE(nss_nickname.empty());
370 EXPECT_EQ(test_ca_cert_pem_, ca_pem);
371 }
372
373 TEST_F(NetworkCertMigratorTest, MigrateNssIpsecVpn) {
374 // Add a new network for migration before the handlers are initialized.
375 SetupVpnWithNss(false /* not OpenVPN */);
376
377 SetupTestCACert();
378 SetupNetworkHandlers();
379
380 base::RunLoop().RunUntilIdle();
381 std::string nss_nickname, ca_pem;
382 GetVpnCACertProperties(false /* not OpenVPN */, &nss_nickname, &ca_pem);
383 EXPECT_TRUE(nss_nickname.empty());
384 EXPECT_EQ(test_ca_cert_pem_, ca_pem);
385 } 215 }
386 216
387 TEST_F(NetworkCertMigratorTest, MigrateEapCertIdNoMatchingCert) { 217 TEST_F(NetworkCertMigratorTest, MigrateEapCertIdNoMatchingCert) {
388 SetupTestClientCert(); 218 SetupTestClientCert();
389 SetupNetworkHandlers(); 219 SetupNetworkHandlers();
390 base::RunLoop().RunUntilIdle(); 220 base::RunLoop().RunUntilIdle();
391 221
392 // Add a new network for migration after the handlers are initialized. 222 // Add a new network for migration after the handlers are initialized.
393 SetupNetworkWithEapCertId(true /* wifi */, "unknown pkcs11 id"); 223 SetupNetworkWithEapCertId(true /* wifi */, "unknown pkcs11 id");
394 224
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 base::RunLoop().RunUntilIdle(); 331 base::RunLoop().RunUntilIdle();
502 332
503 std::string pkcs11_id; 333 std::string pkcs11_id;
504 std::string slot_id; 334 std::string slot_id;
505 GetVpnCertId(false /* IPsec */, &slot_id, &pkcs11_id); 335 GetVpnCertId(false /* IPsec */, &slot_id, &pkcs11_id);
506 EXPECT_EQ(test_client_cert_pkcs11_id_, pkcs11_id); 336 EXPECT_EQ(test_client_cert_pkcs11_id_, pkcs11_id);
507 EXPECT_EQ(test_client_cert_slot_id_, slot_id); 337 EXPECT_EQ(test_client_cert_slot_id_, slot_id);
508 } 338 }
509 339
510 } // namespace chromeos 340 } // namespace chromeos
511
512 #endif
OLDNEW
« no previous file with comments | « chromeos/network/network_cert_migrator.cc ('k') | chromeos/network/network_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698