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.2
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL TLS_TRACE_LEVEL
39 
40 //Dependencies
41 #include "tls/tls.h"
42 #include "tls/tls_cipher_suites.h"
43 #include "tls/tls_handshake.h"
44 #include "tls/tls_client.h"
46 #include "tls/tls_client_misc.h"
47 #include "tls/tls_common.h"
48 #include "tls/tls_extensions.h"
49 #include "tls/tls_certificate.h"
50 #include "tls/tls_sign_misc.h"
51 #include "tls/tls_key_material.h"
53 #include "tls/tls_record.h"
54 #include "tls/tls_misc.h"
55 #include "tls13/tls13_client.h"
58 #include "dtls/dtls_record.h"
59 #include "dtls/dtls_misc.h"
60 #include "quic/tls_quic_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[0]);
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_2 &&
611  context->version == TLS_VERSION_1_3)
612  {
613  //When sending the new ClientHello, the client must copy the contents
614  //of the Cookie extension received in the HelloRetryRequest into a
615  //Cookie extension in the new ClientHello
616  error = tls13FormatClientCookieExtension(context, p, &n);
617  //Any error to report?
618  if(error)
619  return error;
620 
621  //Fix the length of the extension list
622  extensionList->length += (uint16_t) n;
623  //Point to the next field
624  p += n;
625  }
626 
627  //The KeyShare extension contains the client's cryptographic parameters
628  error = tls13FormatClientKeyShareExtension(context, p, &n);
629  //Any error to report?
630  if(error)
631  return error;
632 
633  //Fix the length of the extension list
634  extensionList->length += (uint16_t) n;
635  //Point to the next field
636  p += n;
637 
638 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
639  //If the client opts to send application data in its first flight of
640  //messages, it must supply both the PreSharedKey and EarlyData extensions
641  error = tls13FormatClientEarlyDataExtension(context, p, &n);
642  //Any error to report?
643  if(error)
644  return error;
645 
646  //Fix the length of the extension list
647  extensionList->length += (uint16_t) n;
648  //Point to the next field
649  p += n;
650 #endif
651 
652  //In order to use PSKs, clients must send a PskKeyExchangeModes extension
653  error = tls13FormatPskKeModesExtension(context, p, &n);
654  //Any error to report?
655  if(error)
656  return error;
657 
658  //Fix the length of the extension list
659  extensionList->length += (uint16_t) n;
660  //Point to the next field
661  p += n;
662 
663 #if (TLS_CERT_AUTHORITIES_SUPPORT == ENABLED)
664  //The CertificateAuthorities extension is used to indicate the CAs which
665  //an endpoint supports and which should be used by the receiving endpoint
666  //to guide certificate selection
667  error = tlsFormatCertAuthoritiesExtension(context, p, &n);
668  //Any error to report?
669  if(error)
670  return error;
671 
672  //Fix the length of the extension list
673  extensionList->length += (uint16_t) n;
674  //Point to the next field
675  p += n;
676 #endif
677 
678 #if (TLS_QUIC_SUPPORT == ENABLED)
679  //QUIC transport?
680  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_QUIC)
681  {
682  //QUIC transport parameters are carried in a TLS extension (refer to
683  //RFC 9001, section 8.2)
684  error = tlsFormatQuicTransportParamsExtension(context, p, &n);
685  //Any error to report?
686  if(error)
687  return error;
688 
689  //Fix the length of the extension list
690  extensionList->length += (uint16_t) n;
691  //Point to the next field
692  p += n;
693  }
694 #endif
695 
696 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
697  //The first pass calculates the length of the PreSharedKey extension
698  error = tls13FormatClientPreSharedKeyExtension(context, p, &n,
699  &identityList, &binderList);
700  //Any error to report?
701  if(error)
702  return error;
703 
704  //Determine the length of the resulting message
705  n += *length + sizeof(TlsExtensionList) + extensionList->length;
706 
707  //Add a padding extension to ensure the ClientHello is never between
708  //256 and 511 bytes in length
709  error = tlsFormatClientHelloPaddingExtension(context, n, p, &n);
710  //Any error to report?
711  if(error)
712  return error;
713 
714  //Fix the length of the extension list
715  extensionList->length += (uint16_t) n;
716  //Point to the next field
717  p += n;
718 #endif
719 
720  //The extensions may appear in any order, with the exception of
721  //PreSharedKey which must be the last extension in the ClientHello
722  error = tls13FormatClientPreSharedKeyExtension(context, p, &n,
723  &identityList, &binderList);
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  //Convert the length of the extension list to network byte order
734  extensionList->length = htons(extensionList->length);
735  //Total length of the message
736  *length += sizeof(TlsExtensionList) + htons(extensionList->length);
737 
738  //Fix PSK binder values in the PreSharedKey extension
739  error = tls13ComputePskBinders(context, message, *length, identityList,
740  binderList);
741  //Any error to report?
742  if(error)
743  return error;
744  }
745  else
746 #endif
747  {
748 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
749  //Retrieve the actual length of the message
750  n = *length;
751 
752  //Any extensions included in the ClientHello message?
753  if(extensionList->length > 0)
754  {
755  n += sizeof(TlsExtensionList) + extensionList->length;
756  }
757 
758  //Add a padding extension to ensure the ClientHello is never between
759  //256 and 511 bytes in length
760  error = tlsFormatClientHelloPaddingExtension(context, n, p, &n);
761  //Any error to report?
762  if(error)
763  return error;
764 
765  //Fix the length of the extension list
766  extensionList->length += (uint16_t) n;
767  //Point to the next field
768  p += n;
769 #endif
770 
771  //Any extensions included in the ClientHello message?
772  if(extensionList->length > 0)
773  {
774  //Convert the length of the extension list to network byte order
775  extensionList->length = htons(extensionList->length);
776  //Total length of the message
777  *length += sizeof(TlsExtensionList) + htons(extensionList->length);
778  }
779  }
780 
781  //Successful processing
782  return NO_ERROR;
783 }
784 
785 
786 /**
787  * @brief Format ClientKeyExchange message
788  * @param[in] context Pointer to the TLS context
789  * @param[out] message Buffer where to format the ClientKeyExchange message
790  * @param[out] length Length of the resulting ClientKeyExchange message
791  * @return Error code
792  **/
793 
796 {
797  error_t error;
798  size_t n;
799  uint8_t *p;
800 
801  //Point to the beginning of the handshake message
802  p = message;
803  //Length of the handshake message
804  *length = 0;
805 
806 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
807  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
808  //PSK key exchange method?
809  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
810  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
811  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
812  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
813  {
814  //The client indicates which key to use by including a PSK identity
815  //in the ClientKeyExchange message
816  error = tlsFormatPskIdentity(context, p, &n);
817  //Any error to report?
818  if(error)
819  return error;
820 
821  //Advance data pointer
822  p += n;
823  //Adjust the length of the message
824  *length += n;
825  }
826 #endif
827 
828  //RSA, Diffie-Hellman or ECDH key exchange method?
829  if(context->keyExchMethod != TLS_KEY_EXCH_PSK)
830  {
831  //Format client's key exchange parameters
832  error = tlsFormatClientKeyParams(context, p, &n);
833  //Any error to report?
834  if(error)
835  return error;
836 
837  //Advance data pointer
838  p += n;
839  //Adjust the length of the message
840  *length += n;
841  }
842 
843 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
844  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
845  //PSK key exchange method?
846  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
847  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
848  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
849  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
850  {
851  //Invalid pre-shared key?
852  if(context->pskLen == 0)
854 
855  //Generate premaster secret
856  error = tlsGeneratePskPremasterSecret(context);
857  //Any error to report?
858  if(error)
859  return error;
860  }
861 #endif
862 
863  //Successful processing
864  return NO_ERROR;
865 }
866 
867 
868 /**
869  * @brief Parse HelloRequest message
870  *
871  * HelloRequest is a simple notification that the client should begin the
872  * negotiation process anew. In response, the client should send a ClientHello
873  * message when convenient
874  *
875  * @param[in] context Pointer to the TLS context
876  * @param[in] message Incoming HelloRequest message to parse
877  * @param[in] length Message length
878  * @return Error code
879  **/
880 
882  const TlsHelloRequest *message, size_t length)
883 {
884  error_t error;
885 
886  //Debug message
887  TRACE_INFO("HelloRequest message received (%" PRIuSIZE " bytes)...\r\n", length);
889 
890  //Check TLS version
891  if(context->version > TLS_VERSION_1_2)
893 
894  //The HelloRequest message does not contain any data
895  if(length != 0)
896  return ERROR_DECODING_FAILED;
897 
898  //Check current state
899  if(context->state == TLS_STATE_APPLICATION_DATA)
900  {
901 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
902  //Check whether the secure_renegotiation flag is set
903  if(context->secureRenegoEnabled && context->secureRenegoFlag)
904  {
905  //Release existing session ticket, if any
906  if(context->ticket != NULL)
907  {
908  osMemset(context->ticket, 0, context->ticketLen);
909  tlsFreeMem(context->ticket);
910  context->ticket = NULL;
911  context->ticketLen = 0;
912  }
913 
914 #if (DTLS_SUPPORT == ENABLED)
915  //Release cookie
916  if(context->cookie != NULL)
917  {
918  tlsFreeMem(context->cookie);
919  context->cookie = NULL;
920  context->cookieLen = 0;
921  }
922 #endif
923  //HelloRequest is a simple notification that the client should begin
924  //the negotiation process anew
926 
927  //Continue processing
928  error = NO_ERROR;
929  }
930  else
931 #endif
932  {
933  //If the connection's secure_renegotiation flag is set to FALSE, it
934  //is recommended that clients refuse this renegotiation request (refer
935  //to RFC 5746, section 4.2)
936  error = tlsSendAlert(context, TLS_ALERT_LEVEL_FATAL,
938  }
939  }
940  else
941  {
942  //The HelloRequest message can be sent at any time but it should be
943  //ignored by the client if it arrives in the middle of a handshake
944  error = NO_ERROR;
945  }
946 
947  //Return status code
948  return error;
949 }
950 
951 
952 /**
953  * @brief Parse ServerHello message
954  *
955  * The server will send this message in response to a ClientHello
956  * message when it was able to find an acceptable set of algorithms.
957  * If it cannot find such a match, it will respond with a handshake
958  * failure alert
959  *
960  * @param[in] context Pointer to the TLS context
961  * @param[in] message Incoming ServerHello message to parse
962  * @param[in] length Message length
963  * @return Error code
964  **/
965 
967  const TlsServerHello *message, size_t length)
968 {
969  error_t error;
970  uint16_t cipherSuite;
971  uint8_t compressMethod;
972  const uint8_t *p;
974 
975  //Debug message
976  TRACE_INFO("ServerHello message received (%" PRIuSIZE " bytes)...\r\n", length);
978 
979  //Check current state
980  if(context->state != TLS_STATE_SERVER_HELLO &&
981  context->state != TLS_STATE_SERVER_HELLO_2 &&
982  context->state != TLS_STATE_SERVER_HELLO_3)
983  {
984  //Report an error
986  }
987 
988  //Check the length of the ServerHello message
989  if(length < sizeof(TlsServerHello))
990  return ERROR_DECODING_FAILED;
991 
992  //Point to the session ID
993  p = message->sessionId;
994  //Remaining bytes to process
995  length -= sizeof(TlsServerHello);
996 
997  //Check the length of the session ID
998  if(message->sessionIdLen > length)
999  return ERROR_DECODING_FAILED;
1000  if(message->sessionIdLen > 32)
1001  return ERROR_DECODING_FAILED;
1002 
1003  //Point to the next field
1004  p += message->sessionIdLen;
1005  //Remaining bytes to process
1006  length -= message->sessionIdLen;
1007 
1008  //Malformed ServerHello message?
1009  if(length < sizeof(uint16_t))
1010  return ERROR_DECODING_FAILED;
1011 
1012  //Get the negotiated cipher suite
1013  cipherSuite = LOAD16BE(p);
1014  //Point to the next field
1015  p += sizeof(uint16_t);
1016  //Remaining bytes to process
1017  length -= sizeof(uint16_t);
1018 
1019  //Malformed ServerHello message?
1020  if(length < sizeof(uint8_t))
1021  return ERROR_DECODING_FAILED;
1022 
1023  //Get the negotiated compression method
1024  compressMethod = *p;
1025  //Point to the next field
1026  p += sizeof(uint8_t);
1027  //Remaining bytes to process
1028  length -= sizeof(uint8_t);
1029 
1030  //Server version
1031  TRACE_INFO(" serverVersion = 0x%04" PRIX16 " (%s)\r\n",
1032  ntohs(message->serverVersion),
1033  tlsGetVersionName(ntohs(message->serverVersion)));
1034 
1035  //Server random value
1036  TRACE_DEBUG(" random\r\n");
1037  TRACE_DEBUG_ARRAY(" ", message->random, 32);
1038 
1039  //Session identifier
1040  TRACE_DEBUG(" sessionId\r\n");
1041  TRACE_DEBUG_ARRAY(" ", message->sessionId, message->sessionIdLen);
1042 
1043  //Cipher suite identifier
1044  TRACE_INFO(" cipherSuite = 0x%04" PRIX16 " (%s)\r\n",
1046 
1047  //Compression method
1048  TRACE_DEBUG(" compressMethod = 0x%02" PRIX8 "\r\n", compressMethod);
1049 
1050  //The CRIME exploit takes advantage of TLS compression, so conservative
1051  //implementations do not accept compression at the TLS level
1052  if(compressMethod != TLS_COMPRESSION_METHOD_NULL)
1053  return ERROR_ILLEGAL_PARAMETER;
1054 
1055  //Parse the list of extensions offered by the server
1057  &extensions);
1058  //Any error to report?
1059  if(error)
1060  return error;
1061 
1062  //TLS protocol?
1063  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM ||
1064  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_QUIC ||
1065  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP)
1066  {
1067  //Check whether the ServerHello message is received in response to the
1068  //initial ClientHello
1069  if(context->state != TLS_STATE_SERVER_HELLO_2)
1070  {
1071  //Release transcript hash context
1072  tlsFreeTranscriptHash(context);
1073 
1074  //Format initial ClientHello message
1075  error = tlsFormatInitialClientHello(context);
1076  //Any error to report?
1077  if(error)
1078  return error;
1079  }
1080  }
1081 
1082  //Select TLS version
1083  error = tlsSelectClientVersion(context, message, &extensions);
1084  //TLS version not supported?
1085  if(error)
1086  return error;
1087 
1088  //Check the list of extensions offered by the server
1089  error = tlsCheckHelloExtensions(TLS_TYPE_SERVER_HELLO, context->version,
1090  &extensions);
1091  //Any error to report?
1092  if(error)
1093  return error;
1094 
1095  //Save server random value
1096  osMemcpy(context->serverRandom, message->random, 32);
1097 
1098 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1099  //TLS 1.0, TLS 1.1 or TLS 1.2 currently selected?
1100  if(context->version <= TLS_VERSION_1_2)
1101  {
1102  //Reset the named group to its default value
1103  context->namedGroup = TLS_GROUP_NONE;
1104 
1105  //Check whether the server has decided to resume a previous session
1106  error = tlsResumeSession(context, message->sessionId,
1107  message->sessionIdLen, cipherSuite);
1108  //Any error to report?
1109  if(error)
1110  return error;
1111 
1112  //Set cipher suite
1113  error = tlsSelectCipherSuite(context, cipherSuite);
1114  //Specified cipher suite not supported?
1115  if(error)
1116  return error;
1117 
1118  //Initialize handshake message hashing
1119  error = tlsInitTranscriptHash(context);
1120  //Any error to report?
1121  if(error)
1122  return error;
1123 
1124  //Save session identifier
1125  osMemcpy(context->sessionId, message->sessionId, message->sessionIdLen);
1126  context->sessionIdLen = message->sessionIdLen;
1127 
1128 #if (TLS_TICKET_SUPPORT == ENABLED)
1129  //Parse SessionTicket extension
1130  error = tlsParseServerSessionTicketExtension(context,
1131  extensions.sessionTicket);
1132  //Any error to report?
1133  if(error)
1134  return error;
1135 #endif
1136 
1137 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1138  //Parse RenegotiationInfo extension
1139  error = tlsParseServerRenegoInfoExtension(context, &extensions);
1140  //Any error to report?
1141  if(error)
1142  return error;
1143 #endif
1144 
1145 #if (TLS_SNI_SUPPORT == ENABLED)
1146  //When the server includes a ServerName extension, the data field of
1147  //this extension may be empty
1148  error = tlsParseServerSniExtension(context, extensions.serverNameList);
1149  //Any error to report?
1150  if(error)
1151  return error;
1152 #endif
1153 
1154 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
1155  //Servers that receive an ClientHello containing a MaxFragmentLength
1156  //extension may accept the requested maximum fragment length by including
1157  //an extension of type MaxFragmentLength in the ServerHello
1158  error = tlsParseServerMaxFragLenExtension(context, extensions.maxFragLen);
1159  //Any error to report?
1160  if(error)
1161  return error;
1162 #endif
1163 
1164 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1165  //The value of RecordSizeLimit is the maximum size of record in octets
1166  //that the peer is willing to receive
1168  extensions.recordSizeLimit);
1169  //Any error to report?
1170  if(error)
1171  return error;
1172 #endif
1173 
1174 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1175  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1176  //A server that selects an ECC cipher suite in response to a ClientHello
1177  //message including an EcPointFormats extension appends this extension
1178  //to its ServerHello message
1179  error = tlsParseServerEcPointFormatsExtension(context,
1180  extensions.ecPointFormatList);
1181  //Any error to report?
1182  if(error)
1183  return error;
1184 #endif
1185 
1186 #if (TLS_ALPN_SUPPORT == ENABLED)
1187  //Parse ALPN extension
1188  error = tlsParseServerAlpnExtension(context, extensions.protocolNameList);
1189  //Any error to report?
1190  if(error)
1191  return error;
1192 #endif
1193 
1194 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1195  //Parse ClientCertType extension
1196  error = tlsParseClientCertTypeExtension(context, extensions.clientCertType);
1197  //Any error to report?
1198  if(error)
1199  return error;
1200 
1201  //Parse ServerCertType extension
1202  error = tlsParseServerCertTypeExtension(context, extensions.serverCertType);
1203  //Any error to report?
1204  if(error)
1205  return error;
1206 #endif
1207 
1208 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1209  //Parse EncryptThenMac extension
1210  error = tlsParseServerEtmExtension(context, extensions.encryptThenMac);
1211  //Any error to report?
1212  if(error)
1213  return error;
1214 #endif
1215 
1216 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1217  //Parse ExtendedMasterSecret extension
1218  error = tlsParseServerEmsExtension(context, extensions.extendedMasterSecret);
1219  //Any error to report?
1220  if(error)
1221  return error;
1222 #endif
1223 
1224 #if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
1225  //Use abbreviated handshake?
1226  if(context->resume)
1227  {
1228  //Derive session keys from the master secret
1229  error = tlsGenerateSessionKeys(context);
1230  //Unable to generate key material?
1231  if(error)
1232  return error;
1233 
1234 #if (TLS_TICKET_SUPPORT == ENABLED)
1235  //The server uses the SessionTicket extension to indicate to the client
1236  //that it will send a new session ticket using the NewSessionTicket
1237  //handshake message
1238  if(context->sessionTicketExtReceived)
1239  {
1240  //Wait for a NewSessionTicket message from the server
1242  }
1243  else
1244 #endif
1245  {
1246  //At this point, both client and server must send ChangeCipherSpec
1247  //messages and proceed directly to Finished messages
1249  }
1250  }
1251  else
1252 #endif
1253  {
1254  //Perform a full handshake
1255  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1256  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1257  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1258  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1259  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1260  {
1261  //The Certificate message is omitted from the server's response
1263  }
1264  else
1265  {
1266  //The server is required to send a Certificate message
1268  }
1269  }
1270  }
1271  else
1272 #endif
1273 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1274  //TLS 1.3 currently selected?
1275  if(context->version == TLS_VERSION_1_3)
1276  {
1277  //DTLS protocol?
1278  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
1279  {
1280  //DTLS 1.3 clients must abort the handshake with an illegal_parameter
1281  //alert if the legacy_session_id_echo field is not empty
1282  if(message->sessionIdLen != 0)
1283  return ERROR_ILLEGAL_PARAMETER;
1284  }
1285  else
1286  {
1287  //A client which receives a legacy_session_id_echo field that does not
1288  //match what it sent in the ClientHello must abort the handshake with
1289  //an illegal_parameter alert (RFC 8446, section 4.1.3)
1290  if(message->sessionIdLen != context->sessionIdLen ||
1291  osMemcmp(message->sessionId, context->sessionId,
1292  message->sessionIdLen))
1293  {
1294  return ERROR_ILLEGAL_PARAMETER;
1295  }
1296  }
1297 
1298  //Check whether the ServerHello message is received in response to the
1299  //initial or the updated ClientHello
1300  if(context->state != TLS_STATE_SERVER_HELLO_2)
1301  {
1302  //Set cipher suite
1303  error = tlsSelectCipherSuite(context, cipherSuite);
1304  //Specified cipher suite not supported?
1305  if(error)
1306  return error;
1307 
1308  //Initialize handshake message hashing
1309  error = tlsInitTranscriptHash(context);
1310  //Any error to report?
1311  if(error)
1312  return error;
1313  }
1314  else
1315  {
1316  //Clients must check that the cipher suite supplied in the ServerHello
1317  //is the same as that in the HelloRetryRequest and otherwise abort the
1318  //handshake with an illegal_parameter alert
1319  if(cipherSuite != context->cipherSuite.identifier)
1320  return ERROR_ILLEGAL_PARAMETER;
1321  }
1322 
1323  //If using (EC)DHE key establishment, servers offer exactly one
1324  //KeyShareEntry in the ServerHello
1325  error = tls13ParseServerKeyShareExtension(context,
1326  extensions.serverShare);
1327  //Any error to report?
1328  if(error)
1329  return error;
1330 
1331  //The PreSharedKey extension contains the selected PSK identity
1332  error = tls13ParseServerPreSharedKeyExtension(context,
1333  extensions.selectedIdentity);
1334  //Any error to report?
1335  if(error)
1336  return error;
1337 
1338  //In TLS 1.3, the cipher suite concept has been changed. The key exchange
1339  //mechanism is negotiated separately from the cipher suite
1340  if(context->keyExchMethod == TLS_KEY_EXCH_NONE)
1341  return ERROR_HANDSHAKE_FAILED;
1342 
1343 #if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
1344  //The middlebox compatibility mode improves the chance of successfully
1345  //connecting through middleboxes
1346  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
1347  context->state == TLS_STATE_SERVER_HELLO)
1348  {
1349  //In middlebox compatibility mode, the client sends a dummy
1350  //ChangeCipherSpec record immediately before its second flight
1352  }
1353  else
1354 #endif
1355  {
1356  //All handshake messages after the ServerHello are now encrypted
1358  }
1359  }
1360  else
1361 #endif
1362  //Invalid TLS version?
1363  {
1364  //Just for sanity
1365  return ERROR_INVALID_VERSION;
1366  }
1367 
1368  //Successful processing
1369  return NO_ERROR;
1370 }
1371 
1372 
1373 /**
1374  * @brief Parse ServerKeyExchange message
1375  *
1376  * The ServerKeyExchange message is sent by the server only when the
1377  * server Certificate message does not contain enough data to allow
1378  * the client to exchange a premaster secret
1379  *
1380  * @param[in] context Pointer to the TLS context
1381  * @param[in] message Incoming ServerKeyExchange message to parse
1382  * @param[in] length Message length
1383  * @return Error code
1384  **/
1385 
1387  const TlsServerKeyExchange *message, size_t length)
1388 {
1389  error_t error;
1390  size_t n;
1391  size_t paramsLen;
1392  const uint8_t *p;
1393  const uint8_t *params;
1394 
1395  //Initialize variables
1396  params = NULL;
1397  paramsLen = 0;
1398 
1399  //Debug message
1400  TRACE_INFO("ServerKeyExchange message received (%" PRIuSIZE " bytes)...\r\n", length);
1402 
1403  //Check TLS version
1404  if(context->version > TLS_VERSION_1_2)
1405  return ERROR_UNEXPECTED_MESSAGE;
1406 
1407  //Check current state
1408  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE)
1409  return ERROR_UNEXPECTED_MESSAGE;
1410 
1411  //Point to the beginning of the handshake message
1412  p = message;
1413 
1414 #if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
1415  TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1416  //PSK key exchange method?
1417  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1418  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
1419  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1420  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1421  {
1422  //To help the client in selecting which identity to use, the server
1423  //can provide a PSK identity hint in the ServerKeyExchange message
1424  error = tlsParsePskIdentityHint(context, p, length, &n);
1425  //Any error to report?
1426  if(error)
1427  return error;
1428 
1429  //Point to the next field
1430  p += n;
1431  //Remaining bytes to process
1432  length -= n;
1433  }
1434 #endif
1435 
1436  //Diffie-Hellman or ECDH key exchange method?
1437  if(context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1438  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1439  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1440  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1441  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1442  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1443  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
1444  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1445  {
1446  //Point to the server's key exchange parameters
1447  params = p;
1448 
1449  //Parse server's key exchange parameters
1450  error = tlsParseServerKeyParams(context, p, length, &paramsLen);
1451  //Any error to report?
1452  if(error)
1453  return error;
1454 
1455  //Point to the next field
1456  p += paramsLen;
1457  //Remaining bytes to process
1458  length -= paramsLen;
1459  }
1460 
1461  //For non-anonymous Diffie-Hellman and ECDH key exchanges, the signature
1462  //over the server's key exchange parameters shall be verified
1463  if(context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1464  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1465  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1466  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
1467  {
1468 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
1469  //TLS 1.0 or TLS 1.1 currently selected?
1470  if(context->version <= TLS_VERSION_1_1)
1471  {
1472  //Signature verification
1473  error = tlsVerifyServerKeySignature(context,
1474  (TlsDigitalSignature *) p, length, params, paramsLen, &n);
1475  }
1476  else
1477 #endif
1478 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1479  //TLS 1.2 currently selected?
1480  if(context->version == TLS_VERSION_1_2)
1481  {
1482  //Signature verification
1483  error = tls12VerifyServerKeySignature(context,
1484  (Tls12DigitalSignature *) p, length, params, paramsLen, &n);
1485  }
1486  else
1487 #endif
1488  {
1489  //Just for sanity
1490  (void) params;
1491  //Report an error
1492  error = ERROR_INVALID_VERSION;
1493  }
1494 
1495  //Any error to report?
1496  if(error)
1497  return error;
1498 
1499  //Point to the next field
1500  p += n;
1501  //Remaining bytes to process
1502  length -= n;
1503  }
1504 
1505  //If the amount of data in the message does not precisely match the format
1506  //of the ServerKeyExchange message, then send a fatal alert
1507  if(length != 0)
1508  return ERROR_DECODING_FAILED;
1509 
1510  //Anomynous server?
1511  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1512  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1513  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1514  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1515  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1516  {
1517  //An anonymous server cannot request client authentication
1519  }
1520  else
1521  {
1522  //A non-anonymous server can optionally request a certificate from
1523  //the client, if appropriate for the selected cipher suite
1525  }
1526 
1527  //Successful processing
1528  return NO_ERROR;
1529 }
1530 
1531 
1532 /**
1533  * @brief Parse CertificateRequest message
1534  *
1535  * A server can optionally request a certificate from the client, if
1536  * appropriate for the selected cipher suite. This message will
1537  * immediately follow the ServerKeyExchange message
1538  *
1539  * @param[in] context Pointer to the TLS context
1540  * @param[in] message Incoming CertificateRequest message to parse
1541  * @param[in] length Message length
1542  * @return Error code
1543  **/
1544 
1546  const TlsCertificateRequest *message, size_t length)
1547 {
1548  error_t error;
1549  uint_t i;
1550  uint_t j;
1551  size_t n;
1552  uint_t certTypesLen;
1553  bool_t acceptable;
1554  const uint8_t *p;
1555  const uint8_t *certTypes;
1556  const TlsCertAuthorities *certAuthorities;
1557  const TlsSignSchemeList *signAlgoList;
1558  const TlsSignSchemeList *certSignAlgoList;
1559 
1560  //Debug message
1561  TRACE_INFO("CertificateRequest message received (%" PRIuSIZE " bytes)...\r\n", length);
1563 
1564  //Check key exchange method
1565  if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
1566  context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
1567  context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
1568  context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
1569  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
1570  {
1571  //It is a fatal handshake failure alert for an anonymous server to
1572  //request client authentication
1573  return ERROR_HANDSHAKE_FAILED;
1574  }
1575  else if(context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1576  {
1577  //If no PSK identity hint is provided by the server, then the
1578  //ServerKeyExchange message is omitted
1579  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1580  context->state != TLS_STATE_CERTIFICATE_REQUEST)
1581  {
1582  //Handshake failure
1583  return ERROR_UNEXPECTED_MESSAGE;
1584  }
1585  }
1586  else if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
1587  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
1588  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE ||
1589  context->keyExchMethod == TLS13_KEY_EXCH_PSK_HYBRID)
1590  {
1591  //Servers which are authenticating with a PSK must not send the
1592  //CertificateRequest message in the main handshake
1593  return ERROR_UNEXPECTED_MESSAGE;
1594  }
1595  else
1596  {
1597  //Check current state
1598  if(context->state != TLS_STATE_CERTIFICATE_REQUEST)
1599  return ERROR_UNEXPECTED_MESSAGE;
1600  }
1601 
1602  //The server requests a certificate from the client, so that connection
1603  //can be mutually authenticated
1604  context->clientCertRequested = TRUE;
1605 
1606  //Point to the beginning of the handshake message
1607  p = (uint8_t *) message;
1608 
1609 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1610  //Version of TLS prior to TLS 1.3?
1611  if(context->version <= TLS_VERSION_1_2)
1612  {
1613  //Check the length of the ServerKeyExchange message
1614  if(length < sizeof(TlsCertificateRequest))
1615  return ERROR_DECODING_FAILED;
1616 
1617  //Remaining bytes to process
1618  length -= sizeof(TlsCertificateRequest);
1619 
1620  //Retrieve the length of the list
1621  n = message->certificateTypesLen;
1622  //Malformed CertificateRequest message?
1623  if(n > length)
1624  return ERROR_DECODING_FAILED;
1625 
1626  //Point to the list of supported certificate types
1627  certTypes = message->certificateTypes;
1628  certTypesLen = message->certificateTypesLen;
1629 
1630  //Point to the next field
1631  p += sizeof(TlsCertificateRequest) + n;
1632  //Remaining bytes to process
1633  length -= n;
1634 
1635  //TLS 1.2 currently selected?
1636  if(context->version == TLS_VERSION_1_2)
1637  {
1638  //Malformed CertificateRequest message?
1639  if(length < sizeof(TlsSignSchemeList))
1640  return ERROR_DECODING_FAILED;
1641 
1642  //Point to the list of the hash/signature algorithm pairs
1643  signAlgoList = (TlsSignSchemeList *) p;
1644  //Remaining bytes to process
1645  length -= sizeof(TlsSignSchemeList);
1646 
1647  //Retrieve the length of the list
1648  n = ntohs(signAlgoList->length);
1649  //Malformed CertificateRequest message?
1650  if(n > length)
1651  return ERROR_DECODING_FAILED;
1652 
1653  //The supported_signature_algorithms field cannot be empty (refer to
1654  //RFC 5246, section 7.4.4)
1655  if(n == 0)
1656  return ERROR_DECODING_FAILED;
1657  if((n % 2) != 0)
1658  return ERROR_DECODING_FAILED;
1659 
1660  //Point to the next field
1661  p += sizeof(TlsSignSchemeList) + n;
1662  //Remaining bytes to process
1663  length -= n;
1664  }
1665  else
1666  {
1667  //Implementations prior to TLS 1.2 do not include a list of supported
1668  //hash/signature algorithm pairs
1669  signAlgoList = NULL;
1670  }
1671 
1672  //List of signature algorithms that may appear in X.509 certificates
1673  certSignAlgoList = signAlgoList;
1674 
1675  //Malformed CertificateRequest message?
1676  if(length < sizeof(TlsCertAuthorities))
1677  return ERROR_DECODING_FAILED;
1678 
1679  //Point to the list of acceptable certificate authorities
1680  certAuthorities = (TlsCertAuthorities *) p;
1681  //Remaining bytes to process
1682  length -= sizeof(TlsCertAuthorities);
1683 
1684  //Retrieve the length of the list
1685  n = ntohs(certAuthorities->length);
1686  //Malformed CertificateRequest message?
1687  if(n != length)
1688  return ERROR_DECODING_FAILED;
1689  }
1690  else
1691 #endif
1692 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1693  //TLS 1.3 currently selected?
1694  if(context->version == TLS_VERSION_1_3)
1695  {
1697  const Tls13CertRequestContext *certRequestContext;
1698 
1699  //Unused parameters
1700  certTypes = NULL;
1701  certTypesLen = 0;
1702 
1703  //Malformed CertificateRequest message?
1704  if(length < sizeof(Tls13CertRequestContext))
1705  return ERROR_DECODING_FAILED;
1706 
1707  //Point to the certificate_request_context field
1708  certRequestContext = (Tls13CertRequestContext *) p;
1709  //Remaining bytes to process
1710  length -= sizeof(Tls13CertRequestContext);
1711 
1712  //Retrieve the length of the field
1713  n = certRequestContext->length;
1714  //Malformed CertificateRequest message?
1715  if(n > length)
1716  return ERROR_DECODING_FAILED;
1717 
1718  //The certificate_request_context field shall be zero length unless
1719  //used for the post-handshake authentication exchange
1720  if(certRequestContext->length != 0)
1721  return ERROR_ILLEGAL_PARAMETER;
1722 
1723  //Point to the next field
1724  p += sizeof(Tls13CertRequestContext) + n;
1725  //Remaining bytes to process
1726  length -= n;
1727 
1728  //The extensions describe the parameters of the certificate being
1729  //requested
1731  length, &extensions);
1732  //Any error to report?
1733  if(error)
1734  return error;
1735 
1736  //Check the list of extensions offered by the server
1738  context->version, &extensions);
1739  //Any error to report?
1740  if(error)
1741  return error;
1742 
1743  //The SignatureAlgorithms extension must be specified (refer to RFC 8446,
1744  //section 4.3.2)
1745  if(extensions.signAlgoList == NULL)
1746  return ERROR_MISSING_EXTENSION;
1747 
1748  //Point to the list of the hash/signature algorithm pairs that
1749  //the server is able to verify
1750  signAlgoList = extensions.signAlgoList;
1751 
1752  //If no SignatureAlgorithmsCert extension is present, then the
1753  //SignatureAlgorithms extension also applies to signatures appearing
1754  //in certificates (RFC 8446, section 4.2.3)
1755  if(extensions.certSignAlgoList != NULL)
1756  {
1757  certSignAlgoList = extensions.certSignAlgoList;
1758  }
1759  else
1760  {
1761  certSignAlgoList = extensions.signAlgoList;
1762  }
1763 
1764  //The CertificateAuthorities extension is used to indicate the CAs which
1765  //an endpoint supports and which should be used by the receiving endpoint
1766  //to guide certificate selection
1767  certAuthorities = extensions.certAuthorities;
1768  }
1769  else
1770 #endif
1771  //Invalid TLS version?
1772  {
1773  //Report an error
1774  return ERROR_INVALID_VERSION;
1775  }
1776 
1777  //No suitable certificate has been found for the moment
1778  context->cert = NULL;
1779  acceptable = FALSE;
1780 
1781  //Select the most appropriate certificate (2-pass process)
1782  for(i = 0; i < 2 && !acceptable; i++)
1783  {
1784  //Loop through the list of available certificates
1785  for(j = 0; j < TLS_MAX_CERTIFICATES && !acceptable; j++)
1786  {
1787  //Check whether the current certificate is suitable
1788  acceptable = tlsIsCertificateAcceptable(context, &context->certs[j],
1789  certTypes, certTypesLen, NULL, certSignAlgoList, certAuthorities);
1790 
1791  //TLS 1.2 and TLS 1.3 require additional examinations
1792  if(acceptable)
1793  {
1794  //The hash and signature algorithms used in the signature of the
1795  //CertificateVerify message must be one of those present in the
1796  //SupportedSignatureAlgorithms field
1797  error = tlsSelectSignAlgo(context, &context->certs[j],
1798  signAlgoList);
1799 
1800  //Check status code
1801  if(error)
1802  {
1803  acceptable = FALSE;
1804  }
1805  }
1806 
1807  //If all the requirements were met, the certificate can be used
1808  if(acceptable)
1809  {
1810  context->cert = &context->certs[j];
1811  }
1812  }
1813 
1814  //The second pass relaxes the constraints
1815  certSignAlgoList = NULL;
1816  certAuthorities = NULL;
1817  }
1818 
1819  //Version of TLS prior to TLS 1.3?
1820  if(context->version <= TLS_VERSION_1_2)
1821  {
1822  //Wait for a ServerHelloDone message
1824  }
1825  else
1826  {
1827  //Wait for a Certificate message
1829  }
1830 
1831  //Successful processing
1832  return NO_ERROR;
1833 }
1834 
1835 
1836 /**
1837  * @brief Parse ServerHelloDone message
1838  *
1839  * The ServerHelloDone message is sent by the server to indicate the
1840  * end of the ServerHello and associated messages. After sending this
1841  * message, the server will wait for a client response
1842  *
1843  * @param[in] context Pointer to the TLS context
1844  * @param[in] message Incoming ServerHelloDone message to parse
1845  * @param[in] length Message length
1846  * @return Error code
1847  **/
1848 
1850  const TlsServerHelloDone *message, size_t length)
1851 {
1852  //Debug message
1853  TRACE_INFO("ServerHelloDone message received (%" PRIuSIZE " bytes)...\r\n", length);
1855 
1856  //Check TLS version
1857  if(context->version > TLS_VERSION_1_2)
1858  return ERROR_UNEXPECTED_MESSAGE;
1859 
1860  //Check key exchange method
1861  if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
1862  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1863  context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
1864  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1865  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
1866  {
1867  //The server may omit the CertificateRequest message and go
1868  //directly to the ServerHelloDone message
1869  if(context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1870  context->state != TLS_STATE_SERVER_HELLO_DONE)
1871  {
1872  //Handshake failure
1873  return ERROR_UNEXPECTED_MESSAGE;
1874  }
1875  }
1876  else if(context->keyExchMethod == TLS_KEY_EXCH_PSK)
1877  {
1878  //If no PSK identity hint is provided by the server, the
1879  //ServerKeyExchange message is omitted
1880  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1881  context->state != TLS_STATE_SERVER_HELLO_DONE)
1882  {
1883  //Handshake failure
1884  return ERROR_UNEXPECTED_MESSAGE;
1885  }
1886  }
1887  else if(context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1888  {
1889  //The server may omit the ServerKeyExchange message and/or
1890  //the CertificateRequest message
1891  if(context->state != TLS_STATE_SERVER_KEY_EXCHANGE &&
1892  context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1893  context->state != TLS_STATE_SERVER_HELLO_DONE)
1894  {
1895  //Handshake failure
1896  return ERROR_UNEXPECTED_MESSAGE;
1897  }
1898  }
1899  else
1900  {
1901  //Check current state
1902  if(context->state != TLS_STATE_SERVER_HELLO_DONE)
1903  return ERROR_UNEXPECTED_MESSAGE;
1904  }
1905 
1906  //The ServerHelloDone message does not contain any data
1907  if(length != 0)
1908  return ERROR_DECODING_FAILED;
1909 
1910  //Another handshake message cannot be packed in the same record as the
1911  //ServerHelloDone
1912  if(context->rxBufferLen != 0)
1913  return ERROR_UNEXPECTED_MESSAGE;
1914 
1915  //The client must send a Certificate message if the server requests it
1917 
1918  //Successful processing
1919  return NO_ERROR;
1920 }
1921 
1922 
1923 /**
1924  * @brief Parse NewSessionTicket message
1925  *
1926  * This NewSessionTicket message is sent by the server during the TLS handshake
1927  * before the ChangeCipherSpec message
1928  *
1929  * @param[in] context Pointer to the TLS context
1930  * @param[in] message Incoming NewSessionTicket message to parse
1931  * @param[in] length Message length
1932  * @return Error code
1933  **/
1934 
1936  const TlsNewSessionTicket *message, size_t length)
1937 {
1938  size_t n;
1939 
1940  //Debug message
1941  TRACE_INFO("NewSessionTicket message received (%" PRIuSIZE " bytes)...\r\n", length);
1943 
1944  //Check TLS version
1945  if(context->version > TLS_VERSION_1_2)
1946  return ERROR_UNEXPECTED_MESSAGE;
1947 
1948  //Check current state
1949  if(context->state != TLS_STATE_NEW_SESSION_TICKET)
1950  return ERROR_UNEXPECTED_MESSAGE;
1951 
1952  //Check the length of the NewSessionTicket message
1953  if(length < sizeof(TlsNewSessionTicket))
1954  return ERROR_DECODING_FAILED;
1955 
1956  //Retrieve the length of the ticket
1957  n = ntohs(message->ticketLen);
1958 
1959  //Malformed NewSessionTicket message?
1960  if(length != (sizeof(TlsNewSessionTicket) + n))
1961  return ERROR_DECODING_FAILED;
1962 
1963 #if (TLS_TICKET_SUPPORT == ENABLED)
1964  //This message must not be sent if the server did not include a SessionTicket
1965  //extension in the ServerHello (refer to RFC 5077, section 3.3)
1966  if(!context->sessionTicketExtReceived)
1967  return ERROR_UNEXPECTED_MESSAGE;
1968 
1969  //Check the length of the session ticket
1970  if(n > 0 && n <= TLS_MAX_TICKET_SIZE)
1971  {
1972  //Release existing session ticket, if any
1973  if(context->ticket != NULL)
1974  {
1975  osMemset(context->ticket, 0, context->ticketLen);
1976  tlsFreeMem(context->ticket);
1977  context->ticket = NULL;
1978  context->ticketLen = 0;
1979  }
1980 
1981  //Allocate a memory block to hold the ticket
1982  context->ticket = tlsAllocMem(n);
1983  //Failed to allocate memory?
1984  if(context->ticket == NULL)
1985  return ERROR_OUT_OF_MEMORY;
1986 
1987  //Copy session ticket
1988  osMemcpy(context->ticket, message->ticket, n);
1989  context->ticketLen = n;
1990 
1991  //The lifetime is relative to when the ticket is received (refer to
1992  //RFC 5077, appendix A)
1993  context->ticketTimestamp = osGetSystemTime();
1994 
1995  //The ticket_lifetime_hint field contains a hint from the server about
1996  //how long the ticket should be stored. A ticket lifetime value of zero
1997  //indicates that the lifetime of the ticket is unspecified
1998  context->ticketLifetime = ntohl(message->ticketLifetimeHint);
1999 
2000  //If the client receives a session ticket from the server, then it
2001  //discards any session ID that was sent in the ServerHello (refer to
2002  //RFC 5077, section 3.4)
2003  context->sessionIdLen = 0;
2004  }
2005 #endif
2006 
2007  //The NewSessionTicket message is sent by the server during the TLS handshake
2008  //before the ChangeCipherSpec message
2010 
2011  //Successful processing
2012  return NO_ERROR;
2013 }
2014 
2015 #endif
@ TLS13_KEY_EXCH_PSK
Definition: tls.h:1224
error_t tlsParseHelloRequest(TlsContext *context, const TlsHelloRequest *message, size_t length)
Parse HelloRequest message.
Definition: tls_client.c:881
#define tlsAllocMem(size)
Definition: tls.h:889
#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:1018
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:2026
TlsDigitalSignature
Definition: tls.h:1860
@ TLS_ALERT_NO_RENEGOTIATION
Definition: tls.h:1170
@ ERROR_WOULD_BLOCK
Definition: error.h:96
void TlsServerHelloDone
ServerHelloDone message.
Definition: tls.h:1962
@ TLS13_KEY_EXCH_PSK_DHE
Definition: tls.h:1225
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:1570
error_t tlsGeneratePskPremasterSecret(TlsContext *context)
Premaster secret generation (for PSK cipher suites)
@ TLS_COMPRESSION_METHOD_NULL
Definition: tls.h:1190
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:1955
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:1572
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:794
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:1017
error_t tlsFormatSignAlgosExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SignatureAlgorithms extension.
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1589
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_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:1228
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:1559
error_t tls13FormatClientEarlyDataExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EarlyData extension.
@ TLS_KEY_EXCH_RSA
Definition: tls.h:1202
error_t tlsParseServerEtmExtension(TlsContext *context, const TlsExtension *encryptThenMac)
Parse EncryptThenMac extension.
TlsExtensionList
Definition: tls.h:1727
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1564
error_t tlsFormatTrustedCaKeysExtension(TlsContext *context, uint8_t *p, size_t *written)
Format TrustedCaKeys extension.
@ TLS_KEY_EXCH_ECDHE_ECDSA
Definition: tls.h:1211
@ TLS_KEY_EXCH_ECDHE_RSA
Definition: tls.h:1209
#define FALSE
Definition: os_port.h:46
@ TLS_KEY_EXCH_ECDH_ANON
Definition: tls.h:1212
error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
Send Alert message.
Definition: tls_common.c:509
error_t tlsParseServerMaxFragLenExtension(TlsContext *context, const TlsExtension *maxFragLen)
Parse MaxFragmentLength extension.
TlsCertAuthorities
Definition: tls.h:1682
#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:947
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:1150
#define TLS_VERSION_1_2
Definition: tls.h:97
@ TLS_GROUP_NONE
Definition: tls.h:1468
@ TLS_KEY_EXCH_DH_ANON
Definition: tls.h:1207
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:1102
error_t tlsSelectClientVersion(TlsContext *context, const TlsServerHello *message, const TlsHelloExtensions *extensions)
Version selection.
Tls12DigitalSignature
Definition: tls.h:1872
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:98
error_t tls13FormatClientCookieExtension(TlsContext *context, uint8_t *p, size_t *written)
Format Cookie extension.
@ TLS_TYPE_SERVER_HELLO
Definition: tls.h:1103
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:839
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1576
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1583
#define TLS_MAX_CERTIFICATES
Definition: tls.h:284
error_t tlsFormatClientAlpnExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ALPN extension.
TlsSignSchemeList
Definition: tls.h:1660
@ TLS_TRANSPORT_PROTOCOL_EAP
Definition: tls.h:1019
@ TLS_STATE_SERVER_HELLO_3
Definition: tls.h:1566
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:1116
@ TLS_KEY_EXCH_DHE_PSK
Definition: tls.h:1215
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1581
uint16_t dtlsTranslateVersion(uint16_t version)
Translate TLS version into DTLS version.
Definition: dtls_misc.c:124
Hello extensions.
Definition: tls.h:2278
Transcript hash calculation.
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1567
@ TLS_KEY_EXCH_RSA_PSK
Definition: tls.h:1214
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:96
@ TLS_KEY_EXCH_NONE
Definition: tls.h:1201
__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:1226
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1560
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:1930
void TlsClientKeyExchange
ClientKeyExchange message.
Definition: tls.h:1969
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1569
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:234
@ TLS_ALERT_LEVEL_FATAL
Definition: tls.h:1135
uint8_t n
error_t tls13ParseServerPreSharedKeyExtension(TlsContext *context, const TlsExtension *selectedIdentity)
Parse PreSharedKey extension.
void TlsHelloRequest
HelloRequest message.
Definition: tls.h:1904
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1574
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC_2
Definition: tls.h:1578
error_t tlsFormatClientEmsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ExtendedMasterSecret extension.
@ TLS_KEY_EXCH_PSK
Definition: tls.h:1213
error_t tlsInitTranscriptHash(TlsContext *context)
Initialize handshake message hashing.
@ TLS_KEY_EXCH_ECDHE_PSK
Definition: tls.h:1216
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:1849
TlsClientHello
Definition: tls.h:1917
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:966
error_t tlsFormatClientCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ClientCertType extension.
@ TLS_TYPE_CERTIFICATE_REQUEST
Definition: tls.h:1113
TLS (Transport Layer Security)
@ TLS_KEY_EXCH_DHE_DSS
Definition: tls.h:1206
error_t tlsParseCertificateRequest(TlsContext *context, const TlsCertificateRequest *message, size_t length)
Parse CertificateRequest message.
Definition: tls_client.c:1545
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:1016
error_t tlsCheckHelloExtensions(TlsMessageType msgType, uint16_t version, TlsHelloExtensions *extensions)
Check Hello extensions.
error_t tlsParseServerRenegoInfoExtension(TlsContext *context, const TlsHelloExtensions *extensions)
Parse RenegotiationInfo extension.
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:1370
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:1935
Handshake message processing (TLS client)
@ TLS_STATE_SERVER_HELLO_DONE
Definition: tls.h:1573
#define tlsFreeMem(p)
Definition: tls.h:894
@ TLS_STATE_SERVER_HELLO_2
Definition: tls.h:1565
error_t tlsGenerateRandomValue(TlsContext *context, uint8_t *random)
Generate client or server random value.
Definition: tls_misc.c:207
TlsNewSessionTicket
Definition: tls.h:1988
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:1204
error_t tlsParseServerKeyExchange(TlsContext *context, const TlsServerKeyExchange *message, size_t length)
Parse ServerKeyExchange message.
Definition: tls_client.c:1386
void TlsServerKeyExchange
ServerKeyExchange message.
Definition: tls.h:1944
#define ntohl(value)
Definition: cpu_endian.h:422
#define TLS_MAX_TICKET_SIZE
Definition: tls.h:172
@ 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.