| 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 "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 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "chromeos/cert_loader.h" | 12 #include "chromeos/cert_loader.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "chromeos/dbus/shill_service_client.h" | 14 #include "chromeos/dbus/shill_service_client.h" |
| 15 #include "chromeos/login/login_state.h" | |
| 16 #include "chromeos/network/network_state_handler.h" | 15 #include "chromeos/network/network_state_handler.h" |
| 17 #include "chromeos/tpm_token_loader.h" | 16 #include "chromeos/tpm_token_loader.h" |
| 18 #include "crypto/nss_util.h" | 17 #include "crypto/nss_util.h" |
| 18 #include "crypto/nss_util_internal.h" |
| 19 #include "net/base/crypto_module.h" | 19 #include "net/base/crypto_module.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/test_data_directory.h" | 21 #include "net/base/test_data_directory.h" |
| 22 #include "net/cert/nss_cert_database.h" | 22 #include "net/cert/nss_cert_database_chromeos.h" |
| 23 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
| 24 #include "net/test/cert_test_util.h" | 24 #include "net/test/cert_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/cros_system_api/dbus/service_constants.h" | 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 27 | 27 |
| 28 namespace chromeos { | 28 namespace chromeos { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const char* kWifiStub = "wifi_stub"; | 32 const char* kWifiStub = "wifi_stub"; |
| 33 const char* kVPNStub = "vpn_stub"; | 33 const char* kVPNStub = "vpn_stub"; |
| 34 const char* kNSSNickname = "nss_nickname"; | 34 const char* kNSSNickname = "nss_nickname"; |
| 35 const char* kFakePEM = "pem"; | 35 const char* kFakePEM = "pem"; |
| 36 | 36 |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 class NetworkCertMigratorTest : public testing::Test { | 39 class NetworkCertMigratorTest : public testing::Test { |
| 40 public: | 40 public: |
| 41 NetworkCertMigratorTest() {} | 41 NetworkCertMigratorTest() : service_test_(NULL), |
| 42 user_("user_hash") { |
| 43 } |
| 42 virtual ~NetworkCertMigratorTest() {} | 44 virtual ~NetworkCertMigratorTest() {} |
| 43 | 45 |
| 44 virtual void SetUp() OVERRIDE { | 46 virtual void SetUp() OVERRIDE { |
| 45 ASSERT_TRUE(test_nssdb_.is_open()); | 47 // Initialize NSS db for the user. |
| 46 slot_ = net::NSSCertDatabase::GetInstance()->GetPublicModule(); | 48 ASSERT_TRUE(user_.constructed_successfully()); |
| 47 ASSERT_TRUE(slot_->os_module_handle()); | 49 user_.FinishInit(); |
| 48 | 50 test_nssdb_.reset(new net::NSSCertDatabaseChromeOS( |
| 49 LoginState::Initialize(); | 51 crypto::GetPublicSlotForChromeOSUser(user_.username_hash()), |
| 52 crypto::GetPrivateSlotForChromeOSUser( |
| 53 user_.username_hash(), |
| 54 base::Callback<void(crypto::ScopedPK11Slot)>()))); |
| 50 | 55 |
| 51 DBusThreadManager::InitializeWithStub(); | 56 DBusThreadManager::InitializeWithStub(); |
| 52 service_test_ = | 57 service_test_ = |
| 53 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 58 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 54 message_loop_.RunUntilIdle(); | 59 base::RunLoop().RunUntilIdle(); |
| 55 service_test_->ClearServices(); | 60 service_test_->ClearServices(); |
| 56 message_loop_.RunUntilIdle(); | 61 base::RunLoop().RunUntilIdle(); |
| 57 | |
| 58 TPMTokenLoader::Initialize(); | |
| 59 TPMTokenLoader* tpm_token_loader = TPMTokenLoader::Get(); | |
| 60 tpm_token_loader->InitializeTPMForTest(); | |
| 61 tpm_token_loader->SetCryptoTaskRunner(message_loop_.message_loop_proxy()); | |
| 62 | 62 |
| 63 CertLoader::Initialize(); | 63 CertLoader::Initialize(); |
| 64 CertLoader::Get()->SetSlowTaskRunnerForTest( | 64 CertLoader* cert_loader_ = CertLoader::Get(); |
| 65 message_loop_.message_loop_proxy()); | 65 cert_loader_->SetSlowTaskRunnerForTest(message_loop_.message_loop_proxy()); |
| 66 cert_loader_->StartWithNSSDB(test_nssdb_.get()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 virtual void TearDown() OVERRIDE { | 69 virtual void TearDown() OVERRIDE { |
| 69 network_cert_migrator_.reset(); | 70 network_cert_migrator_.reset(); |
| 70 network_state_handler_.reset(); | 71 network_state_handler_.reset(); |
| 71 CertLoader::Shutdown(); | 72 CertLoader::Shutdown(); |
| 72 TPMTokenLoader::Shutdown(); | |
| 73 DBusThreadManager::Shutdown(); | 73 DBusThreadManager::Shutdown(); |
| 74 LoginState::Shutdown(); | |
| 75 CleanupTestCert(); | 74 CleanupTestCert(); |
| 76 } | 75 } |
| 77 | 76 |
| 78 protected: | 77 protected: |
| 79 void SetupTestCACert() { | 78 void SetupTestCACert() { |
| 80 scoped_refptr<net::X509Certificate> cert_wo_nickname = | 79 scoped_refptr<net::X509Certificate> cert_wo_nickname = |
| 81 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), | 80 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), |
| 82 "eku-test-root.pem", | 81 "eku-test-root.pem", |
| 83 net::X509Certificate::FORMAT_AUTO) | 82 net::X509Certificate::FORMAT_AUTO) |
| 84 .back(); | 83 .back(); |
| 85 net::X509Certificate::GetPEMEncoded(cert_wo_nickname->os_cert_handle(), | 84 net::X509Certificate::GetPEMEncoded(cert_wo_nickname->os_cert_handle(), |
| 86 &test_ca_cert_pem_); | 85 &test_ca_cert_pem_); |
| 87 std::string der_encoded; | 86 std::string der_encoded; |
| 88 net::X509Certificate::GetDEREncoded(cert_wo_nickname->os_cert_handle(), | 87 net::X509Certificate::GetDEREncoded(cert_wo_nickname->os_cert_handle(), |
| 89 &der_encoded); | 88 &der_encoded); |
| 90 cert_wo_nickname = NULL; | 89 cert_wo_nickname = NULL; |
| 91 | 90 |
| 92 test_ca_cert_ = net::X509Certificate::CreateFromBytesWithNickname( | 91 test_ca_cert_ = net::X509Certificate::CreateFromBytesWithNickname( |
| 93 der_encoded.data(), der_encoded.size(), kNSSNickname); | 92 der_encoded.data(), der_encoded.size(), kNSSNickname); |
| 94 net::NSSCertDatabase* cert_database = net::NSSCertDatabase::GetInstance(); | |
| 95 net::CertificateList cert_list; | 93 net::CertificateList cert_list; |
| 96 cert_list.push_back(test_ca_cert_); | 94 cert_list.push_back(test_ca_cert_); |
| 97 net::NSSCertDatabase::ImportCertFailureList failures; | 95 net::NSSCertDatabase::ImportCertFailureList failures; |
| 98 EXPECT_TRUE(cert_database->ImportCACerts( | 96 EXPECT_TRUE(test_nssdb_->ImportCACerts( |
| 99 cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures)); | 97 cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures)); |
| 100 ASSERT_TRUE(failures.empty()) << net::ErrorToString(failures[0].net_error); | 98 ASSERT_TRUE(failures.empty()) << net::ErrorToString(failures[0].net_error); |
| 101 } | 99 } |
| 102 | 100 |
| 103 void SetupNetworkHandlers() { | 101 void SetupNetworkHandlers() { |
| 104 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); | 102 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); |
| 105 network_cert_migrator_.reset(new NetworkCertMigrator); | 103 network_cert_migrator_.reset(new NetworkCertMigrator); |
| 106 network_cert_migrator_->Init(network_state_handler_.get()); | 104 network_cert_migrator_->Init(network_state_handler_.get()); |
| 107 } | 105 } |
| 108 | 106 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 ca_pems->GetString(0, ca_pem); | 172 ca_pems->GetString(0, ca_pem); |
| 175 } | 173 } |
| 176 | 174 |
| 177 ShillServiceClient::TestInterface* service_test_; | 175 ShillServiceClient::TestInterface* service_test_; |
| 178 scoped_refptr<net::X509Certificate> test_ca_cert_; | 176 scoped_refptr<net::X509Certificate> test_ca_cert_; |
| 179 std::string test_ca_cert_pem_; | 177 std::string test_ca_cert_pem_; |
| 180 base::MessageLoop message_loop_; | 178 base::MessageLoop message_loop_; |
| 181 | 179 |
| 182 private: | 180 private: |
| 183 void CleanupTestCert() { | 181 void CleanupTestCert() { |
| 184 ASSERT_TRUE(net::NSSCertDatabase::GetInstance()->DeleteCertAndKey( | 182 ASSERT_TRUE(test_nssdb_->DeleteCertAndKey(test_ca_cert_.get())); |
| 185 test_ca_cert_.get())); | |
| 186 } | 183 } |
| 187 | 184 |
| 188 scoped_ptr<NetworkStateHandler> network_state_handler_; | 185 scoped_ptr<NetworkStateHandler> network_state_handler_; |
| 189 scoped_ptr<NetworkCertMigrator> network_cert_migrator_; | 186 scoped_ptr<NetworkCertMigrator> network_cert_migrator_; |
| 190 scoped_refptr<net::CryptoModule> slot_; | 187 crypto::ScopedTestNSSChromeOSUser user_; |
| 191 crypto::ScopedTestNSSDB test_nssdb_; | 188 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; |
| 192 | 189 |
| 193 DISALLOW_COPY_AND_ASSIGN(NetworkCertMigratorTest); | 190 DISALLOW_COPY_AND_ASSIGN(NetworkCertMigratorTest); |
| 194 }; | 191 }; |
| 195 | 192 |
| 196 TEST_F(NetworkCertMigratorTest, MigrateNssOnInitialization) { | 193 TEST_F(NetworkCertMigratorTest, MigrateNssOnInitialization) { |
| 197 // Add a new network for migration before the handlers are initialized. | 194 // Add a new network for migration before the handlers are initialized. |
| 198 SetupWifiWithNss(); | 195 SetupWifiWithNss(); |
| 199 SetupTestCACert(); | 196 SetupTestCACert(); |
| 200 SetupNetworkHandlers(); | 197 SetupNetworkHandlers(); |
| 201 | 198 |
| 202 message_loop_.RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
| 203 std::string nss_nickname, ca_pem; | 200 std::string nss_nickname, ca_pem; |
| 204 GetEapCACertProperties(&nss_nickname, &ca_pem); | 201 GetEapCACertProperties(&nss_nickname, &ca_pem); |
| 205 EXPECT_TRUE(nss_nickname.empty()); | 202 EXPECT_TRUE(nss_nickname.empty()); |
| 206 EXPECT_EQ(test_ca_cert_pem_, ca_pem); | 203 EXPECT_EQ(test_ca_cert_pem_, ca_pem); |
| 207 } | 204 } |
| 208 | 205 |
| 209 TEST_F(NetworkCertMigratorTest, MigrateNssOnNetworkAppearance) { | 206 TEST_F(NetworkCertMigratorTest, MigrateNssOnNetworkAppearance) { |
| 210 SetupTestCACert(); | 207 SetupTestCACert(); |
| 211 SetupNetworkHandlers(); | 208 SetupNetworkHandlers(); |
| 212 message_loop_.RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 213 | 210 |
| 214 // Add a new network for migration after the handlers are initialized. | 211 // Add a new network for migration after the handlers are initialized. |
| 215 SetupWifiWithNss(); | 212 SetupWifiWithNss(); |
| 216 | 213 |
| 217 message_loop_.RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
| 218 std::string nss_nickname, ca_pem; | 215 std::string nss_nickname, ca_pem; |
| 219 GetEapCACertProperties(&nss_nickname, &ca_pem); | 216 GetEapCACertProperties(&nss_nickname, &ca_pem); |
| 220 EXPECT_TRUE(nss_nickname.empty()); | 217 EXPECT_TRUE(nss_nickname.empty()); |
| 221 EXPECT_EQ(test_ca_cert_pem_, ca_pem); | 218 EXPECT_EQ(test_ca_cert_pem_, ca_pem); |
| 222 } | 219 } |
| 223 | 220 |
| 224 TEST_F(NetworkCertMigratorTest, DoNotMigrateNssIfPemSet) { | 221 TEST_F(NetworkCertMigratorTest, DoNotMigrateNssIfPemSet) { |
| 225 // Add a new network with an already set PEM property. | 222 // Add a new network with an already set PEM property. |
| 226 SetupWifiWithNss(); | 223 SetupWifiWithNss(); |
| 227 base::ListValue ca_pems; | 224 base::ListValue ca_pems; |
| 228 ca_pems.AppendString(kFakePEM); | 225 ca_pems.AppendString(kFakePEM); |
| 229 service_test_->SetServiceProperty( | 226 service_test_->SetServiceProperty( |
| 230 kWifiStub, shill::kEapCaCertPemProperty, ca_pems); | 227 kWifiStub, shill::kEapCaCertPemProperty, ca_pems); |
| 231 | 228 |
| 232 SetupTestCACert(); | 229 SetupTestCACert(); |
| 233 SetupNetworkHandlers(); | 230 SetupNetworkHandlers(); |
| 234 message_loop_.RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
| 235 | 232 |
| 236 std::string nss_nickname, ca_pem; | 233 std::string nss_nickname, ca_pem; |
| 237 GetEapCACertProperties(&nss_nickname, &ca_pem); | 234 GetEapCACertProperties(&nss_nickname, &ca_pem); |
| 238 EXPECT_TRUE(nss_nickname.empty()); | 235 EXPECT_TRUE(nss_nickname.empty()); |
| 239 EXPECT_EQ(kFakePEM, ca_pem); | 236 EXPECT_EQ(kFakePEM, ca_pem); |
| 240 } | 237 } |
| 241 | 238 |
| 242 TEST_F(NetworkCertMigratorTest, MigrateOpenVpn) { | 239 TEST_F(NetworkCertMigratorTest, MigrateOpenVpn) { |
| 243 // Add a new network for migration before the handlers are initialized. | 240 // Add a new network for migration before the handlers are initialized. |
| 244 SetupVpnWithNss(true /* OpenVPN */); | 241 SetupVpnWithNss(true /* OpenVPN */); |
| 245 | 242 |
| 246 SetupTestCACert(); | 243 SetupTestCACert(); |
| 247 SetupNetworkHandlers(); | 244 SetupNetworkHandlers(); |
| 248 | 245 |
| 249 message_loop_.RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
| 250 std::string nss_nickname, ca_pem; | 247 std::string nss_nickname, ca_pem; |
| 251 GetVpnCACertProperties(true /* OpenVPN */, &nss_nickname, &ca_pem); | 248 GetVpnCACertProperties(true /* OpenVPN */, &nss_nickname, &ca_pem); |
| 252 EXPECT_TRUE(nss_nickname.empty()); | 249 EXPECT_TRUE(nss_nickname.empty()); |
| 253 EXPECT_EQ(test_ca_cert_pem_, ca_pem); | 250 EXPECT_EQ(test_ca_cert_pem_, ca_pem); |
| 254 } | 251 } |
| 255 | 252 |
| 256 TEST_F(NetworkCertMigratorTest, MigrateIpsecVpn) { | 253 TEST_F(NetworkCertMigratorTest, MigrateIpsecVpn) { |
| 257 // Add a new network for migration before the handlers are initialized. | 254 // Add a new network for migration before the handlers are initialized. |
| 258 SetupVpnWithNss(false /* not OpenVPN */); | 255 SetupVpnWithNss(false /* not OpenVPN */); |
| 259 | 256 |
| 260 SetupTestCACert(); | 257 SetupTestCACert(); |
| 261 SetupNetworkHandlers(); | 258 SetupNetworkHandlers(); |
| 262 | 259 |
| 263 message_loop_.RunUntilIdle(); | 260 base::RunLoop().RunUntilIdle(); |
| 264 std::string nss_nickname, ca_pem; | 261 std::string nss_nickname, ca_pem; |
| 265 GetVpnCACertProperties(false /* not OpenVPN */, &nss_nickname, &ca_pem); | 262 GetVpnCACertProperties(false /* not OpenVPN */, &nss_nickname, &ca_pem); |
| 266 EXPECT_TRUE(nss_nickname.empty()); | 263 EXPECT_TRUE(nss_nickname.empty()); |
| 267 EXPECT_EQ(test_ca_cert_pem_, ca_pem); | 264 EXPECT_EQ(test_ca_cert_pem_, ca_pem); |
| 268 } | 265 } |
| 269 | 266 |
| 270 | |
| 271 } // namespace chromeos | 267 } // namespace chromeos |
| OLD | NEW |