QUIC PORT: Make QUIC_PORT responsible for creation of all channels
authorHugo Landau <hlandau@openssl.org>
Thu, 9 Nov 2023 10:27:13 +0000 (10:27 +0000)
committerHugo Landau <hlandau@openssl.org>
Thu, 21 Dec 2023 08:11:59 +0000 (08:11 +0000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22674)

include/internal/quic_channel.h
include/internal/quic_port.h
ssl/quic/quic_impl.c
ssl/quic/quic_port.c
ssl/quic/quic_tserver.c

index 9a434fbb7f97a0975321259edf7c4e8b50f983b2..59caf58b30c45a8fa00d731aae0c61cb2dbbbc65 100644 (file)
@@ -164,6 +164,8 @@ typedef struct quic_terminate_cause_st {
 /*
  * Create a new QUIC channel using the given arguments. The argument structure
  * does not need to remain allocated. Returns NULL on failure.
+ *
+ * Only QUIC_PORT should use this function.
  */
 QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args);
 
index cb277c1971fc3e73394bf420b0e009a5729282f0..08740f5afa76bb28820836b8ec04692d12a8a1d6 100644 (file)
@@ -65,6 +65,20 @@ QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args);
 
 void ossl_quic_port_free(QUIC_PORT *port);
 
+/*
+ * Operations
+ * ==========
+ */
+
+/* Create an outgoing channel using this port. */
+QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls);
+
+/*
+ * Create an incoming channel using this port. XXX for temporary TSERVER use
+ * only - will be removed.
+ */
+QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls);
+
 /*
  * Queries and Accessors
  * =====================
index 45666190cf4817835700f3a762e3c430d09b2b04..d1138f89f5c77314bac427ef02a47c24da209e6b 100644 (file)
@@ -1490,7 +1490,6 @@ QUIC_NEEDS_LOCK
 static int create_channel(QUIC_CONNECTION *qc)
 {
     QUIC_PORT_ARGS port_args = {0};
-    QUIC_CHANNEL_ARGS ch_args = {0};
 
     port_args.libctx        = qc->ssl.ctx->libctx;
     port_args.propq         = qc->ssl.ctx->propq;
@@ -1505,11 +1504,7 @@ static int create_channel(QUIC_CONNECTION *qc)
         return 0;
     }
 
-    ch_args.port       = qc->port;
-    ch_args.is_server  = qc->as_server;
-    ch_args.tls        = qc->tls;
-
-    qc->ch = ossl_quic_channel_new(&ch_args);
+    qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
     if (qc->ch == NULL) {
         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
         ossl_quic_port_free(qc->port);
index 661b6c6cb8ca186312d0c8d97875062695fd4fa9..66e0d3b0d533b8a8bb51c233cac1c95fb5cf7c08 100644 (file)
@@ -214,6 +214,61 @@ int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio)
     return 1;
 }
 
+/*
+ * QUIC Port: Channel Lifecycle
+ * ============================
+ */
+
+static SSL *port_new_handshake_layer(QUIC_PORT *port)
+{
+    SSL *tls = NULL;
+    SSL_CONNECTION *tls_conn = NULL;
+
+    tls = ossl_ssl_connection_new_int(port->channel_ctx, TLS_method());
+    if (tls == NULL || (tls_conn = SSL_CONNECTION_FROM_SSL(tls)) == NULL)
+        return NULL;
+
+    /* Override the user_ssl of the inner connection. */
+    tls_conn->s3.flags      |= TLS1_FLAGS_QUIC;
+
+    /* Restrict options derived from the SSL_CTX. */
+    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, int is_server)
+{
+    QUIC_CHANNEL_ARGS args = {0};
+    QUIC_CHANNEL *ch;
+
+    args.port       = port;
+    args.is_server  = is_server;
+    args.tls        = (tls != NULL ? tls : port_new_handshake_layer(port));
+    if (args.tls == NULL)
+        return NULL;
+
+    ch = ossl_quic_channel_new(&args);
+    if (ch == NULL) {
+        if (tls == NULL)
+            SSL_free(args.tls);
+
+        return NULL;
+    }
+
+    return ch;
+}
+
+QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls)
+{
+    return port_make_channel(port, tls, /*is_server=*/0);
+}
+
+QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls)
+{
+    return port_make_channel(port, tls, /*is_server=*/1);
+}
+
 /*
  * QUIC Port: Ticker-Mutator
  * =========================
index 7882cca700ede8e6579f0345dd15c2053fa04354..72469632535980af2a392900d350daba97e5a88f 100644 (file)
@@ -79,7 +79,6 @@ QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
 {
     QUIC_TSERVER *srv = NULL;
     QUIC_PORT_ARGS port_args = {0};
-    QUIC_CHANNEL_ARGS ch_args = {0};
     QUIC_CONNECTION *qc = NULL;
 
     if (args->net_rbio == NULL || args->net_wbio == NULL)
@@ -127,11 +126,7 @@ QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
     if ((srv->port = ossl_quic_port_new(&port_args)) == NULL)
         goto err;
 
-    ch_args.port        = srv->port;
-    ch_args.tls         = srv->tls;
-    ch_args.is_server   = 1;
-
-    if ((srv->ch = ossl_quic_channel_new(&ch_args)) == NULL)
+    if ((srv->ch = ossl_quic_port_create_incoming(srv->port, srv->tls)) == NULL)
         goto err;
 
     if (!ossl_quic_channel_set_net_rbio(srv->ch, srv->args.net_rbio)