Skip to content

Commit d832cce

Browse files
committed
MINOR: quic: Allow the use of the new OpenSSL 3.5.0 QUIC TLS API (to be completed)
This patch allows the use of the new OpenSSL 3.5.0 QUIC TLS API when it is available and detected at compilation time. The detection relies on the presence of the OSSL_FUNC_SSL_QUIC_TLS_CRYPTO_SEND macro from openssl-compat.h. Indeed this macro is defined by OpenSSL since 3.5.0 version. It is not defined by quictls. This helps in distinguishing these two TLS stacks. When the detection succeeds, HAVE_OPENSSL_QUIC is also defined by openssl-compat.h. Then, this is this new macro which is used to detect the availability of the new OpenSSL 3.5.0 QUIC TLS API. Note that this detection is done only if USE_QUIC_OPENSSL_COMPAT is not asked. So, USE_QUIC_OPENSSL_COMPAT and HAVE_OPENSSL_QUIC are exclusive. At the same location, from openssl-compat.h, ssl_encryption_level_t enum is defined. This enum was defined by quictls and expansively used by the haproxy QUIC implementation. SSL_set_quic_transport_params() is replaced by SSL_set_quic_tls_transport_params. SSL_set_quic_early_data_enabled() (quictls) is also replaced by SSL_set_quic_tls_early_data_enabled() (OpenSSL). SSL_quic_read_level() (quictls) is not defined by OpenSSL. It is only used by the traces to log the current TLS stack decryption level (read). A macro makes it return -1 which is an usused values. The most of the differences between quictls and OpenSSL QUI APIs are in quic_ssl.c where some callbacks must be defined for these two APIs. This is why this patch modifies quic_ssl.c to define an array of OSSL_DISPATCH structs: <ha_quic_dispatch>. Each element of this arry defines a callback. So, this patch implements these six callabcks: - ha_quic_ossl_crypto_send() - ha_quic_ossl_crypto_recv_rcd() - ha_quic_ossl_crypto_release_rcd() - ha_quic_ossl_yield_secret() - ha_quic_ossl_got_transport_params() and - ha_quic_ossl_alert(). But at this time, these implementations which must return an int return 0 interpreted as a failure by the OpenSSL QUIC API, except for ha_quic_ossl_alert() which is implemented the same was as for quictls. The five remaining functions above will be implemented by the next patches to come. ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() have been moved to be defined for both quictls and OpenSSL QUIC API. These callbacks are attached to the SSL objects (sessions) calling qc_ssl_set_cbs() new function. This latter callback the correct function to attached the correct callbacks to the SSL objects (defined by <ha_quic_method> for quictls, and <ha_quic_dispatch> for OpenSSL). The calls to SSL_provide_quic_data() and SSL_process_quic_post_handshake() have been also disabled. These functions are not defined by OpenSSL QUIC API. At this time, the functions which call them are still defined when HAVE_OPENSSL_QUIC is defined.
1 parent bc8e454 commit d832cce

File tree

4 files changed

+169
-23
lines changed

4 files changed

+169
-23
lines changed

include/haproxy/openssl-compat.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,25 @@
4646

4747
#ifdef USE_QUIC_OPENSSL_COMPAT
4848
#include <haproxy/quic_openssl_compat.h>
49+
#else
50+
#if defined(OSSL_FUNC_SSL_QUIC_TLS_CRYPTO_SEND)
51+
/* This macro is defined by the new OpenSSL 3.5.0 QUIC TLS API and it is not
52+
* defined by quictls.
53+
*/
54+
#define HAVE_OPENSSL_QUIC
55+
#define SSL_set_quic_transport_params SSL_set_quic_tls_transport_params
56+
#define SSL_set_quic_early_data_enabled SSL_set_quic_tls_early_data_enabled
57+
#define SSL_quic_read_level(arg) -1
58+
59+
enum ssl_encryption_level_t {
60+
ssl_encryption_initial = 0,
61+
ssl_encryption_early_data,
62+
ssl_encryption_handshake,
63+
ssl_encryption_application
64+
};
65+
4966
#endif
67+
#endif /* USE_QUIC_OPENSSL_COMPAT */
5068

5169
#if defined(OPENSSL_IS_AWSLC)
5270
#define OPENSSL_NO_DH

include/haproxy/quic_ssl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
int ssl_quic_initial_ctx(struct bind_conf *bind_conf);
3737
int qc_alloc_ssl_sock_ctx(struct quic_conn *qc);
3838
int qc_ssl_provide_all_quic_data(struct quic_conn *qc, struct ssl_sock_ctx *ctx);
39+
int quic_ssl_set_tls_cbs(SSL *ssl);
3940

4041
static inline void qc_free_ssl_sock_ctx(struct ssl_sock_ctx **ctx)
4142
{

include/haproxy/quic_trace-t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,6 @@ struct quic_rx_crypto_frm {
9999
#define QUIC_EV_CONN_KP (1ULL << 50)
100100
#define QUIC_EV_CONN_SSL_COMPAT (1ULL << 51)
101101
#define QUIC_EV_CONN_BIND_TID (1ULL << 52)
102+
#define QUIC_EV_CONN_RELEASE_RCD (1ULL << 53)
102103

103104
#endif /* _HAPROXY_QUIC_TRACE_T_H */

src/quic_ssl.c

Lines changed: 149 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -325,27 +325,6 @@ static int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t
325325
return ret;
326326
}
327327

328-
#if defined(OPENSSL_IS_AWSLC)
329-
/* compatibility function for split read/write encryption secrets to be used
330-
* with the API which uses 2 callbacks. */
331-
static inline int ha_quic_set_read_secret(SSL *ssl, enum ssl_encryption_level_t level,
332-
const SSL_CIPHER *cipher, const uint8_t *secret,
333-
size_t secret_len)
334-
{
335-
return ha_quic_set_encryption_secrets(ssl, level, secret, NULL, secret_len);
336-
337-
}
338-
339-
static inline int ha_quic_set_write_secret(SSL *ssl, enum ssl_encryption_level_t level,
340-
const SSL_CIPHER *cipher, const uint8_t *secret,
341-
size_t secret_len)
342-
{
343-
344-
return ha_quic_set_encryption_secrets(ssl, level, NULL, secret, secret_len);
345-
346-
}
347-
#endif
348-
349328
/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
350329
* wants to provide the QUIC layer with CRYPTO data.
351330
* Returns 1 if succeeded, 0 if not.
@@ -389,6 +368,137 @@ static int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t leve
389368
return ret;
390369
}
391370

371+
#ifdef HAVE_OPENSSL_QUIC
372+
/************************** OpenSSL QUIC TLS API (>= 3.5.0) *******************/
373+
374+
static int ha_quic_ossl_crypto_send(SSL *ssl,
375+
const unsigned char *buf, size_t buf_len,
376+
size_t *consumed, void *arg)
377+
{
378+
int ret = 0;
379+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
380+
381+
TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
382+
TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
383+
384+
return ret;
385+
}
386+
387+
static int ha_quic_ossl_crypto_recv_rcd(SSL *ssl,
388+
const unsigned char **buf, size_t *bytes_read,
389+
void *arg)
390+
{
391+
int ret = 0;
392+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
393+
394+
TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
395+
TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
396+
397+
return ret;
398+
}
399+
400+
static int ha_quic_ossl_crypto_release_rcd(SSL *ssl, size_t bytes_read, void *arg)
401+
{
402+
int ret = 0;
403+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
404+
405+
TRACE_ENTER(QUIC_EV_CONN_RELEASE_RCD, qc);
406+
TRACE_LEAVE(QUIC_EV_CONN_RELEASE_RCD, qc);
407+
408+
return ret;
409+
}
410+
411+
static int ha_quic_ossl_yield_secret(SSL *ssl, uint32_t prot_level, int direction,
412+
const unsigned char *secret, size_t secret_len,
413+
void *arg)
414+
{
415+
int ret = 0;
416+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
417+
418+
TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
419+
TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
420+
421+
return ret;
422+
}
423+
424+
static int ha_quic_ossl_got_transport_params(SSL *ssl, const unsigned char *params,
425+
size_t params_len, void *arg)
426+
{
427+
int ret = 0;
428+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
429+
430+
TRACE_ENTER(QUIC_EV_TRANSP_PARAMS, qc);
431+
TRACE_LEAVE(QUIC_EV_TRANSP_PARAMS, qc);
432+
433+
return ret;
434+
}
435+
436+
static int ha_quic_ossl_alert(SSL *ssl, unsigned char alert_code, void *arg)
437+
{
438+
int ret = 1, alert = alert_code;
439+
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
440+
441+
TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
442+
443+
TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert);
444+
quic_set_tls_alert(qc, alert_code);
445+
446+
TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
447+
448+
return ret;
449+
}
450+
451+
static const OSSL_DISPATCH ha_quic_dispatch[] = {
452+
{
453+
OSSL_FUNC_SSL_QUIC_TLS_CRYPTO_SEND,
454+
(OSSL_FUNC)ha_quic_ossl_crypto_send,
455+
},
456+
{
457+
OSSL_FUNC_SSL_QUIC_TLS_CRYPTO_RECV_RCD,
458+
(OSSL_FUNC)ha_quic_ossl_crypto_recv_rcd,
459+
},
460+
{
461+
OSSL_FUNC_SSL_QUIC_TLS_CRYPTO_RELEASE_RCD,
462+
(OSSL_FUNC)ha_quic_ossl_crypto_release_rcd,
463+
},
464+
{
465+
OSSL_FUNC_SSL_QUIC_TLS_YIELD_SECRET,
466+
(OSSL_FUNC)ha_quic_ossl_yield_secret,
467+
},
468+
{
469+
OSSL_FUNC_SSL_QUIC_TLS_GOT_TRANSPORT_PARAMS,
470+
(OSSL_FUNC)ha_quic_ossl_got_transport_params,
471+
},
472+
{
473+
OSSL_FUNC_SSL_QUIC_TLS_ALERT,
474+
(OSSL_FUNC)ha_quic_ossl_alert,
475+
},
476+
OSSL_DISPATCH_END,
477+
};
478+
#else /* !HAVE_OPENSSL_QUIC */
479+
/***************************** QUICTLS QUIC API ******************************/
480+
481+
#if defined(OPENSSL_IS_AWSLC)
482+
/* compatibility function for split read/write encryption secrets to be used
483+
* with the API which uses 2 callbacks. */
484+
static inline int ha_quic_set_read_secret(SSL *ssl, enum ssl_encryption_level_t level,
485+
const SSL_CIPHER *cipher, const uint8_t *secret,
486+
size_t secret_len)
487+
{
488+
return ha_quic_set_encryption_secrets(ssl, level, secret, NULL, secret_len);
489+
490+
}
491+
492+
static inline int ha_quic_set_write_secret(SSL *ssl, enum ssl_encryption_level_t level,
493+
const SSL_CIPHER *cipher, const uint8_t *secret,
494+
size_t secret_len)
495+
{
496+
497+
return ha_quic_set_encryption_secrets(ssl, level, NULL, secret, secret_len);
498+
499+
}
500+
#endif
501+
392502
static int ha_quic_flush_flight(SSL *ssl)
393503
{
394504
struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
@@ -432,6 +542,7 @@ static SSL_QUIC_METHOD ha_quic_method = {
432542
.send_alert = ha_quic_send_alert,
433543
};
434544
#endif
545+
#endif /* HAVE_OPENSSL_QUIC */
435546

436547
/* Initialize the TLS context of a listener with <bind_conf> as configuration.
437548
* Returns an error count.
@@ -533,11 +644,13 @@ static int qc_ssl_provide_quic_data(struct ncbuf *ncbuf,
533644

534645
TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
535646

647+
#ifndef HAVE_OPENSSL_QUIC
536648
if (SSL_provide_quic_data(ctx->ssl, level, data, len) != 1) {
537649
TRACE_ERROR("SSL_provide_quic_data() error",
538650
QUIC_EV_CONN_SSLDATA, qc, NULL, NULL, ctx->ssl);
539651
goto leave;
540652
}
653+
#endif
541654

542655
state = qc->state;
543656
if (state < QUIC_HS_ST_COMPLETE) {
@@ -645,7 +758,9 @@ static int qc_ssl_provide_quic_data(struct ncbuf *ncbuf,
645758
TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
646759
goto leave;
647760
}
648-
} else {
761+
}
762+
#ifndef HAVE_OPENSSL_QUIC
763+
else {
649764
ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
650765
if (ssl_err != 1) {
651766
ssl_err = SSL_get_error(ctx->ssl, ssl_err);
@@ -662,6 +777,7 @@ static int qc_ssl_provide_quic_data(struct ncbuf *ncbuf,
662777

663778
TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
664779
}
780+
#endif
665781

666782
out:
667783
ret = 1;
@@ -733,6 +849,16 @@ int qc_ssl_provide_all_quic_data(struct quic_conn *qc, struct ssl_sock_ctx *ctx)
733849
return ret;
734850
}
735851

852+
/* Simple helper to set the specifig OpenSSL/quictls QUIC API callbacks */
853+
int quic_ssl_set_tls_cbs(SSL *ssl)
854+
{
855+
#ifdef HAVE_OPENSSL_QUIC
856+
return SSL_set_quic_tls_cbs(ssl, ha_quic_dispatch, NULL);
857+
#else
858+
return SSL_set_quic_method(ssl, &ha_quic_method);
859+
#endif
860+
}
861+
736862
/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
737863
* with <ssl_ctx> as SSL context inherited settings. Also set the transport
738864
* parameters of this session.
@@ -759,7 +885,7 @@ static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl)
759885
}
760886

761887
if (!SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
762-
!SSL_set_quic_method(*ssl, &ha_quic_method)) {
888+
!quic_ssl_set_tls_cbs(*ssl)) {
763889
SSL_free(*ssl);
764890
*ssl = NULL;
765891
if (!retry--)

0 commit comments

Comments
 (0)