-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_server.c
1773 lines (1548 loc) · 55.2 KB
/
tls_server.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.c
* @brief Handshake message processing (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.
*
* @section Description
*
* The TLS protocol provides communications security over the Internet. The
* protocol allows client/server applications to communicate in a way that
* is designed to prevent eavesdropping, tampering, or message forgery
*
* @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_handshake.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_sign_misc.h"
#include "tls_key_material.h"
#include "tls_transcript_hash.h"
#include "tls_cache.h"
#include "tls_ffdhe.h"
#include "tls_record.h"
#include "tls_misc.h"
#include "tls13_server.h"
#include "tls13_server_extensions.h"
#include "tls13_server_misc.h"
#include "dtls_record.h"
#include "dtls_misc.h"
#include "pkix/pem_import.h"
#include "pkix/x509_cert_parse.h"
#include "date_time.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED)
/**
* @brief Send ServerHello message
*
* The server will send this message in response to a ClientHello
* message when it was able to find an acceptable set of algorithms.
* If it cannot find such a match, it will respond with a handshake
* failure alert
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsSendServerHello(TlsContext *context)
{
error_t error;
size_t length;
TlsServerHello *message;
//Point to the buffer where to format the message
message = (TlsServerHello *) (context->txBuffer + context->txBufferLen);
//Generate the server random value using a cryptographically-safe
//pseudorandom number generator
error = tlsGenerateRandomValue(context, context->serverRandom);
//Check status code
if(!error)
{
//Format ServerHello message
error = tlsFormatServerHello(context, message, &length);
}
//Check status code
if(!error)
{
//Debug message
TRACE_INFO("Sending ServerHello message (%" PRIuSIZE " bytes)...\r\n", length);
TRACE_DEBUG_ARRAY(" ", message, length);
//Send handshake message
error = tlsSendHandshakeMessage(context, message, length,
TLS_TYPE_SERVER_HELLO);
}
//Check status code
if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
#if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
//Use abbreviated handshake?
if(context->resume)
{
//Derive session keys from the master secret
error = tlsGenerateSessionKeys(context);
//Key material successfully generated?
if(!error)
{
#if (TLS_TICKET_SUPPORT == ENABLED)
//The server uses a zero-length SessionTicket extension to
//indicate to the client that it will send a new session ticket
//using the NewSessionTicket handshake message
if(context->sessionTicketExtSent)
{
//Send a NewSessionTicket message to the client
tlsChangeState(context, TLS_STATE_NEW_SESSION_TICKET);
}
else
#endif
{
//At this point, both client and server must send ChangeCipherSpec
//messages and proceed directly to Finished messages
tlsChangeState(context, TLS_STATE_SERVER_CHANGE_CIPHER_SPEC);
}
}
}
else
#endif
{
//Perform a full handshake
tlsChangeState(context, TLS_STATE_SERVER_CERTIFICATE);
}
}
else
{
#if (TLS13_MIDDLEBOX_COMPAT_SUPPORT == ENABLED)
//First handshake message sent by the server?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
context->state == TLS_STATE_SERVER_HELLO)
{
//In middlebox compatibility mode, the server must send a dummy
//ChangeCipherSpec record immediately after its first handshake
//message
tlsChangeState(context, TLS_STATE_SERVER_CHANGE_CIPHER_SPEC);
}
else
#endif
{
//All handshake messages after the ServerHello are now encrypted
tlsChangeState(context, TLS_STATE_HANDSHAKE_TRAFFIC_KEYS);
}
}
}
//Return status code
return error;
}
/**
* @brief Send ServerKeyExchange message
*
* The ServerKeyExchange message is sent by the server only when the
* server Certificate message does not contain enough data to allow
* the client to exchange a premaster secret
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsSendServerKeyExchange(TlsContext *context)
{
error_t error;
size_t length;
TlsServerKeyExchange *message;
//Initialize status code
error = NO_ERROR;
//Point to the buffer where to format the message
message = (TlsServerKeyExchange *) (context->txBuffer + context->txBufferLen);
//Initialize length
length = 0;
//The ServerKeyExchange message is sent by the server only when the server
//Certificate message (if sent) does not contain enough data to allow the
//client to exchange a premaster secret
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 ||
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)
{
//Format ServerKeyExchange message
error = tlsFormatServerKeyExchange(context, message, &length);
}
else if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK)
{
#if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//If no PSK identity hint is provided by the server, the
//ServerKeyExchange message is omitted...
if(context->pskIdentityHint != NULL)
{
//Format ServerKeyExchange message
error = tlsFormatServerKeyExchange(context, message, &length);
}
#endif
}
//Check status code
if(!error)
{
//Any message to send?
if(length > 0)
{
//Debug message
TRACE_INFO("Sending ServerKeyExchange message (%" PRIuSIZE " bytes)...\r\n", length);
TRACE_DEBUG_ARRAY(" ", message, length);
//Send handshake message
error = tlsSendHandshakeMessage(context, message, length,
TLS_TYPE_SERVER_KEY_EXCHANGE);
}
}
//Check status code
if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//A server can optionally request a certificate from the client
tlsChangeState(context, TLS_STATE_CERTIFICATE_REQUEST);
}
//Return status code
return error;
}
/**
* @brief Send CertificateRequest message
*
* A server can optionally request a certificate from the client, if
* appropriate for the selected cipher suite. This message will
* immediately follow the ServerKeyExchange message
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsSendCertificateRequest(TlsContext *context)
{
error_t error;
size_t length;
TlsCertificateRequest *message;
//Initialize status code
error = NO_ERROR;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED || \
TLS_DSA_SIGN_SUPPORT == ENABLED || TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//A server can optionally request a certificate from the client
if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
{
//Non-anonymous key exchange?
if(context->keyExchMethod == TLS_KEY_EXCH_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA ||
context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
context->keyExchMethod == TLS13_KEY_EXCH_ECDHE ||
context->keyExchMethod == TLS13_KEY_EXCH_HYBRID)
{
//Point to the buffer where to format the message
message = (TlsCertificateRequest *) (context->txBuffer + context->txBufferLen);
//Format CertificateRequest message
error = tlsFormatCertificateRequest(context, message, &length);
//Check status code
if(!error)
{
//Debug message
TRACE_INFO("Sending CertificateRequest message (%" PRIuSIZE " bytes)...\r\n", length);
TRACE_DEBUG_ARRAY(" ", message, length);
//Send handshake message
error = tlsSendHandshakeMessage(context, message, length,
TLS_TYPE_CERTIFICATE_REQUEST);
}
}
}
#endif
//Check status code
if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
//Send a ServerHelloDone message to the client
tlsChangeState(context, TLS_STATE_SERVER_HELLO_DONE);
}
else
{
//Send a Certificate message to the client
tlsChangeState(context, TLS_STATE_SERVER_CERTIFICATE);
}
}
//Return status code
return error;
}
/**
* @brief Send ServerHelloDone message
*
* The ServerHelloDone message is sent by the server to indicate the
* end of the ServerHello and associated messages. After sending this
* message, the server will wait for a client response
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsSendServerHelloDone(TlsContext *context)
{
error_t error;
size_t length;
TlsServerHelloDone *message;
//Point to the buffer where to format the message
message = (TlsServerHelloDone *) (context->txBuffer + context->txBufferLen);
//Format ServerHelloDone message
error = tlsFormatServerHelloDone(context, message, &length);
//Check status code
if(!error)
{
//Debug message
TRACE_INFO("Sending ServerHelloDone message (%" PRIuSIZE " bytes)...\r\n", length);
TRACE_DEBUG_ARRAY(" ", message, length);
//Send handshake message
error = tlsSendHandshakeMessage(context, message, length,
TLS_TYPE_SERVER_HELLO_DONE);
}
//Check status code
if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//The client must send a Certificate message if the server requests it
if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
{
tlsChangeState(context, TLS_STATE_CLIENT_CERTIFICATE);
}
else
{
tlsChangeState(context, TLS_STATE_CLIENT_KEY_EXCHANGE);
}
}
//Return status code
return error;
}
/**
* @brief Send NewSessionTicket message
*
* This NewSessionTicket message is sent by the server during the TLS handshake
* before the ChangeCipherSpec message
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsSendNewSessionTicket(TlsContext *context)
{
error_t error;
size_t length;
TlsNewSessionTicket *message;
//Point to the buffer where to format the message
message = (TlsNewSessionTicket *) (context->txBuffer + context->txBufferLen);
//Format NewSessionTicket message
error = tlsFormatNewSessionTicket(context, message, &length);
//Check status code
if(!error)
{
//Debug message
TRACE_INFO("Sending NewSessionTicket message (%" PRIuSIZE " bytes)...\r\n", length);
TRACE_DEBUG_ARRAY(" ", message, length);
//Send handshake message
error = tlsSendHandshakeMessage(context, message, length,
TLS_TYPE_NEW_SESSION_TICKET);
}
//Check status code
if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//The NewSessionTicket message is sent by the server during the TLS
//handshake before the ChangeCipherSpec message
tlsChangeState(context, TLS_STATE_SERVER_CHANGE_CIPHER_SPEC);
}
//Return status code
return error;
}
/**
* @brief Format ServerHello message
* @param[in] context Pointer to the TLS context
* @param[out] message Buffer where to format the ServerHello message
* @param[out] length Length of the resulting ServerHello message
* @return Error code
**/
error_t tlsFormatServerHello(TlsContext *context,
TlsServerHello *message, size_t *length)
{
error_t error;
uint16_t version;
size_t n;
uint8_t *p;
TlsExtensionList *extensionList;
//In TLS 1.3, the client indicates its version preferences in the
//SupportedVersions extension and the legacy_version field must be
//set to 0x0303, which is the version number for TLS 1.2
version = MIN(context->version, TLS_VERSION_1_2);
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Get the corresponding DTLS version
version = dtlsTranslateVersion(version);
}
#endif
//In previous versions of TLS, the version field contains the lower of
//the version suggested by the client in the ClientHello and the highest
//supported by the server
message->serverVersion = htons(version);
//Server random value
osMemcpy(message->random, context->serverRandom, 32);
//Point to the session ID
p = message->sessionId;
//Length of the handshake message
*length = sizeof(TlsServerHello);
//Version of TLS prior to TLS 1.3?
if(context->version <= TLS_VERSION_1_2)
{
#if (TLS_SESSION_RESUME_SUPPORT == ENABLED)
//The session ID uniquely identifies the current session
osMemcpy(message->sessionId, context->sessionId, context->sessionIdLen);
message->sessionIdLen = (uint8_t) context->sessionIdLen;
#else
//The server may return an empty session ID to indicate that the session
//will not be cached and therefore cannot be resumed
message->sessionIdLen = 0;
#endif
}
else
{
//The legacy_session_id_echo echoes the contents of the client's
//legacy_session_id field
osMemcpy(message->sessionId, context->sessionId, context->sessionIdLen);
message->sessionIdLen = (uint8_t) context->sessionIdLen;
}
//Debug message
TRACE_DEBUG("Session ID (%" PRIu8 " bytes):\r\n", message->sessionIdLen);
TRACE_DEBUG_ARRAY(" ", message->sessionId, message->sessionIdLen);
//Advance data pointer
p += message->sessionIdLen;
//Adjust the length of the message
*length += message->sessionIdLen;
//The cipher_suite field contains the cipher suite selected by the server
STORE16BE(context->cipherSuite.identifier, p);
//Advance data pointer
p += sizeof(uint16_t);
//Adjust the length of the message
*length += sizeof(uint16_t);
//The CRIME exploit takes advantage of TLS compression, so conservative
//implementations do not enable compression at the TLS level
*p = TLS_COMPRESSION_METHOD_NULL;
//Advance data pointer
p += sizeof(uint8_t);
//Adjust the length of the message
*length += sizeof(uint8_t);
//Only extensions offered by the client can appear in the server's list
extensionList = (TlsExtensionList *) p;
//Total length of the extension list
extensionList->length = 0;
//Point to the first extension of the list
p += sizeof(TlsExtensionList);
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//TLS 1.0, TLS 1.1 or TLS 1.2 selected by the server?
if(context->version <= TLS_VERSION_1_2)
{
#if (TLS_SNI_SUPPORT == ENABLED)
//The server may include a SNI extension in the ServerHello
error = tlsFormatServerSniExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//Servers that receive an ClientHello containing a MaxFragmentLength
//extension may accept the requested maximum fragment length by including
//an extension of type MaxFragmentLength in the ServerHello
error = tlsFormatServerMaxFragLenExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
//The value of RecordSizeLimit is the maximum size of record in octets
//that the endpoint is willing to receive
error = tlsFormatServerRecordSizeLimitExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#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)
//A server that selects an ECC cipher suite in response to a ClientHello
//message including an EcPointFormats extension appends this extension
//to its ServerHello message
error = tlsFormatServerEcPointFormatsExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_ALPN_SUPPORT == ENABLED)
//The ALPN extension contains the name of the selected protocol
error = tlsFormatServerAlpnExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//The ClientCertType extension in the ServerHello indicates the type
//of certificates the client is requested to provide in a subsequent
//certificate payload
error = tlsFormatClientCertTypeExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
//With the ServerCertType extension in the ServerHello, the TLS server
//indicates the certificate type carried in the certificate payload
error = tlsFormatServerCertTypeExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
//On connecting, the client includes the EncryptThenMac extension in
//its ClientHello if it wishes to use encrypt-then-MAC rather than the
//default MAC-then-encrypt. If the server is capable of meeting this
//requirement, it responds with an EncryptThenMac in its ServerHello
error = tlsFormatServerEtmExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
//If a server implementing RFC 7627 receives the ExtendedMasterSecret
//extension, it must include the extension in its ServerHello message
error = tlsFormatServerEmsExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_TICKET_SUPPORT == ENABLED)
//The server uses the SessionTicket extension to indicate to the client
//that it will send a new session ticket using the NewSessionTicket
//handshake message
error = tlsFormatServerSessionTicketExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
#if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
//During secure renegotiation, the server must include a renegotiation_info
//extension containing the saved client_verify_data and server_verify_data
error = tlsFormatServerRenegoInfoExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
#endif
}
else
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.3 selected by the server?
if(context->version == TLS_VERSION_1_3)
{
//A server which negotiates TLS 1.3 must respond by sending a
//SupportedVersions extension containing the selected version value
error = tls13FormatServerSupportedVersionsExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
//If using (EC)DHE key establishment, servers offer exactly one
//KeyShareEntry in the ServerHello
error = tls13FormatServerKeyShareExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
//In order to accept PSK key establishment, the server sends a
//PreSharedKey extension indicating the selected identity
error = tls13FormatServerPreSharedKeyExtension(context, p, &n);
//Any error to report?
if(error)
return error;
//Fix the length of the extension list
extensionList->length += (uint16_t) n;
//Point to the next field
p += n;
}
else
#endif
//Invalid TLS version?
{
//Report an error
return ERROR_INVALID_VERSION;
}
//Any extensions included in the ServerHello message?
if(extensionList->length > 0)
{
//Convert the length of the extension list to network byte order
extensionList->length = htons(extensionList->length);
//Total length of the message
*length += sizeof(TlsExtensionList) + htons(extensionList->length);
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Format ServerKeyExchange message
* @param[in] context Pointer to the TLS context
* @param[out] message Buffer where to format the ServerKeyExchange message
* @param[out] length Length of the resulting ServerKeyExchange message
* @return Error code
**/
error_t tlsFormatServerKeyExchange(TlsContext *context,
TlsServerKeyExchange *message, size_t *length)
{
error_t error;
size_t n;
size_t paramsLen;
uint8_t *p;
uint8_t *params;
//Point to the beginning of the handshake message
p = message;
//Length of the handshake message
*length = 0;
#if (TLS_PSK_KE_SUPPORT == ENABLED || TLS_RSA_PSK_KE_SUPPORT == ENABLED || \
TLS_DHE_PSK_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//PSK key exchange method?
if(context->keyExchMethod == TLS_KEY_EXCH_PSK ||
context->keyExchMethod == TLS_KEY_EXCH_RSA_PSK ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_PSK ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_PSK)
{
//To help the client in selecting which identity to use, the server
//can provide a PSK identity hint in the ServerKeyExchange message
error = tlsFormatPskIdentityHint(context, p, &n);
//Any error to report?
if(error)
return error;
//Advance data pointer
p += n;
//Adjust the length of the message
*length += n;
}
#endif
//Diffie-Hellman or ECDH 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 ||
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)
{
//Point to the server's key exchange parameters
params = p;
//Format server's key exchange parameters
error = tlsFormatServerKeyParams(context, p, ¶msLen);
//Any error to report?
if(error)
return error;
//Advance data pointer
p += paramsLen;
//Adjust the length of the message
*length += paramsLen;
}
else
{
//Just for sanity
params = NULL;
paramsLen = 0;
}
//For non-anonymous Diffie-Hellman and ECDH key exchanges, a signature
//over the server's key exchange parameters shall be generated
if(context->keyExchMethod == TLS_KEY_EXCH_DHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_DHE_DSS ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_RSA ||
context->keyExchMethod == TLS_KEY_EXCH_ECDHE_ECDSA)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//TLS 1.0 or TLS 1.1 currently selected?
if(context->version <= TLS_VERSION_1_1)
{
//Sign server's key exchange parameters
error = tlsGenerateServerKeySignature(context,
(TlsDigitalSignature *) p, params, paramsLen, &n);
}
else
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//TLS 1.2 currently selected?
if(context->version == TLS_VERSION_1_2)
{
//Sign server's key exchange parameters
error = tls12GenerateServerKeySignature(context,
(Tls12DigitalSignature *) p, params, paramsLen, &n);
}
else
#endif
{
//Report an error
error = ERROR_INVALID_VERSION;
}
//Any error to report?
if(error)
return error;
//Advance data pointer
p += n;
//Adjust the length of the message
*length += n;
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Format CertificateRequest message
* @param[in] context Pointer to the TLS context
* @param[out] message Buffer where to format the CertificateRequest message
* @param[out] length Length of the resulting CertificateRequest message
* @return Error code
**/
error_t tlsFormatCertificateRequest(TlsContext *context,
TlsCertificateRequest *message, size_t *length)
{
error_t error;
size_t n;
uint8_t *p;
//Initialize status code
error = NO_ERROR;
//Point to the beginning of the message
p = (uint8_t *) message;
#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)
{
//Enumerate the types of certificate types that the client may offer
n = 0;
#if (TLS_RSA_SIGN_SUPPORT == ENABLED || TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//Accept certificates that contain an RSA public key
message->certificateTypes[n++] = TLS_CERT_RSA_SIGN;
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//Accept certificates that contain a DSA public key
message->certificateTypes[n++] = TLS_CERT_DSS_SIGN;
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//Accept certificates that contain an ECDSA public key
message->certificateTypes[n++] = TLS_CERT_ECDSA_SIGN;
#endif
//Fix the length of the list
message->certificateTypesLen = (uint8_t) n;
//Length of the handshake message
*length = sizeof(TlsCertificateRequest) + n;
//TLS 1.2 currently selected?
if(context->version == TLS_VERSION_1_2)
{
//The supported_signature_algorithms list contains the hash/signature
//algorithm pairs that the server is able to verify. Servers can
//minimize the computation cost by offering a restricted set of digest
//algorithms
error = tlsFormatSupportedSignAlgos(context, p + *length, &n);
//Check status code
if(!error)
{
//Adjust the length of the message
*length += n;
}
}
//Check status code
if(!error)
{
//The certificate_authorities list contains the distinguished names of
//acceptable certificate authorities, represented in DER-encoded format
error = tlsFormatCertAuthorities(context, p + *length, &n);
}
//Check status code
if(!error)
{
//Adjust the length of the message
*length += n;
}
}
else
#endif
#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)
{
Tls13CertRequestContext *certRequestContext;
TlsExtensionList *extensionList;
//Point to the certificate_request_context field
certRequestContext = (Tls13CertRequestContext *) p;
//The certificate_request_context field shall be zero length unless
//used for the post-handshake authentication exchange
certRequestContext->length = 0;
//Point to the next field
p += sizeof(Tls13CertRequestContext);
//Length of the handshake message
*length = sizeof(Tls13CertRequestContext);
//The extensions describe the parameters of the certificate being
//requested
extensionList = (TlsExtensionList *) p;
//Total length of the extension list
extensionList->length = 0;
//Point to the first extension of the list
p += sizeof(TlsExtensionList);
//Adjust the length of the message
*length += sizeof(TlsExtensionList);
//The SignatureAlgorithms extension contains the list of signature
//algorithms which the server would accept (refer to RFC 8446,