tls13_server_extensions.c
Go to the documentation of this file.
1 /**
2  * @file tls13_server_extensions.c
3  * @brief Formatting and parsing of extensions (TLS 1.3 server)
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 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls.h"
36 #include "tls_misc.h"
38 #include "tls13_server_misc.h"
39 #include "tls13_ticket.h"
40 #include "debug.h"
41 
42 //Check TLS library configuration
43 #if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED && \
44  TLS_MAX_VERSION >= TLS_VERSION_1_3)
45 
46 
47 /**
48  * @brief Format SupportedVersions extension
49  * @param[in] context Pointer to the TLS context
50  * @param[in] p Output stream where to write the SupportedVersions extension
51  * @param[out] written Total number of bytes that have been written
52  * @return Error code
53  **/
54 
56  uint8_t *p, size_t *written)
57 {
58  size_t n;
59  uint16_t version;
60  TlsExtension *extension;
61 
62  //Add the SupportedVersions extension
63  extension = (TlsExtension *) p;
64  //Type of the extension
65  extension->type = HTONS(TLS_EXT_SUPPORTED_VERSIONS);
66 
67 #if (DTLS_SUPPORT == ENABLED)
68  //DTLS protocol?
69  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
70  {
71  //Retrieve the selected DTLS version
72  version = dtlsTranslateVersion(context->version);
73  }
74  else
75 #endif
76  {
77  //Retrieve the selected TLS version
78  version = context->version;
79  }
80 
81  //The extension contains the selected version value
82  STORE16BE(version, extension->value);
83 
84  //The extension data field contains a 16-bit unsigned integer
85  n = sizeof(uint16_t);
86  //Fix the length of the extension
87  extension->length = htons(n);
88 
89  //Compute the length, in bytes, of the SupportedVersions extension
90  n += sizeof(TlsExtension);
91 
92  //Total number of bytes that have been written
93  *written = n;
94 
95  //Successful processing
96  return NO_ERROR;
97 }
98 
99 
100 /**
101  * @brief Format KeyShare extension (HelloRetryRequest message)
102  * @param[in] context Pointer to the TLS context
103  * @param[in] p Output stream where to write the KeyShare extension
104  * @param[out] written Total number of bytes that have been written
105  * @return Error code
106  **/
107 
109  uint8_t *p, size_t *written)
110 {
111  size_t n = 0;
112 
113 #if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
114  TLS13_ECDHE_KE_SUPPORT == ENABLED || TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
115  //Check whether the selected ECDHE or FFDHE group is valid
116  if(context->namedGroup != TLS_GROUP_NONE)
117  {
118  TlsExtension *extension;
119 
120  //Add the KeyShare extension
121  extension = (TlsExtension *) p;
122  //Type of the extension
123  extension->type = HTONS(TLS_EXT_KEY_SHARE);
124 
125  //The extension contains the mutually supported group the server intends
126  //to negotiate
127  STORE16BE(context->namedGroup, extension->value);
128 
129  //The extension data field contains a 16-bit unsigned integer
130  n = sizeof(uint16_t);
131  //Fix the length of the extension
132  extension->length = htons(n);
133 
134  //Compute the length, in bytes, of the KeyShare extension
135  n += sizeof(TlsExtension);
136  }
137 #endif
138 
139  //Total number of bytes that have been written
140  *written = n;
141 
142  //Successful processing
143  return NO_ERROR;
144 }
145 
146 
147 /**
148  * @brief Format KeyShare extension (ServerHello message)
149  * @param[in] context Pointer to the TLS context
150  * @param[in] p Output stream where to write the KeyShare extension
151  * @param[out] written Total number of bytes that have been written
152  * @return Error code
153  **/
154 
156  uint8_t *p, size_t *written)
157 {
158  size_t n = 0;
159 
160 #if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
161  TLS13_ECDHE_KE_SUPPORT == ENABLED || TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
162  //If using (EC)DHE key establishment, servers offer exactly one
163  //KeyShareEntry in the ServerHello
164  if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
165  context->keyExchMethod == TLS13_KEY_EXCH_ECDHE ||
166  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
167  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
168  {
169  error_t error;
170  TlsExtension *extension;
171  Tls13KeyShareEntry *keyShareEntry;
172 
173  //Add the KeyShare extension
174  extension = (TlsExtension *) p;
175  //Type of the extension
176  extension->type = HTONS(TLS_EXT_KEY_SHARE);
177 
178  //If using (EC)DHE key establishment, servers offer exactly one
179  //KeyShareEntry in the ServerHello
180  keyShareEntry = (Tls13KeyShareEntry *) extension->value;
181 
182  //The key share value must be in the same group as the KeyShareEntry
183  //value offered by the client that the server has selected for the
184  //negotiated key exchange
185  keyShareEntry->group = htons(context->namedGroup);
186 
187 #if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED)
188  //DHE key exchange method?
189  if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
190  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE)
191  {
192  //Retrieve the length of the modulus
193  n = mpiGetByteLength(&context->dhContext.params.p);
194 
195  //Diffie-Hellman parameters are encoded in the opaque key_exchange field
196  //of the KeyShareEntry. The opaque value contains the Diffie-Hellman
197  //public value for the specified group encoded as a big-endian integer
198  //and padded to the left with zeros to the size of p in bytes
199  error = mpiExport(&context->dhContext.ya,
200  keyShareEntry->keyExchange, n, MPI_FORMAT_BIG_ENDIAN);
201  //Any error to report?
202  if(error)
203  return error;
204  }
205  else
206 #endif
207 #if (TLS13_ECDHE_KE_SUPPORT == ENABLED || TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
208  //ECDHE key exchange method?
209  if(context->keyExchMethod == TLS13_KEY_EXCH_ECDHE ||
210  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
211  {
212  //ECDHE parameters are encoded in the opaque key_exchange field of
213  //the KeyShareEntry
214  error = ecExport(&context->ecdhContext.params,
215  &context->ecdhContext.qa.q, keyShareEntry->keyExchange, &n);
216  //Any error to report?
217  if(error)
218  return error;
219  }
220  else
221 #endif
222  //Unknown key exchange method?
223  {
224  //Report an error
225  return ERROR_FAILURE;
226  }
227 
228  //Set the length of the key_exchange field
229  keyShareEntry->length = htons(n);
230 
231  //Compute the length, in bytes, of the KeyShareEntry
232  n += sizeof(Tls13KeyShareEntry);
233  //Fix the length of the extension
234  extension->length = htons(n);
235 
236  //Compute the length, in bytes, of the KeyShare extension
237  n += sizeof(TlsExtension);
238  }
239 #endif
240 
241  //Total number of bytes that have been written
242  *written = n;
243 
244  //Successful processing
245  return NO_ERROR;
246 }
247 
248 
249 /**
250  * @brief Format PreSharedKey extension
251  * @param[in] context Pointer to the TLS context
252  * @param[in] p Output stream where to write the PreSharedKey extension
253  * @param[out] written Total number of bytes that have been written
254  * @return Error code
255  **/
256 
258  uint8_t *p, size_t *written)
259 {
260  size_t n = 0;
261 
262 #if (TLS13_PSK_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
263  TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
264  //PSK key exchange method?
265  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
266  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
267  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
268  {
269  TlsExtension *extension;
270 
271  //Add the PreSharedKey extension
272  extension = (TlsExtension *) p;
273  //Type of the extension
274  extension->type = HTONS(TLS_EXT_PRE_SHARED_KEY);
275 
276  //The extension contains the selected identity
277  STORE16BE(context->selectedIdentity, extension->value);
278 
279  //The extension data field contains a 16-bit unsigned integer
280  n = sizeof(uint16_t);
281  //Fix the length of the extension
282  extension->length = htons(n);
283 
284  //Compute the length, in bytes, of the PreSharedKey extension
285  n += sizeof(TlsExtension);
286  }
287 #endif
288 
289  //Total number of bytes that have been written
290  *written = n;
291 
292  //Successful processing
293  return NO_ERROR;
294 }
295 
296 
297 /**
298  * @brief Format EarlyData extension
299  * @param[in] context Pointer to the TLS context
300  * @param[in] msgType Handshake message type
301  * @param[in] p Output stream where to write the EarlyData extension
302  * @param[out] written Total number of bytes that have been written
303  * @return Error code
304  **/
305 
307  TlsMessageType msgType, uint8_t *p, size_t *written)
308 {
309  size_t n = 0;
310 
311 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
312  //The extension may appear in EncryptedExtensions and NewSessionTicket
313  //messages
315  {
316  //If the server intends to process the early data, then it returns its
317  //own EarlyData extension in EncryptedExtensions
318  if(context->earlyDataExtReceived && !context->earlyDataRejected)
319  {
320  TlsExtension *extension;
321 
322  //Add the EarlyData extension
323  extension = (TlsExtension *) p;
324  //Type of the extension
325  extension->type = HTONS(TLS_EXT_EARLY_DATA);
326 
327  //The extension data field of this extension is empty
328  extension->length = HTONS(0);
329 
330  //Compute the length, in bytes, of the EarlyData extension
331  n = sizeof(TlsExtension);
332  }
333  }
335  {
336  TlsExtension *extension;
337 
338  //Add the EarlyData extension
339  extension = (TlsExtension *) p;
340  //Type of the extension
341  extension->type = HTONS(TLS_EXT_EARLY_DATA);
342 
343  //The extension contains the maximum amount of 0-RTT data that the
344  //client is allowed to send when using this ticket, in bytes
345  STORE32BE(context->maxEarlyDataSize, extension->value);
346 
347  //The extension data field contains a 32-bit unsigned integer
348  n = sizeof(uint32_t);
349  //Fix the length of the extension
350  extension->length = htons(n);
351 
352  //Compute the length, in bytes, of the EarlyData extension
353  n += sizeof(TlsExtension);
354  }
355  else
356  {
357  //Just for sanity
358  }
359 #endif
360 
361  //Total number of bytes that have been written
362  *written = n;
363 
364  //Successful processing
365  return NO_ERROR;
366 }
367 
368 
369 /**
370  * @brief Parse KeyShare extension
371  * @param[in] context Pointer to the TLS context
372  * @param[in] keyShareList Pointer to the KeyShare extension
373  * @param[in] groupList Pointer to the SupportedGroups extension
374  * @return Error code
375  **/
376 
378  const Tls13KeyShareList *keyShareList,
379  const TlsSupportedGroupList *groupList)
380 {
381 #if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
382  TLS13_ECDHE_KE_SUPPORT == ENABLED || TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
383  //KeyShare extension found?
384  if(keyShareList != NULL)
385  {
386  error_t error;
387  bool_t acceptable;
388  size_t n;
389  size_t length;
390  const uint8_t *p;
391  const Tls13KeyShareEntry *keyShareEntry;
392 
393  //Initialize variables
394  acceptable = FALSE;
395  keyShareEntry = NULL;
396 
397  //Point to the first KeyShareEntry of the list
398  p = keyShareList->value;
399  //Retrieve the length of the list
400  length = ntohs(keyShareList->length);
401 
402  //The extension contains a list of KeyShareEntry values offered by the
403  //client. The values are indicated in descending order of preference
404  while(length > 0 && !acceptable)
405  {
406  //Malformed extension?
407  if(length < sizeof(Tls13KeyShareEntry))
408  return ERROR_DECODING_FAILED;
409 
410  //Point to the current key share entry
411  keyShareEntry = (Tls13KeyShareEntry *) p;
412  //Retrieve the length of the key_exchange field
413  n = ntohs(keyShareEntry->length);
414 
415  //Malformed extension?
416  if(length < (sizeof(Tls13KeyShareEntry) + n))
417  return ERROR_DECODING_FAILED;
418 
419  //Each KeyShareEntry value must correspond to a group offered in the
420  //SupportedGroups extension (refer to RFC 8446, section 4.2.8)
421  if(!tls13IsGroupOffered(ntohs(keyShareEntry->group), groupList))
423 
424  //Initial or updated ClientHello?
425  if(context->state == TLS_STATE_CLIENT_HELLO)
426  {
427  //Check whether the ECDHE or FFDHE group is supported
428  acceptable = tls13IsGroupSupported(context,
429  ntohs(keyShareEntry->group));
430  }
431  else
432  {
433  //If the server has sent a HelloRetryRequest, the client needs
434  //to restart the handshake with an appropriate group
435  if(ntohs(keyShareEntry->group) == context->namedGroup)
436  {
437  //Check whether the ECDHE or FFDHE group is supported
438  acceptable = tls13IsGroupSupported(context,
439  ntohs(keyShareEntry->group));
440  }
441  }
442 
443  //Point to the next entry
444  p += sizeof(Tls13KeyShareEntry) + n;
445  //Remaining bytes to process
446  length -= sizeof(Tls13KeyShareEntry) + n;
447  }
448 
449  //Acceptable ECDHE or FFDHE group found?
450  if(acceptable)
451  {
452  //Generate an ephemeral key pair
453  error = tls13GenerateKeyShare(context, ntohs(keyShareEntry->group));
454  //Any error to report?
455  if(error)
456  return error;
457 
458  //Compute (EC)DHE shared secret
459  error = tls13GenerateSharedSecret(context, keyShareEntry->keyExchange,
460  ntohs(keyShareEntry->length));
461  //Any error to report?
462  if(error)
463  return error;
464 
465  //Elliptic curve group?
466  if(tls13IsEcdheGroupSupported(context, context->namedGroup))
467  {
468  //ECDHE key exchange mechanism provides forward secrecy
469  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
470  {
471  context->keyExchMethod = TLS13_KEY_EXCH_PSK_ECDHE;
472  }
473  else
474  {
475  context->keyExchMethod = TLS13_KEY_EXCH_ECDHE;
476  }
477  }
478  //Finite field group?
479  else if(tls13IsFfdheGroupSupported(context, context->namedGroup))
480  {
481  //DHE key exchange mechanism provides forward secrecy
482  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
483  {
484  context->keyExchMethod = TLS13_KEY_EXCH_PSK_DHE;
485  }
486  else
487  {
488  context->keyExchMethod = TLS13_KEY_EXCH_DHE;
489  }
490  }
491  //Unknown group?
492  else
493  {
494  //Just for sanity
495  return ERROR_FAILURE;
496  }
497  }
498  else
499  {
500  //If no common cryptographic parameters can be negotiated, the server
501  //must abort the handshake with an appropriate alert
502  if(context->state == TLS_STATE_CLIENT_HELLO_2)
503  return ERROR_HANDSHAKE_FAILED;
504  }
505  }
506 #endif
507 
508  //Successful processing
509  return NO_ERROR;
510 }
511 
512 
513 /**
514  * @brief Parse PskKeyExchangeModes extension
515  * @param[in] context Pointer to the TLS context
516  * @param[in] pskKeModeList Pointer to the PskKeyExchangeModes extension
517  * @return Error code
518  **/
519 
521  const Tls13PskKeModeList *pskKeModeList)
522 {
523  error_t error;
524  uint_t i;
525 
526  //PskKeyExchangeModes extension found?
527  if(pskKeModeList != NULL)
528  {
529  //Check whether the client supports session resumption with a PSK
530  for(i = 0; i < pskKeModeList->length; i++)
531  {
532  //PSK key establishment supported?
533  if(pskKeModeList->value[i] == TLS_PSK_KEY_EXCH_MODE_PSK_KE ||
534  pskKeModeList->value[i] == TLS_PSK_KEY_EXCH_MODE_PSK_DHE_KE)
535  {
536  context->pskKeModeSupported = TRUE;
537  }
538  }
539  }
540 
541  //PSK key exchange method?
542  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
543  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
544  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
545  {
546  //PskKeyExchangeModes extension found?
547  if(pskKeModeList != NULL)
548  {
549  //Initialize status code
550  error = ERROR_HANDSHAKE_FAILED;
551 
552  //The extension contains a list of supported PSK key exchange modes
553  for(i = 0; i < pskKeModeList->length && error; i++)
554  {
555 #if (TLS13_PSK_KE_SUPPORT == ENABLED)
556  //PSK-only key establishment?
557  if(pskKeModeList->value[i] == TLS_PSK_KEY_EXCH_MODE_PSK_KE)
558  {
559  //Servers must not select a key exchange mode that is not listed
560  //by the client
561  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
562  {
563  error = NO_ERROR;
564  }
565  }
566  else
567 #endif
568 #if (TLS13_PSK_DHE_KE_SUPPORT == ENABLED || TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
569  //PSK with (EC)DHE key establishment?
570  if(pskKeModeList->value[i] == TLS_PSK_KEY_EXCH_MODE_PSK_DHE_KE)
571  {
572  //Servers must not select a key exchange mode that is not listed
573  //by the client
574  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
575  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
576  {
577  error = NO_ERROR;
578  }
579  }
580  else
581 #endif
582  //Unknown key exchange method?
583  {
584  //Just for sanity
585  }
586  }
587  }
588  else
589  {
590  //A client must provide a PskKeyExchangeModes extension if it offers a
591  //PreSharedKey extension
592  error = ERROR_MISSING_EXTENSION;
593  }
594  }
595  else
596  {
597  //If no acceptable PSKs are found, the server should perform a non-PSK
598  //handshake if possible
599  error = NO_ERROR;
600  }
601 
602  //Return status code
603  return error;
604 }
605 
606 
607 /**
608  * @brief Parse PreSharedKey extension
609  * @param[in] context Pointer to the TLS context
610  * @param[in] clientHello Pointer to the ClientHello message
611  * @param[in] clientHelloLen Length of the ClientHello message
612  * @param[in] identityList List of the identities that the client is willing
613  * to negotiate with the server
614  * @param[in] binderList List of HMAC values, one for each PSK offered in the
615  * PreSharedKey extension
616  * @return Error code
617  **/
618 
620  const TlsClientHello *clientHello, size_t clientHelloLen,
621  const Tls13PskIdentityList *identityList, const Tls13PskBinderList *binderList)
622 {
623  #if (TLS13_PSK_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
624  TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
625  //PreSharedKey extension found?
626  if(identityList != NULL && binderList != NULL)
627  {
628  error_t error;
629  int_t i;
630  size_t n;
631  size_t m;
632  uint32_t obfuscatedTicketAge;
633  const uint8_t *p;
634  const uint8_t *q;
635  const Tls13PskIdentity *identity;
636  const Tls13PskBinder *binder;
637  const HashAlgo *hashAlgo;
638 
639  //Initialize variables
640  identity = NULL;
641  binder = NULL;
642 
643  //Reset the server's chosen identity to its default value
644  context->selectedIdentity = -1;
645 
646  //Debug message
647  TRACE_DEBUG("PSK identity list:\r\n");
648  TRACE_DEBUG_ARRAY(" ", identityList, ntohs(identityList->length) + 2);
649  TRACE_DEBUG("PSK binder list:\r\n");
650  TRACE_DEBUG_ARRAY(" ", binderList, ntohs(binderList->length) + 2);
651 
652  //Point to the list of the identities that the client is willing to
653  //negotiate with the server
654  p = identityList->value;
655  n = ntohs(identityList->length);
656 
657  //Point to the list of HMAC values, one for each PSK offered in the
658  //PreSharedKey extension
659  q = binderList->value;
660  m = ntohs(binderList->length);
661 
662  //Loop through the list of PSK identities
663  for(i = 0; n > 0; i++)
664  {
665  //Point to the current PskIdentity entry
666  identity = (Tls13PskIdentity *) p;
667 
668  //Malformed PreSharedKey extension?
669  if(n < sizeof(TlsPskIdentity))
670  return ERROR_DECODING_FAILED;
671  if(n < (sizeof(TlsPskIdentity) + ntohs(identity->length)))
672  return ERROR_DECODING_FAILED;
673 
674  //Debug message
675  TRACE_DEBUG("PSK identity #%u:\r\n", i);
676  TRACE_DEBUG_ARRAY(" ", identity->value, ntohs(identity->length));
677 
678  //Point to the obfuscated_ticket_age field
679  p += sizeof(TlsPskIdentity) + ntohs(identity->length);
680  n -= sizeof(TlsPskIdentity) + ntohs(identity->length);
681 
682  //Malformed PreSharedKey extension?
683  if(n < sizeof(uint32_t))
684  return ERROR_DECODING_FAILED;
685 
686  //The obfuscated_ticket_age field is a 32-bit unsigned integer
687  obfuscatedTicketAge = LOAD32BE(p);
688 
689  //Point to the next PskIdentity entry
690  p += sizeof(uint32_t);
691  n -= sizeof(uint32_t);
692 
693  //Point to the PskBinderEntry
694  binder = (Tls13PskBinder *) q;
695 
696  //If the binder is not present, the server must abort the handshake
697  if(context->selectedIdentity >= 0 && m == 0)
699 
700  //Malformed PreSharedKey extension?
701  if(m < sizeof(Tls13PskBinder))
702  return ERROR_DECODING_FAILED;
703  if(m < (sizeof(Tls13PskBinder) + binder->length))
704  return ERROR_DECODING_FAILED;
705 
706  //Debug message
707  TRACE_DEBUG("PSK binder #%u:\r\n", i);
708  TRACE_DEBUG_ARRAY(" ", binder->value, binder->length);
709 
710  //Point to the next PskBinderEntry
711  q += sizeof(Tls13PskBinder) + binder->length;
712  m -= sizeof(Tls13PskBinder) + binder->length;
713 
714  //The server should select a single PSK
715  if(context->selectedIdentity < 0)
716  {
717  //Any registered callback?
718  if(context->pskCallback != NULL)
719  {
720  //Check whether the PSK identity provided by the client matches
721  //any externally established PSK
722  error = context->pskCallback(context, identity->value,
723  ntohs(identity->length));
724 
725  //Valid PSK?
726  if(!error && tls13IsPskValid(context))
727  {
728  //For externally established PSKs, the hash algorithm must be
729  //set when the PSK is established
730  hashAlgo = tlsGetHashAlgo(context->pskHashAlgo);
731 
732  //Make sure the hash algorithm is valid
733  if(hashAlgo != NULL)
734  {
735  //Save the hash algorithm associated with the PSK
736  context->cipherSuite.prfHashAlgo = hashAlgo;
737 
738  //The server's chosen identity is expressed as a 0-based
739  //index into the identities in the client's list
740  context->selectedIdentity = i;
741  }
742  }
743  }
744  }
745 
746  //The server should select a single PSK
747  if(context->selectedIdentity < 0)
748  {
749  //Decrypt the received ticket and verify the ticket's validity
750  error = tls13VerifyTicket(context, identity->value,
751  htons(identity->length), obfuscatedTicketAge);
752 
753  //Valid ticket?
754  if(!error)
755  {
756  //The server's chosen identity is expressed as a 0-based index
757  //into the identities in the client's list
758  context->selectedIdentity = i;
759  }
760  }
761  }
762 
763  //Extra binders found?
764  if(m != 0)
765  {
767  }
768  }
769  else
770 #endif
771  {
772  //Initial or updated ClientHello?
773  if(context->state == TLS_STATE_CLIENT_HELLO_2)
774  {
775  //When responding to a HelloRetryRequest, the client must send the
776  //same ClientHello without modification
777  if(context->selectedIdentity >= 0)
778  {
780  }
781  }
782 
783  //The ClientHello message does not contain any PreSharedKey extension
784  context->selectedIdentity = -1;
785  }
786 
787  //Successful processing
788  return NO_ERROR;
789 }
790 
791 
792 /**
793  * @brief Parse EarlyData extension
794  * @param[in] context Pointer to the TLS context
795  * @param[in] earlyDataIndication Pointer to the EarlyData extension
796  * @return Error code
797  **/
798 
800  const TlsExtension *earlyDataIndication)
801 {
802  //EarlyData extension found?
803  if(earlyDataIndication != NULL)
804  {
805  //Early data is not permitted after a HelloRetryRequest (refer to
806  //RFC 8446, section 4.1.2)
807  if(context->state == TLS_STATE_CLIENT_HELLO_2)
808  {
809  context->earlyDataRejected = TRUE;
810  }
811 
812  //In order to accept early data, the server must have accepted a PSK
813  //cipher suite and selected the first key offered in the client's
814  //PreSharedKey extension (refer to RFC 8446, section 4.2.10)
815  if(context->selectedIdentity != 0)
816  {
817  context->earlyDataRejected = TRUE;
818  }
819 
820  //A valid EarlyData extension has been received
821  context->earlyDataExtReceived = TRUE;
822  }
823  else
824  {
825  //The ClientHello message does not contain any EarlyData extension
826  context->earlyDataExtReceived = FALSE;
827  }
828 
829  //Successful processing
830  return NO_ERROR;
831 }
832 
833 #endif
uint8_t version
Definition: coap_common.h:177
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
#define HTONS(value)
Definition: cpu_endian.h:410
#define LOAD32BE(p)
Definition: cpu_endian.h:210
#define htons(value)
Definition: cpu_endian.h:413
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
#define STORE16BE(a, p)
Definition: cpu_endian.h:262
#define ntohs(value)
Definition: cpu_endian.h:421
Debugging facilities.
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint8_t n
uint16_t dtlsTranslateVersion(uint16_t version)
Translate TLS version into DTLS version.
Definition: dtls_misc.c:112
error_t ecExport(const EcDomainParameters *params, const EcPoint *a, uint8_t *data, size_t *length)
Convert an EC point to an octet string.
Definition: ec.c:438
error_t
Error codes.
Definition: error.h:43
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:242
@ ERROR_MISSING_EXTENSION
Definition: error.h:243
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:232
@ ERROR_DECODING_FAILED
Definition: error.h:240
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:195
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:709
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:71
uint8_t msgType
uint8_t p
Definition: ndp.h:300
uint8_t m
Definition: ndp.h:304
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
Common interface for hash algorithms.
Definition: crypto.h:1014
uint8_t length
Definition: tcp.h:368
bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given ECDHE group is supported.
Definition: tls13_misc.c:649
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
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:538
bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given named group is supported.
Definition: tls13_misc.c:576
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:260
Tls13PskBinderList
Definition: tls13_misc.h:247
Tls13KeyShareList
Definition: tls13_misc.h:192
Tls13KeyShareEntry
Definition: tls13_misc.h:181
@ 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
Tls13PskIdentityList
Definition: tls13_misc.h:225
Tls13PskBinder
Definition: tls13_misc.h:236
Tls13PskIdentity
Definition: tls13_misc.h:214
Tls13PskKeModeList
Definition: tls13_misc.h:203
error_t tls13ParseClientPreSharedKeyExtension(TlsContext *context, const TlsClientHello *clientHello, size_t clientHelloLen, const Tls13PskIdentityList *identityList, const Tls13PskBinderList *binderList)
Parse PreSharedKey extension.
error_t tls13ParseClientEarlyDataExtension(TlsContext *context, const TlsExtension *earlyDataIndication)
Parse EarlyData extension.
error_t tls13FormatSelectedGroupExtension(TlsContext *context, uint8_t *p, size_t *written)
Format KeyShare extension (HelloRetryRequest message)
error_t tls13FormatServerEarlyDataExtension(TlsContext *context, TlsMessageType msgType, uint8_t *p, size_t *written)
Format EarlyData extension.
error_t tls13ParseClientKeyShareExtension(TlsContext *context, const Tls13KeyShareList *keyShareList, const TlsSupportedGroupList *groupList)
Parse KeyShare extension.
error_t tls13ParsePskKeModesExtension(TlsContext *context, const Tls13PskKeModeList *pskKeModeList)
Parse PskKeyExchangeModes extension.
error_t tls13FormatServerKeyShareExtension(TlsContext *context, uint8_t *p, size_t *written)
Format KeyShare extension (ServerHello message)
error_t tls13FormatServerPreSharedKeyExtension(TlsContext *context, uint8_t *p, size_t *written)
Format PreSharedKey extension.
error_t tls13FormatServerSupportedVersionsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedVersions extension.
Formatting and parsing of extensions (TLS 1.3 server)
bool_t tls13IsGroupOffered(uint16_t namedGroup, const TlsSupportedGroupList *groupList)
Check whether a group is offered in the SupportedGroups extension.
Helper functions for TLS 1.3 server.
error_t tls13VerifyTicket(TlsContext *context, const uint8_t *ticket, size_t length, uint32_t obfuscatedTicketAge)
Session ticket verification.
Definition: tls13_ticket.c:377
TLS 1.3 session tickets.
TLS (Transport Layer Security)
TlsClientHello
Definition: tls.h:1757
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1440
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1439
@ TLS13_KEY_EXCH_PSK_DHE
Definition: tls.h:1146
@ TLS13_KEY_EXCH_ECDHE
Definition: tls.h:1144
@ TLS13_KEY_EXCH_PSK
Definition: tls.h:1145
@ TLS13_KEY_EXCH_DHE
Definition: tls.h:1143
@ TLS13_KEY_EXCH_PSK_ECDHE
Definition: tls.h:1147
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:942
TlsExtension
Definition: tls.h:1556
TlsPskIdentity
Definition: tls.h:1678
@ TLS_EXT_EARLY_DATA
Definition: tls.h:1304
@ TLS_EXT_PRE_SHARED_KEY
Definition: tls.h:1303
@ TLS_EXT_KEY_SHARE
Definition: tls.h:1312
@ TLS_EXT_SUPPORTED_VERSIONS
Definition: tls.h:1305
TlsSupportedGroupList
Definition: tls.h:1634
#define TlsContext
Definition: tls.h:36
@ TLS_GROUP_NONE
Definition: tls.h:1353
TlsMessageType
Handshake message type.
Definition: tls.h:1024
@ TLS_TYPE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1032
@ TLS_TYPE_NEW_SESSION_TICKET
Definition: tls.h:1029
const HashAlgo * tlsGetHashAlgo(TlsHashAlgo hashAlgoId)
Get the hash algorithm that matches the specified identifier.
Definition: tls_misc.c:1173
TLS helper functions.