changeset 8381:6e100d8c138a quic

Preserve original DCID and unbreak parsing 0-RTT packets. As per QUIC transport, the first flight of 0-RTT packets obviously uses same Destination and Source Connection ID values as the client's first Initial. The fix is to match 0-RTT against original DCID after it has been switched.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 12 May 2020 18:18:58 +0300
parents efe1f104caf7
children b7704303a7e5
files src/event/ngx_event_quic.c
diffstat 1 files changed, 15 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/event/ngx_event_quic.c
+++ b/src/event/ngx_event_quic.c
@@ -87,6 +87,7 @@ typedef struct {
 struct ngx_quic_connection_s {
     ngx_str_t                         scid;
     ngx_str_t                         dcid;
+    ngx_str_t                         odcid;
     ngx_str_t                         token;
 
     ngx_uint_t                        client_tp_done;
@@ -621,6 +622,13 @@ ngx_quic_new_connection(ngx_connection_t
     ngx_quic_hexdump(c->log, "quic server CID", qc->dcid.data, qc->dcid.len);
 #endif
 
+    qc->odcid.len = pkt->dcid.len;
+    qc->odcid.data = ngx_pnalloc(c->pool, qc->odcid.len);
+    if (qc->odcid.data == NULL) {
+        return NGX_ERROR;
+    }
+    ngx_memcpy(qc->odcid.data, pkt->dcid.data, qc->odcid.len);
+
     qc->scid.len = pkt->scid.len;
     qc->scid.data = ngx_pnalloc(c->pool, qc->scid.len);
     if (qc->scid.data == NULL) {
@@ -638,7 +646,7 @@ ngx_quic_new_connection(ngx_connection_t
     keys = &c->quic->keys[ssl_encryption_initial];
 
     if (ngx_quic_set_initial_secret(c->pool, &keys->client, &keys->server,
-                                    &pkt->dcid)
+                                    &qc->odcid)
         != NGX_OK)
     {
         return NGX_ERROR;
@@ -1232,12 +1240,16 @@ ngx_quic_early_input(ngx_connection_t *c
 static ngx_int_t
 ngx_quic_check_peer(ngx_quic_connection_t *qc, ngx_quic_header_t *pkt)
 {
-    if (pkt->dcid.len != qc->dcid.len) {
+    ngx_str_t  *dcid;
+
+    dcid = ngx_quic_pkt_zrtt(pkt->flags) ? &qc->odcid : &qc->dcid;
+
+    if (pkt->dcid.len != dcid->len) {
         ngx_log_error(NGX_LOG_INFO, pkt->log, 0, "quic unexpected quic dcidl");
         return NGX_ERROR;
     }
 
-    if (ngx_memcmp(pkt->dcid.data, qc->dcid.data, qc->dcid.len) != 0) {
+    if (ngx_memcmp(pkt->dcid.data, dcid->data, dcid->len) != 0) {
         ngx_log_error(NGX_LOG_INFO, pkt->log, 0, "quic unexpected quic dcid");
         return NGX_ERROR;
     }