Feersum
view release on metacpan or search on metacpan
feersum_tls.c.inc view on Meta::CPAN
static ptls_cipher_suite_t *feer_tls_cipher_suites[] = {
&ptls_openssl_aes256gcmsha384,
&ptls_openssl_aes128gcmsha256,
#if PTLS_OPENSSL_HAVE_CHACHA20_POLY1305
&ptls_openssl_chacha20poly1305sha256,
#endif
NULL
};
static ptls_context_t *
feer_tls_create_context(pTHX_ const char *cert_file, const char *key_file, int h2)
{
ptls_context_t *ctx;
FILE *fp;
int ret;
Newxz(ctx, 1, ptls_context_t);
ctx->random_bytes = ptls_openssl_random_bytes;
ctx->get_time = &ptls_get_time;
ctx->key_exchanges = feer_tls_key_exchanges;
ctx->cipher_suites = feer_tls_cipher_suites;
ctx->max_buffer_size = FEER_TLS_MAX_HANDSHAKE_BUF;
ret = ptls_load_certificates(ctx, cert_file);
if (ret != 0) {
warn("Feersum TLS: failed to load certificate from '%s' (error %d)\n",
cert_file, ret);
feer_tls_free_context(ctx);
return NULL;
}
fp = fopen(key_file, "r");
if (!fp) {
warn("Feersum TLS: failed to open key file '%s': %s\n",
key_file, strerror(errno));
goto cert_cleanup;
}
/* "" rather than NULL: a NULL passphrase makes OpenSSL prompt on the tty */
EVP_PKEY *pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void *)"");
fclose(fp);
if (!pkey) {
warn("Feersum TLS: failed to read private key from '%s' "
"(encrypted keys are not supported)\n", key_file);
goto cert_cleanup;
}
/* picotls's PEM reader loads a truncated certificate as garbage DER without
* complaint, and nothing else checks that the key matches the leaf */
if (ctx->certificates.count > 0) {
const unsigned char *der = ctx->certificates.list[0].base;
X509 *leaf = d2i_X509(NULL, &der, (long)ctx->certificates.list[0].len);
if (!leaf) {
warn("Feersum TLS: certificate in '%s' is not valid DER "
"(truncated or corrupt PEM?)\n", cert_file);
EVP_PKEY_free(pkey);
goto cert_cleanup;
}
if (X509_check_private_key(leaf, pkey) != 1) {
X509_free(leaf);
EVP_PKEY_free(pkey);
warn("Feersum TLS: private key '%s' does not match certificate "
"'%s'\n", key_file, cert_file);
goto cert_cleanup;
}
X509_free(leaf);
}
ptls_openssl_sign_certificate_t *sign_cert;
Newx(sign_cert, 1, ptls_openssl_sign_certificate_t);
if (ptls_openssl_init_sign_certificate(sign_cert, pkey) != 0) {
Safefree(sign_cert);
EVP_PKEY_free(pkey);
warn("Feersum TLS: incompatible private key type in '%s'\n", key_file);
goto cert_cleanup;
}
EVP_PKEY_free(pkey);
ctx->sign_certificate = &sign_cert->super;
if (h2)
ctx->on_client_hello = &on_client_hello;
else
ctx->on_client_hello = &on_client_hello_no_h2;
trace("TLS context created: cert=%s key=%s h2=%d\n", cert_file, key_file, h2);
return ctx;
cert_cleanup:
feer_tls_free_context(ctx);
return NULL;
}
static void
feer_tls_free_context(ptls_context_t *ctx)
{
if (!ctx) return;
if (ctx->certificates.list) {
size_t i;
for (i = 0; i < ctx->certificates.count; i++)
free(ctx->certificates.list[i].base);
free(ctx->certificates.list);
}
if (ctx->sign_certificate) {
ptls_openssl_sign_certificate_t *sign_cert =
(ptls_openssl_sign_certificate_t *)ctx->sign_certificate;
ptls_openssl_dispose_sign_certificate(sign_cert);
Safefree(sign_cert);
}
Safefree(ctx);
}
/* Live connections hold a ref, so set_tls rotation or listener slot reuse
* cannot free a context a ptls_t still points at. */
static struct feer_tls_ctx_ref *
feer_tls_ctx_ref_new(ptls_context_t *ctx)
{
struct feer_tls_ctx_ref *ref;
Newx(ref, 1, struct feer_tls_ctx_ref);
ref->ctx = ctx;
( run in 0.865 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )