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