90 using namespace openssl;
92 static auto defaultRSA = []() {
93 BIGNUM* bn = BN_new();
94 BN_set_word(bn, RSA_F4);
109 static auto defaultEphemeralPrivateKey = []() {
110 auto pkey = EVP_PKEY_new();
117 if (RSA_up_ref(defaultRSA) != 1)
118 LogicError(
"EVP_PKEY_assign_RSA: incrementing reference count failed");
120 if (!EVP_PKEY_assign_RSA(pkey, defaultRSA))
126 static auto defaultCert = []() {
127 auto x509 = X509_new();
135 X509_set_version(x509, 2);
141 auto const ts =
std::time(
nullptr) - (25 * 60 * 60);
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);
178 X509V3_EXT_conf_nid(
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(
185 nullptr, &ctx, NID_ext_key_usage,
"critical,serverAuth,clientAuth"))
187 X509_add_ext(x509, ext, -1);
188 X509_EXTENSION_free(ext);
192 X509V3_EXT_conf_nid(
nullptr, &ctx, NID_key_usage,
"critical,digitalSignature"))
194 X509_add_ext(x509, ext, -1);
195 X509_EXTENSION_free(ext);
198 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_subject_key_identifier,
"hash"))
200 X509_add_ext(x509, ext, -1);
201 X509_EXTENSION_free(ext);
206 X509_set_pubkey(x509, defaultEphemeralPrivateKey);
208 if (!X509_sign(x509, defaultEphemeralPrivateKey, EVP_sha256()))
214 SSL_CTX*
const ctx = context.native_handle();
216 if (SSL_CTX_use_certificate(ctx, defaultCert) <= 0)
219 if (SSL_CTX_use_PrivateKey(ctx, defaultEphemeralPrivateKey) <= 0)
225 boost::asio::ssl::context& context,
230 auto fmt_error = [](boost::system::error_code ec) ->
std::string {
231 return " [" +
std::to_string(ec.value()) +
": " + ec.message() +
"]";
234 SSL_CTX*
const ssl = context.native_handle();
236 bool cert_set =
false;
238 if (!cert_file.
empty())
240 boost::system::error_code ec;
242 context.use_certificate_file(cert_file, boost::asio::ssl::context::pem, ec);
245 LogicError(
"Problem with SSL certificate file" + fmt_error(ec));
250 if (!chain_file.
empty())
253 FILE* f = fopen(chain_file.
c_str(),
"r");
258 "Problem opening SSL chain file" +
259 fmt_error(boost::system::error_code(errno, boost::system::generic_category())));
266 X509*
const x = PEM_read_X509(f,
nullptr,
nullptr,
nullptr);
273 if (SSL_CTX_use_certificate(ssl, x) != 1)
275 "Problem retrieving SSL certificate from chain "
280 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
283 LogicError(
"Problem adding SSL chain certificate.");
293 std::string(
"Reading the SSL chain file generated an exception: ") + ex.
what());
297 if (!key_file.
empty())
299 boost::system::error_code ec;
301 context.use_private_key_file(key_file, boost::asio::ssl::context::pem, ec);
305 LogicError(
"Problem using the SSL private key file" + fmt_error(ec));
309 if (SSL_CTX_check_private_key(ssl) != 1)
311 LogicError(
"Invalid key in SSL private key file.");
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);
326 if (cipherList.
empty())
329 if (
auto result = SSL_CTX_set_cipher_list(c->native_handle(), cipherList.
c_str()); result != 1)
338 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);