client_cert_identity_test_util.h 2.1 KB

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