20 #include <ripple/basics/chrono.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/make_SSLContext.h>
23 #include <ripple/beast/container/aged_unordered_set.h>
38 "HIGH:MEDIUM:!aNULL:!MD5:!DSS:!3DES:!RC4:!EXPORT";
48 void operator() (RSA* rsa)
const
59 void operator() (EVP_PKEY* evp_pkey)
const
61 EVP_PKEY_free (evp_pkey);
70 void operator() (X509* x509)
const
81 void operator() (DH* dh)
const
96 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
97 BIGNUM *bn = BN_new();
98 BN_set_word(bn, RSA_F4);
100 RSA* rsa = RSA_new();
101 if (RSA_generate_key_ex(rsa, n_bits, bn,
nullptr) != 1)
109 RSA* rsa = RSA_generate_key (n_bits, RSA_F4,
nullptr,
nullptr);
124 EVP_PKEY* evp_pkey = EVP_PKEY_new();
126 if (evp_pkey ==
nullptr)
134 if (! EVP_PKEY_assign_RSA (evp_pkey, rsa.
get()))
146 X509* x509 = X509_new();
151 X509_set_version (x509, NID_X509);
153 int const margin = 60 * 60;
154 int const length = 10 * 365.25 * 24 * 60 * 60;
156 X509_gmtime_adj (X509_get_notBefore (x509), -margin);
157 X509_gmtime_adj (X509_get_notAfter (x509), length);
164 X509_set_pubkey (x509, evp_pkey);
169 if (! X509_sign (x509, evp_pkey, EVP_sha1()))
175 if (SSL_CTX_use_certificate (ctx, cert.
get()) <= 0)
176 LogicError (
"SSL_CTX_use_certificate failed");
181 if (SSL_CTX_use_PrivateKey (ctx, key.
get()) <= 0)
185 #ifdef SSL_FLAGS_NO_RENEGOTIATE_CIPHERS
188 disallowRenegotiation (SSL
const* ssl,
bool isNew)
204 static StaticData sd;
209 for (
auto iter (sd.set.chronological.begin ());
210 (iter != sd.set.chronological.end ()) && (iter.when () <= expired);
211 iter = sd.set.chronological.begin ())
216 auto iter = sd.set.find (ssl);
217 if (iter != sd.set.end ())
229 sd.set.emplace (ssl);
237 info_handler (SSL
const* ssl,
int event,
int)
239 #if OPENSSL_VERSION_NUMBER < 0x10100000L
240 if ((ssl->s3) && (event & SSL_CB_HANDSHAKE_START))
242 if (disallowRenegotiation (ssl, SSL_in_before (ssl)))
243 ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
254 boost::system::error_code
const& ec)
260 " (" << ec.value() <<
")";
267 boost::asio::ssl::context& context)
269 using namespace openssl;
278 SSL_CTX*
const ctx = context.native_handle();
286 boost::asio::ssl::context& context,
291 SSL_CTX*
const ssl = context.native_handle ();
293 bool cert_set =
false;
295 if (! cert_file.
empty ())
297 boost::system::error_code ec;
299 context.use_certificate_file (
300 cert_file, boost::asio::ssl::context::pem, ec);
305 "Problem with SSL certificate file.", ec).c_str());
311 if (! chain_file.
empty ())
314 FILE* f = fopen (chain_file.
c_str (),
"r");
319 "Problem opening SSL chain file.", boost::system::error_code (errno,
320 boost::system::generic_category())).c_str());
327 X509*
const x = PEM_read_X509 (f,
nullptr,
nullptr,
nullptr);
334 if (SSL_CTX_use_certificate (ssl, x) != 1)
335 LogicError (
"Problem retrieving SSL certificate from chain file.");
339 else if (SSL_CTX_add_extra_chain_cert (ssl, x) != 1)
342 LogicError (
"Problem adding SSL chain certificate.");
351 LogicError (
"Reading the SSL chain file generated an exception.");
355 if (! key_file.
empty ())
357 boost::system::error_code ec;
359 context.use_private_key_file (key_file,
360 boost::asio::ssl::context::pem, ec);
365 "Problem using the SSL private key file.", ec).c_str());
369 if (SSL_CTX_check_private_key (ssl) != 1)
371 LogicError (
"Invalid key in SSL private key file.");
378 auto c = std::make_shared<boost::asio::ssl::context> (
379 boost::asio::ssl::context::sslv23);
382 boost::asio::ssl::context::default_workarounds |
383 boost::asio::ssl::context::no_sslv2 |
384 boost::asio::ssl::context::no_sslv3 |
385 boost::asio::ssl::context::single_dh_use);
389 auto result = SSL_CTX_set_cipher_list (
390 c->native_handle (), l.c_str());
392 LogicError (
"SSL_CTX_set_cipher_list failed");
399 unsigned char const params[] =
401 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0x8f, 0xca, 0x66,
402 0x85, 0x33, 0xcb, 0xcf, 0x36, 0x27, 0xb2, 0x4c, 0xb8, 0x50, 0xb8, 0xf9,
403 0x53, 0xf8, 0xb9, 0x2d, 0x1c, 0xa2, 0xad, 0x86, 0x58, 0x29, 0x3b, 0x88,
404 0x3e, 0xf5, 0x65, 0xb8, 0xda, 0x22, 0xf4, 0x8b, 0x21, 0x12, 0x18, 0xf7,
405 0x16, 0xcd, 0x7c, 0xc7, 0x3a, 0x2d, 0x61, 0xb7, 0x11, 0xf6, 0xb0, 0x65,
406 0xa0, 0x5b, 0xa4, 0x06, 0x95, 0x28, 0xa4, 0x4f, 0x76, 0xc0, 0xeb, 0xfa,
407 0x95, 0xdf, 0xbf, 0x19, 0x90, 0x64, 0x8f, 0x60, 0xd5, 0x36, 0xba, 0xab,
408 0x0d, 0x5a, 0x5c, 0x94, 0xd5, 0xf7, 0x32, 0xd6, 0x2a, 0x76, 0x77, 0x83,
409 0x10, 0xc4, 0x2f, 0x10, 0x96, 0x3e, 0x37, 0x84, 0x45, 0x9c, 0xef, 0x33,
410 0xf6, 0xd0, 0x2a, 0xa7, 0xce, 0x0a, 0xce, 0x0d, 0xa1, 0xa7, 0x44, 0x5d,
411 0x18, 0x3f, 0x4f, 0xa4, 0x23, 0x9c, 0x5d, 0x74, 0x4f, 0xee, 0xdf, 0xaa,
412 0x0d, 0x0a, 0x52, 0x57, 0x73, 0xb1, 0xe4, 0xc5, 0x72, 0x93, 0x9d, 0x03,
413 0xe9, 0xf5, 0x48, 0x8c, 0xd1, 0xe6, 0x7c, 0x21, 0x65, 0x4e, 0x16, 0x51,
414 0xa3, 0x16, 0x51, 0x10, 0x75, 0x60, 0x37, 0x93, 0xb8, 0x15, 0xd6, 0x14,
415 0x41, 0x4a, 0x61, 0xc9, 0x1a, 0x4e, 0x9f, 0x38, 0xd8, 0x2c, 0xa5, 0x31,
416 0xe1, 0x87, 0xda, 0x1f, 0xa4, 0x31, 0xa2, 0xa4, 0x42, 0x1e, 0xe0, 0x30,
417 0xea, 0x2f, 0x9b, 0x77, 0x91, 0x59, 0x3e, 0xd5, 0xd0, 0xc5, 0x84, 0x45,
418 0x17, 0x19, 0x74, 0x8b, 0x18, 0xb0, 0xc1, 0xe0, 0xfc, 0x1c, 0xaf, 0xe6,
419 0x2a, 0xef, 0x4e, 0x0e, 0x8a, 0x5c, 0xc2, 0x91, 0xb9, 0x2b, 0xf8, 0x17,
420 0x8d, 0xed, 0x44, 0xaa, 0x47, 0xaa, 0x52, 0xa2, 0xdb, 0xb6, 0xf5, 0xa1,
421 0x88, 0x85, 0xa1, 0xd5, 0x87, 0xb8, 0x07, 0xd3, 0x97, 0xbe, 0x37, 0x74,
422 0x72, 0xf1, 0xa8, 0x29, 0xf1, 0xa7, 0x7d, 0x19, 0xc3, 0x27, 0x09, 0xcf,
423 0x23, 0x02, 0x01, 0x02
426 unsigned char const *data = ¶ms[0];
430 LogicError (
"d2i_DHparams returned nullptr.");
432 SSL_CTX_set_tmp_dh (c->native_handle (), dh.get());
434 #ifdef SSL_FLAGS_NO_RENEGOTIATE_CIPHERS
435 SSL_CTX_set_info_callback (c->native_handle (), info_handler);
452 context->set_verify_mode(boost::asio::ssl::verify_none);
465 keyFile, certFile, chainFile);