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