Coverage Report

Created: 2026-07-16 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/libssl.hh
Line
Count
Source
1
#pragma once
2
3
#include <atomic>
4
#include <fstream>
5
#include <map>
6
#include <memory>
7
#include <optional>
8
#include <string>
9
#include <vector>
10
#include <optional>
11
12
#include "config.h"
13
#include "circular_buffer.hh"
14
#include "lock.hh"
15
#include "misc.hh"
16
17
enum class LibsslTLSVersion : uint8_t { Unknown, TLS10, TLS11, TLS12, TLS13 };
18
19
struct TLSCertKeyPair
20
{
21
  std::string d_cert;
22
  std::optional<std::string> d_key;
23
  std::optional<std::string> d_password;
24
  explicit TLSCertKeyPair(const std::string& cert, std::optional<std::string> key = std::nullopt, std::optional<std::string> password = std::nullopt):
25
0
    d_cert(cert), d_key(std::move(key)), d_password(std::move(password)) {
26
0
  }
27
};
28
29
class TLSConfig
30
{
31
public:
32
  std::vector<TLSCertKeyPair> d_certKeyPairs;
33
  std::vector<std::string> d_ocspFiles;
34
35
  std::string d_ciphers;
36
  std::string d_ciphers13;
37
  std::string d_ecdheCurves;
38
  std::string d_ticketKeyFile;
39
  std::string d_keyLogFile;
40
41
  size_t d_maxStoredSessions{20480};
42
  time_t d_sessionTimeout{0};
43
  time_t d_ticketsKeyRotationDelay{43200};
44
  uint8_t d_numberOfTicketsKeys{5};
45
  LibsslTLSVersion d_minTLSVersion{LibsslTLSVersion::TLS10};
46
47
  bool d_preferServerCiphers{true};
48
  bool d_enableTickets{true};
49
  /* whether OpenSSL will release I/O buffers when the connection
50
     becomes idle, saving memory */
51
  bool d_releaseBuffers{true};
52
  /* whether so-called secure renegotiation should be allowed for TLS < 1.3 */
53
  bool d_enableRenegotiation{false};
54
  /* enable TLS async mode, if supported by any engine */
55
  bool d_asyncMode{false};
56
  /* enable kTLS mode, if supported */
57
  bool d_ktls{false};
58
  /* set read ahead mode, if supported */
59
  bool d_readAhead{true};
60
};
61
62
struct TLSErrorCounters
63
{
64
  std::atomic<uint64_t> d_dhKeyTooSmall{0}; /* the other side sent a DH value that is not large enough */
65
  std::atomic<uint64_t> d_inappropriateFallBack{0}; /* SCSV indicates that the client previously tried a higher version,
66
                                                       something bad is happening */
67
  std::atomic<uint64_t> d_noSharedCipher{0}; /* we could not agree on a cipher to use */
68
  std::atomic<uint64_t> d_unknownCipherType{0}; /* unknown cipher type */
69
  std::atomic<uint64_t> d_unknownKeyExchangeType{0}; /* * unknown exchange type, weird */
70
  std::atomic<uint64_t> d_unknownProtocol{0}; /* unknown protocol (SSLv2 or TLS 1.4, who knows? */
71
  std::atomic<uint64_t> d_unsupportedEC{0}; /* unsupported elliptic curve */
72
  std::atomic<uint64_t> d_unsupportedProtocol{0}; /* we don't accept this TLS version, sorry */
73
};
74
75
#ifdef HAVE_LIBSSL
76
#include <openssl/ssl.h>
77
78
void registerOpenSSLUser();
79
void unregisterOpenSSLUser();
80
81
/* From rfc5077 Section 4. Recommended Ticket Construction */
82
#define TLS_TICKETS_KEY_NAME_SIZE (16)
83
84
/* AES-256 */
85
#define TLS_TICKETS_CIPHER_KEY_SIZE (32)
86
#define TLS_TICKETS_CIPHER_ALGO (EVP_aes_256_cbc)
87
88
/* HMAC SHA-256 */
89
#define TLS_TICKETS_MAC_KEY_SIZE (32)
90
#define TLS_TICKETS_MAC_ALGO (EVP_sha256)
91
92
class OpenSSLTLSTicketKey
93
{
94
public:
95
  OpenSSLTLSTicketKey();
96
  OpenSSLTLSTicketKey(std::ifstream& file);
97
  OpenSSLTLSTicketKey(const std::string& key);
98
  ~OpenSSLTLSTicketKey();
99
100
  bool nameMatches(const unsigned char name[TLS_TICKETS_KEY_NAME_SIZE]) const;
101
102
#if OPENSSL_VERSION_MAJOR >= 3
103
  int encrypt(unsigned char keyName[TLS_TICKETS_KEY_NAME_SIZE], unsigned char* iv, EVP_CIPHER_CTX* ectx, EVP_MAC_CTX* hctx) const;
104
  bool decrypt(const unsigned char* iv, EVP_CIPHER_CTX* ectx, EVP_MAC_CTX* hctx) const;
105
#else
106
  int encrypt(unsigned char keyName[TLS_TICKETS_KEY_NAME_SIZE], unsigned char* iv, EVP_CIPHER_CTX* ectx, HMAC_CTX* hctx) const;
107
  bool decrypt(const unsigned char* iv, EVP_CIPHER_CTX* ectx, HMAC_CTX* hctx) const;
108
#endif
109
110
  [[nodiscard]] std::string content() const;
111
112
private:
113
  unsigned char d_name[TLS_TICKETS_KEY_NAME_SIZE];
114
  unsigned char d_cipherKey[TLS_TICKETS_CIPHER_KEY_SIZE];
115
  unsigned char d_hmacKey[TLS_TICKETS_MAC_KEY_SIZE];
116
};
117
118
class OpenSSLTLSTicketKeysRing
119
{
120
public:
121
  OpenSSLTLSTicketKeysRing(size_t capacity);
122
  ~OpenSSLTLSTicketKeysRing();
123
  std::shared_ptr<OpenSSLTLSTicketKey> getEncryptionKey();
124
  std::shared_ptr<OpenSSLTLSTicketKey> getDecryptionKey(unsigned char name[TLS_TICKETS_KEY_NAME_SIZE], bool& activeKey);
125
  size_t getKeysCount();
126
  void loadTicketsKeys(const std::string& keyFile);
127
  void loadTicketsKey(const std::string& key);
128
  void rotateTicketsKey(time_t now);
129
130
private:
131
  void addKey(std::shared_ptr<OpenSSLTLSTicketKey>&& newKey);
132
  SharedLockGuarded<boost::circular_buffer<std::shared_ptr<OpenSSLTLSTicketKey> > > d_ticketKeys;
133
};
134
135
void* libssl_get_ticket_key_callback_data(SSL* s);
136
void libssl_set_ticket_key_callback_data(SSL_CTX* ctx, void* data);
137
138
#if OPENSSL_VERSION_MAJOR >= 3
139
int libssl_ticket_key_callback(SSL* s, OpenSSLTLSTicketKeysRing& keyring, unsigned char keyName[TLS_TICKETS_KEY_NAME_SIZE], unsigned char* iv, EVP_CIPHER_CTX* ectx, EVP_MAC_CTX* hctx, int enc);
140
#else
141
int libssl_ticket_key_callback(SSL* s, OpenSSLTLSTicketKeysRing& keyring, unsigned char keyName[TLS_TICKETS_KEY_NAME_SIZE], unsigned char* iv, EVP_CIPHER_CTX* ectx, HMAC_CTX* hctx, int enc);
142
#endif
143
144
#ifndef DISABLE_OCSP_STAPLING
145
int libssl_ocsp_stapling_callback(SSL* ssl, const std::map<int, std::string>& ocspMap);
146
#ifdef HAVE_OCSP_BASIC_SIGN
147
bool libssl_generate_ocsp_response(const std::string& certFile, const std::string& caCert, const std::string& caKey, const std::string& outFile, int ndays, int nmin);
148
#endif
149
#endif /* DISABLE_OCSP_STAPLING */
150
151
void libssl_set_error_counters_callback(SSL_CTX& ctx, TLSErrorCounters* counters);
152
153
LibsslTLSVersion libssl_tls_version_from_string(const std::string& str);
154
const std::string& libssl_tls_version_to_string(LibsslTLSVersion version);
155
156
157
namespace pdns::libssl {
158
class ServerContext
159
{
160
public:
161
  using SharedContext = std::shared_ptr<SSL_CTX>;
162
  using SNIToContextMap  = std::map<std::string, SharedContext, std::less<>>;
163
164
  SharedContext d_defaultContext;
165
  SNIToContextMap d_sniMap;
166
  std::map<int, std::string> d_ocspResponses;
167
};
168
}
169
170
/* add a keypair to an SSL context */
171
void libssl_setup_context_no_sni(SSL_CTX* ctx, const TLSCertKeyPair& pair, std::vector<int>& keyTypes);
172
173
/* return the created context, and a list of warning messages for issues not severe enough
174
   to trigger raising an exception, like failing to load an OCSP response file */
175
std::pair<std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>, std::vector<std::string>> libssl_init_server_context_no_sni(const TLSConfig& config,
176
                                                                                                                         std::map<int, std::string>& ocspResponses);
177
std::pair<pdns::libssl::ServerContext, std::vector<std::string>> libssl_init_server_context(const TLSConfig& config);
178
179
pdns::UniqueFilePtr libssl_set_key_log_file(SSL_CTX* ctx, const std::string& logFile);
180
181
/* called in a server context, to select an ALPN value advertised by the client if any */
182
void libssl_set_alpn_select_callback(SSL_CTX* ctx, int (*callback)(SSL* ssl, const unsigned char** out, unsigned char* outlen, const unsigned char* inPtr, unsigned int inlen, void* arg), void* arg);
183
/* set the supported ALPN protos in client context */
184
bool libssl_set_alpn_protos(SSL_CTX* ctx, const std::vector<std::vector<uint8_t>>& protos);
185
186
std::string libssl_get_error_string();
187
188
#if defined(HAVE_LIBSSL) && OPENSSL_VERSION_MAJOR >= 3 && defined(HAVE_TLS_PROVIDERS)
189
std::pair<bool, std::string> libssl_load_provider(const std::string& engineName);
190
#endif /* HAVE_LIBSSL && OPENSSL_VERSION_MAJOR >= 3 && HAVE_TLS_PROVIDERS */
191
192
#if defined(HAVE_LIBSSL) && !defined(HAVE_TLS_PROVIDERS)
193
std::pair<bool, std::string> libssl_load_engine(const std::string& engineName, const std::optional<std::string>& defaultString);
194
#endif /* HAVE_LIBSSL && !HAVE_TLS_PROVIDERS */
195
196
#endif /* HAVE_LIBSSL */