1 // Copyright 2017 The Chromium Authors 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_IDENTITY_TEST_UTIL_H_ 6 #define NET_SSL_CLIENT_CERT_IDENTITY_TEST_UTIL_H_ 7 8 #include "net/ssl/client_cert_identity.h" 9 10 namespace base { 11 class FilePath; 12 } 13 14 namespace net { 15 16 // Simple ClientCertIdentity implementation for testing. 17 // Note: this implementation of AcquirePrivateKey will always call the callback 18 // synchronously. 19 class FakeClientCertIdentity : public ClientCertIdentity { 20 public: 21 FakeClientCertIdentity(scoped_refptr<X509Certificate> cert, 22 scoped_refptr<SSLPrivateKey> key); 23 ~FakeClientCertIdentity() override; 24 25 // Creates a FakeClientCertIdentity from a certificate file (DER or PEM) and 26 // private key file (unencrypted pkcs8). Returns nullptr on error. 27 static std::unique_ptr<FakeClientCertIdentity> CreateFromCertAndKeyFiles( 28 const base::FilePath& dir, 29 const std::string& cert_filename, 30 const std::string& key_filename); 31 32 // Creates a FakeClientCertIdentity from a certificate file (DER or PEM). 33 // Signing attempts will fail. Returns nullptr on error. 34 static std::unique_ptr<FakeClientCertIdentity> CreateFromCertAndFailSigning( 35 const base::FilePath& dir, 36 const std::string& cert_filename); 37 38 // Duplicates the FakeClientCertIdentity. 39 std::unique_ptr<FakeClientCertIdentity> Copy(); 40 41 // Returns the SSLPrivateKey in a more convenient way, for tests. ssl_private_key()42 SSLPrivateKey* ssl_private_key() const { return key_.get(); } 43 44 // ClientCertIdentity implementation: 45 void AcquirePrivateKey(base::OnceCallback<void(scoped_refptr<SSLPrivateKey>)> 46 private_key_callback) override; 47 48 private: 49 scoped_refptr<SSLPrivateKey> key_; 50 }; 51 52 // Converts a CertificateList to a ClientCertIdentityList of 53 // FakeClientCertIdentity, with null private keys. 54 ClientCertIdentityList FakeClientCertIdentityListFromCertificateList( 55 const CertificateList& certs); 56 57 } // namespace net 58 59 #endif // NET_SSL_CLIENT_CERT_IDENTITY_TEST_UTIL_H_ 60