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