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);
177 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_basic_constraints,
"critical,CA:FALSE"))
179 X509_add_ext(x509, ext, -1);
180 X509_EXTENSION_free(ext);
183 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_ext_key_usage,
"critical,serverAuth,clientAuth"))
185 X509_add_ext(x509, ext, -1);
186 X509_EXTENSION_free(ext);
189 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_key_usage,
"critical,digitalSignature"))
191 X509_add_ext(x509, ext, -1);
192 X509_EXTENSION_free(ext);
195 if (
auto ext = X509V3_EXT_conf_nid(
nullptr, &ctx, NID_subject_key_identifier,
"hash"))
197 X509_add_ext(x509, ext, -1);
198 X509_EXTENSION_free(ext);
203 X509_set_pubkey(x509, defaultEphemeralPrivateKey);
205 if (!X509_sign(x509, defaultEphemeralPrivateKey, EVP_sha256()))
211 SSL_CTX*
const ctx = context.native_handle();
213 if (SSL_CTX_use_certificate(ctx, defaultCert) <= 0)
216 if (SSL_CTX_use_PrivateKey(ctx, defaultEphemeralPrivateKey) <= 0)
222 boost::asio::ssl::context& context,
227 auto fmt_error = [](boost::system::error_code ec) ->
std::string {
228 return " [" +
std::to_string(ec.value()) +
": " + ec.message() +
"]";
231 SSL_CTX*
const ssl = context.native_handle();
233 bool cert_set =
false;
235 if (!cert_file.
empty())
237 boost::system::error_code ec;
239 context.use_certificate_file(cert_file, boost::asio::ssl::context::pem, ec);
242 LogicError(
"Problem with SSL certificate file" + fmt_error(ec));
247 if (!chain_file.
empty())
250 FILE* f = fopen(chain_file.
c_str(),
"r");
255 "Problem opening SSL chain file" +
256 fmt_error(boost::system::error_code(errno, boost::system::generic_category())));
263 X509*
const x = PEM_read_X509(f,
nullptr,
nullptr,
nullptr);
270 if (SSL_CTX_use_certificate(ssl, x) != 1)
272 "Problem retrieving SSL certificate from chain "
277 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
280 LogicError(
"Problem adding SSL chain certificate.");
293 if (!key_file.
empty())
295 boost::system::error_code ec;
297 context.use_private_key_file(key_file, boost::asio::ssl::context::pem, ec);
301 LogicError(
"Problem using the SSL private key file" + fmt_error(ec));
305 if (SSL_CTX_check_private_key(ssl) != 1)
307 LogicError(
"Invalid key in SSL private key file.");
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);
322 if (cipherList.
empty())
325 if (
auto result = SSL_CTX_set_cipher_list(c->native_handle(), cipherList.
c_str()); result != 1)
334 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);