1 /* $OpenBSD: d1_pkt.c,v 1.63 2017/05/07 04:22:24 beck Exp $ */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
116 #include <machine/endian.h>
121 #include "ssl_locl.h"
123 #include <openssl/buffer.h>
124 #include <openssl/evp.h>
127 #include "bytestring.h"
129 static int do_dtls1_write(SSL
*s
, int type
, const unsigned char *buf
,
133 /* mod 128 saturating subtract of two 64-bit values in big-endian order */
135 satsub64be(const unsigned char *v1
, const unsigned char *v2
)
137 int ret
, sat
, brw
, i
;
139 if (sizeof(long) == 8)
143 if (BYTE_ORDER
== LITTLE_ENDIAN
)
145 /* not reached on little-endians */
146 /* following test is redundant, because input is
147 * always aligned, but I take no chances... */
148 if (((size_t)v1
| (size_t)v2
) & 0x7)
161 ret
= (int)v1
[7] - (int)v2
[7];
163 brw
= ret
>> 8; /* brw is either 0 or -1 */
165 for (i
= 6; i
>= 0; i
--) {
166 brw
+= (int)v1
[i
]-(int)v2
[i
];
171 for (i
= 6; i
>= 0; i
--) {
172 brw
+= (int)v1
[i
]-(int)v2
[i
];
177 brw
<<= 8; /* brw is either 0 or -256 */
182 return brw
+ (ret
& 0xFF);
185 static int have_handshake_fragment(SSL
*s
, int type
, unsigned char *buf
,
187 static int dtls1_record_replay_check(SSL
*s
, DTLS1_BITMAP
*bitmap
);
188 static void dtls1_record_bitmap_update(SSL
*s
, DTLS1_BITMAP
*bitmap
);
189 static DTLS1_BITMAP
*dtls1_get_bitmap(SSL
*s
, SSL3_RECORD
*rr
,
190 unsigned int *is_next_epoch
);
191 static int dtls1_buffer_record(SSL
*s
, record_pqueue
*q
,
192 unsigned char *priority
);
193 static int dtls1_process_record(SSL
*s
);
195 /* copy buffered record into SSL structure */
197 dtls1_copy_record(SSL
*s
, pitem
*item
)
199 DTLS1_RECORD_DATA
*rdata
;
201 rdata
= (DTLS1_RECORD_DATA
*)item
->data
;
203 free(s
->s3
->rbuf
.buf
);
205 s
->internal
->packet
= rdata
->packet
;
206 s
->internal
->packet_length
= rdata
->packet_length
;
207 memcpy(&(s
->s3
->rbuf
), &(rdata
->rbuf
), sizeof(SSL3_BUFFER
));
208 memcpy(&(S3I(s
)->rrec
), &(rdata
->rrec
), sizeof(SSL3_RECORD
));
210 /* Set proper sequence number for mac calculation */
211 memcpy(&(S3I(s
)->read_sequence
[2]), &(rdata
->packet
[5]), 6);
218 dtls1_buffer_record(SSL
*s
, record_pqueue
*queue
, unsigned char *priority
)
220 DTLS1_RECORD_DATA
*rdata
;
223 /* Limit the size of the queue to prevent DOS attacks */
224 if (pqueue_size(queue
->q
) >= 100)
227 rdata
= malloc(sizeof(DTLS1_RECORD_DATA
));
228 item
= pitem_new(priority
, rdata
);
229 if (rdata
== NULL
|| item
== NULL
)
232 rdata
->packet
= s
->internal
->packet
;
233 rdata
->packet_length
= s
->internal
->packet_length
;
234 memcpy(&(rdata
->rbuf
), &(s
->s3
->rbuf
), sizeof(SSL3_BUFFER
));
235 memcpy(&(rdata
->rrec
), &(S3I(s
)->rrec
), sizeof(SSL3_RECORD
));
240 s
->internal
->packet
= NULL
;
241 s
->internal
->packet_length
= 0;
242 memset(&(s
->s3
->rbuf
), 0, sizeof(SSL3_BUFFER
));
243 memset(&(S3I(s
)->rrec
), 0, sizeof(SSL3_RECORD
));
245 if (!ssl3_setup_buffers(s
))
248 /* insert should not fail, since duplicates are dropped */
249 if (pqueue_insert(queue
->q
, item
) == NULL
)
255 free(rdata
->rbuf
.buf
);
258 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
266 dtls1_retrieve_buffered_record(SSL
*s
, record_pqueue
*queue
)
270 item
= pqueue_pop(queue
->q
);
272 dtls1_copy_record(s
, item
);
284 /* retrieve a buffered record that belongs to the new epoch, i.e., not processed
286 #define dtls1_get_unprocessed_record(s) \
287 dtls1_retrieve_buffered_record((s), \
288 &((D1I(s))->unprocessed_rcds))
290 /* retrieve a buffered record that belongs to the current epoch, ie, processed */
291 #define dtls1_get_processed_record(s) \
292 dtls1_retrieve_buffered_record((s), \
293 &((D1I(s))->processed_rcds))
296 dtls1_process_buffered_records(SSL
*s
)
300 item
= pqueue_peek(D1I(s
)->unprocessed_rcds
.q
);
302 /* Check if epoch is current. */
303 if (D1I(s
)->unprocessed_rcds
.epoch
!= D1I(s
)->r_epoch
)
307 /* Process all the records. */
308 while (pqueue_peek(D1I(s
)->unprocessed_rcds
.q
)) {
309 dtls1_get_unprocessed_record(s
);
310 if (! dtls1_process_record(s
))
312 if (dtls1_buffer_record(s
, &(D1I(s
)->processed_rcds
),
313 S3I(s
)->rrec
.seq_num
) < 0)
318 /* sync epoch numbers once all the unprocessed records
319 * have been processed */
320 D1I(s
)->processed_rcds
.epoch
= D1I(s
)->r_epoch
;
321 D1I(s
)->unprocessed_rcds
.epoch
= D1I(s
)->r_epoch
+ 1;
327 dtls1_process_record(SSL
*s
)
333 unsigned int mac_size
, orig_len
;
334 unsigned char md
[EVP_MAX_MD_SIZE
];
336 rr
= &(S3I(s
)->rrec
);
339 /* At this point, s->internal->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
340 * and we have that many bytes in s->internal->packet
342 rr
->input
= &(s
->internal
->packet
[DTLS1_RT_HEADER_LENGTH
]);
344 /* ok, we can now read from 's->internal->packet' data into 'rr'
345 * rr->input points at rr->length bytes, which
346 * need to be copied into rr->data by either
347 * the decryption or by the decompression
348 * When the data is 'copied' into the rr->data buffer,
349 * rr->input will be pointed at the new buffer */
351 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
352 * rr->length bytes of encrypted compressed stuff. */
354 /* check is not needed I believe */
355 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
) {
356 al
= SSL_AD_RECORD_OVERFLOW
;
357 SSLerror(s
, SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
361 /* decrypt in place in 'rr->input' */
362 rr
->data
= rr
->input
;
364 enc_err
= s
->method
->internal
->ssl3_enc
->enc(s
, 0);
366 * 0: (in non-constant time) if the record is publically invalid.
367 * 1: if the padding is valid
368 * -1: if the padding is invalid */
370 /* For DTLS we simply ignore bad packets. */
372 s
->internal
->packet_length
= 0;
377 /* r->length is now the compressed data plus mac */
378 if ((sess
!= NULL
) && (s
->enc_read_ctx
!= NULL
) &&
379 (EVP_MD_CTX_md(s
->read_hash
) != NULL
)) {
380 /* s->read_hash != NULL => mac_size != -1 */
381 unsigned char *mac
= NULL
;
382 unsigned char mac_tmp
[EVP_MAX_MD_SIZE
];
383 mac_size
= EVP_MD_CTX_size(s
->read_hash
);
384 OPENSSL_assert(mac_size
<= EVP_MAX_MD_SIZE
);
386 /* kludge: *_cbc_remove_padding passes padding length in rr->type */
387 orig_len
= rr
->length
+ ((unsigned int)rr
->type
>> 8);
389 /* orig_len is the length of the record before any padding was
390 * removed. This is public information, as is the MAC in use,
391 * therefore we can safely process the record in a different
392 * amount of time if it's too short to possibly contain a MAC.
394 if (orig_len
< mac_size
||
395 /* CBC records must have a padding length byte too. */
396 (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
397 orig_len
< mac_size
+ 1)) {
398 al
= SSL_AD_DECODE_ERROR
;
399 SSLerror(s
, SSL_R_LENGTH_TOO_SHORT
);
403 if (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
) {
404 /* We update the length so that the TLS header bytes
405 * can be constructed correctly but we need to extract
406 * the MAC in constant time from within the record,
407 * without leaking the contents of the padding bytes.
410 ssl3_cbc_copy_mac(mac_tmp
, rr
, mac_size
, orig_len
);
411 rr
->length
-= mac_size
;
413 /* In this case there's no padding, so |orig_len|
414 * equals |rec->length| and we checked that there's
415 * enough bytes for |mac_size| above. */
416 rr
->length
-= mac_size
;
417 mac
= &rr
->data
[rr
->length
];
420 i
= tls1_mac(s
, md
, 0 /* not send */);
421 if (i
< 0 || mac
== NULL
|| timingsafe_memcmp(md
, mac
, (size_t)mac_size
) != 0)
423 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+ mac_size
)
428 /* decryption failed, silently discard message */
430 s
->internal
->packet_length
= 0;
434 if (rr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
) {
435 al
= SSL_AD_RECORD_OVERFLOW
;
436 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
441 /* So at this point the following is true
442 * ssl->s3->internal->rrec.type is the type of record
443 * ssl->s3->internal->rrec.length == number of bytes in record
444 * ssl->s3->internal->rrec.off == offset to first valid byte
445 * ssl->s3->internal->rrec.data == where to take bytes from, increment
449 /* we have pulled in a full packet so zero things */
450 s
->internal
->packet_length
= 0;
454 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
460 /* Call this to get a new input record.
461 * It will return <= 0 if more data is needed, normally due to an error
462 * or non-blocking IO.
463 * When it finishes, one packet has been decoded and can be found in
464 * ssl->s3->internal->rrec.type - is the type of record
465 * ssl->s3->internal->rrec.data, - data
466 * ssl->s3->internal->rrec.length, - number of bytes
468 /* used only by dtls1_read_bytes */
470 dtls1_get_record(SSL
*s
)
473 unsigned char *p
= NULL
;
474 DTLS1_BITMAP
*bitmap
;
475 unsigned int is_next_epoch
;
478 rr
= &(S3I(s
)->rrec
);
480 /* The epoch may have changed. If so, process all the
481 * pending records. This is a non-blocking operation. */
482 if (dtls1_process_buffered_records(s
) < 0)
485 /* if we're renegotiating, then there may be buffered records */
486 if (dtls1_get_processed_record(s
))
489 /* get something from the wire */
492 /* dump this record on all retries */
494 s
->internal
->packet_length
= 0;
497 /* check if we have the header */
498 if ((s
->internal
->rstate
!= SSL_ST_READ_BODY
) ||
499 (s
->internal
->packet_length
< DTLS1_RT_HEADER_LENGTH
)) {
501 uint16_t epoch
, len
, ssl_version
;
504 n
= ssl3_packet_read(s
, DTLS1_RT_HEADER_LENGTH
);
508 /* If this packet contained a partial record, dump it. */
509 if (n
!= DTLS1_RT_HEADER_LENGTH
)
512 s
->internal
->rstate
= SSL_ST_READ_BODY
;
514 CBS_init(&header
, s
->internal
->packet
, s
->internal
->packet_length
);
516 /* Pull apart the header into the DTLS1_RECORD */
517 if (!CBS_get_u8(&header
, &type
))
519 if (!CBS_get_u16(&header
, &ssl_version
))
522 /* sequence number is 64 bits, with top 2 bytes = epoch */
523 if (!CBS_get_u16(&header
, &epoch
) ||
524 !CBS_get_bytes(&header
, &seq_no
, 6))
527 if (!CBS_write_bytes(&seq_no
, &(S3I(s
)->read_sequence
[2]),
528 sizeof(S3I(s
)->read_sequence
) - 2, NULL
))
530 if (!CBS_get_u16(&header
, &len
))
537 /* unexpected version, silently discard */
538 if (!s
->internal
->first_packet
&& ssl_version
!= s
->version
)
541 /* wrong version, silently discard record */
542 if ((ssl_version
& 0xff00) != (s
->version
& 0xff00))
545 /* record too long, silently discard it */
546 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
)
549 /* now s->internal->rstate == SSL_ST_READ_BODY */
550 p
= (unsigned char *)CBS_data(&header
);
553 /* s->internal->rstate == SSL_ST_READ_BODY, get and decode the data */
555 n
= ssl3_packet_extend(s
, DTLS1_RT_HEADER_LENGTH
+ rr
->length
);
559 /* If this packet contained a partial record, dump it. */
560 if (n
!= DTLS1_RT_HEADER_LENGTH
+ rr
->length
)
563 s
->internal
->rstate
= SSL_ST_READ_HEADER
; /* set state for later operations */
565 /* match epochs. NULL means the packet is dropped on the floor */
566 bitmap
= dtls1_get_bitmap(s
, rr
, &is_next_epoch
);
571 * Check whether this is a repeat, or aged record.
572 * Don't check if we're listening and this message is
573 * a ClientHello. They can look as if they're replayed,
574 * since they arrive from different connections and
575 * would be dropped unnecessarily.
577 if (!(D1I(s
)->listen
&& rr
->type
== SSL3_RT_HANDSHAKE
&&
578 p
!= NULL
&& *p
== SSL3_MT_CLIENT_HELLO
) &&
579 !dtls1_record_replay_check(s
, bitmap
))
582 /* just read a 0 length packet */
586 /* If this record is from the next epoch (either HM or ALERT),
587 * and a handshake is currently in progress, buffer it since it
588 * cannot be processed at this time. However, do not buffer
589 * anything while listening.
592 if ((SSL_in_init(s
) || s
->internal
->in_handshake
) && !D1I(s
)->listen
) {
593 if (dtls1_buffer_record(s
, &(D1I(s
)->unprocessed_rcds
),
596 /* Mark receipt of record. */
597 dtls1_record_bitmap_update(s
, bitmap
);
602 if (!dtls1_process_record(s
))
605 /* Mark receipt of record. */
606 dtls1_record_bitmap_update(s
, bitmap
);
611 /* Return up to 'len' payload bytes received in 'type' records.
612 * 'type' is one of the following:
614 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
615 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
616 * - 0 (during a shutdown, no data has to be returned)
618 * If we don't have stored data to work from, read a SSL/TLS record first
619 * (possibly multiple records if we still don't have anything to return).
621 * This function must handle any surprises the peer may have for us, such as
622 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
623 * a surprise, but handled as if it were), or renegotiation requests.
624 * Also if record payloads contain fragments too small to process, we store
625 * them until there is enough for the respective protocol (the record protocol
626 * may use arbitrary fragmentation and even interleaving):
627 * Change cipher spec protocol
628 * just 1 byte needed, no need for keeping anything stored
630 * 2 bytes needed (AlertLevel, AlertDescription)
632 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
633 * to detect unexpected Client Hello and Hello Request messages
634 * here, anything else is handled by higher layers
635 * Application data protocol
636 * none of our business
639 dtls1_read_bytes(SSL
*s
, int type
, unsigned char *buf
, int len
, int peek
)
644 void (*cb
)(const SSL
*ssl
, int type2
, int val
) = NULL
;
646 if (s
->s3
->rbuf
.buf
== NULL
) /* Not initialized yet */
647 if (!ssl3_setup_buffers(s
))
651 type
!= SSL3_RT_APPLICATION_DATA
&& type
!= SSL3_RT_HANDSHAKE
) ||
652 (peek
&& (type
!= SSL3_RT_APPLICATION_DATA
))) {
653 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
657 /* check whether there's a handshake message (client hello?) waiting */
658 if ((ret
= have_handshake_fragment(s
, type
, buf
, len
, peek
)))
661 /* Now D1I(s)->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
663 if (!s
->internal
->in_handshake
&& SSL_in_init(s
))
665 /* type == SSL3_RT_APPLICATION_DATA */
666 i
= s
->internal
->handshake_func(s
);
670 SSLerror(s
, SSL_R_SSL_HANDSHAKE_FAILURE
);
676 s
->internal
->rwstate
= SSL_NOTHING
;
678 /* S3I(s)->rrec.type - is the type of record
679 * S3I(s)->rrec.data, - data
680 * S3I(s)->rrec.off, - offset into 'data' for next read
681 * S3I(s)->rrec.length, - number of bytes. */
682 rr
= &(S3I(s
)->rrec
);
684 /* We are not handshaking and have no data yet,
685 * so process data buffered during the last handshake
686 * in advance, if any.
688 if (S3I(s
)->hs
.state
== SSL_ST_OK
&& rr
->length
== 0) {
690 item
= pqueue_pop(D1I(s
)->buffered_app_data
.q
);
693 dtls1_copy_record(s
, item
);
700 /* Check for timeout */
701 if (dtls1_handle_timeout(s
) > 0)
704 /* get new packet if necessary */
705 if ((rr
->length
== 0) || (s
->internal
->rstate
== SSL_ST_READ_BODY
)) {
706 ret
= dtls1_get_record(s
);
708 ret
= dtls1_read_failed(s
, ret
);
709 /* anything other than a timeout is an error */
717 if (D1I(s
)->listen
&& rr
->type
!= SSL3_RT_HANDSHAKE
) {
722 /* we now have a packet which can be read and processed */
724 if (S3I(s
)->change_cipher_spec
/* set when we receive ChangeCipherSpec,
725 * reset by ssl3_get_finished */
726 && (rr
->type
!= SSL3_RT_HANDSHAKE
)) {
727 /* We now have application data between CCS and Finished.
728 * Most likely the packets were reordered on their way, so
729 * buffer the application data for later processing rather
730 * than dropping the connection.
732 if (dtls1_buffer_record(s
, &(D1I(s
)->buffered_app_data
),
734 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
741 /* If the other end has shut down, throw anything we read away
742 * (even in 'peek' mode) */
743 if (s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
745 s
->internal
->rwstate
= SSL_NOTHING
;
750 if (type
== rr
->type
) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
752 /* make sure that we are not getting application data when we
753 * are doing a handshake for the first time */
754 if (SSL_in_init(s
) && (type
== SSL3_RT_APPLICATION_DATA
) &&
755 (s
->enc_read_ctx
== NULL
)) {
756 al
= SSL_AD_UNEXPECTED_MESSAGE
;
757 SSLerror(s
, SSL_R_APP_DATA_IN_HANDSHAKE
);
764 if ((unsigned int)len
> rr
->length
)
767 n
= (unsigned int)len
;
769 memcpy(buf
, &(rr
->data
[rr
->off
]), n
);
773 if (rr
->length
== 0) {
774 s
->internal
->rstate
= SSL_ST_READ_HEADER
;
783 /* If we get here, then type != rr->type; if we have a handshake
784 * message, then it was unexpected (Hello Request or Client Hello). */
786 /* In case of record types for which we have 'fragment' storage,
787 * fill that so that we can process the data at a fixed place.
790 unsigned int k
, dest_maxlen
= 0;
791 unsigned char *dest
= NULL
;
792 unsigned int *dest_len
= NULL
;
794 if (rr
->type
== SSL3_RT_HANDSHAKE
) {
795 dest_maxlen
= sizeof D1I(s
)->handshake_fragment
;
796 dest
= D1I(s
)->handshake_fragment
;
797 dest_len
= &D1I(s
)->handshake_fragment_len
;
798 } else if (rr
->type
== SSL3_RT_ALERT
) {
799 dest_maxlen
= sizeof(D1I(s
)->alert_fragment
);
800 dest
= D1I(s
)->alert_fragment
;
801 dest_len
= &D1I(s
)->alert_fragment_len
;
803 /* else it's a CCS message, or application data or wrong */
804 else if (rr
->type
!= SSL3_RT_CHANGE_CIPHER_SPEC
) {
805 /* Application data while renegotiating
806 * is allowed. Try again reading.
808 if (rr
->type
== SSL3_RT_APPLICATION_DATA
) {
810 S3I(s
)->in_read_app_data
= 2;
811 bio
= SSL_get_rbio(s
);
812 s
->internal
->rwstate
= SSL_READING
;
813 BIO_clear_retry_flags(bio
);
814 BIO_set_retry_read(bio
);
818 /* Not certain if this is the right error handling */
819 al
= SSL_AD_UNEXPECTED_MESSAGE
;
820 SSLerror(s
, SSL_R_UNEXPECTED_RECORD
);
824 if (dest_maxlen
> 0) {
825 /* XDTLS: In a pathalogical case, the Client Hello
826 * may be fragmented--don't always expect dest_maxlen bytes */
827 if (rr
->length
< dest_maxlen
) {
828 #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
830 * for normal alerts rr->length is 2, while
831 * dest_maxlen is 7 if we were to handle this
832 * non-existing alert...
836 s
->internal
->rstate
= SSL_ST_READ_HEADER
;
841 /* now move 'n' bytes: */
842 for ( k
= 0; k
< dest_maxlen
; k
++) {
843 dest
[k
] = rr
->data
[rr
->off
++];
846 *dest_len
= dest_maxlen
;
850 /* D1I(s)->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
851 * D1I(s)->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
852 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
854 /* If we are a client, check for an incoming 'Hello Request': */
856 (D1I(s
)->handshake_fragment_len
>= DTLS1_HM_HEADER_LENGTH
) &&
857 (D1I(s
)->handshake_fragment
[0] == SSL3_MT_HELLO_REQUEST
) &&
858 (s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
)) {
859 D1I(s
)->handshake_fragment_len
= 0;
861 if ((D1I(s
)->handshake_fragment
[1] != 0) ||
862 (D1I(s
)->handshake_fragment
[2] != 0) ||
863 (D1I(s
)->handshake_fragment
[3] != 0)) {
864 al
= SSL_AD_DECODE_ERROR
;
865 SSLerror(s
, SSL_R_BAD_HELLO_REQUEST
);
869 /* no need to check sequence number on HELLO REQUEST messages */
871 if (s
->internal
->msg_callback
)
872 s
->internal
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
873 D1I(s
)->handshake_fragment
, 4, s
, s
->internal
->msg_callback_arg
);
875 if (SSL_is_init_finished(s
) &&
876 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
) &&
877 !S3I(s
)->renegotiate
) {
878 D1I(s
)->handshake_read_seq
++;
879 s
->internal
->new_session
= 1;
881 if (ssl3_renegotiate_check(s
)) {
882 i
= s
->internal
->handshake_func(s
);
886 SSLerror(s
, SSL_R_SSL_HANDSHAKE_FAILURE
);
890 if (!(s
->internal
->mode
& SSL_MODE_AUTO_RETRY
)) {
891 if (s
->s3
->rbuf
.left
== 0) /* no read-ahead left? */
894 /* In the case where we try to read application data,
895 * but we trigger an SSL handshake, we return -1 with
896 * the retry option set. Otherwise renegotiation may
897 * cause nasty problems in the blocking world */
898 s
->internal
->rwstate
= SSL_READING
;
899 bio
= SSL_get_rbio(s
);
900 BIO_clear_retry_flags(bio
);
901 BIO_set_retry_read(bio
);
907 /* we either finished a handshake or ignored the request,
908 * now try again to obtain the (application) data we were asked for */
912 if (D1I(s
)->alert_fragment_len
>= DTLS1_AL_HEADER_LENGTH
) {
913 int alert_level
= D1I(s
)->alert_fragment
[0];
914 int alert_descr
= D1I(s
)->alert_fragment
[1];
916 D1I(s
)->alert_fragment_len
= 0;
918 if (s
->internal
->msg_callback
)
919 s
->internal
->msg_callback(0, s
->version
, SSL3_RT_ALERT
,
920 D1I(s
)->alert_fragment
, 2, s
, s
->internal
->msg_callback_arg
);
922 if (s
->internal
->info_callback
!= NULL
)
923 cb
= s
->internal
->info_callback
;
924 else if (s
->ctx
->internal
->info_callback
!= NULL
)
925 cb
= s
->ctx
->internal
->info_callback
;
928 j
= (alert_level
<< 8) | alert_descr
;
929 cb(s
, SSL_CB_READ_ALERT
, j
);
932 if (alert_level
== 1) /* warning */
934 S3I(s
)->warn_alert
= alert_descr
;
935 if (alert_descr
== SSL_AD_CLOSE_NOTIFY
) {
936 s
->internal
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
939 } else if (alert_level
== 2) /* fatal */
941 s
->internal
->rwstate
= SSL_NOTHING
;
942 S3I(s
)->fatal_alert
= alert_descr
;
943 SSLerror(s
, SSL_AD_REASON_OFFSET
+ alert_descr
);
944 ERR_asprintf_error_data("SSL alert number %d",
946 s
->internal
->shutdown
|=SSL_RECEIVED_SHUTDOWN
;
947 SSL_CTX_remove_session(s
->ctx
, s
->session
);
950 al
= SSL_AD_ILLEGAL_PARAMETER
;
951 SSLerror(s
, SSL_R_UNKNOWN_ALERT_TYPE
);
958 if (s
->internal
->shutdown
& SSL_SENT_SHUTDOWN
) /* but we have not received a shutdown */
960 s
->internal
->rwstate
= SSL_NOTHING
;
965 if (rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
966 struct ccs_header_st ccs_hdr
;
967 unsigned int ccs_hdr_len
= DTLS1_CCS_HEADER_LENGTH
;
969 dtls1_get_ccs_header(rr
->data
, &ccs_hdr
);
971 /* 'Change Cipher Spec' is just a single byte, so we know
972 * exactly what the record payload has to look like */
973 /* XDTLS: check that epoch is consistent */
974 if ((rr
->length
!= ccs_hdr_len
) ||
975 (rr
->off
!= 0) || (rr
->data
[0] != SSL3_MT_CCS
)) {
976 i
= SSL_AD_ILLEGAL_PARAMETER
;
977 SSLerror(s
, SSL_R_BAD_CHANGE_CIPHER_SPEC
);
983 if (s
->internal
->msg_callback
)
984 s
->internal
->msg_callback(0, s
->version
, SSL3_RT_CHANGE_CIPHER_SPEC
,
985 rr
->data
, 1, s
, s
->internal
->msg_callback_arg
);
987 /* We can't process a CCS now, because previous handshake
988 * messages are still missing, so just drop it.
990 if (!D1I(s
)->change_cipher_spec_ok
) {
994 D1I(s
)->change_cipher_spec_ok
= 0;
996 S3I(s
)->change_cipher_spec
= 1;
997 if (!ssl3_do_change_cipher_spec(s
))
1000 /* do this whenever CCS is processed */
1001 dtls1_reset_seq_numbers(s
, SSL3_CC_READ
);
1006 /* Unexpected handshake message (Client Hello, or protocol violation) */
1007 if ((D1I(s
)->handshake_fragment_len
>= DTLS1_HM_HEADER_LENGTH
) &&
1008 !s
->internal
->in_handshake
) {
1009 struct hm_header_st msg_hdr
;
1011 /* this may just be a stale retransmit */
1012 if (!dtls1_get_message_header(rr
->data
, &msg_hdr
))
1014 if (rr
->epoch
!= D1I(s
)->r_epoch
) {
1019 /* If we are server, we may have a repeated FINISHED of the
1020 * client here, then retransmit our CCS and FINISHED.
1022 if (msg_hdr
.type
== SSL3_MT_FINISHED
) {
1023 if (dtls1_check_timeout_num(s
) < 0)
1026 dtls1_retransmit_buffered_messages(s
);
1031 if (((S3I(s
)->hs
.state
&SSL_ST_MASK
) == SSL_ST_OK
) &&
1032 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
)) {
1033 S3I(s
)->hs
.state
= s
->server
? SSL_ST_ACCEPT
: SSL_ST_CONNECT
;
1034 s
->internal
->renegotiate
= 1;
1035 s
->internal
->new_session
= 1;
1037 i
= s
->internal
->handshake_func(s
);
1041 SSLerror(s
, SSL_R_SSL_HANDSHAKE_FAILURE
);
1045 if (!(s
->internal
->mode
& SSL_MODE_AUTO_RETRY
)) {
1046 if (s
->s3
->rbuf
.left
== 0) /* no read-ahead left? */
1049 /* In the case where we try to read application data,
1050 * but we trigger an SSL handshake, we return -1 with
1051 * the retry option set. Otherwise renegotiation may
1052 * cause nasty problems in the blocking world */
1053 s
->internal
->rwstate
= SSL_READING
;
1054 bio
= SSL_get_rbio(s
);
1055 BIO_clear_retry_flags(bio
);
1056 BIO_set_retry_read(bio
);
1065 /* TLS just ignores unknown message types */
1066 if (s
->version
== TLS1_VERSION
) {
1070 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1071 SSLerror(s
, SSL_R_UNEXPECTED_RECORD
);
1073 case SSL3_RT_CHANGE_CIPHER_SPEC
:
1075 case SSL3_RT_HANDSHAKE
:
1076 /* we already handled all of these, with the possible exception
1077 * of SSL3_RT_HANDSHAKE when s->internal->in_handshake is set, but that
1078 * should not happen when type != rr->type */
1079 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1080 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1082 case SSL3_RT_APPLICATION_DATA
:
1083 /* At this point, we were expecting handshake data,
1084 * but have application data. If the library was
1085 * running inside ssl3_read() (i.e. in_read_app_data
1086 * is set) and it makes sense to read application data
1087 * at this point (session renegotiation not yet started),
1088 * we will indulge it.
1090 if (S3I(s
)->in_read_app_data
&&
1091 (S3I(s
)->total_renegotiations
!= 0) &&
1092 (((S3I(s
)->hs
.state
& SSL_ST_CONNECT
) &&
1093 (S3I(s
)->hs
.state
>= SSL3_ST_CW_CLNT_HELLO_A
) &&
1094 (S3I(s
)->hs
.state
<= SSL3_ST_CR_SRVR_HELLO_A
)) || (
1095 (S3I(s
)->hs
.state
& SSL_ST_ACCEPT
) &&
1096 (S3I(s
)->hs
.state
<= SSL3_ST_SW_HELLO_REQ_A
) &&
1097 (S3I(s
)->hs
.state
>= SSL3_ST_SR_CLNT_HELLO_A
)))) {
1098 S3I(s
)->in_read_app_data
= 2;
1101 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1102 SSLerror(s
, SSL_R_UNEXPECTED_RECORD
);
1109 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1115 dtls1_write_app_data_bytes(SSL
*s
, int type
, const void *buf_
, int len
)
1119 if (SSL_in_init(s
) && !s
->internal
->in_handshake
)
1121 i
= s
->internal
->handshake_func(s
);
1125 SSLerror(s
, SSL_R_SSL_HANDSHAKE_FAILURE
);
1130 if (len
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1131 SSLerror(s
, SSL_R_DTLS_MESSAGE_TOO_BIG
);
1135 i
= dtls1_write_bytes(s
, type
, buf_
, len
);
1140 /* this only happens when a client hello is received and a handshake
1143 have_handshake_fragment(SSL
*s
, int type
, unsigned char *buf
,
1147 if ((type
== SSL3_RT_HANDSHAKE
) && (D1I(s
)->handshake_fragment_len
> 0))
1148 /* (partially) satisfy request from storage */
1150 unsigned char *src
= D1I(s
)->handshake_fragment
;
1151 unsigned char *dst
= buf
;
1156 while ((len
> 0) && (D1I(s
)->handshake_fragment_len
> 0)) {
1159 D1I(s
)->handshake_fragment_len
--;
1162 /* move any remaining fragment bytes: */
1163 for (k
= 0; k
< D1I(s
)->handshake_fragment_len
; k
++)
1164 D1I(s
)->handshake_fragment
[k
] = *src
++;
1172 /* Call this to write data in records of type 'type'
1173 * It will return <= 0 if not all data has been sent or non-blocking IO.
1176 dtls1_write_bytes(SSL
*s
, int type
, const void *buf
, int len
)
1180 OPENSSL_assert(len
<= SSL3_RT_MAX_PLAIN_LENGTH
);
1181 s
->internal
->rwstate
= SSL_NOTHING
;
1182 i
= do_dtls1_write(s
, type
, buf
, len
);
1187 do_dtls1_write(SSL
*s
, int type
, const unsigned char *buf
, unsigned int len
)
1189 unsigned char *p
, *pseq
;
1190 int i
, mac_size
, clear
= 0;
1197 /* first check if there is a SSL3_BUFFER still being written
1198 * out. This will happen with non blocking IO */
1199 if (s
->s3
->wbuf
.left
!= 0) {
1200 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
1201 return (ssl3_write_pending(s
, type
, buf
, len
));
1204 /* If we have an alert to send, lets send it */
1205 if (s
->s3
->alert_dispatch
) {
1206 i
= s
->method
->ssl_dispatch_alert(s
);
1209 /* if it went, fall through and send more stuff */
1215 wr
= &(S3I(s
)->wrec
);
1216 wb
= &(s
->s3
->wbuf
);
1219 if ((sess
== NULL
) || (s
->internal
->enc_write_ctx
== NULL
) ||
1220 (EVP_MD_CTX_md(s
->internal
->write_hash
) == NULL
))
1226 mac_size
= EVP_MD_CTX_size(s
->internal
->write_hash
);
1231 /* DTLS implements explicit IV, so no need for empty fragments. */
1233 p
= wb
->buf
+ prefix_len
;
1235 /* write the header */
1240 *(p
++) = (s
->version
>> 8);
1241 *(p
++) = s
->version
&0xff;
1243 /* field where we are to write out packet epoch, seq num and len */
1248 /* lets setup the record stuff. */
1250 /* Make space for the explicit IV in case of CBC.
1251 * (this is a bit of a boundary violation, but what the heck).
1253 if (s
->internal
->enc_write_ctx
&&
1254 (EVP_CIPHER_mode(s
->internal
->enc_write_ctx
->cipher
) & EVP_CIPH_CBC_MODE
))
1255 bs
= EVP_CIPHER_block_size(s
->internal
->enc_write_ctx
->cipher
);
1260 /* make room for IV in case of CBC */
1261 wr
->length
= (int)len
;
1262 wr
->input
= (unsigned char *)buf
;
1264 /* we now 'read' from wr->input, wr->length bytes into
1267 memcpy(wr
->data
, wr
->input
, wr
->length
);
1268 wr
->input
= wr
->data
;
1270 /* we should still have the output to wr->data and the input
1271 * from wr->input. Length should be wr->length.
1272 * wr->data still points in the wb->buf */
1274 if (mac_size
!= 0) {
1275 if (tls1_mac(s
, &(p
[wr
->length
+ bs
]), 1) < 0)
1277 wr
->length
+= mac_size
;
1280 /* this is true regardless of mac size */
1285 /* ssl3_enc can only have an error on read */
1286 if (bs
) /* bs != 0 in case of CBC */
1288 arc4random_buf(p
, bs
);
1289 /* master IV and last CBC residue stand for
1290 * the rest of randomness */
1294 s
->method
->internal
->ssl3_enc
->enc(s
, 1);
1296 /* record length after mac and block padding */
1297 /* if (type == SSL3_RT_APPLICATION_DATA ||
1298 (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */
1300 /* there's only one epoch between handshake and app data */
1302 s2n(D1I(s
)->w_epoch
, pseq
);
1306 s2n(D1I(s)->handshake_epoch, pseq);
1309 memcpy(pseq
, &(S3I(s
)->write_sequence
[2]), 6);
1311 s2n(wr
->length
, pseq
);
1313 /* we should now have
1314 * wr->data pointing to the encrypted data, which is
1315 * wr->length long */
1316 wr
->type
=type
; /* not needed but helps for debugging */
1317 wr
->length
+= DTLS1_RT_HEADER_LENGTH
;
1319 tls1_record_sequence_increment(S3I(s
)->write_sequence
);
1321 /* now let's set up wb */
1322 wb
->left
= prefix_len
+ wr
->length
;
1325 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
1326 S3I(s
)->wpend_tot
= len
;
1327 S3I(s
)->wpend_buf
= buf
;
1328 S3I(s
)->wpend_type
= type
;
1329 S3I(s
)->wpend_ret
= len
;
1331 /* we now just need to write the buffer */
1332 return ssl3_write_pending(s
, type
, buf
, len
);
1340 dtls1_record_replay_check(SSL
*s
, DTLS1_BITMAP
*bitmap
)
1344 const unsigned char *seq
= S3I(s
)->read_sequence
;
1346 cmp
= satsub64be(seq
, bitmap
->max_seq_num
);
1348 memcpy (S3I(s
)->rrec
.seq_num
, seq
, 8);
1349 return 1; /* this record in new */
1352 if (shift
>= sizeof(bitmap
->map
)*8)
1353 return 0; /* stale, outside the window */
1354 else if (bitmap
->map
& (1UL << shift
))
1355 return 0; /* record previously received */
1357 memcpy(S3I(s
)->rrec
.seq_num
, seq
, 8);
1363 dtls1_record_bitmap_update(SSL
*s
, DTLS1_BITMAP
*bitmap
)
1367 const unsigned char *seq
= S3I(s
)->read_sequence
;
1369 cmp
= satsub64be(seq
, bitmap
->max_seq_num
);
1372 if (shift
< sizeof(bitmap
->map
)*8)
1373 bitmap
->map
<<= shift
, bitmap
->map
|= 1UL;
1376 memcpy(bitmap
->max_seq_num
, seq
, 8);
1379 if (shift
< sizeof(bitmap
->map
) * 8)
1380 bitmap
->map
|= 1UL << shift
;
1386 dtls1_dispatch_alert(SSL
*s
)
1389 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
1390 unsigned char buf
[DTLS1_AL_HEADER_LENGTH
];
1391 unsigned char *ptr
= &buf
[0];
1393 s
->s3
->alert_dispatch
= 0;
1395 memset(buf
, 0x00, sizeof(buf
));
1396 *ptr
++ = s
->s3
->send_alert
[0];
1397 *ptr
++ = s
->s3
->send_alert
[1];
1399 #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1400 if (s
->s3
->send_alert
[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
) {
1401 s2n(D1I(s
)->handshake_read_seq
, ptr
);
1402 l2n3(D1I(s
)->r_msg_hdr
.frag_off
, ptr
);
1406 i
= do_dtls1_write(s
, SSL3_RT_ALERT
, &buf
[0], sizeof(buf
));
1408 s
->s3
->alert_dispatch
= 1;
1409 /* fprintf( stderr, "not done with alert\n" ); */
1411 if (s
->s3
->send_alert
[0] == SSL3_AL_FATAL
1412 #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1413 || s
->s3
->send_alert
[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1416 (void)BIO_flush(s
->wbio
);
1418 if (s
->internal
->msg_callback
)
1419 s
->internal
->msg_callback(1, s
->version
, SSL3_RT_ALERT
,
1420 s
->s3
->send_alert
, 2, s
, s
->internal
->msg_callback_arg
);
1422 if (s
->internal
->info_callback
!= NULL
)
1423 cb
= s
->internal
->info_callback
;
1424 else if (s
->ctx
->internal
->info_callback
!= NULL
)
1425 cb
= s
->ctx
->internal
->info_callback
;
1428 j
= (s
->s3
->send_alert
[0]<<8)|s
->s3
->send_alert
[1];
1429 cb(s
, SSL_CB_WRITE_ALERT
, j
);
1436 static DTLS1_BITMAP
*
1437 dtls1_get_bitmap(SSL
*s
, SSL3_RECORD
*rr
, unsigned int *is_next_epoch
)
1442 /* In current epoch, accept HM, CCS, DATA, & ALERT */
1443 if (rr
->epoch
== D1I(s
)->r_epoch
)
1444 return &D1I(s
)->bitmap
;
1446 /* Only HM and ALERT messages can be from the next epoch */
1447 else if (rr
->epoch
== (unsigned long)(D1I(s
)->r_epoch
+ 1) &&
1448 (rr
->type
== SSL3_RT_HANDSHAKE
|| rr
->type
== SSL3_RT_ALERT
)) {
1450 return &D1I(s
)->next_bitmap
;
1457 dtls1_reset_seq_numbers(SSL
*s
, int rw
)
1460 unsigned int seq_bytes
= sizeof(S3I(s
)->read_sequence
);
1462 if (rw
& SSL3_CC_READ
) {
1463 seq
= S3I(s
)->read_sequence
;
1465 memcpy(&(D1I(s
)->bitmap
), &(D1I(s
)->next_bitmap
), sizeof(DTLS1_BITMAP
));
1466 memset(&(D1I(s
)->next_bitmap
), 0x00, sizeof(DTLS1_BITMAP
));
1468 seq
= S3I(s
)->write_sequence
;
1469 memcpy(D1I(s
)->last_write_sequence
, seq
, sizeof(S3I(s
)->write_sequence
));
1473 memset(seq
, 0x00, seq_bytes
);