ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
tls.h File Reference

Go to the source code of this file.

Data Structures

struct  quic_tls_t

Typedefs

typedef struct http_server http_server_t

Functions

asciichat_error_t quic_tls_server_create (const char *alpn, const char *certificate_file, const char *private_key_file, quic_tls_t **out)
asciichat_error_t quic_tls_client_create (const char *alpn, const char *ca_file, quic_tls_t **out)
asciichat_error_t quic_tls_client_configure (quic_tls_t *tls, SSL *ssl, const char *server_name)
asciichat_error_t quic_tls_session_create (quic_tls_t *tls, const char *server_name, ngtcp2_crypto_conn_ref *connection, SSL **out)
asciichat_error_t quic_tls_verify_ready (quic_tls_t *tls, SSL *ssl)
void quic_tls_session_destroy (SSL **ssl)
void quic_tls_destroy (quic_tls_t **tls)

Typedef Documentation

◆ http_server_t

typedef struct http_server http_server_t

Definition at line 8 of file tls.h.

Function Documentation

◆ quic_tls_client_configure()

asciichat_error_t quic_tls_client_configure ( quic_tls_t * tls,
SSL * ssl,
const char * server_name )

Configure certificate identity verification, SNI and ALPN on a client SSL.

Definition at line 106 of file tls.c.

106 {
107 if (!tls || tls->server || !ssl || SSL_get_SSL_CTX(ssl) != tls->ssl_ctx || !server_name || !*server_name)
108 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC client requires the expected server identity");
109 ERR_clear_error();
110 X509_VERIFY_PARAM *verify = SSL_get0_param(ssl);
111 X509_VERIFY_PARAM_set_hostflags(verify, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
112 if (SSL_set_alpn_protos(ssl, tls->alpn, tls->alpn_length) != 0 ||
113 (is_valid_ip(server_name) ? X509_VERIFY_PARAM_set1_ip_asc(verify, server_name) != 1 : (SSL_set_tlsext_host_name(ssl, server_name) != 1 || SSL_set1_host(ssl, server_name) != 1)))
114 return tls_error("Cannot configure QUIC server identity");
115 return ASCIICHAT_OK;
116}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
int is_valid_ip(const char *ip)
Check if string is a valid IP address (IPv4 or IPv6).
Definition ip.c:472
SSL_CTX * ssl_ctx
Definition tls.h:12
unsigned char alpn[256]
Definition tls.h:13
bool server
Definition tls.h:15
unsigned int alpn_length
Definition tls.h:14

References quic_tls_t::alpn, quic_tls_t::alpn_length, ASCIICHAT_OK, ERROR_INVALID_PARAM, is_valid_ip(), quic_tls_t::server, SET_ERRNO, and quic_tls_t::ssl_ctx.

Referenced by quic_tls_session_create().

◆ quic_tls_client_create()

asciichat_error_t quic_tls_client_create ( const char * alpn,
const char * ca_file,
quic_tls_t ** out )

Create a TLS 1.3 client context. NULL ca_file selects the system trust store.

Definition at line 85 of file tls.c.

85 {
86 if (!out)
87 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC client trust configuration");
88 *out = NULL;
89 if (ca_file && !*ca_file)
90 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC client trust configuration");
91 quic_tls_t *tls = NULL;
92 asciichat_error_t result = create_context(false, alpn, &tls);
93 if (result != ASCIICHAT_OK)
94 return result;
95 ERR_clear_error();
96 int loaded = ca_file ? SSL_CTX_load_verify_locations(tls->ssl_ctx, ca_file, NULL) : SSL_CTX_set_default_verify_paths(tls->ssl_ctx);
97 if (loaded != 1) {
98 result = tls_error("Cannot load QUIC client trust roots");
99 quic_tls_destroy(&tls);
100 return result;
101 }
102 *out = tls;
103 return ASCIICHAT_OK;
104}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
static bool size_t gotel_query_t * out
Definition queries.h:10
void quic_tls_destroy(quic_tls_t **tls)
Definition tls.c:190

References ASCIICHAT_OK, ERROR_INVALID_PARAM, out, quic_tls_destroy(), SET_ERRNO, and quic_tls_t::ssl_ctx.

Referenced by quic_endpoint_open().

◆ quic_tls_destroy()

void quic_tls_destroy ( quic_tls_t ** tls)

Definition at line 190 of file tls.c.

190 {
191 if (tls && *tls) {
192 SSL_CTX_free((*tls)->ssl_ctx);
193 SAFE_FREE(*tls);
194 }
195}
#define SAFE_FREE(ptr)

References SAFE_FREE.

Referenced by quic_endpoint_destroy(), quic_endpoint_open(), quic_tls_client_create(), and quic_tls_server_create().

◆ quic_tls_server_create()

asciichat_error_t quic_tls_server_create ( const char * alpn,
const char * certificate_file,
const char * private_key_file,
quic_tls_t ** out )

Create a TLS 1.3 server context. Certificate and key are copied by OpenSSL.

Definition at line 64 of file tls.c.

64 {
65 if (!out)
66 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC server requires a certificate and private key");
67 *out = NULL;
68 if (!certificate_file || !*certificate_file || !private_key_file || !*private_key_file)
69 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC server requires a certificate and private key");
70 quic_tls_t *tls = NULL;
71 asciichat_error_t result = create_context(true, alpn, &tls);
72 if (result != ASCIICHAT_OK)
73 return result;
74 ERR_clear_error();
75 if (SSL_CTX_use_certificate_chain_file(tls->ssl_ctx, certificate_file) != 1 || SSL_CTX_use_PrivateKey_file(tls->ssl_ctx, private_key_file, SSL_FILETYPE_PEM) != 1 ||
76 SSL_CTX_check_private_key(tls->ssl_ctx) != 1) {
77 result = tls_error("Cannot load QUIC server certificate or private key");
78 quic_tls_destroy(&tls);
79 return result;
80 }
81 *out = tls;
82 return ASCIICHAT_OK;
83}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, out, quic_tls_destroy(), SET_ERRNO, and quic_tls_t::ssl_ctx.

Referenced by quic_endpoint_open().

◆ quic_tls_session_create()

asciichat_error_t quic_tls_session_create ( quic_tls_t * tls,
const char * server_name,
ngtcp2_crypto_conn_ref * connection,
SSL ** out )

The connection reference is borrowed and must remain valid while SSL uses it. The caller owns the SSL and must clear its app data before freeing it if the ngtcp2 connection or reference can have been destroyed first.

Definition at line 118 of file tls.c.

118 {
119 if (!out)
120 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC TLS session arguments");
121 *out = NULL;
122 if (!tls || !tls->ssl_ctx || !connection || !connection->get_conn || (tls->server && server_name))
123 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC TLS session arguments");
124 ERR_clear_error();
125 SSL *ssl = SSL_new(tls->ssl_ctx);
126 if (!ssl)
127 return tls_error("Cannot create QUIC TLS session");
128 if (SSL_set_app_data(ssl, connection) != 1) {
129 asciichat_error_t result = tls_error("Cannot attach QUIC connection to TLS session");
130 SSL_free(ssl);
131 return result;
132 }
134 if (tls->server) {
135 SSL_set_accept_state(ssl);
136 if (ngtcp2_crypto_ossl_configure_server_session(ssl) != 0)
137 result = tls_error("Cannot configure server QUIC TLS callbacks");
138 } else {
139 SSL_set_connect_state(ssl);
140 result = quic_tls_client_configure(tls, ssl, server_name);
141 if (result == ASCIICHAT_OK && ngtcp2_crypto_ossl_configure_client_session(ssl) != 0)
142 result = tls_error("Cannot configure client QUIC TLS callbacks");
143 }
144 if (result != ASCIICHAT_OK) {
146 return result;
147 }
148 *out = ssl;
149 return ASCIICHAT_OK;
150}
void quic_tls_session_destroy(SSL **ssl)
Definition tls.c:182
asciichat_error_t quic_tls_client_configure(quic_tls_t *tls, SSL *ssl, const char *server_name)
Definition tls.c:106

References ASCIICHAT_OK, ERROR_INVALID_PARAM, out, quic_tls_client_configure(), quic_tls_session_destroy(), quic_tls_t::server, SET_ERRNO, and quic_tls_t::ssl_ctx.

◆ quic_tls_session_destroy()

void quic_tls_session_destroy ( SSL ** ssl)

Definition at line 182 of file tls.c.

182 {
183 if (ssl && *ssl) {
184 SSL_set_app_data(*ssl, NULL);
185 SSL_free(*ssl);
186 *ssl = NULL;
187 }
188}

Referenced by quic_connection_destroy(), and quic_tls_session_create().

◆ quic_tls_verify_ready()

asciichat_error_t quic_tls_verify_ready ( quic_tls_t * tls,
SSL * ssl )

Call after the TLS handshake and before exposing the connection as ready. Requires the configured ALPN; a client also requires a verified peer cert.

Definition at line 152 of file tls.c.

152 {
153 if (!tls || !ssl || SSL_get_SSL_CTX(ssl) != tls->ssl_ctx || !SSL_is_init_finished(ssl))
154 return SET_ERRNO(ERROR_INVALID_STATE, "QUIC TLS handshake is not complete");
155 const unsigned char *selected = NULL;
156 unsigned int length = 0;
157 SSL_get0_alpn_selected(ssl, &selected, &length);
158 bool negotiated = false;
159 for (size_t offset = 0; offset < tls->alpn_length;) {
160 size_t protocol_length = tls->alpn[offset++];
161 if (protocol_length > tls->alpn_length - offset)
162 break;
163 if (selected && protocol_length == (size_t)length && !memcmp(selected, tls->alpn + offset, protocol_length))
164 negotiated = true;
165 offset += protocol_length;
166 }
167 if (!negotiated)
168 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "QUIC TLS ALPN was not negotiated");
169 if (!tls->server) {
170 X509_VERIFY_PARAM *verify = SSL_get0_param(ssl);
171 const char *host = X509_VERIFY_PARAM_get0_host(verify, 0);
172 char *ip = X509_VERIFY_PARAM_get1_ip_asc(verify);
173 X509 *peer = SSL_get0_peer_certificate(ssl);
174 bool identity_matches = peer && (host || ip) && (!host || X509_check_host(peer, host, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL) == 1) && (!ip || X509_check_ip_asc(peer, ip, 0) == 1);
175 OPENSSL_free(ip);
176 if (!identity_matches || !(SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) || SSL_get_verify_result(ssl) != X509_V_OK)
177 return SET_ERRNO(ERROR_CRYPTO, "QUIC TLS peer certificate or identity was not verified");
178 }
179 return ASCIICHAT_OK;
180}
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_CRYPTO

References quic_tls_t::alpn, quic_tls_t::alpn_length, ASCIICHAT_OK, ERROR_CRYPTO, ERROR_INVALID_STATE, ERROR_NETWORK_PROTOCOL, quic_tls_t::server, SET_ERRNO, and quic_tls_t::ssl_ctx.