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 = X509V3_EXT_conf_nid(nullptr, &ctx, NID_basic_constraints, "critical,CA:FALSE"))
178 {
179 X509_add_ext(x509, ext, -1);
180 X509_EXTENSION_free(ext);
181 }
182
183 if (auto ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_ext_key_usage, "critical,serverAuth,clientAuth"))
184 {
185 X509_add_ext(x509, ext, -1);
186 X509_EXTENSION_free(ext);
187 }
188
189 if (auto ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_key_usage, "critical,digitalSignature"))
190 {
191 X509_add_ext(x509, ext, -1);
192 X509_EXTENSION_free(ext);
193 }
194
195 if (auto ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_subject_key_identifier, "hash"))
196 {
197 X509_add_ext(x509, ext, -1);
198 X509_EXTENSION_free(ext);
199 }
200 }
201
202 // And a private key
203 X509_set_pubkey(x509, defaultEphemeralPrivateKey);
204
205 if (!X509_sign(x509, defaultEphemeralPrivateKey, EVP_sha256()))
206 LogicError("X509_sign failed");
207
208 return x509;
209 }();
210
211 SSL_CTX* const ctx = context.native_handle();
212
213 if (SSL_CTX_use_certificate(ctx, defaultCert) <= 0)
214 LogicError("SSL_CTX_use_certificate failed");
215
216 if (SSL_CTX_use_PrivateKey(ctx, defaultEphemeralPrivateKey) <= 0)
217 LogicError("SSL_CTX_use_PrivateKey failed");
218}
219
220static void
222 boost::asio::ssl::context& context,
223 std::string const& key_file,
224 std::string const& cert_file,
225 std::string const& chain_file)
226{
227 auto fmt_error = [](boost::system::error_code ec) -> std::string {
228 return " [" + std::to_string(ec.value()) + ": " + ec.message() + "]";
229 };
230
231 SSL_CTX* const ssl = context.native_handle();
232
233 bool cert_set = false;
234
235 if (!cert_file.empty())
236 {
237 boost::system::error_code ec;
238
239 context.use_certificate_file(cert_file, boost::asio::ssl::context::pem, ec);
240
241 if (ec)
242 LogicError("Problem with SSL certificate file" + fmt_error(ec));
243
244 cert_set = true;
245 }
246
247 if (!chain_file.empty())
248 {
249 // VFALCO Replace fopen() with RAII
250 FILE* f = fopen(chain_file.c_str(), "r");
251
252 if (!f)
253 {
255 "Problem opening SSL chain file" +
256 fmt_error(boost::system::error_code(errno, boost::system::generic_category())));
257 }
258
259 try
260 {
261 for (;;)
262 {
263 X509* const x = PEM_read_X509(f, nullptr, nullptr, nullptr);
264
265 if (x == nullptr)
266 break;
267
268 if (!cert_set)
269 {
270 if (SSL_CTX_use_certificate(ssl, x) != 1)
272 "Problem retrieving SSL certificate from chain "
273 "file.");
274
275 cert_set = true;
276 }
277 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
278 {
279 X509_free(x);
280 LogicError("Problem adding SSL chain certificate.");
281 }
282 }
283
284 fclose(f);
285 }
286 catch (std::exception const& ex)
287 {
288 fclose(f);
289 LogicError(std::string("Reading the SSL chain file generated an exception: ") + ex.what());
290 }
291 }
292
293 if (!key_file.empty())
294 {
295 boost::system::error_code ec;
296
297 context.use_private_key_file(key_file, boost::asio::ssl::context::pem, ec);
298
299 if (ec)
300 {
301 LogicError("Problem using the SSL private key file" + fmt_error(ec));
302 }
303 }
304
305 if (SSL_CTX_check_private_key(ssl) != 1)
306 {
307 LogicError("Invalid key in SSL private key file.");
308 }
309}
310
313{
314 auto c = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
315
316 c->set_options(
317 boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 |
318 boost::asio::ssl::context::no_sslv3 | boost::asio::ssl::context::no_tlsv1 |
319 boost::asio::ssl::context::no_tlsv1_1 | boost::asio::ssl::context::single_dh_use |
320 boost::asio::ssl::context::no_compression);
321
322 if (cipherList.empty())
323 cipherList = defaultCipherList;
324
325 if (auto result = SSL_CTX_set_cipher_list(c->native_handle(), cipherList.c_str()); result != 1)
326 LogicError("SSL_CTX_set_cipher_list failed");
327
328 c->use_tmp_dh({std::addressof(detail::defaultDH), sizeof(defaultDH)});
329
330 // Disable all renegotiation support in TLS v1.2. This can help prevent
331 // exploitation of the bug described in CVE-2021-3499 (for details see
332 // https://www.openssl.org/news/secadv/20210325.txt) when linking
333 // against OpenSSL versions prior to 1.1.1k.
334 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);
335
336 return c;
337}
338
339} // namespace detail
340} // namespace openssl
341
342//------------------------------------------------------------------------------
344make_SSLContext(std::string const& cipherList)
345{
346 auto context = openssl::detail::get_context(cipherList);
348 // VFALCO NOTE, It seems the WebSocket context never has
349 // set_verify_mode called, for either setting of WEBSOCKET_SECURE
350 context->set_verify_mode(boost::asio::ssl::verify_none);
351 return context;
352}
353
356 std::string const& keyFile,
357 std::string const& certFile,
358 std::string const& chainFile,
359 std::string const& cipherList)
360{
361 auto context = openssl::detail::get_context(cipherList);
362 openssl::detail::initAuthenticated(*context, keyFile, certFile, chainFile);
363 return context;
364}
365
366} // 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:6
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)