forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls13_key_material.c
784 lines (678 loc) · 24.3 KB
/
tls13_key_material.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
/**
* @file tls13_key_material.c
* @brief TLS 1.3 key schedule
*
* @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_misc.h"
#include "tls_key_material.h"
#include "tls_transcript_hash.h"
#include "tls13_key_material.h"
#include "tls13_ticket.h"
#include "kdf/hkdf.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
/**
* @brief HKDF-Expand-Label function
* @param[in] hash Hash function used by HKDF
* @param[in] secret Pointer to the secret
* @param[in] secretLen Length of the secret
* @param[in] label Identifying label (NULL-terminated string)
* @param[in] context Pointer to the upper-layer context
* @param[in] contextLen Length of the upper-layer context
* @param[out] output Pointer to the output
* @param[in] outputLen Desired output length
* @return Error code
**/
error_t tls13HkdfExpandLabel(const HashAlgo *hash, const uint8_t *secret,
size_t secretLen, const char_t *label, const uint8_t *context,
size_t contextLen, uint8_t *output, size_t outputLen)
{
error_t error;
size_t n;
size_t labelLen;
uint8_t *hkdfLabel;
//Check parameters
if(label == NULL)
return ERROR_INVALID_PARAMETER;
if(context == NULL && contextLen != 0)
return ERROR_INVALID_PARAMETER;
//Retrieve the length of the label
labelLen = osStrlen(label);
//Check parameters
if(labelLen > (255 - 6) || contextLen > 255)
return ERROR_INVALID_LENGTH;
//Compute the length of the HkdfLabel structure
n = labelLen + contextLen + 10;
//Allocate a memory buffer to hold the HkdfLabel structure
hkdfLabel = tlsAllocMem(n);
//Successful memory allocation?
if(hkdfLabel != NULL)
{
//Format the HkdfLabel structure
hkdfLabel[0] = MSB(outputLen);
hkdfLabel[1] = LSB(outputLen);
hkdfLabel[2] = (uint8_t) (labelLen + 6);
osMemcpy(hkdfLabel + 3, "tls13 ", 6);
osMemcpy(hkdfLabel + 9, label, labelLen);
hkdfLabel[labelLen + 9] = (uint8_t) contextLen;
osMemcpy(hkdfLabel + labelLen + 10, context, contextLen);
//Debug message
TRACE_DEBUG("HkdfLabel (%" PRIuSIZE " bytes):\r\n", n);
TRACE_DEBUG_ARRAY(" ", hkdfLabel, n);
//Compute HKDF-Expand(Secret, HkdfLabel, Length)
error = hkdfExpand(hash, secret, secretLen, hkdfLabel, n, output, outputLen);
//Release previously allocated memory
tlsFreeMem(hkdfLabel);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Return status code
return error;
}
/**
* @brief Derive-Secret function
* @param[in] context Pointer to the TLS context
* @param[in] secret Pointer to the secret
* @param[in] secretLen Length of the secret
* @param[in] label Identifying label (NULL-terminated string)
* @param[in] message Concatenation of the indicated handshake messages
* @param[in] messageLen Length of the indicated handshake messages
* @param[out] output Pointer to the output
* @param[in] outputLen Desired output length
* @return Error code
**/
error_t tls13DeriveSecret(TlsContext *context, const uint8_t *secret,
size_t secretLen, const char_t *label, const char_t *message,
size_t messageLen, uint8_t *output, size_t outputLen)
{
error_t error;
const HashAlgo *hash;
uint8_t digest[TLS_MAX_HKDF_DIGEST_SIZE];
//The hash function used by HKDF is the cipher suite hash algorithm
hash = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hash != NULL)
{
//Any handshake messages specified?
if(message != NULL)
{
//Compute Transcript-Hash(Messages);
error = hash->compute(message, messageLen, digest);
}
else
{
//Implementations can implement the transcript by keeping a running
//transcript hash value based on the negotiated hash
error = tlsFinalizeTranscriptHash(context, hash,
context->transcriptHashContext, "", digest);
}
//Debug message
TRACE_DEBUG("Transcript hash (%" PRIuSIZE " bytes):\r\n", hash->digestSize);
TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
//Check status code
if(!error)
{
//Compute HKDF-Expand-Label(Secret, Label, Transcript-Hash, Hash.length)
error = tls13HkdfExpandLabel(hash, secret, secretLen, label, digest,
hash->digestSize, output, outputLen);
}
}
else
{
//Invalid HKDF hash algorithm
error = ERROR_FAILURE;
}
//Return status code
return error;
}
/**
* @brief Compute early traffic keys
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tls13GenerateEarlyTrafficKeys(TlsContext *context)
{
error_t error;
size_t ikmLen;
const uint8_t *ikm;
const HashAlgo *hash;
//The hash function used by HKDF is the cipher suite hash algorithm
hash = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hash == NULL)
return ERROR_FAILURE;
//Although PSKs can be established out of band, PSKs can also be established
//in a previous connection
if(tls13IsPskValid(context))
{
//IKM is a pre-shared key established externally
ikm = context->psk;
ikmLen = context->pskLen;
}
else if(tls13IsTicketValid(context))
{
//IKM is a pre-shared key derived from the resumption master secret from
//a previous connection
ikm = context->ticketPsk;
ikmLen = context->ticketPskLen;
}
else
{
//The pre-shared key is not valid
return ERROR_FAILURE;
}
//Calculate early secret
error = hkdfExtract(hash, ikm, ikmLen, NULL, 0, context->secret);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Early secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//Calculate client early traffic secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"c e traffic", NULL, 0, context->clientEarlyTrafficSecret,
hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Client early secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->clientEarlyTrafficSecret, hash->digestSize);
//The traffic keying material is generated from the traffic secret value
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//Calculate client early traffic keys
error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientEarlyTrafficSecret);
}
else
{
//The implementation must verify that its receive buffer is empty
if(context->rxBufferLen == 0)
{
//Calculate client early traffic keys
error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientEarlyTrafficSecret);
}
else
{
//The receive buffer is not empty
error = ERROR_UNEXPECTED_MESSAGE;
}
}
//Failed to generate traffic keying material?
if(error)
return error;
//Calculate early exporter master secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"e exp master", NULL, 0, context->exporterMasterSecret, hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Early exporter master secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->exporterMasterSecret, hash->digestSize);
#if (TLS_KEY_LOG_SUPPORT == ENABLED)
//Log client early traffic secret
tlsDumpSecret(context, "CLIENT_EARLY_TRAFFIC_SECRET",
context->clientEarlyTrafficSecret, hash->digestSize);
//Log early exporter master secret
tlsDumpSecret(context, "EARLY_EXPORTER_SECRET",
context->exporterMasterSecret, hash->digestSize);
#endif
//When a PSK is used and early data is allowed for that PSK, the client can
//send application data in its first flight of messages
context->state = TLS_STATE_EARLY_DATA;
//Successful processing
return NO_ERROR;
}
/**
* @brief Compute handshake traffic keys
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tls13GenerateHandshakeTrafficKeys(TlsContext *context)
{
error_t error;
size_t ikmLen;
const uint8_t *ikm;
const HashAlgo *hash;
//The hash function used by HKDF is the cipher suite hash algorithm
hash = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hash == NULL)
return ERROR_FAILURE;
#if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_ECDHE_KE_SUPPORT == ENABLED)
//(EC)DHE key exchange method?
if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
context->keyExchMethod == TLS13_KEY_EXCH_ECDHE)
{
//If PSK is not in use, IKM is a string of Hash-lengths bytes set to 0
osMemset(context->secret, 0, hash->digestSize);
//Point to the IKM argument
ikm = context->secret;
ikmLen = hash->digestSize;
}
else
#endif
#if (TLS13_PSK_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
//PSK-only or PSK with (EC)DHE key exchange method?
if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
{
//Although PSKs can be established out of band, PSKs can also be
//established in a previous connection
if(tls13IsPskValid(context))
{
//IKM is a pre-shared key established externally
ikm = context->psk;
ikmLen = context->pskLen;
}
else if(tls13IsTicketValid(context))
{
//IKM is a pre-shared key derived from the resumption master secret
//from a previous connection
ikm = context->ticketPsk;
ikmLen = context->ticketPskLen;
}
else
{
//The pre-shared key is not valid
return ERROR_FAILURE;
}
}
else
#endif
//Invalid key exchange method?
{
//Report an error
return ERROR_FAILURE;
}
//Calculate early secret
error = hkdfExtract(hash, ikm, ikmLen, NULL, 0, context->secret);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Early secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//Derive early secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"derived", "", 0, context->secret, hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Derived secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//PSK-only key exchange method?
if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
{
//If the (EC)DHE shared secret is not available, then the 0-value
//consisting of a string of Hash.length bytes set to zeros is used
osMemset(context->premasterSecret, 0, hash->digestSize);
context->premasterSecretLen = hash->digestSize;
}
//Calculate handshake secret
error = hkdfExtract(hash, context->premasterSecret,
context->premasterSecretLen, context->secret, hash->digestSize,
context->secret);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Handshake secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//Calculate client handshake traffic secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"c hs traffic", NULL, 0, context->clientHsTrafficSecret,
hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Client handshake secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->clientHsTrafficSecret, hash->digestSize);
//Calculate server handshake traffic secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"s hs traffic", NULL, 0, context->serverHsTrafficSecret,
hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Server handshake secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->serverHsTrafficSecret, hash->digestSize);
//The implementation must verify that its receive buffer is empty before
//switching to encrypted handshake
if(context->rxBufferLen != 0)
return ERROR_HANDSHAKE_FAILED;
//The traffic keying material is generated from the traffic secret value
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//Release encryption engine
tlsFreeEncryptionEngine(&context->encryptionEngine);
//Calculate client handshake traffic keys
error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientHsTrafficSecret);
//Check status code
if(!error)
{
//Calculate server handshake traffic keys
error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
TLS_CONNECTION_END_SERVER, context->serverHsTrafficSecret);
}
}
else
{
//Release decryption engine
tlsFreeEncryptionEngine(&context->decryptionEngine);
//Calculate client handshake traffic keys
error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientHsTrafficSecret);
//Check status code
if(!error)
{
//Calculate server handshake traffic keys
error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
TLS_CONNECTION_END_SERVER, context->serverHsTrafficSecret);
}
}
//Failed to generate traffic keying material?
if(error)
return error;
#if (TLS_KEY_LOG_SUPPORT == ENABLED)
//Log client handshake traffic secret
tlsDumpSecret(context, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
context->clientHsTrafficSecret, hash->digestSize);
//Log server handshake traffic secret
tlsDumpSecret(context, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
context->serverHsTrafficSecret, hash->digestSize);
#endif
//In all handshakes, the server must send the EncryptedExtensions message
//immediately after the ServerHello message
context->state = TLS_STATE_ENCRYPTED_EXTENSIONS;
//Successful processing
return NO_ERROR;
}
/**
* @brief Compute server application traffic keys
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tls13GenerateServerAppTrafficKeys(TlsContext *context)
{
error_t error;
const HashAlgo *hash;
uint8_t ikm[TLS_MAX_HKDF_DIGEST_SIZE];
//The hash function used by HKDF is the cipher suite hash algorithm
hash = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hash == NULL)
return ERROR_FAILURE;
//Derive handshake secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"derived", "", 0, context->secret, hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Derived secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//IKM is a string of Hash-lengths bytes set to 0
osMemset(ikm, 0, hash->digestSize);
//Calculate master secret
error = hkdfExtract(hash, ikm, hash->digestSize, context->secret,
hash->digestSize, context->secret);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Master secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
//Calculate client application traffic secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"c ap traffic", NULL, 0, context->clientAppTrafficSecret,
hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Client application secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->clientAppTrafficSecret, hash->digestSize);
//Calculate server application traffic secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"s ap traffic", NULL, 0, context->serverAppTrafficSecret,
hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Server application secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->serverAppTrafficSecret, hash->digestSize);
//All the traffic keying material is recomputed when changing from the
//handshake to application data keys
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//The implementation must verify that its receive buffer is empty before
//rekeying
if(context->rxBufferLen == 0)
{
//Release decryption engine
tlsFreeEncryptionEngine(&context->decryptionEngine);
//Inform the record layer that subsequent records will be protected
//under the new traffic keys
error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
TLS_CONNECTION_END_SERVER, context->serverAppTrafficSecret);
}
else
{
//The receive buffer is not empty
error = ERROR_UNEXPECTED_MESSAGE;
}
}
else
{
//Release encryption engine
tlsFreeEncryptionEngine(&context->encryptionEngine);
//Inform the record layer that subsequent records will be protected
//under the new traffic keys
error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
TLS_CONNECTION_END_SERVER, context->serverAppTrafficSecret);
}
//Failed to generate traffic keying material?
if(error)
return error;
//Calculate exporter master secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"exp master", NULL, 0, context->exporterMasterSecret, hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Exporter master secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->exporterMasterSecret, hash->digestSize);
#if (TLS_KEY_LOG_SUPPORT == ENABLED)
//Log client application traffic secret
tlsDumpSecret(context, "CLIENT_TRAFFIC_SECRET_0",
context->clientAppTrafficSecret, hash->digestSize);
//Log server application traffic secret
tlsDumpSecret(context, "SERVER_TRAFFIC_SECRET_0",
context->serverAppTrafficSecret, hash->digestSize);
//Log exporter master secret
tlsDumpSecret(context, "EXPORTER_SECRET",
context->exporterMasterSecret, hash->digestSize);
#endif
//Check whether TLS operates as a client or a server
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//If the server sent an EarlyData extension, the client must send an
//EndOfEarlyData message after receiving the server Finished
if(context->earlyDataEnabled && context->earlyDataExtReceived)
{
//Send a EndOfEarlyData message to the server
context->state = TLS_STATE_END_OF_EARLY_DATA;
}
else
{
//PSK key exchange method?
if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
{
//Send a Finished message to the server
context->state = TLS_STATE_CLIENT_FINISHED;
}
else
{
//Send a Certificate message if the server requests it
context->state = TLS_STATE_CLIENT_CERTIFICATE;
}
}
}
else
{
//PSK key exchange method?
if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
{
//Wait for a Finished message from the client
context->state = TLS_STATE_CLIENT_FINISHED;
}
else
{
//The client must send a Certificate message if the server requests it
if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
{
context->state = TLS_STATE_CLIENT_CERTIFICATE;
}
else
{
context->state = TLS_STATE_CLIENT_FINISHED;
}
}
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Compute client application traffic keys
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tls13GenerateClientAppTrafficKeys(TlsContext *context)
{
error_t error;
const HashAlgo *hash;
//The hash function used by HKDF is the cipher suite hash algorithm
hash = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hash == NULL)
return ERROR_FAILURE;
//At this point, the handshake is complete, and the client and server
//derive the keying material required by the record layer to exchange
//application-layer data protected through authenticated encryption
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//Release encryption engine
tlsFreeEncryptionEngine(&context->encryptionEngine);
//Inform the record layer that subsequent records will be protected
//under the new traffic keys
error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientAppTrafficSecret);
}
else
{
//The implementation must verify that its receive buffer is empty before
//rekeying
if(context->rxBufferLen == 0)
{
//Release decryption engine
tlsFreeEncryptionEngine(&context->decryptionEngine);
//Inform the record layer that subsequent records will be protected
//under the new traffic keys
error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
TLS_CONNECTION_END_CLIENT, context->clientAppTrafficSecret);
}
else
{
//The receive buffer is not empty
error = ERROR_UNEXPECTED_MESSAGE;
}
}
//Failed to generate traffic keying material?
if(error)
return error;
//Calculate resumption master secret
error = tls13DeriveSecret(context, context->secret, hash->digestSize,
"res master", NULL, 0, context->resumptionMasterSecret, hash->digestSize);
//Any error to report?
if(error)
return error;
//Debug message
TRACE_DEBUG("Resumption master secret:\r\n");
TRACE_DEBUG_ARRAY(" ", context->resumptionMasterSecret, hash->digestSize);
//Once all the values which are to be derived from a given secret have been
//computed, that secret should be erased
osMemset(context->secret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
osMemset(context->clientEarlyTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
osMemset(context->clientHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
osMemset(context->serverHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
#if (TLS_TICKET_SUPPORT == ENABLED)
//Check whether session ticket mechanism is enabled
if(context->entity == TLS_CONNECTION_END_SERVER &&
context->ticketEncryptCallback != NULL &&
context->pskKeModeSupported)
{
//At any time after the server has received the client Finished message,
//it may send a NewSessionTicket message
context->state = TLS_STATE_NEW_SESSION_TICKET;
}
else
#endif
{
//At this point, the handshake is complete, and the client and server
//can exchange application-layer data
context->state = TLS_STATE_APPLICATION_DATA;
}
//Successful processing
return NO_ERROR;
}
#endif