summaryrefslogtreecommitdiffstats
path: root/net/ssl/client_cert_store_chromeos_unittest.cc
blob: 73e03264aad0a57b3f33a332d22060cb15f1383f (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
// 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 "base/bind.h"
#include "base/callback.h"
#include "base/file_util.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "crypto/nss_util.h"
#include "crypto/nss_util_internal.h"
#include "net/cert/nss_cert_database.h"
#include "net/ssl/client_cert_store_unittest-inl.h"

namespace net {

class ClientCertStoreChromeOSTestDelegate {
 public:
  ClientCertStoreChromeOSTestDelegate()
      : store_("usernamehash",
               ClientCertStoreChromeOS::PasswordDelegateFactory()) {
    store_.InitForTesting(
        crypto::ScopedPK11Slot(crypto::GetPublicNSSKeySlot()),
        crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot()));
  }

  bool SelectClientCerts(const CertificateList& input_certs,
                         const SSLCertRequestInfo& cert_request_info,
                         CertificateList* selected_certs) {
    return store_.SelectClientCertsForTesting(
        input_certs, cert_request_info, selected_certs);
  }

 private:
  ClientCertStoreChromeOS store_;
};

INSTANTIATE_TYPED_TEST_CASE_P(ChromeOS,
                              ClientCertStoreTest,
                              ClientCertStoreChromeOSTestDelegate);

class ClientCertStoreChromeOSTest : public ::testing::Test {
 public:
  scoped_refptr<X509Certificate> ImportCertForUser(
      const std::string& username_hash,
      const std::string& filename,
      const std::string& password) {
    crypto::ScopedPK11Slot slot(
        crypto::GetPublicSlotForChromeOSUser(username_hash));
    EXPECT_TRUE(slot.get());
    if (!slot.get())
      return NULL;

    net::CertificateList cert_list;

    base::FilePath p12_path = GetTestCertsDirectory().AppendASCII(filename);
    std::string p12_data;
    if (!base::ReadFileToString(p12_path, &p12_data)) {
      EXPECT_TRUE(false);
      return NULL;
    }

    scoped_refptr<net::CryptoModule> module(
        net::CryptoModule::CreateFromHandle(slot.get()));
    int rv = NSSCertDatabase::GetInstance()->ImportFromPKCS12(
        module.get(), p12_data, base::UTF8ToUTF16(password), false, &cert_list);

    EXPECT_EQ(0, rv);
    EXPECT_EQ(1U, cert_list.size());
    if (rv || cert_list.size() != 1)
      return NULL;

    return cert_list[0];
  }
};

// TODO(mattm): Do better testing of cert_authorities matching below. Update
// net/data/ssl/scripts/generate-client-certificates.sh so that it actually
// saves the .p12 files, and regenerate them.

TEST_F(ClientCertStoreChromeOSTest, WaitForNSSInit) {
  crypto::ScopedTestNSSChromeOSUser user("scopeduser");
  ASSERT_TRUE(user.constructed_successfully());
  ClientCertStoreChromeOS store(
      user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory());
  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user.username_hash(), "client.p12", "12345"));
  scoped_refptr<X509Certificate> cert_2(
      ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", ""));

  std::vector<std::string> authority_1(
      1,
      std::string(reinterpret_cast<const char*>(kAuthority1DN),
                  sizeof(kAuthority1DN)));
  scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo());
  request_1->cert_authorities = authority_1;

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

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

  // Callbacks won't be run until nss_util init finishes for the user.
  user.FinishInit();

  run_loop_1.Run();
  run_loop_all.Run();

  ASSERT_EQ(0u, request_1->client_certs.size());
  ASSERT_EQ(2u, request_all->client_certs.size());
}

TEST_F(ClientCertStoreChromeOSTest, NSSAlreadyInitialized) {
  crypto::ScopedTestNSSChromeOSUser user("scopeduser");
  ASSERT_TRUE(user.constructed_successfully());
  user.FinishInit();

  ClientCertStoreChromeOS store(
      user.username_hash(), ClientCertStoreChromeOS::PasswordDelegateFactory());
  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user.username_hash(), "client.p12", "12345"));
  scoped_refptr<X509Certificate> cert_2(
      ImportCertForUser(user.username_hash(), "websocket_client_cert.p12", ""));

  std::vector<std::string> authority_1(
      1,
      std::string(reinterpret_cast<const char*>(kAuthority1DN),
                  sizeof(kAuthority1DN)));
  scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo());
  request_1->cert_authorities = authority_1;

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

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

  run_loop_1.Run();
  run_loop_all.Run();

  ASSERT_EQ(0u, request_1->client_certs.size());
  ASSERT_EQ(2u, request_all->client_certs.size());
}

TEST_F(ClientCertStoreChromeOSTest, TwoUsers) {
  crypto::ScopedTestNSSChromeOSUser user1("scopeduser1");
  ASSERT_TRUE(user1.constructed_successfully());
  crypto::ScopedTestNSSChromeOSUser user2("scopeduser2");
  ASSERT_TRUE(user2.constructed_successfully());
  ClientCertStoreChromeOS store1(
      user1.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());
  ClientCertStoreChromeOS store2(
      user2.username_hash(),
      ClientCertStoreChromeOS::PasswordDelegateFactory());
  scoped_refptr<X509Certificate> cert_1(
      ImportCertForUser(user1.username_hash(), "client.p12", "12345"));
  scoped_refptr<X509Certificate> cert_2(ImportCertForUser(
      user2.username_hash(), "websocket_client_cert.p12", ""));

  scoped_refptr<SSLCertRequestInfo> request_1(new SSLCertRequestInfo());
  scoped_refptr<SSLCertRequestInfo> request_2(new SSLCertRequestInfo());

  base::RunLoop run_loop_1;
  base::RunLoop run_loop_2;
  store1.GetClientCerts(
      *request_1, &request_1->client_certs, run_loop_1.QuitClosure());
  store2.GetClientCerts(
      *request_2, &request_2->client_certs, run_loop_2.QuitClosure());

  // Callbacks won't be run until nss_util init finishes for the user.
  user1.FinishInit();
  user2.FinishInit();

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

  ASSERT_EQ(1u, request_1->client_certs.size());
  EXPECT_TRUE(cert_1->Equals(request_1->client_certs[0]));
  // TODO(mattm): Request for second user will have zero results due to
  // crbug.com/315285.  Update the test once that is fixed.
}

}  // namespace net