tls_certificate.c
Go to the documentation of this file.
1 /**
2  * @file tls_certificate.c
3  * @brief X.509 certificate handling
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 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.2.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include <string.h>
36 #include <ctype.h>
37 #include "tls.h"
38 #include "tls_certificate.h"
39 #include "tls_misc.h"
40 #include "encoding/asn1.h"
41 #include "encoding/oid.h"
42 #include "pkix/pem_import.h"
43 #include "pkix/x509_cert_parse.h"
45 #include "pkix/x509_key_parse.h"
46 #include "debug.h"
47 
48 //Check TLS library configuration
49 #if (TLS_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief Format certificate chain
54  * @param[in] context Pointer to the TLS context
55  * @param[in] p Output stream where to write the certificate chain
56  * @param[out] written Total number of bytes that have been written
57  * @return Error code
58  **/
59 
61  size_t *written)
62 {
63  error_t error;
64  size_t m;
65  size_t n;
66  size_t certChainLen;
67  const char_t *certChain;
68 
69  //Initialize status code
70  error = NO_ERROR;
71 
72  //Length of the certificate list in bytes
73  *written = 0;
74 
75  //Check whether a certificate is available
76  if(context->cert != NULL)
77  {
78  //Point to the certificate chain
79  certChain = context->cert->certChain;
80  //Get the total length, in bytes, of the certificate chain
81  certChainLen = context->cert->certChainLen;
82  }
83  else
84  {
85  //If no suitable certificate is available, the message contains an
86  //empty certificate list
87  certChain = NULL;
88  certChainLen = 0;
89  }
90 
91  //Parse the certificate chain
92  while(certChainLen > 0)
93  {
94  //The first pass calculates the length of the DER-encoded certificate
95  error = pemImportCertificate(certChain, certChainLen, NULL, &n, NULL);
96 
97  //End of file detected?
98  if(error)
99  {
100  //Exit immediately
101  error = NO_ERROR;
102  break;
103  }
104 
105  //Buffer overflow?
106  if((*written + n + 3) > context->txBufferMaxLen)
107  {
108  //Report an error
109  error = ERROR_MESSAGE_TOO_LONG;
110  break;
111  }
112 
113  //Each certificate is preceded by a 3-byte length field
114  STORE24BE(n, p);
115 
116  //The second pass decodes the PEM certificate
117  error = pemImportCertificate(certChain, certChainLen, p + 3, &n, &m);
118  //Any error to report?
119  if(error)
120  break;
121 
122  //Advance read pointer
123  certChain += m;
124  certChainLen -= m;
125 
126  //Advance write pointer
127  p += n + 3;
128  *written += n + 3;
129 
130 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
131  //TLS 1.3 currently selected?
132  if(context->version == TLS_VERSION_1_3)
133  {
134  //Format the list of extensions for the current CertificateEntry
135  error = tls13FormatCertExtensions(p, &n);
136  //Any error to report?
137  if(error)
138  break;
139 
140  //Advance write pointer
141  p += n;
142  *written += n;
143  }
144 #endif
145  }
146 
147  //Return status code
148  return error;
149 }
150 
151 
152 /**
153  * @brief Format raw public key
154  * @param[in] context Pointer to the TLS context
155  * @param[in] p Output stream where to write the raw public key
156  * @param[out] written Total number of bytes that have been written
157  * @return Error code
158  **/
159 
161  size_t *written)
162 {
163  error_t error;
164 
165  //Initialize status code
166  error = NO_ERROR;
167 
168  //Length of the certificate list in bytes
169  *written = 0;
170 
171 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
172  //Check whether a certificate is available
173  if(context->cert != NULL)
174  {
175  size_t n;
176  uint8_t *derCert;
177  size_t derCertLen;
178  X509CertificateInfo *certInfo;
179 
180  //Initialize variables
181  derCert = NULL;
182  certInfo = NULL;
183 
184  //Start of exception handling block
185  do
186  {
187  //The first pass calculates the length of the DER-encoded certificate
188  error = pemImportCertificate(context->cert->certChain,
189  context->cert->certChainLen, NULL, &derCertLen, NULL);
190  //Any error to report?
191  if(error)
192  break;
193 
194  //Allocate a memory buffer to hold the DER-encoded certificate
195  derCert = tlsAllocMem(derCertLen);
196  //Failed to allocate memory?
197  if(derCert == NULL)
198  {
199  error = ERROR_OUT_OF_MEMORY;
200  break;
201  }
202 
203  //The second pass decodes the PEM certificate
204  error = pemImportCertificate(context->cert->certChain,
205  context->cert->certChainLen, derCert, &derCertLen, NULL);
206  //Any error to report?
207  if(error)
208  break;
209 
210  //Allocate a memory buffer to store X.509 certificate info
211  certInfo = tlsAllocMem(sizeof(X509CertificateInfo));
212  //Failed to allocate memory?
213  if(certInfo == NULL)
214  {
215  error = ERROR_OUT_OF_MEMORY;
216  break;
217  }
218 
219  //Parse X.509 certificate
220  error = x509ParseCertificate(derCert, derCertLen, certInfo);
221  //Failed to parse the X.509 certificate?
222  if(error)
223  break;
224 
225  //Retrieve the length of the raw public key
227 
228 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
229  //TLS 1.3 currently selected?
230  if(context->version == TLS_VERSION_1_3)
231  {
232  //The raw public key is preceded by a 3-byte length field
233  STORE24BE(n, p);
234  //Copy the raw public key
235  osMemcpy(p + 3, certInfo->tbsCert.subjectPublicKeyInfo.rawData, n);
236 
237  //Advance data pointer
238  p += n + 3;
239  //Adjust the length of the certificate list
240  *written += n + 3;
241 
242  //Format the list of extensions for the current CertificateEntry
243  error = tls13FormatCertExtensions(p, &n);
244  //Any error to report?
245  if(error)
246  break;
247 
248  //Advance data pointer
249  p += n;
250  //Adjust the length of the certificate list
251  *written += n;
252  }
253  else
254 #endif
255  {
256  //Copy the raw public key
258 
259  //Advance data pointer
260  p += n;
261  //Adjust the length of the certificate list
262  *written += n;
263  }
264 
265  //End of exception handling block
266  } while(0);
267 
268  //Release previously allocated memory
269  tlsFreeMem(derCert);
270  tlsFreeMem(certInfo);
271  }
272 #endif
273 
274  //Return status code
275  return error;
276 }
277 
278 
279 /**
280  * @brief Parse certificate chain
281  * @param[in] context Pointer to the TLS context
282  * @param[in] p Input stream where to read the certificate chain
283  * @param[in] length Number of bytes available in the input stream
284  * @return Error code
285  **/
286 
288  const uint8_t *p, size_t length)
289 {
290  error_t error;
291  error_t certValidResult;
292  uint_t i;
293  size_t n;
294  const char_t *subjectName;
295  X509CertificateInfo *certInfo;
296  X509CertificateInfo *issuerCertInfo;
297 
298  //Initialize X.509 certificates
299  certInfo = NULL;
300  issuerCertInfo = NULL;
301 
302  //Start of exception handling block
303  do
304  {
305  //Allocate a memory buffer to store X.509 certificate info
306  certInfo = tlsAllocMem(sizeof(X509CertificateInfo));
307  //Failed to allocate memory?
308  if(certInfo == NULL)
309  {
310  //Report an error
311  error = ERROR_OUT_OF_MEMORY;
312  break;
313  }
314 
315  //Allocate a memory buffer to store the parent certificate
316  issuerCertInfo = tlsAllocMem(sizeof(X509CertificateInfo));
317  //Failed to allocate memory?
318  if(issuerCertInfo == NULL)
319  {
320  //Report an error
321  error = ERROR_OUT_OF_MEMORY;
322  break;
323  }
324 
325  //The end-user certificate is preceded by a 3-byte length field
326  if(length < 3)
327  {
328  //Report an error
329  error = ERROR_DECODING_FAILED;
330  break;
331  }
332 
333  //Get the size occupied by the certificate
334  n = LOAD24BE(p);
335  //Jump to the beginning of the DER-encoded certificate
336  p += 3;
337  length -= 3;
338 
339  //Malformed Certificate message?
340  if(n == 0 || n > length)
341  {
342  //Report an error
343  error = ERROR_DECODING_FAILED;
344  break;
345  }
346 
347  //Display ASN.1 structure
348  error = asn1DumpObject(p, n, 0);
349  //Any error to report?
350  if(error)
351  break;
352 
353  //Parse end-user certificate
354  error = x509ParseCertificate(p, n, certInfo);
355  //Failed to parse the X.509 certificate?
356  if(error)
357  {
358  //Report an error
359  error = ERROR_BAD_CERTIFICATE;
360  break;
361  }
362 
363  //Check certificate key usage
364  error = tlsCheckKeyUsage(certInfo, context->entity,
365  context->keyExchMethod);
366  //Any error to report?
367  if(error)
368  break;
369 
370  //Extract the public key from the end-user certificate
371  error = tlsReadSubjectPublicKey(context,
372  &certInfo->tbsCert.subjectPublicKeyInfo);
373  //Any error to report?
374  if(error)
375  break;
376 
377 #if (TLS_CLIENT_SUPPORT == ENABLED)
378  //Client mode?
379  if(context->entity == TLS_CONNECTION_END_CLIENT)
380  {
381  TlsCertificateType certType;
382  TlsNamedGroup namedCurve;
383 
384  //Retrieve the type of the X.509 certificate
385  error = tlsGetCertificateType(certInfo, &certType, &namedCurve);
386  //Unsupported certificate?
387  if(error)
388  break;
389 
390  //Version of TLS prior to TLS 1.3?
391  if(context->version <= TLS_VERSION_1_2)
392  {
393  //ECDSA certificate?
394  if(certType == TLS_CERT_ECDSA_SIGN)
395  {
396  //Make sure the elliptic curve is supported
397  if(tlsGetCurveInfo(context, namedCurve) == NULL)
398  {
399  error = ERROR_BAD_CERTIFICATE;
400  break;
401  }
402  }
403  }
404 
405  //Point to the subject name
406  subjectName = context->serverName;
407 
408  //Check the subject name in the server certificate against the actual
409  //FQDN name that is being requested
410  error = x509CheckSubjectName(certInfo, subjectName);
411  //Any error to report?
412  if(error)
413  {
414  //Debug message
415  TRACE_WARNING("Server name mismatch!\r\n");
416 
417  //Report an error
418  error = ERROR_BAD_CERTIFICATE;
419  break;
420  }
421  }
422  else
423 #endif
424  //Server mode?
425  {
426  //Do not check name constraints
427  subjectName = NULL;
428  }
429 
430  //Check if the end-user certificate can be matched with a trusted CA
431  certValidResult = tlsValidateCertificate(context, certInfo, 0,
432  subjectName);
433 
434  //Check validation result
435  if(certValidResult != NO_ERROR && certValidResult != ERROR_UNKNOWN_CA)
436  {
437  //The certificate is not valid
438  error = certValidResult;
439  break;
440  }
441 
442  //Next certificate
443  p += n;
444  length -= n;
445 
446 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
447  //TLS 1.3 currently selected?
448  if(context->version == TLS_VERSION_1_3)
449  {
450  //Parse the list of extensions for the current CertificateEntry
451  error = tls13ParseCertExtensions(p, length, &n);
452  //Any error to report?
453  if(error)
454  break;
455 
456  //Point to the next field
457  p += n;
458  //Remaining bytes to process
459  length -= n;
460  }
461 #endif
462 
463  //PKIX path validation
464  for(i = 0; length > 0; i++)
465  {
466  //Each intermediate certificate is preceded by a 3-byte length field
467  if(length < 3)
468  {
469  //Report an error
470  error = ERROR_DECODING_FAILED;
471  break;
472  }
473 
474  //Get the size occupied by the certificate
475  n = LOAD24BE(p);
476  //Jump to the beginning of the DER-encoded certificate
477  p += 3;
478  //Remaining bytes to process
479  length -= 3;
480 
481  //Malformed Certificate message?
482  if(n == 0 || n > length)
483  {
484  //Report an error
485  error = ERROR_DECODING_FAILED;
486  break;
487  }
488 
489  //Display ASN.1 structure
490  error = asn1DumpObject(p, n, 0);
491  //Any error to report?
492  if(error)
493  break;
494 
495  //Parse intermediate certificate
496  error = x509ParseCertificate(p, n, issuerCertInfo);
497  //Failed to parse the X.509 certificate?
498  if(error)
499  {
500  //Report an error
501  error = ERROR_BAD_CERTIFICATE;
502  break;
503  }
504 
505  //Certificate chain validation in progress?
506  if(certValidResult == ERROR_UNKNOWN_CA)
507  {
508  //Validate current certificate
509  error = x509ValidateCertificate(certInfo, issuerCertInfo, i);
510  //Certificate validation failed?
511  if(error)
512  break;
513 
514  //Check name constraints
515  error = x509CheckNameConstraints(subjectName, issuerCertInfo);
516  //Should the application reject the certificate?
517  if(error)
518  return ERROR_BAD_CERTIFICATE;
519 
520  //Check the version of the certificate
521  if(issuerCertInfo->tbsCert.version < X509_VERSION_3)
522  {
523  //Conforming implementations may choose to reject all version 1
524  //and version 2 intermediate certificates (refer to RFC 5280,
525  //section 6.1.4)
526  error = ERROR_BAD_CERTIFICATE;
527  break;
528  }
529 
530  //Check if the intermediate certificate can be matched with a
531  //trusted CA
532  certValidResult = tlsValidateCertificate(context, issuerCertInfo,
533  i, subjectName);
534 
535  //Check validation result
536  if(certValidResult != NO_ERROR && certValidResult != ERROR_UNKNOWN_CA)
537  {
538  //The certificate is not valid
539  error = certValidResult;
540  break;
541  }
542  }
543 
544  //Keep track of the issuer certificate
545  *certInfo = *issuerCertInfo;
546 
547  //Next certificate
548  p += n;
549  length -= n;
550 
551 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
552  //TLS 1.3 currently selected?
553  if(context->version == TLS_VERSION_1_3)
554  {
555  //Parse the list of extensions for the current CertificateEntry
556  error = tls13ParseCertExtensions(p, length, &n);
557  //Any error to report?
558  if(error)
559  break;
560 
561  //Point to the next field
562  p += n;
563  //Remaining bytes to process
564  length -= n;
565  }
566 #endif
567  }
568 
569  //Certificate chain validation failed?
570  if(error == NO_ERROR && certValidResult != NO_ERROR)
571  {
572  //A valid certificate chain or partial chain was received, but the
573  //certificate was not accepted because the CA certificate could not
574  //be matched with a known, trusted CA
575  error = ERROR_UNKNOWN_CA;
576  }
577 
578  //End of exception handling block
579  } while(0);
580 
581  //Free previously allocated memory
582  tlsFreeMem(certInfo);
583  tlsFreeMem(issuerCertInfo);
584 
585  //Return status code
586  return error;
587 }
588 
589 
590 /**
591  * @brief Parse raw public key
592  * @param[in] context Pointer to the TLS context
593  * @param[in] p Input stream where to read the raw public key
594  * @param[in] length Number of bytes available in the input stream
595  * @return Error code
596  **/
597 
598 error_t tlsParseRawPublicKey(TlsContext *context, const uint8_t *p,
599  size_t length)
600 {
601  error_t error;
602 
603 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
604  //Any registered callback?
605  if(context->rpkVerifyCallback != NULL)
606  {
607  size_t n;
608  size_t rawPublicKeyLen;
609  const uint8_t *rawPublicKey;
610  X509SubjectPublicKeyInfo subjectPublicKeyInfo;
611 
612 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
613  //TLS 1.3 currently selected?
614  if(context->version == TLS_VERSION_1_3)
615  {
616  //The raw public key is preceded by a 3-byte length field
617  if(length < 3)
618  return ERROR_DECODING_FAILED;
619 
620  //Get the size occupied by the raw public key
621  rawPublicKeyLen = LOAD24BE(p);
622  //Advance data pointer
623  p += 3;
624  //Remaining bytes to process
625  length -= 3;
626 
627  //Malformed Certificate message?
628  if(length < rawPublicKeyLen)
629  return ERROR_DECODING_FAILED;
630  }
631  else
632 #endif
633  {
634  //The payload of the Certificate message contains a SubjectPublicKeyInfo
635  //structure
636  rawPublicKeyLen = length;
637  }
638 
639  //Point to the raw public key
640  rawPublicKey = p;
641 
642  //Decode the SubjectPublicKeyInfo structure
643  error = x509ParseSubjectPublicKeyInfo(rawPublicKey, rawPublicKeyLen, &n,
644  &subjectPublicKeyInfo);
645  //Any error to report?
646  if(error)
647  return error;
648 
649  //Advance data pointer
650  p += rawPublicKeyLen;
651  //Remaining bytes to process
652  length -= rawPublicKeyLen;
653 
654 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
655  //TLS 1.3 currently selected?
656  if(context->version == TLS_VERSION_1_3)
657  {
658  //Parse the list of extensions for the current CertificateEntry
659  error = tls13ParseCertExtensions(p, length, &n);
660  //Any error to report?
661  if(error)
662  return error;
663 
664  //Advance data pointer
665  p += n;
666  //Remaining bytes to process
667  length -= n;
668  }
669 #endif
670 
671  //If the RawPublicKey certificate type was negotiated, the certificate
672  //list must contain no more than one CertificateEntry (refer to RFC 8446,
673  //section 4.4.2)
674  if(length != 0)
675  return ERROR_DECODING_FAILED;
676 
677  //Extract the public key from the SubjectPublicKeyInfo structure
678  error = tlsReadSubjectPublicKey(context, &subjectPublicKeyInfo);
679  //Any error to report?
680  if(error)
681  return error;
682 
683  //When raw public keys are used, authentication of the peer is supported
684  //only through authentication of the received SubjectPublicKeyInfo via an
685  //out-of-band method
686  error = context->rpkVerifyCallback(context, rawPublicKey,
687  rawPublicKeyLen);
688  }
689  else
690 #endif
691  {
692  //Report an error
693  error = ERROR_BAD_CERTIFICATE;
694  }
695 
696  //Return status code
697  return error;
698 }
699 
700 
701 /**
702  * @brief Check whether a certificate is acceptable
703  * @param[in] context Pointer to the TLS context
704  * @param[in] cert End entity certificate
705  * @param[in] certTypes List of supported certificate types
706  * @param[in] numCertTypes Size of the list that contains the supported
707  * certificate types
708  * @param[in] signHashAlgos List of signature algorithms that may be used in
709  * digital signatures
710  * @param[in] certSignHashAlgos List of signature algorithms that may be used
711  * in X.509 certificates
712  * @param[in] curveList List of supported elliptic curves
713  * @param[in] certAuthorities List of trusted CA
714  * @return TRUE if the specified certificate conforms to the requirements,
715  * else FALSE
716  **/
717 
719  const uint8_t *certTypes, size_t numCertTypes, const TlsSignHashAlgos *signHashAlgos,
720  const TlsSignHashAlgos *certSignHashAlgos, const TlsSupportedGroupList *curveList,
721  const TlsCertAuthorities *certAuthorities)
722 {
723  size_t i;
724  size_t n;
725  size_t length;
726  bool_t acceptable;
727 
728  //Make sure that a valid certificate has been loaded
729  if(cert->certChain == NULL || cert->certChainLen == 0)
730  return FALSE;
731 
732 #if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
733  //RSA certificate?
734  if(cert->type == TLS_CERT_RSA_SIGN)
735  {
736  //This flag tells whether the certificate is acceptable
737  acceptable = TRUE;
738 
739  //Version of TLS prior to TLS 1.2?
740  if(context->version <= TLS_VERSION_1_1)
741  {
742  //the signing algorithm for the certificate must be the same as the
743  //algorithm for the certificate key
744  if(cert->signAlgo != TLS_SIGN_ALGO_RSA)
745  acceptable = FALSE;
746  }
747 
748  //Filter out certificates with unsupported type
749  if(acceptable && certTypes != NULL)
750  {
751  //Loop through the list of supported certificate types
752  for(i = 0, acceptable = FALSE; i < numCertTypes && !acceptable; i++)
753  {
754  //Check whether the certificate type is acceptable
755  if(certTypes[i] == TLS_CERT_RSA_SIGN)
756  {
757  acceptable = TRUE;
758  }
759  }
760  }
761 
762  //Filter out certificates that are not compatible with the supported
763  //signature schemes
764  if(acceptable && signHashAlgos != NULL)
765  {
766  //Retrieve the number of items in the list
767  n = ntohs(signHashAlgos->length) / sizeof(TlsSignHashAlgo);
768 
769  //Loop through the list of supported hash/signature algorithm pairs
770  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
771  {
772  uint8_t signAlgo;
773  uint8_t hashAlgo;
774 
775  //Retrieve signature and hash algorithms
776  signAlgo = signHashAlgos->value[i].signature;
777  hashAlgo = signHashAlgos->value[i].hash;
778 
779 #if (TLS_RSA_SIGN_SUPPORT == ENABLED)
780  //RSASSA-PKCS1-v1_5 signature scheme?
781  if(signAlgo == TLS_SIGN_ALGO_RSA &&
782  hashAlgo != TLS_HASH_ALGO_NONE &&
783  context->version <= TLS_VERSION_1_2)
784  {
785  acceptable = TRUE;
786  }
787  else
788 #endif
789 #if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA256_SUPPORT == ENABLED)
790  //RSASSA-PSS RSAE signature scheme with SHA-256?
791  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 &&
792  hashAlgo == TLS_HASH_ALGO_INTRINSIC &&
793  context->version >= TLS_VERSION_1_2)
794  {
795  acceptable = TRUE;
796  }
797  else
798 #endif
799 #if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA384_SUPPORT == ENABLED)
800  //RSASSA-PSS RSAE signature scheme with SHA-384?
801  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 &&
802  hashAlgo == TLS_HASH_ALGO_INTRINSIC &&
803  context->version >= TLS_VERSION_1_2)
804  {
805  acceptable = TRUE;
806  }
807  else
808 #endif
809 #if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA512_SUPPORT == ENABLED)
810  //RSASSA-PSS RSAE signature scheme with SHA-512?
811  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512 &&
812  hashAlgo == TLS_HASH_ALGO_INTRINSIC &&
813  context->version >= TLS_VERSION_1_2)
814  {
815  acceptable = TRUE;
816  }
817  else
818 #endif
819  //Unknown signature scheme?
820  {
821  acceptable = FALSE;
822  }
823  }
824  }
825  }
826  else
827 #endif
828 #if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
829  //RSA-PSS certificate?
830  if(cert->type == TLS_CERT_RSA_PSS_SIGN)
831  {
832  //TLS 1.2 and TLS 1.3 support RSASSA-PSS signature schemes
833  if(context->version >= TLS_VERSION_1_2)
834  {
835  //Filter out certificates that are not compatible with the supported
836  //signature schemes
837  if(signHashAlgos != NULL)
838  {
839  //Retrieve the number of items in the list
840  n = ntohs(signHashAlgos->length) / sizeof(TlsSignHashAlgo);
841 
842  //Loop through the list of supported hash/signature algorithm pairs
843  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
844  {
845  uint8_t signAlgo;
846  uint8_t hashAlgo;
847 
848  //Retrieve signature and hash algorithms
849  signAlgo = signHashAlgos->value[i].signature;
850  hashAlgo = signHashAlgos->value[i].hash;
851 
852 #if (TLS_SHA256_SUPPORT == ENABLED)
853  //RSASSA-PSS PSS signature scheme with SHA-256?
854  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256 &&
855  hashAlgo == TLS_HASH_ALGO_INTRINSIC)
856  {
857  acceptable = TRUE;
858  }
859  else
860 #endif
861 #if (TLS_SHA384_SUPPORT == ENABLED)
862  //RSASSA-PSS PSS signature scheme with SHA-384?
863  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384 &&
864  hashAlgo == TLS_HASH_ALGO_INTRINSIC)
865  {
866  acceptable = TRUE;
867  }
868  else
869 #endif
870 #if (TLS_SHA512_SUPPORT == ENABLED)
871  //RSASSA-PSS PSS signature scheme with SHA-512?
872  if(signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512 &&
873  hashAlgo == TLS_HASH_ALGO_INTRINSIC)
874  {
875  acceptable = TRUE;
876  }
877  else
878 #endif
879  //Unknown signature scheme?
880  {
881  acceptable = FALSE;
882  }
883  }
884  }
885  else
886  {
887  //The SignatureAlgorithms extension must be specified (refer to
888  //RFC 8446, section 4.3.2)
889  acceptable = FALSE;
890  }
891  }
892  else
893  {
894  //RSA-PSS is not supported by TLS 1.2 and earlier
895  acceptable = FALSE;
896  }
897  }
898  else
899 #endif
900 #if (TLS_DSA_SIGN_SUPPORT == ENABLED)
901  //DSA certificate?
902  if(cert->type == TLS_CERT_DSS_SIGN)
903  {
904  //Version of TLS prior to TLS 1.3?
905  if(context->version <= TLS_VERSION_1_2)
906  {
907  //This flag tells whether the certificate is acceptable
908  acceptable = TRUE;
909 
910  //Version of TLS prior to TLS 1.2?
911  if(context->version <= TLS_VERSION_1_1)
912  {
913  //the signing algorithm for the certificate must be the same as the
914  //algorithm for the certificate key
915  if(cert->signAlgo != TLS_SIGN_ALGO_DSA)
916  acceptable = FALSE;
917  }
918 
919  //Filter out certificates with unsupported type
920  if(acceptable && certTypes != NULL)
921  {
922  //Loop through the list of supported certificate types
923  for(i = 0, acceptable = FALSE; i < numCertTypes && !acceptable; i++)
924  {
925  //Check whether the certificate type is acceptable
926  if(certTypes[i] == TLS_CERT_DSS_SIGN)
927  {
928  acceptable = TRUE;
929  }
930  }
931  }
932 
933  //Filter out certificates that are not compatible with the supported
934  //signature schemes
935  if(acceptable && signHashAlgos != NULL)
936  {
937  //Retrieve the number of items in the list
938  n = ntohs(signHashAlgos->length) / sizeof(TlsSignHashAlgo);
939 
940  //Loop through the list of supported hash/signature algorithm pairs
941  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
942  {
943  //Check whether DSA signature scheme is supported
944  if(signHashAlgos->value[i].signature == TLS_SIGN_ALGO_DSA)
945  {
946  acceptable = TRUE;
947  }
948  }
949  }
950  }
951  else
952  {
953  //TLS 1.3 removes support for DSA certificates
954  acceptable = FALSE;
955  }
956  }
957  else
958 #endif
959 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
960  //ECDSA certificate?
961  if(cert->type == TLS_CERT_ECDSA_SIGN)
962  {
963  //This flag tells whether the certificate is acceptable
964  acceptable = TRUE;
965 
966  //Version of TLS prior to TLS 1.2?
967  if(context->version <= TLS_VERSION_1_1)
968  {
969  //the signing algorithm for the certificate must be the same as the
970  //algorithm for the certificate key
971  if(cert->signAlgo != TLS_SIGN_ALGO_ECDSA)
972  {
973  acceptable = FALSE;
974  }
975  }
976 
977  //Filter out certificates with unsupported type
978  if(acceptable && certTypes != NULL)
979  {
980  //Loop through the list of supported certificate types
981  for(i = 0, acceptable = FALSE; i < numCertTypes && !acceptable; i++)
982  {
983  //Check whether the certificate type is acceptable
984  if(certTypes[i] == TLS_CERT_ECDSA_SIGN)
985  {
986  acceptable = TRUE;
987  }
988  }
989  }
990 
991  //Version of TLS prior to TLS 1.3?
992  if(context->version <= TLS_VERSION_1_2)
993  {
994  //In versions of TLS prior to TLS 1.3, the EllipticCurves extension is
995  //used to negotiate ECDSA curves (refer to RFC 8446, section 4.2.7)
996  if(acceptable && curveList != NULL)
997  {
998  //Retrieve the number of items in the list
999  n = ntohs(curveList->length) / sizeof(uint16_t);
1000 
1001  //Loop through the list of supported elliptic curves
1002  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
1003  {
1004  //Check whether the elliptic curve is supported
1005  if(ntohs(curveList->value[i]) == cert->namedCurve)
1006  {
1007  acceptable = TRUE;
1008  }
1009  }
1010  }
1011  }
1012 
1013  //Filter out certificates that are not compatible with the supported
1014  //signature schemes
1015  if(acceptable && signHashAlgos != NULL)
1016  {
1017  //Retrieve the number of items in the list
1018  n = ntohs(signHashAlgos->length) / sizeof(TlsSignHashAlgo);
1019 
1020  //Loop through the list of supported hash/signature algorithm pairs
1021  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
1022  {
1023  //Check whether ECDSA signature scheme is supported
1024  if(signHashAlgos->value[i].signature == TLS_SIGN_ALGO_ECDSA)
1025  {
1026  acceptable = TRUE;
1027  }
1028  }
1029  }
1030  }
1031  else
1032 #endif
1033 #if (TLS_EDDSA_SIGN_SUPPORT == ENABLED)
1034  //EdDSA certificate?
1035  if(cert->type == TLS_CERT_ED25519_SIGN ||
1036  cert->type == TLS_CERT_ED448_SIGN)
1037  {
1038  //TLS 1.2 and TLS 1.3 support EdDSA signature schemes
1039  if((context->version >= TLS_VERSION_1_2 &&
1040  context->entity == TLS_CONNECTION_END_SERVER) ||
1041  (context->version >= TLS_VERSION_1_3 &&
1042  context->entity == TLS_CONNECTION_END_CLIENT))
1043  {
1044  //This flag tells whether the certificate is acceptable
1045  acceptable = TRUE;
1046 
1047  //Filter out certificates with unsupported type
1048  if(certTypes != NULL)
1049  {
1050  //Loop through the list of supported certificate types
1051  for(i = 0, acceptable = FALSE; i < numCertTypes && !acceptable; i++)
1052  {
1053  //Check whether the certificate type is acceptable
1054  if(certTypes[i] == TLS_CERT_ECDSA_SIGN)
1055  {
1056  acceptable = TRUE;
1057  }
1058  }
1059  }
1060 
1061  //Filter out certificates that are not compatible with the supported
1062  //signature schemes
1063  if(acceptable && signHashAlgos != NULL)
1064  {
1065  //Retrieve the number of items in the list
1066  n = ntohs(signHashAlgos->length) / sizeof(TlsSignHashAlgo);
1067 
1068  //Loop through the list of supported hash/signature algorithm pairs
1069  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
1070  {
1071 #if (TLS_ED25519_SUPPORT == ENABLED)
1072  //Ed25519 certificate?
1073  if(cert->type == TLS_CERT_ED25519_SIGN)
1074  {
1075  //Check whether Ed25519 signature scheme is supported
1076  if(signHashAlgos->value[i].signature == TLS_SIGN_ALGO_ED25519 &&
1077  signHashAlgos->value[i].hash == TLS_HASH_ALGO_INTRINSIC)
1078  {
1079  acceptable = TRUE;
1080  }
1081  }
1082  else
1083 #endif
1084 #if (TLS_ED448_SUPPORT == ENABLED)
1085  //Ed448 certificate?
1086  if(cert->type == TLS_CERT_ED448_SIGN)
1087  {
1088  //Check whether Ed448 signature scheme is supported
1089  if(signHashAlgos->value[i].signature == TLS_SIGN_ALGO_ED448 &&
1090  signHashAlgos->value[i].hash == TLS_HASH_ALGO_INTRINSIC)
1091  {
1092  acceptable = TRUE;
1093  }
1094  }
1095  else
1096 #endif
1097  //Unknown certificate type?
1098  {
1099  acceptable = FALSE;
1100  }
1101  }
1102  }
1103  else
1104  {
1105  //The certificate is not acceptable
1106  acceptable = FALSE;
1107  }
1108  }
1109  else
1110  {
1111  //EdDSA is not supported by TLS 1.1 and earlier
1112  acceptable = FALSE;
1113  }
1114  }
1115  else
1116 #endif
1117  //Unsupported certificate type?
1118  {
1119  //The certificate is not acceptable
1120  acceptable = FALSE;
1121  }
1122 
1123  //Filter out certificates that are signed with an unsupported algorithm
1124  if(acceptable && certSignHashAlgos != NULL)
1125  {
1126  //Retrieve the number of items in the list
1127  n = ntohs(certSignHashAlgos->length) / sizeof(TlsSignHashAlgo);
1128 
1129  //Loop through the list of supported hash/signature algorithm pairs
1130  for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
1131  {
1132  //The certificate must be signed using a valid hash algorithm
1133  if(certSignHashAlgos->value[i].signature == cert->signAlgo &&
1134  certSignHashAlgos->value[i].hash == cert->hashAlgo)
1135  {
1136  acceptable = TRUE;
1137  }
1138  }
1139  }
1140 
1141  //Filter out certificates that are issued by a non trusted CA
1142  if(acceptable && certAuthorities != NULL)
1143  {
1144  //Retrieve the length of the list
1145  length = ntohs(certAuthorities->length);
1146 
1147  //If the certificate authorities list is empty, then the client
1148  //may send any certificate of the appropriate type
1149  if(length > 0)
1150  {
1151  error_t error;
1152  size_t pemCertLen;
1153  const char_t *certChain;
1154  size_t certChainLen;
1155  uint8_t *derCert;
1156  size_t derCertLen;
1157  X509CertificateInfo *certInfo;
1158 
1159  //The list of acceptable certificate authorities describes the
1160  //known roots CA
1161  acceptable = FALSE;
1162 
1163  //Point to the end entity certificate
1164  certChain = cert->certChain;
1165  //Get the total length, in bytes, of the certificate chain
1166  certChainLen = cert->certChainLen;
1167 
1168  //Allocate a memory buffer to store X.509 certificate info
1169  certInfo = tlsAllocMem(sizeof(X509CertificateInfo));
1170 
1171  //Successful memory allocation?
1172  if(certInfo != NULL)
1173  {
1174  //Parse the certificate chain
1175  while(certChainLen > 0 && !acceptable)
1176  {
1177  //The first pass calculates the length of the DER-encoded
1178  //certificate
1179  error = pemImportCertificate(certChain, certChainLen, NULL,
1180  &derCertLen, &pemCertLen);
1181 
1182  //Check status code
1183  if(!error)
1184  {
1185  //Allocate a memory buffer to hold the DER-encoded certificate
1186  derCert = tlsAllocMem(derCertLen);
1187 
1188  //Successful memory allocation?
1189  if(derCert != NULL)
1190  {
1191  //The second pass decodes the PEM certificate
1192  error = pemImportCertificate(certChain, certChainLen,
1193  derCert, &derCertLen, NULL);
1194 
1195  //Check status code
1196  if(!error)
1197  {
1198  //Parse X.509 certificate
1199  error = x509ParseCertificate(derCert, derCertLen,
1200  certInfo);
1201  }
1202 
1203  //Check status code
1204  if(!error)
1205  {
1206  //Parse each distinguished name of the list
1207  for(i = 0; i < length; i += n + 2)
1208  {
1209  //Sanity check
1210  if((i + 2) > length)
1211  break;
1212 
1213  //Each distinguished name is preceded by a 2-byte
1214  //length field
1215  n = LOAD16BE(certAuthorities->value + i);
1216 
1217  //Make sure the length field is valid
1218  if((i + n + 2) > length)
1219  break;
1220 
1221  //Check if the distinguished name matches the root CA
1222  if(x509CompareName(certAuthorities->value + i + 2, n,
1223  certInfo->tbsCert.issuer.rawData,
1224  certInfo->tbsCert.issuer.rawDataLen))
1225  {
1226  acceptable = TRUE;
1227  break;
1228  }
1229  }
1230  }
1231 
1232  //Free previously allocated memory
1233  tlsFreeMem(derCert);
1234  }
1235 
1236  //Advance read pointer
1237  certChain += pemCertLen;
1238  certChainLen -= pemCertLen;
1239  }
1240  else
1241  {
1242  //No more CA certificates in the list
1243  break;
1244  }
1245  }
1246 
1247  //Free previously allocated memory
1248  tlsFreeMem(certInfo);
1249  }
1250  }
1251  }
1252 
1253  //The return value specifies whether all the criteria were matched
1254  return acceptable;
1255 }
1256 
1257 
1258 /**
1259  * @brief Verify certificate against root CAs
1260  * @param[in] context Pointer to the TLS context
1261  * @param[in] certInfo X.509 certificate to be verified
1262  * @param[in] pathLen Certificate path length
1263  * @param[in] subjectName Subject name (optional parameter)
1264  * @return Error code
1265  **/
1266 
1268  const X509CertificateInfo *certInfo, uint_t pathLen,
1269  const char_t *subjectName)
1270 {
1271  error_t error;
1272  size_t pemCertLen;
1273  const char_t *trustedCaList;
1274  size_t trustedCaListLen;
1275  uint8_t *derCert;
1276  size_t derCertLen;
1277  X509CertificateInfo *caCertInfo;
1278 
1279  //Initialize status code
1280  error = ERROR_UNKNOWN_CA;
1281 
1282  //Any registered callback?
1283  if(context->certVerifyCallback != NULL)
1284  {
1285  //Invoke user callback function
1286  error = context->certVerifyCallback(context, certInfo, pathLen,
1287  context->certVerifyParam);
1288  }
1289 
1290  //Check status code
1291  if(error == NO_ERROR)
1292  {
1293  //The certificate is valid
1294  }
1295  else if(error == ERROR_UNKNOWN_CA)
1296  {
1297  //Check whether the certificate should be checked against root CAs
1298  if(context->trustedCaListLen > 0)
1299  {
1300  //Point to the first trusted CA certificate
1301  trustedCaList = context->trustedCaList;
1302  //Get the total length, in bytes, of the trusted CA list
1303  trustedCaListLen = context->trustedCaListLen;
1304 
1305  //Allocate a memory buffer to store X.509 certificate info
1306  caCertInfo = tlsAllocMem(sizeof(X509CertificateInfo));
1307 
1308  //Successful memory allocation?
1309  if(caCertInfo != NULL)
1310  {
1311  //Loop through the list of trusted CA certificates
1312  while(trustedCaListLen > 0 && error == ERROR_UNKNOWN_CA)
1313  {
1314  //The first pass calculates the length of the DER-encoded
1315  //certificate
1316  error = pemImportCertificate(trustedCaList, trustedCaListLen,
1317  NULL, &derCertLen, &pemCertLen);
1318 
1319  //Check status code
1320  if(!error)
1321  {
1322  //Allocate a memory buffer to hold the DER-encoded certificate
1323  derCert = tlsAllocMem(derCertLen);
1324 
1325  //Successful memory allocation?
1326  if(derCert != NULL)
1327  {
1328  //The second pass decodes the PEM certificate
1329  error = pemImportCertificate(trustedCaList,
1330  trustedCaListLen, derCert, &derCertLen, NULL);
1331 
1332  //Check status code
1333  if(!error)
1334  {
1335  //Parse X.509 certificate
1336  error = x509ParseCertificate(derCert, derCertLen,
1337  caCertInfo);
1338  }
1339 
1340  //Check status code
1341  if(!error)
1342  {
1343  //Validate the certificate with the current CA
1344  error = x509ValidateCertificate(certInfo, caCertInfo,
1345  pathLen);
1346  }
1347 
1348  //Check status code
1349  if(!error)
1350  {
1351  //Check name constraints
1352  error = x509CheckNameConstraints(subjectName, caCertInfo);
1353  }
1354 
1355  //Check status code
1356  if(!error)
1357  {
1358  //The certificate is issued by a trusted CA
1359  error = NO_ERROR;
1360  }
1361  else
1362  {
1363  //The certificate cannot be matched with the current CA
1364  error = ERROR_UNKNOWN_CA;
1365  }
1366 
1367  //Free previously allocated memory
1368  tlsFreeMem(derCert);
1369  }
1370  else
1371  {
1372  //Failed to allocate memory
1373  error = ERROR_OUT_OF_MEMORY;
1374  }
1375 
1376  //Advance read pointer
1377  trustedCaList += pemCertLen;
1378  trustedCaListLen -= pemCertLen;
1379  }
1380  else
1381  {
1382  //No more CA certificates in the list
1383  trustedCaListLen = 0;
1384  error = ERROR_UNKNOWN_CA;
1385  }
1386  }
1387 
1388  //Free previously allocated memory
1389  tlsFreeMem(caCertInfo);
1390  }
1391  else
1392  {
1393  //Failed to allocate memory
1394  error = ERROR_OUT_OF_MEMORY;
1395  }
1396  }
1397  else
1398  {
1399  //Do not check the certificate against root CAs
1400  error = NO_ERROR;
1401  }
1402  }
1403  else if(error == ERROR_BAD_CERTIFICATE ||
1404  error == ERROR_UNSUPPORTED_CERTIFICATE ||
1405  error == ERROR_UNKNOWN_CERTIFICATE ||
1406  error == ERROR_CERTIFICATE_REVOKED ||
1407  error == ERROR_CERTIFICATE_EXPIRED ||
1408  error == ERROR_HANDSHAKE_FAILED)
1409  {
1410  //The certificate is not valid
1411  }
1412  else
1413  {
1414  //Report an error
1415  error = ERROR_BAD_CERTIFICATE;
1416  }
1417 
1418  //Return status code
1419  return error;
1420 }
1421 
1422 
1423 /**
1424  * @brief Retrieve the certificate type
1425  * @param[in] certInfo X.509 certificate
1426  * @param[out] certType Certificate type
1427  * @param[out] namedCurve Elliptic curve (only for ECDSA certificates)
1428  * @return Error code
1429  **/
1430 
1432  TlsCertificateType *certType, TlsNamedGroup *namedCurve)
1433 {
1434  size_t oidLen;
1435  const uint8_t *oid;
1436 
1437  //Check parameters
1438  if(certInfo == NULL || certType == NULL || namedCurve == NULL)
1439  return ERROR_INVALID_PARAMETER;
1440 
1441  //Point to the public key identifier
1442  oid = certInfo->tbsCert.subjectPublicKeyInfo.oid;
1444 
1445 #if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
1446  //RSA public key?
1448  {
1449  //Save certificate type
1450  *certType = TLS_CERT_RSA_SIGN;
1451  //No named curve applicable
1452  *namedCurve = TLS_GROUP_NONE;
1453  }
1454  else
1455 #endif
1456 #if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
1457  //RSA-PSS public key?
1459  {
1460  //Save certificate type
1461  *certType = TLS_CERT_RSA_PSS_SIGN;
1462  //No named curve applicable
1463  *namedCurve = TLS_GROUP_NONE;
1464  }
1465  else
1466 #endif
1467 #if (TLS_DSA_SIGN_SUPPORT == ENABLED)
1468  //DSA public key?
1469  if(!oidComp(oid, oidLen, DSA_OID, sizeof(DSA_OID)))
1470  {
1471  //Save certificate type
1472  *certType = TLS_CERT_DSS_SIGN;
1473  //No named curve applicable
1474  *namedCurve = TLS_GROUP_NONE;
1475  }
1476  else
1477 #endif
1478 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
1479  //EC public key?
1481  {
1482  const X509EcParameters *params;
1483 
1484  //Point to the EC parameters
1485  params = &certInfo->tbsCert.subjectPublicKeyInfo.ecParams;
1486 
1487  //Save certificate type
1488  *certType = TLS_CERT_ECDSA_SIGN;
1489  //Retrieve the named curve that has been used to generate the EC public key
1490  *namedCurve = tlsGetNamedCurve(params->namedCurve, params->namedCurveLen);
1491  }
1492  else
1493 #endif
1494 #if (TLS_EDDSA_SIGN_SUPPORT == ENABLED)
1495  //Ed25519 public key?
1496  if(!oidComp(oid, oidLen, ED25519_OID, sizeof(ED25519_OID)))
1497  {
1498  //Save certificate type
1499  *certType = TLS_CERT_ED25519_SIGN;
1500  //No named curve applicable
1501  *namedCurve = TLS_GROUP_NONE;
1502  }
1503  else
1504  //Ed448 public key?
1505  if(!oidComp(oid, oidLen, ED448_OID, sizeof(ED448_OID)))
1506  {
1507  //Save certificate type
1508  *certType = TLS_CERT_ED448_SIGN;
1509  //No named curve applicable
1510  *namedCurve = TLS_GROUP_NONE;
1511  }
1512  else
1513 #endif
1514  //Invalid public key?
1515  {
1516  //The certificate does not contain any valid public key
1517  return ERROR_BAD_CERTIFICATE;
1518  }
1519 
1520  //Successful processing
1521  return NO_ERROR;
1522 }
1523 
1524 
1525 /**
1526  * @brief Retrieve the signature algorithm used to sign the certificate
1527  * @param[in] certInfo X.509 certificate
1528  * @param[out] signAlgo Signature algorithm
1529  * @param[out] hashAlgo Hash algorithm
1530  * @return Error code
1531  **/
1532 
1534  TlsSignatureAlgo *signAlgo, TlsHashAlgo *hashAlgo)
1535 {
1536  size_t oidLen;
1537  const uint8_t *oid;
1538 
1539  //Check parameters
1540  if(certInfo == NULL || signAlgo == NULL || hashAlgo == NULL)
1541  return ERROR_INVALID_PARAMETER;
1542 
1543  //Point to the signature algorithm
1544  oid = certInfo->signatureAlgo.oid;
1545  oidLen = certInfo->signatureAlgo.oidLen;
1546 
1547 #if (RSA_SUPPORT == ENABLED)
1548  //RSA signature algorithm?
1550  sizeof(MD5_WITH_RSA_ENCRYPTION_OID)))
1551  {
1552  //MD5 with RSA signature algorithm
1553  *signAlgo = TLS_SIGN_ALGO_RSA;
1554  *hashAlgo = TLS_HASH_ALGO_MD5;
1555  }
1558  {
1559  //SHA-1 with RSA signature algorithm
1560  *signAlgo = TLS_SIGN_ALGO_RSA;
1561  *hashAlgo = TLS_HASH_ALGO_SHA1;
1562  }
1565  {
1566  //SHA-256 with RSA signature algorithm
1567  *signAlgo = TLS_SIGN_ALGO_RSA;
1568  *hashAlgo = TLS_HASH_ALGO_SHA256;
1569  }
1572  {
1573  //SHA-384 with RSA signature algorithm
1574  *signAlgo = TLS_SIGN_ALGO_RSA;
1575  *hashAlgo = TLS_HASH_ALGO_SHA384;
1576  }
1579  {
1580  //SHA-512 with RSA signature algorithm
1581  *signAlgo = TLS_SIGN_ALGO_RSA;
1582  *hashAlgo = TLS_HASH_ALGO_SHA512;
1583  }
1584  else
1585 #endif
1586 #if (RSA_PSS_SUPPORT == ENABLED)
1587  //RSA-PSS signature algorithm?
1589  {
1590  size_t hashAlgoLen;
1591  const uint8_t *hashAlgo;
1592 
1593  //Get the OID of the hash algorithm
1594  hashAlgo = certInfo->signatureAlgo.rsaPssParams.hashAlgo;
1595  hashAlgoLen = certInfo->signatureAlgo.rsaPssParams.hashAlgoLen;
1596 
1597 #if (SHA256_SUPPORT == ENABLED)
1598  //SHA-256 hash algorithm identifier?
1599  if(!oidComp(hashAlgo, hashAlgoLen, SHA256_OID, sizeof(SHA256_OID)))
1600  {
1601  //RSA-PSS with SHA-256 signature algorithm
1603  *hashAlgo = TLS_HASH_ALGO_INTRINSIC;
1604  }
1605  else
1606 #endif
1607 #if (SHA384_SUPPORT == ENABLED)
1608  //SHA-384 hash algorithm identifier?
1609  if(!oidComp(hashAlgo, hashAlgoLen, SHA384_OID, sizeof(SHA384_OID)))
1610  {
1611  //RSA-PSS with SHA-384 signature algorithm
1613  *hashAlgo = TLS_HASH_ALGO_INTRINSIC;
1614  }
1615  else
1616 #endif
1617 #if (SHA512_SUPPORT == ENABLED)
1618  //SHA-512 hash algorithm identifier?
1619  if(!oidComp(hashAlgo, hashAlgoLen, SHA512_OID, sizeof(SHA512_OID)))
1620  {
1621  //RSA-PSS with SHA-512 signature algorithm
1623  *hashAlgo = TLS_HASH_ALGO_INTRINSIC;
1624  }
1625  else
1626 #endif
1627  //Unknown hash algorithm identifier?
1628  {
1629  //The signature algorithm is not supported
1630  return ERROR_BAD_CERTIFICATE;
1631  }
1632  }
1633  else
1634 #endif
1635 #if (DSA_SUPPORT == ENABLED)
1636  //DSA signature algorithm?
1638  sizeof(DSA_WITH_SHA1_OID)))
1639  {
1640  //DSA with SHA-1 signature algorithm
1641  *signAlgo = TLS_SIGN_ALGO_DSA;
1642  *hashAlgo = TLS_HASH_ALGO_SHA1;
1643  }
1645  sizeof(DSA_WITH_SHA224_OID)))
1646  {
1647  //DSA with SHA-224 signature algorithm
1648  *signAlgo = TLS_SIGN_ALGO_DSA;
1649  *hashAlgo = TLS_HASH_ALGO_SHA224;
1650  }
1652  sizeof(DSA_WITH_SHA256_OID)))
1653  {
1654  //DSA with SHA-256 signature algorithm
1655  *signAlgo = TLS_SIGN_ALGO_DSA;
1656  *hashAlgo = TLS_HASH_ALGO_SHA256;
1657  }
1658  else
1659 #endif
1660 #if (ECDSA_SUPPORT == ENABLED)
1661  //ECDSA signature algorithm?
1663  sizeof(ECDSA_WITH_SHA1_OID)))
1664  {
1665  //ECDSA with SHA-1 signature algorithm
1666  *signAlgo = TLS_SIGN_ALGO_ECDSA;
1667  *hashAlgo = TLS_HASH_ALGO_SHA1;
1668  }
1670  sizeof(ECDSA_WITH_SHA224_OID)))
1671  {
1672  //ECDSA with SHA-224 signature algorithm
1673  *signAlgo = TLS_SIGN_ALGO_ECDSA;
1674  *hashAlgo = TLS_HASH_ALGO_SHA224;
1675  }
1677  sizeof(ECDSA_WITH_SHA256_OID)))
1678  {
1679  //ECDSA with SHA-256 signature algorithm
1680  *signAlgo = TLS_SIGN_ALGO_ECDSA;
1681  *hashAlgo = TLS_HASH_ALGO_SHA256;
1682  }
1684  sizeof(ECDSA_WITH_SHA384_OID)))
1685  {
1686  //ECDSA with SHA-384 signature algorithm
1687  *signAlgo = TLS_SIGN_ALGO_ECDSA;
1688  *hashAlgo = TLS_HASH_ALGO_SHA384;
1689  }
1691  sizeof(ECDSA_WITH_SHA512_OID)))
1692  {
1693  //ECDSA with SHA-512 signature algorithm
1694  *signAlgo = TLS_SIGN_ALGO_ECDSA;
1695  *hashAlgo = TLS_HASH_ALGO_SHA512;
1696  }
1697  else
1698 #endif
1699 #if (ED25519_SUPPORT == ENABLED)
1700  //Ed25519 signature algorithm?
1701  if(!oidComp(oid, oidLen, ED25519_OID, sizeof(ED25519_OID)))
1702  {
1703  //Ed25519 signature algorithm
1704  *signAlgo = TLS_SIGN_ALGO_ED25519;
1705  *hashAlgo = TLS_HASH_ALGO_INTRINSIC;
1706  }
1707  else
1708 #endif
1709 #if (ED448_SUPPORT == ENABLED)
1710  //Ed448 signature algorithm?
1711  if(!oidComp(oid, oidLen, ED448_OID, sizeof(ED448_OID)))
1712  {
1713  //Ed448 signature algorithm
1714  *signAlgo = TLS_SIGN_ALGO_ED448;
1715  *hashAlgo = TLS_HASH_ALGO_INTRINSIC;
1716  }
1717  else
1718 #endif
1719  //Unknown signature algorithm?
1720  {
1721  //The signature algorithm is not supported
1722  return ERROR_BAD_CERTIFICATE;
1723  }
1724 
1725  //Successful processing
1726  return NO_ERROR;
1727 }
1728 
1729 
1730 /**
1731  * @brief Extract the subject public key from the received certificate
1732  * @param[in] context Pointer to the TLS context
1733  * @param[in] subjectPublicKeyInfo Pointer to the subject's public key
1734  * @return Error code
1735  **/
1736 
1738  const X509SubjectPublicKeyInfo *subjectPublicKeyInfo)
1739 {
1740  error_t error;
1741  size_t oidLen;
1742  const uint8_t *oid;
1743 
1744  //Retrieve public key identifier
1745  oid = subjectPublicKeyInfo->oid;
1746  oidLen = subjectPublicKeyInfo->oidLen;
1747 
1748 #if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
1749  //RSA public key?
1752  {
1753  uint_t k;
1754 
1755  //Import the RSA public key
1756  error = x509ImportRsaPublicKey(subjectPublicKeyInfo,
1757  &context->peerRsaPublicKey);
1758 
1759  //Check status code
1760  if(!error)
1761  {
1762  //Get the length of the modulus, in bits
1763  k = mpiGetBitLength(&context->peerRsaPublicKey.n);
1764 
1765  //Applications should also enforce minimum and maximum key sizes (refer
1766  //to RFC 8446, appendix C.2)
1767  if(k < TLS_MIN_RSA_MODULUS_SIZE || k > TLS_MAX_RSA_MODULUS_SIZE)
1768  {
1769  //Report an error
1770  error = ERROR_BAD_CERTIFICATE;
1771  }
1772  }
1773 
1774  //Check status code
1775  if(!error)
1776  {
1777  //RSA or RSA-PSS certificate?
1779  {
1780  //The certificate contains a valid RSA public key
1781  context->peerCertType = TLS_CERT_RSA_SIGN;
1782  }
1783  else if(!oidComp(oid, oidLen, RSASSA_PSS_OID, sizeof(RSASSA_PSS_OID)))
1784  {
1785  //The certificate contains a valid RSA-PSS public key
1786  context->peerCertType = TLS_CERT_RSA_PSS_SIGN;
1787  }
1788  else
1789  {
1790  //Just for sanity
1791  error = ERROR_BAD_CERTIFICATE;
1792  }
1793  }
1794  }
1795  else
1796 #endif
1797 #if (TLS_DSA_SIGN_SUPPORT == ENABLED)
1798  //DSA public key?
1799  if(!oidComp(oid, oidLen, DSA_OID, sizeof(DSA_OID)))
1800  {
1801  uint_t k;
1802 
1803  //Import the DSA public key
1804  error = x509ImportDsaPublicKey(subjectPublicKeyInfo,
1805  &context->peerDsaPublicKey);
1806 
1807  //Check status code
1808  if(!error)
1809  {
1810  //Get the length of the prime modulus, in bits
1811  k = mpiGetBitLength(&context->peerDsaPublicKey.params.p);
1812 
1813  //Make sure the prime modulus is acceptable
1814  if(k < TLS_MIN_DSA_MODULUS_SIZE || k > TLS_MAX_DSA_MODULUS_SIZE)
1815  {
1816  //Report an error
1817  error = ERROR_BAD_CERTIFICATE;
1818  }
1819  }
1820 
1821  //Check status code
1822  if(!error)
1823  {
1824  //The certificate contains a valid DSA public key
1825  context->peerCertType = TLS_CERT_DSS_SIGN;
1826  }
1827  }
1828  else
1829 #endif
1830 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
1831  //EC public key?
1833  {
1834  const EcCurveInfo *curveInfo;
1835 
1836  //Retrieve EC domain parameters
1837  curveInfo = x509GetCurveInfo(subjectPublicKeyInfo->ecParams.namedCurve,
1838  subjectPublicKeyInfo->ecParams.namedCurveLen);
1839 
1840  //Make sure the specified elliptic curve is supported
1841  if(curveInfo != NULL)
1842  {
1843  //Load EC domain parameters
1844  error = ecLoadDomainParameters(&context->peerEcParams, curveInfo);
1845 
1846  //Check status code
1847  if(!error)
1848  {
1849  //Retrieve the EC public key
1850  error = ecImport(&context->peerEcParams, &context->peerEcPublicKey.q,
1851  subjectPublicKeyInfo->ecPublicKey.q, subjectPublicKeyInfo->ecPublicKey.qLen);
1852  }
1853  }
1854  else
1855  {
1856  //The specified elliptic curve is not supported
1857  error = ERROR_BAD_CERTIFICATE;
1858  }
1859 
1860  //Check status code
1861  if(!error)
1862  {
1863  //The certificate contains a valid EC public key
1864  context->peerCertType = TLS_CERT_ECDSA_SIGN;
1865  }
1866  }
1867  else
1868 #endif
1869 #if (TLS_EDDSA_SIGN_SUPPORT == ENABLED)
1870  //Ed25519 or Ed448 public key?
1871  if(!oidComp(oid, oidLen, ED25519_OID, sizeof(ED25519_OID)) ||
1872  !oidComp(oid, oidLen, ED448_OID, sizeof(ED448_OID)))
1873  {
1874  const EcCurveInfo *curveInfo;
1875 
1876  //Retrieve EC domain parameters
1877  curveInfo = x509GetCurveInfo(oid, oidLen);
1878 
1879  //Make sure the specified elliptic curve is supported
1880  if(curveInfo != NULL)
1881  {
1882  //Load EC domain parameters
1883  error = ecLoadDomainParameters(&context->peerEcParams, curveInfo);
1884 
1885  //Check status code
1886  if(!error)
1887  {
1888  //Retrieve the EC public key
1889  error = ecImport(&context->peerEcParams, &context->peerEcPublicKey.q,
1890  subjectPublicKeyInfo->ecPublicKey.q, subjectPublicKeyInfo->ecPublicKey.qLen);
1891  }
1892  }
1893  else
1894  {
1895  //The specified elliptic curve is not supported
1896  error = ERROR_BAD_CERTIFICATE;
1897  }
1898 
1899  //Check status code
1900  if(!error)
1901  {
1902  //Ed25519 or Ed448 certificate?
1903  if(!oidComp(oid, oidLen, ED25519_OID, sizeof(ED25519_OID)))
1904  {
1905  //The certificate contains a valid Ed25519 public key
1906  context->peerCertType = TLS_CERT_ED25519_SIGN;
1907  }
1908  else if(!oidComp(oid, oidLen, ED448_OID, sizeof(ED448_OID)))
1909  {
1910  //The certificate contains a valid Ed448 public key
1911  context->peerCertType = TLS_CERT_ED448_SIGN;
1912  }
1913  else
1914  {
1915  //Just for sanity
1916  error = ERROR_BAD_CERTIFICATE;
1917  }
1918  }
1919  }
1920  else
1921 #endif
1922  //Invalid public key?
1923  {
1924  //The certificate does not contain any valid public key
1926  }
1927 
1928 #if (TLS_CLIENT_SUPPORT == ENABLED)
1929  //Check status code
1930  if(!error)
1931  {
1932  //Client mode?
1933  if(context->entity == TLS_CONNECTION_END_CLIENT)
1934  {
1935  //Check key exchange method
1936  if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
1937  context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
1938  context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
1939  context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
1940  {
1941  //The client expects a valid RSA certificate whenever the agreed-
1942  //upon key exchange method uses RSA certificates for authentication
1943  if(context->peerCertType != TLS_CERT_RSA_SIGN &&
1944  context->peerCertType != TLS_CERT_RSA_PSS_SIGN)
1945  {
1947  }
1948  }
1949  else if(context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS)
1950  {
1951  //The client expects a valid DSA certificate whenever the agreed-
1952  //upon key exchange method uses DSA certificates for authentication
1953  if(context->peerCertType != TLS_CERT_DSS_SIGN)
1954  {
1956  }
1957  }
1958  else if(context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
1959  {
1960  //The client expects a valid ECDSA certificate whenever the agreed-
1961  //upon key exchange method uses ECDSA certificates for authentication
1962  if(context->peerCertType != TLS_CERT_ECDSA_SIGN &&
1963  context->peerCertType != TLS_CERT_ED25519_SIGN &&
1964  context->peerCertType != TLS_CERT_ED448_SIGN)
1965  {
1967  }
1968  }
1969  else if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
1970  context->keyExchMethod == TLS13_KEY_EXCH_ECDHE)
1971  {
1972  //TLS 1.3 removes support for DSA certificates
1973  if(context->peerCertType != TLS_CERT_RSA_SIGN &&
1974  context->peerCertType != TLS_CERT_RSA_PSS_SIGN &&
1975  context->peerCertType != TLS_CERT_ECDSA_SIGN &&
1976  context->peerCertType != TLS_CERT_ED25519_SIGN &&
1977  context->peerCertType != TLS_CERT_ED448_SIGN)
1978  {
1980  }
1981  }
1982  else
1983  {
1984  //Just for sanity
1986  }
1987  }
1988  }
1989 #endif
1990 
1991  //Return status code
1992  return error;
1993 }
1994 
1995 
1996 /**
1997  * @brief Check certificate key usage
1998  * @param[in] certInfo Pointer to the X.509 certificate
1999  * @param[in] entity Specifies whether this entity is considered a client or a server
2000  * @param[in] keyExchMethod TLS key exchange method
2001  * @return Error code
2002  **/
2003 
2005  TlsConnectionEnd entity, TlsKeyExchMethod keyExchMethod)
2006 {
2007 #if (TLS_CERT_KEY_USAGE_SUPPORT == ENABLED)
2008  error_t error;
2009  const X509KeyUsage *keyUsage;
2010  const X509ExtendedKeyUsage *extKeyUsage;
2011 
2012  //Initialize status code
2013  error = NO_ERROR;
2014 
2015  //Point to the KeyUsage extension
2016  keyUsage = &certInfo->tbsCert.extensions.keyUsage;
2017 
2018  //Check if the KeyUsage extension is present
2019  if(keyUsage->bitmap != 0)
2020  {
2021  //Check whether TLS operates as a client or a server
2022  if(entity == TLS_CONNECTION_END_CLIENT)
2023  {
2024  //Check key exchange method
2025  if(keyExchMethod == TLS_KEY_EXCH_RSA ||
2026  keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
2027  {
2028  //The keyEncipherment bit must be asserted when the subject public
2029  //key is used for enciphering private or secret keys
2030  if((keyUsage->bitmap & X509_KEY_USAGE_KEY_ENCIPHERMENT) == 0)
2031  {
2032  error = ERROR_BAD_CERTIFICATE;
2033  }
2034  }
2035  else if(keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
2036  keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
2037  keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
2038  keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
2039  keyExchMethod == TLS13_KEY_EXCH_DHE ||
2040  keyExchMethod == TLS13_KEY_EXCH_ECDHE)
2041  {
2042  //The digitalSignature bit must be asserted when the subject public
2043  //key is used for verifying digital signatures, other than signatures
2044  //on certificates and CRLs
2045  if((keyUsage->bitmap & X509_KEY_USAGE_DIGITAL_SIGNATURE) == 0)
2046  {
2047  error = ERROR_BAD_CERTIFICATE;
2048  }
2049  }
2050  else
2051  {
2052  //Just for sanity
2053  }
2054  }
2055  else
2056  {
2057  //The digitalSignature bit must be asserted when the subject public
2058  //key is used for verifying digital signatures, other than signatures
2059  //on certificates and CRLs
2060  if((keyUsage->bitmap & X509_KEY_USAGE_DIGITAL_SIGNATURE) == 0)
2061  {
2062  error = ERROR_BAD_CERTIFICATE;
2063  }
2064  }
2065  }
2066 
2067  //Point to the ExtendedKeyUsage extension
2068  extKeyUsage = &certInfo->tbsCert.extensions.extKeyUsage;
2069 
2070  //Check if the ExtendedKeyUsage extension is present
2071  if(extKeyUsage->bitmap != 0)
2072  {
2073  //Check whether TLS operates as a client or a server
2074  if(entity == TLS_CONNECTION_END_CLIENT)
2075  {
2076  //Make sure the certificate can be used for server authentication
2077  if((extKeyUsage->bitmap & X509_EXT_KEY_USAGE_SERVER_AUTH) == 0)
2078  {
2079  error = ERROR_BAD_CERTIFICATE;
2080  }
2081  }
2082  else
2083  {
2084  //Make sure the certificate can be used for client authentication
2085  if((extKeyUsage->bitmap & X509_EXT_KEY_USAGE_CLIENT_AUTH) == 0)
2086  {
2087  error = ERROR_BAD_CERTIFICATE;
2088  }
2089  }
2090  }
2091 
2092  //Return status code
2093  return error;
2094 #else
2095  //Do not check key usage
2096  return NO_ERROR;
2097 #endif
2098 }
2099 
2100 #endif
__start_packed struct @14 TlsSupportedGroupList
List of supported groups.
#define tlsAllocMem(size)
Definition: tls.h:820
uint8_t length
Definition: coap_common.h:193
@ TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256
Definition: tls.h:1185
@ TLS_SIGN_ALGO_DSA
Definition: tls.h:1183
TLS helper functions.
bool_t tlsIsCertificateAcceptable(TlsContext *context, const TlsCertDesc *cert, const uint8_t *certTypes, size_t numCertTypes, const TlsSignHashAlgos *signHashAlgos, const TlsSignHashAlgos *certSignHashAlgos, const TlsSupportedGroupList *curveList, const TlsCertAuthorities *certAuthorities)
Check whether a certificate is acceptable.
X.509 certificate parsing.
const uint8_t * oid
Definition: x509_common.h:714
const uint8_t * oid
Definition: x509_common.h:897
int bool_t
Definition: compiler_port.h:53
#define SHA512_OID
Definition: sha512.h:49
const uint8_t DSA_WITH_SHA224_OID[9]
Definition: dsa.c:55
const EcCurveInfo * x509GetCurveInfo(const uint8_t *oid, size_t length)
Get the elliptic curve that matches the specified OID.
Definition: x509_common.c:853
const uint8_t MD5_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:62
const uint8_t SHA512_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:72
error_t x509ImportRsaPublicKey(const X509SubjectPublicKeyInfo *publicKeyInfo, RsaPublicKey *publicKey)
Import an RSA public key.
#define TLS_MAX_DSA_MODULUS_SIZE
Definition: tls.h:761
__weak_func error_t tlsParseCertificateList(TlsContext *context, const uint8_t *p, size_t length)
Parse certificate chain.
@ ERROR_UNKNOWN_CERTIFICATE
Definition: error.h:236
error_t ecImport(const EcDomainParameters *params, EcPoint *r, const uint8_t *data, size_t length)
Convert an octet string to an EC point.
Definition: ec.c:365
X509Extensions extensions
Definition: x509_common.h:931
error_t x509ParseSubjectPublicKeyInfo(const uint8_t *data, size_t length, size_t *totalLength, X509SubjectPublicKeyInfo *publicKeyInfo)
Parse SubjectPublicKeyInfo structure.
OID (Object Identifier)
uint8_t p
Definition: ndp.h:298
X509KeyUsage keyUsage
Definition: x509_common.h:863
TlsConnectionEnd
TLS connection end.
Definition: tls.h:921
#define TRUE
Definition: os_port.h:52
@ TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384
Definition: tls.h:1191
size_t rawDataLen
Definition: x509_common.h:574
X509RsaPssParameters rsaPssParams
Definition: x509_common.h:900
const uint8_t EC_PUBLIC_KEY_OID[7]
Definition: ec.c:43
TlsHashAlgo hashAlgo
Hash algorithm used to sign the end entity certificate.
Definition: tls.h:2016
TlsCertificateType type
End entity certificate type.
Definition: tls.h:2014
X.509 certificate.
Definition: x509_common.h:940
X509EcParameters ecParams
Definition: x509_common.h:724
@ TLS_HASH_ALGO_SHA1
Definition: tls.h:1166
X509ExtendedKeyUsage extKeyUsage
Definition: x509_common.h:864
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:706
TlsHashAlgo
Hash algorithms.
Definition: tls.h:1163
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:232
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ TLS13_KEY_EXCH_ECDHE
Definition: tls.h:1113
@ TLS_CERT_DSS_SIGN
Definition: tls.h:1141
const uint8_t * q
Definition: x509_common.h:701
@ X509_EXT_KEY_USAGE_CLIENT_AUTH
Definition: x509_common.h:419
@ ERROR_UNSUPPORTED_CERTIFICATE
Definition: error.h:235
@ TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512
Definition: tls.h:1192
error_t tlsReadSubjectPublicKey(TlsContext *context, const X509SubjectPublicKeyInfo *subjectPublicKeyInfo)
Extract the subject public key from the received certificate.
const uint8_t RSASSA_PSS_OID[9]
Definition: rsa.c:88
const uint8_t ECDSA_WITH_SHA256_OID[8]
Definition: ecdsa.c:49
@ ERROR_CERTIFICATE_REVOKED
Definition: error.h:238
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:103
uint8_t oid[]
Definition: lldp_tlv.h:298
@ TLS_SIGN_ALGO_ED448
Definition: tls.h:1189
@ TLS_HASH_ALGO_NONE
Definition: tls.h:1164
error_t tls13ParseCertExtensions(const uint8_t *p, size_t length, size_t *consumed)
Parse certificate extensions.
Definition: tls13_misc.c:767
@ TLS_HASH_ALGO_SHA224
Definition: tls.h:1167
@ TLS_KEY_EXCH_RSA
Definition: tls.h:1094
const uint8_t DSA_OID[7]
Definition: dsa.c:51
bool_t x509CompareName(const uint8_t *name1, size_t nameLen1, const uint8_t *name2, size_t nameLen2)
Compare distinguished names.
#define SHA256_OID
Definition: sha256.h:49
const uint8_t SHA384_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:70
error_t x509CheckNameConstraints(const char_t *subjectName, const X509CertificateInfo *certInfo)
Check name constraints.
error_t tlsCheckKeyUsage(const X509CertificateInfo *certInfo, TlsConnectionEnd entity, TlsKeyExchMethod keyExchMethod)
Check certificate key usage.
@ TLS_HASH_ALGO_SHA512
Definition: tls.h:1170
@ TLS_KEY_EXCH_ECDHE_ECDSA
Definition: tls.h:1103
error_t ecLoadDomainParameters(EcDomainParameters *params, const EcCurveInfo *curveInfo)
Load EC domain parameters.
Definition: ec.c:90
error_t tlsGetCertificateSignAlgo(const X509CertificateInfo *certInfo, TlsSignatureAlgo *signAlgo, TlsHashAlgo *hashAlgo)
Retrieve the signature algorithm used to sign the certificate.
@ TLS_KEY_EXCH_ECDHE_RSA
Definition: tls.h:1101
size_t certChainLen
Length of the certificate chain.
Definition: tls.h:2010
#define FALSE
Definition: os_port.h:48
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:60
Elliptic curve parameters.
Definition: ec_curves.h:293
__start_packed struct @3 TlsSignHashAlgo
Signature algorithm.
PEM file import functions.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:140
error_t x509ImportDsaPublicKey(const X509SubjectPublicKeyInfo *publicKeyInfo, DsaPublicKey *publicKey)
Import a DSA public key.
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
EC parameters.
Definition: x509_common.h:689
@ TLS_CERT_ED25519_SIGN
Definition: tls.h:1153
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:923
TlsKeyExchMethod
Key exchange methods.
Definition: tls.h:1092
Extended key usage.
Definition: x509_common.h:772
#define TLS_VERSION_1_2
Definition: tls.h:98
@ TLS_GROUP_NONE
Definition: tls.h:1292
const uint8_t * rawData
Definition: x509_common.h:712
X509Version version
Definition: x509_common.h:924
@ TLS13_KEY_EXCH_DHE
Definition: tls.h:1112
@ TLS_HASH_ALGO_INTRINSIC
Definition: tls.h:1171
#define TLS_VERSION_1_3
Definition: tls.h:99
@ TLS_HASH_ALGO_SHA384
Definition: tls.h:1169
@ TLS_CERT_RSA_PSS_SIGN
Definition: tls.h:1152
@ X509_VERSION_3
Definition: x509_common.h:390
const uint8_t ECDSA_WITH_SHA384_OID[8]
Definition: ecdsa.c:51
__start_packed struct @4 TlsSignHashAlgos
List of signature algorithms.
@ ERROR_BAD_CERTIFICATE
Definition: error.h:234
@ TLS_SIGN_ALGO_ED25519
Definition: tls.h:1188
@ TLS_HASH_ALGO_SHA256
Definition: tls.h:1168
@ TLS_CERT_ED448_SIGN
Definition: tls.h:1154
error_t tlsFormatRawPublicKey(TlsContext *context, uint8_t *p, size_t *written)
Format raw public key.
__start_packed struct @6 TlsCertAuthorities
List of certificate authorities.
@ X509_EXT_KEY_USAGE_SERVER_AUTH
Definition: x509_common.h:418
@ TLS_CERT_RSA_SIGN
Definition: tls.h:1140
@ X509_KEY_USAGE_DIGITAL_SIGNATURE
Definition: x509_common.h:400
const uint8_t * rawData
Definition: x509_common.h:573
TlsNamedGroup tlsGetNamedCurve(const uint8_t *oid, size_t length)
Get the named curve that matches the specified OID.
Definition: tls_misc.c:1376
size_t namedCurveLen
Definition: x509_common.h:691
@ ERROR_MESSAGE_TOO_LONG
Definition: error.h:136
error_t tls13FormatCertExtensions(uint8_t *p, size_t *written)
Format certificate extensions.
Definition: tls13_misc.c:738
uint16_t bitmap
Definition: x509_common.h:763
error_t tlsValidateCertificate(TlsContext *context, const X509CertificateInfo *certInfo, uint_t pathLen, const char_t *subjectName)
Verify certificate against root CAs.
const uint8_t ECDSA_WITH_SHA1_OID[7]
Definition: ecdsa.c:45
const uint8_t ECDSA_WITH_SHA224_OID[8]
Definition: ecdsa.c:47
const uint8_t SHA256_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:68
TlsCertificateType
Certificate types.
Definition: tls.h:1138
const uint8_t ED448_OID[3]
Definition: ec_curves.c:98
error_t tlsFormatCertificateList(TlsContext *context, uint8_t *p, size_t *written)
Format certificate chain.
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:195
@ TLS_HASH_ALGO_MD5
Definition: tls.h:1165
Certificate descriptor.
Definition: tls.h:2008
const uint8_t ECDSA_WITH_SHA512_OID[8]
Definition: ecdsa.c:53
const uint8_t ED25519_OID[3]
Definition: ec_curves.c:96
const uint8_t RSA_ENCRYPTION_OID[9]
Definition: rsa.c:57
@ TLS_KEY_EXCH_RSA_PSK
Definition: tls.h:1106
#define ntohs(value)
Definition: cpu_endian.h:421
#define TRACE_WARNING(...)
Definition: debug.h:85
TlsSignatureAlgo signAlgo
Signature algorithm used to sign the end entity certificate.
Definition: tls.h:2015
char char_t
Definition: compiler_port.h:48
#define TLS_VERSION_1_1
Definition: tls.h:97
error_t tlsParseRawPublicKey(TlsContext *context, const uint8_t *p, size_t length)
Parse raw public key.
uint8_t m
Definition: ndp.h:302
uint8_t n
Subject public key information.
Definition: x509_common.h:711
@ TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512
Definition: tls.h:1187
@ TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256
Definition: tls.h:1190
#define TLS_MAX_RSA_MODULUS_SIZE
Definition: tls.h:747
@ ERROR_CERTIFICATE_EXPIRED
Definition: error.h:237
#define LOAD24BE(p)
Definition: cpu_endian.h:197
TlsSignatureAlgo
Signature algorithms.
Definition: tls.h:1180
const uint8_t DSA_WITH_SHA1_OID[7]
Definition: dsa.c:53
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:922
const uint8_t DSA_WITH_SHA256_OID[9]
Definition: dsa.c:57
X.509 certificate handling.
uint8_t oidLen
Definition: lldp_tlv.h:297
const EcCurveInfo * tlsGetCurveInfo(TlsContext *context, uint16_t namedCurve)
Get the EC domain parameters that match the specified named curve.
Definition: tls_misc.c:1228
TLS (Transport Layer Security)
@ TLS_SIGN_ALGO_RSA
Definition: tls.h:1182
error_t x509ValidateCertificate(const X509CertificateInfo *certInfo, const X509CertificateInfo *issuerCertInfo, uint_t pathLen)
X.509 certificate validation.
X.509 certificate validation.
#define STORE24BE(a, p)
Definition: cpu_endian.h:273
@ TLS_CERT_ECDSA_SIGN
Definition: tls.h:1147
@ TLS_KEY_EXCH_DHE_DSS
Definition: tls.h:1098
@ ERROR_UNKNOWN_CA
Definition: error.h:239
error_t x509ParseCertificate(const uint8_t *data, size_t length, X509CertificateInfo *certInfo)
Parse a X.509 certificate.
X509SignatureAlgoId signatureAlgo
Definition: x509_common.h:942
const char_t * certChain
End entity certificate chain (PEM format)
Definition: tls.h:2009
Parsing of ASN.1 encoded keys.
TlsNamedGroup
Named groups.
Definition: tls.h:1291
@ ERROR_DECODING_FAILED
Definition: error.h:240
unsigned int uint_t
Definition: compiler_port.h:50
#define LOAD16BE(p)
Definition: cpu_endian.h:186
#define tlsFreeMem(p)
Definition: tls.h:825
@ X509_KEY_USAGE_KEY_ENCIPHERMENT
Definition: x509_common.h:402
X509EcPublicKey ecPublicKey
Definition: x509_common.h:725
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:930
error_t tlsGetCertificateType(const X509CertificateInfo *certInfo, TlsCertificateType *certType, TlsNamedGroup *namedCurve)
Retrieve the certificate type.
@ TLS_SIGN_ALGO_ECDSA
Definition: tls.h:1184
@ TLS_KEY_EXCH_DHE_RSA
Definition: tls.h:1096
const uint8_t * namedCurve
Definition: x509_common.h:690
#define SHA384_OID
Definition: sha384.h:45
const uint8_t SHA1_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:64
@ TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384
Definition: tls.h:1186
TlsNamedGroup namedCurve
Named curve used to generate the EC public key.
Definition: tls.h:2017
X509TbsCertificate tbsCert
Definition: x509_common.h:941
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
const uint8_t * hashAlgo
Definition: x509_common.h:881
Key usage.
Definition: x509_common.h:761
error_t x509CheckSubjectName(const X509CertificateInfo *certInfo, const char_t *fqdn)
Check whether the certificate matches the specified FQDN.