# HG changeset patch # User Sergey Kandaurov # Date 1613744839 -10800 # Node ID 44b4c61801065e135afa803be7437f91742c9802 # Parent 81bb3a690c10b63b91bb388d748184847c3d91a1 QUIC: multiple versions support. Draft-29 and beyond are now supported simultaneously, no need to recompile. diff --git a/src/event/quic/ngx_event_quic.c b/src/event/quic/ngx_event_quic.c --- a/src/event/quic/ngx_event_quic.c +++ b/src/event/quic/ngx_event_quic.c @@ -1234,7 +1234,8 @@ ngx_quic_new_connection(ngx_connection_t qc->server_seqnum = NGX_QUIC_UNSET_PN; - if (ngx_quic_keys_set_initial_secret(c->pool, qc->keys, &pkt->dcid) + if (ngx_quic_keys_set_initial_secret(c->pool, qc->keys, &pkt->dcid, + qc->version) != NGX_OK) { return NULL; @@ -2611,7 +2612,8 @@ ngx_quic_send_early_cc(ngx_connection_t return NGX_ERROR; } - if (ngx_quic_keys_set_initial_secret(c->pool, pkt.keys, &inpkt->dcid) + if (ngx_quic_keys_set_initial_secret(c->pool, pkt.keys, &inpkt->dcid, + inpkt->version) != NGX_OK) { return NGX_ERROR; diff --git a/src/event/quic/ngx_event_quic_protection.c b/src/event/quic/ngx_event_quic_protection.c --- a/src/event/quic/ngx_event_quic_protection.c +++ b/src/event/quic/ngx_event_quic_protection.c @@ -142,7 +142,7 @@ ngx_quic_ciphers(ngx_uint_t id, ngx_quic ngx_int_t ngx_quic_keys_set_initial_secret(ngx_pool_t *pool, ngx_quic_keys_t *keys, - ngx_str_t *secret) + ngx_str_t *secret, uint32_t version) { size_t is_len; uint8_t is[SHA256_DIGEST_LENGTH]; @@ -152,13 +152,11 @@ ngx_quic_keys_set_initial_secret(ngx_poo ngx_quic_secret_t *client, *server; static const uint8_t salt[20] = -#if (NGX_QUIC_DRAFT_VERSION >= 33) "\x38\x76\x2c\xf7\xf5\x59\x34\xb3\x4d\x17" "\x9a\xe6\xa4\xc8\x0c\xad\xcc\xbb\x7f\x0a"; -#else + static const uint8_t salt29[20] = "\xaf\xbf\xec\x28\x99\x93\xd2\x4c\x9e\x97" "\x86\xf1\x9c\x61\x11\xe0\x43\x90\xa8\x99"; -#endif client = &keys->secrets[ssl_encryption_initial].client; server = &keys->secrets[ssl_encryption_initial].server; @@ -169,7 +167,7 @@ ngx_quic_keys_set_initial_secret(ngx_poo digest = EVP_sha256(); if (ngx_hkdf_extract(is, &is_len, digest, secret->data, secret->len, - salt, sizeof(salt)) + (version & 0xff000000) ? salt29 : salt, sizeof(salt)) != NGX_OK) { return NGX_ERROR; @@ -889,17 +887,13 @@ ngx_quic_create_retry_packet(ngx_quic_he /* 5.8. Retry Packet Integrity */ static u_char key[16] = -#if (NGX_QUIC_DRAFT_VERSION >= 33) "\xbe\x0c\x69\x0b\x9f\x66\x57\x5a\x1d\x76\x6b\x54\xe3\x68\xc8\x4e"; -#else + static u_char key29[16] = "\xcc\xce\x18\x7e\xd0\x9a\x09\xd0\x57\x28\x15\x5a\x6c\xb9\x6b\xe1"; -#endif static u_char nonce[12] = -#if (NGX_QUIC_DRAFT_VERSION >= 33) "\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb"; -#else + static u_char nonce29[12] = "\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c"; -#endif static ngx_str_t in = ngx_string(""); ad.data = res->data; @@ -918,10 +912,12 @@ ngx_quic_create_retry_packet(ngx_quic_he } secret.key.len = sizeof(key); - secret.key.data = key; + secret.key.data = (pkt->version & 0xff000000) ? key29 : key; secret.iv.len = sizeof(nonce); - if (ngx_quic_tls_seal(ciphers.c, &secret, &itag, nonce, &in, &ad, pkt->log) + if (ngx_quic_tls_seal(ciphers.c, &secret, &itag, + (pkt->version & 0xff000000) ? nonce29 : nonce, + &in, &ad, pkt->log) != NGX_OK) { return NGX_ERROR; diff --git a/src/event/quic/ngx_event_quic_protection.h b/src/event/quic/ngx_event_quic_protection.h --- a/src/event/quic/ngx_event_quic_protection.h +++ b/src/event/quic/ngx_event_quic_protection.h @@ -19,7 +19,7 @@ ngx_quic_keys_t *ngx_quic_keys_new(ngx_pool_t *pool); ngx_int_t ngx_quic_keys_set_initial_secret(ngx_pool_t *pool, - ngx_quic_keys_t *keys, ngx_str_t *secret); + ngx_quic_keys_t *keys, ngx_str_t *secret, uint32_t version); int ngx_quic_keys_set_encryption_secret(ngx_pool_t *pool, ngx_uint_t is_write, ngx_quic_keys_t *keys, enum ssl_encryption_level_t level, const SSL_CIPHER *cipher, const uint8_t *secret, size_t secret_len); diff --git a/src/event/quic/ngx_event_quic_transport.c b/src/event/quic/ngx_event_quic_transport.c --- a/src/event/quic/ngx_event_quic_transport.c +++ b/src/event/quic/ngx_event_quic_transport.c @@ -126,16 +126,12 @@ static ngx_int_t ngx_quic_parse_transpor uint32_t ngx_quic_versions[] = { -#if (NGX_QUIC_DRAFT_VERSION >= 33) /* QUICv1 */ 0x00000001, - NGX_QUIC_VERSION(33), -#else NGX_QUIC_VERSION(29), NGX_QUIC_VERSION(30), NGX_QUIC_VERSION(31), NGX_QUIC_VERSION(32), -#endif }; #define NGX_QUIC_NVERSIONS \