/src/openssl/ssl/statem/statem_dtls.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2005-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <assert.h> |
11 | | #include <limits.h> |
12 | | #include <string.h> |
13 | | #include <stdio.h> |
14 | | #include "../ssl_local.h" |
15 | | #include "statem_local.h" |
16 | | #include "internal/cryptlib.h" |
17 | | #include "internal/ssl_unwrap.h" |
18 | | #include <openssl/buffer.h> |
19 | | |
20 | 0 | #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8) |
21 | | |
22 | | #define RSMBLY_BITMASK_MARK(bitmask, start, end) \ |
23 | 0 | { \ |
24 | 0 | if ((end) - (start) <= 8) { \ |
25 | 0 | long ii; \ |
26 | 0 | for (ii = (start); ii < (end); ii++) \ |
27 | 0 | bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \ |
28 | 0 | } else { \ |
29 | 0 | long ii; \ |
30 | 0 | bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \ |
31 | 0 | for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) \ |
32 | 0 | bitmask[ii] = 0xff; \ |
33 | 0 | bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \ |
34 | 0 | } \ |
35 | 0 | } |
36 | | |
37 | | #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) \ |
38 | 0 | { \ |
39 | 0 | long ii; \ |
40 | 0 | is_complete = 1; \ |
41 | 0 | if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) \ |
42 | 0 | is_complete = 0; \ |
43 | 0 | if (is_complete) \ |
44 | 0 | for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0; ii--) \ |
45 | 0 | if (bitmask[ii] != 0xff) { \ |
46 | 0 | is_complete = 0; \ |
47 | 0 | break; \ |
48 | 0 | } \ |
49 | 0 | } |
50 | | |
51 | | static const unsigned char bitmask_start_values[] = { |
52 | | 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 |
53 | | }; |
54 | | static const unsigned char bitmask_end_values[] = { |
55 | | 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f |
56 | | }; |
57 | | |
58 | | static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype, |
59 | | size_t *len); |
60 | | |
61 | | /* |
62 | | * Check if CCS is expected in current state. |
63 | | * |
64 | | * RFC 6347 Section 4.1 states DTLS must handle message reordering since UDP |
65 | | * does not guarantee in-order delivery. This function determines when a |
66 | | * buffered CCS should be delivered to the state machine. |
67 | | * |
68 | | * Server states where CCS is expected: |
69 | | * - TLS_ST_SR_KEY_EXCH: After key exchange (anonymous or no_cert_verify) |
70 | | * - TLS_ST_SR_CERT_VRFY: After certificate verify |
71 | | * - TLS_ST_SW_FINISHED: Session resumption (abbreviated handshake) |
72 | | * |
73 | | * Client states where CCS is expected: |
74 | | * - TLS_ST_CR_SRVR_HELLO: Abbreviated handshake without ticket |
75 | | * - TLS_ST_CW_FINISHED: After sending Finished, before server CCS |
76 | | * - TLS_ST_CR_SESSION_TICKET: After receiving session ticket |
77 | | */ |
78 | | static int dtls_ccs_expected(SSL_CONNECTION *s) |
79 | 0 | { |
80 | 0 | OSSL_HANDSHAKE_STATE st = s->statem.hand_state; |
81 | |
|
82 | 0 | if (s->server) { |
83 | 0 | switch (st) { |
84 | 0 | case TLS_ST_SR_KEY_EXCH: |
85 | | /* Anonymous or no client cert: CCS follows KeyExchange */ |
86 | 0 | if (s->session->peer == NULL && s->session->peer_rpk == NULL) |
87 | 0 | return 1; |
88 | | /* Client cert but no verify message required */ |
89 | 0 | return s->statem.no_cert_verify; |
90 | 0 | case TLS_ST_SR_CERT_VRFY: |
91 | 0 | return 1; |
92 | 0 | case TLS_ST_SW_FINISHED: |
93 | | /* Abbreviated handshake: server sends first, then receives CCS */ |
94 | 0 | return s->hit; |
95 | 0 | default: |
96 | 0 | return 0; |
97 | 0 | } |
98 | 0 | } else { |
99 | 0 | switch (st) { |
100 | 0 | case TLS_ST_CR_SRVR_HELLO: |
101 | | /* Abbreviated handshake without session ticket */ |
102 | 0 | return (s->hit && !s->ext.ticket_expected); |
103 | 0 | case TLS_ST_CW_FINISHED: |
104 | | /* Full handshake: waiting for server CCS after sending Finished */ |
105 | 0 | return !s->ext.ticket_expected; |
106 | 0 | case TLS_ST_CR_SESSION_TICKET: |
107 | 0 | return 1; |
108 | 0 | default: |
109 | 0 | return 0; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | static dtls_sent_msg *dtls1_sent_msg_new(size_t msg_len, size_t body_len, |
115 | | int track_coverage) |
116 | 0 | { |
117 | 0 | const size_t bitmask_len = (track_coverage && body_len > 0 ? RSMBLY_BITMASK_SIZE(body_len) : 0); |
118 | 0 | dtls_sent_msg *msg = OPENSSL_malloc(sizeof(*msg) + msg_len + bitmask_len); |
119 | |
|
120 | 0 | if (msg == NULL) |
121 | 0 | return NULL; |
122 | | |
123 | 0 | memset(msg, 0, sizeof(*msg)); |
124 | | |
125 | | /* zero length msg gets msg->msg_buf == NULL */ |
126 | 0 | if (msg_len > 0) |
127 | 0 | msg->msg_buf = (unsigned char *)(msg + 1); |
128 | | |
129 | | /* |
130 | | * body_len > 0 implies msg_len > 0 (msg_len == body_len + headerlen, and |
131 | | * headerlen is never 0), so msg->msg_buf is already set here. Coverage |
132 | | * tracking only means anything for DTLS 1.3, the only version that ever |
133 | | * processes an ACK; older versions never read msg->covered, so don't pay |
134 | | * for the allocation on their behalf. |
135 | | */ |
136 | 0 | if (track_coverage && body_len > 0) { |
137 | 0 | msg->covered = msg->msg_buf + msg_len; |
138 | 0 | memset(msg->covered, 0, bitmask_len); |
139 | 0 | } |
140 | |
|
141 | 0 | return msg; |
142 | 0 | } |
143 | | |
144 | | void dtls1_sent_msg_free(dtls_sent_msg *msg) |
145 | 0 | { |
146 | 0 | if (msg != NULL) |
147 | 0 | ossl_list_record_number_elem_free(&msg->rec_nums); |
148 | |
|
149 | 0 | OPENSSL_free(msg); |
150 | 0 | } |
151 | | |
152 | | static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly) |
153 | 0 | { |
154 | 0 | const size_t bitmask_len = (reassembly ? RSMBLY_BITMASK_SIZE(frag_len) : 0); |
155 | 0 | hm_fragment *frag = OPENSSL_malloc(sizeof(*frag) + frag_len + bitmask_len); |
156 | |
|
157 | 0 | if (frag == NULL) |
158 | 0 | return NULL; |
159 | | |
160 | 0 | memset(frag, 0, sizeof(*frag)); |
161 | | |
162 | | /* zero length fragment gets zero frag->fragment */ |
163 | 0 | if (frag_len > 0) |
164 | 0 | frag->fragment = (unsigned char *)(frag + 1); |
165 | | |
166 | | /* Initialize reassembly bitmask if necessary */ |
167 | 0 | if (bitmask_len > 0) { |
168 | 0 | if (frag->fragment == NULL) |
169 | 0 | frag->reassembly = (unsigned char *)(frag + 1); |
170 | 0 | else |
171 | 0 | frag->reassembly = frag->fragment + frag_len; |
172 | |
|
173 | 0 | memset(frag->reassembly, 0, bitmask_len); |
174 | 0 | } |
175 | |
|
176 | 0 | return frag; |
177 | 0 | } |
178 | | |
179 | | void dtls1_hm_fragment_free(hm_fragment *frag) |
180 | 0 | { |
181 | 0 | OPENSSL_free(frag); |
182 | 0 | } |
183 | | |
184 | | static int dtls1_write_hm_header(unsigned char *msgheaderstart, |
185 | | unsigned char msg_type, size_t msg_len, |
186 | | unsigned short msg_seq, size_t fragoff, |
187 | | size_t fraglen) |
188 | 0 | { |
189 | 0 | WPACKET msgheader; |
190 | 0 | size_t msgheaderlen; |
191 | |
|
192 | 0 | if (!WPACKET_init_static_len(&msgheader, msgheaderstart, |
193 | 0 | DTLS1_HM_HEADER_LENGTH, 0) |
194 | 0 | || !WPACKET_put_bytes_u8(&msgheader, msg_type) |
195 | 0 | || !WPACKET_put_bytes_u24(&msgheader, msg_len) |
196 | 0 | || !WPACKET_put_bytes_u16(&msgheader, msg_seq) |
197 | 0 | || !WPACKET_put_bytes_u24(&msgheader, fragoff) |
198 | 0 | || !WPACKET_put_bytes_u24(&msgheader, fraglen) |
199 | 0 | || !WPACKET_get_total_written(&msgheader, &msgheaderlen) |
200 | 0 | || msgheaderlen != DTLS1_HM_HEADER_LENGTH |
201 | 0 | || !WPACKET_finish(&msgheader)) { |
202 | 0 | WPACKET_cleanup(&msgheader); |
203 | 0 | return 0; |
204 | 0 | } |
205 | | |
206 | 0 | return 1; |
207 | 0 | } |
208 | | |
209 | | /* |
210 | | * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE, |
211 | | * SSL3_RT_CHANGE_CIPHER_SPEC or SSL3_RT_ACK) |
212 | | * |
213 | | * When sending a fragmented handshake message this function will re-use |
214 | | * s->init_buf->data but overwrite previously sent data to fill out the handshake |
215 | | * message header for the next fragment. |
216 | | * |
217 | | * E.g. |
218 | | * |-------------------------s->init_buf->data------------------------------| |
219 | | * |-- header1 --||-- fragment1 --| |
220 | | * |-- header2 --||-- fragment2 --| |
221 | | * |-- header3 --||-- fragment3 --| |
222 | | * ......... |
223 | | */ |
224 | | int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype) |
225 | 0 | { |
226 | 0 | int ret; |
227 | 0 | size_t written; |
228 | 0 | size_t curr_mtu; |
229 | 0 | int retry = 1; |
230 | 0 | size_t len, overhead, used_len; |
231 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
232 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
233 | 0 | uint8_t saved_payload[DTLS1_HM_HEADER_LENGTH]; |
234 | | /* msg_len, msg_seq, msg_type are only used for recordtype == SSL3_RT_HANDSHAKE */ |
235 | 0 | const size_t msg_len = s->d1->w_msg.msg_body_len; |
236 | 0 | const unsigned short msg_seq = s->d1->w_msg.msg_seq; |
237 | 0 | const unsigned char msg_type = s->d1->w_msg.msg_type; |
238 | 0 | const size_t min_len = recordtype == SSL3_RT_ACK |
239 | 0 | ? DTLS13_ACK_MIN_NONEMPTY_LEN |
240 | 0 | : DTLS1_HM_HEADER_LENGTH + 1; |
241 | |
|
242 | 0 | if (!dtls1_query_mtu(s)) |
243 | 0 | return -1; |
244 | | |
245 | 0 | if (s->d1->mtu < dtls1_min_mtu(s)) |
246 | | /* should have something reasonable now */ |
247 | 0 | return -1; |
248 | | |
249 | 0 | if (s->init_off == 0 && recordtype == SSL3_RT_HANDSHAKE) { |
250 | 0 | if (!ossl_assert(s->init_num == msg_len + DTLS1_HM_HEADER_LENGTH)) |
251 | 0 | return -1; |
252 | 0 | } |
253 | | |
254 | 0 | overhead = s->rlayer.wrlmethod->get_max_record_overhead(s->rlayer.wrl); |
255 | |
|
256 | 0 | s->rwstate = SSL_NOTHING; |
257 | | |
258 | | /* s->init_num shouldn't ever be < 0...but just in case */ |
259 | 0 | while (s->init_num > 0) { |
260 | 0 | unsigned char *msgstart; |
261 | |
|
262 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && s->init_off > 0) { |
263 | | /* |
264 | | * We must be writing a fragment other than the first one |
265 | | * and this is the first attempt at writing out this fragment |
266 | | */ |
267 | 0 | if (s->init_off <= DTLS1_HM_HEADER_LENGTH) { |
268 | | /* |
269 | | * Each fragment that was already sent must at least have |
270 | | * contained the message header plus one other byte. |
271 | | * Therefore |init_off| must have progressed by at least |
272 | | * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went |
273 | | * wrong. |
274 | | */ |
275 | 0 | return -1; |
276 | 0 | } |
277 | | |
278 | | /* |
279 | | * Adjust |init_off| and |init_num| to allow room for a new |
280 | | * message header for this fragment. |
281 | | */ |
282 | 0 | s->init_off -= DTLS1_HM_HEADER_LENGTH; |
283 | 0 | s->init_num += DTLS1_HM_HEADER_LENGTH; |
284 | 0 | } |
285 | | |
286 | 0 | used_len = BIO_wpending(s->wbio) + overhead; |
287 | 0 | if (s->d1->mtu > used_len) |
288 | 0 | curr_mtu = s->d1->mtu - used_len; |
289 | 0 | else |
290 | 0 | curr_mtu = 0; |
291 | |
|
292 | 0 | if (curr_mtu < min_len) { |
293 | | /* |
294 | | * grr.. we could get an error if MTU picked was wrong |
295 | | */ |
296 | 0 | ret = BIO_flush(s->wbio); |
297 | 0 | if (ret <= 0) { |
298 | 0 | s->rwstate = SSL_WRITING; |
299 | 0 | return ret; |
300 | 0 | } |
301 | 0 | if (s->d1->mtu >= overhead + min_len) { |
302 | 0 | curr_mtu = s->d1->mtu - overhead; |
303 | 0 | } else { |
304 | | /* Shouldn't happen */ |
305 | 0 | return -1; |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | 0 | if (s->init_num > curr_mtu) |
310 | 0 | len = curr_mtu; |
311 | 0 | else |
312 | 0 | len = s->init_num; |
313 | |
|
314 | 0 | if (len > ssl_get_max_send_fragment(s)) |
315 | 0 | len = ssl_get_max_send_fragment(s); |
316 | | |
317 | | /* |
318 | | * For DTLS1.3 and padding lets update the max fragment size |
319 | | * accordingly |
320 | | */ |
321 | 0 | if (SSL_CONNECTION_IS_DTLS13(s)) |
322 | 0 | s->rlayer.wrlmethod->set_curr_mtu(s->rlayer.wrl, curr_mtu); |
323 | |
|
324 | 0 | msgstart = (unsigned char *)&s->init_buf->data[s->init_off]; |
325 | |
|
326 | 0 | if (recordtype == SSL3_RT_ACK) { |
327 | | /* Each record needs a complete vector of record numbers. */ |
328 | 0 | if (!ossl_assert(len >= DTLS13_ACK_HEADER_LEN)) |
329 | 0 | return -1; |
330 | 0 | len = DTLS13_ACK_HEADER_LEN |
331 | 0 | + ((len - DTLS13_ACK_HEADER_LEN) / DTLS13_RECORD_NUMBER_LEN) |
332 | 0 | * DTLS13_RECORD_NUMBER_LEN; |
333 | 0 | msgstart[0] = (unsigned char)((len - DTLS13_ACK_HEADER_LEN) >> 8); |
334 | 0 | msgstart[1] = (unsigned char)(len - DTLS13_ACK_HEADER_LEN); |
335 | 0 | } |
336 | | |
337 | 0 | if (recordtype == SSL3_RT_HANDSHAKE) { |
338 | 0 | const size_t fragoff = s->init_off; |
339 | 0 | const size_t fraglen = len - DTLS1_HM_HEADER_LENGTH; |
340 | | |
341 | | /* |
342 | | * Save the data that will be overwritten by |
343 | | * the following code so no corruption occurs when using |
344 | | * a msg callback. |
345 | | */ |
346 | 0 | if (s->msg_callback && s->init_off != 0) |
347 | 0 | memcpy(saved_payload, msgstart, sizeof(saved_payload)); |
348 | |
|
349 | 0 | if (len < DTLS1_HM_HEADER_LENGTH |
350 | 0 | || !dtls1_write_hm_header(msgstart, msg_type, msg_len, |
351 | 0 | msg_seq, fragoff, fraglen)) |
352 | | /* |
353 | | * len is so small that we really can't do anything sensible |
354 | | * so fail |
355 | | */ |
356 | 0 | return -1; |
357 | | |
358 | | /* |
359 | | * Recorded so do_dtls1_write() (rec_layer_d1.c) can tag this |
360 | | * fragment's record number with the byte range it covers. |
361 | | */ |
362 | 0 | s->d1->w_frag_off = fragoff; |
363 | 0 | s->d1->w_frag_len = fraglen; |
364 | 0 | } |
365 | | |
366 | 0 | ret = dtls1_write_bytes(s, recordtype, msgstart, len, |
367 | 0 | &written); |
368 | |
|
369 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && s->msg_callback && s->init_off != 0) |
370 | 0 | memcpy(msgstart, saved_payload, sizeof(saved_payload)); |
371 | |
|
372 | 0 | if (ret <= 0) { |
373 | | /* |
374 | | * might need to update MTU here, but we don't know which |
375 | | * previous packet caused the failure -- so can't really |
376 | | * retransmit anything. continue as if everything is fine and |
377 | | * wait for an alert to handle the retransmit |
378 | | */ |
379 | 0 | if (retry && BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0 |
380 | 0 | && !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
381 | 0 | size_t old_mtu = s->d1->mtu; |
382 | |
|
383 | 0 | if (!dtls1_query_mtu(s)) |
384 | 0 | return -1; |
385 | | /* If MTU didn't change, retry is meaningless */ |
386 | 0 | if (s->d1->mtu == old_mtu) |
387 | 0 | return -1; |
388 | | /* Have one more go */ |
389 | 0 | retry = 0; |
390 | 0 | } else { |
391 | 0 | return -1; |
392 | 0 | } |
393 | 0 | } else { |
394 | | |
395 | | /* |
396 | | * bad if this assert fails, only part of the handshake message |
397 | | * got sent. but why would this happen? |
398 | | */ |
399 | 0 | if (!ossl_assert(len == written)) |
400 | 0 | return -1; |
401 | | |
402 | | /* |
403 | | * We should not exceed the MTU size. If compression is in use |
404 | | * then the max record overhead calculation is unreliable so we do |
405 | | * not check in that case. We use assert rather than ossl_assert |
406 | | * because in a production build, if this assert were ever to fail, |
407 | | * then the best thing to do is probably carry on regardless. |
408 | | */ |
409 | 0 | assert(s->s3.tmp.new_compression != NULL |
410 | 0 | || BIO_wpending(s->wbio) <= (int)s->d1->mtu); |
411 | |
|
412 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && !s->d1->retransmitting |
413 | 0 | && s->init_off == 0) { |
414 | 0 | size_t xlen = s->init_num; |
415 | |
|
416 | 0 | if (s->version == DTLS1_BAD_VER) { |
417 | 0 | msgstart += DTLS1_HM_HEADER_LENGTH; |
418 | 0 | xlen -= DTLS1_HM_HEADER_LENGTH; |
419 | 0 | } else { |
420 | | /* |
421 | | * Now prepare to calculate the transcript hash. For |
422 | | * versions prior to DTLSv1.3 this means: |
423 | | * |
424 | | * rfc6347: Hash calculations include entire handshake |
425 | | * messages, including DTLS-specific fields: message_seq, |
426 | | * fragment_offset, and fragment_length. However, the |
427 | | * Finished MAC MUST be computed as if each handshake |
428 | | * message had been sent as a single fragment. |
429 | | * |
430 | | * For DTLSv1.3 DTLS-specific fields: message_seq, |
431 | | * fragment_offset, and fragment_length are not used in the |
432 | | * calculation i.e. the MAC-calculation is the same as TLS |
433 | | * even though DTLS handshake messages may be fragmented. |
434 | | */ |
435 | 0 | if (!dtls1_write_hm_header(msgstart, msg_type, msg_len, |
436 | 0 | msg_seq, 0, msg_len)) |
437 | 0 | return -1; |
438 | 0 | } |
439 | | |
440 | | /* |
441 | | * should not be done for 'Hello Request's, but in that case we'll |
442 | | * ignore the result anyway |
443 | | * DTLS1.3 KeyUpdate and NewSessionTicket do not need to be added |
444 | | */ |
445 | 0 | if (!SSL_CONNECTION_IS_DTLS13(s) |
446 | 0 | || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET |
447 | 0 | && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE |
448 | 0 | && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE)) |
449 | 0 | if (!ssl3_finish_mac(s, msgstart, xlen)) |
450 | 0 | return -1; |
451 | 0 | } |
452 | 0 | if (recordtype == SSL3_RT_ACK && s->msg_callback != NULL) |
453 | 0 | s->msg_callback(1, s->version, recordtype, msgstart, written, |
454 | 0 | ussl, s->msg_callback_arg); |
455 | |
|
456 | 0 | if (written == s->init_num) { |
457 | 0 | if (s->msg_callback && recordtype != SSL3_RT_ACK) |
458 | 0 | s->msg_callback(1, s->version, recordtype, s->init_buf->data, |
459 | 0 | s->init_off + s->init_num, ussl, |
460 | 0 | s->msg_callback_arg); |
461 | |
|
462 | 0 | s->init_off = 0; /* done writing this message */ |
463 | 0 | s->init_num = 0; |
464 | |
|
465 | 0 | return 1; |
466 | 0 | } |
467 | | /* Reuse the last two sent bytes for the next ACK vector length. */ |
468 | 0 | if (recordtype == SSL3_RT_ACK) |
469 | 0 | written -= DTLS13_ACK_HEADER_LEN; |
470 | 0 | s->init_off += written; |
471 | 0 | s->init_num -= written; |
472 | 0 | } |
473 | 0 | } |
474 | 0 | return 0; |
475 | 0 | } |
476 | | |
477 | | int dtls_get_message(SSL_CONNECTION *s, int *mt) |
478 | 0 | { |
479 | 0 | unsigned char *rec_data; |
480 | 0 | size_t tmplen; |
481 | 0 | int errtype; |
482 | |
|
483 | 0 | s->d1->r_msg_seq = 0; |
484 | |
|
485 | 0 | again: |
486 | 0 | if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) { |
487 | 0 | if (s->statem.ack_for_retransmit) |
488 | 0 | return 0; |
489 | 0 | if (errtype == DTLS1_HM_BAD_FRAGMENT |
490 | 0 | || errtype == DTLS1_HM_FRAGMENT_RETRY) { |
491 | | /* bad fragment received */ |
492 | 0 | goto again; |
493 | 0 | } |
494 | 0 | return 0; |
495 | 0 | } |
496 | | |
497 | 0 | *mt = s->s3.tmp.message_type; |
498 | |
|
499 | 0 | rec_data = (unsigned char *)s->init_buf->data; |
500 | | |
501 | | /* |
502 | | * If this isn't a real handshake message skip the processing below. |
503 | | */ |
504 | 0 | if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC || *mt == DTLS13_MT_ACK) |
505 | 0 | return 1; |
506 | | |
507 | | /* reconstruct message header */ |
508 | 0 | dtls1_write_hm_header(rec_data, s->s3.tmp.message_type, s->s3.tmp.message_size, |
509 | 0 | s->d1->r_msg_seq, 0, s->s3.tmp.message_size); |
510 | |
|
511 | 0 | s->d1->r_msg_seq = 0; |
512 | |
|
513 | 0 | s->d1->handshake_read_seq++; |
514 | |
|
515 | 0 | s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
516 | |
|
517 | 0 | return 1; |
518 | 0 | } |
519 | | |
520 | | /* |
521 | | * Actually we already have the message body - but this is an opportunity for |
522 | | * DTLS to do any further processing it wants at the same point that TLS would |
523 | | * be asked for the message body. |
524 | | */ |
525 | | int dtls_get_message_body(SSL_CONNECTION *s, size_t *len) |
526 | 0 | { |
527 | 0 | unsigned char *msg = (unsigned char *)s->init_buf->data; |
528 | 0 | size_t msg_len; |
529 | 0 | int recordtype; |
530 | |
|
531 | 0 | switch (s->s3.tmp.message_type) { |
532 | 0 | default: |
533 | 0 | recordtype = SSL3_RT_HANDSHAKE; |
534 | 0 | msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH; |
535 | |
|
536 | 0 | break; |
537 | 0 | case DTLS13_MT_ACK: |
538 | 0 | recordtype = SSL3_RT_ACK; |
539 | 0 | msg_len = s->init_num; |
540 | |
|
541 | 0 | goto end; |
542 | 0 | case SSL3_MT_CHANGE_CIPHER_SPEC: |
543 | 0 | recordtype = SSL3_RT_CHANGE_CIPHER_SPEC; |
544 | 0 | msg_len = 1; |
545 | |
|
546 | 0 | goto end; |
547 | 0 | } |
548 | | |
549 | | /* |
550 | | * If receiving Finished, record MAC of prior handshake messages for |
551 | | * Finished verification. |
552 | | */ |
553 | 0 | if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) { |
554 | | /* SSLfatal() already called */ |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 0 | if (!tls_common_finish_mac(s)) |
559 | 0 | return 0; |
560 | | |
561 | 0 | end: |
562 | 0 | if (s->msg_callback) |
563 | 0 | s->msg_callback(0, s->version, recordtype, msg, msg_len, |
564 | 0 | SSL_CONNECTION_GET_USER_SSL(s), s->msg_callback_arg); |
565 | |
|
566 | 0 | *len = s->init_num; |
567 | 0 | return 1; |
568 | 0 | } |
569 | | |
570 | | /* |
571 | | * dtls1_max_handshake_message_len returns the maximum number of bytes |
572 | | * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but |
573 | | * may be greater if the maximum certificate list size requires it. |
574 | | */ |
575 | | static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s) |
576 | 0 | { |
577 | 0 | size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; |
578 | 0 | if (max_len < s->max_cert_list) |
579 | 0 | return s->max_cert_list; |
580 | 0 | return max_len; |
581 | 0 | } |
582 | | |
583 | | static int dtls1_preprocess_fragment(SSL_CONNECTION *s, |
584 | | const struct hm_header_st *const msg_hdr) |
585 | 0 | { |
586 | 0 | size_t frag_off, frag_len, msg_len; |
587 | |
|
588 | 0 | msg_len = msg_hdr->msg_len; |
589 | 0 | frag_off = msg_hdr->frag_off; |
590 | 0 | frag_len = msg_hdr->frag_len; |
591 | | |
592 | | /* sanity checking */ |
593 | 0 | if ((frag_off + frag_len) > msg_len |
594 | 0 | || msg_len > dtls1_max_handshake_message_len(s)) { |
595 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
596 | 0 | return 0; |
597 | 0 | } |
598 | | |
599 | | /* |
600 | | * msg_len is limited to 2^24, but is effectively checked against |
601 | | * dtls_max_handshake_message_len(s) above |
602 | | */ |
603 | 0 | if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) { |
604 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB); |
605 | 0 | return 0; |
606 | 0 | } |
607 | | |
608 | 0 | s->s3.tmp.message_size = msg_len; |
609 | 0 | s->s3.tmp.message_type = msg_hdr->type; |
610 | 0 | s->d1->r_msg_seq = msg_hdr->seq; |
611 | |
|
612 | 0 | return 1; |
613 | 0 | } |
614 | | |
615 | | static int add_record_to_ack_list(SSL_CONNECTION *sc) |
616 | 0 | { |
617 | 0 | DTLS1_RECORD_NUMBER *recnum; |
618 | 0 | uint64_t epoch = sc->s3.tmp.record_epoch; |
619 | 0 | uint64_t sequence = sc->s3.tmp.record_seq_num; |
620 | | |
621 | | /* |
622 | | * Retain at most one maximum-sized ACK record's worth of record numbers. |
623 | | * Check before the duplicate scan to bound the work once the list is full. |
624 | | * Excess records may be omitted from ACKs (RFC 9147, section 7.1). |
625 | | */ |
626 | 0 | if (ossl_list_record_number_num(&sc->d1->ack_rec_num) |
627 | 0 | >= (SSL3_RT_MAX_PLAIN_LENGTH - DTLS13_ACK_HEADER_LEN) / DTLS13_RECORD_NUMBER_LEN) |
628 | 0 | return 1; |
629 | | |
630 | 0 | for (recnum = ossl_list_record_number_head(&sc->d1->ack_rec_num); |
631 | 0 | recnum != NULL; |
632 | 0 | recnum = ossl_list_record_number_next(recnum)) { |
633 | | /* Is the record number already in the list? */ |
634 | 0 | if (recnum->epoch == epoch && recnum->seqnum == sequence) |
635 | 0 | return 1; |
636 | 0 | } |
637 | | |
638 | 0 | recnum = dtls1_record_number_new(epoch, sequence, 0, 0); |
639 | |
|
640 | 0 | if (recnum == NULL) |
641 | 0 | return 0; |
642 | | |
643 | 0 | ossl_list_record_number_insert_tail(&sc->d1->ack_rec_num, recnum); |
644 | |
|
645 | 0 | return 1; |
646 | 0 | } |
647 | | |
648 | | /* |
649 | | * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a |
650 | | * fatal error. |
651 | | */ |
652 | | static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len) |
653 | 0 | { |
654 | | /*- |
655 | | * (0) check whether the desired fragment is available |
656 | | * if so: |
657 | | * (1) copy over the fragment to s->init_buf->data[] |
658 | | * (2) update s->init_num |
659 | | */ |
660 | 0 | pitem *item; |
661 | 0 | piterator iter; |
662 | 0 | hm_fragment *frag; |
663 | 0 | int ret; |
664 | 0 | int chretran = 0; |
665 | 0 | pqueue *rcvd_messages = &s->d1->rcvd_messages; |
666 | |
|
667 | 0 | iter = pqueue_iterator(rcvd_messages); |
668 | 0 | do { |
669 | 0 | item = pqueue_next(&iter); |
670 | 0 | if (item == NULL) |
671 | 0 | return 0; |
672 | | |
673 | 0 | frag = (hm_fragment *)item->data; |
674 | |
|
675 | 0 | if (frag->msg_header.seq < s->d1->handshake_read_seq) { |
676 | 0 | pitem *next; |
677 | 0 | hm_fragment *nextfrag; |
678 | |
|
679 | 0 | if (!s->server |
680 | 0 | || frag->msg_header.seq != 0 |
681 | 0 | || s->d1->handshake_read_seq != 1 |
682 | 0 | || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { |
683 | | /* |
684 | | * This is a stale message that has been buffered so clear it. |
685 | | * It is safe to pop this message from the queue even though |
686 | | * we have an active iterator |
687 | | */ |
688 | 0 | pqueue_pop(rcvd_messages); |
689 | 0 | dtls1_hm_fragment_free(frag); |
690 | 0 | pitem_free(item); |
691 | 0 | item = NULL; |
692 | 0 | frag = NULL; |
693 | 0 | } else { |
694 | | /* |
695 | | * We have fragments for a ClientHello without a cookie, |
696 | | * even though we have sent a HelloVerifyRequest. It is possible |
697 | | * that the HelloVerifyRequest got lost and this is a |
698 | | * retransmission of the original ClientHello |
699 | | */ |
700 | 0 | next = pqueue_next(&iter); |
701 | 0 | if (next != NULL) { |
702 | 0 | nextfrag = (hm_fragment *)next->data; |
703 | 0 | if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) { |
704 | | /* |
705 | | * We have fragments for both a ClientHello without |
706 | | * cookie and one with. Ditch the one without. |
707 | | */ |
708 | 0 | pqueue_pop(rcvd_messages); |
709 | 0 | dtls1_hm_fragment_free(frag); |
710 | 0 | pitem_free(item); |
711 | 0 | item = next; |
712 | 0 | frag = nextfrag; |
713 | 0 | } else { |
714 | 0 | chretran = 1; |
715 | 0 | } |
716 | 0 | } else { |
717 | 0 | chretran = 1; |
718 | 0 | } |
719 | 0 | } |
720 | 0 | } |
721 | 0 | } while (item == NULL); |
722 | | |
723 | | /* Don't return if reassembly still in progress */ |
724 | 0 | if (frag->reassembly != NULL) |
725 | 0 | return 0; |
726 | | |
727 | 0 | if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) { |
728 | 0 | size_t frag_len = frag->msg_header.frag_len; |
729 | |
|
730 | 0 | pqueue_pop(rcvd_messages); |
731 | | |
732 | | /* Calls SSLfatal() as required */ |
733 | 0 | ret = dtls1_preprocess_fragment(s, &frag->msg_header); |
734 | |
|
735 | 0 | if (ret && frag->msg_header.frag_len > 0) { |
736 | 0 | unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
737 | 0 | memcpy(&p[frag->msg_header.frag_off], frag->fragment, |
738 | 0 | frag->msg_header.frag_len); |
739 | 0 | } |
740 | |
|
741 | 0 | dtls1_hm_fragment_free(frag); |
742 | 0 | pitem_free(item); |
743 | |
|
744 | 0 | if (ret) { |
745 | 0 | if (chretran) { |
746 | | /* |
747 | | * We got a new ClientHello with a message sequence of 0. |
748 | | * Reset the read/write sequences back to the beginning. |
749 | | * We process it like this is the first time we've seen a |
750 | | * ClientHello from the client. |
751 | | */ |
752 | 0 | s->d1->handshake_read_seq = 0; |
753 | 0 | s->d1->next_handshake_write_seq = 0; |
754 | 0 | } |
755 | 0 | *len = frag_len; |
756 | 0 | return 1; |
757 | 0 | } |
758 | | |
759 | | /* Fatal error */ |
760 | 0 | s->init_num = 0; |
761 | 0 | return -1; |
762 | 0 | } else { |
763 | 0 | return 0; |
764 | 0 | } |
765 | 0 | } |
766 | | |
767 | | static int dtls1_reassemble_fragment(SSL_CONNECTION *s, |
768 | | const struct hm_header_st *msg_hdr) |
769 | 0 | { |
770 | 0 | hm_fragment *frag = NULL; |
771 | 0 | pitem *item = NULL; |
772 | 0 | int i = -1, is_complete; |
773 | 0 | size_t frag_len = msg_hdr->frag_len; |
774 | 0 | size_t readbytes; |
775 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
776 | |
|
777 | 0 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len |
778 | 0 | || msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) |
779 | 0 | goto err; |
780 | | |
781 | 0 | if (frag_len == 0) { |
782 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
783 | 0 | } |
784 | | |
785 | | /* Try to find item in queue */ |
786 | 0 | item = pqueue_find_u64(&s->d1->rcvd_messages, msg_hdr->seq); |
787 | |
|
788 | 0 | if (item == NULL) { |
789 | 0 | frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1); |
790 | 0 | if (frag == NULL) |
791 | 0 | goto err; |
792 | 0 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
793 | 0 | frag->msg_header.frag_len = frag->msg_header.msg_len; |
794 | 0 | frag->msg_header.frag_off = 0; |
795 | 0 | } else { |
796 | 0 | frag = (hm_fragment *)item->data; |
797 | 0 | if (frag->msg_header.msg_len != msg_hdr->msg_len) { |
798 | 0 | item = NULL; |
799 | 0 | frag = NULL; |
800 | 0 | goto err; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | | /* |
805 | | * If message is already reassembled, this must be a retransmit and can |
806 | | * be dropped. In this case item != NULL and so frag does not need to be |
807 | | * freed. |
808 | | */ |
809 | 0 | if (frag->reassembly == NULL) { |
810 | 0 | unsigned char devnull[256]; |
811 | |
|
812 | 0 | while (frag_len) { |
813 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
814 | 0 | devnull, |
815 | 0 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes); |
816 | 0 | if (i <= 0) |
817 | 0 | goto err; |
818 | 0 | frag_len -= readbytes; |
819 | 0 | } |
820 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
821 | 0 | } |
822 | | |
823 | | /* read the body of the fragment (header has already been read */ |
824 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
825 | 0 | frag->fragment + msg_hdr->frag_off, |
826 | 0 | frag_len, 0, &readbytes); |
827 | 0 | if (i <= 0 || readbytes != frag_len) |
828 | 0 | goto err; |
829 | | |
830 | 0 | RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off, |
831 | 0 | (long)(msg_hdr->frag_off + frag_len)); |
832 | |
|
833 | 0 | if (!ossl_assert(msg_hdr->msg_len > 0)) |
834 | 0 | goto err; |
835 | 0 | RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len, |
836 | 0 | is_complete); |
837 | |
|
838 | 0 | if (is_complete) |
839 | 0 | frag->reassembly = NULL; |
840 | |
|
841 | 0 | if (item == NULL) { |
842 | 0 | item = pitem_new_u64(msg_hdr->seq, frag); |
843 | 0 | if (item == NULL) |
844 | 0 | goto err; |
845 | | |
846 | 0 | item = pqueue_insert(&s->d1->rcvd_messages, item); |
847 | | /* |
848 | | * pqueue_insert fails iff a duplicate item is inserted. However, |
849 | | * |item| cannot be a duplicate. If it were, |pqueue_find_u64|, above, |
850 | | * would have returned it and control would never have reached this |
851 | | * branch. |
852 | | */ |
853 | 0 | if (!ossl_assert(item != NULL)) |
854 | 0 | goto err; |
855 | 0 | } |
856 | | |
857 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr->type) |
858 | 0 | && !add_record_to_ack_list(s)) |
859 | 0 | goto err; |
860 | | |
861 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
862 | | |
863 | 0 | err: |
864 | 0 | if (item == NULL) |
865 | 0 | dtls1_hm_fragment_free(frag); |
866 | 0 | return -1; |
867 | 0 | } |
868 | | |
869 | | /* |
870 | | * True if the message currently being processed only authenticated because |
871 | | * the read record layer retained a previous epoch's keys for retransmission |
872 | | * recovery -- i.e. it did not actually arrive at the currently active |
873 | | * read epoch. Such a message must never be treated as new content: it may |
874 | | * only, at most, trigger a replacement ACK for something already fully |
875 | | * processed. |
876 | | */ |
877 | | int dtls_record_from_retained_epoch(SSL_CONNECTION *s) |
878 | 0 | { |
879 | 0 | return SSL_CONNECTION_IS_DTLS13(s) |
880 | 0 | && s->s3.tmp.record_epoch != dtls1_get_epoch(s, SSL3_CC_READ); |
881 | 0 | } |
882 | | |
883 | | int dtls1_process_out_of_seq_message(SSL_CONNECTION *s, |
884 | | const struct hm_header_st *msg_hdr) |
885 | 0 | { |
886 | 0 | int i = -1; |
887 | 0 | hm_fragment *frag = NULL; |
888 | 0 | pitem *item = NULL; |
889 | 0 | size_t frag_len = msg_hdr->frag_len; |
890 | 0 | size_t readbytes; |
891 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
892 | |
|
893 | 0 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) |
894 | 0 | goto err; |
895 | | |
896 | | /* Try to find item in queue, to prevent duplicate entries */ |
897 | 0 | item = pqueue_find_u64(&s->d1->rcvd_messages, msg_hdr->seq); |
898 | | |
899 | | /* |
900 | | * If we already have an entry and this one is a fragment, don't discard |
901 | | * it and rather try to reassemble it. |
902 | | */ |
903 | 0 | if (item != NULL && frag_len != msg_hdr->msg_len) |
904 | 0 | item = NULL; |
905 | | |
906 | | /* |
907 | | * Discard the message if sequence number was already there, is too far |
908 | | * in the future, already in the queue, if we received a FINISHED |
909 | | * before the SERVER_HELLO (which then must be a stale retransmit), or |
910 | | * if it only authenticated via a retained previous epoch: such a |
911 | | * message must never be buffered for future reassembly and eventually |
912 | | * processed as new content, no matter what sequence number it claims. |
913 | | */ |
914 | 0 | if (msg_hdr->seq <= s->d1->handshake_read_seq |
915 | 0 | || msg_hdr->seq > s->d1->handshake_read_seq + 10 |
916 | 0 | || item != NULL |
917 | 0 | || (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED) |
918 | 0 | || dtls_record_from_retained_epoch(s)) { |
919 | 0 | unsigned char devnull[256]; |
920 | |
|
921 | 0 | while (frag_len) { |
922 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
923 | 0 | devnull, |
924 | 0 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes); |
925 | 0 | if (i <= 0) |
926 | 0 | goto err; |
927 | 0 | frag_len -= readbytes; |
928 | 0 | } |
929 | | /* |
930 | | * A lost ACK can cause an already processed message to be |
931 | | * retransmitted in a new record. ACK it without processing it |
932 | | * again. Epoch 2 is included alongside the post-handshake epochs |
933 | | * (3+) so that a client's Finished, retransmitted after the server |
934 | | * has already moved on to epoch 3, still gets ACKed instead of |
935 | | * silently dropped (see dtls_get_more_records()'s retained |
936 | | * prev_epoch_rl handling, which is what let this record |
937 | | * authenticate at all). |
938 | | */ |
939 | 0 | if (SSL_CONNECTION_IS_DTLS13(s) |
940 | 0 | && s->s3.tmp.record_epoch >= 2 |
941 | 0 | && msg_hdr->seq < s->d1->handshake_read_seq |
942 | 0 | && dtls_msg_needs_ack(!s->server, msg_hdr->type)) { |
943 | 0 | if (!add_record_to_ack_list(s)) |
944 | 0 | goto err; |
945 | 0 | s->statem.ack_for_retransmit = 1; |
946 | 0 | } |
947 | 0 | } else { |
948 | 0 | if (frag_len != msg_hdr->msg_len) { |
949 | 0 | return dtls1_reassemble_fragment(s, msg_hdr); |
950 | 0 | } |
951 | | |
952 | 0 | if (frag_len > dtls1_max_handshake_message_len(s)) |
953 | 0 | goto err; |
954 | | |
955 | 0 | frag = dtls1_hm_fragment_new(frag_len, 0); |
956 | 0 | if (frag == NULL) |
957 | 0 | goto err; |
958 | | |
959 | 0 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
960 | |
|
961 | 0 | if (frag_len) { |
962 | | /* |
963 | | * read the body of the fragment (header has already been read |
964 | | */ |
965 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
966 | 0 | frag->fragment, frag_len, 0, |
967 | 0 | &readbytes); |
968 | 0 | if (i <= 0 || readbytes != frag_len) |
969 | 0 | goto err; |
970 | 0 | } |
971 | | |
972 | 0 | item = pitem_new_u64(msg_hdr->seq, frag); |
973 | 0 | if (item == NULL) |
974 | 0 | goto err; |
975 | | |
976 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr->type) |
977 | 0 | && !add_record_to_ack_list(s)) { |
978 | 0 | pitem_free(item); |
979 | 0 | item = NULL; |
980 | 0 | goto err; |
981 | 0 | } |
982 | | |
983 | 0 | item = pqueue_insert(&s->d1->rcvd_messages, item); |
984 | | /* |
985 | | * pqueue_insert fails iff a duplicate item is inserted. However, |
986 | | * |item| cannot be a duplicate. If it were, |pqueue_find|, above, |
987 | | * would have returned it. Then, either |frag_len| != |
988 | | * |msg_hdr->msg_len| in which case |item| is set to NULL and it will |
989 | | * have been processed with |dtls1_reassemble_fragment|, above, or |
990 | | * the record will have been discarded. |
991 | | */ |
992 | 0 | if (!ossl_assert(item != NULL)) |
993 | 0 | goto err; |
994 | 0 | } |
995 | | |
996 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
997 | | |
998 | 0 | err: |
999 | 0 | if (item == NULL) |
1000 | 0 | dtls1_hm_fragment_free(frag); |
1001 | 0 | return 0; |
1002 | 0 | } |
1003 | | |
1004 | | static int dtls1_read_hm_header(unsigned char *msgheaderstart, |
1005 | | struct hm_header_st *msg_hdr) |
1006 | 0 | { |
1007 | 0 | unsigned long msg_len, frag_off, frag_len; |
1008 | 0 | unsigned int msg_seq, msg_type; |
1009 | 0 | PACKET msgheader; |
1010 | |
|
1011 | 0 | if (!PACKET_buf_init(&msgheader, msgheaderstart, DTLS1_HM_HEADER_LENGTH) |
1012 | 0 | || !PACKET_get_1(&msgheader, &msg_type) |
1013 | 0 | || !PACKET_get_net_3(&msgheader, &msg_len) |
1014 | 0 | || !PACKET_get_net_2(&msgheader, &msg_seq) |
1015 | 0 | || !PACKET_get_net_3(&msgheader, &frag_off) |
1016 | 0 | || !PACKET_get_net_3(&msgheader, &frag_len) |
1017 | 0 | || PACKET_remaining(&msgheader) != 0) { |
1018 | 0 | return 0; |
1019 | 0 | } |
1020 | | |
1021 | | /* We just checked that values did not exceed max size so cast must be alright */ |
1022 | 0 | msg_hdr->type = (unsigned char)msg_type; |
1023 | 0 | msg_hdr->msg_len = (size_t)msg_len; |
1024 | 0 | msg_hdr->seq = (unsigned short)msg_seq; |
1025 | 0 | msg_hdr->frag_off = (size_t)frag_off; |
1026 | 0 | msg_hdr->frag_len = (size_t)frag_len; |
1027 | |
|
1028 | 0 | return 1; |
1029 | 0 | } |
1030 | | |
1031 | | /* |
1032 | | * DTLS 1.3 reserves handshake message type 0, so a HelloRequest must reach the |
1033 | | * state machine and be rejected there whenever DTLS 1.3 is still possible. |
1034 | | * |
1035 | | * s->version is the negotiated, or maximum version: before ServerHello this is |
1036 | | * the effective maximum, and after ServerHello or during renegotiation it is the |
1037 | | * selected version. |
1038 | | */ |
1039 | | static int dtls_should_skip_hello_request(const SSL_CONNECTION *s) |
1040 | 0 | { |
1041 | 0 | if (SSL_CONNECTION_IS_DTLS13(s)) |
1042 | 0 | return 0; |
1043 | | |
1044 | 0 | return s->version > 0 |
1045 | 0 | && ssl_version_cmp(s, s->version, DTLS1_3_VERSION) < 0; |
1046 | 0 | } |
1047 | | |
1048 | | static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype, |
1049 | | size_t *len) |
1050 | 0 | { |
1051 | 0 | int i, ret; |
1052 | 0 | uint8_t recvd_type; |
1053 | 0 | struct hm_header_st msg_hdr; |
1054 | 0 | size_t readbytes; |
1055 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1056 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1057 | 0 | int chretran = 0; |
1058 | 0 | unsigned char *p; |
1059 | |
|
1060 | 0 | *errtype = 0; |
1061 | |
|
1062 | 0 | p = (unsigned char *)s->init_buf->data; |
1063 | |
|
1064 | 0 | redo: |
1065 | | /* Check for buffered CCS */ |
1066 | 0 | if ((s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION |
1067 | 0 | || s->version == DTLS1_BAD_VER) |
1068 | 0 | && s->d1->has_change_cipher_spec && dtls_ccs_expected(s)) { |
1069 | 0 | size_t extra = (s->version == DTLS1_BAD_VER) ? 2 : 0; |
1070 | |
|
1071 | 0 | s->d1->has_change_cipher_spec = 0; |
1072 | 0 | p[0] = SSL3_MT_CCS; |
1073 | | /* |
1074 | | * The extra 2 bytes are never consumed, only checked for |
1075 | | * length -- zero-fill to avoid old init_buf content. |
1076 | | */ |
1077 | 0 | if (extra > 0) |
1078 | 0 | memset(p + 1, 0, extra); |
1079 | 0 | s->init_num = extra; |
1080 | 0 | s->init_msg = p + 1; |
1081 | 0 | s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC; |
1082 | 0 | s->s3.tmp.message_size = extra; |
1083 | 0 | *len = extra; |
1084 | 0 | return 1; |
1085 | 0 | } |
1086 | | |
1087 | | /* see if we have the required fragment already */ |
1088 | 0 | ret = dtls1_retrieve_buffered_fragment(s, &msg_hdr.frag_len); |
1089 | 0 | if (ret < 0) { |
1090 | | /* SSLfatal() already called */ |
1091 | 0 | return 0; |
1092 | 0 | } |
1093 | 0 | if (ret > 0) { |
1094 | 0 | s->init_num = msg_hdr.frag_len; |
1095 | 0 | *len = msg_hdr.frag_len; |
1096 | 0 | return 1; |
1097 | 0 | } |
1098 | | |
1099 | | /* read handshake message header */ |
1100 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, p, |
1101 | 0 | DTLS1_HM_HEADER_LENGTH, 0, &readbytes); |
1102 | 0 | if (i <= 0) { /* nbio, or an error */ |
1103 | 0 | s->rwstate = SSL_READING; |
1104 | 0 | *len = 0; |
1105 | 0 | return 0; |
1106 | 0 | } |
1107 | 0 | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1108 | 0 | if (p[0] != SSL3_MT_CCS) { |
1109 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1110 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1111 | 0 | goto f_err; |
1112 | 0 | } |
1113 | | |
1114 | | /* Buffer CCS for reorder tolerance */ |
1115 | 0 | if (s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION |
1116 | 0 | || s->version == DTLS1_BAD_VER) { |
1117 | 0 | size_t expected = (s->version == DTLS1_BAD_VER) ? 3 : 1; |
1118 | |
|
1119 | 0 | if (readbytes != expected) { |
1120 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
1121 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1122 | 0 | goto f_err; |
1123 | 0 | } |
1124 | 0 | s->d1->has_change_cipher_spec = 1; |
1125 | 0 | goto redo; |
1126 | 0 | } |
1127 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1128 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1129 | 0 | goto f_err; |
1130 | 0 | } |
1131 | 0 | if (recvd_type == SSL3_RT_ACK) { |
1132 | | /* |
1133 | | * An ACK has no handshake message header: the bytes already read |
1134 | | * are body, and an ACK never spans records (RFC 9147 section 4), |
1135 | | * so the rest of the body - if any - is whatever is left in the |
1136 | | * current record. Take it directly to avoid re-entering the |
1137 | | * timeout path while assembling the ACK. |
1138 | | */ |
1139 | 0 | if (readbytes == DTLS1_HM_HEADER_LENGTH |
1140 | 0 | && s->rlayer.curr_rec < s->rlayer.num_recs) { |
1141 | 0 | TLS_RECORD *rr = &s->rlayer.tlsrecs[s->rlayer.curr_rec]; |
1142 | | |
1143 | | /* |
1144 | | * DTLS currently processes one record at a time, so an exhausted |
1145 | | * record is excluded above. Keep these checks to avoid consuming |
1146 | | * a following record if pipelining is added. |
1147 | | */ |
1148 | 0 | if (rr->off > 0 && rr->length > 0) { |
1149 | | /* |
1150 | | * init_buf has capacity for a full plaintext record, whose size |
1151 | | * has already been checked by the record layer. |
1152 | | */ |
1153 | 0 | memcpy(p + DTLS1_HM_HEADER_LENGTH, rr->data + rr->off, |
1154 | 0 | rr->length); |
1155 | 0 | readbytes += rr->length; |
1156 | 0 | if (!ssl_release_record(s, rr, rr->length)) { |
1157 | | /* SSLfatal() already called */ |
1158 | 0 | goto f_err; |
1159 | 0 | } |
1160 | 0 | } |
1161 | 0 | } |
1162 | 0 | s->init_num = readbytes; |
1163 | 0 | s->init_msg = s->init_buf->data; |
1164 | 0 | s->s3.tmp.message_type = DTLS13_MT_ACK; |
1165 | 0 | s->s3.tmp.message_size = readbytes; |
1166 | 0 | *len = readbytes; |
1167 | 0 | return 1; |
1168 | 0 | } |
1169 | | |
1170 | | /* Handshake fails if message header is incomplete */ |
1171 | 0 | if (readbytes != DTLS1_HM_HEADER_LENGTH) { |
1172 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1173 | 0 | goto f_err; |
1174 | 0 | } |
1175 | | |
1176 | | /* parse the message fragment header */ |
1177 | 0 | if (!dtls1_read_hm_header(p, &msg_hdr)) { |
1178 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1179 | 0 | goto f_err; |
1180 | 0 | } |
1181 | | |
1182 | | /* |
1183 | | * We must have at least frag_len bytes left in the record to be read. |
1184 | | * Fragments must not span records. |
1185 | | */ |
1186 | 0 | if (msg_hdr.frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) { |
1187 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1188 | 0 | goto f_err; |
1189 | 0 | } |
1190 | | |
1191 | | /* |
1192 | | * if this is a future (or stale) message it gets buffered |
1193 | | * (or dropped)--no further processing at this time |
1194 | | * While listening, we accept seq 1 (ClientHello with cookie) |
1195 | | * although we're still expecting seq 0 (ClientHello) |
1196 | | * |
1197 | | * A message that only authenticated via a retained previous epoch must |
1198 | | * always be routed to dtls1_process_out_of_seq_message(), even when its |
1199 | | * claimed sequence number happens to match the next expected one: that |
1200 | | * match does not mean this content is actually new. |
1201 | | */ |
1202 | 0 | if (msg_hdr.seq != s->d1->handshake_read_seq |
1203 | 0 | || dtls_record_from_retained_epoch(s)) { |
1204 | 0 | if (!s->server |
1205 | 0 | || msg_hdr.seq != 0 |
1206 | 0 | || s->d1->handshake_read_seq != 1 |
1207 | 0 | || msg_hdr.type != SSL3_MT_CLIENT_HELLO |
1208 | 0 | || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { |
1209 | 0 | *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr); |
1210 | 0 | return 0; |
1211 | 0 | } |
1212 | | /* |
1213 | | * We received a ClientHello and sent back a HelloVerifyRequest. We |
1214 | | * now seem to have received a retransmitted initial ClientHello. That |
1215 | | * is allowed (possibly our HelloVerifyRequest got lost). |
1216 | | */ |
1217 | 0 | chretran = 1; |
1218 | 0 | } |
1219 | | |
1220 | 0 | if (msg_hdr.frag_len > 0 && msg_hdr.frag_len < msg_hdr.msg_len) { |
1221 | 0 | *errtype = dtls1_reassemble_fragment(s, &msg_hdr); |
1222 | 0 | return 0; |
1223 | 0 | } |
1224 | | |
1225 | 0 | if (!s->server |
1226 | 0 | && s->statem.hand_state != TLS_ST_OK |
1227 | 0 | && msg_hdr.type == SSL3_MT_HELLO_REQUEST |
1228 | 0 | && dtls_should_skip_hello_request(s)) { |
1229 | | /* |
1230 | | * The server may always send 'Hello Request' messages -- we are |
1231 | | * doing a handshake anyway now, so ignore them if their format is |
1232 | | * correct. Does not count for 'Finished' MAC. |
1233 | | */ |
1234 | 0 | if (msg_hdr.msg_len == 0) { |
1235 | 0 | if (s->msg_callback) |
1236 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
1237 | 0 | p, DTLS1_HM_HEADER_LENGTH, ussl, |
1238 | 0 | s->msg_callback_arg); |
1239 | |
|
1240 | 0 | s->init_num = 0; |
1241 | 0 | goto redo; |
1242 | 0 | } else { |
1243 | | /* Incorrectly formatted Hello request */ |
1244 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1245 | 0 | goto f_err; |
1246 | 0 | } |
1247 | 0 | } |
1248 | | |
1249 | 0 | if (!dtls1_preprocess_fragment(s, &msg_hdr)) { |
1250 | | /* SSLfatal() already called */ |
1251 | 0 | goto f_err; |
1252 | 0 | } |
1253 | | |
1254 | 0 | if (msg_hdr.frag_len > 0) { |
1255 | | /* dtls1_preprocess_fragment() above could reallocate init_buf */ |
1256 | 0 | p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH + msg_hdr.frag_off; |
1257 | |
|
1258 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
1259 | 0 | p, msg_hdr.frag_len, 0, &readbytes); |
1260 | | |
1261 | | /* |
1262 | | * This shouldn't ever fail due to NBIO because we already checked |
1263 | | * that we have enough data in the record |
1264 | | */ |
1265 | 0 | if (i <= 0) { |
1266 | 0 | s->rwstate = SSL_READING; |
1267 | 0 | *len = 0; |
1268 | 0 | return 0; |
1269 | 0 | } |
1270 | 0 | } else { |
1271 | 0 | readbytes = 0; |
1272 | 0 | } |
1273 | | |
1274 | | /* |
1275 | | * XDTLS: an incorrectly formatted fragment should cause the handshake |
1276 | | * to fail |
1277 | | */ |
1278 | 0 | if (readbytes != msg_hdr.frag_len) { |
1279 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1280 | 0 | goto f_err; |
1281 | 0 | } |
1282 | | |
1283 | 0 | if (chretran) { |
1284 | | /* |
1285 | | * We got a new ClientHello with a message sequence of 0. |
1286 | | * Reset the read/write sequences back to the beginning. |
1287 | | * We process it like this is the first time we've seen a ClientHello |
1288 | | * from the client. |
1289 | | */ |
1290 | 0 | s->d1->handshake_read_seq = 0; |
1291 | 0 | s->d1->next_handshake_write_seq = 0; |
1292 | 0 | } |
1293 | |
|
1294 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr.type) |
1295 | 0 | && !add_record_to_ack_list(s)) { |
1296 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1297 | 0 | goto f_err; |
1298 | 0 | } |
1299 | | |
1300 | | /* |
1301 | | * Note that s->init_num is *not* used as current offset in |
1302 | | * s->init_buf->data, but as a counter summing up fragments' lengths: as |
1303 | | * soon as they sum up to handshake packet length, we assume we have got |
1304 | | * all the fragments. |
1305 | | */ |
1306 | 0 | *len = s->init_num = msg_hdr.frag_len; |
1307 | 0 | return 1; |
1308 | | |
1309 | 0 | f_err: |
1310 | 0 | s->init_num = 0; |
1311 | 0 | *len = 0; |
1312 | 0 | return 0; |
1313 | 0 | } |
1314 | | |
1315 | | /*- |
1316 | | * for these 2 messages, we need to |
1317 | | * ssl->session->read_sym_enc assign |
1318 | | * ssl->session->read_compression assign |
1319 | | * ssl->session->read_hash assign |
1320 | | */ |
1321 | | CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s, |
1322 | | WPACKET *pkt) |
1323 | 0 | { |
1324 | 0 | if (s->version == DTLS1_BAD_VER) { |
1325 | 0 | s->d1->next_handshake_write_seq++; |
1326 | |
|
1327 | 0 | if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) { |
1328 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1329 | 0 | return CON_FUNC_ERROR; |
1330 | 0 | } |
1331 | 0 | } |
1332 | | |
1333 | 0 | return CON_FUNC_SUCCESS; |
1334 | 0 | } |
1335 | | |
1336 | | CON_FUNC_RETURN dtls_construct_ack(SSL_CONNECTION *s, WPACKET *pkt) |
1337 | 0 | { |
1338 | 0 | DTLS1_RECORD_NUMBER *recnum; |
1339 | 0 | DTLS1_RECORD_NUMBER *recnumnext = ossl_list_record_number_head(&s->d1->ack_rec_num); |
1340 | |
|
1341 | 0 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
1342 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1343 | 0 | return CON_FUNC_ERROR; |
1344 | 0 | } |
1345 | | |
1346 | 0 | while ((recnum = recnumnext) != NULL) { |
1347 | | /* |
1348 | | * rfc9147: section 4. |
1349 | | * |
1350 | | * Record numbers are encoded as |
1351 | | * struct { |
1352 | | * uint64 epoch; |
1353 | | * uint64 sequence_number; |
1354 | | * } RecordNumber; |
1355 | | */ |
1356 | |
|
1357 | 0 | recnumnext = ossl_list_record_number_next(recnum); |
1358 | |
|
1359 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s) |
1360 | 0 | || recnum->epoch <= dtls1_get_epoch(s, SSL3_CC_WRITE)) { |
1361 | | /* |
1362 | | * rfc9147: |
1363 | | * During the handshake, ACK records MUST be sent with an epoch which |
1364 | | * is equal to or higher than the record which is being acknowledged. |
1365 | | * After the handshake, the sending and receiving epochs can differ. |
1366 | | */ |
1367 | 0 | if (!WPACKET_put_bytes_u64(pkt, recnum->epoch) |
1368 | 0 | || !WPACKET_put_bytes_u64(pkt, recnum->seqnum)) { |
1369 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1370 | 0 | return CON_FUNC_ERROR; |
1371 | 0 | } |
1372 | | |
1373 | 0 | ossl_list_record_number_remove(&s->d1->ack_rec_num, recnum); |
1374 | 0 | OPENSSL_free(recnum); |
1375 | 0 | } |
1376 | 0 | } |
1377 | | |
1378 | 0 | if (!WPACKET_close(pkt)) { |
1379 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1380 | 0 | return CON_FUNC_ERROR; |
1381 | 0 | } |
1382 | | |
1383 | 0 | return CON_FUNC_SUCCESS; |
1384 | 0 | } |
1385 | | |
1386 | | MSG_PROCESS_RETURN dtls_process_ack(SSL_CONNECTION *s, PACKET *pkt) |
1387 | 0 | { |
1388 | 0 | PACKET record_numbers; |
1389 | |
|
1390 | 0 | if (!PACKET_as_length_prefixed_2(pkt, &record_numbers)) { |
1391 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG); |
1392 | 0 | return MSG_PROCESS_ERROR; |
1393 | 0 | } |
1394 | | |
1395 | 0 | while (PACKET_remaining(&record_numbers) > 0) { |
1396 | | /* |
1397 | | * rfc9147: section 4. |
1398 | | * |
1399 | | * Record numbers are encoded as |
1400 | | * struct { |
1401 | | * uint64 epoch; |
1402 | | * uint64 sequence_number; |
1403 | | * } RecordNumber; |
1404 | | */ |
1405 | 0 | pitem *item; |
1406 | 0 | piterator iter; |
1407 | 0 | uint64_t epoch; |
1408 | 0 | uint64_t sequence_number; |
1409 | |
|
1410 | 0 | if (!PACKET_get_net_8(&record_numbers, &epoch) |
1411 | 0 | || !PACKET_get_net_8(&record_numbers, &sequence_number)) { |
1412 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
1413 | 0 | return MSG_PROCESS_ERROR; |
1414 | 0 | } |
1415 | | |
1416 | | /* |
1417 | | * Epoch 2 is the fixed handshake epoch and is never used again once |
1418 | | * epoch 3 (the first application epoch) is installed. An ACK that |
1419 | | * only authenticated via a retained epoch-2 layer must not be |
1420 | | * trusted to cancel retransmission of a post-handshake (epoch 3+) |
1421 | | * flight. Retained epochs 3+ are unrestricted: unlike epoch 2, which |
1422 | | * is never reissued, each of those epochs was freshly minted by an |
1423 | | * SSL_key_update() call and gets superseded by the next one, so a |
1424 | | * legitimate delayed ACK can authenticate behind a message's own |
1425 | | * recorded epoch with no protocol violation. |
1426 | | */ |
1427 | 0 | if (dtls_record_from_retained_epoch(s) |
1428 | 0 | && s->s3.tmp.record_epoch == 2 |
1429 | 0 | && epoch > 2) |
1430 | 0 | continue; |
1431 | | |
1432 | 0 | iter = pqueue_iterator(&s->d1->sent_messages); |
1433 | |
|
1434 | 0 | while ((item = pqueue_next(&iter)) != NULL) { |
1435 | 0 | dtls_sent_msg *msg = (dtls_sent_msg *)item->data; |
1436 | 0 | DTLS1_RECORD_NUMBER *recnum; |
1437 | 0 | DTLS1_RECORD_NUMBER *recnum_next = ossl_list_record_number_head(&msg->rec_nums); |
1438 | 0 | int matched = 0; |
1439 | |
|
1440 | 0 | while ((recnum = recnum_next) != NULL) { |
1441 | 0 | recnum_next = ossl_list_record_number_next(recnum_next); |
1442 | |
|
1443 | 0 | if (recnum->epoch == epoch && recnum->seqnum == sequence_number) { |
1444 | | /* |
1445 | | * Mark this record's byte range covered *before* |
1446 | | * freeing it -- coverage tracks the message as a whole |
1447 | | * across every transmission round, not just whether |
1448 | | * this one specific record number was ever matched. |
1449 | | * |
1450 | | * The range check guards against a corrupt recorded |
1451 | | * range |
1452 | | */ |
1453 | 0 | if (msg->covered != NULL |
1454 | 0 | && recnum->frag_off <= msg->msg_info.msg_body_len |
1455 | 0 | && recnum->frag_len |
1456 | 0 | <= msg->msg_info.msg_body_len - recnum->frag_off) |
1457 | 0 | RSMBLY_BITMASK_MARK(msg->covered, (long)recnum->frag_off, |
1458 | 0 | (long)(recnum->frag_off + recnum->frag_len)); |
1459 | 0 | ossl_list_record_number_remove(&msg->rec_nums, recnum); |
1460 | 0 | OPENSSL_free(recnum); |
1461 | 0 | matched = 1; |
1462 | 0 | } |
1463 | 0 | } |
1464 | | |
1465 | | /* |
1466 | | * RFC 9147 section 7.2 is a per-record rule, but completeness is |
1467 | | * per-message: the peer needs every byte of the message, from |
1468 | | * any combination of rounds, not just any one matching record. |
1469 | | * Once this ACK completes coverage of the whole message, fully |
1470 | | * drain rec_nums -- not just the node that matched -- so every |
1471 | | * other consumer that keys off list emptiness |
1472 | | * (dtls_any_sent_messages_are_missing_acknowledge(), |
1473 | | * dtls1_clear_sent_buffer(), ...) sees it retire, even though |
1474 | | * other rounds' record numbers may still be sitting unmatched. |
1475 | | */ |
1476 | 0 | if (matched && msg->covered != NULL) { |
1477 | 0 | int is_complete; |
1478 | |
|
1479 | 0 | RSMBLY_BITMASK_IS_COMPLETE(msg->covered, |
1480 | 0 | (long)msg->msg_info.msg_body_len, is_complete); |
1481 | 0 | if (is_complete) |
1482 | 0 | ossl_list_record_number_elem_free(&msg->rec_nums); |
1483 | 0 | } |
1484 | 0 | } |
1485 | 0 | } |
1486 | | |
1487 | | /* Keep the retransmit timer running until the whole flight is ACKed. */ |
1488 | 0 | if (dtls_any_sent_messages_are_missing_acknowledge(s)) |
1489 | 0 | return MSG_PROCESS_CONTINUE_READING; |
1490 | | |
1491 | 0 | return MSG_PROCESS_FINISHED_READING; |
1492 | 0 | } |
1493 | | |
1494 | | #ifndef OPENSSL_NO_SCTP |
1495 | | /* |
1496 | | * Wait for a dry event. Should only be called at a point in the handshake |
1497 | | * where we are not expecting any data from the peer except an alert. |
1498 | | */ |
1499 | | WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s) |
1500 | | { |
1501 | | int ret, errtype; |
1502 | | size_t len; |
1503 | | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1504 | | |
1505 | | /* read app data until dry event */ |
1506 | | ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl)); |
1507 | | if (ret < 0) { |
1508 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1509 | | return WORK_ERROR; |
1510 | | } |
1511 | | |
1512 | | if (ret == 0) { |
1513 | | /* |
1514 | | * We're not expecting any more messages from the peer at this point - |
1515 | | * but we could get an alert. If an alert is waiting then we will never |
1516 | | * return successfully. Therefore we attempt to read a message. This |
1517 | | * should never succeed but will process any waiting alerts. |
1518 | | */ |
1519 | | if (dtls_get_reassembled_message(s, &errtype, &len)) { |
1520 | | /* The call succeeded! This should never happen */ |
1521 | | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1522 | | return WORK_ERROR; |
1523 | | } |
1524 | | |
1525 | | s->s3.in_read_app_data = 2; |
1526 | | s->rwstate = SSL_READING; |
1527 | | BIO_clear_retry_flags(SSL_get_rbio(ssl)); |
1528 | | BIO_set_retry_read(SSL_get_rbio(ssl)); |
1529 | | return WORK_MORE_A; |
1530 | | } |
1531 | | return WORK_FINISHED_CONTINUE; |
1532 | | } |
1533 | | #endif |
1534 | | |
1535 | | int dtls1_read_failed(SSL_CONNECTION *s, int code) |
1536 | 0 | { |
1537 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1538 | |
|
1539 | 0 | if (code > 0) { |
1540 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1541 | 0 | return 0; |
1542 | 0 | } |
1543 | | |
1544 | 0 | if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) { |
1545 | | /* |
1546 | | * not a timeout, none of our business, let higher layers handle |
1547 | | * this. in fact it's probably an error |
1548 | | */ |
1549 | 0 | return code; |
1550 | 0 | } |
1551 | | /* done, no need to send a retransmit */ |
1552 | 0 | if (!SSL_in_init(ssl)) { |
1553 | 0 | BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ); |
1554 | 0 | return code; |
1555 | 0 | } |
1556 | | |
1557 | 0 | return dtls1_handle_timeout(s); |
1558 | 0 | } |
1559 | | |
1560 | | void dtls1_get_queue_priority(unsigned char *prio64be, unsigned short seq, |
1561 | | int record_type) |
1562 | 0 | { |
1563 | | /* |
1564 | | * The index of the retransmission queue actually is the message sequence |
1565 | | * number, since the queue only contains messages of a single handshake. |
1566 | | * However, the ChangeCipherSpec has no message sequence number and so |
1567 | | * using only the sequence will result in the CCS and Finished having the |
1568 | | * same index. To prevent this, the sequence number is multiplied by 2. |
1569 | | * In case of a CCS 1 is subtracted. This does not only differ CSS and |
1570 | | * Finished, it also maintains the order of the index (important for |
1571 | | * priority queues) and fits in the unsigned short variable. |
1572 | | */ |
1573 | 0 | int lsb = (record_type == SSL3_RT_CHANGE_CIPHER_SPEC); |
1574 | 0 | const uint16_t prio = seq * 2 - lsb; |
1575 | |
|
1576 | 0 | memset(prio64be, 0, 8); |
1577 | 0 | prio64be[6] = (unsigned char)(prio >> 8); |
1578 | 0 | prio64be[7] = (unsigned char)(prio); |
1579 | 0 | } |
1580 | | |
1581 | | int dtls1_retransmit_sent_messages(SSL_CONNECTION *s) |
1582 | 0 | { |
1583 | 0 | piterator iter = pqueue_iterator(&s->d1->sent_messages); |
1584 | 0 | pitem *item; |
1585 | |
|
1586 | 0 | for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) { |
1587 | 0 | dtls_sent_msg *sent_msg = (dtls_sent_msg *)item->data; |
1588 | |
|
1589 | 0 | if (SSL_CONNECTION_IS_DTLS13(s) |
1590 | 0 | && ossl_list_record_number_is_empty(&sent_msg->rec_nums)) |
1591 | | /* rfc9147: Implementations must not retransmit acknowledged msgs */ |
1592 | 0 | continue; |
1593 | | |
1594 | 0 | if (dtls1_retransmit_message(s, sent_msg) <= 0) |
1595 | 0 | return -1; |
1596 | 0 | } |
1597 | | |
1598 | 0 | return 1; |
1599 | 0 | } |
1600 | | |
1601 | | int dtls1_buffer_sent_message(SSL_CONNECTION *s, int record_type) |
1602 | 0 | { |
1603 | 0 | pitem *item; |
1604 | 0 | dtls_sent_msg *sent_msg; |
1605 | 0 | unsigned char seq64be[8]; |
1606 | 0 | size_t headerlen; |
1607 | | |
1608 | | /* |
1609 | | * this function is called immediately after a message has been |
1610 | | * serialized |
1611 | | */ |
1612 | 0 | if (!ossl_assert(s->init_off == 0)) |
1613 | 0 | return 0; |
1614 | | |
1615 | 0 | sent_msg = dtls1_sent_msg_new(s->init_num, s->d1->w_msg.msg_body_len, |
1616 | 0 | SSL_CONNECTION_IS_DTLS13(s)); |
1617 | 0 | if (sent_msg == NULL) |
1618 | 0 | return 0; |
1619 | | |
1620 | 0 | memcpy(sent_msg->msg_buf, s->init_buf->data, s->init_num); |
1621 | |
|
1622 | 0 | if (record_type == SSL3_RT_CHANGE_CIPHER_SPEC) |
1623 | | /* For DTLS1_BAD_VER the header length is non-standard */ |
1624 | 0 | headerlen = (s->version == DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH; |
1625 | 0 | else |
1626 | 0 | headerlen = DTLS1_HM_HEADER_LENGTH; |
1627 | |
|
1628 | 0 | if (!ossl_assert(s->d1->w_msg.msg_body_len + headerlen == s->init_num)) { |
1629 | 0 | dtls1_sent_msg_free(sent_msg); |
1630 | 0 | return 0; |
1631 | 0 | } |
1632 | | |
1633 | 0 | memcpy(&sent_msg->msg_info, &s->d1->w_msg, sizeof(s->d1->w_msg)); |
1634 | | |
1635 | | /* save current state */ |
1636 | 0 | sent_msg->saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod; |
1637 | 0 | sent_msg->saved_retransmit_state.wrl = s->rlayer.wrl; |
1638 | |
|
1639 | 0 | dtls1_get_queue_priority(seq64be, sent_msg->msg_info.msg_seq, sent_msg->msg_info.record_type); |
1640 | |
|
1641 | 0 | item = pitem_new(seq64be, sent_msg); |
1642 | 0 | if (item == NULL) { |
1643 | 0 | dtls1_sent_msg_free(sent_msg); |
1644 | 0 | return 0; |
1645 | 0 | } |
1646 | | |
1647 | 0 | if (pqueue_insert(&s->d1->sent_messages, item) == NULL) { |
1648 | 0 | dtls1_sent_msg_free(sent_msg); |
1649 | 0 | pitem_free(item); |
1650 | 0 | return 0; |
1651 | 0 | } |
1652 | 0 | return 1; |
1653 | 0 | } |
1654 | | |
1655 | | int dtls1_retransmit_message(SSL_CONNECTION *s, dtls_sent_msg *sent_msg) |
1656 | 0 | { |
1657 | 0 | int ret; |
1658 | 0 | unsigned long header_length; |
1659 | 0 | struct dtls1_retransmit_state saved_state; |
1660 | |
|
1661 | 0 | if (sent_msg->msg_info.record_type == SSL3_RT_CHANGE_CIPHER_SPEC) |
1662 | 0 | header_length = DTLS1_CCS_HEADER_LENGTH; |
1663 | 0 | else |
1664 | 0 | header_length = DTLS1_HM_HEADER_LENGTH; |
1665 | | |
1666 | | /* |
1667 | | * Deliberately not clearing rec_nums here: RFC 9147 section 7.2 requires |
1668 | | * treating a record as acknowledged if it appears in *any* ACK, so a |
1669 | | * late ACK matching an earlier round's record number must still be able |
1670 | | * to match something. rec_nums accumulates across every retransmission |
1671 | | * round instead; dtls_process_ack() retires entries by tracking byte |
1672 | | * range coverage, not by this list ever being reset per round. |
1673 | | */ |
1674 | |
|
1675 | 0 | memcpy(s->init_buf->data, sent_msg->msg_buf, |
1676 | 0 | sent_msg->msg_info.msg_body_len + header_length); |
1677 | 0 | s->init_num = sent_msg->msg_info.msg_body_len + header_length; |
1678 | | /* Always retransmit from the start, not wherever init_off was left */ |
1679 | 0 | s->init_off = 0; |
1680 | |
|
1681 | 0 | memcpy(&s->d1->w_msg, &sent_msg->msg_info, sizeof(sent_msg->msg_info)); |
1682 | | |
1683 | | /* save current state */ |
1684 | 0 | saved_state.wrlmethod = s->rlayer.wrlmethod; |
1685 | 0 | saved_state.wrl = s->rlayer.wrl; |
1686 | |
|
1687 | 0 | s->d1->retransmitting = 1; |
1688 | | |
1689 | | /* restore state in which the message was originally sent */ |
1690 | 0 | s->rlayer.wrlmethod = sent_msg->saved_retransmit_state.wrlmethod; |
1691 | 0 | s->rlayer.wrl = sent_msg->saved_retransmit_state.wrl; |
1692 | | |
1693 | | /* |
1694 | | * The old wrl may be still pointing at an old BIO. Update it to what we're |
1695 | | * using now. |
1696 | | */ |
1697 | 0 | s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio); |
1698 | |
|
1699 | 0 | ret = dtls1_do_write(s, sent_msg->msg_info.record_type); |
1700 | | |
1701 | | /* restore current state */ |
1702 | 0 | s->rlayer.wrlmethod = saved_state.wrlmethod; |
1703 | 0 | s->rlayer.wrl = saved_state.wrl; |
1704 | |
|
1705 | 0 | s->d1->retransmitting = 0; |
1706 | |
|
1707 | 0 | (void)BIO_flush(s->wbio); |
1708 | 0 | return ret; |
1709 | 0 | } |
1710 | | |
1711 | | int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype) |
1712 | 0 | { |
1713 | 0 | s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
1714 | 0 | s->d1->w_msg.msg_seq = s->d1->handshake_write_seq; |
1715 | 0 | s->d1->w_msg.msg_body_len = 0; |
1716 | |
|
1717 | 0 | if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) { |
1718 | 0 | s->d1->w_msg.record_type = SSL3_RT_CHANGE_CIPHER_SPEC; |
1719 | 0 | s->d1->w_msg.msg_type = SSL3_MT_CCS; |
1720 | |
|
1721 | 0 | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) |
1722 | 0 | return 0; |
1723 | 0 | } else if (htype == DTLS13_MT_ACK) { |
1724 | 0 | s->d1->w_msg.record_type = SSL3_RT_ACK; |
1725 | 0 | s->d1->w_msg.msg_type = 0; |
1726 | 0 | } else { |
1727 | 0 | size_t subpacket_offset = DTLS1_HM_HEADER_LENGTH - SSL3_HM_HEADER_LENGTH; |
1728 | |
|
1729 | 0 | s->d1->next_handshake_write_seq++; |
1730 | 0 | s->d1->w_msg.record_type = SSL3_RT_HANDSHAKE; |
1731 | 0 | s->d1->w_msg.msg_type = htype; |
1732 | | |
1733 | | /* Set the content type and 3 bytes for the message len */ |
1734 | 0 | if (!WPACKET_put_bytes_u8(pkt, htype) |
1735 | | /* |
1736 | | * We allocate space for DTLS specific fields. |
1737 | | * These gets filled later. |
1738 | | */ |
1739 | 0 | || !WPACKET_start_sub_packet_u24_at_offset(pkt, subpacket_offset)) |
1740 | 0 | return 0; |
1741 | 0 | } |
1742 | | |
1743 | 0 | return 1; |
1744 | 0 | } |
1745 | | |
1746 | | int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype) |
1747 | 0 | { |
1748 | 0 | size_t msglen; |
1749 | |
|
1750 | 0 | if ((s->d1->w_msg.record_type == SSL3_RT_HANDSHAKE && !WPACKET_close(pkt)) |
1751 | 0 | || !WPACKET_get_length(pkt, &msglen) |
1752 | 0 | || msglen > INT_MAX) |
1753 | 0 | return 0; |
1754 | | |
1755 | 0 | if (s->d1->w_msg.record_type == SSL3_RT_HANDSHAKE) |
1756 | 0 | s->d1->w_msg.msg_body_len = msglen - DTLS1_HM_HEADER_LENGTH; |
1757 | |
|
1758 | 0 | s->init_num = msglen; |
1759 | 0 | s->init_off = 0; |
1760 | |
|
1761 | 0 | if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST |
1762 | 0 | && s->d1->w_msg.record_type != SSL3_RT_ACK) { |
1763 | | /* Buffer the message to handle re-xmits */ |
1764 | 0 | if (!dtls1_buffer_sent_message(s, s->d1->w_msg.record_type)) |
1765 | 0 | return 0; |
1766 | 0 | } |
1767 | | |
1768 | 0 | return 1; |
1769 | 0 | } |