client_cert_identity_test_util.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "net/ssl/client_cert_identity_test_util.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "net/ssl/ssl_private_key.h"
  10. #include "net/ssl/test_ssl_private_key.h"
  11. #include "net/test/cert_test_util.h"
  12. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  13. #include "third_party/boringssl/src/include/openssl/evp.h"
  14. namespace net {
  15. FakeClientCertIdentity::FakeClientCertIdentity(
  16. scoped_refptr<X509Certificate> cert,
  17. scoped_refptr<SSLPrivateKey> key)
  18. : ClientCertIdentity(std::move(cert)), key_(std::move(key)) {}
  19. FakeClientCertIdentity::~FakeClientCertIdentity() = default;
  20. // static
  21. std::unique_ptr<FakeClientCertIdentity>
  22. FakeClientCertIdentity::CreateFromCertAndKeyFiles(
  23. const base::FilePath& dir,
  24. const std::string& cert_filename,
  25. const std::string& key_filename) {
  26. scoped_refptr<X509Certificate> cert =
  27. net::ImportCertFromFile(dir, cert_filename);
  28. if (!cert)
  29. return nullptr;
  30. std::string pkcs8;
  31. if (!base::ReadFileToString(dir.AppendASCII(key_filename), &pkcs8))
  32. return nullptr;
  33. CBS cbs;
  34. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pkcs8.data()), pkcs8.size());
  35. bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
  36. if (!pkey || CBS_len(&cbs) != 0)
  37. return nullptr;
  38. scoped_refptr<SSLPrivateKey> ssl_private_key =
  39. WrapOpenSSLPrivateKey(std::move(pkey));
  40. if (!ssl_private_key)
  41. return nullptr;
  42. return std::make_unique<FakeClientCertIdentity>(cert, ssl_private_key);
  43. }
  44. // static
  45. std::unique_ptr<FakeClientCertIdentity>
  46. FakeClientCertIdentity::CreateFromCertAndFailSigning(
  47. const base::FilePath& dir,
  48. const std::string& cert_filename) {
  49. scoped_refptr<X509Certificate> cert =
  50. net::ImportCertFromFile(dir, cert_filename);
  51. if (!cert)
  52. return nullptr;
  53. return std::make_unique<FakeClientCertIdentity>(
  54. cert, CreateFailSigningSSLPrivateKey());
  55. }
  56. std::unique_ptr<FakeClientCertIdentity> FakeClientCertIdentity::Copy() {
  57. return std::make_unique<FakeClientCertIdentity>(certificate(), key_);
  58. }
  59. void FakeClientCertIdentity::AcquirePrivateKey(
  60. base::OnceCallback<void(scoped_refptr<SSLPrivateKey>)>
  61. private_key_callback) {
  62. std::move(private_key_callback).Run(key_);
  63. }
  64. ClientCertIdentityList FakeClientCertIdentityListFromCertificateList(
  65. const CertificateList& certs) {
  66. ClientCertIdentityList result;
  67. for (const auto& cert : certs) {
  68. result.push_back(std::make_unique<FakeClientCertIdentity>(cert, nullptr));
  69. }
  70. return result;
  71. }
  72. } // namespace net