/src/picotls/lib/picotls.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #ifdef _WINDOWS |
23 | | #include "wincompat.h" |
24 | | #endif |
25 | | #include <assert.h> |
26 | | #include <stdarg.h> |
27 | | #include <stddef.h> |
28 | | #include <stdio.h> |
29 | | #include <stdlib.h> |
30 | | #include <string.h> |
31 | | #ifndef _WINDOWS |
32 | | #include <errno.h> |
33 | | #include <pthread.h> |
34 | | #include <unistd.h> |
35 | | #include <sys/socket.h> |
36 | | #include <arpa/inet.h> |
37 | | #include <netinet/in.h> |
38 | | #include <sys/time.h> |
39 | | #include <sys/types.h> |
40 | | #endif |
41 | | #ifdef __linux__ |
42 | | #include <sys/syscall.h> |
43 | | #endif |
44 | | #ifdef __APPLE__ |
45 | | #include <AvailabilityMacros.h> |
46 | | #endif |
47 | | #include "picotls.h" |
48 | | #if PICOTLS_USE_DTRACE |
49 | | #include "picotls-probes.h" |
50 | | #endif |
51 | | |
52 | 28.6k | #define PTLS_MAX_PLAINTEXT_RECORD_SIZE 16384 |
53 | 10.4k | #define PTLS_MAX_ENCRYPTED_RECORD_SIZE (16384 + 256) |
54 | | |
55 | 0 | #define PTLS_RECORD_VERSION_MAJOR 3 |
56 | 0 | #define PTLS_RECORD_VERSION_MINOR 3 |
57 | | |
58 | 38.2k | #define PTLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC 20 |
59 | 10.5k | #define PTLS_CONTENT_TYPE_ALERT 21 |
60 | 78.6k | #define PTLS_CONTENT_TYPE_HANDSHAKE 22 |
61 | 141k | #define PTLS_CONTENT_TYPE_APPDATA 23 |
62 | | |
63 | 119 | #define PTLS_PSK_KE_MODE_PSK 0 |
64 | 119 | #define PTLS_PSK_KE_MODE_PSK_DHE 1 |
65 | | |
66 | 2.03k | #define PTLS_HANDSHAKE_HEADER_SIZE 4 |
67 | | |
68 | | #define PTLS_EXTENSION_TYPE_SERVER_NAME 0 |
69 | | #define PTLS_EXTENSION_TYPE_STATUS_REQUEST 5 |
70 | | #define PTLS_EXTENSION_TYPE_SUPPORTED_GROUPS 10 |
71 | | #define PTLS_EXTENSION_TYPE_SIGNATURE_ALGORITHMS 13 |
72 | | #define PTLS_EXTENSION_TYPE_ALPN 16 |
73 | | #define PTLS_EXTENSION_TYPE_SERVER_CERTIFICATE_TYPE 20 |
74 | | #define PTLS_EXTENSION_TYPE_COMPRESS_CERTIFICATE 27 |
75 | | #define PTLS_EXTENSION_TYPE_PRE_SHARED_KEY 41 |
76 | | #define PTLS_EXTENSION_TYPE_EARLY_DATA 42 |
77 | | #define PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS 43 |
78 | | #define PTLS_EXTENSION_TYPE_COOKIE 44 |
79 | | #define PTLS_EXTENSION_TYPE_PSK_KEY_EXCHANGE_MODES 45 |
80 | | #define PTLS_EXTENSION_TYPE_CERTIFICATE_AUTHORITIES 47 |
81 | | #define PTLS_EXTENSION_TYPE_KEY_SHARE 51 |
82 | | #define PTLS_EXTENSION_TYPE_TICKET_REQUEST 58 |
83 | | #define PTLS_EXTENSION_TYPE_ECH_OUTER_EXTENSIONS 0xfd00 |
84 | | #define PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO 0xfe0d |
85 | | |
86 | | #define PTLS_SERVER_NAME_TYPE_HOSTNAME 0 |
87 | | |
88 | | #define PTLS_ECH_CONFIG_VERSION 0xfe0d |
89 | 28 | #define PTLS_ECH_CLIENT_HELLO_TYPE_OUTER 0 |
90 | 0 | #define PTLS_ECH_CLIENT_HELLO_TYPE_INNER 1 |
91 | | |
92 | 0 | #define PTLS_ECH_CONFIRM_LENGTH 8 |
93 | | |
94 | | static const char ech_info_prefix[8] = "tls ech"; |
95 | | |
96 | 242 | #define PTLS_SERVER_CERTIFICATE_VERIFY_CONTEXT_STRING "TLS 1.3, server CertificateVerify" |
97 | 0 | #define PTLS_CLIENT_CERTIFICATE_VERIFY_CONTEXT_STRING "TLS 1.3, client CertificateVerify" |
98 | | #define PTLS_MAX_CERTIFICATE_VERIFY_SIGNDATA_SIZE \ |
99 | | (64 + sizeof(PTLS_SERVER_CERTIFICATE_VERIFY_CONTEXT_STRING) + PTLS_MAX_DIGEST_SIZE * 2) |
100 | | |
101 | 102 | #define PTLS_EARLY_DATA_MAX_DELAY 10000 /* max. RTT (in msec) to permit early data */ |
102 | | |
103 | | #ifndef PTLS_MAX_EARLY_DATA_SKIP_SIZE |
104 | 10.0k | #define PTLS_MAX_EARLY_DATA_SKIP_SIZE 65536 |
105 | | #endif |
106 | | #if defined(PTLS_DEBUG) && PTLS_DEBUG |
107 | | #define PTLS_DEBUGF(...) fprintf(stderr, __VA_ARGS__) |
108 | | #else |
109 | | #define PTLS_DEBUGF(...) |
110 | | #endif |
111 | | |
112 | | #ifndef PTLS_MEMORY_DEBUG |
113 | 117k | #define PTLS_MEMORY_DEBUG 0 |
114 | | #endif |
115 | | |
116 | | #if PICOTLS_USE_DTRACE |
117 | | #define PTLS_PROBE0(LABEL, tls) \ |
118 | | do { \ |
119 | | if (PTLS_UNLIKELY(PICOTLS_##LABEL##_ENABLED())) \ |
120 | | PICOTLS_##LABEL(tls); \ |
121 | | } while (0) |
122 | | #define PTLS_PROBE(LABEL, tls, ...) \ |
123 | | do { \ |
124 | | if (PTLS_UNLIKELY(PICOTLS_##LABEL##_ENABLED())) \ |
125 | | PICOTLS_##LABEL((tls), __VA_ARGS__); \ |
126 | | } while (0) |
127 | | #else |
128 | | #define PTLS_PROBE0(LABEL, tls) |
129 | | #define PTLS_PROBE(LABEL, tls, ...) |
130 | | #endif |
131 | | |
132 | | /** |
133 | | * list of supported versions in the preferred order |
134 | | */ |
135 | | static const uint16_t supported_versions[] = {PTLS_PROTOCOL_VERSION_TLS13}; |
136 | | |
137 | | static const uint8_t hello_retry_random[PTLS_HELLO_RANDOM_SIZE] = {0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, |
138 | | 0x02, 0x1E, 0x65, 0xB8, 0x91, 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, |
139 | | 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C}; |
140 | | |
141 | | struct st_ptls_traffic_protection_t { |
142 | | uint8_t secret[PTLS_MAX_DIGEST_SIZE]; |
143 | | size_t epoch; |
144 | | /* the following fields are not used if the key_change callback is set */ |
145 | | ptls_aead_context_t *aead; |
146 | | uint64_t seq; |
147 | | unsigned tls12 : 1; |
148 | | uint64_t tls12_enc_record_iv; |
149 | | }; |
150 | | |
151 | | struct st_ptls_record_message_emitter_t { |
152 | | ptls_message_emitter_t super; |
153 | | size_t rec_start; |
154 | | }; |
155 | | |
156 | | struct st_ptls_signature_algorithms_t { |
157 | | uint16_t list[PTLS_MAX_SIGNATURE_ALGORITHMS]; |
158 | | size_t count; |
159 | | }; |
160 | | |
161 | | struct st_ptls_certificate_request_t { |
162 | | /** |
163 | | * context.base becomes non-NULL when a CertificateRequest is pending for processing |
164 | | */ |
165 | | ptls_iovec_t context; |
166 | | struct st_ptls_signature_algorithms_t signature_algorithms; |
167 | | }; |
168 | | |
169 | | struct st_decoded_ech_config_t { |
170 | | uint8_t id; |
171 | | ptls_hpke_kem_t *kem; |
172 | | ptls_iovec_t public_key; |
173 | | ptls_hpke_cipher_suite_t *cipher; |
174 | | uint8_t max_name_length; |
175 | | ptls_iovec_t public_name; |
176 | | ptls_iovec_t bytes; |
177 | | }; |
178 | | |
179 | | /** |
180 | | * Properties for ECH. Iff ECH is used and not rejected, `aead` is non-NULL. |
181 | | */ |
182 | | struct st_ptls_ech_t { |
183 | | /** |
184 | | * ECH state for this connection. `OFFERED` and `ACCEPTED` are used on both client and server; `GREASE` is client-only (server |
185 | | * cannot distinguish GREASE from a config mismatch, both are simply ECH that fails to decrypt). |
186 | | */ |
187 | | enum en_ptls_ech_state_t { |
188 | | PTLS_ECH_STATE_NONE = 0, |
189 | | PTLS_ECH_STATE_OFFERED, |
190 | | PTLS_ECH_STATE_ACCEPTED, |
191 | | PTLS_ECH_STATE_GREASE |
192 | | } state; |
193 | | uint8_t config_id; |
194 | | ptls_hpke_kem_t *kem; |
195 | | ptls_hpke_cipher_suite_t *cipher; |
196 | | ptls_aead_context_t *aead; |
197 | | uint8_t inner_client_random[PTLS_HELLO_RANDOM_SIZE]; |
198 | | struct { |
199 | | ptls_iovec_t enc; |
200 | | uint8_t max_name_length; |
201 | | char *public_name; |
202 | | /** |
203 | | * retains a copy of entire ECH extension so that it can be replayed in the 2nd CH when ECH is rejected via HRR |
204 | | */ |
205 | | ptls_iovec_t first_ech; |
206 | | } client; |
207 | | }; |
208 | | |
209 | | struct st_ptls_t { |
210 | | /** |
211 | | * the context |
212 | | */ |
213 | | ptls_context_t *ctx; |
214 | | /** |
215 | | * the state |
216 | | */ |
217 | | enum en_ptls_state_t { |
218 | | PTLS_STATE_CLIENT_HANDSHAKE_START, |
219 | | PTLS_STATE_CLIENT_EXPECT_SERVER_HELLO, |
220 | | PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO, |
221 | | PTLS_STATE_CLIENT_EXPECT_ENCRYPTED_EXTENSIONS, |
222 | | PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE, |
223 | | PTLS_STATE_CLIENT_EXPECT_CERTIFICATE, |
224 | | PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_VERIFY, |
225 | | PTLS_STATE_CLIENT_EXPECT_FINISHED, |
226 | | PTLS_STATE_SERVER_EXPECT_CLIENT_HELLO, |
227 | | PTLS_STATE_SERVER_EXPECT_SECOND_CLIENT_HELLO, |
228 | | PTLS_STATE_SERVER_GENERATING_CERTIFICATE_VERIFY, |
229 | | PTLS_STATE_SERVER_EXPECT_CERTIFICATE, |
230 | | PTLS_STATE_SERVER_EXPECT_CERTIFICATE_VERIFY, |
231 | | /* ptls_send can be called if the state is below here */ |
232 | | PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA, |
233 | | PTLS_STATE_SERVER_EXPECT_FINISHED, |
234 | | PTLS_STATE_POST_HANDSHAKE_MIN, |
235 | | PTLS_STATE_CLIENT_POST_HANDSHAKE = PTLS_STATE_POST_HANDSHAKE_MIN, |
236 | | PTLS_STATE_SERVER_POST_HANDSHAKE |
237 | | } state; |
238 | | /** |
239 | | * receive buffers |
240 | | */ |
241 | | struct { |
242 | | ptls_buffer_t rec; |
243 | | ptls_buffer_t mess; |
244 | | } recvbuf; |
245 | | /** |
246 | | * key schedule |
247 | | */ |
248 | | ptls_key_schedule_t *key_schedule; |
249 | | /** |
250 | | * values used for record protection |
251 | | */ |
252 | | struct { |
253 | | struct st_ptls_traffic_protection_t dec; |
254 | | struct st_ptls_traffic_protection_t enc; |
255 | | } traffic_protection; |
256 | | /** |
257 | | * server-name passed using SNI |
258 | | */ |
259 | | char *server_name; |
260 | | /** |
261 | | * result of ALPN |
262 | | */ |
263 | | char *negotiated_protocol; |
264 | | /** |
265 | | * selected key-exchange |
266 | | */ |
267 | | ptls_key_exchange_algorithm_t *key_share; |
268 | | /** |
269 | | * selected cipher-suite |
270 | | */ |
271 | | ptls_cipher_suite_t *cipher_suite; |
272 | | /** |
273 | | * ClientHello.random that appears on the wire. When ECH is used, that of inner CH is retained separately. |
274 | | */ |
275 | | uint8_t client_random[PTLS_HELLO_RANDOM_SIZE]; |
276 | | /** |
277 | | * exporter master secret (either 0rtt or 1rtt) |
278 | | */ |
279 | | struct { |
280 | | uint8_t *early; |
281 | | uint8_t *one_rtt; |
282 | | } exporter_master_secret; |
283 | | /** |
284 | | * ECH |
285 | | */ |
286 | | struct st_ptls_ech_t ech; |
287 | | /* flags */ |
288 | | unsigned is_server : 1; |
289 | | unsigned is_psk_handshake : 1; |
290 | | unsigned send_change_cipher_spec : 1; |
291 | | unsigned needs_key_update : 1; |
292 | | unsigned key_update_send_request : 1; |
293 | | #if PTLS_HAVE_LOG |
294 | | /** |
295 | | * see ptls_log |
296 | | */ |
297 | | ptls_log_conn_state_t log_state; |
298 | | #endif |
299 | | struct { |
300 | | uint32_t active_conns; |
301 | | uint32_t generation; |
302 | | } log_sni; |
303 | | /** |
304 | | * misc. |
305 | | */ |
306 | | union { |
307 | | struct { |
308 | | ptls_iovec_t legacy_session_id; |
309 | | uint8_t legacy_session_id_buf[32]; |
310 | | ptls_key_exchange_context_t *key_share_ctx; |
311 | | unsigned offered_psk : 1; |
312 | | /** |
313 | | * if 1-RTT write key is active |
314 | | */ |
315 | | unsigned using_early_data : 1; |
316 | | struct st_ptls_certificate_request_t certificate_request; |
317 | | } client; |
318 | | struct { |
319 | | uint8_t pending_traffic_secret[PTLS_MAX_DIGEST_SIZE]; |
320 | | uint32_t early_data_skipped_bytes; /* if not UINT32_MAX, the server is skipping early data */ |
321 | | uint8_t num_tickets_to_send; |
322 | | ptls_async_job_t *async_job; |
323 | | } server; |
324 | | }; |
325 | | /** |
326 | | * certificate verify; will be used by the client and the server (if require_client_authentication is set) |
327 | | */ |
328 | | struct { |
329 | | int (*cb)(void *verify_ctx, uint16_t algo, ptls_iovec_t data, ptls_iovec_t signature); |
330 | | void *verify_ctx; |
331 | | } certificate_verify; |
332 | | /** |
333 | | * handshake traffic secret to be commisioned (an array of `uint8_t [PTLS_MAX_DIGEST_SIZE]` or NULL) |
334 | | */ |
335 | | uint8_t *pending_handshake_secret; |
336 | | /** |
337 | | * user data |
338 | | */ |
339 | | void *data_ptr; |
340 | | }; |
341 | | |
342 | | struct st_ptls_record_t { |
343 | | uint8_t type; |
344 | | uint16_t version; |
345 | | size_t length; |
346 | | const uint8_t *fragment; |
347 | | }; |
348 | | |
349 | 0 | #define MAX_UNKNOWN_EXTENSIONS 16 |
350 | | #define MAX_CERTIFICATE_TYPES 8 |
351 | | |
352 | | struct st_ptls_client_hello_t { |
353 | | uint16_t legacy_version; |
354 | | const uint8_t *random_bytes; |
355 | | ptls_iovec_t legacy_session_id; |
356 | | struct { |
357 | | const uint8_t *ids; |
358 | | size_t count; |
359 | | } compression_methods; |
360 | | uint16_t selected_version; |
361 | | ptls_iovec_t cipher_suites; |
362 | | ptls_iovec_t negotiated_groups; |
363 | | ptls_iovec_t key_shares; |
364 | | struct st_ptls_signature_algorithms_t signature_algorithms; |
365 | | ptls_iovec_t server_name; |
366 | | struct { |
367 | | ptls_iovec_t list[16]; |
368 | | size_t count; |
369 | | } alpn; |
370 | | struct { |
371 | | uint16_t list[16]; |
372 | | size_t count; |
373 | | } cert_compression_algos; |
374 | | struct { |
375 | | ptls_iovec_t all; |
376 | | ptls_iovec_t tbs; |
377 | | ptls_iovec_t ch1_hash; |
378 | | ptls_iovec_t signature; |
379 | | unsigned sent_key_share : 1; |
380 | | } cookie; |
381 | | struct { |
382 | | uint8_t list[MAX_CERTIFICATE_TYPES]; |
383 | | size_t count; |
384 | | } server_certificate_types; |
385 | | unsigned status_request : 1; |
386 | | struct { |
387 | | uint8_t new_session_count; |
388 | | uint8_t resumption_count; |
389 | | } ticket_request; |
390 | | /** |
391 | | * ECH: payload.base != NULL indicates that the extension was received |
392 | | */ |
393 | | struct { |
394 | | uint8_t type; |
395 | | uint8_t config_id; |
396 | | ptls_hpke_cipher_suite_id_t cipher_suite; |
397 | | ptls_iovec_t enc; |
398 | | ptls_iovec_t payload; |
399 | | } ech; |
400 | | struct { |
401 | | const uint8_t *hash_end; |
402 | | struct { |
403 | | ptls_client_hello_psk_identity_t list[4]; |
404 | | size_t count; |
405 | | } identities; |
406 | | unsigned ke_modes; |
407 | | unsigned early_data_indication : 1; |
408 | | unsigned is_last_extension : 1; |
409 | | } psk; |
410 | | ptls_raw_extension_t unknown_extensions[MAX_UNKNOWN_EXTENSIONS + 1]; |
411 | | size_t first_extension_at; |
412 | | }; |
413 | | |
414 | | struct st_ptls_server_hello_t { |
415 | | uint8_t random_[PTLS_HELLO_RANDOM_SIZE]; |
416 | | ptls_iovec_t legacy_session_id; |
417 | | int is_retry_request; |
418 | | union { |
419 | | ptls_iovec_t peerkey; |
420 | | struct { |
421 | | uint16_t selected_group; |
422 | | ptls_iovec_t cookie; |
423 | | const uint8_t *ech; |
424 | | } retry_request; |
425 | | }; |
426 | | }; |
427 | | |
428 | | struct st_ptls_key_schedule_t { |
429 | | unsigned generation; /* early secret (1), hanshake secret (2), master secret (3) */ |
430 | | uint8_t secret[PTLS_MAX_DIGEST_SIZE]; |
431 | | size_t num_hashes; |
432 | | struct { |
433 | | ptls_hash_algorithm_t *algo; |
434 | | ptls_hash_context_t *ctx, *ctx_outer; |
435 | | } hashes[1]; |
436 | | }; |
437 | | |
438 | | struct st_ptls_extension_decoder_t { |
439 | | uint16_t type; |
440 | | int (*cb)(ptls_t *tls, void *arg, const uint8_t *src, const uint8_t *const end); |
441 | | }; |
442 | | |
443 | | struct st_ptls_extension_bitmap_t { |
444 | | uint64_t bits; |
445 | | }; |
446 | | |
447 | | static const uint8_t zeroes_of_max_digest_size[PTLS_MAX_DIGEST_SIZE] = {0}; |
448 | | |
449 | | static ptls_aead_context_t *new_aead(ptls_aead_algorithm_t *aead, ptls_hash_algorithm_t *hash, int is_enc, const void *secret, |
450 | | ptls_iovec_t hash_value, const char *label_prefix); |
451 | | static int server_finish_handshake(ptls_t *tls, ptls_message_emitter_t *emitter, int send_cert_verify, |
452 | | struct st_ptls_signature_algorithms_t *signature_algorithms); |
453 | | |
454 | | static int is_supported_version(uint16_t v) |
455 | 882 | { |
456 | 882 | size_t i; |
457 | 998 | for (i = 0; i != PTLS_ELEMENTSOF(supported_versions); ++i) |
458 | 882 | if (supported_versions[i] == v) |
459 | 766 | return 1; |
460 | 116 | return 0; |
461 | 882 | } |
462 | | |
463 | | static int extension_bitmap_testandset(struct st_ptls_extension_bitmap_t *bitmap, int hstype, uint16_t extid) |
464 | 5.09k | { |
465 | 39.1k | #define HSTYPE_TO_BIT(hstype) ((uint64_t)1 << ((hstype) + 1)) /* min(hstype) is -1 (PSEUDO_HRR) */ |
466 | 35.6k | #define DEFINE_BIT(abbrev, hstype) static const uint64_t abbrev = HSTYPE_TO_BIT(PTLS_HANDSHAKE_TYPE_##hstype) |
467 | 5.09k | #define EXT(candext, allowed_bits) \ |
468 | 51.9k | do { \ |
469 | 51.9k | if (PTLS_UNLIKELY(extid == PTLS_EXTENSION_TYPE_##candext)) { \ |
470 | 3.56k | allowed_hs_bits = allowed_bits; \ |
471 | 3.56k | goto Found; \ |
472 | 3.56k | } \ |
473 | 51.9k | ext_bitmap_mask <<= 1; \ |
474 | 48.3k | } while (0) |
475 | | |
476 | 5.09k | DEFINE_BIT(CH, CLIENT_HELLO); |
477 | 5.09k | DEFINE_BIT(SH, SERVER_HELLO); |
478 | 5.09k | DEFINE_BIT(HRR, PSEUDO_HRR); |
479 | 5.09k | DEFINE_BIT(EE, ENCRYPTED_EXTENSIONS); |
480 | 5.09k | DEFINE_BIT(CR, CERTIFICATE_REQUEST); |
481 | 5.09k | DEFINE_BIT(CT, CERTIFICATE); |
482 | 5.09k | DEFINE_BIT(NST, NEW_SESSION_TICKET); |
483 | | |
484 | 5.09k | uint64_t allowed_hs_bits, ext_bitmap_mask = 1; |
485 | | |
486 | | /* clang-format off */ |
487 | | /* RFC 8446 section 4.2: "The table below indicates the messages where a given extension may appear... If an implementation |
488 | | * receives an extension which it recognizes and which is not specified for the message in which it appears, it MUST abort the |
489 | | * handshake with an "illegal_parameter" alert. |
490 | | * |
491 | | * +-------------------------+---------------+ |
492 | | * + Extension | Allowed | |
493 | | * +-------------------------+---------------+ */ |
494 | 5.09k | EXT( SERVER_NAME , CH + EE ); |
495 | 4.93k | EXT( STATUS_REQUEST , CH + CR + CT ); |
496 | 4.91k | EXT( SUPPORTED_GROUPS , CH + EE ); |
497 | 4.65k | EXT( SIGNATURE_ALGORITHMS , CH + CR ); |
498 | 4.32k | EXT( ALPN , CH + EE ); |
499 | 4.22k | EXT( SERVER_CERTIFICATE_TYPE , CH + EE ); |
500 | 4.14k | EXT( KEY_SHARE , CH + SH + HRR ); |
501 | 3.54k | EXT( PRE_SHARED_KEY , CH + SH ); |
502 | 3.14k | EXT( PSK_KEY_EXCHANGE_MODES , CH ); |
503 | 2.80k | EXT( EARLY_DATA , CH + EE + NST ); |
504 | 2.60k | EXT( COOKIE , CH + HRR ); |
505 | 2.59k | EXT( SUPPORTED_VERSIONS , CH + SH + HRR ); |
506 | 1.73k | EXT( COMPRESS_CERTIFICATE , CH + CR ); /* from RFC 8879 */ |
507 | 1.66k | EXT( ENCRYPTED_CLIENT_HELLO , CH + HRR + EE ); /* from draft-ietf-tls-esni-15 */ |
508 | 1.52k | EXT( ECH_OUTER_EXTENSIONS , 0 ); |
509 | | /* +-----------------------------------------+ */ |
510 | | /* clang-format on */ |
511 | | |
512 | 1.52k | return 1; |
513 | | |
514 | 3.56k | Found: |
515 | 3.56k | if ((allowed_hs_bits & HSTYPE_TO_BIT(hstype)) == 0) |
516 | 3 | return 0; |
517 | 3.56k | if ((bitmap->bits & ext_bitmap_mask) != 0) |
518 | 34 | return 0; |
519 | 3.52k | bitmap->bits |= ext_bitmap_mask; |
520 | 3.52k | return 1; |
521 | | |
522 | 3.56k | #undef HSTYPE_TO_BIT |
523 | 3.56k | #undef DEFINE_ABBREV |
524 | 3.56k | #undef EXT |
525 | 3.56k | } |
526 | | |
527 | | #ifndef ntoh16 |
528 | | static uint16_t ntoh16(const uint8_t *src) |
529 | 118k | { |
530 | 118k | return (uint16_t)src[0] << 8 | src[1]; |
531 | 118k | } |
532 | | #endif |
533 | | |
534 | | #ifndef ntoh24 |
535 | | static uint32_t ntoh24(const uint8_t *src) |
536 | 5.42k | { |
537 | 5.42k | return (uint32_t)src[0] << 16 | (uint32_t)src[1] << 8 | src[2]; |
538 | 5.42k | } |
539 | | #endif |
540 | | |
541 | | #ifndef ntoh32 |
542 | | static uint32_t ntoh32(const uint8_t *src) |
543 | 1.39k | { |
544 | 1.39k | return (uint32_t)src[0] << 24 | (uint32_t)src[1] << 16 | (uint32_t)src[2] << 8 | src[3]; |
545 | 1.39k | } |
546 | | #endif |
547 | | |
548 | | #ifndef ntoh64 |
549 | | static uint64_t ntoh64(const uint8_t *src) |
550 | 172 | { |
551 | 172 | return (uint64_t)src[0] << 56 | (uint64_t)src[1] << 48 | (uint64_t)src[2] << 40 | (uint64_t)src[3] << 32 | |
552 | 172 | (uint64_t)src[4] << 24 | (uint64_t)src[5] << 16 | (uint64_t)src[6] << 8 | src[7]; |
553 | 172 | } |
554 | | #endif |
555 | | |
556 | | static void encode64(uint8_t *dst, uint64_t v) |
557 | 0 | { |
558 | 0 | for (size_t i = 0; i < 8; ++i) |
559 | 0 | dst[i] = (uint8_t)(v >> (56 - 8 * i)); |
560 | 0 | } |
561 | | |
562 | | static char *duplicate_as_str(const void *src, size_t len) |
563 | 0 | { |
564 | 0 | char *dst; |
565 | |
|
566 | 0 | if ((dst = malloc(len + 1)) == NULL) |
567 | 0 | return NULL; |
568 | 0 | memcpy(dst, src, len); |
569 | 0 | dst[len] = '\0'; |
570 | 0 | return dst; |
571 | 0 | } |
572 | | |
573 | | void ptls_buffer__release_memory(ptls_buffer_t *buf) |
574 | 80.0k | { |
575 | 80.0k | ptls_clear_memory(buf->base, buf->off); |
576 | 80.0k | if (buf->is_allocated) { |
577 | | #ifdef _WINDOWS |
578 | | if (buf->align_bits != 0) { |
579 | | _aligned_free(buf->base); |
580 | | } else { |
581 | | free(buf->base); |
582 | | } |
583 | | #else |
584 | 3.17k | free(buf->base); |
585 | 3.17k | #endif |
586 | 3.17k | } |
587 | 80.0k | } |
588 | | |
589 | | int ptls_buffer_reserve(ptls_buffer_t *buf, size_t delta) |
590 | 57.7k | { |
591 | 57.7k | return ptls_buffer_reserve_aligned(buf, delta, 0); |
592 | 57.7k | } |
593 | | |
594 | | int ptls_buffer_reserve_aligned(ptls_buffer_t *buf, size_t delta, uint8_t align_bits) |
595 | 58.7k | { |
596 | 58.7k | if (buf->base == NULL) |
597 | 0 | return PTLS_ERROR_NO_MEMORY; |
598 | | |
599 | 58.7k | if (PTLS_MEMORY_DEBUG || buf->capacity < buf->off + delta || |
600 | 55.5k | (buf->align_bits < align_bits && ((uintptr_t)buf->base & (((uintptr_t)1 << align_bits) - 1)) != 0)) { |
601 | 3.17k | void *newp; |
602 | 3.17k | size_t new_capacity = buf->capacity; |
603 | 3.17k | if (new_capacity < 1024) |
604 | 2.70k | new_capacity = 1024; |
605 | 4.37k | while (new_capacity < buf->off + delta) { |
606 | 1.19k | new_capacity *= 2; |
607 | 1.19k | } |
608 | 3.17k | if (align_bits != 0) { |
609 | | #ifdef _WINDOWS |
610 | | if ((newp = _aligned_malloc(new_capacity, (size_t)1 << align_bits)) == NULL) |
611 | | return PTLS_ERROR_NO_MEMORY; |
612 | | #else |
613 | 0 | if (posix_memalign(&newp, 1 << align_bits, new_capacity) != 0) |
614 | 0 | return PTLS_ERROR_NO_MEMORY; |
615 | 0 | #endif |
616 | 3.17k | } else { |
617 | 3.17k | if ((newp = malloc(new_capacity)) == NULL) |
618 | 0 | return PTLS_ERROR_NO_MEMORY; |
619 | 3.17k | } |
620 | 3.17k | memcpy(newp, buf->base, buf->off); |
621 | 3.17k | ptls_buffer__release_memory(buf); |
622 | 3.17k | buf->base = newp; |
623 | 3.17k | buf->capacity = new_capacity; |
624 | 3.17k | buf->is_allocated = 1; |
625 | 3.17k | buf->align_bits = align_bits; |
626 | 3.17k | } |
627 | | |
628 | 58.7k | return 0; |
629 | 58.7k | } |
630 | | |
631 | | int ptls_buffer__do_pushv(ptls_buffer_t *buf, const void *src, size_t len) |
632 | 44.8k | { |
633 | 44.8k | int ret; |
634 | | |
635 | 44.8k | if (len == 0) |
636 | 2.74k | return 0; |
637 | 42.0k | if ((ret = ptls_buffer_reserve(buf, len)) != 0) |
638 | 0 | return ret; |
639 | 42.0k | memcpy(buf->base + buf->off, src, len); |
640 | 42.0k | buf->off += len; |
641 | 42.0k | return 0; |
642 | 42.0k | } |
643 | | |
644 | | int ptls_buffer__adjust_quic_blocksize(ptls_buffer_t *buf, size_t body_size) |
645 | 0 | { |
646 | 0 | uint8_t sizebuf[PTLS_ENCODE_QUICINT_CAPACITY]; |
647 | 0 | size_t sizelen = ptls_encode_quicint(sizebuf, body_size) - sizebuf; |
648 | | |
649 | | /* adjust amount of space before body_size to `sizelen` bytes */ |
650 | 0 | if (sizelen != 1) { |
651 | 0 | int ret; |
652 | 0 | if ((ret = ptls_buffer_reserve(buf, sizelen - 1)) != 0) |
653 | 0 | return ret; |
654 | 0 | memmove(buf->base + buf->off - body_size - 1 + sizelen, buf->base + buf->off - body_size, body_size); |
655 | 0 | buf->off += sizelen - 1; |
656 | 0 | } |
657 | | |
658 | | /* write the size */ |
659 | 0 | memcpy(buf->base + buf->off - body_size - sizelen, sizebuf, sizelen); |
660 | |
|
661 | 0 | return 0; |
662 | 0 | } |
663 | | |
664 | | int ptls_buffer__adjust_asn1_blocksize(ptls_buffer_t *buf, size_t body_size) |
665 | 0 | { |
666 | 0 | fprintf(stderr, "unimplemented\n"); |
667 | 0 | abort(); |
668 | 0 | } |
669 | | |
670 | | int ptls_buffer_push_asn1_ubigint(ptls_buffer_t *buf, const void *bignum, size_t size) |
671 | 0 | { |
672 | 0 | const uint8_t *p = bignum, *const end = p + size; |
673 | 0 | int ret; |
674 | | |
675 | | /* skip zeroes */ |
676 | 0 | for (; end - p >= 1; ++p) |
677 | 0 | if (*p != 0) |
678 | 0 | break; |
679 | | |
680 | | /* emit */ |
681 | 0 | ptls_buffer_push(buf, 2); |
682 | 0 | ptls_buffer_push_asn1_block(buf, { |
683 | 0 | if (*p >= 0x80) |
684 | 0 | ptls_buffer_push(buf, 0); |
685 | 0 | if (p != end) { |
686 | 0 | ptls_buffer_pushv(buf, p, end - p); |
687 | 0 | } else { |
688 | 0 | ptls_buffer_pushv(buf, "", 1); |
689 | 0 | } |
690 | 0 | }); |
691 | 0 | ret = 0; |
692 | |
|
693 | 0 | Exit: |
694 | 0 | return ret; |
695 | 0 | } |
696 | | |
697 | | #if PTLS_FUZZ_HANDSHAKE |
698 | | |
699 | | static size_t aead_encrypt(struct st_ptls_traffic_protection_t *ctx, void *output, const void *input, size_t inlen, |
700 | | uint8_t content_type) |
701 | 999 | { |
702 | 999 | memcpy(output, input, inlen); |
703 | 999 | memcpy(output + inlen, &content_type, 1); |
704 | 999 | return inlen + 1 + 16; |
705 | 999 | } |
706 | | |
707 | | static int aead_decrypt(struct st_ptls_traffic_protection_t *ctx, void *output, size_t *outlen, const void *input, size_t inlen) |
708 | 9.92k | { |
709 | 9.92k | if (inlen < 16) { |
710 | 9.56k | return PTLS_ALERT_BAD_RECORD_MAC; |
711 | 9.56k | } |
712 | 360 | memcpy(output, input, inlen - 16); |
713 | 360 | *outlen = inlen - 16; /* removing the 16 bytes of tag */ |
714 | 360 | return 0; |
715 | 9.92k | } |
716 | | |
717 | | #else |
718 | | |
719 | | static void build_aad(uint8_t aad[5], size_t reclen) |
720 | | { |
721 | | aad[0] = PTLS_CONTENT_TYPE_APPDATA; |
722 | | aad[1] = PTLS_RECORD_VERSION_MAJOR; |
723 | | aad[2] = PTLS_RECORD_VERSION_MINOR; |
724 | | aad[3] = (uint8_t)(reclen >> 8); |
725 | | aad[4] = (uint8_t)reclen; |
726 | | } |
727 | | |
728 | | static size_t aead_encrypt(struct st_ptls_traffic_protection_t *ctx, void *output, const void *input, size_t inlen, |
729 | | uint8_t content_type) |
730 | | { |
731 | | ptls_iovec_t invec[2] = {ptls_iovec_init(input, inlen), ptls_iovec_init(&content_type, 1)}; |
732 | | uint8_t aad[5]; |
733 | | |
734 | | build_aad(aad, inlen + 1 + ctx->aead->algo->tag_size); |
735 | | ptls_aead_encrypt_v(ctx->aead, output, invec, PTLS_ELEMENTSOF(invec), ctx->seq++, aad, sizeof(aad)); |
736 | | |
737 | | return inlen + 1 + ctx->aead->algo->tag_size; |
738 | | } |
739 | | |
740 | | static int aead_decrypt(struct st_ptls_traffic_protection_t *ctx, void *output, size_t *outlen, const void *input, size_t inlen) |
741 | | { |
742 | | uint8_t aad[5]; |
743 | | |
744 | | build_aad(aad, inlen); |
745 | | if ((*outlen = ptls_aead_decrypt(ctx->aead, output, input, inlen, ctx->seq, aad, sizeof(aad))) == SIZE_MAX) |
746 | | return PTLS_ALERT_BAD_RECORD_MAC; |
747 | | ++ctx->seq; |
748 | | return 0; |
749 | | } |
750 | | |
751 | | #endif /* #if PTLS_FUZZ_HANDSHAKE */ |
752 | | |
753 | | static void build_tls12_aad(uint8_t *aad, uint8_t type, uint64_t seq, uint16_t length) |
754 | 0 | { |
755 | 0 | for (size_t i = 0; i < 8; ++i) |
756 | 0 | aad[i] = (uint8_t)(seq >> (56 - i * 8)); |
757 | 0 | aad[8] = type; |
758 | 0 | aad[9] = PTLS_RECORD_VERSION_MAJOR; |
759 | 0 | aad[10] = PTLS_RECORD_VERSION_MINOR; |
760 | 0 | aad[11] = length >> 8; |
761 | 0 | aad[12] = (uint8_t)length; |
762 | 0 | } |
763 | | |
764 | | #define buffer_push_record(buf, type, block) \ |
765 | 1.82k | do { \ |
766 | 1.82k | ptls_buffer_push((buf), (type), PTLS_RECORD_VERSION_MAJOR, PTLS_RECORD_VERSION_MINOR); \ |
767 | 1.82k | ptls_buffer_push_block((buf), 2, block); \ |
768 | 1.82k | } while (0) |
769 | | |
770 | | static int buffer_push_encrypted_records(ptls_buffer_t *buf, uint8_t type, const uint8_t *src, size_t len, |
771 | | struct st_ptls_traffic_protection_t *enc) |
772 | 0 | { |
773 | 0 | int ret = 0; |
774 | |
|
775 | 0 | while (len != 0) { |
776 | 0 | size_t chunk_size = len; |
777 | 0 | if (chunk_size > PTLS_MAX_PLAINTEXT_RECORD_SIZE) |
778 | 0 | chunk_size = PTLS_MAX_PLAINTEXT_RECORD_SIZE; |
779 | 0 | if (enc->tls12) { |
780 | 0 | buffer_push_record(buf, type, { |
781 | | /* reserve memory */ |
782 | 0 | if ((ret = ptls_buffer_reserve_aligned( |
783 | 0 | buf, enc->aead->algo->tls12.record_iv_size + chunk_size + enc->aead->algo->tag_size, |
784 | 0 | enc->aead->algo->align_bits)) != 0) |
785 | 0 | goto Exit; |
786 | | /* determine nonce, as well as prepending that walue as the record IV (AES-GCM) */ |
787 | 0 | uint64_t nonce; |
788 | 0 | if (enc->aead->algo->tls12.record_iv_size != 0) { |
789 | 0 | assert(enc->aead->algo->tls12.record_iv_size == 8); |
790 | 0 | nonce = enc->tls12_enc_record_iv++; |
791 | 0 | encode64(buf->base + buf->off, nonce); |
792 | 0 | buf->off += 8; |
793 | 0 | } else { |
794 | 0 | nonce = enc->seq; |
795 | 0 | } |
796 | | /* build AAD */ |
797 | 0 | uint8_t aad[PTLS_TLS12_AAD_SIZE]; |
798 | 0 | build_tls12_aad(aad, type, enc->seq, (uint16_t)chunk_size); |
799 | | /* encrypt */ |
800 | 0 | buf->off += ptls_aead_encrypt(enc->aead, buf->base + buf->off, src, chunk_size, nonce, aad, sizeof(aad)); |
801 | 0 | ++enc->seq; |
802 | 0 | }); |
803 | 0 | } else { |
804 | 0 | buffer_push_record(buf, PTLS_CONTENT_TYPE_APPDATA, { |
805 | 0 | if ((ret = ptls_buffer_reserve_aligned(buf, chunk_size + enc->aead->algo->tag_size + 1, |
806 | 0 | enc->aead->algo->align_bits)) != 0) |
807 | 0 | goto Exit; |
808 | 0 | buf->off += aead_encrypt(enc, buf->base + buf->off, src, chunk_size, type); |
809 | 0 | }); |
810 | 0 | } |
811 | 0 | src += chunk_size; |
812 | 0 | len -= chunk_size; |
813 | 0 | } |
814 | | |
815 | 0 | Exit: |
816 | 0 | return ret; |
817 | 0 | } |
818 | | |
819 | | static int buffer_encrypt_record(ptls_buffer_t *buf, size_t rec_start, struct st_ptls_traffic_protection_t *enc) |
820 | 999 | { |
821 | 999 | size_t bodylen = buf->off - rec_start - 5; |
822 | 999 | uint8_t *tmpbuf, type = buf->base[rec_start]; |
823 | 999 | int ret; |
824 | | |
825 | | /* Fast path: do in-place encryption if only one record needs to be emitted. (For simplicity, do not take this path if TLS 1.2 |
826 | | * is used, as this function will be called no more than once per connection, for encrypting an alert.) */ |
827 | 999 | if (!enc->tls12 && bodylen <= PTLS_MAX_PLAINTEXT_RECORD_SIZE) { |
828 | 999 | size_t overhead = 1 + enc->aead->algo->tag_size; |
829 | 999 | if ((ret = ptls_buffer_reserve_aligned(buf, overhead, enc->aead->algo->align_bits)) != 0) |
830 | 0 | return ret; |
831 | 999 | size_t encrypted_len = aead_encrypt(enc, buf->base + rec_start + 5, buf->base + rec_start + 5, bodylen, type); |
832 | 999 | assert(encrypted_len == bodylen + overhead); |
833 | 999 | buf->off += overhead; |
834 | 999 | buf->base[rec_start] = PTLS_CONTENT_TYPE_APPDATA; |
835 | 999 | buf->base[rec_start + 3] = (encrypted_len >> 8) & 0xff; |
836 | 999 | buf->base[rec_start + 4] = encrypted_len & 0xff; |
837 | 999 | return 0; |
838 | 999 | } |
839 | | |
840 | | /* move plaintext to temporary buffer */ |
841 | 0 | if ((tmpbuf = malloc(bodylen)) == NULL) { |
842 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
843 | 0 | goto Exit; |
844 | 0 | } |
845 | 0 | memcpy(tmpbuf, buf->base + rec_start + 5, bodylen); |
846 | 0 | ptls_clear_memory(buf->base + rec_start, bodylen + 5); |
847 | 0 | buf->off = rec_start; |
848 | | |
849 | | /* push encrypted records */ |
850 | 0 | ret = buffer_push_encrypted_records(buf, type, tmpbuf, bodylen, enc); |
851 | |
|
852 | 0 | Exit: |
853 | 0 | if (tmpbuf != NULL) { |
854 | 0 | ptls_clear_memory(tmpbuf, bodylen); |
855 | 0 | free(tmpbuf); |
856 | 0 | } |
857 | 0 | return ret; |
858 | 0 | } |
859 | | |
860 | | static int begin_record_message(ptls_message_emitter_t *_self) |
861 | 1.39k | { |
862 | 1.39k | struct st_ptls_record_message_emitter_t *self = (void *)_self; |
863 | 1.39k | int ret; |
864 | | |
865 | 1.39k | self->rec_start = self->super.buf->off; |
866 | 1.39k | ptls_buffer_push(self->super.buf, PTLS_CONTENT_TYPE_HANDSHAKE, PTLS_RECORD_VERSION_MAJOR, PTLS_RECORD_VERSION_MINOR, 0, 0); |
867 | 1.39k | ret = 0; |
868 | 1.39k | Exit: |
869 | 1.39k | return ret; |
870 | 1.39k | } |
871 | | |
872 | | static int commit_record_message(ptls_message_emitter_t *_self) |
873 | 1.39k | { |
874 | 1.39k | struct st_ptls_record_message_emitter_t *self = (void *)_self; |
875 | 1.39k | int ret; |
876 | | |
877 | 1.39k | if (self->super.enc->aead != NULL) { |
878 | 985 | ret = buffer_encrypt_record(self->super.buf, self->rec_start, self->super.enc); |
879 | 985 | } else { |
880 | | /* TODO allow CH,SH,HRR above 16KB */ |
881 | 410 | size_t sz = self->super.buf->off - self->rec_start - 5; |
882 | 410 | assert(sz <= PTLS_MAX_PLAINTEXT_RECORD_SIZE); |
883 | 410 | self->super.buf->base[self->rec_start + 3] = (uint8_t)(sz >> 8); |
884 | 410 | self->super.buf->base[self->rec_start + 4] = (uint8_t)(sz); |
885 | 410 | ret = 0; |
886 | 410 | } |
887 | | |
888 | 1.39k | return ret; |
889 | 1.39k | } |
890 | | |
891 | | #define buffer_push_extension(buf, type, block) \ |
892 | 0 | do { \ |
893 | 0 | ptls_buffer_push16((buf), (type)); \ |
894 | 0 | ptls_buffer_push_block((buf), 2, block); \ |
895 | 0 | } while (0); |
896 | | |
897 | | #define decode_open_extensions(src, end, hstype, exttype, block) \ |
898 | 1.86k | do { \ |
899 | 1.86k | struct st_ptls_extension_bitmap_t bitmap = {0}; \ |
900 | 1.86k | ptls_decode_open_block((src), end, 2, { \ |
901 | 1.86k | while ((src) != end) { \ |
902 | 1.86k | if ((ret = ptls_decode16((exttype), &(src), end)) != 0) \ |
903 | 1.86k | goto Exit; \ |
904 | 1.86k | if (!extension_bitmap_testandset(&bitmap, (hstype), *(exttype))) { \ |
905 | 1.86k | ret = PTLS_ALERT_ILLEGAL_PARAMETER; \ |
906 | 1.86k | goto Exit; \ |
907 | 1.86k | } \ |
908 | 1.86k | ptls_decode_open_block((src), end, 2, block); \ |
909 | 1.86k | } \ |
910 | 1.86k | }); \ |
911 | 1.86k | } while (0) |
912 | | |
913 | | #define decode_extensions(src, end, hstype, exttype, block) \ |
914 | 1.86k | do { \ |
915 | 1.86k | decode_open_extensions((src), end, hstype, exttype, block); \ |
916 | 1.86k | ptls_decode_assert_block_close((src), end); \ |
917 | 883 | } while (0) |
918 | | |
919 | | int ptls_decode8(uint8_t *value, const uint8_t **src, const uint8_t *end) |
920 | 3.56k | { |
921 | 3.56k | if (*src == end) |
922 | 23 | return PTLS_ALERT_DECODE_ERROR; |
923 | 3.54k | *value = *(*src)++; |
924 | 3.54k | return 0; |
925 | 3.56k | } |
926 | | |
927 | | int ptls_decode16(uint16_t *value, const uint8_t **src, const uint8_t *end) |
928 | 42.5k | { |
929 | 42.5k | if (end - *src < 2) |
930 | 135 | return PTLS_ALERT_DECODE_ERROR; |
931 | 42.4k | *value = ntoh16(*src); |
932 | 42.4k | *src += 2; |
933 | 42.4k | return 0; |
934 | 42.5k | } |
935 | | |
936 | | int ptls_decode24(uint32_t *value, const uint8_t **src, const uint8_t *end) |
937 | 0 | { |
938 | 0 | if (end - *src < 3) |
939 | 0 | return PTLS_ALERT_DECODE_ERROR; |
940 | 0 | *value = ((uint32_t)(*src)[0] << 16) | ((uint32_t)(*src)[1] << 8) | (*src)[2]; |
941 | 0 | *src += 3; |
942 | 0 | return 0; |
943 | 0 | } |
944 | | |
945 | | int ptls_decode32(uint32_t *value, const uint8_t **src, const uint8_t *end) |
946 | 1.39k | { |
947 | 1.39k | if (end - *src < 4) |
948 | 6 | return PTLS_ALERT_DECODE_ERROR; |
949 | 1.39k | *value = ntoh32(*src); |
950 | 1.39k | *src += 4; |
951 | 1.39k | return 0; |
952 | 1.39k | } |
953 | | |
954 | | int ptls_decode64(uint64_t *value, const uint8_t **src, const uint8_t *end) |
955 | 172 | { |
956 | 172 | if (end - *src < 8) |
957 | 0 | return PTLS_ALERT_DECODE_ERROR; |
958 | 172 | *value = ntoh64(*src); |
959 | 172 | *src += 8; |
960 | 172 | return 0; |
961 | 172 | } |
962 | | |
963 | | uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end) |
964 | 0 | { |
965 | 0 | if (PTLS_UNLIKELY(*src == end)) |
966 | 0 | return UINT64_MAX; |
967 | | |
968 | 0 | uint8_t b = *(*src)++; |
969 | |
|
970 | 0 | if (PTLS_LIKELY(b <= 0x3f)) |
971 | 0 | return b; |
972 | | |
973 | 0 | uint64_t v = b & 0x3f; |
974 | 0 | unsigned bytes_left = (1 << (b >> 6)) - 1; |
975 | 0 | if (PTLS_UNLIKELY((size_t)(end - *src) < bytes_left)) |
976 | 0 | return UINT64_MAX; |
977 | 0 | do { |
978 | 0 | v = (v << 8) | *(*src)++; |
979 | 0 | } while (--bytes_left != 0); |
980 | 0 | return v; |
981 | 0 | } |
982 | | |
983 | | static void log_secret(ptls_t *tls, const char *type, ptls_iovec_t secret) |
984 | 754 | { |
985 | 754 | char hexbuf[PTLS_MAX_DIGEST_SIZE * 2 + 1]; |
986 | | |
987 | 754 | PTLS_PROBE(NEW_SECRET, tls, type, ptls_hexdump(hexbuf, secret.base, secret.len)); |
988 | 754 | PTLS_LOG_CONN(new_secret, tls, { PTLS_LOG_ELEMENT_SAFESTR(label, type); }); |
989 | | |
990 | 754 | if (tls->ctx->log_event != NULL) |
991 | 0 | tls->ctx->log_event->cb(tls->ctx->log_event, tls, type, "%s", ptls_hexdump(hexbuf, secret.base, secret.len)); |
992 | 754 | } |
993 | | |
994 | | /** |
995 | | * This function preserves the flags and modes (e.g., `offered`, `accepted`, `cipher`), they can be used afterwards. |
996 | | */ |
997 | | static void clear_ech(struct st_ptls_ech_t *ech, int is_server) |
998 | 2.50k | { |
999 | 2.50k | if (ech->aead != NULL) { |
1000 | 0 | ptls_aead_free(ech->aead); |
1001 | 0 | ech->aead = NULL; |
1002 | 0 | } |
1003 | 2.50k | ptls_clear_memory(ech->inner_client_random, PTLS_HELLO_RANDOM_SIZE); |
1004 | 2.50k | if (!is_server) { |
1005 | 0 | free(ech->client.enc.base); |
1006 | 0 | ech->client.enc = ptls_iovec_init(NULL, 0); |
1007 | 0 | if (ech->client.public_name != NULL) { |
1008 | 0 | free(ech->client.public_name); |
1009 | 0 | ech->client.public_name = NULL; |
1010 | 0 | } |
1011 | 0 | free(ech->client.first_ech.base); |
1012 | 0 | ech->client.first_ech = ptls_iovec_init(NULL, 0); |
1013 | 0 | } |
1014 | 2.50k | } |
1015 | | |
1016 | | /** |
1017 | | * Decodes one ECHConfigContents (tls-esni-15 section 4). `decoded->kem` and `cipher` may be NULL even when the function returns |
1018 | | * zero, if the corresponding entries are not found. |
1019 | | */ |
1020 | | static int decode_one_ech_config(ptls_hpke_kem_t **kems, ptls_hpke_cipher_suite_t **ciphers, |
1021 | | struct st_decoded_ech_config_t *decoded, const uint8_t **src, const uint8_t *const end) |
1022 | 0 | { |
1023 | 0 | char *public_name_buf = NULL; |
1024 | 0 | int ret; |
1025 | |
|
1026 | 0 | *decoded = (struct st_decoded_ech_config_t){0}; |
1027 | |
|
1028 | 0 | if ((ret = ptls_decode8(&decoded->id, src, end)) != 0) |
1029 | 0 | goto Exit; |
1030 | 0 | uint16_t kem_id; |
1031 | 0 | if ((ret = ptls_decode16(&kem_id, src, end)) != 0) |
1032 | 0 | goto Exit; |
1033 | 0 | for (size_t i = 0; kems[i] != NULL; ++i) { |
1034 | 0 | if (kems[i]->id == kem_id) { |
1035 | 0 | decoded->kem = kems[i]; |
1036 | 0 | break; |
1037 | 0 | } |
1038 | 0 | } |
1039 | 0 | ptls_decode_open_block(*src, end, 2, { |
1040 | 0 | if (*src == end) { |
1041 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
1042 | 0 | goto Exit; |
1043 | 0 | } |
1044 | 0 | decoded->public_key = ptls_iovec_init(*src, end - *src); |
1045 | 0 | *src = end; |
1046 | 0 | }); |
1047 | 0 | ptls_decode_open_block(*src, end, 2, { |
1048 | 0 | do { |
1049 | 0 | uint16_t kdf_id; |
1050 | 0 | uint16_t aead_id; |
1051 | 0 | if ((ret = ptls_decode16(&kdf_id, src, end)) != 0) |
1052 | 0 | goto Exit; |
1053 | 0 | if ((ret = ptls_decode16(&aead_id, src, end)) != 0) |
1054 | 0 | goto Exit; |
1055 | 0 | if (decoded->cipher == NULL) { |
1056 | 0 | for (size_t i = 0; ciphers[i] != NULL; ++i) { |
1057 | 0 | if (ciphers[i]->id.kdf == kdf_id && ciphers[i]->id.aead == aead_id) { |
1058 | 0 | decoded->cipher = ciphers[i]; |
1059 | 0 | break; |
1060 | 0 | } |
1061 | 0 | } |
1062 | 0 | } |
1063 | 0 | } while (*src != end); |
1064 | 0 | }); |
1065 | 0 | if ((ret = ptls_decode8(&decoded->max_name_length, src, end)) != 0) |
1066 | 0 | goto Exit; |
1067 | | |
1068 | 0 | #define SKIP_DECODED() \ |
1069 | 0 | do { \ |
1070 | 0 | decoded->kem = NULL; \ |
1071 | 0 | decoded->cipher = NULL; \ |
1072 | 0 | } while (0) |
1073 | | |
1074 | | /* Decode public_name. The specification requires clients to ignore (upon parsing ESNIConfigList) or reject (upon handshake) |
1075 | | * public names that are not DNS names or IPv4 addresses. We ignore IPv4 and v6 addresses during parsing (IPv6 addresses never |
1076 | | * looks like DNS names), and delegate the responsibility of rejecting non-DNS names to the certificate verify callback. */ |
1077 | 0 | ptls_decode_open_block(*src, end, 1, { |
1078 | 0 | if (*src == end) { |
1079 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
1080 | 0 | goto Exit; |
1081 | 0 | } |
1082 | 0 | if ((public_name_buf = duplicate_as_str(*src, end - *src)) == NULL) { |
1083 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
1084 | 0 | goto Exit; |
1085 | 0 | } |
1086 | 0 | if (ptls_server_name_is_ipaddr(public_name_buf)) { |
1087 | 0 | SKIP_DECODED(); |
1088 | 0 | } else { |
1089 | 0 | decoded->public_name = ptls_iovec_init(*src, end - *src); |
1090 | 0 | } |
1091 | 0 | *src = end; |
1092 | 0 | }); |
1093 | | |
1094 | 0 | ptls_decode_block(*src, end, 2, { |
1095 | 0 | while (*src < end) { |
1096 | 0 | uint16_t type; |
1097 | 0 | if ((ret = ptls_decode16(&type, src, end)) != 0) |
1098 | 0 | goto Exit; |
1099 | 0 | ptls_decode_open_block(*src, end, 2, { *src = end; }); |
1100 | | /* if a critital extension is found, indicate that the config cannot be used */ |
1101 | 0 | if ((type & 0x8000) != 0) |
1102 | 0 | SKIP_DECODED(); |
1103 | 0 | } |
1104 | 0 | }); |
1105 | | |
1106 | 0 | #undef SKIP_DECODED |
1107 | | |
1108 | 0 | Exit: |
1109 | 0 | free(public_name_buf); |
1110 | 0 | return ret; |
1111 | 0 | } |
1112 | | |
1113 | | static int client_decode_ech_config_list(ptls_context_t *ctx, struct st_decoded_ech_config_t *decoded, ptls_iovec_t config_list) |
1114 | 0 | { |
1115 | 0 | const uint8_t *src = config_list.base, *const end = src + config_list.len; |
1116 | 0 | int match_found = 0, ret; |
1117 | |
|
1118 | 0 | *decoded = (struct st_decoded_ech_config_t){0}; |
1119 | |
|
1120 | 0 | ptls_decode_block(src, end, 2, { |
1121 | 0 | do { |
1122 | 0 | const uint8_t *config_start = src; |
1123 | 0 | uint16_t version; |
1124 | 0 | if ((ret = ptls_decode16(&version, &src, end)) != 0) |
1125 | 0 | goto Exit; |
1126 | 0 | ptls_decode_open_block(src, end, 2, { |
1127 | | /* If the block is the one that we recognize, parse it, then adopt if if possible. Otherwise, skip. */ |
1128 | 0 | if (version == PTLS_ECH_CONFIG_VERSION) { |
1129 | 0 | struct st_decoded_ech_config_t thisconf; |
1130 | 0 | if ((ret = decode_one_ech_config(ctx->ech.client.kems, ctx->ech.client.ciphers, &thisconf, &src, end)) != 0) |
1131 | 0 | goto Exit; |
1132 | 0 | if (!match_found && thisconf.kem != NULL && thisconf.cipher != NULL) { |
1133 | 0 | *decoded = thisconf; |
1134 | 0 | decoded->bytes = ptls_iovec_init(config_start, end - config_start); |
1135 | 0 | match_found = 1; |
1136 | 0 | } |
1137 | 0 | } else { |
1138 | 0 | src = end; |
1139 | 0 | } |
1140 | 0 | }); |
1141 | 0 | } while (src != end); |
1142 | 0 | }); |
1143 | 0 | ret = 0; |
1144 | |
|
1145 | 0 | Exit: |
1146 | 0 | if (ret != 0) |
1147 | 0 | *decoded = (struct st_decoded_ech_config_t){0}; |
1148 | 0 | return ret; |
1149 | 0 | } |
1150 | | |
1151 | | static int client_setup_ech(struct st_ptls_ech_t *ech, struct st_decoded_ech_config_t *decoded, |
1152 | | void (*random_bytes)(void *, size_t)) |
1153 | 0 | { |
1154 | 0 | ptls_buffer_t infobuf; |
1155 | 0 | uint8_t infobuf_smallbuf[256]; |
1156 | 0 | int ret; |
1157 | | |
1158 | | /* setup `enc` and `aead` by running HPKE */ |
1159 | 0 | ptls_buffer_init(&infobuf, infobuf_smallbuf, sizeof(infobuf_smallbuf)); |
1160 | 0 | ptls_buffer_pushv(&infobuf, ech_info_prefix, sizeof(ech_info_prefix)); |
1161 | 0 | ptls_buffer_pushv(&infobuf, decoded->bytes.base, decoded->bytes.len); |
1162 | 0 | if ((ret = ptls_hpke_setup_base_s(decoded->kem, decoded->cipher, &ech->client.enc, &ech->aead, decoded->public_key, |
1163 | 0 | ptls_iovec_init(infobuf.base, infobuf.off))) != 0) |
1164 | 0 | goto Exit; |
1165 | | |
1166 | | /* setup the rest */ |
1167 | 0 | ech->config_id = decoded->id; |
1168 | 0 | ech->kem = decoded->kem; |
1169 | 0 | ech->cipher = decoded->cipher; |
1170 | 0 | random_bytes(ech->inner_client_random, PTLS_HELLO_RANDOM_SIZE); |
1171 | 0 | ech->client.max_name_length = decoded->max_name_length; |
1172 | 0 | if ((ech->client.public_name = duplicate_as_str(decoded->public_name.base, decoded->public_name.len)) == NULL) { |
1173 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
1174 | 0 | goto Exit; |
1175 | 0 | } |
1176 | | |
1177 | 0 | Exit: |
1178 | 0 | if (ret != 0) |
1179 | 0 | clear_ech(ech, 0); |
1180 | 0 | return ret; |
1181 | 0 | } |
1182 | | |
1183 | | static void client_setup_ech_grease(struct st_ptls_ech_t *ech, void (*random_bytes)(void *, size_t), ptls_hpke_kem_t **kems, |
1184 | | ptls_hpke_cipher_suite_t **ciphers, const char *sni_name) |
1185 | 0 | { |
1186 | 0 | static const size_t x25519_key_size = 32; |
1187 | 0 | uint8_t random_secret[PTLS_AES128_KEY_SIZE + PTLS_AES_IV_SIZE]; |
1188 | | |
1189 | | /* pick up X25519, AES-128-GCM or bail out */ |
1190 | 0 | for (size_t i = 0; kems[i] != NULL; ++i) { |
1191 | 0 | if (kems[i]->id == PTLS_HPKE_KEM_X25519_SHA256) { |
1192 | 0 | ech->kem = kems[i]; |
1193 | 0 | break; |
1194 | 0 | } |
1195 | 0 | } |
1196 | 0 | for (size_t i = 0; ciphers[i] != NULL; ++i) { |
1197 | 0 | if (ciphers[i]->id.kdf == PTLS_HPKE_HKDF_SHA256 && ciphers[i]->id.aead == PTLS_HPKE_AEAD_AES_128_GCM) { |
1198 | 0 | ech->cipher = ciphers[i]; |
1199 | 0 | break; |
1200 | 0 | } |
1201 | 0 | } |
1202 | 0 | if (ech->kem == NULL || ech->cipher == NULL) |
1203 | 0 | goto Fail; |
1204 | | |
1205 | | /* aead is generated from random */ |
1206 | 0 | random_bytes(random_secret, sizeof(random_secret)); |
1207 | 0 | ech->aead = ptls_aead_new_direct(ech->cipher->aead, 1, random_secret, random_secret + PTLS_AES128_KEY_SIZE); |
1208 | | |
1209 | | /* `enc` is random bytes */ |
1210 | 0 | if ((ech->client.enc.base = malloc(x25519_key_size)) == NULL) |
1211 | 0 | goto Fail; |
1212 | 0 | ech->client.enc.len = x25519_key_size; |
1213 | 0 | random_bytes(ech->client.enc.base, ech->client.enc.len); |
1214 | | |
1215 | | /* setup the rest (inner_client_random is left zeros) */ |
1216 | 0 | random_bytes(&ech->config_id, sizeof(ech->config_id)); |
1217 | 0 | ech->client.max_name_length = 64; |
1218 | 0 | if ((ech->client.public_name = duplicate_as_str(sni_name, strlen(sni_name))) == NULL) |
1219 | 0 | goto Fail; |
1220 | | |
1221 | 0 | return; |
1222 | | |
1223 | 0 | Fail: |
1224 | 0 | clear_ech(ech, 0); |
1225 | 0 | } |
1226 | | |
1227 | 0 | #define ECH_CONFIRMATION_SERVER_HELLO "ech accept confirmation" |
1228 | 0 | #define ECH_CONFIRMATION_HRR "hrr ech accept confirmation" |
1229 | | static int ech_calc_confirmation(ptls_key_schedule_t *sched, void *dst, const uint8_t *inner_random, const char *label, |
1230 | | ptls_iovec_t message) |
1231 | 0 | { |
1232 | 0 | ptls_hash_context_t *hash = NULL; |
1233 | 0 | uint8_t secret[PTLS_MAX_DIGEST_SIZE], transcript_hash[PTLS_MAX_DIGEST_SIZE]; |
1234 | 0 | int ret; |
1235 | | |
1236 | | /* calc transcript hash using the modified ServerHello / HRR */ |
1237 | 0 | if ((hash = sched->hashes[0].ctx->clone_(sched->hashes[0].ctx)) == NULL) { |
1238 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
1239 | 0 | goto Exit; |
1240 | 0 | } |
1241 | 0 | hash->update(hash, message.base, message.len); |
1242 | 0 | hash->final(hash, transcript_hash, PTLS_HASH_FINAL_MODE_FREE); |
1243 | 0 | hash = NULL; |
1244 | | |
1245 | | /* HKDF extract and expand */ |
1246 | 0 | if ((ret = ptls_hkdf_extract(sched->hashes[0].algo, secret, ptls_iovec_init(NULL, 0), |
1247 | 0 | ptls_iovec_init(inner_random, PTLS_HELLO_RANDOM_SIZE))) != 0) |
1248 | 0 | goto Exit; |
1249 | 0 | if ((ret = ptls_hkdf_expand_label(sched->hashes[0].algo, dst, 8, ptls_iovec_init(secret, sched->hashes[0].algo->digest_size), |
1250 | 0 | label, ptls_iovec_init(transcript_hash, sched->hashes[0].algo->digest_size), NULL)) != 0) |
1251 | 0 | goto Exit; |
1252 | | |
1253 | 0 | Exit: |
1254 | 0 | ptls_clear_memory(secret, sizeof(secret)); |
1255 | 0 | ptls_clear_memory(transcript_hash, sizeof(transcript_hash)); |
1256 | 0 | if (hash != NULL) |
1257 | 0 | hash->final(hash, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1258 | 0 | return ret; |
1259 | 0 | } |
1260 | | |
1261 | | static void key_schedule_free(ptls_key_schedule_t *sched) |
1262 | 671 | { |
1263 | 671 | size_t i; |
1264 | 671 | ptls_clear_memory(sched->secret, sizeof(sched->secret)); |
1265 | 1.34k | for (i = 0; i != sched->num_hashes; ++i) { |
1266 | 671 | sched->hashes[i].ctx->final(sched->hashes[i].ctx, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1267 | 671 | if (sched->hashes[i].ctx_outer != NULL) |
1268 | 0 | sched->hashes[i].ctx_outer->final(sched->hashes[i].ctx_outer, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1269 | 671 | } |
1270 | 671 | free(sched); |
1271 | 671 | } |
1272 | | |
1273 | | static ptls_key_schedule_t *key_schedule_new(ptls_cipher_suite_t *preferred, ptls_cipher_suite_t **offered, int use_outer) |
1274 | 671 | { |
1275 | 671 | #define FOREACH_HASH(block) \ |
1276 | 1.34k | do { \ |
1277 | 1.34k | ptls_cipher_suite_t *cs; \ |
1278 | 1.34k | if ((cs = preferred) != NULL) { \ |
1279 | 2.01k | block \ |
1280 | 1.34k | } \ |
1281 | 1.34k | if (offered != NULL) { \ |
1282 | 0 | size_t i, j; \ |
1283 | 0 | for (i = 0; (cs = offered[i]) != NULL; ++i) { \ |
1284 | 0 | if (preferred == NULL || cs->hash != preferred->hash) { \ |
1285 | 0 | for (j = 0; j != i; ++j) \ |
1286 | 0 | if (cs->hash == offered[j]->hash) \ |
1287 | 0 | break; \ |
1288 | 0 | if (j == i) { \ |
1289 | 0 | block \ |
1290 | 0 | } \ |
1291 | 0 | } \ |
1292 | 0 | } \ |
1293 | 0 | } \ |
1294 | 1.34k | } while (0) |
1295 | | |
1296 | 671 | ptls_key_schedule_t *sched; |
1297 | | |
1298 | 671 | { /* allocate */ |
1299 | 671 | size_t num_hashes = 0; |
1300 | 671 | FOREACH_HASH({ ++num_hashes; }); |
1301 | 671 | if ((sched = malloc(offsetof(ptls_key_schedule_t, hashes) + sizeof(sched->hashes[0]) * num_hashes)) == NULL) |
1302 | 0 | return NULL; |
1303 | 671 | *sched = (ptls_key_schedule_t){0}; |
1304 | 671 | } |
1305 | | |
1306 | | /* setup the hash algos and contexts */ |
1307 | 671 | FOREACH_HASH({ |
1308 | 671 | sched->hashes[sched->num_hashes].algo = cs->hash; |
1309 | 671 | if ((sched->hashes[sched->num_hashes].ctx = cs->hash->create()) == NULL) |
1310 | 671 | goto Fail; |
1311 | 671 | if (use_outer) { |
1312 | 671 | if ((sched->hashes[sched->num_hashes].ctx_outer = cs->hash->create()) == NULL) |
1313 | 671 | goto Fail; |
1314 | 671 | } else { |
1315 | 671 | sched->hashes[sched->num_hashes].ctx_outer = NULL; |
1316 | 671 | } |
1317 | 671 | ++sched->num_hashes; |
1318 | 671 | }); |
1319 | | |
1320 | 671 | return sched; |
1321 | 0 | Fail: |
1322 | 0 | key_schedule_free(sched); |
1323 | 0 | return NULL; |
1324 | | |
1325 | 671 | #undef FOREACH_HASH |
1326 | 671 | } |
1327 | | |
1328 | | static int key_schedule_extract(ptls_key_schedule_t *sched, ptls_iovec_t ikm) |
1329 | 1.01k | { |
1330 | 1.01k | int ret; |
1331 | | |
1332 | 1.01k | if (ikm.base == NULL) |
1333 | 756 | ikm = ptls_iovec_init(zeroes_of_max_digest_size, sched->hashes[0].algo->digest_size); |
1334 | | |
1335 | 1.01k | if (sched->generation != 0 && |
1336 | 498 | (ret = ptls_hkdf_expand_label(sched->hashes[0].algo, sched->secret, sched->hashes[0].algo->digest_size, |
1337 | 498 | ptls_iovec_init(sched->secret, sched->hashes[0].algo->digest_size), "derived", |
1338 | 498 | ptls_iovec_init(sched->hashes[0].algo->empty_digest, sched->hashes[0].algo->digest_size), |
1339 | 498 | NULL)) != 0) |
1340 | 0 | return ret; |
1341 | | |
1342 | 1.01k | ++sched->generation; |
1343 | 1.01k | ret = ptls_hkdf_extract(sched->hashes[0].algo, sched->secret, |
1344 | 1.01k | ptls_iovec_init(sched->secret, sched->hashes[0].algo->digest_size), ikm); |
1345 | 1.01k | PTLS_DEBUGF("%s: %u, %02x%02x\n", __FUNCTION__, sched->generation, (int)sched->secret[0], (int)sched->secret[1]); |
1346 | 1.01k | return ret; |
1347 | 1.01k | } |
1348 | | |
1349 | | static int key_schedule_select_cipher(ptls_key_schedule_t *sched, ptls_cipher_suite_t *cs, int reset, ptls_iovec_t reset_ikm) |
1350 | 0 | { |
1351 | 0 | size_t found_slot = SIZE_MAX, i; |
1352 | 0 | int ret; |
1353 | |
|
1354 | 0 | assert(sched->generation == 1); |
1355 | | |
1356 | | /* find the one, while freeing others */ |
1357 | 0 | for (i = 0; i != sched->num_hashes; ++i) { |
1358 | 0 | if (sched->hashes[i].algo == cs->hash) { |
1359 | 0 | assert(found_slot == SIZE_MAX); |
1360 | 0 | found_slot = i; |
1361 | 0 | } else { |
1362 | 0 | sched->hashes[i].ctx->final(sched->hashes[i].ctx, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1363 | 0 | if (sched->hashes[i].ctx_outer != NULL) |
1364 | 0 | sched->hashes[i].ctx_outer->final(sched->hashes[i].ctx_outer, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1365 | 0 | } |
1366 | 0 | } |
1367 | 0 | assert(found_slot != SIZE_MAX); |
1368 | 0 | if (found_slot != 0) { |
1369 | 0 | sched->hashes[0] = sched->hashes[found_slot]; |
1370 | 0 | reset = 1; |
1371 | 0 | } |
1372 | 0 | sched->num_hashes = 1; |
1373 | | |
1374 | | /* recalculate the hash if a different hash as been selected than the one we used for calculating the early secrets */ |
1375 | 0 | if (reset) { |
1376 | 0 | --sched->generation; |
1377 | 0 | memset(sched->secret, 0, sizeof(sched->secret)); |
1378 | 0 | if ((ret = key_schedule_extract(sched, reset_ikm)) != 0) |
1379 | 0 | goto Exit; |
1380 | 0 | } |
1381 | | |
1382 | 0 | ret = 0; |
1383 | 0 | Exit: |
1384 | 0 | return ret; |
1385 | 0 | } |
1386 | | |
1387 | | static void key_schedule_select_outer(ptls_key_schedule_t *sched) |
1388 | 0 | { |
1389 | | /* This function is called when receiving a cleartext message (Server Hello), after the cipher-suite is determined (and hence |
1390 | | * the hash also), if ECH was offered */ |
1391 | 0 | assert(sched->generation == 1); |
1392 | 0 | assert(sched->num_hashes == 1); |
1393 | 0 | assert(sched->hashes[0].ctx_outer != NULL); |
1394 | | |
1395 | 0 | sched->hashes[0].ctx->final(sched->hashes[0].ctx, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1396 | 0 | sched->hashes[0].ctx = sched->hashes[0].ctx_outer; |
1397 | 0 | sched->hashes[0].ctx_outer = NULL; |
1398 | 0 | } |
1399 | | |
1400 | | void ptls__key_schedule_update_hash(ptls_key_schedule_t *sched, const uint8_t *msg, size_t msglen, int use_outer) |
1401 | 2.17k | { |
1402 | 2.17k | size_t i; |
1403 | | |
1404 | 2.17k | PTLS_DEBUGF("%s:%p:len=%zu\n", __FUNCTION__, sched, msglen); |
1405 | 4.35k | for (i = 0; i != sched->num_hashes; ++i) { |
1406 | 2.17k | ptls_hash_context_t *ctx = use_outer ? sched->hashes[i].ctx_outer : sched->hashes[i].ctx; |
1407 | 2.17k | ctx->update(ctx, msg, msglen); |
1408 | | #if defined(PTLS_DEBUG) && PTLS_DEBUG |
1409 | | { |
1410 | | uint8_t digest[PTLS_MAX_DIGEST_SIZE]; |
1411 | | ctx->final(ctx, digest, PTLS_HASH_FINAL_MODE_SNAPSHOT); |
1412 | | PTLS_DEBUGF(" %zu: %02x%02x%02x%02x\n", i, digest[0], digest[1], digest[2], digest[3]); |
1413 | | } |
1414 | | #endif |
1415 | 2.17k | } |
1416 | 2.17k | } |
1417 | | |
1418 | | static void key_schedule_update_ch1hash_prefix(ptls_key_schedule_t *sched) |
1419 | 0 | { |
1420 | 0 | uint8_t prefix[4] = {PTLS_HANDSHAKE_TYPE_MESSAGE_HASH, 0, 0, (uint8_t)sched->hashes[0].algo->digest_size}; |
1421 | 0 | ptls__key_schedule_update_hash(sched, prefix, sizeof(prefix), 0); |
1422 | 0 | } |
1423 | | |
1424 | | static void key_schedule_extract_ch1hash(ptls_key_schedule_t *sched, uint8_t *hash) |
1425 | 0 | { |
1426 | 0 | assert(sched->hashes[0].ctx_outer == NULL); |
1427 | 0 | sched->hashes[0].ctx->final(sched->hashes[0].ctx, hash, PTLS_HASH_FINAL_MODE_RESET); |
1428 | 0 | } |
1429 | | |
1430 | | static void key_schedule_transform_post_ch1hash(ptls_key_schedule_t *sched) |
1431 | 154 | { |
1432 | 154 | size_t digest_size = sched->hashes[0].algo->digest_size; |
1433 | 154 | ptls_hash_context_t *hashes[3] = {sched->hashes[0].ctx, sched->hashes[0].ctx_outer, NULL}; |
1434 | 154 | uint8_t ch1hash[PTLS_MAX_DIGEST_SIZE]; |
1435 | 154 | uint8_t prefix[4] = {PTLS_HANDSHAKE_TYPE_MESSAGE_HASH, 0, 0, (uint8_t)digest_size}; |
1436 | | |
1437 | 308 | for (size_t i = 0; hashes[i] != NULL; ++i) { |
1438 | 154 | hashes[i]->final(hashes[i], ch1hash, PTLS_HASH_FINAL_MODE_RESET); |
1439 | 154 | hashes[i]->update(hashes[i], prefix, sizeof(prefix)); |
1440 | 154 | hashes[i]->update(hashes[i], ch1hash, digest_size); |
1441 | 154 | } |
1442 | | |
1443 | 154 | ptls_clear_memory(ch1hash, sizeof(ch1hash)); |
1444 | 154 | } |
1445 | | |
1446 | | static int derive_secret_with_hash(ptls_key_schedule_t *sched, void *secret, const char *label, const uint8_t *hash) |
1447 | 1.24k | { |
1448 | 1.24k | int ret = ptls_hkdf_expand_label(sched->hashes[0].algo, secret, sched->hashes[0].algo->digest_size, |
1449 | 1.24k | ptls_iovec_init(sched->secret, sched->hashes[0].algo->digest_size), label, |
1450 | 1.24k | ptls_iovec_init(hash, sched->hashes[0].algo->digest_size), NULL); |
1451 | 1.24k | PTLS_DEBUGF("%s: (label=%s, hash=%02x%02x) => %02x%02x\n", __FUNCTION__, label, hash[0], hash[1], ((uint8_t *)secret)[0], |
1452 | 1.24k | ((uint8_t *)secret)[1]); |
1453 | 1.24k | return ret; |
1454 | 1.24k | } |
1455 | | |
1456 | | static int derive_secret(ptls_key_schedule_t *sched, void *secret, const char *label) |
1457 | 1.24k | { |
1458 | 1.24k | uint8_t hash_value[PTLS_MAX_DIGEST_SIZE]; |
1459 | | |
1460 | 1.24k | sched->hashes[0].ctx->final(sched->hashes[0].ctx, hash_value, PTLS_HASH_FINAL_MODE_SNAPSHOT); |
1461 | 1.24k | int ret = derive_secret_with_hash(sched, secret, label, hash_value); |
1462 | 1.24k | ptls_clear_memory(hash_value, sizeof(hash_value)); |
1463 | 1.24k | return ret; |
1464 | 1.24k | } |
1465 | | |
1466 | | static int derive_secret_with_empty_digest(ptls_key_schedule_t *sched, void *secret, const char *label) |
1467 | 1 | { |
1468 | 1 | return derive_secret_with_hash(sched, secret, label, sched->hashes[0].algo->empty_digest); |
1469 | 1 | } |
1470 | | |
1471 | | static int derive_exporter_secret(ptls_t *tls, int is_early) |
1472 | 242 | { |
1473 | 242 | int ret; |
1474 | | |
1475 | 242 | if (!tls->ctx->use_exporter) |
1476 | 242 | return 0; |
1477 | | |
1478 | 0 | uint8_t **slot = is_early ? &tls->exporter_master_secret.early : &tls->exporter_master_secret.one_rtt; |
1479 | 0 | assert(*slot == NULL); |
1480 | 0 | if ((*slot = malloc(tls->key_schedule->hashes[0].algo->digest_size)) == NULL) |
1481 | 0 | return PTLS_ERROR_NO_MEMORY; |
1482 | | |
1483 | 0 | if ((ret = derive_secret(tls->key_schedule, *slot, is_early ? "e exp master" : "exp master")) != 0) |
1484 | 0 | return ret; |
1485 | | |
1486 | 0 | log_secret(tls, is_early ? "EARLY_EXPORTER_SECRET" : "EXPORTER_SECRET", |
1487 | 0 | ptls_iovec_init(*slot, tls->key_schedule->hashes[0].algo->digest_size)); |
1488 | |
|
1489 | 0 | return 0; |
1490 | 0 | } |
1491 | | |
1492 | | static void free_exporter_master_secret(ptls_t *tls, int is_early) |
1493 | 4.49k | { |
1494 | 4.49k | uint8_t *slot = is_early ? tls->exporter_master_secret.early : tls->exporter_master_secret.one_rtt; |
1495 | 4.49k | if (slot == NULL) |
1496 | 4.49k | return; |
1497 | 4.49k | assert(tls->key_schedule != NULL); |
1498 | 0 | ptls_clear_memory(slot, tls->key_schedule->hashes[0].algo->digest_size); |
1499 | 0 | free(slot); |
1500 | 0 | } |
1501 | | |
1502 | | static int derive_resumption_secret(ptls_key_schedule_t *sched, uint8_t *secret, ptls_iovec_t nonce) |
1503 | 245 | { |
1504 | 245 | int ret; |
1505 | | |
1506 | 245 | if ((ret = derive_secret(sched, secret, "res master")) != 0) |
1507 | 0 | goto Exit; |
1508 | 245 | if ((ret = ptls_hkdf_expand_label(sched->hashes[0].algo, secret, sched->hashes[0].algo->digest_size, |
1509 | 245 | ptls_iovec_init(secret, sched->hashes[0].algo->digest_size), "resumption", nonce, NULL)) != 0) |
1510 | 0 | goto Exit; |
1511 | | |
1512 | 245 | Exit: |
1513 | 245 | if (ret != 0) |
1514 | 0 | ptls_clear_memory(secret, sched->hashes[0].algo->digest_size); |
1515 | 245 | return ret; |
1516 | 245 | } |
1517 | | |
1518 | | static int decode_new_session_ticket(ptls_t *tls, uint32_t *lifetime, uint32_t *age_add, ptls_iovec_t *nonce, ptls_iovec_t *ticket, |
1519 | | uint32_t *max_early_data_size, const uint8_t *src, const uint8_t *const end) |
1520 | 0 | { |
1521 | 0 | uint16_t exttype; |
1522 | 0 | int ret; |
1523 | |
|
1524 | 0 | if ((ret = ptls_decode32(lifetime, &src, end)) != 0) |
1525 | 0 | goto Exit; |
1526 | 0 | if ((ret = ptls_decode32(age_add, &src, end)) != 0) |
1527 | 0 | goto Exit; |
1528 | 0 | ptls_decode_open_block(src, end, 1, { |
1529 | 0 | *nonce = ptls_iovec_init(src, end - src); |
1530 | 0 | src = end; |
1531 | 0 | }); |
1532 | 0 | ptls_decode_open_block(src, end, 2, { |
1533 | 0 | if (src == end) { |
1534 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
1535 | 0 | goto Exit; |
1536 | 0 | } |
1537 | 0 | *ticket = ptls_iovec_init(src, end - src); |
1538 | 0 | src = end; |
1539 | 0 | }); |
1540 | | |
1541 | 0 | *max_early_data_size = 0; |
1542 | 0 | decode_extensions(src, end, PTLS_HANDSHAKE_TYPE_NEW_SESSION_TICKET, &exttype, { |
1543 | 0 | if (tls->ctx->on_extension != NULL && |
1544 | 0 | (ret = tls->ctx->on_extension->cb(tls->ctx->on_extension, tls, PTLS_HANDSHAKE_TYPE_NEW_SESSION_TICKET, exttype, |
1545 | 0 | ptls_iovec_init(src, end - src)) != 0)) |
1546 | 0 | goto Exit; |
1547 | 0 | switch (exttype) { |
1548 | 0 | case PTLS_EXTENSION_TYPE_EARLY_DATA: |
1549 | 0 | if ((ret = ptls_decode32(max_early_data_size, &src, end)) != 0) |
1550 | 0 | goto Exit; |
1551 | 0 | break; |
1552 | 0 | default: |
1553 | 0 | src = end; |
1554 | 0 | break; |
1555 | 0 | } |
1556 | 0 | }); |
1557 | | |
1558 | 0 | ret = 0; |
1559 | 0 | Exit: |
1560 | 0 | return ret; |
1561 | 0 | } |
1562 | | |
1563 | | static int decode_stored_session_ticket(ptls_t *tls, ptls_key_exchange_algorithm_t **key_share, ptls_cipher_suite_t **cs, |
1564 | | ptls_iovec_t *secret, uint32_t *obfuscated_ticket_age, ptls_iovec_t *ticket, |
1565 | | uint32_t *max_early_data_size, const uint8_t *src, const uint8_t *const end) |
1566 | 0 | { |
1567 | 0 | uint16_t kxid, csid; |
1568 | 0 | uint32_t lifetime, age_add; |
1569 | 0 | uint64_t obtained_at, now; |
1570 | 0 | ptls_iovec_t nonce; |
1571 | 0 | int ret; |
1572 | | |
1573 | | /* decode */ |
1574 | 0 | if ((ret = ptls_decode64(&obtained_at, &src, end)) != 0) |
1575 | 0 | goto Exit; |
1576 | 0 | if ((ret = ptls_decode16(&kxid, &src, end)) != 0) |
1577 | 0 | goto Exit; |
1578 | 0 | if ((ret = ptls_decode16(&csid, &src, end)) != 0) |
1579 | 0 | goto Exit; |
1580 | 0 | ptls_decode_open_block(src, end, 3, { |
1581 | 0 | if ((ret = decode_new_session_ticket(tls, &lifetime, &age_add, &nonce, ticket, max_early_data_size, src, end)) != 0) |
1582 | 0 | goto Exit; |
1583 | 0 | src = end; |
1584 | 0 | }); |
1585 | 0 | ptls_decode_block(src, end, 2, { |
1586 | 0 | *secret = ptls_iovec_init(src, end - src); |
1587 | 0 | src = end; |
1588 | 0 | }); |
1589 | | |
1590 | 0 | { /* determine the key-exchange */ |
1591 | 0 | ptls_key_exchange_algorithm_t **cand; |
1592 | 0 | for (cand = tls->ctx->key_exchanges; *cand != NULL; ++cand) |
1593 | 0 | if ((*cand)->id == kxid) |
1594 | 0 | break; |
1595 | 0 | if (*cand == NULL) { |
1596 | 0 | ret = PTLS_ERROR_LIBRARY; |
1597 | 0 | goto Exit; |
1598 | 0 | } |
1599 | 0 | *key_share = *cand; |
1600 | 0 | } |
1601 | | |
1602 | 0 | { /* determine the cipher-suite */ |
1603 | 0 | ptls_cipher_suite_t **cand; |
1604 | 0 | for (cand = tls->ctx->cipher_suites; *cand != NULL; ++cand) |
1605 | 0 | if ((*cand)->id == csid) |
1606 | 0 | break; |
1607 | 0 | if (*cand == NULL) { |
1608 | 0 | ret = PTLS_ERROR_LIBRARY; |
1609 | 0 | goto Exit; |
1610 | 0 | } |
1611 | 0 | *cs = *cand; |
1612 | 0 | } |
1613 | | |
1614 | | /* calculate obfuscated_ticket_age */ |
1615 | 0 | now = tls->ctx->get_time->cb(tls->ctx->get_time); |
1616 | 0 | if (!(obtained_at <= now && now - obtained_at < 7 * 86400 * 1000)) { |
1617 | 0 | ret = PTLS_ERROR_LIBRARY; |
1618 | 0 | goto Exit; |
1619 | 0 | } |
1620 | 0 | *obfuscated_ticket_age = (uint32_t)(now - obtained_at) + age_add; |
1621 | |
|
1622 | 0 | ret = 0; |
1623 | 0 | Exit: |
1624 | 0 | return ret; |
1625 | 0 | } |
1626 | | |
1627 | | static int get_traffic_key(ptls_hash_algorithm_t *algo, void *key, size_t key_size, int is_iv, const void *secret, |
1628 | | ptls_iovec_t hash_value, const char *label_prefix) |
1629 | 1.50k | { |
1630 | 1.50k | return ptls_hkdf_expand_label(algo, key, key_size, ptls_iovec_init(secret, algo->digest_size), is_iv ? "iv" : "key", hash_value, |
1631 | 1.50k | label_prefix); |
1632 | 1.50k | } |
1633 | | |
1634 | | static int get_traffic_keys(ptls_aead_algorithm_t *aead, ptls_hash_algorithm_t *hash, void *key, void *iv, const void *secret, |
1635 | | ptls_iovec_t hash_value, const char *label_prefix) |
1636 | 754 | { |
1637 | 754 | int ret; |
1638 | | |
1639 | 754 | if ((ret = get_traffic_key(hash, key, aead->key_size, 0, secret, hash_value, label_prefix)) != 0 || |
1640 | 754 | (ret = get_traffic_key(hash, iv, aead->iv_size, 1, secret, hash_value, label_prefix)) != 0) { |
1641 | 0 | ptls_clear_memory(key, aead->key_size); |
1642 | 0 | ptls_clear_memory(iv, aead->iv_size); |
1643 | 0 | } |
1644 | | |
1645 | 754 | return ret; |
1646 | 754 | } |
1647 | | |
1648 | | static int setup_traffic_protection(ptls_t *tls, int is_enc, const char *secret_label, size_t epoch, uint64_t seq, int skip_notify) |
1649 | 754 | { |
1650 | 754 | static const char *log_labels[2][4] = { |
1651 | 754 | {NULL, "CLIENT_EARLY_TRAFFIC_SECRET", "CLIENT_HANDSHAKE_TRAFFIC_SECRET", "CLIENT_TRAFFIC_SECRET_0"}, |
1652 | 754 | {NULL, NULL, "SERVER_HANDSHAKE_TRAFFIC_SECRET", "SERVER_TRAFFIC_SECRET_0"}}; |
1653 | 754 | struct st_ptls_traffic_protection_t *ctx = is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec; |
1654 | | |
1655 | 754 | if (secret_label != NULL) { |
1656 | 754 | int ret; |
1657 | 754 | if ((ret = derive_secret(tls->key_schedule, ctx->secret, secret_label)) != 0) |
1658 | 0 | return ret; |
1659 | 754 | } |
1660 | | |
1661 | 754 | ctx->epoch = epoch; |
1662 | | |
1663 | 754 | log_secret(tls, log_labels[ptls_is_server(tls) == is_enc][epoch], |
1664 | 754 | ptls_iovec_init(ctx->secret, tls->key_schedule->hashes[0].algo->digest_size)); |
1665 | | |
1666 | | /* special path for applications having their own record layer */ |
1667 | 754 | if (tls->ctx->update_traffic_key != NULL) { |
1668 | 0 | if (skip_notify) |
1669 | 0 | return 0; |
1670 | 0 | return tls->ctx->update_traffic_key->cb(tls->ctx->update_traffic_key, tls, is_enc, epoch, ctx->secret); |
1671 | 0 | } |
1672 | | |
1673 | 754 | if (ctx->aead != NULL) |
1674 | 242 | ptls_aead_free(ctx->aead); |
1675 | 754 | if ((ctx->aead = ptls_aead_new(tls->cipher_suite->aead, tls->cipher_suite->hash, is_enc, ctx->secret, |
1676 | 754 | tls->ctx->hkdf_label_prefix__obsolete)) == NULL) |
1677 | 0 | return PTLS_ERROR_NO_MEMORY; /* TODO obtain error from ptls_aead_new */ |
1678 | 754 | ctx->seq = seq; |
1679 | | |
1680 | | #if defined(PTLS_DEBUG) && PTLS_DEBUG |
1681 | | { |
1682 | | uint8_t static_iv[PTLS_MAX_IV_SIZE]; |
1683 | | ptls_aead_get_iv(ctx->aead, static_iv); |
1684 | | PTLS_DEBUGF("[%s] %02x%02x,%02x%02x\n", log_labels[ptls_is_server(tls)][epoch], (unsigned)ctx->secret[0], |
1685 | | (unsigned)ctx->secret[1], static_iv[0], static_iv[1]); |
1686 | | } |
1687 | | #endif |
1688 | | |
1689 | 754 | return 0; |
1690 | 754 | } |
1691 | | |
1692 | | static int commission_handshake_secret(ptls_t *tls) |
1693 | 0 | { |
1694 | 0 | int is_enc = !ptls_is_server(tls); |
1695 | |
|
1696 | 0 | assert(tls->pending_handshake_secret != NULL); |
1697 | 0 | memcpy((is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec)->secret, tls->pending_handshake_secret, |
1698 | 0 | PTLS_MAX_DIGEST_SIZE); |
1699 | 0 | ptls_clear_memory(tls->pending_handshake_secret, PTLS_MAX_DIGEST_SIZE); |
1700 | 0 | free(tls->pending_handshake_secret); |
1701 | 0 | tls->pending_handshake_secret = NULL; |
1702 | |
|
1703 | 0 | return setup_traffic_protection(tls, is_enc, NULL, 2, 0, 1); |
1704 | 0 | } |
1705 | | |
1706 | | static void log_client_random(ptls_t *tls) |
1707 | 710 | { |
1708 | | #if PICOTLS_USE_DTRACE |
1709 | | char buf[sizeof(tls->client_random) * 2 + 1]; |
1710 | | #endif |
1711 | | |
1712 | 710 | PTLS_PROBE(CLIENT_RANDOM, tls, ptls_hexdump(buf, tls->client_random, sizeof(tls->client_random))); |
1713 | 710 | PTLS_LOG_CONN(client_random, tls, { PTLS_LOG_ELEMENT_HEXDUMP(bytes, tls->client_random, sizeof(tls->client_random)); }); |
1714 | 710 | } |
1715 | | |
1716 | | #define SESSION_IDENTIFIER_MAGIC "ptls0001" /* the number should be changed upon incompatible format change */ |
1717 | | #define SESSION_IDENTIFIER_MAGIC_SIZE (sizeof(SESSION_IDENTIFIER_MAGIC) - 1) |
1718 | | |
1719 | | static int encode_session_identifier(ptls_context_t *ctx, ptls_buffer_t *buf, uint32_t ticket_age_add, ptls_iovec_t ticket_nonce, |
1720 | | ptls_key_schedule_t *sched, const char *server_name, uint16_t key_exchange_id, uint16_t csid, |
1721 | | const char *negotiated_protocol) |
1722 | 245 | { |
1723 | 245 | int ret = 0; |
1724 | | |
1725 | 245 | ptls_buffer_push_block(buf, 2, { |
1726 | | /* format id */ |
1727 | 245 | ptls_buffer_pushv(buf, SESSION_IDENTIFIER_MAGIC, SESSION_IDENTIFIER_MAGIC_SIZE); |
1728 | | /* date */ |
1729 | 245 | ptls_buffer_push64(buf, ctx->get_time->cb(ctx->get_time)); |
1730 | | /* resumption master secret */ |
1731 | 245 | ptls_buffer_push_block(buf, 2, { |
1732 | 245 | if ((ret = ptls_buffer_reserve(buf, sched->hashes[0].algo->digest_size)) != 0) |
1733 | 245 | goto Exit; |
1734 | 245 | if ((ret = derive_resumption_secret(sched, buf->base + buf->off, ticket_nonce)) != 0) |
1735 | 245 | goto Exit; |
1736 | 245 | buf->off += sched->hashes[0].algo->digest_size; |
1737 | 245 | }); |
1738 | | /* key-exchange */ |
1739 | 245 | ptls_buffer_push16(buf, key_exchange_id); |
1740 | | /* cipher-suite */ |
1741 | 245 | ptls_buffer_push16(buf, csid); |
1742 | | /* ticket_age_add */ |
1743 | 245 | ptls_buffer_push32(buf, ticket_age_add); |
1744 | | /* session ID context */ |
1745 | 245 | ptls_buffer_push_block(buf, 2, { |
1746 | 245 | if (ctx->ticket_context.is_set) { |
1747 | 245 | ptls_buffer_pushv(buf, ctx->ticket_context.bytes, sizeof(ctx->ticket_context.bytes)); |
1748 | 245 | } else if (server_name != NULL) { |
1749 | 245 | ptls_buffer_pushv(buf, server_name, strlen(server_name)); |
1750 | 245 | } |
1751 | 245 | }); |
1752 | | /* alpn */ |
1753 | 245 | ptls_buffer_push_block(buf, 1, { |
1754 | 245 | if (negotiated_protocol != NULL) |
1755 | 245 | ptls_buffer_pushv(buf, negotiated_protocol, strlen(negotiated_protocol)); |
1756 | 245 | }); |
1757 | 245 | }); |
1758 | | |
1759 | 245 | Exit: |
1760 | 245 | return ret; |
1761 | 245 | } |
1762 | | |
1763 | | int decode_session_identifier(uint64_t *issued_at, ptls_iovec_t *psk, uint32_t *ticket_age_add, ptls_iovec_t *ticket_ctx, |
1764 | | uint16_t *key_exchange_id, uint16_t *csid, ptls_iovec_t *negotiated_protocol, const uint8_t *src, |
1765 | | const uint8_t *const end) |
1766 | 172 | { |
1767 | 172 | int ret = 0; |
1768 | | |
1769 | 172 | ptls_decode_block(src, end, 2, { |
1770 | 172 | if (end - src < SESSION_IDENTIFIER_MAGIC_SIZE || |
1771 | 172 | memcmp(src, SESSION_IDENTIFIER_MAGIC, SESSION_IDENTIFIER_MAGIC_SIZE) != 0) { |
1772 | 172 | ret = PTLS_ALERT_DECODE_ERROR; |
1773 | 172 | goto Exit; |
1774 | 172 | } |
1775 | 172 | src += SESSION_IDENTIFIER_MAGIC_SIZE; |
1776 | 172 | if ((ret = ptls_decode64(issued_at, &src, end)) != 0) |
1777 | 172 | goto Exit; |
1778 | 172 | ptls_decode_open_block(src, end, 2, { |
1779 | 172 | *psk = ptls_iovec_init(src, end - src); |
1780 | 172 | src = end; |
1781 | 172 | }); |
1782 | 172 | if ((ret = ptls_decode16(key_exchange_id, &src, end)) != 0) |
1783 | 172 | goto Exit; |
1784 | 172 | if ((ret = ptls_decode16(csid, &src, end)) != 0) |
1785 | 172 | goto Exit; |
1786 | 172 | if ((ret = ptls_decode32(ticket_age_add, &src, end)) != 0) |
1787 | 172 | goto Exit; |
1788 | 172 | ptls_decode_open_block(src, end, 2, { |
1789 | 172 | *ticket_ctx = ptls_iovec_init(src, end - src); |
1790 | 172 | src = end; |
1791 | 172 | }); |
1792 | 172 | ptls_decode_open_block(src, end, 1, { |
1793 | 172 | *negotiated_protocol = ptls_iovec_init(src, end - src); |
1794 | 172 | src = end; |
1795 | 172 | }); |
1796 | 172 | }); |
1797 | | |
1798 | 172 | Exit: |
1799 | 172 | return ret; |
1800 | 172 | } |
1801 | | |
1802 | | static size_t build_certificate_verify_signdata(uint8_t *data, ptls_key_schedule_t *sched, const char *context_string) |
1803 | 0 | { |
1804 | 0 | size_t datalen = 0; |
1805 | |
|
1806 | 0 | memset(data + datalen, 32, 64); |
1807 | 0 | datalen += 64; |
1808 | 0 | memcpy(data + datalen, context_string, strlen(context_string) + 1); |
1809 | 0 | datalen += strlen(context_string) + 1; |
1810 | 0 | sched->hashes[0].ctx->final(sched->hashes[0].ctx, data + datalen, PTLS_HASH_FINAL_MODE_SNAPSHOT); |
1811 | 0 | datalen += sched->hashes[0].algo->digest_size; |
1812 | 0 | assert(datalen <= PTLS_MAX_CERTIFICATE_VERIFY_SIGNDATA_SIZE); |
1813 | | |
1814 | 0 | return datalen; |
1815 | 0 | } |
1816 | | |
1817 | | static int calc_verify_data(void *output, ptls_key_schedule_t *sched, const void *secret) |
1818 | 492 | { |
1819 | 492 | ptls_hash_context_t *hmac; |
1820 | 492 | uint8_t digest[PTLS_MAX_DIGEST_SIZE]; |
1821 | 492 | int ret; |
1822 | | |
1823 | 492 | if ((ret = ptls_hkdf_expand_label(sched->hashes[0].algo, digest, sched->hashes[0].algo->digest_size, |
1824 | 492 | ptls_iovec_init(secret, sched->hashes[0].algo->digest_size), "finished", |
1825 | 492 | ptls_iovec_init(NULL, 0), NULL)) != 0) |
1826 | 0 | return ret; |
1827 | 492 | if ((hmac = ptls_hmac_create(sched->hashes[0].algo, digest, sched->hashes[0].algo->digest_size)) == NULL) { |
1828 | 0 | ptls_clear_memory(digest, sizeof(digest)); |
1829 | 0 | return PTLS_ERROR_NO_MEMORY; |
1830 | 0 | } |
1831 | | |
1832 | 492 | sched->hashes[0].ctx->final(sched->hashes[0].ctx, digest, PTLS_HASH_FINAL_MODE_SNAPSHOT); |
1833 | 492 | PTLS_DEBUGF("%s: %02x%02x,%02x%02x\n", __FUNCTION__, ((uint8_t *)secret)[0], ((uint8_t *)secret)[1], digest[0], digest[1]); |
1834 | 492 | hmac->update(hmac, digest, sched->hashes[0].algo->digest_size); |
1835 | 492 | ptls_clear_memory(digest, sizeof(digest)); |
1836 | 492 | hmac->final(hmac, output, PTLS_HASH_FINAL_MODE_FREE); |
1837 | | |
1838 | 492 | return 0; |
1839 | 492 | } |
1840 | | |
1841 | | static int verify_finished(ptls_t *tls, ptls_iovec_t message) |
1842 | 19 | { |
1843 | 19 | uint8_t verify_data[PTLS_MAX_DIGEST_SIZE]; |
1844 | 19 | int ret; |
1845 | | |
1846 | 19 | if (PTLS_HANDSHAKE_HEADER_SIZE + tls->key_schedule->hashes[0].algo->digest_size != message.len) { |
1847 | 15 | ret = PTLS_ALERT_DECODE_ERROR; |
1848 | 15 | goto Exit; |
1849 | 15 | } |
1850 | | |
1851 | 4 | if ((ret = calc_verify_data(verify_data, tls->key_schedule, tls->traffic_protection.dec.secret)) != 0) |
1852 | 0 | goto Exit; |
1853 | 4 | if (!ptls_mem_equal(message.base + PTLS_HANDSHAKE_HEADER_SIZE, verify_data, tls->key_schedule->hashes[0].algo->digest_size)) { |
1854 | 4 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
1855 | 4 | goto Exit; |
1856 | 4 | } |
1857 | | |
1858 | 19 | Exit: |
1859 | 19 | ptls_clear_memory(verify_data, sizeof(verify_data)); |
1860 | 19 | return ret; |
1861 | 4 | } |
1862 | | |
1863 | | static int send_finished(ptls_t *tls, ptls_message_emitter_t *emitter) |
1864 | 242 | { |
1865 | 242 | int ret; |
1866 | | |
1867 | 242 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_FINISHED, { |
1868 | 242 | if ((ret = ptls_buffer_reserve(emitter->buf, tls->key_schedule->hashes[0].algo->digest_size)) != 0) |
1869 | 242 | goto Exit; |
1870 | 242 | if ((ret = calc_verify_data(emitter->buf->base + emitter->buf->off, tls->key_schedule, |
1871 | 242 | tls->traffic_protection.enc.secret)) != 0) |
1872 | 242 | goto Exit; |
1873 | 242 | emitter->buf->off += tls->key_schedule->hashes[0].algo->digest_size; |
1874 | 242 | }); |
1875 | | |
1876 | 242 | Exit: |
1877 | 242 | return ret; |
1878 | 242 | } |
1879 | | |
1880 | | static int send_session_ticket(ptls_t *tls, ptls_message_emitter_t *emitter) |
1881 | 245 | { |
1882 | 245 | ptls_hash_context_t *msghash_backup = tls->key_schedule->hashes[0].ctx->clone_(tls->key_schedule->hashes[0].ctx); |
1883 | 245 | ptls_buffer_t session_id; |
1884 | 245 | char session_id_smallbuf[128]; |
1885 | 245 | uint32_t ticket_age_add; |
1886 | 245 | int ret = 0; |
1887 | | |
1888 | 245 | assert(tls->ctx->ticket_lifetime != 0); |
1889 | 245 | assert(tls->ctx->encrypt_ticket != NULL); |
1890 | | |
1891 | 245 | ptls_buffer_init(&session_id, session_id_smallbuf, sizeof(session_id_smallbuf)); |
1892 | | |
1893 | 245 | { /* calculate verify-data that will be sent by the client */ |
1894 | 245 | size_t orig_off = emitter->buf->off; |
1895 | 245 | if (tls->pending_handshake_secret != NULL && !tls->ctx->omit_end_of_early_data) { |
1896 | 0 | assert(tls->state == PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA); |
1897 | 0 | ptls_buffer_push_message_body(emitter->buf, tls->key_schedule, PTLS_HANDSHAKE_TYPE_END_OF_EARLY_DATA, {}); |
1898 | 0 | emitter->buf->off = orig_off; |
1899 | 0 | } |
1900 | 245 | ptls_buffer_push_message_body(emitter->buf, tls->key_schedule, PTLS_HANDSHAKE_TYPE_FINISHED, { |
1901 | 245 | if ((ret = ptls_buffer_reserve(emitter->buf, tls->key_schedule->hashes[0].algo->digest_size)) != 0) |
1902 | 245 | goto Exit; |
1903 | 245 | if ((ret = calc_verify_data(emitter->buf->base + emitter->buf->off, tls->key_schedule, |
1904 | 245 | tls->pending_handshake_secret != NULL ? tls->pending_handshake_secret |
1905 | 245 | : tls->traffic_protection.dec.secret)) != 0) |
1906 | 245 | goto Exit; |
1907 | 245 | emitter->buf->off += tls->key_schedule->hashes[0].algo->digest_size; |
1908 | 245 | }); |
1909 | 245 | emitter->buf->off = orig_off; |
1910 | 245 | } |
1911 | | |
1912 | 0 | tls->ctx->random_bytes(&ticket_age_add, sizeof(ticket_age_add)); |
1913 | | |
1914 | | /* build the raw nsk */ |
1915 | 245 | if (tls->key_share != NULL && (ret = encode_session_identifier(tls->ctx, &session_id, ticket_age_add, ptls_iovec_init(NULL, 0), |
1916 | 245 | tls->key_schedule, tls->server_name, tls->key_share->id, |
1917 | 245 | tls->cipher_suite->id, tls->negotiated_protocol)) != 0) |
1918 | 0 | goto Exit; |
1919 | | |
1920 | | /* encrypt and send */ |
1921 | 245 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_NEW_SESSION_TICKET, { |
1922 | 245 | ptls_buffer_push32(emitter->buf, tls->ctx->ticket_lifetime); |
1923 | 245 | ptls_buffer_push32(emitter->buf, ticket_age_add); |
1924 | 245 | ptls_buffer_push_block(emitter->buf, 1, {}); |
1925 | 245 | ptls_buffer_push_block(emitter->buf, 2, { |
1926 | 245 | if ((ret = tls->ctx->encrypt_ticket->cb(tls->ctx->encrypt_ticket, tls, 1, emitter->buf, |
1927 | 245 | ptls_iovec_init(session_id.base, session_id.off))) != 0) |
1928 | 245 | goto Exit; |
1929 | 245 | }); |
1930 | 245 | ptls_buffer_push_block(emitter->buf, 2, { |
1931 | 245 | if (tls->ctx->max_early_data_size != 0) |
1932 | 245 | buffer_push_extension(emitter->buf, PTLS_EXTENSION_TYPE_EARLY_DATA, |
1933 | 245 | { ptls_buffer_push32(emitter->buf, tls->ctx->max_early_data_size); }); |
1934 | 245 | }); |
1935 | 245 | }); |
1936 | | |
1937 | 245 | Exit: |
1938 | 245 | ptls_buffer_dispose(&session_id); |
1939 | | |
1940 | | /* restore handshake state */ |
1941 | 245 | tls->key_schedule->hashes[0].ctx->final(tls->key_schedule->hashes[0].ctx, NULL, PTLS_HASH_FINAL_MODE_FREE); |
1942 | 245 | tls->key_schedule->hashes[0].ctx = msghash_backup; |
1943 | | |
1944 | 245 | return ret; |
1945 | 245 | } |
1946 | | |
1947 | | static int push_change_cipher_spec(ptls_t *tls, ptls_message_emitter_t *emitter) |
1948 | 256 | { |
1949 | 256 | int ret; |
1950 | | |
1951 | | /* check if we are requested to (or still need to) */ |
1952 | 256 | if (!tls->send_change_cipher_spec) { |
1953 | 182 | ret = 0; |
1954 | 182 | goto Exit; |
1955 | 182 | } |
1956 | | |
1957 | | /* CCS is a record, can only be sent when using a record-based protocol. */ |
1958 | 74 | if (emitter->begin_message != begin_record_message) { |
1959 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
1960 | 0 | goto Exit; |
1961 | 0 | } |
1962 | | |
1963 | | /* emit CCS */ |
1964 | 74 | buffer_push_record(emitter->buf, PTLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC, { ptls_buffer_push(emitter->buf, 1); }); |
1965 | | |
1966 | 74 | tls->send_change_cipher_spec = 0; |
1967 | 74 | ret = 0; |
1968 | 256 | Exit: |
1969 | 256 | return ret; |
1970 | 74 | } |
1971 | | |
1972 | | static int push_additional_extensions(ptls_handshake_properties_t *properties, ptls_buffer_t *sendbuf) |
1973 | 256 | { |
1974 | 256 | int ret; |
1975 | | |
1976 | 256 | if (properties != NULL && properties->additional_extensions != NULL) { |
1977 | 0 | ptls_raw_extension_t *ext; |
1978 | 0 | for (ext = properties->additional_extensions; ext->type != UINT16_MAX; ++ext) { |
1979 | 0 | buffer_push_extension(sendbuf, ext->type, { ptls_buffer_pushv(sendbuf, ext->data.base, ext->data.len); }); |
1980 | 0 | } |
1981 | 0 | } |
1982 | 256 | ret = 0; |
1983 | 256 | Exit: |
1984 | 256 | return ret; |
1985 | 256 | } |
1986 | | |
1987 | | static int push_signature_algorithms(ptls_verify_certificate_t *vc, ptls_buffer_t *sendbuf) |
1988 | 0 | { |
1989 | | /* The list sent when verify callback is not registered */ |
1990 | 0 | static const uint16_t default_algos[] = {PTLS_SIGNATURE_RSA_PSS_RSAE_SHA384, PTLS_SIGNATURE_RSA_PSS_RSAE_SHA256, |
1991 | 0 | PTLS_SIGNATURE_ECDSA_SECP384R1_SHA384, PTLS_SIGNATURE_ECDSA_SECP256R1_SHA256, |
1992 | 0 | PTLS_SIGNATURE_RSA_PKCS1_SHA256, PTLS_SIGNATURE_RSA_PKCS1_SHA1, UINT16_MAX}; |
1993 | 0 | int ret; |
1994 | |
|
1995 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
1996 | 0 | for (const uint16_t *p = vc != NULL ? vc->algos : default_algos; *p != UINT16_MAX; ++p) |
1997 | 0 | ptls_buffer_push16(sendbuf, *p); |
1998 | 0 | }); |
1999 | | |
2000 | 0 | ret = 0; |
2001 | 0 | Exit: |
2002 | 0 | return ret; |
2003 | 0 | } |
2004 | | |
2005 | | static int decode_signature_algorithms(struct st_ptls_signature_algorithms_t *sa, const uint8_t **src, const uint8_t *end) |
2006 | 331 | { |
2007 | 331 | int ret; |
2008 | | |
2009 | 331 | ptls_decode_block(*src, end, 2, { |
2010 | 331 | do { |
2011 | 331 | uint16_t id; |
2012 | 331 | if ((ret = ptls_decode16(&id, src, end)) != 0) |
2013 | 331 | goto Exit; |
2014 | 331 | if (sa->count < PTLS_ELEMENTSOF(sa->list)) |
2015 | 331 | sa->list[sa->count++] = id; |
2016 | 331 | } while (*src != end); |
2017 | 331 | }); |
2018 | | |
2019 | 264 | ret = 0; |
2020 | 331 | Exit: |
2021 | 331 | return ret; |
2022 | 264 | } |
2023 | | |
2024 | | /** |
2025 | | * @param hash optional argument for restricting the underlying hash algorithm |
2026 | | */ |
2027 | | static int select_cipher(ptls_cipher_suite_t **selected, ptls_cipher_suite_t **candidates, const uint8_t *src, |
2028 | | const uint8_t *const end, int server_preference, int server_chacha_priority, ptls_hash_algorithm_t *hash) |
2029 | 725 | { |
2030 | 725 | size_t found_index = SIZE_MAX; |
2031 | 725 | int ret; |
2032 | | |
2033 | 2.66k | while (src != end) { |
2034 | 2.63k | uint16_t id; |
2035 | 2.63k | if ((ret = ptls_decode16(&id, &src, end)) != 0) |
2036 | 0 | goto Exit; |
2037 | 6.89k | for (size_t i = 0; candidates[i] != NULL; ++i) { |
2038 | 4.95k | if (candidates[i]->id == id && (hash == NULL || candidates[i]->hash == hash)) { |
2039 | 696 | if (server_preference && !(server_chacha_priority && id == PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256)) { |
2040 | | /* preserve smallest matching index, and proceed to the next input */ |
2041 | 0 | if (i < found_index) { |
2042 | 0 | found_index = i; |
2043 | 0 | break; |
2044 | 0 | } |
2045 | 696 | } else { |
2046 | | /* return the pointer matching to the first input that can be used */ |
2047 | 696 | *selected = candidates[i]; |
2048 | 696 | goto Exit; |
2049 | 696 | } |
2050 | 696 | } |
2051 | 4.95k | } |
2052 | | /* first position of the server list matched (server_preference) */ |
2053 | 1.93k | if (found_index == 0) |
2054 | 0 | break; |
2055 | | /* server preference is overridden only if the first entry of client-provided list is chachapoly */ |
2056 | 1.93k | server_chacha_priority = 0; |
2057 | 1.93k | } |
2058 | 29 | if (found_index != SIZE_MAX) { |
2059 | 0 | *selected = candidates[found_index]; |
2060 | 0 | ret = 0; |
2061 | 29 | } else { |
2062 | 29 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
2063 | 29 | } |
2064 | | |
2065 | 725 | Exit: |
2066 | 725 | return ret; |
2067 | 29 | } |
2068 | | |
2069 | | static int push_key_share_entry(ptls_buffer_t *buf, uint16_t group, ptls_iovec_t pubkey) |
2070 | 0 | { |
2071 | 0 | int ret; |
2072 | |
|
2073 | 0 | ptls_buffer_push16(buf, group); |
2074 | 0 | ptls_buffer_push_block(buf, 2, { ptls_buffer_pushv(buf, pubkey.base, pubkey.len); }); |
2075 | 0 | ret = 0; |
2076 | 0 | Exit: |
2077 | 0 | return ret; |
2078 | 0 | } |
2079 | | |
2080 | | static int decode_key_share_entry(uint16_t *group, ptls_iovec_t *key_exchange, const uint8_t **src, const uint8_t *const end) |
2081 | 1.76k | { |
2082 | 1.76k | int ret; |
2083 | | |
2084 | 1.76k | if ((ret = ptls_decode16(group, src, end)) != 0) |
2085 | 21 | goto Exit; |
2086 | 1.74k | ptls_decode_open_block(*src, end, 2, { |
2087 | 1.74k | *key_exchange = ptls_iovec_init(*src, end - *src); |
2088 | 1.74k | *src = end; |
2089 | 1.74k | }); |
2090 | | |
2091 | 1.76k | Exit: |
2092 | 1.76k | return ret; |
2093 | 1.74k | } |
2094 | | |
2095 | | static int select_key_share(ptls_key_exchange_algorithm_t **selected, ptls_iovec_t *peer_key, |
2096 | | ptls_key_exchange_algorithm_t **candidates, const uint8_t **src, const uint8_t *const end, |
2097 | | int expect_one) |
2098 | 544 | { |
2099 | 544 | int ret; |
2100 | | |
2101 | 544 | *selected = NULL; |
2102 | | |
2103 | 544 | if (expect_one && *src == end) { |
2104 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2105 | 0 | goto Exit; |
2106 | 0 | } |
2107 | | |
2108 | 2.25k | while (*src != end) { |
2109 | 1.76k | uint16_t group; |
2110 | 1.76k | ptls_iovec_t key; |
2111 | 1.76k | if ((ret = decode_key_share_entry(&group, &key, src, end)) != 0) |
2112 | 54 | goto Exit; |
2113 | 1.71k | ptls_key_exchange_algorithm_t **c = candidates; |
2114 | 3.42k | for (; *c != NULL; ++c) { |
2115 | 1.71k | if (*selected == NULL && (*c)->id == group) { |
2116 | 270 | *selected = *c; |
2117 | 270 | *peer_key = key; |
2118 | 270 | } |
2119 | 1.71k | } |
2120 | 1.71k | if (expect_one) { |
2121 | 0 | ret = *selected != NULL ? 0 : PTLS_ALERT_ILLEGAL_PARAMETER; |
2122 | 0 | goto Exit; |
2123 | 0 | } |
2124 | 1.71k | } |
2125 | | |
2126 | 490 | ret = 0; |
2127 | | |
2128 | 544 | Exit: |
2129 | 544 | return ret; |
2130 | 490 | } |
2131 | | |
2132 | | static int emit_server_name_extension(ptls_buffer_t *buf, const char *server_name) |
2133 | 0 | { |
2134 | 0 | int ret; |
2135 | |
|
2136 | 0 | ptls_buffer_push_block(buf, 2, { |
2137 | 0 | ptls_buffer_push(buf, PTLS_SERVER_NAME_TYPE_HOSTNAME); |
2138 | 0 | ptls_buffer_push_block(buf, 2, { ptls_buffer_pushv(buf, server_name, strlen(server_name)); }); |
2139 | 0 | }); |
2140 | | |
2141 | 0 | ret = 0; |
2142 | 0 | Exit: |
2143 | 0 | return ret; |
2144 | 0 | } |
2145 | | |
2146 | | /** |
2147 | | * Within the outer ECH extension, returns the number of bytes that preceeds the AEAD-encrypted payload. |
2148 | | */ |
2149 | | static inline size_t outer_ech_header_size(size_t enc_size) |
2150 | 0 | { |
2151 | 0 | return 10 + enc_size; |
2152 | 0 | } |
2153 | | |
2154 | | /** |
2155 | | * Flag to indicate which of ClientHelloInner, EncodedClientHelloInner, ClientHelloOuter is to be generated. When ECH is inactive, |
2156 | | * only ClientHelloInner is used. |
2157 | | */ |
2158 | | enum encode_ch_mode { ENCODE_CH_MODE_INNER, ENCODE_CH_MODE_ENCODED_INNER, ENCODE_CH_MODE_OUTER }; |
2159 | | |
2160 | | static int encode_client_hello(ptls_context_t *ctx, ptls_buffer_t *sendbuf, enum encode_ch_mode mode, int is_second_flight, |
2161 | | ptls_handshake_properties_t *properties, const void *client_random, |
2162 | | ptls_key_exchange_context_t *key_share_ctx, const char *sni_name, ptls_iovec_t legacy_session_id, |
2163 | | struct st_ptls_ech_t *ech, size_t *ech_size_offset, ptls_iovec_t ech_replay, ptls_iovec_t psk_secret, |
2164 | | ptls_iovec_t psk_identity, uint32_t obfuscated_ticket_age, size_t psk_binder_size, |
2165 | | ptls_iovec_t *cookie, int using_early_data) |
2166 | 0 | { |
2167 | 0 | int ret; |
2168 | |
|
2169 | 0 | assert(mode == ENCODE_CH_MODE_INNER || ech != NULL); |
2170 | | |
2171 | 0 | ptls_buffer_push_message_body(sendbuf, NULL, PTLS_HANDSHAKE_TYPE_CLIENT_HELLO, { |
2172 | | /* legacy_version */ |
2173 | 0 | ptls_buffer_push16(sendbuf, 0x0303); |
2174 | | /* random_bytes */ |
2175 | 0 | ptls_buffer_pushv(sendbuf, client_random, PTLS_HELLO_RANDOM_SIZE); |
2176 | | /* lecagy_session_id */ |
2177 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2178 | 0 | if (mode != ENCODE_CH_MODE_ENCODED_INNER) |
2179 | 0 | ptls_buffer_pushv(sendbuf, legacy_session_id.base, legacy_session_id.len); |
2180 | 0 | }); |
2181 | | /* cipher_suites */ |
2182 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2183 | 0 | ptls_cipher_suite_t **cs = ctx->cipher_suites; |
2184 | 0 | for (; *cs != NULL; ++cs) |
2185 | 0 | ptls_buffer_push16(sendbuf, (*cs)->id); |
2186 | 0 | }); |
2187 | | /* legacy_compression_methods */ |
2188 | 0 | ptls_buffer_push_block(sendbuf, 1, { ptls_buffer_push(sendbuf, 0); }); |
2189 | | /* extensions */ |
2190 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2191 | 0 | if (mode == ENCODE_CH_MODE_OUTER) { |
2192 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO, { |
2193 | 0 | size_t ext_payload_from = sendbuf->off; |
2194 | 0 | ptls_buffer_push(sendbuf, PTLS_ECH_CLIENT_HELLO_TYPE_OUTER); |
2195 | 0 | ptls_buffer_push16(sendbuf, ech->cipher->id.kdf); |
2196 | 0 | ptls_buffer_push16(sendbuf, ech->cipher->id.aead); |
2197 | 0 | ptls_buffer_push(sendbuf, ech->config_id); |
2198 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2199 | 0 | if (!is_second_flight) |
2200 | 0 | ptls_buffer_pushv(sendbuf, ech->client.enc.base, ech->client.enc.len); |
2201 | 0 | }); |
2202 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2203 | 0 | assert(sendbuf->off - ext_payload_from == |
2204 | 0 | outer_ech_header_size(is_second_flight ? 0 : ech->client.enc.len)); |
2205 | 0 | if ((ret = ptls_buffer_reserve(sendbuf, *ech_size_offset)) != 0) |
2206 | 0 | goto Exit; |
2207 | 0 | memset(sendbuf->base + sendbuf->off, 0, *ech_size_offset); |
2208 | 0 | sendbuf->off += *ech_size_offset; |
2209 | 0 | *ech_size_offset = sendbuf->off - *ech_size_offset; |
2210 | 0 | }); |
2211 | 0 | }); |
2212 | 0 | } else if (ech->aead != NULL) { |
2213 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO, |
2214 | 0 | { ptls_buffer_push(sendbuf, PTLS_ECH_CLIENT_HELLO_TYPE_INNER); }); |
2215 | 0 | } else if (ech_replay.base != NULL) { |
2216 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO, |
2217 | 0 | { ptls_buffer_pushv(sendbuf, ech_replay.base, ech_replay.len); }); |
2218 | 0 | } |
2219 | 0 | if (mode == ENCODE_CH_MODE_ENCODED_INNER) { |
2220 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ECH_OUTER_EXTENSIONS, { |
2221 | 0 | ptls_buffer_push_block(sendbuf, 1, { ptls_buffer_push16(sendbuf, PTLS_EXTENSION_TYPE_KEY_SHARE); }); |
2222 | 0 | }); |
2223 | 0 | } else { |
2224 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_KEY_SHARE, { |
2225 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2226 | 0 | if (key_share_ctx != NULL && |
2227 | 0 | (ret = push_key_share_entry(sendbuf, key_share_ctx->algo->id, key_share_ctx->pubkey)) != 0) |
2228 | 0 | goto Exit; |
2229 | 0 | }); |
2230 | 0 | }); |
2231 | 0 | } |
2232 | 0 | if (sni_name != NULL) { |
2233 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SERVER_NAME, { |
2234 | 0 | if ((ret = emit_server_name_extension(sendbuf, sni_name)) != 0) |
2235 | 0 | goto Exit; |
2236 | 0 | }); |
2237 | 0 | } |
2238 | 0 | if (properties != NULL && properties->client.negotiated_protocols.count != 0) { |
2239 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ALPN, { |
2240 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2241 | 0 | size_t i; |
2242 | 0 | for (i = 0; i != properties->client.negotiated_protocols.count; ++i) { |
2243 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2244 | 0 | ptls_iovec_t p = properties->client.negotiated_protocols.list[i]; |
2245 | 0 | ptls_buffer_pushv(sendbuf, p.base, p.len); |
2246 | 0 | }); |
2247 | 0 | } |
2248 | 0 | }); |
2249 | 0 | }); |
2250 | 0 | } |
2251 | 0 | if (ctx->decompress_certificate != NULL) { |
2252 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_COMPRESS_CERTIFICATE, { |
2253 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2254 | 0 | const uint16_t *algo = ctx->decompress_certificate->supported_algorithms; |
2255 | 0 | assert(*algo != UINT16_MAX); |
2256 | 0 | for (; *algo != UINT16_MAX; ++algo) |
2257 | 0 | ptls_buffer_push16(sendbuf, *algo); |
2258 | 0 | }); |
2259 | 0 | }); |
2260 | 0 | } |
2261 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS, { |
2262 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2263 | 0 | size_t i; |
2264 | 0 | for (i = 0; i != PTLS_ELEMENTSOF(supported_versions); ++i) |
2265 | 0 | ptls_buffer_push16(sendbuf, supported_versions[i]); |
2266 | 0 | }); |
2267 | 0 | }); |
2268 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SIGNATURE_ALGORITHMS, { |
2269 | 0 | if ((ret = push_signature_algorithms(ctx->verify_certificate, sendbuf)) != 0) |
2270 | 0 | goto Exit; |
2271 | 0 | }); |
2272 | 0 | if (ctx->key_exchanges != NULL) { |
2273 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SUPPORTED_GROUPS, { |
2274 | 0 | ptls_key_exchange_algorithm_t **algo = ctx->key_exchanges; |
2275 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2276 | 0 | for (; *algo != NULL; ++algo) |
2277 | 0 | ptls_buffer_push16(sendbuf, (*algo)->id); |
2278 | 0 | }); |
2279 | 0 | }); |
2280 | 0 | } |
2281 | 0 | if (cookie != NULL && cookie->base != NULL) { |
2282 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_COOKIE, { |
2283 | 0 | ptls_buffer_push_block(sendbuf, 2, { ptls_buffer_pushv(sendbuf, cookie->base, cookie->len); }); |
2284 | 0 | }); |
2285 | 0 | } |
2286 | 0 | if (ctx->use_raw_public_keys) { |
2287 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SERVER_CERTIFICATE_TYPE, { |
2288 | 0 | ptls_buffer_push_block(sendbuf, 1, { ptls_buffer_push(sendbuf, PTLS_CERTIFICATE_TYPE_RAW_PUBLIC_KEY); }); |
2289 | 0 | }); |
2290 | 0 | } |
2291 | 0 | if (ctx->save_ticket != NULL && |
2292 | 0 | (ctx->ticket_requests.client.new_session_count != 0 || ctx->ticket_requests.client.resumption_count != 0)) { |
2293 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_TICKET_REQUEST, { |
2294 | 0 | ptls_buffer_push(sendbuf, ctx->ticket_requests.client.new_session_count, |
2295 | 0 | ctx->ticket_requests.client.resumption_count); |
2296 | 0 | }); |
2297 | 0 | } |
2298 | 0 | if ((ret = push_additional_extensions(properties, sendbuf)) != 0) |
2299 | 0 | goto Exit; |
2300 | 0 | if (ctx->save_ticket != NULL || psk_secret.base != NULL) { |
2301 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_PSK_KEY_EXCHANGE_MODES, { |
2302 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2303 | 0 | if (!ctx->require_dhe_on_psk) |
2304 | 0 | ptls_buffer_push(sendbuf, PTLS_PSK_KE_MODE_PSK); |
2305 | 0 | ptls_buffer_push(sendbuf, PTLS_PSK_KE_MODE_PSK_DHE); |
2306 | 0 | }); |
2307 | 0 | }); |
2308 | 0 | } |
2309 | 0 | if (psk_secret.base != NULL) { |
2310 | 0 | if (using_early_data && !is_second_flight) |
2311 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_EARLY_DATA, {}); |
2312 | | /* pre-shared key "MUST be the last extension in the ClientHello" (draft-17 section 4.2.6) */ |
2313 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_PRE_SHARED_KEY, { |
2314 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2315 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2316 | 0 | if (mode == ENCODE_CH_MODE_OUTER && ech->state != PTLS_ECH_STATE_GREASE) { |
2317 | 0 | if ((ret = ptls_buffer_reserve(sendbuf, psk_identity.len)) != 0) |
2318 | 0 | goto Exit; |
2319 | 0 | ctx->random_bytes(sendbuf->base + sendbuf->off, psk_identity.len); |
2320 | 0 | sendbuf->off += psk_identity.len; |
2321 | 0 | } else { |
2322 | 0 | ptls_buffer_pushv(sendbuf, psk_identity.base, psk_identity.len); |
2323 | 0 | } |
2324 | 0 | }); |
2325 | 0 | uint32_t age; |
2326 | 0 | if (mode == ENCODE_CH_MODE_OUTER && ech->state != PTLS_ECH_STATE_GREASE) { |
2327 | 0 | ctx->random_bytes(&age, sizeof(age)); |
2328 | 0 | } else { |
2329 | 0 | age = obfuscated_ticket_age; |
2330 | 0 | } |
2331 | 0 | ptls_buffer_push32(sendbuf, age); |
2332 | 0 | }); |
2333 | | /* allocate space for PSK binder. The space is filled initially filled by a random value (meeting the |
2334 | | * requirement of ClientHelloOuter), and later gets filled with the correct binder value if necessary. */ |
2335 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
2336 | 0 | ptls_buffer_push_block(sendbuf, 1, { |
2337 | 0 | if ((ret = ptls_buffer_reserve(sendbuf, psk_binder_size)) != 0) |
2338 | 0 | goto Exit; |
2339 | 0 | ctx->random_bytes(sendbuf->base + sendbuf->off, psk_binder_size); |
2340 | 0 | sendbuf->off += psk_binder_size; |
2341 | 0 | }); |
2342 | 0 | }); |
2343 | 0 | }); |
2344 | 0 | } |
2345 | 0 | }); |
2346 | 0 | }); |
2347 | | |
2348 | 0 | Exit: |
2349 | 0 | return ret; |
2350 | 0 | } |
2351 | | |
2352 | | /** |
2353 | | * Feeds the CH message into the hash, computing the PSK binder if necessary. `binder_key` must be derived before calling this |
2354 | | * function. |
2355 | | */ |
2356 | | static int update_ch_hash_and_binder(ptls_key_schedule_t *ks, uint8_t *ch, size_t ch_start, size_t ch_end, int has_psk, |
2357 | | uint8_t *binder_key, int is_outer) |
2358 | 0 | { |
2359 | 0 | int ret = 0; |
2360 | 0 | size_t hash_off = ch_start; |
2361 | |
|
2362 | 0 | if (has_psk) { |
2363 | 0 | size_t psk_binder_off = ch_end - (3 + ks->hashes[0].algo->digest_size); |
2364 | 0 | ptls__key_schedule_update_hash(ks, ch + hash_off, psk_binder_off - hash_off, is_outer); |
2365 | 0 | hash_off = psk_binder_off; |
2366 | 0 | if ((ret = calc_verify_data(ch + psk_binder_off + 3, ks, binder_key)) != 0) |
2367 | 0 | return ret; |
2368 | 0 | } |
2369 | 0 | ptls__key_schedule_update_hash(ks, ch + hash_off, ch_end - hash_off, is_outer); |
2370 | |
|
2371 | 0 | return ret; |
2372 | 0 | } |
2373 | | |
2374 | | static int send_client_hello(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_handshake_properties_t *properties, |
2375 | | ptls_iovec_t *cookie) |
2376 | 0 | { |
2377 | 0 | struct { |
2378 | 0 | ptls_iovec_t secret; |
2379 | 0 | ptls_iovec_t identity; |
2380 | 0 | const char *label; |
2381 | 0 | } psk = {{NULL}}; |
2382 | 0 | uint32_t obfuscated_ticket_age = 0; |
2383 | 0 | const char *sni_name = NULL; |
2384 | 0 | size_t mess_start; |
2385 | 0 | uint8_t binder_key[PTLS_MAX_DIGEST_SIZE]; |
2386 | 0 | ptls_buffer_t encoded_ch_inner; |
2387 | 0 | int ret, is_second_flight = tls->key_schedule != NULL; |
2388 | |
|
2389 | 0 | ptls_buffer_init(&encoded_ch_inner, "", 0); |
2390 | |
|
2391 | 0 | if (tls->server_name != NULL && !ptls_server_name_is_ipaddr(tls->server_name)) |
2392 | 0 | sni_name = tls->server_name; |
2393 | | |
2394 | | /* try to use ECH (ignore broken ECHConfigList; it is delivered insecurely) */ |
2395 | 0 | if (properties != NULL) { |
2396 | 0 | if (!is_second_flight && sni_name != NULL && tls->ctx->ech.client.ciphers != NULL) { |
2397 | 0 | if (properties->client.ech.configs.len != 0) { |
2398 | 0 | struct st_decoded_ech_config_t decoded; |
2399 | 0 | client_decode_ech_config_list(tls->ctx, &decoded, properties->client.ech.configs); |
2400 | 0 | if (decoded.kem != NULL && decoded.cipher != NULL) { |
2401 | 0 | if ((ret = client_setup_ech(&tls->ech, &decoded, tls->ctx->random_bytes)) != 0) |
2402 | 0 | goto Exit; |
2403 | 0 | } |
2404 | 0 | } else if (properties->client.ech.configs.base != NULL) { |
2405 | | /* zero-length config with non-NULL base indicates ECH greasing; NULL base means no ECH */ |
2406 | 0 | client_setup_ech_grease(&tls->ech, tls->ctx->random_bytes, tls->ctx->ech.client.kems, tls->ctx->ech.client.ciphers, |
2407 | 0 | sni_name); |
2408 | 0 | tls->ech.state = PTLS_ECH_STATE_GREASE; |
2409 | 0 | } |
2410 | 0 | } |
2411 | 0 | } |
2412 | | |
2413 | | /* use external PSK if provided */ |
2414 | 0 | if (tls->ctx->pre_shared_key.identity.base != NULL) { |
2415 | 0 | if (!is_second_flight) { |
2416 | 0 | tls->client.offered_psk = 1; |
2417 | 0 | for (size_t i = 0; tls->ctx->cipher_suites[i] != NULL; ++i) { |
2418 | 0 | if (tls->ctx->cipher_suites[i]->hash == tls->ctx->pre_shared_key.hash) { |
2419 | 0 | tls->cipher_suite = tls->ctx->cipher_suites[i]; |
2420 | 0 | break; |
2421 | 0 | } |
2422 | 0 | } |
2423 | 0 | assert(tls->cipher_suite != NULL && "no compatible cipher-suite provided that matches psk.hash"); |
2424 | 0 | if (properties != NULL && properties->client.max_early_data_size != NULL) { |
2425 | 0 | tls->client.using_early_data = 1; |
2426 | 0 | *properties->client.max_early_data_size = SIZE_MAX; |
2427 | 0 | } |
2428 | 0 | } else { |
2429 | 0 | assert(tls->cipher_suite != NULL && tls->cipher_suite->hash == tls->ctx->pre_shared_key.hash); |
2430 | 0 | } |
2431 | 0 | psk.secret = tls->ctx->pre_shared_key.secret; |
2432 | 0 | psk.identity = tls->ctx->pre_shared_key.identity; |
2433 | 0 | psk.label = "ext binder"; |
2434 | 0 | } |
2435 | | |
2436 | | /* try to setup resumption-related data, unless external PSK is used */ |
2437 | 0 | if (psk.secret.base == NULL && properties != NULL && properties->client.session_ticket.base != NULL && |
2438 | 0 | tls->ctx->key_exchanges != NULL) { |
2439 | 0 | ptls_key_exchange_algorithm_t *key_share = NULL; |
2440 | 0 | ptls_cipher_suite_t *cipher_suite = NULL; |
2441 | 0 | uint32_t max_early_data_size; |
2442 | 0 | if (decode_stored_session_ticket(tls, &key_share, &cipher_suite, &psk.secret, &obfuscated_ticket_age, &psk.identity, |
2443 | 0 | &max_early_data_size, properties->client.session_ticket.base, |
2444 | 0 | properties->client.session_ticket.base + properties->client.session_ticket.len) == 0) { |
2445 | 0 | psk.label = "res binder"; |
2446 | 0 | tls->client.offered_psk = 1; |
2447 | | /* key-share selected by HRR should not be overridden */ |
2448 | 0 | if (tls->key_share == NULL) |
2449 | 0 | tls->key_share = key_share; |
2450 | 0 | tls->cipher_suite = cipher_suite; |
2451 | 0 | if (!is_second_flight && max_early_data_size != 0 && properties->client.max_early_data_size != NULL) { |
2452 | 0 | tls->client.using_early_data = 1; |
2453 | 0 | *properties->client.max_early_data_size = max_early_data_size; |
2454 | 0 | } |
2455 | 0 | } else { |
2456 | 0 | psk.secret = ptls_iovec_init(NULL, 0); |
2457 | 0 | } |
2458 | 0 | } |
2459 | | |
2460 | | /* send 0-RTT related signals back to the client */ |
2461 | 0 | if (properties != NULL) { |
2462 | 0 | if (tls->client.using_early_data) { |
2463 | 0 | properties->client.early_data_acceptance = PTLS_EARLY_DATA_ACCEPTANCE_UNKNOWN; |
2464 | 0 | } else { |
2465 | 0 | if (properties->client.max_early_data_size != NULL) |
2466 | 0 | *properties->client.max_early_data_size = 0; |
2467 | 0 | properties->client.early_data_acceptance = PTLS_EARLY_DATA_REJECTED; |
2468 | 0 | } |
2469 | 0 | } |
2470 | | |
2471 | | /* use the default key share if still not undetermined */ |
2472 | 0 | if (tls->key_share == NULL && tls->ctx->key_exchanges != NULL && |
2473 | 0 | !(properties != NULL && properties->client.negotiate_before_key_exchange)) |
2474 | 0 | tls->key_share = tls->ctx->key_exchanges[0]; |
2475 | | |
2476 | | /* instantiate key share context */ |
2477 | 0 | assert(tls->client.key_share_ctx == NULL); |
2478 | 0 | if (tls->key_share != NULL) { |
2479 | 0 | if ((ret = tls->key_share->create(tls->key_share, &tls->client.key_share_ctx)) != 0) |
2480 | 0 | goto Exit; |
2481 | 0 | } |
2482 | | |
2483 | | /* initialize key schedule */ |
2484 | 0 | if (!is_second_flight) { |
2485 | 0 | if ((tls->key_schedule = key_schedule_new(tls->cipher_suite, tls->ctx->cipher_suites, tls->ech.aead != NULL)) == NULL) { |
2486 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
2487 | 0 | goto Exit; |
2488 | 0 | } |
2489 | 0 | if ((ret = key_schedule_extract(tls->key_schedule, psk.secret)) != 0) |
2490 | 0 | goto Exit; |
2491 | 0 | } |
2492 | | |
2493 | | /* start generating CH */ |
2494 | 0 | if ((ret = emitter->begin_message(emitter)) != 0) |
2495 | 0 | goto Exit; |
2496 | 0 | mess_start = emitter->buf->off; |
2497 | | |
2498 | | /* generate true (inner) CH */ |
2499 | 0 | if ((ret = encode_client_hello(tls->ctx, emitter->buf, ENCODE_CH_MODE_INNER, is_second_flight, properties, |
2500 | 0 | tls->ech.aead != NULL ? tls->ech.inner_client_random : tls->client_random, |
2501 | 0 | tls->client.key_share_ctx, sni_name, tls->client.legacy_session_id, &tls->ech, NULL, |
2502 | 0 | tls->ech.client.first_ech, psk.secret, psk.identity, obfuscated_ticket_age, |
2503 | 0 | tls->key_schedule->hashes[0].algo->digest_size, cookie, tls->client.using_early_data)) != 0) |
2504 | 0 | goto Exit; |
2505 | | |
2506 | | /* update the message hash, filling in the PSK binder HMAC if necessary */ |
2507 | 0 | if (psk.secret.base != NULL) { |
2508 | 0 | if ((ret = derive_secret_with_empty_digest(tls->key_schedule, binder_key, psk.label)) != 0) |
2509 | 0 | goto Exit; |
2510 | 0 | } |
2511 | 0 | if ((ret = update_ch_hash_and_binder(tls->key_schedule, emitter->buf->base, mess_start, emitter->buf->off, |
2512 | 0 | psk.secret.base != NULL, binder_key, 0)) != 0) |
2513 | 0 | goto Exit; |
2514 | | |
2515 | | /* ECH */ |
2516 | 0 | if (tls->ech.aead != NULL) { |
2517 | | /* build EncodedCHInner */ |
2518 | 0 | if ((ret = encode_client_hello(tls->ctx, &encoded_ch_inner, ENCODE_CH_MODE_ENCODED_INNER, is_second_flight, properties, |
2519 | 0 | tls->ech.inner_client_random, tls->client.key_share_ctx, sni_name, |
2520 | 0 | tls->client.legacy_session_id, &tls->ech, NULL, ptls_iovec_init(NULL, 0), psk.secret, |
2521 | 0 | psk.identity, obfuscated_ticket_age, tls->key_schedule->hashes[0].algo->digest_size, cookie, |
2522 | 0 | tls->client.using_early_data)) != 0) |
2523 | 0 | goto Exit; |
2524 | 0 | if (psk.secret.base != NULL) |
2525 | 0 | memcpy(encoded_ch_inner.base + encoded_ch_inner.off - tls->key_schedule->hashes[0].algo->digest_size, |
2526 | 0 | emitter->buf->base + emitter->buf->off - tls->key_schedule->hashes[0].algo->digest_size, |
2527 | 0 | tls->key_schedule->hashes[0].algo->digest_size); |
2528 | 0 | { /* pad EncodedCHInner (following draft-ietf-tls-esni-15 6.1.3) */ |
2529 | 0 | size_t padding_len; |
2530 | 0 | if (sni_name != NULL) { |
2531 | 0 | padding_len = strlen(sni_name); |
2532 | 0 | if (padding_len < tls->ech.client.max_name_length) |
2533 | 0 | padding_len = tls->ech.client.max_name_length; |
2534 | 0 | } else { |
2535 | 0 | padding_len = tls->ech.client.max_name_length + 9; |
2536 | 0 | } |
2537 | 0 | size_t final_len = encoded_ch_inner.off - PTLS_HANDSHAKE_HEADER_SIZE + padding_len; |
2538 | 0 | final_len = (final_len + 31) / 32 * 32; |
2539 | 0 | padding_len = final_len - (encoded_ch_inner.off - PTLS_HANDSHAKE_HEADER_SIZE); |
2540 | 0 | if (padding_len != 0) { |
2541 | 0 | if ((ret = ptls_buffer_reserve(&encoded_ch_inner, padding_len)) != 0) |
2542 | 0 | goto Exit; |
2543 | 0 | memset(encoded_ch_inner.base + encoded_ch_inner.off, 0, padding_len); |
2544 | 0 | encoded_ch_inner.off += padding_len; |
2545 | 0 | } |
2546 | 0 | } |
2547 | | /* flush CHInner, build CHOuterAAD */ |
2548 | 0 | emitter->buf->off = mess_start; |
2549 | 0 | size_t ech_payload_size = encoded_ch_inner.off - PTLS_HANDSHAKE_HEADER_SIZE + tls->ech.aead->algo->tag_size, |
2550 | 0 | ech_size_offset = ech_payload_size; |
2551 | 0 | if ((ret = encode_client_hello(tls->ctx, emitter->buf, ENCODE_CH_MODE_OUTER, is_second_flight, properties, |
2552 | 0 | tls->client_random, tls->client.key_share_ctx, tls->ech.client.public_name, |
2553 | 0 | tls->client.legacy_session_id, &tls->ech, &ech_size_offset, ptls_iovec_init(NULL, 0), |
2554 | 0 | psk.secret, psk.identity, obfuscated_ticket_age, |
2555 | 0 | tls->key_schedule->hashes[0].algo->digest_size, cookie, tls->client.using_early_data)) != 0) |
2556 | 0 | goto Exit; |
2557 | | /* overwrite ECH payload */ |
2558 | 0 | ptls_aead_encrypt(tls->ech.aead, emitter->buf->base + ech_size_offset, encoded_ch_inner.base + PTLS_HANDSHAKE_HEADER_SIZE, |
2559 | 0 | encoded_ch_inner.off - PTLS_HANDSHAKE_HEADER_SIZE, is_second_flight, |
2560 | 0 | emitter->buf->base + mess_start + PTLS_HANDSHAKE_HEADER_SIZE, |
2561 | 0 | emitter->buf->off - (mess_start + PTLS_HANDSHAKE_HEADER_SIZE)); |
2562 | | /* keep the copy of the 1st ECH extension so that we can send it again in 2nd CH in response to rejection with HRR */ |
2563 | 0 | if (!is_second_flight) { |
2564 | 0 | size_t len = outer_ech_header_size(tls->ech.client.enc.len) + ech_payload_size; |
2565 | 0 | if ((tls->ech.client.first_ech.base = malloc(len)) == NULL) { |
2566 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
2567 | 0 | goto Exit; |
2568 | 0 | } |
2569 | 0 | memcpy(tls->ech.client.first_ech.base, |
2570 | 0 | emitter->buf->base + ech_size_offset - outer_ech_header_size(tls->ech.client.enc.len), len); |
2571 | 0 | tls->ech.client.first_ech.len = len; |
2572 | 0 | if (tls->ech.state != PTLS_ECH_STATE_GREASE) |
2573 | 0 | tls->ech.state = PTLS_ECH_STATE_OFFERED; |
2574 | 0 | } |
2575 | 0 | if (tls->ech.state == PTLS_ECH_STATE_GREASE) { |
2576 | | /* For grease ECH, the server sees the outer CH. Discard the inner state and adopt the outer, then compute the PSK |
2577 | | * binder over the outer CH using the standard flow. */ |
2578 | 0 | for (size_t i = 0; i < tls->key_schedule->num_hashes; ++i) { |
2579 | 0 | tls->key_schedule->hashes[i].ctx->final(tls->key_schedule->hashes[i].ctx, NULL, PTLS_HASH_FINAL_MODE_FREE); |
2580 | 0 | tls->key_schedule->hashes[i].ctx = tls->key_schedule->hashes[i].ctx_outer; |
2581 | 0 | tls->key_schedule->hashes[i].ctx_outer = NULL; |
2582 | 0 | } |
2583 | 0 | ptls_aead_free(tls->ech.aead); |
2584 | 0 | tls->ech.aead = NULL; |
2585 | 0 | if ((ret = update_ch_hash_and_binder(tls->key_schedule, emitter->buf->base, mess_start, emitter->buf->off, |
2586 | 0 | psk.secret.base != NULL, binder_key, 0)) != 0) |
2587 | 0 | goto Exit; |
2588 | 0 | } else { |
2589 | | /* update outer hash */ |
2590 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, emitter->buf->base + mess_start, emitter->buf->off - mess_start, 1); |
2591 | 0 | } |
2592 | 0 | } |
2593 | | |
2594 | | /* commit CH to the record layer */ |
2595 | 0 | if ((ret = emitter->commit_message(emitter)) != 0) |
2596 | 0 | goto Exit; |
2597 | | |
2598 | 0 | if (tls->client.using_early_data) { |
2599 | 0 | assert(!is_second_flight); |
2600 | 0 | if ((ret = setup_traffic_protection(tls, 1, "c e traffic", 1, 0, 0)) != 0) |
2601 | 0 | goto Exit; |
2602 | 0 | if ((ret = push_change_cipher_spec(tls, emitter)) != 0) |
2603 | 0 | goto Exit; |
2604 | 0 | } |
2605 | 0 | if (psk.secret.base != NULL && !is_second_flight) { |
2606 | 0 | if ((ret = derive_exporter_secret(tls, 1)) != 0) |
2607 | 0 | goto Exit; |
2608 | 0 | } |
2609 | 0 | tls->state = cookie == NULL ? PTLS_STATE_CLIENT_EXPECT_SERVER_HELLO : PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO; |
2610 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
2611 | |
|
2612 | 0 | Exit: |
2613 | 0 | ptls_buffer_dispose(&encoded_ch_inner); |
2614 | 0 | ptls_clear_memory(binder_key, sizeof(binder_key)); |
2615 | 0 | return ret; |
2616 | 0 | } |
2617 | | |
2618 | | ptls_cipher_suite_t *ptls_find_cipher_suite(ptls_cipher_suite_t **cipher_suites, uint16_t id) |
2619 | 0 | { |
2620 | 0 | ptls_cipher_suite_t **cs; |
2621 | 0 | if (cipher_suites == NULL) |
2622 | 0 | return NULL; |
2623 | 0 | for (cs = cipher_suites; *cs != NULL && (*cs)->id != id; ++cs) |
2624 | 0 | ; |
2625 | 0 | return *cs; |
2626 | 0 | } |
2627 | | |
2628 | | static int decode_server_hello(ptls_t *tls, struct st_ptls_server_hello_t *sh, const uint8_t *src, const uint8_t *const end) |
2629 | 0 | { |
2630 | 0 | int ret; |
2631 | |
|
2632 | 0 | *sh = (struct st_ptls_server_hello_t){{0}}; |
2633 | | |
2634 | | /* ignore legacy-version */ |
2635 | 0 | if (end - src < 2) { |
2636 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2637 | 0 | goto Exit; |
2638 | 0 | } |
2639 | 0 | src += 2; |
2640 | | |
2641 | | /* random */ |
2642 | 0 | if (end - src < PTLS_HELLO_RANDOM_SIZE) { |
2643 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2644 | 0 | goto Exit; |
2645 | 0 | } |
2646 | 0 | sh->is_retry_request = memcmp(src, hello_retry_random, PTLS_HELLO_RANDOM_SIZE) == 0; |
2647 | 0 | src += PTLS_HELLO_RANDOM_SIZE; |
2648 | 0 | if (sh->is_retry_request && tls->state == PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO) { |
2649 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
2650 | 0 | goto Exit; |
2651 | 0 | } |
2652 | | |
2653 | | /* legacy_session_id */ |
2654 | 0 | ptls_decode_open_block(src, end, 1, { |
2655 | 0 | if (end - src > 32) { |
2656 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2657 | 0 | goto Exit; |
2658 | 0 | } |
2659 | 0 | sh->legacy_session_id = ptls_iovec_init(src, end - src); |
2660 | 0 | src = end; |
2661 | 0 | }); |
2662 | | |
2663 | 0 | { /* select cipher_suite */ |
2664 | 0 | uint16_t csid; |
2665 | 0 | if ((ret = ptls_decode16(&csid, &src, end)) != 0) |
2666 | 0 | goto Exit; |
2667 | 0 | if (tls->state == PTLS_STATE_CLIENT_EXPECT_SERVER_HELLO) { |
2668 | 0 | if ((tls->cipher_suite = ptls_find_cipher_suite(tls->ctx->cipher_suites, csid)) == NULL) { |
2669 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2670 | 0 | goto Exit; |
2671 | 0 | } |
2672 | 0 | } else { |
2673 | 0 | assert(tls->state == PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO); |
2674 | 0 | if (tls->cipher_suite->id != csid) { |
2675 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2676 | 0 | goto Exit; |
2677 | 0 | } |
2678 | 0 | } |
2679 | 0 | } |
2680 | | |
2681 | 0 | { /* legacy_compression_method */ |
2682 | 0 | uint8_t method; |
2683 | 0 | if ((ret = ptls_decode8(&method, &src, end)) != 0) |
2684 | 0 | goto Exit; |
2685 | 0 | if (method != 0) { |
2686 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2687 | 0 | goto Exit; |
2688 | 0 | } |
2689 | 0 | } |
2690 | | |
2691 | 0 | if (sh->is_retry_request) |
2692 | 0 | sh->retry_request.selected_group = UINT16_MAX; |
2693 | |
|
2694 | 0 | uint16_t exttype, found_version = UINT16_MAX, selected_psk_identity = UINT16_MAX; |
2695 | 0 | decode_extensions(src, end, sh->is_retry_request ? PTLS_HANDSHAKE_TYPE_PSEUDO_HRR : PTLS_HANDSHAKE_TYPE_SERVER_HELLO, &exttype, |
2696 | 0 | { |
2697 | 0 | if (tls->ctx->on_extension != NULL && |
2698 | 0 | (ret = tls->ctx->on_extension->cb(tls->ctx->on_extension, tls, PTLS_HANDSHAKE_TYPE_SERVER_HELLO, |
2699 | 0 | exttype, ptls_iovec_init(src, end - src)) != 0)) |
2700 | 0 | goto Exit; |
2701 | 0 | switch (exttype) { |
2702 | 0 | case PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS: |
2703 | 0 | if ((ret = ptls_decode16(&found_version, &src, end)) != 0) |
2704 | 0 | goto Exit; |
2705 | 0 | break; |
2706 | 0 | case PTLS_EXTENSION_TYPE_KEY_SHARE: |
2707 | 0 | if (tls->ctx->key_exchanges == NULL) { |
2708 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
2709 | 0 | goto Exit; |
2710 | 0 | } |
2711 | 0 | if (sh->is_retry_request) { |
2712 | 0 | if ((ret = ptls_decode16(&sh->retry_request.selected_group, &src, end)) != 0) |
2713 | 0 | goto Exit; |
2714 | 0 | } else { |
2715 | 0 | uint16_t group; |
2716 | 0 | if ((ret = decode_key_share_entry(&group, &sh->peerkey, &src, end)) != 0) |
2717 | 0 | goto Exit; |
2718 | 0 | if (src != end) { |
2719 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2720 | 0 | goto Exit; |
2721 | 0 | } |
2722 | 0 | if (tls->key_share == NULL || tls->key_share->id != group) { |
2723 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2724 | 0 | goto Exit; |
2725 | 0 | } |
2726 | 0 | } |
2727 | 0 | break; |
2728 | 0 | case PTLS_EXTENSION_TYPE_COOKIE: |
2729 | 0 | assert(sh->is_retry_request); |
2730 | 0 | ptls_decode_block(src, end, 2, { |
2731 | 0 | if (src == end) { |
2732 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2733 | 0 | goto Exit; |
2734 | 0 | } |
2735 | 0 | sh->retry_request.cookie = ptls_iovec_init(src, end - src); |
2736 | 0 | src = end; |
2737 | 0 | }); |
2738 | 0 | break; |
2739 | 0 | case PTLS_EXTENSION_TYPE_PRE_SHARED_KEY: |
2740 | 0 | assert(!sh->is_retry_request); |
2741 | 0 | if ((ret = ptls_decode16(&selected_psk_identity, &src, end)) != 0) |
2742 | 0 | goto Exit; |
2743 | 0 | break; |
2744 | 0 | case PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO: |
2745 | 0 | assert(sh->is_retry_request); |
2746 | 0 | if (tls->ech.state == PTLS_ECH_STATE_NONE) { |
2747 | 0 | ret = PTLS_ALERT_UNSUPPORTED_EXTENSION; |
2748 | 0 | goto Exit; |
2749 | 0 | } |
2750 | 0 | if (end - src != PTLS_ECH_CONFIRM_LENGTH) { |
2751 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
2752 | 0 | goto Exit; |
2753 | 0 | } |
2754 | 0 | sh->retry_request.ech = src; |
2755 | 0 | src = end; |
2756 | 0 | break; |
2757 | 0 | default: |
2758 | 0 | src = end; |
2759 | 0 | break; |
2760 | 0 | } |
2761 | 0 | }); |
2762 | | |
2763 | 0 | if (!is_supported_version(found_version)) { |
2764 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2765 | 0 | goto Exit; |
2766 | 0 | } |
2767 | 0 | if (!sh->is_retry_request) { |
2768 | 0 | if (selected_psk_identity != UINT16_MAX) { |
2769 | 0 | if (!tls->client.offered_psk) { |
2770 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2771 | 0 | goto Exit; |
2772 | 0 | } |
2773 | 0 | if (selected_psk_identity != 0) { |
2774 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2775 | 0 | goto Exit; |
2776 | 0 | } |
2777 | 0 | tls->is_psk_handshake = 1; |
2778 | 0 | } |
2779 | 0 | if (sh->peerkey.base == NULL && !tls->is_psk_handshake) { |
2780 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2781 | 0 | goto Exit; |
2782 | 0 | } |
2783 | 0 | } |
2784 | | |
2785 | 0 | ret = 0; |
2786 | 0 | Exit: |
2787 | 0 | return ret; |
2788 | 0 | } |
2789 | | |
2790 | | static int handle_hello_retry_request(ptls_t *tls, ptls_message_emitter_t *emitter, struct st_ptls_server_hello_t *sh, |
2791 | | ptls_iovec_t message, ptls_handshake_properties_t *properties) |
2792 | 0 | { |
2793 | 0 | int ret; |
2794 | |
|
2795 | 0 | if (tls->client.key_share_ctx != NULL) { |
2796 | 0 | tls->client.key_share_ctx->on_exchange(&tls->client.key_share_ctx, 1, NULL, ptls_iovec_init(NULL, 0)); |
2797 | 0 | tls->client.key_share_ctx = NULL; |
2798 | 0 | } |
2799 | 0 | if (tls->client.using_early_data) { |
2800 | | /* release traffic encryption key so that 2nd CH goes out in cleartext, but keep the epoch at 1 since we've already |
2801 | | * called derive-secret */ |
2802 | 0 | if (tls->ctx->update_traffic_key == NULL) { |
2803 | 0 | assert(tls->traffic_protection.enc.aead != NULL); |
2804 | 0 | ptls_aead_free(tls->traffic_protection.enc.aead); |
2805 | 0 | tls->traffic_protection.enc.aead = NULL; |
2806 | 0 | } |
2807 | 0 | tls->client.using_early_data = 0; |
2808 | 0 | } |
2809 | | |
2810 | 0 | if (sh->retry_request.selected_group != UINT16_MAX) { |
2811 | | /* we offer the first key_exchanges[0] as KEY_SHARE unless client.negotiate_before_key_exchange is set */ |
2812 | 0 | ptls_key_exchange_algorithm_t **cand; |
2813 | 0 | for (cand = tls->ctx->key_exchanges; *cand != NULL; ++cand) |
2814 | 0 | if ((*cand)->id == sh->retry_request.selected_group) |
2815 | 0 | break; |
2816 | 0 | if (*cand == NULL) { |
2817 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2818 | 0 | goto Exit; |
2819 | 0 | } |
2820 | 0 | if (tls->key_share != NULL && sh->retry_request.selected_group == tls->key_share->id) { |
2821 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2822 | 0 | goto Exit; |
2823 | 0 | } |
2824 | 0 | tls->key_share = *cand; |
2825 | 0 | } else if (tls->key_share != NULL) { |
2826 | | /* retain the key-share using in first CH, if server does not specify one */ |
2827 | 0 | } else { |
2828 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2829 | 0 | goto Exit; |
2830 | 0 | } |
2831 | | |
2832 | 0 | ret = send_client_hello(tls, emitter, properties, &sh->retry_request.cookie); |
2833 | |
|
2834 | 0 | Exit: |
2835 | 0 | return ret; |
2836 | 0 | } |
2837 | | |
2838 | | static int client_ech_select_hello(ptls_t *tls, ptls_iovec_t message, size_t confirm_hash_off, const char *label) |
2839 | 0 | { |
2840 | 0 | uint8_t confirm_hash_delivered[PTLS_ECH_CONFIRM_LENGTH], confirm_hash_expected[PTLS_ECH_CONFIRM_LENGTH]; |
2841 | 0 | int ret = 0; |
2842 | | |
2843 | | /* Determine if ECH has been accepted by checking the confirmation hash. `confirm_hash_off` set to zero indicates that HRR was |
2844 | | * received wo. ECH extension, which is an indication that ECH was rejected. */ |
2845 | 0 | if (confirm_hash_off != 0) { |
2846 | 0 | memcpy(confirm_hash_delivered, message.base + confirm_hash_off, sizeof(confirm_hash_delivered)); |
2847 | 0 | memset(message.base + confirm_hash_off, 0, sizeof(confirm_hash_delivered)); |
2848 | 0 | if ((ret = ech_calc_confirmation(tls->key_schedule, confirm_hash_expected, tls->ech.inner_client_random, label, message)) != |
2849 | 0 | 0) |
2850 | 0 | goto Exit; |
2851 | 0 | int accepted = ptls_mem_equal(confirm_hash_delivered, confirm_hash_expected, sizeof(confirm_hash_delivered)); |
2852 | 0 | memcpy(message.base + confirm_hash_off, confirm_hash_delivered, sizeof(confirm_hash_delivered)); |
2853 | 0 | if (accepted) { |
2854 | 0 | tls->ech.state = PTLS_ECH_STATE_ACCEPTED; |
2855 | 0 | goto Exit; |
2856 | 0 | } else if (tls->ech.state == PTLS_ECH_STATE_ACCEPTED) { |
2857 | | /* Per RFC 9849 Section 6.1.5: if HRR confirmed ECH acceptance, ServerHello MUST also confirm it. */ |
2858 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2859 | 0 | goto Exit; |
2860 | 0 | } |
2861 | 0 | } |
2862 | | |
2863 | | /* dispose ECH AEAD state to indicate rejection, adopting outer CH for the rest of the handshake */ |
2864 | 0 | ptls_aead_free(tls->ech.aead); |
2865 | 0 | tls->ech.aead = NULL; |
2866 | 0 | key_schedule_select_outer(tls->key_schedule); |
2867 | |
|
2868 | 0 | Exit: |
2869 | 0 | PTLS_PROBE(ECH_SELECTION, tls, tls->ech.state == PTLS_ECH_STATE_ACCEPTED); |
2870 | 0 | PTLS_LOG_CONN(ech_selection, tls, { PTLS_LOG_ELEMENT_BOOL(is_ech, tls->ech.state == PTLS_ECH_STATE_ACCEPTED); }); |
2871 | 0 | ptls_clear_memory(confirm_hash_expected, sizeof(confirm_hash_expected)); |
2872 | 0 | return ret; |
2873 | 0 | } |
2874 | | |
2875 | | static int client_handle_hello(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message, |
2876 | | ptls_handshake_properties_t *properties) |
2877 | 0 | { |
2878 | 0 | struct st_ptls_server_hello_t sh; |
2879 | 0 | ptls_iovec_t ecdh_secret = {NULL}; |
2880 | 0 | int ret; |
2881 | |
|
2882 | 0 | if ((ret = decode_server_hello(tls, &sh, message.base + PTLS_HANDSHAKE_HEADER_SIZE, message.base + message.len)) != 0) |
2883 | 0 | goto Exit; |
2884 | 0 | if (!(sh.legacy_session_id.len == tls->client.legacy_session_id.len && |
2885 | 0 | ptls_mem_equal(sh.legacy_session_id.base, tls->client.legacy_session_id.base, tls->client.legacy_session_id.len))) { |
2886 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
2887 | 0 | goto Exit; |
2888 | 0 | } |
2889 | | |
2890 | 0 | if (sh.is_retry_request) { |
2891 | 0 | if ((ret = key_schedule_select_cipher(tls->key_schedule, tls->cipher_suite, 0, tls->ctx->pre_shared_key.secret)) != 0) |
2892 | 0 | goto Exit; |
2893 | 0 | key_schedule_transform_post_ch1hash(tls->key_schedule); |
2894 | 0 | if (tls->ech.aead != NULL) { |
2895 | 0 | size_t confirm_hash_off = 0; |
2896 | 0 | if (tls->ech.state != PTLS_ECH_STATE_GREASE) { |
2897 | 0 | if (sh.retry_request.ech != NULL) |
2898 | 0 | confirm_hash_off = sh.retry_request.ech - message.base; |
2899 | 0 | } |
2900 | 0 | if ((ret = client_ech_select_hello(tls, message, confirm_hash_off, ECH_CONFIRMATION_HRR)) != 0) |
2901 | 0 | goto Exit; |
2902 | 0 | } |
2903 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
2904 | 0 | return handle_hello_retry_request(tls, emitter, &sh, message, properties); |
2905 | 0 | } |
2906 | | |
2907 | 0 | if ((ret = key_schedule_select_cipher(tls->key_schedule, tls->cipher_suite, tls->client.offered_psk && !tls->is_psk_handshake, |
2908 | 0 | ptls_iovec_init(NULL, 0))) != 0) |
2909 | 0 | goto Exit; |
2910 | | |
2911 | | /* check if ECH is accepted */ |
2912 | 0 | if (tls->ech.aead != NULL) { |
2913 | 0 | size_t confirm_hash_off = 0; |
2914 | 0 | if (tls->ech.state != PTLS_ECH_STATE_GREASE) { |
2915 | 0 | confirm_hash_off = |
2916 | 0 | PTLS_HANDSHAKE_HEADER_SIZE + 2 /* legacy_version */ + PTLS_HELLO_RANDOM_SIZE - PTLS_ECH_CONFIRM_LENGTH; |
2917 | 0 | } |
2918 | 0 | if ((ret = client_ech_select_hello(tls, message, confirm_hash_off, ECH_CONFIRMATION_SERVER_HELLO)) != 0) |
2919 | 0 | goto Exit; |
2920 | 0 | } |
2921 | | |
2922 | | /* clear sensitive and space-consuming ECH state, now that are done with handling sending and decoding Hellos */ |
2923 | 0 | clear_ech(&tls->ech, 0); |
2924 | 0 | if (tls->key_schedule->hashes[0].ctx_outer != NULL) { |
2925 | 0 | tls->key_schedule->hashes[0].ctx_outer->final(tls->key_schedule->hashes[0].ctx_outer, NULL, PTLS_HASH_FINAL_MODE_FREE); |
2926 | 0 | tls->key_schedule->hashes[0].ctx_outer = NULL; |
2927 | 0 | } |
2928 | | |
2929 | | /* if the client offered external PSK but the server did not use that, we call it a handshake failure */ |
2930 | 0 | if (tls->ctx->pre_shared_key.identity.base != NULL && !tls->is_psk_handshake) { |
2931 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
2932 | 0 | goto Exit; |
2933 | 0 | } |
2934 | | |
2935 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
2936 | |
|
2937 | 0 | if (sh.peerkey.base != NULL) { |
2938 | 0 | if ((ret = tls->client.key_share_ctx->on_exchange(&tls->client.key_share_ctx, 1, &ecdh_secret, sh.peerkey)) != 0) { |
2939 | 0 | assert(ecdh_secret.base == NULL); |
2940 | 0 | goto Exit; |
2941 | 0 | } |
2942 | 0 | } |
2943 | | |
2944 | 0 | if ((ret = key_schedule_extract(tls->key_schedule, ecdh_secret)) != 0) |
2945 | 0 | goto Exit; |
2946 | 0 | if ((ret = setup_traffic_protection(tls, 0, "s hs traffic", 2, 0, 0)) != 0) |
2947 | 0 | goto Exit; |
2948 | 0 | if (tls->client.using_early_data) { |
2949 | 0 | if ((tls->pending_handshake_secret = malloc(PTLS_MAX_DIGEST_SIZE)) == NULL) { |
2950 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
2951 | 0 | goto Exit; |
2952 | 0 | } |
2953 | 0 | if ((ret = derive_secret(tls->key_schedule, tls->pending_handshake_secret, "c hs traffic")) != 0) |
2954 | 0 | goto Exit; |
2955 | 0 | if (tls->ctx->update_traffic_key != NULL && |
2956 | 0 | (ret = tls->ctx->update_traffic_key->cb(tls->ctx->update_traffic_key, tls, 1, 2, tls->pending_handshake_secret)) != 0) |
2957 | 0 | goto Exit; |
2958 | 0 | } else { |
2959 | 0 | if ((ret = setup_traffic_protection(tls, 1, "c hs traffic", 2, 0, 0)) != 0) |
2960 | 0 | goto Exit; |
2961 | 0 | } |
2962 | | |
2963 | 0 | tls->state = PTLS_STATE_CLIENT_EXPECT_ENCRYPTED_EXTENSIONS; |
2964 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
2965 | |
|
2966 | 0 | Exit: |
2967 | 0 | if (ecdh_secret.base != NULL) { |
2968 | 0 | ptls_clear_memory(ecdh_secret.base, ecdh_secret.len); |
2969 | 0 | free(ecdh_secret.base); |
2970 | 0 | } |
2971 | 0 | return ret; |
2972 | 0 | } |
2973 | | |
2974 | | static int should_collect_unknown_extension(ptls_t *tls, ptls_handshake_properties_t *properties, uint16_t type) |
2975 | 1.07k | { |
2976 | 1.07k | return properties != NULL && properties->collect_extension != NULL && properties->collect_extension(tls, properties, type); |
2977 | 1.07k | } |
2978 | | |
2979 | | static int collect_unknown_extension(ptls_t *tls, uint16_t type, const uint8_t *src, const uint8_t *const end, |
2980 | | ptls_raw_extension_t *slots) |
2981 | 0 | { |
2982 | 0 | size_t i; |
2983 | 0 | for (i = 0; slots[i].type != UINT16_MAX; ++i) { |
2984 | 0 | assert(i < MAX_UNKNOWN_EXTENSIONS); |
2985 | 0 | if (slots[i].type == type) |
2986 | 0 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
2987 | 0 | } |
2988 | 0 | if (i < MAX_UNKNOWN_EXTENSIONS) { |
2989 | 0 | slots[i].type = type; |
2990 | 0 | slots[i].data = ptls_iovec_init(src, end - src); |
2991 | 0 | slots[i + 1].type = UINT16_MAX; |
2992 | 0 | } |
2993 | 0 | return 0; |
2994 | 0 | } |
2995 | | |
2996 | | static int report_unknown_extensions(ptls_t *tls, ptls_handshake_properties_t *properties, ptls_raw_extension_t *slots) |
2997 | 384 | { |
2998 | 384 | if (properties != NULL && properties->collect_extension != NULL) { |
2999 | 0 | assert(properties->collected_extensions != NULL); |
3000 | 0 | return properties->collected_extensions(tls, properties, slots); |
3001 | 384 | } else { |
3002 | 384 | return 0; |
3003 | 384 | } |
3004 | 384 | } |
3005 | | |
3006 | | static int client_handle_encrypted_extensions(ptls_t *tls, ptls_iovec_t message, ptls_handshake_properties_t *properties) |
3007 | 0 | { |
3008 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
3009 | 0 | uint16_t type; |
3010 | 0 | static const ptls_raw_extension_t no_unknown_extensions = {UINT16_MAX}; |
3011 | 0 | ptls_raw_extension_t *unknown_extensions = (ptls_raw_extension_t *)&no_unknown_extensions; |
3012 | 0 | int ret, skip_early_data = 1; |
3013 | 0 | uint8_t server_offered_cert_type = PTLS_CERTIFICATE_TYPE_X509; |
3014 | |
|
3015 | 0 | decode_extensions(src, end, PTLS_HANDSHAKE_TYPE_ENCRYPTED_EXTENSIONS, &type, { |
3016 | 0 | if (tls->ctx->on_extension != NULL && |
3017 | 0 | (ret = tls->ctx->on_extension->cb(tls->ctx->on_extension, tls, PTLS_HANDSHAKE_TYPE_ENCRYPTED_EXTENSIONS, type, |
3018 | 0 | ptls_iovec_init(src, end - src)) != 0)) |
3019 | 0 | goto Exit; |
3020 | 0 | switch (type) { |
3021 | 0 | case PTLS_EXTENSION_TYPE_SERVER_NAME: |
3022 | 0 | if (src != end) { |
3023 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
3024 | 0 | goto Exit; |
3025 | 0 | } |
3026 | 0 | if (!(tls->server_name != NULL && !ptls_server_name_is_ipaddr(tls->server_name))) { |
3027 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3028 | 0 | goto Exit; |
3029 | 0 | } |
3030 | 0 | break; |
3031 | 0 | case PTLS_EXTENSION_TYPE_ALPN: |
3032 | 0 | ptls_decode_block(src, end, 2, { |
3033 | 0 | ptls_decode_open_block(src, end, 1, { |
3034 | 0 | if (src == end) { |
3035 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
3036 | 0 | goto Exit; |
3037 | 0 | } |
3038 | 0 | if ((ret = ptls_set_negotiated_protocol(tls, (const char *)src, end - src)) != 0) |
3039 | 0 | goto Exit; |
3040 | 0 | src = end; |
3041 | 0 | }); |
3042 | 0 | if (src != end) { |
3043 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
3044 | 0 | goto Exit; |
3045 | 0 | } |
3046 | 0 | }); |
3047 | 0 | break; |
3048 | 0 | case PTLS_EXTENSION_TYPE_EARLY_DATA: |
3049 | 0 | if (!tls->client.using_early_data) { |
3050 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3051 | 0 | goto Exit; |
3052 | 0 | } |
3053 | 0 | skip_early_data = 0; |
3054 | 0 | break; |
3055 | 0 | case PTLS_EXTENSION_TYPE_SERVER_CERTIFICATE_TYPE: |
3056 | 0 | if (end - src != 1) { |
3057 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
3058 | 0 | goto Exit; |
3059 | 0 | } |
3060 | 0 | server_offered_cert_type = *src; |
3061 | 0 | src = end; |
3062 | 0 | break; |
3063 | 0 | case PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO: { |
3064 | | /* accept retry_configs only if we offered ECH (or grease) but rejected */ |
3065 | 0 | if (!(tls->ech.state == PTLS_ECH_STATE_OFFERED || tls->ech.state == PTLS_ECH_STATE_GREASE)) { |
3066 | 0 | ret = PTLS_ALERT_UNSUPPORTED_EXTENSION; |
3067 | 0 | goto Exit; |
3068 | 0 | } |
3069 | | /* parse retry_config, and if it is applicable, provide that to the application (grease clients just verify syntax) */ |
3070 | 0 | struct st_decoded_ech_config_t decoded; |
3071 | 0 | if ((ret = client_decode_ech_config_list(tls->ctx, &decoded, ptls_iovec_init(src, end - src))) != 0) |
3072 | 0 | goto Exit; |
3073 | 0 | if (tls->ech.state == PTLS_ECH_STATE_GREASE) { |
3074 | | /* GREASE clients ignore retry_configs after verifying the syntax */ |
3075 | 0 | } else if (decoded.kem != NULL && decoded.cipher != NULL && properties != NULL && |
3076 | 0 | properties->client.ech.retry_configs != NULL) { |
3077 | 0 | if ((properties->client.ech.retry_configs->base = malloc(end - src)) == NULL) { |
3078 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
3079 | 0 | goto Exit; |
3080 | 0 | } |
3081 | 0 | memcpy(properties->client.ech.retry_configs->base, src, end - src); |
3082 | 0 | properties->client.ech.retry_configs->len = end - src; |
3083 | 0 | } |
3084 | 0 | src = end; |
3085 | 0 | } break; |
3086 | 0 | default: |
3087 | 0 | if (should_collect_unknown_extension(tls, properties, type)) { |
3088 | 0 | if (unknown_extensions == &no_unknown_extensions) { |
3089 | 0 | if ((unknown_extensions = malloc(sizeof(*unknown_extensions) * (MAX_UNKNOWN_EXTENSIONS + 1))) == NULL) { |
3090 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
3091 | 0 | goto Exit; |
3092 | 0 | } |
3093 | 0 | unknown_extensions[0].type = UINT16_MAX; |
3094 | 0 | } |
3095 | 0 | if ((ret = collect_unknown_extension(tls, type, src, end, unknown_extensions)) != 0) |
3096 | 0 | goto Exit; |
3097 | 0 | } |
3098 | 0 | break; |
3099 | 0 | } |
3100 | 0 | src = end; |
3101 | 0 | }); |
3102 | | |
3103 | 0 | if (server_offered_cert_type != |
3104 | 0 | (tls->ctx->use_raw_public_keys ? PTLS_CERTIFICATE_TYPE_RAW_PUBLIC_KEY : PTLS_CERTIFICATE_TYPE_X509)) { |
3105 | 0 | ret = PTLS_ALERT_UNSUPPORTED_CERTIFICATE; |
3106 | 0 | goto Exit; |
3107 | 0 | } |
3108 | | |
3109 | 0 | if (tls->client.using_early_data) { |
3110 | 0 | if (skip_early_data) |
3111 | 0 | tls->client.using_early_data = 0; |
3112 | 0 | if (properties != NULL) |
3113 | 0 | properties->client.early_data_acceptance = skip_early_data ? PTLS_EARLY_DATA_REJECTED : PTLS_EARLY_DATA_ACCEPTED; |
3114 | 0 | } |
3115 | 0 | if ((ret = report_unknown_extensions(tls, properties, unknown_extensions)) != 0) |
3116 | 0 | goto Exit; |
3117 | | |
3118 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3119 | 0 | tls->state = |
3120 | 0 | tls->is_psk_handshake ? PTLS_STATE_CLIENT_EXPECT_FINISHED : PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE; |
3121 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
3122 | |
|
3123 | 0 | Exit: |
3124 | 0 | if (unknown_extensions != &no_unknown_extensions) |
3125 | 0 | free(unknown_extensions); |
3126 | 0 | return ret; |
3127 | 0 | } |
3128 | | |
3129 | | static int decode_certificate_request(ptls_t *tls, struct st_ptls_certificate_request_t *cr, const uint8_t *src, |
3130 | | const uint8_t *const end) |
3131 | 0 | { |
3132 | 0 | int ret; |
3133 | 0 | uint16_t exttype = 0; |
3134 | | |
3135 | | /* certificate request context */ |
3136 | 0 | ptls_decode_open_block(src, end, 1, { |
3137 | 0 | size_t len = end - src; |
3138 | 0 | if (len > 255) { |
3139 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
3140 | 0 | goto Exit; |
3141 | 0 | } |
3142 | 0 | if ((cr->context.base = malloc(len != 0 ? len : 1)) == NULL) { |
3143 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
3144 | 0 | goto Exit; |
3145 | 0 | } |
3146 | 0 | cr->context.len = len; |
3147 | 0 | memcpy(cr->context.base, src, len); |
3148 | 0 | src = end; |
3149 | 0 | }); |
3150 | | |
3151 | | /* decode extensions */ |
3152 | 0 | decode_extensions(src, end, PTLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST, &exttype, { |
3153 | 0 | if (tls->ctx->on_extension != NULL && |
3154 | 0 | (ret = tls->ctx->on_extension->cb(tls->ctx->on_extension, tls, PTLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST, exttype, |
3155 | 0 | ptls_iovec_init(src, end - src)) != 0)) |
3156 | 0 | goto Exit; |
3157 | 0 | switch (exttype) { |
3158 | 0 | case PTLS_EXTENSION_TYPE_SIGNATURE_ALGORITHMS: |
3159 | 0 | if ((ret = decode_signature_algorithms(&cr->signature_algorithms, &src, end)) != 0) |
3160 | 0 | goto Exit; |
3161 | 0 | break; |
3162 | 0 | } |
3163 | 0 | src = end; |
3164 | 0 | }); |
3165 | | |
3166 | 0 | if (cr->signature_algorithms.count == 0) { |
3167 | 0 | ret = PTLS_ALERT_MISSING_EXTENSION; |
3168 | 0 | goto Exit; |
3169 | 0 | } |
3170 | | |
3171 | 0 | ret = 0; |
3172 | 0 | Exit: |
3173 | 0 | return ret; |
3174 | 0 | } |
3175 | | |
3176 | | int ptls_build_certificate_message(ptls_buffer_t *buf, ptls_iovec_t context, ptls_iovec_t *certificates, size_t num_certificates, |
3177 | | ptls_iovec_t ocsp_status) |
3178 | 242 | { |
3179 | 242 | int ret; |
3180 | | |
3181 | 242 | ptls_buffer_push_block(buf, 1, { ptls_buffer_pushv(buf, context.base, context.len); }); |
3182 | 242 | ptls_buffer_push_block(buf, 3, { |
3183 | 242 | size_t i; |
3184 | 242 | for (i = 0; i != num_certificates; ++i) { |
3185 | 242 | ptls_buffer_push_block(buf, 3, { ptls_buffer_pushv(buf, certificates[i].base, certificates[i].len); }); |
3186 | 242 | ptls_buffer_push_block(buf, 2, { |
3187 | 242 | if (i == 0 && ocsp_status.len != 0) { |
3188 | 242 | buffer_push_extension(buf, PTLS_EXTENSION_TYPE_STATUS_REQUEST, { |
3189 | 242 | ptls_buffer_push(buf, 1); /* status_type == ocsp */ |
3190 | 242 | ptls_buffer_push_block(buf, 3, { ptls_buffer_pushv(buf, ocsp_status.base, ocsp_status.len); }); |
3191 | 242 | }); |
3192 | 242 | } |
3193 | 242 | }); |
3194 | 242 | } |
3195 | 242 | }); |
3196 | | |
3197 | 242 | ret = 0; |
3198 | 242 | Exit: |
3199 | 242 | return ret; |
3200 | 242 | } |
3201 | | |
3202 | | static int default_emit_certificate_cb(ptls_emit_certificate_t *_self, ptls_t *tls, ptls_message_emitter_t *emitter, |
3203 | | ptls_key_schedule_t *key_sched, ptls_iovec_t context, int push_status_request, |
3204 | | const uint16_t *compress_algos, size_t num_compress_algos) |
3205 | 242 | { |
3206 | 242 | int ret; |
3207 | | |
3208 | 242 | ptls_push_message(emitter, key_sched, PTLS_HANDSHAKE_TYPE_CERTIFICATE, { |
3209 | 242 | if ((ret = ptls_build_certificate_message(emitter->buf, context, tls->ctx->certificates.list, tls->ctx->certificates.count, |
3210 | 242 | ptls_iovec_init(NULL, 0))) != 0) |
3211 | 242 | goto Exit; |
3212 | 242 | }); |
3213 | | |
3214 | 242 | ret = 0; |
3215 | 242 | Exit: |
3216 | 242 | return ret; |
3217 | 242 | } |
3218 | | |
3219 | | static int send_certificate(ptls_t *tls, ptls_message_emitter_t *emitter, |
3220 | | struct st_ptls_signature_algorithms_t *signature_algorithms, ptls_iovec_t context, |
3221 | | int push_status_request, const uint16_t *compress_algos, size_t num_compress_algos) |
3222 | 256 | { |
3223 | 256 | int ret; |
3224 | | |
3225 | 256 | if (signature_algorithms->count == 0) { |
3226 | 14 | ret = PTLS_ALERT_MISSING_EXTENSION; |
3227 | 14 | goto Exit; |
3228 | 14 | } |
3229 | | |
3230 | 242 | { /* send Certificate (or the equivalent) */ |
3231 | 242 | static ptls_emit_certificate_t default_emit_certificate = {default_emit_certificate_cb}; |
3232 | 242 | ptls_emit_certificate_t *emit_certificate = |
3233 | 242 | tls->ctx->emit_certificate != NULL ? tls->ctx->emit_certificate : &default_emit_certificate; |
3234 | 242 | Redo: |
3235 | 242 | if ((ret = emit_certificate->cb(emit_certificate, tls, emitter, tls->key_schedule, context, push_status_request, |
3236 | 242 | compress_algos, num_compress_algos)) != 0) { |
3237 | 0 | if (ret == PTLS_ERROR_DELEGATE) { |
3238 | 0 | assert(emit_certificate != &default_emit_certificate); |
3239 | 0 | emit_certificate = &default_emit_certificate; |
3240 | 0 | goto Redo; |
3241 | 0 | } |
3242 | 0 | goto Exit; |
3243 | 0 | } |
3244 | 242 | } |
3245 | | |
3246 | 256 | Exit: |
3247 | 256 | return ret; |
3248 | 242 | } |
3249 | | |
3250 | | static int send_certificate_verify(ptls_t *tls, ptls_message_emitter_t *emitter, |
3251 | | struct st_ptls_signature_algorithms_t *signature_algorithms, const char *context_string) |
3252 | 242 | { |
3253 | 242 | size_t start_off = emitter->buf->off; |
3254 | 242 | int ret; |
3255 | | |
3256 | 242 | if (tls->ctx->sign_certificate == NULL) |
3257 | 242 | return 0; |
3258 | | /* build and send CertificateVerify */ |
3259 | 0 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY, { |
3260 | 0 | ptls_buffer_t *sendbuf = emitter->buf; |
3261 | 0 | size_t algo_off = sendbuf->off; |
3262 | 0 | ptls_buffer_push16(sendbuf, 0); /* filled in later */ |
3263 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
3264 | 0 | uint16_t algo; |
3265 | 0 | uint8_t data[PTLS_MAX_CERTIFICATE_VERIFY_SIGNDATA_SIZE]; |
3266 | 0 | size_t datalen = build_certificate_verify_signdata(data, tls->key_schedule, context_string); |
3267 | 0 | if ((ret = tls->ctx->sign_certificate->cb( |
3268 | 0 | tls->ctx->sign_certificate, tls, tls->is_server ? &tls->server.async_job : NULL, &algo, sendbuf, |
3269 | 0 | ptls_iovec_init(data, datalen), signature_algorithms != NULL ? signature_algorithms->list : NULL, |
3270 | 0 | signature_algorithms != NULL ? signature_algorithms->count : 0)) == PTLS_ERROR_ASYNC_OPERATION) { |
3271 | 0 | assert(tls->is_server || !"async operation only supported on the server-side"); |
3272 | 0 | assert(tls->server.async_job != NULL); |
3273 | | /* Reset the output to the end of the previous handshake message. CertificateVerify will be rebuilt when the async |
3274 | | * operation completes. */ |
3275 | 0 | emitter->buf->off = start_off; |
3276 | 0 | goto Exit; |
3277 | 0 | } |
3278 | 0 | assert(!tls->is_server || tls->server.async_job == NULL); |
3279 | 0 | if (ret != 0) |
3280 | 0 | goto Exit; |
3281 | 0 | sendbuf->base[algo_off] = (uint8_t)(algo >> 8); |
3282 | 0 | sendbuf->base[algo_off + 1] = (uint8_t)algo; |
3283 | 0 | }); |
3284 | 0 | }); |
3285 | 0 | Exit: |
3286 | 0 | return ret; |
3287 | 0 | } |
3288 | | |
3289 | | static int client_handle_certificate_request(ptls_t *tls, ptls_iovec_t message, ptls_handshake_properties_t *properties) |
3290 | 0 | { |
3291 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
3292 | 0 | int ret = 0; |
3293 | |
|
3294 | 0 | assert(!tls->is_psk_handshake && "state machine asserts that this message is never delivered when PSK is used"); |
3295 | | |
3296 | 0 | if ((ret = decode_certificate_request(tls, &tls->client.certificate_request, src, end)) != 0) |
3297 | 0 | return ret; |
3298 | | |
3299 | | /* This field SHALL be zero length unless used for the post-handshake authentication exchanges (section 4.3.2) */ |
3300 | 0 | if (tls->client.certificate_request.context.len != 0) |
3301 | 0 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
3302 | | |
3303 | 0 | tls->state = PTLS_STATE_CLIENT_EXPECT_CERTIFICATE; |
3304 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3305 | |
|
3306 | 0 | return PTLS_ERROR_IN_PROGRESS; |
3307 | 0 | } |
3308 | | |
3309 | | static int handle_certificate(ptls_t *tls, const uint8_t *src, const uint8_t *end, int *got_certs) |
3310 | 0 | { |
3311 | 0 | ptls_iovec_t certs[16]; |
3312 | 0 | size_t num_certs = 0; |
3313 | 0 | int ret = 0; |
3314 | | |
3315 | | /* certificate request context */ |
3316 | 0 | ptls_decode_open_block(src, end, 1, { |
3317 | 0 | if (src != end) { |
3318 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3319 | 0 | goto Exit; |
3320 | 0 | } |
3321 | 0 | }); |
3322 | | /* certificate_list */ |
3323 | 0 | ptls_decode_block(src, end, 3, { |
3324 | 0 | while (src != end) { |
3325 | 0 | ptls_decode_open_block(src, end, 3, { |
3326 | 0 | if (num_certs < PTLS_ELEMENTSOF(certs)) |
3327 | 0 | certs[num_certs++] = ptls_iovec_init(src, end - src); |
3328 | 0 | src = end; |
3329 | 0 | }); |
3330 | 0 | uint16_t type; |
3331 | 0 | decode_open_extensions(src, end, PTLS_HANDSHAKE_TYPE_CERTIFICATE, &type, { |
3332 | 0 | if (tls->ctx->on_extension != NULL && |
3333 | 0 | (ret = tls->ctx->on_extension->cb(tls->ctx->on_extension, tls, PTLS_HANDSHAKE_TYPE_CERTIFICATE, type, |
3334 | 0 | ptls_iovec_init(src, end - src)) != 0)) |
3335 | 0 | goto Exit; |
3336 | 0 | src = end; |
3337 | 0 | }); |
3338 | 0 | } |
3339 | 0 | }); |
3340 | | |
3341 | 0 | if (tls->ctx->verify_certificate != NULL) { |
3342 | 0 | const char *server_name = NULL; |
3343 | 0 | if (!ptls_is_server(tls)) { |
3344 | 0 | if (tls->ech.state == PTLS_ECH_STATE_OFFERED) { |
3345 | 0 | server_name = tls->ech.client.public_name; |
3346 | 0 | } else { |
3347 | 0 | server_name = tls->server_name; |
3348 | 0 | } |
3349 | 0 | } |
3350 | 0 | if ((ret = tls->ctx->verify_certificate->cb(tls->ctx->verify_certificate, tls, server_name, &tls->certificate_verify.cb, |
3351 | 0 | &tls->certificate_verify.verify_ctx, certs, num_certs)) != 0) |
3352 | 0 | goto Exit; |
3353 | 0 | } |
3354 | | |
3355 | 0 | *got_certs = num_certs != 0; |
3356 | |
|
3357 | 0 | Exit: |
3358 | 0 | return ret; |
3359 | 0 | } |
3360 | | |
3361 | | static int client_do_handle_certificate(ptls_t *tls, const uint8_t *src, const uint8_t *end) |
3362 | 0 | { |
3363 | 0 | int got_certs, ret; |
3364 | |
|
3365 | 0 | if ((ret = handle_certificate(tls, src, end, &got_certs)) != 0) |
3366 | 0 | return ret; |
3367 | 0 | if (!got_certs) |
3368 | 0 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
3369 | | |
3370 | 0 | return 0; |
3371 | 0 | } |
3372 | | |
3373 | | static int client_handle_certificate(ptls_t *tls, ptls_iovec_t message) |
3374 | 0 | { |
3375 | 0 | int ret; |
3376 | |
|
3377 | 0 | if ((ret = client_do_handle_certificate(tls, message.base + PTLS_HANDSHAKE_HEADER_SIZE, message.base + message.len)) != 0) |
3378 | 0 | return ret; |
3379 | | |
3380 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3381 | |
|
3382 | 0 | tls->state = PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_VERIFY; |
3383 | 0 | return PTLS_ERROR_IN_PROGRESS; |
3384 | 0 | } |
3385 | | |
3386 | | static int client_handle_compressed_certificate(ptls_t *tls, ptls_iovec_t message) |
3387 | 0 | { |
3388 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
3389 | 0 | uint16_t algo; |
3390 | 0 | uint32_t uncompressed_size; |
3391 | 0 | uint8_t *uncompressed = NULL; |
3392 | 0 | int ret; |
3393 | |
|
3394 | 0 | if (tls->ctx->decompress_certificate == NULL) { |
3395 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
3396 | 0 | goto Exit; |
3397 | 0 | } |
3398 | | |
3399 | | /* decode */ |
3400 | 0 | if ((ret = ptls_decode16(&algo, &src, end)) != 0) |
3401 | 0 | goto Exit; |
3402 | 0 | if ((ret = ptls_decode24(&uncompressed_size, &src, end)) != 0) |
3403 | 0 | goto Exit; |
3404 | 0 | if (uncompressed_size > 65536) { /* TODO find a sensible number */ |
3405 | 0 | ret = PTLS_ALERT_BAD_CERTIFICATE; |
3406 | 0 | goto Exit; |
3407 | 0 | } |
3408 | 0 | if ((uncompressed = malloc(uncompressed_size)) == NULL) { |
3409 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
3410 | 0 | goto Exit; |
3411 | 0 | } |
3412 | 0 | ptls_decode_block(src, end, 3, { |
3413 | 0 | if ((ret = tls->ctx->decompress_certificate->cb(tls->ctx->decompress_certificate, tls, algo, |
3414 | 0 | ptls_iovec_init(uncompressed, uncompressed_size), |
3415 | 0 | ptls_iovec_init(src, end - src))) != 0) |
3416 | 0 | goto Exit; |
3417 | 0 | src = end; |
3418 | 0 | }); |
3419 | | |
3420 | | /* handle */ |
3421 | 0 | if ((ret = client_do_handle_certificate(tls, uncompressed, uncompressed + uncompressed_size)) != 0) |
3422 | 0 | goto Exit; |
3423 | | |
3424 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3425 | 0 | tls->state = PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_VERIFY; |
3426 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
3427 | |
|
3428 | 0 | Exit: |
3429 | 0 | free(uncompressed); |
3430 | 0 | return ret; |
3431 | 0 | } |
3432 | | |
3433 | | static int server_handle_certificate(ptls_t *tls, ptls_iovec_t message) |
3434 | 0 | { |
3435 | 0 | int got_certs, ret; |
3436 | |
|
3437 | 0 | if ((ret = handle_certificate(tls, message.base + PTLS_HANDSHAKE_HEADER_SIZE, message.base + message.len, &got_certs)) != 0) |
3438 | 0 | return ret; |
3439 | | |
3440 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3441 | |
|
3442 | 0 | if (got_certs) { |
3443 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_CERTIFICATE_VERIFY; |
3444 | 0 | } else { |
3445 | | /* Client did not provide certificate, and the verifier says we can fail open. Therefore, the next message is Finished. */ |
3446 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_FINISHED; |
3447 | 0 | } |
3448 | |
|
3449 | 0 | return PTLS_ERROR_IN_PROGRESS; |
3450 | 0 | } |
3451 | | |
3452 | | static int handle_certificate_verify(ptls_t *tls, ptls_iovec_t message, const char *context_string) |
3453 | 0 | { |
3454 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
3455 | 0 | uint16_t algo; |
3456 | 0 | ptls_iovec_t signature; |
3457 | 0 | uint8_t signdata[PTLS_MAX_CERTIFICATE_VERIFY_SIGNDATA_SIZE]; |
3458 | 0 | size_t signdata_size; |
3459 | 0 | int ret; |
3460 | | |
3461 | | /* decode */ |
3462 | 0 | if ((ret = ptls_decode16(&algo, &src, end)) != 0) |
3463 | 0 | goto Exit; |
3464 | 0 | ptls_decode_block(src, end, 2, { |
3465 | 0 | signature = ptls_iovec_init(src, end - src); |
3466 | 0 | src = end; |
3467 | 0 | }); |
3468 | | |
3469 | 0 | signdata_size = build_certificate_verify_signdata(signdata, tls->key_schedule, context_string); |
3470 | 0 | if (tls->certificate_verify.cb != NULL) { |
3471 | 0 | ret = tls->certificate_verify.cb(tls->certificate_verify.verify_ctx, algo, ptls_iovec_init(signdata, signdata_size), |
3472 | 0 | signature); |
3473 | 0 | } else { |
3474 | 0 | ret = 0; |
3475 | 0 | } |
3476 | 0 | ptls_clear_memory(signdata, signdata_size); |
3477 | 0 | tls->certificate_verify.cb = NULL; |
3478 | 0 | if (ret != 0) { |
3479 | 0 | goto Exit; |
3480 | 0 | } |
3481 | | |
3482 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3483 | |
|
3484 | 0 | Exit: |
3485 | 0 | return ret; |
3486 | 0 | } |
3487 | | |
3488 | | static int client_handle_certificate_verify(ptls_t *tls, ptls_iovec_t message) |
3489 | 0 | { |
3490 | 0 | int ret = handle_certificate_verify(tls, message, PTLS_SERVER_CERTIFICATE_VERIFY_CONTEXT_STRING); |
3491 | |
|
3492 | 0 | if (ret == 0) { |
3493 | 0 | tls->state = PTLS_STATE_CLIENT_EXPECT_FINISHED; |
3494 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
3495 | 0 | } |
3496 | |
|
3497 | 0 | return ret; |
3498 | 0 | } |
3499 | | |
3500 | | static int server_handle_certificate_verify(ptls_t *tls, ptls_iovec_t message) |
3501 | 0 | { |
3502 | 0 | int ret = handle_certificate_verify(tls, message, PTLS_CLIENT_CERTIFICATE_VERIFY_CONTEXT_STRING); |
3503 | |
|
3504 | 0 | if (ret == 0) { |
3505 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_FINISHED; |
3506 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
3507 | 0 | } |
3508 | |
|
3509 | 0 | return ret; |
3510 | 0 | } |
3511 | | |
3512 | | static int client_handle_finished(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message) |
3513 | 0 | { |
3514 | 0 | uint8_t send_secret[PTLS_MAX_DIGEST_SIZE]; |
3515 | 0 | int alert_ech_required = tls->ech.state == PTLS_ECH_STATE_OFFERED, ret; |
3516 | |
|
3517 | 0 | if ((ret = verify_finished(tls, message)) != 0) |
3518 | 0 | goto Exit; |
3519 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
3520 | | |
3521 | | /* update traffic keys by using messages upto ServerFinished, but commission them after sending ClientFinished */ |
3522 | 0 | if ((ret = key_schedule_extract(tls->key_schedule, ptls_iovec_init(NULL, 0))) != 0) |
3523 | 0 | goto Exit; |
3524 | 0 | if ((ret = setup_traffic_protection(tls, 0, "s ap traffic", 3, 0, 0)) != 0) |
3525 | 0 | goto Exit; |
3526 | 0 | if ((ret = derive_secret(tls->key_schedule, send_secret, "c ap traffic")) != 0) |
3527 | 0 | goto Exit; |
3528 | 0 | if ((ret = derive_exporter_secret(tls, 0)) != 0) |
3529 | 0 | goto Exit; |
3530 | | |
3531 | | /* if sending early data, emit EOED and commision the client handshake traffic secret */ |
3532 | 0 | if (tls->pending_handshake_secret != NULL) { |
3533 | 0 | assert(tls->traffic_protection.enc.aead != NULL || tls->ctx->update_traffic_key != NULL); |
3534 | 0 | if (tls->client.using_early_data && !tls->ctx->omit_end_of_early_data) |
3535 | 0 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_END_OF_EARLY_DATA, {}); |
3536 | 0 | tls->client.using_early_data = 0; |
3537 | 0 | if ((ret = commission_handshake_secret(tls)) != 0) |
3538 | 0 | goto Exit; |
3539 | 0 | } |
3540 | | |
3541 | 0 | if ((ret = push_change_cipher_spec(tls, emitter)) != 0) |
3542 | 0 | goto Exit; |
3543 | | |
3544 | 0 | if (!alert_ech_required && tls->client.certificate_request.context.base != NULL) { |
3545 | 0 | if ((ret = send_certificate(tls, emitter, &tls->client.certificate_request.signature_algorithms, |
3546 | 0 | tls->client.certificate_request.context, 0, NULL, 0)) == 0) |
3547 | 0 | ret = send_certificate_verify(tls, emitter, &tls->client.certificate_request.signature_algorithms, |
3548 | 0 | PTLS_CLIENT_CERTIFICATE_VERIFY_CONTEXT_STRING); |
3549 | 0 | free(tls->client.certificate_request.context.base); |
3550 | 0 | tls->client.certificate_request.context = ptls_iovec_init(NULL, 0); |
3551 | 0 | if (ret != 0) |
3552 | 0 | goto Exit; |
3553 | 0 | } |
3554 | | |
3555 | 0 | ret = send_finished(tls, emitter); |
3556 | |
|
3557 | 0 | memcpy(tls->traffic_protection.enc.secret, send_secret, sizeof(send_secret)); |
3558 | 0 | if ((ret = setup_traffic_protection(tls, 1, NULL, 3, 0, 0)) != 0) |
3559 | 0 | goto Exit; |
3560 | | |
3561 | 0 | tls->state = PTLS_STATE_CLIENT_POST_HANDSHAKE; |
3562 | | |
3563 | | /* if ECH was rejected, close the connection with ECH_REQUIRED alert after verifying messages up to Finished */ |
3564 | 0 | if (alert_ech_required) |
3565 | 0 | ret = PTLS_ALERT_ECH_REQUIRED; |
3566 | |
|
3567 | 0 | Exit: |
3568 | 0 | ptls_clear_memory(send_secret, sizeof(send_secret)); |
3569 | 0 | return ret; |
3570 | 0 | } |
3571 | | |
3572 | | static int client_handle_new_session_ticket(ptls_t *tls, ptls_iovec_t message) |
3573 | 0 | { |
3574 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
3575 | 0 | ptls_iovec_t ticket_nonce; |
3576 | 0 | int ret; |
3577 | |
|
3578 | 0 | { /* verify the format */ |
3579 | 0 | uint32_t ticket_lifetime, ticket_age_add, max_early_data_size; |
3580 | 0 | ptls_iovec_t ticket; |
3581 | 0 | if ((ret = decode_new_session_ticket(tls, &ticket_lifetime, &ticket_age_add, &ticket_nonce, &ticket, &max_early_data_size, |
3582 | 0 | src, end)) != 0) |
3583 | 0 | return ret; |
3584 | 0 | } |
3585 | | |
3586 | | /* do nothing if use of session ticket is disabled */ |
3587 | 0 | if (tls->ctx->save_ticket == NULL) |
3588 | 0 | return 0; |
3589 | | |
3590 | | /* save the extension, along with the key of myself */ |
3591 | 0 | ptls_buffer_t ticket_buf; |
3592 | 0 | ptls_buffer_init(&ticket_buf, "", 0); |
3593 | 0 | ptls_buffer_push64(&ticket_buf, tls->ctx->get_time->cb(tls->ctx->get_time)); |
3594 | 0 | ptls_buffer_push16(&ticket_buf, tls->key_share->id); |
3595 | 0 | ptls_buffer_push16(&ticket_buf, tls->cipher_suite->id); |
3596 | 0 | ptls_buffer_push_block(&ticket_buf, 3, { ptls_buffer_pushv(&ticket_buf, src, end - src); }); |
3597 | 0 | ptls_buffer_push_block(&ticket_buf, 2, { |
3598 | 0 | if ((ret = ptls_buffer_reserve(&ticket_buf, tls->key_schedule->hashes[0].algo->digest_size)) != 0) |
3599 | 0 | goto Exit; |
3600 | 0 | if ((ret = derive_resumption_secret(tls->key_schedule, ticket_buf.base + ticket_buf.off, ticket_nonce)) != 0) |
3601 | 0 | goto Exit; |
3602 | 0 | ticket_buf.off += tls->key_schedule->hashes[0].algo->digest_size; |
3603 | 0 | }); |
3604 | | |
3605 | 0 | if ((ret = tls->ctx->save_ticket->cb(tls->ctx->save_ticket, tls, ptls_iovec_init(ticket_buf.base, ticket_buf.off))) != 0) |
3606 | 0 | goto Exit; |
3607 | | |
3608 | 0 | ret = 0; |
3609 | 0 | Exit: |
3610 | 0 | ptls_buffer_dispose(&ticket_buf); |
3611 | 0 | return ret; |
3612 | 0 | } |
3613 | | |
3614 | | static int client_hello_decode_server_name(ptls_iovec_t *name, const uint8_t **src, const uint8_t *const end) |
3615 | 141 | { |
3616 | 141 | int ret = 0; |
3617 | | |
3618 | 141 | ptls_decode_open_block(*src, end, 2, { |
3619 | 141 | do { |
3620 | 141 | uint8_t type; |
3621 | 141 | if ((ret = ptls_decode8(&type, src, end)) != 0) |
3622 | 141 | goto Exit; |
3623 | 141 | ptls_decode_open_block(*src, end, 2, { |
3624 | 141 | switch (type) { |
3625 | 141 | case PTLS_SERVER_NAME_TYPE_HOSTNAME: |
3626 | 141 | if (end - *src == 0) { |
3627 | 141 | ret = PTLS_ALERT_DECODE_ERROR; |
3628 | 141 | goto Exit; |
3629 | 141 | } |
3630 | 141 | if (memchr(*src, '\0', end - *src) != 0) { |
3631 | 141 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3632 | 141 | goto Exit; |
3633 | 141 | } |
3634 | 141 | *name = ptls_iovec_init(*src, end - *src); |
3635 | 141 | break; |
3636 | 141 | default: |
3637 | 141 | break; |
3638 | 141 | } |
3639 | 141 | *src = end; |
3640 | 141 | }); |
3641 | 141 | } while (*src != end); |
3642 | 141 | }); |
3643 | | |
3644 | 141 | Exit: |
3645 | 141 | return ret; |
3646 | 141 | } |
3647 | | |
3648 | | static int select_negotiated_group(ptls_key_exchange_algorithm_t **selected, ptls_key_exchange_algorithm_t **candidates, |
3649 | | const uint8_t *src, const uint8_t *const end) |
3650 | 218 | { |
3651 | 218 | int ret; |
3652 | | |
3653 | 218 | ptls_decode_block(src, end, 2, { |
3654 | 218 | while (src != end) { |
3655 | 218 | uint16_t group; |
3656 | 218 | if ((ret = ptls_decode16(&group, &src, end)) != 0) |
3657 | 218 | goto Exit; |
3658 | 218 | ptls_key_exchange_algorithm_t **c = candidates; |
3659 | 218 | for (; *c != NULL; ++c) { |
3660 | 218 | if ((*c)->id == group) { |
3661 | 218 | *selected = *c; |
3662 | 218 | return 0; |
3663 | 218 | } |
3664 | 218 | } |
3665 | 218 | } |
3666 | 218 | }); |
3667 | | |
3668 | 22 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
3669 | | |
3670 | 64 | Exit: |
3671 | 64 | return ret; |
3672 | 22 | } |
3673 | | |
3674 | | static int decode_client_hello(ptls_context_t *ctx, struct st_ptls_client_hello_t *ch, const uint8_t *src, const uint8_t *const end, |
3675 | | ptls_handshake_properties_t *properties, ptls_t *tls_cbarg) |
3676 | 2.01k | { |
3677 | 2.01k | const uint8_t *start = src; |
3678 | 2.01k | uint16_t exttype = 0; |
3679 | 2.01k | int ret; |
3680 | | |
3681 | | /* decode protocol version (do not bare to decode something older than TLS 1.0) */ |
3682 | 2.01k | if ((ret = ptls_decode16(&ch->legacy_version, &src, end)) != 0) |
3683 | 5 | goto Exit; |
3684 | 2.00k | if (ch->legacy_version < 0x0301) { |
3685 | 14 | ret = PTLS_ALERT_PROTOCOL_VERSION; |
3686 | 14 | goto Exit; |
3687 | 14 | } |
3688 | | |
3689 | | /* skip random */ |
3690 | 1.99k | if (end - src < PTLS_HELLO_RANDOM_SIZE) { |
3691 | 28 | ret = PTLS_ALERT_DECODE_ERROR; |
3692 | 28 | goto Exit; |
3693 | 28 | } |
3694 | 1.96k | ch->random_bytes = src; |
3695 | 1.96k | src += PTLS_HELLO_RANDOM_SIZE; |
3696 | | |
3697 | | /* skip legacy_session_id */ |
3698 | 1.96k | ptls_decode_open_block(src, end, 1, { |
3699 | 1.96k | if (end - src > 32) { |
3700 | 1.96k | ret = PTLS_ALERT_DECODE_ERROR; |
3701 | 1.96k | goto Exit; |
3702 | 1.96k | } |
3703 | 1.96k | ch->legacy_session_id = ptls_iovec_init(src, end - src); |
3704 | 1.96k | src = end; |
3705 | 1.96k | }); |
3706 | | |
3707 | | /* decode and select from ciphersuites */ |
3708 | 1.94k | ptls_decode_open_block(src, end, 2, { |
3709 | 1.94k | if ((end - src) % 2 != 0) { |
3710 | 1.94k | ret = PTLS_ALERT_DECODE_ERROR; |
3711 | 1.94k | goto Exit; |
3712 | 1.94k | } |
3713 | 1.94k | ch->cipher_suites = ptls_iovec_init(src, end - src); |
3714 | 1.94k | src = end; |
3715 | 1.94k | }); |
3716 | | |
3717 | | /* decode legacy_compression_methods */ |
3718 | 1.90k | ptls_decode_open_block(src, end, 1, { |
3719 | 1.90k | if (src == end) { |
3720 | 1.90k | ret = PTLS_ALERT_DECODE_ERROR; |
3721 | 1.90k | goto Exit; |
3722 | 1.90k | } |
3723 | 1.90k | ch->compression_methods.ids = src; |
3724 | 1.90k | ch->compression_methods.count = end - src; |
3725 | 1.90k | src = end; |
3726 | 1.90k | }); |
3727 | | |
3728 | | /* In TLS versions 1.2 and earlier CH might not have an extensions block (or they might, see what OpenSSL 1.0.0 sends); so bail |
3729 | | * out if that is the case after parsing the main variables. Zero is returned as it is a valid ClientHello. However |
3730 | | * `ptls_t::selected_version` remains zero indicating that no compatible version were found. */ |
3731 | 1.87k | if (src == end) { |
3732 | 13 | ret = 0; |
3733 | 13 | goto Exit; |
3734 | 13 | } |
3735 | | |
3736 | | /* decode extensions */ |
3737 | 1.86k | ch->first_extension_at = src - start + 2; |
3738 | 1.86k | decode_extensions(src, end, PTLS_HANDSHAKE_TYPE_CLIENT_HELLO, &exttype, { |
3739 | 1.86k | ch->psk.is_last_extension = 0; |
3740 | 1.86k | if (ctx->on_extension != NULL && tls_cbarg != NULL && |
3741 | 1.86k | (ret = ctx->on_extension->cb(ctx->on_extension, tls_cbarg, PTLS_HANDSHAKE_TYPE_CLIENT_HELLO, exttype, |
3742 | 1.86k | ptls_iovec_init(src, end - src)) != 0)) |
3743 | 1.86k | goto Exit; |
3744 | 1.86k | switch (exttype) { |
3745 | 1.86k | case PTLS_EXTENSION_TYPE_SERVER_NAME: |
3746 | 1.86k | if ((ret = client_hello_decode_server_name(&ch->server_name, &src, end)) != 0) |
3747 | 1.86k | goto Exit; |
3748 | 1.86k | if (src != end) { |
3749 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3750 | 1.86k | goto Exit; |
3751 | 1.86k | } |
3752 | 1.86k | break; |
3753 | 1.86k | case PTLS_EXTENSION_TYPE_ALPN: |
3754 | 1.86k | ptls_decode_block(src, end, 2, { |
3755 | 1.86k | do { |
3756 | 1.86k | ptls_decode_open_block(src, end, 1, { |
3757 | | /* rfc7301 3.1: empty strings MUST NOT be included */ |
3758 | 1.86k | if (src == end) { |
3759 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3760 | 1.86k | goto Exit; |
3761 | 1.86k | } |
3762 | 1.86k | if (ch->alpn.count < PTLS_ELEMENTSOF(ch->alpn.list)) |
3763 | 1.86k | ch->alpn.list[ch->alpn.count++] = ptls_iovec_init(src, end - src); |
3764 | 1.86k | src = end; |
3765 | 1.86k | }); |
3766 | 1.86k | } while (src != end); |
3767 | 1.86k | }); |
3768 | 1.86k | break; |
3769 | 1.86k | case PTLS_EXTENSION_TYPE_SERVER_CERTIFICATE_TYPE: |
3770 | 1.86k | ptls_decode_block(src, end, 1, { |
3771 | 1.86k | size_t list_size = end - src; |
3772 | | |
3773 | | /* RFC7250 4.1: No empty list, no list with single x509 element */ |
3774 | 1.86k | if (list_size == 0 || (list_size == 1 && *src == PTLS_CERTIFICATE_TYPE_X509)) { |
3775 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3776 | 1.86k | goto Exit; |
3777 | 1.86k | } |
3778 | | |
3779 | 1.86k | do { |
3780 | 1.86k | if (ch->server_certificate_types.count < PTLS_ELEMENTSOF(ch->server_certificate_types.list)) |
3781 | 1.86k | ch->server_certificate_types.list[ch->server_certificate_types.count++] = *src; |
3782 | 1.86k | src++; |
3783 | 1.86k | } while (src != end); |
3784 | 1.86k | }); |
3785 | 1.86k | break; |
3786 | 1.86k | case PTLS_EXTENSION_TYPE_COMPRESS_CERTIFICATE: |
3787 | 1.86k | ptls_decode_block(src, end, 1, { |
3788 | 1.86k | do { |
3789 | 1.86k | uint16_t id; |
3790 | 1.86k | if ((ret = ptls_decode16(&id, &src, end)) != 0) |
3791 | 1.86k | goto Exit; |
3792 | 1.86k | if (ch->cert_compression_algos.count < PTLS_ELEMENTSOF(ch->cert_compression_algos.list)) |
3793 | 1.86k | ch->cert_compression_algos.list[ch->cert_compression_algos.count++] = id; |
3794 | 1.86k | } while (src != end); |
3795 | 1.86k | }); |
3796 | 1.86k | break; |
3797 | 1.86k | case PTLS_EXTENSION_TYPE_SUPPORTED_GROUPS: |
3798 | 1.86k | ch->negotiated_groups = ptls_iovec_init(src, end - src); |
3799 | 1.86k | break; |
3800 | 1.86k | case PTLS_EXTENSION_TYPE_SIGNATURE_ALGORITHMS: |
3801 | 1.86k | if ((ret = decode_signature_algorithms(&ch->signature_algorithms, &src, end)) != 0) |
3802 | 1.86k | goto Exit; |
3803 | 1.86k | break; |
3804 | 1.86k | case PTLS_EXTENSION_TYPE_KEY_SHARE: |
3805 | 1.86k | ch->key_shares = ptls_iovec_init(src, end - src); |
3806 | 1.86k | break; |
3807 | 1.86k | case PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS: |
3808 | 1.86k | ptls_decode_block(src, end, 1, { |
3809 | 1.86k | size_t selected_index = PTLS_ELEMENTSOF(supported_versions); |
3810 | 1.86k | do { |
3811 | 1.86k | size_t i; |
3812 | 1.86k | uint16_t v; |
3813 | 1.86k | if ((ret = ptls_decode16(&v, &src, end)) != 0) |
3814 | 1.86k | goto Exit; |
3815 | 1.86k | for (i = 0; i != selected_index; ++i) { |
3816 | 1.86k | if (supported_versions[i] == v) { |
3817 | 1.86k | selected_index = i; |
3818 | 1.86k | break; |
3819 | 1.86k | } |
3820 | 1.86k | } |
3821 | 1.86k | } while (src != end); |
3822 | 1.86k | if (selected_index != PTLS_ELEMENTSOF(supported_versions)) |
3823 | 1.86k | ch->selected_version = supported_versions[selected_index]; |
3824 | 1.86k | }); |
3825 | 1.86k | break; |
3826 | 1.86k | case PTLS_EXTENSION_TYPE_COOKIE: |
3827 | 1.86k | if (properties == NULL || properties->server.cookie.key == NULL) { |
3828 | 1.86k | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3829 | 1.86k | goto Exit; |
3830 | 1.86k | } |
3831 | 1.86k | ch->cookie.all = ptls_iovec_init(src, end - src); |
3832 | 1.86k | ptls_decode_block(src, end, 2, { |
3833 | 1.86k | ch->cookie.tbs.base = (void *)src; |
3834 | 1.86k | ptls_decode_open_block(src, end, 2, { |
3835 | 1.86k | ptls_decode_open_block(src, end, 1, { |
3836 | 1.86k | ch->cookie.ch1_hash = ptls_iovec_init(src, end - src); |
3837 | 1.86k | src = end; |
3838 | 1.86k | }); |
3839 | 1.86k | uint8_t sent_key_share; |
3840 | 1.86k | if ((ret = ptls_decode8(&sent_key_share, &src, end)) != 0) |
3841 | 1.86k | goto Exit; |
3842 | 1.86k | switch (sent_key_share) { |
3843 | 1.86k | case 0: |
3844 | 1.86k | assert(!ch->cookie.sent_key_share); |
3845 | 1.86k | break; |
3846 | 1.86k | case 1: |
3847 | 1.86k | ch->cookie.sent_key_share = 1; |
3848 | 1.86k | break; |
3849 | 1.86k | default: |
3850 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3851 | 1.86k | goto Exit; |
3852 | 1.86k | } |
3853 | 1.86k | }); |
3854 | 1.86k | ch->cookie.tbs.len = src - ch->cookie.tbs.base; |
3855 | 1.86k | ptls_decode_block(src, end, 1, { |
3856 | 1.86k | ch->cookie.signature = ptls_iovec_init(src, end - src); |
3857 | 1.86k | src = end; |
3858 | 1.86k | }); |
3859 | 1.86k | }); |
3860 | 1.86k | break; |
3861 | 1.86k | case PTLS_EXTENSION_TYPE_PRE_SHARED_KEY: { |
3862 | 1.86k | size_t num_identities = 0; |
3863 | 1.86k | ptls_decode_open_block(src, end, 2, { |
3864 | 1.86k | do { |
3865 | 1.86k | ptls_client_hello_psk_identity_t psk = {{NULL}}; |
3866 | 1.86k | ptls_decode_open_block(src, end, 2, { |
3867 | 1.86k | if (end - src < 1) { |
3868 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3869 | 1.86k | goto Exit; |
3870 | 1.86k | } |
3871 | 1.86k | psk.identity = ptls_iovec_init(src, end - src); |
3872 | 1.86k | src = end; |
3873 | 1.86k | }); |
3874 | 1.86k | if ((ret = ptls_decode32(&psk.obfuscated_ticket_age, &src, end)) != 0) |
3875 | 1.86k | goto Exit; |
3876 | 1.86k | if (ch->psk.identities.count < PTLS_ELEMENTSOF(ch->psk.identities.list)) |
3877 | 1.86k | ch->psk.identities.list[ch->psk.identities.count++] = psk; |
3878 | 1.86k | ++num_identities; |
3879 | 1.86k | } while (src != end); |
3880 | 1.86k | }); |
3881 | 1.86k | ch->psk.hash_end = src; |
3882 | 1.86k | ptls_decode_block(src, end, 2, { |
3883 | 1.86k | size_t num_binders = 0; |
3884 | 1.86k | do { |
3885 | 1.86k | ptls_decode_open_block(src, end, 1, { |
3886 | 1.86k | if (num_binders < ch->psk.identities.count) |
3887 | 1.86k | ch->psk.identities.list[num_binders].binder = ptls_iovec_init(src, end - src); |
3888 | 1.86k | src = end; |
3889 | 1.86k | }); |
3890 | 1.86k | ++num_binders; |
3891 | 1.86k | } while (src != end); |
3892 | 1.86k | if (num_identities != num_binders) { |
3893 | 1.86k | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3894 | 1.86k | goto Exit; |
3895 | 1.86k | } |
3896 | 1.86k | }); |
3897 | 1.86k | ch->psk.is_last_extension = 1; |
3898 | 1.86k | } break; |
3899 | 1.86k | case PTLS_EXTENSION_TYPE_PSK_KEY_EXCHANGE_MODES: |
3900 | 1.86k | ptls_decode_block(src, end, 1, { |
3901 | 1.86k | do { |
3902 | 1.86k | uint8_t mode; |
3903 | 1.86k | if ((ret = ptls_decode8(&mode, &src, end)) != 0) |
3904 | 1.86k | goto Exit; |
3905 | 1.86k | if (mode < sizeof(ch->psk.ke_modes) * 8) |
3906 | 1.86k | ch->psk.ke_modes |= 1u << mode; |
3907 | 1.86k | } while (src != end); |
3908 | 1.86k | }); |
3909 | 1.86k | break; |
3910 | 1.86k | case PTLS_EXTENSION_TYPE_EARLY_DATA: |
3911 | 1.86k | ch->psk.early_data_indication = 1; |
3912 | 1.86k | break; |
3913 | 1.86k | case PTLS_EXTENSION_TYPE_STATUS_REQUEST: |
3914 | 1.86k | ch->status_request = 1; |
3915 | 1.86k | break; |
3916 | 1.86k | case PTLS_EXTENSION_TYPE_TICKET_REQUEST: |
3917 | 1.86k | if (end - src != 2) { |
3918 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3919 | 1.86k | goto Exit; |
3920 | 1.86k | } |
3921 | 1.86k | ch->ticket_request.new_session_count = *src++; |
3922 | 1.86k | ch->ticket_request.resumption_count = *src++; |
3923 | 1.86k | break; |
3924 | 1.86k | case PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO: |
3925 | 1.86k | if ((ret = ptls_decode8(&ch->ech.type, &src, end)) != 0) |
3926 | 1.86k | goto Exit; |
3927 | 1.86k | switch (ch->ech.type) { |
3928 | 1.86k | case PTLS_ECH_CLIENT_HELLO_TYPE_OUTER: |
3929 | 1.86k | if ((ret = ptls_decode16(&ch->ech.cipher_suite.kdf, &src, end)) != 0 || |
3930 | 1.86k | (ret = ptls_decode16(&ch->ech.cipher_suite.aead, &src, end)) != 0) |
3931 | 1.86k | goto Exit; |
3932 | 1.86k | if ((ret = ptls_decode8(&ch->ech.config_id, &src, end)) != 0) |
3933 | 1.86k | goto Exit; |
3934 | 1.86k | ptls_decode_open_block(src, end, 2, { |
3935 | 1.86k | ch->ech.enc = ptls_iovec_init(src, end - src); |
3936 | 1.86k | src = end; |
3937 | 1.86k | }); |
3938 | 1.86k | ptls_decode_open_block(src, end, 2, { |
3939 | 1.86k | if (src == end) { |
3940 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3941 | 1.86k | goto Exit; |
3942 | 1.86k | } |
3943 | 1.86k | ch->ech.payload = ptls_iovec_init(src, end - src); |
3944 | 1.86k | src = end; |
3945 | 1.86k | }); |
3946 | 1.86k | break; |
3947 | 1.86k | case PTLS_ECH_CLIENT_HELLO_TYPE_INNER: |
3948 | 1.86k | if (src != end) { |
3949 | 1.86k | ret = PTLS_ALERT_DECODE_ERROR; |
3950 | 1.86k | goto Exit; |
3951 | 1.86k | } |
3952 | 1.86k | ch->ech.payload = ptls_iovec_init("", 0); /* non-zero base indicates that the extension was received */ |
3953 | 1.86k | break; |
3954 | 1.86k | default: |
3955 | 1.86k | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3956 | 1.86k | goto Exit; |
3957 | 1.86k | } |
3958 | 1.86k | src = end; |
3959 | 1.86k | break; |
3960 | 1.86k | default: |
3961 | 1.86k | if (tls_cbarg != NULL && should_collect_unknown_extension(tls_cbarg, properties, exttype)) { |
3962 | 1.86k | if ((ret = collect_unknown_extension(tls_cbarg, exttype, src, end, ch->unknown_extensions)) != 0) |
3963 | 1.86k | goto Exit; |
3964 | 1.86k | } |
3965 | 1.86k | break; |
3966 | 1.86k | } |
3967 | 1.86k | src = end; |
3968 | 1.86k | }); |
3969 | | |
3970 | 883 | ret = 0; |
3971 | 2.01k | Exit: |
3972 | 2.01k | return ret; |
3973 | 883 | } |
3974 | | |
3975 | | static int rebuild_ch_inner_extensions(ptls_buffer_t *buf, const uint8_t **src, const uint8_t *const end, const uint8_t *outer_ext, |
3976 | | const uint8_t *outer_ext_end) |
3977 | 0 | { |
3978 | 0 | int ret; |
3979 | |
|
3980 | 0 | ptls_buffer_push_block(buf, 2, { |
3981 | 0 | ptls_decode_open_block(*src, end, 2, { |
3982 | 0 | while (*src != end) { |
3983 | 0 | uint16_t exttype; |
3984 | 0 | if ((ret = ptls_decode16(&exttype, src, end)) != 0) |
3985 | 0 | goto Exit; |
3986 | 0 | ptls_decode_open_block(*src, end, 2, { |
3987 | 0 | if (exttype == PTLS_EXTENSION_TYPE_ECH_OUTER_EXTENSIONS) { |
3988 | 0 | ptls_decode_open_block(*src, end, 1, { |
3989 | 0 | do { |
3990 | 0 | uint16_t reftype; |
3991 | 0 | uint16_t outertype; |
3992 | 0 | uint16_t outersize; |
3993 | 0 | if ((ret = ptls_decode16(&reftype, src, end)) != 0) |
3994 | 0 | goto Exit; |
3995 | 0 | if (reftype == PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO) { |
3996 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
3997 | 0 | goto Exit; |
3998 | 0 | } |
3999 | 0 | while (1) { |
4000 | 0 | if (ptls_decode16(&outertype, &outer_ext, outer_ext_end) != 0 || |
4001 | 0 | ptls_decode16(&outersize, &outer_ext, outer_ext_end) != 0) { |
4002 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4003 | 0 | goto Exit; |
4004 | 0 | } |
4005 | 0 | assert(outer_ext_end - outer_ext >= outersize); |
4006 | 0 | if (outertype == reftype) |
4007 | 0 | break; |
4008 | 0 | outer_ext += outersize; |
4009 | 0 | } |
4010 | 0 | buffer_push_extension(buf, reftype, { |
4011 | 0 | ptls_buffer_pushv(buf, outer_ext, outersize); |
4012 | 0 | outer_ext += outersize; |
4013 | 0 | }); |
4014 | 0 | } while (*src != end); |
4015 | 0 | }); |
4016 | 0 | } else { |
4017 | 0 | buffer_push_extension(buf, exttype, { |
4018 | 0 | ptls_buffer_pushv(buf, *src, end - *src); |
4019 | 0 | *src = end; |
4020 | 0 | }); |
4021 | 0 | } |
4022 | 0 | }); |
4023 | 0 | } |
4024 | 0 | }); |
4025 | 0 | }); |
4026 | | |
4027 | 0 | Exit: |
4028 | 0 | return ret; |
4029 | 0 | } |
4030 | | |
4031 | | static int rebuild_ch_inner(ptls_buffer_t *buf, const uint8_t *src, const uint8_t *const end, |
4032 | | struct st_ptls_client_hello_t *outer_ch, const uint8_t *outer_ext, const uint8_t *outer_ext_end) |
4033 | 0 | { |
4034 | 0 | #define COPY_BLOCK(capacity) \ |
4035 | 0 | do { \ |
4036 | 0 | ptls_decode_open_block(src, end, (capacity), { \ |
4037 | 0 | ptls_buffer_push_block(buf, (capacity), { ptls_buffer_pushv(buf, src, end - src); }); \ |
4038 | 0 | src = end; \ |
4039 | 0 | }); \ |
4040 | 0 | } while (0) |
4041 | |
|
4042 | 0 | int ret; |
4043 | |
|
4044 | 0 | ptls_buffer_push_message_body(buf, NULL, PTLS_HANDSHAKE_TYPE_CLIENT_HELLO, { |
4045 | 0 | { /* legacy_version */ |
4046 | 0 | uint16_t legacy_version; |
4047 | 0 | if ((ret = ptls_decode16(&legacy_version, &src, end)) != 0) |
4048 | 0 | goto Exit; |
4049 | 0 | ptls_buffer_push16(buf, legacy_version); |
4050 | 0 | } |
4051 | | |
4052 | | /* hello random */ |
4053 | 0 | if (end - src < PTLS_HELLO_RANDOM_SIZE) { |
4054 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
4055 | 0 | goto Exit; |
4056 | 0 | } |
4057 | 0 | ptls_buffer_pushv(buf, src, PTLS_HELLO_RANDOM_SIZE); |
4058 | 0 | src += PTLS_HELLO_RANDOM_SIZE; |
4059 | |
|
4060 | 0 | ptls_decode_open_block(src, end, 1, { |
4061 | 0 | if (src != end) { |
4062 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4063 | 0 | goto Exit; |
4064 | 0 | } |
4065 | 0 | }); |
4066 | 0 | ptls_buffer_push_block(buf, 1, |
4067 | 0 | { ptls_buffer_pushv(buf, outer_ch->legacy_session_id.base, outer_ch->legacy_session_id.len); }); |
4068 | | |
4069 | | /* cipher-suites and legacy-compression-methods */ |
4070 | 0 | COPY_BLOCK(2); |
4071 | 0 | COPY_BLOCK(1); |
4072 | | |
4073 | | /* extensions */ |
4074 | 0 | if ((ret = rebuild_ch_inner_extensions(buf, &src, end, outer_ext, outer_ext_end)) != 0) |
4075 | 0 | goto Exit; |
4076 | 0 | }); |
4077 | | |
4078 | | /* padding must be all zero */ |
4079 | 0 | for (; src != end; ++src) { |
4080 | 0 | if (*src != '\0') { |
4081 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4082 | 0 | goto Exit; |
4083 | 0 | } |
4084 | 0 | } |
4085 | | |
4086 | 0 | Exit: |
4087 | 0 | return ret; |
4088 | |
|
4089 | 0 | #undef COPY_BLOCK |
4090 | 0 | } |
4091 | | |
4092 | | /* Wrapper function for invoking the on_client_hello callback, taking an exhaustive list of parameters as arguments. The intention |
4093 | | * is to not miss setting them as we add new parameters to the struct. */ |
4094 | | static inline int call_on_client_hello_cb(ptls_t *tls, ptls_iovec_t server_name, ptls_iovec_t raw_message, |
4095 | | ptls_iovec_t cipher_suites, ptls_iovec_t *alpns, size_t num_alpns, |
4096 | | const uint16_t *sig_algos, size_t num_sig_algos, const uint16_t *cert_comp_algos, |
4097 | | size_t num_cert_comp_algos, const uint8_t *server_cert_types, |
4098 | | size_t num_server_cert_types, const ptls_client_hello_psk_identity_t *psk_identities, |
4099 | | size_t num_psk_identities, int incompatible_version) |
4100 | 825 | { |
4101 | 825 | if (tls->ctx->on_client_hello == NULL) |
4102 | 825 | return 0; |
4103 | | |
4104 | 0 | ptls_on_client_hello_parameters_t params = {server_name, |
4105 | 0 | raw_message, |
4106 | 0 | cipher_suites, |
4107 | 0 | {alpns, num_alpns}, |
4108 | 0 | {sig_algos, num_sig_algos}, |
4109 | 0 | {cert_comp_algos, num_cert_comp_algos}, |
4110 | 0 | {server_cert_types, num_server_cert_types}, |
4111 | 0 | {psk_identities, num_psk_identities}, |
4112 | 0 | incompatible_version}; |
4113 | 0 | return tls->ctx->on_client_hello->cb(tls->ctx->on_client_hello, tls, ¶ms); |
4114 | 825 | } |
4115 | | |
4116 | | static int check_client_hello_constraints(ptls_context_t *ctx, struct st_ptls_client_hello_t *ch, const void *prev_random, |
4117 | | int ech_is_inner_ch, ptls_iovec_t raw_message, ptls_t *tls_cbarg) |
4118 | 896 | { |
4119 | 896 | int is_second_flight = prev_random != 0; |
4120 | | |
4121 | | /* The following check is necessary so that we would be able to track the connection in SSLKEYLOGFILE, even though it might not |
4122 | | * be for the safety of the protocol. */ |
4123 | 896 | if (is_second_flight && !ptls_mem_equal(ch->random_bytes, prev_random, PTLS_HELLO_RANDOM_SIZE)) |
4124 | 14 | return PTLS_ALERT_HANDSHAKE_FAILURE; |
4125 | | |
4126 | | /* bail out if CH cannot be handled as TLS 1.3 */ |
4127 | 882 | if (!is_supported_version(ch->selected_version)) { |
4128 | | /* ECH: server MUST abort with an "illegal_parameter" alert if the client offers TLS 1.2 or below (draft-15 7.1) */ |
4129 | 116 | if (ech_is_inner_ch) |
4130 | 0 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4131 | | /* fail with PROTOCOL_VERSION alert, after providing the applications the raw CH and SNI to help them fallback */ |
4132 | 116 | if (!is_second_flight) { |
4133 | 115 | int ret; |
4134 | 115 | if ((ret = call_on_client_hello_cb(tls_cbarg, ch->server_name, raw_message, ch->cipher_suites, ch->alpn.list, |
4135 | 115 | ch->alpn.count, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 1)) != 0) |
4136 | 0 | return ret; |
4137 | 115 | } |
4138 | 116 | return PTLS_ALERT_PROTOCOL_VERSION; |
4139 | 116 | } |
4140 | | |
4141 | | /* Check TLS 1.3-specific constraints. Hereafter, we might exit without calling on_client_hello. That's fine because this CH is |
4142 | | * ought to be rejected. */ |
4143 | 766 | if (ch->legacy_version <= 0x0300) { |
4144 | | /* RFC 8446 Appendix D.5: any endpoint receiving a Hello message with legacy_version set to 0x0300 MUST abort the handshake |
4145 | | * with a "protocol_version" alert. */ |
4146 | 0 | return PTLS_ALERT_PROTOCOL_VERSION; |
4147 | 0 | } |
4148 | 766 | if (!(ch->compression_methods.count == 1 && ch->compression_methods.ids[0] == 0)) |
4149 | 22 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4150 | | /* pre-shared key */ |
4151 | 744 | if (ch->psk.hash_end != NULL) { |
4152 | | /* PSK must be the last extension */ |
4153 | 199 | if (!ch->psk.is_last_extension) |
4154 | 1 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4155 | 545 | } else { |
4156 | 545 | if (ch->psk.early_data_indication) |
4157 | 1 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4158 | 545 | } |
4159 | | |
4160 | 742 | if (ech_is_inner_ch && ch->ech.payload.base == NULL) |
4161 | 0 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4162 | 742 | if (ch->ech.payload.base != NULL && |
4163 | 16 | ch->ech.type != (ech_is_inner_ch ? PTLS_ECH_CLIENT_HELLO_TYPE_INNER : PTLS_ECH_CLIENT_HELLO_TYPE_OUTER)) |
4164 | 4 | return PTLS_ALERT_ILLEGAL_PARAMETER; |
4165 | | |
4166 | 738 | return 0; |
4167 | 742 | } |
4168 | | |
4169 | | static int vec_is_string(ptls_iovec_t x, const char *y) |
4170 | 0 | { |
4171 | 0 | return strncmp((const char *)x.base, y, x.len) == 0 && y[x.len] == '\0'; |
4172 | 0 | } |
4173 | | |
4174 | | /** |
4175 | | * Looks for a PSK identity that can be used, and if found, updates the handshake state and returns the necessary variables. If |
4176 | | * `ptls_context_t::pre_shared_key` is set, only tries handshake using those keys provided. Otherwise, tries resumption. |
4177 | | */ |
4178 | | static int try_psk_handshake(ptls_t *tls, size_t *psk_index, int *accept_early_data, struct st_ptls_client_hello_t *ch, |
4179 | | ptls_iovec_t ch_trunc, int is_second_flight) |
4180 | 116 | { |
4181 | 116 | ptls_buffer_t decbuf; |
4182 | 116 | ptls_iovec_t secret, ticket_ctx, ticket_negotiated_protocol; |
4183 | 116 | uint64_t issue_at, now = tls->ctx->get_time->cb(tls->ctx->get_time); |
4184 | 116 | uint32_t age_add; |
4185 | 116 | uint16_t ticket_key_exchange_id, ticket_csid; |
4186 | 116 | uint8_t binder_key[PTLS_MAX_DIGEST_SIZE]; |
4187 | 116 | int ret; |
4188 | | |
4189 | 116 | ptls_buffer_init(&decbuf, "", 0); |
4190 | | |
4191 | 287 | for (*psk_index = 0; *psk_index < ch->psk.identities.count; ++*psk_index) { |
4192 | 172 | ptls_client_hello_psk_identity_t *identity = ch->psk.identities.list + *psk_index; |
4193 | | |
4194 | | /* negotiate using fixed pre-shared key */ |
4195 | 172 | if (tls->ctx->pre_shared_key.identity.base != NULL) { |
4196 | 0 | if (identity->identity.len == tls->ctx->pre_shared_key.identity.len && |
4197 | 0 | memcmp(identity->identity.base, tls->ctx->pre_shared_key.identity.base, identity->identity.len) == 0) { |
4198 | 0 | *accept_early_data = ch->psk.early_data_indication && *psk_index == 0; |
4199 | 0 | tls->key_share = NULL; |
4200 | 0 | secret = tls->ctx->pre_shared_key.secret; |
4201 | 0 | goto Found; |
4202 | 0 | } |
4203 | 0 | continue; |
4204 | 0 | } |
4205 | | |
4206 | | /* decrypt ticket and decode */ |
4207 | 172 | if (tls->ctx->encrypt_ticket == NULL || tls->ctx->key_exchanges == NULL) |
4208 | 0 | continue; |
4209 | 172 | int can_accept_early_data = *psk_index == 0; |
4210 | 172 | decbuf.off = 0; |
4211 | 172 | switch (tls->ctx->encrypt_ticket->cb(tls->ctx->encrypt_ticket, tls, 0, &decbuf, identity->identity)) { |
4212 | 172 | case 0: /* decrypted */ |
4213 | 172 | break; |
4214 | 0 | case PTLS_ERROR_REJECT_EARLY_DATA: /* decrypted, but early data is rejected */ |
4215 | 0 | can_accept_early_data = 0; |
4216 | 0 | break; |
4217 | 0 | default: /* decryption failure */ |
4218 | 0 | continue; |
4219 | 172 | } |
4220 | 172 | if (decode_session_identifier(&issue_at, &secret, &age_add, &ticket_ctx, &ticket_key_exchange_id, &ticket_csid, |
4221 | 172 | &ticket_negotiated_protocol, decbuf.base, decbuf.base + decbuf.off) != 0) |
4222 | 0 | continue; |
4223 | | /* check age */ |
4224 | 172 | if (now < issue_at) |
4225 | 0 | continue; |
4226 | 172 | if (now - issue_at > (uint64_t)tls->ctx->ticket_lifetime * 1000) |
4227 | 0 | continue; |
4228 | 172 | *accept_early_data = 0; |
4229 | 172 | if (ch->psk.early_data_indication && can_accept_early_data) { |
4230 | | /* accept early-data if abs(diff) between the reported age and the actual age is within += 10 seconds */ |
4231 | 102 | int64_t delta = (now - issue_at) - (identity->obfuscated_ticket_age - age_add); |
4232 | 102 | if (delta < 0) |
4233 | 0 | delta = -delta; |
4234 | 102 | if (tls->ctx->max_early_data_size != 0 && delta <= PTLS_EARLY_DATA_MAX_DELAY) |
4235 | 0 | *accept_early_data = 1; |
4236 | 102 | } |
4237 | | /* check ticket context */ |
4238 | 172 | if (tls->ctx->ticket_context.is_set) { |
4239 | 0 | if (!(ticket_ctx.len == sizeof(tls->ctx->ticket_context.bytes) && |
4240 | 0 | memcmp(ticket_ctx.base, tls->ctx->ticket_context.bytes, ticket_ctx.len) == 0)) |
4241 | 0 | continue; |
4242 | 172 | } else { |
4243 | | /* check server-name */ |
4244 | 172 | if (ticket_ctx.len != 0) { |
4245 | 0 | if (tls->server_name == NULL) |
4246 | 0 | continue; |
4247 | 0 | if (!vec_is_string(ticket_ctx, tls->server_name)) |
4248 | 0 | continue; |
4249 | 172 | } else { |
4250 | 172 | if (tls->server_name != NULL) |
4251 | 0 | continue; |
4252 | 172 | } |
4253 | 172 | } |
4254 | 172 | { /* check key-exchange */ |
4255 | 172 | ptls_key_exchange_algorithm_t **a; |
4256 | 172 | for (a = tls->ctx->key_exchanges; *a != NULL && (*a)->id != ticket_key_exchange_id; ++a) |
4257 | 0 | ; |
4258 | 172 | if (*a == NULL) |
4259 | 0 | continue; |
4260 | 172 | tls->key_share = *a; |
4261 | 172 | } |
4262 | | /* check cipher-suite */ |
4263 | 172 | if (ticket_csid != tls->cipher_suite->id) |
4264 | 82 | continue; |
4265 | | /* check negotiated-protocol */ |
4266 | 90 | if (ticket_negotiated_protocol.len != 0) { |
4267 | 0 | if (tls->negotiated_protocol == NULL) |
4268 | 0 | continue; |
4269 | 0 | if (!vec_is_string(ticket_negotiated_protocol, tls->negotiated_protocol)) |
4270 | 0 | continue; |
4271 | 0 | } |
4272 | | /* check the length of the decrypted psk and the PSK binder */ |
4273 | 90 | if (secret.len != tls->key_schedule->hashes[0].algo->digest_size) |
4274 | 0 | continue; |
4275 | 90 | if (ch->psk.identities.list[*psk_index].binder.len != tls->key_schedule->hashes[0].algo->digest_size) |
4276 | 89 | continue; |
4277 | | |
4278 | | /* found */ |
4279 | 1 | goto Found; |
4280 | 90 | } |
4281 | | |
4282 | | /* not found */ |
4283 | 115 | *psk_index = SIZE_MAX; |
4284 | 115 | *accept_early_data = 0; |
4285 | 115 | tls->key_share = NULL; |
4286 | 115 | ret = 0; |
4287 | 115 | goto Exit; |
4288 | | |
4289 | 1 | Found: |
4290 | 1 | if (!is_second_flight && (ret = key_schedule_extract(tls->key_schedule, secret)) != 0) |
4291 | 0 | goto Exit; |
4292 | 1 | if ((ret = derive_secret_with_empty_digest(tls->key_schedule, binder_key, |
4293 | 1 | tls->ctx->pre_shared_key.secret.base != NULL ? "ext binder" : "res binder")) != 0) |
4294 | 0 | goto Exit; |
4295 | 1 | ptls__key_schedule_update_hash(tls->key_schedule, ch_trunc.base, ch_trunc.len, 0); |
4296 | 1 | if ((ret = calc_verify_data(binder_key /* to conserve space, reuse binder_key for storing verify_data */, tls->key_schedule, |
4297 | 1 | binder_key)) != 0) |
4298 | 0 | goto Exit; |
4299 | 1 | if (!ptls_mem_equal(ch->psk.identities.list[*psk_index].binder.base, binder_key, |
4300 | 1 | tls->key_schedule->hashes[0].algo->digest_size)) { |
4301 | 1 | ret = PTLS_ALERT_DECRYPT_ERROR; |
4302 | 1 | goto Exit; |
4303 | 1 | } |
4304 | 0 | ret = 0; |
4305 | |
|
4306 | 116 | Exit: |
4307 | 116 | ptls_buffer_dispose(&decbuf); |
4308 | 116 | ptls_clear_memory(binder_key, sizeof(binder_key)); |
4309 | 116 | return ret; |
4310 | 0 | } |
4311 | | |
4312 | | static int calc_cookie_signature(ptls_t *tls, ptls_handshake_properties_t *properties, |
4313 | | ptls_key_exchange_algorithm_t *negotiated_group, ptls_iovec_t tbs, uint8_t *sig) |
4314 | 0 | { |
4315 | 0 | ptls_hash_algorithm_t *algo = tls->ctx->cipher_suites[0]->hash; |
4316 | 0 | ptls_hash_context_t *hctx; |
4317 | |
|
4318 | 0 | if ((hctx = ptls_hmac_create(algo, properties->server.cookie.key, algo->digest_size)) == NULL) |
4319 | 0 | return PTLS_ERROR_NO_MEMORY; |
4320 | | |
4321 | 0 | #define UPDATE_BLOCK(p, _len) \ |
4322 | 0 | do { \ |
4323 | 0 | size_t len = (_len); \ |
4324 | 0 | assert(len < UINT8_MAX); \ |
4325 | 0 | uint8_t len8 = (uint8_t)len; \ |
4326 | 0 | hctx->update(hctx, &len8, 1); \ |
4327 | 0 | hctx->update(hctx, (p), len); \ |
4328 | 0 | } while (0) |
4329 | 0 | #define UPDATE16(_v) \ |
4330 | 0 | do { \ |
4331 | 0 | uint16_t v = (_v); \ |
4332 | 0 | uint8_t b[2] = {v >> 8, v & 0xff}; \ |
4333 | 0 | hctx->update(hctx, b, 2); \ |
4334 | 0 | } while (0) |
4335 | | |
4336 | 0 | UPDATE_BLOCK(tls->client_random, sizeof(tls->client_random)); |
4337 | 0 | UPDATE_BLOCK(tls->server_name, tls->server_name != NULL ? strlen(tls->server_name) : 0); |
4338 | 0 | UPDATE16(tls->cipher_suite->id); |
4339 | 0 | UPDATE16(negotiated_group != NULL ? negotiated_group->id : 0); |
4340 | 0 | UPDATE_BLOCK(properties->server.cookie.additional_data.base, properties->server.cookie.additional_data.len); |
4341 | | |
4342 | 0 | UPDATE_BLOCK(tbs.base, tbs.len); |
4343 | | |
4344 | 0 | #undef UPDATE_BLOCK |
4345 | 0 | #undef UPDATE16 |
4346 | | |
4347 | 0 | hctx->final(hctx, sig, PTLS_HASH_FINAL_MODE_FREE); |
4348 | 0 | return 0; |
4349 | 0 | } |
4350 | | |
4351 | | static int certificate_type_exists(uint8_t *list, size_t count, uint8_t desired_type) |
4352 | 710 | { |
4353 | | /* empty type list means that we default to x509 */ |
4354 | 710 | if (desired_type == PTLS_CERTIFICATE_TYPE_X509 && count == 0) |
4355 | 688 | return 1; |
4356 | 62 | for (size_t i = 0; i < count; i++) { |
4357 | 51 | if (list[i] == desired_type) |
4358 | 11 | return 1; |
4359 | 51 | } |
4360 | 11 | return 0; |
4361 | 22 | } |
4362 | | |
4363 | | static int server_handle_hello(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message, |
4364 | | ptls_handshake_properties_t *properties) |
4365 | 2.01k | { |
4366 | 2.01k | #define EMIT_SERVER_HELLO(sched, fill_rand, extensions, post_action) \ |
4367 | 2.01k | do { \ |
4368 | 410 | size_t sh_start_off; \ |
4369 | 410 | ptls_push_message(emitter, NULL, PTLS_HANDSHAKE_TYPE_SERVER_HELLO, { \ |
4370 | 410 | sh_start_off = emitter->buf->off - PTLS_HANDSHAKE_HEADER_SIZE; \ |
4371 | 410 | ptls_buffer_push16(emitter->buf, 0x0303 /* legacy version */); \ |
4372 | 410 | if ((ret = ptls_buffer_reserve(emitter->buf, PTLS_HELLO_RANDOM_SIZE)) != 0) \ |
4373 | 410 | goto Exit; \ |
4374 | 410 | do { \ |
4375 | 410 | fill_rand \ |
4376 | 410 | } while (0); \ |
4377 | 410 | emitter->buf->off += PTLS_HELLO_RANDOM_SIZE; \ |
4378 | 410 | ptls_buffer_push_block(emitter->buf, 1, \ |
4379 | 410 | { ptls_buffer_pushv(emitter->buf, ch->legacy_session_id.base, ch->legacy_session_id.len); }); \ |
4380 | 410 | ptls_buffer_push16(emitter->buf, tls->cipher_suite->id); \ |
4381 | 410 | ptls_buffer_push(emitter->buf, 0); \ |
4382 | 410 | ptls_buffer_push_block(emitter->buf, 2, { \ |
4383 | 410 | buffer_push_extension(emitter->buf, PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS, \ |
4384 | 410 | { ptls_buffer_push16(emitter->buf, ch->selected_version); }); \ |
4385 | 410 | do { \ |
4386 | 410 | extensions \ |
4387 | 410 | } while (0); \ |
4388 | 410 | }); \ |
4389 | 410 | }); \ |
4390 | 410 | do { \ |
4391 | 820 | post_action \ |
4392 | 410 | } while (0); \ |
4393 | 410 | ptls__key_schedule_update_hash((sched), emitter->buf->base + sh_start_off, emitter->buf->off - sh_start_off, 0); \ |
4394 | 410 | } while (0) |
4395 | | |
4396 | 2.01k | #define EMIT_HELLO_RETRY_REQUEST(sched, negotiated_group, additional_extensions, post_action) \ |
4397 | 2.01k | EMIT_SERVER_HELLO((sched), { memcpy(emitter->buf->base + emitter->buf->off, hello_retry_random, PTLS_HELLO_RANDOM_SIZE); }, \ |
4398 | 154 | { \ |
4399 | 154 | ptls_key_exchange_algorithm_t *_negotiated_group = (negotiated_group); \ |
4400 | 154 | if (_negotiated_group != NULL) { \ |
4401 | 154 | buffer_push_extension(emitter->buf, PTLS_EXTENSION_TYPE_KEY_SHARE, \ |
4402 | 154 | { ptls_buffer_push16(emitter->buf, _negotiated_group->id); }); \ |
4403 | 154 | } \ |
4404 | 154 | do { \ |
4405 | 154 | additional_extensions \ |
4406 | 154 | } while (0); \ |
4407 | 154 | }, \ |
4408 | 154 | post_action) |
4409 | 2.01k | struct st_ptls_client_hello_t *ch; |
4410 | 2.01k | struct { |
4411 | 2.01k | ptls_key_exchange_algorithm_t *algorithm; |
4412 | 2.01k | ptls_iovec_t peer_key; |
4413 | 2.01k | } key_share = {NULL}; |
4414 | 2.01k | struct { |
4415 | 2.01k | uint8_t *encoded_ch_inner; |
4416 | 2.01k | uint8_t *ch_outer_aad; |
4417 | 2.01k | ptls_buffer_t ch_inner; |
4418 | 2.01k | } ech = {NULL}; |
4419 | 2.01k | enum { HANDSHAKE_MODE_FULL, HANDSHAKE_MODE_PSK, HANDSHAKE_MODE_PSK_DHE } mode; |
4420 | 2.01k | size_t psk_index = SIZE_MAX; |
4421 | 2.01k | ptls_iovec_t pubkey = {0}, ecdh_secret = {0}; |
4422 | 2.01k | int accept_early_data = 0, is_second_flight = tls->state == PTLS_STATE_SERVER_EXPECT_SECOND_CLIENT_HELLO, ret; |
4423 | | |
4424 | 2.01k | ptls_buffer_init(&ech.ch_inner, "", 0); |
4425 | | |
4426 | 2.01k | if ((ch = malloc(sizeof(*ch))) == NULL) { |
4427 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
4428 | 0 | goto Exit; |
4429 | 0 | } |
4430 | | |
4431 | 2.01k | *ch = (struct st_ptls_client_hello_t){.unknown_extensions = {{UINT16_MAX}}}; |
4432 | | |
4433 | | /* decode ClientHello */ |
4434 | 2.01k | if ((ret = decode_client_hello(tls->ctx, ch, message.base + PTLS_HANDSHAKE_HEADER_SIZE, message.base + message.len, properties, |
4435 | 2.01k | tls)) != 0) |
4436 | 1.11k | goto Exit; |
4437 | 896 | if ((ret = check_client_hello_constraints(tls->ctx, ch, is_second_flight ? tls->client_random : NULL, 0, message, tls)) != 0) |
4438 | 158 | goto Exit; |
4439 | 738 | if (!is_second_flight) { |
4440 | 710 | memcpy(tls->client_random, ch->random_bytes, PTLS_HELLO_RANDOM_SIZE); |
4441 | 710 | log_client_random(tls); |
4442 | 710 | } else { |
4443 | | /* consistency check for ECH extension in response to HRR */ |
4444 | 28 | if (tls->ech.aead != NULL) { |
4445 | 0 | if (ch->ech.payload.base == NULL) { |
4446 | 0 | ret = PTLS_ALERT_MISSING_EXTENSION; |
4447 | 0 | goto Exit; |
4448 | 0 | } |
4449 | 0 | if (!(ch->ech.config_id == tls->ech.config_id && ch->ech.cipher_suite.kdf == tls->ech.cipher->id.kdf && |
4450 | 0 | ch->ech.cipher_suite.aead == tls->ech.cipher->id.aead && ch->ech.enc.len == 0)) { |
4451 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4452 | 0 | goto Exit; |
4453 | 0 | } |
4454 | 0 | } |
4455 | 28 | } |
4456 | | |
4457 | | /* ECH */ |
4458 | 738 | if (ch->ech.payload.base != NULL) { |
4459 | 12 | if (ch->ech.type != PTLS_ECH_CLIENT_HELLO_TYPE_OUTER) { |
4460 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4461 | 0 | goto Exit; |
4462 | 0 | } |
4463 | 12 | if (!is_second_flight) |
4464 | 10 | tls->ech.state = PTLS_ECH_STATE_OFFERED; |
4465 | | /* obtain AEAD context for opening inner CH */ |
4466 | 12 | if (!is_second_flight && ch->ech.payload.base != NULL && tls->ctx->ech.server.create_opener != NULL) { |
4467 | 0 | if ((tls->ech.aead = tls->ctx->ech.server.create_opener->cb( |
4468 | 0 | tls->ctx->ech.server.create_opener, &tls->ech.kem, &tls->ech.cipher, tls, ch->ech.config_id, |
4469 | 0 | ch->ech.cipher_suite, ch->ech.enc, ptls_iovec_init(ech_info_prefix, sizeof(ech_info_prefix)))) != NULL) |
4470 | 0 | tls->ech.config_id = ch->ech.config_id; |
4471 | 0 | } |
4472 | 12 | if (!is_second_flight) { |
4473 | 10 | PTLS_PROBE(ECH_SELECTION, tls, tls->ech.aead != NULL); |
4474 | 10 | PTLS_LOG_CONN(ech_selection, tls, { PTLS_LOG_ELEMENT_BOOL(is_ech, tls->ech.aead != NULL); }); |
4475 | 10 | } |
4476 | 12 | if (tls->ech.aead != NULL) { |
4477 | | /* now that AEAD context is available, create AAD and decrypt inner CH */ |
4478 | 0 | if (ch->ech.payload.len <= tls->ech.aead->algo->tag_size) { |
4479 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
4480 | 0 | goto Exit; |
4481 | 0 | } |
4482 | 0 | if ((ech.encoded_ch_inner = malloc(ch->ech.payload.len - tls->ech.aead->algo->tag_size)) == NULL || |
4483 | 0 | (ech.ch_outer_aad = malloc(message.len - PTLS_HANDSHAKE_HEADER_SIZE)) == NULL) { |
4484 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
4485 | 0 | goto Exit; |
4486 | 0 | } |
4487 | 0 | memcpy(ech.ch_outer_aad, message.base + PTLS_HANDSHAKE_HEADER_SIZE, message.len - PTLS_HANDSHAKE_HEADER_SIZE); |
4488 | 0 | memset(ech.ch_outer_aad + (ch->ech.payload.base - (message.base + PTLS_HANDSHAKE_HEADER_SIZE)), 0, ch->ech.payload.len); |
4489 | 0 | if (ptls_aead_decrypt(tls->ech.aead, ech.encoded_ch_inner, ch->ech.payload.base, ch->ech.payload.len, is_second_flight, |
4490 | 0 | ech.ch_outer_aad, message.len - PTLS_HANDSHAKE_HEADER_SIZE) != SIZE_MAX) { |
4491 | 0 | tls->ech.state = PTLS_ECH_STATE_ACCEPTED; |
4492 | | /* successfully decrypted EncodedCHInner, build CHInner */ |
4493 | 0 | if ((ret = rebuild_ch_inner(&ech.ch_inner, ech.encoded_ch_inner, |
4494 | 0 | ech.encoded_ch_inner + ch->ech.payload.len - tls->ech.aead->algo->tag_size, ch, |
4495 | 0 | message.base + PTLS_HANDSHAKE_HEADER_SIZE + ch->first_extension_at, |
4496 | 0 | message.base + message.len)) != 0) |
4497 | 0 | goto Exit; |
4498 | | /* treat inner ch as the message being received, re-decode it */ |
4499 | 0 | message = ptls_iovec_init(ech.ch_inner.base, ech.ch_inner.off); |
4500 | 0 | *ch = (struct st_ptls_client_hello_t){.unknown_extensions = {{UINT16_MAX}}}; |
4501 | 0 | if ((ret = decode_client_hello(tls->ctx, ch, ech.ch_inner.base + PTLS_HANDSHAKE_HEADER_SIZE, |
4502 | 0 | ech.ch_inner.base + ech.ch_inner.off, properties, tls)) != 0) |
4503 | 0 | goto Exit; |
4504 | 0 | if ((ret = check_client_hello_constraints(tls->ctx, ch, is_second_flight ? tls->ech.inner_client_random : NULL, 1, |
4505 | 0 | message, tls)) != 0) |
4506 | 0 | goto Exit; |
4507 | 0 | if (!is_second_flight) |
4508 | 0 | memcpy(tls->ech.inner_client_random, ch->random_bytes, PTLS_HELLO_RANDOM_SIZE); |
4509 | 0 | } else if (is_second_flight) { |
4510 | | /* decryption failure of inner CH in 2nd CH is fatal */ |
4511 | 0 | ret = PTLS_ALERT_DECRYPT_ERROR; |
4512 | 0 | goto Exit; |
4513 | 0 | } else { |
4514 | | /* decryption failure of 1st CH indicates key mismatch; dispose of AEAD context to indicate adoption of outerCH */ |
4515 | 0 | ptls_aead_free(tls->ech.aead); |
4516 | 0 | tls->ech.aead = NULL; |
4517 | 0 | } |
4518 | 0 | } |
4519 | 726 | } else if (tls->ech.state != PTLS_ECH_STATE_NONE) { |
4520 | 1 | assert(is_second_flight); |
4521 | 1 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
4522 | 1 | goto Exit; |
4523 | 1 | } |
4524 | | |
4525 | 737 | if (tls->ctx->require_dhe_on_psk) |
4526 | 0 | ch->psk.ke_modes &= ~(1u << PTLS_PSK_KE_MODE_PSK); |
4527 | | |
4528 | | /* handle client_random, legacy_session_id, SNI, ESNI */ |
4529 | 737 | if (!is_second_flight) { |
4530 | 710 | if (ch->legacy_session_id.len != 0) |
4531 | 270 | tls->send_change_cipher_spec = 1; |
4532 | 710 | ptls_iovec_t server_name = {NULL}; |
4533 | 710 | if (ch->server_name.base != NULL) |
4534 | 4 | server_name = ch->server_name; |
4535 | 710 | if ((ret = call_on_client_hello_cb(tls, server_name, message, ch->cipher_suites, ch->alpn.list, ch->alpn.count, |
4536 | 710 | ch->signature_algorithms.list, ch->signature_algorithms.count, |
4537 | 710 | ch->cert_compression_algos.list, ch->cert_compression_algos.count, |
4538 | 710 | ch->server_certificate_types.list, ch->server_certificate_types.count, |
4539 | 710 | ch->psk.identities.list, ch->psk.identities.count, 0)) != 0) |
4540 | 0 | goto Exit; |
4541 | 710 | if (!certificate_type_exists(ch->server_certificate_types.list, ch->server_certificate_types.count, |
4542 | 710 | tls->ctx->use_raw_public_keys ? PTLS_CERTIFICATE_TYPE_RAW_PUBLIC_KEY |
4543 | 710 | : PTLS_CERTIFICATE_TYPE_X509)) { |
4544 | 11 | ret = PTLS_ALERT_UNSUPPORTED_CERTIFICATE; |
4545 | 11 | goto Exit; |
4546 | 11 | } |
4547 | 710 | } else { |
4548 | 27 | if (ch->psk.early_data_indication) { |
4549 | 1 | ret = PTLS_ALERT_DECODE_ERROR; |
4550 | 1 | goto Exit; |
4551 | 1 | } |
4552 | | /* We compare SNI only when the value is saved by the on_client_hello callback. This should be OK because we are |
4553 | | * ignoring the value unless the callback saves the server-name. */ |
4554 | 26 | if (tls->server_name != NULL) { |
4555 | 0 | size_t l = strlen(tls->server_name); |
4556 | 0 | if (!(ch->server_name.len == l && memcmp(ch->server_name.base, tls->server_name, l) == 0)) { |
4557 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
4558 | 0 | goto Exit; |
4559 | 0 | } |
4560 | 0 | } |
4561 | 26 | } |
4562 | | |
4563 | 725 | { /* select (or check) cipher-suite, create key_schedule */ |
4564 | 725 | ptls_cipher_suite_t *cs; |
4565 | 725 | if ((ret = select_cipher(&cs, tls->ctx->cipher_suites, ch->cipher_suites.base, |
4566 | 725 | ch->cipher_suites.base + ch->cipher_suites.len, tls->ctx->server_cipher_preference, |
4567 | 725 | tls->ctx->server_cipher_chacha_priority, tls->ctx->pre_shared_key.hash)) != 0) |
4568 | 29 | goto Exit; |
4569 | 696 | if (!is_second_flight) { |
4570 | 671 | tls->cipher_suite = cs; |
4571 | 671 | if ((tls->key_schedule = key_schedule_new(cs, NULL, 0)) == NULL) { |
4572 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
4573 | 0 | goto Exit; |
4574 | 0 | } |
4575 | 671 | } else { |
4576 | 25 | if (tls->cipher_suite != cs) { |
4577 | 2 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
4578 | 2 | goto Exit; |
4579 | 2 | } |
4580 | 25 | } |
4581 | 696 | } |
4582 | | |
4583 | | /* select key_share */ |
4584 | 694 | if (key_share.algorithm == NULL && ch->key_shares.base != NULL && tls->ctx->key_exchanges != NULL) { |
4585 | 574 | const uint8_t *src = ch->key_shares.base, *const end = src + ch->key_shares.len; |
4586 | 574 | ptls_decode_block(src, end, 2, { |
4587 | 574 | if ((ret = select_key_share(&key_share.algorithm, &key_share.peer_key, tls->ctx->key_exchanges, &src, end, 0)) != 0) |
4588 | 574 | goto Exit; |
4589 | 574 | }); |
4590 | 574 | } |
4591 | | |
4592 | 605 | if (!is_second_flight) { |
4593 | 582 | if (ch->cookie.all.len != 0 && key_share.algorithm != NULL) { |
4594 | |
|
4595 | 0 | { /* use cookie to check the integrity of the handshake, and update the context */ |
4596 | 0 | uint8_t sig[PTLS_MAX_DIGEST_SIZE]; |
4597 | 0 | size_t sigsize = tls->ctx->cipher_suites[0]->hash->digest_size; |
4598 | 0 | if ((ret = calc_cookie_signature(tls, properties, key_share.algorithm, ch->cookie.tbs, sig)) != 0) |
4599 | 0 | goto Exit; |
4600 | 0 | if (!(ch->cookie.signature.len == sigsize && ptls_mem_equal(ch->cookie.signature.base, sig, sigsize))) { |
4601 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
4602 | 0 | goto Exit; |
4603 | 0 | } |
4604 | 0 | } |
4605 | | /* integrity check passed; update states */ |
4606 | 0 | key_schedule_update_ch1hash_prefix(tls->key_schedule); |
4607 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, ch->cookie.ch1_hash.base, ch->cookie.ch1_hash.len, 0); |
4608 | 0 | key_schedule_extract(tls->key_schedule, |
4609 | 0 | tls->ctx->pre_shared_key.secret /* this argument will be a zero-length vector unless external PSK |
4610 | 0 | is used, and that's fine; we never resume when sending HRR */); |
4611 | | /* ... reusing sendbuf to rebuild HRR for hash calculation */ |
4612 | 0 | size_t hrr_start = emitter->buf->off; |
4613 | 0 | EMIT_HELLO_RETRY_REQUEST(tls->key_schedule, ch->cookie.sent_key_share ? key_share.algorithm : NULL, |
4614 | 0 | { |
4615 | 0 | buffer_push_extension(emitter->buf, PTLS_EXTENSION_TYPE_COOKIE, { |
4616 | 0 | ptls_buffer_pushv(emitter->buf, ch->cookie.all.base, ch->cookie.all.len); |
4617 | 0 | }); |
4618 | 0 | }, |
4619 | 0 | {}); |
4620 | 0 | emitter->buf->off = hrr_start; |
4621 | 0 | is_second_flight = 1; |
4622 | |
|
4623 | 582 | } else if (ch->key_shares.base != NULL && tls->ctx->key_exchanges != NULL && |
4624 | 463 | (key_share.algorithm == NULL || (properties != NULL && properties->server.enforce_retry))) { |
4625 | | /* send HelloRetryRequest, when trying to negotiate the key share but enforced by config or upon key-share mismatch */ |
4626 | 221 | if (ch->negotiated_groups.base == NULL) { |
4627 | 3 | ret = PTLS_ALERT_MISSING_EXTENSION; |
4628 | 3 | goto Exit; |
4629 | 3 | } |
4630 | 218 | ptls_key_exchange_algorithm_t *negotiated_group; |
4631 | 218 | if ((ret = select_negotiated_group(&negotiated_group, tls->ctx->key_exchanges, ch->negotiated_groups.base, |
4632 | 218 | ch->negotiated_groups.base + ch->negotiated_groups.len)) != 0) |
4633 | 64 | goto Exit; |
4634 | 154 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
4635 | 154 | assert(tls->key_schedule->generation == 0); |
4636 | | |
4637 | | /* Either send a stateless retry (w. cookies) or a stateful one. When sending the latter, run the state machine. At the |
4638 | | * moment, stateless retry is disabled when ECH is used (do we need to support it?). */ |
4639 | 154 | int retry_uses_cookie = |
4640 | 154 | properties != NULL && properties->server.retry_uses_cookie && !ptls_is_ech_handshake(tls, NULL, NULL, NULL); |
4641 | 154 | if (!retry_uses_cookie) { |
4642 | 154 | key_schedule_transform_post_ch1hash(tls->key_schedule); |
4643 | 154 | key_schedule_extract(tls->key_schedule, tls->ctx->pre_shared_key.secret /* see comment above */); |
4644 | 154 | } |
4645 | 154 | size_t ech_confirm_off = 0; |
4646 | 154 | EMIT_HELLO_RETRY_REQUEST( |
4647 | 154 | tls->key_schedule, key_share.algorithm != NULL ? NULL : negotiated_group, |
4648 | 154 | { |
4649 | 154 | ptls_buffer_t *sendbuf = emitter->buf; |
4650 | 154 | if (ptls_is_ech_handshake(tls, NULL, NULL, NULL)) { |
4651 | 154 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO, { |
4652 | 154 | if ((ret = ptls_buffer_reserve(sendbuf, PTLS_ECH_CONFIRM_LENGTH)) != 0) |
4653 | 154 | goto Exit; |
4654 | 154 | memset(sendbuf->base + sendbuf->off, 0, PTLS_ECH_CONFIRM_LENGTH); |
4655 | 154 | ech_confirm_off = sendbuf->off; |
4656 | 154 | sendbuf->off += PTLS_ECH_CONFIRM_LENGTH; |
4657 | 154 | }); |
4658 | 154 | } |
4659 | 154 | if (retry_uses_cookie) { |
4660 | 154 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_COOKIE, { |
4661 | 154 | ptls_buffer_push_block(sendbuf, 2, { |
4662 | | /* push to-be-signed data */ |
4663 | 154 | size_t tbs_start = sendbuf->off; |
4664 | 154 | ptls_buffer_push_block(sendbuf, 2, { |
4665 | | /* first block of the cookie data is the hash(ch1) */ |
4666 | 154 | ptls_buffer_push_block(sendbuf, 1, { |
4667 | 154 | size_t sz = tls->cipher_suite->hash->digest_size; |
4668 | 154 | if ((ret = ptls_buffer_reserve(sendbuf, sz)) != 0) |
4669 | 154 | goto Exit; |
4670 | 154 | key_schedule_extract_ch1hash(tls->key_schedule, sendbuf->base + sendbuf->off); |
4671 | 154 | sendbuf->off += sz; |
4672 | 154 | }); |
4673 | | /* second is if we have sent key_share extension */ |
4674 | 154 | ptls_buffer_push(sendbuf, key_share.algorithm == NULL); |
4675 | | /* we can add more data here */ |
4676 | 154 | }); |
4677 | 154 | size_t tbs_len = sendbuf->off - tbs_start; |
4678 | | /* push the signature */ |
4679 | 154 | ptls_buffer_push_block(sendbuf, 1, { |
4680 | 154 | size_t sz = tls->ctx->cipher_suites[0]->hash->digest_size; |
4681 | 154 | if ((ret = ptls_buffer_reserve(sendbuf, sz)) != 0) |
4682 | 154 | goto Exit; |
4683 | 154 | if ((ret = calc_cookie_signature(tls, properties, negotiated_group, |
4684 | 154 | ptls_iovec_init(sendbuf->base + tbs_start, tbs_len), |
4685 | 154 | sendbuf->base + sendbuf->off)) != 0) |
4686 | 154 | goto Exit; |
4687 | 154 | sendbuf->off += sz; |
4688 | 154 | }); |
4689 | 154 | }); |
4690 | 154 | }); |
4691 | 154 | } |
4692 | 154 | }, |
4693 | 154 | { |
4694 | 154 | if (ech_confirm_off != 0 && |
4695 | 154 | (ret = ech_calc_confirmation( |
4696 | 154 | tls->key_schedule, emitter->buf->base + ech_confirm_off, tls->ech.inner_client_random, |
4697 | 154 | ECH_CONFIRMATION_HRR, |
4698 | 154 | ptls_iovec_init(emitter->buf->base + sh_start_off, emitter->buf->off - sh_start_off))) != 0) |
4699 | 154 | goto Exit; |
4700 | 154 | }); |
4701 | 154 | if (retry_uses_cookie) { |
4702 | 0 | if ((ret = push_change_cipher_spec(tls, emitter)) != 0) |
4703 | 0 | goto Exit; |
4704 | 0 | ret = PTLS_ERROR_STATELESS_RETRY; |
4705 | 154 | } else { |
4706 | 154 | tls->state = PTLS_STATE_SERVER_EXPECT_SECOND_CLIENT_HELLO; |
4707 | 154 | if (ch->psk.early_data_indication) |
4708 | 75 | tls->server.early_data_skipped_bytes = 0; |
4709 | 154 | ret = PTLS_ERROR_IN_PROGRESS; |
4710 | 154 | } |
4711 | 154 | goto Exit; |
4712 | 154 | } |
4713 | 582 | } |
4714 | | |
4715 | | /* handle unknown extensions */ |
4716 | 384 | if ((ret = report_unknown_extensions(tls, properties, ch->unknown_extensions)) != 0) |
4717 | 0 | goto Exit; |
4718 | | |
4719 | | /* try psk handshake */ |
4720 | 384 | if (ch->psk.hash_end != 0 && (ch->psk.ke_modes & ((1u << PTLS_PSK_KE_MODE_PSK) | (1u << PTLS_PSK_KE_MODE_PSK_DHE))) != 0 && |
4721 | 117 | !tls->ctx->require_client_authentication && |
4722 | 117 | ((!is_second_flight && tls->ctx->encrypt_ticket != NULL) || tls->ctx->pre_shared_key.identity.base != NULL)) { |
4723 | 116 | if ((ret = try_psk_handshake(tls, &psk_index, &accept_early_data, ch, |
4724 | 116 | ptls_iovec_init(message.base, ch->psk.hash_end - message.base), is_second_flight)) != 0) { |
4725 | 1 | goto Exit; |
4726 | 1 | } |
4727 | 116 | } |
4728 | | |
4729 | | /* If the server was setup to use an external PSK but failed to agree, abort the handshake. Because external PSK is a form of |
4730 | | * mutual authentication, it makes sense to abort (at least as the default). */ |
4731 | 383 | if (tls->ctx->pre_shared_key.identity.base != NULL && psk_index == SIZE_MAX) { |
4732 | 0 | ret = PTLS_ALERT_UNKNOWN_PSK_IDENTITY; |
4733 | 0 | goto Exit; |
4734 | 0 | } |
4735 | | |
4736 | | /* If client authentication is enabled, we always force a full handshake. |
4737 | | * TODO: Check for `post_handshake_auth` extension and if that is present, do not force full handshake! |
4738 | | * Remove also the check `!require_client_authentication` above. |
4739 | | * |
4740 | | * adjust key_schedule, determine handshake mode |
4741 | | */ |
4742 | 383 | if (psk_index == SIZE_MAX || tls->ctx->require_client_authentication) { |
4743 | 383 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
4744 | 383 | if (!is_second_flight) { |
4745 | 360 | assert(tls->key_schedule->generation == 0); |
4746 | 360 | key_schedule_extract(tls->key_schedule, ptls_iovec_init(NULL, 0)); |
4747 | 360 | } |
4748 | 383 | mode = HANDSHAKE_MODE_FULL; |
4749 | 383 | if (properties != NULL) |
4750 | 383 | properties->server.selected_psk_binder.len = 0; |
4751 | 383 | } else { |
4752 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, ch->psk.hash_end, message.base + message.len - ch->psk.hash_end, 0); |
4753 | 0 | if ((ch->psk.ke_modes & (1u << PTLS_PSK_KE_MODE_PSK)) != 0) { |
4754 | 0 | mode = HANDSHAKE_MODE_PSK; |
4755 | 0 | } else { |
4756 | 0 | assert((ch->psk.ke_modes & (1u << PTLS_PSK_KE_MODE_PSK_DHE)) != 0); |
4757 | 0 | mode = HANDSHAKE_MODE_PSK_DHE; |
4758 | 0 | } |
4759 | 0 | tls->is_psk_handshake = 1; |
4760 | 0 | if (properties != NULL) { |
4761 | 0 | ptls_iovec_t *selected = &ch->psk.identities.list[psk_index].binder; |
4762 | 0 | memcpy(properties->server.selected_psk_binder.base, selected->base, selected->len); |
4763 | 0 | properties->server.selected_psk_binder.len = selected->len; |
4764 | 0 | } |
4765 | 0 | } |
4766 | | |
4767 | | /* determine number of tickets to send */ |
4768 | 383 | if (ch->psk.ke_modes != 0 && tls->ctx->ticket_lifetime != 0) { |
4769 | 231 | if (ch->ticket_request.new_session_count != 0) { |
4770 | 77 | tls->server.num_tickets_to_send = |
4771 | 77 | tls->is_psk_handshake ? ch->ticket_request.resumption_count : ch->ticket_request.new_session_count; |
4772 | 154 | } else { |
4773 | 154 | tls->server.num_tickets_to_send = 1; |
4774 | 154 | } |
4775 | 231 | uint8_t max_tickets = tls->ctx->ticket_requests.server.max_count; |
4776 | 231 | if (max_tickets == 0) |
4777 | 231 | max_tickets = PTLS_DEFAULT_MAX_TICKETS_TO_SERVE; |
4778 | 231 | if (tls->server.num_tickets_to_send > max_tickets) |
4779 | 50 | tls->server.num_tickets_to_send = max_tickets; |
4780 | 231 | } else { |
4781 | 152 | tls->server.num_tickets_to_send = 0; |
4782 | 152 | } |
4783 | | |
4784 | 383 | if (accept_early_data && tls->ctx->max_early_data_size != 0 && psk_index == 0) { |
4785 | 0 | if ((tls->pending_handshake_secret = malloc(PTLS_MAX_DIGEST_SIZE)) == NULL) { |
4786 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
4787 | 0 | goto Exit; |
4788 | 0 | } |
4789 | 0 | if ((ret = derive_exporter_secret(tls, 1)) != 0) |
4790 | 0 | goto Exit; |
4791 | 0 | if ((ret = setup_traffic_protection(tls, 0, "c e traffic", 1, 0, 0)) != 0) |
4792 | 0 | goto Exit; |
4793 | 0 | } |
4794 | | |
4795 | | /* run key-exchange, to obtain pubkey and secret */ |
4796 | 383 | if (mode != HANDSHAKE_MODE_PSK) { |
4797 | 383 | if (key_share.algorithm == NULL) { |
4798 | 122 | ret = ch->key_shares.base != NULL ? PTLS_ALERT_HANDSHAKE_FAILURE : PTLS_ALERT_MISSING_EXTENSION; |
4799 | 122 | goto Exit; |
4800 | 122 | } |
4801 | 261 | if ((ret = key_share.algorithm->exchange(key_share.algorithm, &pubkey, &ecdh_secret, key_share.peer_key)) != 0) { |
4802 | 5 | assert(pubkey.base == NULL); |
4803 | 5 | assert(ecdh_secret.base == NULL); |
4804 | 5 | goto Exit; |
4805 | 5 | } |
4806 | 256 | tls->key_share = key_share.algorithm; |
4807 | 256 | } |
4808 | | |
4809 | 256 | { /* send ServerHello */ |
4810 | 256 | size_t ech_confirm_off = 0; |
4811 | 256 | EMIT_SERVER_HELLO( |
4812 | 256 | tls->key_schedule, |
4813 | 256 | { |
4814 | 256 | tls->ctx->random_bytes(emitter->buf->base + emitter->buf->off, PTLS_HELLO_RANDOM_SIZE); |
4815 | | /* when accepting CHInner, last 8 byte of SH.random is zero for the handshake transcript */ |
4816 | 256 | if (ptls_is_ech_handshake(tls, NULL, NULL, NULL)) { |
4817 | 256 | ech_confirm_off = emitter->buf->off + PTLS_HELLO_RANDOM_SIZE - PTLS_ECH_CONFIRM_LENGTH; |
4818 | 256 | memset(emitter->buf->base + ech_confirm_off, 0, PTLS_ECH_CONFIRM_LENGTH); |
4819 | 256 | } |
4820 | 256 | }, |
4821 | 256 | { |
4822 | 256 | ptls_buffer_t *sendbuf = emitter->buf; |
4823 | 256 | if (mode != HANDSHAKE_MODE_PSK) { |
4824 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_KEY_SHARE, { |
4825 | 256 | ptls_buffer_push16(sendbuf, key_share.algorithm->id); |
4826 | 256 | ptls_buffer_push_block(sendbuf, 2, { ptls_buffer_pushv(sendbuf, pubkey.base, pubkey.len); }); |
4827 | 256 | }); |
4828 | 256 | } |
4829 | 256 | if (mode != HANDSHAKE_MODE_FULL) { |
4830 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_PRE_SHARED_KEY, |
4831 | 256 | { ptls_buffer_push16(sendbuf, (uint16_t)psk_index); }); |
4832 | 256 | } |
4833 | 256 | }, |
4834 | 256 | { |
4835 | 256 | if (ech_confirm_off != 0 && |
4836 | 256 | (ret = ech_calc_confirmation( |
4837 | 256 | tls->key_schedule, emitter->buf->base + ech_confirm_off, tls->ech.inner_client_random, |
4838 | 256 | ECH_CONFIRMATION_SERVER_HELLO, |
4839 | 256 | ptls_iovec_init(emitter->buf->base + sh_start_off, emitter->buf->off - sh_start_off))) != 0) |
4840 | 256 | goto Exit; |
4841 | 256 | }); |
4842 | 256 | } |
4843 | | |
4844 | | /* processing of ECH is complete; dispose state */ |
4845 | 256 | clear_ech(&tls->ech, 1); |
4846 | | |
4847 | 256 | if ((ret = push_change_cipher_spec(tls, emitter)) != 0) |
4848 | 0 | goto Exit; |
4849 | | |
4850 | | /* create protection contexts for the handshake */ |
4851 | 256 | assert(tls->key_schedule->generation == 1); |
4852 | 256 | key_schedule_extract(tls->key_schedule, ecdh_secret); |
4853 | 256 | if ((ret = setup_traffic_protection(tls, 1, "s hs traffic", 2, 0, 0)) != 0) |
4854 | 0 | goto Exit; |
4855 | 256 | if (tls->pending_handshake_secret != NULL) { |
4856 | 0 | if ((ret = derive_secret(tls->key_schedule, tls->pending_handshake_secret, "c hs traffic")) != 0) |
4857 | 0 | goto Exit; |
4858 | 0 | if (tls->ctx->update_traffic_key != NULL && |
4859 | 0 | (ret = tls->ctx->update_traffic_key->cb(tls->ctx->update_traffic_key, tls, 0, 2, tls->pending_handshake_secret)) != 0) |
4860 | 0 | goto Exit; |
4861 | 256 | } else { |
4862 | 256 | if ((ret = setup_traffic_protection(tls, 0, "c hs traffic", 2, 0, 0)) != 0) |
4863 | 0 | goto Exit; |
4864 | 256 | if (ch->psk.early_data_indication) |
4865 | 57 | tls->server.early_data_skipped_bytes = 0; |
4866 | 256 | } |
4867 | | |
4868 | | /* send EncryptedExtensions */ |
4869 | 256 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_ENCRYPTED_EXTENSIONS, { |
4870 | 256 | ptls_buffer_t *sendbuf = emitter->buf; |
4871 | 256 | ptls_buffer_push_block(sendbuf, 2, { |
4872 | 256 | if (tls->server_name != NULL) { |
4873 | | /* In this event, the server SHALL include an extension of type "server_name" in the (extended) server hello. |
4874 | | * The "extension_data" field of this extension SHALL be empty. (RFC 6066 section 3) */ |
4875 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SERVER_NAME, {}); |
4876 | 256 | } |
4877 | 256 | if (tls->ctx->use_raw_public_keys) { |
4878 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SERVER_CERTIFICATE_TYPE, |
4879 | 256 | { ptls_buffer_push(sendbuf, PTLS_CERTIFICATE_TYPE_RAW_PUBLIC_KEY); }); |
4880 | 256 | } |
4881 | 256 | if (tls->negotiated_protocol != NULL) { |
4882 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ALPN, { |
4883 | 256 | ptls_buffer_push_block(sendbuf, 2, { |
4884 | 256 | ptls_buffer_push_block(sendbuf, 1, { |
4885 | 256 | ptls_buffer_pushv(sendbuf, tls->negotiated_protocol, strlen(tls->negotiated_protocol)); |
4886 | 256 | }); |
4887 | 256 | }); |
4888 | 256 | }); |
4889 | 256 | } |
4890 | 256 | if (tls->pending_handshake_secret != NULL) |
4891 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_EARLY_DATA, {}); |
4892 | | /* send ECH retry_configs, if ECH was offered by rejected, even though we (the server) could have accepted ECH */ |
4893 | 256 | if (tls->ech.state == PTLS_ECH_STATE_OFFERED && tls->ctx->ech.server.create_opener != NULL && |
4894 | 256 | tls->ctx->ech.server.retry_configs.len != 0) |
4895 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_ENCRYPTED_CLIENT_HELLO, { |
4896 | 256 | ptls_buffer_pushv(sendbuf, tls->ctx->ech.server.retry_configs.base, tls->ctx->ech.server.retry_configs.len); |
4897 | 256 | }); |
4898 | 256 | if (ch->ticket_request.new_session_count != 0 && tls->server.num_tickets_to_send != 0) |
4899 | 256 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_TICKET_REQUEST, |
4900 | 256 | { ptls_buffer_push(sendbuf, tls->server.num_tickets_to_send); }); |
4901 | 256 | if ((ret = push_additional_extensions(properties, sendbuf)) != 0) |
4902 | 256 | goto Exit; |
4903 | 256 | }); |
4904 | 256 | }); |
4905 | | |
4906 | 256 | if (mode == HANDSHAKE_MODE_FULL) { |
4907 | | /* send certificate request if client authentication is activated */ |
4908 | 256 | if (tls->ctx->require_client_authentication) { |
4909 | 0 | ptls_push_message(emitter, tls->key_schedule, PTLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST, { |
4910 | 0 | ptls_buffer_t *sendbuf = emitter->buf; |
4911 | | /* certificate_request_context: this field SHALL be zero length, unless the certificate request is used for post- |
4912 | | * handshake authentication. */ |
4913 | 0 | ptls_buffer_push(sendbuf, 0); |
4914 | | /* extensions */ |
4915 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
4916 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SIGNATURE_ALGORITHMS, { |
4917 | 0 | if ((ret = push_signature_algorithms(tls->ctx->verify_certificate, sendbuf)) != 0) |
4918 | 0 | goto Exit; |
4919 | 0 | }); |
4920 | | /* certificate authorities entension */ |
4921 | 0 | if (tls->ctx->client_ca_names.count > 0) { |
4922 | 0 | buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_CERTIFICATE_AUTHORITIES, { |
4923 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
4924 | 0 | for (size_t i = 0; i != tls->ctx->client_ca_names.count; ++i) { |
4925 | 0 | ptls_buffer_push_block(sendbuf, 2, { |
4926 | 0 | ptls_iovec_t name = tls->ctx->client_ca_names.list[i]; |
4927 | 0 | ptls_buffer_pushv(sendbuf, name.base, name.len); |
4928 | 0 | }); |
4929 | 0 | } |
4930 | 0 | }); |
4931 | 0 | }); |
4932 | 0 | } |
4933 | 0 | }); |
4934 | 0 | }); |
4935 | | |
4936 | 0 | if (ret != 0) { |
4937 | 0 | goto Exit; |
4938 | 0 | } |
4939 | 0 | } |
4940 | | |
4941 | | /* send certificate */ |
4942 | 256 | if ((ret = send_certificate(tls, emitter, &ch->signature_algorithms, ptls_iovec_init(NULL, 0), ch->status_request, |
4943 | 256 | ch->cert_compression_algos.list, ch->cert_compression_algos.count)) != 0) |
4944 | 14 | goto Exit; |
4945 | | /* send certificateverify, finished, and complete the handshake */ |
4946 | 242 | if ((ret = server_finish_handshake(tls, emitter, 1, &ch->signature_algorithms)) != 0) |
4947 | 0 | goto Exit; |
4948 | 242 | } else { |
4949 | | /* send finished, and complete the handshake */ |
4950 | 0 | if ((ret = server_finish_handshake(tls, emitter, 0, NULL)) != 0) |
4951 | 0 | goto Exit; |
4952 | 0 | } |
4953 | | |
4954 | 2.01k | Exit: |
4955 | 2.01k | free(pubkey.base); |
4956 | 2.01k | if (ecdh_secret.base != NULL) { |
4957 | 256 | ptls_clear_memory(ecdh_secret.base, ecdh_secret.len); |
4958 | 256 | free(ecdh_secret.base); |
4959 | 256 | } |
4960 | 2.01k | free(ech.encoded_ch_inner); |
4961 | 2.01k | free(ech.ch_outer_aad); |
4962 | 2.01k | ptls_buffer_dispose(&ech.ch_inner); |
4963 | 2.01k | free(ch); |
4964 | 2.01k | return ret; |
4965 | | |
4966 | 256 | #undef EMIT_SERVER_HELLO |
4967 | 256 | #undef EMIT_HELLO_RETRY_REQUEST |
4968 | 256 | } |
4969 | | |
4970 | | static int server_finish_handshake(ptls_t *tls, ptls_message_emitter_t *emitter, int send_cert_verify, |
4971 | | struct st_ptls_signature_algorithms_t *signature_algorithms) |
4972 | 242 | { |
4973 | 242 | int ret; |
4974 | | |
4975 | 242 | if (send_cert_verify) { |
4976 | 242 | if ((ret = send_certificate_verify(tls, emitter, signature_algorithms, PTLS_SERVER_CERTIFICATE_VERIFY_CONTEXT_STRING)) != |
4977 | 242 | 0) { |
4978 | 0 | if (ret == PTLS_ERROR_ASYNC_OPERATION) { |
4979 | 0 | tls->state = PTLS_STATE_SERVER_GENERATING_CERTIFICATE_VERIFY; |
4980 | 0 | } |
4981 | 0 | goto Exit; |
4982 | 0 | } |
4983 | 242 | } |
4984 | | |
4985 | 242 | if ((ret = send_finished(tls, emitter)) != 0) |
4986 | 0 | goto Exit; |
4987 | | |
4988 | 242 | assert(tls->key_schedule->generation == 2); |
4989 | 242 | if ((ret = key_schedule_extract(tls->key_schedule, ptls_iovec_init(NULL, 0))) != 0) |
4990 | 0 | goto Exit; |
4991 | 242 | if ((ret = setup_traffic_protection(tls, 1, "s ap traffic", 3, 0, 0)) != 0) |
4992 | 0 | goto Exit; |
4993 | 242 | if ((ret = derive_secret(tls->key_schedule, tls->server.pending_traffic_secret, "c ap traffic")) != 0) |
4994 | 0 | goto Exit; |
4995 | 242 | if ((ret = derive_exporter_secret(tls, 0)) != 0) |
4996 | 0 | goto Exit; |
4997 | | |
4998 | 242 | if (tls->pending_handshake_secret != NULL) { |
4999 | 0 | if (tls->ctx->omit_end_of_early_data) { |
5000 | 0 | if ((ret = commission_handshake_secret(tls)) != 0) |
5001 | 0 | goto Exit; |
5002 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_FINISHED; |
5003 | 0 | } else { |
5004 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA; |
5005 | 0 | } |
5006 | 242 | } else if (tls->ctx->require_client_authentication) { |
5007 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_CERTIFICATE; |
5008 | 242 | } else { |
5009 | 242 | tls->state = PTLS_STATE_SERVER_EXPECT_FINISHED; |
5010 | 242 | } |
5011 | | |
5012 | | /* send session ticket if necessary */ |
5013 | 242 | if (tls->server.num_tickets_to_send != 0) { |
5014 | 112 | assert(tls->ctx->ticket_lifetime != 0); |
5015 | 357 | for (uint8_t i = 0; i < tls->server.num_tickets_to_send; ++i) |
5016 | 245 | if ((ret = send_session_ticket(tls, emitter)) != 0) |
5017 | 0 | goto Exit; |
5018 | 112 | } |
5019 | | |
5020 | 242 | if (tls->ctx->require_client_authentication) { |
5021 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
5022 | 242 | } else { |
5023 | 242 | ret = 0; |
5024 | 242 | } |
5025 | | |
5026 | 242 | Exit: |
5027 | 242 | return ret; |
5028 | 242 | } |
5029 | | |
5030 | | static int server_handle_end_of_early_data(ptls_t *tls, ptls_iovec_t message) |
5031 | 0 | { |
5032 | 0 | int ret; |
5033 | |
|
5034 | 0 | if ((ret = commission_handshake_secret(tls)) != 0) |
5035 | 0 | goto Exit; |
5036 | | |
5037 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
5038 | 0 | tls->state = PTLS_STATE_SERVER_EXPECT_FINISHED; |
5039 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
5040 | |
|
5041 | 0 | Exit: |
5042 | 0 | return ret; |
5043 | 0 | } |
5044 | | |
5045 | | static int server_handle_finished(ptls_t *tls, ptls_iovec_t message) |
5046 | 19 | { |
5047 | 19 | int ret; |
5048 | | |
5049 | 19 | if ((ret = verify_finished(tls, message)) != 0) |
5050 | 19 | return ret; |
5051 | | |
5052 | 0 | memcpy(tls->traffic_protection.dec.secret, tls->server.pending_traffic_secret, sizeof(tls->server.pending_traffic_secret)); |
5053 | 0 | ptls_clear_memory(tls->server.pending_traffic_secret, sizeof(tls->server.pending_traffic_secret)); |
5054 | 0 | if ((ret = setup_traffic_protection(tls, 0, NULL, 3, 0, 0)) != 0) |
5055 | 0 | return ret; |
5056 | | |
5057 | 0 | ptls__key_schedule_update_hash(tls->key_schedule, message.base, message.len, 0); |
5058 | |
|
5059 | 0 | tls->state = PTLS_STATE_SERVER_POST_HANDSHAKE; |
5060 | 0 | return 0; |
5061 | 0 | } |
5062 | | |
5063 | | static int update_traffic_key(ptls_t *tls, int is_enc) |
5064 | 0 | { |
5065 | 0 | struct st_ptls_traffic_protection_t *tp = is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec; |
5066 | 0 | uint8_t secret[PTLS_MAX_DIGEST_SIZE]; |
5067 | 0 | int ret; |
5068 | |
|
5069 | 0 | ptls_hash_algorithm_t *hash = tls->key_schedule->hashes[0].algo; |
5070 | 0 | if ((ret = ptls_hkdf_expand_label(hash, secret, hash->digest_size, ptls_iovec_init(tp->secret, hash->digest_size), |
5071 | 0 | "traffic upd", ptls_iovec_init(NULL, 0), NULL)) != 0) |
5072 | 0 | goto Exit; |
5073 | 0 | memcpy(tp->secret, secret, sizeof(secret)); |
5074 | 0 | ret = setup_traffic_protection(tls, is_enc, NULL, 3, 0, 1); |
5075 | |
|
5076 | 0 | Exit: |
5077 | 0 | ptls_clear_memory(secret, sizeof(secret)); |
5078 | 0 | return ret; |
5079 | 0 | } |
5080 | | |
5081 | | static int handle_key_update(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message) |
5082 | 0 | { |
5083 | 0 | const uint8_t *src = message.base + PTLS_HANDSHAKE_HEADER_SIZE, *const end = message.base + message.len; |
5084 | 0 | int ret; |
5085 | | |
5086 | | /* validate */ |
5087 | 0 | if (end - src != 1 || *src > 1) |
5088 | 0 | return PTLS_ALERT_DECODE_ERROR; |
5089 | | |
5090 | | /* update receive key */ |
5091 | 0 | if ((ret = update_traffic_key(tls, 0)) != 0) |
5092 | 0 | return ret; |
5093 | | |
5094 | 0 | if (*src) { |
5095 | 0 | if (tls->ctx->update_traffic_key != NULL) |
5096 | 0 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
5097 | 0 | tls->needs_key_update = 1; |
5098 | 0 | } |
5099 | | |
5100 | 0 | return 0; |
5101 | 0 | } |
5102 | | |
5103 | | static int parse_record_header(struct st_ptls_record_t *rec, const uint8_t *src) |
5104 | 38.0k | { |
5105 | 38.0k | rec->type = src[0]; |
5106 | 38.0k | rec->version = ntoh16(src + 1); |
5107 | 38.0k | rec->length = ntoh16(src + 3); |
5108 | | |
5109 | 38.0k | if (rec->length > |
5110 | 38.0k | (size_t)(rec->type == PTLS_CONTENT_TYPE_APPDATA ? PTLS_MAX_ENCRYPTED_RECORD_SIZE : PTLS_MAX_PLAINTEXT_RECORD_SIZE)) |
5111 | 2 | return PTLS_ALERT_DECODE_ERROR; |
5112 | | |
5113 | 38.0k | return 0; |
5114 | 38.0k | } |
5115 | | |
5116 | | static int parse_record(ptls_t *tls, struct st_ptls_record_t *rec, const uint8_t *src, size_t *len) |
5117 | 38.0k | { |
5118 | 38.0k | int ret; |
5119 | | |
5120 | 38.0k | assert(*len != 0); |
5121 | | |
5122 | | /* Check if the first byte is something that we can handle, otherwise do not bother parsing / buffering the entire record as it |
5123 | | * is obviously broken. SSL 2.0 handshakes fall into this path as well. */ |
5124 | 38.0k | if (tls->recvbuf.rec.base == NULL) { |
5125 | 38.0k | uint8_t type = src[0]; |
5126 | 38.0k | switch (type) { |
5127 | 435 | case PTLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC: |
5128 | 494 | case PTLS_CONTENT_TYPE_ALERT: |
5129 | 27.5k | case PTLS_CONTENT_TYPE_HANDSHAKE: |
5130 | 37.9k | case PTLS_CONTENT_TYPE_APPDATA: |
5131 | 37.9k | break; |
5132 | 63 | default: |
5133 | 63 | return PTLS_ALERT_DECODE_ERROR; |
5134 | 38.0k | } |
5135 | 38.0k | } |
5136 | | |
5137 | 37.9k | if (tls->recvbuf.rec.base == NULL && *len >= 5) { |
5138 | | /* fast path */ |
5139 | 37.9k | if ((ret = parse_record_header(rec, src)) != 0) |
5140 | 2 | return ret; |
5141 | 37.9k | if (5 + rec->length <= *len) { |
5142 | 37.8k | rec->fragment = src + 5; |
5143 | 37.8k | *len = rec->length + 5; |
5144 | 37.8k | return 0; |
5145 | 37.8k | } |
5146 | 37.9k | } |
5147 | | |
5148 | | /* slow path */ |
5149 | 131 | const uint8_t *const end = src + *len; |
5150 | 131 | *rec = (struct st_ptls_record_t){0}; |
5151 | | |
5152 | 131 | if (tls->recvbuf.rec.base == NULL) { |
5153 | 131 | ptls_buffer_init(&tls->recvbuf.rec, "", 0); |
5154 | 131 | if ((ret = ptls_buffer_reserve(&tls->recvbuf.rec, 5)) != 0) |
5155 | 0 | return ret; |
5156 | 131 | } |
5157 | | |
5158 | | /* fill and parse the header */ |
5159 | 668 | while (tls->recvbuf.rec.off < 5) { |
5160 | 568 | if (src == end) |
5161 | 31 | return PTLS_ERROR_IN_PROGRESS; |
5162 | 537 | tls->recvbuf.rec.base[tls->recvbuf.rec.off++] = *src++; |
5163 | 537 | } |
5164 | 100 | if ((ret = parse_record_header(rec, tls->recvbuf.rec.base)) != 0) |
5165 | 0 | return ret; |
5166 | | |
5167 | | /* fill the fragment */ |
5168 | 100 | size_t addlen = rec->length + 5 - tls->recvbuf.rec.off; |
5169 | 100 | if (addlen != 0) { |
5170 | 100 | if ((ret = ptls_buffer_reserve(&tls->recvbuf.rec, addlen)) != 0) |
5171 | 0 | return ret; |
5172 | 100 | if (addlen > (size_t)(end - src)) |
5173 | 100 | addlen = end - src; |
5174 | 100 | if (addlen != 0) { |
5175 | 47 | memcpy(tls->recvbuf.rec.base + tls->recvbuf.rec.off, src, addlen); |
5176 | 47 | tls->recvbuf.rec.off += addlen; |
5177 | 47 | src += addlen; |
5178 | 47 | } |
5179 | 100 | } |
5180 | | |
5181 | | /* set rec->fragment if a complete record has been parsed */ |
5182 | 100 | if (tls->recvbuf.rec.off == rec->length + 5) { |
5183 | 0 | rec->fragment = tls->recvbuf.rec.base + 5; |
5184 | 0 | ret = 0; |
5185 | 100 | } else { |
5186 | 100 | ret = PTLS_ERROR_IN_PROGRESS; |
5187 | 100 | } |
5188 | | |
5189 | 100 | *len -= end - src; |
5190 | 100 | return ret; |
5191 | 100 | } |
5192 | | |
5193 | | static void update_open_count(ptls_context_t *ctx, ssize_t delta) |
5194 | 4.49k | { |
5195 | 4.49k | if (ctx->update_open_count != NULL) |
5196 | 0 | ctx->update_open_count->cb(ctx->update_open_count, delta); |
5197 | 4.49k | } |
5198 | | |
5199 | | static ptls_t *new_instance(ptls_context_t *ctx, int is_server) |
5200 | 2.24k | { |
5201 | 2.24k | ptls_t *tls; |
5202 | | |
5203 | | /* check consistency of `ptls_context_t` before instantiating a connection object */ |
5204 | 2.24k | assert(ctx->get_time != NULL && "please set ctx->get_time to `&ptls_get_time`; see #92"); |
5205 | 2.24k | if (ctx->pre_shared_key.identity.base != NULL) { |
5206 | 0 | assert(ctx->pre_shared_key.identity.len != 0 && ctx->pre_shared_key.secret.base != NULL && |
5207 | 0 | ctx->pre_shared_key.secret.len != 0 && ctx->pre_shared_key.hash != NULL && |
5208 | 0 | "`ptls_context_t::pre_shared_key` in incosistent state"); |
5209 | 2.24k | } else { |
5210 | 2.24k | assert(ctx->pre_shared_key.identity.len == 0 && ctx->pre_shared_key.secret.base == NULL && |
5211 | 2.24k | ctx->pre_shared_key.secret.len == 0 && ctx->pre_shared_key.hash == NULL && |
5212 | 2.24k | "`ptls_context_t::pre_shared_key` in inconsitent state"); |
5213 | 2.24k | } |
5214 | | |
5215 | 2.24k | if ((tls = malloc(sizeof(*tls))) == NULL) |
5216 | 0 | return NULL; |
5217 | | |
5218 | 2.24k | update_open_count(ctx, 1); |
5219 | 2.24k | *tls = (ptls_t){ctx}; |
5220 | 2.24k | tls->is_server = is_server; |
5221 | 2.24k | tls->send_change_cipher_spec = ctx->send_change_cipher_spec; |
5222 | | |
5223 | 2.24k | #if PTLS_HAVE_LOG |
5224 | 2.24k | if (ptls_log_conn_state_override != NULL) { |
5225 | 0 | tls->log_state = *ptls_log_conn_state_override; |
5226 | 2.24k | } else { |
5227 | 2.24k | ptls_log_init_conn_state(&tls->log_state, ctx->random_bytes, 0, NULL); |
5228 | 2.24k | } |
5229 | 2.24k | #endif |
5230 | | |
5231 | 2.24k | return tls; |
5232 | 2.24k | } |
5233 | | |
5234 | | ptls_t *ptls_client_new(ptls_context_t *ctx) |
5235 | 0 | { |
5236 | 0 | ptls_t *tls = new_instance(ctx, 0); |
5237 | 0 | tls->state = PTLS_STATE_CLIENT_HANDSHAKE_START; |
5238 | 0 | tls->ctx->random_bytes(tls->client_random, sizeof(tls->client_random)); |
5239 | 0 | log_client_random(tls); |
5240 | 0 | if (tls->send_change_cipher_spec) { |
5241 | 0 | tls->client.legacy_session_id = |
5242 | 0 | ptls_iovec_init(tls->client.legacy_session_id_buf, sizeof(tls->client.legacy_session_id_buf)); |
5243 | 0 | tls->ctx->random_bytes(tls->client.legacy_session_id.base, tls->client.legacy_session_id.len); |
5244 | 0 | } |
5245 | |
|
5246 | 0 | PTLS_PROBE(NEW, tls, 0); |
5247 | 0 | PTLS_LOG_CONN(new, tls, { PTLS_LOG_ELEMENT_BOOL(is_server, 0); }); |
5248 | 0 | return tls; |
5249 | 0 | } |
5250 | | |
5251 | | ptls_t *ptls_server_new(ptls_context_t *ctx) |
5252 | 2.24k | { |
5253 | 2.24k | ptls_t *tls = new_instance(ctx, 1); |
5254 | 2.24k | tls->state = PTLS_STATE_SERVER_EXPECT_CLIENT_HELLO; |
5255 | 2.24k | tls->server.early_data_skipped_bytes = UINT32_MAX; |
5256 | | |
5257 | 2.24k | PTLS_PROBE(NEW, tls, 1); |
5258 | 2.24k | PTLS_LOG_CONN(new, tls, { PTLS_LOG_ELEMENT_BOOL(is_server, 1); }); |
5259 | 2.24k | return tls; |
5260 | 2.24k | } |
5261 | | |
5262 | | #define export_tls_params(output, is_server, session_reused, protocol_version, cipher, client_random, server_name, \ |
5263 | | negotiated_protocol, ver_block) \ |
5264 | 0 | do { \ |
5265 | 0 | const char *_server_name = (server_name); \ |
5266 | 0 | ptls_iovec_t _negotiated_protocol = (negotiated_protocol); \ |
5267 | 0 | ptls_buffer_push_block((output), 2, { \ |
5268 | 0 | ptls_buffer_push((output), (is_server)); \ |
5269 | 0 | ptls_buffer_push((output), (session_reused)); \ |
5270 | 0 | ptls_buffer_push16((output), (protocol_version)); \ |
5271 | 0 | ptls_buffer_push16((output), (cipher)->id); \ |
5272 | 0 | ptls_buffer_pushv((output), (client_random), PTLS_HELLO_RANDOM_SIZE); \ |
5273 | 0 | ptls_buffer_push_block((output), 2, { \ |
5274 | 0 | size_t len = _server_name != NULL ? strlen(_server_name) : 0; \ |
5275 | 0 | ptls_buffer_pushv((output), _server_name, len); \ |
5276 | 0 | }); \ |
5277 | 0 | ptls_buffer_push_block((output), 2, \ |
5278 | 0 | { ptls_buffer_pushv((output), _negotiated_protocol.base, _negotiated_protocol.len); }); \ |
5279 | 0 | ptls_buffer_push_block((output), 2, {ver_block}); /* version-specific block */ \ |
5280 | 0 | ptls_buffer_push_block((output), 2, {}); /* for future extensions */ \ |
5281 | 0 | }); \ |
5282 | 0 | } while (0) |
5283 | | |
5284 | | static int export_tls12_params(ptls_buffer_t *output, int is_server, int session_reused, ptls_cipher_suite_t *cipher, |
5285 | | const void *client_random, const char *server_name, ptls_iovec_t negotiated_protocol, |
5286 | | const void *enc_key, const void *enc_iv, uint64_t enc_seq, uint64_t enc_record_iv, |
5287 | | const void *dec_key, const void *dec_iv, uint64_t dec_seq) |
5288 | 0 | { |
5289 | 0 | int ret; |
5290 | |
|
5291 | 0 | export_tls_params(output, is_server, session_reused, PTLS_PROTOCOL_VERSION_TLS12, cipher, client_random, server_name, |
5292 | 0 | negotiated_protocol, { |
5293 | 0 | ptls_buffer_pushv(output, enc_key, cipher->aead->key_size); |
5294 | 0 | ptls_buffer_pushv(output, enc_iv, cipher->aead->tls12.fixed_iv_size); |
5295 | 0 | ptls_buffer_push64(output, enc_seq); |
5296 | 0 | if (cipher->aead->tls12.record_iv_size != 0) |
5297 | 0 | ptls_buffer_push64(output, enc_record_iv); |
5298 | 0 | ptls_buffer_pushv(output, dec_key, cipher->aead->key_size); |
5299 | 0 | ptls_buffer_pushv(output, dec_iv, cipher->aead->tls12.fixed_iv_size); |
5300 | 0 | ptls_buffer_push64(output, dec_seq); |
5301 | 0 | }); |
5302 | 0 | ret = 0; |
5303 | |
|
5304 | 0 | Exit: |
5305 | 0 | return ret; |
5306 | 0 | } |
5307 | | |
5308 | | int ptls_build_tls12_export_params(ptls_context_t *ctx, ptls_buffer_t *output, int is_server, int session_reused, |
5309 | | ptls_cipher_suite_t *cipher, const void *master_secret, const void *hello_randoms, |
5310 | | uint64_t next_send_record_iv, const char *server_name, ptls_iovec_t negotiated_protocol) |
5311 | 0 | { |
5312 | 0 | assert(cipher->aead->tls12.fixed_iv_size + cipher->aead->tls12.record_iv_size != 0 || !"given cipher-suite supports TLS/1.2"); |
5313 | | |
5314 | 0 | uint8_t key_block[(PTLS_MAX_SECRET_SIZE + PTLS_MAX_IV_SIZE) * 2]; |
5315 | 0 | size_t key_block_len = (cipher->aead->key_size + cipher->aead->tls12.fixed_iv_size) * 2; |
5316 | 0 | int ret; |
5317 | |
|
5318 | 0 | assert(key_block_len <= sizeof(key_block)); |
5319 | | |
5320 | | /* generate key block */ |
5321 | 0 | if ((ret = |
5322 | 0 | ptls_tls12_phash(cipher->hash, key_block, key_block_len, ptls_iovec_init(master_secret, PTLS_TLS12_MASTER_SECRET_SIZE), |
5323 | 0 | "key expansion", ptls_iovec_init(hello_randoms, PTLS_HELLO_RANDOM_SIZE * 2))) != 0) |
5324 | 0 | goto Exit; |
5325 | | |
5326 | | /* determine key locations */ |
5327 | 0 | struct { |
5328 | 0 | const void *key; |
5329 | 0 | const void *iv; |
5330 | 0 | } client_secret, server_secret, *enc_secret = is_server ? &server_secret : &client_secret, |
5331 | 0 | *dec_secret = is_server ? &client_secret : &server_secret; |
5332 | 0 | client_secret.key = key_block; |
5333 | 0 | server_secret.key = key_block + cipher->aead->key_size; |
5334 | 0 | client_secret.iv = key_block + cipher->aead->key_size * 2; |
5335 | 0 | server_secret.iv = key_block + cipher->aead->key_size * 2 + cipher->aead->tls12.fixed_iv_size; |
5336 | | |
5337 | | /* Serialize prams. Sequence number of the first application record is 1, because Finished is the only message sent after |
5338 | | * ChangeCipherSpec. */ |
5339 | 0 | ret = export_tls12_params(output, is_server, session_reused, cipher, (uint8_t *)hello_randoms + PTLS_HELLO_RANDOM_SIZE, |
5340 | 0 | server_name, negotiated_protocol, enc_secret->key, enc_secret->iv, 1, next_send_record_iv, |
5341 | 0 | dec_secret->key, dec_secret->iv, 1); |
5342 | |
|
5343 | 0 | Exit: |
5344 | 0 | ptls_clear_memory(key_block, sizeof(key_block)); |
5345 | 0 | return ret; |
5346 | 0 | } |
5347 | | |
5348 | | int ptls_export(ptls_t *tls, ptls_buffer_t *output) |
5349 | 0 | { |
5350 | 0 | ptls_iovec_t negotiated_protocol = |
5351 | 0 | ptls_iovec_init(tls->negotiated_protocol, tls->negotiated_protocol != NULL ? strlen(tls->negotiated_protocol) : 0); |
5352 | 0 | int ret; |
5353 | |
|
5354 | 0 | if (tls->state != PTLS_STATE_SERVER_POST_HANDSHAKE) { |
5355 | 0 | ret = PTLS_ERROR_LIBRARY; |
5356 | 0 | goto Exit; |
5357 | 0 | } |
5358 | | |
5359 | 0 | if (ptls_get_protocol_version(tls) == PTLS_PROTOCOL_VERSION_TLS13) { |
5360 | 0 | export_tls_params(output, tls->is_server, tls->is_psk_handshake, PTLS_PROTOCOL_VERSION_TLS13, tls->cipher_suite, |
5361 | 0 | tls->client_random, tls->server_name, negotiated_protocol, { |
5362 | 0 | ptls_buffer_pushv(output, tls->traffic_protection.enc.secret, tls->cipher_suite->hash->digest_size); |
5363 | 0 | ptls_buffer_push64(output, tls->traffic_protection.enc.seq); |
5364 | 0 | ptls_buffer_pushv(output, tls->traffic_protection.dec.secret, tls->cipher_suite->hash->digest_size); |
5365 | 0 | ptls_buffer_push64(output, tls->traffic_protection.dec.seq); |
5366 | 0 | }); |
5367 | 0 | ret = 0; |
5368 | 0 | } else { |
5369 | 0 | if ((ret = export_tls12_params(output, tls->is_server, tls->is_psk_handshake, tls->cipher_suite, tls->client_random, |
5370 | 0 | tls->server_name, negotiated_protocol, tls->traffic_protection.enc.secret, |
5371 | 0 | tls->traffic_protection.enc.secret + PTLS_MAX_SECRET_SIZE, tls->traffic_protection.enc.seq, |
5372 | 0 | tls->traffic_protection.enc.tls12_enc_record_iv, tls->traffic_protection.dec.secret, |
5373 | 0 | tls->traffic_protection.dec.secret + PTLS_MAX_SECRET_SIZE, |
5374 | 0 | tls->traffic_protection.dec.seq)) != 0) |
5375 | 0 | goto Exit; |
5376 | 0 | } |
5377 | | |
5378 | 0 | Exit: |
5379 | 0 | return ret; |
5380 | 0 | } |
5381 | | |
5382 | | static int import_tls12_traffic_protection(ptls_t *tls, int is_enc, const uint8_t **src, const uint8_t *const end) |
5383 | 0 | { |
5384 | 0 | struct st_ptls_traffic_protection_t *tp = is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec; |
5385 | |
|
5386 | 0 | if ((size_t)(end - *src) < tls->cipher_suite->aead->key_size + tls->cipher_suite->aead->tls12.fixed_iv_size + sizeof(uint64_t)) |
5387 | 0 | return PTLS_ALERT_DECODE_ERROR; |
5388 | | |
5389 | | /* set properties */ |
5390 | 0 | memcpy(tp->secret, *src, tls->cipher_suite->aead->key_size); |
5391 | 0 | *src += tls->cipher_suite->aead->key_size; |
5392 | 0 | memcpy(tp->secret + PTLS_MAX_SECRET_SIZE, *src, tls->cipher_suite->aead->tls12.fixed_iv_size); |
5393 | 0 | *src += tls->cipher_suite->aead->tls12.fixed_iv_size; |
5394 | 0 | if (ptls_decode64(&tp->seq, src, end) != 0) |
5395 | 0 | return PTLS_ALERT_DECODE_ERROR; |
5396 | 0 | if (is_enc && tls->cipher_suite->aead->tls12.record_iv_size != 0) { |
5397 | 0 | if (ptls_decode64(&tp->tls12_enc_record_iv, src, end) != 0) |
5398 | 0 | return PTLS_ALERT_DECODE_ERROR; |
5399 | 0 | } |
5400 | 0 | tp->tls12 = 1; |
5401 | | |
5402 | | /* instantiate aead */ |
5403 | 0 | if ((tp->aead = ptls_aead_new_direct(tls->cipher_suite->aead, is_enc, tp->secret, tp->secret + PTLS_MAX_SECRET_SIZE)) == NULL) |
5404 | 0 | return PTLS_ERROR_NO_MEMORY; |
5405 | | |
5406 | 0 | return 0; |
5407 | 0 | } |
5408 | | |
5409 | | static int import_tls13_traffic_protection(ptls_t *tls, int is_enc, const uint8_t **src, const uint8_t *const end) |
5410 | 0 | { |
5411 | 0 | struct st_ptls_traffic_protection_t *tp = is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec; |
5412 | | |
5413 | | /* set properties */ |
5414 | 0 | memcpy(tp->secret, *src, tls->cipher_suite->hash->digest_size); |
5415 | 0 | *src += tls->cipher_suite->hash->digest_size; |
5416 | 0 | if (ptls_decode64(&tp->seq, src, end) != 0) |
5417 | 0 | return PTLS_ALERT_DECODE_ERROR; |
5418 | | |
5419 | 0 | if (setup_traffic_protection(tls, is_enc, NULL, 3, tp->seq, 0) != 0) |
5420 | 0 | return PTLS_ERROR_INCOMPATIBLE_KEY; |
5421 | | |
5422 | 0 | return 0; |
5423 | 0 | } |
5424 | | |
5425 | | int ptls_import(ptls_context_t *ctx, ptls_t **tls, ptls_iovec_t params) |
5426 | 0 | { |
5427 | 0 | const uint8_t *src = params.base, *const end = src + params.len; |
5428 | 0 | uint16_t protocol_version, csid; |
5429 | 0 | int ret; |
5430 | |
|
5431 | 0 | *tls = NULL; |
5432 | | |
5433 | | /* TODO handle flags like psk_handshake, ech_handshake as we add support for TLS/1.3 import */ |
5434 | 0 | ptls_decode_block(src, end, 2, { |
5435 | | /* instantiate, based on the is_server flag */ |
5436 | 0 | if (end - src < 2) { |
5437 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
5438 | 0 | goto Exit; |
5439 | 0 | } |
5440 | 0 | if ((*tls = new_instance(ctx, *src++)) == NULL) { |
5441 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
5442 | 0 | goto Exit; |
5443 | 0 | } |
5444 | 0 | (*tls)->is_psk_handshake = *src++; |
5445 | | /* determine protocol version and cipher suite */ |
5446 | 0 | if ((ret = ptls_decode16(&protocol_version, &src, end)) != 0) |
5447 | 0 | goto Exit; |
5448 | 0 | if ((ret = ptls_decode16(&csid, &src, end)) != 0) |
5449 | 0 | goto Exit; |
5450 | | /* other version-independent stuff */ |
5451 | 0 | if (end - src < PTLS_HELLO_RANDOM_SIZE) { |
5452 | 0 | ret = PTLS_ALERT_DECODE_ERROR; |
5453 | 0 | goto Exit; |
5454 | 0 | } |
5455 | 0 | memcpy((*tls)->client_random, src, PTLS_HELLO_RANDOM_SIZE); |
5456 | 0 | src += PTLS_HELLO_RANDOM_SIZE; |
5457 | 0 | ptls_decode_open_block(src, end, 2, { |
5458 | 0 | if (src != end) { |
5459 | 0 | if ((ret = ptls_set_server_name(*tls, (const char *)src, end - src)) != 0) |
5460 | 0 | goto Exit; |
5461 | 0 | src = end; |
5462 | 0 | } |
5463 | 0 | }); |
5464 | 0 | ptls_decode_open_block(src, end, 2, { |
5465 | 0 | if (src != end) { |
5466 | 0 | if ((ret = ptls_set_negotiated_protocol(*tls, (const char *)src, end - src)) != 0) |
5467 | 0 | goto Exit; |
5468 | 0 | src = end; |
5469 | 0 | } |
5470 | 0 | }); |
5471 | | /* version-dependent stuff */ |
5472 | 0 | ptls_decode_open_block(src, end, 2, { |
5473 | 0 | switch (protocol_version) { |
5474 | 0 | case PTLS_PROTOCOL_VERSION_TLS12: |
5475 | 0 | (*tls)->cipher_suite = ptls_find_cipher_suite(ctx->tls12_cipher_suites, csid); |
5476 | 0 | if ((*tls)->cipher_suite == NULL) { |
5477 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
5478 | 0 | goto Exit; |
5479 | 0 | } |
5480 | | /* setup AEAD keys */ |
5481 | 0 | if ((ret = import_tls12_traffic_protection(*tls, 1, &src, end)) != 0) |
5482 | 0 | goto Exit; |
5483 | 0 | if ((ret = import_tls12_traffic_protection(*tls, 0, &src, end)) != 0) |
5484 | 0 | goto Exit; |
5485 | 0 | break; |
5486 | 0 | case PTLS_PROTOCOL_VERSION_TLS13: |
5487 | 0 | (*tls)->cipher_suite = ptls_find_cipher_suite(ctx->cipher_suites, csid); |
5488 | 0 | if ((*tls)->cipher_suite == NULL) { |
5489 | 0 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
5490 | 0 | goto Exit; |
5491 | 0 | } |
5492 | | /* setup AEAD keys */ |
5493 | 0 | if (((*tls)->key_schedule = key_schedule_new((*tls)->cipher_suite, NULL, (*tls)->ech.aead != NULL)) == NULL) { |
5494 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
5495 | 0 | goto Exit; |
5496 | 0 | } |
5497 | 0 | if ((ret = import_tls13_traffic_protection(*tls, 1, &src, end)) != 0) |
5498 | 0 | goto Exit; |
5499 | 0 | if ((ret = import_tls13_traffic_protection(*tls, 0, &src, end)) != 0) |
5500 | 0 | goto Exit; |
5501 | 0 | break; |
5502 | 0 | default: |
5503 | 0 | ret = PTLS_ALERT_ILLEGAL_PARAMETER; |
5504 | 0 | goto Exit; |
5505 | 0 | } |
5506 | 0 | }); |
5507 | | /* extensions */ |
5508 | 0 | ptls_decode_open_block(src, end, 2, { |
5509 | 0 | src = end; /* unused */ |
5510 | 0 | }); |
5511 | 0 | }); |
5512 | | |
5513 | 0 | (*tls)->state = ptls_is_server(*tls) ? PTLS_STATE_SERVER_POST_HANDSHAKE : PTLS_STATE_CLIENT_POST_HANDSHAKE; |
5514 | |
|
5515 | 0 | Exit: |
5516 | 0 | if (ret != 0) { |
5517 | 0 | if (*tls != NULL) { |
5518 | 0 | ptls_free(*tls); |
5519 | 0 | *tls = NULL; |
5520 | 0 | } |
5521 | 0 | } |
5522 | 0 | return ret; |
5523 | 0 | } |
5524 | | |
5525 | | void ptls_free(ptls_t *tls) |
5526 | 2.24k | { |
5527 | 2.24k | PTLS_PROBE0(FREE, tls); |
5528 | 2.24k | PTLS_LOG_CONN(free, tls, {}); |
5529 | | |
5530 | 2.24k | ptls_buffer_dispose(&tls->recvbuf.rec); |
5531 | 2.24k | ptls_buffer_dispose(&tls->recvbuf.mess); |
5532 | 2.24k | free_exporter_master_secret(tls, 1); |
5533 | 2.24k | free_exporter_master_secret(tls, 0); |
5534 | 2.24k | if (tls->key_schedule != NULL) |
5535 | 671 | key_schedule_free(tls->key_schedule); |
5536 | 2.24k | if (tls->traffic_protection.dec.aead != NULL) |
5537 | 256 | ptls_aead_free(tls->traffic_protection.dec.aead); |
5538 | 2.24k | if (tls->traffic_protection.enc.aead != NULL) |
5539 | 256 | ptls_aead_free(tls->traffic_protection.enc.aead); |
5540 | 2.24k | free(tls->server_name); |
5541 | 2.24k | free(tls->negotiated_protocol); |
5542 | 2.24k | clear_ech(&tls->ech, tls->is_server); |
5543 | 2.24k | if (tls->is_server) { |
5544 | 2.24k | if (tls->server.async_job != NULL) |
5545 | 0 | tls->server.async_job->destroy_(tls->server.async_job); |
5546 | 2.24k | } else { |
5547 | 0 | if (tls->client.key_share_ctx != NULL) |
5548 | 0 | tls->client.key_share_ctx->on_exchange(&tls->client.key_share_ctx, 1, NULL, ptls_iovec_init(NULL, 0)); |
5549 | 0 | if (tls->client.certificate_request.context.base != NULL) |
5550 | 0 | free(tls->client.certificate_request.context.base); |
5551 | 0 | } |
5552 | 2.24k | if (tls->certificate_verify.cb != NULL) |
5553 | 0 | tls->certificate_verify.cb(tls->certificate_verify.verify_ctx, 0, ptls_iovec_init(NULL, 0), ptls_iovec_init(NULL, 0)); |
5554 | 2.24k | if (tls->pending_handshake_secret != NULL) { |
5555 | 0 | ptls_clear_memory(tls->pending_handshake_secret, PTLS_MAX_DIGEST_SIZE); |
5556 | 0 | free(tls->pending_handshake_secret); |
5557 | 0 | } |
5558 | 2.24k | update_open_count(tls->ctx, -1); |
5559 | 2.24k | ptls_clear_memory(tls, sizeof(*tls)); |
5560 | 2.24k | free(tls); |
5561 | 2.24k | } |
5562 | | |
5563 | | ptls_context_t *ptls_get_context(ptls_t *tls) |
5564 | 0 | { |
5565 | 0 | return tls->ctx; |
5566 | 0 | } |
5567 | | |
5568 | | void ptls_set_context(ptls_t *tls, ptls_context_t *ctx) |
5569 | 0 | { |
5570 | 0 | update_open_count(ctx, 1); |
5571 | 0 | update_open_count(tls->ctx, -1); |
5572 | 0 | tls->ctx = ctx; |
5573 | 0 | } |
5574 | | |
5575 | | ptls_async_job_t *ptls_get_async_job(ptls_t *tls) |
5576 | 0 | { |
5577 | 0 | return tls->server.async_job; |
5578 | 0 | } |
5579 | | |
5580 | | ptls_iovec_t ptls_get_client_random(ptls_t *tls) |
5581 | 0 | { |
5582 | 0 | return ptls_iovec_init(tls->client_random, PTLS_HELLO_RANDOM_SIZE); |
5583 | 0 | } |
5584 | | |
5585 | | ptls_cipher_suite_t *ptls_get_cipher(ptls_t *tls) |
5586 | 0 | { |
5587 | 0 | return tls->cipher_suite; |
5588 | 0 | } |
5589 | | |
5590 | | uint16_t ptls_get_protocol_version(ptls_t *tls) |
5591 | 0 | { |
5592 | 0 | if (tls->traffic_protection.enc.tls12) |
5593 | 0 | return PTLS_PROTOCOL_VERSION_TLS12; |
5594 | | |
5595 | 0 | return PTLS_PROTOCOL_VERSION_TLS13; |
5596 | 0 | } |
5597 | | |
5598 | | int ptls_get_traffic_keys(ptls_t *tls, int is_enc, uint8_t *key, uint8_t *iv, uint64_t *seq) |
5599 | 0 | { |
5600 | 0 | struct st_ptls_traffic_protection_t *ctx = is_enc ? &tls->traffic_protection.enc : &tls->traffic_protection.dec; |
5601 | 0 | int ret; |
5602 | |
|
5603 | 0 | if ((ret = get_traffic_keys(tls->cipher_suite->aead, tls->cipher_suite->hash, key, iv, ctx->secret, ptls_iovec_init(NULL, 0), |
5604 | 0 | NULL)) != 0) |
5605 | 0 | return ret; |
5606 | 0 | *seq = ctx->seq; |
5607 | 0 | return 0; |
5608 | 0 | } |
5609 | | |
5610 | | const char *ptls_get_server_name(ptls_t *tls) |
5611 | 0 | { |
5612 | 0 | return tls->server_name; |
5613 | 0 | } |
5614 | | |
5615 | | int ptls_set_server_name(ptls_t *tls, const char *server_name, size_t server_name_len) |
5616 | 0 | { |
5617 | 0 | char *duped = NULL; |
5618 | |
|
5619 | 0 | if (server_name != NULL && |
5620 | 0 | (duped = duplicate_as_str(server_name, server_name_len != 0 ? server_name_len : strlen(server_name))) == NULL) |
5621 | 0 | return PTLS_ERROR_NO_MEMORY; |
5622 | | |
5623 | 0 | free(tls->server_name); |
5624 | 0 | tls->server_name = duped; |
5625 | |
|
5626 | 0 | return 0; |
5627 | 0 | } |
5628 | | |
5629 | | const char *ptls_get_negotiated_protocol(ptls_t *tls) |
5630 | 0 | { |
5631 | 0 | return tls->negotiated_protocol; |
5632 | 0 | } |
5633 | | |
5634 | | int ptls_set_negotiated_protocol(ptls_t *tls, const char *protocol, size_t protocol_len) |
5635 | 0 | { |
5636 | 0 | char *duped = NULL; |
5637 | |
|
5638 | 0 | if (protocol != NULL && (duped = duplicate_as_str(protocol, protocol_len != 0 ? protocol_len : strlen(protocol))) == NULL) |
5639 | 0 | return PTLS_ERROR_NO_MEMORY; |
5640 | | |
5641 | 0 | free(tls->negotiated_protocol); |
5642 | 0 | tls->negotiated_protocol = duped; |
5643 | |
|
5644 | 0 | return 0; |
5645 | 0 | } |
5646 | | |
5647 | | int ptls_handshake_is_complete(ptls_t *tls) |
5648 | 0 | { |
5649 | 0 | return tls->state >= PTLS_STATE_POST_HANDSHAKE_MIN; |
5650 | 0 | } |
5651 | | |
5652 | | int ptls_is_psk_handshake(ptls_t *tls) |
5653 | 0 | { |
5654 | 0 | return tls->is_psk_handshake; |
5655 | 0 | } |
5656 | | |
5657 | | int ptls_is_ech_handshake(ptls_t *tls, uint8_t *config_id, ptls_hpke_kem_t **kem, ptls_hpke_cipher_suite_t **cipher) |
5658 | 410 | { |
5659 | 410 | if (tls->ech.state == PTLS_ECH_STATE_ACCEPTED) { |
5660 | 0 | if (config_id != NULL) |
5661 | 0 | *config_id = tls->ech.config_id; |
5662 | 0 | if (kem != NULL) |
5663 | 0 | *kem = tls->ech.kem; |
5664 | 0 | if (cipher != NULL) |
5665 | 0 | *cipher = tls->ech.cipher; |
5666 | 0 | return 1; |
5667 | 0 | } |
5668 | 410 | return 0; |
5669 | 410 | } |
5670 | | |
5671 | | void **ptls_get_data_ptr(ptls_t *tls) |
5672 | 0 | { |
5673 | 0 | return &tls->data_ptr; |
5674 | 0 | } |
5675 | | |
5676 | | ptls_log_conn_state_t *ptls_get_log_state(ptls_t *tls) |
5677 | 0 | { |
5678 | 0 | #if PTLS_HAVE_LOG |
5679 | 0 | return &tls->log_state; |
5680 | | #else |
5681 | | return &ptls_log.dummy_conn_state; |
5682 | | #endif |
5683 | 0 | } |
5684 | | |
5685 | | static int handle_client_handshake_message(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message, int is_end_of_record, |
5686 | | ptls_handshake_properties_t *properties) |
5687 | 0 | { |
5688 | 0 | uint8_t type = message.base[0]; |
5689 | 0 | int ret; |
5690 | |
|
5691 | 0 | switch (tls->state) { |
5692 | 0 | case PTLS_STATE_CLIENT_EXPECT_SERVER_HELLO: |
5693 | 0 | case PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO: |
5694 | 0 | if (type == PTLS_HANDSHAKE_TYPE_SERVER_HELLO && is_end_of_record) { |
5695 | 0 | ret = client_handle_hello(tls, emitter, message, properties); |
5696 | 0 | } else { |
5697 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5698 | 0 | } |
5699 | 0 | break; |
5700 | 0 | case PTLS_STATE_CLIENT_EXPECT_ENCRYPTED_EXTENSIONS: |
5701 | 0 | if (type == PTLS_HANDSHAKE_TYPE_ENCRYPTED_EXTENSIONS) { |
5702 | 0 | ret = client_handle_encrypted_extensions(tls, message, properties); |
5703 | 0 | } else { |
5704 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5705 | 0 | } |
5706 | 0 | break; |
5707 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE: |
5708 | 0 | if (type == PTLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST) { |
5709 | 0 | ret = client_handle_certificate_request(tls, message, properties); |
5710 | 0 | break; |
5711 | 0 | } |
5712 | | /* fall through */ |
5713 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE: |
5714 | 0 | switch (type) { |
5715 | 0 | case PTLS_HANDSHAKE_TYPE_CERTIFICATE: |
5716 | 0 | ret = client_handle_certificate(tls, message); |
5717 | 0 | break; |
5718 | 0 | case PTLS_HANDSHAKE_TYPE_COMPRESSED_CERTIFICATE: |
5719 | 0 | ret = client_handle_compressed_certificate(tls, message); |
5720 | 0 | break; |
5721 | 0 | default: |
5722 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5723 | 0 | break; |
5724 | 0 | } |
5725 | 0 | break; |
5726 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_VERIFY: |
5727 | 0 | if (type == PTLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY) { |
5728 | 0 | ret = client_handle_certificate_verify(tls, message); |
5729 | 0 | } else { |
5730 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5731 | 0 | } |
5732 | 0 | break; |
5733 | 0 | case PTLS_STATE_CLIENT_EXPECT_FINISHED: |
5734 | 0 | if (type == PTLS_HANDSHAKE_TYPE_FINISHED && is_end_of_record) { |
5735 | 0 | ret = client_handle_finished(tls, emitter, message); |
5736 | 0 | } else { |
5737 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5738 | 0 | } |
5739 | 0 | break; |
5740 | 0 | case PTLS_STATE_CLIENT_POST_HANDSHAKE: |
5741 | 0 | switch (type) { |
5742 | 0 | case PTLS_HANDSHAKE_TYPE_NEW_SESSION_TICKET: |
5743 | 0 | ret = client_handle_new_session_ticket(tls, message); |
5744 | 0 | break; |
5745 | 0 | case PTLS_HANDSHAKE_TYPE_KEY_UPDATE: |
5746 | 0 | ret = handle_key_update(tls, emitter, message); |
5747 | 0 | break; |
5748 | 0 | default: |
5749 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5750 | 0 | break; |
5751 | 0 | } |
5752 | 0 | break; |
5753 | 0 | default: |
5754 | 0 | assert(!"unexpected state"); |
5755 | 0 | ret = PTLS_ALERT_INTERNAL_ERROR; |
5756 | 0 | break; |
5757 | 0 | } |
5758 | | |
5759 | 0 | PTLS_PROBE(RECEIVE_MESSAGE, tls, message.base[0], message.base + PTLS_HANDSHAKE_HEADER_SIZE, |
5760 | 0 | message.len - PTLS_HANDSHAKE_HEADER_SIZE, ret); |
5761 | 0 | PTLS_LOG_CONN(receive_message, tls, { |
5762 | 0 | PTLS_LOG_ELEMENT_UNSIGNED(message, message.base[0]); |
5763 | 0 | PTLS_LOG_ELEMENT_UNSIGNED(len, message.len - PTLS_HANDSHAKE_HEADER_SIZE); |
5764 | 0 | PTLS_LOG_ELEMENT_SIGNED(result, ret); |
5765 | 0 | }); |
5766 | |
|
5767 | 0 | return ret; |
5768 | 0 | } |
5769 | | |
5770 | | static int handle_server_handshake_message(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message, int is_end_of_record, |
5771 | | ptls_handshake_properties_t *properties) |
5772 | 2.10k | { |
5773 | 2.10k | uint8_t type = message.base[0]; |
5774 | 2.10k | int ret; |
5775 | | |
5776 | 2.10k | switch (tls->state) { |
5777 | 1.98k | case PTLS_STATE_SERVER_EXPECT_CLIENT_HELLO: |
5778 | 2.06k | case PTLS_STATE_SERVER_EXPECT_SECOND_CLIENT_HELLO: |
5779 | 2.06k | if (type == PTLS_HANDSHAKE_TYPE_CLIENT_HELLO && is_end_of_record) { |
5780 | 2.01k | ret = server_handle_hello(tls, emitter, message, properties); |
5781 | 2.01k | } else { |
5782 | 48 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
5783 | 48 | } |
5784 | 2.06k | break; |
5785 | 0 | case PTLS_STATE_SERVER_EXPECT_CERTIFICATE: |
5786 | 0 | if (type == PTLS_HANDSHAKE_TYPE_CERTIFICATE) { |
5787 | 0 | ret = server_handle_certificate(tls, message); |
5788 | 0 | } else { |
5789 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5790 | 0 | } |
5791 | 0 | break; |
5792 | 0 | case PTLS_STATE_SERVER_EXPECT_CERTIFICATE_VERIFY: |
5793 | 0 | if (type == PTLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY) { |
5794 | 0 | ret = server_handle_certificate_verify(tls, message); |
5795 | 0 | } else { |
5796 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5797 | 0 | } |
5798 | 0 | break; |
5799 | 0 | case PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA: |
5800 | 0 | assert(!tls->ctx->omit_end_of_early_data); |
5801 | 0 | if (type == PTLS_HANDSHAKE_TYPE_END_OF_EARLY_DATA) { |
5802 | 0 | ret = server_handle_end_of_early_data(tls, message); |
5803 | 0 | } else { |
5804 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5805 | 0 | } |
5806 | 0 | break; |
5807 | 41 | case PTLS_STATE_SERVER_EXPECT_FINISHED: |
5808 | 41 | if (type == PTLS_HANDSHAKE_TYPE_FINISHED && is_end_of_record) { |
5809 | 19 | ret = server_handle_finished(tls, message); |
5810 | 22 | } else { |
5811 | 22 | ret = PTLS_ALERT_HANDSHAKE_FAILURE; |
5812 | 22 | } |
5813 | 41 | break; |
5814 | 0 | case PTLS_STATE_SERVER_POST_HANDSHAKE: |
5815 | 0 | switch (type) { |
5816 | 0 | case PTLS_HANDSHAKE_TYPE_KEY_UPDATE: |
5817 | 0 | ret = handle_key_update(tls, emitter, message); |
5818 | 0 | break; |
5819 | 0 | default: |
5820 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5821 | 0 | break; |
5822 | 0 | } |
5823 | 0 | break; |
5824 | 0 | default: |
5825 | 0 | assert(!"unexpected state"); |
5826 | 0 | ret = PTLS_ALERT_INTERNAL_ERROR; |
5827 | 0 | break; |
5828 | 2.10k | } |
5829 | | |
5830 | 2.10k | PTLS_PROBE(RECEIVE_MESSAGE, tls, message.base[0], message.base + PTLS_HANDSHAKE_HEADER_SIZE, |
5831 | 2.10k | message.len - PTLS_HANDSHAKE_HEADER_SIZE, ret); |
5832 | 2.10k | PTLS_LOG_CONN(receive_message, tls, { |
5833 | 2.10k | PTLS_LOG_ELEMENT_UNSIGNED(message, message.base[0]); |
5834 | 2.10k | PTLS_LOG_ELEMENT_UNSIGNED(len, message.len - PTLS_HANDSHAKE_HEADER_SIZE); |
5835 | 2.10k | PTLS_LOG_ELEMENT_SIGNED(result, ret); |
5836 | 2.10k | }); |
5837 | | |
5838 | 2.10k | return ret; |
5839 | 2.10k | } |
5840 | | |
5841 | | static int handle_alert(ptls_t *tls, const uint8_t *src, size_t len) |
5842 | 50 | { |
5843 | 50 | if (len != 2) |
5844 | 18 | return PTLS_ALERT_DECODE_ERROR; |
5845 | | |
5846 | 32 | uint8_t desc = src[1]; |
5847 | | |
5848 | | /* all fatal alerts and USER_CANCELLED warning tears down the connection immediately, regardless of the transmitted level */ |
5849 | 32 | return PTLS_ALERT_TO_PEER_ERROR(desc); |
5850 | 50 | } |
5851 | | |
5852 | | static int message_buffer_is_overflow(ptls_context_t *ctx, size_t size) |
5853 | 7.50k | { |
5854 | 7.50k | if (ctx->max_buffer_size == 0) |
5855 | 7.50k | return 0; |
5856 | 0 | if (size <= ctx->max_buffer_size) |
5857 | 0 | return 0; |
5858 | 0 | return 1; |
5859 | 0 | } |
5860 | | |
5861 | | static int handle_handshake_record(ptls_t *tls, |
5862 | | int (*cb)(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_iovec_t message, |
5863 | | int is_end_of_record, ptls_handshake_properties_t *properties), |
5864 | | ptls_message_emitter_t *emitter, struct st_ptls_record_t *rec, |
5865 | | ptls_handshake_properties_t *properties) |
5866 | 27.3k | { |
5867 | 27.3k | int ret; |
5868 | | |
5869 | | /* handshake */ |
5870 | 27.3k | if (rec->type != PTLS_CONTENT_TYPE_HANDSHAKE) |
5871 | 19 | return PTLS_ALERT_DECODE_ERROR; |
5872 | | |
5873 | | /* flatten the unhandled messages */ |
5874 | 27.2k | const uint8_t *src, *src_end; |
5875 | 27.2k | if (tls->recvbuf.mess.base == NULL) { |
5876 | 23.6k | src = rec->fragment; |
5877 | 23.6k | src_end = src + rec->length; |
5878 | 23.6k | } else { |
5879 | 3.68k | if (message_buffer_is_overflow(tls->ctx, tls->recvbuf.mess.off + rec->length)) |
5880 | 0 | return PTLS_ALERT_HANDSHAKE_FAILURE; |
5881 | 3.68k | if ((ret = ptls_buffer_reserve(&tls->recvbuf.mess, rec->length)) != 0) |
5882 | 0 | return ret; |
5883 | 3.68k | memcpy(tls->recvbuf.mess.base + tls->recvbuf.mess.off, rec->fragment, rec->length); |
5884 | 3.68k | tls->recvbuf.mess.off += rec->length; |
5885 | 3.68k | src = tls->recvbuf.mess.base; |
5886 | 3.68k | src_end = src + tls->recvbuf.mess.off; |
5887 | 3.68k | } |
5888 | | |
5889 | | /* handle the messages */ |
5890 | 27.2k | ret = PTLS_ERROR_IN_PROGRESS; |
5891 | 27.6k | while (src_end - src >= 4) { |
5892 | 5.42k | size_t mess_len = 4 + ntoh24(src + 1); |
5893 | 5.42k | if (src_end - src < (int)mess_len) |
5894 | 3.32k | break; |
5895 | 2.10k | ret = cb(tls, emitter, ptls_iovec_init(src, mess_len), src_end - src == mess_len, properties); |
5896 | 2.10k | switch (ret) { |
5897 | 242 | case 0: |
5898 | 242 | case PTLS_ERROR_ASYNC_OPERATION: |
5899 | 396 | case PTLS_ERROR_IN_PROGRESS: |
5900 | 396 | break; |
5901 | 1.70k | default: |
5902 | 1.70k | ptls_buffer_dispose(&tls->recvbuf.mess); |
5903 | 1.70k | return ret; |
5904 | 2.10k | } |
5905 | 396 | src += mess_len; |
5906 | 396 | } |
5907 | | |
5908 | | /* keep last partial message in buffer */ |
5909 | 25.5k | if (src != src_end) { |
5910 | 3.81k | size_t new_size = src_end - src; |
5911 | 3.81k | if (message_buffer_is_overflow(tls->ctx, new_size)) |
5912 | 0 | return PTLS_ALERT_HANDSHAKE_FAILURE; |
5913 | 3.81k | if (tls->recvbuf.mess.base == NULL) { |
5914 | 230 | ptls_buffer_init(&tls->recvbuf.mess, "", 0); |
5915 | 230 | if ((ret = ptls_buffer_reserve(&tls->recvbuf.mess, new_size)) != 0) |
5916 | 0 | return ret; |
5917 | 230 | memcpy(tls->recvbuf.mess.base, src, new_size); |
5918 | 3.58k | } else { |
5919 | 3.58k | memmove(tls->recvbuf.mess.base, src, new_size); |
5920 | 3.58k | } |
5921 | 3.81k | tls->recvbuf.mess.off = new_size; |
5922 | 3.81k | ret = PTLS_ERROR_IN_PROGRESS; |
5923 | 21.7k | } else { |
5924 | 21.7k | ptls_buffer_dispose(&tls->recvbuf.mess); |
5925 | 21.7k | } |
5926 | | |
5927 | 25.5k | return ret; |
5928 | 25.5k | } |
5929 | | |
5930 | | static int handle_input(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_buffer_t *decryptbuf, const void *input, size_t *inlen, |
5931 | | ptls_handshake_properties_t *properties) |
5932 | 38.0k | { |
5933 | 38.0k | struct st_ptls_record_t rec; |
5934 | 38.0k | int ret; |
5935 | | |
5936 | | /* extract the record */ |
5937 | 38.0k | if ((ret = parse_record(tls, &rec, input, inlen)) != 0) |
5938 | 196 | return ret; |
5939 | 38.0k | assert(rec.fragment != NULL); |
5940 | | |
5941 | | /* decrypt the record */ |
5942 | 37.8k | if (rec.type == PTLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) { |
5943 | 422 | if (tls->state < PTLS_STATE_POST_HANDSHAKE_MIN) { |
5944 | 422 | if (!(rec.length == 1 && rec.fragment[0] == 0x01)) |
5945 | 28 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
5946 | 422 | } else { |
5947 | 0 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
5948 | 0 | } |
5949 | 394 | ret = PTLS_ERROR_IN_PROGRESS; |
5950 | 394 | goto NextRecord; |
5951 | 422 | } |
5952 | 37.4k | if (tls->traffic_protection.dec.aead != NULL && rec.type != PTLS_CONTENT_TYPE_ALERT) { |
5953 | 9.93k | size_t decrypted_length; |
5954 | 9.93k | if (rec.type != PTLS_CONTENT_TYPE_APPDATA) |
5955 | 9 | return PTLS_ALERT_HANDSHAKE_FAILURE; |
5956 | 9.92k | if ((ret = ptls_buffer_reserve(decryptbuf, 5 + rec.length)) != 0) |
5957 | 0 | return ret; |
5958 | 9.92k | if ((ret = aead_decrypt(&tls->traffic_protection.dec, decryptbuf->base + decryptbuf->off, &decrypted_length, rec.fragment, |
5959 | 9.92k | rec.length)) != 0) { |
5960 | 9.56k | if (tls->is_server && tls->server.early_data_skipped_bytes != UINT32_MAX) |
5961 | 9.55k | goto ServerSkipEarlyData; |
5962 | 9 | return ret; |
5963 | 9.56k | } |
5964 | 360 | rec.length = decrypted_length; |
5965 | 360 | rec.fragment = decryptbuf->base + decryptbuf->off; |
5966 | | /* skip padding */ |
5967 | 1.26k | for (; rec.length != 0; --rec.length) |
5968 | 1.25k | if (rec.fragment[rec.length - 1] != 0) |
5969 | 354 | break; |
5970 | 360 | if (rec.length == 0) |
5971 | 6 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
5972 | 354 | rec.type = rec.fragment[--rec.length]; |
5973 | 354 | if (rec.length == 0 && (rec.type == PTLS_CONTENT_TYPE_ALERT || rec.type == PTLS_CONTENT_TYPE_HANDSHAKE)) |
5974 | 3 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
5975 | 27.5k | } else if (rec.type == PTLS_CONTENT_TYPE_APPDATA && tls->is_server && tls->server.early_data_skipped_bytes != UINT32_MAX) { |
5976 | 450 | goto ServerSkipEarlyData; |
5977 | 450 | } |
5978 | | |
5979 | 27.4k | if (tls->recvbuf.mess.base != NULL || rec.type == PTLS_CONTENT_TYPE_HANDSHAKE) { |
5980 | | /* handshake record */ |
5981 | 27.3k | ret = handle_handshake_record(tls, tls->is_server ? handle_server_handshake_message : handle_client_handshake_message, |
5982 | 27.3k | emitter, &rec, properties); |
5983 | 27.3k | } else { |
5984 | | /* handling of an alert or an application record */ |
5985 | 92 | switch (rec.type) { |
5986 | 8 | case PTLS_CONTENT_TYPE_APPDATA: |
5987 | 8 | if (tls->state >= PTLS_STATE_POST_HANDSHAKE_MIN) { |
5988 | 0 | decryptbuf->off += rec.length; |
5989 | 0 | ret = 0; |
5990 | 8 | } else if (tls->state == PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA) { |
5991 | 0 | if (tls->traffic_protection.dec.aead != NULL) |
5992 | 0 | decryptbuf->off += rec.length; |
5993 | 0 | ret = 0; |
5994 | 8 | } else { |
5995 | 8 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
5996 | 8 | } |
5997 | 8 | break; |
5998 | 50 | case PTLS_CONTENT_TYPE_ALERT: |
5999 | 50 | ret = handle_alert(tls, rec.fragment, rec.length); |
6000 | 50 | break; |
6001 | 34 | default: |
6002 | 34 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
6003 | 34 | break; |
6004 | 92 | } |
6005 | 92 | } |
6006 | | |
6007 | 37.7k | NextRecord: |
6008 | 37.7k | ptls_buffer_dispose(&tls->recvbuf.rec); |
6009 | 37.7k | return ret; |
6010 | | |
6011 | 10.0k | ServerSkipEarlyData: |
6012 | 10.0k | tls->server.early_data_skipped_bytes += (uint32_t)rec.length; |
6013 | 10.0k | if (tls->server.early_data_skipped_bytes > PTLS_MAX_EARLY_DATA_SKIP_SIZE) |
6014 | 2 | return PTLS_ALERT_HANDSHAKE_FAILURE; |
6015 | 10.0k | ret = PTLS_ERROR_IN_PROGRESS; |
6016 | 10.0k | goto NextRecord; |
6017 | 10.0k | } |
6018 | | |
6019 | | static int handle_input_tls12(ptls_t *tls, ptls_buffer_t *decryptbuf, const void *input, size_t *inlen) |
6020 | 0 | { |
6021 | 0 | struct st_ptls_record_t rec; |
6022 | 0 | int ret; |
6023 | | |
6024 | | /* extract the record, or bail out */ |
6025 | 0 | if ((ret = parse_record(tls, &rec, input, inlen)) != 0) |
6026 | 0 | return ret; |
6027 | 0 | assert(rec.fragment != NULL); |
6028 | | |
6029 | 0 | const uint8_t *src = rec.fragment, *end = src + rec.length; |
6030 | 0 | uint64_t nonce; |
6031 | 0 | uint8_t aad[PTLS_TLS12_AAD_SIZE]; |
6032 | | |
6033 | | /* determine the nonce */ |
6034 | 0 | if (tls->traffic_protection.dec.aead->algo->tls12.record_iv_size != 0) { |
6035 | 0 | assert(tls->traffic_protection.dec.aead->algo->tls12.record_iv_size == 8); |
6036 | 0 | if ((ret = ptls_decode64(&nonce, &src, end)) != 0) |
6037 | 0 | goto Exit; |
6038 | 0 | } else { |
6039 | 0 | nonce = tls->traffic_protection.dec.seq; |
6040 | 0 | } |
6041 | | |
6042 | | /* determine cleartext length */ |
6043 | 0 | size_t textlen = end - src; |
6044 | 0 | if (textlen < tls->traffic_protection.dec.aead->algo->tag_size) { |
6045 | 0 | ret = PTLS_ALERT_BAD_RECORD_MAC; |
6046 | 0 | goto Exit; |
6047 | 0 | } |
6048 | 0 | textlen -= tls->traffic_protection.dec.aead->algo->tag_size; |
6049 | | |
6050 | | /* build aad */ |
6051 | 0 | build_tls12_aad(aad, rec.type, tls->traffic_protection.dec.seq, (uint16_t)textlen); |
6052 | | |
6053 | | /* decrypt input to decryptbuf */ |
6054 | 0 | if ((ret = ptls_buffer_reserve(decryptbuf, textlen)) != 0) |
6055 | 0 | goto Exit; |
6056 | 0 | if (ptls_aead_decrypt(tls->traffic_protection.dec.aead, decryptbuf->base + decryptbuf->off, src, end - src, nonce, aad, |
6057 | 0 | sizeof(aad)) != textlen) { |
6058 | 0 | ret = PTLS_ALERT_BAD_RECORD_MAC; |
6059 | 0 | goto Exit; |
6060 | 0 | } |
6061 | 0 | ++tls->traffic_protection.dec.seq; |
6062 | | |
6063 | | /* record-type specific action */ |
6064 | 0 | switch (rec.type) { |
6065 | 0 | case PTLS_CONTENT_TYPE_APPDATA: |
6066 | | /* if application data, retain the bytes being decrypted */ |
6067 | 0 | decryptbuf->off += textlen; |
6068 | 0 | break; |
6069 | 0 | case PTLS_CONTENT_TYPE_ALERT: |
6070 | | /* submit alert without adjusting decryptbuf, so that the decrypted data would be dropped after handling the alert */ |
6071 | 0 | ret = handle_alert(tls, decryptbuf->base + decryptbuf->off, textlen); |
6072 | 0 | break; |
6073 | 0 | default: |
6074 | 0 | ret = PTLS_ALERT_UNEXPECTED_MESSAGE; |
6075 | 0 | break; |
6076 | 0 | } |
6077 | | |
6078 | 0 | Exit: |
6079 | 0 | ptls_buffer_dispose(&tls->recvbuf.rec); |
6080 | 0 | ptls_clear_memory(aad, sizeof(aad)); |
6081 | 0 | return ret; |
6082 | 0 | } |
6083 | | |
6084 | | static void init_record_message_emitter(ptls_t *tls, struct st_ptls_record_message_emitter_t *emitter, ptls_buffer_t *sendbuf) |
6085 | 2.24k | { |
6086 | 2.24k | *emitter = (struct st_ptls_record_message_emitter_t){ |
6087 | 2.24k | {sendbuf, &tls->traffic_protection.enc, 5, begin_record_message, commit_record_message}}; |
6088 | 2.24k | } |
6089 | | |
6090 | | int ptls_handshake(ptls_t *tls, ptls_buffer_t *_sendbuf, const void *input, size_t *inlen, ptls_handshake_properties_t *properties) |
6091 | 2.24k | { |
6092 | 2.24k | struct st_ptls_record_message_emitter_t emitter; |
6093 | 2.24k | int ret; |
6094 | | |
6095 | 2.24k | assert(tls->state < PTLS_STATE_POST_HANDSHAKE_MIN); |
6096 | | |
6097 | 2.24k | init_record_message_emitter(tls, &emitter, _sendbuf); |
6098 | 2.24k | size_t sendbuf_orig_off = emitter.super.buf->off; |
6099 | | |
6100 | | /* special handlings */ |
6101 | 2.24k | switch (tls->state) { |
6102 | 0 | case PTLS_STATE_CLIENT_HANDSHAKE_START: { |
6103 | 0 | assert(input == NULL || *inlen == 0); |
6104 | 0 | return send_client_hello(tls, &emitter.super, properties, NULL); |
6105 | 0 | } |
6106 | 0 | case PTLS_STATE_SERVER_GENERATING_CERTIFICATE_VERIFY: |
6107 | 0 | return server_finish_handshake(tls, &emitter.super, 1, NULL); |
6108 | 2.24k | default: |
6109 | 2.24k | break; |
6110 | 2.24k | } |
6111 | | |
6112 | 2.24k | const uint8_t *src = input, *const src_end = src + *inlen; |
6113 | 2.24k | ptls_buffer_t decryptbuf; |
6114 | | |
6115 | 2.24k | ptls_buffer_init(&decryptbuf, "", 0); |
6116 | | |
6117 | | /* perform handhake until completion or until all the input has been swallowed */ |
6118 | 2.24k | ret = PTLS_ERROR_IN_PROGRESS; |
6119 | 30.1k | while (ret == PTLS_ERROR_IN_PROGRESS && src != src_end) { |
6120 | 27.8k | size_t consumed = src_end - src; |
6121 | 27.8k | ret = handle_input(tls, &emitter.super, &decryptbuf, src, &consumed, properties); |
6122 | 27.8k | src += consumed; |
6123 | 27.8k | assert(decryptbuf.off == 0); |
6124 | 27.8k | } |
6125 | | |
6126 | 2.24k | ptls_buffer_dispose(&decryptbuf); |
6127 | | |
6128 | 2.24k | switch (ret) { |
6129 | 242 | case 0: |
6130 | 478 | case PTLS_ERROR_IN_PROGRESS: |
6131 | 478 | case PTLS_ERROR_STATELESS_RETRY: |
6132 | 478 | case PTLS_ERROR_ASYNC_OPERATION: |
6133 | 478 | break; |
6134 | 1.77k | default: |
6135 | | /* Flush handshake messages that have been written partially. ECH_REQUIRED sticks out because it is a message sent |
6136 | | * post-handshake compared to other alerts that are generating *during* the handshake. */ |
6137 | 1.77k | if (ret != PTLS_ALERT_ECH_REQUIRED) { |
6138 | 1.77k | ptls_clear_memory(emitter.super.buf->base + sendbuf_orig_off, emitter.super.buf->off - sendbuf_orig_off); |
6139 | 1.77k | emitter.super.buf->off = sendbuf_orig_off; |
6140 | 1.77k | } |
6141 | | /* send alert immediately */ |
6142 | 1.77k | if (PTLS_ERROR_GET_CLASS(ret) != PTLS_ERROR_CLASS_PEER_ALERT) |
6143 | 1.75k | if (ptls_send_alert(tls, emitter.super.buf, PTLS_ALERT_LEVEL_FATAL, |
6144 | 1.75k | PTLS_ERROR_GET_CLASS(ret) == PTLS_ERROR_CLASS_SELF_ALERT ? ret : PTLS_ALERT_INTERNAL_ERROR) != 0) |
6145 | 0 | emitter.super.buf->off = sendbuf_orig_off; |
6146 | 1.77k | break; |
6147 | 2.24k | } |
6148 | | |
6149 | 2.24k | *inlen -= src_end - src; |
6150 | 2.24k | return ret; |
6151 | 2.24k | } |
6152 | | |
6153 | | int ptls_receive(ptls_t *tls, ptls_buffer_t *decryptbuf, const void *_input, size_t *inlen) |
6154 | 218 | { |
6155 | 218 | const uint8_t *input = (const uint8_t *)_input, *const end = input + *inlen; |
6156 | 218 | size_t decryptbuf_orig_size = decryptbuf->off; |
6157 | 218 | int ret = 0; |
6158 | | |
6159 | 218 | assert(tls->state >= PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA); |
6160 | | |
6161 | | /* loop until we decrypt some application data (or an error) */ |
6162 | 10.4k | while (ret == 0 && input != end && decryptbuf_orig_size == decryptbuf->off) { |
6163 | 10.1k | size_t consumed = end - input; |
6164 | 10.1k | if (tls->traffic_protection.dec.tls12) { |
6165 | 0 | ret = handle_input_tls12(tls, decryptbuf, input, &consumed); |
6166 | 10.1k | } else { |
6167 | 10.1k | ret = handle_input(tls, NULL, decryptbuf, input, &consumed, NULL); |
6168 | 10.1k | } |
6169 | 10.1k | input += consumed; |
6170 | | |
6171 | 10.1k | switch (ret) { |
6172 | 0 | case 0: |
6173 | 0 | break; |
6174 | 10.0k | case PTLS_ERROR_IN_PROGRESS: |
6175 | 10.0k | ret = 0; |
6176 | 10.0k | break; |
6177 | 1 | case PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY: |
6178 | | /* TODO send close alert */ |
6179 | 1 | break; |
6180 | 169 | default: |
6181 | 169 | if (PTLS_ERROR_GET_CLASS(ret) == PTLS_ERROR_CLASS_SELF_ALERT) { |
6182 | | /* TODO send alert */ |
6183 | 156 | } |
6184 | 169 | break; |
6185 | 10.1k | } |
6186 | 10.1k | } |
6187 | | |
6188 | 218 | *inlen -= end - input; |
6189 | | |
6190 | 218 | return ret; |
6191 | 218 | } |
6192 | | |
6193 | | static int update_send_key(ptls_t *tls, ptls_buffer_t *_sendbuf, int request_update) |
6194 | 0 | { |
6195 | 0 | struct st_ptls_record_message_emitter_t emitter; |
6196 | 0 | int ret; |
6197 | |
|
6198 | 0 | init_record_message_emitter(tls, &emitter, _sendbuf); |
6199 | 0 | size_t sendbuf_orig_off = emitter.super.buf->off; |
6200 | |
|
6201 | 0 | ptls_push_message(&emitter.super, NULL, PTLS_HANDSHAKE_TYPE_KEY_UPDATE, |
6202 | 0 | { ptls_buffer_push(emitter.super.buf, !!request_update); }); |
6203 | 0 | if ((ret = update_traffic_key(tls, 1)) != 0) |
6204 | 0 | goto Exit; |
6205 | 0 | ret = 0; |
6206 | |
|
6207 | 0 | Exit: |
6208 | 0 | if (ret != 0) |
6209 | 0 | emitter.super.buf->off = sendbuf_orig_off; |
6210 | 0 | return ret; |
6211 | 0 | } |
6212 | | |
6213 | | int ptls_send(ptls_t *tls, ptls_buffer_t *sendbuf, const void *input, size_t inlen) |
6214 | 0 | { |
6215 | 0 | if (!(tls->traffic_protection.enc.aead != NULL && |
6216 | 0 | (tls->traffic_protection.enc.tls12 || tls->traffic_protection.enc.epoch == 1 || |
6217 | 0 | tls->traffic_protection.enc.epoch == 3))) |
6218 | 0 | return PTLS_ERROR_IN_PROGRESS; |
6219 | | |
6220 | | /* "For AES-GCM, up to 2^24.5 full-size records (about 24 million) may be encrypted on a given connection while keeping a |
6221 | | * safety margin of approximately 2^-57 for Authenticated Encryption (AE) security." (RFC 8446 section 5.5). |
6222 | | * |
6223 | | * Key updates do not happen with tls 1.2, check `key_schedule` to see if we are using tls/1.3 |
6224 | | */ |
6225 | 0 | if (tls->traffic_protection.enc.seq >= 16777216 && tls->key_schedule != NULL) |
6226 | 0 | tls->needs_key_update = 1; |
6227 | |
|
6228 | 0 | if (tls->needs_key_update) { |
6229 | 0 | int ret; |
6230 | 0 | if ((ret = update_send_key(tls, sendbuf, tls->key_update_send_request)) != 0) |
6231 | 0 | return ret; |
6232 | 0 | tls->needs_key_update = 0; |
6233 | 0 | tls->key_update_send_request = 0; |
6234 | 0 | } |
6235 | | |
6236 | 0 | return buffer_push_encrypted_records(sendbuf, PTLS_CONTENT_TYPE_APPDATA, input, inlen, &tls->traffic_protection.enc); |
6237 | 0 | } |
6238 | | |
6239 | | int ptls_update_key(ptls_t *tls, int request_update) |
6240 | 0 | { |
6241 | 0 | assert(tls->ctx->update_traffic_key == NULL); |
6242 | 0 | tls->needs_key_update = 1; |
6243 | 0 | tls->key_update_send_request = request_update; |
6244 | 0 | return 0; |
6245 | 0 | } |
6246 | | |
6247 | | size_t ptls_get_record_overhead(ptls_t *tls) |
6248 | 0 | { |
6249 | 0 | ptls_aead_algorithm_t *algo = tls->traffic_protection.enc.aead->algo; |
6250 | |
|
6251 | 0 | if (tls->traffic_protection.enc.tls12) { |
6252 | 0 | return 5 + algo->tls12.record_iv_size + algo->tag_size; |
6253 | 0 | } else { |
6254 | 0 | return 6 + algo->tag_size; |
6255 | 0 | } |
6256 | 0 | } |
6257 | | |
6258 | | int ptls_send_alert(ptls_t *tls, ptls_buffer_t *sendbuf, uint8_t level, uint8_t description) |
6259 | 1.75k | { |
6260 | 1.75k | size_t rec_start = sendbuf->off; |
6261 | 1.75k | int ret = 0; |
6262 | | |
6263 | 1.75k | buffer_push_record(sendbuf, PTLS_CONTENT_TYPE_ALERT, { ptls_buffer_push(sendbuf, level, description); }); |
6264 | | /* encrypt the alert if we have the encryption keys, unless when it is the early data key */ |
6265 | 1.75k | if (tls->traffic_protection.enc.aead != NULL && !(tls->state <= PTLS_STATE_CLIENT_EXPECT_FINISHED)) { |
6266 | 14 | if ((ret = buffer_encrypt_record(sendbuf, rec_start, &tls->traffic_protection.enc)) != 0) |
6267 | 0 | goto Exit; |
6268 | 14 | } |
6269 | | |
6270 | 1.75k | Exit: |
6271 | 1.75k | return ret; |
6272 | 1.75k | } |
6273 | | |
6274 | | int ptls_export_secret(ptls_t *tls, void *output, size_t outlen, const char *label, ptls_iovec_t context_value, int is_early) |
6275 | 0 | { |
6276 | 0 | ptls_hash_algorithm_t *algo = tls->key_schedule->hashes[0].algo; |
6277 | 0 | uint8_t *master_secret = is_early ? tls->exporter_master_secret.early : tls->exporter_master_secret.one_rtt, |
6278 | 0 | derived_secret[PTLS_MAX_DIGEST_SIZE], context_value_hash[PTLS_MAX_DIGEST_SIZE]; |
6279 | 0 | int ret; |
6280 | |
|
6281 | 0 | if (master_secret == NULL) { |
6282 | 0 | if (is_early) { |
6283 | 0 | switch (tls->state) { |
6284 | 0 | case PTLS_STATE_CLIENT_HANDSHAKE_START: |
6285 | 0 | case PTLS_STATE_SERVER_EXPECT_CLIENT_HELLO: |
6286 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
6287 | 0 | break; |
6288 | 0 | default: |
6289 | 0 | ret = PTLS_ERROR_NOT_AVAILABLE; |
6290 | 0 | break; |
6291 | 0 | } |
6292 | 0 | } else { |
6293 | 0 | ret = PTLS_ERROR_IN_PROGRESS; |
6294 | 0 | } |
6295 | 0 | return ret; |
6296 | 0 | } |
6297 | | |
6298 | 0 | if ((ret = ptls_calc_hash(algo, context_value_hash, context_value.base, context_value.len)) != 0) |
6299 | 0 | return ret; |
6300 | | |
6301 | 0 | if ((ret = ptls_hkdf_expand_label(algo, derived_secret, algo->digest_size, ptls_iovec_init(master_secret, algo->digest_size), |
6302 | 0 | label, ptls_iovec_init(algo->empty_digest, algo->digest_size), NULL)) != 0) |
6303 | 0 | goto Exit; |
6304 | 0 | ret = ptls_hkdf_expand_label(algo, output, outlen, ptls_iovec_init(derived_secret, algo->digest_size), "exporter", |
6305 | 0 | ptls_iovec_init(context_value_hash, algo->digest_size), NULL); |
6306 | |
|
6307 | 0 | Exit: |
6308 | 0 | ptls_clear_memory(derived_secret, sizeof(derived_secret)); |
6309 | 0 | ptls_clear_memory(context_value_hash, sizeof(context_value_hash)); |
6310 | 0 | return ret; |
6311 | 0 | } |
6312 | | |
6313 | | struct st_picotls_hmac_context_t { |
6314 | | ptls_hash_context_t super; |
6315 | | ptls_hash_algorithm_t *algo; |
6316 | | ptls_hash_context_t *hash; |
6317 | | uint8_t key[1]; |
6318 | | }; |
6319 | | |
6320 | | static void hmac_update(ptls_hash_context_t *_ctx, const void *src, size_t len) |
6321 | 9.47k | { |
6322 | 9.47k | struct st_picotls_hmac_context_t *ctx = (struct st_picotls_hmac_context_t *)_ctx; |
6323 | 9.47k | ctx->hash->update(ctx->hash, src, len); |
6324 | 9.47k | } |
6325 | | |
6326 | | static void hmac_apply_key(struct st_picotls_hmac_context_t *ctx, uint8_t pad) |
6327 | 14.9k | { |
6328 | 14.9k | size_t i; |
6329 | | |
6330 | 1.56M | for (i = 0; i != ctx->algo->block_size; ++i) |
6331 | 1.55M | ctx->key[i] ^= pad; |
6332 | 14.9k | ctx->hash->update(ctx->hash, ctx->key, ctx->algo->block_size); |
6333 | 1.56M | for (i = 0; i != ctx->algo->block_size; ++i) |
6334 | 1.55M | ctx->key[i] ^= pad; |
6335 | 14.9k | } |
6336 | | |
6337 | | static void hmac_final(ptls_hash_context_t *_ctx, void *md, ptls_hash_final_mode_t mode) |
6338 | 9.47k | { |
6339 | 9.47k | struct st_picotls_hmac_context_t *ctx = (struct st_picotls_hmac_context_t *)_ctx; |
6340 | | |
6341 | 9.47k | assert(mode != PTLS_HASH_FINAL_MODE_SNAPSHOT || !"not supported"); |
6342 | | |
6343 | 9.47k | if (md != NULL) { |
6344 | 5.49k | ctx->hash->final(ctx->hash, md, PTLS_HASH_FINAL_MODE_RESET); |
6345 | 5.49k | hmac_apply_key(ctx, 0x5c); |
6346 | 5.49k | ctx->hash->update(ctx->hash, md, ctx->algo->digest_size); |
6347 | 5.49k | } |
6348 | 9.47k | ctx->hash->final(ctx->hash, md, mode); |
6349 | | |
6350 | 9.47k | switch (mode) { |
6351 | 5.49k | case PTLS_HASH_FINAL_MODE_FREE: |
6352 | 5.49k | ptls_clear_memory(ctx->key, ctx->algo->block_size); |
6353 | 5.49k | free(ctx); |
6354 | 5.49k | break; |
6355 | 3.98k | case PTLS_HASH_FINAL_MODE_RESET: |
6356 | 3.98k | hmac_apply_key(ctx, 0x36); |
6357 | 3.98k | break; |
6358 | 0 | default: |
6359 | 0 | assert(!"FIXME"); |
6360 | 0 | break; |
6361 | 9.47k | } |
6362 | 9.47k | } |
6363 | | |
6364 | | int ptls_calc_hash(ptls_hash_algorithm_t *algo, void *output, const void *src, size_t len) |
6365 | 0 | { |
6366 | 0 | ptls_hash_context_t *ctx; |
6367 | |
|
6368 | 0 | if ((ctx = algo->create()) == NULL) |
6369 | 0 | return PTLS_ERROR_NO_MEMORY; |
6370 | 0 | ctx->update(ctx, src, len); |
6371 | 0 | ctx->final(ctx, output, PTLS_HASH_FINAL_MODE_FREE); |
6372 | 0 | return 0; |
6373 | 0 | } |
6374 | | |
6375 | | ptls_hash_context_t *ptls_hmac_create(ptls_hash_algorithm_t *algo, const void *key, size_t key_size) |
6376 | 5.49k | { |
6377 | 5.49k | struct st_picotls_hmac_context_t *ctx; |
6378 | | |
6379 | 5.49k | assert(key_size <= algo->block_size); |
6380 | | |
6381 | 5.49k | if ((ctx = malloc(offsetof(struct st_picotls_hmac_context_t, key) + algo->block_size)) == NULL) |
6382 | 0 | return NULL; |
6383 | | |
6384 | 5.49k | *ctx = (struct st_picotls_hmac_context_t){{hmac_update, hmac_final}, algo}; |
6385 | 5.49k | if ((ctx->hash = algo->create()) == NULL) { |
6386 | 0 | free(ctx); |
6387 | 0 | return NULL; |
6388 | 0 | } |
6389 | 5.49k | memset(ctx->key, 0, algo->block_size); |
6390 | 5.49k | memcpy(ctx->key, key, key_size); |
6391 | | |
6392 | 5.49k | hmac_apply_key(ctx, 0x36); |
6393 | | |
6394 | 5.49k | return &ctx->super; |
6395 | 5.49k | } |
6396 | | |
6397 | | int ptls_hkdf_extract(ptls_hash_algorithm_t *algo, void *output, ptls_iovec_t salt, ptls_iovec_t ikm) |
6398 | 1.01k | { |
6399 | 1.01k | ptls_hash_context_t *hash; |
6400 | | |
6401 | 1.01k | if (salt.len == 0) |
6402 | 0 | salt = ptls_iovec_init(zeroes_of_max_digest_size, algo->digest_size); |
6403 | | |
6404 | 1.01k | if ((hash = ptls_hmac_create(algo, salt.base, salt.len)) == NULL) |
6405 | 0 | return PTLS_ERROR_NO_MEMORY; |
6406 | 1.01k | hash->update(hash, ikm.base, ikm.len); |
6407 | 1.01k | hash->final(hash, output, PTLS_HASH_FINAL_MODE_FREE); |
6408 | 1.01k | return 0; |
6409 | 1.01k | } |
6410 | | |
6411 | | int ptls_hkdf_expand(ptls_hash_algorithm_t *algo, void *output, size_t outlen, ptls_iovec_t prk, ptls_iovec_t info) |
6412 | 3.98k | { |
6413 | 3.98k | ptls_hash_context_t *hmac = NULL; |
6414 | 3.98k | size_t i; |
6415 | 3.98k | uint8_t digest[PTLS_MAX_DIGEST_SIZE]; |
6416 | | |
6417 | 7.97k | for (i = 0; (i * algo->digest_size) < outlen; ++i) { |
6418 | 3.98k | if (hmac == NULL) { |
6419 | 3.98k | if ((hmac = ptls_hmac_create(algo, prk.base, prk.len)) == NULL) |
6420 | 0 | return PTLS_ERROR_NO_MEMORY; |
6421 | 3.98k | } else { |
6422 | 0 | hmac->update(hmac, digest, algo->digest_size); |
6423 | 0 | } |
6424 | 3.98k | hmac->update(hmac, info.base, info.len); |
6425 | 3.98k | uint8_t gen = (uint8_t)(i + 1); |
6426 | 3.98k | hmac->update(hmac, &gen, 1); |
6427 | 3.98k | hmac->final(hmac, digest, 1); |
6428 | | |
6429 | 3.98k | size_t off_start = i * algo->digest_size, off_end = off_start + algo->digest_size; |
6430 | 3.98k | if (off_end > outlen) |
6431 | 1.50k | off_end = outlen; |
6432 | 3.98k | memcpy((uint8_t *)output + off_start, digest, off_end - off_start); |
6433 | 3.98k | } |
6434 | | |
6435 | 3.98k | if (hmac != NULL) |
6436 | 3.98k | hmac->final(hmac, NULL, PTLS_HASH_FINAL_MODE_FREE); |
6437 | | |
6438 | 3.98k | ptls_clear_memory(digest, algo->digest_size); |
6439 | | |
6440 | 3.98k | return 0; |
6441 | 3.98k | } |
6442 | | |
6443 | | int ptls_hkdf_expand_label(ptls_hash_algorithm_t *algo, void *output, size_t outlen, ptls_iovec_t secret, const char *label, |
6444 | | ptls_iovec_t hash_value, const char *label_prefix) |
6445 | 3.98k | { |
6446 | 3.98k | ptls_buffer_t hkdf_label; |
6447 | 3.98k | uint8_t hkdf_label_buf[80]; |
6448 | 3.98k | int ret; |
6449 | | |
6450 | 3.98k | ptls_buffer_init(&hkdf_label, hkdf_label_buf, sizeof(hkdf_label_buf)); |
6451 | | |
6452 | 3.98k | ptls_buffer_push16(&hkdf_label, (uint16_t)outlen); |
6453 | 3.98k | ptls_buffer_push_block(&hkdf_label, 1, { |
6454 | 3.98k | if (label_prefix == NULL) |
6455 | 3.98k | label_prefix = PTLS_HKDF_EXPAND_LABEL_PREFIX; |
6456 | 3.98k | ptls_buffer_pushv(&hkdf_label, label_prefix, strlen(label_prefix)); |
6457 | 3.98k | ptls_buffer_pushv(&hkdf_label, label, strlen(label)); |
6458 | 3.98k | }); |
6459 | 3.98k | ptls_buffer_push_block(&hkdf_label, 1, { ptls_buffer_pushv(&hkdf_label, hash_value.base, hash_value.len); }); |
6460 | | |
6461 | 3.98k | ret = ptls_hkdf_expand(algo, output, outlen, secret, ptls_iovec_init(hkdf_label.base, hkdf_label.off)); |
6462 | | |
6463 | 3.98k | Exit: |
6464 | 3.98k | ptls_buffer_dispose(&hkdf_label); |
6465 | 3.98k | return ret; |
6466 | 3.98k | } |
6467 | | |
6468 | | int ptls_tls12_phash(ptls_hash_algorithm_t *algo, void *output, size_t outlen, ptls_iovec_t secret, const char *label, |
6469 | | ptls_iovec_t seed) |
6470 | 0 | { |
6471 | 0 | ptls_hash_context_t *hmac; |
6472 | 0 | uint8_t An[PTLS_MAX_DIGEST_SIZE]; |
6473 | 0 | size_t output_off = 0; |
6474 | |
|
6475 | 0 | if ((hmac = ptls_hmac_create(algo, secret.base, secret.len)) == NULL) |
6476 | 0 | return PTLS_ERROR_NO_MEMORY; |
6477 | | |
6478 | | /* A(1) = HMAC_hash(secret, label + seed) */ |
6479 | 0 | if (label != NULL) |
6480 | 0 | hmac->update(hmac, label, strlen(label)); |
6481 | 0 | hmac->update(hmac, seed.base, seed.len); |
6482 | 0 | hmac->final(hmac, An, PTLS_HASH_FINAL_MODE_RESET); |
6483 | |
|
6484 | 0 | while (1) { |
6485 | | /* output += HMAC_hash(secret, A(i) + label + seed) */ |
6486 | 0 | hmac->update(hmac, An, algo->digest_size); |
6487 | 0 | if (label != NULL) |
6488 | 0 | hmac->update(hmac, label, strlen(label)); |
6489 | 0 | hmac->update(hmac, seed.base, seed.len); |
6490 | 0 | if (outlen - output_off <= algo->digest_size) { |
6491 | | /* digest of last chunk is at first written to An then the necessary bytes are copied to output */ |
6492 | 0 | hmac->final(hmac, An, PTLS_HASH_FINAL_MODE_FREE); |
6493 | 0 | memcpy((uint8_t *)output + output_off, An, outlen - output_off); |
6494 | 0 | break; |
6495 | 0 | } |
6496 | 0 | hmac->final(hmac, (uint8_t *)output + output_off, PTLS_HASH_FINAL_MODE_RESET); |
6497 | 0 | output_off += algo->digest_size; |
6498 | | |
6499 | | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
6500 | 0 | hmac->update(hmac, An, algo->digest_size); |
6501 | 0 | hmac->final(hmac, An, PTLS_HASH_FINAL_MODE_RESET); |
6502 | 0 | } |
6503 | |
|
6504 | 0 | ptls_clear_memory(An, algo->digest_size); |
6505 | |
|
6506 | 0 | return 0; |
6507 | 0 | } |
6508 | | |
6509 | | ptls_cipher_context_t *ptls_cipher_new(ptls_cipher_algorithm_t *algo, int is_enc, const void *key) |
6510 | 0 | { |
6511 | 0 | ptls_cipher_context_t *ctx; |
6512 | |
|
6513 | 0 | if ((ctx = (ptls_cipher_context_t *)malloc(algo->context_size)) == NULL) |
6514 | 0 | return NULL; |
6515 | 0 | *ctx = (ptls_cipher_context_t){algo}; |
6516 | 0 | if (algo->setup_crypto(ctx, is_enc, key) != 0) { |
6517 | 0 | free(ctx); |
6518 | 0 | ctx = NULL; |
6519 | 0 | } |
6520 | 0 | return ctx; |
6521 | 0 | } |
6522 | | |
6523 | | void ptls_cipher_free(ptls_cipher_context_t *ctx) |
6524 | 0 | { |
6525 | 0 | ctx->do_dispose(ctx); |
6526 | 0 | free(ctx); |
6527 | 0 | } |
6528 | | |
6529 | | ptls_aead_context_t *new_aead(ptls_aead_algorithm_t *aead, ptls_hash_algorithm_t *hash, int is_enc, const void *secret, |
6530 | | ptls_iovec_t hash_value, const char *label_prefix) |
6531 | 754 | { |
6532 | 754 | ptls_aead_context_t *ctx = NULL; |
6533 | 754 | struct { |
6534 | 754 | uint8_t key[PTLS_MAX_SECRET_SIZE]; |
6535 | 754 | uint8_t iv[PTLS_MAX_IV_SIZE]; |
6536 | 754 | } key_iv; |
6537 | 754 | int ret; |
6538 | | |
6539 | 754 | if ((ret = get_traffic_keys(aead, hash, key_iv.key, key_iv.iv, secret, hash_value, label_prefix)) != 0) |
6540 | 0 | goto Exit; |
6541 | 754 | ctx = ptls_aead_new_direct(aead, is_enc, key_iv.key, key_iv.iv); |
6542 | 754 | Exit: |
6543 | 754 | ptls_clear_memory(&key_iv, sizeof(key_iv)); |
6544 | 754 | return ctx; |
6545 | 754 | } |
6546 | | |
6547 | | ptls_aead_context_t *ptls_aead_new(ptls_aead_algorithm_t *aead, ptls_hash_algorithm_t *hash, int is_enc, const void *secret, |
6548 | | const char *label_prefix) |
6549 | 754 | { |
6550 | 754 | return new_aead(aead, hash, is_enc, secret, ptls_iovec_init(NULL, 0), label_prefix); |
6551 | 754 | } |
6552 | | |
6553 | | ptls_aead_context_t *ptls_aead_new_direct(ptls_aead_algorithm_t *aead, int is_enc, const void *key, const void *iv) |
6554 | 754 | { |
6555 | 754 | ptls_aead_context_t *ctx; |
6556 | | |
6557 | 754 | if ((ctx = (ptls_aead_context_t *)malloc(aead->context_size)) == NULL) |
6558 | 0 | return NULL; |
6559 | | |
6560 | 754 | *ctx = (ptls_aead_context_t){aead}; |
6561 | | |
6562 | 754 | if (aead->setup_crypto(ctx, is_enc, key, iv) != 0) { |
6563 | 0 | free(ctx); |
6564 | 0 | return NULL; |
6565 | 0 | } |
6566 | | |
6567 | 754 | return ctx; |
6568 | 754 | } |
6569 | | |
6570 | | void ptls_aead_free(ptls_aead_context_t *ctx) |
6571 | 754 | { |
6572 | 754 | ctx->dispose_crypto(ctx); |
6573 | 754 | free(ctx); |
6574 | 754 | } |
6575 | | |
6576 | | void ptls_aead_xor_iv(ptls_aead_context_t *ctx, const void *_bytes, size_t len) |
6577 | 0 | { |
6578 | 0 | const uint8_t *bytes = _bytes; |
6579 | 0 | uint8_t iv[PTLS_MAX_IV_SIZE]; |
6580 | |
|
6581 | 0 | ptls_aead_get_iv(ctx, iv); |
6582 | 0 | for (size_t i = 0; i < len; ++i) |
6583 | 0 | iv[i] ^= bytes[i]; |
6584 | 0 | ptls_aead_set_iv(ctx, iv); |
6585 | 0 | } |
6586 | | |
6587 | | void ptls_aead__build_iv(ptls_aead_algorithm_t *algo, uint8_t *iv, const uint8_t *static_iv, uint64_t seq) |
6588 | 0 | { |
6589 | 0 | size_t iv_size = algo->iv_size, i; |
6590 | 0 | const uint8_t *s = static_iv; |
6591 | 0 | uint8_t *d = iv; |
6592 | | |
6593 | | /* build iv */ |
6594 | 0 | for (i = iv_size - 8; i != 0; --i) |
6595 | 0 | *d++ = *s++; |
6596 | 0 | i = 64; |
6597 | 0 | do { |
6598 | 0 | i -= 8; |
6599 | 0 | *d++ = *s++ ^ (uint8_t)(seq >> i); |
6600 | 0 | } while (i != 0); |
6601 | 0 | } |
6602 | | |
6603 | | static void clear_memory(void *p, size_t len) |
6604 | 107k | { |
6605 | 107k | if (len != 0) |
6606 | 33.3k | memset(p, 0, len); |
6607 | 107k | } |
6608 | | |
6609 | | void (*volatile ptls_clear_memory)(void *p, size_t len) = clear_memory; |
6610 | | |
6611 | | static int mem_equal(const void *_x, const void *_y, size_t len) |
6612 | 48 | { |
6613 | 48 | const volatile uint8_t *x = _x, *y = _y; |
6614 | 48 | uint8_t t = 0; |
6615 | | |
6616 | 1.61k | for (; len != 0; --len) |
6617 | 1.56k | t |= *x++ ^ *y++; |
6618 | | |
6619 | 48 | return t == 0; |
6620 | 48 | } |
6621 | | |
6622 | | int (*volatile ptls_mem_equal)(const void *x, const void *y, size_t len) = mem_equal; |
6623 | | |
6624 | | static uint64_t get_time(ptls_get_time_t *self) |
6625 | 361 | { |
6626 | 361 | struct timeval tv; |
6627 | 361 | gettimeofday(&tv, NULL); |
6628 | 361 | return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; |
6629 | 361 | } |
6630 | | |
6631 | | ptls_get_time_t ptls_get_time = {get_time}; |
6632 | | |
6633 | | int ptls_is_server(ptls_t *tls) |
6634 | 754 | { |
6635 | 754 | return tls->is_server; |
6636 | 754 | } |
6637 | | |
6638 | | struct st_ptls_raw_message_emitter_t { |
6639 | | ptls_message_emitter_t super; |
6640 | | size_t start_off; |
6641 | | size_t *epoch_offsets; |
6642 | | }; |
6643 | | |
6644 | | static int begin_raw_message(ptls_message_emitter_t *_self) |
6645 | 0 | { |
6646 | 0 | struct st_ptls_raw_message_emitter_t *self = (void *)_self; |
6647 | |
|
6648 | 0 | self->start_off = self->super.buf->off; |
6649 | 0 | return 0; |
6650 | 0 | } |
6651 | | |
6652 | | static int commit_raw_message(ptls_message_emitter_t *_self) |
6653 | 0 | { |
6654 | 0 | struct st_ptls_raw_message_emitter_t *self = (void *)_self; |
6655 | 0 | size_t epoch; |
6656 | | |
6657 | | /* epoch is the key epoch, with the only exception being 2nd CH generated after 0-RTT key */ |
6658 | 0 | epoch = self->super.enc->epoch; |
6659 | 0 | if (epoch == 1 && self->super.buf->base[self->start_off] == PTLS_HANDSHAKE_TYPE_CLIENT_HELLO) |
6660 | 0 | epoch = 0; |
6661 | |
|
6662 | 0 | for (++epoch; epoch < 5; ++epoch) { |
6663 | 0 | assert(self->epoch_offsets[epoch] == self->start_off); |
6664 | 0 | self->epoch_offsets[epoch] = self->super.buf->off; |
6665 | 0 | } |
6666 | | |
6667 | 0 | self->start_off = SIZE_MAX; |
6668 | |
|
6669 | 0 | return 0; |
6670 | 0 | } |
6671 | | |
6672 | | size_t ptls_get_read_epoch(ptls_t *tls) |
6673 | 0 | { |
6674 | 0 | switch (tls->state) { |
6675 | 0 | case PTLS_STATE_CLIENT_HANDSHAKE_START: |
6676 | 0 | case PTLS_STATE_CLIENT_EXPECT_SERVER_HELLO: |
6677 | 0 | case PTLS_STATE_CLIENT_EXPECT_SECOND_SERVER_HELLO: |
6678 | 0 | case PTLS_STATE_SERVER_EXPECT_CLIENT_HELLO: |
6679 | 0 | case PTLS_STATE_SERVER_EXPECT_SECOND_CLIENT_HELLO: |
6680 | 0 | return 0; /* plaintext */ |
6681 | 0 | case PTLS_STATE_SERVER_EXPECT_END_OF_EARLY_DATA: |
6682 | 0 | assert(!tls->ctx->omit_end_of_early_data); |
6683 | 0 | return 1; /* 0-rtt */ |
6684 | 0 | case PTLS_STATE_CLIENT_EXPECT_ENCRYPTED_EXTENSIONS: |
6685 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE: |
6686 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE: |
6687 | 0 | case PTLS_STATE_CLIENT_EXPECT_CERTIFICATE_VERIFY: |
6688 | 0 | case PTLS_STATE_CLIENT_EXPECT_FINISHED: |
6689 | 0 | case PTLS_STATE_SERVER_GENERATING_CERTIFICATE_VERIFY: |
6690 | 0 | case PTLS_STATE_SERVER_EXPECT_CERTIFICATE: |
6691 | 0 | case PTLS_STATE_SERVER_EXPECT_CERTIFICATE_VERIFY: |
6692 | 0 | case PTLS_STATE_SERVER_EXPECT_FINISHED: |
6693 | 0 | return 2; /* handshake */ |
6694 | 0 | case PTLS_STATE_CLIENT_POST_HANDSHAKE: |
6695 | 0 | case PTLS_STATE_SERVER_POST_HANDSHAKE: |
6696 | 0 | return 3; /* 1-rtt */ |
6697 | 0 | default: |
6698 | 0 | assert(!"invalid state"); |
6699 | 0 | return SIZE_MAX; |
6700 | 0 | } |
6701 | 0 | } |
6702 | | |
6703 | | int ptls_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input, |
6704 | | size_t inlen, ptls_handshake_properties_t *properties) |
6705 | 0 | { |
6706 | 0 | return tls->is_server ? ptls_server_handle_message(tls, sendbuf, epoch_offsets, in_epoch, input, inlen, properties) |
6707 | 0 | : ptls_client_handle_message(tls, sendbuf, epoch_offsets, in_epoch, input, inlen, properties); |
6708 | 0 | } |
6709 | | |
6710 | | int ptls_client_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input, |
6711 | | size_t inlen, ptls_handshake_properties_t *properties) |
6712 | 0 | { |
6713 | 0 | assert(!tls->is_server); |
6714 | | |
6715 | 0 | struct st_ptls_raw_message_emitter_t emitter = { |
6716 | 0 | {sendbuf, &tls->traffic_protection.enc, 0, begin_raw_message, commit_raw_message}, SIZE_MAX, epoch_offsets}; |
6717 | 0 | struct st_ptls_record_t rec = {PTLS_CONTENT_TYPE_HANDSHAKE, 0, inlen, input}; |
6718 | |
|
6719 | 0 | if (input == NULL) |
6720 | 0 | return send_client_hello(tls, &emitter.super, properties, NULL); |
6721 | | |
6722 | 0 | if (ptls_get_read_epoch(tls) != in_epoch) |
6723 | 0 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
6724 | | |
6725 | 0 | return handle_handshake_record(tls, handle_client_handshake_message, &emitter.super, &rec, properties); |
6726 | 0 | } |
6727 | | |
6728 | | int ptls_server_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input, |
6729 | | size_t inlen, ptls_handshake_properties_t *properties) |
6730 | 0 | { |
6731 | 0 | assert(tls->is_server); |
6732 | | |
6733 | 0 | struct st_ptls_raw_message_emitter_t emitter = { |
6734 | 0 | {sendbuf, &tls->traffic_protection.enc, 0, begin_raw_message, commit_raw_message}, SIZE_MAX, epoch_offsets}; |
6735 | 0 | struct st_ptls_record_t rec = {PTLS_CONTENT_TYPE_HANDSHAKE, 0, inlen, input}; |
6736 | |
|
6737 | 0 | if (tls->state == PTLS_STATE_SERVER_GENERATING_CERTIFICATE_VERIFY) { |
6738 | 0 | assert(input == NULL || inlen == 0); |
6739 | 0 | return server_finish_handshake(tls, &emitter.super, 1, NULL); |
6740 | 0 | } |
6741 | | |
6742 | 0 | assert(input != NULL); |
6743 | | |
6744 | 0 | if (ptls_get_read_epoch(tls) != in_epoch) |
6745 | 0 | return PTLS_ALERT_UNEXPECTED_MESSAGE; |
6746 | | |
6747 | 0 | return handle_handshake_record(tls, handle_server_handshake_message, &emitter.super, &rec, properties); |
6748 | 0 | } |
6749 | | |
6750 | | /** |
6751 | | * checks if given name looks like an IP address |
6752 | | */ |
6753 | | int ptls_server_name_is_ipaddr(const char *name) |
6754 | 0 | { |
6755 | 0 | #ifdef AF_INET |
6756 | 0 | struct sockaddr_in sin; |
6757 | 0 | if (inet_pton(AF_INET, name, &sin) == 1) |
6758 | 0 | return 1; |
6759 | 0 | #endif |
6760 | 0 | #ifdef AF_INET6 |
6761 | 0 | struct sockaddr_in6 sin6; |
6762 | 0 | if (inet_pton(AF_INET6, name, &sin6) == 1) |
6763 | 0 | return 1; |
6764 | 0 | #endif |
6765 | 0 | return 0; |
6766 | 0 | } |
6767 | | |
6768 | | int ptls_ech_encode_config(ptls_buffer_t *buf, uint8_t config_id, ptls_hpke_kem_t *kem, ptls_iovec_t public_key, |
6769 | | ptls_hpke_cipher_suite_t **ciphers, uint8_t max_name_length, const char *public_name) |
6770 | 0 | { |
6771 | 0 | int ret; |
6772 | |
|
6773 | 0 | ptls_buffer_push16(buf, PTLS_ECH_CONFIG_VERSION); |
6774 | 0 | ptls_buffer_push_block(buf, 2, { |
6775 | 0 | ptls_buffer_push(buf, config_id); |
6776 | 0 | ptls_buffer_push16(buf, kem->id); |
6777 | 0 | ptls_buffer_push_block(buf, 2, { ptls_buffer_pushv(buf, public_key.base, public_key.len); }); |
6778 | 0 | ptls_buffer_push_block(buf, 2, { |
6779 | 0 | for (size_t i = 0; ciphers[i] != NULL; ++i) { |
6780 | 0 | ptls_buffer_push16(buf, ciphers[i]->id.kdf); |
6781 | 0 | ptls_buffer_push16(buf, ciphers[i]->id.aead); |
6782 | 0 | } |
6783 | 0 | }); |
6784 | 0 | ptls_buffer_push(buf, max_name_length); |
6785 | 0 | ptls_buffer_push_block(buf, 1, { ptls_buffer_pushv(buf, public_name, strlen(public_name)); }); |
6786 | 0 | ptls_buffer_push_block(buf, 2, {/* extensions */}); |
6787 | 0 | }); |
6788 | | |
6789 | 0 | Exit: |
6790 | 0 | return ret; |
6791 | 0 | } |
6792 | | |
6793 | | static char *byte_to_hex(char *dst, uint8_t v) |
6794 | 0 | { |
6795 | 0 | *dst++ = "0123456789abcdef"[v >> 4]; |
6796 | 0 | *dst++ = "0123456789abcdef"[v & 0xf]; |
6797 | 0 | return dst; |
6798 | 0 | } |
6799 | | |
6800 | | char *ptls_hexdump(char *dst, const void *_src, size_t len) |
6801 | 0 | { |
6802 | 0 | char *buf = dst; |
6803 | 0 | const uint8_t *src = _src; |
6804 | |
|
6805 | 0 | for (size_t i = 0; i != len; ++i) |
6806 | 0 | dst = byte_to_hex(dst, src[i]); |
6807 | 0 | *dst = '\0'; |
6808 | 0 | return buf; |
6809 | 0 | } |
6810 | | |
6811 | | char *ptls_jsonescape(char *buf, const char *unsafe_str, size_t len) |
6812 | 0 | { |
6813 | 0 | char *dst = buf; |
6814 | 0 | const uint8_t *src = (const uint8_t *)unsafe_str, *end = src + len; |
6815 | |
|
6816 | 0 | for (; src != end; ++src) { |
6817 | 0 | switch (*src) { |
6818 | 0 | #define MAP(ch, escaped) \ |
6819 | 0 | case ch: \ |
6820 | 0 | memcpy(dst, (escaped), sizeof(escaped) - 1); \ |
6821 | 0 | dst += sizeof(escaped) - 1; \ |
6822 | 0 | break |
6823 | 0 | MAP('"', "\\\""); |
6824 | 0 | MAP('\\', "\\\\"); |
6825 | 0 | MAP('/', "\\/"); |
6826 | 0 | MAP('\b', "\\b"); |
6827 | 0 | MAP('\f', "\\f"); |
6828 | 0 | MAP('\n', "\\n"); |
6829 | 0 | MAP('\r', "\\r"); |
6830 | 0 | MAP('\t', "\\t"); |
6831 | 0 | #undef MAP |
6832 | 0 | default: |
6833 | 0 | if (*src < 0x20 || *src == 0x7f) { |
6834 | 0 | *dst++ = '\\'; |
6835 | 0 | *dst++ = 'u'; |
6836 | 0 | *dst++ = '0'; |
6837 | 0 | *dst++ = '0'; |
6838 | 0 | dst = byte_to_hex(dst, *src); |
6839 | 0 | } else { |
6840 | 0 | *dst++ = *src; |
6841 | 0 | } |
6842 | 0 | break; |
6843 | 0 | } |
6844 | 0 | } |
6845 | 0 | *dst = '\0'; |
6846 | |
|
6847 | 0 | return dst; |
6848 | 0 | } |
6849 | | |
6850 | | void ptls_build_v4_mapped_v6_address(void *v6, const void *v4) |
6851 | 0 | { |
6852 | 0 | memset(v6, 0, 10); |
6853 | 0 | memset((uint8_t *)v6 + 10, 0xff, 2); |
6854 | 0 | memcpy((uint8_t *)v6 + 12, v4, 4); |
6855 | 0 | } |
6856 | | |
6857 | | struct st_ptls_log_t ptls_log = { |
6858 | | .dummy_conn_state = {.random_ = 1 /* never log */}, |
6859 | | ._generation = 1, /* starts from 1 so that recalc can be forced by setting to zero (i.e., the initial) */ |
6860 | | }; |
6861 | | PTLS_THREADLOCAL ptls_log_conn_state_t *ptls_log_conn_state_override = NULL; |
6862 | | |
6863 | | #if PTLS_HAVE_LOG |
6864 | | |
6865 | | static struct { |
6866 | | /** |
6867 | | * list of connections; the slot is connected if points != NULL |
6868 | | */ |
6869 | | struct { |
6870 | | /** |
6871 | | * file descriptor |
6872 | | */ |
6873 | | int fd; |
6874 | | /** |
6875 | | * see `ptls_log_add_fd` |
6876 | | */ |
6877 | | char *points; |
6878 | | /** |
6879 | | * |
6880 | | */ |
6881 | | char *snis; |
6882 | | /** |
6883 | | * list of addresses terminated by ip6addr_any |
6884 | | */ |
6885 | | struct in6_addr *addresses; |
6886 | | /** |
6887 | | * |
6888 | | */ |
6889 | | float sample_ratio; |
6890 | | /** |
6891 | | * |
6892 | | */ |
6893 | | unsigned appdata : 1; |
6894 | | } conns[sizeof(((struct st_ptls_log_state_t *)NULL)->active_conns) * 8]; |
6895 | | /** |
6896 | | * counts the number of writes that failed |
6897 | | */ |
6898 | | size_t num_lost; |
6899 | | /** |
6900 | | * anchor of the single-linked list of log points; the tail refers to itself (i.e., point->next == point) |
6901 | | */ |
6902 | | struct st_ptls_log_point_t *points; |
6903 | | /** |
6904 | | * |
6905 | | */ |
6906 | | pthread_mutex_t mutex; |
6907 | | } logctx = {.mutex = PTHREAD_MUTEX_INITIALIZER}; |
6908 | | |
6909 | | static PTLS_THREADLOCAL struct { |
6910 | | ptls_buffer_t buf; /* buf.base == NULL upon failre */ |
6911 | | char smallbuf[128]; |
6912 | | struct { |
6913 | | char buf[sizeof(",\"tid\":-9223372036854775808")]; |
6914 | | size_t len; |
6915 | | } tid; |
6916 | | } logbuf; |
6917 | | |
6918 | | static void close_log_fd(size_t slot) |
6919 | 0 | { |
6920 | 0 | assert(logctx.conns[slot].fd >= 0 && logctx.conns[slot].points != NULL); |
6921 | | |
6922 | 0 | close(logctx.conns[slot].fd); |
6923 | | |
6924 | | /* clear the connection information */ |
6925 | 0 | logctx.conns[slot].fd = -1; |
6926 | 0 | logctx.conns[slot].sample_ratio = 0; |
6927 | 0 | free(logctx.conns[slot].points); |
6928 | 0 | logctx.conns[slot].points = NULL; |
6929 | 0 | free(logctx.conns[slot].snis); |
6930 | 0 | logctx.conns[slot].snis = NULL; |
6931 | 0 | free(logctx.conns[slot].addresses); |
6932 | 0 | logctx.conns[slot].addresses = NULL; |
6933 | 0 | logctx.conns[slot].appdata = 0; |
6934 | 0 | ++ptls_log._generation; |
6935 | 0 | } |
6936 | | |
6937 | | static char *duplicate_stringlist(const char *input) |
6938 | 0 | { |
6939 | 0 | if (input == NULL) |
6940 | 0 | return strdup(""); |
6941 | | |
6942 | 0 | char *result; |
6943 | 0 | const char *in_tail; |
6944 | |
|
6945 | 0 | for (in_tail = input; in_tail[0] != '\0'; in_tail += strlen(in_tail) + 1) |
6946 | 0 | ; |
6947 | 0 | ++in_tail; |
6948 | 0 | if ((result = malloc(in_tail - input)) == NULL) |
6949 | 0 | return NULL; |
6950 | 0 | memcpy(result, input, in_tail - input); |
6951 | 0 | return result; |
6952 | 0 | } |
6953 | | |
6954 | | static int is_in_stringlist(const char *list, const char *search_for) |
6955 | 0 | { |
6956 | 0 | if (list[0] == '\0') |
6957 | 0 | return 1; |
6958 | | |
6959 | 0 | if (search_for == NULL) |
6960 | 0 | return 0; |
6961 | | |
6962 | 0 | for (const char *element = list; element[0] != '\0'; element += strlen(element) + 1) |
6963 | 0 | if (strcmp(element, search_for) == 0) |
6964 | 0 | return 1; |
6965 | 0 | return 0; |
6966 | 0 | } |
6967 | | |
6968 | | static int is_in_addresslist(const struct in6_addr *list, const struct in6_addr *search_for) |
6969 | 0 | { |
6970 | 0 | #define IS_EQUAL(x, y) (memcmp((x), (y), sizeof(struct in6_addr)) == 0) |
6971 | |
|
6972 | 0 | if (IS_EQUAL(&list[0], &in6addr_any)) |
6973 | 0 | return 1; |
6974 | | |
6975 | 0 | if (IS_EQUAL(search_for, &in6addr_any)) |
6976 | 0 | return 0; |
6977 | | |
6978 | 0 | for (const struct in6_addr *element = list; !IS_EQUAL(element, &in6addr_any); ++element) |
6979 | 0 | if (IS_EQUAL(element, search_for)) |
6980 | 0 | return 1; |
6981 | 0 | return 0; |
6982 | |
|
6983 | 0 | #undef IS_EQUAL |
6984 | 0 | } |
6985 | | |
6986 | | void ptls_log__recalc_point(int caller_locked, struct st_ptls_log_point_t *point) |
6987 | 6 | { |
6988 | 6 | if (!caller_locked) |
6989 | 6 | pthread_mutex_lock(&logctx.mutex); |
6990 | | |
6991 | 6 | if (point->state.generation != ptls_log._generation) { |
6992 | | /* update active bitmap */ |
6993 | 6 | uint32_t new_active = 0; |
6994 | 198 | for (size_t slot = 0; slot < PTLS_ELEMENTSOF(logctx.conns); ++slot) |
6995 | 192 | if (logctx.conns[slot].points != NULL && is_in_stringlist(logctx.conns[slot].points, point->name)) |
6996 | 0 | new_active |= (uint32_t)1 << slot; |
6997 | 6 | point->state.active_conns = new_active; |
6998 | 6 | point->state.generation = ptls_log._generation; |
6999 | 6 | } |
7000 | | |
7001 | 6 | if (!caller_locked) |
7002 | 6 | pthread_mutex_unlock(&logctx.mutex); |
7003 | 6 | } |
7004 | | |
7005 | | void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni) |
7006 | 0 | { |
7007 | 0 | if (!caller_locked) |
7008 | 0 | pthread_mutex_lock(&logctx.mutex); |
7009 | |
|
7010 | 0 | if (conn->state.generation != ptls_log._generation) { |
7011 | | /* update active bitmap */ |
7012 | 0 | uint32_t new_active = 0; |
7013 | 0 | const char *sni = getsni.cb != NULL ? getsni.cb(getsni.arg) : NULL; |
7014 | 0 | for (size_t slot = 0; slot < PTLS_ELEMENTSOF(logctx.conns); ++slot) { |
7015 | 0 | if (logctx.conns[slot].points != NULL && conn->random_ < logctx.conns[slot].sample_ratio && |
7016 | 0 | is_in_stringlist(logctx.conns[slot].snis, sni) && |
7017 | 0 | is_in_addresslist(logctx.conns[slot].addresses, (struct in6_addr *)&conn->address)) { |
7018 | 0 | new_active |= (uint32_t)1 << slot; |
7019 | 0 | } |
7020 | 0 | } |
7021 | 0 | conn->state.active_conns = new_active; |
7022 | 0 | conn->state.generation = ptls_log._generation; |
7023 | 0 | } |
7024 | |
|
7025 | 0 | if (!caller_locked) |
7026 | 0 | pthread_mutex_unlock(&logctx.mutex); |
7027 | 0 | } |
7028 | | |
7029 | | static int expand_logbuf_or_invalidate(const char *prefix, size_t prefix_len, size_t capacity) |
7030 | 0 | { |
7031 | 0 | if (logbuf.buf.base == NULL) |
7032 | 0 | return 0; |
7033 | | |
7034 | 0 | if (ptls_buffer_reserve(&logbuf.buf, prefix_len + capacity) != 0) { |
7035 | 0 | ptls_buffer_dispose(&logbuf.buf); |
7036 | 0 | assert(logbuf.buf.base == NULL); |
7037 | 0 | return 0; |
7038 | 0 | } |
7039 | | |
7040 | 0 | memcpy(logbuf.buf.base + logbuf.buf.off, prefix, prefix_len); |
7041 | 0 | logbuf.buf.off += prefix_len; |
7042 | |
|
7043 | 0 | return 1; |
7044 | 0 | } |
7045 | | |
7046 | | __attribute__((format(printf, 4, 5))) static void pushf_logbuf_or_invalidate(const char *prefix, size_t prefix_len, size_t capacity, |
7047 | | const char *fmt, ...) |
7048 | 0 | { |
7049 | 0 | if (!expand_logbuf_or_invalidate(prefix, prefix_len, capacity)) |
7050 | 0 | return; |
7051 | | |
7052 | 0 | va_list args; |
7053 | 0 | va_start(args, fmt); |
7054 | 0 | int l = vsnprintf((char *)logbuf.buf.base + logbuf.buf.off, logbuf.buf.capacity - logbuf.buf.off, fmt, args); |
7055 | 0 | va_end(args); |
7056 | |
|
7057 | 0 | assert(l < logbuf.buf.capacity - logbuf.buf.off && "insufficent capacity"); |
7058 | 0 | logbuf.buf.off += l; |
7059 | 0 | } |
7060 | | |
7061 | | void ptls_log__do_push_element_safestr(const char *prefix, size_t prefix_len, const char *s, size_t l) |
7062 | 0 | { |
7063 | 0 | if (expand_logbuf_or_invalidate(prefix, prefix_len, l + 2)) { |
7064 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7065 | 0 | memcpy(logbuf.buf.base + logbuf.buf.off, s, l); |
7066 | 0 | logbuf.buf.off += l; |
7067 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7068 | 0 | } |
7069 | 0 | } |
7070 | | |
7071 | | void ptls_log__do_push_element_unsafestr(const char *prefix, size_t prefix_len, const char *s, size_t l) |
7072 | 0 | { |
7073 | 0 | if (expand_logbuf_or_invalidate(prefix, prefix_len, l * (sizeof("\\uXXXX") - 1) + 2)) { |
7074 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7075 | 0 | logbuf.buf.off = (uint8_t *)ptls_jsonescape((char *)logbuf.buf.base + logbuf.buf.off, s, l) - logbuf.buf.base; |
7076 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7077 | 0 | } |
7078 | 0 | } |
7079 | | |
7080 | | void ptls_log__do_push_element_hexdump(const char *prefix, size_t prefix_len, const void *s, size_t l) |
7081 | 0 | { |
7082 | 0 | if (expand_logbuf_or_invalidate(prefix, prefix_len, l * 2 + 2)) { |
7083 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7084 | 0 | ptls_hexdump((char *)logbuf.buf.base + logbuf.buf.off, s, l); |
7085 | 0 | logbuf.buf.off += l * 2; |
7086 | 0 | logbuf.buf.base[logbuf.buf.off++] = '"'; |
7087 | 0 | } |
7088 | 0 | } |
7089 | | |
7090 | | void ptls_log__do_push_element_signed32(const char *prefix, size_t prefix_len, int32_t v) |
7091 | 0 | { |
7092 | 0 | pushf_logbuf_or_invalidate(prefix, prefix_len, sizeof("-2147483648"), "%" PRId32, v); |
7093 | 0 | } |
7094 | | |
7095 | | void ptls_log__do_push_element_signed64(const char *prefix, size_t prefix_len, int64_t v) |
7096 | 0 | { |
7097 | 0 | pushf_logbuf_or_invalidate(prefix, prefix_len, sizeof("-9223372036854775808"), "%" PRId64, v); |
7098 | 0 | } |
7099 | | |
7100 | | void ptls_log__do_push_element_unsigned32(const char *prefix, size_t prefix_len, uint32_t v) |
7101 | 0 | { |
7102 | 0 | pushf_logbuf_or_invalidate(prefix, prefix_len, sizeof("4294967295"), "%" PRIu32, v); |
7103 | 0 | } |
7104 | | |
7105 | | void ptls_log__do_push_element_unsigned64(const char *prefix, size_t prefix_len, uint64_t v) |
7106 | 0 | { |
7107 | 0 | pushf_logbuf_or_invalidate(prefix, prefix_len, sizeof("18446744073709551615"), "%" PRIu64, v); |
7108 | 0 | } |
7109 | | |
7110 | | void ptls_log__do_push_element_bool(const char *prefix, size_t prefix_len, int v) |
7111 | 0 | { |
7112 | 0 | if (expand_logbuf_or_invalidate(prefix, prefix_len, 5)) { |
7113 | 0 | if (v) { |
7114 | 0 | memcpy(logbuf.buf.base + logbuf.buf.off, "true", 4); |
7115 | 0 | logbuf.buf.off += 4; |
7116 | 0 | } else { |
7117 | 0 | memcpy(logbuf.buf.base + logbuf.buf.off, "false", 5); |
7118 | 0 | logbuf.buf.off += 5; |
7119 | 0 | } |
7120 | 0 | } |
7121 | 0 | } |
7122 | | |
7123 | | void ptls_log__do_write_start(struct st_ptls_log_point_t *point, int add_time) |
7124 | 0 | { |
7125 | 0 | assert(logbuf.buf.base == NULL); |
7126 | 0 | ptls_buffer_init(&logbuf.buf, logbuf.smallbuf, sizeof(logbuf.smallbuf)); |
7127 | | |
7128 | | /* add module and type name */ |
7129 | 0 | const char *colon_at = strchr(point->name, ':'); |
7130 | 0 | int written = snprintf((char *)logbuf.buf.base, logbuf.buf.capacity, "{\"module\":\"%.*s\",\"type\":\"%s\"", |
7131 | 0 | (int)(colon_at - point->name), point->name, colon_at + 1); |
7132 | | |
7133 | | /* obtain and stringify thread id once */ |
7134 | 0 | if (logbuf.tid.len == 0) { |
7135 | 0 | #if defined(__linux__) |
7136 | 0 | logbuf.tid.len = sprintf(logbuf.tid.buf, ",\"tid\":%" PRId64, (int64_t)syscall(SYS_gettid)); |
7137 | | #elif defined(__APPLE__) |
7138 | | uint64_t t = 0; |
7139 | | #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 || defined(__POWERPC__) |
7140 | | t = pthread_mach_thread_np(pthread_self()); |
7141 | | #else |
7142 | | (void)pthread_threadid_np(NULL, &t); |
7143 | | #endif |
7144 | | logbuf.tid.len = sprintf(logbuf.tid.buf, ",\"tid\":%" PRIu64, t); |
7145 | | #else |
7146 | | /* other platforms: skip emitting tid, by keeping logbuf.tid.len == 0 */ |
7147 | | #endif |
7148 | 0 | } |
7149 | | /* append tid */ |
7150 | 0 | assert(written > 0 && written + logbuf.tid.len < logbuf.buf.capacity); |
7151 | 0 | memcpy((char *)logbuf.buf.base + written, logbuf.tid.buf, logbuf.tid.len + 1); |
7152 | 0 | written += logbuf.tid.len; |
7153 | | |
7154 | | /* append time if requested */ |
7155 | 0 | if (add_time) { |
7156 | 0 | struct timeval tv; |
7157 | 0 | gettimeofday(&tv, NULL); |
7158 | 0 | written += snprintf((char *)logbuf.buf.base + written, logbuf.buf.capacity - written, ",\"time\":%" PRIu64, |
7159 | 0 | (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000); |
7160 | 0 | } |
7161 | 0 | assert(written > 0 && written < logbuf.buf.capacity && "caller MUST provide smallbuf suffient to emit the prefix"); |
7162 | | |
7163 | 0 | logbuf.buf.off = (size_t)written; |
7164 | 0 | } |
7165 | | |
7166 | | int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni, |
7167 | | int includes_appdata) |
7168 | 0 | { |
7169 | 0 | if (!expand_logbuf_or_invalidate("}\n", 2, 0)) |
7170 | 0 | return 0; |
7171 | | |
7172 | 0 | int needs_appdata = 0; |
7173 | |
|
7174 | 0 | pthread_mutex_lock(&logctx.mutex); |
7175 | | |
7176 | | /* calc the active conn bits, updating stale information if necessary */ |
7177 | 0 | if (point->state.generation != ptls_log._generation) |
7178 | 0 | ptls_log__recalc_point(1, point); |
7179 | 0 | uint32_t active = point->state.active_conns; |
7180 | 0 | if (conn != NULL && conn->state.generation != ptls_log._generation) { |
7181 | 0 | ptls_log__recalc_conn(1, conn, getsni); |
7182 | 0 | active &= conn->state.active_conns; |
7183 | 0 | } |
7184 | | |
7185 | | /* iterate through the active connctions */ |
7186 | 0 | for (size_t slot = 0; active != 0; ++slot, active >>= 1) { |
7187 | 0 | if ((active & 1) == 0) |
7188 | 0 | continue; |
7189 | | |
7190 | 0 | assert(logctx.conns[slot].points != NULL); |
7191 | | |
7192 | 0 | if (logctx.conns[slot].appdata != includes_appdata) { |
7193 | 0 | if (!includes_appdata && ptls_log.may_include_appdata) |
7194 | 0 | needs_appdata = 1; |
7195 | 0 | continue; |
7196 | 0 | } |
7197 | | |
7198 | | /* write */ |
7199 | 0 | ssize_t wret; |
7200 | 0 | while ((wret = write(logctx.conns[slot].fd, logbuf.buf.base, logbuf.buf.off)) == -1 && errno == EINTR) |
7201 | 0 | ; |
7202 | 0 | if (wret == logbuf.buf.off) { |
7203 | | /* success */ |
7204 | 0 | } else if (wret > 0 || (wret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))) { |
7205 | | /* partial write or buffer full */ |
7206 | 0 | ++logctx.num_lost; |
7207 | 0 | } else { |
7208 | | /* write error; close and unregister the connection */ |
7209 | 0 | close_log_fd(slot); |
7210 | 0 | } |
7211 | 0 | } |
7212 | | |
7213 | 0 | pthread_mutex_unlock(&logctx.mutex); |
7214 | |
|
7215 | 0 | if (includes_appdata) |
7216 | 0 | assert(!needs_appdata); |
7217 | | |
7218 | 0 | ptls_buffer_dispose(&logbuf.buf); |
7219 | 0 | assert(logbuf.buf.base == NULL); |
7220 | 0 | return needs_appdata; |
7221 | 0 | } |
7222 | | |
7223 | | #endif |
7224 | | |
7225 | | void ptls_log_init_conn_state(ptls_log_conn_state_t *state, void (*random_bytes)(void *, size_t), uint64_t conn_id, void *_peeraddr) |
7226 | 2.24k | { |
7227 | 2.24k | struct sockaddr *peeraddr = _peeraddr; |
7228 | 2.24k | uint32_t r; |
7229 | 2.24k | random_bytes(&r, sizeof(r)); |
7230 | | |
7231 | 2.24k | *state = (ptls_log_conn_state_t){ |
7232 | 2.24k | .random_ = (float)r / ((uint64_t)UINT32_MAX + 1), /* [0..1), so that any(r) < sample_ratio where sample_ratio is [0..1] */ |
7233 | 2.24k | .address = {0}, /* inaddr6_any */ |
7234 | 2.24k | .conn_id = conn_id, |
7235 | 2.24k | }; |
7236 | 2.24k | if (peeraddr != NULL) { |
7237 | 0 | switch (peeraddr->sa_family) { |
7238 | 0 | case AF_INET: /* store as v6-mapped v4 address */ |
7239 | 0 | ptls_build_v4_mapped_v6_address(state->address, &((struct sockaddr_in *)peeraddr)->sin_addr); |
7240 | 0 | break; |
7241 | 0 | case AF_INET6: |
7242 | 0 | memcpy(state->address, ((struct sockaddr_in6 *)peeraddr)->sin6_addr.s6_addr, sizeof(state->address)); |
7243 | 0 | break; |
7244 | 0 | default: |
7245 | 0 | break; |
7246 | 0 | } |
7247 | 0 | } |
7248 | 2.24k | } |
7249 | | |
7250 | | size_t ptls_log_num_lost(void) |
7251 | 0 | { |
7252 | 0 | #if PTLS_HAVE_LOG |
7253 | 0 | return logctx.num_lost; |
7254 | | #else |
7255 | | return 0; |
7256 | | #endif |
7257 | 0 | } |
7258 | | |
7259 | | int ptls_log_add_fd(int fd, float sample_ratio, const char *_points, const char *_snis, const char *_addresses, int appdata) |
7260 | 0 | { |
7261 | 0 | #if PTLS_HAVE_LOG |
7262 | |
|
7263 | 0 | char *points = NULL, *snis = NULL; |
7264 | 0 | struct in6_addr *addresses = NULL; |
7265 | 0 | int ret; |
7266 | |
|
7267 | 0 | pthread_mutex_lock(&logctx.mutex); |
7268 | |
|
7269 | 0 | if ((points = duplicate_stringlist(_points)) == NULL) { |
7270 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
7271 | 0 | goto Exit; |
7272 | 0 | } |
7273 | 0 | if ((snis = duplicate_stringlist(_snis)) == NULL) { |
7274 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
7275 | 0 | goto Exit; |
7276 | 0 | } |
7277 | 0 | { |
7278 | 0 | size_t num_addresses = 0; |
7279 | 0 | for (const char *input = _addresses; input != NULL && *input != '\0'; input += strlen(input) + 1) |
7280 | 0 | ++num_addresses; |
7281 | 0 | if ((addresses = malloc(sizeof(*addresses) * (num_addresses + 1))) == NULL) { |
7282 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
7283 | 0 | goto Exit; |
7284 | 0 | } |
7285 | 0 | size_t index = 0; |
7286 | 0 | for (const char *input = _addresses; input != NULL && *input != '\0'; input += strlen(input) + 1) { |
7287 | | /* note: for consistency to the handling of points, erroneous input is ignored. V4 addresses will use the mapped form |
7288 | | * (::ffff:192.0.2.1) */ |
7289 | 0 | if (!inet_pton(AF_INET6, input, &addresses[index])) { |
7290 | 0 | struct in_addr v4; |
7291 | 0 | if (!inet_pton(AF_INET, input, &v4)) |
7292 | 0 | continue; |
7293 | 0 | ptls_build_v4_mapped_v6_address(&addresses[index], &v4); |
7294 | 0 | } |
7295 | 0 | if (memcmp(&addresses[index], &in6addr_any, sizeof(struct in6_addr)) == 0) |
7296 | 0 | continue; |
7297 | 0 | ++index; |
7298 | 0 | } |
7299 | 0 | addresses[index] = in6addr_any; |
7300 | 0 | } |
7301 | | |
7302 | | /* find slot, or return if not available */ |
7303 | 0 | size_t slot_index; |
7304 | 0 | for (slot_index = 0; slot_index < PTLS_ELEMENTSOF(logctx.conns); ++slot_index) |
7305 | 0 | if (logctx.conns[slot_index].points == NULL) |
7306 | 0 | break; |
7307 | 0 | if (slot_index == PTLS_ELEMENTSOF(logctx.conns)) { |
7308 | 0 | ret = PTLS_ERROR_NO_MEMORY; |
7309 | 0 | goto Exit; |
7310 | 0 | } |
7311 | | |
7312 | | /* setup the slot */ |
7313 | 0 | logctx.conns[slot_index].fd = fd; |
7314 | 0 | logctx.conns[slot_index].points = points; |
7315 | 0 | logctx.conns[slot_index].snis = snis; |
7316 | 0 | logctx.conns[slot_index].addresses = addresses; |
7317 | 0 | logctx.conns[slot_index].sample_ratio = sample_ratio; |
7318 | 0 | logctx.conns[slot_index].appdata = appdata; |
7319 | 0 | ++ptls_log._generation; |
7320 | |
|
7321 | 0 | ret = 0; /* success */ |
7322 | |
|
7323 | 0 | Exit: |
7324 | 0 | pthread_mutex_unlock(&logctx.mutex); |
7325 | 0 | if (ret != 0) { |
7326 | 0 | free(points); |
7327 | 0 | free(snis); |
7328 | 0 | free(addresses); |
7329 | 0 | } |
7330 | 0 | return ret; |
7331 | |
|
7332 | | #else |
7333 | | return PTLS_ERROR_NOT_AVAILABLE; |
7334 | | #endif |
7335 | 0 | } |