tls13_misc.h
Go to the documentation of this file.
1 /**
2  * @file tls13_misc.h
3  * @brief TLS 1.3 helper functions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSL Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _TLS13_MISC_H
32 #define _TLS13_MISC_H
33 
34 //DHE key establishment
35 #ifndef TLS13_DHE_KE_SUPPORT
36  #define TLS13_DHE_KE_SUPPORT ENABLED
37 #elif (TLS13_DHE_KE_SUPPORT != ENABLED && TLS13_DHE_KE_SUPPORT != DISABLED)
38  #error TLS13_DHE_KE_SUPPORT parameter is not valid
39 #endif
40 
41 //ECDHE key establishment
42 #ifndef TLS13_ECDHE_KE_SUPPORT
43  #define TLS13_ECDHE_KE_SUPPORT ENABLED
44 #elif (TLS13_ECDHE_KE_SUPPORT != ENABLED && TLS13_ECDHE_KE_SUPPORT != DISABLED)
45  #error TLS13_ECDHE_KE_SUPPORT parameter is not valid
46 #endif
47 
48 //PSK-only key establishment
49 #ifndef TLS13_PSK_KE_SUPPORT
50  #define TLS13_PSK_KE_SUPPORT DISABLED
51 #elif (TLS13_PSK_KE_SUPPORT != ENABLED && TLS13_PSK_KE_SUPPORT != DISABLED)
52  #error TLS13_PSK_KE_SUPPORT parameter is not valid
53 #endif
54 
55 //PSK with DHE key establishment
56 #ifndef TLS13_PSK_DHE_KE_SUPPORT
57  #define TLS13_PSK_DHE_KE_SUPPORT ENABLED
58 #elif (TLS13_PSK_DHE_KE_SUPPORT != ENABLED && TLS13_PSK_DHE_KE_SUPPORT != DISABLED)
59  #error TLS13_PSK_DHE_KE_SUPPORT parameter is not valid
60 #endif
61 
62 //PSK with ECDHE key establishment
63 #ifndef TLS13_PSK_ECDHE_KE_SUPPORT
64  #define TLS13_PSK_ECDHE_KE_SUPPORT ENABLED
65 #elif (TLS13_PSK_ECDHE_KE_SUPPORT != ENABLED && TLS13_PSK_ECDHE_KE_SUPPORT != DISABLED)
66  #error TLS13_PSK_ECDHE_KE_SUPPORT parameter is not valid
67 #endif
68 
69 //Early data support
70 #ifndef TLS13_EARLY_DATA_SUPPORT
71  #define TLS13_EARLY_DATA_SUPPORT DISABLED
72 #elif (TLS13_EARLY_DATA_SUPPORT != ENABLED && TLS13_EARLY_DATA_SUPPORT != DISABLED)
73  #error TLS13_EARLY_DATA_SUPPORT parameter is not valid
74 #endif
75 
76 //Middlebox compatibility mode
77 #ifndef TLS13_MIDDLEBOX_COMPAT_SUPPORT
78  #define TLS13_MIDDLEBOX_COMPAT_SUPPORT ENABLED
79 #elif (TLS13_MIDDLEBOX_COMPAT_SUPPORT != ENABLED && TLS13_MIDDLEBOX_COMPAT_SUPPORT != DISABLED)
80  #error TLS13_MIDDLEBOX_COMPAT_SUPPORT parameter is not valid
81 #endif
82 
83 //Maximum size for cookies
84 #ifndef TLS13_MAX_COOKIE_SIZE
85  #define TLS13_MAX_COOKIE_SIZE 256
86 #elif (TLS13_MAX_COOKIE_SIZE < 32)
87  #error TLS13_MAX_COOKIE_SIZE parameter is not valid
88 #endif
89 
90 //Maximum size for session tickets
91 #ifndef TLS13_MAX_TICKET_SIZE
92  #define TLS13_MAX_TICKET_SIZE 1024
93 #elif (TLS13_MAX_TICKET_SIZE < 32)
94  #error TLS13_MAX_TICKET_SIZE parameter is not valid
95 #endif
96 
97 //Maximum lifetime of session tickets
98 #ifndef TLS13_MAX_TICKET_LIFETIME
99  #define TLS13_MAX_TICKET_LIFETIME 604800
100 #elif (TLS13_MAX_TICKET_LIFETIME < 0)
101  #error TLS13_MAX_TICKET_LIFETIME parameter is not valid
102 #endif
103 
104 //Age tolerance for tickets, in milliseconds
105 #ifndef TLS13_TICKET_AGE_TOLERANCE
106  #define TLS13_TICKET_AGE_TOLERANCE 5000
107 #elif (TLS13_TICKET_AGE_TOLERANCE < 0)
108  #error TLS13_TICKET_AGE_TOLERANCE parameter is not valid
109 #endif
110 
111 //Number of NewSessionTicket message sent by the server
112 #ifndef TLS13_NEW_SESSION_TICKET_COUNT
113  #define TLS13_NEW_SESSION_TICKET_COUNT 2
114 #elif (TLS13_NEW_SESSION_TICKET_COUNT < 0)
115  #error TLS13_NEW_SESSION_TICKET_COUNT parameter is not valid
116 #endif
117 
118 //Maximum size for HKDF digests
119 #if (TLS_SHA384_SUPPORT == ENABLED)
120  #define TLS13_MAX_HKDF_DIGEST_SIZE 48
121 #else
122  #define TLS13_MAX_HKDF_DIGEST_SIZE 32
123 #endif
124 
125 //C++ guard
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129 
130 
131 /**
132  * @brief PSK key exchange modes
133  **/
134 
135 typedef enum
136 {
140 
141 
142 /**
143  * @brief Key update requests
144  **/
145 
146 typedef enum
147 {
151 
152 
153 //CC-RX, CodeWarrior or Win32 compiler?
154 #if defined(__CCRX__)
155  #pragma pack
156 #elif defined(__CWCC__) || defined(_WIN32)
157  #pragma pack(push, 1)
158 #endif
159 
160 
161 /**
162  * @brief Cookie
163  **/
164 
166 {
167  uint16_t length; //0-1
168  uint8_t value[]; //2
170 
171 
172 /**
173  * @brief Key share entry
174  **/
175 
176 typedef __packed_struct
177 {
178  uint16_t group; //0
179  uint16_t length; //1
180  uint8_t keyExchange[]; //2
182 
183 
184 /**
185  * @brief List of key shares
186  **/
187 
188 typedef __packed_struct
189 {
190  uint16_t length; //0
191  uint8_t value[]; //1
193 
194 
195 /**
196  * @brief List of PSK key exchange modes
197  **/
198 
199 typedef __packed_struct
200 {
201  uint8_t length; //0
202  uint8_t value[]; //1
204 
205 
206 /**
207  * @brief PSK identity
208  **/
209 
210 typedef __packed_struct
211 {
212  uint16_t length; //0-1
213  uint8_t value[]; //2
215 
216 
217 /**
218  * @brief List of PSK identities
219  **/
220 
221 typedef __packed_struct
222 {
223  uint16_t length; //0-1
224  uint8_t value[]; //2
226 
227 
228 /**
229  * @brief PSK binder
230  **/
231 
232 typedef __packed_struct
233 {
234  uint8_t length; //0
235  uint8_t value[]; //1
237 
238 
239 /**
240  * @brief List of PSK binders
241  **/
242 
243 typedef __packed_struct
244 {
245  uint16_t length; //0-1
246  uint8_t value[]; //2
248 
249 
250 /**
251  * @brief Certificate request context
252  **/
253 
254 typedef __packed_struct
255 {
256  uint8_t length; //0
257  uint8_t value[]; //1
259 
260 
261 /**
262  * @brief Digitally-signed element (TLS 1.3)
263  **/
264 
265 typedef __packed_struct
266 {
267  uint16_t algorithm; //0-1
268  uint16_t length; //2-3
269  uint8_t value[]; //4
271 
272 
273 /**
274  * @brief HelloRetryRequest message
275  **/
276 
277 typedef __packed_struct
278 {
279  uint16_t serverVersion; //0-1
280  uint8_t random[32]; //2-33
281  uint8_t sessionIdLen; //34
282  uint8_t sessionId[]; //35
284 
285 
286 /**
287  * @brief EndOfEarlyData message
288  **/
289 
290 typedef void *Tls13EndOfEarlyData;
291 
292 
293 /**
294  * @brief EncryptedExtensions message
295  **/
296 
297 typedef __packed_struct
298 {
299  uint16_t extensionsLen; //0-1
300  uint8_t extensions[]; //2
302 
303 
304 /**
305  * @brief NewSessionTicket message (TLS 1.3)
306  **/
307 
308 typedef __packed_struct
309 {
310  uint32_t ticketLifetime; //0-3
311  uint32_t ticketAgeAdd; //4-7
312  uint8_t ticketNonceLen; //8
313  uint8_t ticketNonce[]; //9
315 
316 
317 /**
318  * @brief KeyUpdate message
319  **/
320 
321 typedef __packed_struct
322 {
323  uint8_t requestUpdate; //0
325 
326 
327 /**
328  * @brief Session ticket
329  **/
330 
331 typedef __packed_struct
332 {
333  uint16_t length; //0-1
334  uint8_t data[]; //2
336 
337 
338 /**
339  * @brief Session state information
340  **/
341 
342 typedef __packed_struct
343 {
344  uint16_t version; ///<Protocol version
345  uint16_t cipherSuite; ///<Cipher suite identifier
346  systime_t ticketTimestamp; ///<Timestamp to manage ticket lifetime
347  uint32_t ticketLifetime; ///<Lifetime of the ticket
348  uint32_t ticketAgeAdd; ///<Random value used to obscure the age of the ticket
349  uint8_t ticketNonce[4]; ///<A per-ticket value that is unique across all tickets issued
350  size_t ticketPskLen; ///<Length of the PSK associated with the ticket
351  uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]; ///<PSK associated with the ticket
353 
354 
355 //CC-RX, CodeWarrior or Win32 compiler?
356 #if defined(__CCRX__)
357  #pragma unpack
358 #elif defined(__CWCC__) || defined(_WIN32)
359  #pragma pack(pop)
360 #endif
361 
362 //TLS 1.3 related constants
363 extern const uint8_t tls11DowngradeRandom[8];
364 extern const uint8_t tls12DowngradeRandom[8];
365 extern const uint8_t tls13HelloRetryRequestRandom[32];
366 
367 //TLS 1.3 related functions
368 error_t tls13ComputePskBinder(TlsContext *context, const void *clientHello,
369  size_t clientHelloLen, size_t truncatedClientHelloLen,
370  const Tls13PskIdentity *identity, uint8_t *binder, size_t binderLen);
371 
372 error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup);
373 
374 error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare,
375  size_t length);
376 
377 error_t tls13ComputeMac(TlsContext *context, TlsEncryptionEngine *encryptionEngine,
378  void *record, const uint8_t *data, size_t dataLen, uint8_t *mac);
379 
381 
383 
384 bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup);
385 bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup);
386 bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup);
387 
388 error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p,
389  size_t length);
390 
391 error_t tls13FormatCertExtensions(uint8_t *p, size_t *written);
392 
393 error_t tls13ParseCertExtensions(const uint8_t *p, size_t length,
394  size_t *consumed);
395 
396 //C++ guard
397 #ifdef __cplusplus
398 }
399 #endif
400 
401 #endif
uint8_t version
Definition: coap_common.h:177
int bool_t
Definition: compiler_port.h:53
uint8_t algorithm
error_t
Error codes.
Definition: error.h:43
uint8_t p
Definition: ndp.h:300
uint32_t systime_t
System time.
uint32_t dataLen
Definition: sftp_common.h:229
Tls13NewSessionTicket
Definition: tls13_misc.h:314
systime_t ticketTimestamp
Timestamp to manage ticket lifetime.
Definition: tls13_misc.h:346
Tls13PskBinderList
Definition: tls13_misc.h:247
Tls13KeyShareList
Definition: tls13_misc.h:192
uint8_t sessionId[]
Definition: tls13_misc.h:282
uint8_t sessionIdLen
Definition: tls13_misc.h:281
uint16_t length
Definition: tls13_misc.h:179
Tls13Ticket
Definition: tls13_misc.h:335
error_t tls13DigestClientHello1(TlsContext *context)
Hash ClientHello1 in the transcript when HelloRetryRequest is used.
Definition: tls13_misc.c:493
error_t tls13FormatCertExtensions(uint8_t *p, size_t *written)
Format certificate extensions.
Definition: tls13_misc.c:754
size_t ticketPskLen
Length of the PSK associated with the ticket.
Definition: tls13_misc.h:350
bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given ECDHE group is supported.
Definition: tls13_misc.c:649
error_t tls13ComputeMac(TlsContext *context, TlsEncryptionEngine *encryptionEngine, void *record, const uint8_t *data, size_t dataLen, uint8_t *mac)
Compute message authentication code.
Definition: tls13_misc.c:451
uint8_t ticketNonce[]
A per-ticket value that is unique across all tickets issued.
Definition: tls13_misc.h:313
Tls13KeyUpdateRequest
Key update requests.
Definition: tls13_misc.h:147
@ TLS_KEY_UPDATE_REQUESTED
Definition: tls13_misc.h:149
@ TLS_KEY_UPDATE_NOT_REQUESTED
Definition: tls13_misc.h:148
uint8_t data[]
Definition: tls13_misc.h:334
void * Tls13EndOfEarlyData
EndOfEarlyData message.
Definition: tls13_misc.h:290
uint8_t random[32]
Definition: tls13_misc.h:280
Tls13KeyShareEntry
Definition: tls13_misc.h:181
bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given FFDHE group is supported.
Definition: tls13_misc.c:609
error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare, size_t length)
(EC)DHE shared secret generation
Definition: tls13_misc.c:352
uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]
PSK associated with the ticket.
Definition: tls13_misc.h:351
const uint8_t tls12DowngradeRandom[8]
Definition: tls13_misc.c:59
Tls13PskKeyExchMode
PSK key exchange modes.
Definition: tls13_misc.h:136
@ TLS_PSK_KEY_EXCH_MODE_PSK_KE
Definition: tls13_misc.h:137
@ TLS_PSK_KEY_EXCH_MODE_PSK_DHE_KE
Definition: tls13_misc.h:138
Tls13Cookie
Definition: tls13_misc.h:169
uint8_t extensions[]
Definition: tls13_misc.h:300
Tls13EncryptedExtensions
Definition: tls13_misc.h:301
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:538
uint8_t keyExchange[]
Definition: tls13_misc.h:180
Tls13PskIdentityList
Definition: tls13_misc.h:225
Tls13PskBinder
Definition: tls13_misc.h:236
error_t tls13ComputePskBinder(TlsContext *context, const void *clientHello, size_t clientHelloLen, size_t truncatedClientHelloLen, const Tls13PskIdentity *identity, uint8_t *binder, size_t binderLen)
Compute PSK binder value.
Definition: tls13_misc.c:86
const uint8_t tls11DowngradeRandom[8]
Definition: tls13_misc.c:53
Tls13HelloRetryRequest
Definition: tls13_misc.h:283
uint8_t ticketNonceLen
Definition: tls13_misc.h:312
bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given named group is supported.
Definition: tls13_misc.c:576
Tls13DigitalSignature
Definition: tls13_misc.h:270
Tls13KeyUpdate
Definition: tls13_misc.h:324
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls13_misc.h:345
typedef __packed_struct
Cookie.
Definition: tls13_misc.h:166
error_t tls13ParseCertExtensions(const uint8_t *p, size_t length, size_t *consumed)
Parse certificate extensions.
Definition: tls13_misc.c:783
Tls13CertRequestContext
Definition: tls13_misc.h:258
error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p, size_t length)
Check whether the specified key share group is a duplicate.
Definition: tls13_misc.c:708
Tls13PlaintextSessionState
Definition: tls13_misc.h:352
uint32_t ticketAgeAdd
Random value used to obscure the age of the ticket.
Definition: tls13_misc.h:311
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:260
#define TLS13_MAX_HKDF_DIGEST_SIZE
Definition: tls13_misc.h:120
uint8_t value[]
Definition: tls13_misc.h:168
uint32_t ticketLifetime
Lifetime of the ticket.
Definition: tls13_misc.h:347
Tls13PskIdentity
Definition: tls13_misc.h:214
const uint8_t tls13HelloRetryRequestRandom[32]
Definition: tls13_misc.c:65
Tls13PskKeModeList
Definition: tls13_misc.h:203
#define TlsContext
Definition: tls.h:36
#define TlsEncryptionEngine
Definition: tls.h:40