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