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
3 // found in the LICENSE file.
4 #include "chromeos/network/client_cert_resolver.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_reader.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chromeos/cert_loader.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/shill_manager_client.h"
20 #include "chromeos/dbus/shill_profile_client.h"
21 #include "chromeos/dbus/shill_service_client.h"
22 #include "chromeos/network/managed_network_configuration_handler_impl.h"
23 #include "chromeos/network/network_configuration_handler.h"
24 #include "chromeos/network/network_profile_handler.h"
25 #include "chromeos/network/network_state_handler.h"
26 #include "components/onc/onc_constants.h"
27 #include "crypto/scoped_nss_types.h"
28 #include "crypto/scoped_test_nss_db.h"
29 #include "net/base/net_errors.h"
30 #include "net/base/test_data_directory.h"
31 #include "net/cert/nss_cert_database_chromeos.h"
32 #include "net/cert/x509_certificate.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/cros_system_api/dbus/service_constants.h"
41 const char* kWifiStub
= "wifi_stub";
42 const char* kWifiSSID
= "wifi_ssid";
43 const char* kUserProfilePath
= "user_profile";
44 const char* kUserHash
= "user_hash";
48 class ClientCertResolverTest
: public testing::Test
,
49 public ClientCertResolver::Observer
{
51 ClientCertResolverTest()
52 : network_properties_changed_count_(0),
56 ~ClientCertResolverTest() override
{}
58 void SetUp() override
{
59 ASSERT_TRUE(test_nssdb_
.is_open());
61 // Use the same DB for public and private slot.
62 test_nsscertdb_
.reset(new net::NSSCertDatabaseChromeOS(
63 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_
.slot())),
64 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_
.slot()))));
65 test_nsscertdb_
->SetSlowTaskRunnerForTest(message_loop_
.task_runner());
67 DBusThreadManager::Initialize();
69 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
71 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
72 profile_test_
->AddProfile(kUserProfilePath
, kUserHash
);
73 base::RunLoop().RunUntilIdle();
74 service_test_
->ClearServices();
75 base::RunLoop().RunUntilIdle();
77 CertLoader::Initialize();
78 cert_loader_
= CertLoader::Get();
79 CertLoader::ForceHardwareBackedForTesting();
82 void TearDown() override
{
83 client_cert_resolver_
->RemoveObserver(this);
84 client_cert_resolver_
.reset();
85 managed_config_handler_
.reset();
86 network_config_handler_
.reset();
87 network_profile_handler_
.reset();
88 network_state_handler_
.reset();
89 CertLoader::Shutdown();
90 DBusThreadManager::Shutdown();
94 void StartCertLoader() {
95 cert_loader_
->StartWithNSSDB(test_nsscertdb_
.get());
96 if (test_client_cert_
.get()) {
98 const std::string pkcs11_id
=
99 CertLoader::GetPkcs11IdAndSlotForCert(*test_client_cert_
, &slot_id
);
100 test_cert_id_
= base::StringPrintf("%i:%s", slot_id
, pkcs11_id
.c_str());
104 // Imports a CA cert (stored as PEM in test_ca_cert_pem_) and a client
105 // certificate signed by that CA. Its PKCS#11 ID is stored in
107 void SetupTestCerts() {
109 net::CertificateList ca_cert_list
=
110 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(),
112 net::X509Certificate::FORMAT_AUTO
);
113 ASSERT_TRUE(!ca_cert_list
.empty());
114 net::NSSCertDatabase::ImportCertFailureList failures
;
115 EXPECT_TRUE(test_nsscertdb_
->ImportCACerts(
116 ca_cert_list
, net::NSSCertDatabase::TRUST_DEFAULT
, &failures
));
117 ASSERT_TRUE(failures
.empty()) << net::ErrorToString(failures
[0].net_error
);
119 net::X509Certificate::GetPEMEncoded(ca_cert_list
[0]->os_cert_handle(),
121 ASSERT_TRUE(!test_ca_cert_pem_
.empty());
123 // Import a client cert signed by that CA.
125 net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
129 ASSERT_TRUE(test_client_cert_
.get());
132 void SetupNetworkHandlers() {
133 network_state_handler_
.reset(NetworkStateHandler::InitializeForTest());
134 network_profile_handler_
.reset(new NetworkProfileHandler());
135 network_config_handler_
.reset(new NetworkConfigurationHandler());
136 managed_config_handler_
.reset(new ManagedNetworkConfigurationHandlerImpl());
137 client_cert_resolver_
.reset(new ClientCertResolver());
139 network_profile_handler_
->Init();
140 network_config_handler_
->Init(network_state_handler_
.get(),
141 NULL
/* network_device_handler */);
142 managed_config_handler_
->Init(network_state_handler_
.get(),
143 network_profile_handler_
.get(),
144 network_config_handler_
.get(),
145 NULL
/* network_device_handler */);
146 // Run all notifications before starting the cert loader to reduce run time.
147 base::RunLoop().RunUntilIdle();
149 client_cert_resolver_
->Init(network_state_handler_
.get(),
150 managed_config_handler_
.get());
151 client_cert_resolver_
->AddObserver(this);
152 client_cert_resolver_
->SetSlowTaskRunnerForTest(
153 message_loop_
.task_runner());
157 service_test_
->SetServiceProperties(kWifiStub
,
163 // Set an arbitrary cert id, so that we can check afterwards whether we
164 // cleared the property or not.
165 service_test_
->SetServiceProperty(
166 kWifiStub
, shill::kEapCertIdProperty
, base::StringValue("invalid id"));
167 profile_test_
->AddService(kUserProfilePath
, kWifiStub
);
169 DBusThreadManager::Get()
170 ->GetShillManagerClient()
172 ->AddManagerService(kWifiStub
, true);
175 // Setup a policy with a certificate pattern that matches any client cert that
176 // is signed by the test CA cert (stored in |test_ca_cert_pem_|). In
177 // particular it will match the test client cert.
179 const char* kTestPolicyTemplate
=
180 "[ { \"GUID\": \"wifi_stub\","
181 " \"Name\": \"wifi_stub\","
182 " \"Type\": \"WiFi\","
184 " \"Security\": \"WPA-EAP\","
185 " \"SSID\": \"wifi_ssid\","
187 " \"Outer\": \"EAP-TLS\","
188 " \"ClientCertType\": \"Pattern\","
189 " \"ClientCertPattern\": {"
190 " \"IssuerCAPEMs\": [ \"%s\" ]"
195 std::string policy_json
=
196 base::StringPrintf(kTestPolicyTemplate
, test_ca_cert_pem_
.c_str());
199 scoped_ptr
<base::Value
> policy_value(
200 base::JSONReader::DeprecatedReadAndReturnError(
201 policy_json
, base::JSON_ALLOW_TRAILING_COMMAS
, NULL
, &error
));
202 ASSERT_TRUE(policy_value
) << error
;
204 base::ListValue
* policy
= NULL
;
205 ASSERT_TRUE(policy_value
->GetAsList(&policy
));
207 managed_config_handler_
->SetPolicy(
208 onc::ONC_SOURCE_USER_POLICY
,
211 base::DictionaryValue() /* no global network config */);
214 void GetClientCertProperties(std::string
* pkcs11_id
) {
216 const base::DictionaryValue
* properties
=
217 service_test_
->GetServiceProperties(kWifiStub
);
220 properties
->GetStringWithoutPathExpansion(shill::kEapCertIdProperty
,
224 int network_properties_changed_count_
;
225 std::string test_cert_id_
;
226 scoped_ptr
<ClientCertResolver
> client_cert_resolver_
;
229 // ClientCertResolver::Observer:
230 void ResolveRequestCompleted(bool network_properties_changed
) override
{
231 if (network_properties_changed
)
232 ++network_properties_changed_count_
;
235 ShillServiceClient::TestInterface
* service_test_
;
236 ShillProfileClient::TestInterface
* profile_test_
;
237 CertLoader
* cert_loader_
;
238 scoped_ptr
<NetworkStateHandler
> network_state_handler_
;
239 scoped_ptr
<NetworkProfileHandler
> network_profile_handler_
;
240 scoped_ptr
<NetworkConfigurationHandler
> network_config_handler_
;
241 scoped_ptr
<ManagedNetworkConfigurationHandlerImpl
> managed_config_handler_
;
242 base::MessageLoop message_loop_
;
243 scoped_refptr
<net::X509Certificate
> test_client_cert_
;
244 std::string test_ca_cert_pem_
;
245 crypto::ScopedTestNSSDB test_nssdb_
;
246 scoped_ptr
<net::NSSCertDatabaseChromeOS
> test_nsscertdb_
;
248 DISALLOW_COPY_AND_ASSIGN(ClientCertResolverTest
);
251 TEST_F(ClientCertResolverTest
, NoMatchingCertificates
) {
254 base::RunLoop().RunUntilIdle();
255 network_properties_changed_count_
= 0;
256 SetupNetworkHandlers();
258 base::RunLoop().RunUntilIdle();
260 // Verify that no client certificate was configured.
261 std::string pkcs11_id
;
262 GetClientCertProperties(&pkcs11_id
);
263 EXPECT_EQ(std::string(), pkcs11_id
);
264 EXPECT_EQ(1, network_properties_changed_count_
);
265 EXPECT_FALSE(client_cert_resolver_
->IsAnyResolveTaskRunning());
268 TEST_F(ClientCertResolverTest
, ResolveOnCertificatesLoaded
) {
271 base::RunLoop().RunUntilIdle();
273 SetupNetworkHandlers();
275 base::RunLoop().RunUntilIdle();
277 network_properties_changed_count_
= 0;
279 base::RunLoop().RunUntilIdle();
281 // Verify that the resolver positively matched the pattern in the policy with
282 // the test client cert and configured the network.
283 std::string pkcs11_id
;
284 GetClientCertProperties(&pkcs11_id
);
285 EXPECT_EQ(test_cert_id_
, pkcs11_id
);
286 EXPECT_EQ(1, network_properties_changed_count_
);
289 TEST_F(ClientCertResolverTest
, ResolveAfterPolicyApplication
) {
292 base::RunLoop().RunUntilIdle();
294 SetupNetworkHandlers();
295 base::RunLoop().RunUntilIdle();
297 // Policy application will trigger the ClientCertResolver.
298 network_properties_changed_count_
= 0;
300 base::RunLoop().RunUntilIdle();
302 // Verify that the resolver positively matched the pattern in the policy with
303 // the test client cert and configured the network.
304 std::string pkcs11_id
;
305 GetClientCertProperties(&pkcs11_id
);
306 EXPECT_EQ(test_cert_id_
, pkcs11_id
);
307 EXPECT_EQ(1, network_properties_changed_count_
);
310 } // namespace chromeos