blob: 83ce480c790fca5d2b6923bac846bb20d1c50e69 [file] [log] [blame]
[email protected]5482ef9e2013-12-11 04:27:431// 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
5#ifndef NET_SSL_CLIENT_CERT_STORE_UNITTEST_INL_H_
6#define NET_SSL_CLIENT_CERT_STORE_UNITTEST_INL_H_
7
danakja9850e12016-04-18 22:28:088#include <memory>
[email protected]5482ef9e2013-12-11 04:27:439#include <string>
10#include <vector>
11
12#include "base/files/file_path.h"
Matt Mueller6c8b07c62017-10-09 21:02:2513#include "base/files/file_util.h"
[email protected]5482ef9e2013-12-11 04:27:4314#include "base/memory/ref_counted.h"
Matt Muellerdd8f4692019-11-21 01:09:3215#include "net/cert/pem.h"
Matt Mueller6c8b07c62017-10-09 21:02:2516#include "net/cert/x509_util.h"
pneubeck385704ec2015-08-25 08:56:3717#include "net/ssl/ssl_cert_request_info.h"
[email protected]5482ef9e2013-12-11 04:27:4318#include "net/test/cert_test_util.h"
rsleevia69c79a2016-06-22 03:28:4319#include "net/test/test_data_directory.h"
Gabriel Charettec7108742019-08-23 03:31:4020#include "net/test/test_with_task_environment.h"
[email protected]5482ef9e2013-12-11 04:27:4321#include "testing/gtest/include/gtest/gtest.h"
22
23namespace net {
24
25namespace {
26
27// "CN=B CA" - DER encoded DN of the issuer of client_1.pem
28const unsigned char kAuthority1DN[] = {
davidben8d569f52016-07-29 16:03:1829 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55,
30 0x04, 0x03, 0x0c, 0x04, 0x42, 0x20, 0x43, 0x41,
[email protected]5482ef9e2013-12-11 04:27:4331};
32
33// "CN=E CA" - DER encoded DN of the issuer of client_2.pem
davidben8d569f52016-07-29 16:03:1834const unsigned char kAuthority2DN[] = {
35 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55,
36 0x04, 0x03, 0x0c, 0x04, 0x45, 0x20, 0x43, 0x41,
37};
38
39// "CN=C Root CA" - DER encoded DN of the issuer of client_1_ca.pem,
40// client_2_ca.pem, and client_3_ca.pem.
41const unsigned char kAuthorityRootDN[] = {
42 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03,
43 0x0c, 0x09, 0x43, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41,
[email protected]5482ef9e2013-12-11 04:27:4344};
45
46} // namespace
47
48// Use a templated test to provide common testcases for all the platform
49// implementations of ClientCertStore. These cases test the client cert
50// filtering behavior.
51//
52// NOTE: If any test cases are added, removed, or renamed, the
Victor Costan2309ea02019-02-13 21:35:4753// REGISTER_TYPED_TEST_SUITE_P macro at the bottom of this file must be updated.
[email protected]5482ef9e2013-12-11 04:27:4354//
Victor Costan2309ea02019-02-13 21:35:4755// The type T provided as the third argument to INSTANTIATE_TYPED_TEST_SUITE_P
56// by the platform implementation should implement this method: bool
57// SelectClientCerts(const CertificateList& input_certs,
[email protected]5482ef9e2013-12-11 04:27:4358// const SSLCertRequestInfo& cert_request_info,
mattm436ccfe2017-06-19 20:24:0859// ClientCertIdentityList* selected_identities);
[email protected]5482ef9e2013-12-11 04:27:4360template <typename T>
Gabriel Charette694c3c332019-08-19 14:53:0561class ClientCertStoreTest : public TestWithTaskEnvironment {
[email protected]5482ef9e2013-12-11 04:27:4362 public:
63 T delegate_;
64};
65
Victor Costan2309ea02019-02-13 21:35:4766TYPED_TEST_SUITE_P(ClientCertStoreTest);
[email protected]5482ef9e2013-12-11 04:27:4367
68TYPED_TEST_P(ClientCertStoreTest, EmptyQuery) {
mattm436ccfe2017-06-19 20:24:0869 CertificateList certs;
David Benjamin1c4b6d012019-07-08 17:12:5770 auto request = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]5482ef9e2013-12-11 04:27:4371
mattm436ccfe2017-06-19 20:24:0872 ClientCertIdentityList selected_identities;
73 bool rv = this->delegate_.SelectClientCerts(certs, *request.get(),
74 &selected_identities);
[email protected]5482ef9e2013-12-11 04:27:4375 EXPECT_TRUE(rv);
mattm436ccfe2017-06-19 20:24:0876 EXPECT_EQ(0u, selected_identities.size());
[email protected]5482ef9e2013-12-11 04:27:4377}
78
79// Verify that CertRequestInfo with empty |cert_authorities| matches all
80// issuers, rather than no issuers.
81TYPED_TEST_P(ClientCertStoreTest, AllIssuersAllowed) {
82 scoped_refptr<X509Certificate> cert(
83 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
84 ASSERT_TRUE(cert.get());
85
86 std::vector<scoped_refptr<X509Certificate> > certs;
87 certs.push_back(cert);
David Benjamin1c4b6d012019-07-08 17:12:5788 auto request = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]5482ef9e2013-12-11 04:27:4389
mattm436ccfe2017-06-19 20:24:0890 ClientCertIdentityList selected_identities;
91 bool rv = this->delegate_.SelectClientCerts(certs, *request.get(),
92 &selected_identities);
[email protected]5482ef9e2013-12-11 04:27:4393 EXPECT_TRUE(rv);
mattm436ccfe2017-06-19 20:24:0894 ASSERT_EQ(1u, selected_identities.size());
Matt Mueller294998d2018-04-17 03:04:5395 EXPECT_TRUE(
96 selected_identities[0]->certificate()->EqualsExcludingChain(cert.get()));
[email protected]5482ef9e2013-12-11 04:27:4397}
98
99// Verify that certificates are correctly filtered against CertRequestInfo with
100// |cert_authorities| containing only |authority_1_DN|.
Matt Muellere340fcaf2019-02-15 00:48:41101TYPED_TEST_P(ClientCertStoreTest, CertAuthorityFiltering) {
[email protected]5482ef9e2013-12-11 04:27:43102 scoped_refptr<X509Certificate> cert_1(
103 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
104 ASSERT_TRUE(cert_1.get());
105 scoped_refptr<X509Certificate> cert_2(
106 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
107 ASSERT_TRUE(cert_2.get());
108
109 std::vector<std::string> authority_1(
110 1, std::string(reinterpret_cast<const char*>(kAuthority1DN),
111 sizeof(kAuthority1DN)));
112 std::vector<std::string> authority_2(
113 1, std::string(reinterpret_cast<const char*>(kAuthority2DN),
114 sizeof(kAuthority2DN)));
115 EXPECT_TRUE(cert_1->IsIssuedByEncoded(authority_1));
116 EXPECT_FALSE(cert_1->IsIssuedByEncoded(authority_2));
117 EXPECT_TRUE(cert_2->IsIssuedByEncoded(authority_2));
118 EXPECT_FALSE(cert_2->IsIssuedByEncoded(authority_1));
119
120 std::vector<scoped_refptr<X509Certificate> > certs;
121 certs.push_back(cert_1);
122 certs.push_back(cert_2);
David Benjamin1c4b6d012019-07-08 17:12:57123 auto request = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]5482ef9e2013-12-11 04:27:43124 request->cert_authorities = authority_1;
125
mattm436ccfe2017-06-19 20:24:08126 ClientCertIdentityList selected_identities;
127 bool rv = this->delegate_.SelectClientCerts(certs, *request.get(),
128 &selected_identities);
[email protected]5482ef9e2013-12-11 04:27:43129 EXPECT_TRUE(rv);
mattm436ccfe2017-06-19 20:24:08130 ASSERT_EQ(1u, selected_identities.size());
Matt Mueller294998d2018-04-17 03:04:53131 EXPECT_TRUE(selected_identities[0]->certificate()->EqualsExcludingChain(
132 cert_1.get()));
[email protected]5482ef9e2013-12-11 04:27:43133}
134
Matt Mueller6c8b07c62017-10-09 21:02:25135TYPED_TEST_P(ClientCertStoreTest, PrintableStringContainingUTF8) {
136 base::FilePath certs_dir =
137 GetTestNetDataDirectory().AppendASCII("parse_certificate_unittest");
138
139 std::string file_data;
140 ASSERT_TRUE(base::ReadFileToString(
141 certs_dir.AppendASCII(
142 "subject_printable_string_containing_utf8_client_cert.pem"),
143 &file_data));
144
145 net::PEMTokenizer pem_tokenizer(file_data, {"CERTIFICATE"});
146 ASSERT_TRUE(pem_tokenizer.GetNext());
147 std::string cert_der(pem_tokenizer.data());
148 ASSERT_FALSE(pem_tokenizer.GetNext());
149
150 bssl::UniquePtr<CRYPTO_BUFFER> cert_handle =
151 x509_util::CreateCryptoBuffer(cert_der);
152 ASSERT_TRUE(cert_handle);
153
154 X509Certificate::UnsafeCreateOptions options;
155 options.printable_string_is_utf8 = true;
156 scoped_refptr<X509Certificate> cert =
Matt Muellera4193272017-12-07 00:23:34157 X509Certificate::CreateFromBufferUnsafeOptions(std::move(cert_handle), {},
Matt Mueller6c8b07c62017-10-09 21:02:25158 options);
159 ASSERT_TRUE(cert);
160
David Benjamin1c4b6d012019-07-08 17:12:57161 auto request = base::MakeRefCounted<SSLCertRequestInfo>();
Matt Mueller6c8b07c62017-10-09 21:02:25162
163 ClientCertIdentityList selected_identities;
164 bool rv = this->delegate_.SelectClientCerts({cert}, *request.get(),
165 &selected_identities);
166 EXPECT_TRUE(rv);
167 ASSERT_EQ(1u, selected_identities.size());
Matt Mueller294998d2018-04-17 03:04:53168 EXPECT_TRUE(
169 selected_identities[0]->certificate()->EqualsExcludingChain(cert.get()));
Matt Mueller6c8b07c62017-10-09 21:02:25170}
171
Victor Costan2309ea02019-02-13 21:35:47172REGISTER_TYPED_TEST_SUITE_P(ClientCertStoreTest,
173 EmptyQuery,
174 AllIssuersAllowed,
Matt Muellere340fcaf2019-02-15 00:48:41175 CertAuthorityFiltering,
Victor Costan2309ea02019-02-13 21:35:47176 PrintableStringContainingUTF8);
[email protected]5482ef9e2013-12-11 04:27:43177
178} // namespace net
179
180#endif // NET_SSL_CLIENT_CERT_STORE_UNITTEST_INL_H_