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