| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/ssl/client_cert_store_nss.h" | 5 #include "net/ssl/client_cert_store_nss.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <certt.h> | 8 #include <certt.h> |
| 9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "crypto/scoped_test_nss_db.h" | 18 #include "crypto/scoped_test_nss_db.h" |
| 18 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
| 20 #include "net/ssl/client_cert_identity_test_util.h" |
| 19 #include "net/ssl/client_cert_store_unittest-inl.h" | 21 #include "net/ssl/client_cert_store_unittest-inl.h" |
| 20 #include "net/ssl/ssl_cert_request_info.h" | 22 #include "net/ssl/ssl_cert_request_info.h" |
| 21 #include "net/test/cert_test_util.h" | 23 #include "net/test/cert_test_util.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 24 namespace net { | 26 namespace net { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 void SaveCertsAndQuitCallback(CertificateList* out_certs, | 30 void SaveCertsAndQuitCallback(ClientCertIdentityList* out_certs, |
| 29 base::Closure quit_closure, | 31 base::Closure quit_closure, |
| 30 CertificateList in_certs) { | 32 ClientCertIdentityList in_certs) { |
| 31 *out_certs = std::move(in_certs); | 33 *out_certs = std::move(in_certs); |
| 32 quit_closure.Run(); | 34 quit_closure.Run(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 class ClientCertStoreNSSTestDelegate { | 39 class ClientCertStoreNSSTestDelegate { |
| 38 public: | 40 public: |
| 39 ClientCertStoreNSSTestDelegate() {} | 41 ClientCertStoreNSSTestDelegate() {} |
| 40 | 42 |
| 41 bool SelectClientCerts(const CertificateList& input_certs, | 43 bool SelectClientCerts(const CertificateList& input_certs, |
| 42 const SSLCertRequestInfo& cert_request_info, | 44 const SSLCertRequestInfo& cert_request_info, |
| 43 CertificateList* selected_certs) { | 45 ClientCertIdentityList* selected_certs) { |
| 44 // Filters |input_certs| using the logic being used to filter the system | 46 for (const auto& cert : input_certs) |
| 47 selected_certs->push_back(base::MakeUnique<FakeClientCertIdentity>(cert)); |
| 48 |
| 49 // Filters |selected_certs| using the logic being used to filter the system |
| 45 // store when GetClientCerts() is called. | 50 // store when GetClientCerts() is called. |
| 46 ClientCertStoreNSS::FilterCertsOnWorkerThread( | 51 ClientCertStoreNSS::FilterCertsOnWorkerThread(selected_certs, |
| 47 input_certs, cert_request_info, selected_certs); | 52 cert_request_info); |
| 48 return true; | 53 return true; |
| 49 } | 54 } |
| 50 }; | 55 }; |
| 51 | 56 |
| 52 INSTANTIATE_TYPED_TEST_CASE_P(NSS, | 57 INSTANTIATE_TYPED_TEST_CASE_P(NSS, |
| 53 ClientCertStoreTest, | 58 ClientCertStoreTest, |
| 54 ClientCertStoreNSSTestDelegate); | 59 ClientCertStoreNSSTestDelegate); |
| 55 | 60 |
| 56 // Tests that ClientCertStoreNSS attempts to build a certificate chain by | 61 // Tests that ClientCertStoreNSS attempts to build a certificate chain by |
| 57 // querying NSS before return a certificate. | 62 // querying NSS before return a certificate. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 71 | 76 |
| 72 std::unique_ptr<ClientCertStoreNSS> store( | 77 std::unique_ptr<ClientCertStoreNSS> store( |
| 73 new ClientCertStoreNSS(ClientCertStoreNSS::PasswordDelegateFactory())); | 78 new ClientCertStoreNSS(ClientCertStoreNSS::PasswordDelegateFactory())); |
| 74 | 79 |
| 75 { | 80 { |
| 76 // Request certificates matching B CA, |client_1|'s issuer. | 81 // Request certificates matching B CA, |client_1|'s issuer. |
| 77 scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo); | 82 scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo); |
| 78 request->cert_authorities.push_back(std::string( | 83 request->cert_authorities.push_back(std::string( |
| 79 reinterpret_cast<const char*>(kAuthority1DN), sizeof(kAuthority1DN))); | 84 reinterpret_cast<const char*>(kAuthority1DN), sizeof(kAuthority1DN))); |
| 80 | 85 |
| 81 CertificateList selected_certs; | 86 ClientCertIdentityList selected_certs; |
| 82 base::RunLoop loop; | 87 base::RunLoop loop; |
| 83 store->GetClientCerts(*request.get(), | 88 store->GetClientCerts(*request.get(), |
| 84 base::Bind(SaveCertsAndQuitCallback, &selected_certs, | 89 base::Bind(SaveCertsAndQuitCallback, &selected_certs, |
| 85 loop.QuitClosure())); | 90 loop.QuitClosure())); |
| 86 loop.Run(); | 91 loop.Run(); |
| 87 | 92 |
| 88 // The result be |client_1| with no intermediates. | 93 // The result be |client_1| with no intermediates. |
| 89 ASSERT_EQ(1u, selected_certs.size()); | 94 ASSERT_EQ(1u, selected_certs.size()); |
| 90 scoped_refptr<X509Certificate> selected_cert = selected_certs[0]; | 95 scoped_refptr<X509Certificate> selected_cert = |
| 96 selected_certs[0]->certificate(); |
| 91 EXPECT_TRUE(X509Certificate::IsSameOSCert(client_1->os_cert_handle(), | 97 EXPECT_TRUE(X509Certificate::IsSameOSCert(client_1->os_cert_handle(), |
| 92 selected_cert->os_cert_handle())); | 98 selected_cert->os_cert_handle())); |
| 93 ASSERT_EQ(0u, selected_cert->GetIntermediateCertificates().size()); | 99 ASSERT_EQ(0u, selected_cert->GetIntermediateCertificates().size()); |
| 94 } | 100 } |
| 95 | 101 |
| 96 { | 102 { |
| 97 // Request certificates matching C Root CA, |client_1_ca|'s issuer. | 103 // Request certificates matching C Root CA, |client_1_ca|'s issuer. |
| 98 scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo); | 104 scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo); |
| 99 request->cert_authorities.push_back( | 105 request->cert_authorities.push_back( |
| 100 std::string(reinterpret_cast<const char*>(kAuthorityRootDN), | 106 std::string(reinterpret_cast<const char*>(kAuthorityRootDN), |
| 101 sizeof(kAuthorityRootDN))); | 107 sizeof(kAuthorityRootDN))); |
| 102 | 108 |
| 103 CertificateList selected_certs; | 109 ClientCertIdentityList selected_certs; |
| 104 base::RunLoop loop; | 110 base::RunLoop loop; |
| 105 store->GetClientCerts(*request.get(), | 111 store->GetClientCerts(*request.get(), |
| 106 base::Bind(SaveCertsAndQuitCallback, &selected_certs, | 112 base::Bind(SaveCertsAndQuitCallback, &selected_certs, |
| 107 loop.QuitClosure())); | 113 loop.QuitClosure())); |
| 108 loop.Run(); | 114 loop.Run(); |
| 109 | 115 |
| 110 // The result be |client_1| with |client_1_ca| as an intermediate. | 116 // The result be |client_1| with |client_1_ca| as an intermediate. |
| 111 ASSERT_EQ(1u, selected_certs.size()); | 117 ASSERT_EQ(1u, selected_certs.size()); |
| 112 scoped_refptr<X509Certificate> selected_cert = selected_certs[0]; | 118 scoped_refptr<X509Certificate> selected_cert = |
| 119 selected_certs[0]->certificate(); |
| 113 EXPECT_TRUE(X509Certificate::IsSameOSCert(client_1->os_cert_handle(), | 120 EXPECT_TRUE(X509Certificate::IsSameOSCert(client_1->os_cert_handle(), |
| 114 selected_cert->os_cert_handle())); | 121 selected_cert->os_cert_handle())); |
| 115 ASSERT_EQ(1u, selected_cert->GetIntermediateCertificates().size()); | 122 ASSERT_EQ(1u, selected_cert->GetIntermediateCertificates().size()); |
| 116 EXPECT_TRUE(X509Certificate::IsSameOSCert( | 123 EXPECT_TRUE(X509Certificate::IsSameOSCert( |
| 117 client_1_ca->os_cert_handle(), | 124 client_1_ca->os_cert_handle(), |
| 118 selected_cert->GetIntermediateCertificates()[0])); | 125 selected_cert->GetIntermediateCertificates()[0])); |
| 119 } | 126 } |
| 120 } | 127 } |
| 121 | 128 |
| 122 } // namespace net | 129 } // namespace net |
| OLD | NEW |