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