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.]
119 #include "ssl_locl.h"
120 #include <openssl/evp.h>
121 #include <openssl/buffer.h>
122 #include <openssl/pqueue.h>
124 static int have_handshake_fragment(SSL
*s
, int type
, unsigned char *buf
,
126 static int dtls1_record_replay_check(SSL
*s
, DTLS1_BITMAP
*bitmap
,
128 static void dtls1_record_bitmap_update(SSL
*s
, DTLS1_BITMAP
*bitmap
);
129 static DTLS1_BITMAP
*dtls1_get_bitmap(SSL
*s
, SSL3_RECORD
*rr
,
130 unsigned int *is_next_epoch
);
132 static int dtls1_record_needs_buffering(SSL
*s
, SSL3_RECORD
*rr
,
133 unsigned short *priority
, unsigned long *offset
);
135 static int dtls1_buffer_record(SSL
*s
, record_pqueue
*q
,
137 static int dtls1_process_record(SSL
*s
);
138 static BN_ULLONG
bytes_to_long_long(unsigned char *bytes
);
139 static void long_long_to_bytes(BN_ULLONG num
, unsigned char *bytes
);
140 static void dtls1_clear_timeouts(SSL
*s
);
143 /* copy buffered record into SSL structure */
145 dtls1_copy_record(SSL
*s
, pitem
*item
)
147 DTLS1_RECORD_DATA
*rdata
;
149 rdata
= (DTLS1_RECORD_DATA
*)item
->data
;
151 if (s
->s3
->rbuf
.buf
!= NULL
)
152 OPENSSL_free(s
->s3
->rbuf
.buf
);
154 s
->packet
= rdata
->packet
;
155 s
->packet_length
= rdata
->packet_length
;
156 memcpy(&(s
->s3
->rbuf
), &(rdata
->rbuf
), sizeof(SSL3_BUFFER
));
157 memcpy(&(s
->s3
->rrec
), &(rdata
->rrec
), sizeof(SSL3_RECORD
));
164 dtls1_buffer_record(SSL
*s
, record_pqueue
*queue
, BN_ULLONG priority
)
166 DTLS1_RECORD_DATA
*rdata
;
169 rdata
= OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA
));
170 item
= pitem_new(priority
, rdata
);
171 if (rdata
== NULL
|| item
== NULL
)
173 if (rdata
!= NULL
) OPENSSL_free(rdata
);
174 if (item
!= NULL
) pitem_free(item
);
176 SSLerr(SSL_F_DTLS1_BUFFER_RECORD
, ERR_R_INTERNAL_ERROR
);
180 rdata
->packet
= s
->packet
;
181 rdata
->packet_length
= s
->packet_length
;
182 memcpy(&(rdata
->rbuf
), &(s
->s3
->rbuf
), sizeof(SSL3_BUFFER
));
183 memcpy(&(rdata
->rrec
), &(s
->s3
->rrec
), sizeof(SSL3_RECORD
));
187 /* insert should not fail, since duplicates are dropped */
188 if (pqueue_insert(queue
->q
, item
) == NULL
)
196 s
->packet_length
= 0;
197 memset(&(s
->s3
->rbuf
), 0, sizeof(SSL3_BUFFER
));
198 memset(&(s
->s3
->rrec
), 0, sizeof(SSL3_RECORD
));
200 ssl3_setup_buffers(s
);
207 dtls1_retrieve_buffered_record(SSL
*s
, record_pqueue
*queue
)
211 item
= pqueue_pop(queue
->q
);
214 dtls1_copy_record(s
, item
);
216 OPENSSL_free(item
->data
);
226 /* retrieve a buffered record that belongs to the new epoch, i.e., not processed
228 #define dtls1_get_unprocessed_record(s) \
229 dtls1_retrieve_buffered_record((s), \
230 &((s)->d1->unprocessed_rcds))
232 /* retrieve a buffered record that belongs to the current epoch, ie, processed */
233 #define dtls1_get_processed_record(s) \
234 dtls1_retrieve_buffered_record((s), \
235 &((s)->d1->processed_rcds))
238 dtls1_process_buffered_records(SSL
*s
)
242 item
= pqueue_peek(s
->d1
->unprocessed_rcds
.q
);
245 DTLS1_RECORD_DATA
*rdata
;
246 rdata
= (DTLS1_RECORD_DATA
*)item
->data
;
248 /* Check if epoch is current. */
249 if (s
->d1
->unprocessed_rcds
.epoch
!= s
->d1
->r_epoch
)
250 return(1); /* Nothing to do. */
252 /* Process all the records. */
253 while (pqueue_peek(s
->d1
->unprocessed_rcds
.q
))
255 dtls1_get_unprocessed_record(s
);
256 if ( ! dtls1_process_record(s
))
258 dtls1_buffer_record(s
, &(s
->d1
->processed_rcds
),
259 s
->s3
->rrec
.seq_num
);
263 /* sync epoch numbers once all the unprocessed records
264 * have been processed */
265 s
->d1
->processed_rcds
.epoch
= s
->d1
->r_epoch
;
266 s
->d1
->unprocessed_rcds
.epoch
= s
->d1
->r_epoch
+ 1;
275 dtls1_get_buffered_record(SSL
*s
)
279 (((BN_ULLONG
)s
->d1
->handshake_read_seq
) << 32) |
280 ((BN_ULLONG
)s
->d1
->r_msg_hdr
.frag_off
);
282 if ( ! SSL_in_init(s
)) /* if we're not (re)negotiating,
287 item
= pqueue_peek(s
->d1
->rcvd_records
);
288 if (item
&& item
->priority
== priority
)
290 /* Check if we've received the record of interest. It must be
291 * a handshake record, since data records as passed up without
293 DTLS1_RECORD_DATA
*rdata
;
294 item
= pqueue_pop(s
->d1
->rcvd_records
);
295 rdata
= (DTLS1_RECORD_DATA
*)item
->data
;
297 if (s
->s3
->rbuf
.buf
!= NULL
)
298 OPENSSL_free(s
->s3
->rbuf
.buf
);
300 s
->packet
= rdata
->packet
;
301 s
->packet_length
= rdata
->packet_length
;
302 memcpy(&(s
->s3
->rbuf
), &(rdata
->rbuf
), sizeof(SSL3_BUFFER
));
303 memcpy(&(s
->s3
->rrec
), &(rdata
->rrec
), sizeof(SSL3_RECORD
));
305 OPENSSL_free(item
->data
);
308 /* s->d1->next_expected_seq_num++; */
318 dtls1_process_record(SSL
*s
)
325 unsigned int mac_size
;
326 unsigned char md
[EVP_MAX_MD_SIZE
];
332 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
333 * and we have that many bytes in s->packet
335 rr
->input
= &(s
->packet
[DTLS1_RT_HEADER_LENGTH
]);
337 /* ok, we can now read from 's->packet' data into 'rr'
338 * rr->input points at rr->length bytes, which
339 * need to be copied into rr->data by either
340 * the decryption or by the decompression
341 * When the data is 'copied' into the rr->data buffer,
342 * rr->input will be pointed at the new buffer */
344 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
345 * rr->length bytes of encrypted compressed stuff. */
347 /* check is not needed I believe */
348 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
)
350 al
=SSL_AD_RECORD_OVERFLOW
;
351 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
355 /* decrypt in place in 'rr->input' */
358 enc_err
= s
->method
->ssl3_enc
->enc(s
,0);
362 /* SSLerr() and ssl3_send_alert() have been called */
365 /* otherwise enc_err == -1 */
366 goto decryption_failed_or_bad_record_mac
;
370 printf("dec %d\n",rr
->length
);
371 { unsigned int z
; for (z
=0; z
<rr
->length
; z
++) printf("%02X%c",rr
->data
[z
],((z
+1)%16)?' ':'\n'); }
375 /* r->length is now the compressed data plus mac */
376 if ( (sess
== NULL
) ||
377 (s
->enc_read_ctx
== NULL
) ||
378 (s
->read_hash
== NULL
))
383 mac_size
=EVP_MD_size(s
->read_hash
);
385 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+mac_size
)
387 #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
388 al
=SSL_AD_RECORD_OVERFLOW
;
389 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_PRE_MAC_LENGTH_TOO_LONG
);
392 goto decryption_failed_or_bad_record_mac
;
395 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
396 if (rr
->length
< mac_size
)
398 #if 0 /* OK only for stream ciphers */
399 al
=SSL_AD_DECODE_ERROR
;
400 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_LENGTH_TOO_SHORT
);
403 goto decryption_failed_or_bad_record_mac
;
406 rr
->length
-=mac_size
;
407 i
=s
->method
->ssl3_enc
->mac(s
,md
,0);
408 if (memcmp(md
,&(rr
->data
[rr
->length
]),mac_size
) != 0)
410 goto decryption_failed_or_bad_record_mac
;
414 /* r->length is now just compressed */
415 if (s
->expand
!= NULL
)
417 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
)
419 al
=SSL_AD_RECORD_OVERFLOW
;
420 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_COMPRESSED_LENGTH_TOO_LONG
);
423 if (!ssl3_do_uncompress(s
))
425 al
=SSL_AD_DECOMPRESSION_FAILURE
;
426 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_BAD_DECOMPRESSION
);
431 if (rr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
)
433 al
=SSL_AD_RECORD_OVERFLOW
;
434 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_DATA_LENGTH_TOO_LONG
);
439 /* So at this point the following is true
440 * ssl->s3->rrec.type is the type of record
441 * ssl->s3->rrec.length == number of bytes in record
442 * ssl->s3->rrec.off == offset to first valid byte
443 * ssl->s3->rrec.data == where to take bytes from, increment
447 /* we have pulled in a full packet so zero things */
449 dtls1_record_bitmap_update(s
, &(s
->d1
->bitmap
));/* Mark receipt of record. */
452 decryption_failed_or_bad_record_mac
:
453 /* Separate 'decryption_failed' alert was introduced with TLS 1.0,
454 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
455 * failure is directly visible from the ciphertext anyway,
456 * we should not reveal which kind of error occured -- this
457 * might become visible to an attacker (e.g. via logfile) */
458 al
=SSL_AD_BAD_RECORD_MAC
;
459 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
461 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
467 /* Call this to get a new input record.
468 * It will return <= 0 if more data is needed, normally due to an error
469 * or non-blocking IO.
470 * When it finishes, one packet has been decoded and can be found in
471 * ssl->s3->rrec.type - is the type of record
472 * ssl->s3->rrec.data, - data
473 * ssl->s3->rrec.length, - number of bytes
475 /* used only by dtls1_read_bytes */
476 int dtls1_get_record(SSL
*s
)
478 int ssl_major
,ssl_minor
,al
;
484 DTLS1_BITMAP
*bitmap
;
485 BN_ULLONG read_sequence
;
486 unsigned int is_next_epoch
;
491 /* The epoch may have changed. If so, process all the
492 * pending records. This is a non-blocking operation. */
493 if ( ! dtls1_process_buffered_records(s
))
496 /* if we're renegotiating, then there may be buffered records */
497 if (dtls1_get_processed_record(s
))
500 /* get something from the wire */
502 /* check if we have the header */
503 if ( (s
->rstate
!= SSL_ST_READ_BODY
) ||
504 (s
->packet_length
< DTLS1_RT_HEADER_LENGTH
))
506 n
=ssl3_read_n(s
, DTLS1_RT_HEADER_LENGTH
, s
->s3
->rbuf
.len
, 0);
507 /* read timeout is handled by dtls1_read_bytes */
508 if (n
<= 0) return(n
); /* error or non-blocking */
510 OPENSSL_assert(s
->packet_length
== DTLS1_RT_HEADER_LENGTH
);
512 s
->rstate
=SSL_ST_READ_BODY
;
516 /* Pull apart the header into the DTLS1_RECORD */
520 version
=(ssl_major
<<8)|ssl_minor
;
522 /* sequence number is 64 bits, with top 2 bytes = epoch */
526 n2l6(p
, read_sequence
);
527 long_long_to_bytes(read_sequence
, s
->s3
->read_sequence
);
530 /* Lets check version */
537 if (version
!= s
->version
)
539 SSLerr(SSL_F_DTLS1_GET_RECORD
,SSL_R_WRONG_VERSION_NUMBER
);
540 /* Send back error using their
541 * version number :-) */
543 al
=SSL_AD_PROTOCOL_VERSION
;
548 if ((version
& 0xff00) != (DTLS1_VERSION
& 0xff00))
550 SSLerr(SSL_F_DTLS1_GET_RECORD
,SSL_R_WRONG_VERSION_NUMBER
);
554 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
)
556 al
=SSL_AD_RECORD_OVERFLOW
;
557 SSLerr(SSL_F_DTLS1_GET_RECORD
,SSL_R_PACKET_LENGTH_TOO_LONG
);
561 /* now s->rstate == SSL_ST_READ_BODY */
564 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
566 if (rr
->length
> s
->packet_length
-DTLS1_RT_HEADER_LENGTH
)
568 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
570 n
=ssl3_read_n(s
,i
,i
,1);
571 if (n
<= 0) return(n
); /* error or non-blocking io */
573 /* this packet contained a partial record, dump it */
576 s
->packet_length
= 0;
580 /* now n == rr->length,
581 * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */
583 s
->rstate
=SSL_ST_READ_HEADER
; /* set state for later operations */
585 /* match epochs. NULL means the packet is dropped on the floor */
586 bitmap
= dtls1_get_bitmap(s
, rr
, &is_next_epoch
);
589 s
->packet_length
= 0; /* dump this record */
590 goto again
; /* get another record */
593 /* check whether this is a repeat, or aged record */
594 if ( ! dtls1_record_replay_check(s
, bitmap
, &(rr
->seq_num
)))
596 s
->packet_length
=0; /* dump this record */
597 goto again
; /* get another record */
600 /* just read a 0 length packet */
601 if (rr
->length
== 0) goto again
;
603 /* If this record is from the next epoch (either HM or ALERT), buffer it
604 * since it cannot be processed at this time.
605 * Records from the next epoch are marked as received even though they are
606 * not processed, so as to prevent any potential resource DoS attack */
609 dtls1_record_bitmap_update(s
, bitmap
);
610 dtls1_buffer_record(s
, &(s
->d1
->unprocessed_rcds
), rr
->seq_num
);
611 s
->packet_length
= 0;
615 if ( ! dtls1_process_record(s
))
618 dtls1_clear_timeouts(s
); /* done waiting */
622 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
627 /* Return up to 'len' payload bytes received in 'type' records.
628 * 'type' is one of the following:
630 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
631 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
632 * - 0 (during a shutdown, no data has to be returned)
634 * If we don't have stored data to work from, read a SSL/TLS record first
635 * (possibly multiple records if we still don't have anything to return).
637 * This function must handle any surprises the peer may have for us, such as
638 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
639 * a surprise, but handled as if it were), or renegotiation requests.
640 * Also if record payloads contain fragments too small to process, we store
641 * them until there is enough for the respective protocol (the record protocol
642 * may use arbitrary fragmentation and even interleaving):
643 * Change cipher spec protocol
644 * just 1 byte needed, no need for keeping anything stored
646 * 2 bytes needed (AlertLevel, AlertDescription)
648 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
649 * to detect unexpected Client Hello and Hello Request messages
650 * here, anything else is handled by higher layers
651 * Application data protocol
652 * none of our business
654 int dtls1_read_bytes(SSL
*s
, int type
, unsigned char *buf
, int len
, int peek
)
659 void (*cb
)(const SSL
*ssl
,int type2
,int val
)=NULL
;
661 if (s
->s3
->rbuf
.buf
== NULL
) /* Not initialized yet */
662 if (!ssl3_setup_buffers(s
))
665 /* XXX: check what the second '&& type' is about */
666 if ((type
&& (type
!= SSL3_RT_APPLICATION_DATA
) &&
667 (type
!= SSL3_RT_HANDSHAKE
) && type
) ||
668 (peek
&& (type
!= SSL3_RT_APPLICATION_DATA
)))
670 SSLerr(SSL_F_DTLS1_READ_BYTES
, ERR_R_INTERNAL_ERROR
);
674 /* check whether there's a handshake message (client hello?) waiting */
675 if ( (ret
= have_handshake_fragment(s
, type
, buf
, len
, peek
)))
678 /* Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
680 if (!s
->in_handshake
&& SSL_in_init(s
))
682 /* type == SSL3_RT_APPLICATION_DATA */
683 i
=s
->handshake_func(s
);
684 if (i
< 0) return(i
);
687 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_SSL_HANDSHAKE_FAILURE
);
693 s
->rwstate
=SSL_NOTHING
;
695 /* s->s3->rrec.type - is the type of record
696 * s->s3->rrec.data, - data
697 * s->s3->rrec.off, - offset into 'data' for next read
698 * s->s3->rrec.length, - number of bytes. */
701 /* get new packet if necessary */
702 if ((rr
->length
== 0) || (s
->rstate
== SSL_ST_READ_BODY
))
704 ret
=dtls1_get_record(s
);
707 ret
= dtls1_read_failed(s
, ret
);
708 /* anything other than a timeout is an error */
716 /* we now have a packet which can be read and processed */
718 if (s
->s3
->change_cipher_spec
/* set when we receive ChangeCipherSpec,
719 * reset by ssl3_get_finished */
720 && (rr
->type
!= SSL3_RT_HANDSHAKE
))
722 al
=SSL_AD_UNEXPECTED_MESSAGE
;
723 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED
);
727 /* If the other end has shut down, throw anything we read away
728 * (even in 'peek' mode) */
729 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
)
732 s
->rwstate
=SSL_NOTHING
;
737 if (type
== rr
->type
) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
739 /* make sure that we are not getting application data when we
740 * are doing a handshake for the first time */
741 if (SSL_in_init(s
) && (type
== SSL3_RT_APPLICATION_DATA
) &&
742 (s
->enc_read_ctx
== NULL
))
744 al
=SSL_AD_UNEXPECTED_MESSAGE
;
745 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_APP_DATA_IN_HANDSHAKE
);
749 if (len
<= 0) return(len
);
751 if ((unsigned int)len
> rr
->length
)
754 n
= (unsigned int)len
;
756 memcpy(buf
,&(rr
->data
[rr
->off
]),n
);
763 s
->rstate
=SSL_ST_READ_HEADER
;
771 /* If we get here, then type != rr->type; if we have a handshake
772 * message, then it was unexpected (Hello Request or Client Hello). */
774 /* In case of record types for which we have 'fragment' storage,
775 * fill that so that we can process the data at a fixed place.
778 unsigned int k
, dest_maxlen
= 0;
779 unsigned char *dest
= NULL
;
780 unsigned int *dest_len
= NULL
;
782 if (rr
->type
== SSL3_RT_HANDSHAKE
)
784 dest_maxlen
= sizeof s
->d1
->handshake_fragment
;
785 dest
= s
->d1
->handshake_fragment
;
786 dest_len
= &s
->d1
->handshake_fragment_len
;
788 else if (rr
->type
== SSL3_RT_ALERT
)
790 dest_maxlen
= sizeof(s
->d1
->alert_fragment
);
791 dest
= s
->d1
->alert_fragment
;
792 dest_len
= &s
->d1
->alert_fragment_len
;
794 else /* else it's a CCS message */
795 OPENSSL_assert(rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
);
800 /* XDTLS: In a pathalogical case, the Client Hello
801 * may be fragmented--don't always expect dest_maxlen bytes */
802 if ( rr
->length
< dest_maxlen
)
804 s
->rstate
=SSL_ST_READ_HEADER
;
809 /* now move 'n' bytes: */
810 for ( k
= 0; k
< dest_maxlen
; k
++)
812 dest
[k
] = rr
->data
[rr
->off
++];
815 *dest_len
= dest_maxlen
;
819 /* s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
820 * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
821 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
823 /* If we are a client, check for an incoming 'Hello Request': */
825 (s
->d1
->handshake_fragment_len
>= DTLS1_HM_HEADER_LENGTH
) &&
826 (s
->d1
->handshake_fragment
[0] == SSL3_MT_HELLO_REQUEST
) &&
827 (s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
))
829 s
->d1
->handshake_fragment_len
= 0;
831 if ((s
->d1
->handshake_fragment
[1] != 0) ||
832 (s
->d1
->handshake_fragment
[2] != 0) ||
833 (s
->d1
->handshake_fragment
[3] != 0))
835 al
=SSL_AD_DECODE_ERROR
;
836 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_BAD_HELLO_REQUEST
);
840 /* no need to check sequence number on HELLO REQUEST messages */
843 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
844 s
->d1
->handshake_fragment
, 4, s
, s
->msg_callback_arg
);
846 if (SSL_is_init_finished(s
) &&
847 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
) &&
851 if (ssl3_renegotiate_check(s
))
853 i
=s
->handshake_func(s
);
854 if (i
< 0) return(i
);
857 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_SSL_HANDSHAKE_FAILURE
);
861 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
))
863 if (s
->s3
->rbuf
.left
== 0) /* no read-ahead left? */
866 /* In the case where we try to read application data,
867 * but we trigger an SSL handshake, we return -1 with
868 * the retry option set. Otherwise renegotiation may
869 * cause nasty problems in the blocking world */
870 s
->rwstate
=SSL_READING
;
872 BIO_clear_retry_flags(bio
);
873 BIO_set_retry_read(bio
);
879 /* we either finished a handshake or ignored the request,
880 * now try again to obtain the (application) data we were asked for */
884 if (s
->d1
->alert_fragment_len
>= DTLS1_AL_HEADER_LENGTH
)
886 int alert_level
= s
->d1
->alert_fragment
[0];
887 int alert_descr
= s
->d1
->alert_fragment
[1];
889 s
->d1
->alert_fragment_len
= 0;
892 s
->msg_callback(0, s
->version
, SSL3_RT_ALERT
,
893 s
->d1
->alert_fragment
, 2, s
, s
->msg_callback_arg
);
895 if (s
->info_callback
!= NULL
)
897 else if (s
->ctx
->info_callback
!= NULL
)
898 cb
=s
->ctx
->info_callback
;
902 j
= (alert_level
<< 8) | alert_descr
;
903 cb(s
, SSL_CB_READ_ALERT
, j
);
906 if (alert_level
== 1) /* warning */
908 s
->s3
->warn_alert
= alert_descr
;
909 if (alert_descr
== SSL_AD_CLOSE_NOTIFY
)
911 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
915 /* XXX: this is a possible improvement in the future */
916 /* now check if it's a missing record */
917 if (alert_descr
== DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
)
920 unsigned int frag_off
;
921 unsigned char *p
= &(s
->d1
->alert_fragment
[2]);
926 dtls1_retransmit_message(s
, seq
, frag_off
, &found
);
927 if ( ! found
&& SSL_in_init(s
))
929 /* fprintf( stderr,"in init = %d\n", SSL_in_init(s)); */
930 /* requested a message not yet sent,
931 send an alert ourselves */
932 ssl3_send_alert(s
,SSL3_AL_WARNING
,
933 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
);
938 else if (alert_level
== 2) /* fatal */
942 s
->rwstate
=SSL_NOTHING
;
943 s
->s3
->fatal_alert
= alert_descr
;
944 SSLerr(SSL_F_DTLS1_READ_BYTES
, SSL_AD_REASON_OFFSET
+ alert_descr
);
945 BIO_snprintf(tmp
,sizeof tmp
,"%d",alert_descr
);
946 ERR_add_error_data(2,"SSL alert number ",tmp
);
947 s
->shutdown
|=SSL_RECEIVED_SHUTDOWN
;
948 SSL_CTX_remove_session(s
->ctx
,s
->session
);
953 al
=SSL_AD_ILLEGAL_PARAMETER
;
954 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_UNKNOWN_ALERT_TYPE
);
961 if (s
->shutdown
& SSL_SENT_SHUTDOWN
) /* but we have not received a shutdown */
963 s
->rwstate
=SSL_NOTHING
;
968 if (rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
)
970 struct ccs_header_st ccs_hdr
;
972 dtls1_get_ccs_header(rr
->data
, &ccs_hdr
);
974 if ( ccs_hdr
.seq
== s
->d1
->handshake_read_seq
)
976 /* 'Change Cipher Spec' is just a single byte, so we know
977 * exactly what the record payload has to look like */
978 /* XDTLS: check that epoch is consistent */
979 if ( (rr
->length
!= DTLS1_CCS_HEADER_LENGTH
) ||
980 (rr
->off
!= 0) || (rr
->data
[0] != SSL3_MT_CCS
))
982 i
=SSL_AD_ILLEGAL_PARAMETER
;
983 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_BAD_CHANGE_CIPHER_SPEC
);
990 s
->msg_callback(0, s
->version
, SSL3_RT_CHANGE_CIPHER_SPEC
,
991 rr
->data
, 1, s
, s
->msg_callback_arg
);
993 s
->s3
->change_cipher_spec
=1;
994 if (!ssl3_do_change_cipher_spec(s
))
997 /* do this whenever CCS is processed */
998 dtls1_reset_seq_numbers(s
, SSL3_CC_READ
);
1000 /* handshake read seq is reset upon handshake completion */
1001 s
->d1
->handshake_read_seq
++;
1012 /* Unexpected handshake message (Client Hello, or protocol violation) */
1013 if ((s
->d1
->handshake_fragment_len
>= DTLS1_HM_HEADER_LENGTH
) &&
1016 struct hm_header_st msg_hdr
;
1018 /* this may just be a stale retransmit */
1019 dtls1_get_message_header(rr
->data
, &msg_hdr
);
1020 if( rr
->epoch
!= s
->d1
->r_epoch
)
1026 if (((s
->state
&SSL_ST_MASK
) == SSL_ST_OK
) &&
1027 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
))
1029 #if 0 /* worked only because C operator preferences are not as expected (and
1030 * because this is not really needed for clients except for detecting
1031 * protocol violations): */
1032 s
->state
=SSL_ST_BEFORE
|(s
->server
)
1036 s
->state
= s
->server
? SSL_ST_ACCEPT
: SSL_ST_CONNECT
;
1040 i
=s
->handshake_func(s
);
1041 if (i
< 0) return(i
);
1044 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_SSL_HANDSHAKE_FAILURE
);
1048 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
))
1050 if (s
->s3
->rbuf
.left
== 0) /* no read-ahead left? */
1053 /* In the case where we try to read application data,
1054 * but we trigger an SSL handshake, we return -1 with
1055 * the retry option set. Otherwise renegotiation may
1056 * cause nasty problems in the blocking world */
1057 s
->rwstate
=SSL_READING
;
1058 bio
=SSL_get_rbio(s
);
1059 BIO_clear_retry_flags(bio
);
1060 BIO_set_retry_read(bio
);
1070 #ifndef OPENSSL_NO_TLS
1071 /* TLS just ignores unknown message types */
1072 if (s
->version
== TLS1_VERSION
)
1078 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1079 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_UNEXPECTED_RECORD
);
1081 case SSL3_RT_CHANGE_CIPHER_SPEC
:
1083 case SSL3_RT_HANDSHAKE
:
1084 /* we already handled all of these, with the possible exception
1085 * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
1086 * should not happen when type != rr->type */
1087 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1088 SSLerr(SSL_F_DTLS1_READ_BYTES
,ERR_R_INTERNAL_ERROR
);
1090 case SSL3_RT_APPLICATION_DATA
:
1091 /* At this point, we were expecting handshake data,
1092 * but have application data. If the library was
1093 * running inside ssl3_read() (i.e. in_read_app_data
1094 * is set) and it makes sense to read application data
1095 * at this point (session renegotiation not yet started),
1096 * we will indulge it.
1098 if (s
->s3
->in_read_app_data
&&
1099 (s
->s3
->total_renegotiations
!= 0) &&
1101 (s
->state
& SSL_ST_CONNECT
) &&
1102 (s
->state
>= SSL3_ST_CW_CLNT_HELLO_A
) &&
1103 (s
->state
<= SSL3_ST_CR_SRVR_HELLO_A
)
1105 (s
->state
& SSL_ST_ACCEPT
) &&
1106 (s
->state
<= SSL3_ST_SW_HELLO_REQ_A
) &&
1107 (s
->state
>= SSL3_ST_SR_CLNT_HELLO_A
)
1111 s
->s3
->in_read_app_data
=2;
1116 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1117 SSLerr(SSL_F_DTLS1_READ_BYTES
,SSL_R_UNEXPECTED_RECORD
);
1124 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1130 dtls1_write_app_data_bytes(SSL
*s
, int type
, const void *buf_
, int len
)
1135 if (SSL_in_init(s
) && !s
->in_handshake
)
1137 i
=s
->handshake_func(s
);
1138 if (i
< 0) return(i
);
1141 SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES
,SSL_R_SSL_HANDSHAKE_FAILURE
);
1151 /* dtls1_write_bytes sends one record at a time, sized according to
1152 * the currently known MTU */
1153 i
= dtls1_write_bytes(s
, type
, buf_
, len
);
1154 if (i
<= 0) return i
;
1156 if ((i
== (int)n
) ||
1157 (type
== SSL3_RT_APPLICATION_DATA
&&
1158 (s
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
)))
1160 /* next chunk of data should get another prepended empty fragment
1161 * in ciphersuites with known-IV weakness: */
1162 s
->s3
->empty_fragment_done
= 0;
1174 /* this only happens when a client hello is received and a handshake
1177 have_handshake_fragment(SSL
*s
, int type
, unsigned char *buf
,
1181 if ((type
== SSL3_RT_HANDSHAKE
) && (s
->d1
->handshake_fragment_len
> 0))
1182 /* (partially) satisfy request from storage */
1184 unsigned char *src
= s
->d1
->handshake_fragment
;
1185 unsigned char *dst
= buf
;
1190 while ((len
> 0) && (s
->d1
->handshake_fragment_len
> 0))
1193 len
--; s
->d1
->handshake_fragment_len
--;
1196 /* move any remaining fragment bytes: */
1197 for (k
= 0; k
< s
->d1
->handshake_fragment_len
; k
++)
1198 s
->d1
->handshake_fragment
[k
] = *src
++;
1208 /* Call this to write data in records of type 'type'
1209 * It will return <= 0 if not all data has been sent or non-blocking IO.
1211 int dtls1_write_bytes(SSL
*s
, int type
, const void *buf_
, int len
)
1213 const unsigned char *buf
=buf_
;
1214 unsigned int tot
,n
,nw
;
1218 s
->rwstate
=SSL_NOTHING
;
1223 /* handshake layer figures out MTU for itself, but data records
1224 * are also sent through this interface, so need to figure out MTU */
1226 mtu
= BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_GET_MTU
, 0, NULL
);
1227 mtu
+= DTLS1_HM_HEADER_LENGTH
; /* HM already inserted */
1231 if (mtu
> SSL3_RT_MAX_PLAIN_LENGTH
)
1232 mtu
= SSL3_RT_MAX_PLAIN_LENGTH
;
1239 i
=do_dtls1_write(s
, type
, &(buf
[tot
]), nw
, 0);
1246 if ( (int)s
->s3
->wnum
+ i
== len
)
1254 int do_dtls1_write(SSL
*s
, int type
, const unsigned char *buf
, unsigned int len
, int create_empty_fragment
)
1256 unsigned char *p
,*pseq
;
1257 int i
,mac_size
,clear
=0;
1264 /* first check if there is a SSL3_BUFFER still being written
1265 * out. This will happen with non blocking IO */
1266 if (s
->s3
->wbuf
.left
!= 0)
1268 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
1269 return(ssl3_write_pending(s
,type
,buf
,len
));
1272 /* If we have an alert to send, lets send it */
1273 if (s
->s3
->alert_dispatch
)
1275 i
=s
->method
->ssl_dispatch_alert(s
);
1278 /* if it went, fall through and send more stuff */
1281 if (len
== 0 && !create_empty_fragment
)
1288 if ( (sess
== NULL
) ||
1289 (s
->enc_write_ctx
== NULL
) ||
1290 (s
->write_hash
== NULL
))
1296 mac_size
=EVP_MD_size(s
->write_hash
);
1298 /* DTLS implements explicit IV, so no need for empty fragments */
1300 /* 'create_empty_fragment' is true only when this function calls itself */
1301 if (!clear
&& !create_empty_fragment
&& !s
->s3
->empty_fragment_done
1302 && SSL_version(s
) != DTLS1_VERSION
)
1304 /* countermeasure against known-IV weakness in CBC ciphersuites
1305 * (see http://www.openssl.org/~bodo/tls-cbc.txt)
1308 if (s
->s3
->need_empty_fragments
&& type
== SSL3_RT_APPLICATION_DATA
)
1310 /* recursive function call with 'create_empty_fragment' set;
1311 * this prepares and buffers the data for an empty fragment
1312 * (these 'prefix_len' bytes are sent out later
1313 * together with the actual payload) */
1314 prefix_len
= s
->method
->do_ssl_write(s
, type
, buf
, 0, 1);
1315 if (prefix_len
<= 0)
1318 if (s
->s3
->wbuf
.len
< (size_t)prefix_len
+ SSL3_RT_MAX_PACKET_SIZE
)
1320 /* insufficient space */
1321 SSLerr(SSL_F_DO_DTLS1_WRITE
, ERR_R_INTERNAL_ERROR
);
1326 s
->s3
->empty_fragment_done
= 1;
1330 p
= wb
->buf
+ prefix_len
;
1332 /* write the header */
1337 *(p
++)=(s
->version
>>8);
1338 *(p
++)=s
->version
&0xff;
1340 /* field where we are to write out packet epoch, seq num and len */
1344 /* lets setup the record stuff. */
1346 /* Make space for the explicit IV in case of CBC.
1347 * (this is a bit of a boundary violation, but what the heck).
1349 if ( s
->enc_write_ctx
&&
1350 (EVP_CIPHER_mode( s
->enc_write_ctx
->cipher
) & EVP_CIPH_CBC_MODE
))
1351 bs
= EVP_CIPHER_block_size(s
->enc_write_ctx
->cipher
);
1355 wr
->data
=p
+ bs
; /* make room for IV in case of CBC */
1356 wr
->length
=(int)len
;
1357 wr
->input
=(unsigned char *)buf
;
1359 /* we now 'read' from wr->input, wr->length bytes into
1362 /* first we compress */
1363 if (s
->compress
!= NULL
)
1365 if (!ssl3_do_compress(s
))
1367 SSLerr(SSL_F_DO_DTLS1_WRITE
,SSL_R_COMPRESSION_FAILURE
);
1373 memcpy(wr
->data
,wr
->input
,wr
->length
);
1377 /* we should still have the output to wr->data and the input
1378 * from wr->input. Length should be wr->length.
1379 * wr->data still points in the wb->buf */
1383 s
->method
->ssl3_enc
->mac(s
,&(p
[wr
->length
+ bs
]),1);
1384 wr
->length
+=mac_size
;
1387 /* this is true regardless of mac size */
1392 /* ssl3_enc can only have an error on read */
1393 wr
->length
+= bs
; /* bs != 0 in case of CBC. The enc fn provides
1395 s
->method
->ssl3_enc
->enc(s
,1);
1397 /* record length after mac and block padding */
1398 /* if (type == SSL3_RT_APPLICATION_DATA ||
1399 (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */
1401 /* there's only one epoch between handshake and app data */
1403 s2n(s
->d1
->w_epoch
, pseq
);
1407 s2n(s->d1->handshake_epoch, pseq); */
1409 l2n6(bytes_to_long_long(s
->s3
->write_sequence
), pseq
);
1410 s2n(wr
->length
,pseq
);
1412 /* we should now have
1413 * wr->data pointing to the encrypted data, which is
1414 * wr->length long */
1415 wr
->type
=type
; /* not needed but helps for debugging */
1416 wr
->length
+=DTLS1_RT_HEADER_LENGTH
;
1418 #if 0 /* this is now done at the message layer */
1419 /* buffer the record, making it easy to handle retransmits */
1420 if ( type
== SSL3_RT_HANDSHAKE
|| type
== SSL3_RT_CHANGE_CIPHER_SPEC
)
1421 dtls1_buffer_record(s
, wr
->data
, wr
->length
,
1422 *((BN_ULLONG
*)&(s
->s3
->write_sequence
[0])));
1425 ssl3_record_sequence_update(&(s
->s3
->write_sequence
[0]));
1427 if (create_empty_fragment
)
1429 /* we are in a recursive call;
1430 * just return the length, don't write out anything here
1435 /* now let's set up wb */
1436 wb
->left
= prefix_len
+ wr
->length
;
1439 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
1440 s
->s3
->wpend_tot
=len
;
1441 s
->s3
->wpend_buf
=buf
;
1442 s
->s3
->wpend_type
=type
;
1443 s
->s3
->wpend_ret
=len
;
1445 /* we now just need to write the buffer */
1446 return ssl3_write_pending(s
,type
,buf
,len
);
1453 static int dtls1_record_replay_check(SSL
*s
, DTLS1_BITMAP
*bitmap
,
1456 BN_ULLONG mask
= 0x0000000000000001L
;
1459 rcd_num
= bytes_to_long_long(s
->s3
->read_sequence
);
1461 if (rcd_num
>= bitmap
->max_seq_num
)
1464 return 1; /* this record is new */
1467 if (bitmap
->max_seq_num
- rcd_num
> bitmap
->length
)
1468 return 0; /* stale, outside the window */
1470 mask
<<= (bitmap
->max_seq_num
- rcd_num
- 1);
1471 if (bitmap
->map
& mask
)
1472 return 0; /* record previously received */
1479 static void dtls1_record_bitmap_update(SSL
*s
, DTLS1_BITMAP
*bitmap
)
1482 BN_ULLONG mask
= 0x0000000000000001L
;
1485 rcd_num
= bytes_to_long_long(s
->s3
->read_sequence
);
1487 if (rcd_num
>= bitmap
->max_seq_num
)
1489 shift
= (unsigned int)(rcd_num
- bitmap
->max_seq_num
) + 1;
1490 bitmap
->max_seq_num
= rcd_num
+ 1;
1491 bitmap
->map
<<= shift
;
1492 bitmap
->map
|= 0x0000000000000001L
;
1496 mask
<<= (bitmap
->max_seq_num
- rcd_num
- 1);
1497 bitmap
->map
|= mask
;
1502 int dtls1_dispatch_alert(SSL
*s
)
1505 void (*cb
)(const SSL
*ssl
,int type
,int val
)=NULL
;
1506 unsigned char buf
[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */
1507 unsigned char *ptr
= &buf
[0];
1509 s
->s3
->alert_dispatch
=0;
1511 memset(buf
, 0x00, sizeof(buf
));
1512 *ptr
++ = s
->s3
->send_alert
[0];
1513 *ptr
++ = s
->s3
->send_alert
[1];
1515 if (s
->s3
->send_alert
[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
)
1517 s2n(s
->d1
->handshake_read_seq
, ptr
);
1519 if ( s
->d1
->r_msg_hdr
.frag_off
== 0) /* waiting for a new msg */
1522 s2n(s
->d1
->r_msg_hdr
.seq
, ptr
); /* partial msg read */
1526 fprintf(stderr
, "s->d1->handshake_read_seq = %d, s->d1->r_msg_hdr.seq = %d\n",s
->d1
->handshake_read_seq
,s
->d1
->r_msg_hdr
.seq
);
1528 l2n3(s
->d1
->r_msg_hdr
.frag_off
, ptr
);
1531 i
= do_dtls1_write(s
, SSL3_RT_ALERT
, &buf
[0], sizeof(buf
), 0);
1534 s
->s3
->alert_dispatch
=1;
1535 /* fprintf( stderr, "not done with alert\n" ); */
1539 if ( s
->s3
->send_alert
[0] == SSL3_AL_FATAL
||
1540 s
->s3
->send_alert
[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
)
1541 (void)BIO_flush(s
->wbio
);
1543 if (s
->msg_callback
)
1544 s
->msg_callback(1, s
->version
, SSL3_RT_ALERT
, s
->s3
->send_alert
,
1545 2, s
, s
->msg_callback_arg
);
1547 if (s
->info_callback
!= NULL
)
1548 cb
=s
->info_callback
;
1549 else if (s
->ctx
->info_callback
!= NULL
)
1550 cb
=s
->ctx
->info_callback
;
1554 j
=(s
->s3
->send_alert
[0]<<8)|s
->s3
->send_alert
[1];
1555 cb(s
,SSL_CB_WRITE_ALERT
,j
);
1562 static DTLS1_BITMAP
*
1563 dtls1_get_bitmap(SSL
*s
, SSL3_RECORD
*rr
, unsigned int *is_next_epoch
)
1568 /* In current epoch, accept HM, CCS, DATA, & ALERT */
1569 if (rr
->epoch
== s
->d1
->r_epoch
)
1570 return &s
->d1
->bitmap
;
1572 /* Only HM and ALERT messages can be from the next epoch */
1573 else if (rr
->epoch
== (unsigned long)(s
->d1
->r_epoch
+ 1) &&
1574 (rr
->type
== SSL3_RT_HANDSHAKE
||
1575 rr
->type
== SSL3_RT_ALERT
))
1578 return &s
->d1
->next_bitmap
;
1586 dtls1_record_needs_buffering(SSL
*s
, SSL3_RECORD
*rr
, unsigned short *priority
,
1587 unsigned long *offset
)
1590 /* alerts are passed up immediately */
1591 if ( rr
->type
== SSL3_RT_APPLICATION_DATA
||
1592 rr
->type
== SSL3_RT_ALERT
)
1595 /* Only need to buffer if a handshake is underway.
1596 * (this implies that Hello Request and Client Hello are passed up
1598 if ( SSL_in_init(s
))
1600 unsigned char *data
= rr
->data
;
1601 /* need to extract the HM/CCS sequence number here */
1602 if ( rr
->type
== SSL3_RT_HANDSHAKE
||
1603 rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
)
1605 unsigned short seq_num
;
1606 struct hm_header_st msg_hdr
;
1607 struct ccs_header_st ccs_hdr
;
1609 if ( rr
->type
== SSL3_RT_HANDSHAKE
)
1611 dtls1_get_message_header(data
, &msg_hdr
);
1612 seq_num
= msg_hdr
.seq
;
1613 *offset
= msg_hdr
.frag_off
;
1617 dtls1_get_ccs_header(data
, &ccs_hdr
);
1618 seq_num
= ccs_hdr
.seq
;
1622 /* this is either a record we're waiting for, or a
1623 * retransmit of something we happened to previously
1624 * receive (higher layers will drop the repeat silently */
1625 if ( seq_num
< s
->d1
->handshake_read_seq
)
1627 if (rr
->type
== SSL3_RT_HANDSHAKE
&&
1628 seq_num
== s
->d1
->handshake_read_seq
&&
1629 msg_hdr
.frag_off
< s
->d1
->r_msg_hdr
.frag_off
)
1631 else if ( seq_num
== s
->d1
->handshake_read_seq
&&
1632 (rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
||
1633 msg_hdr
.frag_off
== s
->d1
->r_msg_hdr
.frag_off
))
1637 *priority
= seq_num
;
1641 else /* unknown record type */
1650 dtls1_reset_seq_numbers(SSL
*s
, int rw
)
1653 unsigned int seq_bytes
= sizeof(s
->s3
->read_sequence
);
1655 if ( rw
& SSL3_CC_READ
)
1657 seq
= s
->s3
->read_sequence
;
1659 memcpy(&(s
->d1
->bitmap
), &(s
->d1
->next_bitmap
), sizeof(DTLS1_BITMAP
));
1660 memset(&(s
->d1
->next_bitmap
), 0x00, sizeof(DTLS1_BITMAP
));
1664 seq
= s
->s3
->write_sequence
;
1668 memset(seq
, 0x00, seq_bytes
);
1673 bytes_to_long_long(unsigned char *bytes
)
1677 num
= (((BN_ULLONG
)bytes
[0]) << 56) |
1678 (((BN_ULLONG
)bytes
[1]) << 48) |
1679 (((BN_ULLONG
)bytes
[2]) << 40) |
1680 (((BN_ULLONG
)bytes
[3]) << 32) |
1681 (((BN_ULLONG
)bytes
[4]) << 24) |
1682 (((BN_ULLONG
)bytes
[5]) << 16) |
1683 (((BN_ULLONG
)bytes
[6]) << 8) |
1684 (((BN_ULLONG
)bytes
[7]) );
1690 long_long_to_bytes(BN_ULLONG num
, unsigned char *bytes
)
1692 bytes
[0] = (unsigned char)((num
>> 56)&0xff);
1693 bytes
[1] = (unsigned char)((num
>> 48)&0xff);
1694 bytes
[2] = (unsigned char)((num
>> 40)&0xff);
1695 bytes
[3] = (unsigned char)((num
>> 32)&0xff);
1696 bytes
[4] = (unsigned char)((num
>> 24)&0xff);
1697 bytes
[5] = (unsigned char)((num
>> 16)&0xff);
1698 bytes
[6] = (unsigned char)((num
>> 8)&0xff);
1699 bytes
[7] = (unsigned char)((num
)&0xff);
1703 dtls1_clear_timeouts(SSL
*s
)
1705 memset(&(s
->d1
->timeout
), 0x00, sizeof(struct dtls1_timeout_st
));