[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 1 | // 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 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/files/file_path.h" |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
Matt Mueller | dd8f469 | 2019-11-21 01:09:32 | [diff] [blame] | 15 | #include "net/cert/pem.h" |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 16 | #include "net/cert/x509_util.h" |
pneubeck | 385704ec | 2015-08-25 08:56:37 | [diff] [blame] | 17 | #include "net/ssl/ssl_cert_request_info.h" |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 18 | #include "net/test/cert_test_util.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 19 | #include "net/test/test_data_directory.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 20 | #include "net/test/test_with_task_environment.h" |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // "CN=B CA" - DER encoded DN of the issuer of client_1.pem |
| 28 | const unsigned char kAuthority1DN[] = { |
davidben | 8d569f5 | 2016-07-29 16:03:18 | [diff] [blame] | 29 | 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, |
| 30 | 0x04, 0x03, 0x0c, 0x04, 0x42, 0x20, 0x43, 0x41, |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // "CN=E CA" - DER encoded DN of the issuer of client_2.pem |
davidben | 8d569f5 | 2016-07-29 16:03:18 | [diff] [blame] | 34 | const 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. |
| 41 | const 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] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 44 | }; |
| 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 Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 53 | // REGISTER_TYPED_TEST_SUITE_P macro at the bottom of this file must be updated. |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 54 | // |
Victor Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 55 | // 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] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 58 | // const SSLCertRequestInfo& cert_request_info, |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 59 | // ClientCertIdentityList* selected_identities); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 60 | template <typename T> |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 61 | class ClientCertStoreTest : public TestWithTaskEnvironment { |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 62 | public: |
| 63 | T delegate_; |
| 64 | }; |
| 65 | |
Victor Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 66 | TYPED_TEST_SUITE_P(ClientCertStoreTest); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 67 | |
| 68 | TYPED_TEST_P(ClientCertStoreTest, EmptyQuery) { |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 69 | CertificateList certs; |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 70 | auto request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 71 | |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 72 | ClientCertIdentityList selected_identities; |
| 73 | bool rv = this->delegate_.SelectClientCerts(certs, *request.get(), |
| 74 | &selected_identities); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 75 | EXPECT_TRUE(rv); |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 76 | EXPECT_EQ(0u, selected_identities.size()); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Verify that CertRequestInfo with empty |cert_authorities| matches all |
| 80 | // issuers, rather than no issuers. |
| 81 | TYPED_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 Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 88 | auto request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 89 | |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 90 | ClientCertIdentityList selected_identities; |
| 91 | bool rv = this->delegate_.SelectClientCerts(certs, *request.get(), |
| 92 | &selected_identities); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 93 | EXPECT_TRUE(rv); |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 94 | ASSERT_EQ(1u, selected_identities.size()); |
Matt Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 95 | EXPECT_TRUE( |
| 96 | selected_identities[0]->certificate()->EqualsExcludingChain(cert.get())); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Verify that certificates are correctly filtered against CertRequestInfo with |
| 100 | // |cert_authorities| containing only |authority_1_DN|. |
Matt Mueller | e340fcaf | 2019-02-15 00:48:41 | [diff] [blame] | 101 | TYPED_TEST_P(ClientCertStoreTest, CertAuthorityFiltering) { |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 102 | 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 Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 123 | auto request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 124 | request->cert_authorities = authority_1; |
| 125 | |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 126 | ClientCertIdentityList selected_identities; |
| 127 | bool rv = this->delegate_.SelectClientCerts(certs, *request.get(), |
| 128 | &selected_identities); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 129 | EXPECT_TRUE(rv); |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 130 | ASSERT_EQ(1u, selected_identities.size()); |
Matt Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 131 | EXPECT_TRUE(selected_identities[0]->certificate()->EqualsExcludingChain( |
| 132 | cert_1.get())); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 133 | } |
| 134 | |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 135 | TYPED_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 Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 157 | X509Certificate::CreateFromBufferUnsafeOptions(std::move(cert_handle), {}, |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 158 | options); |
| 159 | ASSERT_TRUE(cert); |
| 160 | |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 161 | auto request = base::MakeRefCounted<SSLCertRequestInfo>(); |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 162 | |
| 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 Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 168 | EXPECT_TRUE( |
| 169 | selected_identities[0]->certificate()->EqualsExcludingChain(cert.get())); |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 170 | } |
| 171 | |
Victor Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 172 | REGISTER_TYPED_TEST_SUITE_P(ClientCertStoreTest, |
| 173 | EmptyQuery, |
| 174 | AllIssuersAllowed, |
Matt Mueller | e340fcaf | 2019-02-15 00:48:41 | [diff] [blame] | 175 | CertAuthorityFiltering, |
Victor Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 176 | PrintableStringContainingUTF8); |
[email protected] | 5482ef9e | 2013-12-11 04:27:43 | [diff] [blame] | 177 | |
| 178 | } // namespace net |
| 179 | |
| 180 | #endif // NET_SSL_CLIENT_CERT_STORE_UNITTEST_INL_H_ |