xrpld
Loading...
Searching...
No Matches
make_SSLContext.cpp
1#include <xrpl/basics/make_SSLContext.h>
2
3#include <xrpl/basics/contract.h>
4
5#include <boost/asio/ssl/context.hpp>
6#include <boost/asio/ssl/verify_mode.hpp>
7#include <boost/system/detail/error_code.hpp>
8#include <boost/system/detail/generic_category.hpp>
9
10#include <openssl/asn1.h>
11#include <openssl/bn.h>
12#include <openssl/crypto.h>
13#include <openssl/evp.h>
14#include <openssl/objects.h> // IWYU pragma: keep
15#include <openssl/ossl_typ.h>
16#include <openssl/pem.h>
17#include <openssl/rsa.h>
18#include <openssl/ssl.h>
19#include <openssl/x509.h>
20#include <openssl/x509v3.h>
21
22#include <cerrno>
23#include <cstdio>
24#include <ctime>
25#include <exception>
26#include <memory>
27#include <string>
28
29namespace xrpl {
30
31namespace openssl::detail {
32
50
63static constexpr char kDefaultDh[] =
64 "-----BEGIN DH PARAMETERS-----\n"
65 "MIIBCAKCAQEApKSWfR7LKy0VoZ/SDCObCvJ5HKX2J93RJ+QN8kJwHh+uuA8G+t8Q\n"
66 "MDRjL5HanlV/sKN9HXqBc7eqHmmbqYwIXKUt9MUZTLNheguddxVlc2IjdP5i9Ps8\n"
67 "l7su8tnP0l1JvC6Rfv3epRsEAw/ZW/lC2IwkQPpOmvnENQhQ6TgrUzcGkv4Bn0X6\n"
68 "pxrDSBpZ+45oehGCUAtcbY8b02vu8zPFoxqo6V/+MIszGzldlik5bVqrJpVF6E8C\n"
69 "tRqHjj6KuDbPbjc+pRGvwx/BSO3SULxmYu9J1NOk090MU1CMt6IJY7TpEc9Xrac9\n"
70 "9yqY3xXZID240RRcaJ25+U4lszFPqP+CEwIBAg==\n"
71 "-----END DH PARAMETERS-----";
72
87std::string const kDefaultCipherList = "TLSv1.2:!CBC:!DSS:!PSK:!eNULL:!aNULL";
88
89static void
90initAnonymous(boost::asio::ssl::context& context)
91{
92 using namespace openssl;
93
94 static auto kDefaultRsa = []() {
95 BIGNUM* bn = BN_new();
96 BN_set_word(bn, RSA_F4);
97
98 auto rsa = RSA_new();
99
100 if (!rsa)
101 logicError("RSA_new failed");
102
103 if (RSA_generate_key_ex(rsa, gDefaultRsaKeyBits, bn, nullptr) != 1)
104 logicError("RSA_generate_key_ex failure");
105
106 BN_clear_free(bn);
107
108 return rsa;
109 }();
110
111 static auto kDefaultEphemeralPrivateKey = []() {
112 auto pkey = EVP_PKEY_new();
113
114 if (!pkey)
115 logicError("EVP_PKEY_new failed");
116
117 // We need to up the reference count of here, since we are retaining a
118 // copy of the key for (potential) reuse.
119 if (RSA_up_ref(kDefaultRsa) != 1)
120 logicError("EVP_PKEY_assign_RSA: incrementing reference count failed");
121
122 if (!EVP_PKEY_assign_RSA(pkey, kDefaultRsa))
123 logicError("EVP_PKEY_assign_RSA failed");
124
125 return pkey;
126 }();
127
128 static auto kDefaultCert = []() {
129 auto x509 = X509_new();
130
131 if (x509 == nullptr)
132 logicError("X509_new failed");
133
134 // According to the standards (X.509 et al), the value should be one
135 // less than the actually certificate version we want. Since we want
136 // version 3, we must use a 2.
137 X509_set_version(x509, 2);
138
139 // To avoid leaking information about the precise time that the
140 // server started up, we adjust the validity period:
141 char buf[16] = {0};
142
143 auto const ts = std::time(nullptr) - (25 * 60 * 60);
144
145 int const ret = std::strftime(buf, sizeof(buf) - 1, "%y%m%d000000Z", std::gmtime(&ts));
146
147 buf[ret] = 0;
148
149 if (ASN1_TIME_set_string_X509(X509_get_notBefore(x509), buf) != 1)
150 logicError("Unable to set certificate validity date");
151
152 // And make it valid for two years
153 X509_gmtime_adj(X509_get_notAfter(x509), 2 * 365 * 24 * 60 * 60);
154
155 // Set a serial number
156 if (auto b = BN_new(); b != nullptr)
157 {
158 if (BN_rand(b, 128, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
159 {
160 if (auto a = ASN1_INTEGER_new(); a != nullptr)
161 {
162 if (BN_to_ASN1_INTEGER(b, a))
163 X509_set_serialNumber(x509, a);
164
165 ASN1_INTEGER_free(a);
166 }
167 }
168
169 BN_clear_free(b);
170 }
171
172 // Some certificate details
173 {
174 X509V3_CTX ctx;
175
176 X509V3_set_ctx_nodb(&ctx);
177 X509V3_set_ctx(&ctx, x509, x509, nullptr, nullptr, 0);
178
179 if (auto ext =
180 X509V3_EXT_conf_nid(nullptr, &ctx, NID_basic_constraints, "critical,CA:FALSE"))
181 {
182 X509_add_ext(x509, ext, -1);
183 X509_EXTENSION_free(ext);
184 }
185
186 if (auto ext = X509V3_EXT_conf_nid(
187 nullptr, &ctx, NID_ext_key_usage, "critical,serverAuth,clientAuth"))
188 {
189 X509_add_ext(x509, ext, -1);
190 X509_EXTENSION_free(ext);
191 }
192
193 if (auto ext =
194 X509V3_EXT_conf_nid(nullptr, &ctx, NID_key_usage, "critical,digitalSignature"))
195 {
196 X509_add_ext(x509, ext, -1);
197 X509_EXTENSION_free(ext);
198 }
199
200 if (auto ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_subject_key_identifier, "hash"))
201 {
202 X509_add_ext(x509, ext, -1);
203 X509_EXTENSION_free(ext);
204 }
205 }
206
207 // And a private key
208 X509_set_pubkey(x509, kDefaultEphemeralPrivateKey);
209
210 if (!X509_sign(x509, kDefaultEphemeralPrivateKey, EVP_sha256()))
211 logicError("X509_sign failed");
212
213 return x509;
214 }();
215
216 SSL_CTX* const ctx = context.native_handle();
217
218 if (SSL_CTX_use_certificate(ctx, kDefaultCert) <= 0)
219 logicError("SSL_CTX_use_certificate failed");
220
221 if (SSL_CTX_use_PrivateKey(ctx, kDefaultEphemeralPrivateKey) <= 0)
222 logicError("SSL_CTX_use_PrivateKey failed");
223}
224
225static void
227 boost::asio::ssl::context& context,
228 std::string const& keyFile,
229 std::string const& certFile,
230 std::string const& chainFile)
231{
232 auto fmtError = [](boost::system::error_code ec) -> std::string {
233 return " [" + std::to_string(ec.value()) + ": " + ec.message() + "]";
234 };
235
236 SSL_CTX* const ssl = context.native_handle();
237
238 bool certSet = false;
239
240 if (!certFile.empty())
241 {
242 boost::system::error_code ec;
243
244 // NOLINTNEXTLINE(bugprone-unused-return-value)
245 context.use_certificate_file(certFile, boost::asio::ssl::context::pem, ec);
246
247 if (ec)
248 logicError("Problem with SSL certificate file" + fmtError(ec));
249
250 certSet = true;
251 }
252
253 if (!chainFile.empty())
254 {
255 // VFALCO Replace fopen() with RAII
256 FILE* f = fopen(chainFile.c_str(), "r");
257
258 if (f == nullptr)
259 {
261 "Problem opening SSL chain file" +
262 fmtError(boost::system::error_code(errno, boost::system::generic_category())));
263 }
264
265 try
266 {
267 for (;;)
268 {
269 X509* const x = PEM_read_X509(f, nullptr, nullptr, nullptr);
270
271 if (x == nullptr)
272 break;
273
274 if (!certSet)
275 {
276 if (SSL_CTX_use_certificate(ssl, x) != 1)
277 {
279 "Problem retrieving SSL certificate from chain "
280 "file.");
281 }
282
283 certSet = true;
284 }
285 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
286 {
287 X509_free(x);
288 logicError("Problem adding SSL chain certificate.");
289 }
290 }
291
292 fclose(f);
293 }
294 catch (std::exception const& ex)
295 {
296 fclose(f);
298 std::string("Reading the SSL chain file generated an exception: ") + ex.what());
299 }
300 }
301
302 if (!keyFile.empty())
303 {
304 boost::system::error_code ec;
305
306 // NOLINTNEXTLINE(bugprone-unused-return-value)
307 context.use_private_key_file(keyFile, boost::asio::ssl::context::pem, ec);
308
309 if (ec)
310 {
311 logicError("Problem using the SSL private key file" + fmtError(ec));
312 }
313 }
314
315 if (SSL_CTX_check_private_key(ssl) != 1)
316 {
317 logicError("Invalid key in SSL private key file.");
318 }
319}
320
323{
324 auto c = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
325
326 c->set_options(
327 boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 |
328 boost::asio::ssl::context::no_sslv3 | boost::asio::ssl::context::no_tlsv1 |
329 boost::asio::ssl::context::no_tlsv1_1 | boost::asio::ssl::context::single_dh_use |
330 boost::asio::ssl::context::no_compression);
331
332 if (cipherList.empty())
333 cipherList = kDefaultCipherList;
334
335 if (auto result = SSL_CTX_set_cipher_list(c->native_handle(), cipherList.c_str()); result != 1)
336 logicError("SSL_CTX_set_cipher_list failed");
337
338 c->use_tmp_dh({std::addressof(detail::kDefaultDh), sizeof(kDefaultDh)});
339
340 // Disable all renegotiation support in TLS v1.2. This can help prevent
341 // exploitation of the bug described in CVE-2021-3499 (for details see
342 // https://www.openssl.org/news/secadv/20210325.txt) when linking
343 // against OpenSSL versions prior to 1.1.1k.
344 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);
345
346 return c;
347}
348
349} // namespace openssl::detail
350
351//------------------------------------------------------------------------------
353makeSslContext(std::string const& cipherList)
354{
355 auto context = openssl::detail::getContext(cipherList);
357 // VFALCO NOTE, It seems the WebSocket context never has
358 // set_verify_mode called, for either setting of WEBSOCKET_SECURE
359 context->set_verify_mode(boost::asio::ssl::verify_none);
360 return context;
361}
362
365 std::string const& keyFile,
366 std::string const& certFile,
367 std::string const& chainFile,
368 std::string const& cipherList)
369{
370 auto context = openssl::detail::getContext(cipherList);
371 openssl::detail::initAuthenticated(*context, keyFile, certFile, chainFile);
372 return context;
373}
374
375} // namespace xrpl
T addressof(T... args)
T c_str(T... args)
T empty(T... args)
T gmtime(T... args)
T make_shared(T... args)
static void initAnonymous(boost::asio::ssl::context &context)
std::shared_ptr< boost::asio::ssl::context > getContext(std::string cipherList)
std::string const kDefaultCipherList
The default list of ciphers we accept over TLS.
static constexpr char kDefaultDh[]
The default DH parameters.
int gDefaultRsaKeyBits
The default strength of self-signed RSA certificates.
static void initAuthenticated(boost::asio::ssl::context &context, std::string const &keyFile, std::string const &certFile, std::string const &chainFile)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
std::shared_ptr< boost::asio::ssl::context > makeSslContextAuthed(std::string const &keyFile, std::string const &certFile, std::string const &chainFile, std::string const &cipherList)
Create an authenticated SSL context using the specified files.
std::shared_ptr< boost::asio::ssl::context > makeSslContext(std::string const &cipherList)
Create a self-signed SSL context that allows anonymous Diffie Hellman.
T strftime(T... args)
T time(T... args)
T to_string(T... args)
T what(T... args)