curl-fuzzer C++ reference
Loading...
Searching...
No Matches
tls_test_credential_cache.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
9
11
12#include <openssl/pem.h>
13#include <openssl/ssl.h>
14
15#include "proto_fuzzer/tls_test_credentials.h"
16
17namespace proto_fuzzer {
18
19namespace {
20
24X509* ParseCertificate(const char* pem) {
25 BIO* bio = BIO_new_mem_buf(pem, -1);
26 if (bio == nullptr) {
27 return nullptr;
28 }
29 X509* certificate = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
30 BIO_free(bio);
31 return certificate;
32}
33
37EVP_PKEY* ParsePrivateKey(const char* pem) {
38 BIO* bio = BIO_new_mem_buf(pem, -1);
39 if (bio == nullptr) {
40 return nullptr;
41 }
42 EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
43 BIO_free(bio);
44 return key;
45}
46
52bool AddCachedExtraChainCertificate(SSL_CTX* context, X509* certificate) {
53 if (certificate == nullptr || X509_up_ref(certificate) != 1) {
54 return false;
55 }
56 if (SSL_CTX_add_extra_chain_cert(context, certificate) != 1) {
57 X509_free(certificate);
58 return false;
59 }
60 return true;
61}
62
65class PrimaryCredentials {
66 public:
68 PrimaryCredentials()
69 : certificate_(ParseCertificate(tls_test_credentials::kCertificatePem)),
70 key_(ParsePrivateKey(tls_test_credentials::kPrivateKeyPem)) {}
71
73 ~PrimaryCredentials() {
74 X509_free(certificate_);
75 EVP_PKEY_free(key_);
76 }
77
79 PrimaryCredentials(const PrimaryCredentials&) = delete;
81 PrimaryCredentials& operator=(const PrimaryCredentials&) = delete;
82
84 bool valid() const { return certificate_ != nullptr && key_ != nullptr; }
86 X509* certificate() const { return certificate_; }
88 EVP_PKEY* key() const { return key_; }
89
90 private:
91 X509* certificate_;
92 EVP_PKEY* key_;
93};
94
96class AuxiliaryCertificates {
97 public:
99 AuxiliaryCertificates()
100 : rsa_(ParseCertificate(tls_test_credentials::kRsaCertificatePem)),
101 dsa_(ParseCertificate(tls_test_credentials::kDsaCertificatePem)),
102 dh_(ParseCertificate(tls_test_credentials::kDhCertificatePem)) {}
103
105 ~AuxiliaryCertificates() {
106 X509_free(rsa_);
107 X509_free(dsa_);
108 X509_free(dh_);
109 }
110
112 AuxiliaryCertificates(const AuxiliaryCertificates&) = delete;
114 AuxiliaryCertificates& operator=(const AuxiliaryCertificates&) = delete;
115
117 bool valid() const { return rsa_ != nullptr && dsa_ != nullptr && dh_ != nullptr; }
119 X509* rsa() const { return rsa_; }
121 X509* dsa() const { return dsa_; }
123 X509* dh() const { return dh_; }
124
125 private:
126 X509* rsa_;
127 X509* dsa_;
128 X509* dh_;
129};
130
132const PrimaryCredentials& CachedPrimaryCredentials() {
133 static const PrimaryCredentials credentials;
134 return credentials;
135}
136
139const AuxiliaryCertificates& CachedAuxiliaryCertificates() {
140 static const AuxiliaryCertificates certificates;
141 return certificates;
142}
143
144} // namespace
145
155 curl::fuzzer::proto::TlsCertificateChainProfile certificate_chain) {
156 if (context == nullptr) {
157 return false;
158 }
159
160 const PrimaryCredentials& primary = CachedPrimaryCredentials();
161 if (!primary.valid() || SSL_CTX_use_certificate(context, primary.certificate()) != 1 ||
162 SSL_CTX_use_PrivateKey(context, primary.key()) != 1 || SSL_CTX_check_private_key(context) != 1) {
163 return false;
164 }
165
166 if (certificate_chain != curl::fuzzer::proto::TLS_CERTIFICATE_CHAIN_ALL_KEY_TYPES) {
167 return true;
168 }
169
170 const AuxiliaryCertificates& auxiliary = CachedAuxiliaryCertificates();
171 return auxiliary.valid() && AddCachedExtraChainCertificate(context, auxiliary.rsa()) &&
172 AddCachedExtraChainCertificate(context, auxiliary.dsa()) &&
173 AddCachedExtraChainCertificate(context, auxiliary.dh());
174}
175
176} // namespace proto_fuzzer
bool InstallCachedTlsTestCredentials(SSL_CTX *context, curl::fuzzer::proto::TlsCertificateChainProfile certificate_chain)
Install references to immutable, process-lifetime test credentials in one fresh TLS context.
Installs cached test credentials into fresh TLS contexts.