20 #include <ripple/basics/contract.h>
21 #include <ripple/basics/make_SSLContext.h>
60 "-----BEGIN DH PARAMETERS-----\n"
61 "MIIBCAKCAQEApKSWfR7LKy0VoZ/SDCObCvJ5HKX2J93RJ+QN8kJwHh+uuA8G+t8Q\n"
62 "MDRjL5HanlV/sKN9HXqBc7eqHmmbqYwIXKUt9MUZTLNheguddxVlc2IjdP5i9Ps8\n"
63 "l7su8tnP0l1JvC6Rfv3epRsEAw/ZW/lC2IwkQPpOmvnENQhQ6TgrUzcGkv4Bn0X6\n"
64 "pxrDSBpZ+45oehGCUAtcbY8b02vu8zPFoxqo6V/+MIszGzldlik5bVqrJpVF6E8C\n"
65 "tRqHjj6KuDbPbjc+pRGvwx/BSO3SULxmYu9J1NOk090MU1CMt6IJY7TpEc9Xrac9\n"
66 "9yqY3xXZID240RRcaJ25+U4lszFPqP+CEwIBAg==\n"
67 "-----END DH PARAMETERS-----";
88 using namespace openssl;
90 static auto defaultRSA = []() {
91 BIGNUM* bn = BN_new();
92 BN_set_word(bn, RSA_F4);
107 static auto defaultEphemeralPrivateKey = []() {
108 auto pkey = EVP_PKEY_new();
115 if (RSA_up_ref(defaultRSA) != 1)
117 "EVP_PKEY_assign_RSA: incrementing reference count failed");
119 if (!EVP_PKEY_assign_RSA(pkey, defaultRSA))
125 static auto defaultCert = []() {
126 auto x509 = X509_new();
134 X509_set_version(x509, 2);
140 auto const ts =
std::time(
nullptr) - (25 * 60 * 60);
143 buf,
sizeof(buf) - 1,
"%y%m%d000000Z",
std::gmtime(&ts));
147 if (ASN1_TIME_set_string_X509(X509_get_notBefore(x509), buf) != 1)
148 LogicError(
"Unable to set certificate validity date");
151 X509_gmtime_adj(X509_get_notAfter(x509), 2 * 365 * 24 * 60 * 60);
154 if (
auto b = BN_new(); b !=
nullptr)
156 if (BN_rand(b, 128, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
158 if (
auto a = ASN1_INTEGER_new(); a !=
nullptr)
160 if (BN_to_ASN1_INTEGER(b, a))
161 X509_set_serialNumber(x509, a);
163 ASN1_INTEGER_free(a);
174 X509V3_set_ctx_nodb(&ctx);
175 X509V3_set_ctx(&ctx, x509, x509,
nullptr,
nullptr, 0);
177 if (
auto ext = X509V3_EXT_conf_nid(
178 nullptr, &ctx, NID_basic_constraints,
"critical,CA:FALSE"))
180 X509_add_ext(x509, ext, -1);
181 X509_EXTENSION_free(ext);
184 if (
auto ext = X509V3_EXT_conf_nid(
188 "critical,serverAuth,clientAuth"))
190 X509_add_ext(x509, ext, -1);
191 X509_EXTENSION_free(ext);
194 if (
auto ext = X509V3_EXT_conf_nid(
195 nullptr, &ctx, NID_key_usage,
"critical,digitalSignature"))
197 X509_add_ext(x509, ext, -1);
198 X509_EXTENSION_free(ext);
201 if (
auto ext = X509V3_EXT_conf_nid(
202 nullptr, &ctx, NID_subject_key_identifier,
"hash"))
204 X509_add_ext(x509, ext, -1);
205 X509_EXTENSION_free(ext);
210 X509_set_pubkey(x509, defaultEphemeralPrivateKey);
212 if (!X509_sign(x509, defaultEphemeralPrivateKey, EVP_sha256()))
218 SSL_CTX*
const ctx = context.native_handle();
220 if (SSL_CTX_use_certificate(ctx, defaultCert) <= 0)
223 if (SSL_CTX_use_PrivateKey(ctx, defaultEphemeralPrivateKey) <= 0)
229 boost::asio::ssl::context& context,
234 auto fmt_error = [](boost::system::error_code ec) ->
std::string {
235 return " [" +
std::to_string(ec.value()) +
": " + ec.message() +
"]";
238 SSL_CTX*
const ssl = context.native_handle();
240 bool cert_set =
false;
242 if (!cert_file.
empty())
244 boost::system::error_code ec;
246 context.use_certificate_file(
247 cert_file, boost::asio::ssl::context::pem, ec);
250 LogicError(
"Problem with SSL certificate file" + fmt_error(ec));
255 if (!chain_file.
empty())
258 FILE* f = fopen(chain_file.
c_str(),
"r");
263 "Problem opening SSL chain file" +
264 fmt_error(boost::system::error_code(
265 errno, boost::system::generic_category())));
272 X509*
const x = PEM_read_X509(f,
nullptr,
nullptr,
nullptr);
279 if (SSL_CTX_use_certificate(ssl, x) != 1)
281 "Problem retrieving SSL certificate from chain "
286 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
289 LogicError(
"Problem adding SSL chain certificate.");
300 "Reading the SSL chain file generated an exception: ") +
305 if (!key_file.
empty())
307 boost::system::error_code ec;
309 context.use_private_key_file(
310 key_file, boost::asio::ssl::context::pem, ec);
315 "Problem using the SSL private key file" + fmt_error(ec));
319 if (SSL_CTX_check_private_key(ssl) != 1)
321 LogicError(
"Invalid key in SSL private key file.");
328 auto c = std::make_shared<boost::asio::ssl::context>(
329 boost::asio::ssl::context::sslv23);
332 boost::asio::ssl::context::default_workarounds |
333 boost::asio::ssl::context::no_sslv2 |
334 boost::asio::ssl::context::no_sslv3 |
335 boost::asio::ssl::context::no_tlsv1 |
336 boost::asio::ssl::context::no_tlsv1_1 |
337 boost::asio::ssl::context::single_dh_use |
338 boost::asio::ssl::context::no_compression);
340 if (cipherList.
empty())
344 SSL_CTX_set_cipher_list(c->native_handle(), cipherList.
c_str());
354 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);
370 context->set_verify_mode(boost::asio::ssl::verify_none);