94 static auto kDefaultRsa = []() {
95 BIGNUM* bn = BN_new();
96 BN_set_word(bn, RSA_F4);
111 static auto kDefaultEphemeralPrivateKey = []() {
112 auto pkey = EVP_PKEY_new();
119 if (RSA_up_ref(kDefaultRsa) != 1)
120 logicError(
"EVP_PKEY_assign_RSA: incrementing reference count failed");
122 if (!EVP_PKEY_assign_RSA(pkey, kDefaultRsa))
128 static auto kDefaultCert = []() {
129 auto x509 = X509_new();
137 X509_set_version(x509, 2);
143 auto const ts =
std::time(
nullptr) - (25 * 60 * 60);
149 if (ASN1_TIME_set_string_X509(X509_get_notBefore(x509), buf) != 1)
150 logicError(
"Unable to set certificate validity date");
153 X509_gmtime_adj(X509_get_notAfter(x509), 2 * 365 * 24 * 60 * 60);
156 if (
auto b = BN_new(); b !=
nullptr)
158 if (BN_rand(b, 128, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
160 if (
auto a = ASN1_INTEGER_new(); a !=
nullptr)
162 if (BN_to_ASN1_INTEGER(b, a))
163 X509_set_serialNumber(x509, a);
165 ASN1_INTEGER_free(a);
176 X509V3_set_ctx_nodb(&ctx);
177 X509V3_set_ctx(&ctx, x509, x509,
nullptr,
nullptr, 0);
180 X509V3_EXT_conf_nid(
nullptr, &ctx, NID_basic_constraints,
"critical,CA:FALSE"))
182 X509_add_ext(x509, ext, -1);
183 X509_EXTENSION_free(ext);
186 if (
auto ext = X509V3_EXT_conf_nid(
187 nullptr, &ctx, NID_ext_key_usage,
"critical,serverAuth,clientAuth"))
189 X509_add_ext(x509, ext, -1);
190 X509_EXTENSION_free(ext);
194 X509V3_EXT_conf_nid(
nullptr, &ctx, NID_key_usage,
"critical,digitalSignature"))
196 X509_add_ext(x509, ext, -1);
197 X509_EXTENSION_free(ext);
200 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_subject_key_identifier,
"hash"))
202 X509_add_ext(x509, ext, -1);
203 X509_EXTENSION_free(ext);
208 X509_set_pubkey(x509, kDefaultEphemeralPrivateKey);
210 if (!X509_sign(x509, kDefaultEphemeralPrivateKey, EVP_sha256()))
216 SSL_CTX*
const ctx = context.native_handle();
218 if (SSL_CTX_use_certificate(ctx, kDefaultCert) <= 0)
221 if (SSL_CTX_use_PrivateKey(ctx, kDefaultEphemeralPrivateKey) <= 0)
227 boost::asio::ssl::context& context,
232 auto fmtError = [](boost::system::error_code ec) ->
std::string {
233 return " [" +
std::to_string(ec.value()) +
": " + ec.message() +
"]";
236 SSL_CTX*
const ssl = context.native_handle();
238 bool certSet =
false;
240 if (!certFile.
empty())
242 boost::system::error_code ec;
245 context.use_certificate_file(certFile, boost::asio::ssl::context::pem, ec);
248 logicError(
"Problem with SSL certificate file" + fmtError(ec));
253 if (!chainFile.
empty())
256 FILE* f = fopen(chainFile.
c_str(),
"r");
261 "Problem opening SSL chain file" +
262 fmtError(boost::system::error_code(errno, boost::system::generic_category())));
269 X509*
const x = PEM_read_X509(f,
nullptr,
nullptr,
nullptr);
276 if (SSL_CTX_use_certificate(ssl, x) != 1)
279 "Problem retrieving SSL certificate from chain "
285 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
288 logicError(
"Problem adding SSL chain certificate.");
298 std::string(
"Reading the SSL chain file generated an exception: ") + ex.
what());
302 if (!keyFile.
empty())
304 boost::system::error_code ec;
307 context.use_private_key_file(keyFile, boost::asio::ssl::context::pem, ec);
311 logicError(
"Problem using the SSL private key file" + fmtError(ec));
315 if (SSL_CTX_check_private_key(ssl) != 1)
317 logicError(
"Invalid key in SSL private key file.");
327 boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 |
328 boost::asio::ssl::context::no_sslv3 | boost::asio::ssl::context::no_tlsv1 |
329 boost::asio::ssl::context::no_tlsv1_1 | boost::asio::ssl::context::single_dh_use |
330 boost::asio::ssl::context::no_compression);
332 if (cipherList.
empty())
335 if (
auto result = SSL_CTX_set_cipher_list(c->native_handle(), cipherList.
c_str()); result != 1)
344 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);