forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls_signature.c
1685 lines (1493 loc) · 51.5 KB
/
tls_signature.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_signature.c
* @brief RSA/DSA/ECDSA/EdDSA signature generation and verification (TLS 1.3)
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2022 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.1.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include <string.h>
#include "tls.h"
#include "tls_signature.h"
#include "tls_transcript_hash.h"
#include "tls_misc.h"
#include "pkix/pem_import.h"
#include "pkc/rsa.h"
#include "pkc/dsa.h"
#include "ecc/ecdsa.h"
#include "ecc/eddsa.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
/**
* @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] supportedSignAlgos List of supported signature/hash algorithm pairs
* @return Error code
**/
error_t tlsSelectSignatureScheme(TlsContext *context, const TlsCertDesc *cert,
const TlsSignHashAlgos *supportedSignAlgos)
{
error_t error;
uint_t i;
uint_t n;
const HashAlgo *hashAlgo;
const TlsSignHashAlgo *p;
//Initialize status code
error = ERROR_HANDSHAKE_FAILED;
//Default signature algorithm
context->signAlgo = TLS_SIGN_ALGO_ANONYMOUS;
context->signHashAlgo = TLS_HASH_ALGO_NONE;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED || \
TLS_DSA_SIGN_SUPPORT == ENABLED || TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//RSA, DSA or ECDSA certificate?
if(cert->type == TLS_CERT_RSA_SIGN ||
cert->type == TLS_CERT_DSS_SIGN ||
cert->type == TLS_CERT_ECDSA_SIGN)
{
//Check whether the peer has provided a list of supported hash/signature
//algorithm pairs
if(supportedSignAlgos != NULL)
{
TlsHashAlgo hashAlgoId;
//Process the list and select the relevant signature algorithm
p = supportedSignAlgos->value;
//Get the number of hash/signature algorithm pairs present in the list
n = ntohs(supportedSignAlgos->length) / sizeof(TlsSignHashAlgo);
//The hash algorithm to be used when generating signatures must be
//one of those present in the list
for(i = 0; i < n; i++)
{
//Reset the hash algorithm identifier to its default value
hashAlgoId = TLS_HASH_ALGO_NONE;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA signature scheme?
if(cert->type == TLS_CERT_RSA_SIGN &&
p[i].signature == 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)
{
//Select current hash algorithm
hashAlgoId = (TlsHashAlgo) p[i].hash;
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSA-PSS signature scheme?
if(cert->type == TLS_CERT_RSA_SIGN &&
p[i].hash == TLS_HASH_ALGO_INTRINSIC)
{
//TLS 1.2 and TLS 1.3 support RSASSA-PSS signature schemes
if(context->version >= TLS_VERSION_1_2)
{
//Check RSA-PSS signature scheme
if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256)
{
//RSASSA-PSS RSAE signature scheme with SHA-256
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
else if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384)
{
//RSASSA-PSS RSAE signature scheme with SHA-384
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
else if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512)
{
//RSASSA-PSS RSAE signature scheme with SHA-512
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
else
{
//Just for sanity
}
}
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA signature scheme?
if(cert->type == TLS_CERT_DSS_SIGN &&
p[i].signature == TLS_SIGN_ALGO_DSA)
{
//TLS 1.3 removes support for DSA certificates
if(context->version <= TLS_VERSION_1_2)
{
//Select current hash algorithm
hashAlgoId = (TlsHashAlgo) p[i].hash;
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(cert->type == TLS_CERT_ECDSA_SIGN &&
p[i].signature == TLS_SIGN_ALGO_ECDSA)
{
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//Select current hash algorithm
hashAlgoId = (TlsHashAlgo) p[i].hash;
}
else
{
//Check elliptic curve and hash algorithm
if(cert->namedCurve == TLS_GROUP_SECP256R1 &&
p[i].hash == TLS_HASH_ALGO_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
else if(cert->namedCurve == TLS_GROUP_SECP384R1 &&
p[i].hash == TLS_HASH_ALGO_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
else if(cert->namedCurve == TLS_GROUP_SECP521R1 &&
p[i].hash == TLS_HASH_ALGO_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
else
{
//Just for sanity
}
}
}
else
#endif
//Unknown signature scheme?
{
//Just for sanity
}
//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(context->version == TLS_VERSION_1_3 ||
context->entity == TLS_CONNECTION_END_SERVER ||
hashAlgoId == TLS_HASH_ALGO_SHA1 ||
hashAlgo == context->cipherSuite.prfHashAlgo)
{
//The signature algorithm is acceptable
context->signAlgo = (TlsSignatureAlgo) p[i].signature;
context->signHashAlgo = (TlsHashAlgo) hashAlgoId;
break;
}
}
}
}
else
{
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//Select the default hash algorithm to be used when generating RSA,
//DSA or ECDSA signatures
if(tlsGetHashAlgo(TLS_HASH_ALGO_SHA1) != NULL)
{
//Select SHA-1 hash algorithm
context->signAlgo = cert->signAlgo;
context->signHashAlgo = TLS_HASH_ALGO_SHA1;
}
else if(tlsGetHashAlgo(TLS_HASH_ALGO_SHA256) != NULL)
{
//Select SHA-256 hash algorithm
context->signAlgo = cert->signAlgo;
context->signHashAlgo = TLS_HASH_ALGO_SHA256;
}
else if(tlsGetHashAlgo(TLS_HASH_ALGO_SHA384) != NULL)
{
//Select SHA-384 hash algorithm
context->signAlgo = cert->signAlgo;
context->signHashAlgo = TLS_HASH_ALGO_SHA384;
}
else if(tlsGetHashAlgo(TLS_HASH_ALGO_SHA512) != NULL)
{
//Select SHA-512 hash algorithm
context->signAlgo = cert->signAlgo;
context->signHashAlgo = TLS_HASH_ALGO_SHA512;
}
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 and TLS 1.3 support RSASSA-PSS signature schemes
if(context->version >= TLS_VERSION_1_2)
{
//Check whether the peer has provided a list of supported hash/signature
//algorithm pairs
if(supportedSignAlgos != NULL)
{
TlsHashAlgo hashAlgoId;
//Process the list and select the relevant signature algorithm
p = supportedSignAlgos->value;
//Get the number of hash/signature algorithm pairs present in the list
n = ntohs(supportedSignAlgos->length) / sizeof(TlsSignHashAlgo);
//The hash algorithm to be used when generating signatures must be
//one of those present in the list
for(i = 0; i < n; i++)
{
//The hashing is intrinsic to the signature algorithm
if(p[i].hash == TLS_HASH_ALGO_INTRINSIC)
{
//Check signature scheme
if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA256;
}
else if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA384;
}
else if(p[i].signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgoId = TLS_HASH_ALGO_SHA512;
}
else
{
//Invalid signature scheme
hashAlgoId = TLS_HASH_ALGO_NONE;
}
//Check whether the hash algorithm is supported
if(tlsGetHashAlgo(hashAlgoId) != NULL)
{
//Acceptable hash algorithm found
context->signAlgo = (TlsSignatureAlgo) p[i].signature;
context->signHashAlgo = (TlsHashAlgo) p[i].hash;
break;
}
}
}
}
}
}
else
#endif
#if (TLS_EDDSA_SIGN_SUPPORT == ENABLED)
//EdDSA certificate?
if(cert->type == TLS_CERT_ED25519_SIGN ||
cert->type == TLS_CERT_ED448_SIGN)
{
//TLS 1.2 or TLS 1.3 currently selected?
if((context->version >= TLS_VERSION_1_2 &&
context->entity == TLS_CONNECTION_END_SERVER) ||
(context->version >= TLS_VERSION_1_3 &&
context->entity == TLS_CONNECTION_END_CLIENT))
{
//Ed25519 and Ed448 are used in PureEdDSA mode, without pre-hashing
context->signAlgo = cert->signAlgo;
context->signHashAlgo = TLS_HASH_ALGO_INTRINSIC;
}
}
else
#endif
//Unsupported signature algorithm?
{
//Just for sanity
}
//If no acceptable choices are presented, return an error
if(context->signAlgo != TLS_SIGN_ALGO_ANONYMOUS &&
context->signHashAlgo != TLS_HASH_ALGO_NONE)
{
error = NO_ERROR;
}
//Return status code
return error;
}
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
/**
* @brief Digital signature generation(TLS 1.0 or TLS 1.1)
* @param[in] context Pointer to the TLS context
* @param[out] p Buffer where to store the digitally-signed element
* @param[out] length Length of the digitally-signed element
* @return Error code
**/
error_t tlsGenerateSignature(TlsContext *context, uint8_t *p,
size_t *length)
{
error_t error;
size_t n;
TlsDigitalSignature *signature;
//The digitally-signed element does not convey the signature algorithm
//to use, and hence implementations need to inspect the certificate to
//find out the signature algorithm to use
signature = (TlsDigitalSignature *) p;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA certificate?
if(context->cert->type == TLS_CERT_RSA_SIGN)
{
RsaPrivateKey privateKey;
//Initialize RSA private key
rsaInitPrivateKey(&privateKey);
//Digest all the handshake messages starting at ClientHello using MD5
error = tlsFinalizeTranscriptHash(context, MD5_HASH_ALGO,
context->transcriptMd5Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Digest all the handshake messages starting at ClientHello using SHA-1
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "",
context->clientVerifyData + MD5_DIGEST_SIZE);
}
//Check status code
if(!error)
{
//Decode the PEM structure that holds the RSA private key
error = pemImportRsaPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, &privateKey);
}
//Check status code
if(!error)
{
//Generate an RSA signature using the client's private key
error = tlsGenerateRsaSignature(&privateKey,
context->clientVerifyData, signature->value, &n);
}
//Release previously allocated resources
rsaFreePrivateKey(&privateKey);
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA certificate?
if(context->cert->type == TLS_CERT_DSS_SIGN)
{
//Digest all the handshake messages starting at ClientHello
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Generate a DSA signature using the client's private key
error = tlsGenerateDsaSignature(context, context->clientVerifyData,
SHA1_DIGEST_SIZE, signature->value, &n);
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA certificate?
if(context->cert->type == TLS_CERT_ECDSA_SIGN)
{
//Digest all the handshake messages starting at ClientHello
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Generate an ECDSA signature using the client's private key
error = tlsGenerateEcdsaSignature(context, context->clientVerifyData,
SHA1_DIGEST_SIZE, signature->value, &n);
}
}
else
#endif
//Invalid certificate?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
//Total length of the digitally-signed element
*length = sizeof(TlsDigitalSignature) + n;
}
//Return status code
return error;
}
/**
* @brief Digital signature verification (TLS 1.0 and TLS 1.1)
* @param[in] context Pointer to the TLS context
* @param[in] p Pointer to the digitally-signed element to be verified
* @param[in] length Length of the digitally-signed element
* @return Error code
**/
error_t tlsVerifySignature(TlsContext *context, const uint8_t *p,
size_t length)
{
error_t error;
const TlsDigitalSignature *signature;
//The digitally-signed element does not convey the signature algorithm
//to use, and hence implementations need to inspect the certificate to
//find out the signature algorithm to use
signature = (TlsDigitalSignature *) p;
//Check the length of the digitally-signed element
if(length < sizeof(TlsDigitalSignature))
return ERROR_DECODING_FAILED;
if(length != (sizeof(TlsDigitalSignature) + ntohs(signature->length)))
return ERROR_DECODING_FAILED;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA certificate?
if(context->peerCertType == TLS_CERT_RSA_SIGN)
{
//Digest all the handshake messages starting at ClientHello using MD5
error = tlsFinalizeTranscriptHash(context, MD5_HASH_ALGO,
context->transcriptMd5Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Digest all the handshake messages starting at ClientHello using SHA-1
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "",
context->clientVerifyData + MD5_DIGEST_SIZE);
}
//Check status code
if(!error)
{
//Verify RSA signature using client's public key
error = tlsVerifyRsaSignature(&context->peerRsaPublicKey,
context->clientVerifyData, signature->value,
ntohs(signature->length));
}
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA certificate?
if(context->peerCertType == TLS_CERT_DSS_SIGN)
{
//Digest all the handshake messages starting at ClientHello
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Verify DSA signature using client's public key
error = tlsVerifyDsaSignature(context, context->clientVerifyData,
SHA1_DIGEST_SIZE, signature->value, ntohs(signature->length));
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA certificate?
if(context->peerCertType == TLS_CERT_ECDSA_SIGN)
{
//Digest all the handshake messages starting at ClientHello
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
//Check status code
if(!error)
{
//Verify ECDSA signature using client's public key
error = tlsVerifyEcdsaSignature(context, context->clientVerifyData,
SHA1_DIGEST_SIZE, signature->value, ntohs(signature->length));
}
}
else
#endif
//Invalid signature algorithm?
{
//Report an error
error = ERROR_INVALID_SIGNATURE;
}
//Return status code
return error;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
/**
* @brief Digital signature generation(TLS 1.2)
* @param[in] context Pointer to the TLS context
* @param[out] p Buffer where to store the digitally-signed element
* @param[out] length Length of the digitally-signed element
* @return Error code
**/
error_t tls12GenerateSignature(TlsContext *context, uint8_t *p,
size_t *length)
{
error_t error;
size_t n;
Tls12DigitalSignature *signature;
const HashAlgo *hashAlgo;
//Point to the digitally-signed element
signature = (Tls12DigitalSignature *) p;
//Retrieve the hash algorithm used for signing
if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256)
{
//The hashing is intrinsic to the signature algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384)
{
//The hashing is intrinsic to the signature algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
//The hashing is intrinsic to the signature algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Select the relevant hash algorithm
hashAlgo = tlsGetHashAlgo(context->signHashAlgo);
}
//Digest all the handshake messages starting at ClientHello
if(hashAlgo == SHA1_HASH_ALGO)
{
//Use SHA-1 hash algorithm
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
}
else if(hashAlgo == context->cipherSuite.prfHashAlgo)
{
//Use PRF hash algorithm (SHA-256 or SHA-384)
error = tlsFinalizeTranscriptHash(context, hashAlgo,
context->transcriptHashContext, "", context->clientVerifyData);
}
else
{
//The specified hash algorithm is not supported
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Handshake message hash successfully computed?
if(!error)
{
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSASSA-PKCS1-v1_5 signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_RSA)
{
RsaPrivateKey privateKey;
//Initialize RSA private key
rsaInitPrivateKey(&privateKey);
//Set the relevant signature algorithm
signature->algorithm.signature = TLS_SIGN_ALGO_RSA;
signature->algorithm.hash = context->signHashAlgo;
//Decode the PEM structure that holds the RSA private key
error = pemImportRsaPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, &privateKey);
//Check status code
if(!error)
{
//Generate RSA signature (RSASSA-PKCS1-v1_5 signature scheme)
error = rsassaPkcs1v15Sign(&privateKey, hashAlgo,
context->clientVerifyData, signature->value, &n);
}
//Release previously allocated resources
rsaFreePrivateKey(&privateKey);
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSASSA-PSS signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
RsaPrivateKey privateKey;
//Initialize RSA private key
rsaInitPrivateKey(&privateKey);
//Set the relevant signature algorithm
signature->algorithm.signature = context->signAlgo;
signature->algorithm.hash = TLS_HASH_ALGO_INTRINSIC;
//Decode the PEM structure that holds the RSA private key
error = pemImportRsaPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, &privateKey);
//Check status code
if(!error)
{
//Generate RSA signature (RSASSA-PSS signature scheme)
error = rsassaPssSign(context->prngAlgo, context->prngContext,
&privateKey, hashAlgo, hashAlgo->digestSize,
context->clientVerifyData, signature->value, &n);
}
//Release previously allocated resources
rsaFreePrivateKey(&privateKey);
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_DSA)
{
//Set the relevant signature algorithm
signature->algorithm.signature = TLS_SIGN_ALGO_DSA;
signature->algorithm.hash = context->signHashAlgo;
//Generate a DSA signature using the client's private key
error = tlsGenerateDsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, &n);
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_ECDSA)
{
//Set the relevant signature algorithm
signature->algorithm.signature = TLS_SIGN_ALGO_ECDSA;
signature->algorithm.hash = context->signHashAlgo;
//Generate an ECDSA signature using the client's private key
error = tlsGenerateEcdsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, &n);
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
//Total length of the digitally-signed element
*length = sizeof(Tls12DigitalSignature) + n;
}
//Return status code
return error;
}
/**
* @brief Digital signature verification (TLS 1.2)
* @param[in] context Pointer to the TLS context
* @param[in] p Pointer to the digitally-signed element to be verified
* @param[in] length Length of the digitally-signed element
* @return Error code
**/
error_t tls12VerifySignature(TlsContext *context, const uint8_t *p,
size_t length)
{
error_t error;
const Tls12DigitalSignature *signature;
const HashAlgo *hashAlgo;
//Point to the digitally-signed element
signature = (Tls12DigitalSignature *) p;
//Check the length of the digitally-signed element
if(length < sizeof(Tls12DigitalSignature))
return ERROR_DECODING_FAILED;
if(length != (sizeof(Tls12DigitalSignature) + ntohs(signature->length)))
return ERROR_DECODING_FAILED;
//Retrieve the hash algorithm used for signing
if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256)
{
//The hashing is intrinsic to the signature algorithm
if(signature->algorithm.hash == TLS_HASH_ALGO_INTRINSIC)
{
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else
{
hashAlgo = NULL;
}
}
else if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384)
{
//The hashing is intrinsic to the signature algorithm
if(signature->algorithm.hash == TLS_HASH_ALGO_INTRINSIC)
{
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else
{
hashAlgo = NULL;
}
}
else if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
//The hashing is intrinsic to the signature algorithm
if(signature->algorithm.hash == TLS_HASH_ALGO_INTRINSIC)
{
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
hashAlgo = NULL;
}
}
else
{
//This field indicates the hash algorithm that is used
hashAlgo = tlsGetHashAlgo(signature->algorithm.hash);
}
//Digest all the handshake messages starting at ClientHello
if(hashAlgo == SHA1_HASH_ALGO)
{
//Use SHA-1 hash algorithm
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", context->clientVerifyData);
}
else if(hashAlgo == context->cipherSuite.prfHashAlgo)
{
//Use PRF hash algorithm (SHA-256 or SHA-384)
error = tlsFinalizeTranscriptHash(context, hashAlgo,
context->transcriptHashContext, "", context->clientVerifyData);
}
else
{
//The specified hash algorithm is not supported
error = ERROR_INVALID_SIGNATURE;
}
//Check status code
if(!error)
{
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSASSA-PKCS1-v1_5 signature scheme?
if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA &&
context->peerCertType == TLS_CERT_RSA_SIGN)
{
//Verify RSA signature (RSASSA-PKCS1-v1_5 signature scheme)
error = rsassaPkcs1v15Verify(&context->peerRsaPublicKey,
hashAlgo, context->clientVerifyData, signature->value,
ntohs(signature->length));
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSASSA-PSS signature scheme (with public key OID rsaEncryption)?
if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_RSA_SIGN)
{
//Verify RSA signature (RSASSA-PSS signature scheme)
error = rsassaPssVerify(&context->peerRsaPublicKey, hashAlgo,
hashAlgo->digestSize, context->clientVerifyData,
signature->value, ntohs(signature->length));
}
else
{
//Invalid certificate
error = ERROR_INVALID_SIGNATURE;
}
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSASSA-PSS signature scheme (with public key OID RSASSA-PSS)?
if(signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384 ||
signature->algorithm.signature == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_RSA_PSS_SIGN)
{
//Verify RSA signature (RSASSA-PSS signature scheme)
error = rsassaPssVerify(&context->peerRsaPublicKey, hashAlgo,
hashAlgo->digestSize, context->clientVerifyData,
signature->value, ntohs(signature->length));
}
else
{
//Invalid certificate
error = ERROR_INVALID_SIGNATURE;
}
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA signature scheme?
if(signature->algorithm.signature == TLS_SIGN_ALGO_DSA &&
context->peerCertType == TLS_CERT_DSS_SIGN)
{
//Verify DSA signature using client's public key
error = tlsVerifyDsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, ntohs(signature->length));
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(signature->algorithm.signature == TLS_SIGN_ALGO_ECDSA &&
context->peerCertType == TLS_CERT_ECDSA_SIGN)
{
//Verify ECDSA signature using client's public key
error = tlsVerifyEcdsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, ntohs(signature->length));
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_INVALID_SIGNATURE;
}
}
//Return status code
return error;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
/**
* @brief Generate RSA signature (TLS 1.0 and TLS 1.1)
* @param[in] key Signer's RSA private key
* @param[in] digest Digest of the message to be signed
* @param[out] signature Resulting signature
* @param[out] signatureLen Length of the resulting signature
* @return Error code
**/
error_t tlsGenerateRsaSignature(const RsaPrivateKey *key,
const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
{
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
error_t error;
size_t k;
size_t paddingLen;
uint8_t *em;
Mpi m;
Mpi s;
//Debug message
TRACE_DEBUG("RSA signature generation...\r\n");
TRACE_DEBUG(" Modulus:\r\n");
TRACE_DEBUG_MPI(" ", &key->n);
TRACE_DEBUG(" Public exponent:\r\n");
TRACE_DEBUG_MPI(" ", &key->e);
TRACE_DEBUG(" Private exponent:\r\n");
TRACE_DEBUG_MPI(" ", &key->d);
TRACE_DEBUG(" Prime 1:\r\n");
TRACE_DEBUG_MPI(" ", &key->p);
TRACE_DEBUG(" Prime 2:\r\n");
TRACE_DEBUG_MPI(" ", &key->q);
TRACE_DEBUG(" Prime exponent 1:\r\n");
TRACE_DEBUG_MPI(" ", &key->dp);
TRACE_DEBUG(" Prime exponent 2:\r\n");
TRACE_DEBUG_MPI(" ", &key->dq);
TRACE_DEBUG(" Coefficient:\r\n");
TRACE_DEBUG_MPI(" ", &key->qinv);
TRACE_DEBUG(" Message digest:\r\n");
TRACE_DEBUG_ARRAY(" ", digest, MD5_DIGEST_SIZE + SHA1_DIGEST_SIZE);
//Initialize multiple-precision integers
mpiInit(&m);
mpiInit(&s);