forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.c
2641 lines (2207 loc) · 70.3 KB
/
tls.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.c
* @brief TLS (Transport Layer Security)
*
* @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.
*
* @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.1.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include <string.h>
#include <ctype.h>
#include "tls.h"
#include "tls_handshake.h"
#include "tls_common.h"
#include "tls_certificate.h"
#include "tls_transcript_hash.h"
#include "tls_record.h"
#include "tls_misc.h"
#include "tls13_client_misc.h"
#include "tls13_ticket.h"
#include "dtls_record.h"
#include "pkix/pem_import.h"
#include "pkix/x509_cert_parse.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
/**
* @brief TLS context initialization
* @return Handle referencing the fully initialized TLS context
**/
TlsContext *tlsInit(void)
{
TlsContext *context;
//Allocate a memory buffer to hold the TLS context
context = tlsAllocMem(sizeof(TlsContext));
//Successful memory allocation?
if(context != NULL)
{
//Clear TLS context
osMemset(context, 0, sizeof(TlsContext));
//Default state
context->state = TLS_STATE_INIT;
//Default transport protocol
context->transportProtocol = TLS_TRANSPORT_PROTOCOL_STREAM;
//Default operation mode
context->entity = TLS_CONNECTION_END_CLIENT;
//Default client authentication mode
context->clientAuthMode = TLS_CLIENT_AUTH_NONE;
//Minimum and maximum versions accepted by the implementation
context->versionMin = TLS_MIN_VERSION;
context->versionMax = TLS_MAX_VERSION;
//Default record layer version number
context->version = TLS_MIN_VERSION;
context->encryptionEngine.version = TLS_MIN_VERSION;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Select default named group
if(tls13IsGroupSupported(context, TLS_GROUP_ECDH_X25519))
{
context->preferredGroup = TLS_GROUP_ECDH_X25519;
}
else if(tls13IsGroupSupported(context, TLS_GROUP_SECP256R1))
{
context->preferredGroup = TLS_GROUP_SECP256R1;
}
else
{
context->preferredGroup = TLS_GROUP_NONE;
}
#endif
#if (DTLS_SUPPORT == ENABLED)
//Default PMTU
context->pmtu = DTLS_DEFAULT_PMTU;
//Default timeout
context->timeout = INFINITE_DELAY;
#endif
#if (DTLS_SUPPORT == ENABLED && DTLS_REPLAY_DETECTION_SUPPORT == ENABLED)
//Anti-replay mechanism is enabled by default
context->replayDetectionEnabled = TRUE;
#endif
#if (TLS_DH_SUPPORT == ENABLED)
//Initialize Diffie-Hellman context
dhInit(&context->dhContext);
#endif
#if (TLS_ECDH_SUPPORT == ENABLED)
//Initialize ECDH context
ecdhInit(&context->ecdhContext);
#endif
#if (TLS_RSA_SUPPORT == ENABLED)
//Initialize peer's RSA public key
rsaInitPublicKey(&context->peerRsaPublicKey);
#endif
#if (TLS_DSA_SIGN_SUPPORT == ENABLED)
//Initialize peer's DSA public key
dsaInitPublicKey(&context->peerDsaPublicKey);
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED || TLS_EDDSA_SIGN_SUPPORT == ENABLED)
//Initialize peer's EC domain parameters
ecInitDomainParameters(&context->peerEcParams);
//Initialize peer's EC public key
ecInitPublicKey(&context->peerEcPublicKey);
#endif
//Maximum number of plaintext data the TX and RX buffers can hold
context->txBufferMaxLen = TLS_MAX_RECORD_LENGTH;
context->rxBufferMaxLen = TLS_MAX_RECORD_LENGTH;
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//Maximum fragment length
context->maxFragLen = TLS_MAX_RECORD_LENGTH;
#endif
#if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
//Maximum record size the peer is willing to receive
context->recordSizeLimit = TLS_MAX_RECORD_LENGTH;
#endif
#if (DTLS_SUPPORT == ENABLED)
//Calculate the required size for the TX buffer
context->txBufferSize = context->txBufferMaxLen + sizeof(DtlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
//Calculate the required size for the RX buffer
context->rxBufferSize = context->rxBufferMaxLen + sizeof(DtlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
#else
//Calculate the required size for the TX buffer
context->txBufferSize = context->txBufferMaxLen + sizeof(TlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
//Calculate the required size for the RX buffer
context->rxBufferSize = context->rxBufferMaxLen + sizeof(TlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
#endif
}
//Return a pointer to the freshly created TLS context
return context;
}
/**
* @brief Retrieve current state
* @param[in] context Pointer to the TLS context
* @return Current TLS state
**/
TlsState tlsGetState(TlsContext *context)
{
TlsState state;
//Valid TLS context?
if(context != NULL)
state = context->state;
else
state = TLS_STATE_INIT;
//Return current state
return state;
}
/**
* @brief Set socket send and receive callbacks
* @param[in] context Pointer to the TLS context
* @param[in] socketSendCallback Send callback function
* @param[in] socketReceiveCallback Receive callback function
* @param[in] handle Socket handle
* @return Error code
**/
error_t tlsSetSocketCallbacks(TlsContext *context,
TlsSocketSendCallback socketSendCallback,
TlsSocketReceiveCallback socketReceiveCallback, TlsSocketHandle handle)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(socketSendCallback == NULL || socketReceiveCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save send and receive callback functions
context->socketSendCallback = socketSendCallback;
context->socketReceiveCallback = socketReceiveCallback;
//This socket handle will be directly passed to the callback functions
context->socketHandle = handle;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set minimum and maximum versions permitted
* @param[in] context Pointer to the TLS context
* @param[in] versionMin Minimum version accepted by the TLS implementation
* @param[in] versionMax Maximum version accepted by the TLS implementation
* @return Error code
**/
error_t tlsSetVersion(TlsContext *context, uint16_t versionMin,
uint16_t versionMax)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(versionMin < TLS_MIN_VERSION || versionMax > TLS_MAX_VERSION)
return ERROR_INVALID_PARAMETER;
if(versionMin > versionMax)
return ERROR_INVALID_PARAMETER;
//Minimum version accepted by the implementation
context->versionMin = versionMin;
//Maximum version accepted by the implementation
context->versionMax = versionMax;
//Default record layer version number
context->version = context->versionMin;
context->encryptionEngine.version = context->versionMin;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set the transport protocol to be used
* @param[in] context Pointer to the TLS context
* @param[in] transportProtocol Transport protocol to be used
* @return Error code
**/
error_t tlsSetTransportProtocol(TlsContext *context,
TlsTransportProtocol transportProtocol)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(transportProtocol != TLS_TRANSPORT_PROTOCOL_STREAM &&
transportProtocol != TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
return ERROR_INVALID_PARAMETER;
}
//Set transport protocol
context->transportProtocol = transportProtocol;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set operation mode (client or server)
* @param[in] context Pointer to the TLS context
* @param[in] entity Specifies whether this entity is considered a client or a server
* @return Error code
**/
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(entity != TLS_CONNECTION_END_CLIENT && entity != TLS_CONNECTION_END_SERVER)
return ERROR_INVALID_PARAMETER;
//Check whether TLS operates as a client or a server
context->entity = entity;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set the pseudo-random number generator to be used
* @param[in] context Pointer to the TLS context
* @param[in] prngAlgo PRNG algorithm
* @param[in] prngContext Pointer to the PRNG context
* @return Error code
**/
error_t tlsSetPrng(TlsContext *context, const PrngAlgo *prngAlgo,
void *prngContext)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(prngAlgo == NULL || prngContext == NULL)
return ERROR_INVALID_PARAMETER;
//PRNG algorithm that will be used to generate random numbers
context->prngAlgo = prngAlgo;
//PRNG context
context->prngContext = prngContext;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set the server name
* @param[in] context Pointer to the TLS context
* @param[in] serverName Fully qualified domain name of the server
* @return Error code
**/
error_t tlsSetServerName(TlsContext *context, const char_t *serverName)
{
size_t i;
size_t length;
//Check parameters
if(context == NULL || serverName == NULL)
return ERROR_INVALID_PARAMETER;
//Retrieve the length of the server name
length = osStrlen(serverName);
//Check whether the server name has already been configured
if(context->serverName != NULL)
{
//Release memory
tlsFreeMem(context->serverName);
context->serverName = NULL;
}
//Valid server name?
if(length > 0)
{
//Allocate a memory block to hold the hostname
context->serverName = tlsAllocMem(length + 1);
//Failed to allocate memory?
if(context->serverName == NULL)
return ERROR_OUT_OF_MEMORY;
//Convert the hostname into lowercase
for(i = 0; i < length; i++)
context->serverName[i] = osTolower(serverName[i]);
//Properly terminate the string with a NULL character
context->serverName[length] = '\0';
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Get the server name
* @param[in] context Pointer to the TLS context
* @return Fully qualified domain name of the server
**/
const char_t *tlsGetServerName(TlsContext *context)
{
static const char_t defaultServerName[] = "";
//Valid protocol name?
if(context != NULL && context->serverName != NULL)
{
//Return the fully qualified domain name of the server
return context->serverName;
}
else
{
//Return an empty string
return defaultServerName;
}
}
/**
* @brief Set session cache
* @param[in] context Pointer to the TLS context
* @param[in] cache Session cache that will be used to save/resume TLS sessions
* @return Error code
**/
error_t tlsSetCache(TlsContext *context, TlsCache *cache)
{
//Check parameters
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//The cache will be used to save/resume TLS sessions
context->cache = cache;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set client authentication mode (for servers only)
* @param[in] context Pointer to the TLS context
* @param[in] mode Client authentication mode
* @return Error code
**/
error_t tlsSetClientAuthMode(TlsContext *context, TlsClientAuthMode mode)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Save client authentication mode
context->clientAuthMode = mode;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set TLS buffer size
* @param[in] context Pointer to the TLS context
* @param[in] txBufferSize TX buffer size
* @param[in] rxBufferSize RX buffer size
* @return Error code
**/
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize,
size_t rxBufferSize)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(txBufferSize < TLS_MIN_RECORD_LENGTH ||
rxBufferSize < TLS_MIN_RECORD_LENGTH)
{
return ERROR_INVALID_PARAMETER;
}
//Maximum number of plaintext data the TX and RX buffers can hold
context->txBufferMaxLen = txBufferSize;
context->rxBufferMaxLen = rxBufferSize;
#if (DTLS_SUPPORT == ENABLED)
//Calculate the required size for the TX buffer
context->txBufferSize = txBufferSize + sizeof(DtlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
//Calculate the required size for the RX buffer
context->rxBufferSize = rxBufferSize + sizeof(DtlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
#else
//Calculate the required size for the TX buffer
context->txBufferSize = txBufferSize + sizeof(TlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
//Calculate the required size for the RX buffer
context->rxBufferSize = rxBufferSize + sizeof(TlsRecord) +
TLS_MAX_RECORD_OVERHEAD;
#endif
//Successful processing
return NO_ERROR;
}
/**
* @brief Set maximum fragment length
* @param[in] context Pointer to the TLS context
* @param[in] maxFragLen Maximum fragment length
* @return Error code
**/
error_t tlsSetMaxFragmentLength(TlsContext *context, size_t maxFragLen)
{
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Make sure the specified value is acceptable (ref to RFC 6066, section 4)
if(maxFragLen != 512 && maxFragLen != 1024 &&
maxFragLen != 2048 && maxFragLen != 4096 &&
maxFragLen != 16384)
{
return ERROR_INVALID_PARAMETER;
}
//Set maximum fragment length
context->maxFragLen = maxFragLen;
//Successful processing
return NO_ERROR;
#else
//Not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Specify the list of allowed cipher suites
* @param[in] context Pointer to the TLS context
* @param[in] cipherSuites List of allowed cipher suites (most preferred
* first). This parameter is taken as reference
* @param[in] length Number of cipher suites in the list
* @return Error code
**/
error_t tlsSetCipherSuites(TlsContext *context, const uint16_t *cipherSuites,
uint_t length)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(cipherSuites == NULL && length != 0)
return ERROR_INVALID_PARAMETER;
//Restrict the cipher suites that can be used
context->cipherSuites = cipherSuites;
context->numCipherSuites = length;
//Successful processing
return NO_ERROR;
}
/**
* @brief Specify the list of allowed ECDHE and FFDHE groups
* @param[in] context Pointer to the TLS context
* @param[in] groups List of named groups (most preferred first). This
* parameter is taken as reference
* @param[in] length Number of named groups in the list
* @return Error code
**/
error_t tlsSetSupportedGroups(TlsContext *context, const uint16_t *groups,
uint_t length)
{
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(groups == NULL && length != 0)
return ERROR_INVALID_PARAMETER;
//Restrict the named groups that can be used
context->supportedGroups = groups;
context->numSupportedGroups = length;
//Successful processing
return NO_ERROR;
}
/**
* @brief Specify the preferred ECDHE or FFDHE group
* @param[in] context Pointer to the TLS context
* @param[in] group Preferred ECDHE or FFDHE named group
* @return Error code
**/
error_t tlsSetPreferredGroup(TlsContext *context, uint16_t group)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Save the preferred named group
context->preferredGroup = group;
//Successful processing
return NO_ERROR;
#else
//Not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Import Diffie-Hellman parameters
* @param[in] context Pointer to the TLS context
* @param[in] params PEM structure that holds Diffie-Hellman parameters. This
* parameter is taken as reference
* @param[in] length Total length of the DER structure
* @return Error code
**/
error_t tlsSetDhParameters(TlsContext *context, const char_t *params,
size_t length)
{
#if (TLS_DH_SUPPORT == ENABLED)
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(params == NULL && length != 0)
return ERROR_INVALID_PARAMETER;
//Decode the PEM structure that holds Diffie-Hellman parameters
return pemImportDhParameters(params, length, &context->dhContext.params);
#else
//Diffie-Hellman is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Register ECDH key agreement callback function
* @param[in] context Pointer to the TLS context
* @param[in] ecdhCallback ECDH callback function
* @return Error code
**/
error_t tlsSetEcdhCallback(TlsContext *context, TlsEcdhCallback ecdhCallback)
{
#if (TLS_ECC_CALLBACK_SUPPORT == ENABLED)
//Check parameters
if(context == NULL || ecdhCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save the ECDH key agreement callback function
context->ecdhCallback = ecdhCallback;
//Successful processing
return NO_ERROR;
#else
//PSK key exchange is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief ECDSA signature generation callback function
* @param[in] context Pointer to the TLS context
* @param[in] ecdsaSignCallback ECDSA signature generation callback function
* @return Error code
**/
error_t tlsSetEcdsaSignCallback(TlsContext *context,
TlsEcdsaSignCallback ecdsaSignCallback)
{
#if (TLS_ECC_CALLBACK_SUPPORT == ENABLED)
//Check parameters
if(context == NULL || ecdsaSignCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save the ECDSA signature generation callback function
context->ecdsaSignCallback = ecdsaSignCallback;
//Successful processing
return NO_ERROR;
#else
//PSK key exchange is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Register ECDSA signature verification callback function
* @param[in] context Pointer to the TLS context
* @param[in] ecdsaVerifyCallback ECDSA signature verification callback function
* @return Error code
**/
error_t tlsSetEcdsaVerifyCallback(TlsContext *context,
TlsEcdsaVerifyCallback ecdsaVerifyCallback)
{
#if (TLS_ECC_CALLBACK_SUPPORT == ENABLED)
//Check parameters
if(context == NULL || ecdsaVerifyCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save the ECDSA signature verification callback function
context->ecdsaVerifyCallback = ecdsaVerifyCallback;
//Successful processing
return NO_ERROR;
#else
//PSK key exchange is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Register key logging callback function (for debugging purpose only)
* @param[in] context Pointer to the TLS context
* @param[in] keyLogCallback Key logging callback function
* @return Error code
**/
error_t tlsSetKeyLogCallback(TlsContext *context,
TlsKeyLogCallback keyLogCallback)
{
#if (TLS_KEY_LOG_SUPPORT == ENABLED)
//Check parameters
if(context == NULL || keyLogCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save the key logging callback function
context->keyLogCallback = keyLogCallback;
//Successful processing
return NO_ERROR;
#else
//Key logging is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Allow unknown ALPN protocols
* @param[in] context Pointer to the TLS context
* @param[in] allowed Specifies whether unknown ALPN protocols are allowed
* @return Error code
**/
error_t tlsAllowUnknownAlpnProtocols(TlsContext *context, bool_t allowed)
{
#if (TLS_ALPN_SUPPORT == ENABLED)
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Allow or disallow unknown ALPN protocols
context->unknownProtocolsAllowed = allowed;
//Successful processing
return NO_ERROR;
#else
//ALPN is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Set the list of supported ALPN protocols
* @param[in] context Pointer to the TLS context
* @param[in] protocolList Comma-delimited list of supported protocols
* @return Error code
**/
error_t tlsSetAlpnProtocolList(TlsContext *context, const char_t *protocolList)
{
#if (TLS_ALPN_SUPPORT == ENABLED)
size_t length;
//Check parameters
if(context == NULL || protocolList == NULL)
return ERROR_INVALID_PARAMETER;
//Retrieve the length of the list
length = osStrlen(protocolList);
//Check whether the list of supported protocols has already been configured
if(context->protocolList != NULL)
{
//Release memory
tlsFreeMem(context->protocolList);
context->protocolList = NULL;
}
//Check whether the list of protocols is valid
if(length > 0)
{
//Allocate a memory block to hold the list
context->protocolList = tlsAllocMem(length + 1);
//Failed to allocate memory?
if(context->protocolList == NULL)
return ERROR_OUT_OF_MEMORY;
//Save the list of supported protocols
osStrcpy(context->protocolList, protocolList);
}
//Successful processing
return NO_ERROR;
#else
//ALPN is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Register ALPN callback function
* @param[in] context Pointer to the TLS context
* @param[in] alpnCallback ALPN callback function
* @return Error code
**/
error_t tlsSetAlpnCallback(TlsContext *context, TlsAlpnCallback alpnCallback)
{
#if (TLS_ALPN_SUPPORT == ENABLED)
//Check parameters
if(context == NULL || alpnCallback == NULL)
return ERROR_INVALID_PARAMETER;
//Save the ALPN callback function
context->alpnCallback = alpnCallback;
//Successful processing
return NO_ERROR;
#else
//ALPN is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Get the name of the selected ALPN protocol
* @param[in] context Pointer to the TLS context
* @return Pointer to the protocol name
**/
const char_t *tlsGetAlpnProtocol(TlsContext *context)
{
static const char_t defaultProtocolName[] = "";
#if (TLS_ALPN_SUPPORT == ENABLED)
//Valid protocol name?
if(context != NULL && context->selectedProtocol != NULL)
{
//Return the name of the selected protocol
return context->selectedProtocol;
}
else
#endif
{
//Return an empty string
return defaultProtocolName;
}
}
/**
* @brief Set the pre-shared key to be used
* @param[in] context Pointer to the TLS context
* @param[in] psk Pointer to the pre-shared key
* @param[in] length Length of the pre-shared key, in bytes
* @return Error code
**/
error_t tlsSetPsk(TlsContext *context, const uint8_t *psk, size_t length)
{
#if (TLS_PSK_SUPPORT == ENABLED)
//Invalid TLS context?
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Check parameters
if(psk == NULL && length != 0)
return ERROR_INVALID_PARAMETER;
//Check whether the pre-shared key has already been configured
if(context->psk != NULL)
{
//Release memory
osMemset(context->psk, 0, context->pskLen);
tlsFreeMem(context->psk);
context->psk = NULL;
context->pskLen = 0;
}
//Valid PSK?
if(length > 0)
{
//Allocate a memory block to hold the pre-shared key
context->psk = tlsAllocMem(length);
//Failed to allocate memory?
if(context->psk == NULL)
return ERROR_OUT_OF_MEMORY;
//Save the pre-shared key
osMemcpy(context->psk, psk, length);
//Save the length of the key
context->pskLen = length;
}
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//For externally established PSKs, the hash algorithm must be set when the
//PSK is established, or default to SHA-256 if no such algorithm is defined
context->pskHashAlgo = TLS_HASH_ALGO_SHA256;
//The cipher suite must be provisioned along with the key
context->pskCipherSuite = 0;
#endif
//Successful processing
return NO_ERROR;
#else
//PSK key exchange is not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Set the PSK identity to be used by the client
* @param[in] context Pointer to the TLS context
* @param[in] pskIdentity NULL-terminated string that contains the PSK identity
* @return Error code
**/
error_t tlsSetPskIdentity(TlsContext *context, const char_t *pskIdentity)
{
#if (TLS_PSK_SUPPORT == ENABLED)
size_t length;
//Check parameters
if(context == NULL || pskIdentity == NULL)
return ERROR_INVALID_PARAMETER;
//Retrieve the length of the PSK identity
length = osStrlen(pskIdentity);
//Check whether the PSK identity has already been configured
if(context->pskIdentity != NULL)
{
//Release memory
tlsFreeMem(context->pskIdentity);
context->pskIdentity = NULL;
}
//Valid PSK identity?
if(length > 0)
{
//Allocate a memory block to hold the PSK identity
context->pskIdentity = tlsAllocMem(length + 1);
//Failed to allocate memory?