1#define _POSIX_C_SOURCE 200809L
16#ifdef CWIST_TLS_MBEDTLS
28#include <openssl/asn1.h>
29#include <openssl/bio.h>
30#include <openssl/bytestring.h>
32#include <openssl/nid.h>
33#include <openssl/obj.h>
34#include <openssl/pem.h>
35#include <openssl/pkcs7.h>
36#include <openssl/x509.h>
38#define CWIST_AIA_MAX_RESPONSE_BYTES (1024u * 1024u)
39#define CWIST_AIA_MAX_CHAIN_DEPTH 8
40#define CWIST_AIA_CONNECT_TIMEOUT_MS 2000L
41#define CWIST_AIA_TIMEOUT_MS 5000L
57static bool cwist_url_has_http_scheme(
const char *url) {
74static size_t cwist_cert_fetch_write_cb(
void *ptr,
size_t size,
size_t nmemb,
void *
userdata) {
77 size_t total = size *
nmemb;
83 size_t need = buf->len + total;
84 if (
need > buf->cap) {
85 size_t new_cap = buf->cap ? buf->cap * 2 : 4096;
100 memcpy(buf->data + buf->len, ptr, total);
117static bool cwist_fetch_cert_url(
const char *url, cwist_cert_fetch_buffer *out) {
118 if (!cwist_url_has_http_scheme(url) || !out)
return false;
127 memset(out, 0,
sizeof(*out));
139#if defined(CURLOPT_PROTOCOLS_STR)
142#elif defined(CURLOPT_PROTOCOLS)
153 memset(out, 0,
sizeof(*out));
200 if (!data || len == 0 || len >
INT_MAX)
return NULL;
240 if (!data || len == 0)
return NULL;
266 if (!data || len == 0 || len >
INT_MAX)
return NULL;
365static bool cwist_ctx_contains_cert(
SSL_CTX *ssl_ctx,
X509 *cert) {
366 if (!ssl_ctx || !cert)
return false;
390static X509 *cwist_ctx_chain_tail(
SSL_CTX *ssl_ctx) {
391 if (!ssl_ctx)
return NULL;
408static bool cwist_x509_is_self_signed(
X509 *cert) {
409 if (!cert)
return true;
444 if (!url)
return NULL;
448 if (!cwist_url_has_http_scheme(url)) {
467static X509 *cwist_fetch_issuer_from_url(
const char *url,
X509 *
subject) {
469 if (!cwist_fetch_cert_url(url, &buf))
return NULL;
509 char *url = cwist_dup_aia_uri(
desc);
538 static _Atomic
long cached = -1;
541 const char *s =
getenv(
"CWIST_TLS_SESS_CACHE_SIZE");
551 return (
unsigned long)
v;
567#ifndef CWIST_TLS_MBEDTLS
569 if (!ssl_ctx)
return -1;
573 X509 *tail = cwist_ctx_chain_tail(ssl_ctx);
574 if (!tail || cwist_x509_is_self_signed(tail))
break;
576 X509 *
issuer = cwist_fetch_issuer_from_aia(tail);
579 if (cwist_x509_is_self_signed(
issuer)) {
584 if (cwist_ctx_contains_cert(ssl_ctx,
issuer)) {
void cwist_curl_global_acquire(void)
Acquire a reference on the global libcurl instance.
void cwist_curl_global_release(void)
Release a reference on the global libcurl instance.
mbedTLS TLS backend: error queue subset.
struct cwist_mtls_ctx SSL_CTX
int cwist_tls_autoload_intermediates(SSL_CTX *ssl_ctx)
Autoload missing intermediate CA certificates into an SSL_CTX chain.
#define CWIST_AIA_TIMEOUT_MS
#define CWIST_AIA_CONNECT_TIMEOUT_MS
struct cwist_cert_fetch_buffer cwist_cert_fetch_buffer
#define CWIST_AIA_MAX_RESPONSE_BYTES
#define CWIST_AIA_MAX_CHAIN_DEPTH
unsigned long cwist_tls_sess_cache_size(void)