xref: /openbsd/lib/libssl/dtls_local.h (revision a9199000)
1 /* $OpenBSD: dtls_local.h,v 1.2 2022/11/26 17:23:18 tb 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_local.h"
68 #include "tls_content.h"
69 
70 __BEGIN_HIDDEN_DECLS
71 
72 typedef struct dtls1_bitmap_st {
73 	unsigned long map;		/* track 32 packets on 32-bit systems
74 					   and 64 - on 64-bit systems */
75 	unsigned char max_seq_num[8];	/* max record number seen so far,
76 					   64-bit value in big-endian
77 					   encoding */
78 } DTLS1_BITMAP;
79 
80 struct dtls1_retransmit_state {
81 	SSL_SESSION *session;
82 	unsigned short epoch;
83 };
84 
85 struct hm_header_st {
86 	unsigned char type;
87 	unsigned long msg_len;
88 	unsigned short seq;
89 	unsigned long frag_off;
90 	unsigned long frag_len;
91 	unsigned int is_ccs;
92 	struct dtls1_retransmit_state saved_retransmit_state;
93 };
94 
95 struct dtls1_timeout_st {
96 	/* Number of read timeouts so far */
97 	unsigned int read_timeouts;
98 
99 	/* Number of write timeouts so far */
100 	unsigned int write_timeouts;
101 
102 	/* Number of alerts received so far */
103 	unsigned int num_alerts;
104 };
105 
106 struct _pqueue;
107 
108 typedef struct record_pqueue_st {
109 	unsigned short epoch;
110 	struct _pqueue *q;
111 } record_pqueue;
112 
113 typedef struct rcontent_pqueue_st {
114 	unsigned short epoch;
115 	struct _pqueue *q;
116 } rcontent_pqueue;
117 
118 typedef struct hm_fragment_st {
119 	struct hm_header_st msg_header;
120 	unsigned char *fragment;
121 	unsigned char *reassembly;
122 } hm_fragment;
123 
124 typedef struct dtls1_record_data_internal_st {
125 	unsigned char *packet;
126 	unsigned int packet_length;
127 	SSL3_BUFFER_INTERNAL rbuf;
128 	SSL3_RECORD_INTERNAL rrec;
129 } DTLS1_RECORD_DATA_INTERNAL;
130 
131 typedef struct dtls1_rcontent_data_internal_st {
132 	struct tls_content *rcontent;
133 } DTLS1_RCONTENT_DATA_INTERNAL;
134 
135 struct dtls1_state_st {
136 	/* Buffered (sent) handshake records */
137 	struct _pqueue *sent_messages;
138 
139 	/* Indicates when the last handshake msg or heartbeat sent will timeout */
140 	struct timeval next_timeout;
141 
142 	/* Timeout duration */
143 	unsigned short timeout_duration;
144 
145 	unsigned int send_cookie;
146 	unsigned char cookie[DTLS1_COOKIE_LENGTH];
147 	unsigned char rcvd_cookie[DTLS1_COOKIE_LENGTH];
148 	unsigned int cookie_len;
149 
150 	/* records being received in the current epoch */
151 	DTLS1_BITMAP bitmap;
152 
153 	/* renegotiation starts a new set of sequence numbers */
154 	DTLS1_BITMAP next_bitmap;
155 
156 	/* handshake message numbers */
157 	unsigned short handshake_write_seq;
158 	unsigned short next_handshake_write_seq;
159 
160 	unsigned short handshake_read_seq;
161 
162 	/* Received handshake records (unprocessed) */
163 	record_pqueue unprocessed_rcds;
164 
165 	/* Buffered handshake messages */
166 	struct _pqueue *buffered_messages;
167 
168 	/* Buffered application records.
169 	 * Only for records between CCS and Finished
170 	 * to prevent either protocol violation or
171 	 * unnecessary message loss.
172 	 */
173 	rcontent_pqueue buffered_app_data;
174 
175 	/* Is set when listening for new connections with dtls1_listen() */
176 	unsigned int listen;
177 
178 	unsigned int mtu; /* max DTLS packet size */
179 
180 	struct hm_header_st w_msg_hdr;
181 	struct hm_header_st r_msg_hdr;
182 
183 	struct dtls1_timeout_st timeout;
184 
185 	unsigned int retransmitting;
186 	unsigned int change_cipher_spec_ok;
187 };
188 
189 int dtls1_do_write(SSL *s, int type);
190 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
191 void dtls1_set_message_header(SSL *s, unsigned char mt, unsigned long len,
192     unsigned long frag_off, unsigned long frag_len);
193 void dtls1_set_message_header_int(SSL *s, unsigned char mt,
194     unsigned long len, unsigned short seq_num, unsigned long frag_off,
195     unsigned long frag_len);
196 
197 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
198     unsigned int len);
199 
200 int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf, int len);
201 int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
202 
203 int dtls1_read_failed(SSL *s, int code);
204 int dtls1_buffer_message(SSL *s, int ccs);
205 int dtls1_retransmit_message(SSL *s, unsigned short seq,
206     unsigned long frag_off, int *found);
207 int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
208 int dtls1_retransmit_buffered_messages(SSL *s);
209 void dtls1_clear_record_buffer(SSL *s);
210 int dtls1_get_message_header(CBS *header, struct hm_header_st *msg_hdr);
211 void dtls1_reset_read_seq_numbers(SSL *s);
212 struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft);
213 int dtls1_check_timeout_num(SSL *s);
214 int dtls1_handle_timeout(SSL *s);
215 const SSL_CIPHER *dtls1_get_cipher(unsigned int u);
216 void dtls1_start_timer(SSL *s);
217 void dtls1_stop_timer(SSL *s);
218 int dtls1_is_timer_expired(SSL *s);
219 void dtls1_double_timeout(SSL *s);
220 unsigned int dtls1_min_mtu(void);
221 
222 int dtls1_new(SSL *s);
223 void dtls1_free(SSL *s);
224 void dtls1_clear(SSL *s);
225 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
226 
227 int dtls1_get_message(SSL *s, int st1, int stn, int mt, long max);
228 int dtls1_get_record(SSL *s);
229 
230 __END_HIDDEN_DECLS
231 
232 #endif /* !HEADER_DTLS_LOCL_H */
233