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