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