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