annotate src/event/ngx_event_quic.c @ 8198:ff14b0fe9731 quic

Fixed header protection with negotiated cipher suite.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 05 Mar 2020 20:05:40 +0300
parents 75a2817808bf
children 1297dc83a6b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
2 /*
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
3 * Copyright (C) Nginx, Inc.
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
4 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
5
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
6
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7 #include <ngx_config.h>
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 #include <ngx_core.h>
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
9
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
10
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
11 #define quic_version 0xff000018
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
12
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
13 #define NGX_AES_128_GCM_SHA256 0x1301
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
14 #define NGX_AES_256_GCM_SHA384 0x1302
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
15
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
16
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
17 #if (NGX_HAVE_NONALIGNED)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
18
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
19 #define ngx_quic_parse_uint16(p) ntohs(*(uint16_t *) (p))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
20 #define ngx_quic_parse_uint32(p) ntohl(*(uint32_t *) (p))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
21
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
22 #define ngx_quic_write_uint16 ngx_quic_write_uint16_aligned
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
23 #define ngx_quic_write_uint32 ngx_quic_write_uint32_aligned
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
24
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
25 #else
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
26
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
27 #define ngx_quic_parse_uint16(p) ((p)[0] << 8 | (p)[1])
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
28 #define ngx_quic_parse_uint32(p) \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
29 ((uint32_t) (p)[0] << 24 | (p)[1] << 16 | (p)[2] << 8 | (p)[3])
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
30
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
31 #define ngx_quic_write_uint16(p, s) \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
32 ((p)[0] = (u_char) ((s) >> 8), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
33 (p)[1] = (u_char) (s), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
34 (p) + sizeof(uint16_t))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
35
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
36 #define ngx_quic_write_uint32(p, s) \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
37 ((p)[0] = (u_char) ((s) >> 24), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
38 (p)[1] = (u_char) ((s) >> 16), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
39 (p)[2] = (u_char) ((s) >> 8), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
40 (p)[3] = (u_char) (s), \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
41 (p) + sizeof(uint32_t))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
42
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
43 #endif
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
44
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
45
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
46 #define ngx_quic_write_uint16_aligned(p, s) \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
47 (*(uint16_t *) (p) = htons((uint16_t) (s)), (p) + sizeof(uint16_t))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
48
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
49 #define ngx_quic_write_uint32_aligned(p, s) \
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
50 (*(uint32_t *) (p) = htonl((uint32_t) (s)), (p) + sizeof(uint32_t))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
51
8191
7bd7c4e24951 Macro for calculating size of varint.
Vladimir Homutov <vl@nginx.com>
parents: 8190
diff changeset
52 #define ngx_quic_varint_len(value) \
7bd7c4e24951 Macro for calculating size of varint.
Vladimir Homutov <vl@nginx.com>
parents: 8190
diff changeset
53 ((value) <= 63 ? 1 : (value) <= 16383 ? 2 : (value) <= 1073741823 ? 4 : 8)
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
54
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
55
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
56 #if (NGX_DEBUG)
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
57
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
58 #define ngx_quic_hexdump(log, fmt, data, len, ...) \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
59 do { \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
60 ngx_int_t m; \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
61 u_char buf[2048]; \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
62 \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
63 if (log->log_level & NGX_LOG_DEBUG_EVENT) { \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
64 m = ngx_hex_dump(buf, (u_char *) data, ngx_min(len, 1024)) - buf; \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
65 ngx_log_debug(NGX_LOG_DEBUG_EVENT, log, 0, \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
66 "%s: " fmt " %*s%s, len: %uz", \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
67 __FUNCTION__, __VA_ARGS__, m, buf, \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
68 len < 2048 ? "" : "...", len); \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
69 } \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
70 } while (0)
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
71
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
72 #else
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
73
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
74 #define ngx_quic_hexdump(log, fmt, data, len, ...)
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
75
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
76 #endif
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
77
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
78 #define ngx_quic_hexdump0(log, fmt, data, len) \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
79 ngx_quic_hexdump(log, fmt "%s", data, len, "") \
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
80
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
81
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
82 /* 17.2. Long Header Packets */
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
83
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
84 #define NGX_QUIC_PKT_LONG 0x80
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
85
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
86 #define NGX_QUIC_PKT_INITIAL 0xc0
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
87 #define NGX_QUIC_PKT_HANDSHAKE 0xe0
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
88
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
89 /* 12.4. Frames and Frame Types */
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
90 #define NGX_QUIC_FT_PADDING 0x00
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
91 #define NGX_QUIC_FT_PING 0x01
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
92 #define NGX_QUIC_FT_ACK 0x02
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
93 #define NGX_QUIC_FT_ACK_ECN 0x03
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
94 #define NGX_QUIC_FT_RESET_STREAM 0x04
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
95 #define NGX_QUIC_FT_STOP_SENDING 0x05
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
96 #define NGX_QUIC_FT_CRYPTO 0x06
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
97 #define NGX_QUIC_FT_NEW_TOKEN 0x07
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
98 #define NGX_QUIC_FT_STREAM 0x08 // - 0x0f
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
99 #define NGX_QUIC_FT_MAX_DATA 0x10
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
100 #define NGX_QUIC_FT_MAX_STREAM_DATA 0x11
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
101 #define NGX_QUIC_FT_MAX_STREAMS 0x12
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
102 #define NGX_QUIC_FT_MAX_STREAMS2 0x13 // XXX
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
103 #define NGX_QUIC_FT_DATA_BLOCKED 0x14
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
104 #define NGX_QUIC_FT_STREAM_DATA_BLOCKED 0x15
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
105 #define NGX_QUIC_FT_STREAMS_BLOCKED 0x16
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
106 #define NGX_QUIC_FT_STREAMS_BLOCKED2 0x17 // XXX
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
107 #define NGX_QUIC_FT_NEW_CONNECTION_ID 0x18
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
108 #define NGX_QUIC_FT_RETIRE_CONNECTION_ID 0x19
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
109 #define NGX_QUIC_FT_PATH_CHALLENGE 0x1a
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
110 #define NGX_QUIC_FT_PATH_RESPONSE 0x1b
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
111 #define NGX_QUIC_FT_CONNECTION_CLOSE 0x1c
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
112 #define NGX_QUIC_FT_CONNECTION_CLOSE2 0x1d // XXX
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
113 #define NGX_QUIC_FT_HANDSHAKE_DONE 0x1e
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
114
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
115
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
116 /* TODO: real states, these are stubs */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
117 typedef enum {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
118 NGX_QUIC_ST_INITIAL,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
119 NGX_QUIC_ST_HANDSHAKE,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
120 NGX_QUIC_ST_APP_DATA
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
121 } ngx_quic_state_t;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
122
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
123
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
124 typedef struct {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
125 ngx_str_t secret;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
126 ngx_str_t key;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
127 ngx_str_t iv;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
128 ngx_str_t hp;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
129 } ngx_quic_secret_t;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
130
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
131
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
132 typedef enum ssl_encryption_level_t ngx_quic_level_t;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
133
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
134 typedef struct ngx_quic_frame_s ngx_quic_frame_t;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
135
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
136 typedef struct {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
137 ngx_uint_t pn;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
138 // ngx_uint_t nranges;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
139 // ...
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
140 } ngx_quic_ack_frame_t;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
141
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
142 typedef ngx_str_t ngx_quic_crypto_frame_t;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
143
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
144 struct ngx_quic_frame_s {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
145 ngx_uint_t type;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
146 ngx_quic_level_t level;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
147 ngx_quic_frame_t *next;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
148 union {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
149 ngx_quic_crypto_frame_t crypto;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
150 ngx_quic_ack_frame_t ack;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
151 // more frames
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
152 } u;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
153
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
154 u_char info[128]; // for debug purposes
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
155 };
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
156
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
157
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
158 struct ngx_quic_connection_s {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
159
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
160 ngx_quic_state_t state;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
161 ngx_ssl_t *ssl;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
162
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
163 ngx_quic_frame_t *frames;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
164
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
165 ngx_str_t scid;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
166 ngx_str_t dcid;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
167 ngx_str_t token;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
168
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
169 /* current packet numbers for each namespace */
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
170 ngx_uint_t initial_pn;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
171 ngx_uint_t handshake_pn;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
172 ngx_uint_t appdata_pn;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
173
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
174 ngx_quic_secret_t client_in;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
175 ngx_quic_secret_t client_hs;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
176 ngx_quic_secret_t client_ad;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
177 ngx_quic_secret_t server_in;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
178 ngx_quic_secret_t server_hs;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
179 ngx_quic_secret_t server_ad;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
180 };
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
181
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
182
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
183 typedef struct {
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
184 ngx_quic_secret_t *secret;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
185 ngx_uint_t type;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
186 ngx_uint_t *number;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
187 ngx_uint_t flags;
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
188 uint32_t version;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
189 ngx_str_t token;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
190 ngx_quic_level_t level;
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
191
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
192 /* filled in by parser */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
193 ngx_str_t buf; /* quic packet from wire */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
194 u_char *pos; /* current parser position */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
195
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
196 /* cleartext fields */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
197 ngx_str_t dcid;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
198 ngx_str_t scid;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
199
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
200 uint64_t pn;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
201
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
202 ngx_str_t payload; /* decrypted payload */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
203
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
204 } ngx_quic_header_t;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
205
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
206
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
207 static ngx_int_t ngx_quic_new_connection(ngx_connection_t *c, ngx_ssl_t *ssl,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
208 ngx_buf_t *b);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
209 static ngx_int_t ngx_quic_handshake_input(ngx_connection_t *c, ngx_buf_t *b);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
210
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
211 static int ngx_quic_set_encryption_secrets(ngx_ssl_conn_t *ssl_conn,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
212 enum ssl_encryption_level_t level, const uint8_t *read_secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
213 const uint8_t *write_secret, size_t secret_len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
214 static int ngx_quic_add_handshake_data(ngx_ssl_conn_t *ssl_conn,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
215 enum ssl_encryption_level_t level, const uint8_t *data, size_t len);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
216 static ngx_int_t ngx_quic_create_long_packet(ngx_connection_t *c,
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
217 ngx_ssl_conn_t *ssl_conn, ngx_quic_header_t *pkt, ngx_str_t *in,
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
218 ngx_str_t *res);
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
219 static ngx_int_t ngx_quic_create_short_packet(ngx_connection_t *c,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
220 ngx_ssl_conn_t *ssl_conn, ngx_quic_header_t *pkt, ngx_str_t *in,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
221 ngx_str_t *res);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
222 static int ngx_quic_flush_flight(ngx_ssl_conn_t *ssl_conn);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
223 static int ngx_quic_send_alert(ngx_ssl_conn_t *ssl_conn,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
224 enum ssl_encryption_level_t level, uint8_t alert);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
225
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
226 static ngx_int_t ngx_quic_process_long_header(ngx_connection_t *c,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
227 ngx_quic_header_t *pkt);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
228 static ngx_int_t ngx_quic_process_initial_header(ngx_connection_t *c,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
229 ngx_quic_header_t *pkt);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
230 static ngx_int_t ngx_quic_process_handshake_header(ngx_connection_t *c,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
231 ngx_quic_header_t *pkt);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
232 static ngx_int_t ngx_quic_initial_secret(ngx_connection_t *c);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
233 static ngx_int_t ngx_quic_decrypt(ngx_connection_t *c, ngx_quic_header_t *pkt);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
234
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
235 static uint64_t ngx_quic_parse_pn(u_char **pos, ngx_int_t len, u_char *mask);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
236 static uint64_t ngx_quic_parse_int(u_char **pos);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
237 static void ngx_quic_build_int(u_char **pos, uint64_t value);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
238
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
239 static ngx_int_t ngx_hkdf_extract(u_char *out_key, size_t *out_len,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
240 const EVP_MD *digest, const u_char *secret, size_t secret_len,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
241 const u_char *salt, size_t salt_len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
242 static ngx_int_t ngx_hkdf_expand(u_char *out_key, size_t out_len,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
243 const EVP_MD *digest, const u_char *prk, size_t prk_len,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
244 const u_char *info, size_t info_len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
245
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
246 static ngx_int_t ngx_quic_hkdf_expand(ngx_connection_t *c, const EVP_MD *digest,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
247 ngx_str_t *out, ngx_str_t *label, const uint8_t *prk, size_t prk_len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
248
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
249 static ngx_int_t ngx_quic_tls_open(ngx_connection_t *c,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
250 const EVP_CIPHER *cipher, ngx_quic_secret_t *s, ngx_str_t *out,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
251 u_char *nonce, ngx_str_t *in, ngx_str_t *ad);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
252 static ngx_int_t ngx_quic_tls_seal(ngx_connection_t *c,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
253 const EVP_CIPHER *cipher, ngx_quic_secret_t *s, ngx_str_t *out,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
254 u_char *nonce, ngx_str_t *in, ngx_str_t *ad);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
255
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
256 static ngx_int_t ngx_quic_tls_hp(ngx_connection_t *c, const EVP_CIPHER *cipher,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
257 ngx_quic_secret_t *s, u_char *out, u_char *in);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
258
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
259
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
260 static SSL_QUIC_METHOD quic_method = {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
261 ngx_quic_set_encryption_secrets,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
262 ngx_quic_add_handshake_data,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
263 ngx_quic_flush_flight,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
264 ngx_quic_send_alert,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
265 };
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
266
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
267
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
268 void
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
269 ngx_quic_init_ssl_methods(SSL_CTX* ctx)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
270 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
271 SSL_CTX_set_quic_method(ctx, &quic_method);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
272 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
273
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
274
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
275 ngx_int_t
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
276 ngx_quic_input(ngx_connection_t *c, ngx_ssl_t *ssl, ngx_buf_t *b)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
277 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
278 if (c->quic == NULL) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
279 return ngx_quic_new_connection(c, ssl, b); //TODO: change state by results
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
280 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
281
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
282 switch (c->quic->state) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
283 case NGX_QUIC_ST_INITIAL:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
284 case NGX_QUIC_ST_HANDSHAKE:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
285 return ngx_quic_handshake_input(c, b);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
286 default:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
287 /* application data */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
288 break;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
289 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
290
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
291 return NGX_OK;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
292 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
293
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
294 static ngx_int_t
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
295 ngx_quic_send_packet(ngx_connection_t *c, ngx_quic_connection_t *qc,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
296 ngx_quic_level_t level, ngx_str_t *payload)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
297 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
298 ngx_str_t res;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
299 ngx_quic_header_t pkt;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
300
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
301 static ngx_str_t initial_token = ngx_null_string;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
302
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
303 ngx_memzero(&pkt, sizeof(ngx_quic_header_t));
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
304 ngx_quic_hexdump0(c->log, "payload", payload->data, payload->len);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
305
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
306 pkt.level = level;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
307
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
308 if (level == ssl_encryption_initial) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
309 pkt.number = &qc->initial_pn;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
310 pkt.flags = NGX_QUIC_PKT_INITIAL;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
311 pkt.secret = &qc->server_in;
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
312 pkt.token = initial_token;
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
313
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
314 if (ngx_quic_create_long_packet(c, c->ssl->connection,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
315 &pkt, payload, &res)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
316 != NGX_OK)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
317 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
318 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
319 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
320
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
321 } else if (level == ssl_encryption_handshake) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
322 pkt.number = &qc->handshake_pn;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
323 pkt.flags = NGX_QUIC_PKT_HANDSHAKE;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
324 pkt.secret = &qc->server_hs;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
325
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
326 if (ngx_quic_create_long_packet(c, c->ssl->connection,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
327 &pkt, payload, &res)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
328 != NGX_OK)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
329 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
330 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
331 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
332
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
333 } else {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
334 pkt.number = &qc->appdata_pn;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
335 pkt.secret = &qc->server_ad;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
336
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
337 if (ngx_quic_create_short_packet(c, c->ssl->connection,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
338 &pkt, payload, &res)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
339 != NGX_OK)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
340 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
341 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
342 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
343 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
344
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
345 ngx_quic_hexdump0(c->log, "packet to send", res.data, res.len);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
346
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
347 c->send(c, res.data, res.len); // TODO: err handling
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
348
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
349 return NGX_OK;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
350 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
351
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
352
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
353 static size_t
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
354 ngx_quic_create_ack(u_char *p, ngx_quic_ack_frame_t *ack)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
355 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
356 if (p == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
357 return 5; /* minimal ACK */
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
358 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
359
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
360 ngx_quic_build_int(&p, NGX_QUIC_FT_ACK);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
361 ngx_quic_build_int(&p, ack->pn);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
362 ngx_quic_build_int(&p, 0);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
363 ngx_quic_build_int(&p, 0);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
364 ngx_quic_build_int(&p, ack->pn);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
365
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
366 return 5;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
367 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
368
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
369
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
370 static size_t
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
371 ngx_quic_create_crypto(u_char *p, ngx_quic_crypto_frame_t *crypto)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
372 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
373 u_char *start;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
374
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
375 if (p == NULL) {
8192
fb0879c65650 Fixed ngx_quic_varint_len misuse in the previous change.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8191
diff changeset
376 return 2 + ngx_quic_varint_len(crypto->len) + crypto->len;
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
377 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
378
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
379 start = p;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
380
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
381 ngx_quic_build_int(&p, NGX_QUIC_FT_CRYPTO);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
382 ngx_quic_build_int(&p, 0);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
383 ngx_quic_build_int(&p, crypto->len);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
384 p = ngx_cpymem(p, crypto->data, crypto->len);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
385
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
386 return p - start;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
387 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
388
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
389 size_t
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
390 ngx_quic_frame_len(ngx_quic_frame_t *frame)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
391 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
392 switch (frame->type) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
393 case NGX_QUIC_FT_ACK:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
394 return ngx_quic_create_ack(NULL, &frame->u.ack);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
395 case NGX_QUIC_FT_CRYPTO:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
396 return ngx_quic_create_crypto(NULL, &frame->u.crypto);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
397 default:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
398 /* BUG: unsupported frame type generated */
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
399 return 0;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
400 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
401 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
402
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
403
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
404 /* pack a group of frames [start; end) into memory p and send as single packet */
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
405 ngx_int_t
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
406 ngx_quic_frames_send(ngx_connection_t *c, ngx_quic_frame_t *start,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
407 ngx_quic_frame_t *end, size_t total)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
408 {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
409 u_char *p;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
410 ngx_str_t out;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
411 ngx_quic_frame_t *f;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
412
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
413 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
414 "sending frames %p...%p", start, end);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
415
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
416 p = ngx_pnalloc(c->pool, total);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
417 if (p == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
418 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
419 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
420
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
421 out.data = p;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
422
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
423 for (f = start; f != end; f = f->next) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
424
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
425 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "frame: %s", f->info);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
426
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
427 switch (f->type) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
428 case NGX_QUIC_FT_ACK:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
429 p += ngx_quic_create_ack(p, &f->u.ack);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
430 break;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
431
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
432 case NGX_QUIC_FT_CRYPTO:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
433 p += ngx_quic_create_crypto(p, &f->u.crypto);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
434 break;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
435
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
436 default:
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
437 /* BUG: unsupported frame type generated */
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
438 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
439 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
440 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
441
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
442 out.len = p - out.data;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
443
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
444 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
445 "packet ready: %ui bytes at level %d",
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
446 out.len, start->level);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
447
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
448 // IOVEC/sendmsg_chain ?
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
449 if (ngx_quic_send_packet(c, c->quic, start->level, &out) != NGX_OK) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
450 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
451 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
452
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
453 return NGX_OK;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
454 }
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
455
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
456 ngx_int_t
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
457 ngx_quic_output(ngx_connection_t *c)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
458 {
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
459 size_t len;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
460 ngx_uint_t lvl;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
461 ngx_quic_frame_t *f, *start;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
462 ngx_quic_connection_t *qc;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
463
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
464 qc = c->quic;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
465
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
466 if (qc->frames == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
467 return NGX_OK;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
468 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
469
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
470 lvl = qc->frames->level;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
471 start = qc->frames;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
472 f = start;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
473
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
474 do {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
475 len = 0;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
476
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
477 do {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
478 /* process same-level group of frames */
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
479
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
480 len += ngx_quic_frame_len(f);// TODO: handle overflow, max size
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
481
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
482 f = f->next;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
483 } while (f && f->level == lvl);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
484
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
485
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
486 if (ngx_quic_frames_send(c, start, f, len) != NGX_OK) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
487 return NGX_ERROR;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
488 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
489
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
490 if (f == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
491 break;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
492 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
493
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
494 lvl = f->level; // TODO: must not decrease (ever, also between calls)
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
495 start = f;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
496
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
497 } while (1);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
498
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
499 qc->frames = NULL;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
500
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
501 return NGX_OK;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
502 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
503
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
504
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
505 static int
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
506 ngx_quic_set_encryption_secrets(ngx_ssl_conn_t *ssl_conn,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
507 enum ssl_encryption_level_t level, const uint8_t *read_secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
508 const uint8_t *write_secret, size_t secret_len)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
509 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
510 ngx_uint_t i;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
511 const EVP_MD *digest;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
512 const EVP_CIPHER *cipher;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
513 ngx_connection_t *c;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
514 ngx_quic_secret_t *client, *server;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
515
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
516 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
517
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
518 ngx_quic_hexdump(c->log, "level:%d read", read_secret, secret_len, level);
8196
d447168ed13f Fixed write secret logging in set_encryption_secrets callback.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8195
diff changeset
519 ngx_quic_hexdump(c->log, "level:%d write", write_secret, secret_len, level);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
520
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
521 switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl_conn)) & 0xffff) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
522
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
523 case NGX_AES_128_GCM_SHA256:
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
524 cipher = EVP_aes_128_gcm();
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
525 digest = EVP_sha256();
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
526 break;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
527
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
528 case NGX_AES_256_GCM_SHA384:
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
529 cipher = EVP_aes_256_gcm();
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
530 digest = EVP_sha384();
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
531 break;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
532
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
533 default:
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
534 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "unexpected cipher");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
535 return 0;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
536 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
537
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
538 switch (level) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
539
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
540 case ssl_encryption_handshake:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
541 client = &c->quic->client_hs;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
542 server = &c->quic->server_hs;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
543
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
544 break;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
545
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
546 case ssl_encryption_application:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
547 client = &c->quic->client_ad;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
548 server = &c->quic->server_ad;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
549
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
550 break;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
551
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
552 default:
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
553 return 0;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
554 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
555
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
556 client->key.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
557 server->key.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
558
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
559 client->iv.len = EVP_CIPHER_iv_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
560 server->iv.len = EVP_CIPHER_iv_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
561
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
562 client->hp.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
563 server->hp.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
564
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
565 struct {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
566 ngx_str_t label;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
567 ngx_str_t *key;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
568 const uint8_t *secret;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
569 } seq[] = {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
570 { ngx_string("tls13 quic key"), &client->key, read_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
571 { ngx_string("tls13 quic iv"), &client->iv, read_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
572 { ngx_string("tls13 quic hp"), &client->hp, read_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
573 { ngx_string("tls13 quic key"), &server->key, write_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
574 { ngx_string("tls13 quic iv"), &server->iv, write_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
575 { ngx_string("tls13 quic hp"), &server->hp, write_secret },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
576 };
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
577
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
578 for (i = 0; i < (sizeof(seq) / sizeof(seq[0])); i++) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
579
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
580 if (ngx_quic_hkdf_expand(c, digest, seq[i].key, &seq[i].label,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
581 seq[i].secret, secret_len)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
582 != NGX_OK)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
583 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
584 return 0;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
585 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
586 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
587
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
588 return 1;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
589 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
590
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
591
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
592 static ngx_int_t
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
593 ngx_quic_create_long_packet(ngx_connection_t *c, ngx_ssl_conn_t *ssl_conn,
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
594 ngx_quic_header_t *pkt, ngx_str_t *payload, ngx_str_t *res)
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
595 {
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
596 u_char *p, *pnp, *nonce, *sample, *packet;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
597 ngx_str_t ad, out;
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
598 const EVP_CIPHER *cipher, *hp;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
599 ngx_quic_connection_t *qc;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
600
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
601 u_char mask[16];
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
602
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
603 qc = c->quic;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
604
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
605 out.len = payload->len + EVP_GCM_TLS_TAG_LEN;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
606
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
607 ad.data = ngx_alloc(346 /*max header*/, c->log);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
608 if (ad.data == 0) {
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
609 return NGX_ERROR;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
610 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
611
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
612 p = ad.data;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
613
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
614 *p++ = pkt->flags;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
615
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
616 p = ngx_quic_write_uint32(p, quic_version);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
617
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
618 *p++ = qc->scid.len;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
619 p = ngx_cpymem(p, qc->scid.data, qc->scid.len);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
620
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
621 *p++ = qc->dcid.len;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
622 p = ngx_cpymem(p, qc->dcid.data, qc->dcid.len);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
623
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
624 if (pkt->level == ssl_encryption_initial) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
625 ngx_quic_build_int(&p, pkt->token.len);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
626 }
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
627
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
628 ngx_quic_build_int(&p, out.len + 1); // length (inc. pnl)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
629 pnp = p;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
630
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
631 *p++ = (*pkt->number)++;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
632
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
633 ad.len = p - ad.data;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
634
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
635 ngx_quic_hexdump0(c->log, "ad", ad.data, ad.len);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
636
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
637 if (pkt->level != ssl_encryption_initial) {
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
638 switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl_conn)) & 0xffff) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
639
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
640 case NGX_AES_128_GCM_SHA256:
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
641 cipher = EVP_aes_128_gcm();
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
642 hp = EVP_aes_128_ecb();
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
643 break;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
644
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
645 case NGX_AES_256_GCM_SHA384:
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
646 cipher = EVP_aes_256_gcm();
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
647 hp = EVP_aes_256_ecb();
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
648 break;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
649
8197
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
650 default:
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
651 return NGX_ERROR;
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
652 }
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
653
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
654 } else {
75a2817808bf Initial packets are protected with AEAD_AES_128_GCM.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8196
diff changeset
655 cipher = EVP_aes_128_gcm();
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
656 hp = EVP_aes_128_ecb();
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
657 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
658
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
659 nonce = ngx_pstrdup(c->pool, &pkt->secret->iv);
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
660 if (pkt->level == ssl_encryption_handshake) {
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
661 nonce[11] ^= (*pkt->number - 1);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
662 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
663
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
664 ngx_quic_hexdump0(c->log, "server_iv", pkt->secret->iv.data, 12);
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
665 ngx_quic_hexdump0(c->log, "nonce", nonce, 12);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
666
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
667 if (ngx_quic_tls_seal(c, cipher, pkt->secret, &out, nonce, payload, &ad) != NGX_OK) {
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
668 return NGX_ERROR;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
669 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
670
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
671 sample = &out.data[3]; // pnl=0
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
672 if (ngx_quic_tls_hp(c, hp, pkt->secret, mask, sample) != NGX_OK) {
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
673 return NGX_ERROR;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
674 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
675
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
676 ngx_quic_hexdump0(c->log, "sample", sample, 16);
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
677 ngx_quic_hexdump0(c->log, "mask", mask, 16);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
678 ngx_quic_hexdump0(c->log, "hp_key", pkt->secret->hp.data, 16);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
679
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
680 // header protection, pnl = 0
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
681 ad.data[0] ^= mask[0] & 0x0f;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
682 *pnp ^= mask[1];
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
683
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
684 packet = ngx_alloc(ad.len + out.len, c->log);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
685 if (packet == 0) {
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
686 return NGX_ERROR;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
687 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
688
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
689 p = ngx_cpymem(packet, ad.data, ad.len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
690 p = ngx_cpymem(p, out.data, out.len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
691
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
692 res->data = packet;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
693 res->len = p - packet;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
694
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
695 return NGX_OK;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
696 }
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
697
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
698
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
699 static ngx_int_t
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
700 ngx_quic_create_short_packet(ngx_connection_t *c, ngx_ssl_conn_t *ssl_conn,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
701 ngx_quic_header_t *pkt, ngx_str_t *payload, ngx_str_t *res)
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
702 {
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
703 u_char *p, *pnp, *nonce, *sample, *packet;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
704 ngx_str_t ad, out;
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
705 const EVP_CIPHER *cipher, *hp;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
706 ngx_quic_connection_t *qc;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
707
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
708 u_char mask[16];
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
709
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
710 qc = c->quic;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
711
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
712 out.len = payload->len + EVP_GCM_TLS_TAG_LEN;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
713
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
714 ad.data = ngx_alloc(25 /*max header*/, c->log);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
715 if (ad.data == 0) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
716 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
717 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
718
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
719 p = ad.data;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
720
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
721 *p++ = 0x40;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
722
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
723 p = ngx_cpymem(p, qc->scid.data, qc->scid.len);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
724
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
725 pnp = p;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
726
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
727 *p++ = (*pkt->number)++;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
728
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
729 ad.len = p - ad.data;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
730
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
731 ngx_quic_hexdump0(c->log, "ad", ad.data, ad.len);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
732
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
733 switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl_conn)) & 0xffff) {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
734
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
735 case NGX_AES_128_GCM_SHA256:
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
736 cipher = EVP_aes_128_gcm();
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
737 hp = EVP_aes_128_ecb();
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
738 break;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
739
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
740 case NGX_AES_256_GCM_SHA384:
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
741 cipher = EVP_aes_256_gcm();
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
742 hp = EVP_aes_256_ecb();
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
743 break;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
744
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
745 default:
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
746 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
747 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
748
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
749 nonce = ngx_pstrdup(c->pool, &pkt->secret->iv);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
750 if (pkt->level == ssl_encryption_handshake) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
751 nonce[11] ^= (*pkt->number - 1);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
752 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
753
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
754 ngx_quic_hexdump0(c->log, "server_iv", pkt->secret->iv.data, 12);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
755 ngx_quic_hexdump0(c->log, "nonce", nonce, 12);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
756
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
757 if (ngx_quic_tls_seal(c, cipher, pkt->secret, &out, nonce, payload, &ad)
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
758 != NGX_OK)
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
759 {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
760 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
761 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
762
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
763 ngx_quic_hexdump0(c->log, "out", out.data, out.len);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
764
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
765 sample = &out.data[3]; // pnl=0
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
766 if (ngx_quic_tls_hp(c, hp, pkt->secret, mask, sample) != NGX_OK) {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
767 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
768 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
769
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
770 ngx_quic_hexdump0(c->log, "sample", sample, 16);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
771 ngx_quic_hexdump0(c->log, "mask", mask, 16);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
772 ngx_quic_hexdump0(c->log, "hp_key", pkt->secret->hp.data, 16);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
773
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
774 // header protection, pnl = 0
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
775 ad.data[0] ^= mask[0] & 0x1f;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
776 *pnp ^= mask[1];
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
777
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
778 packet = ngx_alloc(ad.len + out.len, c->log);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
779 if (packet == 0) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
780 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
781 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
782
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
783 p = ngx_cpymem(packet, ad.data, ad.len);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
784 p = ngx_cpymem(p, out.data, out.len);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
785
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
786 ngx_quic_hexdump0(c->log, "packet", packet, p - packet);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
787
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
788 res->data = packet;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
789 res->len = p - packet;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
790
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
791 return NGX_OK;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
792 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
793
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
794
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
795 static void
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
796 ngx_quic_queue_frame(ngx_quic_connection_t *qc, ngx_quic_frame_t *frame)
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
797 {
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
798 ngx_quic_frame_t *f;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
799
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
800 if (qc->frames == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
801 qc->frames = frame;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
802 return;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
803 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
804
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
805 for (f = qc->frames; f->next; f = f->next) { /* void */ }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
806
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
807 f->next = frame;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
808 }
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
809
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
810
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
811 static int
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
812 ngx_quic_add_handshake_data(ngx_ssl_conn_t *ssl_conn,
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
813 enum ssl_encryption_level_t level, const uint8_t *data, size_t len)
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
814 {
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
815 u_char *p;
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
816 ngx_quic_frame_t *frame;
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
817 ngx_connection_t *c;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
818 ngx_quic_connection_t *qc;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
819
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
820 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
821 qc = c->quic;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
822
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
823 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
824 "ngx_quic_add_handshake_data");
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
825
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
826 frame = ngx_pcalloc(c->pool, sizeof(ngx_quic_frame_t));
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
827 if (frame == NULL) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
828 return 0;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
829 }
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
830
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
831 p = ngx_pnalloc(c->pool, len);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
832 if (p == NULL) {
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
833 return 0;
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
834 }
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
835
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
836 ngx_memcpy(p, data, len);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
837
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
838 frame->level = level;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
839 frame->type = NGX_QUIC_FT_CRYPTO;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
840 frame->u.crypto.len = len;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
841 frame->u.crypto.data = p;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
842
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
843 ngx_sprintf(frame->info, "crypto, generated by SSL len=%ui level=%d", len, level);
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
844
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
845 ngx_quic_queue_frame(qc, frame);
8184
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
846
ec1f84996990 Split frame and packet generation into separate steps.
Vladimir Homutov <vl@nginx.com>
parents: 8183
diff changeset
847 if (level == ssl_encryption_initial) {
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
848 frame = ngx_pcalloc(c->pool, sizeof(ngx_quic_frame_t));
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
849 if (frame == NULL) {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
850 return 0;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
851 }
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
852 frame->level = level;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
853 frame->type = NGX_QUIC_FT_ACK;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
854 frame->u.ack.pn = 0;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
855 ngx_sprintf(frame->info, "ACK for PN=0 at initial, added manually from add_handshake_data");
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
856
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
857 ngx_quic_queue_frame(qc, frame);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
858 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
859
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
860 return 1;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
861 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
862
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
863
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
864 static int
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
865 ngx_quic_flush_flight(ngx_ssl_conn_t *ssl_conn)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
866 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
867 ngx_connection_t *c;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
868
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
869 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
870
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
871 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_quic_flush_flight()");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
872
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
873 if (ngx_quic_output(c) != NGX_OK) {
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
874 return 0;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
875 }
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
876
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
877 return 1;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
878 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
879
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
880
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
881 static int
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
882 ngx_quic_send_alert(ngx_ssl_conn_t *ssl_conn, enum ssl_encryption_level_t level,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
883 uint8_t alert)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
884 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
885 ngx_connection_t *c;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
886
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
887 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
888
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
889 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
890 "ngx_quic_send_alert(), lvl=%d, alert=%d",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
891 (int) level, (int) alert);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
892
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
893 return 1;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
894 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
895
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
896
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
897 static ngx_int_t
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
898 ngx_quic_process_long_header(ngx_connection_t *c, ngx_quic_header_t *pkt)
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
899 {
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
900 u_char *p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
901
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
902 p = pkt->buf.data;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
903
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
904 ngx_quic_hexdump0(c->log, "input", pkt->buf.data, pkt->buf.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
905
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
906 if (!(p[0] & NGX_QUIC_PKT_LONG)) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
907 ngx_log_error(NGX_LOG_INFO, c->log, 0, "not a long packet");
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
908 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
909 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
910
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
911 pkt->flags = *p++;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
912
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
913 pkt->version = ngx_quic_parse_uint32(p);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
914 p += sizeof(uint32_t);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
915
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
916 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
917 "quic flags:%xi version:%xD", pkt->flags, pkt->version);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
918
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
919 if (pkt->version != quic_version) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
920 ngx_log_error(NGX_LOG_INFO, c->log, 0, "unsupported quic version");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
921 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
922 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
923
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
924 pkt->dcid.len = *p++;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
925 pkt->dcid.data = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
926 p += pkt->dcid.len;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
927
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
928 pkt->scid.len = *p++;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
929 pkt->scid.data = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
930 p += pkt->scid.len;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
931
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
932 pkt->pos = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
933
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
934 return NGX_OK;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
935 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
936
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
937
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
938 static ngx_int_t
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
939 ngx_quic_process_initial_header(ngx_connection_t *c, ngx_quic_header_t *pkt)
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
940 {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
941 u_char *p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
942 ngx_int_t plen;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
943
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
944 p = pkt->pos;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
945
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
946 pkt->token.len = ngx_quic_parse_int(&p);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
947 pkt->token.data = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
948
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
949 p += pkt->token.len;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
950
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
951 plen = ngx_quic_parse_int(&p);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
952
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
953 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
954 "quic packet length: %d", plen);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
955
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
956 if (plen > pkt->buf.data + pkt->buf.len - p) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
957 ngx_log_error(NGX_LOG_INFO, c->log, 0, "truncated initial packet");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
958 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
959 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
960
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
961 pkt->pos = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
962 pkt->buf.len = plen;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
963
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
964 ngx_quic_hexdump0(c->log, "DCID", pkt->dcid.data, pkt->dcid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
965 ngx_quic_hexdump0(c->log, "SCID", pkt->scid.data, pkt->scid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
966 ngx_quic_hexdump0(c->log, "token", pkt->token.data, pkt->token.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
967
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
968 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
969 "quic packet length: %d", plen);
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
970
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
971 return NGX_OK;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
972 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
973
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
974 static ngx_int_t
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
975 ngx_quic_process_handshake_header(ngx_connection_t *c, ngx_quic_header_t *pkt)
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
976 {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
977 u_char *p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
978 ngx_int_t plen;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
979
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
980 p = pkt->pos;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
981
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
982 plen = ngx_quic_parse_int(&p);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
983
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
984 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
985 "quic packet length: %d", plen);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
986
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
987 if (plen > pkt->buf.data + pkt->buf.len - p) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
988 ngx_log_error(NGX_LOG_INFO, c->log, 0, "truncated handshake packet");
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
989 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
990 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
991
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
992 pkt->pos = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
993 pkt->buf.len = plen;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
994
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
995 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
996 "quic packet length: %d", plen);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
997
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
998 return NGX_OK;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
999 }
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1000
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1001
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1002 static ngx_int_t
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1003 ngx_quic_initial_secret(ngx_connection_t *c)
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1004 {
8194
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1005 size_t is_len;
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1006 uint8_t is[SHA256_DIGEST_LENGTH];
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1007 ngx_uint_t i;
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1008 const EVP_MD *digest;
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1009 const EVP_CIPHER *cipher;
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1010 ngx_quic_connection_t *qc;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1011
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1012 static const uint8_t salt[20] =
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1013 "\xc3\xee\xf7\x12\xc7\x2e\xbb\x5a\x11\xa7"
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1014 "\xd2\x43\x2b\xb4\x63\x65\xbe\xf9\xf5\x02";
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1015
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1016 /* AEAD_AES_128_GCM prior to handshake, quic-tls-23#section-5.3 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1017
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1018 cipher = EVP_aes_128_gcm();
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1019 digest = EVP_sha256();
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1020
8194
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1021 qc = c->quic;
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1022
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1023 if (ngx_hkdf_extract(is, &is_len, digest, qc->dcid.data, qc->dcid.len,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1024 salt, sizeof(salt))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1025 != NGX_OK)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1026 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1027 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1028 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1029
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1030 ngx_str_t iss = {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1031 .data = is,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1032 .len = is_len
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1033 };
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1034
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1035 ngx_quic_hexdump0(c->log, "salt", salt, sizeof(salt));
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1036 ngx_quic_hexdump0(c->log, "initial secret", is, is_len);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1037
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1038 /* draft-ietf-quic-tls-23#section-5.2 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1039 qc->client_in.secret.len = SHA256_DIGEST_LENGTH;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1040 qc->server_in.secret.len = SHA256_DIGEST_LENGTH;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1041
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1042 qc->client_in.key.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1043 qc->server_in.key.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1044
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1045 qc->client_in.hp.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1046 qc->server_in.hp.len = EVP_CIPHER_key_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1047
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1048 qc->client_in.iv.len = EVP_CIPHER_iv_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1049 qc->server_in.iv.len = EVP_CIPHER_iv_length(cipher);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1050
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1051 struct {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1052 ngx_str_t label;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1053 ngx_str_t *key;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1054 ngx_str_t *prk;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1055 } seq[] = {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1056
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1057 /* draft-ietf-quic-tls-23#section-5.2 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1058 { ngx_string("tls13 client in"), &qc->client_in.secret, &iss },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1059 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1060 ngx_string("tls13 quic key"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1061 &qc->client_in.key,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1062 &qc->client_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1063 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1064 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1065 ngx_string("tls13 quic iv"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1066 &qc->client_in.iv,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1067 &qc->client_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1068 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1069 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1070 /* AEAD_AES_128_GCM prior to handshake, quic-tls-23#section-5.4.1 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1071 ngx_string("tls13 quic hp"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1072 &qc->client_in.hp,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1073 &qc->client_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1074 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1075 { ngx_string("tls13 server in"), &qc->server_in.secret, &iss },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1076 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1077 /* AEAD_AES_128_GCM prior to handshake, quic-tls-23#section-5.3 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1078 ngx_string("tls13 quic key"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1079 &qc->server_in.key,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1080 &qc->server_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1081 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1082 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1083 ngx_string("tls13 quic iv"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1084 &qc->server_in.iv,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1085 &qc->server_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1086 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1087 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1088 /* AEAD_AES_128_GCM prior to handshake, quic-tls-23#section-5.4.1 */
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1089 ngx_string("tls13 quic hp"),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1090 &qc->server_in.hp,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1091 &qc->server_in.secret,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1092 },
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1093
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1094 };
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1095
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1096 for (i = 0; i < (sizeof(seq) / sizeof(seq[0])); i++) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1097
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1098 if (ngx_quic_hkdf_expand(c, digest, seq[i].key, &seq[i].label,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1099 seq[i].prk->data, seq[i].prk->len)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1100 != NGX_OK)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1101 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1102 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1103 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1104 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1105
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1106 return NGX_OK;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1107 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1108
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1109
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1110 static ngx_int_t
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1111 ngx_quic_decrypt(ngx_connection_t *c, ngx_quic_header_t *pkt)
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1112 {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1113 u_char clearflags, *p, *sample;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1114 uint8_t *nonce;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1115 uint64_t pn;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1116 ngx_int_t pnl, rc;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1117 ngx_str_t in, ad;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1118
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1119 const EVP_CIPHER *cipher, *hp;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1120
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1121 uint8_t mask[16];
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1122
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1123 if (c->ssl) {
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1124 switch (SSL_CIPHER_get_id(SSL_get_current_cipher(c->ssl->connection)) & 0xffff) {
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1125
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1126 case NGX_AES_128_GCM_SHA256:
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1127 cipher = EVP_aes_128_gcm();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1128 hp = EVP_aes_128_ecb();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1129 break;
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1130 case NGX_AES_256_GCM_SHA384:
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1131 cipher = EVP_aes_256_gcm();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1132 hp = EVP_aes_256_ecb();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1133 break;
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1134 default:
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1135 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "unexpected cipher");
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1136 return NGX_ERROR;
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1137 }
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1138
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1139 } else {
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1140 /* initial packets */
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1141 cipher = EVP_aes_128_gcm();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1142 hp = EVP_aes_128_ecb();
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1143 }
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1144
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1145 p = pkt->pos;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1146
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1147 /* draft-ietf-quic-tls-23#section-5.4.2:
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1148 * the Packet Number field is assumed to be 4 bytes long
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1149 * draft-ietf-quic-tls-23#section-5.4.[34]:
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1150 * AES-Based and ChaCha20-Based header protections sample 16 bytes
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1151 */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1152
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1153 sample = p + 4;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1154
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1155 ngx_quic_hexdump0(c->log, "sample", sample, 16);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1156
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1157 /* header protection */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1158
8198
ff14b0fe9731 Fixed header protection with negotiated cipher suite.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8197
diff changeset
1159 if (ngx_quic_tls_hp(c, hp, pkt->secret, mask, sample) != NGX_OK) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1160 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1161 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1162
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1163 clearflags = pkt->flags ^ (mask[0] & 0x0f);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1164 pnl = (clearflags & 0x03) + 1;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1165 pn = ngx_quic_parse_pn(&p, pnl, &mask[1]);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1166
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1167 pkt->pn = pn;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1168
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1169 ngx_quic_hexdump0(c->log, "mask", mask, 5);
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1170 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1171 "quic clear flags: %xi", clearflags);
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1172 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1173 "quic packet number: %uL, len: %xi", pn, pnl);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1174
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1175 /* packet protection */
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1176
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1177 in.data = p;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1178 in.len = pkt->buf.len - pnl;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1179
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1180 ad.len = p - pkt->buf.data;;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1181 ad.data = ngx_pnalloc(c->pool, ad.len);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1182 if (ad.data == NULL) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1183 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1184 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1185
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1186 ngx_memcpy(ad.data, pkt->buf.data, ad.len);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1187 ad.data[0] = clearflags;
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1188 ad.data[ad.len - pnl] = (u_char) pn;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1189
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1190 nonce = ngx_pstrdup(c->pool, &pkt->secret->iv);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1191 nonce[11] ^= pn;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1192
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1193 ngx_quic_hexdump0(c->log, "nonce", nonce, 12);
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1194 ngx_quic_hexdump0(c->log, "ad", ad.data, ad.len);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1195
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1196 rc = ngx_quic_tls_open(c, cipher, pkt->secret, &pkt->payload,
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1197 nonce, &in, &ad);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1198
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1199 ngx_quic_hexdump0(c->log, "packet payload",
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1200 pkt->payload.data, pkt->payload.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1201
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1202 return rc;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1203 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1204
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1205
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1206 static ngx_int_t
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1207 ngx_quic_new_connection(ngx_connection_t *c, ngx_ssl_t *ssl, ngx_buf_t *b)
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1208 {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1209 int n, sslerr;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1210 ngx_str_t out;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1211 ngx_quic_connection_t *qc;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1212
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1213 ngx_quic_header_t pkt = { 0 };
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1214
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1215 pkt.buf.data = b->start;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1216 pkt.buf.len = b->last - b->pos;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1217
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1218 if (ngx_buf_size(b) < 1200) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1219 ngx_log_error(NGX_LOG_INFO, c->log, 0, "too small UDP datagram");
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1220 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1221 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1222
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1223 if (ngx_quic_process_long_header(c, &pkt) != NGX_OK) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1224 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1225 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1226
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1227 if ((pkt.flags & 0xf0) != NGX_QUIC_PKT_INITIAL) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1228 ngx_log_error(NGX_LOG_INFO, c->log, 0,
8195
2d7202fc05d1 Fixed format specifiers.
Vladimir Homutov <vl@nginx.com>
parents: 8194
diff changeset
1229 "invalid initial packet: 0x%xi", pkt.flags);
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1230 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1231 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1232
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1233 if (ngx_quic_process_initial_header(c, &pkt) != NGX_OK) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1234 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1235 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1236
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1237 qc = ngx_pcalloc(c->pool, sizeof(ngx_quic_connection_t));
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1238 if (qc == NULL) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1239 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1240 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1241
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1242 c->quic = qc;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1243
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1244 qc->dcid.len = pkt.dcid.len;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1245 qc->dcid.data = ngx_pnalloc(c->pool, pkt.dcid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1246 if (qc->dcid.data == NULL) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1247 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1248 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1249 ngx_memcpy(qc->dcid.data, pkt.dcid.data, qc->dcid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1250
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1251 qc->scid.len = pkt.scid.len;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1252 qc->scid.data = ngx_pnalloc(c->pool, qc->scid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1253 if (qc->scid.data == NULL) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1254 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1255 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1256 ngx_memcpy(qc->scid.data, pkt.scid.data, qc->scid.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1257
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1258 qc->token.len = pkt.token.len;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1259 qc->token.data = ngx_pnalloc(c->pool, qc->token.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1260 if (qc->token.data == NULL) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1261 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1262 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1263 ngx_memcpy(qc->token.data, pkt.token.data, qc->token.len);
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1264
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1265
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1266 if (ngx_quic_initial_secret(c) != NGX_OK) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1267 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1268 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1269
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1270 pkt.secret = &qc->client_in;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1271
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1272 if (ngx_quic_decrypt(c, &pkt) != NGX_OK) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1273 return NGX_ERROR;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1274 }
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1275
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1276 out = pkt.payload;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1277
8194
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1278 if (out.data[0] != NGX_QUIC_FT_CRYPTO) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1279 ngx_log_error(NGX_LOG_INFO, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1280 "unexpected frame in initial packet");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1281 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1282 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1283
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1284 if (out.data[1] != 0x00) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1285 ngx_log_error(NGX_LOG_INFO, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1286 "unexpected CRYPTO offset in initial packet");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1287 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1288 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1289
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1290 uint8_t *crypto = &out.data[2];
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1291 uint64_t crypto_len = ngx_quic_parse_int(&crypto);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1292
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1293 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1294 "quic initial packet CRYPTO length: %uL pp:%p:%p",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1295 crypto_len, out.data, crypto);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1296
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1297 if (ngx_ssl_create_connection(ssl, c, NGX_SSL_BUFFER) != NGX_OK) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1298 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1299 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1300
8187
de5917df2c30 Adjusted transport parameters stub for active_connection_id_limit.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8186
diff changeset
1301 /* STUB: initial_max_streams_uni=3, active_connection_id_limit=5 */
de5917df2c30 Adjusted transport parameters stub for active_connection_id_limit.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8186
diff changeset
1302 static const uint8_t params[12] = "\x00\x0a\x00\x0e\x00\x01\x05\x00\x09\x00\x01\x03";
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1303
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1304 if (SSL_set_quic_transport_params(c->ssl->connection, params,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1305 sizeof(params)) == 0)
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1306 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1307 ngx_log_error(NGX_LOG_INFO, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1308 "SSL_set_quic_transport_params() failed");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1309 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1310 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1311
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1312 n = SSL_do_handshake(c->ssl->connection);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1313
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1314 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1315
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1316 if (n == -1) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1317 sslerr = SSL_get_error(c->ssl->connection, n);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1318
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1319 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1320 sslerr);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1321 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1322
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1323 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1324 "SSL_quic_read_level: %d, SSL_quic_write_level: %d",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1325 (int) SSL_quic_read_level(c->ssl->connection),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1326 (int) SSL_quic_write_level(c->ssl->connection));
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1327
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1328 if (!SSL_provide_quic_data(c->ssl->connection,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1329 SSL_quic_read_level(c->ssl->connection),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1330 crypto, crypto_len))
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1331 {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1332 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1333 "SSL_provide_quic_data() failed");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1334 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1335 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1336
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1337 n = SSL_do_handshake(c->ssl->connection);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1338
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1339 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1340
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1341 if (n == -1) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1342 sslerr = SSL_get_error(c->ssl->connection, n);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1343
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1344 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1345 sslerr);
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1346
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1347 if (sslerr == SSL_ERROR_SSL) {
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1348 ngx_ssl_error(NGX_LOG_ERR, c->log, 0, "SSL_do_handshake() failed");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1349 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1350 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1351
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1352 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1353 "SSL_quic_read_level: %d, SSL_quic_write_level: %d",
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1354 (int) SSL_quic_read_level(c->ssl->connection),
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1355 (int) SSL_quic_write_level(c->ssl->connection));
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1356
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1357 return NGX_OK;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1358 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1359
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1360
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1361 static ngx_int_t
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1362 ngx_quic_handshake_input(ngx_connection_t *c, ngx_buf_t *b)
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1363 {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1364 int sslerr;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1365 ssize_t n;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1366 ngx_str_t out;
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1367 ngx_ssl_conn_t *ssl_conn;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1368 ngx_quic_connection_t *qc;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1369
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1370 ngx_quic_header_t pkt = { 0 };
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1371
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1372 qc = c->quic;
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1373 ssl_conn = c->ssl->connection;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1374
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1375 pkt.buf.data = b->start;
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1376 pkt.buf.len = b->last - b->pos;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1377
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1378 /* extract cleartext data into pkt */
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1379 if (ngx_quic_process_long_header(c, &pkt) != NGX_OK) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1380 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1381 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1382
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1383 if (pkt.dcid.len != qc->dcid.len) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1384 ngx_log_error(NGX_LOG_INFO, c->log, 0, "unexpected quic dcidl");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1385 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1386 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1387
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1388 if (ngx_memcmp(pkt.dcid.data, qc->dcid.data, qc->dcid.len) != 0) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1389 ngx_log_error(NGX_LOG_INFO, c->log, 0, "unexpected quic dcid");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1390 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1391 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1392
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1393 if (pkt.scid.len != qc->scid.len) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1394 ngx_log_error(NGX_LOG_INFO, c->log, 0, "unexpected quic scidl");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1395 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1396 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1397
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1398 if (ngx_memcmp(pkt.scid.data, qc->scid.data, qc->scid.len) != 0) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1399 ngx_log_error(NGX_LOG_INFO, c->log, 0, "unexpected quic scid");
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1400 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1401 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1402
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1403 if ((pkt.flags & 0xf0) != NGX_QUIC_PKT_HANDSHAKE) {
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1404 ngx_log_error(NGX_LOG_INFO, c->log, 0,
8195
2d7202fc05d1 Fixed format specifiers.
Vladimir Homutov <vl@nginx.com>
parents: 8194
diff changeset
1405 "invalid packet type: 0x%xi", pkt.flags);
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1406 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1407 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1408
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1409 if (ngx_quic_process_handshake_header(c, &pkt) != NGX_OK) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1410 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1411 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1412
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1413 pkt.secret = &qc->client_hs;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1414
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1415 if (ngx_quic_decrypt(c, &pkt) != NGX_OK) {
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1416 return NGX_ERROR;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1417 }
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1418
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1419 out = pkt.payload;
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1420
8194
Vladimir Homutov <vl@nginx.com>
parents: 8193
diff changeset
1421 if (out.data[0] != NGX_QUIC_FT_CRYPTO) {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1422 ngx_log_error(NGX_LOG_INFO, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1423 "non-CRYPTO frame in HS packet, skipping");
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1424 return NGX_OK;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1425 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1426
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1427 if (out.data[1] != 0x00) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1428 ngx_log_error(NGX_LOG_INFO, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1429 "not yet supported CRYPTO offset in initial packet");
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1430 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1431 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1432
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1433 uint8_t *crypto = &out.data[2];
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1434 uint64_t crypto_len = ngx_quic_parse_int(&crypto);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1435
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1436 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1437 "quic Handshake packet CRYPTO length: %uL pp:%p:%p",
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1438 crypto_len, out.data, crypto);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1439
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1440 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1441 "SSL_quic_read_level: %d, SSL_quic_write_level: %d",
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1442 (int) SSL_quic_read_level(ssl_conn),
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1443 (int) SSL_quic_write_level(ssl_conn));
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1444
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1445 if (!SSL_provide_quic_data(ssl_conn, SSL_quic_read_level(ssl_conn),
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1446 crypto, crypto_len))
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1447 {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1448 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1449 "SSL_provide_quic_data() failed");
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1450 return NGX_ERROR;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1451 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1452
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1453 n = SSL_do_handshake(ssl_conn);
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1454
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1455 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1456
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1457 if (n == -1) {
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1458 sslerr = SSL_get_error(ssl_conn, n);
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1459
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1460 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d",
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1461 sslerr);
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1462
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1463 if (sslerr == SSL_ERROR_SSL) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1464 ngx_ssl_error(NGX_LOG_ERR, c->log, 0, "SSL_do_handshake() failed");
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1465 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1466 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1467
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1468 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1469 "SSL_quic_read_level: %d, SSL_quic_write_level: %d",
8188
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1470 (int) SSL_quic_read_level(ssl_conn),
bf555b94e387 Using cached ssl_conn in ngx_quic_handshake_input(), NFC.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8187
diff changeset
1471 (int) SSL_quic_write_level(ssl_conn));
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1472
8189
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
1473 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
1474 "quic ssl cipher: %s", SSL_get_cipher(ssl_conn));
80a07843c711 Using SSL cipher suite id to obtain cipher/digest, part 1.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8188
diff changeset
1475
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1476 // ACK Client Finished
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1477
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1478 ngx_quic_frame_t *frame;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1479
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1480 frame = ngx_pcalloc(c->pool, sizeof(ngx_quic_frame_t));
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1481 if (frame == NULL) {
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1482 return 0;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1483 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1484
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1485 frame->level = ssl_encryption_handshake;
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1486 frame->type = NGX_QUIC_FT_ACK;
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1487 frame->u.ack.pn = pkt.pn;
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1488
8193
4355efde26d8 Added functions to decrypt long packets.
Vladimir Homutov <vl@nginx.com>
parents: 8192
diff changeset
1489 ngx_sprintf(frame->info, "ACK for PN=%d at handshake level, in respond to client finished", pkt.pn);
8186
0a2683df5f11 Implemented improved version of quic_output().
Vladimir Homutov <vl@nginx.com>
parents: 8185
diff changeset
1490 ngx_quic_queue_frame(qc, frame);
8185
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1491
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1492 if (ngx_quic_output(c) != NGX_OK) {
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1493 return 0;
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1494 }
6a76d9657772 QUIC handshake final bits.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8184
diff changeset
1495
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1496 return NGX_OK;
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1497 }
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1498
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1499
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1500 uint64_t
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1501 ngx_quic_parse_int(u_char **pos)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1502 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1503 u_char *p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1504 uint64_t value;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1505 ngx_uint_t len;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1506
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1507 p = *pos;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1508 len = 1 << ((*p & 0xc0) >> 6);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1509 value = *p++ & 0x3f;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1510
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1511 while (--len) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1512 value = (value << 8) + *p++;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1513 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1514
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1515 *pos = p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1516 return value;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1517 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1518
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1519
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1520 void
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1521 ngx_quic_build_int(u_char **pos, uint64_t value)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1522 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1523 u_char *p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1524 ngx_uint_t len;//, len2;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1525
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1526 p = *pos;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1527 len = 0;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1528
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1529 while (value >> ((1 << len) * 8 - 2)) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1530 len++;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1531 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1532
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1533 *p = len << 6;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1534
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1535 // len2 =
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1536 len = (1 << len);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1537 len--;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1538 *p |= value >> (len * 8);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1539 p++;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1540
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1541 while (len) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1542 *p++ = value >> ((len-- - 1) * 8);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1543 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1544
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1545 *pos = p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1546 // return len2;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1547 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1548
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1549
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1550 static uint64_t
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1551 ngx_quic_parse_pn(u_char **pos, ngx_int_t len, u_char *mask)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1552 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1553 u_char *p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1554 uint64_t value;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1555
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1556 p = *pos;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1557 value = *p++ ^ *mask++;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1558
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1559 while (--len) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1560 value = (value << 8) + (*p++ ^ *mask++);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1561 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1562
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1563 *pos = p;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1564 return value;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1565 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1566
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1567
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1568 static ngx_int_t
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1569 ngx_hkdf_extract(u_char *out_key, size_t *out_len, const EVP_MD *digest,
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1570 const u_char *secret, size_t secret_len, const u_char *salt,
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1571 size_t salt_len)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1572 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1573 #ifdef OPENSSL_IS_BORINGSSL
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1574 if (HKDF_extract(out_key, out_len, digest, secret, secret_len, salt,
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1575 salt_len)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1576 == 0)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1577 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1578 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1579 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1580 #else
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1581
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1582 EVP_PKEY_CTX *pctx;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1583
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1584 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1585
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1586 if (EVP_PKEY_derive_init(pctx) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1587 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1588 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1589
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1590 if (EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1591 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1592 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1593
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1594 if (EVP_PKEY_CTX_set_hkdf_md(pctx, digest) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1595 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1596 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1597
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1598 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, secret_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1599 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1600 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1601
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1602 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, salt_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1603 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1604 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1605
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1606 if (EVP_PKEY_derive(pctx, out_key, out_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1607 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1608 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1609
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1610 #endif
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1611
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1612 return NGX_OK;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1613 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1614
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1615
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1616 static ngx_int_t
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1617 ngx_quic_hkdf_expand(ngx_connection_t *c, const EVP_MD *digest, ngx_str_t *out,
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1618 ngx_str_t *label, const uint8_t *prk, size_t prk_len)
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1619 {
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1620 uint8_t *p;
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1621 size_t info_len;
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1622 uint8_t info[20];
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1623
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1624 out->data = ngx_pnalloc(c->pool, out->len);
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1625 if (out->data == NULL) {
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1626 return NGX_ERROR;
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1627 }
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1628
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1629 info_len = 2 + 1 + label->len + 1;
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1630
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1631 info[0] = 0;
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1632 info[1] = out->len;
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1633 info[2] = label->len;
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1634 p = ngx_cpymem(&info[3], label->data, label->len);
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1635 *p = '\0';
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1636
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1637 if (ngx_hkdf_expand(out->data, out->len, digest,
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1638 prk, prk_len, info, info_len)
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1639 != NGX_OK)
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1640 {
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1641 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1642 "ngx_hkdf_expand(%V) failed", label);
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1643 return NGX_ERROR;
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1644 }
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1645
8183
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1646 ngx_quic_hexdump(c->log, "%V info", info, info_len, label);
6091506af0f7 Aded the "ngx_quic_hexdump" macro.
Vladimir Homutov <vl@nginx.com>
parents: 8182
diff changeset
1647 ngx_quic_hexdump(c->log, "%V key", out->data, out->len, label);
8179
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1648
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1649 return NGX_OK;
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1650 }
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1651
7ee1ada04c8a Generic function for HKDF expansion.
Vladimir Homutov <vl@nginx.com>
parents: 8178
diff changeset
1652
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1653 static ngx_int_t
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1654 ngx_hkdf_expand(u_char *out_key, size_t out_len, const EVP_MD *digest,
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1655 const uint8_t *prk, size_t prk_len, const u_char *info, size_t info_len)
8171
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1656 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1657 #ifdef OPENSSL_IS_BORINGSSL
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1658 if (HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1659 == 0)
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1660 {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1661 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1662 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1663 #else
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1664
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1665 EVP_PKEY_CTX *pctx;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1666
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1667 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1668
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1669 if (EVP_PKEY_derive_init(pctx) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1670 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1671 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1672
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1673 if (EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1674 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1675 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1676
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1677 if (EVP_PKEY_CTX_set_hkdf_md(pctx, digest) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1678 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1679 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1680
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1681 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, prk, prk_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1682 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1683 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1684
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1685 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, info_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1686 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1687 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1688
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1689 if (EVP_PKEY_derive(pctx, out_key, &out_len) <= 0) {
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1690 return NGX_ERROR;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1691 }
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1692
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1693 #endif
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1694
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1695 return NGX_OK;
4daf03d2bd0a OpenSSL compatibility.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1696 }
8177
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1697
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1698
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1699 static ngx_int_t
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1700 ngx_quic_tls_open(ngx_connection_t *c, const EVP_CIPHER *cipher,
8177
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1701 ngx_quic_secret_t *s, ngx_str_t *out, u_char *nonce, ngx_str_t *in,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1702 ngx_str_t *ad)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1703 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1704 out->len = in->len - EVP_GCM_TLS_TAG_LEN;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1705 out->data = ngx_pnalloc(c->pool, out->len);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1706 if (out->data == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1707 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1708 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1709
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1710 #ifdef OPENSSL_IS_BORINGSSLL
8177
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1711 EVP_AEAD_CTX *ctx;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1712
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1713 ctx = EVP_AEAD_CTX_new(cipher, s->key.data, s->key.len,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1714 EVP_AEAD_DEFAULT_TAG_LENGTH);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1715 if (ctx == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1716 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_AEAD_CTX_new() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1717 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1718 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1719
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1720 if (EVP_AEAD_CTX_open(ctx, out->data, &out->len, out->len, nonce, s->iv.len,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1721 in->data, in->len, ad->data, ad->len)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1722 != 1)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1723 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1724 EVP_AEAD_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1725 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_AEAD_CTX_open() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1726 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1727 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1728
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1729 EVP_AEAD_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1730 #else
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1731 int len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1732 u_char *tag;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1733 EVP_CIPHER_CTX *ctx;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1734
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1735 ctx = EVP_CIPHER_CTX_new();
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1736 if (ctx == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1737 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_CIPHER_CTX_new() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1738 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1739 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1740
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1741 if (EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1742 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1743 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_DecryptInit_ex() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1744 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1745 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1746
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1747 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, s->iv.len, NULL)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1748 == 0)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1749 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1750 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1751 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1752 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_IVLEN) failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1753 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1754 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1755
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1756 if (EVP_DecryptInit_ex(ctx, NULL, NULL, s->key.data, nonce) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1757 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1758 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_DecryptInit_ex() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1759 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1760 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1761
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1762 if (EVP_DecryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1763 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1764 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_DecryptUpdate() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1765 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1766 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1767
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1768 if (EVP_DecryptUpdate(ctx, out->data, &len, in->data,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1769 in->len - EVP_GCM_TLS_TAG_LEN)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1770 != 1)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1771 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1772 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1773 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_DecryptUpdate() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1774 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1775 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1776
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1777 out->len = len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1778 tag = in->data + in->len - EVP_GCM_TLS_TAG_LEN;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1779
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1780 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, EVP_GCM_TLS_TAG_LEN, tag)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1781 == 0)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1782 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1783 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1784 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1785 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_TAG) failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1786 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1787 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1788
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1789 if (EVP_DecryptFinal_ex(ctx, out->data + len, &len) <= 0) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1790 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1791 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_DecryptFinal_ex failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1792 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1793 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1794
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1795 out->len += len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1796
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1797 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1798 #endif
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1799
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1800 return NGX_OK;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1801 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1802
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1803
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1804 static ngx_int_t
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1805 ngx_quic_tls_seal(ngx_connection_t *c, const EVP_CIPHER *cipher,
8177
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1806 ngx_quic_secret_t *s, ngx_str_t *out, u_char *nonce, ngx_str_t *in,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1807 ngx_str_t *ad)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1808 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1809 out->len = in->len + EVP_GCM_TLS_TAG_LEN;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1810 out->data = ngx_pnalloc(c->pool, out->len);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1811 if (out->data == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1812 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1813 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1814
8180
01dc595de244 Cleanup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 8179
diff changeset
1815 #ifdef OPENSSL_IS_BORINGSSLL
8177
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1816 EVP_AEAD_CTX *ctx;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1817
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1818 ctx = EVP_AEAD_CTX_new(cipher, s->key.data, s->key.len,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1819 EVP_AEAD_DEFAULT_TAG_LENGTH);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1820 if (ctx == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1821 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_AEAD_CTX_new() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1822 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1823 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1824
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1825 if (EVP_AEAD_CTX_seal(ctx, out->data, &out->len, out->len, nonce, s->iv.len,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1826 in->data, in->len, ad->data, ad->len)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1827 != 1)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1828 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1829 EVP_AEAD_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1830 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_AEAD_CTX_seal() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1831 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1832 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1833
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1834 EVP_AEAD_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1835 #else
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1836 int len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1837 EVP_CIPHER_CTX *ctx;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1838
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1839 ctx = EVP_CIPHER_CTX_new();
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1840 if (ctx == NULL) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1841 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_CIPHER_CTX_new() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1842 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1843 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1844
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1845 if (EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1846 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1847 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptInit_ex() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1848 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1849 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1850
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1851 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, s->iv.len, NULL)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1852 == 0)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1853 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1854 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1855 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1856 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_IVLEN) failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1857 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1858 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1859
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1860 if (EVP_EncryptInit_ex(ctx, NULL, NULL, s->key.data, nonce) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1861 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1862 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptInit_ex() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1863 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1864 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1865
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1866 if (EVP_EncryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1867 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1868 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptUpdate() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1869 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1870 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1871
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1872 if (EVP_EncryptUpdate(ctx, out->data, &len, in->data, in->len) != 1) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1873 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1874 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptUpdate() failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1875 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1876 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1877
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1878 out->len = len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1879
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1880 if (EVP_EncryptFinal_ex(ctx, out->data + out->len, &len) <= 0) {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1881 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1882 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptFinal_ex failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1883 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1884 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1885
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1886 out->len += len;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1887
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1888 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, EVP_GCM_TLS_TAG_LEN,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1889 out->data + in->len)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1890 == 0)
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1891 {
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1892 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1893 ngx_ssl_error(NGX_LOG_INFO, c->log, 0,
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1894 "EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_GET_TAG) failed");
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1895 return NGX_ERROR;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1896 }
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1897
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1898 EVP_CIPHER_CTX_free(ctx);
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1899
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1900 out->len += EVP_GCM_TLS_TAG_LEN;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1901 #endif
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1902
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1903 return NGX_OK;
76e29ff31cd3 AEAD routines, introduced ngx_quic_tls_open()/ngx_quic_tls_seal().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8171
diff changeset
1904 }
8178
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1905
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1906
8182
b28ea685a56e Moved all QUIC code into ngx_event_quic.c
Vladimir Homutov <vl@nginx.com>
parents: 8180
diff changeset
1907 static ngx_int_t
8178
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1908 ngx_quic_tls_hp(ngx_connection_t *c, const EVP_CIPHER *cipher,
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1909 ngx_quic_secret_t *s, u_char *out, u_char *in)
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1910 {
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1911 int outlen;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1912 EVP_CIPHER_CTX *ctx;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1913
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1914 ctx = EVP_CIPHER_CTX_new();
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1915 if (ctx == NULL) {
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1916 return NGX_ERROR;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1917 }
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1918
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1919 if (EVP_EncryptInit_ex(ctx, cipher, NULL, s->hp.data, NULL) != 1) {
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1920 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptInit_ex() failed");
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1921 goto failed;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1922 }
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1923
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1924 if (!EVP_EncryptUpdate(ctx, out, &outlen, in, 16)) {
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1925 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, "EVP_EncryptUpdate() failed");
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1926 goto failed;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1927 }
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1928
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1929 EVP_CIPHER_CTX_free(ctx);
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1930
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1931 return NGX_OK;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1932
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1933 failed:
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1934
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1935 EVP_CIPHER_CTX_free(ctx);
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1936
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1937 return NGX_ERROR;
a9ff4392ecde QUIC header protection routines, introduced ngx_quic_tls_hp().
Sergey Kandaurov <pluknet@nginx.com>
parents: 8177
diff changeset
1938 }