#include "internal/quic_port.h"
#include "internal/quic_channel.h"
#include "internal/quic_lcidm.h"
#include "internal/quic_srtm.h"
#include "internal/quic_txp.h"
#include "internal/ssl_unwrap.h"
#include "quic_port_local.h"
#include "quic_channel_local.h"
#include "quic_engine_local.h"
#include "quic_local.h"
#include "../ssl_local.h"
#include <openssl/rand.h>
#define INIT_DCID_LEN 8
static int port_init(QUIC_PORT *port);
static void port_cleanup(QUIC_PORT *port);
static OSSL_TIME get_time(void *arg);
static void port_default_packet_handler(QUIC_URXE *e, void *arg,
const QUIC_CONN_ID *dcid);
static void port_rx_pre(QUIC_PORT *port);
typedef struct validation_token {
OSSL_TIME timestamp;
QUIC_CONN_ID odcid;
QUIC_CONN_ID rscid;
size_t remote_addr_len;
unsigned char *remote_addr;
unsigned char is_retry;
} QUIC_VALIDATION_TOKEN;
#define MARSHALLED_TOKEN_MAX_LEN 169
#define ENCRYPTED_TOKEN_MAX_LEN (MARSHALLED_TOKEN_MAX_LEN + 16 + 12)
DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL);
DEFINE_LIST_OF_IMPL(incoming_ch, QUIC_CHANNEL);
DEFINE_LIST_OF_IMPL(port, QUIC_PORT);
QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args)
{
QUIC_PORT *port;
if ((port = OPENSSL_zalloc(sizeof(QUIC_PORT))) == NULL)
return NULL;
port->engine = args->engine;
port->channel_ctx = args->channel_ctx;
port->is_multi_conn = args->is_multi_conn;
port->validate_addr = args->do_addr_validation;
port->get_conn_user_ssl = args->get_conn_user_ssl;
port->user_ssl_arg = args->user_ssl_arg;
if (!port_init(port)) {
OPENSSL_free(port);
return NULL;
}
return port;
}
void ossl_quic_port_free(QUIC_PORT *port)
{
if (port == NULL)
return;
port_cleanup(port);
OPENSSL_free(port);
}
static int port_init(QUIC_PORT *port)
{
size_t rx_short_dcid_len = (port->is_multi_conn ? INIT_DCID_LEN : 0);
int key_len;
EVP_CIPHER *cipher = NULL;
unsigned char *token_key = NULL;
int ret = 0;
if (port->engine == NULL || port->channel_ctx == NULL)
goto err;
if ((port->err_state = OSSL_ERR_STATE_new()) == NULL)
goto err;
if ((port->demux = ossl_quic_demux_new(NULL,
rx_short_dcid_len,
get_time, port))
== NULL)
goto err;
ossl_quic_demux_set_default_handler(port->demux,
port_default_packet_handler,
port);
if ((port->srtm = ossl_quic_srtm_new(port->engine->libctx,
port->engine->propq))
== NULL)
goto err;
if ((port->lcidm = ossl_quic_lcidm_new(port->engine->libctx,
rx_short_dcid_len))
== NULL)
goto err;
port->rx_short_dcid_len = (unsigned char)rx_short_dcid_len;
port->tx_init_dcid_len = INIT_DCID_LEN;
port->state = QUIC_PORT_STATE_RUNNING;
ossl_list_port_insert_tail(&port->engine->port_list, port);
port->on_engine_list = 1;
port->bio_changed = 1;
if ((port->token_ctx = EVP_CIPHER_CTX_new()) == NULL
|| (cipher = EVP_CIPHER_fetch(port->engine->libctx,
"AES-256-GCM", NULL))
== NULL
|| !EVP_EncryptInit_ex(port->token_ctx, cipher, NULL, NULL, NULL)
|| (key_len = EVP_CIPHER_CTX_get_key_length(port->token_ctx)) <= 0
|| (token_key = OPENSSL_malloc(key_len)) == NULL
|| !RAND_bytes_ex(port->engine->libctx, token_key, key_len, 0)
|| !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, token_key, NULL))
goto err;
ret = 1;
err:
EVP_CIPHER_free(cipher);
OPENSSL_free(token_key);
if (!ret)
port_cleanup(port);
return ret;
}
static void port_cleanup(QUIC_PORT *port)
{
assert(ossl_list_ch_num(&port->channel_list) == 0);
ossl_quic_demux_free(port->demux);
port->demux = NULL;
ossl_quic_srtm_free(port->srtm);
port->srtm = NULL;
ossl_quic_lcidm_free(port->lcidm);
port->lcidm = NULL;
OSSL_ERR_STATE_free(port->err_state);
port->err_state = NULL;
if (port->on_engine_list) {
ossl_list_port_remove(&port->engine->port_list, port);
port->on_engine_list = 0;
}
EVP_CIPHER_CTX_free(port->token_ctx);
port->token_ctx = NULL;
}
static void port_transition_failed(QUIC_PORT *port)
{
if (port->state == QUIC_PORT_STATE_FAILED)
return;
port->state = QUIC_PORT_STATE_FAILED;
}
int ossl_quic_port_is_running(const QUIC_PORT *port)
{
return port->state == QUIC_PORT_STATE_RUNNING;
}
QUIC_ENGINE *ossl_quic_port_get0_engine(QUIC_PORT *port)
{
return port->engine;
}
QUIC_REACTOR *ossl_quic_port_get0_reactor(QUIC_PORT *port)
{
return ossl_quic_engine_get0_reactor(port->engine);
}
QUIC_DEMUX *ossl_quic_port_get0_demux(QUIC_PORT *port)
{
return port->demux;
}
CRYPTO_MUTEX *ossl_quic_port_get0_mutex(QUIC_PORT *port)
{
return ossl_quic_engine_get0_mutex(port->engine);
}
OSSL_TIME ossl_quic_port_get_time(QUIC_PORT *port)
{
return ossl_quic_engine_get_time(port->engine);
}
static OSSL_TIME get_time(void *port)
{
return ossl_quic_port_get_time((QUIC_PORT *)port);
}
int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT *port)
{
return port->rx_short_dcid_len;
}
int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT *port)
{
return port->tx_init_dcid_len;
}
size_t ossl_quic_port_get_num_incoming_channels(const QUIC_PORT *port)
{
return ossl_list_incoming_ch_num(&port->incoming_channel_list);
}
static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d)
{
if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
return 1;
}
BIO *ossl_quic_port_get_net_rbio(QUIC_PORT *port)
{
return port->net_rbio;
}
BIO *ossl_quic_port_get_net_wbio(QUIC_PORT *port)
{
return port->net_wbio;
}
static int port_update_poll_desc(QUIC_PORT *port, BIO *net_bio, int for_write)
{
BIO_POLL_DESCRIPTOR d = { 0 };
if (net_bio == NULL
|| (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d))
|| (for_write && !BIO_get_wpoll_descriptor(net_bio, &d)))
d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
if (!validate_poll_descriptor(&d))
return 0;
if (for_write)
ossl_quic_reactor_set_poll_w(&port->engine->rtor, &d);
else
ossl_quic_reactor_set_poll_r(&port->engine->rtor, &d);
return 1;
}
int ossl_quic_port_update_poll_descriptors(QUIC_PORT *port, int force)
{
int ok = 1;
if (!force && !port->bio_changed)
return 0;
if (!port_update_poll_desc(port, port->net_rbio, 0))
ok = 0;
if (!port_update_poll_desc(port, port->net_wbio, 1))
ok = 0;
port->bio_changed = 0;
return ok;
}
static void port_update_addressing_mode(QUIC_PORT *port)
{
long rcaps = 0, wcaps = 0;
if (port->net_rbio != NULL)
rcaps = BIO_dgram_get_effective_caps(port->net_rbio);
if (port->net_wbio != NULL)
wcaps = BIO_dgram_get_effective_caps(port->net_wbio);
port->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);
port->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);
port->bio_changed = 1;
}
int ossl_quic_port_is_addressed_r(const QUIC_PORT *port)
{
return port->addressed_mode_r;
}
int ossl_quic_port_is_addressed_w(const QUIC_PORT *port)
{
return port->addressed_mode_w;
}
int ossl_quic_port_is_addressed(const QUIC_PORT *port)
{
return ossl_quic_port_is_addressed_r(port) && ossl_quic_port_is_addressed_w(port);
}
int ossl_quic_port_set_net_rbio(QUIC_PORT *port, BIO *net_rbio)
{
if (port->net_rbio == net_rbio)
return 1;
if (!port_update_poll_desc(port, net_rbio, 0))
return 0;
ossl_quic_demux_set_bio(port->demux, net_rbio);
port->net_rbio = net_rbio;
port_update_addressing_mode(port);
return 1;
}
int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio)
{
QUIC_CHANNEL *ch;
if (port->net_wbio == net_wbio)
return 1;
if (!port_update_poll_desc(port, net_wbio, 1))
return 0;
OSSL_LIST_FOREACH(ch, ch, &port->channel_list)
ossl_qtx_set_bio(ch->qtx, net_wbio);
port->net_wbio = net_wbio;
port_update_addressing_mode(port);
return 1;
}
SSL_CTX *ossl_quic_port_get_channel_ctx(QUIC_PORT *port)
{
return port->channel_ctx;
}
static SSL *port_new_handshake_layer(QUIC_PORT *port, QUIC_CHANNEL *ch)
{
SSL *tls = NULL;
SSL_CONNECTION *tls_conn = NULL;
SSL *user_ssl = NULL;
QUIC_CONNECTION *qc = NULL;
QUIC_LISTENER *ql = NULL;
if (!ossl_assert(port->get_conn_user_ssl != NULL))
return NULL;
user_ssl = port->get_conn_user_ssl(ch, port->user_ssl_arg);
if (user_ssl == NULL)
return NULL;
qc = (QUIC_CONNECTION *)user_ssl;
ql = (QUIC_LISTENER *)port->user_ssl_arg;
if (!ossl_assert(qc->tls == NULL)) {
SSL_free(user_ssl);
return NULL;
}
tls = ossl_ssl_connection_new_int(port->channel_ctx, user_ssl, TLS_method());
qc->tls = tls;
if (tls == NULL || (tls_conn = SSL_CONNECTION_FROM_SSL(tls)) == NULL) {
SSL_free(user_ssl);
return NULL;
}
if (ql != NULL && ql->obj.ssl.ctx->new_pending_conn_cb != NULL)
if (!ql->obj.ssl.ctx->new_pending_conn_cb(ql->obj.ssl.ctx, user_ssl,
ql->obj.ssl.ctx->new_pending_conn_arg)) {
SSL_free(user_ssl);
return NULL;
}
tls_conn->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
tls_conn->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
tls_conn->pha_enabled = 0;
return tls;
}
static QUIC_CHANNEL *port_make_channel(QUIC_PORT *port, SSL *tls, OSSL_QRX *qrx,
int is_server, int is_tserver)
{
QUIC_CHANNEL_ARGS args = { 0 };
QUIC_CHANNEL *ch;
args.port = port;
args.is_server = is_server;
args.lcidm = port->lcidm;
args.srtm = port->srtm;
args.qrx = qrx;
args.is_tserver_ch = is_tserver;
ch = ossl_quic_channel_alloc(&args);
if (ch == NULL)
return NULL;
ch->tls = (tls != NULL) ? tls : port_new_handshake_layer(port, ch);
if (ch->tls == NULL) {
OPENSSL_free(ch);
return NULL;
}
#ifndef OPENSSL_NO_QLOG
ch->use_qlog = 1;
if (ch->tls->ctx->qlog_title != NULL) {
if ((ch->qlog_title = OPENSSL_strdup(ch->tls->ctx->qlog_title)) == NULL) {
OPENSSL_free(ch);
return NULL;
}
}
#endif
if (!ossl_quic_channel_init(ch)) {
OPENSSL_free(ch);
return NULL;
}
ossl_qtx_set_bio(ch->qtx, port->net_wbio);
return ch;
}
QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls)
{
return port_make_channel(port, tls, NULL, 0,
0);
}
QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls)
{
QUIC_CHANNEL *ch;
assert(port->tserver_ch == NULL);
ch = port_make_channel(port, tls, NULL, 1,
1);
port->tserver_ch = ch;
port->allow_incoming = 1;
return ch;
}
QUIC_CHANNEL *ossl_quic_port_pop_incoming(QUIC_PORT *port)
{
QUIC_CHANNEL *ch;
ch = ossl_list_incoming_ch_head(&port->incoming_channel_list);
if (ch == NULL)
return NULL;
ossl_list_incoming_ch_remove(&port->incoming_channel_list, ch);
return ch;
}
int ossl_quic_port_have_incoming(QUIC_PORT *port)
{
return ossl_list_incoming_ch_head(&port->incoming_channel_list) != NULL;
}
void ossl_quic_port_drop_incoming(QUIC_PORT *port)
{
QUIC_CHANNEL *ch;
SSL *tls;
SSL *user_ssl;
SSL_CONNECTION *sc;
for (;;) {
ch = ossl_quic_port_pop_incoming(port);
if (ch == NULL)
break;
tls = ossl_quic_channel_get0_tls(ch);
sc = SSL_CONNECTION_FROM_SSL(tls);
if (sc == NULL)
break;
user_ssl = SSL_CONNECTION_GET_USER_SSL(sc);
if (user_ssl == tls) {
ossl_quic_channel_free(ch);
SSL_free(tls);
} else {
SSL_free(user_ssl);
}
}
}
void ossl_quic_port_set_allow_incoming(QUIC_PORT *port, int allow_incoming)
{
port->allow_incoming = allow_incoming;
}
void ossl_quic_port_subtick(QUIC_PORT *port, QUIC_TICK_RESULT *res,
uint32_t flags)
{
QUIC_CHANNEL *ch;
res->net_read_desired = ossl_quic_port_is_running(port);
res->net_write_desired = 0;
res->notify_other_threads = 0;
res->tick_deadline = ossl_time_infinite();
if (!port->engine->inhibit_tick) {
if (ossl_quic_port_is_running(port))
port_rx_pre(port);
OSSL_LIST_FOREACH(ch, ch, &port->channel_list)
{
QUIC_TICK_RESULT subr = { 0 };
ossl_quic_channel_subtick(ch, &subr, flags);
ossl_quic_tick_result_merge_into(res, &subr);
}
}
}
static void port_rx_pre(QUIC_PORT *port)
{
int ret;
if (!port->allow_incoming && !port->have_sent_any_pkt)
return;
ret = ossl_quic_demux_pump(port->demux);
if (ret == QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL)
ossl_quic_port_raise_net_error(port, NULL);
}
static void port_bind_channel(QUIC_PORT *port, const BIO_ADDR *peer,
const QUIC_CONN_ID *scid, const QUIC_CONN_ID *dcid,
const QUIC_CONN_ID *odcid, OSSL_QRX *qrx,
QUIC_CHANNEL **new_ch)
{
QUIC_CHANNEL *ch;
if (port->tserver_ch != NULL) {
ch = port->tserver_ch;
port->tserver_ch = NULL;
ossl_quic_channel_bind_qrx(ch, qrx);
ossl_qrx_set_msg_callback(ch->qrx, ch->msg_callback,
ch->msg_callback_ssl);
ossl_qrx_set_msg_callback_arg(ch->qrx, ch->msg_callback_arg);
} else {
ch = port_make_channel(port, NULL, qrx, 1,
0);
}
if (ch == NULL)
return;
if (qrx == NULL)
if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,
ch->port->engine->propq,
dcid, 1,
ch->qrx, NULL))
return;
if (odcid->id_len != 0) {
ossl_quic_tx_packetiser_set_validated(ch->txp);
if (!ossl_quic_bind_channel(ch, peer, scid, dcid, odcid)) {
ossl_quic_channel_free(ch);
return;
}
} else {
if (!ossl_quic_channel_on_new_conn(ch, peer, scid, dcid)) {
ossl_quic_channel_free(ch);
return;
}
}
ossl_list_incoming_ch_insert_tail(&port->incoming_channel_list, ch);
*new_ch = ch;
}
static int port_try_handle_stateless_reset(QUIC_PORT *port, const QUIC_URXE *e)
{
size_t i;
const unsigned char *data = ossl_quic_urxe_data(e);
void *opaque = NULL;
if (e->data_len < QUIC_STATELESS_RESET_TOKEN_LEN + 5
|| (0100 & *data) != 0100)
return 0;
for (i = 0;; ++i) {
if (!ossl_quic_srtm_lookup(port->srtm,
(QUIC_STATELESS_RESET_TOKEN *)(data + e->data_len
- sizeof(QUIC_STATELESS_RESET_TOKEN)),
i, &opaque, NULL))
break;
assert(opaque != NULL);
ossl_quic_channel_on_stateless_reset((QUIC_CHANNEL *)opaque);
}
return i > 0;
}
static void cleanup_validation_token(QUIC_VALIDATION_TOKEN *token)
{
OPENSSL_free(token->remote_addr);
}
static int generate_token(BIO_ADDR *peer, QUIC_CONN_ID odcid,
QUIC_CONN_ID rscid, QUIC_VALIDATION_TOKEN *token,
int is_retry)
{
token->is_retry = is_retry;
token->timestamp = ossl_time_now();
token->remote_addr = NULL;
token->odcid = odcid;
token->rscid = rscid;
if (!BIO_ADDR_rawaddress(peer, NULL, &token->remote_addr_len)
|| token->remote_addr_len == 0
|| (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL
|| !BIO_ADDR_rawaddress(peer, token->remote_addr,
&token->remote_addr_len)) {
cleanup_validation_token(token);
return 0;
}
return 1;
}
static int marshal_validation_token(QUIC_VALIDATION_TOKEN *token,
unsigned char *buffer, size_t *buffer_len)
{
WPACKET wpkt = { 0 };
BUF_MEM *buf_mem = BUF_MEM_new();
if (buffer == NULL || buf_mem == NULL
|| (token->is_retry != 0 && token->is_retry != 1)) {
BUF_MEM_free(buf_mem);
return 0;
}
if (!WPACKET_init(&wpkt, buf_mem)
|| !WPACKET_memset(&wpkt, token->is_retry, 1)
|| !WPACKET_memcpy(&wpkt, &token->timestamp,
sizeof(token->timestamp))
|| (token->is_retry
&& (!WPACKET_sub_memcpy_u8(&wpkt, &token->odcid.id,
token->odcid.id_len)
|| !WPACKET_sub_memcpy_u8(&wpkt, &token->rscid.id,
token->rscid.id_len)))
|| !WPACKET_sub_memcpy_u8(&wpkt, token->remote_addr, token->remote_addr_len)
|| !WPACKET_get_total_written(&wpkt, buffer_len)
|| *buffer_len > MARSHALLED_TOKEN_MAX_LEN
|| !WPACKET_finish(&wpkt)) {
WPACKET_cleanup(&wpkt);
BUF_MEM_free(buf_mem);
return 0;
}
memcpy(buffer, buf_mem->data, *buffer_len);
BUF_MEM_free(buf_mem);
return 1;
}
static int encrypt_validation_token(const QUIC_PORT *port,
const unsigned char *plaintext,
size_t pt_len,
unsigned char *ciphertext,
size_t *ct_len)
{
int iv_len, len, ret = 0;
size_t tag_len;
unsigned char *iv = ciphertext, *data, *tag;
if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) == 0
|| (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0)
goto err;
*ct_len = iv_len + pt_len + tag_len + QUIC_RETRY_INTEGRITY_TAG_LEN;
if (ciphertext == NULL) {
ret = 1;
goto err;
}
data = ciphertext + iv_len;
tag = data + pt_len;
if (!RAND_bytes_ex(port->engine->libctx, ciphertext, iv_len, 0)
|| !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv)
|| !EVP_EncryptUpdate(port->token_ctx, data, &len, plaintext, pt_len)
|| !EVP_EncryptFinal_ex(port->token_ctx, data + pt_len, &len)
|| !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_GET_TAG, tag_len, tag))
goto err;
ret = 1;
err:
return ret;
}
static int decrypt_validation_token(const QUIC_PORT *port,
const unsigned char *ciphertext,
size_t ct_len,
unsigned char *plaintext,
size_t *pt_len)
{
int iv_len, len = 0, ret = 0;
size_t tag_len;
const unsigned char *iv = ciphertext, *data, *tag;
if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) == 0
|| (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0)
goto err;
if (ct_len < (iv_len + tag_len) || ct_len > ENCRYPTED_TOKEN_MAX_LEN)
goto err;
*pt_len = ct_len - iv_len - tag_len;
if (plaintext == NULL) {
ret = 1;
goto err;
}
data = ciphertext + iv_len;
tag = ciphertext + ct_len - tag_len;
if (!EVP_DecryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv)
|| !EVP_DecryptUpdate(port->token_ctx, plaintext, &len, data,
ct_len - iv_len - tag_len)
|| !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_SET_TAG, tag_len,
(void *)tag)
|| !EVP_DecryptFinal_ex(port->token_ctx, plaintext + len, &len))
goto err;
ret = 1;
err:
return ret;
}
static int parse_validation_token(QUIC_VALIDATION_TOKEN *token,
const unsigned char *buf, size_t buf_len)
{
PACKET pkt, subpkt;
if (buf == NULL || token == NULL)
return 0;
token->remote_addr = NULL;
if (!PACKET_buf_init(&pkt, buf, buf_len)
|| !PACKET_copy_bytes(&pkt, &token->is_retry, sizeof(token->is_retry))
|| !(token->is_retry == 0 || token->is_retry == 1)
|| !PACKET_copy_bytes(&pkt, (unsigned char *)&token->timestamp,
sizeof(token->timestamp))
|| (token->is_retry
&& (!PACKET_get_length_prefixed_1(&pkt, &subpkt)
|| (token->odcid.id_len = (unsigned char)PACKET_remaining(&subpkt))
> QUIC_MAX_CONN_ID_LEN
|| !PACKET_copy_bytes(&subpkt,
(unsigned char *)&token->odcid.id,
token->odcid.id_len)
|| !PACKET_get_length_prefixed_1(&pkt, &subpkt)
|| (token->rscid.id_len = (unsigned char)PACKET_remaining(&subpkt))
> QUIC_MAX_CONN_ID_LEN
|| !PACKET_copy_bytes(&subpkt, (unsigned char *)&token->rscid.id,
token->rscid.id_len)))
|| !PACKET_get_length_prefixed_1(&pkt, &subpkt)
|| (token->remote_addr_len = PACKET_remaining(&subpkt)) == 0
|| (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL
|| !PACKET_copy_bytes(&subpkt, token->remote_addr, token->remote_addr_len)
|| PACKET_remaining(&pkt) != 0) {
cleanup_validation_token(token);
return 0;
}
return 1;
}
static void port_send_retry(QUIC_PORT *port,
BIO_ADDR *peer,
QUIC_PKT_HDR *client_hdr)
{
BIO_MSG msg[1];
unsigned char buffer[512];
unsigned char ct_buf[ENCRYPTED_TOKEN_MAX_LEN];
WPACKET wpkt;
size_t written, token_buf_len, ct_len;
QUIC_PKT_HDR hdr = { 0 };
QUIC_VALIDATION_TOKEN token = { 0 };
int ok;
if (!ossl_assert(sizeof(buffer) >= MARSHALLED_TOKEN_MAX_LEN))
return;
memset(&hdr, 0, sizeof(QUIC_PKT_HDR));
hdr.dst_conn_id = client_hdr->src_conn_id;
ok = ossl_quic_lcidm_get_unused_cid(port->lcidm, &hdr.src_conn_id);
if (ok == 0)
goto err;
memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN));
if (!generate_token(peer, client_hdr->dst_conn_id,
hdr.src_conn_id, &token, 1)
|| !marshal_validation_token(&token, buffer, &token_buf_len)
|| !encrypt_validation_token(port, buffer, token_buf_len, NULL,
&ct_len)
|| ct_len > ENCRYPTED_TOKEN_MAX_LEN
|| !encrypt_validation_token(port, buffer, token_buf_len, ct_buf,
&ct_len)
|| !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN))
goto err;
hdr.dst_conn_id = client_hdr->src_conn_id;
hdr.type = QUIC_PKT_TYPE_RETRY;
hdr.fixed = 1;
hdr.version = 1;
hdr.len = ct_len;
hdr.data = ct_buf;
ok = ossl_quic_calculate_retry_integrity_tag(port->engine->libctx,
port->engine->propq, &hdr,
&client_hdr->dst_conn_id,
ct_buf + ct_len
- QUIC_RETRY_INTEGRITY_TAG_LEN);
if (ok == 0)
goto err;
hdr.token = hdr.data;
hdr.token_len = hdr.len;
msg[0].data = buffer;
msg[0].peer = peer;
msg[0].local = NULL;
msg[0].flags = 0;
ok = WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0);
if (ok == 0)
goto err;
ok = ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len,
&hdr, NULL);
if (ok == 0)
goto err;
ok = WPACKET_get_total_written(&wpkt, &msg[0].data_len);
if (ok == 0)
goto err;
ok = WPACKET_finish(&wpkt);
if (ok == 0)
goto err;
if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written))
ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,
"port retry send failed due to network BIO I/O error");
err:
cleanup_validation_token(&token);
}
static void port_send_version_negotiation(QUIC_PORT *port, BIO_ADDR *peer,
QUIC_PKT_HDR *client_hdr)
{
BIO_MSG msg[1];
unsigned char buffer[1024];
QUIC_PKT_HDR hdr;
WPACKET wpkt;
uint32_t supported_versions[1];
size_t written;
size_t i;
memset(&hdr, 0, sizeof(QUIC_PKT_HDR));
hdr.dst_conn_id = client_hdr->src_conn_id;
hdr.src_conn_id = client_hdr->dst_conn_id;
supported_versions[0] = QUIC_VERSION_1;
hdr.type = QUIC_PKT_TYPE_VERSION_NEG;
hdr.version = 0;
hdr.token = 0;
hdr.token_len = 0;
hdr.len = sizeof(supported_versions);
hdr.data = (unsigned char *)supported_versions;
msg[0].data = buffer;
msg[0].peer = peer;
msg[0].local = NULL;
msg[0].flags = 0;
if (!WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0))
return;
if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len,
&hdr, NULL))
return;
for (i = 0; i < OSSL_NELEM(supported_versions); i++) {
if (!WPACKET_put_bytes_u32(&wpkt, supported_versions[i]))
return;
}
if (!WPACKET_get_total_written(&wpkt, &msg[0].data_len))
return;
if (!WPACKET_finish(&wpkt))
return;
if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written))
ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,
"port version negotiation send failed");
}
#define RETRY_LIFETIME 10
#define NEW_TOKEN_LIFETIME 3600
static int port_validate_token(QUIC_PKT_HDR *hdr, QUIC_PORT *port,
BIO_ADDR *peer, QUIC_CONN_ID *odcid,
QUIC_CONN_ID *scid, uint8_t *gen_new_token)
{
int ret = 0;
QUIC_VALIDATION_TOKEN token = { 0 };
uint64_t time_diff;
size_t remote_addr_len, dec_token_len;
unsigned char *remote_addr = NULL, dec_token[MARSHALLED_TOKEN_MAX_LEN];
OSSL_TIME now = ossl_time_now();
*gen_new_token = 0;
if (!decrypt_validation_token(port, hdr->token, hdr->token_len, NULL,
&dec_token_len)
|| dec_token_len > MARSHALLED_TOKEN_MAX_LEN
|| !decrypt_validation_token(port, hdr->token, hdr->token_len,
dec_token, &dec_token_len)
|| !parse_validation_token(&token, dec_token, dec_token_len))
goto err;
if (ossl_time_compare(now, token.timestamp) < 0)
goto err;
time_diff = ossl_time2seconds(ossl_time_abs_difference(token.timestamp,
now));
if ((token.is_retry && time_diff > RETRY_LIFETIME)
|| (!token.is_retry && time_diff > NEW_TOKEN_LIFETIME))
goto err;
if (!BIO_ADDR_rawaddress(peer, NULL, &remote_addr_len)
|| remote_addr_len != token.remote_addr_len
|| (remote_addr = OPENSSL_malloc(remote_addr_len)) == NULL
|| !BIO_ADDR_rawaddress(peer, remote_addr, &remote_addr_len)
|| memcmp(remote_addr, token.remote_addr, remote_addr_len) != 0)
goto err;
if (token.is_retry) {
if (token.rscid.id_len != hdr->dst_conn_id.id_len
|| memcmp(&token.rscid.id, &hdr->dst_conn_id.id,
token.rscid.id_len)
!= 0)
goto err;
*odcid = token.odcid;
*scid = token.rscid;
} else {
if (!ossl_quic_lcidm_get_unused_cid(port->lcidm, odcid))
goto err;
*scid = hdr->src_conn_id;
}
if (token.is_retry) {
*gen_new_token = 1;
} else {
if (time_diff > ((NEW_TOKEN_LIFETIME * 9) / 10))
*gen_new_token = 1;
}
ret = 1;
err:
cleanup_validation_token(&token);
OPENSSL_free(remote_addr);
return ret;
}
static void generate_new_token(QUIC_CHANNEL *ch, BIO_ADDR *peer)
{
QUIC_CONN_ID rscid = { 0 };
QUIC_VALIDATION_TOKEN token;
unsigned char buffer[ENCRYPTED_TOKEN_MAX_LEN];
unsigned char *ct_buf;
size_t ct_len;
size_t token_buf_len = 0;
if (!ch->is_server)
return;
ct_buf = OPENSSL_zalloc(ENCRYPTED_TOKEN_MAX_LEN);
if (ct_buf == NULL)
return;
rscid.id_len = 8;
if (!RAND_bytes_ex(ch->port->engine->libctx, rscid.id, 8, 0)) {
OPENSSL_free(ct_buf);
return;
}
memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN));
if (!generate_token(peer, ch->init_dcid, rscid, &token, 0)
|| !marshal_validation_token(&token, buffer, &token_buf_len)
|| !encrypt_validation_token(ch->port, buffer, token_buf_len, NULL,
&ct_len)
|| ct_len > ENCRYPTED_TOKEN_MAX_LEN
|| !encrypt_validation_token(ch->port, buffer, token_buf_len, ct_buf,
&ct_len)
|| !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN)) {
OPENSSL_free(ct_buf);
cleanup_validation_token(&token);
return;
}
ch->pending_new_token = ct_buf;
ch->pending_new_token_len = ct_len;
cleanup_validation_token(&token);
}
static void port_default_packet_handler(QUIC_URXE *e, void *arg,
const QUIC_CONN_ID *dcid)
{
QUIC_PORT *port = arg;
PACKET pkt;
QUIC_PKT_HDR hdr;
QUIC_CHANNEL *ch = NULL, *new_ch = NULL;
QUIC_CONN_ID odcid, scid;
uint8_t gen_new_token = 0;
OSSL_QRX *qrx = NULL;
OSSL_QRX *qrx_src = NULL;
OSSL_QRX_ARGS qrx_args = { 0 };
uint64_t cause_flags = 0;
OSSL_QRX_PKT *qrx_pkt = NULL;
if (!ossl_quic_port_is_running(port))
goto undesirable;
if (port_try_handle_stateless_reset(port, e))
goto undesirable;
if (dcid != NULL
&& ossl_quic_lcidm_lookup(port->lcidm, dcid, NULL,
(void **)&ch)) {
assert(ch != NULL);
ossl_quic_channel_inject(ch, e);
return;
}
if (!port->allow_incoming)
goto undesirable;
if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN)
goto undesirable;
if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len))
goto undesirable;
if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL,
&cause_flags)) {
if ((cause_flags & QUIC_PKT_HDR_DECODE_BAD_VERSION) == 0)
goto undesirable;
}
switch (hdr.version) {
case QUIC_VERSION_1:
break;
case QUIC_VERSION_NONE:
default:
if (e->data_len < 1200)
goto undesirable;
port_send_version_negotiation(port, &e->peer, &hdr);
goto undesirable;
}
if (hdr.type != QUIC_PKT_TYPE_INITIAL)
goto undesirable;
odcid.id_len = 0;
qrx_args.libctx = port->engine->libctx;
qrx_args.demux = port->demux;
qrx_args.short_conn_id_len = dcid->id_len;
qrx_args.max_deferred = 32;
qrx = ossl_qrx_new(&qrx_args);
if (qrx == NULL)
goto undesirable;
if (!ossl_quic_provide_initial_secret(port->engine->libctx,
port->engine->propq,
&hdr.dst_conn_id,
1,
qrx, NULL))
goto undesirable;
if (ossl_qrx_validate_initial_packet(qrx, e, (const QUIC_CONN_ID *)dcid) == 0)
goto undesirable;
if (port->validate_addr == 0) {
qrx_src = qrx;
qrx = NULL;
}
if (port->validate_addr == 1 && hdr.token == NULL) {
port_send_retry(port, &e->peer, &hdr);
goto undesirable;
}
if (hdr.token != NULL
&& port_validate_token(&hdr, port, &e->peer,
&odcid, &scid,
&gen_new_token)
== 0) {
if (port->validate_addr == 1) {
port_send_retry(port, &e->peer, &hdr);
goto undesirable;
}
qrx_src = qrx;
qrx = NULL;
}
port_bind_channel(port, &e->peer, &scid, &hdr.dst_conn_id,
&odcid, qrx, &new_ch);
if (new_ch == NULL)
goto undesirable;
if (gen_new_token == 1)
generate_new_token(new_ch, &e->peer);
if (qrx != NULL) {
qrx = NULL;
} else {
while (ossl_qrx_read_pkt(qrx_src, &qrx_pkt) == 1)
ossl_quic_channel_inject_pkt(new_ch, qrx_pkt);
ossl_qrx_update_pn_space(qrx_src, new_ch->qrx);
}
undesirable:
ossl_qrx_free(qrx);
ossl_qrx_free(qrx_src);
ossl_quic_demux_release_urxe(port->demux, e);
}
void ossl_quic_port_raise_net_error(QUIC_PORT *port,
QUIC_CHANNEL *triggering_ch)
{
QUIC_CHANNEL *ch;
if (!ossl_quic_port_is_running(port))
return;
ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,
"port failed due to network BIO I/O error");
OSSL_ERR_STATE_save(port->err_state);
port_transition_failed(port);
if (triggering_ch != NULL)
ossl_quic_channel_raise_net_error(triggering_ch);
OSSL_LIST_FOREACH(ch, ch, &port->channel_list)
if (ch != triggering_ch)
ossl_quic_channel_raise_net_error(ch);
}
void ossl_quic_port_restore_err_state(const QUIC_PORT *port)
{
ERR_clear_error();
OSSL_ERR_STATE_restore(port->err_state);
}