/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) |
115 | 0 | { |
116 | 0 | dtls_sent_msg *msg = OPENSSL_malloc(sizeof(*msg) + msg_len); |
117 | |
|
118 | 0 | if (msg == NULL) |
119 | 0 | return NULL; |
120 | | |
121 | 0 | memset(msg, 0, sizeof(*msg)); |
122 | | |
123 | | /* zero length msg gets msg->msg_buf == NULL */ |
124 | 0 | if (msg_len > 0) |
125 | 0 | msg->msg_buf = (unsigned char *)(msg + 1); |
126 | |
|
127 | 0 | return msg; |
128 | 0 | } |
129 | | |
130 | | void dtls1_sent_msg_free(dtls_sent_msg *msg) |
131 | 0 | { |
132 | 0 | if (msg != NULL) |
133 | 0 | ossl_list_record_number_elem_free(&msg->rec_nums); |
134 | |
|
135 | 0 | OPENSSL_free(msg); |
136 | 0 | } |
137 | | |
138 | | static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly) |
139 | 0 | { |
140 | 0 | const size_t bitmask_len = (reassembly ? RSMBLY_BITMASK_SIZE(frag_len) : 0); |
141 | 0 | hm_fragment *frag = OPENSSL_malloc(sizeof(*frag) + frag_len + bitmask_len); |
142 | |
|
143 | 0 | if (frag == NULL) |
144 | 0 | return NULL; |
145 | | |
146 | 0 | memset(frag, 0, sizeof(*frag)); |
147 | | |
148 | | /* zero length fragment gets zero frag->fragment */ |
149 | 0 | if (frag_len > 0) |
150 | 0 | frag->fragment = (unsigned char *)(frag + 1); |
151 | | |
152 | | /* Initialize reassembly bitmask if necessary */ |
153 | 0 | if (bitmask_len > 0) { |
154 | 0 | if (frag->fragment == NULL) |
155 | 0 | frag->reassembly = (unsigned char *)(frag + 1); |
156 | 0 | else |
157 | 0 | frag->reassembly = frag->fragment + frag_len; |
158 | |
|
159 | 0 | memset(frag->reassembly, 0, bitmask_len); |
160 | 0 | } |
161 | |
|
162 | 0 | return frag; |
163 | 0 | } |
164 | | |
165 | | void dtls1_hm_fragment_free(hm_fragment *frag) |
166 | 0 | { |
167 | 0 | OPENSSL_free(frag); |
168 | 0 | } |
169 | | |
170 | | static int dtls1_write_hm_header(unsigned char *msgheaderstart, |
171 | | unsigned char msg_type, size_t msg_len, |
172 | | unsigned short msg_seq, size_t fragoff, |
173 | | size_t fraglen) |
174 | 0 | { |
175 | 0 | WPACKET msgheader; |
176 | 0 | size_t msgheaderlen; |
177 | |
|
178 | 0 | if (!WPACKET_init_static_len(&msgheader, msgheaderstart, |
179 | 0 | DTLS1_HM_HEADER_LENGTH, 0) |
180 | 0 | || !WPACKET_put_bytes_u8(&msgheader, msg_type) |
181 | 0 | || !WPACKET_put_bytes_u24(&msgheader, msg_len) |
182 | 0 | || !WPACKET_put_bytes_u16(&msgheader, msg_seq) |
183 | 0 | || !WPACKET_put_bytes_u24(&msgheader, fragoff) |
184 | 0 | || !WPACKET_put_bytes_u24(&msgheader, fraglen) |
185 | 0 | || !WPACKET_get_total_written(&msgheader, &msgheaderlen) |
186 | 0 | || msgheaderlen != DTLS1_HM_HEADER_LENGTH |
187 | 0 | || !WPACKET_finish(&msgheader)) { |
188 | 0 | WPACKET_cleanup(&msgheader); |
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 0 | return 1; |
193 | 0 | } |
194 | | |
195 | | /* |
196 | | * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or |
197 | | * SSL3_RT_CHANGE_CIPHER_SPEC) |
198 | | * |
199 | | * When sending a fragmented handshake message this function will re-use |
200 | | * s->init_buf->data but overwrite previously sent data to fill out the handshake |
201 | | * message header for the next fragment. |
202 | | * |
203 | | * E.g. |
204 | | * |-------------------------s->init_buf->data------------------------------| |
205 | | * |-- header1 --||-- fragment1 --| |
206 | | * |-- header2 --||-- fragment2 --| |
207 | | * |-- header3 --||-- fragment3 --| |
208 | | * ......... |
209 | | */ |
210 | | int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype) |
211 | 0 | { |
212 | 0 | int ret; |
213 | 0 | size_t written; |
214 | 0 | size_t curr_mtu; |
215 | 0 | int retry = 1; |
216 | 0 | size_t len, overhead, used_len; |
217 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
218 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
219 | 0 | uint8_t saved_payload[DTLS1_HM_HEADER_LENGTH]; |
220 | | /* msg_len, msg_seq, msg_type are only used for recordtype == SSL3_RT_HANDSHAKE */ |
221 | 0 | const size_t msg_len = s->d1->w_msg.msg_body_len; |
222 | 0 | const unsigned short msg_seq = s->d1->w_msg.msg_seq; |
223 | 0 | const unsigned char msg_type = s->d1->w_msg.msg_type; |
224 | |
|
225 | 0 | if (!dtls1_query_mtu(s)) |
226 | 0 | return -1; |
227 | | |
228 | 0 | if (s->d1->mtu < dtls1_min_mtu(s)) |
229 | | /* should have something reasonable now */ |
230 | 0 | return -1; |
231 | | |
232 | 0 | if (s->init_off == 0 && recordtype == SSL3_RT_HANDSHAKE) { |
233 | 0 | if (!ossl_assert(s->init_num == msg_len + DTLS1_HM_HEADER_LENGTH)) |
234 | 0 | return -1; |
235 | 0 | } |
236 | | |
237 | 0 | overhead = s->rlayer.wrlmethod->get_max_record_overhead(s->rlayer.wrl); |
238 | |
|
239 | 0 | s->rwstate = SSL_NOTHING; |
240 | | |
241 | | /* s->init_num shouldn't ever be < 0...but just in case */ |
242 | 0 | while (s->init_num > 0) { |
243 | 0 | unsigned char *msgstart; |
244 | |
|
245 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && s->init_off > 0) { |
246 | | /* |
247 | | * We must be writing a fragment other than the first one |
248 | | * and this is the first attempt at writing out this fragment |
249 | | */ |
250 | 0 | if (s->init_off <= DTLS1_HM_HEADER_LENGTH) { |
251 | | /* |
252 | | * Each fragment that was already sent must at least have |
253 | | * contained the message header plus one other byte. |
254 | | * Therefore |init_off| must have progressed by at least |
255 | | * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went |
256 | | * wrong. |
257 | | */ |
258 | 0 | return -1; |
259 | 0 | } |
260 | | |
261 | | /* |
262 | | * Adjust |init_off| and |init_num| to allow room for a new |
263 | | * message header for this fragment. |
264 | | */ |
265 | 0 | s->init_off -= DTLS1_HM_HEADER_LENGTH; |
266 | 0 | s->init_num += DTLS1_HM_HEADER_LENGTH; |
267 | 0 | } |
268 | | |
269 | 0 | used_len = BIO_wpending(s->wbio) + overhead; |
270 | 0 | if (s->d1->mtu > used_len) |
271 | 0 | curr_mtu = s->d1->mtu - used_len; |
272 | 0 | else |
273 | 0 | curr_mtu = 0; |
274 | |
|
275 | 0 | if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { |
276 | | /* |
277 | | * grr.. we could get an error if MTU picked was wrong |
278 | | */ |
279 | 0 | ret = BIO_flush(s->wbio); |
280 | 0 | if (ret <= 0) { |
281 | 0 | s->rwstate = SSL_WRITING; |
282 | 0 | return ret; |
283 | 0 | } |
284 | 0 | if (s->d1->mtu > overhead + DTLS1_HM_HEADER_LENGTH) { |
285 | 0 | curr_mtu = s->d1->mtu - overhead; |
286 | 0 | } else { |
287 | | /* Shouldn't happen */ |
288 | 0 | return -1; |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | 0 | if (s->init_num > curr_mtu) |
293 | 0 | len = curr_mtu; |
294 | 0 | else |
295 | 0 | len = s->init_num; |
296 | |
|
297 | 0 | if (len > ssl_get_max_send_fragment(s)) |
298 | 0 | len = ssl_get_max_send_fragment(s); |
299 | | |
300 | | /* |
301 | | * For DTLS1.3 and padding lets update the max fragment size |
302 | | * accordingly |
303 | | */ |
304 | 0 | if (SSL_CONNECTION_IS_DTLS13(s)) |
305 | 0 | s->rlayer.wrlmethod->set_curr_mtu(s->rlayer.wrl, curr_mtu); |
306 | |
|
307 | 0 | msgstart = (unsigned char *)&s->init_buf->data[s->init_off]; |
308 | |
|
309 | 0 | if (recordtype == SSL3_RT_HANDSHAKE) { |
310 | 0 | const size_t fragoff = s->init_off; |
311 | 0 | const size_t fraglen = len - DTLS1_HM_HEADER_LENGTH; |
312 | | |
313 | | /* |
314 | | * Save the data that will be overwritten by |
315 | | * the following code so no corruption occurs when using |
316 | | * a msg callback. |
317 | | */ |
318 | 0 | if (s->msg_callback && s->init_off != 0) |
319 | 0 | memcpy(saved_payload, msgstart, sizeof(saved_payload)); |
320 | |
|
321 | 0 | if (len < DTLS1_HM_HEADER_LENGTH |
322 | 0 | || !dtls1_write_hm_header(msgstart, msg_type, msg_len, |
323 | 0 | msg_seq, fragoff, fraglen)) |
324 | | /* |
325 | | * len is so small that we really can't do anything sensible |
326 | | * so fail |
327 | | */ |
328 | 0 | return -1; |
329 | 0 | } |
330 | | |
331 | 0 | ret = dtls1_write_bytes(s, recordtype, msgstart, len, |
332 | 0 | &written); |
333 | |
|
334 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && s->msg_callback && s->init_off != 0) |
335 | 0 | memcpy(msgstart, saved_payload, sizeof(saved_payload)); |
336 | |
|
337 | 0 | if (ret <= 0) { |
338 | | /* |
339 | | * might need to update MTU here, but we don't know which |
340 | | * previous packet caused the failure -- so can't really |
341 | | * retransmit anything. continue as if everything is fine and |
342 | | * wait for an alert to handle the retransmit |
343 | | */ |
344 | 0 | if (retry && BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0 |
345 | 0 | && !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU) |
346 | 0 | && dtls1_query_mtu(s)) |
347 | | /* Have one more go */ |
348 | 0 | retry = 0; |
349 | 0 | else |
350 | 0 | return -1; |
351 | 0 | } else { |
352 | | |
353 | | /* |
354 | | * bad if this assert fails, only part of the handshake message |
355 | | * got sent. but why would this happen? |
356 | | */ |
357 | 0 | if (!ossl_assert(len == written)) |
358 | 0 | return -1; |
359 | | |
360 | | /* |
361 | | * We should not exceed the MTU size. If compression is in use |
362 | | * then the max record overhead calculation is unreliable so we do |
363 | | * not check in that case. We use assert rather than ossl_assert |
364 | | * because in a production build, if this assert were ever to fail, |
365 | | * then the best thing to do is probably carry on regardless. |
366 | | */ |
367 | 0 | assert(s->s3.tmp.new_compression != NULL |
368 | 0 | || BIO_wpending(s->wbio) <= (int)s->d1->mtu); |
369 | |
|
370 | 0 | if (recordtype == SSL3_RT_HANDSHAKE && !s->d1->retransmitting |
371 | 0 | && s->init_off == 0) { |
372 | 0 | size_t xlen = s->init_num; |
373 | |
|
374 | 0 | if (s->version == DTLS1_BAD_VER) { |
375 | 0 | msgstart += DTLS1_HM_HEADER_LENGTH; |
376 | 0 | xlen -= DTLS1_HM_HEADER_LENGTH; |
377 | 0 | } else { |
378 | | /* |
379 | | * Now prepare to calculate the transcript hash. For |
380 | | * versions prior to DTLSv1.3 this means: |
381 | | * |
382 | | * rfc6347: Hash calculations include entire handshake |
383 | | * messages, including DTLS-specific fields: message_seq, |
384 | | * fragment_offset, and fragment_length. However, the |
385 | | * Finished MAC MUST be computed as if each handshake |
386 | | * message had been sent as a single fragment. |
387 | | * |
388 | | * For DTLSv1.3 DTLS-specific fields: message_seq, |
389 | | * fragment_offset, and fragment_length are not used in the |
390 | | * calculation i.e. the MAC-calculation is the same as TLS |
391 | | * even though DTLS handshake messages may be fragmented. |
392 | | */ |
393 | 0 | if (!dtls1_write_hm_header(msgstart, msg_type, msg_len, |
394 | 0 | msg_seq, 0, msg_len)) |
395 | 0 | return -1; |
396 | 0 | } |
397 | | |
398 | | /* |
399 | | * should not be done for 'Hello Request's, but in that case we'll |
400 | | * ignore the result anyway |
401 | | * DTLS1.3 KeyUpdate and NewSessionTicket do not need to be added |
402 | | */ |
403 | 0 | if (!SSL_CONNECTION_IS_DTLS13(s) |
404 | 0 | || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET |
405 | 0 | && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE |
406 | 0 | && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE)) |
407 | 0 | if (!ssl3_finish_mac(s, msgstart, xlen)) |
408 | 0 | return -1; |
409 | 0 | } |
410 | 0 | if (written == s->init_num) { |
411 | 0 | if (s->msg_callback) |
412 | 0 | s->msg_callback(1, s->version, recordtype, s->init_buf->data, |
413 | 0 | s->init_off + s->init_num, ussl, |
414 | 0 | s->msg_callback_arg); |
415 | |
|
416 | 0 | s->init_off = 0; /* done writing this message */ |
417 | 0 | s->init_num = 0; |
418 | |
|
419 | 0 | return 1; |
420 | 0 | } |
421 | 0 | s->init_off += written; |
422 | 0 | s->init_num -= written; |
423 | 0 | written -= DTLS1_HM_HEADER_LENGTH; |
424 | 0 | } |
425 | 0 | } |
426 | 0 | return 0; |
427 | 0 | } |
428 | | |
429 | | int dtls_get_message(SSL_CONNECTION *s, int *mt) |
430 | 0 | { |
431 | 0 | unsigned char *rec_data; |
432 | 0 | size_t tmplen; |
433 | 0 | int errtype; |
434 | |
|
435 | 0 | s->d1->r_msg_seq = 0; |
436 | |
|
437 | 0 | again: |
438 | 0 | if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) { |
439 | 0 | if (errtype == DTLS1_HM_BAD_FRAGMENT |
440 | 0 | || errtype == DTLS1_HM_FRAGMENT_RETRY) { |
441 | | /* bad fragment received */ |
442 | 0 | goto again; |
443 | 0 | } |
444 | 0 | return 0; |
445 | 0 | } |
446 | | |
447 | 0 | *mt = s->s3.tmp.message_type; |
448 | |
|
449 | 0 | rec_data = (unsigned char *)s->init_buf->data; |
450 | | |
451 | | /* |
452 | | * If this isn't a real handshake message skip the processing below. |
453 | | */ |
454 | 0 | if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC || *mt == DTLS13_MT_ACK) |
455 | 0 | return 1; |
456 | | |
457 | | /* reconstruct message header */ |
458 | 0 | dtls1_write_hm_header(rec_data, s->s3.tmp.message_type, s->s3.tmp.message_size, |
459 | 0 | s->d1->r_msg_seq, 0, s->s3.tmp.message_size); |
460 | |
|
461 | 0 | s->d1->r_msg_seq = 0; |
462 | |
|
463 | 0 | s->d1->handshake_read_seq++; |
464 | |
|
465 | 0 | s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
466 | |
|
467 | 0 | return 1; |
468 | 0 | } |
469 | | |
470 | | /* |
471 | | * Actually we already have the message body - but this is an opportunity for |
472 | | * DTLS to do any further processing it wants at the same point that TLS would |
473 | | * be asked for the message body. |
474 | | */ |
475 | | int dtls_get_message_body(SSL_CONNECTION *s, size_t *len) |
476 | 0 | { |
477 | 0 | unsigned char *msg = (unsigned char *)s->init_buf->data; |
478 | 0 | size_t msg_len; |
479 | 0 | int recordtype; |
480 | |
|
481 | 0 | switch (s->s3.tmp.message_type) { |
482 | 0 | default: |
483 | 0 | recordtype = SSL3_RT_HANDSHAKE; |
484 | 0 | msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH; |
485 | |
|
486 | 0 | break; |
487 | 0 | case DTLS13_MT_ACK: |
488 | 0 | recordtype = SSL3_RT_ACK; |
489 | 0 | msg_len = s->init_num; |
490 | |
|
491 | 0 | goto end; |
492 | 0 | case SSL3_MT_CHANGE_CIPHER_SPEC: |
493 | 0 | recordtype = SSL3_RT_CHANGE_CIPHER_SPEC; |
494 | 0 | msg_len = 1; |
495 | |
|
496 | 0 | goto end; |
497 | 0 | } |
498 | | |
499 | | /* |
500 | | * If receiving Finished, record MAC of prior handshake messages for |
501 | | * Finished verification. |
502 | | */ |
503 | 0 | if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) { |
504 | | /* SSLfatal() already called */ |
505 | 0 | return 0; |
506 | 0 | } |
507 | | |
508 | 0 | if (!tls_common_finish_mac(s)) |
509 | 0 | return 0; |
510 | | |
511 | 0 | end: |
512 | 0 | if (s->msg_callback) |
513 | 0 | s->msg_callback(0, s->version, recordtype, msg, msg_len, |
514 | 0 | SSL_CONNECTION_GET_USER_SSL(s), s->msg_callback_arg); |
515 | |
|
516 | 0 | *len = s->init_num; |
517 | 0 | return 1; |
518 | 0 | } |
519 | | |
520 | | /* |
521 | | * dtls1_max_handshake_message_len returns the maximum number of bytes |
522 | | * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but |
523 | | * may be greater if the maximum certificate list size requires it. |
524 | | */ |
525 | | static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s) |
526 | 0 | { |
527 | 0 | size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; |
528 | 0 | if (max_len < s->max_cert_list) |
529 | 0 | return s->max_cert_list; |
530 | 0 | return max_len; |
531 | 0 | } |
532 | | |
533 | | static int dtls1_preprocess_fragment(SSL_CONNECTION *s, |
534 | | const struct hm_header_st *const msg_hdr) |
535 | 0 | { |
536 | 0 | size_t frag_off, frag_len, msg_len; |
537 | |
|
538 | 0 | msg_len = msg_hdr->msg_len; |
539 | 0 | frag_off = msg_hdr->frag_off; |
540 | 0 | frag_len = msg_hdr->frag_len; |
541 | | |
542 | | /* sanity checking */ |
543 | 0 | if ((frag_off + frag_len) > msg_len |
544 | 0 | || msg_len > dtls1_max_handshake_message_len(s)) { |
545 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
546 | 0 | return 0; |
547 | 0 | } |
548 | | |
549 | | /* |
550 | | * msg_len is limited to 2^24, but is effectively checked against |
551 | | * dtls_max_handshake_message_len(s) above |
552 | | */ |
553 | 0 | if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) { |
554 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB); |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 0 | s->s3.tmp.message_size = msg_len; |
559 | 0 | s->s3.tmp.message_type = msg_hdr->type; |
560 | 0 | s->d1->r_msg_seq = msg_hdr->seq; |
561 | |
|
562 | 0 | return 1; |
563 | 0 | } |
564 | | |
565 | | static int add_record_to_ack_list(SSL_CONNECTION *sc) |
566 | 0 | { |
567 | 0 | DTLS1_RECORD_NUMBER *recnum; |
568 | 0 | uint64_t epoch = sc->s3.tmp.record_epoch; |
569 | 0 | uint64_t sequence = sc->s3.tmp.record_seq_num; |
570 | |
|
571 | 0 | for (recnum = ossl_list_record_number_head(&sc->d1->ack_rec_num); |
572 | 0 | recnum != NULL; |
573 | 0 | recnum = ossl_list_record_number_next(recnum)) { |
574 | | /* Is the record number already in the list? */ |
575 | 0 | if (recnum->epoch == epoch && recnum->seqnum == sequence) |
576 | 0 | return 1; |
577 | 0 | } |
578 | | |
579 | 0 | recnum = dtls1_record_number_new(epoch, sequence); |
580 | |
|
581 | 0 | if (recnum == NULL) |
582 | 0 | return 0; |
583 | | |
584 | 0 | ossl_list_record_number_insert_tail(&sc->d1->ack_rec_num, recnum); |
585 | |
|
586 | 0 | return 1; |
587 | 0 | } |
588 | | |
589 | | /* |
590 | | * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a |
591 | | * fatal error. |
592 | | */ |
593 | | static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len) |
594 | 0 | { |
595 | | /*- |
596 | | * (0) check whether the desired fragment is available |
597 | | * if so: |
598 | | * (1) copy over the fragment to s->init_buf->data[] |
599 | | * (2) update s->init_num |
600 | | */ |
601 | 0 | pitem *item; |
602 | 0 | piterator iter; |
603 | 0 | hm_fragment *frag; |
604 | 0 | int ret; |
605 | 0 | int chretran = 0; |
606 | 0 | pqueue *rcvd_messages = &s->d1->rcvd_messages; |
607 | |
|
608 | 0 | iter = pqueue_iterator(rcvd_messages); |
609 | 0 | do { |
610 | 0 | item = pqueue_next(&iter); |
611 | 0 | if (item == NULL) |
612 | 0 | return 0; |
613 | | |
614 | 0 | frag = (hm_fragment *)item->data; |
615 | |
|
616 | 0 | if (frag->msg_header.seq < s->d1->handshake_read_seq) { |
617 | 0 | pitem *next; |
618 | 0 | hm_fragment *nextfrag; |
619 | |
|
620 | 0 | if (!s->server |
621 | 0 | || frag->msg_header.seq != 0 |
622 | 0 | || s->d1->handshake_read_seq != 1 |
623 | 0 | || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { |
624 | | /* |
625 | | * This is a stale message that has been buffered so clear it. |
626 | | * It is safe to pop this message from the queue even though |
627 | | * we have an active iterator |
628 | | */ |
629 | 0 | pqueue_pop(rcvd_messages); |
630 | 0 | dtls1_hm_fragment_free(frag); |
631 | 0 | pitem_free(item); |
632 | 0 | item = NULL; |
633 | 0 | frag = NULL; |
634 | 0 | } else { |
635 | | /* |
636 | | * We have fragments for a ClientHello without a cookie, |
637 | | * even though we have sent a HelloVerifyRequest. It is possible |
638 | | * that the HelloVerifyRequest got lost and this is a |
639 | | * retransmission of the original ClientHello |
640 | | */ |
641 | 0 | next = pqueue_next(&iter); |
642 | 0 | if (next != NULL) { |
643 | 0 | nextfrag = (hm_fragment *)next->data; |
644 | 0 | if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) { |
645 | | /* |
646 | | * We have fragments for both a ClientHello without |
647 | | * cookie and one with. Ditch the one without. |
648 | | */ |
649 | 0 | pqueue_pop(rcvd_messages); |
650 | 0 | dtls1_hm_fragment_free(frag); |
651 | 0 | pitem_free(item); |
652 | 0 | item = next; |
653 | 0 | frag = nextfrag; |
654 | 0 | } else { |
655 | 0 | chretran = 1; |
656 | 0 | } |
657 | 0 | } else { |
658 | 0 | chretran = 1; |
659 | 0 | } |
660 | 0 | } |
661 | 0 | } |
662 | 0 | } while (item == NULL); |
663 | | |
664 | | /* Don't return if reassembly still in progress */ |
665 | 0 | if (frag->reassembly != NULL) |
666 | 0 | return 0; |
667 | | |
668 | 0 | if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) { |
669 | 0 | size_t frag_len = frag->msg_header.frag_len; |
670 | |
|
671 | 0 | pqueue_pop(rcvd_messages); |
672 | | |
673 | | /* Calls SSLfatal() as required */ |
674 | 0 | ret = dtls1_preprocess_fragment(s, &frag->msg_header); |
675 | |
|
676 | 0 | if (ret && frag->msg_header.frag_len > 0) { |
677 | 0 | unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
678 | 0 | memcpy(&p[frag->msg_header.frag_off], frag->fragment, |
679 | 0 | frag->msg_header.frag_len); |
680 | 0 | } |
681 | |
|
682 | 0 | dtls1_hm_fragment_free(frag); |
683 | 0 | pitem_free(item); |
684 | |
|
685 | 0 | if (ret) { |
686 | 0 | if (chretran) { |
687 | | /* |
688 | | * We got a new ClientHello with a message sequence of 0. |
689 | | * Reset the read/write sequences back to the beginning. |
690 | | * We process it like this is the first time we've seen a |
691 | | * ClientHello from the client. |
692 | | */ |
693 | 0 | s->d1->handshake_read_seq = 0; |
694 | 0 | s->d1->next_handshake_write_seq = 0; |
695 | 0 | } |
696 | 0 | *len = frag_len; |
697 | 0 | return 1; |
698 | 0 | } |
699 | | |
700 | | /* Fatal error */ |
701 | 0 | s->init_num = 0; |
702 | 0 | return -1; |
703 | 0 | } else { |
704 | 0 | return 0; |
705 | 0 | } |
706 | 0 | } |
707 | | |
708 | | static int dtls1_reassemble_fragment(SSL_CONNECTION *s, |
709 | | const struct hm_header_st *msg_hdr) |
710 | 0 | { |
711 | 0 | hm_fragment *frag = NULL; |
712 | 0 | pitem *item = NULL; |
713 | 0 | int i = -1, is_complete; |
714 | 0 | size_t frag_len = msg_hdr->frag_len; |
715 | 0 | size_t readbytes; |
716 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
717 | |
|
718 | 0 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len |
719 | 0 | || msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) |
720 | 0 | goto err; |
721 | | |
722 | 0 | if (frag_len == 0) { |
723 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
724 | 0 | } |
725 | | |
726 | | /* Try to find item in queue */ |
727 | 0 | item = pqueue_find_u64(&s->d1->rcvd_messages, msg_hdr->seq); |
728 | |
|
729 | 0 | if (item == NULL) { |
730 | 0 | frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1); |
731 | 0 | if (frag == NULL) |
732 | 0 | goto err; |
733 | 0 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
734 | 0 | frag->msg_header.frag_len = frag->msg_header.msg_len; |
735 | 0 | frag->msg_header.frag_off = 0; |
736 | 0 | } else { |
737 | 0 | frag = (hm_fragment *)item->data; |
738 | 0 | if (frag->msg_header.msg_len != msg_hdr->msg_len) { |
739 | 0 | item = NULL; |
740 | 0 | frag = NULL; |
741 | 0 | goto err; |
742 | 0 | } |
743 | 0 | } |
744 | | |
745 | | /* |
746 | | * If message is already reassembled, this must be a retransmit and can |
747 | | * be dropped. In this case item != NULL and so frag does not need to be |
748 | | * freed. |
749 | | */ |
750 | 0 | if (frag->reassembly == NULL) { |
751 | 0 | unsigned char devnull[256]; |
752 | |
|
753 | 0 | while (frag_len) { |
754 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
755 | 0 | devnull, |
756 | 0 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes); |
757 | 0 | if (i <= 0) |
758 | 0 | goto err; |
759 | 0 | frag_len -= readbytes; |
760 | 0 | } |
761 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
762 | 0 | } |
763 | | |
764 | | /* read the body of the fragment (header has already been read */ |
765 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
766 | 0 | frag->fragment + msg_hdr->frag_off, |
767 | 0 | frag_len, 0, &readbytes); |
768 | 0 | if (i <= 0 || readbytes != frag_len) |
769 | 0 | goto err; |
770 | | |
771 | 0 | RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off, |
772 | 0 | (long)(msg_hdr->frag_off + frag_len)); |
773 | |
|
774 | 0 | if (!ossl_assert(msg_hdr->msg_len > 0)) |
775 | 0 | goto err; |
776 | 0 | RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len, |
777 | 0 | is_complete); |
778 | |
|
779 | 0 | if (is_complete) |
780 | 0 | frag->reassembly = NULL; |
781 | |
|
782 | 0 | if (item == NULL) { |
783 | 0 | item = pitem_new_u64(msg_hdr->seq, frag); |
784 | 0 | if (item == NULL) |
785 | 0 | goto err; |
786 | | |
787 | 0 | item = pqueue_insert(&s->d1->rcvd_messages, item); |
788 | | /* |
789 | | * pqueue_insert fails iff a duplicate item is inserted. However, |
790 | | * |item| cannot be a duplicate. If it were, |pqueue_find_u64|, above, |
791 | | * would have returned it and control would never have reached this |
792 | | * branch. |
793 | | */ |
794 | 0 | if (!ossl_assert(item != NULL)) |
795 | 0 | goto err; |
796 | 0 | } |
797 | | |
798 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr->type) |
799 | 0 | && !add_record_to_ack_list(s)) |
800 | 0 | goto err; |
801 | | |
802 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
803 | | |
804 | 0 | err: |
805 | 0 | if (item == NULL) |
806 | 0 | dtls1_hm_fragment_free(frag); |
807 | 0 | return -1; |
808 | 0 | } |
809 | | |
810 | | static int dtls1_process_out_of_seq_message(SSL_CONNECTION *s, |
811 | | const struct hm_header_st *msg_hdr) |
812 | 0 | { |
813 | 0 | int i = -1; |
814 | 0 | hm_fragment *frag = NULL; |
815 | 0 | pitem *item = NULL; |
816 | 0 | size_t frag_len = msg_hdr->frag_len; |
817 | 0 | size_t readbytes; |
818 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
819 | |
|
820 | 0 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) |
821 | 0 | goto err; |
822 | | |
823 | | /* Try to find item in queue, to prevent duplicate entries */ |
824 | 0 | item = pqueue_find_u64(&s->d1->rcvd_messages, msg_hdr->seq); |
825 | | |
826 | | /* |
827 | | * If we already have an entry and this one is a fragment, don't discard |
828 | | * it and rather try to reassemble it. |
829 | | */ |
830 | 0 | if (item != NULL && frag_len != msg_hdr->msg_len) |
831 | 0 | item = NULL; |
832 | | |
833 | | /* |
834 | | * Discard the message if sequence number was already there, is too far |
835 | | * in the future, already in the queue or if we received a FINISHED |
836 | | * before the SERVER_HELLO, which then must be a stale retransmit. |
837 | | */ |
838 | 0 | if (msg_hdr->seq <= s->d1->handshake_read_seq || msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL || (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) { |
839 | 0 | unsigned char devnull[256]; |
840 | |
|
841 | 0 | while (frag_len) { |
842 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
843 | 0 | devnull, |
844 | 0 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes); |
845 | 0 | if (i <= 0) |
846 | 0 | goto err; |
847 | 0 | frag_len -= readbytes; |
848 | 0 | } |
849 | 0 | } else { |
850 | 0 | if (frag_len != msg_hdr->msg_len) { |
851 | 0 | return dtls1_reassemble_fragment(s, msg_hdr); |
852 | 0 | } |
853 | | |
854 | 0 | if (frag_len > dtls1_max_handshake_message_len(s)) |
855 | 0 | goto err; |
856 | | |
857 | 0 | frag = dtls1_hm_fragment_new(frag_len, 0); |
858 | 0 | if (frag == NULL) |
859 | 0 | goto err; |
860 | | |
861 | 0 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
862 | |
|
863 | 0 | if (frag_len) { |
864 | | /* |
865 | | * read the body of the fragment (header has already been read |
866 | | */ |
867 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
868 | 0 | frag->fragment, frag_len, 0, |
869 | 0 | &readbytes); |
870 | 0 | if (i <= 0 || readbytes != frag_len) |
871 | 0 | goto err; |
872 | 0 | } |
873 | | |
874 | 0 | item = pitem_new_u64(msg_hdr->seq, frag); |
875 | 0 | if (item == NULL) |
876 | 0 | goto err; |
877 | | |
878 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr->type) |
879 | 0 | && !add_record_to_ack_list(s)) |
880 | 0 | goto err; |
881 | | |
882 | 0 | item = pqueue_insert(&s->d1->rcvd_messages, item); |
883 | | /* |
884 | | * pqueue_insert fails iff a duplicate item is inserted. However, |
885 | | * |item| cannot be a duplicate. If it were, |pqueue_find|, above, |
886 | | * would have returned it. Then, either |frag_len| != |
887 | | * |msg_hdr->msg_len| in which case |item| is set to NULL and it will |
888 | | * have been processed with |dtls1_reassemble_fragment|, above, or |
889 | | * the record will have been discarded. |
890 | | */ |
891 | 0 | if (!ossl_assert(item != NULL)) |
892 | 0 | goto err; |
893 | 0 | } |
894 | | |
895 | 0 | return DTLS1_HM_FRAGMENT_RETRY; |
896 | | |
897 | 0 | err: |
898 | 0 | if (item == NULL) |
899 | 0 | dtls1_hm_fragment_free(frag); |
900 | 0 | return 0; |
901 | 0 | } |
902 | | |
903 | | static int dtls1_read_hm_header(unsigned char *msgheaderstart, |
904 | | struct hm_header_st *msg_hdr) |
905 | 0 | { |
906 | 0 | unsigned long msg_len, frag_off, frag_len; |
907 | 0 | unsigned int msg_seq, msg_type; |
908 | 0 | PACKET msgheader; |
909 | |
|
910 | 0 | if (!PACKET_buf_init(&msgheader, msgheaderstart, DTLS1_HM_HEADER_LENGTH) |
911 | 0 | || !PACKET_get_1(&msgheader, &msg_type) |
912 | 0 | || !PACKET_get_net_3(&msgheader, &msg_len) |
913 | 0 | || !PACKET_get_net_2(&msgheader, &msg_seq) |
914 | 0 | || !PACKET_get_net_3(&msgheader, &frag_off) |
915 | 0 | || !PACKET_get_net_3(&msgheader, &frag_len) |
916 | 0 | || PACKET_remaining(&msgheader) != 0) { |
917 | 0 | return 0; |
918 | 0 | } |
919 | | |
920 | | /* We just checked that values did not exceed max size so cast must be alright */ |
921 | 0 | msg_hdr->type = (unsigned char)msg_type; |
922 | 0 | msg_hdr->msg_len = (size_t)msg_len; |
923 | 0 | msg_hdr->seq = (unsigned short)msg_seq; |
924 | 0 | msg_hdr->frag_off = (size_t)frag_off; |
925 | 0 | msg_hdr->frag_len = (size_t)frag_len; |
926 | |
|
927 | 0 | return 1; |
928 | 0 | } |
929 | | |
930 | | /* |
931 | | * DTLS 1.3 reserves handshake message type 0, so a HelloRequest must reach the |
932 | | * state machine and be rejected there whenever DTLS 1.3 is still possible. |
933 | | * |
934 | | * s->version is the negotiated, or maximum version: before ServerHello this is |
935 | | * the effective maximum, and after ServerHello or during renegotiation it is the |
936 | | * selected version. |
937 | | */ |
938 | | static int dtls_should_skip_hello_request(const SSL_CONNECTION *s) |
939 | 0 | { |
940 | 0 | if (SSL_CONNECTION_IS_DTLS13(s)) |
941 | 0 | return 0; |
942 | | |
943 | 0 | return s->version > 0 |
944 | 0 | && ssl_version_cmp(s, s->version, DTLS1_3_VERSION) < 0; |
945 | 0 | } |
946 | | |
947 | | static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype, |
948 | | size_t *len) |
949 | 0 | { |
950 | 0 | int i, ret; |
951 | 0 | uint8_t recvd_type; |
952 | 0 | struct hm_header_st msg_hdr; |
953 | 0 | size_t readbytes; |
954 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
955 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
956 | 0 | int chretran = 0; |
957 | 0 | unsigned char *p; |
958 | |
|
959 | 0 | *errtype = 0; |
960 | |
|
961 | 0 | p = (unsigned char *)s->init_buf->data; |
962 | |
|
963 | 0 | redo: |
964 | | /* Check for buffered CCS */ |
965 | 0 | if ((s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION |
966 | 0 | || s->version == DTLS1_BAD_VER) |
967 | 0 | && s->d1->has_change_cipher_spec && dtls_ccs_expected(s)) { |
968 | 0 | size_t extra = (s->version == DTLS1_BAD_VER) ? 2 : 0; |
969 | |
|
970 | 0 | s->d1->has_change_cipher_spec = 0; |
971 | 0 | p[0] = SSL3_MT_CCS; |
972 | | /* |
973 | | * The extra 2 bytes are never consumed, only checked for |
974 | | * length -- zero-fill to avoid old init_buf content. |
975 | | */ |
976 | 0 | if (extra > 0) |
977 | 0 | memset(p + 1, 0, extra); |
978 | 0 | s->init_num = extra; |
979 | 0 | s->init_msg = p + 1; |
980 | 0 | s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC; |
981 | 0 | s->s3.tmp.message_size = extra; |
982 | 0 | *len = extra; |
983 | 0 | return 1; |
984 | 0 | } |
985 | | |
986 | | /* see if we have the required fragment already */ |
987 | 0 | ret = dtls1_retrieve_buffered_fragment(s, &msg_hdr.frag_len); |
988 | 0 | if (ret < 0) { |
989 | | /* SSLfatal() already called */ |
990 | 0 | return 0; |
991 | 0 | } |
992 | 0 | if (ret > 0) { |
993 | 0 | s->init_num = msg_hdr.frag_len; |
994 | 0 | *len = msg_hdr.frag_len; |
995 | 0 | return 1; |
996 | 0 | } |
997 | | |
998 | | /* read handshake message header */ |
999 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, p, |
1000 | 0 | DTLS1_HM_HEADER_LENGTH, 0, &readbytes); |
1001 | 0 | if (i <= 0) { /* nbio, or an error */ |
1002 | 0 | s->rwstate = SSL_READING; |
1003 | 0 | *len = 0; |
1004 | 0 | return 0; |
1005 | 0 | } |
1006 | 0 | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1007 | 0 | if (p[0] != SSL3_MT_CCS) { |
1008 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1009 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1010 | 0 | goto f_err; |
1011 | 0 | } |
1012 | | |
1013 | | /* Buffer CCS for reorder tolerance */ |
1014 | 0 | if (s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION |
1015 | 0 | || s->version == DTLS1_BAD_VER) { |
1016 | 0 | size_t expected = (s->version == DTLS1_BAD_VER) ? 3 : 1; |
1017 | |
|
1018 | 0 | if (readbytes != expected) { |
1019 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
1020 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1021 | 0 | goto f_err; |
1022 | 0 | } |
1023 | 0 | s->d1->has_change_cipher_spec = 1; |
1024 | 0 | goto redo; |
1025 | 0 | } |
1026 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1027 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1028 | 0 | goto f_err; |
1029 | 0 | } |
1030 | 0 | if (recvd_type == SSL3_RT_ACK) { |
1031 | 0 | if (readbytes == DTLS1_HM_HEADER_LENGTH) { |
1032 | 0 | const size_t first_readbytes = readbytes; |
1033 | |
|
1034 | 0 | p += DTLS1_HM_HEADER_LENGTH; |
1035 | |
|
1036 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, p, |
1037 | 0 | s->init_num - DTLS1_HM_HEADER_LENGTH, |
1038 | 0 | 0, &readbytes); |
1039 | 0 | readbytes += first_readbytes; |
1040 | | /* |
1041 | | * This shouldn't ever fail due to NBIO because we already checked |
1042 | | * that we have enough data in the record |
1043 | | */ |
1044 | 0 | if (i <= 0) { |
1045 | 0 | s->rwstate = SSL_READING; |
1046 | 0 | *len = 0; |
1047 | 0 | return 0; |
1048 | 0 | } |
1049 | 0 | } |
1050 | 0 | s->init_num = readbytes; |
1051 | 0 | s->init_msg = s->init_buf->data; |
1052 | 0 | s->s3.tmp.message_type = DTLS13_MT_ACK; |
1053 | 0 | s->s3.tmp.message_size = readbytes; |
1054 | 0 | *len = readbytes; |
1055 | 0 | return 1; |
1056 | 0 | } |
1057 | | |
1058 | | /* Handshake fails if message header is incomplete */ |
1059 | 0 | if (readbytes != DTLS1_HM_HEADER_LENGTH) { |
1060 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1061 | 0 | goto f_err; |
1062 | 0 | } |
1063 | | |
1064 | | /* parse the message fragment header */ |
1065 | 0 | if (!dtls1_read_hm_header(p, &msg_hdr)) { |
1066 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1067 | 0 | goto f_err; |
1068 | 0 | } |
1069 | | |
1070 | | /* |
1071 | | * We must have at least frag_len bytes left in the record to be read. |
1072 | | * Fragments must not span records. |
1073 | | */ |
1074 | 0 | if (msg_hdr.frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) { |
1075 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1076 | 0 | goto f_err; |
1077 | 0 | } |
1078 | | |
1079 | | /* |
1080 | | * if this is a future (or stale) message it gets buffered |
1081 | | * (or dropped)--no further processing at this time |
1082 | | * While listening, we accept seq 1 (ClientHello with cookie) |
1083 | | * although we're still expecting seq 0 (ClientHello) |
1084 | | */ |
1085 | 0 | if (msg_hdr.seq != s->d1->handshake_read_seq) { |
1086 | 0 | if (!s->server |
1087 | 0 | || msg_hdr.seq != 0 |
1088 | 0 | || s->d1->handshake_read_seq != 1 |
1089 | 0 | || msg_hdr.type != SSL3_MT_CLIENT_HELLO |
1090 | 0 | || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { |
1091 | 0 | *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr); |
1092 | 0 | return 0; |
1093 | 0 | } |
1094 | | /* |
1095 | | * We received a ClientHello and sent back a HelloVerifyRequest. We |
1096 | | * now seem to have received a retransmitted initial ClientHello. That |
1097 | | * is allowed (possibly our HelloVerifyRequest got lost). |
1098 | | */ |
1099 | 0 | chretran = 1; |
1100 | 0 | } |
1101 | | |
1102 | 0 | if (msg_hdr.frag_len > 0 && msg_hdr.frag_len < msg_hdr.msg_len) { |
1103 | 0 | *errtype = dtls1_reassemble_fragment(s, &msg_hdr); |
1104 | 0 | return 0; |
1105 | 0 | } |
1106 | | |
1107 | 0 | if (!s->server |
1108 | 0 | && s->statem.hand_state != TLS_ST_OK |
1109 | 0 | && msg_hdr.type == SSL3_MT_HELLO_REQUEST |
1110 | 0 | && dtls_should_skip_hello_request(s)) { |
1111 | | /* |
1112 | | * The server may always send 'Hello Request' messages -- we are |
1113 | | * doing a handshake anyway now, so ignore them if their format is |
1114 | | * correct. Does not count for 'Finished' MAC. |
1115 | | */ |
1116 | 0 | if (msg_hdr.msg_len == 0) { |
1117 | 0 | if (s->msg_callback) |
1118 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
1119 | 0 | p, DTLS1_HM_HEADER_LENGTH, ussl, |
1120 | 0 | s->msg_callback_arg); |
1121 | |
|
1122 | 0 | s->init_num = 0; |
1123 | 0 | goto redo; |
1124 | 0 | } else { |
1125 | | /* Incorrectly formatted Hello request */ |
1126 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1127 | 0 | goto f_err; |
1128 | 0 | } |
1129 | 0 | } |
1130 | | |
1131 | 0 | if (!dtls1_preprocess_fragment(s, &msg_hdr)) { |
1132 | | /* SSLfatal() already called */ |
1133 | 0 | goto f_err; |
1134 | 0 | } |
1135 | | |
1136 | 0 | if (msg_hdr.frag_len > 0) { |
1137 | | /* dtls1_preprocess_fragment() above could reallocate init_buf */ |
1138 | 0 | p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH + msg_hdr.frag_off; |
1139 | |
|
1140 | 0 | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
1141 | 0 | p, msg_hdr.frag_len, 0, &readbytes); |
1142 | | |
1143 | | /* |
1144 | | * This shouldn't ever fail due to NBIO because we already checked |
1145 | | * that we have enough data in the record |
1146 | | */ |
1147 | 0 | if (i <= 0) { |
1148 | 0 | s->rwstate = SSL_READING; |
1149 | 0 | *len = 0; |
1150 | 0 | return 0; |
1151 | 0 | } |
1152 | 0 | } else { |
1153 | 0 | readbytes = 0; |
1154 | 0 | } |
1155 | | |
1156 | | /* |
1157 | | * XDTLS: an incorrectly formatted fragment should cause the handshake |
1158 | | * to fail |
1159 | | */ |
1160 | 0 | if (readbytes != msg_hdr.frag_len) { |
1161 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH); |
1162 | 0 | goto f_err; |
1163 | 0 | } |
1164 | | |
1165 | 0 | if (chretran) { |
1166 | | /* |
1167 | | * We got a new ClientHello with a message sequence of 0. |
1168 | | * Reset the read/write sequences back to the beginning. |
1169 | | * We process it like this is the first time we've seen a ClientHello |
1170 | | * from the client. |
1171 | | */ |
1172 | 0 | s->d1->handshake_read_seq = 0; |
1173 | 0 | s->d1->next_handshake_write_seq = 0; |
1174 | 0 | } |
1175 | |
|
1176 | 0 | if (dtls_msg_needs_ack(!s->server, msg_hdr.type) |
1177 | 0 | && !add_record_to_ack_list(s)) { |
1178 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1179 | 0 | goto f_err; |
1180 | 0 | } |
1181 | | |
1182 | | /* |
1183 | | * Note that s->init_num is *not* used as current offset in |
1184 | | * s->init_buf->data, but as a counter summing up fragments' lengths: as |
1185 | | * soon as they sum up to handshake packet length, we assume we have got |
1186 | | * all the fragments. |
1187 | | */ |
1188 | 0 | *len = s->init_num = msg_hdr.frag_len; |
1189 | 0 | return 1; |
1190 | | |
1191 | 0 | f_err: |
1192 | 0 | s->init_num = 0; |
1193 | 0 | *len = 0; |
1194 | 0 | return 0; |
1195 | 0 | } |
1196 | | |
1197 | | /*- |
1198 | | * for these 2 messages, we need to |
1199 | | * ssl->session->read_sym_enc assign |
1200 | | * ssl->session->read_compression assign |
1201 | | * ssl->session->read_hash assign |
1202 | | */ |
1203 | | CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s, |
1204 | | WPACKET *pkt) |
1205 | 0 | { |
1206 | 0 | if (s->version == DTLS1_BAD_VER) { |
1207 | 0 | s->d1->next_handshake_write_seq++; |
1208 | |
|
1209 | 0 | if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) { |
1210 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1211 | 0 | return CON_FUNC_ERROR; |
1212 | 0 | } |
1213 | 0 | } |
1214 | | |
1215 | 0 | return CON_FUNC_SUCCESS; |
1216 | 0 | } |
1217 | | |
1218 | | CON_FUNC_RETURN dtls_construct_ack(SSL_CONNECTION *s, WPACKET *pkt) |
1219 | 0 | { |
1220 | 0 | DTLS1_RECORD_NUMBER *recnum; |
1221 | 0 | DTLS1_RECORD_NUMBER *recnumnext = ossl_list_record_number_head(&s->d1->ack_rec_num); |
1222 | |
|
1223 | 0 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
1224 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1225 | 0 | return CON_FUNC_ERROR; |
1226 | 0 | } |
1227 | | |
1228 | 0 | while ((recnum = recnumnext) != NULL) { |
1229 | | /* |
1230 | | * rfc9147: section 4. |
1231 | | * |
1232 | | * Record numbers are encoded as |
1233 | | * struct { |
1234 | | * uint64 epoch; |
1235 | | * uint64 sequence_number; |
1236 | | * } RecordNumber; |
1237 | | */ |
1238 | |
|
1239 | 0 | recnumnext = ossl_list_record_number_next(recnum); |
1240 | |
|
1241 | 0 | if (recnum->epoch <= dtls1_get_epoch(s, SSL3_CC_WRITE)) { |
1242 | | /* |
1243 | | * rfc9147: |
1244 | | * During the handshake, ACK records MUST be sent with an epoch which |
1245 | | * is equal to or higher than the record which is being acknowledged |
1246 | | */ |
1247 | 0 | if (!WPACKET_put_bytes_u64(pkt, recnum->epoch) |
1248 | 0 | || !WPACKET_put_bytes_u64(pkt, recnum->seqnum)) { |
1249 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1250 | 0 | return CON_FUNC_ERROR; |
1251 | 0 | } |
1252 | | |
1253 | 0 | ossl_list_record_number_remove(&s->d1->ack_rec_num, recnum); |
1254 | 0 | OPENSSL_free(recnum); |
1255 | 0 | } |
1256 | 0 | } |
1257 | | |
1258 | 0 | if (!WPACKET_close(pkt)) { |
1259 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1260 | 0 | return CON_FUNC_ERROR; |
1261 | 0 | } |
1262 | | |
1263 | 0 | return CON_FUNC_SUCCESS; |
1264 | 0 | } |
1265 | | |
1266 | | MSG_PROCESS_RETURN dtls_process_ack(SSL_CONNECTION *s, PACKET *pkt) |
1267 | 0 | { |
1268 | 0 | PACKET record_numbers; |
1269 | |
|
1270 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &record_numbers)) { |
1271 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG); |
1272 | 0 | return MSG_PROCESS_ERROR; |
1273 | 0 | } |
1274 | | |
1275 | 0 | while (PACKET_remaining(&record_numbers) > 0) { |
1276 | | /* |
1277 | | * rfc9147: section 4. |
1278 | | * |
1279 | | * Record numbers are encoded as |
1280 | | * struct { |
1281 | | * uint64 epoch; |
1282 | | * uint64 sequence_number; |
1283 | | * } RecordNumber; |
1284 | | */ |
1285 | 0 | pitem *item; |
1286 | 0 | piterator iter; |
1287 | 0 | uint64_t epoch; |
1288 | 0 | uint64_t sequence_number; |
1289 | |
|
1290 | 0 | if (!PACKET_get_net_8(&record_numbers, &epoch) |
1291 | 0 | || !PACKET_get_net_8(&record_numbers, &sequence_number)) { |
1292 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
1293 | 0 | return MSG_PROCESS_ERROR; |
1294 | 0 | } |
1295 | | |
1296 | 0 | iter = pqueue_iterator(&s->d1->sent_messages); |
1297 | |
|
1298 | 0 | while ((item = pqueue_next(&iter)) != NULL) { |
1299 | 0 | dtls_sent_msg *msg = (dtls_sent_msg *)item->data; |
1300 | 0 | DTLS1_RECORD_NUMBER *recnum; |
1301 | 0 | DTLS1_RECORD_NUMBER *recnum_next = ossl_list_record_number_head(&msg->rec_nums); |
1302 | |
|
1303 | 0 | while ((recnum = recnum_next) != NULL) { |
1304 | 0 | recnum_next = ossl_list_record_number_next(recnum_next); |
1305 | |
|
1306 | 0 | if (recnum->epoch == epoch && recnum->seqnum == sequence_number) { |
1307 | 0 | ossl_list_record_number_remove(&msg->rec_nums, recnum); |
1308 | 0 | OPENSSL_free(recnum); |
1309 | 0 | } |
1310 | 0 | } |
1311 | 0 | } |
1312 | 0 | } |
1313 | | |
1314 | 0 | return MSG_PROCESS_FINISHED_READING; |
1315 | 0 | } |
1316 | | |
1317 | | #ifndef OPENSSL_NO_SCTP |
1318 | | /* |
1319 | | * Wait for a dry event. Should only be called at a point in the handshake |
1320 | | * where we are not expecting any data from the peer except an alert. |
1321 | | */ |
1322 | | WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s) |
1323 | | { |
1324 | | int ret, errtype; |
1325 | | size_t len; |
1326 | | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1327 | | |
1328 | | /* read app data until dry event */ |
1329 | | ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl)); |
1330 | | if (ret < 0) { |
1331 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1332 | | return WORK_ERROR; |
1333 | | } |
1334 | | |
1335 | | if (ret == 0) { |
1336 | | /* |
1337 | | * We're not expecting any more messages from the peer at this point - |
1338 | | * but we could get an alert. If an alert is waiting then we will never |
1339 | | * return successfully. Therefore we attempt to read a message. This |
1340 | | * should never succeed but will process any waiting alerts. |
1341 | | */ |
1342 | | if (dtls_get_reassembled_message(s, &errtype, &len)) { |
1343 | | /* The call succeeded! This should never happen */ |
1344 | | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1345 | | return WORK_ERROR; |
1346 | | } |
1347 | | |
1348 | | s->s3.in_read_app_data = 2; |
1349 | | s->rwstate = SSL_READING; |
1350 | | BIO_clear_retry_flags(SSL_get_rbio(ssl)); |
1351 | | BIO_set_retry_read(SSL_get_rbio(ssl)); |
1352 | | return WORK_MORE_A; |
1353 | | } |
1354 | | return WORK_FINISHED_CONTINUE; |
1355 | | } |
1356 | | #endif |
1357 | | |
1358 | | int dtls1_read_failed(SSL_CONNECTION *s, int code) |
1359 | 0 | { |
1360 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1361 | |
|
1362 | 0 | if (code > 0) { |
1363 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1364 | 0 | return 0; |
1365 | 0 | } |
1366 | | |
1367 | 0 | if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) { |
1368 | | /* |
1369 | | * not a timeout, none of our business, let higher layers handle |
1370 | | * this. in fact it's probably an error |
1371 | | */ |
1372 | 0 | return code; |
1373 | 0 | } |
1374 | | /* done, no need to send a retransmit */ |
1375 | 0 | if (!SSL_in_init(ssl)) { |
1376 | 0 | BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ); |
1377 | 0 | return code; |
1378 | 0 | } |
1379 | | |
1380 | 0 | return dtls1_handle_timeout(s); |
1381 | 0 | } |
1382 | | |
1383 | | void dtls1_get_queue_priority(unsigned char *prio64be, unsigned short seq, |
1384 | | int record_type) |
1385 | 0 | { |
1386 | | /* |
1387 | | * The index of the retransmission queue actually is the message sequence |
1388 | | * number, since the queue only contains messages of a single handshake. |
1389 | | * However, the ChangeCipherSpec has no message sequence number and so |
1390 | | * using only the sequence will result in the CCS and Finished having the |
1391 | | * same index. To prevent this, the sequence number is multiplied by 2. |
1392 | | * In case of a CCS 1 is subtracted. This does not only differ CSS and |
1393 | | * Finished, it also maintains the order of the index (important for |
1394 | | * priority queues) and fits in the unsigned short variable. |
1395 | | */ |
1396 | 0 | int lsb = (record_type == SSL3_RT_CHANGE_CIPHER_SPEC); |
1397 | 0 | const uint16_t prio = seq * 2 - lsb; |
1398 | |
|
1399 | 0 | memset(prio64be, 0, 8); |
1400 | 0 | prio64be[6] = (unsigned char)(prio >> 8); |
1401 | 0 | prio64be[7] = (unsigned char)(prio); |
1402 | 0 | } |
1403 | | |
1404 | | int dtls1_retransmit_sent_messages(SSL_CONNECTION *s) |
1405 | 0 | { |
1406 | 0 | piterator iter = pqueue_iterator(&s->d1->sent_messages); |
1407 | 0 | pitem *item; |
1408 | |
|
1409 | 0 | for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) { |
1410 | 0 | dtls_sent_msg *sent_msg = (dtls_sent_msg *)item->data; |
1411 | |
|
1412 | 0 | if (SSL_CONNECTION_IS_DTLS13(s) |
1413 | 0 | && ossl_list_record_number_is_empty(&sent_msg->rec_nums)) |
1414 | | /* rfc9147: Implementations must not retransmit acknowledged msgs */ |
1415 | 0 | continue; |
1416 | | |
1417 | 0 | if (dtls1_retransmit_message(s, sent_msg) <= 0) |
1418 | 0 | return -1; |
1419 | 0 | } |
1420 | | |
1421 | 0 | return 1; |
1422 | 0 | } |
1423 | | |
1424 | | int dtls1_buffer_sent_message(SSL_CONNECTION *s, int record_type) |
1425 | 0 | { |
1426 | 0 | pitem *item; |
1427 | 0 | dtls_sent_msg *sent_msg; |
1428 | 0 | unsigned char seq64be[8]; |
1429 | 0 | size_t headerlen; |
1430 | | |
1431 | | /* |
1432 | | * this function is called immediately after a message has been |
1433 | | * serialized |
1434 | | */ |
1435 | 0 | if (!ossl_assert(s->init_off == 0)) |
1436 | 0 | return 0; |
1437 | | |
1438 | 0 | sent_msg = dtls1_sent_msg_new(s->init_num); |
1439 | 0 | if (sent_msg == NULL) |
1440 | 0 | return 0; |
1441 | | |
1442 | 0 | memcpy(sent_msg->msg_buf, s->init_buf->data, s->init_num); |
1443 | |
|
1444 | 0 | if (record_type == SSL3_RT_CHANGE_CIPHER_SPEC) |
1445 | | /* For DTLS1_BAD_VER the header length is non-standard */ |
1446 | 0 | headerlen = (s->version == DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH; |
1447 | 0 | else |
1448 | 0 | headerlen = DTLS1_HM_HEADER_LENGTH; |
1449 | |
|
1450 | 0 | if (!ossl_assert(s->d1->w_msg.msg_body_len + headerlen == s->init_num)) { |
1451 | 0 | dtls1_sent_msg_free(sent_msg); |
1452 | 0 | return 0; |
1453 | 0 | } |
1454 | | |
1455 | 0 | memcpy(&sent_msg->msg_info, &s->d1->w_msg, sizeof(s->d1->w_msg)); |
1456 | | |
1457 | | /* save current state */ |
1458 | 0 | sent_msg->saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod; |
1459 | 0 | sent_msg->saved_retransmit_state.wrl = s->rlayer.wrl; |
1460 | |
|
1461 | 0 | dtls1_get_queue_priority(seq64be, sent_msg->msg_info.msg_seq, sent_msg->msg_info.record_type); |
1462 | |
|
1463 | 0 | item = pitem_new(seq64be, sent_msg); |
1464 | 0 | if (item == NULL) { |
1465 | 0 | dtls1_sent_msg_free(sent_msg); |
1466 | 0 | return 0; |
1467 | 0 | } |
1468 | | |
1469 | 0 | if (pqueue_insert(&s->d1->sent_messages, item) == NULL) { |
1470 | 0 | dtls1_sent_msg_free(sent_msg); |
1471 | 0 | pitem_free(item); |
1472 | 0 | return 0; |
1473 | 0 | } |
1474 | 0 | return 1; |
1475 | 0 | } |
1476 | | |
1477 | | int dtls1_retransmit_message(SSL_CONNECTION *s, dtls_sent_msg *sent_msg) |
1478 | 0 | { |
1479 | 0 | int ret; |
1480 | 0 | unsigned long header_length; |
1481 | 0 | struct dtls1_retransmit_state saved_state; |
1482 | |
|
1483 | 0 | if (sent_msg->msg_info.record_type == SSL3_RT_CHANGE_CIPHER_SPEC) |
1484 | 0 | header_length = DTLS1_CCS_HEADER_LENGTH; |
1485 | 0 | else |
1486 | 0 | header_length = DTLS1_HM_HEADER_LENGTH; |
1487 | | |
1488 | | /* Clear the record number list to be acked for retransmitted messages */ |
1489 | 0 | ossl_list_record_number_elem_free(&sent_msg->rec_nums); |
1490 | |
|
1491 | 0 | memcpy(s->init_buf->data, sent_msg->msg_buf, |
1492 | 0 | sent_msg->msg_info.msg_body_len + header_length); |
1493 | 0 | s->init_num = sent_msg->msg_info.msg_body_len + header_length; |
1494 | |
|
1495 | 0 | memcpy(&s->d1->w_msg, &sent_msg->msg_info, sizeof(sent_msg->msg_info)); |
1496 | | |
1497 | | /* save current state */ |
1498 | 0 | saved_state.wrlmethod = s->rlayer.wrlmethod; |
1499 | 0 | saved_state.wrl = s->rlayer.wrl; |
1500 | |
|
1501 | 0 | s->d1->retransmitting = 1; |
1502 | | |
1503 | | /* restore state in which the message was originally sent */ |
1504 | 0 | s->rlayer.wrlmethod = sent_msg->saved_retransmit_state.wrlmethod; |
1505 | 0 | s->rlayer.wrl = sent_msg->saved_retransmit_state.wrl; |
1506 | | |
1507 | | /* |
1508 | | * The old wrl may be still pointing at an old BIO. Update it to what we're |
1509 | | * using now. |
1510 | | */ |
1511 | 0 | s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio); |
1512 | |
|
1513 | 0 | ret = dtls1_do_write(s, sent_msg->msg_info.record_type); |
1514 | | |
1515 | | /* restore current state */ |
1516 | 0 | s->rlayer.wrlmethod = saved_state.wrlmethod; |
1517 | 0 | s->rlayer.wrl = saved_state.wrl; |
1518 | |
|
1519 | 0 | s->d1->retransmitting = 0; |
1520 | |
|
1521 | 0 | (void)BIO_flush(s->wbio); |
1522 | 0 | return ret; |
1523 | 0 | } |
1524 | | |
1525 | | int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype) |
1526 | 0 | { |
1527 | 0 | s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
1528 | 0 | s->d1->w_msg.msg_seq = s->d1->handshake_write_seq; |
1529 | 0 | s->d1->w_msg.msg_body_len = 0; |
1530 | |
|
1531 | 0 | if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) { |
1532 | 0 | s->d1->w_msg.record_type = SSL3_RT_CHANGE_CIPHER_SPEC; |
1533 | 0 | s->d1->w_msg.msg_type = SSL3_MT_CCS; |
1534 | |
|
1535 | 0 | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) |
1536 | 0 | return 0; |
1537 | 0 | } else if (htype == DTLS13_MT_ACK) { |
1538 | 0 | s->d1->w_msg.record_type = SSL3_RT_ACK; |
1539 | 0 | s->d1->w_msg.msg_type = 0; |
1540 | 0 | } else { |
1541 | 0 | size_t subpacket_offset = DTLS1_HM_HEADER_LENGTH - SSL3_HM_HEADER_LENGTH; |
1542 | |
|
1543 | 0 | s->d1->next_handshake_write_seq++; |
1544 | 0 | s->d1->w_msg.record_type = SSL3_RT_HANDSHAKE; |
1545 | 0 | s->d1->w_msg.msg_type = htype; |
1546 | | |
1547 | | /* Set the content type and 3 bytes for the message len */ |
1548 | 0 | if (!WPACKET_put_bytes_u8(pkt, htype) |
1549 | | /* |
1550 | | * We allocate space for DTLS specific fields. |
1551 | | * These gets filled later. |
1552 | | */ |
1553 | 0 | || !WPACKET_start_sub_packet_u24_at_offset(pkt, subpacket_offset)) |
1554 | 0 | return 0; |
1555 | 0 | } |
1556 | | |
1557 | 0 | return 1; |
1558 | 0 | } |
1559 | | |
1560 | | int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype) |
1561 | 0 | { |
1562 | 0 | size_t msglen; |
1563 | |
|
1564 | 0 | if ((s->d1->w_msg.record_type == SSL3_RT_HANDSHAKE && !WPACKET_close(pkt)) |
1565 | 0 | || !WPACKET_get_length(pkt, &msglen) |
1566 | 0 | || msglen > INT_MAX) |
1567 | 0 | return 0; |
1568 | | |
1569 | 0 | if (s->d1->w_msg.record_type == SSL3_RT_HANDSHAKE) |
1570 | 0 | s->d1->w_msg.msg_body_len = msglen - DTLS1_HM_HEADER_LENGTH; |
1571 | |
|
1572 | 0 | s->init_num = msglen; |
1573 | 0 | s->init_off = 0; |
1574 | |
|
1575 | 0 | if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST |
1576 | 0 | && s->d1->w_msg.record_type != SSL3_RT_ACK) { |
1577 | | /* Buffer the message to handle re-xmits */ |
1578 | 0 | if (!dtls1_buffer_sent_message(s, s->d1->w_msg.record_type)) |
1579 | 0 | return 0; |
1580 | 0 | } |
1581 | | |
1582 | 0 | return 1; |
1583 | 0 | } |