File: client_cert_store_kcer_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (446 lines) | stat: -rw-r--r-- 17,328 bytes parent folder | download | duplicates (6)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/net/client_cert_store_kcer.h"

#include <memory>
#include <optional>
#include <string>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/certificate_provider/certificate_provider.h"
#include "chromeos/ash/components/kcer/kcer_nss/test_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "crypto/nss_util.h"
#include "crypto/nss_util_internal.h"
#include "crypto/scoped_nss_types.h"
#include "crypto/scoped_test_nss_chromeos_user.h"
#include "crypto/scoped_test_nss_db.h"
#include "crypto/scoped_test_system_nss_key_slot.h"
#include "net/base/features.h"
#include "net/cert/cert_database.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "net/cert/x509_util_nss.h"
#include "net/ssl/client_cert_identity_test_util.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "net/ssl/ssl_private_key.h"
#include "net/ssl/ssl_private_key_test_util.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

// "CN=B CA" - DER encoded DN of the issuer of client_1.pem
const unsigned char kAuthority1DN[] = {0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b,
                                       0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
                                       0x04, 0x42, 0x20, 0x43, 0x41};

// "CN=C Root CA" - DER encoded DN of the issuer of client_1_ca.pem,
// client_2_ca.pem, and client_3_ca.pem.
const unsigned char kAuthorityRootDN[] = {
    0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03,
    0x0c, 0x09, 0x43, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41,
};

void SaveIdentitiesAndQuitCallback(net::ClientCertIdentityList* out_identities,
                                   base::OnceClosure quit_closure,
                                   net::ClientCertIdentityList in_identities) {
  *out_identities = std::move(in_identities);
  std::move(quit_closure).Run();
}

void SavePrivateKeyAndQuitCallback(scoped_refptr<net::SSLPrivateKey>* out_key,
                                   base::OnceClosure quit_closure,
                                   scoped_refptr<net::SSLPrivateKey> in_key) {
  *out_key = std::move(in_key);
  std::move(quit_closure).Run();
}

void GetClientCertIssuerSourceForCerts(
    net::CertificateList certs,
    net::ClientCertIssuerSourceGetterCallback callback) {
  std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> cert_buffers;
  for (const auto& cert : certs) {
    cert_buffers.push_back(bssl::UpRef(cert->cert_buffer()));
  }

  net::ClientCertIssuerSourceCollection sources;
  sources.push_back(std::make_unique<net::ClientCertIssuerSourceInMemory>(
      std::move(cert_buffers)));
  // The callback can be run either synchronously or asynchronously. Run it
  // synchronously as re-entrancy is probably more the more stressful case to
  // test.
  std::move(callback).Run(std::move(sources));
}

net::ClientCertIssuerSourceGetter IssuerSourceGetter(
    net::CertificateList certs) {
  return base::BindOnce(&GetClientCertIssuerSourceForCerts, std::move(certs));
}

}  // namespace

class ClientCertStoreKcerTest : public ::testing::Test {
 public:
  ClientCertStoreKcerTest() = default;

  void SetUp() override {
    ASSERT_TRUE(user1_.constructed_successfully());
    ASSERT_TRUE(user2_.constructed_successfully());
    ASSERT_TRUE(system_slot_.ConstructedSuccessfully());
    kcer_holder_.emplace(
        crypto::GetPublicSlotForChromeOSUser(user1_.username_hash()).get(),
        system_slot_.slot());
  }

  void TearDown() override { kcer_holder_.reset(); }

  scoped_refptr<net::X509Certificate> ImportCertToSlot(
      const std::string& cert_filename,
      const std::string& key_filename,
      PK11SlotInfo* slot) {
    scoped_refptr<net::X509Certificate> result =
        net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
                                            cert_filename, key_filename, slot);
    // Kcer relies on notifications to invalidate caches when the client
    // certificate store changes.
    net::CertDatabase::GetInstance()->NotifyObserversClientCertStoreChanged();
    return result;
  }

 protected:
  crypto::ScopedTestNSSChromeOSUser user1_{"user1"};
  crypto::ScopedTestNSSChromeOSUser user2_{"user2"};
  crypto::ScopedTestSystemNSSKeySlot system_slot_{
      /*simulate_token_loader=*/true};
  std::optional<kcer::TestKcerHolder> kcer_holder_;
  content::BrowserTaskEnvironment task_environment_;
};

// Ensure that cert requests, that are started before NSS is initialized,
// will wait for the initialization and succeed afterwards.
TEST_F(ClientCertStoreKcerTest, RequestWaitsForNSSInitAndSucceeds) {
  ClientCertStoreKcer store(nullptr /* no additional provider */,
                            kcer_holder_->GetKcer(), IssuerSourceGetter({}));

  scoped_refptr<net::X509Certificate> cert_1(ImportCertToSlot(
      "client_1.pem", "client_1.pk8",
      crypto::GetPublicSlotForChromeOSUser(user1_.username_hash()).get()));
  ASSERT_TRUE(cert_1);

  // Request any client certificate, which is expected to match client_1.
  auto request_all = base::MakeRefCounted<net::SSLCertRequestInfo>();

  net::ClientCertIdentityList selected_identities;
  base::RunLoop run_loop;
  store.GetClientCerts(
      request_all,
      base::BindOnce(SaveIdentitiesAndQuitCallback, &selected_identities,
                     run_loop.QuitClosure()));

  {
    base::RunLoop run_loop_inner;
    run_loop_inner.RunUntilIdle();
    // GetClientCerts should wait for the initialization of NSS to finish.
    ASSERT_EQ(0u, selected_identities.size());
  }

  user1_.FinishInit();
  run_loop.Run();

  ASSERT_EQ(1u, selected_identities.size());
}

// Ensure that cert requests, that are started after NSS was initialized,
// will succeed.
TEST_F(ClientCertStoreKcerTest, RequestsAfterNSSInitSucceed) {
  user1_.FinishInit();

  ClientCertStoreKcer store(nullptr /* no additional provider */,
                            kcer_holder_->GetKcer(), IssuerSourceGetter({}));

  scoped_refptr<net::X509Certificate> cert_1(ImportCertToSlot(
      "client_1.pem", "client_1.pk8",
      crypto::GetPublicSlotForChromeOSUser(user1_.username_hash()).get()));
  ASSERT_TRUE(cert_1);

  auto request_all = base::MakeRefCounted<net::SSLCertRequestInfo>();

  base::RunLoop run_loop;
  net::ClientCertIdentityList selected_identities;
  store.GetClientCerts(
      request_all,
      base::BindOnce(SaveIdentitiesAndQuitCallback, &selected_identities,
                     run_loop.QuitClosure()));
  run_loop.Run();

  ASSERT_EQ(1u, selected_identities.size());
}

TEST_F(ClientCertStoreKcerTest, Filter) {
  user1_.FinishInit();
  user2_.FinishInit();

  crypto::ScopedPK11Slot slot_private1 = crypto::GetPrivateSlotForChromeOSUser(
      user1_.username_hash(), base::DoNothing());
  crypto::ScopedPK11Slot slot_private2 = crypto::GetPrivateSlotForChromeOSUser(
      user2_.username_hash(), base::DoNothing());
  // crypto::GetPrivateSlotForChromeOSUser() may return nullptr if the slot is
  // not initialized yet, but this should not happen.
  ASSERT_TRUE(slot_private1);
  ASSERT_TRUE(slot_private2);

  kcer::TestKcerHolder user1_kcer_without_system_slot(slot_private1.get(),
                                                      nullptr);
  kcer::TestKcerHolder user1_kcer_with_system_slot(slot_private1.get(),
                                                   system_slot_.slot());
  kcer::TestKcerHolder user2_kcer_without_system_slot(slot_private2.get(),
                                                      nullptr);
  kcer::TestKcerHolder user2_kcer_with_system_slot(slot_private2.get(),
                                                   system_slot_.slot());

  // Import a certificate into each slot.
  scoped_refptr<net::X509Certificate> cert_private1(
      ImportCertToSlot("client_1.pem", "client_1.pk8", slot_private1.get()));
  ASSERT_TRUE(cert_private1);
  scoped_refptr<net::X509Certificate> cert_private2(
      ImportCertToSlot("client_2.pem", "client_2.pk8", slot_private2.get()));
  ASSERT_TRUE(cert_private2);
  scoped_refptr<net::X509Certificate> cert_system(
      ImportCertToSlot("client_3.pem", "client_3.pk8", system_slot_.slot()));
  ASSERT_TRUE(cert_system);

  const struct FilterTest {
    std::string description;
    base::WeakPtr<kcer::Kcer> kcer;
    std::vector<raw_ptr<net::X509Certificate, VectorExperimental>> results;
  } kTests[] = {
      {"user1 without system slot",
       user1_kcer_without_system_slot.GetKcer(),
       {cert_private1.get()}},
      {"user1 with system slot",
       user1_kcer_with_system_slot.GetKcer(),
       {cert_private1.get(), cert_system.get()}},
      {"user2 without system slot",
       user2_kcer_without_system_slot.GetKcer(),
       {cert_private2.get()}},
      {"user2 with system slot",
       user2_kcer_with_system_slot.GetKcer(),
       {cert_private2.get(), cert_system.get()}},
  };

  for (const auto& test : kTests) {
    SCOPED_TRACE(test.description);

    ClientCertStoreKcer store(nullptr /* no additional provider */, test.kcer,
                              IssuerSourceGetter({}));

    auto request_all = base::MakeRefCounted<net::SSLCertRequestInfo>();

    base::RunLoop run_loop;
    net::ClientCertIdentityList selected_identities;
    store.GetClientCerts(
        request_all,
        base::BindOnce(SaveIdentitiesAndQuitCallback, &selected_identities,
                       run_loop.QuitClosure()));
    run_loop.Run();

    ASSERT_EQ(test.results.size(), selected_identities.size());
    for (size_t i = 0; i < test.results.size(); i++) {
      bool found_cert = false;
      for (const auto& identity : selected_identities) {
        if (test.results[i]->EqualsExcludingChain(identity->certificate())) {
          found_cert = true;
          break;
        }
      }
      EXPECT_TRUE(found_cert) << "Could not find certificate " << i;
    }
  }
}

class ClientCertStoreKcerMatchingTest
    : public ClientCertStoreKcerTest,
      public testing::WithParamInterface<bool> {
 public:
  ClientCertStoreKcerMatchingTest() {
    scoped_feature_list_.InitWithFeatureState(
        net::features::kNewClientCertPathBuilding, UseNewPathBuilding());
  }

  bool UseNewPathBuilding() const { return GetParam(); }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Ensure that the delegation of the request matching to the base class is
// functional.
TEST_P(ClientCertStoreKcerMatchingTest, CertRequestMatching) {
  user1_.FinishInit();

  ClientCertStoreKcer store(nullptr,  // no additional provider
                            kcer_holder_->GetKcer(), IssuerSourceGetter({}));

  crypto::ScopedPK11Slot slot =
      crypto::GetPublicSlotForChromeOSUser(user1_.username_hash());
  scoped_refptr<net::X509Certificate> cert_1(
      ImportCertToSlot("client_1.pem", "client_1.pk8", slot.get()));
  ASSERT_TRUE(cert_1);
  scoped_refptr<net::X509Certificate> cert_2(
      ImportCertToSlot("client_2.pem", "client_2.pk8", slot.get()));
  ASSERT_TRUE(cert_2.get());

  std::vector<std::string> authority_1 = {
      std::string(std::begin(kAuthority1DN), std::end(kAuthority1DN))};
  auto request = base::MakeRefCounted<net::SSLCertRequestInfo>();
  request->cert_authorities = authority_1;

  base::RunLoop run_loop;
  net::ClientCertIdentityList selected_identities;
  store.GetClientCerts(
      request, base::BindOnce(SaveIdentitiesAndQuitCallback,
                              &selected_identities, run_loop.QuitClosure()));
  run_loop.Run();

  ASSERT_EQ(1u, selected_identities.size());
  EXPECT_TRUE(
      cert_1->EqualsExcludingChain(selected_identities[0]->certificate()));
}

// Tests that ClientCertStoreKcer attempts to build a certificate chain by
// querying NSS before return a certificate.
TEST_P(ClientCertStoreKcerMatchingTest, BuildsCertificateChain) {
  user1_.FinishInit();

  crypto::ScopedPK11Slot private_slot = crypto::GetPrivateSlotForChromeOSUser(
      user1_.username_hash(), base::DoNothing());
  // crypto::GetPrivateSlotForChromeOSUser() may return nullptr if the slot is
  // not initialized yet, but this should not happen.
  ASSERT_TRUE(private_slot);

  // Import client_1.pem into the slot of user1.
  scoped_refptr<net::X509Certificate> client_1(
      net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
                                          "client_1.pem", "client_1.pk8",
                                          private_slot.get()));
  ASSERT_TRUE(client_1.get());
  std::string pkcs8_key;
  ASSERT_TRUE(base::ReadFileToString(
      net::GetTestCertsDirectory().AppendASCII("client_1.pk8"), &pkcs8_key));

  scoped_refptr<net::X509Certificate> client_1_ca(
      net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_1_ca.pem"));
  ASSERT_TRUE(client_1_ca.get());

  crypto::ScopedTestNSSDB public_slot;
  net::CertificateList issuers_for_new_pathbuilder;
  if (UseNewPathBuilding()) {
    issuers_for_new_pathbuilder.push_back(client_1_ca);
  } else {
    // Import client_1_ca.pem into a separate slot, that is visible to NSS in
    // general, but not to Kcer for user1.
    ASSERT_TRUE(net::ImportClientCertToSlot(client_1_ca, public_slot.slot()));
  }

  // These test keys are RSA keys.
  std::vector<uint16_t> expected =
      net::SSLPrivateKey::DefaultAlgorithmPreferences(EVP_PKEY_RSA,
                                                      true /* supports PSS */);

  {
    ClientCertStoreKcer store(nullptr,  // no additional provider
                              kcer_holder_->GetKcer(),
                              IssuerSourceGetter(issuers_for_new_pathbuilder));

    // Request certificates matching B CA, |client_1|'s issuer.
    auto request = base::MakeRefCounted<net::SSLCertRequestInfo>();
    request->cert_authorities.emplace_back(
        reinterpret_cast<const char*>(kAuthority1DN), sizeof(kAuthority1DN));

    net::ClientCertIdentityList selected_identities;
    base::RunLoop loop;
    store.GetClientCerts(
        request, base::BindOnce(SaveIdentitiesAndQuitCallback,
                                &selected_identities, loop.QuitClosure()));
    loop.Run();

    // The result be |client_1| with no intermediates.
    ASSERT_EQ(1u, selected_identities.size());
    scoped_refptr<net::X509Certificate> selected_cert =
        selected_identities[0]->certificate();
    EXPECT_TRUE(net::x509_util::CryptoBufferEqual(
        client_1->cert_buffer(), selected_cert->cert_buffer()));
    ASSERT_EQ(0u, selected_cert->intermediate_buffers().size());

    scoped_refptr<net::SSLPrivateKey> ssl_private_key;
    base::RunLoop key_loop;
    selected_identities[0]->AcquirePrivateKey(
        base::BindOnce(SavePrivateKeyAndQuitCallback, &ssl_private_key,
                       key_loop.QuitClosure()));
    key_loop.Run();

    ASSERT_TRUE(ssl_private_key);
    EXPECT_EQ(expected, ssl_private_key->GetAlgorithmPreferences());
    TestSSLPrivateKeyMatches(ssl_private_key.get(), pkcs8_key);
  }

  {
    ClientCertStoreKcer store(nullptr,  // no additional provider
                              kcer_holder_->GetKcer(),
                              IssuerSourceGetter(issuers_for_new_pathbuilder));

    // Request certificates matching C Root CA, |client_1_ca|'s issuer.
    auto request = base::MakeRefCounted<net::SSLCertRequestInfo>();
    request->cert_authorities.emplace_back(
        reinterpret_cast<const char*>(kAuthorityRootDN),
        sizeof(kAuthorityRootDN));

    net::ClientCertIdentityList selected_identities;
    base::RunLoop loop;
    store.GetClientCerts(
        request, base::BindOnce(SaveIdentitiesAndQuitCallback,
                                &selected_identities, loop.QuitClosure()));
    loop.Run();

    // The result be |client_1| with |client_1_ca| as an intermediate.
    ASSERT_EQ(1u, selected_identities.size());
    scoped_refptr<net::X509Certificate> selected_cert =
        selected_identities[0]->certificate();
    EXPECT_TRUE(net::x509_util::CryptoBufferEqual(
        client_1->cert_buffer(), selected_cert->cert_buffer()));
    ASSERT_EQ(1u, selected_cert->intermediate_buffers().size());
    EXPECT_TRUE(net::x509_util::CryptoBufferEqual(
        client_1_ca->cert_buffer(),
        selected_cert->intermediate_buffers()[0].get()));

    scoped_refptr<net::SSLPrivateKey> ssl_private_key;
    base::RunLoop key_loop;
    selected_identities[0]->AcquirePrivateKey(
        base::BindOnce(SavePrivateKeyAndQuitCallback, &ssl_private_key,
                       key_loop.QuitClosure()));
    key_loop.Run();
    ASSERT_TRUE(ssl_private_key);
    EXPECT_EQ(expected, ssl_private_key->GetAlgorithmPreferences());
    TestSSLPrivateKeyMatches(ssl_private_key.get(), pkcs8_key);
  }
}

INSTANTIATE_TEST_SUITE_P(, ClientCertStoreKcerMatchingTest, testing::Bool());

}  // namespace ash