tls_client.c
Go to the documentation of this file.
1 /**
2  * @file tls_client.c
3  * @brief Handshake message processing (TLS client)
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_client.h"
45 #include "tls_client_extensions.h"
46 #include "tls_client_misc.h"
47 #include "tls_common.h"
48 #include "tls_extensions.h"
49 #include "tls_certificate.h"
50 #include "tls_sign_misc.h"
51 #include "tls_key_material.h"
52 #include "tls_transcript_hash.h"
53 #include "tls_quic_misc.h"
54 #include "tls_record.h"
55 #include "tls_misc.h"
56 #include "tls13_client.h"
58 #include "tls13_client_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_CLIENT_SUPPORT == ENABLED)
66 
67 
68 /**
69  * @brief Send ClientHello message
70  *
71  * When a client first connects to a server, it is required to send
72  * the ClientHello as its first message. The client can also send a
73  * ClientHello in response to a HelloRequest or on its own initiative
74  * in order to renegotiate the security parameters in an existing
75  * connection
76  *
77  * @param[in] context Pointer to the TLS context
78  * @return Error code
79  **/
80 
82 {
83  error_t error;
84  size_t length;
86 
87  //Point to the buffer where to format the message
88  message = (TlsClientHello *) (context->txBuffer + context->txBufferLen);
89 
90  //Initial or updated ClientHello?
91  if(context->state == TLS_STATE_CLIENT_HELLO)
92  {
93  //Generate the client random value using a cryptographically-safe
94  //pseudorandom number generator
95  error = tlsGenerateRandomValue(context, context->clientRandom);
96  }
97  else
98  {
99  //When responding to a HelloVerifyRequest or a HelloRetryRequest, the
100  //client must use the same random value as it did in the initial
101  //ClientHello
102  error = NO_ERROR;
103  }
104 
105 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
106  //Check status code
107  if(!error)
108  {
109  //In versions of TLS prior to TLS 1.3, the SessionTicket extension is used
110  //to resume a TLS session without requiring session-specific state at the
111  //TLS server
112  if(context->versionMin <= TLS_VERSION_1_2)
113  {
114  //Initial ClientHello?
115  if(context->state == TLS_STATE_CLIENT_HELLO)
116  {
117 #if (TLS_TICKET_SUPPORT == ENABLED)
118  //When presenting a ticket, the client may generate and include a
119  //session ID in the TLS ClientHello
120  if(tlsIsTicketValid(context) && context->sessionIdLen == 0)
121  {
122  //If the server accepts the ticket and the session ID is not
123  //empty, then it must respond with the same session ID present in
124  //the ClientHello. This allows the client to easily differentiate
125  //when the server is resuming a session from when it is falling
126  //back to a full handshake
127  error = tlsGenerateSessionId(context, 32);
128  }
129 #endif
130  }
131  }
132  }
133 #endif
134 
135 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
136  //Check status code
137  if(!error)
138  {
139  //TLS 1.3 supported by the client?
140  if(context->versionMax >= TLS_VERSION_1_3)
141  {
142  //Initial or updated ClientHello?
143  if(context->state == TLS_STATE_CLIENT_HELLO)
144  {
145 #if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
146  //In compatibility mode the session ID field must be non-empty
147  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
148  context->sessionIdLen == 0)
149  {
150  //A client not offering a pre-TLS 1.3 session must generate a
151  //new 32-byte value. This value need not be random but should
152  //be unpredictable to avoid implementations fixating on a
153  //specific value (refer to RFC 8446, section 4.1.2)
154  error = tlsGenerateSessionId(context, 32);
155  }
156 #endif
157  //Check status code
158  if(!error)
159  {
160  //Any preferred ECDHE or FFDHE group?
161  if(tls13IsGroupSupported(context, context->preferredGroup))
162  {
163  //Pregenerate key share using preferred named group
164  error = tls13GenerateKeyShare(context, context->preferredGroup);
165  }
166  else
167  {
168  //Request group selection from the server, at the cost of an
169  //additional round trip
170  context->preferredGroup = TLS_GROUP_NONE;
171  }
172  }
173  }
174  else
175  {
176  //The updated ClientHello message is not encrypted
177  tlsFreeEncryptionEngine(&context->encryptionEngine);
178  }
179  }
180 
181  //Save current time
182  context->clientHelloTimestamp = osGetSystemTime();
183  }
184 #endif
185 
186  //Check status code
187  if(!error)
188  {
189  //Format ClientHello message
190  error = tlsFormatClientHello(context, message, &length);
191  }
192 
193  //Check status code
194  if(!error)
195  {
196  //Debug message
197  TRACE_INFO("Sending ClientHello message (%" PRIuSIZE " bytes)...\r\n", length);
199 
200  //Send handshake message
201  error = tlsSendHandshakeMessage(context, message, length,
203  }
204 
205  //Check status code
206  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
207  {
208  //Initial ClientHello?
209  if(context->state == TLS_STATE_CLIENT_HELLO)
210  {
211  //Wait for a ServerHello or HelloRetryRequest message
213  }
214  else
215  {
216  //Wait for a ServerHello message
218  }
219  }
220 
221  //Return status code
222  return error;
223 }
224 
225 
226 /**
227  * @brief Send ClientKeyExchange message
228  *
229  * This message is always sent by the client. It must immediately
230  * follow the client Certificate message, if it is sent. Otherwise,
231  * it must be the first message sent by the client after it receives
232  * the ServerHelloDone message
233  *
234  * @param[in] context Pointer to the TLS context
235  * @return Error code
236  **/
237 
239 {
240  error_t error;
241  size_t length;
243 
244  //Point to the buffer where to format the message
245  message = (TlsClientKeyExchange *) (context->txBuffer + context->txBufferLen);
246 
247  //Format ClientKeyExchange message
248  error = tlsFormatClientKeyExchange(context, message, &length);
249 
250  //Check status code
251  if(!error)
252  {
253  //Debug message
254  TRACE_INFO("Sending ClientKeyExchange message (%" PRIuSIZE " bytes)...\r\n", length);
256 
257  //Send handshake message
258  error = tlsSendHandshakeMessage(context, message, length,
260  }
261 
262  //Check status code
263  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
264  {
265  //Derive session keys from the premaster secret
266  error = tlsGenerateSessionKeys(context);
267 
268  //Key material successfully generated?
269  if(!error)
270  {
271  //Send a CertificateVerify message to the server
273  }
274  }
275 
276  //Return status code
277  return error;
278 }
279 
280 
281 /**
282  * @brief Format ClientHello message
283  * @param[in] context Pointer to the TLS context
284  * @param[out] message Buffer where to format the ClientHello message
285  * @param[out] length Length of the resulting ClientHello message
286  * @return Error code
287  **/
288 
290  TlsClientHello *message, size_t *length)
291 {
292  error_t error;
293  size_t n;
294  uint8_t *p;
295  TlsExtensionList *extensionList;
296 
297  //In TLS 1.3, the client indicates its version preferences in the
298  //SupportedVersions extension and the legacy_version field must be set
299  //to 0x0303, which is the version number for TLS 1.2
300  context->clientVersion = MIN(context->versionMax, TLS_VERSION_1_2);
301 
302 #if (DTLS_SUPPORT == ENABLED)
303  //DTLS protocol?
304  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
305  {
306  //Translate TLS version into DTLS version
307  context->clientVersion = dtlsTranslateVersion(context->clientVersion);
308  }
309 #endif
310 
311  //In previous versions of TLS, the version field is used for version
312  //negotiation and represents the highest version number supported by the
313  //client
314  message->clientVersion = htons(context->clientVersion);
315 
316  //Client random value
317  osMemcpy(message->random, context->clientRandom, 32);
318 
319  //Point to the session ID
320  p = message->sessionId;
321  //Length of the handshake message
322  *length = sizeof(TlsClientHello);
323 
324  //The session ID value identifies a session the client wishes to reuse for
325  //this connection
326  error = tlsFormatSessionId(context, p, &n);
327  //Any error to report?
328  if(error)
329  return error;
330 
331  //Fix the length of the session ID
332  message->sessionIdLen = (uint8_t) n;
333 
334  //Point to the next field
335  p += n;
336  //Adjust the length of the message
337  *length += n;
338 
339 #if (DTLS_SUPPORT == ENABLED)
340  //DTLS protocol?
341  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
342  {
343  //Format Cookie field
344  error = dtlsFormatCookie(context, p, &n);
345  //Any error to report?
346  if(error)
347  return error;
348 
349  //Point to the next field
350  p += n;
351  //Adjust the length of the message
352  *length += n;
353  }
354 #endif
355 
356  //Format the list of cipher suites supported by the client
357  error = tlsFormatCipherSuites(context, p, &n);
358  //Any error to report?
359  if(error)
360  return error;
361 
362  //Point to the next field
363  p += n;
364  //Adjust the length of the message
365  *length += n;
366 
367  //Format the list of compression methods supported by the client
368  error = tlsFormatCompressMethods(context, p, &n);
369  //Any error to report?
370  if(error)
371  return error;
372 
373  //Point to the next field
374  p += n;
375  //Adjust the length of the message
376  *length += n;
377 
378  //Clients may request extended functionality from servers by sending
379  //data in the extensions field
380  extensionList = (TlsExtensionList *) p;
381  //Total length of the extension list
382  extensionList->length = 0;
383 
384  //Point to the first extension of the list
385  p += sizeof(TlsExtensionList);
386 
387  //In TLS 1.2, the client can indicate its version preferences in the
388  //SupportedVersions extension
389  error = tlsFormatClientSupportedVersionsExtension(context, p, &n);
390  //Any error to report?
391  if(error)
392  return error;
393 
394  //Fix the length of the extension list
395  extensionList->length += (uint16_t) n;
396  //Point to the next field
397  p += n;
398 
399 #if (TLS_SNI_SUPPORT == ENABLED)
400  //In order to provide the server name, clients may include a ServerName
401  //extension
402  error = tlsFormatClientSniExtension(context, p, &n);
403  //Any error to report?
404  if(error)
405  return error;
406 
407  //Fix the length of the extension list
408  extensionList->length += (uint16_t) n;
409  //Point to the next field
410  p += n;
411 #endif
412 
413 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
414  //In order to negotiate smaller maximum fragment lengths, clients may
415  //include a MaxFragmentLength extension
416  error = tlsFormatClientMaxFragLenExtension(context, p, &n);
417  //Any error to report?
418  if(error)
419  return error;
420 
421  //Fix the length of the extension list
422  extensionList->length += (uint16_t) n;
423  //Point to the next field
424  p += n;
425 #endif
426 
427 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
428  //The value of RecordSizeLimit is the maximum size of record in octets
429  //that the endpoint is willing to receive
430  error = tlsFormatClientRecordSizeLimitExtension(context, p, &n);
431  //Any error to report?
432  if(error)
433  return error;
434 
435  //Fix the length of the extension list
436  extensionList->length += (uint16_t) n;
437  //Point to the next field
438  p += n;
439 #endif
440 
441  //A client that proposes ECC/FFDHE cipher suites in its ClientHello message
442  //should send the SupportedGroups extension
443  error = tlsFormatSupportedGroupsExtension(context, p, &n);
444  //Any error to report?
445  if(error)
446  return error;
447 
448  //Fix the length of the extension list
449  extensionList->length += (uint16_t) n;
450  //Point to the next field
451  p += n;
452 
453  //A client that proposes ECC cipher suites in its ClientHello message
454  //should send the EcPointFormats extension
455  error = tlsFormatClientEcPointFormatsExtension(context, p, &n);
456  //Any error to report?
457  if(error)
458  return error;
459 
460  //Fix the length of the extension list
461  extensionList->length += (uint16_t) n;
462  //Point to the next field
463  p += n;
464 
465  //Include the SignatureAlgorithms extension only if TLS 1.2 is supported
466  error = tlsFormatSignAlgosExtension(context, p, &n);
467  //Any error to report?
468  if(error)
469  return error;
470 
471  //Fix the length of the extension list
472  extensionList->length += (uint16_t) n;
473  //Point to the next field
474  p += n;
475 
476 #if (TLS_SIGN_ALGOS_CERT_SUPPORT == ENABLED)
477  //The SignatureAlgorithmsCert extension allows a client to indicate which
478  //signature algorithms it can validate in X.509 certificates
479  error = tlsFormatSignAlgosCertExtension(context, p, &n);
480  //Any error to report?
481  if(error)
482  return error;
483 
484  //Fix the length of the extension list
485  extensionList->length += (uint16_t) n;
486  //Point to the next field
487  p += n;
488 #endif
489 
490 #if (TLS_ALPN_SUPPORT == ENABLED)
491  //The ALPN extension contains the list of protocols advertised by the
492  //client, in descending order of preference
493  error = tlsFormatClientAlpnExtension(context, p, &n);
494  //Any error to report?
495  if(error)
496  return error;
497 
498  //Fix the length of the extension list
499  extensionList->length += (uint16_t) n;
500  //Point to the next field
501  p += n;
502 #endif
503 
504 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
505  //In order to indicate the support of raw public keys, clients include the
506  //ClientCertType extension in an extended ClientHello message
507  error = tlsFormatClientCertTypeListExtension(context, p, &n);
508  //Any error to report?
509  if(error)
510  return error;
511 
512  //Fix the length of the extension list
513  extensionList->length += (uint16_t) n;
514  //Point to the next field
515  p += n;
516 
517  //In order to indicate the support of raw public keys, clients include the
518  //ServerCertType extension in an extended ClientHello message
519  error = tlsFormatServerCertTypeListExtension(context, p, &n);
520  //Any error to report?
521  if(error)
522  return error;
523 
524  //Fix the length of the extension list
525  extensionList->length += (uint16_t) n;
526  //Point to the next field
527  p += n;
528 #endif
529 
530 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
531  //On connecting, the client includes the EncryptThenMac extension in its
532  //ClientHello if it wishes to use encrypt-then-MAC rather than the default
533  //MAC-then-encrypt (refer to RFC 7366, section 2)
534  error = tlsFormatClientEtmExtension(context, p, &n);
535  //Any error to report?
536  if(error)
537  return error;
538 
539  //Fix the length of the extension list
540  extensionList->length += (uint16_t) n;
541  //Point to the next field
542  p += n;
543 #endif
544 
545 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
546  //In all handshakes, a client implementing RFC 7627 must send the
547  //ExtendedMasterSecret extension in its ClientHello
548  error = tlsFormatClientEmsExtension(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_TRUSTED_CA_KEYS_SUPPORT == ENABLED)
560  //In order to indicate which CA root keys they possess, clients may include
561  //an extension of type "trusted_ca_keys" in the extended ClientHello message
562  //(refer to RFC 6066, section 6)
563  error = tlsFormatTrustedCaKeysExtension(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_TICKET_SUPPORT == ENABLED)
575  //The SessionTicket extension is used to resume a TLS session without
576  //requiring session-specific state at the TLS server
577  error = tlsFormatClientSessionTicketExtension(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_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
589  //If the connection's secure_renegotiation flag is set to TRUE, the client
590  //must include a RenegotiationInfo extension in its ClientHello message
591  error = tlsFormatClientRenegoInfoExtension(context, p, &n);
592  //Any error to report?
593  if(error)
594  return error;
595 
596  //Fix the length of the extension list
597  extensionList->length += (uint16_t) n;
598  //Point to the next field
599  p += n;
600 #endif
601 
602 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
603  //TLS 1.3 supported by the client?
604  if(context->versionMax >= TLS_VERSION_1_3)
605  {
606  Tls13PskIdentityList *identityList;
607  Tls13PskBinderList *binderList;
608 
609  //Updated ClientHello?
610  if(context->state == TLS_STATE_CLIENT_HELLO_3)
611  {
612  //When sending the new ClientHello, the client must copy the contents
613  //of the Cookie extension received in the HelloRetryRequest into a
614  //Cookie extension in the new ClientHello
615  error = tls13FormatCookieExtension(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  }
625 
626  //The KeyShare extension contains the client's cryptographic parameters
627  error = tls13FormatClientKeyShareExtension(context, p, &n);
628  //Any error to report?
629  if(error)
630  return error;
631 
632  //Fix the length of the extension list
633  extensionList->length += (uint16_t) n;
634  //Point to the next field
635  p += n;
636 
637 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
638  //If the client opts to send application data in its first flight of
639  //messages, it must supply both the PreSharedKey and EarlyData extensions
640  error = tls13FormatClientEarlyDataExtension(context, p, &n);
641  //Any error to report?
642  if(error)
643  return error;
644 
645  //Fix the length of the extension list
646  extensionList->length += (uint16_t) n;
647  //Point to the next field
648  p += n;
649 #endif
650 
651  //In order to use PSKs, clients must send a PskKeyExchangeModes extension
652  error = tls13FormatPskKeModesExtension(context, p, &n);
653  //Any error to report?
654  if(error)
655  return error;
656 
657  //Fix the length of the extension list
658  extensionList->length += (uint16_t) n;
659  //Point to the next field
660  p += n;
661 
662 #if (TLS_CERT_AUTHORITIES_SUPPORT == ENABLED)
663  //The CertificateAuthorities extension is used to indicate the CAs which
664  //an endpoint supports and which should be used by the receiving endpoint
665  //to guide certificate selection
666  error = tlsFormatCertAuthoritiesExtension(context, p, &n);
667  //Any error to report?
668  if(error)
669  return error;
670 
671  //Fix the length of the extension list
672  extensionList->length += (uint16_t) n;
673  //Point to the next field
674  p += n;
675 #endif
676 
677 #if (TLS_QUIC_SUPPORT == ENABLED)
678  //QUIC transport?
679  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_QUIC)
680  {
681  //QUIC transport parameters are carried in a TLS extension (refer to
682  //RFC 9001, section 8.2)
683  error = tlsFormatQuicTransportParamsExtension(context, p, &n);
684  //Any error to report?
685  if(error)
686  return error;
687 
688  //Fix the length of the extension list
689  extensionList->length += (uint16_t) n;
690  //Point to the next field
691  p += n;
692  }
693 #endif
694 
695 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
696  //The first pass calculates the length of the PreSharedKey extension
697  error = tls13FormatClientPreSharedKeyExtension(context, p, &n,
698  &identityList, &binderList);
699  //Any error to report?
700  if(error)
701  return error;
702 
703  //Determine the length of the resulting message
704  n += *length + sizeof(TlsExtensionList) + extensionList->length;
705 
706  //Add a padding extension to ensure the ClientHello is never between
707  //256 and 511 bytes in length
708  error = tlsFormatClientHelloPaddingExtension(context, n, p, &n);
709  //Any error to report?
710  if(error)
711  return error;
712 
713  //Fix the length of the extension list
714  extensionList->length += (uint16_t) n;
715  //Point to the next field
716  p += n;
717 #endif
718 
719  //The extensions may appear in any order, with the exception of
720  //PreSharedKey which must be the last extension in the ClientHello
721  error = tls13FormatClientPreSharedKeyExtension(context, p, &n,
722  &identityList, &binderList);
723  //Any error to report?
724  if(error)
725  return error;
726 
727  //Fix the length of the extension list
728  extensionList->length += (uint16_t) n;
729  //Point to the next field
730  p += n;
731 
732  //Convert the length of the extension list to network byte order
733  extensionList->length = htons(extensionList->length);
734  //Total length of the message
735  *length += sizeof(TlsExtensionList) + htons(extensionList->length);
736 
737  //Fix PSK binder values in the PreSharedKey extension
738  error = tls13ComputePskBinders(context, message, *length, identityList,
739  binderList);
740  //Any error to report?
741  if(error)
742  return error;
743  }
744  else
745 #endif
746  {
747 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
748  //Retrieve the actual length of the message
749  n = *length;
750 
751  //Any extensions included in the ClientHello message?
752  if(extensionList->length > 0)
753  {
754  n += sizeof(TlsExtensionList) + extensionList->length;
755  }
756 
757  //Add a padding extension to ensure the ClientHello is never between
758  //256 and 511 bytes in length
759  error = tlsFormatClientHelloPaddingExtension(context, n, p, &n);
760  //Any error to report?
761  if(error)
762  return error;
763 
764  //Fix the length of the extension list
765  extensionList->length += (uint16_t) n;
766  //Point to the next field
767  p += n;
768 #endif
769 
770  //Any extensions included in the ClientHello message?
771  if(extensionList->length > 0)
772  {
773  //Convert the length of the extension list to network byte order
774  extensionList->length = htons(extensionList->length);
775  //Total length of the message
776  *length += sizeof(TlsExtensionList) + htons(extensionList->length);
777  }
778  }
779 
780  //Successful processing
781  return NO_ERROR;
782 }
783 
784 
785 /**
786  * @brief Format ClientKeyExchange message
787  * @param[in] context Pointer to the TLS context
788  * @param[out] message Buffer where to format the ClientKeyExchange message
789  * @param[out] length Length of the resulting ClientKeyExchange message
790  * @return Error code
791  **/
792 
795 {
796  error_t error;
797  size_t n;
798  uint8_t *p;
799 
800  //Point to the beginning of the handshake message
801  p = message;
802  //Length of the handshake message
803  *length = 0;
804 
805 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
806  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
807  //PSK key exchange method?
808  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
809  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
810  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
811  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
812  {
813  //The client indicates which key to use by including a PSK identity
814  //in the ClientKeyExchange message
815  error = tlsFormatPskIdentity(context, p, &n);
816  //Any error to report?
817  if(error)
818  return error;
819 
820  //Advance data pointer
821  p += n;
822  //Adjust the length of the message
823  *length += n;
824  }
825 #endif
826 
827  //RSA, Diffie-Hellman or ECDH key exchange method?
828  if(context->keyExchMethod != TLS_KEY_EXCH_PSK)
829  {
830  //Format client's key exchange parameters
831  error = tlsFormatClientKeyParams(context, p, &n);
832  //Any error to report?
833  if(error)
834  return error;
835 
836  //Advance data pointer
837  p += n;
838  //Adjust the length of the message
839  *length += n;
840  }
841 
842 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
843  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
844  //PSK key exchange method?
845  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
846  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
847  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
848  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
849  {
850  //Invalid pre-shared key?
851  if(context->pskLen == 0)
853 
854  //Generate premaster secret
855  error = tlsGeneratePskPremasterSecret(context);
856  //Any error to report?
857  if(error)
858  return error;
859  }
860 #endif
861 
862  //Successful processing
863  return NO_ERROR;
864 }
865 
866 
867 /**
868  * @brief Parse HelloRequest message
869  *
870  * HelloRequest is a simple notification that the client should begin the
871  * negotiation process anew. In response, the client should send a ClientHello
872  * message when convenient
873  *
874  * @param[in] context Pointer to the TLS context
875  * @param[in] message Incoming HelloRequest message to parse
876  * @param[in] length Message length
877  * @return Error code
878  **/
879 
881  const TlsHelloRequest *message, size_t length)
882 {
883  error_t error;
884 
885  //Debug message
886  TRACE_INFO("HelloRequest message received (%" PRIuSIZE " bytes)...\r\n", length);
888 
889  //Check TLS version
890  if(context->version > TLS_VERSION_1_2)
892 
893  //The HelloRequest message does not contain any data
894  if(length != 0)
895  return ERROR_DECODING_FAILED;
896 
897  //Check current state
898  if(context->state == TLS_STATE_APPLICATION_DATA)
899  {
900 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
901  //Check whether the secure_renegotiation flag is set
902  if(context->secureRenegoEnabled && context->secureRenegoFlag)
903  {
904  //Release existing session ticket, if any
905  if(context->ticket != NULL)
906  {
907  osMemset(context->ticket, 0, context->ticketLen);
908  tlsFreeMem(context->ticket);
909  context->ticket = NULL;
910  context->ticketLen = 0;
911  }
912 
913 #if (DTLS_SUPPORT == ENABLED)
914  //Release DTLS cookie
915  if(context->cookie != NULL)
916  {
917  tlsFreeMem(context->cookie);
918  context->cookie = NULL;
919  context->cookieLen = 0;
920  }
921 #endif
922  //HelloRequest is a simple notification that the client should begin
923  //the negotiation process anew
925 
926  //Continue processing
927  error = NO_ERROR;
928  }
929  else
930 #endif
931  {
932  //If the connection's secure_renegotiation flag is set to FALSE, it
933  //is recommended that clients refuse this renegotiation request (refer
934  //to RFC 5746, section 4.2)
935  error = tlsSendAlert(context, TLS_ALERT_LEVEL_FATAL,
937  }
938  }
939  else
940  {
941  //The HelloRequest message can be sent at any time but it should be
942  //ignored by the client if it arrives in the middle of a handshake
943  error = NO_ERROR;
944  }
945 
946  //Return status code
947  return error;
948 }
949 
950 
951 /**
952  * @brief Parse ServerHello message
953  *
954  * The server will send this message in response to a ClientHello
955  * message when it was able to find an acceptable set of algorithms.
956  * If it cannot find such a match, it will respond with a handshake
957  * failure alert
958  *
959  * @param[in] context Pointer to the TLS context
960  * @param[in] message Incoming ServerHello message to parse
961  * @param[in] length Message length
962  * @return Error code
963  **/
964 
966  const TlsServerHello *message, size_t length)
967 {
968  error_t error;
969  uint16_t cipherSuite;
970  uint8_t compressMethod;
971  const uint8_t *p;
973 
974  //Debug message
975  TRACE_INFO("ServerHello message received (%" PRIuSIZE " bytes)...\r\n", length);
977 
978  //Check current state
979  if(context->state != TLS_STATE_SERVER_HELLO &&
980  context->state != TLS_STATE_SERVER_HELLO_2 &&
981  context->state != TLS_STATE_SERVER_HELLO_3)
982  {
983  //Report an error
985  }
986 
987  //Check the length of the ServerHello message
988  if(length < sizeof(TlsServerHello))
989  return ERROR_DECODING_FAILED;
990 
991  //Point to the session ID
992  p = message->sessionId;
993  //Remaining bytes to process
994  length -= sizeof(TlsServerHello);
995 
996  //Check the length of the session ID
997  if(message->sessionIdLen > length)
998  return ERROR_DECODING_FAILED;
999  if(message->sessionIdLen > 32)
1000  return ERROR_DECODING_FAILED;
1001 
1002  //Point to the next field
1003  p += message->sessionIdLen;
1004  //Remaining bytes to process
1005  length -= message->sessionIdLen;
1006 
1007  //Malformed ServerHello message?
1008  if(length < sizeof(uint16_t))
1009  return ERROR_DECODING_FAILED;
1010 
1011  //Get the negotiated cipher suite
1012  cipherSuite = LOAD16BE(p);
1013  //Point to the next field
1014  p += sizeof(uint16_t);
1015  //Remaining bytes to process
1016  length -= sizeof(uint16_t);
1017 
1018  //Malformed ServerHello message?
1019  if(length < sizeof(uint8_t))
1020  return ERROR_DECODING_FAILED;
1021 
1022  //Get the negotiated compression method
1023  compressMethod = *p;
1024  //Point to the next field
1025  p += sizeof(uint8_t);
1026  //Remaining bytes to process
1027  length -= sizeof(uint8_t);
1028 
1029  //Server version
1030  TRACE_INFO(" serverVersion = 0x%04" PRIX16 " (%s)\r\n",
1031  ntohs(message->serverVersion),
1032  tlsGetVersionName(ntohs(message->serverVersion)));
1033 
1034  //Server random value
1035  TRACE_DEBUG(" random\r\n");
1036  TRACE_DEBUG_ARRAY(" ", message->random, 32);
1037 
1038  //Session identifier
1039  TRACE_DEBUG(" sessionId\r\n");
1040  TRACE_DEBUG_ARRAY(" ", message->sessionId, message->sessionIdLen);
1041 
1042  //Cipher suite identifier
1043  TRACE_INFO(" cipherSuite = 0x%04" PRIX16 " (%s)\r\n",
1045 
1046  //Compression method
1047  TRACE_DEBUG(" compressMethod = 0x%02" PRIX8 "\r\n", compressMethod);
1048 
1049  //The CRIME exploit takes advantage of TLS compression, so conservative
1050  //implementations do not accept compression at the TLS level
1051  if(compressMethod != TLS_COMPRESSION_METHOD_NULL)
1052  return ERROR_ILLEGAL_PARAMETER;
1053 
1054  //Parse the list of extensions offered by the server
1056  &extensions);
1057  //Any error to report?
1058  if(error)
1059  return error;
1060 
1061  //TLS protocol?
1062  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM ||
1063  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_QUIC ||
1064  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP)
1065  {
1066  //Check whether the ServerHello message is received in response to the
1067  //initial ClientHello
1068  if(context->state != TLS_STATE_SERVER_HELLO_2)
1069  {
1070  //Release transcript hash context
1071  tlsFreeTranscriptHash(context);
1072 
1073  //Format initial ClientHello message
1074  error = tlsFormatInitialClientHello(context);
1075  //Any error to report?
1076  if(error)
1077  return error;
1078  }
1079  }
1080 
1081  //Select TLS version
1082  error = tlsSelectClientVersion(context, message, &extensions);
1083  //TLS version not supported?
1084  if(error)
1085  return error;
1086 
1087  //Check the list of extensions offered by the server
1088  error = tlsCheckHelloExtensions(TLS_TYPE_SERVER_HELLO, context->version,
1089  &extensions);
1090  //Any error to report?
1091  if(error)
1092  return error;
1093 
1094  //Save server random value
1095  osMemcpy(context->serverRandom, message->random, 32);
1096 
1097 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1098  //TLS 1.0, TLS 1.1 or TLS 1.2 currently selected?
1099  if(context->version <= TLS_VERSION_1_2)
1100  {
1101  //Reset the named group to its default value
1102  context->namedGroup = TLS_GROUP_NONE;
1103 
1104  //Check whether the server has decided to resume a previous session
1105  error = tlsResumeSession(context, message->sessionId,
1106  message->sessionIdLen, cipherSuite);
1107  //Any error to report?
1108  if(error)
1109  return error;
1110 
1111  //Set cipher suite
1112  error = tlsSelectCipherSuite(context, cipherSuite);
1113  //Specified cipher suite not supported?
1114  if(error)
1115  return error;
1116 
1117  //Initialize handshake message hashing
1118  error = tlsInitTranscriptHash(context);
1119  //Any error to report?
1120  if(error)
1121  return error;
1122 
1123  //Save session identifier
1124  osMemcpy(context->sessionId, message->sessionId, message->sessionIdLen);
1125  context->sessionIdLen = message->sessionIdLen;
1126 
1127 #if (TLS_TICKET_SUPPORT == ENABLED)
1128  //Parse SessionTicket extension
1129  error = tlsParseServerSessionTicketExtension(context,
1130  extensions.sessionTicket);
1131  //Any error to report?
1132  if(error)
1133  return error;
1134 #endif
1135 
1136 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1137  //Parse RenegotiationInfo extension
1138  error = tlsParseServerRenegoInfoExtension(context, &extensions);
1139  //Any error to report?
1140  if(error)
1141  return error;
1142 #endif
1143 
1144 #if (TLS_SNI_SUPPORT == ENABLED)
1145  //When the server includes a ServerName extension, the data field of
1146  //this extension may be empty
1147  error = tlsParseServerSniExtension(context, extensions.serverNameList);
1148  //Any error to report?
1149  if(error)
1150  return error;
1151 #endif
1152 
1153 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
1154  //Servers that receive an ClientHello containing a MaxFragmentLength
1155  //extension may accept the requested maximum fragment length by including
1156  //an extension of type MaxFragmentLength in the ServerHello
1157  error = tlsParseServerMaxFragLenExtension(context, extensions.maxFragLen);
1158  //Any error to report?
1159  if(error)
1160  return error;
1161 #endif
1162 
1163 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1164  //The value of RecordSizeLimit is the maximum size of record in octets
1165  //that the peer is willing to receive
1167  extensions.recordSizeLimit);
1168  //Any error to report?
1169  if(error)
1170  return error;
1171 #endif
1172 
1173 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1174  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1175  //A server that selects an ECC cipher suite in response to a ClientHello
1176  //message including an EcPointFormats extension appends this extension
1177  //to its ServerHello message
1178  error = tlsParseServerEcPointFormatsExtension(context,
1179  extensions.ecPointFormatList);
1180  //Any error to report?
1181  if(error)
1182  return error;
1183 #endif
1184 
1185 #if (TLS_ALPN_SUPPORT == ENABLED)
1186  //Parse ALPN extension
1187  error = tlsParseServerAlpnExtension(context, extensions.protocolNameList);
1188  //Any error to report?
1189  if(error)
1190  return error;
1191 #endif
1192 
1193 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1194  //Parse ClientCertType extension
1195  error = tlsParseClientCertTypeExtension(context, extensions.clientCertType);
1196  //Any error to report?
1197  if(error)
1198  return error;
1199 
1200  //Parse ServerCertType extension
1201  error = tlsParseServerCertTypeExtension(context, extensions.serverCertType);
1202  //Any error to report?
1203  if(error)
1204  return error;
1205 #endif
1206 
1207 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1208  //Parse EncryptThenMac extension
1209  error = tlsParseServerEtmExtension(context, extensions.encryptThenMac);
1210  //Any error to report?
1211  if(error)
1212  return error;
1213 #endif
1214 
1215 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1216  //Parse ExtendedMasterSecret extension
1217  error = tlsParseServerEmsExtension(context, extensions.extendedMasterSecret);
1218  //Any error to report?
1219  if(error)
1220  return error;
1221 #endif
1222 
1223 #if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
1224  //Use abbreviated handshake?
1225  if(context->resume)
1226  {
1227  //Derive session keys from the master secret
1228  error = tlsGenerateSessionKeys(context);
1229  //Unable to generate key material?
1230  if(error)
1231  return error;
1232 
1233 #if (TLS_TICKET_SUPPORT == ENABLED)
1234  //The server uses the SessionTicket extension to indicate to the client
1235  //that it will send a new session ticket using the NewSessionTicket
1236  //handshake message
1237  if(context->sessionTicketExtReceived)
1238  {
1239  //Wait for a NewSessionTicket message from the server
1241  }
1242  else
1243 #endif
1244  {
1245  //At this point, both client and server must send ChangeCipherSpec
1246  //messages and proceed directly to Finished messages
1248  }
1249  }
1250  else
1251 #endif
1252  {
1253  //Perform a full handshake
1254  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1255  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1256  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1257  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1258  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1259  {
1260  //The Certificate message is omitted from the server's response
1262  }
1263  else
1264  {
1265  //The server is required to send a Certificate message
1267  }
1268  }
1269  }
1270  else
1271 #endif
1272 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1273  //TLS 1.3 currently selected?
1274  if(context->version == TLS_VERSION_1_3)
1275  {
1276  //A client which receives a legacy_session_id_echo field that does not
1277  //match what it sent in the ClientHello must abort the handshake with an
1278  //illegal_parameter alert (RFC 8446, section 4.1.3)
1279  if(message->sessionIdLen != context->sessionIdLen ||
1280  osMemcmp(message->sessionId, context->sessionId, message->sessionIdLen))
1281  {
1282  //The legacy_session_id_echo field is not valid
1283  return ERROR_ILLEGAL_PARAMETER;
1284  }
1285 
1286  //Check whether the ServerHello message is received in response to the
1287  //initial or the updated ClientHello
1288  if(context->state != TLS_STATE_SERVER_HELLO_2)
1289  {
1290  //Set cipher suite
1291  error = tlsSelectCipherSuite(context, cipherSuite);
1292  //Specified cipher suite not supported?
1293  if(error)
1294  return error;
1295 
1296  //Initialize handshake message hashing
1297  error = tlsInitTranscriptHash(context);
1298  //Any error to report?
1299  if(error)
1300  return error;
1301  }
1302  else
1303  {
1304  //Clients must check that the cipher suite supplied in the ServerHello
1305  //is the same as that in the HelloRetryRequest and otherwise abort the
1306  //handshake with an illegal_parameter alert
1307  if(cipherSuite != context->cipherSuite.identifier)
1308  return ERROR_ILLEGAL_PARAMETER;
1309  }
1310 
1311  //If using (EC)DHE key establishment, servers offer exactly one
1312  //KeyShareEntry in the ServerHello
1313  error = tls13ParseServerKeyShareExtension(context,
1314  extensions.serverShare);
1315  //Any error to report?
1316  if(error)
1317  return error;
1318 
1319  //The PreSharedKey extension contains the selected PSK identity
1320  error = tls13ParseServerPreSharedKeyExtension(context,
1321  extensions.selectedIdentity);
1322  //Any error to report?
1323  if(error)
1324  return error;
1325 
1326  //In TLS 1.3, the cipher suite concept has been changed. The key exchange
1327  //mechanism is negotiated separately from the cipher suite
1328  if(context->keyExchMethod == TLS_KEY_EXCH_NONE)
1329  return ERROR_HANDSHAKE_FAILED;
1330 
1331 #if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
1332  //The middlebox compatibility mode improves the chance of successfully
1333  //connecting through middleboxes
1334  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
1335  context->state == TLS_STATE_SERVER_HELLO)
1336  {
1337  //In middlebox compatibility mode, the client sends a dummy
1338  //ChangeCipherSpec record immediately before its second flight
1340  }
1341  else
1342 #endif
1343  {
1344  //All handshake messages after the ServerHello are now encrypted
1346  }
1347  }
1348  else
1349 #endif
1350  //Invalid TLS version?
1351  {
1352  //Just for sanity
1353  return ERROR_INVALID_VERSION;
1354  }
1355 
1356  //Successful processing
1357  return NO_ERROR;
1358 }
1359 
1360 
1361 /**
1362  * @brief Parse ServerKeyExchange message
1363  *
1364  * The ServerKeyExchange message is sent by the server only when the
1365  * server Certificate message does not contain enough data to allow
1366  * the client to exchange a premaster secret
1367  *
1368  * @param[in] context Pointer to the TLS context
1369  * @param[in] message Incoming ServerKeyExchange message to parse
1370  * @param[in] length Message length
1371  * @return Error code
1372  **/
1373 
1375  const TlsServerKeyExchange *message, size_t length)
1376 {
1377  error_t error;
1378  size_t n;
1379  size_t paramsLen;
1380  const uint8_t *p;
1381  const uint8_t *params;
1382 
1383  //Initialize variables
1384  params = NULL;
1385  paramsLen = 0;
1386 
1387  //Debug message
1388  TRACE_INFO("ServerKeyExchange message received (%" PRIuSIZE " bytes)...\r\n", length);
1390 
1391  //Check TLS version
1392  if(context->version > TLS_VERSION_1_2)
1393  return ERROR_UNEXPECTED_MESSAGE;
1394 
1395  //Check current state
1396  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE)
1397  return ERROR_UNEXPECTED_MESSAGE;
1398 
1399  //Point to the beginning of the handshake message
1400  p = message;
1401 
1402 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
1403  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1404  //PSK key exchange method?
1405  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1406  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
1407  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1408  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1409  {
1410  //To help the client in selecting which identity to use, the server
1411  //can provide a PSK identity hint in the ServerKeyExchange message
1412  error = tlsParsePskIdentityHint(context, p, length, &n);
1413  //Any error to report?
1414  if(error)
1415  return error;
1416 
1417  //Point to the next field
1418  p += n;
1419  //Remaining bytes to process
1420  length -= n;
1421  }
1422 #endif
1423 
1424  //Diffie-Hellman or ECDH key exchange method?
1425  if(context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1426  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1427  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1428  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1429  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1430  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1431  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
1432  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1433  {
1434  //Point to the server's key exchange parameters
1435  params = p;
1436 
1437  //Parse server's key exchange parameters
1438  error = tlsParseServerKeyParams(context, p, length, &paramsLen);
1439  //Any error to report?
1440  if(error)
1441  return error;
1442 
1443  //Point to the next field
1444  p += paramsLen;
1445  //Remaining bytes to process
1446  length -= paramsLen;
1447  }
1448 
1449  //For non-anonymous Diffie-Hellman and ECDH key exchanges, the signature
1450  //over the server's key exchange parameters shall be verified
1451  if(context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1452  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1453  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1454  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
1455  {
1456 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
1457  //TLS 1.0 or TLS 1.1 currently selected?
1458  if(context->version <= TLS_VERSION_1_1)
1459  {
1460  //Signature verification
1461  error = tlsVerifyServerKeySignature(context,
1462  (TlsDigitalSignature *) p, length, params, paramsLen, &n);
1463  }
1464  else
1465 #endif
1466 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1467  //TLS 1.2 currently selected?
1468  if(context->version == TLS_VERSION_1_2)
1469  {
1470  //Signature verification
1471  error = tls12VerifyServerKeySignature(context,
1472  (Tls12DigitalSignature *) p, length, params, paramsLen, &n);
1473  }
1474  else
1475 #endif
1476  {
1477  //Just for sanity
1478  (void) params;
1479  //Report an error
1480  error = ERROR_INVALID_VERSION;
1481  }
1482 
1483  //Any error to report?
1484  if(error)
1485  return error;
1486 
1487  //Point to the next field
1488  p += n;
1489  //Remaining bytes to process
1490  length -= n;
1491  }
1492 
1493  //If the amount of data in the message does not precisely match the format
1494  //of the ServerKeyExchange message, then send a fatal alert
1495  if(length != 0)
1496  return ERROR_DECODING_FAILED;
1497 
1498  //Anomynous server?
1499  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1500  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1501  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1502  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1503  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1504  {
1505  //An anonymous server cannot request client authentication
1507  }
1508  else
1509  {
1510  //A non-anonymous server can optionally request a certificate from
1511  //the client, if appropriate for the selected cipher suite
1513  }
1514 
1515  //Successful processing
1516  return NO_ERROR;
1517 }
1518 
1519 
1520 /**
1521  * @brief Parse CertificateRequest message
1522  *
1523  * A server can optionally request a certificate from the client, if
1524  * appropriate for the selected cipher suite. This message will
1525  * immediately follow the ServerKeyExchange message
1526  *
1527  * @param[in] context Pointer to the TLS context
1528  * @param[in] message Incoming CertificateRequest message to parse
1529  * @param[in] length Message length
1530  * @return Error code
1531  **/
1532 
1534  const TlsCertificateRequest *message, size_t length)
1535 {
1536  error_t error;
1537  uint_t i;
1538  uint_t j;
1539  size_t n;
1540  uint_t certTypesLen;
1541  bool_t acceptable;
1542  const uint8_t *p;
1543  const uint8_t *certTypes;
1544  const TlsCertAuthorities *certAuthorities;
1545  const TlsSignSchemeList *signAlgoList;
1546  const TlsSignSchemeList *certSignAlgoList;
1547 
1548  //Debug message
1549  TRACE_INFO("CertificateRequest message received (%" PRIuSIZE " bytes)...\r\n", length);
1551 
1552  //Check key exchange method
1553  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1554  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1555  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1556  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1557  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1558  {
1559  //It is a fatal handshake failure alert for an anonymous server to
1560  //request client authentication
1561  return ERROR_HANDSHAKE_FAILED;
1562  }
1563  else if(context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1564  {
1565  //If no PSK identity hint is provided by the server, then the
1566  //ServerKeyExchange message is omitted
1567  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1568  context->state != TLS_STATE_CERTIFICATE_REQUEST)
1569  {
1570  //Handshake failure
1571  return ERROR_UNEXPECTED_MESSAGE;
1572  }
1573  }
1574  else if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
1575  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
1576  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE ||
1577  context->keyExchMethod == TLS13_KEY_EXCH_PSK_HYBRID)
1578  {
1579  //Servers which are authenticating with a PSK must not send the
1580  //CertificateRequest message in the main handshake
1581  return ERROR_UNEXPECTED_MESSAGE;
1582  }
1583  else
1584  {
1585  //Check current state
1586  if(context->state != TLS_STATE_CERTIFICATE_REQUEST)
1587  return ERROR_UNEXPECTED_MESSAGE;
1588  }
1589 
1590  //The server requests a certificate from the client, so that connection
1591  //can be mutually authenticated
1592  context->clientCertRequested = TRUE;
1593 
1594  //Point to the beginning of the handshake message
1595  p = (uint8_t *) message;
1596 
1597 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1598  //Version of TLS prior to TLS 1.3?
1599  if(context->version <= TLS_VERSION_1_2)
1600  {
1601  //Check the length of the ServerKeyExchange message
1602  if(length < sizeof(TlsCertificateRequest))
1603  return ERROR_DECODING_FAILED;
1604 
1605  //Remaining bytes to process
1606  length -= sizeof(TlsCertificateRequest);
1607 
1608  //Retrieve the length of the list
1609  n = message->certificateTypesLen;
1610  //Malformed CertificateRequest message?
1611  if(n > length)
1612  return ERROR_DECODING_FAILED;
1613 
1614  //Point to the list of supported certificate types
1615  certTypes = message->certificateTypes;
1616  certTypesLen = message->certificateTypesLen;
1617 
1618  //Point to the next field
1619  p += sizeof(TlsCertificateRequest) + n;
1620  //Remaining bytes to process
1621  length -= n;
1622 
1623  //TLS 1.2 currently selected?
1624  if(context->version == TLS_VERSION_1_2)
1625  {
1626  //Malformed CertificateRequest message?
1627  if(length < sizeof(TlsSignSchemeList))
1628  return ERROR_DECODING_FAILED;
1629 
1630  //Point to the list of the hash/signature algorithm pairs
1631  signAlgoList = (TlsSignSchemeList *) p;
1632  //Remaining bytes to process
1633  length -= sizeof(TlsSignSchemeList);
1634 
1635  //Retrieve the length of the list
1636  n = ntohs(signAlgoList->length);
1637  //Malformed CertificateRequest message?
1638  if(n > length)
1639  return ERROR_DECODING_FAILED;
1640 
1641  //The supported_signature_algorithms field cannot be empty (refer to
1642  //RFC 5246, section 7.4.4)
1643  if(n == 0)
1644  return ERROR_DECODING_FAILED;
1645  if((n % 2) != 0)
1646  return ERROR_DECODING_FAILED;
1647 
1648  //Point to the next field
1649  p += sizeof(TlsSignSchemeList) + n;
1650  //Remaining bytes to process
1651  length -= n;
1652  }
1653  else
1654  {
1655  //Implementations prior to TLS 1.2 do not include a list of supported
1656  //hash/signature algorithm pairs
1657  signAlgoList = NULL;
1658  }
1659 
1660  //List of signature algorithms that may appear in X.509 certificates
1661  certSignAlgoList = signAlgoList;
1662 
1663  //Malformed CertificateRequest message?
1664  if(length < sizeof(TlsCertAuthorities))
1665  return ERROR_DECODING_FAILED;
1666 
1667  //Point to the list of acceptable certificate authorities
1668  certAuthorities = (TlsCertAuthorities *) p;
1669  //Remaining bytes to process
1670  length -= sizeof(TlsCertAuthorities);
1671 
1672  //Retrieve the length of the list
1673  n = ntohs(certAuthorities->length);
1674  //Malformed CertificateRequest message?
1675  if(n != length)
1676  return ERROR_DECODING_FAILED;
1677  }
1678  else
1679 #endif
1680 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1681  //TLS 1.3 currently selected?
1682  if(context->version == TLS_VERSION_1_3)
1683  {
1685  const Tls13CertRequestContext *certRequestContext;
1686 
1687  //Unused parameters
1688  certTypes = NULL;
1689  certTypesLen = 0;
1690 
1691  //Malformed CertificateRequest message?
1692  if(length < sizeof(Tls13CertRequestContext))
1693  return ERROR_DECODING_FAILED;
1694 
1695  //Point to the certificate_request_context field
1696  certRequestContext = (Tls13CertRequestContext *) p;
1697  //Remaining bytes to process
1698  length -= sizeof(Tls13CertRequestContext);
1699 
1700  //Retrieve the length of the field
1701  n = certRequestContext->length;
1702  //Malformed CertificateRequest message?
1703  if(n > length)
1704  return ERROR_DECODING_FAILED;
1705 
1706  //The certificate_request_context field shall be zero length unless
1707  //used for the post-handshake authentication exchange
1708  if(certRequestContext->length != 0)
1709  return ERROR_ILLEGAL_PARAMETER;
1710 
1711  //Point to the next field
1712  p += sizeof(Tls13CertRequestContext) + n;
1713  //Remaining bytes to process
1714  length -= n;
1715 
1716  //The extensions describe the parameters of the certificate being
1717  //requested
1719  length, &extensions);
1720  //Any error to report?
1721  if(error)
1722  return error;
1723 
1724  //Check the list of extensions offered by the server
1726  context->version, &extensions);
1727  //Any error to report?
1728  if(error)
1729  return error;
1730 
1731  //The SignatureAlgorithms extension must be specified (refer to RFC 8446,
1732  //section 4.3.2)
1733  if(extensions.signAlgoList == NULL)
1734  return ERROR_MISSING_EXTENSION;
1735 
1736  //Point to the list of the hash/signature algorithm pairs that
1737  //the server is able to verify
1738  signAlgoList = extensions.signAlgoList;
1739 
1740  //If no SignatureAlgorithmsCert extension is present, then the
1741  //SignatureAlgorithms extension also applies to signatures appearing
1742  //in certificates (RFC 8446, section 4.2.3)
1743  if(extensions.certSignAlgoList != NULL)
1744  {
1745  certSignAlgoList = extensions.certSignAlgoList;
1746  }
1747  else
1748  {
1749  certSignAlgoList = extensions.signAlgoList;
1750  }
1751 
1752  //The CertificateAuthorities extension is used to indicate the CAs which
1753  //an endpoint supports and which should be used by the receiving endpoint
1754  //to guide certificate selection
1755  certAuthorities = extensions.certAuthorities;
1756  }
1757  else
1758 #endif
1759  //Invalid TLS version?
1760  {
1761  //Report an error
1762  return ERROR_INVALID_VERSION;
1763  }
1764 
1765  //No suitable certificate has been found for the moment
1766  context->cert = NULL;
1767  acceptable = FALSE;
1768 
1769  //Select the most appropriate certificate (2-pass process)
1770  for(i = 0; i < 2 && !acceptable; i++)
1771  {
1772  //Loop through the list of available certificates
1773  for(j = 0; j < TLS_MAX_CERTIFICATES && !acceptable; j++)
1774  {
1775  //Check whether the current certificate is suitable
1776  acceptable = tlsIsCertificateAcceptable(context, &context->certs[j],
1777  certTypes, certTypesLen, NULL, certSignAlgoList, certAuthorities);
1778 
1779  //TLS 1.2 and TLS 1.3 require additional examinations
1780  if(acceptable)
1781  {
1782  //The hash and signature algorithms used in the signature of the
1783  //CertificateVerify message must be one of those present in the
1784  //SupportedSignatureAlgorithms field
1785  error = tlsSelectSignAlgo(context, &context->certs[j],
1786  signAlgoList);
1787 
1788  //Check status code
1789  if(error)
1790  {
1791  acceptable = FALSE;
1792  }
1793  }
1794 
1795  //If all the requirements were met, the certificate can be used
1796  if(acceptable)
1797  {
1798  context->cert = &context->certs[j];
1799  }
1800  }
1801 
1802  //The second pass relaxes the constraints
1803  certSignAlgoList = NULL;
1804  certAuthorities = NULL;
1805  }
1806 
1807  //Version of TLS prior to TLS 1.3?
1808  if(context->version <= TLS_VERSION_1_2)
1809  {
1810  //Wait for a ServerHelloDone message
1812  }
1813  else
1814  {
1815  //Wait for a Certificate message
1817  }
1818 
1819  //Successful processing
1820  return NO_ERROR;
1821 }
1822 
1823 
1824 /**
1825  * @brief Parse ServerHelloDone message
1826  *
1827  * The ServerHelloDone message is sent by the server to indicate the
1828  * end of the ServerHello and associated messages. After sending this
1829  * message, the server will wait for a client response
1830  *
1831  * @param[in] context Pointer to the TLS context
1832  * @param[in] message Incoming ServerHelloDone message to parse
1833  * @param[in] length Message length
1834  * @return Error code
1835  **/
1836 
1838  const TlsServerHelloDone *message, size_t length)
1839 {
1840  //Debug message
1841  TRACE_INFO("ServerHelloDone message received (%" PRIuSIZE " bytes)...\r\n", length);
1843 
1844  //Check TLS version
1845  if(context->version > TLS_VERSION_1_2)
1846  return ERROR_UNEXPECTED_MESSAGE;
1847 
1848  //Check key exchange method
1849  if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
1850  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1851  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1852  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1853  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
1854  {
1855  //The server may omit the CertificateRequest message and go
1856  //directly to the ServerHelloDone message
1857  if(context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1858  context->state != TLS_STATE_SERVER_HELLO_DONE)
1859  {
1860  //Handshake failure
1861  return ERROR_UNEXPECTED_MESSAGE;
1862  }
1863  }
1864  else if(context->keyExchMethod == TLS_KEY_EXCH_PSK)
1865  {
1866  //If no PSK identity hint is provided by the server, the
1867  //ServerKeyExchange message is omitted
1868  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1869  context->state != TLS_STATE_SERVER_HELLO_DONE)
1870  {
1871  //Handshake failure
1872  return ERROR_UNEXPECTED_MESSAGE;
1873  }
1874  }
1875  else if(context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1876  {
1877  //The server may omit the ServerKeyExchange message and/or
1878  //the CertificateRequest message
1879  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1880  context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1881  context->state != TLS_STATE_SERVER_HELLO_DONE)
1882  {
1883  //Handshake failure
1884  return ERROR_UNEXPECTED_MESSAGE;
1885  }
1886  }
1887  else
1888  {
1889  //Check current state
1890  if(context->state != TLS_STATE_SERVER_HELLO_DONE)
1891  return ERROR_UNEXPECTED_MESSAGE;
1892  }
1893 
1894  //The ServerHelloDone message does not contain any data
1895  if(length != 0)
1896  return ERROR_DECODING_FAILED;
1897 
1898  //Another handshake message cannot be packed in the same record as the
1899  //ServerHelloDone
1900  if(context->rxBufferLen != 0)
1901  return ERROR_UNEXPECTED_MESSAGE;
1902 
1903  //The client must send a Certificate message if the server requests it
1905 
1906  //Successful processing
1907  return NO_ERROR;
1908 }
1909 
1910 
1911 /**
1912  * @brief Parse NewSessionTicket message
1913  *
1914  * This NewSessionTicket message is sent by the server during the TLS handshake
1915  * before the ChangeCipherSpec message
1916  *
1917  * @param[in] context Pointer to the TLS context
1918  * @param[in] message Incoming NewSessionTicket message to parse
1919  * @param[in] length Message length
1920  * @return Error code
1921  **/
1922 
1924  const TlsNewSessionTicket *message, size_t length)
1925 {
1926  size_t n;
1927 
1928  //Debug message
1929  TRACE_INFO("NewSessionTicket message received (%" PRIuSIZE " bytes)...\r\n", length);
1931 
1932  //Check TLS version
1933  if(context->version > TLS_VERSION_1_2)
1934  return ERROR_UNEXPECTED_MESSAGE;
1935 
1936  //Check current state
1937  if(context->state != TLS_STATE_NEW_SESSION_TICKET)
1938  return ERROR_UNEXPECTED_MESSAGE;
1939 
1940  //Check the length of the NewSessionTicket message
1941  if(length < sizeof(TlsNewSessionTicket))
1942  return ERROR_DECODING_FAILED;
1943 
1944  //Retrieve the length of the ticket
1945  n = ntohs(message->ticketLen);
1946 
1947  //Malformed NewSessionTicket message?
1948  if(length != (sizeof(TlsNewSessionTicket) + n))
1949  return ERROR_DECODING_FAILED;
1950 
1951 #if (TLS_TICKET_SUPPORT == ENABLED)
1952  //This message must not be sent if the server did not include a SessionTicket
1953  //extension in the ServerHello (refer to RFC 5077, section 3.3)
1954  if(!context->sessionTicketExtReceived)
1955  return ERROR_UNEXPECTED_MESSAGE;
1956 
1957  //Check the length of the session ticket
1958  if(n > 0 && n <= TLS_MAX_TICKET_SIZE)
1959  {
1960  //Release existing session ticket, if any
1961  if(context->ticket != NULL)
1962  {
1963  osMemset(context->ticket, 0, context->ticketLen);
1964  tlsFreeMem(context->ticket);
1965  context->ticket = NULL;
1966  context->ticketLen = 0;
1967  }
1968 
1969  //Allocate a memory block to hold the ticket
1970  context->ticket = tlsAllocMem(n);
1971  //Failed to allocate memory?
1972  if(context->ticket == NULL)
1973  return ERROR_OUT_OF_MEMORY;
1974 
1975  //Copy session ticket
1976  osMemcpy(context->ticket, message->ticket, n);
1977  context->ticketLen = n;
1978 
1979  //The lifetime is relative to when the ticket is received (refer to
1980  //RFC 5077, appendix A)
1981  context->ticketTimestamp = osGetSystemTime();
1982 
1983  //The ticket_lifetime_hint field contains a hint from the server about
1984  //how long the ticket should be stored. A ticket lifetime value of zero
1985  //indicates that the lifetime of the ticket is unspecified
1986  context->ticketLifetime = ntohl(message->ticketLifetimeHint);
1987 
1988  //If the client receives a session ticket from the server, then it
1989  //discards any session ID that was sent in the ServerHello (refer to
1990  //RFC 5077, section 3.4)
1991  context->sessionIdLen = 0;
1992  }
1993 #endif
1994 
1995  //The NewSessionTicket message is sent by the server during the TLS handshake
1996  //before the ChangeCipherSpec message
1998 
1999  //Successful processing
2000  return NO_ERROR;
2001 }
2002 
2003 #endif
@ TLS13_KEY_EXCH_PSK
Definition: tls.h:1207
error_t tlsParseHelloRequest(TlsContext *context, const TlsHelloRequest *message, size_t length)
Parse HelloRequest message.
Definition: tls_client.c:880
#define tlsAllocMem(size)
Definition: tls.h:888
#define htons(value)
Definition: cpu_endian.h:413
DTLS (Datagram Transport Layer Security)
Parsing and checking of TLS extensions.
TLS helper functions.
Date and time management.
uint8_t extensions[]
Definition: ntp_common.h:213
@ TLS_TRANSPORT_PROTOCOL_QUIC
Definition: tls.h:1001
Tls13PskBinderList
Definition: tls13_misc.h:275
int bool_t
Definition: compiler_port.h:63
TLS cipher suites.
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls.h:2005
TlsDigitalSignature
Definition: tls.h:1839
@ TLS_ALERT_NO_RENEGOTIATION
Definition: tls.h:1153
@ ERROR_WOULD_BLOCK
Definition: error.h:96
void TlsServerHelloDone
ServerHelloDone message.
Definition: tls.h:1941
@ TLS13_KEY_EXCH_PSK_DHE
Definition: tls.h:1208
error_t tlsFormatClientSupportedVersionsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedVersions extension.
error_t tlsGenerateSessionId(TlsContext *context, size_t length)
Generate a random session identifier.
Definition: tls_misc.c:268
Key material generation.
TLS handshake.
@ TLS_STATE_SERVER_KEY_EXCHANGE
Definition: tls.h:1554
error_t tlsGeneratePskPremasterSecret(TlsContext *context)
Premaster secret generation (for PSK cipher suites)
@ TLS_COMPRESSION_METHOD_NULL
Definition: tls.h:1173
error_t tlsFormatClientEcPointFormatsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EcPointFormats extension.
error_t tlsVerifyServerKeySignature(TlsContext *context, const TlsDigitalSignature *signature, size_t length, const uint8_t *params, size_t paramsLen, size_t *consumed)
Verify server's key exchange parameters signature (TLS 1.0 and TLS 1.1)
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:244
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
error_t tlsFormatCipherSuites(TlsContext *context, uint8_t *p, size_t *written)
Format the list of cipher suites supported by the client.
QUIC helper functions.
uint8_t p
Definition: ndp.h:300
Helper functions for TLS client.
TlsCertificateRequest
Definition: tls.h:1934
uint8_t message[]
Definition: chap.h:154
#define TRUE
Definition: os_port.h:50
bool_t tlsIsCertificateAcceptable(TlsContext *context, const TlsCertDesc *cert, const uint8_t *certTypes, size_t numCertTypes, const TlsSupportedGroupList *curveList, const TlsSignSchemeList *certSignAlgoList, const TlsCertAuthorities *certAuthorities)
Check whether a certificate is acceptable.
@ TLS_STATE_CERTIFICATE_REQUEST
Definition: tls.h:1556
error_t tlsResumeSession(TlsContext *context, const uint8_t *sessionId, size_t sessionIdLen, uint16_t cipherSuite)
Resume TLS session via session ID.
error_t tlsFormatClientKeyExchange(TlsContext *context, TlsClientKeyExchange *message, size_t *length)
Format ClientKeyExchange message.
Definition: tls_client.c:793
error_t tls13ParseServerKeyShareExtension(TlsContext *context, const Tls13KeyShareEntry *serverShare)
Parse KeyShare extension (ServerHello message)
error_t tlsParseServerRecordSizeLimitExtension(TlsContext *context, const TlsExtension *recordSizeLimit)
Parse RecordSizeLimit extension.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:1000
error_t tlsFormatSignAlgosExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SignatureAlgorithms extension.
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1572
error_t tlsFormatClientSessionTicketExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SessionTicket extension.
#define osMemcmp(p1, p2, length)
Definition: os_port.h:156
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t tlsFormatSessionId(TlsContext *context, uint8_t *p, size_t *written)
Format session ID.
error_t tlsParseServerSniExtension(TlsContext *context, const TlsServerNameList *serverNameList)
Parse SNI extension.
error_t tlsParseClientCertTypeExtension(TlsContext *context, const TlsExtension *clientCertType)
Parse ClientCertType extension.
error_t tls13FormatCookieExtension(TlsContext *context, uint8_t *p, size_t *written)
Format Cookie extension.
@ ERROR_INVALID_VERSION
Definition: error.h:118
error_t tlsSendHandshakeMessage(TlsContext *context, const void *data, size_t length, TlsMessageType type)
Send handshake message.
@ TLS13_KEY_EXCH_PSK_HYBRID
Definition: tls.h:1211
error_t tlsParsePskIdentityHint(TlsContext *context, const uint8_t *p, size_t length, size_t *consumed)
Parse PSK identity hint.
error_t tlsFormatSupportedGroupsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedGroups extension.
error_t tlsFormatClientSniExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SNI extension.
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1542
error_t tls13FormatClientEarlyDataExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EarlyData extension.
@ TLS_KEY_EXCH_RSA
Definition: tls.h:1185
error_t tlsParseServerEtmExtension(TlsContext *context, const TlsExtension *encryptThenMac)
Parse EncryptThenMac extension.
TlsExtensionList
Definition: tls.h:1706
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1548
error_t tlsFormatTrustedCaKeysExtension(TlsContext *context, uint8_t *p, size_t *written)
Format TrustedCaKeys extension.
@ TLS_KEY_EXCH_ECDHE_ECDSA
Definition: tls.h:1194
@ TLS_KEY_EXCH_ECDHE_RSA
Definition: tls.h:1192
#define FALSE
Definition: os_port.h:46
@ TLS_KEY_EXCH_ECDH_ANON
Definition: tls.h:1195
error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
Send Alert message.
Definition: tls_common.c:524
error_t tlsParseServerMaxFragLenExtension(TlsContext *context, const TlsExtension *maxFragLen)
Parse MaxFragmentLength extension.
TlsCertAuthorities
Definition: tls.h:1661
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t tlsParseServerCertTypeExtension(TlsContext *context, const TlsExtension *serverCertType)
Parse ServerCertType extension.
DTLS record layer.
error_t tlsSendClientKeyExchange(TlsContext *context)
Send ClientKeyExchange message.
Definition: tls_client.c:238
bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given named group is supported.
Definition: tls13_misc.c:953
error_t tlsFormatClientRenegoInfoExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RenegotiationInfo extension.
error_t tlsFormatClientEtmExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EncryptThenMac extension.
void tlsFreeEncryptionEngine(TlsEncryptionEngine *encryptionEngine)
Release encryption engine.
Definition: tls_misc.c:1047
#define TLS_VERSION_1_2
Definition: tls.h:96
@ TLS_GROUP_NONE
Definition: tls.h:1451
@ TLS_KEY_EXCH_DH_ANON
Definition: tls.h:1190
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
Definition: tls_misc.c:333
error_t tlsParseHelloExtensions(TlsMessageType msgType, const uint8_t *p, size_t length, TlsHelloExtensions *extensions)
Parse Hello extensions.
error_t tlsFormatQuicTransportParamsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format QuicTransportParameters extension.
Definition: tls_quic_misc.c:57
error_t tlsParseServerSessionTicketExtension(TlsContext *context, const TlsExtension *sessionTicket)
Parse SessionTicket extension.
@ TLS_TYPE_CLIENT_HELLO
Definition: tls.h:1085
error_t tlsSelectClientVersion(TlsContext *context, const TlsServerHello *message, const TlsHelloExtensions *extensions)
Version selection.
Tls12DigitalSignature
Definition: tls.h:1851
Helper functions for TLS 1.3 client.
error_t tls13FormatClientKeyShareExtension(TlsContext *context, uint8_t *p, size_t *written)
Format KeyShare extension (ClientHello message)
@ ERROR_MISSING_EXTENSION
Definition: error.h:245
#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_t tlsSelectSignAlgo(TlsContext *context, const TlsCertDesc *cert, const TlsSignSchemeList *signAlgoList)
Select the algorithm to be used when generating digital signatures.
Definition: tls_sign_misc.c:85
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
__weak_func error_t tls12VerifyServerKeySignature(TlsContext *context, const Tls12DigitalSignature *signature, size_t length, const uint8_t *params, size_t paramsLen, size_t *consumed)
Verify server's key exchange parameters signature (TLS 1.2)
error_t tlsFormatCertAuthoritiesExtension(TlsContext *context, uint8_t *p, size_t *written)
Format CertificateAuthorities extension.
Definition: tls_common.c:854
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1560
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1565
#define TLS_MAX_CERTIFICATES
Definition: tls.h:283
error_t tlsFormatClientAlpnExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ALPN extension.
TlsSignSchemeList
Definition: tls.h:1639
@ TLS_TRANSPORT_PROTOCOL_EAP
Definition: tls.h:1002
@ TLS_STATE_SERVER_HELLO_3
Definition: tls.h:1550
error_t tlsParseServerEcPointFormatsExtension(TlsContext *context, const TlsEcPointFormatList *ecPointFormatList)
Parse EcPointFormats extension.
error_t tlsFormatInitialClientHello(TlsContext *context)
Format initial ClientHello message.
error_t tls13FormatClientPreSharedKeyExtension(TlsContext *context, uint8_t *p, size_t *written, Tls13PskIdentityList **identityList, Tls13PskBinderList **binderList)
Format PreSharedKey extension.
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
@ TLS_TYPE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1099
@ TLS_KEY_EXCH_DHE_PSK
Definition: tls.h:1198
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1570
uint16_t dtlsTranslateVersion(uint16_t version)
Translate TLS version into DTLS version.
Definition: dtls_misc.c:124
Hello extensions.
Definition: tls.h:2257
Transcript hash calculation.
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1551
@ TLS_KEY_EXCH_RSA_PSK
Definition: tls.h:1197
Formatting and parsing of extensions (TLS client)
#define ntohs(value)
Definition: cpu_endian.h:421
error_t tlsFormatClientHelloPaddingExtension(TlsContext *context, size_t clientHelloLen, uint8_t *p, size_t *written)
Format ClientHello Padding extension.
#define TRACE_DEBUG(...)
Definition: debug.h:119
error_t tls13ComputePskBinders(TlsContext *context, const void *clientHello, size_t clientHelloLen, const Tls13PskIdentityList *identityList, Tls13PskBinderList *binderList)
Compute PSK binder values.
@ ERROR_TIMEOUT
Definition: error.h:95
#define TLS_VERSION_1_1
Definition: tls.h:95
@ TLS_KEY_EXCH_NONE
Definition: tls.h:1184
__weak_func error_t tlsFormatClientKeyParams(TlsContext *context, uint8_t *p, size_t *written)
Format client's key exchange parameters.
@ TLS13_KEY_EXCH_PSK_ECDHE
Definition: tls.h:1209
error_t tlsParseServerEmsExtension(TlsContext *context, const TlsExtension *extendedMasterSecret)
Parse ExtendedMasterSecret extension.
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
const char_t * tlsGetCipherSuiteName(uint16_t identifier)
Convert cipher suite identifier to string representation.
TlsServerHello
Definition: tls.h:1909
void TlsClientKeyExchange
ClientKeyExchange message.
Definition: tls.h:1948
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1553
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:260
@ TLS_ALERT_LEVEL_FATAL
Definition: tls.h:1118
uint8_t n
error_t tls13ParseServerPreSharedKeyExtension(TlsContext *context, const TlsExtension *selectedIdentity)
Parse PreSharedKey extension.
void TlsHelloRequest
HelloRequest message.
Definition: tls.h:1883
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1558
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC_2
Definition: tls.h:1562
error_t tlsFormatClientEmsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ExtendedMasterSecret extension.
@ TLS_KEY_EXCH_PSK
Definition: tls.h:1196
error_t tlsInitTranscriptHash(TlsContext *context)
Initialize handshake message hashing.
@ TLS_KEY_EXCH_ECDHE_PSK
Definition: tls.h:1199
error_t tls13FormatPskKeModesExtension(TlsContext *context, uint8_t *p, size_t *written)
Format PskKeyExchangeModes extension.
error_t tlsParseServerHelloDone(TlsContext *context, const TlsServerHelloDone *message, size_t length)
Parse ServerHelloDone message.
Definition: tls_client.c:1837
TlsClientHello
Definition: tls.h:1896
X.509 certificate handling.
Formatting and parsing of extensions (TLS 1.3 client)
Helper functions for signature generation and verification.
error_t tlsParseServerHello(TlsContext *context, const TlsServerHello *message, size_t length)
Parse ServerHello message.
Definition: tls_client.c:965
error_t tlsFormatClientCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ClientCertType extension.
@ TLS_TYPE_CERTIFICATE_REQUEST
Definition: tls.h:1096
TLS (Transport Layer Security)
@ TLS_KEY_EXCH_DHE_DSS
Definition: tls.h:1189
error_t tlsParseCertificateRequest(TlsContext *context, const TlsCertificateRequest *message, size_t length)
Parse CertificateRequest message.
Definition: tls_client.c:1533
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:999
error_t tlsCheckHelloExtensions(TlsMessageType msgType, uint16_t version, TlsHelloExtensions *extensions)
Check Hello extensions.
error_t tlsParseServerRenegoInfoExtension(TlsContext *context, const TlsHelloExtensions *extensions)
Parse RenegotiationInfo extension.
@ TLS_STATE_CLIENT_HELLO_3
Definition: tls.h:1544
error_t tlsFormatClientRecordSizeLimitExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RecordSizeLimit extension.
error_t tlsParseServerKeyParams(TlsContext *context, const uint8_t *p, size_t length, size_t *consumed)
Parse server's key exchange parameters.
Tls13CertRequestContext
Definition: tls13_misc.h:286
error_t tlsParseServerAlpnExtension(TlsContext *context, const TlsProtocolNameList *protocolNameList)
Parse ALPN extension.
error_t tlsSendClientHello(TlsContext *context)
Send ClientHello message.
Definition: tls_client.c:81
error_t tlsGenerateSessionKeys(TlsContext *context)
Generate session keys.
error_t tlsFormatSignAlgosCertExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SignatureAlgorithmsCert extension.
const char_t * tlsGetVersionName(uint16_t version)
Convert TLS version to string representation.
Definition: tls_misc.c:1264
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
error_t tlsFormatServerCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ServerCertType extension.
@ ERROR_DECODING_FAILED
Definition: error.h:242
error_t tlsFormatPskIdentity(TlsContext *context, uint8_t *p, size_t *written)
Format PSK identity.
Tls13PskIdentityList
Definition: tls13_misc.h:253
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:57
#define LOAD16BE(p)
Definition: cpu_endian.h:186
#define osMemset(p, value, length)
Definition: os_port.h:138
error_t tlsParseNewSessionTicket(TlsContext *context, const TlsNewSessionTicket *message, size_t length)
Parse NewSessionTicket message.
Definition: tls_client.c:1923
Handshake message processing (TLS client)
@ TLS_STATE_SERVER_HELLO_DONE
Definition: tls.h:1557
#define tlsFreeMem(p)
Definition: tls.h:893
@ 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
Handshake message processing (TLS 1.3 client)
error_t dtlsFormatCookie(TlsContext *context, uint8_t *p, size_t *written)
Format Cookie field.
Definition: dtls_misc.c:156
@ TLS_KEY_EXCH_DHE_RSA
Definition: tls.h:1187
error_t tlsParseServerKeyExchange(TlsContext *context, const TlsServerKeyExchange *message, size_t length)
Parse ServerKeyExchange message.
Definition: tls_client.c:1374
void TlsServerKeyExchange
ServerKeyExchange message.
Definition: tls.h:1923
#define ntohl(value)
Definition: cpu_endian.h:422
#define TLS_MAX_TICKET_SIZE
Definition: tls.h:171
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t tlsFormatClientHello(TlsContext *context, TlsClientHello *message, size_t *length)
Format ClientHello message.
Definition: tls_client.c:289
void tlsFreeTranscriptHash(TlsContext *context)
Release transcript hash context.
error_t tlsFormatCompressMethods(TlsContext *context, uint8_t *p, size_t *written)
Format the list of compression methods supported by the client.
bool_t tlsIsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
error_t tlsFormatClientMaxFragLenExtension(TlsContext *context, uint8_t *p, size_t *written)
Format MaxFragmentLength extension.
systime_t osGetSystemTime(void)
Retrieve system time.