-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_server_misc.c
2115 lines (1869 loc) · 67.9 KB
/
tls_server_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_server_misc.c
* @brief Helper functions for TLS server
*
* @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_server.h"
#include "tls_server_extensions.h"
#include "tls_server_misc.h"
#include "tls_common.h"
#include "tls_extensions.h"
#include "tls_certificate.h"
#include "tls_sign_generate.h"
#include "tls_sign_misc.h"
#include "tls_cache.h"
#include "tls_ffdhe.h"
#include "tls_record.h"
#include "tls_misc.h"
#include "pkix/pem_import.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED)
/**
* @brief Format PSK identity hint
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the PSK identity hint
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatPskIdentityHint(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n;
TlsPskIdentityHint *pskIdentityHint;
//Point to the PSK identity hint
pskIdentityHint = (TlsPskIdentityHint *) p;
#if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//Any PSK identity hint defined?
if(context->pskIdentityHint != NULL)
{
//Determine the length of the PSK identity hint
n = osStrlen(context->pskIdentityHint);
//Copy PSK identity hint
osMemcpy(pskIdentityHint->value, context->pskIdentityHint, n);
}
else
#endif
{
//No PSK identity hint is provided
n = 0;
}
//The PSK identity hint is preceded by a 2-byte length field
pskIdentityHint->length = htons(n);
//Total number of bytes that have been written
*written = sizeof(TlsPskIdentityHint) + n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format server's key exchange parameters
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the server's key exchange parameters
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerKeyParams(TlsContext *context,
uint8_t *p, size_t *written)
{
error_t error;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//Initialize status code
error = NO_ERROR;
//Total number of bytes that have been written
*written = 0;
#if (TLS_DH_ANON_KE_SUPPORT == ENABLED || TLS_DHE_RSA_KE_SUPPORT == ENABLED || \
TLS_DHE_DSS_KE_SUPPORT == ENABLED || TLS_DHE_PSK_KE_SUPPORT == ENABLED)
//Diffie-Hellman key exchange method?
if(context->keyExchMethod == TLS_KEY_EXCH_DH_ANON ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK)
{
size_t n;
#if (TLS_FFDHE_SUPPORT == ENABLED)
const TlsFfdheGroup *ffdheGroup;
//Get the FFDHE parameters that match the specified named group
ffdheGroup = tlsGetFfdheGroup(context, context->namedGroup);
//Valid FFDHE group?
if(ffdheGroup != NULL)
{
//Load FFDHE parameters
error = tlsLoadFfdheParameters(&context->dhContext.params, ffdheGroup);
}
#endif
//Check status code
if(!error)
{
//Generate an ephemeral key pair
error = dhGenerateKeyPair(&context->dhContext, context->prngAlgo,
context->prngContext);
}
//Check status code
if(!error)
{
//Debug message
TRACE_DEBUG("Diffie-Hellman parameters:\r\n");
TRACE_DEBUG(" Prime modulus:\r\n");
TRACE_DEBUG_MPI(" ", &context->dhContext.params.p);
TRACE_DEBUG(" Generator:\r\n");
TRACE_DEBUG_MPI(" ", &context->dhContext.params.g);
TRACE_DEBUG(" Server public value:\r\n");
TRACE_DEBUG_MPI(" ", &context->dhContext.ya);
//Encode the prime modulus to an opaque vector
error = tlsWriteMpi(&context->dhContext.params.p, p, &n);
}
//Check status code
if(!error)
{
//Advance data pointer
p += n;
//Total number of bytes that have been written
*written += n;
//Encode the generator to an opaque vector
error = tlsWriteMpi(&context->dhContext.params.g, p, &n);
}
//Check status code
if(!error)
{
//Advance data pointer
p += n;
//Total number of bytes that have been written
*written += n;
//Encode the server's public value to an opaque vector
error = tlsWriteMpi(&context->dhContext.ya, p, &n);
}
//Check status code
if(!error)
{
//Advance data pointer
p += n;
//Adjust the length of the key exchange parameters
*written += n;
}
}
else
#endif
#if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//ECDH key exchange method?
if(context->keyExchMethod == TLS_KEY_EXCH_ECDH_ANON ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
{
size_t n;
const EcCurveInfo *curveInfo;
//Retrieve the elliptic curve to be used
curveInfo = tlsGetCurveInfo(context, context->namedGroup);
//Make sure the elliptic curve is supported
if(curveInfo != NULL)
{
//Load EC domain parameters
error = ecLoadDomainParameters(&context->ecdhContext.params,
curveInfo);
//Check status code
if(!error)
{
#if (TLS_ECC_CALLBACK_SUPPORT == ENABLED)
//Any registered callback?
if(context->ecdhCallback != NULL)
{
//Invoke user callback function
error = context->ecdhCallback(context);
}
else
#endif
{
//No callback function defined
error = ERROR_UNSUPPORTED_ELLIPTIC_CURVE;
}
//Check status code
if(error == ERROR_UNSUPPORTED_ELLIPTIC_CURVE)
{
//Generate an ephemeral key pair
error = ecdhGenerateKeyPair(&context->ecdhContext,
context->prngAlgo, context->prngContext);
}
}
//Check status code
if(!error)
{
//Debug message
TRACE_DEBUG(" Server public key X:\r\n");
TRACE_DEBUG_MPI(" ", &context->ecdhContext.qa.q.x);
TRACE_DEBUG(" Server public key Y:\r\n");
TRACE_DEBUG_MPI(" ", &context->ecdhContext.qa.q.y);
//Set the type of the elliptic curve domain parameters
*p = TLS_EC_CURVE_TYPE_NAMED_CURVE;
//Advance data pointer
p += sizeof(uint8_t);
//Total number of bytes that have been written
*written += sizeof(uint8_t);
//Write elliptic curve identifier
STORE16BE(context->namedGroup, p);
//Advance data pointer
p += sizeof(uint16_t);
//Total number of bytes that have been written
*written += sizeof(uint16_t);
//Write server's public key
error = tlsWriteEcPoint(&context->ecdhContext.params,
&context->ecdhContext.qa.q, p, &n);
}
//Check status code
if(!error)
{
//Advance data pointer
p +=n;
//Total number of bytes that have been written
*written += n;
}
}
else
{
//The specified elliptic curve is not supported
error = ERROR_FAILURE;
}
}
else
#endif
//Any other exchange method?
{
//It is not legal to send the ServerKeyExchange message when a key
//exchange method other than DHE_DSS, DHE_RSA, DH_anon, ECDHE_RSA,
//ECDHE_ECDSA or ECDH_anon is selected
error = ERROR_FAILURE;
}
#else
//Not implemented
error = ERROR_NOT_IMPLEMENTED;
#endif
//Return status code
return error;
}
/**
* @brief Sign server's key exchange parameters (TLS 1.0 and TLS 1.1)
* @param[in] context Pointer to the TLS context
* @param[in] signature Output stream where to write the digital signature
* @param[in] params Pointer to the server's key exchange parameters
* @param[in] paramsLen Length of the server's key exchange parameters
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsGenerateServerKeySignature(TlsContext *context,
TlsDigitalSignature *signature, const uint8_t *params,
size_t paramsLen, size_t *written)
{
error_t error;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//Initialize status code
error = NO_ERROR;
//Total number of bytes that have been written
*written = 0;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA certificate?
if(context->cert->type == TLS_CERT_RSA_SIGN)
{
Md5Context *md5Context;
Sha1Context *sha1Context;
RsaPrivateKey privateKey;
//Initialize RSA private key
rsaInitPrivateKey(&privateKey);
//Allocate a memory buffer to hold the MD5 context
md5Context = tlsAllocMem(sizeof(Md5Context));
//Successful memory allocation?
if(md5Context != NULL)
{
//Compute MD5(ClientHello.random + ServerHello.random +
//ServerKeyExchange.params)
md5Init(md5Context);
md5Update(md5Context, context->clientRandom, TLS_RANDOM_SIZE);
md5Update(md5Context, context->serverRandom, TLS_RANDOM_SIZE);
md5Update(md5Context, params, paramsLen);
md5Final(md5Context, context->serverVerifyData);
//Release previously allocated memory
tlsFreeMem(md5Context);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Check status code
if(!error)
{
//Allocate a memory buffer to hold the SHA-1 context
sha1Context = tlsAllocMem(sizeof(Sha1Context));
//Successful memory allocation?
if(sha1Context != NULL)
{
//Compute SHA(ClientHello.random + ServerHello.random +
//ServerKeyExchange.params)
sha1Init(sha1Context);
sha1Update(sha1Context, context->clientRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, context->serverRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, params, paramsLen);
sha1Final(sha1Context, context->serverVerifyData + MD5_DIGEST_SIZE);
//Release previously allocated memory
tlsFreeMem(sha1Context);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
}
//Check status code
if(!error)
{
//Decode the PEM structure that holds the RSA private key
error = pemImportRsaPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, context->cert->password, &privateKey);
}
//Check status code
if(!error)
{
//Sign the key exchange parameters using RSA
error = tlsGenerateRsaSignature(&privateKey,
context->serverVerifyData, signature->value, written);
}
//Release previously allocated resources
rsaFreePrivateKey(&privateKey);
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA certificate?
if(context->cert->type == TLS_CERT_DSS_SIGN)
{
Sha1Context *sha1Context;
//Allocate a memory buffer to hold the SHA-1 context
sha1Context = tlsAllocMem(sizeof(Sha1Context));
//Successful memory allocation?
if(sha1Context != NULL)
{
//Compute SHA(ClientHello.random + ServerHello.random +
//ServerKeyExchange.params)
sha1Init(sha1Context);
sha1Update(sha1Context, context->clientRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, context->serverRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, params, paramsLen);
sha1Final(sha1Context, context->serverVerifyData);
//Release previously allocated memory
tlsFreeMem(sha1Context);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Check status code
if(!error)
{
//Sign the key exchange parameters using DSA
error = tlsGenerateDsaSignature(context, context->serverVerifyData,
SHA1_DIGEST_SIZE, signature->value, written);
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA certificate?
if(context->cert->type == TLS_CERT_ECDSA_SIGN)
{
Sha1Context *sha1Context;
//Allocate a memory buffer to hold the SHA-1 context
sha1Context = tlsAllocMem(sizeof(Sha1Context));
//Successful memory allocation?
if(sha1Context != NULL)
{
//Compute SHA(ClientHello.random + ServerHello.random +
//ServerKeyExchange.params)
sha1Init(sha1Context);
sha1Update(sha1Context, context->clientRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, context->serverRandom, TLS_RANDOM_SIZE);
sha1Update(sha1Context, params, paramsLen);
sha1Final(sha1Context, context->serverVerifyData);
//Release previously allocated memory
tlsFreeMem(sha1Context);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Check status code
if(!error)
{
//Sign the key exchange parameters using ECDSA
error = tlsGenerateEcdsaSignature(context, context->serverVerifyData,
SHA1_DIGEST_SIZE, signature->value, written);
}
}
else
#endif
//Invalid certificate?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
//Fix the length of the digitally-signed element
signature->length = htons(*written);
//Adjust the length of the signature
*written += sizeof(TlsDigitalSignature);
}
#else
//Not implemented
error = ERROR_NOT_IMPLEMENTED;
#endif
//Return status code
return error;
}
/**
* @brief Sign server's key exchange parameters (TLS 1.2)
* @param[in] context Pointer to the TLS context
* @param[in] signature Output stream where to write the digital signature
* @param[in] params Pointer to the server's key exchange parameters
* @param[in] paramsLen Length of the server's key exchange parameters
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tls12GenerateServerKeySignature(TlsContext *context,
Tls12DigitalSignature *signature, const uint8_t *params,
size_t paramsLen, size_t *written)
{
error_t error;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//Initialize status code
error = NO_ERROR;
//Total number of bytes that have been written
*written = 0;
//The algorithm field specifies the signature scheme
signature->algorithm = htons(context->signScheme);
#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 signature scheme?
if(TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_RSA ||
TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_DSA ||
TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_ECDSA ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
const HashAlgo *hashAlgo;
HashContext *hashContext;
//Retrieve the hash algorithm used for signing
if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256)
{
//The hashing is intrinsic to the signature algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384)
{
//The hashing is intrinsic to the signature algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_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(TLS_HASH_ALGO(context->signScheme));
}
//Make sure the hash algorithm is supported
if(hashAlgo != NULL)
{
//Allocate a memory buffer to hold the hash context
hashContext = tlsAllocMem(hashAlgo->contextSize);
//Successful memory allocation?
if(hashContext != NULL)
{
//Compute hash(ClientHello.random + ServerHello.random +
//ServerKeyExchange.params)
hashAlgo->init(hashContext);
hashAlgo->update(hashContext, context->clientRandom, TLS_RANDOM_SIZE);
hashAlgo->update(hashContext, context->serverRandom, TLS_RANDOM_SIZE);
hashAlgo->update(hashContext, params, paramsLen);
hashAlgo->final(hashContext, NULL);
#if (TLS_RSA_SIGN_SUPPORT == ENABLED)
//RSA signature scheme?
if(TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_RSA)
{
//Sign the key exchange parameters using RSA
error = tlsGenerateRsaPkcs1Signature(context, hashAlgo,
hashContext->digest, signature->value, written);
}
else
#endif
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSA-PSS signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Sign the key exchange parameters using RSA-PSS
error = tlsGenerateRsaPssSignature(context, hashAlgo,
hashContext->digest, signature->value, written);
}
else
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//DSA signature scheme?
if(TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_DSA)
{
//Sign the key exchange parameters using DSA
error = tlsGenerateDsaSignature(context, hashContext->digest,
hashAlgo->digestSize, signature->value, written);
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(TLS_SIGN_ALGO(context->signScheme) == TLS_SIGN_ALGO_ECDSA)
{
//Sign the key exchange parameters using ECDSA
error = tlsGenerateEcdsaSignature(context, hashContext->digest,
hashAlgo->digestSize, signature->value, written);
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Release previously allocated memory
tlsFreeMem(hashContext);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
}
else
{
//Hash algorithm not supported
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
else
#endif
#if (TLS_ED25519_SIGN_SUPPORT == ENABLED)
//Ed25519 signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_ED25519)
{
DataChunk messageChunks[4];
//Data to be signed is run through the EdDSA algorithm without pre-hashing
messageChunks[0].buffer = context->clientRandom;
messageChunks[0].length = TLS_RANDOM_SIZE;
messageChunks[1].buffer = context->serverRandom;
messageChunks[1].length = TLS_RANDOM_SIZE;
messageChunks[2].buffer = params;
messageChunks[2].length = paramsLen;
messageChunks[3].buffer = NULL;
messageChunks[3].length = 0;
//Sign the key exchange parameters using Ed25519
error = tlsGenerateEd25519Signature(context, messageChunks,
signature->value, written);
}
else
#endif
#if (TLS_ED448_SIGN_SUPPORT == ENABLED)
//Ed448 signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_ED448)
{
DataChunk messageChunks[4];
//Data to be signed is run through the EdDSA algorithm without pre-hashing
messageChunks[0].buffer = context->clientRandom;
messageChunks[0].length = TLS_RANDOM_SIZE;
messageChunks[1].buffer = context->serverRandom;
messageChunks[1].length = TLS_RANDOM_SIZE;
messageChunks[2].buffer = params;
messageChunks[2].length = paramsLen;
messageChunks[3].buffer = NULL;
messageChunks[3].length = 0;
//Sign the key exchange parameters using Ed448
error = tlsGenerateEd448Signature(context, messageChunks,
signature->value, written);
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
//Fix the length of the digitally-signed element
signature->length = htons(*written);
//Adjust the length of the message
*written += sizeof(Tls12DigitalSignature);
}
#else
//Not implemented
error = ERROR_NOT_IMPLEMENTED;
#endif
//Return status code
return error;
}
/**
* @brief Check whether the ClientHello includes any SCSV cipher suites
* @param[in] context Pointer to the TLS context
* @param[in] cipherSuites List of cipher suites offered by the client
* @return Error code
**/
error_t tlsCheckSignalingCipherSuiteValues(TlsContext *context,
const TlsCipherSuites *cipherSuites)
{
error_t error;
uint_t i;
uint_t n;
uint16_t serverVersion;
//Initialize status code
error = NO_ERROR;
//Get the highest version supported by the implementation (legacy version)
serverVersion = MIN(context->versionMax, TLS_VERSION_1_2);
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Translate TLS version into DTLS version
serverVersion = dtlsTranslateVersion(serverVersion);
}
#endif
//Get the number of cipher suite identifiers present in the list
n = ntohs(cipherSuites->length) / 2;
//Debug message
TRACE_DEBUG("Cipher suites:\r\n");
//Loop through the list of cipher suite identifiers
for(i = 0; i < n; i++)
{
//Debug message
TRACE_DEBUG(" 0x%04" PRIX16 " (%s)\r\n", ntohs(cipherSuites->value[i]),
tlsGetCipherSuiteName(ntohs(cipherSuites->value[i])));
#if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
//TLS_EMPTY_RENEGOTIATION_INFO_SCSV signaling cipher suite?
if(ntohs(cipherSuites->value[i]) == TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
{
//Initial handshake?
if(context->clientVerifyDataLen == 0)
{
//Set the secure_renegotiation flag to TRUE
context->secureRenegoFlag = TRUE;
}
//Secure renegotiation?
else
{
//When a ClientHello is received, the server must verify that it
//does not contain the TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV. If
//the SCSV is present, the server must abort the handshake
error = ERROR_HANDSHAKE_FAILED;
break;
}
}
else
#endif
//TLS_FALLBACK_SCSV signaling cipher suite?
if(ntohs(cipherSuites->value[i]) == TLS_FALLBACK_SCSV)
{
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Test if the highest protocol version supported by the server is
//higher than the version indicated by the client
if(serverVersion < context->clientVersion)
{
//The server must respond with a fatal inappropriate_fallback alert
error = ERROR_INAPPROPRIATE_FALLBACK;
break;
}
}
else
#endif
//TLS protocol?
{
//Test if the highest protocol version supported by the server is
//higher than the version indicated by the client
if(serverVersion > context->clientVersion)
{
//The server must respond with a fatal inappropriate_fallback alert
error = ERROR_INAPPROPRIATE_FALLBACK;
break;
}
}
}
}
//Return status code
return error;
}
/**
* @brief Resume TLS session via session ID
* @param[in] context Pointer to the TLS context
* @param[in] sessionId Pointer to the session ID offered by the client
* @param[in] sessionIdLen Length of the session ID, in bytes
* @param[in] cipherSuites List of cipher suites offered by the client
* @param[in] extensions ClientHello extensions offered by the client
* @return Error code
**/
error_t tlsResumeStatefulSession(TlsContext *context, const uint8_t *sessionId,
size_t sessionIdLen, const TlsCipherSuites *cipherSuites,
const TlsHelloExtensions *extensions)
{
error_t error;
//Initialize status code
error = NO_ERROR;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2 && \
TLS_SESSION_RESUME_SUPPORT == ENABLED)
//Check whether session caching is supported
if(context->cache != NULL)
{
uint_t i;
uint_t n;
TlsSessionState *session;
//If the session ID was non-empty, the server will look in its session
//cache for a match
session = tlsFindCache(context->cache, sessionId, sessionIdLen);
//Matching session found?
if(session != NULL)
{
//Whenever a client already knows the highest protocol version known
//to a server (for example, when resuming a session), it should
//initiate the connection in that native protocol
if(session->version != context->version)
{
session = NULL;
}
}
//Matching session found?
if(session != NULL)
{
//Get the total number of cipher suites offered by the client
n = ntohs(cipherSuites->length) / 2;
//Loop through the list of cipher suite identifiers
for(i = 0; i < n; i++)
{
//Matching cipher suite?
if(ntohs(cipherSuites->value[i]) == session->cipherSuite)
{
break;
}
}
//If the cipher suite is not present in the list cipher suites offered
//by the client, the server must not perform the abbreviated handshake
if(i >= n)
{
session = NULL;
}
}
#if (TLS_SNI_SUPPORT == ENABLED)
//Matching session found?
if(session != NULL)
{
//ServerName extension found?
if(session->serverName != NULL && context->serverName != NULL)
{
//A server that implements this extension must not accept the
//request to resume the session if the ServerName extension contains
//a different name (refer to RFC 6066, section 3)
if(osStrcmp(session->serverName, context->serverName) != 0)
{
//Instead, the server proceeds with a full handshake to establish
//a new session
session = NULL;
}
}
else if(session->serverName == NULL && context->serverName == NULL)
{
//The ServerName extension is not present
}
else
{
//The server proceeds with a full handshake to establish a new
//session
session = NULL;
}
}
#endif
#if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
//Matching session found?
if(session != NULL)
{
//ExtendedMasterSecret extension found?
if(extensions->extendedMasterSecret != NULL)
{
//If the original session did not use the ExtendedMasterSecret
//extension but the new ClientHello contains the extension, then
//the server must not perform the abbreviated handshake
if(!session->extendedMasterSecret)
{
session = NULL;
}
}
}
#endif
//Check whether the server has decided to resume a previous session
if(session != NULL)
{
//Perform abbreviated handshake
context->resume = TRUE;
//Restore cached session parameters
error = tlsRestoreSessionId(context, session);
//Check status code
if(!error)
{
//Select the relevant cipher suite
error = tlsSelectCipherSuite(context, session->cipherSuite);
}
//Check status code
if(!error)
{
//Retrieve the type of the cipher suite presented by the client
context->cipherSuiteTypes = tlsGetCipherSuiteType(
session->cipherSuite);
}
}
else
{
//Perform a full handshake
context->resume = FALSE;
//Generate a new random session ID
error = tlsGenerateSessionId(context, 32);
}
}
else
#endif
{
//Perform a full handshake
context->resume = FALSE;
//The session cannot be resumed
context->sessionIdLen = 0;
}