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