summaryrefslogtreecommitdiff
path: root/chromium/net/ssl/client_cert_identity_test_util.cc
blob: 43491222db33c44d38599137e929ceb9809228ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/ssl/client_cert_identity_test_util.h"

#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "net/ssl/ssl_private_key.h"
#include "net/ssl/test_ssl_private_key.h"
#include "net/test/cert_test_util.h"
#include "third_party/boringssl/src/include/openssl/bytestring.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

namespace net {

FakeClientCertIdentity::FakeClientCertIdentity(
    scoped_refptr<X509Certificate> cert,
    scoped_refptr<SSLPrivateKey> key)
    : ClientCertIdentity(std::move(cert)), key_(std::move(key)) {}

FakeClientCertIdentity::~FakeClientCertIdentity() = default;

// static
std::unique_ptr<FakeClientCertIdentity>
FakeClientCertIdentity::CreateFromCertAndKeyFiles(
    const base::FilePath& dir,
    const std::string& cert_filename,
    const std::string& key_filename) {
  scoped_refptr<X509Certificate> cert =
      net::ImportCertFromFile(dir, cert_filename);
  if (!cert)
    return nullptr;

  std::string pkcs8;
  if (!base::ReadFileToString(dir.AppendASCII(key_filename), &pkcs8))
    return nullptr;

  CBS cbs;
  CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pkcs8.data()), pkcs8.size());
  bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
  if (!pkey || CBS_len(&cbs) != 0)
    return nullptr;

  scoped_refptr<SSLPrivateKey> ssl_private_key =
      WrapOpenSSLPrivateKey(std::move(pkey));
  if (!ssl_private_key)
    return nullptr;

  return std::make_unique<FakeClientCertIdentity>(cert, ssl_private_key);
}

// static
std::unique_ptr<FakeClientCertIdentity>
FakeClientCertIdentity::CreateFromCertAndFailSigning(
    const base::FilePath& dir,
    const std::string& cert_filename) {
  scoped_refptr<X509Certificate> cert =
      net::ImportCertFromFile(dir, cert_filename);
  if (!cert)
    return nullptr;

  return std::make_unique<FakeClientCertIdentity>(
      cert, CreateFailSigningSSLPrivateKey());
}

std::unique_ptr<FakeClientCertIdentity> FakeClientCertIdentity::Copy() {
  return std::make_unique<FakeClientCertIdentity>(certificate(), key_);
}

void FakeClientCertIdentity::AcquirePrivateKey(
    base::OnceCallback<void(scoped_refptr<SSLPrivateKey>)>
        private_key_callback) {
  std::move(private_key_callback).Run(key_);
}

ClientCertIdentityList FakeClientCertIdentityListFromCertificateList(
    const CertificateList& certs) {
  ClientCertIdentityList result;
  for (const auto& cert : certs) {
    result.push_back(std::make_unique<FakeClientCertIdentity>(cert, nullptr));
  }
  return result;
}

}  // namespace net