-
Notifications
You must be signed in to change notification settings - Fork 16
/
dtls_misc.h
283 lines (225 loc) · 6.52 KB
/
dtls_misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @file dtls_misc.h
* @brief DTLS (Datagram Transport Layer Security)
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneSSL Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.4.4
**/
#ifndef _DTLS_MISC_H
#define _DTLS_MISC_H
//DTLS version numbers
#define DTLS_VERSION_1_0 0xFEFF
#define DTLS_VERSION_1_2 0xFEFD
#define DTLS_VERSION_1_3 0xFEFC
//DTLS support
#ifndef DTLS_SUPPORT
#define DTLS_SUPPORT DISABLED
#elif (DTLS_SUPPORT != ENABLED && DTLS_SUPPORT != DISABLED)
#error DTLS_SUPPORT parameter is not valid
#endif
//Default PMTU value
#ifndef DTLS_DEFAULT_PMTU
#define DTLS_DEFAULT_PMTU 1452
#elif (DTLS_DEFAULT_PMTU < 64)
#error DTLS_DEFAULT_PMTU parameter is not valid
#endif
//Minimum PMTU value
#ifndef DTLS_MIN_PMTU
#define DTLS_MIN_PMTU 528
#elif (DTLS_MIN_PMTU < 64)
#error DTLS_MIN_PMTU parameter is not valid
#endif
//Replay protection
#ifndef DTLS_REPLAY_DETECTION_SUPPORT
#define DTLS_REPLAY_DETECTION_SUPPORT ENABLED
#elif (DTLS_REPLAY_DETECTION_SUPPORT != ENABLED && DTLS_REPLAY_DETECTION_SUPPORT != DISABLED)
#error DTLS_REPLAY_DETECTION_SUPPORT parameter is not valid
#endif
//Size of the sliding window for replay protection
#ifndef DTLS_REPLAY_WINDOW_SIZE
#define DTLS_REPLAY_WINDOW_SIZE 64
#elif (DTLS_REPLAY_WINDOW_SIZE < 1)
#error DTLS_REPLAY_WINDOW_SIZE parameter is not valid
#endif
//Maximum size for cookies
#ifndef DTLS_MAX_COOKIE_SIZE
#define DTLS_MAX_COOKIE_SIZE 32
#elif (DTLS_MAX_COOKIE_SIZE < 32)
#error DTLS_MAX_COOKIE_SIZE parameter is not valid
#endif
//Maximum number of retransmissions
#ifndef DTLS_MAX_RETRIES
#define DTLS_MAX_RETRIES 5
#elif (DTLS_MAX_RETRIES < 1)
#error DTLS_MAX_RETRIES parameter is not valid
#endif
//Initial retransmission timeout
#ifndef DTLS_INIT_TIMEOUT
#define DTLS_INIT_TIMEOUT 1000
#elif (DTLS_INIT_TIMEOUT < 100)
#error DTLS_INIT_TIMEOUT parameter is not valid
#endif
//Minimum retransmission timeout
#ifndef DTLS_MIN_TIMEOUT
#define DTLS_MIN_TIMEOUT 500
#elif (DTLS_MIN_TIMEOUT < 100)
#error DTLS_MIN_TIMEOUT parameter is not valid
#endif
//Maximum retransmission timeout
#ifndef DTLS_MAX_TIMEOUT
#define DTLS_MAX_TIMEOUT 60000
#elif (DTLS_MAX_TIMEOUT < 1000)
#error DTLS_MAX_TIMEOUT parameter is not valid
#endif
//C++ guard
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief DTLS retransmission states
**/
typedef enum
{
DTLS_RETRANSMIT_STATE_PREPARING = 0,
DTLS_RETRANSMIT_STATE_SENDING = 1,
DTLS_RETRANSMIT_STATE_WAITING = 2,
DTLS_RETRANSMIT_STATE_FINISHED = 3
} DtlsRetransmitState;
//CC-RX, CodeWarrior or Win32 compiler?
#if defined(__CCRX__)
#pragma pack
#elif defined(__CWCC__) || defined(_WIN32)
#pragma pack(push, 1)
#endif
/**
* @brief Sequence number
**/
typedef __packed_struct
{
uint8_t b[6];
} DtlsSequenceNumber;
/**
* @brief Cookie
**/
typedef __packed_struct
{
uint8_t length; //0
uint8_t value[]; //1
} DtlsCookie;
/**
* @brief List of supported versions
**/
typedef __packed_struct
{
uint8_t length; //0
uint16_t value[]; //1
} DtlsSupportedVersionList;
/**
* @brief DTLS record
**/
typedef __packed_struct
{
uint8_t type; //0
uint16_t version; //1-2
uint16_t epoch; //3-4
DtlsSequenceNumber seqNum; //5-10
uint16_t length; //11-12
uint8_t data[]; //13
} DtlsRecord;
/**
* @brief DTLS handshake message
**/
typedef __packed_struct
{
uint8_t msgType; //0
uint8_t length[3]; //1-3
uint16_t msgSeq; //4-5
uint8_t fragOffset[3]; //6-8
uint8_t fragLength[3]; //9-11
uint8_t data[]; //12
} DtlsHandshake;
/**
* @brief HelloVerifyRequest message
**/
typedef __packed_struct
{
uint16_t serverVersion; //0-1
uint8_t cookieLength; //2
uint8_t cookie[]; //3
} DtlsHelloVerifyRequest;
//CC-RX, CodeWarrior or Win32 compiler?
#if defined(__CCRX__)
#pragma unpack
#elif defined(__CWCC__) || defined(_WIN32)
#pragma pack(pop)
#endif
/**
* @brief Client parameters
**/
typedef struct
{
uint16_t version;
const uint8_t *random;
size_t randomLen;
const uint8_t *sessionId;
size_t sessionIdLen;
const uint8_t *cipherSuites;
size_t cipherSuitesLen;
const uint8_t *compressMethods;
size_t compressMethodsLen;
} DtlsClientParameters;
/**
* @brief DTLS cookie generation callback function
**/
typedef error_t (*DtlsCookieGenerateCallback)(TlsContext *context,
const DtlsClientParameters *clientParams, uint8_t *cookie,
size_t *length, void *param);
/**
* @brief DTLS cookie verification callback function
**/
typedef error_t (*DtlsCookieVerifyCallback)(TlsContext *context,
const DtlsClientParameters *clientParams, const uint8_t *cookie,
size_t length, void *param);
//DTLS specific functions
error_t dtlsSelectVersion(TlsContext *context, uint16_t version);
uint16_t dtlsTranslateVersion(uint16_t version);
error_t dtlsFormatCookie(TlsContext *context, uint8_t *p, size_t *written);
error_t dtlsVerifyCookie(TlsContext *context, const DtlsCookie *cookie,
const DtlsClientParameters *clientParams);
error_t dtlsSendHelloVerifyRequest(TlsContext *context);
error_t dtlsFormatHelloVerifyRequest(TlsContext *context,
DtlsHelloVerifyRequest *message, size_t *length);
error_t dtlsParseHelloVerifyRequest(TlsContext *context,
const DtlsHelloVerifyRequest *message, size_t length);
error_t dtlsParseClientSupportedVersionsExtension(TlsContext *context,
const DtlsSupportedVersionList *supportedVersionList);
void dtlsInitReplayWindow(TlsContext *context);
error_t dtlsCheckReplayWindow(TlsContext *context, DtlsSequenceNumber *seqNum);
void dtlsUpdateReplayWindow(TlsContext *context, DtlsSequenceNumber *seqNum);
//C++ guard
#ifdef __cplusplus
}
#endif
#endif