1 /* ssl/dtls1.h */ 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 module deimos.openssl.dtls1; 61 62 import deimos.openssl._d_util; 63 64 import deimos.openssl.comp; // Needed for COMP_CTX. 65 import deimos.openssl.ssl; // Needed for SSL_SESSION. 66 import deimos.openssl.ssl3; // Needed for SSL3_BUFFER. 67 68 public import deimos.openssl.buffer; 69 public import deimos.openssl.pqueue; 70 // #ifdef OPENSSL_SYS_VMS 71 // #include <resource.h> 72 // #include <sys/timeb.h> 73 // #endif 74 version (Windows) { 75 /* Needed for timeval */ 76 import std.c.windows.winsock; 77 // #elif defined(OPENSSL_SYS_NETWARE) && !defined(_WINSOCK2API_) 78 // #include <sys/timeval.h> 79 // #else 80 // #if defined(OPENSSL_SYS_VXWORKS) 81 // #include <sys/times.h> 82 // #else 83 } else version (Win64) { 84 import std.c.windows.winsock; 85 } else { 86 import core.sys.posix.sys.time; 87 } 88 89 extern (C): 90 nothrow: 91 92 enum DTLS1_VERSION = 0xFEFF; 93 enum DTLS1_BAD_VER = 0x0100; 94 95 version (none) { 96 /* this alert description is not specified anywhere... */ 97 enum DTLS1_AD_MISSING_HANDSHAKE_MESSAGE = 110; 98 } 99 100 /* lengths of messages */ 101 enum DTLS1_COOKIE_LENGTH = 256; 102 103 enum DTLS1_RT_HEADER_LENGTH = 13; 104 105 enum DTLS1_HM_HEADER_LENGTH = 12; 106 107 enum DTLS1_HM_BAD_FRAGMENT = -2; 108 enum DTLS1_HM_FRAGMENT_RETRY = -3; 109 110 enum DTLS1_CCS_HEADER_LENGTH = 1; 111 112 version (none) { // #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE 113 enum DTLS1_AL_HEADER_LENGTH = 7; 114 } else { 115 enum DTLS1_AL_HEADER_LENGTH = 2; 116 } 117 118 version(OPENSSL_NO_SSL_INTERN) {} else { 119 120 version(OPENSSL_NO_SCTP) {} else { 121 enum DTLS1_SCTP_AUTH_LABEL = "EXPORTER_DTLS_OVER_SCTP"; 122 } 123 124 struct dtls1_bitmap_st { 125 c_ulong map; /* track 32 packets on 32-bit systems 126 and 64 - on 64-bit systems */ 127 ubyte[8] max_seq_num; /* max record number seen so far, 128 64-bit value in big-endian 129 encoding */ 130 } 131 alias dtls1_bitmap_st DTLS1_BITMAP; 132 133 struct dtls1_retransmit_state 134 { 135 EVP_CIPHER_CTX* enc_write_ctx; /* cryptographic state */ 136 EVP_MD_CTX* write_hash; /* used for mac generation */ 137 version(OPENSSL_NO_COMP) { 138 char* compress; 139 } else { 140 COMP_CTX* compress; /* compression */ 141 } 142 SSL_SESSION* session; 143 ushort epoch; 144 }; 145 146 struct hm_header_st 147 { 148 ubyte type; 149 c_ulong msg_len; 150 ushort seq; 151 c_ulong frag_off; 152 c_ulong frag_len; 153 uint is_ccs; 154 dtls1_retransmit_state saved_retransmit_state; 155 }; 156 157 struct ccs_header_st 158 { 159 ubyte type; 160 ushort seq; 161 }; 162 163 struct dtls1_timeout_st 164 { 165 /* Number of read timeouts so far */ 166 uint read_timeouts; 167 168 /* Number of write timeouts so far */ 169 uint write_timeouts; 170 171 /* Number of alerts received so far */ 172 uint num_alerts; 173 }; 174 175 struct record_pqueue_st { 176 ushort epoch; 177 pqueue q; 178 } 179 alias record_pqueue_st record_pqueue; 180 181 struct hm_fragment_st { 182 hm_header_st msg_header; 183 ubyte* fragment; 184 ubyte* reassembly; 185 } 186 alias hm_fragment_st hm_fragment; 187 188 struct dtls1_state_st { 189 uint send_cookie; 190 ubyte[DTLS1_COOKIE_LENGTH] cookie; 191 ubyte[DTLS1_COOKIE_LENGTH] rcvd_cookie; 192 uint cookie_len; 193 194 /* 195 * The current data and handshake epoch. This is initially 196 * undefined, and starts at zero once the initial handshake is 197 * completed 198 */ 199 ushort r_epoch; 200 ushort w_epoch; 201 202 /* records being received in the current epoch */ 203 DTLS1_BITMAP bitmap; 204 205 /* renegotiation starts a new set of sequence numbers */ 206 DTLS1_BITMAP next_bitmap; 207 208 /* handshake message numbers */ 209 ushort handshake_write_seq; 210 ushort next_handshake_write_seq; 211 212 ushort handshake_read_seq; 213 214 /* save last sequence number for retransmissions */ 215 ubyte[8] last_write_sequence; 216 217 /* Received handshake records (processed and unprocessed) */ 218 record_pqueue unprocessed_rcds; 219 record_pqueue processed_rcds; 220 221 /* Buffered handshake messages */ 222 pqueue buffered_messages; 223 224 /* Buffered (sent) handshake records */ 225 pqueue sent_messages; 226 227 /* Buffered application records. 228 * Only for records between CCS and Finished 229 * to prevent either protocol violation or 230 * unnecessary message loss. 231 */ 232 record_pqueue buffered_app_data; 233 234 /* Is set when listening for new connections with dtls1_listen() */ 235 uint listen; 236 237 uint mtu; /* max DTLS packet size */ 238 239 hm_header_st w_msg_hdr; 240 hm_header_st r_msg_hdr; 241 242 dtls1_timeout_st timeout; 243 244 /* Indicates when the last handshake msg or heartbeat sent will timeout */ 245 timeval next_timeout; 246 247 /* Timeout duration */ 248 ushort timeout_duration; 249 250 /* storage for Alert/Handshake protocol data received but not 251 * yet processed by ssl3_read_bytes: */ 252 ubyte[DTLS1_AL_HEADER_LENGTH] alert_fragment; 253 uint alert_fragment_len; 254 ubyte[DTLS1_HM_HEADER_LENGTH] handshake_fragment; 255 uint handshake_fragment_len; 256 257 uint retransmitting; 258 uint change_cipher_spec_ok; 259 260 version(OPENSSL_NO_SCTP) {} else { 261 /* used when SSL_ST_XX_FLUSH is entered */ 262 int next_state; 263 264 int shutdown_received; 265 } 266 267 } 268 alias dtls1_state_st DTLS1_STATE; 269 270 struct dtls1_record_data_st { 271 ubyte* packet; 272 uint packet_length; 273 SSL3_BUFFER rbuf; 274 SSL3_RECORD rrec; 275 version(OPENSSL_NO_SCTP) {} else { 276 bio_dgram_sctp_rcvinfo recordinfo; 277 } 278 } 279 alias dtls1_record_data_st DTLS1_RECORD_DATA; 280 281 } 282 283 /* Timeout multipliers (timeout slice is defined in apps/timeouts.h */ 284 enum DTLS1_TMO_READ_COUNT = 2; 285 enum DTLS1_TMO_WRITE_COUNT = 2; 286 287 enum DTLS1_TMO_ALERT_COUNT = 12;