summaryrefslogtreecommitdiffstats
path: root/net/ssl/client_cert_store_chromeos_unittest.cc
blob: 0d381ea28831931a8d61931f3bba29d4ab632290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/ssl/client_cert_store_chromeos.h"

#include <string>

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_util.h"
#include "base/run_loop.h"
#include "crypto/nss_util_internal.h"
#include "crypto/rsa_private_key.h"
#include "crypto/scoped_test_nss_chromeos_user.h"
#include "crypto/scoped_test_system_nss_key_slot.h"
#include "net/base/test_data_directory.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/client_cert_store_unittest-inl.h"
#include "net/test/cert_test_util.h"

namespace net {

namespace {

enum ReadFromSlot {
  READ_FROM_SLOT_USER,
  READ_FROM_SLOT_SYSTEM
};

enum SystemSlotAvailability {
  SYSTEM_SLOT_AVAILABILITY_ENABLED,
  SYSTEM_SLOT_AVAILABILITY_DISABLED
};

}  // namespace

// Define a delegate to be used for instantiating the parameterized test set
// ClientCertStoreTest.
template <ReadFromSlot read_from,
          SystemSlotAvailability system_slot_availability>
class ClientCertStoreChromeOSTestDelegate {
 public:
  ClientCertStoreChromeOSTestDelegate()
      : user_("scopeduser"),
        store_(system_slot_availability == SYSTEM_SLOT_AVAILABILITY_ENABLED,
               user_.username_hash(),
               ClientCertStoreChromeOS::PasswordDelegateFactory()) {
    // Defer futher initialization and checks to SelectClientCerts, because the
    // constructor doesn't allow us to return an initialization result. Could be
    // cleaned up by adding an Init() function.
  }

  // Called by the ClientCertStoreTest tests.
  // |inpurt_certs| contains certificates to select from. Because
  // ClientCertStoreChromeOS filters also for the right slot, we have to import
  // the certs at first.
  // Since the certs are imported, the store can be tested by using its public
  // interface (GetClientCerts), which will read the certs from NSS.
  bool SelectClientCerts(const CertificateList& input_certs,
                         const SSLCertRequestInfo& cert_request_info,
                         CertificateList* selected_certs) {
    if (!user_.constructed_successfully()) {
      LOG(ERROR) << "Scoped test user DB could not be constructed.";
      return false;
    }
    user_.FinishInit();

    crypto::ScopedPK11Slot slot;
    switch (read_from) {
      case READ_FROM_SLOT_USER:
        slot = crypto::GetPublicSlotForChromeOSUser(user_.username_hash());
        break;
      case READ_FROM_SLOT_SYSTEM:
        slot.reset(PK11_ReferenceSlot(system_db_.slot()));
        break;
      default:
        CHECK(false);
    }
    if (!slot) {
      LOG(ERROR) << "Could not get the NSS key slot";
      return false;
    }

    // Only user certs are considered for the cert request, which means that the
    // private key must be known to NSS. Import all private keys for certs that
    // are used througout the test.
    if (!ImportSensitiveKeyFromFile(
            GetTestCertsDirectory(), "client_1.pk8", slot.get()) ||
        !ImportSensitiveKeyFromFile(
            GetTestCertsDirectory(), "client_2.pk8", slot.get())) {
      return false;
    }

    for (CertificateList::const_iterator it = input_certs.begin();
         it != input_certs.end();
         ++it) {
      if (!ImportClientCertToSlot(*it, slot.get()))
        return false;
    }
    base::RunLoop run_loop;
    store_.GetClientCerts(
        cert_request_info, selected_certs, run_loop.QuitClosure());
    run_loop.Run();
    return true;
  }

 private:
  crypto::ScopedTestNSSChromeOSUser user_;
  crypto::ScopedTestSystemNSSKeySlot system_db_;
  ClientCertStoreChromeOS store_;
};

// ClientCertStoreChromeOS derives from ClientCertStoreNSS and delegates the
// filtering by issuer to that base class.
// To verify that this delegation is functional, run the same filtering tests as
// for the other implementations. These tests are defined in
// client_cert_store_unittest-inl.h and are instantiated for each platform.

// In this case, all requested certs are read from the user's slot and the
// system slot is not enabled in the store.
typedef ClientCertStoreChromeOSTestDelegate<READ_FROM_SLOT_USER,
                                            SYSTEM_SLOT_AVAILABILITY_DISABLED>
    DelegateReadUserDisableSystem;
INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS_ReadUserDisableSystem,
                              ClientCertStoreTest,
                              DelegateReadUserDisableSystem);

// In this case, all requested certs are read from the user's slot and the
// system slot is enabled in the store.
typedef ClientCertStoreChromeOSTestDelegate<READ_FROM_SLOT_USER,
                                            SYSTEM_SLOT_AVAILABILITY_ENABLED>
    DelegateReadUserEnableSystem;
INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS_ReadUserEnableSystem,
                              ClientCertStoreTest,
                              DelegateReadUserEnableSystem);

// In this case, all requested certs are read from the system slot, therefore
// the system slot is enabled in the store.
typedef ClientCertStoreChromeOSTestDelegate<READ_FROM_SLOT_SYSTEM,
                                            SYSTEM_SLOT_AVAILABILITY_ENABLED>
    DelegateReadSystem;
INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS_ReadSystem,
                              ClientCertStoreTest,
                              DelegateReadSystem);

class ClientCertStoreChromeOSTest : public ::testing::Test {
 public:
  scoped_refptr<X509Certificate> ImportCertForUser(
      const std::string& username_hash,
      const std::string& cert_filename,
      const std::string& key_filename) {
    crypto::ScopedPK11Slot slot(
        crypto::GetPublicSlotForChromeOSUser(username_hash));
    if (!slot) {
      LOG(ERROR) << "No slot for user " << username_hash;
      return NULL;
    }

    return ImportClientCertAndKeyFromFile(
        GetTestCertsDirectory(), cert_filename, key_filename, slot.get());
  }

};

// Ensure that cert requests, that are started before the user's NSS DB is
// initialized, will wait for the initialization and succeed afterwards.
TEST_F(ClientCertStoreChromeOSTest, RequestWaitsForNSSInitAndSucceeds) {
  crypto::ScopedTestNSSChromeOSUser user("scopeduser");
  ASSERT_TRUE(user.constructed_successfully());

  crypto::ScopedTestSystemNSSKeySlot system_slot;

  ClientCertStoreChromeOS store(
      true /* use system slot */,
      user.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());
  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8"));
  ASSERT_TRUE(cert_1);

  // Request any client certificate, which is expected to match client_1.
  scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());

  base::RunLoop run_loop;
  store.GetClientCerts(
      *request_all, &request_all->client_certs, run_loop.QuitClosure());

  {
    base::RunLoop run_loop_inner;
    run_loop_inner.RunUntilIdle();
    // GetClientCerts should wait for the initialization of the user's DB to
    // finish.
    ASSERT_EQ(0u, request_all->client_certs.size());
  }
  // This should trigger the GetClientCerts operation to finish and to call
  // back.
  user.FinishInit();

  run_loop.Run();

  ASSERT_EQ(1u, request_all->client_certs.size());
}

// Ensure that cert requests, that are started after the user's NSS DB was
// initialized, will succeed.
TEST_F(ClientCertStoreChromeOSTest, RequestsAfterNSSInitSucceed) {
  crypto::ScopedTestNSSChromeOSUser user("scopeduser");
  ASSERT_TRUE(user.constructed_successfully());
  user.FinishInit();

  crypto::ScopedTestSystemNSSKeySlot system_slot;

  ClientCertStoreChromeOS store(
      true /* use system slot */,
      user.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());
  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user.username_hash(), "client_1.pem", "client_1.pk8"));
  ASSERT_TRUE(cert_1);

  scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());

  base::RunLoop run_loop;
  store.GetClientCerts(
      *request_all, &request_all->client_certs, run_loop.QuitClosure());
  run_loop.Run();

  ASSERT_EQ(1u, request_all->client_certs.size());
}

// This verifies that a request in the context of User1 doesn't see certificates
// of User2, and the other way round. We check both directions, to ensure that
// the behavior doesn't depend on initialization order of the DBs, for example.
TEST_F(ClientCertStoreChromeOSTest, RequestDoesCrossReadOtherUserDB) {
  crypto::ScopedTestNSSChromeOSUser user1("scopeduser1");
  ASSERT_TRUE(user1.constructed_successfully());
  crypto::ScopedTestNSSChromeOSUser user2("scopeduser2");
  ASSERT_TRUE(user2.constructed_successfully());

  user1.FinishInit();
  user2.FinishInit();

  crypto::ScopedTestSystemNSSKeySlot system_slot;

  ClientCertStoreChromeOS store1(
      true /* use system slot */,
      user1.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());
  ClientCertStoreChromeOS store2(
      true /* use system slot */,
      user2.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());

  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user1.username_hash(), "client_1.pem", "client_1.pk8"));
  ASSERT_TRUE(cert_1);
  scoped_refptr<X509Certificate> cert_2(
      ImportCertForUser(user2.username_hash(), "client_2.pem", "client_2.pk8"));
  ASSERT_TRUE(cert_2);

  scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());

  base::RunLoop run_loop_1;
  base::RunLoop run_loop_2;

  CertificateList selected_certs1, selected_certs2;
  store1.GetClientCerts(
      *request_all, &selected_certs1, run_loop_1.QuitClosure());
  store2.GetClientCerts(
      *request_all, &selected_certs2, run_loop_2.QuitClosure());

  run_loop_1.Run();
  run_loop_2.Run();

  // store1 should only return certs of user1, namely cert_1.
  ASSERT_EQ(1u, selected_certs1.size());
  EXPECT_TRUE(cert_1->Equals(selected_certs1[0]));

  // store2 should only return certs of user2, namely cert_2.
  ASSERT_EQ(1u, selected_certs2.size());
  EXPECT_TRUE(cert_2->Equals(selected_certs2[0]));
}

// This verifies that a request in the context of User1 doesn't see certificates
// of the system store if the system store is disabled.
TEST_F(ClientCertStoreChromeOSTest, RequestDoesCrossReadSystemDB) {
  crypto::ScopedTestNSSChromeOSUser user1("scopeduser1");
  ASSERT_TRUE(user1.constructed_successfully());

  user1.FinishInit();

  crypto::ScopedTestSystemNSSKeySlot system_slot;

  ClientCertStoreChromeOS store(
      false /* do not use system slot */,
      user1.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());

  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user1.username_hash(), "client_1.pem", "client_1.pk8"));
  ASSERT_TRUE(cert_1);
  scoped_refptr<X509Certificate> cert_2(
      ImportClientCertAndKeyFromFile(GetTestCertsDirectory(),
                                     "client_2.pem",
                                     "client_2.pk8",
                                     system_slot.slot()));
  ASSERT_TRUE(cert_2);

  scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo());

  base::RunLoop run_loop;

  CertificateList selected_certs;
  store.GetClientCerts(*request_all, &selected_certs, run_loop.QuitClosure());

  run_loop.Run();

  // store should only return certs of the user, namely cert_1.
  ASSERT_EQ(1u, selected_certs.size());
  EXPECT_TRUE(cert_1->Equals(selected_certs[0]));
}

}  // namespace net