-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_sign_misc.c
1206 lines (1127 loc) · 38 KB
/
tls_sign_misc.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_sign_misc.c
* @brief Helper functions for signature generation and verification
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2022-2024 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneIPSEC 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_sign_misc.h"
#include "tls_misc.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
//List of supported signature algorithms
const uint16_t tlsSupportedSignAlgos[] =
{
TLS_SIGN_SCHEME_ED25519,
TLS_SIGN_SCHEME_ED448,
TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256,
TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384,
TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512,
TLS_SIGN_SCHEME_ECDSA_BP256R1_TLS13_SHA256,
TLS_SIGN_SCHEME_ECDSA_BP384R1_TLS13_SHA384,
TLS_SIGN_SCHEME_ECDSA_BP512R1_TLS13_SHA512,
TLS_SIGN_SCHEME_SM2SIG_SM3,
TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256,
TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384,
TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512,
TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256,
TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384,
TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512,
TLS_SIGN_SCHEME_RSA_PKCS1_SHA256,
TLS_SIGN_SCHEME_RSA_PKCS1_SHA384,
TLS_SIGN_SCHEME_RSA_PKCS1_SHA512,
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_DSA, TLS_HASH_ALGO_SHA256),
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_ECDSA, TLS_HASH_ALGO_SHA224),
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_RSA, TLS_HASH_ALGO_SHA224),
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_DSA, TLS_HASH_ALGO_SHA224),
TLS_SIGN_SCHEME_ECDSA_SHA1,
TLS_SIGN_SCHEME_RSA_PKCS1_SHA1,
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_DSA, TLS_HASH_ALGO_SHA1),
TLS_SIGN_SCHEME(TLS_SIGN_ALGO_RSA, TLS_HASH_ALGO_MD5)
};
/**
* @brief Select the algorithm to be used when generating digital signatures
* @param[in] context Pointer to the TLS context
* @param[in] cert End entity certificate
* @param[in] signAlgoList List of signature/hash algorithm pairs offered by
* the peer
* @return Error code
**/
error_t tlsSelectSignAlgo(TlsContext *context, const TlsCertDesc *cert,
const TlsSignSchemeList *signAlgoList)
{
error_t error;
uint_t i;
uint_t n;
uint16_t signScheme;
//Initialize status code
error = NO_ERROR;
//Default signature algorithm
context->signScheme = TLS_SIGN_SCHEME_NONE;
//TLS 1.0 or TLS 1.1 currently selected?
if(context->version <= TLS_VERSION_1_1)
{
//Check certificate type
if(cert->type != TLS_CERT_RSA_SIGN &&
cert->type != TLS_CERT_DSS_SIGN &&
cert->type != TLS_CERT_ECDSA_SIGN)
{
error = ERROR_HANDSHAKE_FAILED;
}
}
else
{
//Check whether the peer has provided a list of supported signature
//algorithms
if(signAlgoList != NULL)
{
//Any preferred preferred signature algorithms?
if(context->numSupportedSignAlgos > 0)
{
//Loop through the list of allowed signature algorithms (most
//preferred first)
for(i = 0; i < context->numSupportedSignAlgos; i++)
{
//Get current signature algorithm
signScheme = context->supportedSignAlgos[i];
//Check whether the signature algorithm is offered by the peer
if(tlsIsSignAlgoOffered(signScheme, signAlgoList))
{
//The signature algorithm must be compatible with the key in
//the end-entity certificate (refer to RFC 5246, section 7.4.3)
if(tlsIsSignAlgoAcceptable(context, signScheme, cert))
{
//Check whether the signature algorithm is supported
if(tlsIsSignAlgoSupported(context, signScheme))
{
context->signScheme = (TlsSignatureScheme) signScheme;
break;
}
}
}
}
}
else
{
//Retrieve the number of items in the list
n = ntohs(signAlgoList->length) / sizeof(uint16_t);
//Loop through the list of signature algorithms offered by the peer
for(i = 0; i < n; i++)
{
//Each SignatureScheme value lists a single signature algorithm
signScheme = ntohs(signAlgoList->value[i]);
//The signature algorithm must be compatible with the key in the
//end-entity certificate (refer to RFC 5246, section 7.4.3)
if(tlsIsSignAlgoAcceptable(context, signScheme, cert))
{
//Check whether the signature algorithm is supported
if(tlsIsSignAlgoSupported(context, signScheme))
{
context->signScheme = (TlsSignatureScheme) signScheme;
break;
}
}
}
}
}
else
{
//TLS 1.2 clients may omit the SignatureAlgorithms extension
if(context->version == TLS_VERSION_1_2)
{
//Check certificate type
if(cert->type == TLS_CERT_RSA_SIGN)
{
//If the negotiated key exchange algorithm is one of RSA,
//DHE_RSA, DH_RSA, RSA_PSK, ECDH_RSA, ECDHE_RSA, behave as if
//client had sent the value {sha1,rsa}
signScheme = TLS_SIGN_SCHEME(TLS_SIGN_ALGO_RSA,
TLS_HASH_ALGO_SHA1);
}
else if(cert->type == TLS_CERT_DSS_SIGN)
{
//If the negotiated key exchange algorithm is one of DHE_DSS,
//DH_DSS, behave as if the client had sent the value {sha1,dsa}
signScheme = TLS_SIGN_SCHEME(TLS_SIGN_ALGO_DSA,
TLS_HASH_ALGO_SHA1);
}
else if(cert->type == TLS_CERT_ECDSA_SIGN)
{
//If the negotiated key exchange algorithm is one of ECDH_ECDSA,
//ECDHE_ECDSA, behave as if the client had sent value {sha1,ecdsa}
signScheme = TLS_SIGN_SCHEME(TLS_SIGN_ALGO_ECDSA,
TLS_HASH_ALGO_SHA1);
}
else
{
//Unknown certificate type
signScheme = TLS_SIGN_SCHEME_NONE;
}
//Check whether the signature algorithm is supported
if(tlsIsSignAlgoSupported(context, signScheme))
{
context->signScheme = (TlsSignatureScheme) signScheme;
}
}
}
//If no acceptable choices are presented, return an error
if(context->signScheme == TLS_SIGN_SCHEME_NONE)
{
error = ERROR_HANDSHAKE_FAILED;
}
}
//Return status code
return error;
}
/**
* @brief Format SignatureAlgorithms extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the SignatureAlgorithms extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatSignAlgosExtension(TlsContext *context, uint8_t *p,
size_t *written)
{
error_t error;
size_t n;
//Initialize status code
error = NO_ERROR;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Check whether TLS 1.2 or TLS 1.3 is supported
if(context->versionMax >= TLS_VERSION_1_2)
{
TlsExtension *extension;
//Add the SignatureAlgorithms extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SIGNATURE_ALGORITHMS);
//The SignatureAlgorithms extension indicates which signature/hash
//algorithm pairs may be used in digital signatures
error = tlsFormatSupportedSignAlgos(context, extension->value, &n);
//Check status code
if(!error)
{
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the SignatureAlgorithms extension
n += sizeof(TlsExtension);
}
}
else
#endif
{
//This extension is not meaningful for TLS versions prior to 1.2.
//Clients must not offer it if they are offering prior versions (refer
//to RFC 5246, section 7.4.1.4.1)
n = 0;
}
//Check status code
if(!error)
{
//Total number of bytes that have been written
*written = n;
}
//Return status code
return error;
}
/**
* @brief Format SignatureAlgorithmsCert extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the SignatureAlgorithmsCert extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatSignAlgosCertExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.2 implementations should also process this extension
if(context->versionMax >= TLS_VERSION_1_2)
{
uint_t i;
TlsExtension *extension;
TlsSignSchemeList *signAlgoList;
//Add the SignatureAlgorithmsCert extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SIGNATURE_ALGORITHMS_CERT);
//The SignatureAlgorithmsCert extension allows an implementation to
//indicate which signature algorithms it can validate in X.509
//certificates
signAlgoList = (TlsSignSchemeList *) extension->value;
//Enumerate the hash/signature algorithm pairs in descending order
//of preference
n = 0;
//Loop through the list of supported signature algorithms
for(i = 0; i < arraysize(tlsSupportedSignAlgos); i++)
{
//Check whether the signature algorithm can be used for X.509
//certificate validation
if(tlsIsCertSignAlgoSupported(tlsSupportedSignAlgos[i]))
{
//Add the current signature algorithm to the list
signAlgoList->value[n++] = htons(tlsSupportedSignAlgos[i]);
}
}
//Compute the length, in bytes, of the list
n *= sizeof(uint16_t);
//Fix the length of the list
signAlgoList->length = htons(n);
//Consider the 2-byte length field that precedes the list
n += sizeof(TlsSignSchemeList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the SignatureAlgorithmsCert extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format the list of supported signature algorithms
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the list of signature algorithms
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatSupportedSignAlgos(TlsContext *context, uint8_t *p,
size_t *written)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
uint_t i;
size_t n;
uint_t numSupportedSignAlgos;
const uint16_t *supportedSignAlgos;
TlsSignSchemeList *signAlgoList;
//The list contains the hash/signature algorithm pairs that the
//implementation is able to verify
signAlgoList = (TlsSignSchemeList *) p;
//Any preferred preferred signature algorithms?
if(context->numSupportedSignAlgos > 0)
{
//Point to the list of preferred signature algorithms
supportedSignAlgos = context->supportedSignAlgos;
numSupportedSignAlgos = context->numSupportedSignAlgos;
}
else
{
//Point to the list of default signature algorithms
supportedSignAlgos = tlsSupportedSignAlgos;
numSupportedSignAlgos = arraysize(tlsSupportedSignAlgos);
}
//Enumerate the hash/signature algorithm pairs in descending order of
//preference
n = 0;
//Loop through the list of signature algorithms
for(i = 0; i < numSupportedSignAlgos; i++)
{
//Check whether the signature algorithm can be used in digital signature
if(tlsIsSignAlgoSupported(context, supportedSignAlgos[i]))
{
//Add the current signature algorithm to the list
signAlgoList->value[n++] = htons(supportedSignAlgos[i]);
}
}
//Compute the length, in bytes, of the list
n *= sizeof(uint16_t);
//Fix the length of the list
signAlgoList->length = htons(n);
//Total number of bytes that have been written
*written = n + sizeof(TlsSignSchemeList);
//Successful processing
return NO_ERROR;
#else
//Not implemented
return FALSE;
#endif
}
/**
* @brief Check whether a signature algorithm is offered in the
* SignatureAlgorithms extension
* @param[in] signScheme Signature scheme
* @param[in] signSchemeList List of signature schemes
* @return TRUE if the signature algorithm is offered in the
* SignatureAlgorithms extension, else FALSE
**/
bool_t tlsIsSignAlgoOffered(uint16_t signScheme,
const TlsSignSchemeList *signSchemeList)
{
uint_t i;
uint_t n;
bool_t found;
//Initialize flag
found = FALSE;
//Valid SignatureAlgorithms extension?
if(signSchemeList != NULL)
{
//Get the number of signature algorithms present in the list
n = ntohs(signSchemeList->length) / sizeof(uint16_t);
//Loop through the list of signature algorithms offered in the
//SignatureAlgorithms extension
for(i = 0; i < n && !found; i++)
{
//Matching signature algorithm?
if(ntohs(signSchemeList->value[i]) == signScheme)
{
found = TRUE;
}
}
}
//Return TRUE if the signature algorithm is offered in the
//SignatureAlgorithms extension
return found;
}
/**
* @brief Check whether a signature algorithm is compatible with the specified
* end-entity certificate
* @param[in] context Pointer to the TLS context
* @param[in] signScheme Signature scheme
* @param[in] cert End entity certificate
* @return TRUE if the signature algorithm is compatible, else FALSE
**/
bool_t tlsIsSignAlgoAcceptable(TlsContext *context, uint16_t signScheme,
const TlsCertDesc *cert)
{
bool_t acceptable;
//Initialize flag
acceptable = FALSE;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA certificate?
if(cert->type == TLS_CERT_RSA_SIGN)
{
//Check signature scheme
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_RSA)
{
//In TLS 1.3, RSASSA-PKCS1-v1_5 signature algorithms refer solely to
//signatures which appear in certificates and are not defined for use
//in signed TLS handshake messages
if(context->version <= TLS_VERSION_1_2)
{
acceptable = TRUE;
}
}
else if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512)
{
//TLS 1.2 or TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
acceptable = TRUE;
}
}
else
{
//Just for sanity
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSA-PSS certificate?
if(cert->type == TLS_CERT_RSA_PSS_SIGN)
{
//TLS 1.2 or TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
//Check signature scheme
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256 ||
signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384 ||
signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
acceptable = TRUE;
}
}
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA certificate?
if(cert->type == TLS_CERT_DSS_SIGN)
{
//TLS 1.3 removes support for DSA certificates
if(context->version <= TLS_VERSION_1_2)
{
//Check signature scheme
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_DSA)
{
acceptable = TRUE;
}
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA certificate?
if(cert->type == TLS_CERT_ECDSA_SIGN)
{
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//Check signature scheme
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_ECDSA)
{
acceptable = TRUE;
}
}
else
{
//Check signature scheme against elliptic curve
if(signScheme == TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256 &&
cert->namedCurve == TLS_GROUP_SECP256R1)
{
acceptable = TRUE;
}
else if(signScheme == TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384 &&
cert->namedCurve == TLS_GROUP_SECP384R1)
{
acceptable = TRUE;
}
else if(signScheme == TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512 &&
cert->namedCurve == TLS_GROUP_SECP521R1)
{
acceptable = TRUE;
}
else if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP256R1_TLS13_SHA256 &&
cert->namedCurve == TLS_GROUP_BRAINPOOLP256R1)
{
acceptable = TRUE;
}
else if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP384R1_TLS13_SHA384 &&
cert->namedCurve == TLS_GROUP_BRAINPOOLP384R1)
{
acceptable = TRUE;
}
else if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP512R1_TLS13_SHA512 &&
cert->namedCurve == TLS_GROUP_BRAINPOOLP512R1)
{
acceptable = TRUE;
}
else
{
}
}
}
else
#endif
#if (TLS_SM2_SIGN_SUPPORT == ENABLED)
//SM2 certificate?
if(cert->type == TLS_CERT_SM2_SIGN)
{
//TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_3)
{
//Check signature scheme
if(signScheme == TLS_SIGN_SCHEME_SM2SIG_SM3)
{
acceptable = TRUE;
}
}
}
else
#endif
#if (TLS_ED25519_SIGN_SUPPORT == ENABLED)
//Ed25519 certificate?
if(cert->type == TLS_CERT_ED25519_SIGN)
{
//TLS 1.2 or TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
//Check signature scheme
if(signScheme == TLS_SIGN_SCHEME_ED25519)
{
acceptable = TRUE;
}
}
}
else
#endif
#if (TLS_ED448_SIGN_SUPPORT == ENABLED)
//Ed448 certificate?
if(cert->type == TLS_CERT_ED448_SIGN)
{
//TLS 1.2 or TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
//Check signature scheme
if(signScheme == TLS_SIGN_SCHEME_ED448)
{
acceptable = TRUE;
}
}
}
else
#endif
//Unsupported certificate type?
{
//Just for sanity
}
//Return TRUE is the signature algorithm is compatible with the key in the
//end-entity certificate
return acceptable;
}
/**
* @brief Check whether a signature algorithm can be used for digital signatures
* @param[in] context Pointer to the TLS context
* @param[in] signScheme Signature scheme
* @return TRUE if the signature algorithm is supported, else FALSE
**/
bool_t tlsIsSignAlgoSupported(TlsContext *context, uint16_t signScheme)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
uint_t i;
uint_t cipherSuiteTypes;
TlsHashAlgo hashAlgoId;
const HashAlgo *hashAlgo;
//Hash algorithm used by the signature scheme
hashAlgoId = TLS_HASH_ALGO_NONE;
//Check TLS version
if(context->version <= TLS_VERSION_1_2)
{
//Check current state
if(context->state == TLS_STATE_CERTIFICATE_REQUEST ||
context->state == TLS_STATE_CLIENT_CERTIFICATE_VERIFY)
{
cipherSuiteTypes = TLS_CIPHER_SUITE_TYPE_RSA |
TLS_CIPHER_SUITE_TYPE_DSA | TLS_CIPHER_SUITE_TYPE_ECDSA;
}
else
{
cipherSuiteTypes = context->cipherSuiteTypes;
}
}
else
{
cipherSuiteTypes = context->cipherSuiteTypes;
}
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSASSA-PKCS1-v1_5 signature algorithm?
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_RSA)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Filter out hash algorithm
if(TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_MD5 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA1 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA224 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA256 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA384 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA512)
{
//Check whether the hash algorithm is supported
if(tlsGetHashAlgo(TLS_HASH_ALGO(signScheme)) != NULL)
{
hashAlgoId = TLS_HASH_ALGO(signScheme);
}
}
}
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA signature algorithm?
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_DSA)
{
//Any DSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_DSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Filter out hash algorithm
if(TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA1 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA224 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA256)
{
//Check whether the hash algorithm is supported
if(tlsGetHashAlgo(TLS_HASH_ALGO(signScheme)) != NULL)
{
hashAlgoId = TLS_HASH_ALGO(signScheme);
}
}
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature algorithm?
if(TLS_SIGN_ALGO(signScheme) == TLS_SIGN_ALGO_ECDSA)
{
//Any ECC cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Filter out hash algorithm
if(TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA1 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA224 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA256 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA384 ||
TLS_HASH_ALGO(signScheme) == TLS_HASH_ALGO_SHA512)
{
//Check whether the hash algorithm is supported
if(tlsGetHashAlgo(TLS_HASH_ALGO(signScheme)) != NULL)
{
hashAlgoId = TLS_HASH_ALGO(signScheme);
}
}
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA256_SUPPORT == ENABLED)
//RSASSA-PSS RSAE signature algorithm with SHA-256?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA384_SUPPORT == ENABLED)
//RSASSA-PSS RSAE signature algorithm with SHA-384?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA512_SUPPORT == ENABLED)
//RSASSA-PSS RSAE signature algorithm with SHA-512?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA256_SUPPORT == ENABLED)
//RSASSA-PSS PSS signature algorithm with SHA-256?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Check whether the X.509 library can parse RSA-PSS certificates
if(x509IsSignAlgoSupported(X509_SIGN_ALGO_RSA_PSS))
{
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA384_SUPPORT == ENABLED)
//RSASSA-PSS PSS signature algorithm with SHA-384?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Check whether the X.509 library can parse RSA-PSS certificates
if(x509IsSignAlgoSupported(X509_SIGN_ALGO_RSA_PSS))
{
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED && TLS_SHA512_SUPPORT == ENABLED)
//RSASSA-PSS PSS signature algorithm with SHA-512?
if(signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Any RSA cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_RSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Check whether the X.509 library can parse RSA-PSS certificates
if(x509IsSignAlgoSupported(X509_SIGN_ALGO_RSA_PSS))
{
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED && TLS_SHA256_SUPPORT == ENABLED && \
TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
//ECDSA signature algorithm with brainpoolP256 curve and SHA-256?
if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP256R1_TLS13_SHA256)
{
//Any TLS 1.3 cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED && TLS_SHA384_SUPPORT == ENABLED && \
TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
//ECDSA signature algorithm with brainpoolP384 curve and SHA-384?
if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP384R1_TLS13_SHA384)
{
//Any TLS 1.3 cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED && TLS_SHA512_SUPPORT == ENABLED && \
TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
//ECDSA signature algorithm with brainpoolP512 curve and SHA-512?
if(signScheme == TLS_SIGN_SCHEME_ECDSA_BP512R1_TLS13_SHA512)
{
//Any TLS 1.3 cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
}
else
#endif
#if (TLS_SM2_SIGN_SUPPORT == ENABLED)
//SM2 signature algorithm?
if(signScheme == TLS_SIGN_SCHEME_SM2SIG_SM3)
{
//Any ShangMi cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_SM) != 0)
{
hashAlgoId = TLS_HASH_ALGO_INTRINSIC;
}
}
else
#endif
#if (TLS_ED25519_SIGN_SUPPORT == ENABLED)
//Ed25519 signature algorithm?
if(signScheme == TLS_SIGN_SCHEME_ED25519)
{
//Any ECC cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_INTRINSIC;
}
}
else
#endif
#if (TLS_ED448_SIGN_SUPPORT == ENABLED)
//Ed448 signature algorithm?
if(signScheme == TLS_SIGN_SCHEME_ED448)
{
//Any ECC cipher suite proposed by the client?
if((cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDSA) != 0 ||
(cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
hashAlgoId = TLS_HASH_ALGO_INTRINSIC;
}
}
else
#endif
{
//Unknown signature algorithm
}
//Check TLS version
if(context->version <= TLS_VERSION_1_2)
{
//Check current state
if(context->state == TLS_STATE_CERTIFICATE_REQUEST ||
context->state == TLS_STATE_CLIENT_CERTIFICATE_VERIFY)
{
//Get the hash algorithm that matches the specified identifier
hashAlgo = tlsGetHashAlgo(hashAlgoId);
//Check whether the hash algorithm is supported
if(hashAlgo != NULL)
{
//In TLS versions prior to 1.3, the client implementation can only
//generate a CertificateVerify using SHA-1 or the hash used by
//the PRF. Supporting all hash algorithms would require the client
//to maintain hashes for every possible signature algorithm that
//the server may request...
if(hashAlgoId != TLS_HASH_ALGO_SHA1 &&
hashAlgo != context->cipherSuite.prfHashAlgo)
{
hashAlgoId = TLS_HASH_ALGO_NONE;
}
}
else
{
hashAlgoId = TLS_HASH_ALGO_NONE;
}
}
}
//Restrict the use of certain signature algorithms
if(context->numSupportedSignAlgos > 0)
{
//Loop through the list of allowed signature algorithms
for(i = 0; i < context->numSupportedSignAlgos; i++)
{
//Compare signature schemes
if(context->supportedSignAlgos[i] == signScheme)
break;
}
//Check whether the use of the signature algorithm is restricted
if(i >= context->numSupportedSignAlgos)
{
hashAlgoId = TLS_HASH_ALGO_NONE;
}
}
//Return TRUE is the signature algorithm is supported
return (hashAlgoId != TLS_HASH_ALGO_NONE) ? TRUE : FALSE;
#else
//Not implemented
return FALSE;
#endif
}