annotate src/event/quic/ngx_event_quic_protection.c @ 9172:4ccb0d973206

QUIC: reusing crypto contexts for packet protection.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 20 Oct 2023 18:05:07 +0400
parents f98636db77ef
children 904a54092d5b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2 /*
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
3 * Copyright (C) Nginx, Inc.
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
4 */
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
5
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
6
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
7 #include <ngx_config.h>
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
8 #include <ngx_core.h>
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
9 #include <ngx_event.h>
8755
b4e6b7049984 QUIC: normalize header inclusion.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8739
diff changeset
10 #include <ngx_event_quic_connection.h>
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
11
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
12
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
13 /* RFC 9001, 5.4.1. Header Protection Application: 5-byte mask */
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
14 #define NGX_QUIC_HP_LEN 5
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
15
8800
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
16 #define NGX_QUIC_AES_128_KEY_LEN 16
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
17
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
18
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
19 static ngx_int_t ngx_hkdf_expand(u_char *out_key, size_t out_len,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
20 const EVP_MD *digest, const u_char *prk, size_t prk_len,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
21 const u_char *info, size_t info_len);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
22 static ngx_int_t ngx_hkdf_extract(u_char *out_key, size_t *out_len,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
23 const EVP_MD *digest, const u_char *secret, size_t secret_len,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
24 const u_char *salt, size_t salt_len);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
25
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
26 static uint64_t ngx_quic_parse_pn(u_char **pos, ngx_int_t len, u_char *mask,
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
27 uint64_t *largest_pn);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
28
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
29 static ngx_int_t ngx_quic_crypto_open(ngx_quic_secret_t *s, ngx_str_t *out,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
30 u_char *nonce, ngx_str_t *in, ngx_str_t *ad, ngx_log_t *log);
9171
f98636db77ef QUIC: renamed protection functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9168
diff changeset
31 static ngx_int_t ngx_quic_crypto_hp(ngx_log_t *log, const EVP_CIPHER *cipher,
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
32 ngx_quic_secret_t *s, u_char *out, u_char *in);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
33
8644
e953bd2c5bb3 QUIC: merged create_long/short_packet() functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8643
diff changeset
34 static ngx_int_t ngx_quic_create_packet(ngx_quic_header_t *pkt,
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
35 ngx_str_t *res);
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
36 static ngx_int_t ngx_quic_create_retry_packet(ngx_quic_header_t *pkt,
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
37 ngx_str_t *res);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
38
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
39
9080
7da4791e0264 QUIC: OpenSSL compatibility layer.
Roman Arutyunyan <arut@nginx.com>
parents: 9047
diff changeset
40 ngx_int_t
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
41 ngx_quic_ciphers(ngx_uint_t id, ngx_quic_ciphers_t *ciphers,
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
42 enum ssl_encryption_level_t level)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
43 {
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
44 ngx_int_t len;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
45
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
46 if (level == ssl_encryption_initial) {
9030
172705615d04 QUIC: using native TLSv1.3 cipher suite constants.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9025
diff changeset
47 id = TLS1_3_CK_AES_128_GCM_SHA256;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
48 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
49
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
50 switch (id) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
51
9030
172705615d04 QUIC: using native TLSv1.3 cipher suite constants.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9025
diff changeset
52 case TLS1_3_CK_AES_128_GCM_SHA256:
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
53 #ifdef OPENSSL_IS_BORINGSSL
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
54 ciphers->c = EVP_aead_aes_128_gcm();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
55 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
56 ciphers->c = EVP_aes_128_gcm();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
57 #endif
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
58 ciphers->hp = EVP_aes_128_ctr();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
59 ciphers->d = EVP_sha256();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
60 len = 16;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
61 break;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
62
9030
172705615d04 QUIC: using native TLSv1.3 cipher suite constants.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9025
diff changeset
63 case TLS1_3_CK_AES_256_GCM_SHA384:
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
64 #ifdef OPENSSL_IS_BORINGSSL
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
65 ciphers->c = EVP_aead_aes_256_gcm();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
66 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
67 ciphers->c = EVP_aes_256_gcm();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
68 #endif
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
69 ciphers->hp = EVP_aes_256_ctr();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
70 ciphers->d = EVP_sha384();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
71 len = 32;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
72 break;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
73
9030
172705615d04 QUIC: using native TLSv1.3 cipher suite constants.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9025
diff changeset
74 case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
75 #ifdef OPENSSL_IS_BORINGSSL
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
76 ciphers->c = EVP_aead_chacha20_poly1305();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
77 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
78 ciphers->c = EVP_chacha20_poly1305();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
79 #endif
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
80 #ifdef OPENSSL_IS_BORINGSSL
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
81 ciphers->hp = (const EVP_CIPHER *) EVP_aead_chacha20_poly1305();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
82 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
83 ciphers->hp = EVP_chacha20();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
84 #endif
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
85 ciphers->d = EVP_sha256();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
86 len = 32;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
87 break;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
88
9128
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
89 #ifndef OPENSSL_IS_BORINGSSL
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
90 case TLS1_3_CK_AES_128_CCM_SHA256:
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
91 ciphers->c = EVP_aes_128_ccm();
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
92 ciphers->hp = EVP_aes_128_ctr();
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
93 ciphers->d = EVP_sha256();
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
94 len = 16;
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
95 break;
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
96 #endif
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
97
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
98 default:
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
99 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
100 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
101
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
102 return len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
103 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
104
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
105
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
106 ngx_int_t
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
107 ngx_quic_keys_set_initial_secret(ngx_quic_keys_t *keys, ngx_str_t *secret,
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
108 ngx_log_t *log)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
109 {
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
110 size_t is_len;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
111 uint8_t is[SHA256_DIGEST_LENGTH];
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
112 ngx_str_t iss;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
113 ngx_uint_t i;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
114 const EVP_MD *digest;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
115 ngx_quic_hkdf_t seq[8];
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
116 ngx_quic_secret_t *client, *server;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
117 ngx_quic_ciphers_t ciphers;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
118
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
119 static const uint8_t salt[20] =
8678
3443ee341cc1 QUIC: draft-33 salt and retry keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8673
diff changeset
120 "\x38\x76\x2c\xf7\xf5\x59\x34\xb3\x4d\x17"
3443ee341cc1 QUIC: draft-33 salt and retry keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8673
diff changeset
121 "\x9a\xe6\xa4\xc8\x0c\xad\xcc\xbb\x7f\x0a";
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
122
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
123 client = &keys->secrets[ssl_encryption_initial].client;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
124 server = &keys->secrets[ssl_encryption_initial].server;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
125
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
126 /*
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
127 * RFC 9001, section 5. Packet Protection
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
128 *
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
129 * Initial packets use AEAD_AES_128_GCM. The hash function
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
130 * for HKDF when deriving initial secrets and keys is SHA-256.
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
131 */
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
132
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
133 digest = EVP_sha256();
8729
0f8565e0fc76 QUIC: HKDF API compatibility with OpenSSL master branch.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8716
diff changeset
134 is_len = SHA256_DIGEST_LENGTH;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
135
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
136 if (ngx_hkdf_extract(is, &is_len, digest, secret->data, secret->len,
8980
d8865baab732 QUIC: removed draft versions support.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8926
diff changeset
137 salt, sizeof(salt))
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
138 != NGX_OK)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
139 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
140 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
141 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
142
9040
8c0bccdf2743 QUIC: avoid using C99 designated initializers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9039
diff changeset
143 iss.len = is_len;
8c0bccdf2743 QUIC: avoid using C99 designated initializers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9039
diff changeset
144 iss.data = is;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
145
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
146 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, log, 0,
8702
d4e02b3b734f QUIC: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8694
diff changeset
147 "quic ngx_quic_set_initial_secret");
8578
52ad697f9d1c QUIC: enabled more key-related debug by default.
Vladimir Homutov <vl@nginx.com>
parents: 8565
diff changeset
148 #ifdef NGX_QUIC_DEBUG_CRYPTO
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
149 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0,
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
150 "quic salt len:%uz %*xs", sizeof(salt), sizeof(salt), salt);
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
151 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0,
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
152 "quic initial secret len:%uz %*xs", is_len, is_len, is);
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
153 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
154
8306
058a5af7ddfc Refactored QUIC secrets storage.
Vladimir Homutov <vl@nginx.com>
parents: 8303
diff changeset
155 client->secret.len = SHA256_DIGEST_LENGTH;
058a5af7ddfc Refactored QUIC secrets storage.
Vladimir Homutov <vl@nginx.com>
parents: 8303
diff changeset
156 server->secret.len = SHA256_DIGEST_LENGTH;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
157
8800
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
158 client->key.len = NGX_QUIC_AES_128_KEY_LEN;
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
159 server->key.len = NGX_QUIC_AES_128_KEY_LEN;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
160
8800
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
161 client->hp.len = NGX_QUIC_AES_128_KEY_LEN;
e617d0ba387a QUIC: optimized initial secrets key length computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8799
diff changeset
162 server->hp.len = NGX_QUIC_AES_128_KEY_LEN;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
163
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
164 client->iv.len = NGX_QUIC_IV_LEN;
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
165 server->iv.len = NGX_QUIC_IV_LEN;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
166
9039
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
167 /* labels per RFC 9001, 5.1. Packet Protection Keys */
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
168 ngx_quic_hkdf_set(&seq[0], "tls13 client in", &client->secret, &iss);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
169 ngx_quic_hkdf_set(&seq[1], "tls13 quic key", &client->key, &client->secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
170 ngx_quic_hkdf_set(&seq[2], "tls13 quic iv", &client->iv, &client->secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
171 ngx_quic_hkdf_set(&seq[3], "tls13 quic hp", &client->hp, &client->secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
172 ngx_quic_hkdf_set(&seq[4], "tls13 server in", &server->secret, &iss);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
173 ngx_quic_hkdf_set(&seq[5], "tls13 quic key", &server->key, &server->secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
174 ngx_quic_hkdf_set(&seq[6], "tls13 quic iv", &server->iv, &server->secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
175 ngx_quic_hkdf_set(&seq[7], "tls13 quic hp", &server->hp, &server->secret);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
176
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
177 for (i = 0; i < (sizeof(seq) / sizeof(seq[0])); i++) {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
178 if (ngx_quic_hkdf_expand(&seq[i], digest, log) != NGX_OK) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
179 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
180 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
181 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
182
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
183 if (ngx_quic_ciphers(0, &ciphers, ssl_encryption_initial) == NGX_ERROR) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
184 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
185 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
186
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
187 if (ngx_quic_crypto_init(ciphers.c, client, 0, log) == NGX_ERROR) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
188 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
189 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
190
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
191 if (ngx_quic_crypto_init(ciphers.c, server, 1, log) == NGX_ERROR) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
192 goto failed;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
193 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
194
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
195 return NGX_OK;
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
196
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
197 failed:
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
198
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
199 ngx_quic_keys_cleanup(keys);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
200
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
201 return NGX_ERROR;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
202 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
203
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
204
9080
7da4791e0264 QUIC: OpenSSL compatibility layer.
Roman Arutyunyan <arut@nginx.com>
parents: 9047
diff changeset
205 ngx_int_t
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
206 ngx_quic_hkdf_expand(ngx_quic_hkdf_t *h, const EVP_MD *digest, ngx_log_t *log)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
207 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
208 size_t info_len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
209 uint8_t *p;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
210 uint8_t info[20];
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
211
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
212 info_len = 2 + 1 + h->label_len + 1;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
213
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
214 info[0] = 0;
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
215 info[1] = h->out_len;
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
216 info[2] = h->label_len;
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
217
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
218 p = ngx_cpymem(&info[3], h->label, h->label_len);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
219 *p = '\0';
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
220
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
221 if (ngx_hkdf_expand(h->out, h->out_len, digest,
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
222 h->prk, h->prk_len, info, info_len)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
223 != NGX_OK)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
224 {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
225 ngx_ssl_error(NGX_LOG_INFO, log, 0,
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
226 "ngx_hkdf_expand(%*s) failed", h->label_len, h->label);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
227 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
228 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
229
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
230 #ifdef NGX_QUIC_DEBUG_CRYPTO
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
231 ngx_log_debug5(NGX_LOG_DEBUG_EVENT, log, 0,
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
232 "quic expand \"%*s\" len:%uz %*xs",
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
233 h->label_len, h->label, h->out_len, h->out_len, h->out);
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
234 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
235
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
236 return NGX_OK;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
237 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
238
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
239
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
240 static ngx_int_t
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
241 ngx_hkdf_expand(u_char *out_key, size_t out_len, const EVP_MD *digest,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
242 const uint8_t *prk, size_t prk_len, const u_char *info, size_t info_len)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
243 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
244 #ifdef OPENSSL_IS_BORINGSSL
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
245
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
246 if (HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
247 == 0)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
248 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
249 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
250 }
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
251
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
252 return NGX_OK;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
253
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
254 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
255
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
256 EVP_PKEY_CTX *pctx;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
257
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
258 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
259 if (pctx == NULL) {
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
260 return NGX_ERROR;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
261 }
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
262
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
263 if (EVP_PKEY_derive_init(pctx) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
264 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
265 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
266
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
267 if (EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
268 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
269 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
270
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
271 if (EVP_PKEY_CTX_set_hkdf_md(pctx, digest) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
272 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
273 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
274
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
275 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, prk, prk_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
276 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
277 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
278
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
279 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, info_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
280 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
281 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
282
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
283 if (EVP_PKEY_derive(pctx, out_key, &out_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
284 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
285 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
286
8739
c0cd180308e4 QUIC: fixed memory leak in ngx_hkdf_extract()/ngx_hkdf_expand().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8729
diff changeset
287 EVP_PKEY_CTX_free(pctx);
c0cd180308e4 QUIC: fixed memory leak in ngx_hkdf_extract()/ngx_hkdf_expand().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8729
diff changeset
288
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
289 return NGX_OK;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
290
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
291 failed:
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
292
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
293 EVP_PKEY_CTX_free(pctx);
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
294
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
295 return NGX_ERROR;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
296
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
297 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
298 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
299
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
300
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
301 static ngx_int_t
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
302 ngx_hkdf_extract(u_char *out_key, size_t *out_len, const EVP_MD *digest,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
303 const u_char *secret, size_t secret_len, const u_char *salt,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
304 size_t salt_len)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
305 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
306 #ifdef OPENSSL_IS_BORINGSSL
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
307
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
308 if (HKDF_extract(out_key, out_len, digest, secret, secret_len, salt,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
309 salt_len)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
310 == 0)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
311 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
312 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
313 }
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
314
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
315 return NGX_OK;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
316
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
317 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
318
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
319 EVP_PKEY_CTX *pctx;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
320
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
321 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
322 if (pctx == NULL) {
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
323 return NGX_ERROR;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
324 }
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
325
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
326 if (EVP_PKEY_derive_init(pctx) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
327 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
328 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
329
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
330 if (EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
331 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
332 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
333
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
334 if (EVP_PKEY_CTX_set_hkdf_md(pctx, digest) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
335 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
336 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
337
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
338 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, secret_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
339 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
340 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
341
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
342 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, salt_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
343 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
344 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
345
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
346 if (EVP_PKEY_derive(pctx, out_key, out_len) <= 0) {
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
347 goto failed;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
348 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
349
8739
c0cd180308e4 QUIC: fixed memory leak in ngx_hkdf_extract()/ngx_hkdf_expand().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8729
diff changeset
350 EVP_PKEY_CTX_free(pctx);
c0cd180308e4 QUIC: fixed memory leak in ngx_hkdf_extract()/ngx_hkdf_expand().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8729
diff changeset
351
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
352 return NGX_OK;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
353
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
354 failed:
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
355
8716
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
356 EVP_PKEY_CTX_free(pctx);
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
357
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
358 return NGX_ERROR;
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
359
1c48629cfa74 QUIC: added error handling to ngx_hkdf_extract()/ngx_hkdf_expand().
Vladimir Homutov <vl@nginx.com>
parents: 8710
diff changeset
360 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
361 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
362
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
363
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
364 ngx_int_t
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
365 ngx_quic_crypto_init(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
366 ngx_int_t enc, ngx_log_t *log)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
367 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
368
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
369 #ifdef OPENSSL_IS_BORINGSSL
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
370 EVP_AEAD_CTX *ctx;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
371
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
372 ctx = EVP_AEAD_CTX_new(cipher, s->key.data, s->key.len,
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
373 EVP_AEAD_DEFAULT_TAG_LENGTH);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
374 if (ctx == NULL) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
375 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_AEAD_CTX_new() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
376 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
377 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
378 #else
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
379 EVP_CIPHER_CTX *ctx;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
380
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
381 ctx = EVP_CIPHER_CTX_new();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
382 if (ctx == NULL) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
383 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_CIPHER_CTX_new() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
384 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
385 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
386
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
387 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) != 1) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
388 EVP_CIPHER_CTX_free(ctx);
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
389 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_CipherInit_ex() failed");
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
390 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
391 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
392
9128
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
393 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
394 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, NGX_QUIC_TAG_LEN,
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
395 NULL)
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
396 == 0)
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
397 {
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
398 EVP_CIPHER_CTX_free(ctx);
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
399 ngx_ssl_error(NGX_LOG_INFO, log, 0,
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
400 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_TAG) failed");
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
401 return NGX_ERROR;
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
402 }
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
403
9127
a7b850a5d98d QUIC: common cipher control constants instead of GCM-related.
Roman Arutyunyan <arut@nginx.com>
parents: 9126
diff changeset
404 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, s->iv.len, NULL)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
405 == 0)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
406 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
407 EVP_CIPHER_CTX_free(ctx);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
408 ngx_ssl_error(NGX_LOG_INFO, log, 0,
9127
a7b850a5d98d QUIC: common cipher control constants instead of GCM-related.
Roman Arutyunyan <arut@nginx.com>
parents: 9126
diff changeset
409 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_IVLEN) failed");
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
410 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
411 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
412
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
413 if (EVP_CipherInit_ex(ctx, NULL, NULL, s->key.data, NULL, enc) != 1) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
414 EVP_CIPHER_CTX_free(ctx);
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
415 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_CipherInit_ex() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
416 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
417 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
418 #endif
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
419
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
420 s->ctx = ctx;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
421 return NGX_OK;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
422 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
423
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
424
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
425 static ngx_int_t
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
426 ngx_quic_crypto_open(ngx_quic_secret_t *s, ngx_str_t *out, u_char *nonce,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
427 ngx_str_t *in, ngx_str_t *ad, ngx_log_t *log)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
428 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
429 ngx_quic_crypto_ctx_t *ctx;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
430
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
431 ctx = s->ctx;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
432
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
433 #ifdef OPENSSL_IS_BORINGSSL
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
434 if (EVP_AEAD_CTX_open(ctx, out->data, &out->len, out->len, nonce, s->iv.len,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
435 in->data, in->len, ad->data, ad->len)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
436 != 1)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
437 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
438 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_AEAD_CTX_open() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
439 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
440 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
441 #else
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
442 int len;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
443
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
444 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, nonce) != 1) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
445 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptInit_ex() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
446 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
447 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
448
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
449 in->len -= NGX_QUIC_TAG_LEN;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
450
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
451 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, NGX_QUIC_TAG_LEN,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
452 in->data + in->len)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
453 == 0)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
454 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
455 ngx_ssl_error(NGX_LOG_INFO, log, 0,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
456 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_TAG) failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
457 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
458 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
459
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
460 if (EVP_CIPHER_mode(EVP_CIPHER_CTX_cipher(ctx)) == EVP_CIPH_CCM_MODE
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
461 && EVP_DecryptUpdate(ctx, NULL, &len, NULL, in->len) != 1)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
462 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
463 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
464 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
465 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
466
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
467 if (EVP_DecryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
468 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
469 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
470 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
471
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
472 if (EVP_DecryptUpdate(ctx, out->data, &len, in->data, in->len) != 1) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
473 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
474 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
475 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
476
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
477 out->len = len;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
478
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
479 if (EVP_DecryptFinal_ex(ctx, out->data + out->len, &len) <= 0) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
480 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptFinal_ex failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
481 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
482 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
483
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
484 out->len += len;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
485 #endif
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
486
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
487 return NGX_OK;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
488 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
489
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
490
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
491 ngx_int_t
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
492 ngx_quic_crypto_seal(ngx_quic_secret_t *s, ngx_str_t *out, u_char *nonce,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
493 ngx_str_t *in, ngx_str_t *ad, ngx_log_t *log)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
494 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
495 ngx_quic_crypto_ctx_t *ctx;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
496
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
497 ctx = s->ctx;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
498
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
499 #ifdef OPENSSL_IS_BORINGSSL
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
500 if (EVP_AEAD_CTX_seal(ctx, out->data, &out->len, out->len, nonce, s->iv.len,
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
501 in->data, in->len, ad->data, ad->len)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
502 != 1)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
503 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
504 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_AEAD_CTX_seal() failed");
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
505 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
506 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
507 #else
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
508 int len;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
509
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
510 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, nonce) != 1) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
511 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptInit_ex() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
512 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
513 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
514
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
515 if (EVP_CIPHER_mode(EVP_CIPHER_CTX_cipher(ctx)) == EVP_CIPH_CCM_MODE
9128
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
516 && EVP_EncryptUpdate(ctx, NULL, &len, NULL, in->len) != 1)
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
517 {
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
518 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
519 return NGX_ERROR;
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
520 }
756ab66de10e QUIC: TLS_AES_128_CCM_SHA256 cipher suite support.
Roman Arutyunyan <arut@nginx.com>
parents: 9127
diff changeset
521
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
522 if (EVP_EncryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
523 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
524 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
525 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
526
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
527 if (EVP_EncryptUpdate(ctx, out->data, &len, in->data, in->len) != 1) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
528 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
529 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
530 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
531
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
532 out->len = len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
533
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
534 if (EVP_EncryptFinal_ex(ctx, out->data + out->len, &len) <= 0) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
535 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptFinal_ex failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
536 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
537 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
538
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
539 out->len += len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
540
9127
a7b850a5d98d QUIC: common cipher control constants instead of GCM-related.
Roman Arutyunyan <arut@nginx.com>
parents: 9126
diff changeset
541 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, NGX_QUIC_TAG_LEN,
9129
7379cb29cd72 QUIC: unified ngx_quic_tls_open() and ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 9128
diff changeset
542 out->data + out->len)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
543 == 0)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
544 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
545 ngx_ssl_error(NGX_LOG_INFO, log, 0,
9127
a7b850a5d98d QUIC: common cipher control constants instead of GCM-related.
Roman Arutyunyan <arut@nginx.com>
parents: 9126
diff changeset
546 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_GET_TAG) failed");
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
547 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
548 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
549
9130
0f23488a9f5a QUIC: style.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9129
diff changeset
550 out->len += NGX_QUIC_TAG_LEN;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
551 #endif
9130
0f23488a9f5a QUIC: style.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9129
diff changeset
552
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
553 return NGX_OK;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
554 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
555
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
556
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
557 void
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
558 ngx_quic_crypto_cleanup(ngx_quic_secret_t *s)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
559 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
560 if (s->ctx) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
561 #ifdef OPENSSL_IS_BORINGSSL
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
562 EVP_AEAD_CTX_free(s->ctx);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
563 #else
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
564 EVP_CIPHER_CTX_free(s->ctx);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
565 #endif
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
566 s->ctx = NULL;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
567 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
568 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
569
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
570
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
571 static ngx_int_t
9171
f98636db77ef QUIC: renamed protection functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9168
diff changeset
572 ngx_quic_crypto_hp(ngx_log_t *log, const EVP_CIPHER *cipher,
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
573 ngx_quic_secret_t *s, u_char *out, u_char *in)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
574 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
575 int outlen;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
576 EVP_CIPHER_CTX *ctx;
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
577 u_char zero[NGX_QUIC_HP_LEN] = {0};
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
578
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
579 #ifdef OPENSSL_IS_BORINGSSL
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
580 uint32_t cnt;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
581
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
582 ngx_memcpy(&cnt, in, sizeof(uint32_t));
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
583
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
584 if (cipher == (const EVP_CIPHER *) EVP_aead_chacha20_poly1305()) {
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
585 CRYPTO_chacha_20(out, zero, NGX_QUIC_HP_LEN, s->hp.data, &in[4], cnt);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
586 return NGX_OK;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
587 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
588 #endif
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
589
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
590 ctx = EVP_CIPHER_CTX_new();
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
591 if (ctx == NULL) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
592 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
593 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
594
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
595 if (EVP_EncryptInit_ex(ctx, cipher, NULL, s->hp.data, in) != 1) {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
596 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptInit_ex() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
597 goto failed;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
598 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
599
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
600 if (!EVP_EncryptUpdate(ctx, out, &outlen, zero, NGX_QUIC_HP_LEN)) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
601 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
602 goto failed;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
603 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
604
8798
fc5719637aff QUIC: consistent use of 5-byte buffers for header protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8797
diff changeset
605 if (!EVP_EncryptFinal_ex(ctx, out + NGX_QUIC_HP_LEN, &outlen)) {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
606 ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptFinal_Ex() failed");
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
607 goto failed;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
608 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
609
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
610 EVP_CIPHER_CTX_free(ctx);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
611
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
612 return NGX_OK;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
613
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
614 failed:
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
615
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
616 EVP_CIPHER_CTX_free(ctx);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
617
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
618 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
619 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
620
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
621
8926
3341e4089c6c QUIC: converted ngx_quic_keys_set_encryption_secret() to NGX codes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8894
diff changeset
622 ngx_int_t
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
623 ngx_quic_keys_set_encryption_secret(ngx_log_t *log, ngx_uint_t is_write,
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
624 ngx_quic_keys_t *keys, enum ssl_encryption_level_t level,
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
625 const SSL_CIPHER *cipher, const uint8_t *secret, size_t secret_len)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
626 {
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
627 ngx_int_t key_len;
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
628 ngx_str_t secret_str;
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
629 ngx_uint_t i;
9039
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
630 ngx_quic_hkdf_t seq[3];
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
631 ngx_quic_secret_t *peer_secret;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
632 ngx_quic_ciphers_t ciphers;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
633
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
634 peer_secret = is_write ? &keys->secrets[level].server
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
635 : &keys->secrets[level].client;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
636
9030
172705615d04 QUIC: using native TLSv1.3 cipher suite constants.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9025
diff changeset
637 keys->cipher = SSL_CIPHER_get_id(cipher);
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
638
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
639 key_len = ngx_quic_ciphers(keys->cipher, &ciphers, level);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
640
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
641 if (key_len == NGX_ERROR) {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
642 ngx_ssl_error(NGX_LOG_INFO, log, 0, "unexpected cipher");
8926
3341e4089c6c QUIC: converted ngx_quic_keys_set_encryption_secret() to NGX codes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8894
diff changeset
643 return NGX_ERROR;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
644 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
645
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
646 if (sizeof(peer_secret->secret.data) < secret_len) {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
647 ngx_log_error(NGX_LOG_ALERT, log, 0,
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
648 "unexpected secret len: %uz", secret_len);
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
649 return NGX_ERROR;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
650 }
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
651
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
652 peer_secret->secret.len = secret_len;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
653 ngx_memcpy(peer_secret->secret.data, secret, secret_len);
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
654
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
655 peer_secret->key.len = key_len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
656 peer_secret->iv.len = NGX_QUIC_IV_LEN;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
657 peer_secret->hp.len = key_len;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
658
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
659 secret_str.len = secret_len;
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
660 secret_str.data = (u_char *) secret;
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
661
9039
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
662 ngx_quic_hkdf_set(&seq[0], "tls13 quic key",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
663 &peer_secret->key, &secret_str);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
664 ngx_quic_hkdf_set(&seq[1], "tls13 quic iv", &peer_secret->iv, &secret_str);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
665 ngx_quic_hkdf_set(&seq[2], "tls13 quic hp", &peer_secret->hp, &secret_str);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
666
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
667 for (i = 0; i < (sizeof(seq) / sizeof(seq[0])); i++) {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
668 if (ngx_quic_hkdf_expand(&seq[i], ciphers.d, log) != NGX_OK) {
8926
3341e4089c6c QUIC: converted ngx_quic_keys_set_encryption_secret() to NGX codes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8894
diff changeset
669 return NGX_ERROR;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
670 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
671 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
672
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
673 if (ngx_quic_crypto_init(ciphers.c, peer_secret, is_write, log)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
674 == NGX_ERROR)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
675 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
676 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
677 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
678
8926
3341e4089c6c QUIC: converted ngx_quic_keys_set_encryption_secret() to NGX codes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8894
diff changeset
679 return NGX_OK;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
680 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
681
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
682
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
683 ngx_uint_t
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
684 ngx_quic_keys_available(ngx_quic_keys_t *keys,
9168
ff98ae7d261e QUIC: split keys availability checks to read and write sides.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9152
diff changeset
685 enum ssl_encryption_level_t level, ngx_uint_t is_write)
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
686 {
9168
ff98ae7d261e QUIC: split keys availability checks to read and write sides.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9152
diff changeset
687 if (is_write == 0) {
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
688 return keys->secrets[level].client.ctx != NULL;
9168
ff98ae7d261e QUIC: split keys availability checks to read and write sides.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9152
diff changeset
689 }
ff98ae7d261e QUIC: split keys availability checks to read and write sides.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9152
diff changeset
690
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
691 return keys->secrets[level].server.ctx != NULL;
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
692 }
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
693
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
694
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
695 void
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
696 ngx_quic_keys_discard(ngx_quic_keys_t *keys,
8702
d4e02b3b734f QUIC: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8694
diff changeset
697 enum ssl_encryption_level_t level)
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
698 {
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
699 ngx_quic_secret_t *client, *server;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
700
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
701 client = &keys->secrets[level].client;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
702 server = &keys->secrets[level].server;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
703
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
704 ngx_quic_crypto_cleanup(client);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
705 ngx_quic_crypto_cleanup(server);
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
706 }
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
707
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
708
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
709 void
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
710 ngx_quic_keys_switch(ngx_connection_t *c, ngx_quic_keys_t *keys)
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
711 {
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
712 ngx_quic_secrets_t *current, *next, tmp;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
713
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
714 current = &keys->secrets[ssl_encryption_application];
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
715 next = &keys->next_key;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
716
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
717 ngx_quic_crypto_cleanup(&current->client);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
718 ngx_quic_crypto_cleanup(&current->server);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
719
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
720 tmp = *current;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
721 *current = *next;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
722 *next = tmp;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
723 }
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
724
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
725
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
726 void
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
727 ngx_quic_keys_update(ngx_event_t *ev)
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
728 {
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
729 ngx_uint_t i;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
730 ngx_quic_hkdf_t seq[6];
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
731 ngx_quic_keys_t *keys;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
732 ngx_connection_t *c;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
733 ngx_quic_ciphers_t ciphers;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
734 ngx_quic_secrets_t *current, *next;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
735 ngx_quic_connection_t *qc;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
736
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
737 c = ev->data;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
738 qc = ngx_quic_get_connection(c);
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
739 keys = qc->keys;
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
740
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
741 current = &keys->secrets[ssl_encryption_application];
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
742 next = &keys->next_key;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
743
8360
f175006124d0 Cleaned up hexdumps in debug output.
Vladimir Homutov <vl@nginx.com>
parents: 8359
diff changeset
744 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic key update");
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
745
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
746 c->log->action = "updating keys";
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
747
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
748 if (ngx_quic_ciphers(keys->cipher, &ciphers, ssl_encryption_application)
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
749 == NGX_ERROR)
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
750 {
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
751 goto failed;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
752 }
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
753
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
754 next->client.secret.len = current->client.secret.len;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
755 next->client.key.len = current->client.key.len;
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
756 next->client.iv.len = NGX_QUIC_IV_LEN;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
757 next->client.hp = current->client.hp;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
758
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
759 next->server.secret.len = current->server.secret.len;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
760 next->server.key.len = current->server.key.len;
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
761 next->server.iv.len = NGX_QUIC_IV_LEN;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
762 next->server.hp = current->server.hp;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
763
9039
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
764 ngx_quic_hkdf_set(&seq[0], "tls13 quic ku",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
765 &next->client.secret, &current->client.secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
766 ngx_quic_hkdf_set(&seq[1], "tls13 quic key",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
767 &next->client.key, &next->client.secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
768 ngx_quic_hkdf_set(&seq[2], "tls13 quic iv",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
769 &next->client.iv, &next->client.secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
770 ngx_quic_hkdf_set(&seq[3], "tls13 quic ku",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
771 &next->server.secret, &current->server.secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
772 ngx_quic_hkdf_set(&seq[4], "tls13 quic key",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
773 &next->server.key, &next->server.secret);
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
774 ngx_quic_hkdf_set(&seq[5], "tls13 quic iv",
a6cc246654f8 QUIC: moved variable declaration to fix build with MSVC 2010.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9030
diff changeset
775 &next->server.iv, &next->server.secret);
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
776
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
777 for (i = 0; i < (sizeof(seq) / sizeof(seq[0])); i++) {
9024
f2925c80401c QUIC: avoided pool usage in ngx_quic_protection.c.
Vladimir Homutov <vl@nginx.com>
parents: 9023
diff changeset
778 if (ngx_quic_hkdf_expand(&seq[i], ciphers.d, c->log) != NGX_OK) {
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
779 goto failed;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
780 }
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
781 }
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
782
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
783 if (ngx_quic_crypto_init(ciphers.c, &next->client, 0, c->log) == NGX_ERROR)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
784 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
785 goto failed;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
786 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
787
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
788 if (ngx_quic_crypto_init(ciphers.c, &next->server, 1, c->log) == NGX_ERROR)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
789 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
790 goto failed;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
791 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
792
9152
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
793 return;
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
794
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
795 failed:
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
796
2880f60a80c3 QUIC: posted generating TLS Key Update next keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9131
diff changeset
797 ngx_quic_close_connection(c, NGX_ERROR);
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
798 }
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
799
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
800
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
801 void
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
802 ngx_quic_keys_cleanup(ngx_quic_keys_t *keys)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
803 {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
804 ngx_uint_t i;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
805 ngx_quic_secrets_t *next;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
806
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
807 for (i = 0; i < NGX_QUIC_ENCRYPTION_LAST; i++) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
808 ngx_quic_keys_discard(keys, i);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
809 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
810
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
811 next = &keys->next_key;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
812
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
813 ngx_quic_crypto_cleanup(&next->client);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
814 ngx_quic_crypto_cleanup(&next->server);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
815 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
816
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
817
8376
2d0f4aa78ed6 Restored ngx_quic_encrypt return type.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8375
diff changeset
818 static ngx_int_t
8644
e953bd2c5bb3 QUIC: merged create_long/short_packet() functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8643
diff changeset
819 ngx_quic_create_packet(ngx_quic_header_t *pkt, ngx_str_t *res)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
820 {
8285
f85749b60e58 Removed memory allocations from encryption code.
Vladimir Homutov <vl@nginx.com>
parents: 8265
diff changeset
821 u_char *pnp, *sample;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
822 ngx_str_t ad, out;
8315
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
823 ngx_uint_t i;
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
824 ngx_quic_secret_t *secret;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
825 ngx_quic_ciphers_t ciphers;
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
826 u_char nonce[NGX_QUIC_IV_LEN], mask[NGX_QUIC_HP_LEN];
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
827
8894
de7b9af30fc6 QUIC: refactored packet creation.
Vladimir Homutov <vl@nginx.com>
parents: 8888
diff changeset
828 ad.data = res->data;
de7b9af30fc6 QUIC: refactored packet creation.
Vladimir Homutov <vl@nginx.com>
parents: 8888
diff changeset
829 ad.len = ngx_quic_create_header(pkt, ad.data, &pnp);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
830
9126
29a6c0e11f75 QUIC: a new constant for AEAD tag length.
Roman Arutyunyan <arut@nginx.com>
parents: 9080
diff changeset
831 out.len = pkt->payload.len + NGX_QUIC_TAG_LEN;
8318
1bb5e8538d0c Removed excessive debugging in QUIC packet creation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8317
diff changeset
832 out.data = res->data + ad.len;
1bb5e8538d0c Removed excessive debugging in QUIC packet creation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8317
diff changeset
833
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
834 #ifdef NGX_QUIC_DEBUG_CRYPTO
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
835 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
836 "quic ad len:%uz %xV", ad.len, &ad);
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
837 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
838
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
839 if (ngx_quic_ciphers(pkt->keys->cipher, &ciphers, pkt->level) == NGX_ERROR)
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
840 {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
841 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
842 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
843
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
844 secret = &pkt->keys->secrets[pkt->level].server;
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
845
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
846 ngx_memcpy(nonce, secret->iv.data, secret->iv.len);
8310
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
847 ngx_quic_compute_nonce(nonce, sizeof(nonce), pkt->number);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
848
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
849 if (ngx_quic_crypto_seal(secret, &out, nonce, &pkt->payload, &ad, pkt->log)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
850 != NGX_OK)
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
851 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
852 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
853 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
854
8315
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
855 sample = &out.data[4 - pkt->num_len];
9171
f98636db77ef QUIC: renamed protection functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9168
diff changeset
856 if (ngx_quic_crypto_hp(pkt->log, ciphers.hp, secret, mask, sample)
8285
f85749b60e58 Removed memory allocations from encryption code.
Vladimir Homutov <vl@nginx.com>
parents: 8265
diff changeset
857 != NGX_OK)
f85749b60e58 Removed memory allocations from encryption code.
Vladimir Homutov <vl@nginx.com>
parents: 8265
diff changeset
858 {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
859 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
860 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
861
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
862 /* RFC 9001, 5.4.1. Header Protection Application */
8643
5fdd0ef42232 QUIC: macros for manipulating header protection and reserved bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8642
diff changeset
863 ad.data[0] ^= mask[0] & ngx_quic_pkt_hp_mask(pkt->flags);
8315
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
864
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
865 for (i = 0; i < pkt->num_len; i++) {
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
866 pnp[i] ^= mask[i + 1];
fdda518d10ba Proper handling of packet number in header.
Vladimir Homutov <vl@nginx.com>
parents: 8313
diff changeset
867 }
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
868
8285
f85749b60e58 Removed memory allocations from encryption code.
Vladimir Homutov <vl@nginx.com>
parents: 8265
diff changeset
869 res->len = ad.len + out.len;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
870
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
871 return NGX_OK;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
872 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
873
8285
f85749b60e58 Removed memory allocations from encryption code.
Vladimir Homutov <vl@nginx.com>
parents: 8265
diff changeset
874
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
875 static ngx_int_t
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
876 ngx_quic_create_retry_packet(ngx_quic_header_t *pkt, ngx_str_t *res)
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
877 {
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
878 u_char *start;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
879 ngx_str_t ad, itag;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
880 ngx_quic_secret_t secret;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
881 ngx_quic_ciphers_t ciphers;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
882
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
883 /* 5.8. Retry Packet Integrity */
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
884 static u_char key[16] =
8678
3443ee341cc1 QUIC: draft-33 salt and retry keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8673
diff changeset
885 "\xbe\x0c\x69\x0b\x9f\x66\x57\x5a\x1d\x76\x6b\x54\xe3\x68\xc8\x4e";
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
886 static u_char nonce[NGX_QUIC_IV_LEN] =
8678
3443ee341cc1 QUIC: draft-33 salt and retry keys.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8673
diff changeset
887 "\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb";
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
888 static ngx_str_t in = ngx_string("");
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
889
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
890 ad.data = res->data;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
891 ad.len = ngx_quic_create_retry_itag(pkt, ad.data, &start);
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
892
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
893 itag.data = ad.data + ad.len;
9126
29a6c0e11f75 QUIC: a new constant for AEAD tag length.
Roman Arutyunyan <arut@nginx.com>
parents: 9080
diff changeset
894 itag.len = NGX_QUIC_TAG_LEN;
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
895
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
896 #ifdef NGX_QUIC_DEBUG_CRYPTO
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
897 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
898 "quic retry itag len:%uz %xV", ad.len, &ad);
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
899 #endif
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
900
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
901 if (ngx_quic_ciphers(0, &ciphers, pkt->level) == NGX_ERROR) {
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
902 return NGX_ERROR;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
903 }
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
904
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
905 secret.key.len = sizeof(key);
9023
d8b3851f172c QUIC: fixed-length buffers for secrets.
Vladimir Homutov <vl@nginx.com>
parents: 8980
diff changeset
906 ngx_memcpy(secret.key.data, key, sizeof(key));
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
907 secret.iv.len = NGX_QUIC_IV_LEN;
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
908
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
909 if (ngx_quic_crypto_init(ciphers.c, &secret, 1, pkt->log) == NGX_ERROR) {
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
910 return NGX_ERROR;
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
911 }
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
912
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
913 if (ngx_quic_crypto_seal(&secret, &itag, nonce, &in, &ad, pkt->log)
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
914 != NGX_OK)
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
915 {
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
916 ngx_quic_crypto_cleanup(&secret);
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
917 return NGX_ERROR;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
918 }
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
919
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
920 ngx_quic_crypto_cleanup(&secret);
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
921
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
922 res->len = itag.data + itag.len - start;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
923 res->data = start;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
924
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
925 return NGX_OK;
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
926 }
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
927
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
928
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
929 ngx_int_t
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
930 ngx_quic_derive_key(ngx_log_t *log, const char *label, ngx_str_t *secret,
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
931 ngx_str_t *salt, u_char *out, size_t len)
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
932 {
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
933 size_t is_len, info_len;
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
934 uint8_t *p;
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
935 const EVP_MD *digest;
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
936
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
937 uint8_t is[SHA256_DIGEST_LENGTH];
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
938 uint8_t info[20];
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
939
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
940 digest = EVP_sha256();
8729
0f8565e0fc76 QUIC: HKDF API compatibility with OpenSSL master branch.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8716
diff changeset
941 is_len = SHA256_DIGEST_LENGTH;
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
942
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
943 if (ngx_hkdf_extract(is, &is_len, digest, secret->data, secret->len,
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
944 salt->data, salt->len)
8702
d4e02b3b734f QUIC: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8694
diff changeset
945 != NGX_OK)
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
946 {
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
947 ngx_ssl_error(NGX_LOG_INFO, log, 0,
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
948 "ngx_hkdf_extract(%s) failed", label);
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
949 return NGX_ERROR;
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
950 }
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
951
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
952 info[0] = 0;
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
953 info[1] = len;
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
954 info[2] = ngx_strlen(label);
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
955
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
956 info_len = 2 + 1 + info[2] + 1;
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
957
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
958 if (info_len >= 20) {
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
959 ngx_log_error(NGX_LOG_INFO, log, 0,
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
960 "ngx_quic_create_key label \"%s\" too long", label);
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
961 return NGX_ERROR;
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
962 }
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
963
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
964 p = ngx_cpymem(&info[3], label, info[2]);
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
965 *p = '\0';
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
966
8694
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
967 if (ngx_hkdf_expand(out, len, digest, is, is_len, info, info_len) != NGX_OK)
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
968 {
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
969 ngx_ssl_error(NGX_LOG_INFO, log, 0,
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
970 "ngx_hkdf_expand(%s) failed", label);
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
971 return NGX_ERROR;
cef042935003 QUIC: the "quic_host_key" directive.
Vladimir Homutov <vl@nginx.com>
parents: 8678
diff changeset
972 }
8562
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
973
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
974 return NGX_OK;
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
975 }
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
976
b31c02454539 QUIC: added stateless reset support.
Vladimir Homutov <vl@nginx.com>
parents: 8558
diff changeset
977
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
978 static uint64_t
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
979 ngx_quic_parse_pn(u_char **pos, ngx_int_t len, u_char *mask,
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
980 uint64_t *largest_pn)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
981 {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
982 u_char *p;
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
983 uint64_t truncated_pn, expected_pn, candidate_pn;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
984 uint64_t pn_nbits, pn_win, pn_hwin, pn_mask;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
985
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
986 pn_nbits = ngx_min(len * 8, 62);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
987
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
988 p = *pos;
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
989 truncated_pn = *p++ ^ *mask++;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
990
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
991 while (--len) {
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
992 truncated_pn = (truncated_pn << 8) + (*p++ ^ *mask++);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
993 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
994
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
995 *pos = p;
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
996
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
997 expected_pn = *largest_pn + 1;
8394
df18ae7161b8 Assorted fixes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8386
diff changeset
998 pn_win = 1ULL << pn_nbits;
8339
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
999 pn_hwin = pn_win / 2;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1000 pn_mask = pn_win - 1;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1001
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1002 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1003
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1004 if ((int64_t) candidate_pn <= (int64_t) (expected_pn - pn_hwin)
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1005 && candidate_pn < (1ULL << 62) - pn_win)
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1006 {
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1007 candidate_pn += pn_win;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1008
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1009 } else if (candidate_pn > expected_pn + pn_hwin
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1010 && candidate_pn >= pn_win)
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1011 {
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1012 candidate_pn -= pn_win;
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1013 }
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1014
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1015 *largest_pn = ngx_max((int64_t) *largest_pn, (int64_t) candidate_pn);
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1016
aba84d9ab256 Parsing of truncated packet numbers.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8324
diff changeset
1017 return candidate_pn;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1018 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1019
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1020
9080
7da4791e0264 QUIC: OpenSSL compatibility layer.
Roman Arutyunyan <arut@nginx.com>
parents: 9047
diff changeset
1021 void
8310
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1022 ngx_quic_compute_nonce(u_char *nonce, size_t len, uint64_t pn)
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1023 {
9047
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1024 nonce[len - 8] ^= (pn >> 56) & 0x3f;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1025 nonce[len - 7] ^= (pn >> 48) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1026 nonce[len - 6] ^= (pn >> 40) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1027 nonce[len - 5] ^= (pn >> 32) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1028 nonce[len - 4] ^= (pn >> 24) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1029 nonce[len - 3] ^= (pn >> 16) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1030 nonce[len - 2] ^= (pn >> 8) & 0xff;
70ce1e927715 QUIC: fixed computation of nonce with packet numbers beyond 2^32.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9041
diff changeset
1031 nonce[len - 1] ^= pn & 0xff;
8310
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1032 }
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1033
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1034
8376
2d0f4aa78ed6 Restored ngx_quic_encrypt return type.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8375
diff changeset
1035 ngx_int_t
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1036 ngx_quic_encrypt(ngx_quic_header_t *pkt, ngx_str_t *res)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1037 {
8383
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
1038 if (ngx_quic_pkt_retry(pkt->flags)) {
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
1039 return ngx_quic_create_retry_packet(pkt, res);
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
1040 }
7ea34e13937f Address validation using Retry packets.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8376
diff changeset
1041
8644
e953bd2c5bb3 QUIC: merged create_long/short_packet() functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8643
diff changeset
1042 return ngx_quic_create_packet(pkt, res);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1043 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1044
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1045
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1046 ngx_int_t
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1047 ngx_quic_decrypt(ngx_quic_header_t *pkt, uint64_t *largest_pn)
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1048 {
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1049 u_char *p, *sample;
8558
0f37b4ef3cd9 QUIC: keep the entire packet size in pkt->len.
Roman Arutyunyan <arut@nginx.com>
parents: 8544
diff changeset
1050 size_t len;
8532
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1051 uint64_t pn, lpn;
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
1052 ngx_int_t pnl;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1053 ngx_str_t in, ad;
9041
e23fd55e1cc6 QUIC: fixed C4389 MSVC warning about signed/unsigned mismatch.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9040
diff changeset
1054 ngx_uint_t key_phase;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1055 ngx_quic_secret_t *secret;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1056 ngx_quic_ciphers_t ciphers;
8799
ef8276c8ccff QUIC: consistent use of 12-byte buffers in nonce computation.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8798
diff changeset
1057 uint8_t nonce[NGX_QUIC_IV_LEN], mask[NGX_QUIC_HP_LEN];
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1058
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1059 if (ngx_quic_ciphers(pkt->keys->cipher, &ciphers, pkt->level) == NGX_ERROR)
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1060 {
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1061 return NGX_ERROR;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1062 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1063
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1064 secret = &pkt->keys->secrets[pkt->level].client;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1065
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1066 p = pkt->raw->pos;
8558
0f37b4ef3cd9 QUIC: keep the entire packet size in pkt->len.
Roman Arutyunyan <arut@nginx.com>
parents: 8544
diff changeset
1067 len = pkt->data + pkt->len - p;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1068
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1069 /*
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1070 * RFC 9001, 5.4.2. Header Protection Sample
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1071 * 5.4.3. AES-Based Header Protection
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1072 * 5.4.4. ChaCha20-Based Header Protection
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1073 *
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1074 * the Packet Number field is assumed to be 4 bytes long
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1075 * AES and ChaCha20 algorithms sample 16 bytes
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1076 */
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1077
9126
29a6c0e11f75 QUIC: a new constant for AEAD tag length.
Roman Arutyunyan <arut@nginx.com>
parents: 9080
diff changeset
1078 if (len < NGX_QUIC_TAG_LEN + 4) {
8543
9aedab0f0dff QUIC: check that the packet length is of at least sample size.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8542
diff changeset
1079 return NGX_DECLINED;
9aedab0f0dff QUIC: check that the packet length is of at least sample size.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8542
diff changeset
1080 }
9aedab0f0dff QUIC: check that the packet length is of at least sample size.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8542
diff changeset
1081
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1082 sample = p + 4;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1083
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1084 /* header protection */
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1085
9171
f98636db77ef QUIC: renamed protection functions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9168
diff changeset
1086 if (ngx_quic_crypto_hp(pkt->log, ciphers.hp, secret, mask, sample)
8287
ccb9cc95ad5e Logging cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8285
diff changeset
1087 != NGX_OK)
ccb9cc95ad5e Logging cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8285
diff changeset
1088 {
8446
df29219988bc Discard short packets which could not be decrypted.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8445
diff changeset
1089 return NGX_DECLINED;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1090 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1091
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1092 pkt->flags ^= mask[0] & ngx_quic_pkt_hp_mask(pkt->flags);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1093
8643
5fdd0ef42232 QUIC: macros for manipulating header protection and reserved bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8642
diff changeset
1094 if (ngx_quic_short_pkt(pkt->flags)) {
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1095 key_phase = (pkt->flags & NGX_QUIC_PKT_KPHASE) != 0;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1096
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1097 if (key_phase != pkt->key_phase) {
8621
9c3be23ddbe7 QUIC: refactored key handling.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8609
diff changeset
1098 secret = &pkt->keys->next_key.client;
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1099 pkt->key_update = 1;
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1100 }
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1101 }
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1102
8532
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1103 lpn = *largest_pn;
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1104
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1105 pnl = (pkt->flags & 0x03) + 1;
8532
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1106 pn = ngx_quic_parse_pn(&p, pnl, &mask[1], &lpn);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1107
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1108 pkt->pn = pn;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1109
8287
ccb9cc95ad5e Logging cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8285
diff changeset
1110 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1111 "quic packet rx clearflags:%xd", pkt->flags);
8287
ccb9cc95ad5e Logging cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8285
diff changeset
1112 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
8609
f32740ddd484 QUIC: got rid of "pkt" abbreviation in logs.
Vladimir Homutov <vl@nginx.com>
parents: 8608
diff changeset
1113 "quic packet rx number:%uL len:%xi", pn, pnl);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1114
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1115 /* packet protection */
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1116
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1117 in.data = p;
8558
0f37b4ef3cd9 QUIC: keep the entire packet size in pkt->len.
Roman Arutyunyan <arut@nginx.com>
parents: 8544
diff changeset
1118 in.len = len - pnl;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1119
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1120 ad.len = p - pkt->data;
8288
ebd5c71b9f02 Got rid of memory allocation in decryption.
Vladimir Homutov <vl@nginx.com>
parents: 8287
diff changeset
1121 ad.data = pkt->plaintext;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1122
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1123 ngx_memcpy(ad.data, pkt->data, ad.len);
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1124 ad.data[0] = pkt->flags;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1125
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1126 do {
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1127 ad.data[ad.len - pnl] = pn >> (8 * (pnl - 1)) % 256;
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1128 } while (--pnl);
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1129
8319
29354c6fc5f2 TLS Key Update in QUIC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8318
diff changeset
1130 ngx_memcpy(nonce, secret->iv.data, secret->iv.len);
8310
7ac890c18f5e Fixed computing nonce by xoring all packet number bytes.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8307
diff changeset
1131 ngx_quic_compute_nonce(nonce, sizeof(nonce), pn);
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1132
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
1133 #ifdef NGX_QUIC_DEBUG_CRYPTO
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
1134 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
1135 "quic ad len:%uz %xV", ad.len, &ad);
8359
2f900ae486bc Debug cleanup.
Vladimir Homutov <vl@nginx.com>
parents: 8339
diff changeset
1136 #endif
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1137
9126
29a6c0e11f75 QUIC: a new constant for AEAD tag length.
Roman Arutyunyan <arut@nginx.com>
parents: 9080
diff changeset
1138 pkt->payload.len = in.len - NGX_QUIC_TAG_LEN;
8288
ebd5c71b9f02 Got rid of memory allocation in decryption.
Vladimir Homutov <vl@nginx.com>
parents: 8287
diff changeset
1139 pkt->payload.data = pkt->plaintext + ad.len;
ebd5c71b9f02 Got rid of memory allocation in decryption.
Vladimir Homutov <vl@nginx.com>
parents: 8287
diff changeset
1140
9172
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
1141 if (ngx_quic_crypto_open(secret, &pkt->payload, nonce, &in, &ad, pkt->log)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
1142 != NGX_OK)
4ccb0d973206 QUIC: reusing crypto contexts for packet protection.
Sergey Kandaurov <pluknet@nginx.com>
parents: 9171
diff changeset
1143 {
8446
df29219988bc Discard short packets which could not be decrypted.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8445
diff changeset
1144 return NGX_DECLINED;
8386
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1145 }
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1146
8646
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1147 if (pkt->payload.len == 0) {
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1148 /*
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1149 * RFC 9000, 12.4. Frames and Frame Types
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1150 *
8646
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1151 * An endpoint MUST treat receipt of a packet containing no
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1152 * frames as a connection error of type PROTOCOL_VIOLATION.
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1153 */
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1154 ngx_log_error(NGX_LOG_INFO, pkt->log, 0, "quic zero-length packet");
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1155 pkt->error = NGX_QUIC_ERR_PROTOCOL_VIOLATION;
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1156 return NGX_ERROR;
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1157 }
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1158
8645
ae4bffb75df8 QUIC: simplified and streamlined ngx_quic_decrypt().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8644
diff changeset
1159 if (pkt->flags & ngx_quic_pkt_rb_mask(pkt->flags)) {
8386
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1160 /*
8797
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1161 * RFC 9000, Reserved Bits
4715f3e669f1 QUIC: updated specification references.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8755
diff changeset
1162 *
8386
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1163 * An endpoint MUST treat receipt of a packet that has
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1164 * a non-zero value for these bits, after removing both
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1165 * packet and header protection, as a connection error
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1166 * of type PROTOCOL_VIOLATION.
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1167 */
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1168 ngx_log_error(NGX_LOG_INFO, pkt->log, 0,
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1169 "quic reserved bit set in packet");
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1170 pkt->error = NGX_QUIC_ERR_PROTOCOL_VIOLATION;
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1171 return NGX_ERROR;
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1172 }
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1173
8646
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1174 #if defined(NGX_QUIC_DEBUG_CRYPTO) && defined(NGX_QUIC_DEBUG_PACKETS)
8651
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
1175 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
1176 "quic packet payload len:%uz %xV",
dbad2d6d1898 QUIC: removed ngx_quic_hexdump() macro.
Vladimir Homutov <vl@nginx.com>
parents: 8646
diff changeset
1177 pkt->payload.len, &pkt->payload);
8646
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1178 #endif
4bf332873a83 QUIC: rejecting zero-length packets with PROTOCOL_VIOLATION.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8645
diff changeset
1179
8532
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1180 *largest_pn = lpn;
b13141d6d250 QUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan <arut@nginx.com>
parents: 8525
diff changeset
1181
8386
81f85c479d7e Discard packets without fixed bit or reserved bits set.
Vladimir Homutov <vl@nginx.com>
parents: 8383
diff changeset
1182 return NGX_OK;
8221
69345a26ba69 Split transport and crypto parts into separate files.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1183 }