1 /* $OpenBSD: dtls_locl.h,v 1.7 2021/09/04 14:24:28 jsing Exp $ */
2 /*
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 */
6 /* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
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
19 * distribution.
20 *
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/)"
25 *
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.
30 *
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.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
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 * ====================================================================
53 *
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).
57 *
58 */
59
60 #ifndef HEADER_DTLS_LOCL_H
61 #define HEADER_DTLS_LOCL_H
62
63 #include <sys/time.h>
64
65 #include <openssl/dtls1.h>
66
67 #include "ssl_locl.h"
68
69 __BEGIN_HIDDEN_DECLS
70
71 typedef struct dtls1_bitmap_st {
72 unsigned long map; /* track 32 packets on 32-bit systems
73 and 64 - on 64-bit systems */
74 unsigned char max_seq_num[8]; /* max record number seen so far,
75 64-bit value in big-endian
76 encoding */
77 } DTLS1_BITMAP;
78
79 struct dtls1_retransmit_state {
80 EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */
81 EVP_MD_CTX *write_hash; /* used for mac generation */
82 SSL_SESSION *session;
83 unsigned short epoch;
84 };
85
86 struct hm_header_st {
87 unsigned char type;
88 unsigned long msg_len;
89 unsigned short seq;
90 unsigned long frag_off;
91 unsigned long frag_len;
92 unsigned int is_ccs;
93 struct dtls1_retransmit_state saved_retransmit_state;
94 };
95
96 struct dtls1_timeout_st {
97 /* Number of read timeouts so far */
98 unsigned int read_timeouts;
99
100 /* Number of write timeouts so far */
101 unsigned int write_timeouts;
102
103 /* Number of alerts received so far */
104 unsigned int num_alerts;
105 };
106
107 struct _pqueue;
108
109 typedef struct record_pqueue_st {
110 unsigned short epoch;
111 struct _pqueue *q;
112 } record_pqueue;
113
114 typedef struct hm_fragment_st {
115 struct hm_header_st msg_header;
116 unsigned char *fragment;
117 unsigned char *reassembly;
118 } hm_fragment;
119
120 typedef struct dtls1_record_data_internal_st {
121 unsigned char *packet;
122 unsigned int packet_length;
123 SSL3_BUFFER_INTERNAL rbuf;
124 SSL3_RECORD_INTERNAL rrec;
125 } DTLS1_RECORD_DATA_INTERNAL;
126
127 struct dtls1_state_internal_st;
128
129 typedef struct dtls1_state_internal_st {
130 unsigned int send_cookie;
131 unsigned char cookie[DTLS1_COOKIE_LENGTH];
132 unsigned char rcvd_cookie[DTLS1_COOKIE_LENGTH];
133 unsigned int cookie_len;
134
135 /* records being received in the current epoch */
136 DTLS1_BITMAP bitmap;
137
138 /* renegotiation starts a new set of sequence numbers */
139 DTLS1_BITMAP next_bitmap;
140
141 /* handshake message numbers */
142 unsigned short handshake_write_seq;
143 unsigned short next_handshake_write_seq;
144
145 unsigned short handshake_read_seq;
146
147 /* Received handshake records (unprocessed) */
148 record_pqueue unprocessed_rcds;
149
150 /* Buffered handshake messages */
srtp_find_profile_by_name(const char * profile_name,const SRTP_PROTECTION_PROFILE ** pptr,unsigned int len)151 struct _pqueue *buffered_messages;
152
153 /* Buffered application records.
154 * Only for records between CCS and Finished
155 * to prevent either protocol violation or
156 * unnecessary message loss.
157 */
158 record_pqueue buffered_app_data;
159
160 /* Is set when listening for new connections with dtls1_listen() */
161 unsigned int listen;
162
163 unsigned int mtu; /* max DTLS packet size */
164
165 struct hm_header_st w_msg_hdr;
166 struct hm_header_st r_msg_hdr;
167
168 struct dtls1_timeout_st timeout;
169
170 unsigned int retransmitting;
srtp_find_profile_by_num(unsigned int profile_num,const SRTP_PROTECTION_PROFILE ** pptr)171 unsigned int change_cipher_spec_ok;
172 } DTLS1_STATE_INTERNAL;
173 #define D1I(s) (s->d1->internal)
174
175 typedef struct dtls1_state_st {
176 /* Buffered (sent) handshake records */
177 struct _pqueue *sent_messages;
178
179 /* Indicates when the last handshake msg or heartbeat sent will timeout */
180 struct timeval next_timeout;
181
182 /* Timeout duration */
183 unsigned short timeout_duration;
184
185 struct dtls1_state_internal_st *internal;
186 } DTLS1_STATE;
187
188 int dtls1_do_write(SSL *s, int type);
ssl_ctx_make_profiles(const char * profiles_string,STACK_OF (SRTP_PROTECTION_PROFILE)** out)189 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
190 void dtls1_set_message_header(SSL *s, unsigned char mt, unsigned long len,
191 unsigned long frag_off, unsigned long frag_len);
192 void dtls1_set_message_header_int(SSL *s, unsigned char mt,
193 unsigned long len, unsigned short seq_num, unsigned long frag_off,
194 unsigned long frag_len);
195
196 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
197 unsigned int len);
198
199 int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf, int len);
200 int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
201
202 int dtls1_read_failed(SSL *s, int code);
203 int dtls1_buffer_message(SSL *s, int ccs);
204 int dtls1_retransmit_message(SSL *s, unsigned short seq,
205 unsigned long frag_off, int *found);
206 int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
207 int dtls1_retransmit_buffered_messages(SSL *s);
208 void dtls1_clear_record_buffer(SSL *s);
209 int dtls1_get_message_header(CBS *header, struct hm_header_st *msg_hdr);
210 void dtls1_reset_read_seq_numbers(SSL *s);
211 struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft);
212 int dtls1_check_timeout_num(SSL *s);
213 int dtls1_handle_timeout(SSL *s);
214 const SSL_CIPHER *dtls1_get_cipher(unsigned int u);
215 void dtls1_start_timer(SSL *s);
216 void dtls1_stop_timer(SSL *s);
217 int dtls1_is_timer_expired(SSL *s);
218 void dtls1_double_timeout(SSL *s);
219 unsigned int dtls1_min_mtu(void);
220
221 int dtls1_new(SSL *s);
222 void dtls1_free(SSL *s);
223 void dtls1_clear(SSL *s);
224 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
SSL_CTX_set_tlsext_use_srtp(SSL_CTX * ctx,const char * profiles)225
226 long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok);
227 int dtls1_get_record(SSL *s);
228
229 __END_HIDDEN_DECLS
230
SSL_set_tlsext_use_srtp(SSL * s,const char * profiles)231 #endif
232