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