tls_server.c
Go to the documentation of this file.
1 /**
2  * @file tls_server.c
3  * @brief Handshake message processing (TLS server)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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  * @section Description
28  *
29  * The TLS protocol provides communications security over the Internet. The
30  * protocol allows client/server applications to communicate in a way that
31  * is designed to prevent eavesdropping, tampering, or message forgery
32  *
33  * @author Oryx Embedded SARL (www.oryx-embedded.com)
34  * @version 2.5.0
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL TLS_TRACE_LEVEL
39 
40 //Dependencies
41 #include "tls.h"
42 #include "tls_cipher_suites.h"
43 #include "tls_handshake.h"
44 #include "tls_server.h"
45 #include "tls_server_extensions.h"
46 #include "tls_server_misc.h"
47 #include "tls_common.h"
48 #include "tls_extensions.h"
49 #include "tls_sign_misc.h"
50 #include "tls_key_material.h"
51 #include "tls_transcript_hash.h"
52 #include "tls_cache.h"
53 #include "tls_ffdhe.h"
54 #include "tls_record.h"
55 #include "tls_misc.h"
56 #include "tls13_server.h"
58 #include "tls13_server_misc.h"
59 #include "dtls_record.h"
60 #include "dtls_misc.h"
61 #include "pkix/pem_import.h"
62 #include "pkix/x509_cert_parse.h"
63 #include "date_time.h"
64 #include "debug.h"
65 
66 //Check TLS library configuration
67 #if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED)
68 
69 
70 /**
71  * @brief Send ServerHello message
72  *
73  * The server will send this message in response to a ClientHello
74  * message when it was able to find an acceptable set of algorithms.
75  * If it cannot find such a match, it will respond with a handshake
76  * failure alert
77  *
78  * @param[in] context Pointer to the TLS context
79  * @return Error code
80  **/
81 
83 {
84  error_t error;
85  size_t length;
87 
88  //Point to the buffer where to format the message
89  message = (TlsServerHello *) (context->txBuffer + context->txBufferLen);
90 
91  //Generate the server random value using a cryptographically-safe
92  //pseudorandom number generator
93  error = tlsGenerateRandomValue(context, context->serverRandom);
94 
95  //Check status code
96  if(!error)
97  {
98  //Format ServerHello message
99  error = tlsFormatServerHello(context, message, &length);
100  }
101 
102  //Check status code
103  if(!error)
104  {
105  //Debug message
106  TRACE_INFO("Sending ServerHello message (%" PRIuSIZE " bytes)...\r\n", length);
108 
109  //Send handshake message
110  error = tlsSendHandshakeMessage(context, message, length,
112  }
113 
114  //Check status code
115  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
116  {
117  //Version of TLS prior to TLS 1.3?
118  if(context->version <= TLS_VERSION_1_2)
119  {
120 #if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
121  //Use abbreviated handshake?
122  if(context->resume)
123  {
124  //Derive session keys from the master secret
125  error = tlsGenerateSessionKeys(context);
126 
127  //Key material successfully generated?
128  if(!error)
129  {
130 #if (TLS_TICKET_SUPPORT == ENABLED)
131  //The server uses a zero-length SessionTicket extension to
132  //indicate to the client that it will send a new session ticket
133  //using the NewSessionTicket handshake message
134  if(context->sessionTicketExtSent)
135  {
136  //Send a NewSessionTicket message to the client
138  }
139  else
140 #endif
141  {
142  //At this point, both client and server must send ChangeCipherSpec
143  //messages and proceed directly to Finished messages
145  }
146  }
147  }
148  else
149 #endif
150  {
151  //Perform a full handshake
153  }
154  }
155  else
156  {
157 #if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
158  //First handshake message sent by the server?
159  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
160  context->state == TLS_STATE_SERVER_HELLO)
161  {
162  //In middlebox compatibility mode, the server must send a dummy
163  //ChangeCipherSpec record immediately after its first handshake
164  //message
166  }
167  else
168 #endif
169  {
170  //All handshake messages after the ServerHello are now encrypted
172  }
173  }
174  }
175 
176  //Return status code
177  return error;
178 }
179 
180 
181 /**
182  * @brief Send ServerKeyExchange message
183  *
184  * The ServerKeyExchange message is sent by the server only when the
185  * server Certificate message does not contain enough data to allow
186  * the client to exchange a premaster secret
187  *
188  * @param[in] context Pointer to the TLS context
189  * @return Error code
190  **/
191 
193 {
194  error_t error;
195  size_t length;
197 
198  //Initialize status code
199  error = NO_ERROR;
200 
201  //Point to the buffer where to format the message
202  message = (TlsServerKeyExchange *) (context->txBuffer + context->txBufferLen);
203  //Initialize length
204  length = 0;
205 
206  //The ServerKeyExchange message is sent by the server only when the server
207  //Certificate message (if sent) does not contain enough data to allow the
208  //client to exchange a premaster secret
209  if(context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
210  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
211  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
212  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
213  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
214  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
215  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
216  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
217  {
218  //Format ServerKeyExchange message
219  error = tlsFormatServerKeyExchange(context, message, &length);
220  }
221  else if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
222  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
223  {
224 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
225  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
226  //If no PSK identity hint is provided by the server, the
227  //ServerKeyExchange message is omitted...
228  if(context->pskIdentityHint != NULL)
229  {
230  //Format ServerKeyExchange message
231  error = tlsFormatServerKeyExchange(context, message, &length);
232  }
233 #endif
234  }
235 
236  //Check status code
237  if(!error)
238  {
239  //Any message to send?
240  if(length > 0)
241  {
242  //Debug message
243  TRACE_INFO("Sending ServerKeyExchange message (%" PRIuSIZE " bytes)...\r\n", length);
245 
246  //Send handshake message
247  error = tlsSendHandshakeMessage(context, message, length,
249  }
250  }
251 
252  //Check status code
253  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
254  {
255  //A server can optionally request a certificate from the client
257  }
258 
259  //Return status code
260  return error;
261 }
262 
263 
264 /**
265  * @brief Send CertificateRequest message
266  *
267  * A server can optionally request a certificate from the client, if
268  * appropriate for the selected cipher suite. This message will
269  * immediately follow the ServerKeyExchange message
270  *
271  * @param[in] context Pointer to the TLS context
272  * @return Error code
273  **/
274 
276 {
277  error_t error;
278  size_t length;
280 
281  //Initialize status code
282  error = NO_ERROR;
283 
284 #if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED || \
285  TLS_DSA_SIGN_SUPPORT == ENABLED || TLS_ECDSA_SIGN_SUPPORT == ENABLED)
286  //A server can optionally request a certificate from the client
287  if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
288  {
289  //Non-anonymous key exchange?
290  if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
291  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
292  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
293  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
294  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
295  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
296  context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
297  context->keyExchMethod == TLS13_KEY_EXCH_ECDHE ||
298  context->keyExchMethod == TLS13_KEY_EXCH_MLKEM ||
299  context->keyExchMethod == TLS13_KEY_EXCH_HYBRID)
300  {
301  //Point to the buffer where to format the message
302  message = (TlsCertificateRequest *) (context->txBuffer + context->txBufferLen);
303 
304  //Format CertificateRequest message
305  error = tlsFormatCertificateRequest(context, message, &length);
306 
307  //Check status code
308  if(!error)
309  {
310  //Debug message
311  TRACE_INFO("Sending CertificateRequest message (%" PRIuSIZE " bytes)...\r\n", length);
313 
314  //Send handshake message
315  error = tlsSendHandshakeMessage(context, message, length,
317  }
318  }
319  }
320 #endif
321 
322  //Check status code
323  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
324  {
325  //Version of TLS prior to TLS 1.3?
326  if(context->version <= TLS_VERSION_1_2)
327  {
328  //Send a ServerHelloDone message to the client
330  }
331  else
332  {
333  //Send a Certificate message to the client
335  }
336  }
337 
338  //Return status code
339  return error;
340 }
341 
342 
343 /**
344  * @brief Send ServerHelloDone message
345  *
346  * The ServerHelloDone message is sent by the server to indicate the
347  * end of the ServerHello and associated messages. After sending this
348  * message, the server will wait for a client response
349  *
350  * @param[in] context Pointer to the TLS context
351  * @return Error code
352  **/
353 
355 {
356  error_t error;
357  size_t length;
359 
360  //Point to the buffer where to format the message
361  message = (TlsServerHelloDone *) (context->txBuffer + context->txBufferLen);
362 
363  //Format ServerHelloDone message
364  error = tlsFormatServerHelloDone(context, message, &length);
365 
366  //Check status code
367  if(!error)
368  {
369  //Debug message
370  TRACE_INFO("Sending ServerHelloDone message (%" PRIuSIZE " bytes)...\r\n", length);
372 
373  //Send handshake message
374  error = tlsSendHandshakeMessage(context, message, length,
376  }
377 
378  //Check status code
379  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
380  {
381  //The client must send a Certificate message if the server requests it
382  if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
383  {
385  }
386  else
387  {
389  }
390  }
391 
392  //Return status code
393  return error;
394 }
395 
396 
397 /**
398  * @brief Send NewSessionTicket message
399  *
400  * This NewSessionTicket message is sent by the server during the TLS handshake
401  * before the ChangeCipherSpec message
402  *
403  * @param[in] context Pointer to the TLS context
404  * @return Error code
405  **/
406 
408 {
409  error_t error;
410  size_t length;
412 
413  //Point to the buffer where to format the message
414  message = (TlsNewSessionTicket *) (context->txBuffer + context->txBufferLen);
415 
416  //Format NewSessionTicket message
417  error = tlsFormatNewSessionTicket(context, message, &length);
418 
419  //Check status code
420  if(!error)
421  {
422  //Debug message
423  TRACE_INFO("Sending NewSessionTicket message (%" PRIuSIZE " bytes)...\r\n", length);
425 
426  //Send handshake message
427  error = tlsSendHandshakeMessage(context, message, length,
429  }
430 
431  //Check status code
432  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
433  {
434  //The NewSessionTicket message is sent by the server during the TLS
435  //handshake before the ChangeCipherSpec message
437  }
438 
439  //Return status code
440  return error;
441 }
442 
443 
444 /**
445  * @brief Format ServerHello message
446  * @param[in] context Pointer to the TLS context
447  * @param[out] message Buffer where to format the ServerHello message
448  * @param[out] length Length of the resulting ServerHello message
449  * @return Error code
450  **/
451 
453  TlsServerHello *message, size_t *length)
454 {
455  error_t error;
456  uint16_t version;
457  size_t n;
458  uint8_t *p;
459  TlsExtensionList *extensionList;
460 
461  //In TLS 1.3, the client indicates its version preferences in the
462  //SupportedVersions extension and the legacy_version field must be
463  //set to 0x0303, which is the version number for TLS 1.2
464  version = MIN(context->version, TLS_VERSION_1_2);
465 
466 #if (DTLS_SUPPORT == ENABLED)
467  //DTLS protocol?
468  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
469  {
470  //Get the corresponding DTLS version
472  }
473 #endif
474 
475  //In previous versions of TLS, the version field contains the lower of
476  //the version suggested by the client in the ClientHello and the highest
477  //supported by the server
478  message->serverVersion = htons(version);
479 
480  //Server random value
481  osMemcpy(message->random, context->serverRandom, 32);
482 
483  //Point to the session ID
484  p = message->sessionId;
485  //Length of the handshake message
486  *length = sizeof(TlsServerHello);
487 
488  //Version of TLS prior to TLS 1.3?
489  if(context->version <= TLS_VERSION_1_2)
490  {
491 #if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
492  //The session ID uniquely identifies the current session
493  osMemcpy(message->sessionId, context->sessionId, context->sessionIdLen);
494  message->sessionIdLen = (uint8_t) context->sessionIdLen;
495 #else
496  //The server may return an empty session ID to indicate that the session
497  //will not be cached and therefore cannot be resumed
498  message->sessionIdLen = 0;
499 #endif
500  }
501  else
502  {
503  //The legacy_session_id_echo echoes the contents of the client's
504  //legacy_session_id field
505  osMemcpy(message->sessionId, context->sessionId, context->sessionIdLen);
506  message->sessionIdLen = (uint8_t) context->sessionIdLen;
507  }
508 
509  //Debug message
510  TRACE_DEBUG("Session ID (%" PRIu8 " bytes):\r\n", message->sessionIdLen);
511  TRACE_DEBUG_ARRAY(" ", message->sessionId, message->sessionIdLen);
512 
513  //Advance data pointer
514  p += message->sessionIdLen;
515  //Adjust the length of the message
516  *length += message->sessionIdLen;
517 
518  //The cipher_suite field contains the cipher suite selected by the server
519  STORE16BE(context->cipherSuite.identifier, p);
520 
521  //Advance data pointer
522  p += sizeof(uint16_t);
523  //Adjust the length of the message
524  *length += sizeof(uint16_t);
525 
526  //The CRIME exploit takes advantage of TLS compression, so conservative
527  //implementations do not enable compression at the TLS level
529 
530  //Advance data pointer
531  p += sizeof(uint8_t);
532  //Adjust the length of the message
533  *length += sizeof(uint8_t);
534 
535  //Only extensions offered by the client can appear in the server's list
536  extensionList = (TlsExtensionList *) p;
537  //Total length of the extension list
538  extensionList->length = 0;
539 
540  //Point to the first extension of the list
541  p += sizeof(TlsExtensionList);
542 
543 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
544  //TLS 1.0, TLS 1.1 or TLS 1.2 selected by the server?
545  if(context->version <= TLS_VERSION_1_2)
546  {
547 #if (TLS_SNI_SUPPORT == ENABLED)
548  //The server may include a SNI extension in the ServerHello
549  error = tlsFormatServerSniExtension(context, p, &n);
550  //Any error to report?
551  if(error)
552  return error;
553 
554  //Fix the length of the extension list
555  extensionList->length += (uint16_t) n;
556  //Point to the next field
557  p += n;
558 #endif
559 
560 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
561  //Servers that receive an ClientHello containing a MaxFragmentLength
562  //extension may accept the requested maximum fragment length by including
563  //an extension of type MaxFragmentLength in the ServerHello
564  error = tlsFormatServerMaxFragLenExtension(context, p, &n);
565  //Any error to report?
566  if(error)
567  return error;
568 
569  //Fix the length of the extension list
570  extensionList->length += (uint16_t) n;
571  //Point to the next field
572  p += n;
573 #endif
574 
575 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
576  //The value of RecordSizeLimit is the maximum size of record in octets
577  //that the endpoint is willing to receive
578  error = tlsFormatServerRecordSizeLimitExtension(context, p, &n);
579  //Any error to report?
580  if(error)
581  return error;
582 
583  //Fix the length of the extension list
584  extensionList->length += (uint16_t) n;
585  //Point to the next field
586  p += n;
587 #endif
588 
589 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
590  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
591  //A server that selects an ECC cipher suite in response to a ClientHello
592  //message including an EcPointFormats extension appends this extension
593  //to its ServerHello message
594  error = tlsFormatServerEcPointFormatsExtension(context, p, &n);
595  //Any error to report?
596  if(error)
597  return error;
598 
599  //Fix the length of the extension list
600  extensionList->length += (uint16_t) n;
601  //Point to the next field
602  p += n;
603 #endif
604 
605 #if (TLS_ALPN_SUPPORT == ENABLED)
606  //The ALPN extension contains the name of the selected protocol
607  error = tlsFormatServerAlpnExtension(context, p, &n);
608  //Any error to report?
609  if(error)
610  return error;
611 
612  //Fix the length of the extension list
613  extensionList->length += (uint16_t) n;
614  //Point to the next field
615  p += n;
616 #endif
617 
618 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
619  //The ClientCertType extension in the ServerHello indicates the type
620  //of certificates the client is requested to provide in a subsequent
621  //certificate payload
622  error = tlsFormatClientCertTypeExtension(context, p, &n);
623  //Any error to report?
624  if(error)
625  return error;
626 
627  //Fix the length of the extension list
628  extensionList->length += (uint16_t) n;
629  //Point to the next field
630  p += n;
631 
632  //With the ServerCertType extension in the ServerHello, the TLS server
633  //indicates the certificate type carried in the certificate payload
634  error = tlsFormatServerCertTypeExtension(context, p, &n);
635  //Any error to report?
636  if(error)
637  return error;
638 
639  //Fix the length of the extension list
640  extensionList->length += (uint16_t) n;
641  //Point to the next field
642  p += n;
643 #endif
644 
645 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
646  //On connecting, the client includes the EncryptThenMac extension in
647  //its ClientHello if it wishes to use encrypt-then-MAC rather than the
648  //default MAC-then-encrypt. If the server is capable of meeting this
649  //requirement, it responds with an EncryptThenMac in its ServerHello
650  error = tlsFormatServerEtmExtension(context, p, &n);
651  //Any error to report?
652  if(error)
653  return error;
654 
655  //Fix the length of the extension list
656  extensionList->length += (uint16_t) n;
657  //Point to the next field
658  p += n;
659 #endif
660 
661 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
662  //If a server implementing RFC 7627 receives the ExtendedMasterSecret
663  //extension, it must include the extension in its ServerHello message
664  error = tlsFormatServerEmsExtension(context, p, &n);
665  //Any error to report?
666  if(error)
667  return error;
668 
669  //Fix the length of the extension list
670  extensionList->length += (uint16_t) n;
671  //Point to the next field
672  p += n;
673 #endif
674 
675 #if (TLS_TICKET_SUPPORT == ENABLED)
676  //The server uses the SessionTicket extension to indicate to the client
677  //that it will send a new session ticket using the NewSessionTicket
678  //handshake message
679  error = tlsFormatServerSessionTicketExtension(context, p, &n);
680  //Any error to report?
681  if(error)
682  return error;
683 
684  //Fix the length of the extension list
685  extensionList->length += (uint16_t) n;
686  //Point to the next field
687  p += n;
688 #endif
689 
690 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
691  //During secure renegotiation, the server must include a renegotiation_info
692  //extension containing the saved client_verify_data and server_verify_data
693  error = tlsFormatServerRenegoInfoExtension(context, p, &n);
694  //Any error to report?
695  if(error)
696  return error;
697 
698  //Fix the length of the extension list
699  extensionList->length += (uint16_t) n;
700  //Point to the next field
701  p += n;
702 #endif
703  }
704  else
705 #endif
706 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
707  //TLS 1.3 selected by the server?
708  if(context->version == TLS_VERSION_1_3)
709  {
710  //A server which negotiates TLS 1.3 must respond by sending a
711  //SupportedVersions extension containing the selected version value
712  error = tls13FormatServerSupportedVersionsExtension(context, p, &n);
713  //Any error to report?
714  if(error)
715  return error;
716 
717  //Fix the length of the extension list
718  extensionList->length += (uint16_t) n;
719  //Point to the next field
720  p += n;
721 
722  //If using (EC)DHE key establishment, servers offer exactly one
723  //KeyShareEntry in the ServerHello
724  error = tls13FormatServerKeyShareExtension(context, p, &n);
725  //Any error to report?
726  if(error)
727  return error;
728 
729  //Fix the length of the extension list
730  extensionList->length += (uint16_t) n;
731  //Point to the next field
732  p += n;
733 
734  //In order to accept PSK key establishment, the server sends a
735  //PreSharedKey extension indicating the selected identity
736  error = tls13FormatServerPreSharedKeyExtension(context, p, &n);
737  //Any error to report?
738  if(error)
739  return error;
740 
741  //Fix the length of the extension list
742  extensionList->length += (uint16_t) n;
743  //Point to the next field
744  p += n;
745  }
746  else
747 #endif
748  //Invalid TLS version?
749  {
750  //Report an error
751  return ERROR_INVALID_VERSION;
752  }
753 
754  //Any extensions included in the ServerHello message?
755  if(extensionList->length > 0)
756  {
757  //Convert the length of the extension list to network byte order
758  extensionList->length = htons(extensionList->length);
759  //Total length of the message
760  *length += sizeof(TlsExtensionList) + htons(extensionList->length);
761  }
762 
763  //Successful processing
764  return NO_ERROR;
765 }
766 
767 
768 /**
769  * @brief Format ServerKeyExchange message
770  * @param[in] context Pointer to the TLS context
771  * @param[out] message Buffer where to format the ServerKeyExchange message
772  * @param[out] length Length of the resulting ServerKeyExchange message
773  * @return Error code
774  **/
775 
778 {
779  error_t error;
780  size_t n;
781  size_t paramsLen;
782  uint8_t *p;
783  uint8_t *params;
784 
785  //Point to the beginning of the handshake message
786  p = message;
787  //Length of the handshake message
788  *length = 0;
789 
790 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
791  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
792  //PSK key exchange method?
793  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
794  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
795  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
796  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
797  {
798  //To help the client in selecting which identity to use, the server
799  //can provide a PSK identity hint in the ServerKeyExchange message
800  error = tlsFormatPskIdentityHint(context, p, &n);
801  //Any error to report?
802  if(error)
803  return error;
804 
805  //Advance data pointer
806  p += n;
807  //Adjust the length of the message
808  *length += n;
809  }
810 #endif
811 
812  //Diffie-Hellman or ECDH key exchange method?
813  if(context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
814  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
815  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
816  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
817  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
818  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
819  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
820  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
821  {
822  //Point to the server's key exchange parameters
823  params = p;
824 
825  //Format server's key exchange parameters
826  error = tlsFormatServerKeyParams(context, p, &paramsLen);
827  //Any error to report?
828  if(error)
829  return error;
830 
831  //Advance data pointer
832  p += paramsLen;
833  //Adjust the length of the message
834  *length += paramsLen;
835  }
836  else
837  {
838  //Just for sanity
839  params = NULL;
840  paramsLen = 0;
841  }
842 
843  //For non-anonymous Diffie-Hellman and ECDH key exchanges, a signature
844  //over the server's key exchange parameters shall be generated
845  if(context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
846  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
847  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
848  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
849  {
850 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
851  //TLS 1.0 or TLS 1.1 currently selected?
852  if(context->version <= TLS_VERSION_1_1)
853  {
854  //Sign server's key exchange parameters
855  error = tlsGenerateServerKeySignature(context,
856  (TlsDigitalSignature *) p, params, paramsLen, &n);
857  }
858  else
859 #endif
860 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
861  //TLS 1.2 currently selected?
862  if(context->version == TLS_VERSION_1_2)
863  {
864  //Sign server's key exchange parameters
865  error = tls12GenerateServerKeySignature(context,
866  (Tls12DigitalSignature *) p, params, paramsLen, &n);
867  }
868  else
869 #endif
870  {
871  //Report an error
872  error = ERROR_INVALID_VERSION;
873  }
874 
875  //Any error to report?
876  if(error)
877  return error;
878 
879  //Advance data pointer
880  p += n;
881  //Adjust the length of the message
882  *length += n;
883  }
884 
885  //Successful processing
886  return NO_ERROR;
887 }
888 
889 
890 /**
891  * @brief Format CertificateRequest message
892  * @param[in] context Pointer to the TLS context
893  * @param[out] message Buffer where to format the CertificateRequest message
894  * @param[out] length Length of the resulting CertificateRequest message
895  * @return Error code
896  **/
897 
900 {
901  error_t error;
902  size_t n;
903  uint8_t *p;
904 
905  //Initialize status code
906  error = NO_ERROR;
907 
908  //Point to the beginning of the message
909  p = (uint8_t *) message;
910 
911 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
912  //Version of TLS prior to TLS 1.3?
913  if(context->version <= TLS_VERSION_1_2)
914  {
915  //Enumerate the types of certificate types that the client may offer
916  n = 0;
917 
918 #if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
919  //Accept certificates that contain an RSA public key
920  message->certificateTypes[n++] = TLS_CERT_RSA_SIGN;
921 #endif
922 #if (TLS_DSA_SIGN_SUPPORT == ENABLED)
923  //Accept certificates that contain a DSA public key
924  message->certificateTypes[n++] = TLS_CERT_DSS_SIGN;
925 #endif
926 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
927  //Accept certificates that contain an ECDSA public key
928  message->certificateTypes[n++] = TLS_CERT_ECDSA_SIGN;
929 #endif
930 
931  //Fix the length of the list
932  message->certificateTypesLen = (uint8_t) n;
933  //Length of the handshake message
934  *length = sizeof(TlsCertificateRequest) + n;
935 
936  //TLS 1.2 currently selected?
937  if(context->version == TLS_VERSION_1_2)
938  {
939  //The supported_signature_algorithms list contains the hash/signature
940  //algorithm pairs that the server is able to verify. Servers can
941  //minimize the computation cost by offering a restricted set of digest
942  //algorithms
943  error = tlsFormatSupportedSignAlgos(context, p + *length, &n);
944 
945  //Check status code
946  if(!error)
947  {
948  //Adjust the length of the message
949  *length += n;
950  }
951  }
952 
953  //Check status code
954  if(!error)
955  {
956  //The certificate_authorities list contains the distinguished names of
957  //acceptable certificate authorities, represented in DER-encoded format
958  error = tlsFormatCertAuthorities(context, p + *length, &n);
959  }
960 
961  //Check status code
962  if(!error)
963  {
964  //Adjust the length of the message
965  *length += n;
966  }
967  }
968  else
969 #endif
970 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
971  //TLS 1.3 currently selected?
972  if(context->version == TLS_VERSION_1_3)
973  {
974  Tls13CertRequestContext *certRequestContext;
975  TlsExtensionList *extensionList;
976 
977  //Point to the certificate_request_context field
978  certRequestContext = (Tls13CertRequestContext *) p;
979 
980  //The certificate_request_context field shall be zero length unless
981  //used for the post-handshake authentication exchange
982  certRequestContext->length = 0;
983 
984  //Point to the next field
985  p += sizeof(Tls13CertRequestContext);
986  //Length of the handshake message
987  *length = sizeof(Tls13CertRequestContext);
988 
989  //The extensions describe the parameters of the certificate being
990  //requested
991  extensionList = (TlsExtensionList *) p;
992  //Total length of the extension list
993  extensionList->length = 0;
994 
995  //Point to the first extension of the list
996  p += sizeof(TlsExtensionList);
997  //Adjust the length of the message
998  *length += sizeof(TlsExtensionList);
999 
1000  //The SignatureAlgorithms extension contains the list of signature
1001  //algorithms which the server would accept (refer to RFC 8446,
1002  //section 4.3.2)
1003  error = tlsFormatSignAlgosExtension(context, p, &n);
1004 
1005 #if (TLS_SIGN_ALGOS_CERT_SUPPORT == ENABLED)
1006  //Check status code
1007  if(!error)
1008  {
1009  //Fix the length of the extension list
1010  extensionList->length += (uint16_t) n;
1011  //Point to the next field
1012  p += n;
1013 
1014  //The SignatureAlgorithmsCert extension may optionally be included
1015  error = tlsFormatSignAlgosCertExtension(context, p, &n);
1016  }
1017 #endif
1018 
1019 #if (TLS_CERT_AUTHORITIES_SUPPORT == ENABLED)
1020  //Check status code
1021  if(!error)
1022  {
1023  //Fix the length of the extension list
1024  extensionList->length += (uint16_t) n;
1025  //Point to the next field
1026  p += n;
1027 
1028  //The CertificateAuthorities extension is used to indicate the CAs
1029  //which an endpoint supports and which should be used by the receiving
1030  //endpoint to guide certificate selection
1031  error = tlsFormatCertAuthoritiesExtension(context, p, &n);
1032  }
1033 #endif
1034 
1035  //Check status code
1036  if(!error)
1037  {
1038  //Fix the length of the extension list
1039  extensionList->length += (uint16_t) n;
1040  //Point to the next field
1041  p += n;
1042  }
1043 
1044  //Convert the length of the extension list to network byte order
1045  extensionList->length = htons(extensionList->length);
1046  //Total length of the message
1047  *length += htons(extensionList->length);
1048  }
1049  else
1050 #endif
1051  //Invalid TLS version?
1052  {
1053  //Report an error
1054  error = ERROR_INVALID_VERSION;
1055  }
1056 
1057  //Return status code
1058  return error;
1059 }
1060 
1061 
1062 /**
1063  * @brief Format ServerHelloDone message
1064  * @param[in] context Pointer to the TLS context
1065  * @param[out] message Buffer where to format the ServerHelloDone message
1066  * @param[out] length Length of the resulting ServerHelloDone message
1067  * @return Error code
1068  **/
1069 
1071  TlsServerHelloDone *message, size_t *length)
1072 {
1073  //The ServerHelloDone message does not contain any data
1074  *length = 0;
1075 
1076  //Successful processing
1077  return NO_ERROR;
1078 }
1079 
1080 
1081 /**
1082  * @brief Format NewSessionTicket message
1083  * @param[in] context Pointer to the TLS context
1084  * @param[out] message Buffer where to format the NewSessionTicket message
1085  * @param[out] length Length of the resulting NewSessionTicket message
1086  * @return Error code
1087  **/
1088 
1091 {
1092 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2 && \
1093  TLS_TICKET_SUPPORT == ENABLED)
1094  error_t error;
1095  size_t n;
1096  TlsPlaintextSessionState *state;
1097 
1098  //The ticket_lifetime_hint field contains a hint from the server about how
1099  //long the ticket should be stored
1100  message->ticketLifetimeHint = HTONL(TLS_TICKET_LIFETIME / 1000);
1101 
1102  //The ticket itself is opaque to the client
1103  state = (TlsPlaintextSessionState *) message->ticket;
1104 
1105  //Save session state
1106  state->version = context->version;
1107  state->cipherSuite = context->cipherSuite.identifier;
1108  osMemcpy(state->secret, context->masterSecret, TLS_MASTER_SECRET_SIZE);
1109  state->ticketTimestamp = osGetSystemTime();
1110  state->ticketLifetime = TLS_TICKET_LIFETIME;
1111 
1112 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1113  //Extended master secret computation
1114  state->extendedMasterSecret = context->emsExtReceived;
1115 #endif
1116 
1117  //Make sure a valid callback has been registered
1118  if(context->ticketEncryptCallback != NULL)
1119  {
1120  //Encrypt the state information
1121  error = context->ticketEncryptCallback(context, (uint8_t *) state,
1122  sizeof(TlsPlaintextSessionState), message->ticket, &n,
1123  context->ticketParam);
1124  }
1125  else
1126  {
1127  //Report en error
1128  error = ERROR_FAILURE;
1129  }
1130 
1131  //Check status code
1132  if(!error)
1133  {
1134  //Fix the length of the ticket
1135  message->ticketLen = htons(n);
1136 
1137  //Total length of the message
1138  *length = sizeof(TlsNewSessionTicket) + n;
1139  }
1140 
1141  //Return status code
1142  return error;
1143 #else
1144  //Session ticket mechanism is not implemented
1145  return ERROR_NOT_IMPLEMENTED;
1146 #endif
1147 }
1148 
1149 
1150 /**
1151  * @brief Parse ClientHello message
1152  *
1153  * When a client first connects to a server, it is required to send
1154  * the ClientHello as its first message. The client can also send a
1155  * ClientHello in response to a HelloRequest or on its own initiative
1156  * in order to renegotiate the security parameters in an existing
1157  * connection
1158  *
1159  * @param[in] context Pointer to the TLS context
1160  * @param[in] message Incoming ClientHello message to parse
1161  * @param[in] length Message length
1162  * @return Error code
1163  **/
1164 
1166  const TlsClientHello *message, size_t length)
1167 {
1168  error_t error;
1169  size_t n;
1170  const uint8_t *p;
1171  const TlsCipherSuites *cipherSuites;
1172  const TlsCompressMethods *compressMethods;
1174 #if (DTLS_SUPPORT == ENABLED)
1175  const DtlsCookie *cookie;
1176 #endif
1177 
1178  //Debug message
1179  TRACE_INFO("ClientHello message received (%" PRIuSIZE " bytes)...\r\n", length);
1181 
1182  //Check current state
1183  if(context->state == TLS_STATE_CLIENT_HELLO)
1184  {
1185  //When a client first connects to a server, it is required to send
1186  //the ClientHello as its first message
1187  }
1188  else if(context->state == TLS_STATE_CLIENT_HELLO_2)
1189  {
1190 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1191  //The client will also send a updated ClientHello when the server has
1192  //responded to its initial ClientHello with a HelloRetryRequest
1193  context->updatedClientHelloReceived = TRUE;
1194 #endif
1195  }
1196  else if(context->state == TLS_STATE_APPLICATION_DATA)
1197  {
1198 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1199  //Version of TLS prior to TLS 1.3?
1200  if(context->version <= TLS_VERSION_1_2)
1201  {
1202 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1203  //Check whether secure renegotiation is enabled
1204  if(context->secureRenegoEnabled)
1205  {
1206  //Make sure the secure_renegotiation flag is set
1207  if(!context->secureRenegoFlag)
1208  {
1209  //If the connection's secure_renegotiation flag is set to
1210  //FALSE, it is recommended that servers do not permit legacy
1211  //renegotiation (refer to RFC 5746, section 4.4)
1212  return ERROR_HANDSHAKE_FAILED;
1213  }
1214  }
1215  else
1216 #endif
1217  {
1218  //Secure renegotiation is disabled
1219  return ERROR_HANDSHAKE_FAILED;
1220  }
1221  }
1222  else
1223 #endif
1224  {
1225  //Because TLS 1.3 forbids renegotiation, if a server has negotiated
1226  //TLS 1.3 and receives a ClientHello at any other time, it must
1227  //terminate the connection with an unexpected_message alert
1228  return ERROR_UNEXPECTED_MESSAGE;
1229  }
1230  }
1231  else
1232  {
1233  //Report an error
1234  return ERROR_UNEXPECTED_MESSAGE;
1235  }
1236 
1237  //Check the length of the ClientHello message
1238  if(length < sizeof(TlsClientHello))
1239  return ERROR_DECODING_FAILED;
1240 
1241  //Get the version the client wishes to use during this session
1242  context->clientVersion = ntohs(message->clientVersion);
1243 
1244  //Point to the session ID
1245  p = message->sessionId;
1246  //Remaining bytes to process
1247  n = length - sizeof(TlsClientHello);
1248 
1249  //Check the length of the session ID
1250  if(message->sessionIdLen > n)
1251  return ERROR_DECODING_FAILED;
1252  if(message->sessionIdLen > 32)
1253  return ERROR_DECODING_FAILED;
1254 
1255  //Debug message
1256  TRACE_DEBUG("Session ID (%" PRIu8 " bytes):\r\n", message->sessionIdLen);
1257  TRACE_DEBUG_ARRAY(" ", message->sessionId, message->sessionIdLen);
1258 
1259  //Point to the next field
1260  p += message->sessionIdLen;
1261  //Remaining bytes to process
1262  n -= message->sessionIdLen;
1263 
1264 #if (DTLS_SUPPORT == ENABLED)
1265  //DTLS protocol?
1266  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
1267  {
1268  //Point to the Cookie field
1269  cookie = (DtlsCookie *) p;
1270 
1271  //Malformed ClientHello message?
1272  if(n < sizeof(DtlsCookie))
1273  return ERROR_DECODING_FAILED;
1274  if(n < (sizeof(DtlsCookie) + cookie->length))
1275  return ERROR_DECODING_FAILED;
1276 
1277  //Check the length of the cookie
1278  if(cookie->length > DTLS_MAX_COOKIE_SIZE)
1279  return ERROR_ILLEGAL_PARAMETER;
1280 
1281  //Point to the next field
1282  p += sizeof(DtlsCookie) + cookie->length;
1283  //Remaining bytes to process
1284  n -= sizeof(DtlsCookie) + cookie->length;
1285  }
1286  else
1287  {
1288  //Just for sanity
1289  cookie = NULL;
1290  }
1291 #endif
1292 
1293  //List of cryptographic algorithms supported by the client
1294  cipherSuites = (TlsCipherSuites *) p;
1295 
1296  //Malformed ClientHello message?
1297  if(n < sizeof(TlsCipherSuites))
1298  return ERROR_DECODING_FAILED;
1299  if(n < (sizeof(TlsCipherSuites) + ntohs(cipherSuites->length)))
1300  return ERROR_DECODING_FAILED;
1301 
1302  //Check the length of the list
1303  if(ntohs(cipherSuites->length) == 0)
1304  return ERROR_DECODING_FAILED;
1305  if((ntohs(cipherSuites->length) % 2) != 0)
1306  return ERROR_DECODING_FAILED;
1307 
1308  //Point to the next field
1309  p += sizeof(TlsCipherSuites) + ntohs(cipherSuites->length);
1310  //Remaining bytes to process
1311  n -= sizeof(TlsCipherSuites) + ntohs(cipherSuites->length);
1312 
1313  //List of compression algorithms supported by the client
1314  compressMethods = (TlsCompressMethods *) p;
1315 
1316  //Malformed ClientHello message?
1317  if(n < sizeof(TlsCompressMethods))
1318  return ERROR_DECODING_FAILED;
1319  if(n < (sizeof(TlsCompressMethods) + compressMethods->length))
1320  return ERROR_DECODING_FAILED;
1321 
1322  //Check the length of the list
1323  if(compressMethods->length == 0)
1324  return ERROR_DECODING_FAILED;
1325 
1326  //Point to the next field
1327  p += sizeof(TlsCompressMethods) + compressMethods->length;
1328  //Remaining bytes to process
1329  n -= sizeof(TlsCompressMethods) + compressMethods->length;
1330 
1331  //Parse the list of extensions offered by the client
1333  //Any error to report?
1334  if(error)
1335  return error;
1336 
1337  //Check whether the ClientHello includes any SCSV cipher suites
1338  error = tlsCheckSignalingCipherSuiteValues(context, cipherSuites);
1339  //Any error to report?
1340  if(error)
1341  return error;
1342 
1343 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1344  //Parse RenegotiationInfo extension
1345  error = tlsParseClientRenegoInfoExtension(context, &extensions);
1346  //Any error to report?
1347  if(error)
1348  return error;
1349 #endif
1350 
1351 #if (DTLS_SUPPORT == ENABLED)
1352  //DTLS protocol?
1353  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
1354  {
1355  DtlsClientParameters clientParams;
1356 
1357  //The server should use client parameters (version, random, session_id,
1358  //cipher_suites, compression_method) to generate its cookie
1359  clientParams.version = ntohs(message->clientVersion);
1360  clientParams.random = message->random;
1361  clientParams.randomLen = 32;
1362  clientParams.sessionId = message->sessionId;
1363  clientParams.sessionIdLen = message->sessionIdLen;
1364  clientParams.cipherSuites = (const uint8_t *) cipherSuites->value;
1365  clientParams.cipherSuitesLen = ntohs(cipherSuites->length);
1366  clientParams.compressMethods = compressMethods->value;
1367  clientParams.compressMethodsLen = compressMethods->length;
1368 
1369  //Verify that the cookie is valid
1370  error = dtlsVerifyCookie(context, cookie, &clientParams);
1371  //Any error to report?
1372  if(error)
1373  return error;
1374 
1375  //The server may respond with a HelloVerifyRequest message containing
1376  //a stateless cookie
1377  if(context->state == TLS_STATE_HELLO_VERIFY_REQUEST)
1378  {
1379  //Exit immediately
1380  return NO_ERROR;
1381  }
1382  }
1383 #endif
1384 
1385  //Perform version negotiation
1386  error = tlsNegotiateVersion(context, ntohs(message->clientVersion),
1387  extensions.supportedVersionList);
1388  //Any error to report?
1389  if(error)
1390  return error;
1391 
1392  //Check the list of extensions offered by the client
1393  error = tlsCheckHelloExtensions(TLS_TYPE_CLIENT_HELLO, context->version,
1394  &extensions);
1395  //Any error to report?
1396  if(error)
1397  return error;
1398 
1399  //The SignatureAlgorithms extension is not meaningful for TLS versions
1400  //prior to 1.2 (refer to RFC 5246, section 7.4.1.4.1)
1401  if(context->version <= TLS_VERSION_1_1)
1402  {
1403  //Even if clients do offer it, the rules specified in RFC 6066 require
1404  //servers to ignore extensions they do not understand
1405  extensions.signAlgoList = NULL;
1406  extensions.certSignAlgoList = NULL;
1407  }
1408 
1409  //Save client random value
1410  osMemcpy(context->clientRandom, message->random, 32);
1411 
1412 #if (TLS_SNI_SUPPORT == ENABLED)
1413  //In order to provide the server name, clients may include a ServerName
1414  //extension
1415  error = tlsParseClientSniExtension(context, extensions.serverNameList);
1416  //Any error to report?
1417  if(error)
1418  return error;
1419 #endif
1420 
1421 #if (TLS_ALPN_SUPPORT == ENABLED)
1422  //Parse ALPN extension
1423  error = tlsParseClientAlpnExtension(context, extensions.protocolNameList);
1424  //Any error to report?
1425  if(error)
1426  return error;
1427 #endif
1428 
1429 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1430  //TLS 1.0, TLS 1.1 or TLS 1.2 currently selected?
1431  if(context->version <= TLS_VERSION_1_2)
1432  {
1433 #if (TLS_TICKET_SUPPORT == ENABLED)
1434  //Parse SessionTicket extension
1435  error = tlsParseClientSessionTicketExtension(context,
1436  extensions.sessionTicket);
1437  //Any error to report?
1438  if(error)
1439  return error;
1440 
1441  //The server attempts to resume TLS session via session ticket
1442  error = tlsResumeStatelessSession(context, message->sessionId,
1443  message->sessionIdLen, cipherSuites, &extensions);
1444 #else
1445  //No session ticket presented by the client
1446  error = ERROR_NO_TICKET;
1447 #endif
1448 
1449  //If a ticket is presented by the client, the server must not attempt
1450  //to use the session ID in the ClientHello for stateful session
1451  //resumption (refer to RFC 5077, section 3.4)
1452  if(error == ERROR_NO_TICKET)
1453  {
1454  //The server attempts to resume TLS session via session ID
1455  error = tlsResumeStatefulSession(context, message->sessionId,
1456  message->sessionIdLen, cipherSuites, &extensions);
1457  //Any error to report?
1458  if(error)
1459  return error;
1460  }
1461 
1462  //Full handshake?
1463  if(!context->resume)
1464  {
1465  //Perform cipher suite negotiation
1466  error = tlsNegotiateCipherSuite(context, NULL, cipherSuites,
1467  &extensions);
1468  //If no acceptable choices are presented, terminate the handshake
1469  if(error)
1470  return ERROR_HANDSHAKE_FAILED;
1471 
1472  //Parse the list of compression methods supported by the client
1473  error = tlsParseCompressMethods(context, compressMethods);
1474  //Any error to report?
1475  if(error)
1476  return error;
1477  }
1478 
1479  //Initialize handshake message hashing
1480  error = tlsInitTranscriptHash(context);
1481  //Any error to report?
1482  if(error)
1483  return error;
1484 
1485 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1486  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1487  //A client that proposes ECC cipher suites in its ClientHello message
1488  //may send the EcPointFormats extension
1489  error = tlsParseClientEcPointFormatsExtension(context,
1490  extensions.ecPointFormatList);
1491  //Any error to report?
1492  if(error)
1493  return error;
1494 #endif
1495 
1496 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1497  //On connecting, the client includes the EncryptThenMac extension in
1498  //its ClientHello if it wishes to use encrypt-then-MAC rather than the
1499  //default MAC-then-encrypt (refer to RFC 7366, section 2)
1500  error = tlsParseClientEtmExtension(context,
1501  extensions.encryptThenMac);
1502  //Any error to report?
1503  if(error)
1504  return error;
1505 #endif
1506 
1507 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1508  //Parse ExtendedMasterSecret extension
1509  error = tlsParseClientEmsExtension(context,
1510  extensions.extendedMasterSecret);
1511  //Any error to report?
1512  if(error)
1513  return error;
1514 #endif
1515  }
1516  else
1517 #endif
1518 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1519  //TLS 1.3 currently selected?
1520  if(context->version == TLS_VERSION_1_3)
1521  {
1522  //Save the client's legacy_session_id field
1523  osMemcpy(context->sessionId, message->sessionId, message->sessionIdLen);
1524  context->sessionIdLen = message->sessionIdLen;
1525 
1526  //Perform cipher suite and key exchange method negotiation
1527  error = tls13NegotiateCipherSuite(context, message, length, cipherSuites,
1528  &extensions);
1529  //If no acceptable choices are presented, terminate the handshake
1530  if(error)
1531  return error;
1532 
1533  //Parse the list of compression methods supported by the client
1534  error = tlsParseCompressMethods(context, compressMethods);
1535  //Any error to report?
1536  if(error)
1537  return error;
1538 
1539  //When a PSK is used and early data is allowed for that PSK, the client
1540  //can send application data in its first flight of messages
1541  if(extensions.earlyDataIndication != NULL)
1542  {
1543  //If the client opts to do so, it must supply both the PreSharedKey
1544  //and EarlyData extensions (refer to RFC 8446, section 4.2.10)
1545  if(extensions.identityList == NULL || extensions.binderList == NULL)
1546  {
1547  context->earlyDataRejected = TRUE;
1548  }
1549  }
1550  }
1551  else
1552 #endif
1553  //Invalid TLS version?
1554  {
1555  //Just for sanity
1556  return ERROR_INVALID_VERSION;
1557  }
1558 
1559 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED && TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1560  //A server that supports the RecordSizeLimit extension must ignore a
1561  //MaxFragmentLength that appears in a ClientHello if both extensions
1562  //appear (refer to RFC 8449, section 5)
1563  if(extensions.maxFragLen != NULL && extensions.recordSizeLimit != NULL)
1564  {
1565  extensions.maxFragLen = NULL;
1566  }
1567 #endif
1568 
1569 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
1570  //In order to negotiate smaller maximum fragment lengths, clients may
1571  //include a MaxFragmentLength extension
1572  error = tlsParseClientMaxFragLenExtension(context, extensions.maxFragLen);
1573  //Any error to report?
1574  if(error)
1575  return error;
1576 #endif
1577 
1578 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1579  //The value of RecordSizeLimit is the maximum size of record in octets
1580  //that the peer is willing to receive
1582  extensions.recordSizeLimit);
1583  //Any error to report?
1584  if(error)
1585  return error;
1586 #endif
1587 
1588 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1589  //Parse ClientCertType extension
1590  error = tlsParseClientCertTypeListExtension(context,
1591  extensions.clientCertTypeList);
1592  //Any error to report?
1593  if(error)
1594  return error;
1595 
1596  //Parse ServerCertType extension
1597  error = tlsParseServerCertTypeListExtension(context,
1598  extensions.serverCertTypeList);
1599  //Any error to report?
1600  if(error)
1601  return error;
1602 #endif
1603 
1604  //Another handshake message cannot be packed in the same record as the
1605  //ClientHello
1606  if(context->rxBufferLen != 0)
1607  return ERROR_UNEXPECTED_MESSAGE;
1608 
1609  //Version of TLS prior to TLS 1.3?
1610  if(context->version <= TLS_VERSION_1_2)
1611  {
1612  //Send a ServerHello message to the client
1614  }
1615  else
1616  {
1617  //Send a ServerHello or a HelloRetryRequest message to the client
1618  if(context->state == TLS_STATE_CLIENT_HELLO)
1619  {
1621  }
1622  else if(context->state == TLS_STATE_CLIENT_HELLO_2)
1623  {
1625  }
1626  else
1627  {
1629  }
1630  }
1631 
1632  //Successful processing
1633  return NO_ERROR;
1634 }
1635 
1636 
1637 /**
1638  * @brief Parse ClientKeyExchange message
1639  *
1640  * This message is always sent by the client. It must immediately
1641  * follow the client Certificate message, if it is sent. Otherwise,
1642  * it must be the first message sent by the client after it receives
1643  * the ServerHelloDone message
1644  *
1645  * @param[in] context Pointer to the TLS context
1646  * @param[in] message Incoming ClientKeyExchange message to parse
1647  * @param[in] length Message length
1648  * @return Error code
1649  **/
1650 
1652  const TlsClientKeyExchange *message, size_t length)
1653 {
1654  error_t error;
1655  size_t n;
1656  const uint8_t *p;
1657 
1658  //Debug message
1659  TRACE_INFO("ClientKeyExchange message received (%" PRIuSIZE " bytes)...\r\n", length);
1661 
1662  //Check TLS version
1663  if(context->version > TLS_VERSION_1_2)
1664  return ERROR_UNEXPECTED_MESSAGE;
1665 
1666  //Check current state
1667  if(context->state == TLS_STATE_CLIENT_CERTIFICATE)
1668  {
1669  //A an non-anonymous server can optionally request a certificate from the client
1670  if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
1671  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1672  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1673  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1674  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
1675  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1676  {
1677  //If client authentication is required by the server for the handshake
1678  //to continue, it may respond with a fatal handshake failure alert
1679  if(context->clientAuthMode == TLS_CLIENT_AUTH_REQUIRED)
1680  return ERROR_HANDSHAKE_FAILED;
1681  }
1682  }
1683  else if(context->state != TLS_STATE_CLIENT_KEY_EXCHANGE)
1684  {
1685  //Send a fatal alert to the client
1686  return ERROR_UNEXPECTED_MESSAGE;
1687  }
1688 
1689  //Point to the beginning of the handshake message
1690  p = message;
1691 
1692 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
1693  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1694  //PSK key exchange method?
1695  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1696  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
1697  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1698  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1699  {
1700  //The PSK identity is sent in cleartext
1701  error = tlsParsePskIdentity(context, p, length, &n);
1702  //Any error to report?
1703  if(error)
1704  return error;
1705 
1706  //Point to the next field
1707  p += n;
1708  //Remaining bytes to process
1709  length -= n;
1710  }
1711 #endif
1712 
1713  //RSA, Diffie-Hellman or ECDH key exchange method?
1714  if(context->keyExchMethod != TLS_KEY_EXCH_PSK)
1715  {
1716  //Parse client's key exchange parameters
1717  error = tlsParseClientKeyParams(context, p, length, &n);
1718  //Any error to report?
1719  if(error)
1720  return error;
1721 
1722  //Point to the next field
1723  p += n;
1724  //Remaining bytes to process
1725  length -= n;
1726  }
1727 
1728  //If the amount of data in the message does not precisely match the format
1729  //of the ClientKeyExchange message, then send a fatal alert
1730  if(length != 0)
1731  return ERROR_DECODING_FAILED;
1732 
1733 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
1734  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1735  //PSK key exchange method?
1736  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1737  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
1738  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1739  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1740  {
1741  //Invalid pre-shared key?
1742  if(context->pskLen == 0)
1743  return ERROR_INVALID_KEY_LENGTH;
1744 
1745  //Generate premaster secret
1746  error = tlsGeneratePskPremasterSecret(context);
1747  //Any error to report?
1748  if(error)
1749  return error;
1750  }
1751 #endif
1752 
1753  //Derive session keys from the premaster secret
1754  error = tlsGenerateSessionKeys(context);
1755  //Unable to generate key material?
1756  if(error)
1757  return error;
1758 
1759  //The client must send a CertificateVerify message when the Certificate
1760  //message is non-empty
1761  if(context->peerCertType != TLS_CERT_NONE)
1762  {
1764  }
1765  else
1766  {
1768  }
1769 
1770  //Successful processing
1771  return NO_ERROR;
1772 }
1773 
1774 #endif
error_t tlsFormatServerSessionTicketExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SessionTicket extension.
#define htons(value)
Definition: cpu_endian.h:413
DTLS (Datagram Transport Layer Security)
Parsing and checking of TLS extensions.
error_t tlsFormatServerEmsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ExtendedMasterSecret extension.
TLS helper functions.
const uint8_t * random
Definition: dtls_misc.h:225
X.509 certificate parsing.
Date and time management.
error_t tlsFormatCertAuthorities(TlsContext *context, uint8_t *p, size_t *written)
Format the list of distinguished names of acceptable CAs.
Definition: tls_common.c:882
uint8_t extensions[]
Definition: ntp_common.h:207
error_t tlsParseClientMaxFragLenExtension(TlsContext *context, const TlsExtension *maxFragLen)
Parse MaxFragmentLength extension.
error_t tlsParsePskIdentity(TlsContext *context, const uint8_t *p, size_t length, size_t *consumed)
Parse PSK identity.
@ TLS_STATE_HELLO_RETRY_REQUEST
Definition: tls.h:1498
TLS cipher suites.
error_t tlsParseClientKeyParams(TlsContext *context, const uint8_t *p, size_t length, size_t *consumed)
Parse client's key exchange parameters.
error_t tlsSendNewSessionTicket(TlsContext *context)
Send NewSessionTicket message.
Definition: tls_server.c:407
error_t tlsFormatPskIdentityHint(TlsContext *context, uint8_t *p, size_t *written)
Format PSK identity hint.
Handshake message processing (TLS 1.3 server)
TlsDigitalSignature
Definition: tls.h:1755
@ TLS13_KEY_EXCH_MLKEM
Definition: tls.h:1182
@ ERROR_WOULD_BLOCK
Definition: error.h:96
void TlsServerHelloDone
ServerHelloDone message.
Definition: tls.h:1857
Key material generation.
TLS handshake.
@ TLS_TYPE_SERVER_HELLO_DONE
Definition: tls.h:1075
error_t tlsGeneratePskPremasterSecret(TlsContext *context)
Premaster secret generation (for PSK cipher suites)
error_t tls12GenerateServerKeySignature(TlsContext *context, Tls12DigitalSignature *signature, const uint8_t *params, size_t paramsLen, size_t *written)
Sign server's key exchange parameters (TLS 1.2)
@ TLS_COMPRESSION_METHOD_NULL
Definition: tls.h:1150
error_t tlsParseClientEcPointFormatsExtension(TlsContext *context, const TlsEcPointFormatList *ecPointFormatList)
Parse EcPointFormats extension.
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:244
error_t tlsNegotiateVersion(TlsContext *context, uint16_t clientVersion, const TlsSupportedVersionList *supportedVersionList)
Version negotiation.
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
error_t tlsSendCertificateRequest(TlsContext *context)
Send CertificateRequest message.
Definition: tls_server.c:275
uint8_t p
Definition: ndp.h:300
error_t tlsParseServerCertTypeListExtension(TlsContext *context, const TlsCertTypeList *serverCertTypeList)
Parse ServerCertType extension.
TlsCertificateRequest
Definition: tls.h:1850
uint8_t message[]
Definition: chap.h:154
#define TRUE
Definition: os_port.h:50
@ TLS_STATE_CERTIFICATE_REQUEST
Definition: tls.h:1507
error_t tlsFormatServerRenegoInfoExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RenegotiationInfo extension.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:979
Session cache management.
error_t tlsFormatSignAlgosExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SignatureAlgorithms extension.
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1523
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ TLS13_KEY_EXCH_ECDHE
Definition: tls.h:1181
@ TLS_CERT_DSS_SIGN
Definition: tls.h:1213
error_t tlsFormatServerHelloDone(TlsContext *context, TlsServerHelloDone *message, size_t *length)
Format ServerHelloDone message.
Definition: tls_server.c:1070
uint8_t version
Definition: coap_common.h:177
error_t tlsParseClientCertTypeListExtension(TlsContext *context, const TlsCertTypeList *clientCertTypeList)
Parse ClientCertType extension.
@ ERROR_INVALID_VERSION
Definition: error.h:118
error_t tls13NegotiateCipherSuite(TlsContext *context, const void *clientHello, size_t clientHelloLen, const TlsCipherSuites *cipherSuites, TlsHelloExtensions *extensions)
Cipher suite and key exchange method negotiation.
error_t tlsSendHandshakeMessage(TlsContext *context, const void *data, size_t length, TlsMessageType type)
Send handshake message.
error_t tlsParseClientHello(TlsContext *context, const TlsClientHello *message, size_t length)
Parse ClientHello message.
Definition: tls_server.c:1165
error_t tlsFormatServerSniExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SNI extension.
#define DTLS_MAX_COOKIE_SIZE
Definition: dtls_misc.h:76
error_t tlsFormatServerKeyParams(TlsContext *context, uint8_t *p, size_t *written)
Format server's key exchange parameters.
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1494
error_t tlsSendServerHelloDone(TlsContext *context)
Send ServerHelloDone message.
Definition: tls_server.c:354
error_t tlsFormatSupportedSignAlgos(TlsContext *context, uint8_t *p, size_t *written)
Format the list of supported signature algorithms.
@ TLS_KEY_EXCH_RSA
Definition: tls.h:1162
error_t tlsParseClientKeyExchange(TlsContext *context, const TlsClientKeyExchange *message, size_t length)
Parse ClientKeyExchange message.
Definition: tls_server.c:1651
TlsExtensionList
Definition: tls.h:1622
TlsCipherSuites
Definition: tls.h:1555
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1499
@ TLS_STATE_HELLO_VERIFY_REQUEST
Definition: tls.h:1497
@ TLS_KEY_EXCH_ECDHE_ECDSA
Definition: tls.h:1171
@ ERROR_NO_TICKET
Definition: error.h:230
@ TLS_KEY_EXCH_ECDHE_RSA
Definition: tls.h:1169
@ TLS_KEY_EXCH_ECDH_ANON
Definition: tls.h:1172
error_t tlsFormatServerRecordSizeLimitExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RecordSizeLimit extension.
PEM file import functions.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t tlsParseClientSessionTicketExtension(TlsContext *context, const TlsExtension *sessionTicket)
Parse SessionTicket extension.
#define HTONL(value)
Definition: cpu_endian.h:411
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
DTLS record protocol.
error_t tlsSendServerKeyExchange(TlsContext *context)
Send ServerKeyExchange message.
Definition: tls_server.c:192
error_t tlsParseClientEtmExtension(TlsContext *context, const TlsExtension *encryptThenMac)
Parse EncryptThenMac extension.
#define TLS_VERSION_1_2
Definition: tls.h:96
Formatting and parsing of extensions (TLS 1.3 server)
@ TLS_KEY_EXCH_DH_ANON
Definition: tls.h:1167
Client parameters.
Definition: dtls_misc.h:223
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const uint8_t * cipherSuites
Definition: dtls_misc.h:229
@ TLS13_KEY_EXCH_DHE
Definition: tls.h:1180
error_t tlsParseHelloExtensions(TlsMessageType msgType, const uint8_t *p, size_t length, TlsHelloExtensions *extensions)
Parse Hello extensions.
#define STORE16BE(a, p)
Definition: cpu_endian.h:262
error_t tlsResumeStatelessSession(TlsContext *context, const uint8_t *sessionId, size_t sessionIdLen, const TlsCipherSuites *cipherSuites, const TlsHelloExtensions *extensions)
Resume TLS session via session ticket.
@ TLS_TYPE_CLIENT_HELLO
Definition: tls.h:1063
Tls12DigitalSignature
Definition: tls.h:1767
error_t tlsFormatClientCertTypeExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ClientCertType extension.
error_t tlsFormatServerHello(TlsContext *context, TlsServerHello *message, size_t *length)
Format ServerHello message.
Definition: tls_server.c:452
error_t tls13FormatServerPreSharedKeyExtension(TlsContext *context, uint8_t *p, size_t *written)
Format PreSharedKey extension.
#define TLS_VERSION_1_3
Definition: tls.h:97
@ TLS_TYPE_SERVER_HELLO
Definition: tls.h:1064
Handshake message processing (TLS client and server)
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
error_t tlsFormatCertAuthoritiesExtension(TlsContext *context, uint8_t *p, size_t *written)
Format CertificateAuthorities extension.
Definition: tls_common.c:830
error_t tlsFormatNewSessionTicket(TlsContext *context, TlsNewSessionTicket *message, size_t *length)
Format NewSessionTicket message.
Definition: tls_server.c:1089
error_t tlsNegotiateCipherSuite(TlsContext *context, const HashAlgo *hashAlgo, const TlsCipherSuites *cipherSuites, TlsHelloExtensions *extensions)
Cipher suite negotiation.
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1511
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1516
@ TLS_CERT_RSA_SIGN
Definition: tls.h:1212
#define TRACE_INFO(...)
Definition: debug.h:105
error_t tlsParseClientAlpnExtension(TlsContext *context, const TlsProtocolNameList *protocolNameList)
Parse ALPN extension.
uint8_t length
Definition: tcp.h:375
@ TLS_CLIENT_AUTH_NONE
Definition: tls.h:1001
#define MIN(a, b)
Definition: os_port.h:63
@ TLS_KEY_EXCH_DHE_PSK
Definition: tls.h:1175
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1521
TlsCompressMethods
Definition: tls.h:1566
uint16_t dtlsTranslateVersion(uint16_t version)
Translate TLS version into DTLS version.
Definition: dtls_misc.c:112
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC
Definition: tls.h:1512
error_t tls13FormatServerSupportedVersionsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedVersions extension.
Helper functions for TLS 1.3 server.
error_t tlsFormatServerMaxFragLenExtension(TlsContext *context, uint8_t *p, size_t *written)
Format MaxFragmentLength extension.
Hello extensions.
Definition: tls.h:2136
#define TLS_MASTER_SECRET_SIZE
Definition: tls.h:815
Transcript hash calculation.
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1502
@ TLS_KEY_EXCH_RSA_PSK
Definition: tls.h:1174
#define ntohs(value)
Definition: cpu_endian.h:421
@ TLS_TYPE_SERVER_KEY_EXCHANGE
Definition: tls.h:1073
error_t tlsFormatCertificateRequest(TlsContext *context, TlsCertificateRequest *message, size_t *length)
Format CertificateRequest message.
Definition: tls_server.c:898
#define TRACE_DEBUG(...)
Definition: debug.h:119
@ ERROR_TIMEOUT
Definition: error.h:95
#define TLS_VERSION_1_1
Definition: tls.h:95
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1495
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
TlsServerHello
Definition: tls.h:1825
void TlsClientKeyExchange
ClientKeyExchange message.
Definition: tls.h:1864
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1504
#define TLS_TICKET_LIFETIME
Definition: tls.h:164
TlsPlaintextSessionState
Definition: tls.h:1928
@ TLS_CLIENT_AUTH_REQUIRED
Definition: tls.h:1003
error_t tlsParseCompressMethods(TlsContext *context, const TlsCompressMethods *compressMethods)
Parse the list of compression methods supported by the client.
uint8_t n
@ TLS_STATE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1510
error_t tlsFormatServerEcPointFormatsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EcPointFormats extension.
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1509
@ TLS13_KEY_EXCH_HYBRID
Definition: tls.h:1183
error_t tlsParseClientRecordSizeLimitExtension(TlsContext *context, const TlsExtension *recordSizeLimit)
Parse RecordSizeLimit extension.
@ TLS_KEY_EXCH_PSK
Definition: tls.h:1173
error_t tlsInitTranscriptHash(TlsContext *context)
Initialize handshake message hashing.
Handshake message processing (TLS server)
@ TLS_KEY_EXCH_ECDHE_PSK
Definition: tls.h:1176
TlsClientHello
Definition: tls.h:1812
const uint8_t * sessionId
Definition: dtls_misc.h:227
error_t tlsResumeStatefulSession(TlsContext *context, const uint8_t *sessionId, size_t sessionIdLen, const TlsCipherSuites *cipherSuites, const TlsHelloExtensions *extensions)
Resume TLS session via session ID.
Helper functions for signature generation and verification.
@ TLS_TYPE_CERTIFICATE_REQUEST
Definition: tls.h:1074
const uint8_t * compressMethods
Definition: dtls_misc.h:231
TLS (Transport Layer Security)
uint8_t cookie[]
Definition: dtls_misc.h:206
error_t tlsCheckSignalingCipherSuiteValues(TlsContext *context, const TlsCipherSuites *cipherSuites)
Check whether the ClientHello includes any SCSV cipher suites.
error_t tlsParseClientSniExtension(TlsContext *context, const TlsServerNameList *serverNameList)
Parse SNI extension.
@ TLS_CERT_ECDSA_SIGN
Definition: tls.h:1219
@ TLS_KEY_EXCH_DHE_DSS
Definition: tls.h:1166
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:978
error_t tlsCheckHelloExtensions(TlsMessageType msgType, uint16_t version, TlsHelloExtensions *extensions)
Check Hello extensions.
@ TLS_CERT_NONE
Definition: tls.h:1211
error_t tls13FormatServerKeyShareExtension(TlsContext *context, uint8_t *p, size_t *written)
Format KeyShare extension (ServerHello message)
FFDHE key exchange.
Tls13CertRequestContext
Definition: tls13_misc.h:286
Helper functions for TLS server.
error_t tlsGenerateSessionKeys(TlsContext *context)
Generate session keys.
error_t tlsFormatSignAlgosCertExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SignatureAlgorithmsCert extension.
Formatting and parsing of extensions (TLS server)
size_t compressMethodsLen
Definition: dtls_misc.h:232
@ TLS_TYPE_NEW_SESSION_TICKET
Definition: tls.h:1066
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
error_t tlsGenerateServerKeySignature(TlsContext *context, TlsDigitalSignature *signature, const uint8_t *params, size_t paramsLen, size_t *written)
Sign server's key exchange parameters (TLS 1.0 and TLS 1.1)
@ ERROR_DECODING_FAILED
Definition: error.h:242
#define PRIuSIZE
error_t tlsFormatServerCertTypeExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ServerCertType extension.
@ TLS_STATE_SERVER_HELLO_DONE
Definition: tls.h:1508
@ TLS_STATE_SERVER_HELLO_2
Definition: tls.h:1500
error_t tlsGenerateRandomValue(TlsContext *context, uint8_t *random)
Generate client or server random value.
Definition: tls_misc.c:207
TlsNewSessionTicket
Definition: tls.h:1883
DtlsCookie
Definition: dtls_misc.h:154
@ TLS_KEY_EXCH_DHE_RSA
Definition: tls.h:1164
error_t tlsFormatServerAlpnExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ALPN extension.
void TlsServerKeyExchange
ServerKeyExchange message.
Definition: tls.h:1839
error_t tlsParseClientRenegoInfoExtension(TlsContext *context, const TlsHelloExtensions *extensions)
Parse RenegotiationInfo extension.
error_t dtlsVerifyCookie(TlsContext *context, const DtlsCookie *cookie, const DtlsClientParameters *clientParams)
Cookie verification.
Definition: dtls_misc.c:178
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t tlsFormatServerEtmExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EncryptThenMac extension.
error_t tlsSendServerHello(TlsContext *context)
Send ServerHello message.
Definition: tls_server.c:82
error_t tlsFormatServerKeyExchange(TlsContext *context, TlsServerKeyExchange *message, size_t *length)
Format ServerKeyExchange message.
Definition: tls_server.c:776
error_t tlsParseClientEmsExtension(TlsContext *context, const TlsExtension *extendedMasterSecret)
Parse ExtendedMasterSecret extension.
systime_t osGetSystemTime(void)
Retrieve system time.