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)
119 "EVP_PKEY_assign_RSA: incrementing reference count failed");
121 if (!EVP_PKEY_assign_RSA(pkey, defaultRSA))
127 static auto defaultCert = []() {
128 auto x509 = X509_new();
136 X509_set_version(x509, 2);
142 auto const ts =
std::time(
nullptr) - (25 * 60 * 60);
145 buf,
sizeof(buf) - 1,
"%y%m%d000000Z",
std::gmtime(&ts));
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);
179 if (
auto ext = X509V3_EXT_conf_nid(
180 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(
190 "critical,serverAuth,clientAuth"))
192 X509_add_ext(x509, ext, -1);
193 X509_EXTENSION_free(ext);
196 if (
auto ext = X509V3_EXT_conf_nid(
197 nullptr, &ctx, NID_key_usage,
"critical,digitalSignature"))
199 X509_add_ext(x509, ext, -1);
200 X509_EXTENSION_free(ext);
203 if (
auto ext = X509V3_EXT_conf_nid(
204 nullptr, &ctx, NID_subject_key_identifier,
"hash"))
206 X509_add_ext(x509, ext, -1);
207 X509_EXTENSION_free(ext);
212 X509_set_pubkey(x509, defaultEphemeralPrivateKey);
214 if (!X509_sign(x509, defaultEphemeralPrivateKey, EVP_sha256()))
220 SSL_CTX*
const ctx = context.native_handle();
222 if (SSL_CTX_use_certificate(ctx, defaultCert) <= 0)
225 if (SSL_CTX_use_PrivateKey(ctx, defaultEphemeralPrivateKey) <= 0)
231 boost::asio::ssl::context& context,
236 auto fmt_error = [](boost::system::error_code ec) ->
std::string {
237 return " [" +
std::to_string(ec.value()) +
": " + ec.message() +
"]";
240 SSL_CTX*
const ssl = context.native_handle();
242 bool cert_set =
false;
244 if (!cert_file.
empty())
246 boost::system::error_code ec;
248 context.use_certificate_file(
249 cert_file, boost::asio::ssl::context::pem, ec);
252 LogicError(
"Problem with SSL certificate file" + fmt_error(ec));
257 if (!chain_file.
empty())
260 FILE* f = fopen(chain_file.
c_str(),
"r");
265 "Problem opening SSL chain file" +
266 fmt_error(boost::system::error_code(
267 errno, boost::system::generic_category())));
274 X509*
const x = PEM_read_X509(f,
nullptr,
nullptr,
nullptr);
281 if (SSL_CTX_use_certificate(ssl, x) != 1)
283 "Problem retrieving SSL certificate from chain "
288 else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1)
291 LogicError(
"Problem adding SSL chain certificate.");
302 "Reading the SSL chain file generated an exception: ") +
307 if (!key_file.
empty())
309 boost::system::error_code ec;
311 context.use_private_key_file(
312 key_file, boost::asio::ssl::context::pem, ec);
317 "Problem using the SSL private key file" + fmt_error(ec));
321 if (SSL_CTX_check_private_key(ssl) != 1)
323 LogicError(
"Invalid key in SSL private key file.");
331 boost::asio::ssl::context::sslv23);
334 boost::asio::ssl::context::default_workarounds |
335 boost::asio::ssl::context::no_sslv2 |
336 boost::asio::ssl::context::no_sslv3 |
337 boost::asio::ssl::context::no_tlsv1 |
338 boost::asio::ssl::context::no_tlsv1_1 |
339 boost::asio::ssl::context::single_dh_use |
340 boost::asio::ssl::context::no_compression);
342 if (cipherList.
empty())
346 SSL_CTX_set_cipher_list(c->native_handle(), cipherList.
c_str());
356 SSL_CTX_set_options(c->native_handle(), SSL_OP_NO_RENEGOTIATION);