]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_dtls.c
Store the list of activated providers in the libctx
[thirdparty/openssl.git] / ssl / statem / statem_dtls.c
CommitLineData
0f113f3e 1/*
f5afac4b 2 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
36d16f8e
BL
8 */
9
10#include <limits.h>
11#include <string.h>
12#include <stdio.h>
706457b7
DMSP
13#include "../ssl_local.h"
14#include "statem_local.h"
67dc995e 15#include "internal/cryptlib.h"
36d16f8e 16#include <openssl/buffer.h>
36d16f8e
BL
17#include <openssl/objects.h>
18#include <openssl/evp.h>
19#include <openssl/x509.h>
20
934e22e8
DSH
21#define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
22
23#define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
0f113f3e
MC
24 if ((end) - (start) <= 8) { \
25 long ii; \
26 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
27 } else { \
28 long ii; \
29 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
30 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
31 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
32 } }
934e22e8
DSH
33
34#define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
0f113f3e 35 long ii; \
0f113f3e
MC
36 is_complete = 1; \
37 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
38 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
39 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
934e22e8 40
0f113f3e
MC
41static unsigned char bitmask_start_values[] =
42 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
43static unsigned char bitmask_end_values[] =
44 { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
36d16f8e 45
7ee8627f
MC
46static void dtls1_fix_message_header(SSL *s, size_t frag_off,
47 size_t frag_len);
0f113f3e 48static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
36d16f8e 49static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
7ee8627f 50 size_t len,
0f113f3e 51 unsigned short seq_num,
7ee8627f
MC
52 size_t frag_off,
53 size_t frag_len);
54static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len);
0f113f3e 55
7ee8627f 56static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly)
0f113f3e
MC
57{
58 hm_fragment *frag = NULL;
59 unsigned char *buf = NULL;
60 unsigned char *bitmask = NULL;
61
cdb10bae 62 if ((frag = OPENSSL_malloc(sizeof(*frag))) == NULL) {
6849b73c 63 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
0f113f3e 64 return NULL;
cdb10bae 65 }
0f113f3e
MC
66
67 if (frag_len) {
cdb10bae 68 if ((buf = OPENSSL_malloc(frag_len)) == NULL) {
6849b73c 69 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
70 OPENSSL_free(frag);
71 return NULL;
72 }
73 }
74
75 /* zero length fragment gets zero frag->fragment */
76 frag->fragment = buf;
77
78 /* Initialize reassembly bitmask if necessary */
79 if (reassembly) {
b51bce94 80 bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
0f113f3e 81 if (bitmask == NULL) {
6849b73c 82 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
b548a1f1 83 OPENSSL_free(buf);
0f113f3e
MC
84 OPENSSL_free(frag);
85 return NULL;
86 }
0f113f3e
MC
87 }
88
89 frag->reassembly = bitmask;
90
91 return frag;
92}
36d16f8e 93
8a35dbb6 94void dtls1_hm_fragment_free(hm_fragment *frag)
0f113f3e 95{
25aaa98a
RS
96 if (!frag)
97 return;
0f113f3e
MC
98 if (frag->msg_header.is_ccs) {
99 EVP_CIPHER_CTX_free(frag->msg_header.
100 saved_retransmit_state.enc_write_ctx);
bfb0641f 101 EVP_MD_CTX_free(frag->msg_header.saved_retransmit_state.write_hash);
0f113f3e 102 }
b548a1f1
RS
103 OPENSSL_free(frag->fragment);
104 OPENSSL_free(frag->reassembly);
0f113f3e
MC
105 OPENSSL_free(frag);
106}
36d16f8e 107
0f113f3e
MC
108/*
109 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
110 * SSL3_RT_CHANGE_CIPHER_SPEC)
111 */
6abb0d1f 112int dtls1_do_write(SSL *s, int type)
0f113f3e
MC
113{
114 int ret;
7ee8627f
MC
115 size_t written;
116 size_t curr_mtu;
0f113f3e 117 int retry = 1;
7ee8627f 118 size_t len, frag_off, mac_size, blocksize, used_len;
0f113f3e
MC
119
120 if (!dtls1_query_mtu(s))
121 return -1;
122
50b4a9ba
SGM
123 if (s->d1->mtu < dtls1_min_mtu(s))
124 /* should have something reasonable now */
125 return -1;
0f113f3e 126
380a522f
MC
127 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
128 if (!ossl_assert(s->init_num ==
129 s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))
130 return -1;
131 }
0f113f3e
MC
132
133 if (s->write_hash) {
134 if (s->enc_write_ctx
f6c95e46 135 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx)) &
3e166c13 136 EVP_CIPH_FLAG_AEAD_CIPHER) != 0)
0f113f3e
MC
137 mac_size = 0;
138 else
139 mac_size = EVP_MD_CTX_size(s->write_hash);
140 } else
141 mac_size = 0;
142
143 if (s->enc_write_ctx &&
144 (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
846ec07d 145 blocksize = 2 * EVP_CIPHER_CTX_block_size(s->enc_write_ctx);
0f113f3e
MC
146 else
147 blocksize = 0;
148
149 frag_off = 0;
67f60be8
MC
150 s->rwstate = SSL_NOTHING;
151
0f113f3e
MC
152 /* s->init_num shouldn't ever be < 0...but just in case */
153 while (s->init_num > 0) {
2ad226e8
MC
154 if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {
155 /* We must be writing a fragment other than the first one */
156
157 if (frag_off > 0) {
158 /* This is the first attempt at writing out this fragment */
159
160 if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {
161 /*
162 * Each fragment that was already sent must at least have
163 * contained the message header plus one other byte.
164 * Therefore |init_off| must have progressed by at least
165 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
166 * wrong.
167 */
168 return -1;
169 }
170
171 /*
172 * Adjust |init_off| and |init_num| to allow room for a new
173 * message header for this fragment.
174 */
175 s->init_off -= DTLS1_HM_HEADER_LENGTH;
176 s->init_num += DTLS1_HM_HEADER_LENGTH;
177 } else {
178 /*
179 * We must have been called again after a retry so use the
180 * fragment offset from our last attempt. We do not need
181 * to adjust |init_off| and |init_num| as above, because
182 * that should already have been done before the retry.
183 */
184 frag_off = s->d1->w_msg_hdr.frag_off;
185 }
186 }
187
2e7dc7cd 188 used_len = BIO_wpending(s->wbio) + DTLS1_RT_HEADER_LENGTH
0f113f3e
MC
189 + mac_size + blocksize;
190 if (s->d1->mtu > used_len)
191 curr_mtu = s->d1->mtu - used_len;
192 else
193 curr_mtu = 0;
194
195 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
196 /*
197 * grr.. we could get an error if MTU picked was wrong
198 */
2e7dc7cd 199 ret = BIO_flush(s->wbio);
67f60be8
MC
200 if (ret <= 0) {
201 s->rwstate = SSL_WRITING;
0f113f3e 202 return ret;
67f60be8 203 }
0f113f3e
MC
204 used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
205 if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {
206 curr_mtu = s->d1->mtu - used_len;
207 } else {
208 /* Shouldn't happen */
209 return -1;
210 }
211 }
212
213 /*
214 * We just checked that s->init_num > 0 so this cast should be safe
215 */
216 if (((unsigned int)s->init_num) > curr_mtu)
217 len = curr_mtu;
218 else
219 len = s->init_num;
220
aefb9256
MC
221 if (len > s->max_send_fragment)
222 len = s->max_send_fragment;
223
0f113f3e
MC
224 /*
225 * XDTLS: this function is too long. split out the CCS part
226 */
227 if (type == SSL3_RT_HANDSHAKE) {
0f113f3e
MC
228 if (len < DTLS1_HM_HEADER_LENGTH) {
229 /*
230 * len is so small that we really can't do anything sensible
231 * so fail
232 */
233 return -1;
234 }
a230b26e 235 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
0f113f3e
MC
236
237 dtls1_write_message_header(s,
238 (unsigned char *)&s->init_buf->
239 data[s->init_off]);
240 }
241
7ee8627f
MC
242 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
243 &written);
0f113f3e
MC
244 if (ret < 0) {
245 /*
246 * might need to update MTU here, but we don't know which
247 * previous packet caused the failure -- so can't really
248 * retransmit anything. continue as if everything is fine and
249 * wait for an alert to handle the retransmit
250 */
251 if (retry && BIO_ctrl(SSL_get_wbio(s),
252 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
253 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
254 if (!dtls1_query_mtu(s))
255 return -1;
256 /* Have one more go */
257 retry = 0;
258 } else
259 return -1;
260 } else {
380a522f 261 return -1;
0f113f3e
MC
262 }
263 } else {
264
265 /*
266 * bad if this assert fails, only part of the handshake message
267 * got sent. but why would this happen?
268 */
380a522f
MC
269 if (!ossl_assert(len == written))
270 return -1;
0f113f3e
MC
271
272 if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
273 /*
274 * should not be done for 'Hello Request's, but in that case
275 * we'll ignore the result anyway
276 */
277 unsigned char *p =
278 (unsigned char *)&s->init_buf->data[s->init_off];
279 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
7ee8627f 280 size_t xlen;
0f113f3e
MC
281
282 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
283 /*
284 * reconstruct message header is if it is being sent in
285 * single fragment
286 */
287 *p++ = msg_hdr->type;
288 l2n3(msg_hdr->msg_len, p);
289 s2n(msg_hdr->seq, p);
290 l2n3(0, p);
291 l2n3(msg_hdr->msg_len, p);
292 p -= DTLS1_HM_HEADER_LENGTH;
7ee8627f 293 xlen = written;
0f113f3e
MC
294 } else {
295 p += DTLS1_HM_HEADER_LENGTH;
7ee8627f 296 xlen = written - DTLS1_HM_HEADER_LENGTH;
0f113f3e
MC
297 }
298
d166ed8c
DSH
299 if (!ssl3_finish_mac(s, p, xlen))
300 return -1;
0f113f3e
MC
301 }
302
7ee8627f 303 if (written == s->init_num) {
0f113f3e
MC
304 if (s->msg_callback)
305 s->msg_callback(1, s->version, type, s->init_buf->data,
306 (size_t)(s->init_off + s->init_num), s,
307 s->msg_callback_arg);
308
309 s->init_off = 0; /* done writing this message */
310 s->init_num = 0;
311
7ee8627f 312 return 1;
0f113f3e 313 }
7ee8627f
MC
314 s->init_off += written;
315 s->init_num -= written;
316 written -= DTLS1_HM_HEADER_LENGTH;
317 frag_off += written;
2ad226e8
MC
318
319 /*
320 * We save the fragment offset for the next fragment so we have it
321 * available in case of an IO retry. We don't know the length of the
322 * next fragment yet so just set that to 0 for now. It will be
323 * updated again later.
324 */
325 dtls1_fix_message_header(s, frag_off, 0);
0f113f3e
MC
326 }
327 }
7ee8627f 328 return 0;
0f113f3e
MC
329}
330
eda75751 331int dtls_get_message(SSL *s, int *mt, size_t *len)
76af3037
MC
332{
333 struct hm_header_st *msg_hdr;
334 unsigned char *p;
7ee8627f
MC
335 size_t msg_len;
336 size_t tmplen;
337 int errtype;
76af3037
MC
338
339 msg_hdr = &s->d1->r_msg_hdr;
340 memset(msg_hdr, 0, sizeof(*msg_hdr));
341
342 again:
7ee8627f
MC
343 if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) {
344 if (errtype == DTLS1_HM_BAD_FRAGMENT
345 || errtype == DTLS1_HM_FRAGMENT_RETRY) {
346 /* bad fragment received */
347 goto again;
348 }
76af3037
MC
349 return 0;
350 }
351
555cbb32 352 *mt = s->s3.tmp.message_type;
76af3037
MC
353
354 p = (unsigned char *)s->init_buf->data;
7ee8627f 355 *len = s->init_num;
76af3037
MC
356
357 if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
358 if (s->msg_callback) {
359 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
360 p, 1, s, s->msg_callback_arg);
361 }
362 /*
363 * This isn't a real handshake message so skip the processing below.
364 */
365 return 1;
366 }
367
368 msg_len = msg_hdr->msg_len;
369
370 /* reconstruct message header */
371 *(p++) = msg_hdr->type;
372 l2n3(msg_len, p);
373 s2n(msg_hdr->seq, p);
374 l2n3(0, p);
375 l2n3(msg_len, p);
376 if (s->version != DTLS1_BAD_VER) {
377 p -= DTLS1_HM_HEADER_LENGTH;
378 msg_len += DTLS1_HM_HEADER_LENGTH;
379 }
380
5d671101
MC
381 /*
382 * If receiving Finished, record MAC of prior handshake messages for
383 * Finished verification.
384 */
385 if (*mt == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
386 /* SSLfatal() already called */
387 return 0;
388 }
389
d166ed8c
DSH
390 if (!ssl3_finish_mac(s, p, msg_len))
391 return 0;
76af3037
MC
392 if (s->msg_callback)
393 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
394 p, msg_len, s, s->msg_callback_arg);
395
396 memset(msg_hdr, 0, sizeof(*msg_hdr));
397
398 s->d1->handshake_read_seq++;
399
76af3037 400 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
76af3037
MC
401
402 return 1;
403}
404
48c054fe
MC
405/*
406 * dtls1_max_handshake_message_len returns the maximum number of bytes
407 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
408 * may be greater if the maximum certificate list size requires it.
409 */
348240c6 410static size_t dtls1_max_handshake_message_len(const SSL *s)
48c054fe 411{
348240c6
MC
412 size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
413 if (max_len < s->max_cert_list)
48c054fe
MC
414 return s->max_cert_list;
415 return max_len;
416}
417
76af3037 418static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr)
0f113f3e
MC
419{
420 size_t frag_off, frag_len, msg_len;
421
422 msg_len = msg_hdr->msg_len;
423 frag_off = msg_hdr->frag_off;
424 frag_len = msg_hdr->frag_len;
425
426 /* sanity checking */
48c054fe
MC
427 if ((frag_off + frag_len) > msg_len
428 || msg_len > dtls1_max_handshake_message_len(s)) {
c48ffbcc 429 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
d273b60b 430 return 0;
0f113f3e
MC
431 }
432
0f113f3e
MC
433 if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
434 /*
48c054fe
MC
435 * msg_len is limited to 2^24, but is effectively checked against
436 * dtls_max_handshake_message_len(s) above
0f113f3e 437 */
a230b26e 438 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
c48ffbcc 439 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
d273b60b 440 return 0;
0f113f3e
MC
441 }
442
555cbb32 443 s->s3.tmp.message_size = msg_len;
0f113f3e 444 s->d1->r_msg_hdr.msg_len = msg_len;
555cbb32 445 s->s3.tmp.message_type = msg_hdr->type;
0f113f3e
MC
446 s->d1->r_msg_hdr.type = msg_hdr->type;
447 s->d1->r_msg_hdr.seq = msg_hdr->seq;
448 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
449 /*
450 * They must be playing with us! BTW, failure to enforce upper limit
451 * would open possibility for buffer overrun.
452 */
c48ffbcc 453 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
d273b60b 454 return 0;
0f113f3e
MC
455 }
456
d273b60b 457 return 1;
0f113f3e 458}
90acf770 459
d273b60b
MC
460/*
461 * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
462 * fatal error.
463 */
7ee8627f 464static int dtls1_retrieve_buffered_fragment(SSL *s, size_t *len)
0f113f3e 465{
50e735f9
MC
466 /*-
467 * (0) check whether the desired fragment is available
468 * if so:
469 * (1) copy over the fragment to s->init_buf->data[]
470 * (2) update s->init_num
471 */
0f113f3e
MC
472 pitem *item;
473 hm_fragment *frag;
d273b60b 474 int ret;
0f113f3e 475
f5c7f5df
MC
476 do {
477 item = pqueue_peek(s->d1->buffered_messages);
478 if (item == NULL)
479 return 0;
480
481 frag = (hm_fragment *)item->data;
482
483 if (frag->msg_header.seq < s->d1->handshake_read_seq) {
484 /* This is a stale message that has been buffered so clear it */
485 pqueue_pop(s->d1->buffered_messages);
486 dtls1_hm_fragment_free(frag);
487 pitem_free(item);
488 item = NULL;
489 frag = NULL;
490 }
491 } while (item == NULL);
0f113f3e
MC
492
493 /* Don't return if reassembly still in progress */
494 if (frag->reassembly != NULL)
495 return 0;
496
497 if (s->d1->handshake_read_seq == frag->msg_header.seq) {
7ee8627f 498 size_t frag_len = frag->msg_header.frag_len;
0f113f3e
MC
499 pqueue_pop(s->d1->buffered_messages);
500
d273b60b
MC
501 /* Calls SSLfatal() as required */
502 ret = dtls1_preprocess_fragment(s, &frag->msg_header);
0f113f3e 503
a925e7db 504 if (ret && frag->msg_header.frag_len > 0) {
0f113f3e
MC
505 unsigned char *p =
506 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
507 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
508 frag->msg_header.frag_len);
509 }
510
511 dtls1_hm_fragment_free(frag);
512 pitem_free(item);
513
d273b60b 514 if (ret) {
7ee8627f
MC
515 *len = frag_len;
516 return 1;
0f113f3e
MC
517 }
518
d273b60b 519 /* Fatal error */
0f113f3e 520 s->init_num = 0;
d273b60b 521 return -1;
7ee8627f
MC
522 } else {
523 return 0;
524 }
0f113f3e
MC
525}
526
934e22e8 527static int
7ee8627f 528dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr)
0f113f3e
MC
529{
530 hm_fragment *frag = NULL;
531 pitem *item = NULL;
532 int i = -1, is_complete;
533 unsigned char seq64be[8];
7ee8627f 534 size_t frag_len = msg_hdr->frag_len;
54105ddd 535 size_t readbytes;
0f113f3e
MC
536
537 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
538 msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
539 goto err;
540
7ee8627f 541 if (frag_len == 0) {
0f113f3e 542 return DTLS1_HM_FRAGMENT_RETRY;
7ee8627f 543 }
0f113f3e
MC
544
545 /* Try to find item in queue */
546 memset(seq64be, 0, sizeof(seq64be));
547 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
548 seq64be[7] = (unsigned char)msg_hdr->seq;
549 item = pqueue_find(s->d1->buffered_messages, seq64be);
550
551 if (item == NULL) {
552 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
553 if (frag == NULL)
554 goto err;
555 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
556 frag->msg_header.frag_len = frag->msg_header.msg_len;
557 frag->msg_header.frag_off = 0;
558 } else {
559 frag = (hm_fragment *)item->data;
560 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
561 item = NULL;
562 frag = NULL;
563 goto err;
564 }
565 }
566
567 /*
568 * If message is already reassembled, this must be a retransmit and can
569 * be dropped. In this case item != NULL and so frag does not need to be
570 * freed.
571 */
572 if (frag->reassembly == NULL) {
573 unsigned char devnull[256];
574
575 while (frag_len) {
657da85e 576 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
577 devnull,
578 frag_len >
579 sizeof(devnull) ? sizeof(devnull) :
54105ddd 580 frag_len, 0, &readbytes);
0f113f3e
MC
581 if (i <= 0)
582 goto err;
54105ddd 583 frag_len -= readbytes;
0f113f3e
MC
584 }
585 return DTLS1_HM_FRAGMENT_RETRY;
586 }
587
588 /* read the body of the fragment (header has already been read */
657da85e 589 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e 590 frag->fragment + msg_hdr->frag_off,
54105ddd
MC
591 frag_len, 0, &readbytes);
592 if (i <= 0 || readbytes != frag_len)
0f113f3e
MC
593 i = -1;
594 if (i <= 0)
595 goto err;
596
597 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
598 (long)(msg_hdr->frag_off + frag_len));
599
380a522f
MC
600 if (!ossl_assert(msg_hdr->msg_len > 0))
601 goto err;
0f113f3e
MC
602 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
603 is_complete);
604
605 if (is_complete) {
606 OPENSSL_free(frag->reassembly);
607 frag->reassembly = NULL;
608 }
609
610 if (item == NULL) {
611 item = pitem_new(seq64be, frag);
612 if (item == NULL) {
613 i = -1;
614 goto err;
615 }
616
617 item = pqueue_insert(s->d1->buffered_messages, item);
618 /*
619 * pqueue_insert fails iff a duplicate item is inserted. However,
620 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
621 * would have returned it and control would never have reached this
622 * branch.
623 */
380a522f
MC
624 if (!ossl_assert(item != NULL))
625 goto err;
0f113f3e
MC
626 }
627
628 return DTLS1_HM_FRAGMENT_RETRY;
629
630 err:
25aaa98a 631 if (item == NULL)
0f113f3e 632 dtls1_hm_fragment_free(frag);
7ee8627f 633 return -1;
0f113f3e 634}
934e22e8 635
36d16f8e 636static int
7ee8627f 637dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr)
36d16f8e 638{
0f113f3e
MC
639 int i = -1;
640 hm_fragment *frag = NULL;
641 pitem *item = NULL;
642 unsigned char seq64be[8];
7ee8627f 643 size_t frag_len = msg_hdr->frag_len;
54105ddd 644 size_t readbytes;
0f113f3e
MC
645
646 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
647 goto err;
648
649 /* Try to find item in queue, to prevent duplicate entries */
650 memset(seq64be, 0, sizeof(seq64be));
651 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
652 seq64be[7] = (unsigned char)msg_hdr->seq;
653 item = pqueue_find(s->d1->buffered_messages, seq64be);
654
655 /*
656 * If we already have an entry and this one is a fragment, don't discard
657 * it and rather try to reassemble it.
658 */
659 if (item != NULL && frag_len != msg_hdr->msg_len)
660 item = NULL;
661
662 /*
663 * Discard the message if sequence number was already there, is too far
664 * in the future, already in the queue or if we received a FINISHED
665 * before the SERVER_HELLO, which then must be a stale retransmit.
666 */
667 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
668 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
a230b26e 669 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
0f113f3e
MC
670 unsigned char devnull[256];
671
672 while (frag_len) {
657da85e 673 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
674 devnull,
675 frag_len >
676 sizeof(devnull) ? sizeof(devnull) :
54105ddd 677 frag_len, 0, &readbytes);
0f113f3e
MC
678 if (i <= 0)
679 goto err;
54105ddd 680 frag_len -= readbytes;
0f113f3e
MC
681 }
682 } else {
7ee8627f 683 if (frag_len != msg_hdr->msg_len) {
0fe2a0af 684 return dtls1_reassemble_fragment(s, msg_hdr);
7ee8627f 685 }
0f113f3e
MC
686
687 if (frag_len > dtls1_max_handshake_message_len(s))
688 goto err;
689
690 frag = dtls1_hm_fragment_new(frag_len, 0);
691 if (frag == NULL)
692 goto err;
693
694 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
695
696 if (frag_len) {
697 /*
698 * read the body of the fragment (header has already been read
699 */
657da85e 700 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
54105ddd
MC
701 frag->fragment, frag_len, 0,
702 &readbytes);
703 if (i<=0 || readbytes != frag_len)
0f113f3e
MC
704 i = -1;
705 if (i <= 0)
706 goto err;
707 }
708
709 item = pitem_new(seq64be, frag);
710 if (item == NULL)
711 goto err;
712
713 item = pqueue_insert(s->d1->buffered_messages, item);
714 /*
715 * pqueue_insert fails iff a duplicate item is inserted. However,
716 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
717 * would have returned it. Then, either |frag_len| !=
718 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
719 * have been processed with |dtls1_reassemble_fragment|, above, or
720 * the record will have been discarded.
721 */
380a522f
MC
722 if (!ossl_assert(item != NULL))
723 goto err;
0f113f3e
MC
724 }
725
726 return DTLS1_HM_FRAGMENT_RETRY;
727
728 err:
25aaa98a 729 if (item == NULL)
0f113f3e 730 dtls1_hm_fragment_free(frag);
7ee8627f 731 return 0;
0f113f3e 732}
36d16f8e 733
7ee8627f 734static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len)
0f113f3e
MC
735{
736 unsigned char wire[DTLS1_HM_HEADER_LENGTH];
7ee8627f 737 size_t mlen, frag_off, frag_len;
d273b60b 738 int i, ret, recvd_type;
0f113f3e 739 struct hm_header_st msg_hdr;
54105ddd 740 size_t readbytes;
0f113f3e 741
7ee8627f
MC
742 *errtype = 0;
743
0f113f3e
MC
744 redo:
745 /* see if we have the required fragment already */
d273b60b
MC
746 ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
747 if (ret < 0) {
748 /* SSLfatal() already called */
749 return 0;
750 }
751 if (ret > 0) {
7ee8627f 752 s->init_num = frag_len;
76af3037 753 *len = frag_len;
7ee8627f 754 return 1;
0f113f3e
MC
755 }
756
757 /* read handshake message header */
c69f2adf 758 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
54105ddd 759 DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
0f113f3e
MC
760 if (i <= 0) { /* nbio, or an error */
761 s->rwstate = SSL_READING;
7ee8627f 762 *len = 0;
76af3037 763 return 0;
0f113f3e 764 }
e8aa8b6c 765 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
76af3037 766 if (wire[0] != SSL3_MT_CCS) {
d273b60b 767 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
d273b60b 768 SSL_R_BAD_CHANGE_CIPHER_SPEC);
76af3037 769 goto f_err;
c69f2adf 770 }
76af3037 771
54105ddd
MC
772 memcpy(s->init_buf->data, wire, readbytes);
773 s->init_num = readbytes - 1;
76af3037 774 s->init_msg = s->init_buf->data + 1;
555cbb32
TS
775 s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
776 s->s3.tmp.message_size = readbytes - 1;
54105ddd 777 *len = readbytes - 1;
76af3037 778 return 1;
c69f2adf
MC
779 }
780
0f113f3e 781 /* Handshake fails if message header is incomplete */
54105ddd 782 if (readbytes != DTLS1_HM_HEADER_LENGTH) {
c48ffbcc 783 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
784 goto f_err;
785 }
786
787 /* parse the message fragment header */
788 dtls1_get_message_header(wire, &msg_hdr);
789
76af3037 790 mlen = msg_hdr.msg_len;
91d13f1a
MC
791 frag_off = msg_hdr.frag_off;
792 frag_len = msg_hdr.frag_len;
793
794 /*
795 * We must have at least frag_len bytes left in the record to be read.
796 * Fragments must not span records.
797 */
798 if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
c48ffbcc 799 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
91d13f1a
MC
800 goto f_err;
801 }
802
0f113f3e
MC
803 /*
804 * if this is a future (or stale) message it gets buffered
805 * (or dropped)--no further processing at this time
806 * While listening, we accept seq 1 (ClientHello with cookie)
807 * although we're still expecting seq 0 (ClientHello)
808 */
76af3037 809 if (msg_hdr.seq != s->d1->handshake_read_seq) {
7ee8627f
MC
810 *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
811 return 0;
76af3037 812 }
0f113f3e 813
76af3037 814 if (frag_len && frag_len < mlen) {
7ee8627f
MC
815 *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
816 return 0;
76af3037 817 }
0f113f3e 818
1f5b44e9
MC
819 if (!s->server
820 && s->d1->r_msg_hdr.frag_off == 0
c7f47786
MC
821 && s->statem.hand_state != TLS_ST_OK
822 && wire[0] == SSL3_MT_HELLO_REQUEST) {
0f113f3e
MC
823 /*
824 * The server may always send 'Hello Request' messages -- we are
825 * doing a handshake anyway now, so ignore them if their format is
826 * correct. Does not count for 'Finished' MAC.
827 */
828 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
829 if (s->msg_callback)
830 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
831 wire, DTLS1_HM_HEADER_LENGTH, s,
832 s->msg_callback_arg);
833
834 s->init_num = 0;
835 goto redo;
60250017 836 } else { /* Incorrectly formatted Hello request */
0f113f3e 837
c48ffbcc 838 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
839 goto f_err;
840 }
841 }
842
d273b60b
MC
843 if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
844 /* SSLfatal() already called */
0f113f3e 845 goto f_err;
d273b60b 846 }
0f113f3e 847
0f113f3e
MC
848 if (frag_len > 0) {
849 unsigned char *p =
850 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
851
657da85e 852 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
54105ddd 853 &p[frag_off], frag_len, 0, &readbytes);
91d13f1a 854
0f113f3e 855 /*
91d13f1a
MC
856 * This shouldn't ever fail due to NBIO because we already checked
857 * that we have enough data in the record
0f113f3e
MC
858 */
859 if (i <= 0) {
860 s->rwstate = SSL_READING;
7ee8627f 861 *len = 0;
76af3037 862 return 0;
0f113f3e 863 }
7ee8627f 864 } else {
54105ddd 865 readbytes = 0;
7ee8627f 866 }
0f113f3e
MC
867
868 /*
869 * XDTLS: an incorrectly formatted fragment should cause the handshake
870 * to fail
871 */
54105ddd 872 if (readbytes != frag_len) {
c48ffbcc 873 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
0f113f3e
MC
874 goto f_err;
875 }
876
0f113f3e
MC
877 /*
878 * Note that s->init_num is *not* used as current offset in
879 * s->init_buf->data, but as a counter summing up fragments' lengths: as
880 * soon as they sum up to handshake packet length, we assume we have got
881 * all the fragments.
882 */
76af3037
MC
883 *len = s->init_num = frag_len;
884 return 1;
0f113f3e
MC
885
886 f_err:
0f113f3e 887 s->init_num = 0;
7ee8627f 888 *len = 0;
76af3037 889 return 0;
0f113f3e 890}
36d16f8e 891
1d97c843
TH
892/*-
893 * for these 2 messages, we need to
0f113f3e 894 * ssl->enc_read_ctx re-init
de07f311 895 * ssl->rlayer.read_sequence zero
555cbb32 896 * ssl->s3.read_mac_secret re-init
0f113f3e
MC
897 * ssl->session->read_sym_enc assign
898 * ssl->session->read_compression assign
899 * ssl->session->read_hash assign
36d16f8e 900 */
7cea05dc 901int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
0f113f3e 902{
473483d4
MC
903 if (s->version == DTLS1_BAD_VER) {
904 s->d1->next_handshake_write_seq++;
85a7a5e6 905
7cea05dc 906 if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
c48ffbcc 907 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
d273b60b 908 return 0;
85a7a5e6 909 }
0f113f3e 910 }
36d16f8e 911
473483d4
MC
912 return 1;
913}
914
915#ifndef OPENSSL_NO_SCTP
2e92af5e
MC
916/*
917 * Wait for a dry event. Should only be called at a point in the handshake
918 * where we are not expecting any data from the peer except an alert.
919 */
be3583fa 920WORK_STATE dtls_wait_for_dry(SSL *s)
473483d4 921{
2e92af5e
MC
922 int ret, errtype;
923 size_t len;
473483d4
MC
924
925 /* read app data until dry event */
926 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
a2c2e000 927 if (ret < 0) {
c48ffbcc 928 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
473483d4 929 return WORK_ERROR;
a2c2e000 930 }
473483d4
MC
931
932 if (ret == 0) {
2e92af5e
MC
933 /*
934 * We're not expecting any more messages from the peer at this point -
935 * but we could get an alert. If an alert is waiting then we will never
936 * return successfully. Therefore we attempt to read a message. This
937 * should never succeed but will process any waiting alerts.
938 */
939 if (dtls_get_reassembled_message(s, &errtype, &len)) {
940 /* The call succeeded! This should never happen */
c48ffbcc 941 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
2e92af5e
MC
942 return WORK_ERROR;
943 }
944
555cbb32 945 s->s3.in_read_app_data = 2;
473483d4
MC
946 s->rwstate = SSL_READING;
947 BIO_clear_retry_flags(SSL_get_rbio(s));
948 BIO_set_retry_read(SSL_get_rbio(s));
949 return WORK_MORE_A;
950 }
951 return WORK_FINISHED_CONTINUE;
0f113f3e 952}
473483d4 953#endif
36d16f8e 954
36d16f8e 955int dtls1_read_failed(SSL *s, int code)
0f113f3e
MC
956{
957 if (code > 0) {
c48ffbcc 958 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
d273b60b 959 return 0;
0f113f3e
MC
960 }
961
c2853382 962 if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) {
0f113f3e
MC
963 /*
964 * not a timeout, none of our business, let higher layers handle
965 * this. in fact it's probably an error
966 */
967 return code;
968 }
0f113f3e
MC
969 /* done, no need to send a retransmit */
970 if (!SSL_in_init(s))
0f113f3e
MC
971 {
972 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
973 return code;
974 }
36d16f8e 975
0f113f3e
MC
976 return dtls1_handle_timeout(s);
977}
36d16f8e 978
0f113f3e
MC
979int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
980{
981 /*
982 * The index of the retransmission queue actually is the message sequence
983 * number, since the queue only contains messages of a single handshake.
984 * However, the ChangeCipherSpec has no message sequence number and so
985 * using only the sequence will result in the CCS and Finished having the
986 * same index. To prevent this, the sequence number is multiplied by 2.
987 * In case of a CCS 1 is subtracted. This does not only differ CSS and
988 * Finished, it also maintains the order of the index (important for
989 * priority queues) and fits in the unsigned short variable.
990 */
991 return seq * 2 - is_ccs;
992}
36d16f8e 993
0f113f3e
MC
994int dtls1_retransmit_buffered_messages(SSL *s)
995{
cf2cede4 996 pqueue *sent = s->d1->sent_messages;
0f113f3e
MC
997 piterator iter;
998 pitem *item;
999 hm_fragment *frag;
1000 int found = 0;
1001
1002 iter = pqueue_iterator(sent);
1003
1004 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1005 frag = (hm_fragment *)item->data;
1006 if (dtls1_retransmit_message(s, (unsigned short)
1007 dtls1_get_queue_priority
1008 (frag->msg_header.seq,
a230b26e 1009 frag->msg_header.is_ccs), &found) <= 0)
0f113f3e 1010 return -1;
0f113f3e
MC
1011 }
1012
1013 return 1;
1014}
36d16f8e 1015
0f113f3e
MC
1016int dtls1_buffer_message(SSL *s, int is_ccs)
1017{
1018 pitem *item;
1019 hm_fragment *frag;
1020 unsigned char seq64be[8];
1021
1022 /*
1023 * this function is called immediately after a message has been
1024 * serialized
1025 */
380a522f
MC
1026 if (!ossl_assert(s->init_off == 0))
1027 return 0;
0f113f3e
MC
1028
1029 frag = dtls1_hm_fragment_new(s->init_num, 0);
a71edf3b 1030 if (frag == NULL)
0f113f3e
MC
1031 return 0;
1032
1033 memcpy(frag->fragment, s->init_buf->data, s->init_num);
1034
1035 if (is_ccs) {
5178a16c 1036 /* For DTLS1_BAD_VER the header length is non-standard */
380a522f
MC
1037 if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
1038 ((s->version ==
1039 DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
48ff651e
P
1040 == (unsigned int)s->init_num)) {
1041 dtls1_hm_fragment_free(frag);
380a522f 1042 return 0;
48ff651e 1043 }
0f113f3e 1044 } else {
380a522f 1045 if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
48ff651e
P
1046 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num)) {
1047 dtls1_hm_fragment_free(frag);
380a522f 1048 return 0;
48ff651e 1049 }
0f113f3e
MC
1050 }
1051
1052 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1053 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1054 frag->msg_header.type = s->d1->w_msg_hdr.type;
1055 frag->msg_header.frag_off = 0;
1056 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1057 frag->msg_header.is_ccs = is_ccs;
1058
1059 /* save current state */
1060 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1061 frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1062 frag->msg_header.saved_retransmit_state.compress = s->compress;
1063 frag->msg_header.saved_retransmit_state.session = s->session;
78a39fe7
MC
1064 frag->msg_header.saved_retransmit_state.epoch =
1065 DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
0f113f3e
MC
1066
1067 memset(seq64be, 0, sizeof(seq64be));
1068 seq64be[6] =
1069 (unsigned
1070 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1071 frag->msg_header.is_ccs) >> 8);
1072 seq64be[7] =
1073 (unsigned
1074 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1075 frag->msg_header.is_ccs));
1076
1077 item = pitem_new(seq64be, frag);
1078 if (item == NULL) {
1079 dtls1_hm_fragment_free(frag);
1080 return 0;
1081 }
36d16f8e 1082
0f113f3e
MC
1083 pqueue_insert(s->d1->sent_messages, item);
1084 return 1;
1085}
36d16f8e 1086
a230b26e 1087int dtls1_retransmit_message(SSL *s, unsigned short seq, int *found)
0f113f3e
MC
1088{
1089 int ret;
1090 /* XDTLS: for now assuming that read/writes are blocking */
1091 pitem *item;
1092 hm_fragment *frag;
1093 unsigned long header_length;
1094 unsigned char seq64be[8];
1095 struct dtls1_retransmit_state saved_state;
0f113f3e 1096
0f113f3e
MC
1097 /* XDTLS: the requested message ought to be found, otherwise error */
1098 memset(seq64be, 0, sizeof(seq64be));
1099 seq64be[6] = (unsigned char)(seq >> 8);
1100 seq64be[7] = (unsigned char)seq;
1101
1102 item = pqueue_find(s->d1->sent_messages, seq64be);
1103 if (item == NULL) {
c48ffbcc 1104 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1105 *found = 0;
1106 return 0;
1107 }
1108
1109 *found = 1;
1110 frag = (hm_fragment *)item->data;
1111
1112 if (frag->msg_header.is_ccs)
1113 header_length = DTLS1_CCS_HEADER_LENGTH;
1114 else
1115 header_length = DTLS1_HM_HEADER_LENGTH;
1116
1117 memcpy(s->init_buf->data, frag->fragment,
1118 frag->msg_header.msg_len + header_length);
1119 s->init_num = frag->msg_header.msg_len + header_length;
1120
1121 dtls1_set_message_header_int(s, frag->msg_header.type,
1122 frag->msg_header.msg_len,
1123 frag->msg_header.seq, 0,
1124 frag->msg_header.frag_len);
1125
1126 /* save current state */
1127 saved_state.enc_write_ctx = s->enc_write_ctx;
1128 saved_state.write_hash = s->write_hash;
1129 saved_state.compress = s->compress;
1130 saved_state.session = s->session;
78a39fe7 1131 saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
0f113f3e
MC
1132
1133 s->d1->retransmitting = 1;
1134
1135 /* restore state in which the message was originally sent */
1136 s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1137 s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1138 s->compress = frag->msg_header.saved_retransmit_state.compress;
1139 s->session = frag->msg_header.saved_retransmit_state.session;
3bb8f87d 1140 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,
a230b26e
EK
1141 frag->msg_header.
1142 saved_retransmit_state.epoch);
0f113f3e 1143
0f113f3e
MC
1144 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1145 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1146
1147 /* restore current state */
1148 s->enc_write_ctx = saved_state.enc_write_ctx;
1149 s->write_hash = saved_state.write_hash;
1150 s->compress = saved_state.compress;
1151 s->session = saved_state.session;
3bb8f87d 1152 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);
0f113f3e
MC
1153
1154 s->d1->retransmitting = 0;
1155
2e7dc7cd 1156 (void)BIO_flush(s->wbio);
0f113f3e
MC
1157 return ret;
1158}
36d16f8e 1159
a773b52a 1160void dtls1_set_message_header(SSL *s,
d736bc1a
MC
1161 unsigned char mt, size_t len,
1162 size_t frag_off, size_t frag_len)
0f113f3e 1163{
912c89c5 1164 if (frag_off == 0) {
0f113f3e
MC
1165 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1166 s->d1->next_handshake_write_seq++;
1167 }
54ef01b5 1168
0f113f3e
MC
1169 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1170 frag_off, frag_len);
0f113f3e 1171}
36d16f8e
BL
1172
1173/* don't actually do the writing, wait till the MTU has been retrieved */
1174static void
1175dtls1_set_message_header_int(SSL *s, unsigned char mt,
7ee8627f
MC
1176 size_t len, unsigned short seq_num,
1177 size_t frag_off, size_t frag_len)
0f113f3e
MC
1178{
1179 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1180
1181 msg_hdr->type = mt;
1182 msg_hdr->msg_len = len;
1183 msg_hdr->seq = seq_num;
1184 msg_hdr->frag_off = frag_off;
1185 msg_hdr->frag_len = frag_len;
1186}
36d16f8e
BL
1187
1188static void
7ee8627f 1189dtls1_fix_message_header(SSL *s, size_t frag_off, size_t frag_len)
0f113f3e
MC
1190{
1191 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
36d16f8e 1192
0f113f3e
MC
1193 msg_hdr->frag_off = frag_off;
1194 msg_hdr->frag_len = frag_len;
1195}
54ef01b5 1196
0f113f3e
MC
1197static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
1198{
1199 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
54ef01b5 1200
0f113f3e
MC
1201 *p++ = msg_hdr->type;
1202 l2n3(msg_hdr->msg_len, p);
54ef01b5 1203
0f113f3e
MC
1204 s2n(msg_hdr->seq, p);
1205 l2n3(msg_hdr->frag_off, p);
1206 l2n3(msg_hdr->frag_len, p);
36d16f8e 1207
0f113f3e
MC
1208 return p;
1209}
36d16f8e 1210
a230b26e 1211void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
0f113f3e 1212{
16f8d4eb 1213 memset(msg_hdr, 0, sizeof(*msg_hdr));
0f113f3e
MC
1214 msg_hdr->type = *(data++);
1215 n2l3(data, msg_hdr->msg_len);
54ef01b5 1216
0f113f3e
MC
1217 n2s(data, msg_hdr->seq);
1218 n2l3(data, msg_hdr->frag_off);
1219 n2l3(data, msg_hdr->frag_len);
1220}
2c7b4dbc 1221
a29fa98c 1222int dtls1_set_handshake_header(SSL *s, WPACKET *pkt, int htype)
2c7b4dbc
MC
1223{
1224 unsigned char *header;
de451856 1225
4a01c59f
MC
1226 if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
1227 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1228 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1229 s->d1->handshake_write_seq, 0, 0);
1230 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
1231 return 0;
1232 } else {
1233 dtls1_set_message_header(s, htype, 0, 0, 0);
1234 /*
1235 * We allocate space at the start for the message header. This gets
1236 * filled in later
1237 */
1238 if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
1239 || !WPACKET_start_sub_packet(pkt))
1240 return 0;
1241 }
2c7b4dbc
MC
1242
1243 return 1;
1244}
1245
4a01c59f 1246int dtls1_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
2c7b4dbc
MC
1247{
1248 size_t msglen;
1249
4a01c59f 1250 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
f1ec23c0 1251 || !WPACKET_get_length(pkt, &msglen)
7cea05dc 1252 || msglen > INT_MAX)
2c7b4dbc 1253 return 0;
4a01c59f
MC
1254
1255 if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
1256 s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
1257 s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
1258 }
2c7b4dbc
MC
1259 s->init_num = (int)msglen;
1260 s->init_off = 0;
1261
4a01c59f
MC
1262 if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
1263 /* Buffer the message to handle re-xmits */
1264 if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC
1265 ? 1 : 0))
1266 return 0;
1267 }
2c7b4dbc
MC
1268
1269 return 1;
1270}