1160814Ssimon/* ssl/dtls1.h */
2296341Sdelphij/*
3160814Ssimon * DTLS implementation written by Nagendra Modadugu
4296341Sdelphij * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    openssl-core@OpenSSL.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon
60246772Sjkim#ifndef HEADER_DTLS1_H
61296341Sdelphij# define HEADER_DTLS1_H
62160814Ssimon
63296341Sdelphij# include <openssl/buffer.h>
64296341Sdelphij# include <openssl/pqueue.h>
65296341Sdelphij# ifdef OPENSSL_SYS_VMS
66296341Sdelphij#  include <resource.h>
67296341Sdelphij#  include <sys/timeb.h>
68296341Sdelphij# endif
69296341Sdelphij# ifdef OPENSSL_SYS_WIN32
70205128Ssimon/* Needed for struct timeval */
71296341Sdelphij#  include <winsock.h>
72296341Sdelphij# elif defined(OPENSSL_SYS_NETWARE) && !defined(_WINSOCK2API_)
73296341Sdelphij#  include <sys/timeval.h>
74296341Sdelphij# else
75296341Sdelphij#  if defined(OPENSSL_SYS_VXWORKS)
76296341Sdelphij#   include <sys/times.h>
77296341Sdelphij#  else
78296341Sdelphij#   include <sys/time.h>
79296341Sdelphij#  endif
80296341Sdelphij# endif
81160814Ssimon
82160814Ssimon#ifdef  __cplusplus
83160814Ssimonextern "C" {
84160814Ssimon#endif
85160814Ssimon
86296341Sdelphij# define DTLS1_VERSION                   0xFEFF
87296341Sdelphij# define DTLS_MAX_VERSION                DTLS1_VERSION
88296341Sdelphij# define DTLS1_VERSION_MAJOR             0xFE
89273399Sdelphij
90296341Sdelphij# define DTLS1_BAD_VER                   0x0100
91160814Ssimon
92296341Sdelphij# if 0
93194206Ssimon/* this alert description is not specified anywhere... */
94296341Sdelphij#  define DTLS1_AD_MISSING_HANDSHAKE_MESSAGE    110
95296341Sdelphij# endif
96160814Ssimon
97160814Ssimon/* lengths of messages */
98296341Sdelphij# define DTLS1_COOKIE_LENGTH                     256
99160814Ssimon
100296341Sdelphij# define DTLS1_RT_HEADER_LENGTH                  13
101160814Ssimon
102296341Sdelphij# define DTLS1_HM_HEADER_LENGTH                  12
103160814Ssimon
104296341Sdelphij# define DTLS1_HM_BAD_FRAGMENT                   -2
105296341Sdelphij# define DTLS1_HM_FRAGMENT_RETRY                 -3
106160814Ssimon
107296341Sdelphij# define DTLS1_CCS_HEADER_LENGTH                  1
108160814Ssimon
109296341Sdelphij# ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
110296341Sdelphij#  define DTLS1_AL_HEADER_LENGTH                   7
111296341Sdelphij# else
112296341Sdelphij#  define DTLS1_AL_HEADER_LENGTH                   2
113296341Sdelphij# endif
114160814Ssimon
115296341Sdelphij# ifndef OPENSSL_NO_SSL_INTERN
116160814Ssimon
117296341Sdelphij#  ifndef OPENSSL_NO_SCTP
118296341Sdelphij#   define DTLS1_SCTP_AUTH_LABEL   "EXPORTER_DTLS_OVER_SCTP"
119296341Sdelphij#  endif
120238405Sjkim
121279264Sdelphij/* Max MTU overhead we know about so far is 40 for IPv6 + 8 for UDP */
122296341Sdelphij#  define DTLS1_MAX_MTU_OVERHEAD                   48
123279264Sdelphij
124296341Sdelphijtypedef struct dtls1_bitmap_st {
125296341Sdelphij    unsigned long map;          /* track 32 packets on 32-bit systems and 64
126296341Sdelphij                                 * - on 64-bit systems */
127296341Sdelphij    unsigned char max_seq_num[8]; /* max record number seen so far, 64-bit
128296341Sdelphij                                   * value in big-endian encoding */
129296341Sdelphij} DTLS1_BITMAP;
130160814Ssimon
131296341Sdelphijstruct dtls1_retransmit_state {
132296341Sdelphij    EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */
133296341Sdelphij    EVP_MD_CTX *write_hash;     /* used for mac generation */
134296341Sdelphij#  ifndef OPENSSL_NO_COMP
135296341Sdelphij    COMP_CTX *compress;         /* compression */
136296341Sdelphij#  else
137296341Sdelphij    char *compress;
138296341Sdelphij#  endif
139296341Sdelphij    SSL_SESSION *session;
140296341Sdelphij    unsigned short epoch;
141296341Sdelphij};
142205128Ssimon
143296341Sdelphijstruct hm_header_st {
144296341Sdelphij    unsigned char type;
145296341Sdelphij    unsigned long msg_len;
146296341Sdelphij    unsigned short seq;
147296341Sdelphij    unsigned long frag_off;
148296341Sdelphij    unsigned long frag_len;
149296341Sdelphij    unsigned int is_ccs;
150296341Sdelphij    struct dtls1_retransmit_state saved_retransmit_state;
151296341Sdelphij};
152160814Ssimon
153296341Sdelphijstruct ccs_header_st {
154296341Sdelphij    unsigned char type;
155296341Sdelphij    unsigned short seq;
156296341Sdelphij};
157160814Ssimon
158296341Sdelphijstruct dtls1_timeout_st {
159296341Sdelphij    /* Number of read timeouts so far */
160296341Sdelphij    unsigned int read_timeouts;
161296341Sdelphij    /* Number of write timeouts so far */
162296341Sdelphij    unsigned int write_timeouts;
163296341Sdelphij    /* Number of alerts received so far */
164296341Sdelphij    unsigned int num_alerts;
165296341Sdelphij};
166160814Ssimon
167296341Sdelphijtypedef struct record_pqueue_st {
168296341Sdelphij    unsigned short epoch;
169296341Sdelphij    pqueue q;
170296341Sdelphij} record_pqueue;
171160814Ssimon
172296341Sdelphijtypedef struct hm_fragment_st {
173296341Sdelphij    struct hm_header_st msg_header;
174296341Sdelphij    unsigned char *fragment;
175296341Sdelphij    unsigned char *reassembly;
176296341Sdelphij} hm_fragment;
177160814Ssimon
178296341Sdelphijtypedef struct dtls1_state_st {
179296341Sdelphij    unsigned int send_cookie;
180296341Sdelphij    unsigned char cookie[DTLS1_COOKIE_LENGTH];
181296341Sdelphij    unsigned char rcvd_cookie[DTLS1_COOKIE_LENGTH];
182296341Sdelphij    unsigned int cookie_len;
183296341Sdelphij    /*
184296341Sdelphij     * The current data and handshake epoch.  This is initially
185296341Sdelphij     * undefined, and starts at zero once the initial handshake is
186296341Sdelphij     * completed
187296341Sdelphij     */
188296341Sdelphij    unsigned short r_epoch;
189296341Sdelphij    unsigned short w_epoch;
190296341Sdelphij    /* records being received in the current epoch */
191296341Sdelphij    DTLS1_BITMAP bitmap;
192296341Sdelphij    /* renegotiation starts a new set of sequence numbers */
193296341Sdelphij    DTLS1_BITMAP next_bitmap;
194296341Sdelphij    /* handshake message numbers */
195296341Sdelphij    unsigned short handshake_write_seq;
196296341Sdelphij    unsigned short next_handshake_write_seq;
197296341Sdelphij    unsigned short handshake_read_seq;
198296341Sdelphij    /* save last sequence number for retransmissions */
199296341Sdelphij    unsigned char last_write_sequence[8];
200296341Sdelphij    /* Received handshake records (processed and unprocessed) */
201296341Sdelphij    record_pqueue unprocessed_rcds;
202296341Sdelphij    record_pqueue processed_rcds;
203296341Sdelphij    /* Buffered handshake messages */
204296341Sdelphij    pqueue buffered_messages;
205296341Sdelphij    /* Buffered (sent) handshake records */
206296341Sdelphij    pqueue sent_messages;
207296341Sdelphij    /*
208296341Sdelphij     * Buffered application records. Only for records between CCS and
209296341Sdelphij     * Finished to prevent either protocol violation or unnecessary message
210296341Sdelphij     * loss.
211296341Sdelphij     */
212296341Sdelphij    record_pqueue buffered_app_data;
213296341Sdelphij    /* Is set when listening for new connections with dtls1_listen() */
214296341Sdelphij    unsigned int listen;
215296341Sdelphij    unsigned int link_mtu;      /* max on-the-wire DTLS packet size */
216296341Sdelphij    unsigned int mtu;           /* max DTLS packet size */
217296341Sdelphij    struct hm_header_st w_msg_hdr;
218296341Sdelphij    struct hm_header_st r_msg_hdr;
219296341Sdelphij    struct dtls1_timeout_st timeout;
220296341Sdelphij    /*
221296341Sdelphij     * Indicates when the last handshake msg or heartbeat sent will timeout
222296341Sdelphij     */
223296341Sdelphij    struct timeval next_timeout;
224296341Sdelphij    /* Timeout duration */
225296341Sdelphij    unsigned short timeout_duration;
226296341Sdelphij    /*
227296341Sdelphij     * storage for Alert/Handshake protocol data received but not yet
228296341Sdelphij     * processed by ssl3_read_bytes:
229296341Sdelphij     */
230296341Sdelphij    unsigned char alert_fragment[DTLS1_AL_HEADER_LENGTH];
231296341Sdelphij    unsigned int alert_fragment_len;
232296341Sdelphij    unsigned char handshake_fragment[DTLS1_HM_HEADER_LENGTH];
233296341Sdelphij    unsigned int handshake_fragment_len;
234296341Sdelphij    unsigned int retransmitting;
235296341Sdelphij    /*
236296341Sdelphij     * Set when the handshake is ready to process peer's ChangeCipherSpec message.
237296341Sdelphij     * Cleared after the message has been processed.
238296341Sdelphij     */
239296341Sdelphij    unsigned int change_cipher_spec_ok;
240296341Sdelphij#  ifndef OPENSSL_NO_SCTP
241296341Sdelphij    /* used when SSL_ST_XX_FLUSH is entered */
242296341Sdelphij    int next_state;
243296341Sdelphij    int shutdown_received;
244296341Sdelphij#  endif
245296341Sdelphij} DTLS1_STATE;
246160814Ssimon
247296341Sdelphijtypedef struct dtls1_record_data_st {
248296341Sdelphij    unsigned char *packet;
249296341Sdelphij    unsigned int packet_length;
250296341Sdelphij    SSL3_BUFFER rbuf;
251296341Sdelphij    SSL3_RECORD rrec;
252296341Sdelphij#  ifndef OPENSSL_NO_SCTP
253296341Sdelphij    struct bio_dgram_sctp_rcvinfo recordinfo;
254296341Sdelphij#  endif
255296341Sdelphij} DTLS1_RECORD_DATA;
256160814Ssimon
257296341Sdelphij# endif
258160814Ssimon
259160814Ssimon/* Timeout multipliers (timeout slice is defined in apps/timeouts.h */
260296341Sdelphij# define DTLS1_TMO_READ_COUNT                      2
261296341Sdelphij# define DTLS1_TMO_WRITE_COUNT                     2
262160814Ssimon
263296341Sdelphij# define DTLS1_TMO_ALERT_COUNT                     12
264160814Ssimon
265160814Ssimon#ifdef  __cplusplus
266160814Ssimon}
267160814Ssimon#endif
268160814Ssimon#endif
269