tls_common.c
Go to the documentation of this file.
1 /**
2  * @file tls_common.c
3  * @brief Handshake message processing (TLS client and server)
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls.h"
36 #include "tls_cipher_suites.h"
37 #include "tls_handshake.h"
38 #include "tls_client.h"
39 #include "tls_server.h"
40 #include "tls_common.h"
41 #include "tls_certificate.h"
42 #include "tls_sign_generate.h"
43 #include "tls_sign_verify.h"
44 #include "tls_transcript_hash.h"
45 #include "tls_cache.h"
46 #include "tls_record.h"
47 #include "tls_misc.h"
48 #include "tls13_sign_generate.h"
49 #include "tls13_sign_verify.h"
50 #include "dtls_record.h"
51 #include "pkix/pem_import.h"
52 #include "pkix/x509_cert_parse.h"
53 #include "debug.h"
54 
55 //Check TLS library configuration
56 #if (TLS_SUPPORT == ENABLED)
57 
58 
59 /**
60  * @brief Send Certificate message
61  * @param[in] context Pointer to the TLS context
62  * @return Error code
63  **/
64 
66 {
67  error_t error;
68  size_t length;
70 
71  //Initialize status code
72  error = NO_ERROR;
73 
74  //Point to the buffer where to format the message
75  message = (TlsCertificate *) (context->txBuffer + context->txBufferLen);
76 
77 #if (TLS_CLIENT_SUPPORT == ENABLED)
78  //Client mode?
79  if(context->entity == TLS_CONNECTION_END_CLIENT)
80  {
81  //The client must send a Certificate message if the server requests it
82  if(context->clientCertRequested)
83  {
84  //Format Certificate message
85  error = tlsFormatCertificate(context, message, &length);
86 
87  //Check status code
88  if(!error)
89  {
90  //Debug message
91  TRACE_INFO("Sending Certificate message (%" PRIuSIZE " bytes)...\r\n", length);
93 
94  //Send handshake message
95  error = tlsSendHandshakeMessage(context, message, length,
97  }
98  }
99  }
100  else
101 #endif
102 #if (TLS_SERVER_SUPPORT == ENABLED)
103  //Server mode?
104  if(context->entity == TLS_CONNECTION_END_SERVER)
105  {
106  //The server must send a Certificate message whenever the agreed-upon
107  //key exchange method uses certificates for authentication
108  if(context->cert != NULL)
109  {
110  //Format Certificate message
111  error = tlsFormatCertificate(context, message, &length);
112 
113  //Check status code
114  if(!error)
115  {
116  //Debug message
117  TRACE_INFO("Sending Certificate message (%" PRIuSIZE " bytes)...\r\n", length);
119 
120  //Send handshake message
121  error = tlsSendHandshakeMessage(context, message, length,
123  }
124  }
125  }
126  else
127 #endif
128  //Unsupported mode of operation?
129  {
130  //Report an error
131  error = ERROR_FAILURE;
132  }
133 
134  //Check status code
135  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
136  {
137  //Version of TLS prior to TLS 1.3?
138  if(context->version <= TLS_VERSION_1_2)
139  {
140  //Check whether TLS operates as a client or a server
141  if(context->entity == TLS_CONNECTION_END_CLIENT)
142  {
144  }
145  else
146  {
148  }
149  }
150  else
151  {
152  //Check whether TLS operates as a client or a server
153  if(context->entity == TLS_CONNECTION_END_CLIENT)
154  {
155  //Clients must send a CertificateVerify message whenever
156  //authenticating via a certificate
157  if(context->clientCertRequested)
158  {
160  }
161  else
162  {
164  }
165  }
166  else
167  {
168  //Servers must send a CertificateVerify message whenever
169  //authenticating via a certificate
171  }
172  }
173  }
174 
175  //Return status code
176  return error;
177 }
178 
179 
180 /**
181  * @brief Send CertificateVerify message
182  *
183  * The CertificateVerify message is used to provide explicit verification
184  * of a client certificate. This message is only sent following a client
185  * certificate that has signing capability
186  *
187  * @param[in] context Pointer to the TLS context
188  * @return Error code
189  **/
190 
192 {
193  error_t error;
194  size_t length;
196 
197  //Initialize status code
198  error = NO_ERROR;
199 
200  //The CertificateVerify message is only sent following a client certificate
201  //that has signing capability
202  if(context->cert != NULL)
203  {
204  //Check certificate type
205  if(context->cert->type == TLS_CERT_RSA_SIGN ||
206  context->cert->type == TLS_CERT_RSA_PSS_SIGN ||
207  context->cert->type == TLS_CERT_DSS_SIGN ||
208  context->cert->type == TLS_CERT_ECDSA_SIGN ||
209  context->cert->type == TLS_CERT_SM2_SIGN ||
210  context->cert->type == TLS_CERT_ED25519_SIGN ||
211  context->cert->type == TLS_CERT_ED448_SIGN)
212  {
213  //Point to the buffer where to format the message
214  message = (TlsCertificateVerify *) (context->txBuffer + context->txBufferLen);
215 
216  //Format CertificateVerify message
217  error = tlsFormatCertificateVerify(context, message, &length);
218 
219  //Check status code
220  if(!error)
221  {
222  //Debug message
223  TRACE_INFO("Sending CertificateVerify message (%" PRIuSIZE " bytes)...\r\n", length);
225 
226  //Send handshake message
227  error = tlsSendHandshakeMessage(context, message, length,
229  }
230  }
231  }
232 
233  //Check status code
234  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
235  {
236  //Version of TLS prior to TLS 1.3?
237  if(context->version <= TLS_VERSION_1_2)
238  {
239  //Send a ChangeCipherSpec message to the server
241  }
242  else
243  {
244  //Send a Finished message to the peer
245  if(context->entity == TLS_CONNECTION_END_CLIENT)
246  {
248  }
249  else
250  {
252  }
253  }
254  }
255 
256  //Return status code
257  return error;
258 }
259 
260 
261 /**
262  * @brief Send ChangeCipherSpec message
263  *
264  * The change cipher spec message is sent by both the client and the
265  * server to notify the receiving party that subsequent records will be
266  * protected under the newly negotiated CipherSpec and keys
267  *
268  * @param[in] context Pointer to the TLS context
269  * @return Error code
270  **/
271 
273 {
274  error_t error;
275  size_t length;
277 
278  //Point to the buffer where to format the message
279  message = (TlsChangeCipherSpec *) (context->txBuffer + context->txBufferLen);
280 
281  //Format ChangeCipherSpec message
282  error = tlsFormatChangeCipherSpec(context, message, &length);
283 
284  //Check status code
285  if(!error)
286  {
287  //Debug message
288  TRACE_INFO("Sending ChangeCipherSpec message (%" PRIuSIZE " bytes)...\r\n", length);
290 
291 #if (DTLS_SUPPORT == ENABLED)
292  //DTLS protocol?
293  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
294  {
295  //Send ChangeCipherSpec message
296  error = dtlsWriteProtocolData(context, (uint8_t *) message,
298  }
299  else
300 #endif
301  //TLS protocol?
302  {
303  //Send ChangeCipherSpec message
304  error = tlsWriteProtocolData(context, (uint8_t *) message,
306  }
307  }
308 
309  //Check status code
310  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
311  {
312  //Version of TLS prior to TLS 1.3?
313  if(context->version <= TLS_VERSION_1_2)
314  {
315 #if (DTLS_SUPPORT == ENABLED)
316  //Release previous encryption engine first
317  tlsFreeEncryptionEngine(&context->prevEncryptionEngine);
318 
319  //Save current encryption engine for later use
320  context->prevEncryptionEngine = context->encryptionEngine;
321 
322  //Reset encryption engine
323  osMemset(&context->encryptionEngine, 0, sizeof(TlsEncryptionEngine));
324  context->encryptionEngine.epoch = context->prevEncryptionEngine.epoch;
325 #else
326  //Release encryption engine first
327  tlsFreeEncryptionEngine(&context->encryptionEngine);
328 #endif
329 
330  //Inform the record layer that subsequent records will be protected
331  //under the newly negotiated encryption algorithm
332  error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
333  context->entity, NULL);
334 
335  //Check status code
336  if(!error)
337  {
338  //Send a Finished message to the peer
339  if(context->entity == TLS_CONNECTION_END_CLIENT)
340  {
342  }
343  else
344  {
346  }
347  }
348  }
349  else
350  {
351 #if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
352  //The middlebox compatibility mode improves the chance of successfully
353  //connecting through middleboxes
354  if(context->state == TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC ||
355  context->state == TLS_STATE_SERVER_CHANGE_CIPHER_SPEC_2)
356  {
357  //The client can send its second flight
359  }
360  else if(context->state == TLS_STATE_SERVER_CHANGE_CIPHER_SPEC ||
361  context->state == TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC_2)
362  {
363  //All handshake messages after the ServerHello are now encrypted
365  }
366  else
367 #endif
368  {
369  //Middlebox compatibility mode is not implemented
370  error = ERROR_UNEXPECTED_STATE;
371  }
372  }
373  }
374 
375  //Return status code
376  return error;
377 }
378 
379 
380 /**
381  * @brief Send Finished message
382  *
383  * A Finished message is always sent immediately after a change
384  * cipher spec message to verify that the key exchange and
385  * authentication processes were successful. It is essential that a
386  * change cipher spec message be received between the other handshake
387  * messages and the Finished message
388  *
389  * @param[in] context Pointer to the TLS context
390  * @return Error code
391  **/
392 
394 {
395  error_t error;
396  size_t length;
398 
399  //Point to the buffer where to format the message
400  message = (TlsFinished *) (context->txBuffer + context->txBufferLen);
401 
402  //Check whether TLS operates as a client or a server
403  if(context->entity == TLS_CONNECTION_END_CLIENT)
404  {
405  //The verify data is generated from all messages in this handshake
406  //up to but not including the Finished message
408  context->clientVerifyData, &context->clientVerifyDataLen);
409  }
410  else
411  {
412  //The verify data is generated from all messages in this handshake
413  //up to but not including the Finished message
415  context->serverVerifyData, &context->serverVerifyDataLen);
416  }
417 
418  //Check status code
419  if(!error)
420  {
421  //Format Finished message
422  error = tlsFormatFinished(context, message, &length);
423  }
424 
425  //Check status code
426  if(!error)
427  {
428  //Debug message
429  TRACE_INFO("Sending Finished message (%" PRIuSIZE " bytes)...\r\n", length);
431 
432  //Send handshake message
433  error = tlsSendHandshakeMessage(context, message, length,
435  }
436 
437  //Check status code
438  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
439  {
440  //Version of TLS prior to TLS 1.3?
441  if(context->version <= TLS_VERSION_1_2)
442  {
443  //Check whether TLS operates as a client or a server
444  if(context->entity == TLS_CONNECTION_END_CLIENT)
445  {
446  //Abbreviated or full handshake?
447  if(context->resume)
448  {
449  //The client and server can now exchange application-layer data
451  }
452  else
453  {
454 #if (TLS_TICKET_SUPPORT == ENABLED)
455  //The server uses the SessionTicket extension to indicate to
456  //the client that it will send a new session ticket using the
457  //NewSessionTicket handshake message
458  if(context->sessionTicketExtReceived)
459  {
460  //Wait for a NewSessionTicket message from the server
462  }
463  else
464 #endif
465  {
466  //Wait for a ChangeCipherSpec message from the server
468  }
469  }
470  }
471  else
472  {
473  //Abbreviated or full handshake?
474  if(context->resume)
475  {
476  //Wait for a ChangeCipherSpec message from the client
478  }
479  else
480  {
481  //The client and server can now exchange application-layer data
483  }
484  }
485  }
486  else
487  {
488  //Check whether TLS operates as a client or a server
489  if(context->entity == TLS_CONNECTION_END_CLIENT)
490  {
491  //Compute client application traffic keys
493  }
494  else
495  {
496  //Compute server application traffic keys
498  }
499  }
500  }
501 
502  //Return status code
503  return error;
504 }
505 
506 
507 /**
508  * @brief Send Alert message
509  * @param[in] context Pointer to the TLS context
510  * @param[in] level Severity of the message (warning or fatal)
511  * @param[in] description Description of the alert
512  * @return Error code
513  **/
514 
515 error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
516 {
517  error_t error;
518  size_t length;
519  TlsAlert *message;
520 
521  //Point to the buffer where to format the message
522  message = (TlsAlert *) (context->txBuffer + context->txBufferLen);
523 
524  //Format Alert message
525  error = tlsFormatAlert(context, level, description, message, &length);
526 
527  //Check status code
528  if(!error)
529  {
530  //Debug message
531  TRACE_INFO("Sending Alert message (%" PRIuSIZE " bytes)...\r\n", length);
533 
534 #if (DTLS_SUPPORT == ENABLED)
535  //DTLS protocol?
536  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
537  {
538  //Send Alert message
539  error = dtlsWriteProtocolData(context, (uint8_t *) message,
541  }
542  else
543 #endif
544  //TLS protocol?
545  {
546  //Send Alert message
547  error = tlsWriteProtocolData(context, (uint8_t *) message,
549  }
550  }
551 
552  //Alert messages convey the severity of the message
553  if(level == TLS_ALERT_LEVEL_WARNING)
554  {
555  //If an alert with a level of warning is sent, generally the
556  //connection can continue normally
558  {
559  //Either party may initiate a close by sending a close_notify alert
560  context->closeNotifySent = TRUE;
561 
562  //Update FSM state
564  }
565  }
566  else if(level == TLS_ALERT_LEVEL_FATAL)
567  {
568  //Alert messages with a level of fatal result in the immediate
569  //termination of the connection
570  context->fatalAlertSent = TRUE;
571 
572 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
573  //Any connection terminated with a fatal alert must not be resumed
574  if(context->entity == TLS_CONNECTION_END_SERVER)
575  {
576  tlsRemoveFromCache(context);
577  }
578 #endif
579 
580  //Servers and clients must forget any session identifiers
581  osMemset(context->sessionId, 0, 32);
582  context->sessionIdLen = 0;
583 
584  //Update FSM state
586  }
587 
588  //Return status code
589  return error;
590 }
591 
592 
593 /**
594  * @brief Format Certificate message
595  * @param[in] context Pointer to the TLS context
596  * @param[out] message Buffer where to format the Certificate message
597  * @param[out] length Length of the resulting Certificate message
598  * @return Error code
599  **/
600 
602  TlsCertificate *message, size_t *length)
603 {
604  error_t error;
605  size_t n;
606  uint8_t *p;
607  TlsCertList *certList;
608 
609  //Point to the beginning of the handshake message
610  p = message;
611  //Length of the handshake message
612  *length = 0;
613 
614 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
615  //TLS 1.3 currently selected?
616  if(context->version == TLS_VERSION_1_3)
617  {
618  Tls13CertRequestContext *certRequestContext;
619 
620  //Point to the certificate request context
621  certRequestContext = (Tls13CertRequestContext *) p;
622 
623  //Check whether TLS operates as a client or a server
624  if(context->entity == TLS_CONNECTION_END_CLIENT)
625  {
626  //The value of the certificate_request_context field from server's
627  //CertificateRequest message is echoed in the Certificate message
628  if(context->certRequestContextLen > 0)
629  {
630  //Copy certificate request context
631  osMemcpy(certRequestContext->value, context->certRequestContext,
632  context->certRequestContextLen);
633  }
634 
635  //The context is preceded by a length field
636  certRequestContext->length = (uint8_t) context->certRequestContextLen;
637  }
638  else
639  {
640  //In the case of server authentication, this field shall be zero length
641  certRequestContext->length = 0;
642  }
643 
644  //Point to the next field
645  p += sizeof(Tls13CertRequestContext) + certRequestContext->length;
646  //Adjust the length of the Certificate message
647  *length += sizeof(Tls13CertRequestContext) + certRequestContext->length;
648  }
649 #endif
650 
651  //Point to the chain of certificates
652  certList = (TlsCertList *) p;
653 
654 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
655  //Check certificate type
656  if(context->certFormat == TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
657  {
658  //Format the raw public key
659  error = tlsFormatRawPublicKey(context, certList->value, &n);
660  }
661  else
662 #endif
663  {
664  //Format the certificate chain
665  error = tlsFormatCertificateList(context, certList->value, &n);
666  }
667 
668  //Check status code
669  if(!error)
670  {
671  //A 3-byte length field shall precede the certificate list
672  STORE24BE(n, certList->length);
673  //Adjust the length of the Certificate message
674  *length += sizeof(TlsCertList) + n;
675  }
676 
677  //Return status code
678  return error;
679 }
680 
681 
682 /**
683  * @brief Format CertificateVerify message
684  * @param[in] context Pointer to the TLS context
685  * @param[out] message Buffer where to format the CertificateVerify message
686  * @param[out] length Length of the resulting CertificateVerify message
687  * @return Error code
688  **/
689 
692 {
693  error_t error;
694 
695  //Length of the handshake message
696  *length = 0;
697 
698 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
699  //TLS 1.0 or TLS 1.1 currently selected?
700  if(context->version <= TLS_VERSION_1_1)
701  {
702  //In TLS version prior to 1.2, the digitally-signed element combines
703  //MD5 and SHA-1
704  error = tlsGenerateSignature(context, message, length);
705  }
706  else
707 #endif
708 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
709  //TLS 1.2 currently selected?
710  if(context->version == TLS_VERSION_1_2)
711  {
712  //In TLS 1.2, the MD5/SHA-1 combination in the digitally-signed element
713  //has been replaced with a single hash. The signed element now includes
714  //a field that explicitly specifies the hash algorithm used
715  error = tls12GenerateSignature(context, message, length);
716  }
717  else
718 #endif
719 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
720  //TLS 1.3 currently selected?
721  if(context->version == TLS_VERSION_1_3)
722  {
723  //In TLS 1.3, the signed element specifies the signature algorithm used.
724  //The content that is covered under the signature is the transcript hash
725  //output
726  error = tls13GenerateSignature(context, message, length);
727  }
728  else
729 #endif
730  //Invalid TLS version?
731  {
732  //Report an error
733  error = ERROR_INVALID_VERSION;
734  }
735 
736  //Return status code
737  return error;
738 }
739 
740 
741 /**
742  * @brief Format ChangeCipherSpec message
743  * @param[in] context Pointer to the TLS context
744  * @param[out] message Buffer where to format the ChangeCipherSpec message
745  * @param[out] length Length of the resulting ChangeCipherSpec message
746  * @return Error code
747  **/
748 
751 {
752  //The message consists of a single byte of value 1
753  message->type = 1;
754 
755  //Length of the ChangeCipherSpec message
756  *length = sizeof(TlsChangeCipherSpec);
757 
758  //Successful processing
759  return NO_ERROR;
760 }
761 
762 
763 /**
764  * @brief Format Finished message
765  * @param[in] context Pointer to the TLS context
766  * @param[out] message Buffer where to format the Finished message
767  * @param[out] length Length of the resulting Finished message
768  * @return Error code
769  **/
770 
772  TlsFinished *message, size_t *length)
773 {
774  //Check whether TLS operates as a client or a server
775  if(context->entity == TLS_CONNECTION_END_CLIENT)
776  {
777  //Copy the client's verify data
778  osMemcpy(message, context->clientVerifyData, context->clientVerifyDataLen);
779  //Length of the handshake message
780  *length = context->clientVerifyDataLen;
781  }
782  else
783  {
784  //Copy the server's verify data
785  osMemcpy(message, context->serverVerifyData, context->serverVerifyDataLen);
786  //Length of the handshake message
787  *length = context->serverVerifyDataLen;
788  }
789 
790  //Successful processing
791  return NO_ERROR;
792 }
793 
794 
795 /**
796  * @brief Format Alert message
797  * @param[in] context Pointer to the TLS context
798  * @param[in] level Severity of the message (warning or fatal)
799  * @param[in] description Description of the alert
800  * @param[out] message Buffer where to format the Alert message
801  * @param[out] length Length of the resulting Alert message
802  * @return Error code
803  **/
804 
805 error_t tlsFormatAlert(TlsContext *context, uint8_t level,
806  uint8_t description, TlsAlert *message, size_t *length)
807 {
808  //Severity of the message
809  message->level = level;
810  //Description of the alert
811  message->description = description;
812 
813  //Length of the Alert message
814  *length = sizeof(TlsAlert);
815 
816  //Successful processing
817  return NO_ERROR;
818 }
819 
820 
821 /**
822  * @brief Format CertificateAuthorities extension
823  * @param[in] context Pointer to the TLS context
824  * @param[in] p Output stream where to write the CertificateAuthorities extension
825  * @param[out] written Total number of bytes that have been written
826  * @return Error code
827  **/
828 
830  uint8_t *p, size_t *written)
831 {
832  error_t error;
833  size_t n;
834  TlsExtension *extension;
835 
836  //Add the CertificateAuthorities extension
837  extension = (TlsExtension *) p;
838  //Type of the extension
839  extension->type = HTONS(TLS_EXT_CERTIFICATE_AUTHORITIES);
840 
841  //The CertificateAuthorities extension is used to indicate the certificate
842  //authorities (CAs) which an endpoint supports and which should be used by
843  //the receiving endpoint to guide certificate selection
844  error = tlsFormatCertAuthorities(context, extension->value, &n);
845 
846  //Check status code
847  if(!error)
848  {
849  //The list must contains at least one distinguished name
850  if(n > sizeof(TlsCertAuthorities))
851  {
852  //Fix the length of the extension
853  extension->length = htons(n);
854 
855  //Compute the length, in bytes, of the CertificateAuthorities extension
856  n += sizeof(TlsExtension);
857  }
858  else
859  {
860  //The list of distinguished names is empty
861  n = 0;
862  }
863 
864  //Total number of bytes that have been written
865  *written = n;
866  }
867 
868  //Return status code
869  return error;
870 }
871 
872 
873 /**
874  * @brief Format the list of distinguished names of acceptable CAs
875  * @param[in] context Pointer to the TLS context
876  * @param[in] p Output stream where to write the list of distinguished names
877  * @param[out] written Total number of bytes that have been written
878  * @return Error code
879  **/
880 
882  size_t *written)
883 {
884  error_t error;
885  size_t n;
886  size_t pemCertLen;
887  const char_t *trustedCaList;
888  size_t trustedCaListLen;
889  uint8_t *derCert;
890  size_t derCertLen;
891  X509CertInfo *certInfo;
892  TlsCertAuthorities *certAuthorities;
893 
894  //Initialize status code
895  error = NO_ERROR;
896 
897  //The list contains the distinguished names of acceptable certificate
898  //authorities, represented in DER-encoded format
899  certAuthorities = (TlsCertAuthorities *) p;
900 
901  //Point to the first certificate authority
902  p = certAuthorities->value;
903  //Length of the list in bytes
904  n = 0;
905 
906  //Point to the first trusted CA certificate
907  trustedCaList = context->trustedCaList;
908  //Get the total length, in bytes, of the trusted CA list
909  trustedCaListLen = context->trustedCaListLen;
910 
911  //Allocate a memory buffer to store X.509 certificate info
912  certInfo = tlsAllocMem(sizeof(X509CertInfo));
913 
914  //Successful memory allocation?
915  if(certInfo != NULL)
916  {
917  //Loop through the list of trusted CA certificates
918  while(trustedCaListLen > 0 && error == NO_ERROR)
919  {
920  //The first pass calculates the length of the DER-encoded certificate
921  error = pemImportCertificate(trustedCaList, trustedCaListLen, NULL,
922  &derCertLen, &pemCertLen);
923 
924  //Check status code
925  if(!error)
926  {
927  //Allocate a memory buffer to hold the DER-encoded certificate
928  derCert = tlsAllocMem(derCertLen);
929 
930  //Successful memory allocation?
931  if(derCert != NULL)
932  {
933  //The second pass decodes the PEM certificate
934  error = pemImportCertificate(trustedCaList, trustedCaListLen,
935  derCert, &derCertLen, NULL);
936 
937  //Check status code
938  if(!error)
939  {
940  //Parse X.509 certificate
941  error = x509ParseCertificate(derCert, derCertLen, certInfo);
942  }
943 
944  //Valid CA certificate?
945  if(!error)
946  {
947  //Each distinguished name is preceded by a 2-byte length field
948  STORE16BE(certInfo->tbsCert.subject.raw.length, p);
949 
950  //The distinguished name shall be DER-encoded
951  osMemcpy(p + 2, certInfo->tbsCert.subject.raw.value,
952  certInfo->tbsCert.subject.raw.length);
953 
954  //Advance write pointer
955  p += certInfo->tbsCert.subject.raw.length + 2;
956  n += certInfo->tbsCert.subject.raw.length + 2;
957  }
958  else
959  {
960  //Discard current CA certificate
961  error = NO_ERROR;
962  }
963 
964  //Free previously allocated memory
965  tlsFreeMem(derCert);
966  }
967  else
968  {
969  //Failed to allocate memory
970  error = ERROR_OUT_OF_MEMORY;
971  }
972 
973  //Advance read pointer
974  trustedCaList += pemCertLen;
975  trustedCaListLen -= pemCertLen;
976  }
977  else
978  {
979  //End of file detected
980  trustedCaListLen = 0;
981  error = NO_ERROR;
982  }
983  }
984 
985  //Fix the length of the list
986  certAuthorities->length = htons(n);
987 
988  //Free previously allocated memory
989  tlsFreeMem(certInfo);
990  }
991  else
992  {
993  //Failed to allocate memory
994  error = ERROR_OUT_OF_MEMORY;
995  }
996 
997  //Check status code
998  if(!error)
999  {
1000  //Total number of bytes that have been written
1001  *written = sizeof(TlsCertAuthorities) + n;
1002  }
1003 
1004  //Return status code
1005  return error;
1006 }
1007 
1008 
1009 /**
1010  * @brief Parse Certificate message
1011  * @param[in] context Pointer to the TLS context
1012  * @param[in] message Incoming Certificate message to parse
1013  * @param[in] length Message length
1014  * @return Error code
1015  **/
1016 
1018  const TlsCertificate *message, size_t length)
1019 {
1020  error_t error;
1021  size_t n;
1022  const uint8_t *p;
1023  const TlsCertList *certList;
1024 
1025  //Debug message
1026  TRACE_INFO("Certificate message received (%" PRIuSIZE " bytes)...\r\n", length);
1028 
1029  //Check whether TLS operates as a client or a server
1030  if(context->entity == TLS_CONNECTION_END_CLIENT)
1031  {
1032  //Version of TLS prior to TLS 1.3?
1033  if(context->version <= TLS_VERSION_1_2)
1034  {
1035  //Check current state
1036  if(context->state != TLS_STATE_SERVER_CERTIFICATE)
1037  return ERROR_UNEXPECTED_MESSAGE;
1038  }
1039  else
1040  {
1041  //The CertificateRequest message is optional
1042  if(context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1043  context->state != TLS_STATE_SERVER_CERTIFICATE)
1044  {
1045  return ERROR_UNEXPECTED_MESSAGE;
1046  }
1047  }
1048  }
1049  else
1050  {
1051  //Check current state
1052  if(context->state != TLS_STATE_CLIENT_CERTIFICATE)
1053  return ERROR_UNEXPECTED_MESSAGE;
1054  }
1055 
1056  //Point to the beginning of the handshake message
1057  p = message;
1058 
1059 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1060  //TLS 1.3 currently selected?
1061  if(context->version == TLS_VERSION_1_3)
1062  {
1063  const Tls13CertRequestContext *certRequestContext;
1064 
1065  //Point to the certificate request context
1066  certRequestContext = (Tls13CertRequestContext *) p;
1067 
1068  //Malformed Certificate message?
1069  if(length < sizeof(Tls13CertRequestContext))
1070  return ERROR_DECODING_FAILED;
1071  if(length < (sizeof(Tls13CertRequestContext) + certRequestContext->length))
1072  return ERROR_DECODING_FAILED;
1073 
1074  //Point to the next field
1075  p += sizeof(Tls13CertRequestContext) + certRequestContext->length;
1076  //Remaining bytes to process
1077  length -= sizeof(Tls13CertRequestContext) + certRequestContext->length;
1078  }
1079 #endif
1080 
1081  //Point to the chain of certificates
1082  certList = (TlsCertList *) p;
1083 
1084  //Malformed Certificate message?
1085  if(length < sizeof(TlsCertList))
1086  return ERROR_DECODING_FAILED;
1087 
1088  //Get the size occupied by the certificate list
1089  n = LOAD24BE(certList->length);
1090  //Remaining bytes to process
1091  length -= sizeof(TlsCertList);
1092 
1093  //Malformed Certificate message?
1094  if(n != length)
1095  return ERROR_DECODING_FAILED;
1096 
1097  //Non-empty certificate list?
1098  if(n > 0)
1099  {
1100 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1101  //Check certificate type
1102  if(context->peerCertFormat == TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
1103  {
1104  //Parse the raw public key
1105  error = tlsParseRawPublicKey(context, certList->value, n);
1106  }
1107  else
1108 #endif
1109  {
1110  //Parse the certificate chain
1111  error = tlsParseCertificateList(context, certList->value, n);
1112  }
1113  }
1114  else
1115  {
1116 #if (TLS_SERVER_SUPPORT == ENABLED)
1117  //Server mode?
1118  if(context->entity == TLS_CONNECTION_END_SERVER)
1119  {
1120  //Check whether client authentication is required
1121  if(context->clientAuthMode == TLS_CLIENT_AUTH_REQUIRED)
1122  {
1123  //Version of TLS prior to TLS 1.3?
1124  if(context->version <= TLS_VERSION_1_2)
1125  {
1126  //If the client does not send any certificates, the server
1127  //responds with a fatal handshake_failure alert (refer to
1128  //RFC 5246, section 7.4.6)
1129  error = ERROR_HANDSHAKE_FAILED;
1130  }
1131  else
1132  {
1133  //If the client does not send any certificates, the server
1134  //aborts the handshake with a certificate_required alert (refer
1135  //to RFC 8446, section 4.4.2.4)
1137  }
1138  }
1139  else
1140  {
1141  //The client did not send any certificates
1142  context->peerCertType = TLS_CERT_NONE;
1143  //The server may continue the handshake without client authentication
1144  error = NO_ERROR;
1145  }
1146  }
1147  else
1148 #endif
1149  //Client mode?
1150  {
1151  //The server's certificate list must always be non-empty (refer to
1152  //RFC 8446, section 4.4.2)
1153  error = ERROR_DECODING_FAILED;
1154  }
1155  }
1156 
1157  //Check status code
1158  if(!error)
1159  {
1160  //Version of TLS prior to TLS 1.3?
1161  if(context->version <= TLS_VERSION_1_2)
1162  {
1163  //Check whether TLS operates as a client or a server
1164  if(context->entity == TLS_CONNECTION_END_CLIENT)
1165  {
1166  //The server does not send a ServerKeyExchange message when RSA
1167  //key exchange method is used
1168  if(context->keyExchMethod == TLS_KEY_EXCH_RSA)
1169  {
1171  }
1172  else
1173  {
1175  }
1176  }
1177  else
1178  {
1179  //Wait for a ClientKeyExchange message from the client
1181  }
1182  }
1183  else
1184  {
1185  //Check whether TLS operates as a client or a server
1186  if(context->entity == TLS_CONNECTION_END_CLIENT)
1187  {
1188  //The server must send a CertificateVerify message immediately
1189  //after the Certificate message
1191  }
1192  else
1193  {
1194  //The client must send a CertificateVerify message when the
1195  //Certificate message is non-empty
1196  if(context->peerCertType != TLS_CERT_NONE)
1197  {
1199  }
1200  else
1201  {
1203  }
1204  }
1205  }
1206  }
1207 
1208  //Return status code
1209  return error;
1210 }
1211 
1212 
1213 /**
1214  * @brief Parse CertificateVerify message
1215  *
1216  * The CertificateVerify message is used to provide explicit verification
1217  * of a client certificate. This message is only sent following a client
1218  * certificate that has signing capability
1219  *
1220  * @param[in] context Pointer to the TLS context
1221  * @param[in] message Incoming CertificateVerify message to parse
1222  * @param[in] length Message length
1223  * @return Error code
1224  **/
1225 
1227  const TlsCertificateVerify *message, size_t length)
1228 {
1229  error_t error;
1230 
1231  //Debug message
1232  TRACE_INFO("CertificateVerify message received (%" PRIuSIZE " bytes)...\r\n", length);
1234 
1235  //Check whether TLS operates as a client or a server
1236  if(context->entity == TLS_CONNECTION_END_CLIENT)
1237  {
1238  //Check current state
1239  if(context->state != TLS_STATE_SERVER_CERTIFICATE_VERIFY)
1240  return ERROR_UNEXPECTED_MESSAGE;
1241  }
1242  else
1243  {
1244  //Check current state
1245  if(context->state != TLS_STATE_CLIENT_CERTIFICATE_VERIFY)
1246  return ERROR_UNEXPECTED_MESSAGE;
1247  }
1248 
1249 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
1250  //TLS 1.0 or TLS 1.1 currently selected?
1251  if(context->version <= TLS_VERSION_1_1)
1252  {
1253  //In TLS version prior to 1.2, the digitally-signed element combines
1254  //MD5 and SHA-1
1255  error = tlsVerifySignature(context, message, length);
1256  }
1257  else
1258 #endif
1259 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1260  //TLS 1.2 currently selected?
1261  if(context->version == TLS_VERSION_1_2)
1262  {
1263  //In TLS 1.2, the MD5/SHA-1 combination in the digitally-signed element
1264  //has been replaced with a single hash. The signed element now includes
1265  //a field that explicitly specifies the hash algorithm used
1266  error = tls12VerifySignature(context, message, length);
1267  }
1268  else
1269 #endif
1270 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1271  //TLS 1.3 currently selected?
1272  if(context->version == TLS_VERSION_1_3)
1273  {
1274  //In TLS 1.3, the signed element specifies the signature algorithm used.
1275  //The content that is covered under the signature is the transcript hash
1276  //output
1277  error = tls13VerifySignature(context, message, length);
1278  }
1279  else
1280 #endif
1281  //Invalid TLS version?
1282  {
1283  //Report an error
1284  error = ERROR_INVALID_VERSION;
1285  }
1286 
1287  //Check status code
1288  if(!error)
1289  {
1290  //Version of TLS prior to TLS 1.3?
1291  if(context->version <= TLS_VERSION_1_2)
1292  {
1293  //Wait for a ChangeCipherSpec message from the client
1295  }
1296  else
1297  {
1298  //Wait for a Finished message from the peer
1299  if(context->entity == TLS_CONNECTION_END_CLIENT)
1300  {
1302  }
1303  else
1304  {
1306  }
1307  }
1308  }
1309 
1310  //Return status code
1311  return error;
1312 }
1313 
1314 
1315 /**
1316  * @brief Parse ChangeCipherSpec message
1317  * @param[in] context Pointer to the TLS context
1318  * @param[in] message Incoming ChangeCipherSpec message to parse
1319  * @param[in] length Message length
1320  * @return Error code
1321  **/
1322 
1324  const TlsChangeCipherSpec *message, size_t length)
1325 {
1326  error_t error;
1327 
1328  //Debug message
1329  TRACE_INFO("ChangeCipherSpec message received (%" PRIuSIZE " bytes)...\r\n", length);
1331 
1332  //Check the length of the ChangeCipherSpec message
1333  if(length != sizeof(TlsChangeCipherSpec))
1334  return ERROR_DECODING_FAILED;
1335 
1336  //The message consists of a single byte of value 1
1337  if(message->type != 0x01)
1338  return ERROR_DECODING_FAILED;
1339 
1340  //Version of TLS prior to TLS 1.3?
1341  if(context->version <= TLS_VERSION_1_2)
1342  {
1343  //Check whether TLS operates as a client or a server
1344  if(context->entity == TLS_CONNECTION_END_CLIENT)
1345  {
1346  //Check current state
1347  if(context->state != TLS_STATE_SERVER_CHANGE_CIPHER_SPEC)
1348  return ERROR_UNEXPECTED_MESSAGE;
1349  }
1350  else
1351  {
1352  //Check current state
1353  if(context->state != TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC)
1354  return ERROR_UNEXPECTED_MESSAGE;
1355  }
1356 
1357  //Release decryption engine first
1358  tlsFreeEncryptionEngine(&context->decryptionEngine);
1359 
1360  //Check whether TLS operates as a client or a server
1361  if(context->entity == TLS_CONNECTION_END_CLIENT)
1362  {
1363  //Initialize decryption engine using server write keys
1364  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
1366  //Any error to report?
1367  if(error)
1368  return error;
1369 
1370  //Wait for a Finished message from the server
1372  }
1373  else
1374  {
1375  //Initialize decryption engine using client write keys
1376  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
1378  //Any error to report?
1379  if(error)
1380  return error;
1381 
1382  //Wait for a Finished message from the client
1384  }
1385 
1386 #if (DTLS_SUPPORT == ENABLED)
1387  //DTLS protocol?
1388  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
1389  {
1390  //Initialize sliding window
1391  dtlsInitReplayWindow(context);
1392  }
1393 #endif
1394  }
1395  else
1396  {
1397  //In TLS 1.3, the ChangeCipherSpec message is used only for compatibility
1398  //purposes and must be dropped without further processing
1399  if(context->entity == TLS_CONNECTION_END_CLIENT)
1400  {
1401  //A ChangeCipherSpec message received received before the first
1402  //ClientHello message or after the server's Finished message must
1403  //be treated as an unexpected record type
1404  if(context->state != TLS_STATE_SERVER_HELLO &&
1405  context->state != TLS_STATE_SERVER_HELLO_2 &&
1406  context->state != TLS_STATE_ENCRYPTED_EXTENSIONS &&
1407  context->state != TLS_STATE_CERTIFICATE_REQUEST &&
1408  context->state != TLS_STATE_SERVER_CERTIFICATE &&
1409  context->state != TLS_STATE_SERVER_CERTIFICATE_VERIFY &&
1410  context->state != TLS_STATE_SERVER_FINISHED)
1411  {
1412  //Report an error
1413  return ERROR_UNEXPECTED_MESSAGE;
1414  }
1415  }
1416  else
1417  {
1418  //A ChangeCipherSpec message received received before the first
1419  //ClientHello message or after the client's Finished message must
1420  //be treated as an unexpected record type
1421  if(context->state != TLS_STATE_CLIENT_HELLO_2 &&
1422  context->state != TLS_STATE_CLIENT_CERTIFICATE &&
1423  context->state != TLS_STATE_CLIENT_CERTIFICATE_VERIFY &&
1424  context->state != TLS_STATE_CLIENT_FINISHED)
1425  {
1426  //Report an error
1427  return ERROR_UNEXPECTED_MESSAGE;
1428  }
1429  }
1430 
1431 #if (TLS_MAX_CHANGE_CIPHER_SPEC_MESSAGES > 0)
1432  //Increment the count of consecutive ChangeCipherSpec messages
1433  context->changeCipherSpecCount++;
1434 
1435  //Do not allow too many consecutive ChangeCipherSpec messages
1436  if(context->changeCipherSpecCount > TLS_MAX_CHANGE_CIPHER_SPEC_MESSAGES)
1437  return ERROR_UNEXPECTED_MESSAGE;
1438 #endif
1439  }
1440 
1441  //Successful processing
1442  return NO_ERROR;
1443 }
1444 
1445 
1446 /**
1447  * @brief Parse Finished message
1448  * @param[in] context Pointer to the TLS context
1449  * @param[in] message Incoming Finished message to parse
1450  * @param[in] length Message length
1451  * @return Error code
1452  **/
1453 
1455  const TlsFinished *message, size_t length)
1456 {
1457  error_t error;
1458 
1459  //Debug message
1460  TRACE_INFO("Finished message received (%" PRIuSIZE " bytes)...\r\n", length);
1462 
1463  //Check whether TLS operates as a client or a server
1464  if(context->entity == TLS_CONNECTION_END_CLIENT)
1465  {
1466  //Check current state
1467  if(context->state != TLS_STATE_SERVER_FINISHED)
1468  return ERROR_UNEXPECTED_MESSAGE;
1469 
1470  //The verify data is generated from all messages in this handshake
1471  //up to but not including the Finished message
1473  context->serverVerifyData, &context->serverVerifyDataLen);
1474  //Unable to generate the verify data?
1475  if(error)
1476  return error;
1477 
1478  //Check the length of the Finished message
1479  if(length != context->serverVerifyDataLen)
1480  {
1481 #if (TLS_MAX_EMPTY_RECORDS > 0)
1482  return ERROR_INVALID_SIGNATURE;
1483 #else
1484  return ERROR_DECODING_FAILED;
1485 #endif
1486  }
1487 
1488  //Check the resulting verify data
1489  if(osMemcmp(message, context->serverVerifyData, context->serverVerifyDataLen))
1490  return ERROR_INVALID_SIGNATURE;
1491  }
1492  else
1493  {
1494  //Check current state
1495  if(context->state != TLS_STATE_CLIENT_FINISHED)
1496  return ERROR_UNEXPECTED_MESSAGE;
1497 
1498  //The verify data is generated from all messages in this handshake
1499  //up to but not including the Finished message
1501  context->clientVerifyData, &context->clientVerifyDataLen);
1502  //Unable to generate the verify data?
1503  if(error)
1504  return error;
1505 
1506  //Check the length of the Finished message
1507  if(length != context->clientVerifyDataLen)
1508  {
1509 #if (TLS_MAX_EMPTY_RECORDS > 0)
1510  return ERROR_INVALID_SIGNATURE;
1511 #else
1512  return ERROR_DECODING_FAILED;
1513 #endif
1514  }
1515 
1516  //Check the resulting verify data
1517  if(osMemcmp(message, context->clientVerifyData, context->clientVerifyDataLen))
1518  return ERROR_INVALID_SIGNATURE;
1519  }
1520 
1521  //Version of TLS prior to TLS 1.3?
1522  if(context->version <= TLS_VERSION_1_2)
1523  {
1524  //Another handshake message cannot be packed in the same record as the
1525  //Finished
1526  if(context->rxBufferLen != 0)
1527  return ERROR_UNEXPECTED_MESSAGE;
1528 
1529  //Check whether TLS operates as a client or a server
1530  if(context->entity == TLS_CONNECTION_END_CLIENT)
1531  {
1532  //Abbreviated or full handshake?
1533  if(context->resume)
1534  {
1535  //Send a ChangeCipherSpec message to the server
1537  }
1538  else
1539  {
1540  //The client and server can now exchange application-layer data
1542  }
1543  }
1544  else
1545  {
1546  //Abbreviated or full handshake?
1547  if(context->resume)
1548  {
1549  //The client and server can now exchange application-layer data
1551  }
1552  else
1553  {
1554 #if (TLS_TICKET_SUPPORT == ENABLED)
1555  //The server uses the SessionTicket extension to indicate to
1556  //the client that it will send a new session ticket using the
1557  //NewSessionTicket handshake message
1558  if(context->sessionTicketExtSent)
1559  {
1560  //Send a NewSessionTicket message to the client
1562  }
1563  else
1564 #endif
1565  {
1566  //Send a ChangeCipherSpec message to the client
1568  }
1569  }
1570  }
1571  }
1572  else
1573  {
1574  //Check whether TLS operates as a client or a server
1575  if(context->entity == TLS_CONNECTION_END_CLIENT)
1576  {
1577  //Compute server application traffic keys
1579  }
1580  else
1581  {
1582  //Compute client application traffic keys
1584  }
1585  }
1586 
1587  //Successful processing
1588  return NO_ERROR;
1589 }
1590 
1591 
1592 /**
1593  * @brief Parse Alert message
1594  * @param[in] context Pointer to the TLS context
1595  * @param[in] message Incoming Alert message to parse
1596  * @param[in] length Message length
1597  * @return Error code
1598  **/
1599 
1601  const TlsAlert *message, size_t length)
1602 {
1603  //Debug message
1604  TRACE_INFO("Alert message received (%" PRIuSIZE " bytes)...\r\n", length);
1606 
1607  //Check message length
1608  if(length != sizeof(TlsAlert))
1609  return ERROR_INVALID_LENGTH;
1610 
1611  //Debug message
1612  TRACE_DEBUG(" Level = %" PRIu8 "\r\n", message->level);
1613  TRACE_DEBUG(" Description = %" PRIu8 "\r\n", message->description);
1614 
1615  //Alert messages convey the severity of the message
1616  if(message->level == TLS_ALERT_LEVEL_WARNING)
1617  {
1618 #if (TLS_MAX_WARNING_ALERTS > 0)
1619  //Increment the count of consecutive warning alerts
1620  context->alertCount++;
1621 
1622  //Do not allow too many consecutive warning alerts
1623  if(context->alertCount > TLS_MAX_WARNING_ALERTS)
1624  return ERROR_UNEXPECTED_MESSAGE;
1625 #endif
1626 
1627  //Check alert type
1628  if(message->description == TLS_ALERT_CLOSE_NOTIFY)
1629  {
1630  //A closure alert has been received
1631  context->closeNotifyReceived = TRUE;
1632 
1633  //Close down the connection immediately
1634  if(context->state == TLS_STATE_APPLICATION_DATA)
1635  {
1637  }
1638  }
1639  else if(message->description == TLS_ALERT_USER_CANCELED)
1640  {
1641  //This alert notifies the recipient that the sender is canceling the
1642  //handshake for some reason unrelated to a protocol failure
1643  }
1644  else
1645  {
1646  //TLS 1.3 currently selected?
1647  if(context->version == TLS_VERSION_1_3)
1648  {
1649  //Unknown alert types must be treated as error alerts
1650  return ERROR_DECODING_FAILED;
1651  }
1652  }
1653  }
1654  else if(message->level == TLS_ALERT_LEVEL_FATAL)
1655  {
1656  //A fatal alert message has been received
1657  context->fatalAlertReceived = TRUE;
1658 
1659 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1660  //Any connection terminated with a fatal alert must not be resumed
1661  if(context->entity == TLS_CONNECTION_END_SERVER)
1662  {
1663  tlsRemoveFromCache(context);
1664  }
1665 #endif
1666 
1667  //Servers and clients must forget any session identifiers
1668  osMemset(context->sessionId, 0, 32);
1669  context->sessionIdLen = 0;
1670 
1671  //Alert messages with a level of fatal result in the immediate
1672  //termination of the connection
1673  tlsChangeState(context, TLS_STATE_CLOSED);
1674  }
1675  else
1676  {
1677  //Report an error
1678  return ERROR_ILLEGAL_PARAMETER;
1679  }
1680 
1681  //Successful processing
1682  return NO_ERROR;
1683 }
1684 
1685 #endif
#define TLS_MAX_CHANGE_CIPHER_SPEC_MESSAGES
Definition: tls.h:850
#define tlsAllocMem(size)
Definition: tls.h:874
#define htons(value)
Definition: cpu_endian.h:413
@ TLS_CERT_FORMAT_RAW_PUBLIC_KEY
Definition: tls.h:1209
TLS helper functions.
#define TRACE_INFO_ARRAY(p, a, n)
Definition: debug.h:106
X.509 certificate parsing.
error_t tlsFormatCertAuthorities(TlsContext *context, uint8_t *p, size_t *written)
Format the list of distinguished names of acceptable CAs.
Definition: tls_common.c:881
RSA/DSA/ECDSA/SM2/EdDSA signature verification (TLS 1.3)
TLS cipher suites.
void dtlsInitReplayWindow(TlsContext *context)
Initialize sliding window.
Definition: dtls_misc.c:452
X509TbsCertificate tbsCert
Definition: x509_common.h:1121
@ TLS_ALERT_CLOSE_NOTIFY
Definition: tls.h:1114
@ ERROR_WOULD_BLOCK
Definition: error.h:96
error_t tlsSendChangeCipherSpec(TlsContext *context)
Send ChangeCipherSpec message.
Definition: tls_common.c:272
TLS handshake.
@ TLS_STATE_SERVER_KEY_EXCHANGE
Definition: tls.h:1523
__weak_func error_t tlsParseCertificateList(TlsContext *context, const uint8_t *p, size_t length)
Parse certificate chain.
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:244
error_t tls13VerifySignature(TlsContext *context, const uint8_t *p, size_t length)
Digital signature verification (TLS 1.3)
error_t tlsSendCertificate(TlsContext *context)
Send Certificate message.
Definition: tls_common.c:65
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
uint8_t p
Definition: ndp.h:300
uint8_t message[]
Definition: chap.h:154
error_t tls12VerifySignature(TlsContext *context, const uint8_t *p, size_t length)
Digital signature verification (TLS 1.2)
#define TRUE
Definition: os_port.h:50
error_t x509ParseCertificate(const uint8_t *data, size_t length, X509CertInfo *certInfo)
Parse a X.509 certificate.
@ TLS_STATE_CERTIFICATE_REQUEST
Definition: tls.h:1525
@ TLS_TYPE_CHANGE_CIPHER_SPEC
Definition: tls.h:1054
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:986
Session cache management.
error_t tlsParseChangeCipherSpec(TlsContext *context, const TlsChangeCipherSpec *message, size_t length)
Parse ChangeCipherSpec message.
Definition: tls_common.c:1323
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1541
#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
@ TLS_STATE_SERVER_APP_TRAFFIC_KEYS
Definition: tls.h:1538
@ TLS_CERT_DSS_SIGN
Definition: tls.h:1222
error_t tlsFormatCertificateVerify(TlsContext *context, TlsCertificateVerify *message, size_t *length)
Format CertificateVerify message.
Definition: tls_common.c:690
TlsChangeCipherSpec
Definition: tls.h:1918
TlsExtension
Definition: tls.h:1629
@ ERROR_INVALID_VERSION
Definition: error.h:118
error_t tlsSendHandshakeMessage(TlsContext *context, const void *data, size_t length, TlsMessageType type)
Send handshake message.
error_t tlsFormatFinished(TlsContext *context, TlsFinished *message, size_t *length)
Format Finished message.
Definition: tls_common.c:771
@ TLS_KEY_EXCH_RSA
Definition: tls.h:1171
__weak_func error_t tlsComputeVerifyData(TlsContext *context, TlsConnectionEnd entity, uint8_t *verifyData, size_t *verifyDataLen)
Compute verify data from previous handshake messages.
@ TLS_TYPE_CERTIFICATE
Definition: tls.h:1080
void TlsFinished
Finished message.
Definition: tls.h:1908
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1517
@ TLS_ALERT_LEVEL_WARNING
Definition: tls.h:1103
error_t pemImportCertificate(const char_t *input, size_t inputLen, uint8_t *output, size_t *outputLen, size_t *consumed)
Decode a PEM file containing a certificate.
Definition: pem_import.c:55
error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
Send Alert message.
Definition: tls_common.c:515
error_t tlsParseAlert(TlsContext *context, const TlsAlert *message, size_t length)
Parse Alert message.
Definition: tls_common.c:1600
PEM file import functions.
TlsCertAuthorities
Definition: tls.h:1617
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
X.509 certificate.
Definition: x509_common.h:1119
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t tls12GenerateSignature(TlsContext *context, uint8_t *p, size_t *length)
Digital signature generation (TLS 1.2)
DTLS record protocol.
@ TLS_CERT_ED25519_SIGN
Definition: tls.h:1235
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:999
void tlsFreeEncryptionEngine(TlsEncryptionEngine *encryptionEngine)
Release encryption engine.
Definition: tls_misc.c:921
#define TLS_VERSION_1_2
Definition: tls.h:96
void TlsCertificateVerify
CertificateVerify message.
Definition: tls.h:1889
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
TlsAlert
Definition: tls.h:1929
#define STORE16BE(a, p)
Definition: cpu_endian.h:262
error_t tlsRemoveFromCache(TlsContext *context)
Remove current session from cache.
Definition: tls_cache.c:269
@ TLS_STATE_SERVER_FINISHED
Definition: tls.h:1536
@ TLS_TYPE_ALERT
Definition: tls.h:1055
#define TLS_VERSION_1_3
Definition: tls.h:97
Handshake message processing (TLS client and server)
@ TLS_CERT_RSA_PSS_SIGN
Definition: tls.h:1233
@ ERROR_INVALID_LENGTH
Definition: error.h:111
error_t tlsFormatCertAuthoritiesExtension(TlsContext *context, uint8_t *p, size_t *written)
Format CertificateAuthorities extension.
Definition: tls_common.c:829
error_t tlsFormatAlert(TlsContext *context, uint8_t level, uint8_t description, TlsAlert *message, size_t *length)
Format Alert message.
Definition: tls_common.c:805
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1529
@ TLS_TYPE_CERTIFICATE_VERIFY
Definition: tls.h:1084
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1534
@ TLS_ALERT_USER_CANCELED
Definition: tls.h:1138
@ TLS_CERT_ED448_SIGN
Definition: tls.h:1236
#define TLS_MAX_WARNING_ALERTS
Definition: tls.h:836
error_t tlsFormatRawPublicKey(TlsContext *context, uint8_t *p, size_t *written)
Format raw public key.
error_t tls13GenerateSignature(TlsContext *context, uint8_t *p, size_t *length)
Digital signature generation (TLS 1.3)
@ TLS_EXT_CERTIFICATE_AUTHORITIES
Definition: tls.h:1376
error_t dtlsWriteProtocolData(TlsContext *context, const uint8_t *data, size_t length, TlsContentType contentType)
Write protocol data.
Definition: dtls_record.c:58
@ TLS_CERT_RSA_SIGN
Definition: tls.h:1221
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
@ TLS_STATE_CLIENT_APP_TRAFFIC_KEYS
Definition: tls.h:1533
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1539
@ TLS_CERT_SM2_SIGN
Definition: tls.h:1234
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC
Definition: tls.h:1530
TlsCertList
Definition: tls.h:1606
RSA/DSA/ECDSA/EdDSA signature verification.
error_t tlsFormatCertificateList(TlsContext *context, uint8_t *p, size_t *written)
Format certificate chain.
error_t tlsParseCertificate(TlsContext *context, const TlsCertificate *message, size_t length)
Parse Certificate message.
Definition: tls_common.c:1017
Transcript hash calculation.
RSA/DSA/ECDSA/SM2/EdDSA signature generation (TLS 1.3)
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1520
#define TRACE_DEBUG(...)
Definition: debug.h:119
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
#define TLS_VERSION_1_1
Definition: tls.h:95
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1513
@ TLS_STATE_CLOSING
Definition: tls.h:1542
@ TLS_STATE_SERVER_CERTIFICATE_VERIFY
Definition: tls.h:1524
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
error_t tlsParseRawPublicKey(TlsContext *context, const uint8_t *p, size_t length)
Parse raw public key.
error_t tlsFormatChangeCipherSpec(TlsContext *context, TlsChangeCipherSpec *message, size_t *length)
Format ChangeCipherSpec message.
Definition: tls_common.c:749
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1522
@ TLS_CLIENT_AUTH_REQUIRED
Definition: tls.h:1011
@ TLS_ALERT_LEVEL_FATAL
Definition: tls.h:1104
#define HTONS(value)
Definition: cpu_endian.h:410
uint8_t n
@ ERROR_UNEXPECTED_STATE
Definition: error.h:99
@ TLS_STATE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1528
error_t tlsGenerateSignature(TlsContext *context, uint8_t *p, size_t *length)
Digital signature generation (TLS 1.0 or TLS 1.1)
error_t tlsParseFinished(TlsContext *context, const TlsFinished *message, size_t length)
Parse Finished message.
Definition: tls_common.c:1454
@ TLS_TYPE_FINISHED
Definition: tls.h:1086
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1527
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC_2
Definition: tls.h:1531
@ TLS_STATE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1521
Handshake message processing (TLS server)
error_t tlsWriteProtocolData(TlsContext *context, const uint8_t *data, size_t length, TlsContentType contentType)
Write protocol data.
Definition: tls_record.c:54
#define LOAD24BE(p)
Definition: cpu_endian.h:197
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:998
X.509 certificate handling.
error_t tlsSendFinished(TlsContext *context)
Send Finished message.
Definition: tls_common.c:393
TLS (Transport Layer Security)
error_t tlsParseCertificateVerify(TlsContext *context, const TlsCertificateVerify *message, size_t length)
Parse CertificateVerify message.
Definition: tls_common.c:1226
#define STORE24BE(a, p)
Definition: cpu_endian.h:273
@ TLS_CERT_ECDSA_SIGN
Definition: tls.h:1228
const uint8_t * value
Definition: x509_common.h:702
@ TLS_CERT_NONE
Definition: tls.h:1220
__weak_func error_t tlsInitEncryptionEngine(TlsContext *context, TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity, const uint8_t *secret)
Initialize encryption engine.
Definition: tls_misc.c:674
Tls13CertRequestContext
Definition: tls13_misc.h:286
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC_2
Definition: tls.h:1535
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
error_t tlsVerifySignature(TlsContext *context, const uint8_t *p, size_t length)
Digital signature verification (TLS 1.0 and TLS 1.1)
@ ERROR_DECODING_FAILED
Definition: error.h:242
#define PRIuSIZE
#define osMemset(p, value, length)
Definition: os_port.h:138
Handshake message processing (TLS client)
#define tlsFreeMem(p)
Definition: tls.h:879
@ TLS_STATE_SERVER_HELLO_2
Definition: tls.h:1518
@ TLS_STATE_CLIENT_FINISHED
Definition: tls.h:1532
@ ERROR_CERTIFICATE_REQUIRED
Definition: error.h:136
@ ERROR_INVALID_SIGNATURE
Definition: error.h:228
error_t tlsFormatCertificate(TlsContext *context, TlsCertificate *message, size_t *length)
Format Certificate message.
Definition: tls_common.c:601
#define TlsEncryptionEngine
Definition: tls.h:40
RSA/DSA/ECDSA/EdDSA signature generation.
X509OctetString raw
Definition: x509_common.h:724
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t tlsSendCertificateVerify(TlsContext *context)
Send CertificateVerify message.
Definition: tls_common.c:191
void TlsCertificate
Certificate message.
Definition: tls.h:1850
uint8_t description
Definition: tls.h:1928
@ TLS_STATE_CLOSED
Definition: tls.h:1543