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