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.4
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 exchange support
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 //Hybrid key exchange support
49 #ifndef TLS13_HYBRID_KE_SUPPORT
50  #define TLS13_HYBRID_KE_SUPPORT DISABLED
51 #elif (TLS13_HYBRID_KE_SUPPORT != ENABLED && TLS13_HYBRID_KE_SUPPORT != DISABLED)
52  #error TLS13_HYBRID_KE_SUPPORT parameter is not valid
53 #endif
54 
55 //PSK-only key exchange support
56 #ifndef TLS13_PSK_KE_SUPPORT
57  #define TLS13_PSK_KE_SUPPORT DISABLED
58 #elif (TLS13_PSK_KE_SUPPORT != ENABLED && TLS13_PSK_KE_SUPPORT != DISABLED)
59  #error TLS13_PSK_KE_SUPPORT parameter is not valid
60 #endif
61 
62 //PSK with DHE key exchange support
63 #ifndef TLS13_PSK_DHE_KE_SUPPORT
64  #define TLS13_PSK_DHE_KE_SUPPORT ENABLED
65 #elif (TLS13_PSK_DHE_KE_SUPPORT != ENABLED && TLS13_PSK_DHE_KE_SUPPORT != DISABLED)
66  #error TLS13_PSK_DHE_KE_SUPPORT parameter is not valid
67 #endif
68 
69 //PSK with ECDHE key exchange support
70 #ifndef TLS13_PSK_ECDHE_KE_SUPPORT
71  #define TLS13_PSK_ECDHE_KE_SUPPORT ENABLED
72 #elif (TLS13_PSK_ECDHE_KE_SUPPORT != ENABLED && TLS13_PSK_ECDHE_KE_SUPPORT != DISABLED)
73  #error TLS13_PSK_ECDHE_KE_SUPPORT parameter is not valid
74 #endif
75 
76 //PSK with hybrid key exchange support
77 #ifndef TLS13_PSK_HYBRID_KE_SUPPORT
78  #define TLS13_PSK_HYBRID_KE_SUPPORT DISABLED
79 #elif (TLS13_PSK_HYBRID_KE_SUPPORT != ENABLED && TLS13_PSK_HYBRID_KE_SUPPORT != DISABLED)
80  #error TLS13_PSK_HYBRID_KE_SUPPORT parameter is not valid
81 #endif
82 
83 //Early data support
84 #ifndef TLS13_EARLY_DATA_SUPPORT
85  #define TLS13_EARLY_DATA_SUPPORT DISABLED
86 #elif (TLS13_EARLY_DATA_SUPPORT != ENABLED && TLS13_EARLY_DATA_SUPPORT != DISABLED)
87  #error TLS13_EARLY_DATA_SUPPORT parameter is not valid
88 #endif
89 
90 //Middlebox compatibility mode
91 #ifndef TLS13_MIDDLEBOX_COMPAT_SUPPORT
92  #define TLS13_MIDDLEBOX_COMPAT_SUPPORT ENABLED
93 #elif (TLS13_MIDDLEBOX_COMPAT_SUPPORT != ENABLED && TLS13_MIDDLEBOX_COMPAT_SUPPORT != DISABLED)
94  #error TLS13_MIDDLEBOX_COMPAT_SUPPORT parameter is not valid
95 #endif
96 
97 //Maximum size for cookies
98 #ifndef TLS13_MAX_COOKIE_SIZE
99  #define TLS13_MAX_COOKIE_SIZE 256
100 #elif (TLS13_MAX_COOKIE_SIZE < 32)
101  #error TLS13_MAX_COOKIE_SIZE parameter is not valid
102 #endif
103 
104 //Maximum size for session tickets
105 #ifndef TLS13_MAX_TICKET_SIZE
106  #define TLS13_MAX_TICKET_SIZE 1024
107 #elif (TLS13_MAX_TICKET_SIZE < 32)
108  #error TLS13_MAX_TICKET_SIZE parameter is not valid
109 #endif
110 
111 //Maximum lifetime of session tickets
112 #ifndef TLS13_MAX_TICKET_LIFETIME
113  #define TLS13_MAX_TICKET_LIFETIME 604800
114 #elif (TLS13_MAX_TICKET_LIFETIME < 0)
115  #error TLS13_MAX_TICKET_LIFETIME parameter is not valid
116 #endif
117 
118 //Age tolerance for tickets, in milliseconds
119 #ifndef TLS13_TICKET_AGE_TOLERANCE
120  #define TLS13_TICKET_AGE_TOLERANCE 5000
121 #elif (TLS13_TICKET_AGE_TOLERANCE < 0)
122  #error TLS13_TICKET_AGE_TOLERANCE parameter is not valid
123 #endif
124 
125 //Number of NewSessionTicket message sent by the server
126 #ifndef TLS13_NEW_SESSION_TICKET_COUNT
127  #define TLS13_NEW_SESSION_TICKET_COUNT 2
128 #elif (TLS13_NEW_SESSION_TICKET_COUNT < 0)
129  #error TLS13_NEW_SESSION_TICKET_COUNT parameter is not valid
130 #endif
131 
132 //Maximum size for HKDF digests
133 #if (TLS_SHA384_SUPPORT == ENABLED)
134  #define TLS13_MAX_HKDF_DIGEST_SIZE 48
135 #else
136  #define TLS13_MAX_HKDF_DIGEST_SIZE 32
137 #endif
138 
139 //C++ guard
140 #ifdef __cplusplus
141 extern "C" {
142 #endif
143 
144 
145 /**
146  * @brief PSK key exchange modes
147  **/
148 
149 typedef enum
150 {
154 
155 
156 /**
157  * @brief Key update requests
158  **/
159 
160 typedef enum
161 {
165 
166 
167 //CC-RX, CodeWarrior or Win32 compiler?
168 #if defined(__CCRX__)
169  #pragma pack
170 #elif defined(__CWCC__) || defined(_WIN32)
171  #pragma pack(push, 1)
172 #endif
173 
174 
175 /**
176  * @brief Cookie
177  **/
178 
180 {
181  uint16_t length; //0-1
182  uint8_t value[]; //2
184 
185 
186 /**
187  * @brief Key share entry
188  **/
189 
190 typedef __packed_struct
191 {
192  uint16_t group; //0
193  uint16_t length; //1
194  uint8_t keyExchange[]; //2
196 
197 
198 /**
199  * @brief List of key shares
200  **/
201 
202 typedef __packed_struct
203 {
204  uint16_t length; //0
205  uint8_t value[]; //1
207 
208 
209 /**
210  * @brief List of PSK key exchange modes
211  **/
212 
213 typedef __packed_struct
214 {
215  uint8_t length; //0
216  uint8_t value[]; //1
218 
219 
220 /**
221  * @brief PSK identity
222  **/
223 
224 typedef __packed_struct
225 {
226  uint16_t length; //0-1
227  uint8_t value[]; //2
229 
230 
231 /**
232  * @brief List of PSK identities
233  **/
234 
235 typedef __packed_struct
236 {
237  uint16_t length; //0-1
238  uint8_t value[]; //2
240 
241 
242 /**
243  * @brief PSK binder
244  **/
245 
246 typedef __packed_struct
247 {
248  uint8_t length; //0
249  uint8_t value[]; //1
251 
252 
253 /**
254  * @brief List of PSK binders
255  **/
256 
257 typedef __packed_struct
258 {
259  uint16_t length; //0-1
260  uint8_t value[]; //2
262 
263 
264 /**
265  * @brief Certificate request context
266  **/
267 
268 typedef __packed_struct
269 {
270  uint8_t length; //0
271  uint8_t value[]; //1
273 
274 
275 /**
276  * @brief Digitally-signed element (TLS 1.3)
277  **/
278 
279 typedef __packed_struct
280 {
281  uint16_t algorithm; //0-1
282  uint16_t length; //2-3
283  uint8_t value[]; //4
285 
286 
287 /**
288  * @brief HelloRetryRequest message
289  **/
290 
291 typedef __packed_struct
292 {
293  uint16_t serverVersion; //0-1
294  uint8_t random[32]; //2-33
295  uint8_t sessionIdLen; //34
296  uint8_t sessionId[]; //35
298 
299 
300 /**
301  * @brief EndOfEarlyData message
302  **/
303 
304 typedef void *Tls13EndOfEarlyData;
305 
306 
307 /**
308  * @brief EncryptedExtensions message
309  **/
310 
311 typedef __packed_struct
312 {
313  uint16_t extensionsLen; //0-1
314  uint8_t extensions[]; //2
316 
317 
318 /**
319  * @brief NewSessionTicket message (TLS 1.3)
320  **/
321 
322 typedef __packed_struct
323 {
324  uint32_t ticketLifetime; //0-3
325  uint32_t ticketAgeAdd; //4-7
326  uint8_t ticketNonceLen; //8
327  uint8_t ticketNonce[]; //9
329 
330 
331 /**
332  * @brief KeyUpdate message
333  **/
334 
335 typedef __packed_struct
336 {
337  uint8_t requestUpdate; //0
339 
340 
341 /**
342  * @brief Session ticket
343  **/
344 
345 typedef __packed_struct
346 {
347  uint16_t length; //0-1
348  uint8_t data[]; //2
350 
351 
352 /**
353  * @brief Session state information
354  **/
355 
356 typedef __packed_struct
357 {
358  uint16_t version; ///<Protocol version
359  uint16_t cipherSuite; ///<Cipher suite identifier
360  systime_t ticketTimestamp; ///<Timestamp to manage ticket lifetime
361  uint32_t ticketLifetime; ///<Lifetime of the ticket
362  uint32_t ticketAgeAdd; ///<Random value used to obscure the age of the ticket
363  uint8_t ticketNonce[4]; ///<A per-ticket value that is unique across all tickets issued
364  size_t ticketPskLen; ///<Length of the PSK associated with the ticket
365  uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]; ///<PSK associated with the ticket
367 
368 
369 //CC-RX, CodeWarrior or Win32 compiler?
370 #if defined(__CCRX__)
371  #pragma unpack
372 #elif defined(__CWCC__) || defined(_WIN32)
373  #pragma pack(pop)
374 #endif
375 
376 //TLS 1.3 related constants
377 extern const uint8_t tls11DowngradeRandom[8];
378 extern const uint8_t tls12DowngradeRandom[8];
379 extern const uint8_t tls13HelloRetryRequestRandom[32];
380 
381 //TLS 1.3 related functions
382 error_t tls13ComputePskBinder(TlsContext *context, const void *clientHello,
383  size_t clientHelloLen, size_t truncatedClientHelloLen,
384  const Tls13PskIdentity *identity, uint8_t *binder, size_t binderLen);
385 
386 error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup);
387 
388 error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare,
389  size_t length);
390 
391 error_t tls13Encapsulate(TlsContext *context, uint16_t namedGroup,
392  const uint8_t *keyShare, size_t length);
393 
394 error_t tls13Decapsulate(TlsContext *context, const uint8_t *keyShare,
395  size_t length);
396 
397 error_t tls13ComputeMac(TlsContext *context, TlsEncryptionEngine *encryptionEngine,
398  void *record, const uint8_t *data, size_t dataLen, uint8_t *mac);
399 
401 
403 
404 bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup);
405 bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup);
406 bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup);
407 bool_t tls13IsHybridKeMethodSupported(TlsContext *context, uint16_t namedGroup);
408 
410  uint16_t namedGroup);
411 
412 const KemAlgo *tls13GetNextGenAlgo(TlsContext *context,
413  uint16_t namedGroup);
414 
415 error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p,
416  size_t length);
417 
418 error_t tls13FormatCertExtensions(uint8_t *p, size_t *written);
419 
420 error_t tls13ParseCertExtensions(const uint8_t *p, size_t length,
421  size_t *consumed);
422 
423 //C++ guard
424 #ifdef __cplusplus
425 }
426 #endif
427 
428 #endif
uint8_t sessionId[]
Definition: tls13_misc.h:296
uint32_t ticketLifetime
Lifetime of the ticket.
Definition: tls13_misc.h:361
Tls13PskBinderList
Definition: tls13_misc.h:261
#define TLS13_MAX_HKDF_DIGEST_SIZE
Definition: tls13_misc.h:134
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:683
uint8_t keyExchange[]
Definition: tls13_misc.h:194
int bool_t
Definition: compiler_port.h:53
const EcCurveInfo * tls13GetTraditionalAlgo(TlsContext *context, uint16_t namedGroup)
Get the traditional algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:984
Tls13Cookie
Definition: tls13_misc.h:183
Tls13KeyUpdate
Definition: tls13_misc.h:338
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:260
uint8_t ticketNonce[]
A per-ticket value that is unique across all tickets issued.
Definition: tls13_misc.h:327
uint8_t algorithm
uint8_t p
Definition: ndp.h:300
uint16_t length
Definition: tls13_misc.h:193
error_t tls13DigestClientHello1(TlsContext *context)
Hash ClientHello1 in the transcript when HelloRetryRequest is used.
Definition: tls13_misc.c:725
typedef __packed_struct
Cookie.
Definition: tls13_misc.h:180
uint8_t data[]
Definition: tls13_misc.h:348
error_t tls13ParseCertExtensions(const uint8_t *p, size_t length, size_t *consumed)
Parse certificate extensions.
Definition: tls13_misc.c:1181
size_t ticketPskLen
Length of the PSK associated with the ticket.
Definition: tls13_misc.h:364
uint8_t version
Definition: coap_common.h:177
Tls13KeyShareEntry
Definition: tls13_misc.h:195
uint8_t ticketNonceLen
Definition: tls13_misc.h:326
error_t tls13Encapsulate(TlsContext *context, uint16_t namedGroup, const uint8_t *keyShare, size_t length)
Encapsulation algorithm.
Definition: tls13_misc.c:498
Tls13KeyUpdateRequest
Key update requests.
Definition: tls13_misc.h:161
uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]
PSK associated with the ticket.
Definition: tls13_misc.h:365
@ TLS_PSK_KEY_EXCH_MODE_PSK_KE
Definition: tls13_misc.h:151
@ TLS_PSK_KEY_EXCH_MODE_PSK_DHE_KE
Definition: tls13_misc.h:152
bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given named group is supported.
Definition: tls13_misc.c:808
Elliptic curve parameters.
Definition: ec_curves.h:302
Tls13PskKeyExchMode
PSK key exchange modes.
Definition: tls13_misc.h:150
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
Tls13PlaintextSessionState
Definition: tls13_misc.h:366
Tls13HelloRetryRequest
Definition: tls13_misc.h:297
systime_t ticketTimestamp
Timestamp to manage ticket lifetime.
Definition: tls13_misc.h:360
const KemAlgo * tls13GetNextGenAlgo(TlsContext *context, uint16_t namedGroup)
Get the next-gen algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1047
error_t tls13Decapsulate(TlsContext *context, const uint8_t *keyShare, size_t length)
Decapsulation algorithm.
Definition: tls13_misc.c:603
Tls13PskBinder
Definition: tls13_misc.h:250
uint32_t dataLen
Definition: sftp_common.h:229
Tls13KeyShareList
Definition: tls13_misc.h:206
Tls13Ticket
Definition: tls13_misc.h:349
uint32_t systime_t
System time.
error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare, size_t length)
(EC)DHE shared secret generation
Definition: tls13_misc.c:402
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
bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given ECDHE group is supported.
Definition: tls13_misc.c:885
uint8_t sessionIdLen
Definition: tls13_misc.h:295
uint8_t extensions[]
Definition: tls13_misc.h:314
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls13_misc.h:359
uint8_t value[]
Definition: tls13_misc.h:182
uint8_t random[32]
Definition: tls13_misc.h:294
Tls13NewSessionTicket
Definition: tls13_misc.h:328
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:770
bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given FFDHE group is supported.
Definition: tls13_misc.c:845
@ TLS_KEY_UPDATE_REQUESTED
Definition: tls13_misc.h:163
Tls13DigitalSignature
Definition: tls13_misc.h:284
Common interface for key encapsulation mechanisms (KEM)
Definition: crypto.h:1087
Tls13PskKeModeList
Definition: tls13_misc.h:217
const uint8_t tls11DowngradeRandom[8]
Definition: tls13_misc.c:53
const uint8_t tls12DowngradeRandom[8]
Definition: tls13_misc.c:59
void * Tls13EndOfEarlyData
EndOfEarlyData message.
Definition: tls13_misc.h:304
Tls13CertRequestContext
Definition: tls13_misc.h:272
@ TLS_KEY_UPDATE_NOT_REQUESTED
Definition: tls13_misc.h:162
bool_t tls13IsHybridKeMethodSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given hybrid key exchange method is supported.
Definition: tls13_misc.c:943
Tls13PskIdentity
Definition: tls13_misc.h:228
Tls13PskIdentityList
Definition: tls13_misc.h:239
uint32_t ticketAgeAdd
Random value used to obscure the age of the ticket.
Definition: tls13_misc.h:325
#define TlsEncryptionEngine
Definition: tls.h:40
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:1106
error_t tls13FormatCertExtensions(uint8_t *p, size_t *written)
Format certificate extensions.
Definition: tls13_misc.c:1152
const uint8_t tls13HelloRetryRequestRandom[32]
Definition: tls13_misc.c:65
Tls13EncryptedExtensions
Definition: tls13_misc.h:315