1 // Copyright (c) 2012 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.
5 #include "net/ssl/client_cert_store_impl.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/cert_test_util.h"
14 #include "net/base/test_data_directory.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 // "CN=Client Auth Test Root 1" - DER encoded DN of the issuer of client_1.pem.
22 const unsigned char kAuthority1DN
[] = {
23 0x30, 0x22, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
24 0x17, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68,
25 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x31
28 // "CN=Client Auth Test Root 2" - DER encoded DN of the issuer of client_2.pem.
29 unsigned char kAuthority2DN
[] = {
30 0x30, 0x22, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
31 0x17, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68,
32 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x32
37 TEST(ClientCertStoreImplTest
, EmptyQuery
) {
38 std::vector
<scoped_refptr
<X509Certificate
> > certs
;
39 scoped_refptr
<SSLCertRequestInfo
> request(new SSLCertRequestInfo());
41 ClientCertStoreImpl store
;
42 std::vector
<scoped_refptr
<X509Certificate
> > selected_certs
;
43 bool rv
= store
.SelectClientCerts(certs
, *request
, &selected_certs
);
45 EXPECT_EQ(0u, selected_certs
.size());
48 // Verify that CertRequestInfo with empty |cert_authorities| matches all
49 // issuers, rather than no issuers.
50 TEST(ClientCertStoreImplTest
, AllIssuersAllowed
) {
51 scoped_refptr
<X509Certificate
> cert(
52 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
55 std::vector
<scoped_refptr
<X509Certificate
> > certs
;
56 certs
.push_back(cert
);
57 scoped_refptr
<SSLCertRequestInfo
> request(new SSLCertRequestInfo());
59 ClientCertStoreImpl store
;
60 std::vector
<scoped_refptr
<X509Certificate
> > selected_certs
;
61 bool rv
= store
.SelectClientCerts(certs
, *request
, &selected_certs
);
63 ASSERT_EQ(1u, selected_certs
.size());
64 EXPECT_TRUE(selected_certs
[0]->Equals(cert
));
67 // Verify that certificates are correctly filtered against CertRequestInfo with
68 // |cert_authorities| containing only |authority_1_DN|.
69 TEST(ClientCertStoreImplTest
, CertAuthorityFiltering
) {
70 scoped_refptr
<X509Certificate
> cert_1(
71 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
73 scoped_refptr
<X509Certificate
> cert_2(
74 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
77 std::vector
<std::string
> authority_1(
78 1, std::string(reinterpret_cast<const char*>(kAuthority1DN
),
79 sizeof(kAuthority1DN
)));
80 std::vector
<std::string
> authority_2(
81 1, std::string(reinterpret_cast<const char*>(kAuthority2DN
),
82 sizeof(kAuthority2DN
)));
83 EXPECT_TRUE(cert_1
->IsIssuedByEncoded(authority_1
));
84 EXPECT_FALSE(cert_1
->IsIssuedByEncoded(authority_2
));
85 EXPECT_TRUE(cert_2
->IsIssuedByEncoded(authority_2
));
86 EXPECT_FALSE(cert_2
->IsIssuedByEncoded(authority_1
));
88 std::vector
<scoped_refptr
<X509Certificate
> > certs
;
89 certs
.push_back(cert_1
);
90 certs
.push_back(cert_2
);
91 scoped_refptr
<SSLCertRequestInfo
> request(new SSLCertRequestInfo());
92 request
->cert_authorities
= authority_1
;
94 ClientCertStoreImpl store
;
95 std::vector
<scoped_refptr
<X509Certificate
> > selected_certs
;
96 bool rv
= store
.SelectClientCerts(certs
, *request
, &selected_certs
);
98 ASSERT_EQ(1u, selected_certs
.size());
99 EXPECT_TRUE(selected_certs
[0]->Equals(cert_1
));
102 #if defined(OS_MACOSX) && !defined(OS_IOS)
103 // Verify that the preferred cert gets filtered out when it doesn't match the
105 TEST(ClientCertStoreImplTest
, FilterOutThePreferredCert
) {
106 scoped_refptr
<X509Certificate
> cert_1(
107 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
110 std::vector
<std::string
> authority_2(
111 1, std::string(reinterpret_cast<const char*>(kAuthority2DN
),
112 sizeof(kAuthority2DN
)));
113 EXPECT_FALSE(cert_1
->IsIssuedByEncoded(authority_2
));
115 std::vector
<scoped_refptr
<X509Certificate
> > certs
;
116 scoped_refptr
<SSLCertRequestInfo
> request(new SSLCertRequestInfo());
117 request
->cert_authorities
= authority_2
;
119 ClientCertStoreImpl store
;
120 std::vector
<scoped_refptr
<X509Certificate
> > selected_certs
;
121 bool rv
= store
.SelectClientCertsGivenPreferred(cert_1
, certs
, *request
,
124 EXPECT_EQ(0u, selected_certs
.size());
127 // Verify that the preferred cert takes the first position in the output list,
128 // when it does not get filtered out.
129 TEST(ClientCertStoreImplTest
, PreferredCertGoesFirst
) {
130 scoped_refptr
<X509Certificate
> cert_1(
131 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
133 scoped_refptr
<X509Certificate
> cert_2(
134 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
137 std::vector
<scoped_refptr
<X509Certificate
> > certs
;
138 certs
.push_back(cert_2
);
139 scoped_refptr
<SSLCertRequestInfo
> request(new SSLCertRequestInfo());
141 ClientCertStoreImpl store
;
142 std::vector
<scoped_refptr
<X509Certificate
> > selected_certs
;
143 bool rv
= store
.SelectClientCertsGivenPreferred(cert_1
, certs
, *request
,
146 ASSERT_EQ(2u, selected_certs
.size());
147 EXPECT_TRUE(selected_certs
[0]->Equals(cert_1
));
148 EXPECT_TRUE(selected_certs
[1]->Equals(cert_2
));