-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_certificate.c
1849 lines (1652 loc) · 54.9 KB
/
tls_certificate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file tls_certificate.c
* @brief X.509 certificate handling
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneSSL Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.4.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include "tls.h"
#include "tls_cipher_suites.h"
#include "tls_certificate.h"
#include "tls_sign_misc.h"
#include "tls_misc.h"
#include "encoding/asn1.h"
#include "encoding/oid.h"
#include "pkix/pem_import.h"
#include "pkix/x509_cert_parse.h"
#include "pkix/x509_cert_validate.h"
#include "pkix/x509_key_parse.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
/**
* @brief Format certificate chain
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the certificate chain
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatCertificateList(TlsContext *context, uint8_t *p,
size_t *written)
{
error_t error;
size_t m;
size_t n;
size_t certChainLen;
const char_t *certChain;
//Initialize status code
error = NO_ERROR;
//Length of the certificate list in bytes
*written = 0;
//Check whether a certificate is available
if(context->cert != NULL)
{
//Point to the certificate chain
certChain = context->cert->certChain;
//Get the total length, in bytes, of the certificate chain
certChainLen = context->cert->certChainLen;
}
else
{
//If no suitable certificate is available, the message contains an
//empty certificate list
certChain = NULL;
certChainLen = 0;
}
//Parse the certificate chain
while(certChainLen > 0)
{
//The first pass calculates the length of the DER-encoded certificate
error = pemImportCertificate(certChain, certChainLen, NULL, &n, NULL);
//End of file detected?
if(error)
{
//Exit immediately
error = NO_ERROR;
break;
}
//Buffer overflow?
if((*written + n + 3) > context->txBufferMaxLen)
{
//Report an error
error = ERROR_MESSAGE_TOO_LONG;
break;
}
//Each certificate is preceded by a 3-byte length field
STORE24BE(n, p);
//The second pass decodes the PEM certificate
error = pemImportCertificate(certChain, certChainLen, p + 3, &n, &m);
//Any error to report?
if(error)
break;
//Advance read pointer
certChain += m;
certChainLen -= m;
//Advance write pointer
p += n + 3;
*written += n + 3;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//Format the list of extensions for the current CertificateEntry
error = tls13FormatCertExtensions(p, &n);
//Any error to report?
if(error)
break;
//Advance write pointer
p += n;
*written += n;
}
#endif
}
//Return status code
return error;
}
/**
* @brief Format raw public key
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the raw public key
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatRawPublicKey(TlsContext *context, uint8_t *p,
size_t *written)
{
error_t error;
//Initialize status code
error = NO_ERROR;
//Length of the certificate list in bytes
*written = 0;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//Check whether a certificate is available
if(context->cert != NULL)
{
size_t n;
uint8_t *derCert;
size_t derCertLen;
X509CertInfo *certInfo;
//Initialize variables
derCert = NULL;
certInfo = NULL;
//Start of exception handling block
do
{
//The first pass calculates the length of the DER-encoded certificate
error = pemImportCertificate(context->cert->certChain,
context->cert->certChainLen, NULL, &derCertLen, NULL);
//Any error to report?
if(error)
break;
//Allocate a memory buffer to hold the DER-encoded certificate
derCert = tlsAllocMem(derCertLen);
//Failed to allocate memory?
if(derCert == NULL)
{
error = ERROR_OUT_OF_MEMORY;
break;
}
//The second pass decodes the PEM certificate
error = pemImportCertificate(context->cert->certChain,
context->cert->certChainLen, derCert, &derCertLen, NULL);
//Any error to report?
if(error)
break;
//Allocate a memory buffer to store X.509 certificate info
certInfo = tlsAllocMem(sizeof(X509CertInfo));
//Failed to allocate memory?
if(certInfo == NULL)
{
error = ERROR_OUT_OF_MEMORY;
break;
}
//Parse X.509 certificate
error = x509ParseCertificate(derCert, derCertLen, certInfo);
//Failed to parse the X.509 certificate?
if(error)
break;
//Retrieve the length of the raw public key
n = certInfo->tbsCert.subjectPublicKeyInfo.raw.length;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//The raw public key is preceded by a 3-byte length field
STORE24BE(n, p);
//Copy the raw public key
osMemcpy(p + 3, certInfo->tbsCert.subjectPublicKeyInfo.raw.value, n);
//Advance data pointer
p += n + 3;
//Adjust the length of the certificate list
*written += n + 3;
//Format the list of extensions for the current CertificateEntry
error = tls13FormatCertExtensions(p, &n);
//Any error to report?
if(error)
break;
//Advance data pointer
p += n;
//Adjust the length of the certificate list
*written += n;
}
else
#endif
{
//Copy the raw public key
osMemcpy(p, certInfo->tbsCert.subjectPublicKeyInfo.raw.value, n);
//Advance data pointer
p += n;
//Adjust the length of the certificate list
*written += n;
}
//End of exception handling block
} while(0);
//Release previously allocated memory
tlsFreeMem(derCert);
tlsFreeMem(certInfo);
}
#endif
//Return status code
return error;
}
/**
* @brief Parse certificate chain
* @param[in] context Pointer to the TLS context
* @param[in] p Input stream where to read the certificate chain
* @param[in] length Number of bytes available in the input stream
* @return Error code
**/
__weak_func error_t tlsParseCertificateList(TlsContext *context,
const uint8_t *p, size_t length)
{
error_t error;
error_t certValidResult;
uint_t i;
size_t n;
const char_t *subjectName;
X509CertInfo *certInfo;
X509CertInfo *issuerCertInfo;
//Initialize X.509 certificates
certInfo = NULL;
issuerCertInfo = NULL;
//Start of exception handling block
do
{
//Allocate a memory buffer to store X.509 certificate info
certInfo = tlsAllocMem(sizeof(X509CertInfo));
//Failed to allocate memory?
if(certInfo == NULL)
{
//Report an error
error = ERROR_OUT_OF_MEMORY;
break;
}
//Allocate a memory buffer to store the parent certificate
issuerCertInfo = tlsAllocMem(sizeof(X509CertInfo));
//Failed to allocate memory?
if(issuerCertInfo == NULL)
{
//Report an error
error = ERROR_OUT_OF_MEMORY;
break;
}
//The end-user certificate is preceded by a 3-byte length field
if(length < 3)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Get the size occupied by the certificate
n = LOAD24BE(p);
//Jump to the beginning of the DER-encoded certificate
p += 3;
length -= 3;
//Malformed Certificate message?
if(n == 0 || n > length)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Display ASN.1 structure
error = asn1DumpObject(p, n, 0);
//Any error to report?
if(error)
break;
//Parse end-user certificate
error = x509ParseCertificate(p, n, certInfo);
//Failed to parse the X.509 certificate?
if(error)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Check certificate key usage
error = tlsCheckKeyUsage(certInfo, context->entity,
context->keyExchMethod);
//Any error to report?
if(error)
break;
//Extract the public key from the end-user certificate
error = tlsReadSubjectPublicKey(context,
&certInfo->tbsCert.subjectPublicKeyInfo);
//Any error to report?
if(error)
break;
#if (TLS_CLIENT_SUPPORT == ENABLED)
//Client mode?
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
TlsCertificateType certType;
TlsNamedGroup namedCurve;
//Retrieve the type of the X.509 certificate
error = tlsGetCertificateType(certInfo, &certType, &namedCurve);
//Unsupported certificate?
if(error)
break;
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//ECDSA certificate?
if(certType == TLS_CERT_ECDSA_SIGN)
{
//Make sure the elliptic curve is supported
if(tlsGetCurveInfo(context, namedCurve) == NULL)
{
error = ERROR_BAD_CERTIFICATE;
break;
}
}
}
//Point to the subject name
subjectName = context->serverName;
//Check the subject name in the server certificate against the actual
//FQDN name that is being requested
error = x509CheckSubjectName(certInfo, subjectName);
//Any error to report?
if(error)
{
//Debug message
TRACE_WARNING("Server name mismatch!\r\n");
//Report an error
error = ERROR_BAD_CERTIFICATE;
break;
}
}
else
#endif
//Server mode?
{
//Do not check name constraints
subjectName = NULL;
}
//Check if the end-user certificate can be matched with a trusted CA
certValidResult = tlsValidateCertificate(context, certInfo, 0,
subjectName);
//Check validation result
if(certValidResult != NO_ERROR && certValidResult != ERROR_UNKNOWN_CA)
{
//The certificate is not valid
error = certValidResult;
break;
}
//Next certificate
p += n;
length -= n;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//Parse the list of extensions for the current CertificateEntry
error = tls13ParseCertExtensions(p, length, &n);
//Any error to report?
if(error)
break;
//Point to the next field
p += n;
//Remaining bytes to process
length -= n;
}
#endif
//PKIX path validation
for(i = 0; length > 0; i++)
{
//Each intermediate certificate is preceded by a 3-byte length field
if(length < 3)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Get the size occupied by the certificate
n = LOAD24BE(p);
//Jump to the beginning of the DER-encoded certificate
p += 3;
//Remaining bytes to process
length -= 3;
//Malformed Certificate message?
if(n == 0 || n > length)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Display ASN.1 structure
error = asn1DumpObject(p, n, 0);
//Any error to report?
if(error)
break;
//Parse intermediate certificate
error = x509ParseCertificate(p, n, issuerCertInfo);
//Failed to parse the X.509 certificate?
if(error)
{
//Report an error
error = ERROR_DECODING_FAILED;
break;
}
//Certificate chain validation in progress?
if(certValidResult == ERROR_UNKNOWN_CA)
{
//Validate current certificate
error = x509ValidateCertificate(certInfo, issuerCertInfo, i);
//Certificate validation failed?
if(error)
break;
//Check name constraints
error = x509CheckNameConstraints(subjectName, issuerCertInfo);
//Should the application reject the certificate?
if(error)
{
//Report an error
error = ERROR_BAD_CERTIFICATE;
break;
}
//Check the version of the certificate
if(issuerCertInfo->tbsCert.version < X509_VERSION_3)
{
//Conforming implementations may choose to reject all version 1
//and version 2 intermediate certificates (refer to RFC 5280,
//section 6.1.4)
error = ERROR_BAD_CERTIFICATE;
break;
}
//Check if the intermediate certificate can be matched with a
//trusted CA
certValidResult = tlsValidateCertificate(context, issuerCertInfo,
i, subjectName);
//Check validation result
if(certValidResult != NO_ERROR && certValidResult != ERROR_UNKNOWN_CA)
{
//The certificate is not valid
error = certValidResult;
break;
}
}
//Keep track of the issuer certificate
*certInfo = *issuerCertInfo;
//Next certificate
p += n;
length -= n;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//Parse the list of extensions for the current CertificateEntry
error = tls13ParseCertExtensions(p, length, &n);
//Any error to report?
if(error)
break;
//Point to the next field
p += n;
//Remaining bytes to process
length -= n;
}
#endif
}
//Certificate chain validation failed?
if(error == NO_ERROR && certValidResult != NO_ERROR)
{
//A valid certificate chain or partial chain was received, but the
//certificate was not accepted because the CA certificate could not
//be matched with a known, trusted CA
error = ERROR_UNKNOWN_CA;
}
//End of exception handling block
} while(0);
//Free previously allocated memory
tlsFreeMem(certInfo);
tlsFreeMem(issuerCertInfo);
//Return status code
return error;
}
/**
* @brief Parse raw public key
* @param[in] context Pointer to the TLS context
* @param[in] p Input stream where to read the raw public key
* @param[in] length Number of bytes available in the input stream
* @return Error code
**/
error_t tlsParseRawPublicKey(TlsContext *context, const uint8_t *p,
size_t length)
{
error_t error;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//Any registered callback?
if(context->rpkVerifyCallback != NULL)
{
size_t n;
size_t rawPublicKeyLen;
const uint8_t *rawPublicKey;
X509SubjectPublicKeyInfo subjectPublicKeyInfo;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//The raw public key is preceded by a 3-byte length field
if(length < 3)
return ERROR_DECODING_FAILED;
//Get the size occupied by the raw public key
rawPublicKeyLen = LOAD24BE(p);
//Advance data pointer
p += 3;
//Remaining bytes to process
length -= 3;
//Malformed Certificate message?
if(length < rawPublicKeyLen)
return ERROR_DECODING_FAILED;
}
else
#endif
{
//The payload of the Certificate message contains a SubjectPublicKeyInfo
//structure
rawPublicKeyLen = length;
}
//Point to the raw public key
rawPublicKey = p;
//Decode the SubjectPublicKeyInfo structure
error = x509ParseSubjectPublicKeyInfo(rawPublicKey, rawPublicKeyLen, &n,
&subjectPublicKeyInfo);
//Any error to report?
if(error)
return error;
//Advance data pointer
p += rawPublicKeyLen;
//Remaining bytes to process
length -= rawPublicKeyLen;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//Parse the list of extensions for the current CertificateEntry
error = tls13ParseCertExtensions(p, length, &n);
//Any error to report?
if(error)
return error;
//Advance data pointer
p += n;
//Remaining bytes to process
length -= n;
}
#endif
//If the RawPublicKey certificate type was negotiated, the certificate
//list must contain no more than one CertificateEntry (refer to RFC 8446,
//section 4.4.2)
if(length != 0)
return ERROR_DECODING_FAILED;
//Extract the public key from the SubjectPublicKeyInfo structure
error = tlsReadSubjectPublicKey(context, &subjectPublicKeyInfo);
//Any error to report?
if(error)
return error;
//When raw public keys are used, authentication of the peer is supported
//only through authentication of the received SubjectPublicKeyInfo via an
//out-of-band method
error = context->rpkVerifyCallback(context, rawPublicKey,
rawPublicKeyLen);
}
else
#endif
{
//Report an error
error = ERROR_BAD_CERTIFICATE;
}
//Return status code
return error;
}
/**
* @brief Check whether a certificate is acceptable
* @param[in] context Pointer to the TLS context
* @param[in] cert End entity certificate
* @param[in] certTypes List of supported certificate types
* @param[in] numCertTypes Size of the list that contains the supported
* certificate types
* @param[in] curveList List of supported elliptic curves
* @param[in] certSignAlgoList List of signature algorithms that may be used
* in X.509 certificates
* @param[in] certAuthorities List of trusted CA
* @return TRUE if the specified certificate conforms to the requirements,
* else FALSE
**/
bool_t tlsIsCertificateAcceptable(TlsContext *context,
const TlsCertDesc *cert, const uint8_t *certTypes, size_t numCertTypes,
const TlsSupportedGroupList *curveList,
const TlsSignSchemeList *certSignAlgoList,
const TlsCertAuthorities *certAuthorities)
{
size_t i;
size_t n;
size_t length;
bool_t acceptable;
//Make sure that a valid certificate has been loaded
if(cert->certChain == NULL || cert->certChainLen == 0)
return FALSE;
//This flag tells whether the certificate is acceptable
acceptable = TRUE;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//Version of TLS prior to TLS 1.2?
if(context->version <= TLS_VERSION_1_1)
{
//Server mode?
if(context->entity == TLS_CONNECTION_END_SERVER)
{
//The signing algorithm for the certificate must be the same as the
//algorithm for the certificate key (refer to RFC 4346, section 7.4.2)
if(cert->type == TLS_CERT_RSA_SIGN &&
TLS_SIGN_ALGO(cert->signScheme) == TLS_SIGN_ALGO_RSA)
{
acceptable = TRUE;
}
else if(cert->type == TLS_CERT_DSS_SIGN &&
TLS_SIGN_ALGO(cert->signScheme) == TLS_SIGN_ALGO_DSA)
{
acceptable = TRUE;
}
else if(cert->type == TLS_CERT_ECDSA_SIGN &&
TLS_SIGN_ALGO(cert->signScheme) == TLS_SIGN_ALGO_ECDSA)
{
acceptable = TRUE;
}
else
{
acceptable = FALSE;
}
}
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//Filter out certificates with unsupported type
if(acceptable && certTypes != NULL)
{
//Loop through the list of supported certificate types
for(i = 0, acceptable = FALSE; i < numCertTypes && !acceptable; i++)
{
//Check certificate type
if(certTypes[i] == TLS_CERT_RSA_SIGN)
{
//The certificate must contain an RSA public key
if(cert->type == TLS_CERT_RSA_SIGN ||
cert->type == TLS_CERT_RSA_PSS_SIGN)
{
acceptable = TRUE;
}
}
else if(certTypes[i] == TLS_CERT_DSS_SIGN)
{
//The certificate must contain a DSA public key
if(cert->type == TLS_CERT_DSS_SIGN)
{
acceptable = TRUE;
}
}
else if(certTypes[i] == TLS_CERT_ECDSA_SIGN)
{
//The certificate must contain an ECDSA or EdDSA public key
if(cert->type == TLS_CERT_ECDSA_SIGN ||
cert->type == TLS_CERT_ED25519_SIGN ||
cert->type == TLS_CERT_ED448_SIGN)
{
acceptable = TRUE;
}
}
else
{
//Unknown certificate type
}
}
}
//ECDSA certificate?
if(cert->type == TLS_CERT_ECDSA_SIGN)
{
//In versions of TLS prior to TLS 1.3, the EllipticCurves extension is
//used to negotiate ECDSA curves (refer to RFC 8446, section 4.2.7)
if(acceptable && curveList != NULL)
{
//Retrieve the number of items in the list
n = ntohs(curveList->length) / sizeof(uint16_t);
//Loop through the list of supported elliptic curves
for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
{
//Check whether the elliptic curve is supported
if(ntohs(curveList->value[i]) == cert->namedCurve)
{
acceptable = TRUE;
}
}
}
}
}
#endif
#if (TLS_SM2_SIGN_SUPPORT == ENABLED)
//SM2 certificate?
if(cert->type == TLS_CERT_SM2_SIGN)
{
//The signature algorithm used by the CA to sign the current certificate
//must be sm2sig_sm3 (refer to RFC 8998, section 3.3.3)
if(cert->signScheme != TLS_SIGN_SCHEME_SM2SIG_SM3)
{
acceptable = FALSE;
}
}
#endif
//Filter out certificates that are signed with an unsupported algorithm
if(acceptable && certSignAlgoList != NULL)
{
//Retrieve the number of items in the list
n = ntohs(certSignAlgoList->length) / sizeof(uint16_t);
//Loop through the list of supported signature algorithms
for(i = 0, acceptable = FALSE; i < n && !acceptable; i++)
{
//The certificate must be signed using a valid algorithm
if(ntohs(certSignAlgoList->value[i]) == cert->signScheme)
{
acceptable = TRUE;
}
}
}
//Filter out certificates that are issued by a non trusted CA
if(acceptable && certAuthorities != NULL)
{
//Retrieve the length of the list
length = ntohs(certAuthorities->length);
//If the certificate authorities list is empty, then the client
//may send any certificate of the appropriate type
if(length > 0)
{
error_t error;
size_t pemCertLen;
const char_t *certChain;
size_t certChainLen;
uint8_t *derCert;
size_t derCertLen;
X509CertInfo *certInfo;
//The list of acceptable certificate authorities describes the
//known roots CA
acceptable = FALSE;
//Point to the end entity certificate
certChain = cert->certChain;
//Get the total length, in bytes, of the certificate chain
certChainLen = cert->certChainLen;
//Allocate a memory buffer to store X.509 certificate info
certInfo = tlsAllocMem(sizeof(X509CertInfo));
//Successful memory allocation?
if(certInfo != NULL)
{
//Parse the certificate chain
while(certChainLen > 0 && !acceptable)
{
//The first pass calculates the length of the DER-encoded
//certificate
error = pemImportCertificate(certChain, certChainLen, NULL,
&derCertLen, &pemCertLen);
//Check status code
if(!error)
{
//Allocate a memory buffer to hold the DER-encoded certificate
derCert = tlsAllocMem(derCertLen);
//Successful memory allocation?
if(derCert != NULL)
{
//The second pass decodes the PEM certificate
error = pemImportCertificate(certChain, certChainLen,
derCert, &derCertLen, NULL);
//Check status code
if(!error)
{
//Parse X.509 certificate
error = x509ParseCertificate(derCert, derCertLen,
certInfo);
}
//Check status code
if(!error)
{
//Parse each distinguished name of the list
for(i = 0; i < length; i += n + 2)
{
//Sanity check
if((i + 2) > length)
break;
//Each distinguished name is preceded by a 2-byte
//length field
n = LOAD16BE(certAuthorities->value + i);
//Make sure the length field is valid
if((i + n + 2) > length)
break;
//Check if the distinguished name matches the root CA
if(x509CompareName(certAuthorities->value + i + 2, n,
certInfo->tbsCert.issuer.raw.value,
certInfo->tbsCert.issuer.raw.length))
{
acceptable = TRUE;
break;
}
}
}
//Free previously allocated memory
tlsFreeMem(derCert);
}
//Advance read pointer
certChain += pemCertLen;
certChainLen -= pemCertLen;
}
else
{
//No more CA certificates in the list
break;
}
}
//Free previously allocated memory
tlsFreeMem(certInfo);
}
}
}
//The return value specifies whether all the criteria were matched
return acceptable;
}
/**
* @brief Verify certificate against root CAs
* @param[in] context Pointer to the TLS context
* @param[in] certInfo X.509 certificate to be verified
* @param[in] pathLen Certificate path length
* @param[in] subjectName Subject name (optional parameter)
* @return Error code
**/
error_t tlsValidateCertificate(TlsContext *context,
const X509CertInfo *certInfo, uint_t pathLen, const char_t *subjectName)
{
error_t error;
size_t pemCertLen;
const char_t *trustedCaList;