forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls_transcript_hash.c
518 lines (448 loc) · 15 KB
/
tls_transcript_hash.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
/**
* @file tls_transcript_hash.c
* @brief Transcript hash calculation
*
* @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_client.h"
#include "tls_key_material.h"
#include "tls_transcript_hash.h"
#include "tls13_key_material.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
/**
* @brief Initialize handshake message hashing
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsInitTranscriptHash(TlsContext *context)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//MD5 context already instantiated?
if(context->transcriptMd5Context != NULL)
{
tlsFreeMem(context->transcriptMd5Context);
context->transcriptMd5Context = NULL;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//SHA-1 context already instantiated?
if(context->transcriptSha1Context != NULL)
{
tlsFreeMem(context->transcriptSha1Context);
context->transcriptSha1Context = NULL;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Hash algorithm context already instantiated?
if(context->transcriptHashContext != NULL)
{
tlsFreeMem(context->transcriptHashContext);
context->transcriptHashContext = NULL;
}
#endif
#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)
{
//Allocate MD5 context
context->transcriptMd5Context = tlsAllocMem(sizeof(Md5Context));
//Failed to allocate memory?
if(context->transcriptMd5Context == NULL)
return ERROR_OUT_OF_MEMORY;
//Initialize MD5 context
md5Init(context->transcriptMd5Context);
}
#endif
#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 currently selected?
if(context->version <= TLS_VERSION_1_2)
{
//Allocate SHA-1 context
context->transcriptSha1Context = tlsAllocMem(sizeof(Sha1Context));
//Failed to allocate memory?
if(context->transcriptSha1Context == NULL)
return ERROR_OUT_OF_MEMORY;
//Initialize SHA-1 context
sha1Init(context->transcriptSha1Context);
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.2 or 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
const HashAlgo *hashAlgo;
//Point to the hash algorithm to be used
hashAlgo = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hashAlgo == NULL)
return ERROR_FAILURE;
//Allocate hash algorithm context
context->transcriptHashContext = tlsAllocMem(hashAlgo->contextSize);
//Failed to allocate memory?
if(context->transcriptHashContext == NULL)
return ERROR_OUT_OF_MEMORY;
//Initialize the hash algorithm context
hashAlgo->init(context->transcriptHashContext);
}
#endif
#if (TLS_CLIENT_SUPPORT == ENABLED)
//Client mode?
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
size_t length;
DtlsRecord *record;
//Point to the DTLS record that holds the ClientHello message
record = (DtlsRecord *) context->txBuffer;
//Sanity check
if(context->txBufferLen > sizeof(DtlsRecord))
{
//Retrieve the length of the handshake message
length = context->txBufferLen - sizeof(DtlsRecord);
//Update the hash value with the ClientHello message
tlsUpdateTranscriptHash(context, record->data, length);
}
}
else
#endif
//TLS protocol?
{
size_t length;
TlsRecord *record;
//Point to the TLS record that holds the ClientHello message
record = (TlsRecord *) context->txBuffer;
//Retrieve the length of the handshake message
length = ntohs(record->length);
//Sanity check
if((length + sizeof(TlsRecord)) <= context->txBufferSize)
{
//Update the hash value with the ClientHello message
tlsUpdateTranscriptHash(context, record->data, length);
}
}
}
#endif
//Successful initialization
return NO_ERROR;
}
/**
* @brief Update hash value with a handshake message
* @param[in] context Pointer to the TLS context
* @param[in] data Pointer to the handshake message being hashed
* @param[in] length Length of the message
**/
void tlsUpdateTranscriptHash(TlsContext *context, const void *data,
size_t length)
{
#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)
{
//Valid MD5 context?
if(context->transcriptMd5Context != NULL)
{
//Update MD5 hash value with message contents
md5Update(context->transcriptMd5Context, data, length);
}
}
#endif
#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 currently selected?
if(context->version <= TLS_VERSION_1_2)
{
//Valid SHA-1 context?
if(context->transcriptSha1Context != NULL)
{
//Update SHA-1 hash value with message contents
sha1Update(context->transcriptSha1Context, data, length);
}
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//TLS 1.2 or TLS 1.3 currently selected?
if(context->version >= TLS_VERSION_1_2)
{
const HashAlgo *hashAlgo;
//Point to the PRF hash algorithm to be used
hashAlgo = context->cipherSuite.prfHashAlgo;
//Valid hash algorithm?
if(hashAlgo != NULL && context->transcriptHashContext != NULL)
{
//Update hash value with message contents
hashAlgo->update(context->transcriptHashContext, data, length);
}
}
#endif
}
/**
* @brief Finalize hash calculation from previous handshake messages
* @param[in] context Pointer to the TLS context
* @param[in] hash Hash function used to digest the handshake messages
* @param[in] hashContext Pointer to the hash context
* @param[in] label NULL-terminated string
* @param[out] output Buffer where to store the resulting hash value
* @return Error code
**/
error_t tlsFinalizeTranscriptHash(TlsContext *context, const HashAlgo *hash,
const void *hashContext, const char_t *label, uint8_t *output)
{
error_t error;
HashContext *tempHashContext;
//Make sure the hash context is valid
if(hash == NULL || hashContext == NULL)
return ERROR_INVALID_PARAMETER;
//Allocate a temporary hash context
tempHashContext = tlsAllocMem(hash->contextSize);
//Successful memory allocation?
if(tempHashContext != NULL)
{
//The original hash context must be preserved
osMemcpy(tempHashContext, hashContext, hash->contextSize);
//Compute hash(handshakeMessages)
hash->final(tempHashContext, output);
//Release previously allocated resources
tlsFreeMem(tempHashContext);
//Successful processing
error = NO_ERROR;
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Return status code
return error;
}
/**
* @brief Release transcript hash context
* @param[in] context Pointer to the TLS context
**/
void tlsFreeTranscriptHash(TlsContext *context)
{
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//Release MD5 hash context
if(context->transcriptMd5Context != NULL)
{
osMemset(context->transcriptMd5Context, 0, sizeof(Md5Context));
tlsFreeMem(context->transcriptMd5Context);
context->transcriptMd5Context = NULL;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//Release SHA-1 hash context
if(context->transcriptSha1Context != NULL)
{
osMemset(context->transcriptSha1Context, 0, sizeof(Sha1Context));
tlsFreeMem(context->transcriptSha1Context);
context->transcriptSha1Context = NULL;
}
#endif
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Release transcript hash context
if(context->transcriptHashContext != NULL)
{
tlsFreeMem(context->transcriptHashContext);
context->transcriptHashContext = NULL;
}
#endif
}
/**
* @brief Compute verify data from previous handshake messages
* @param[in] context Pointer to the TLS context
* @param[in] entity Specifies whether the computation is performed at client
* or server side
* @param[out] verifyData Pointer to the buffer where to store the verify data
* @param[out] verifyDataLen Length of the verify data
* @return Error code
**/
error_t tlsComputeVerifyData(TlsContext *context, TlsConnectionEnd entity,
uint8_t *verifyData, size_t *verifyDataLen)
{
error_t error;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_1)
//TLS 1.0 or 1.1 currently selected?
if(context->version == TLS_VERSION_1_0 || context->version == TLS_VERSION_1_1)
{
const char_t *label;
uint8_t digest[MD5_DIGEST_SIZE + SHA1_DIGEST_SIZE];
//Finalize MD5 hash computation
error = tlsFinalizeTranscriptHash(context, MD5_HASH_ALGO,
context->transcriptMd5Context, "", digest);
//Check status code
if(!error)
{
//Finalize SHA-1 hash computation
error = tlsFinalizeTranscriptHash(context, SHA1_HASH_ALGO,
context->transcriptSha1Context, "", digest + MD5_DIGEST_SIZE);
}
//Check status code
if(!error)
{
//Check whether the computation is performed at client or server side
if(entity == TLS_CONNECTION_END_CLIENT)
{
label = "client finished";
}
else
{
label = "server finished";
}
//The verify data is always 12-byte long for TLS 1.0 and 1.1
error = tlsPrf(context->masterSecret, TLS_MASTER_SECRET_SIZE,
label, digest, sizeof(digest), verifyData, 12);
}
}
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)
{
const char_t *label;
const HashAlgo *hashAlgo;
HashContext *hashContext;
//Point to the hash algorithm to be used
hashAlgo = context->cipherSuite.prfHashAlgo;
//Valid hash algorithm?
if(hashAlgo != NULL && context->transcriptHashContext != NULL)
{
//Allocate hash algorithm context
hashContext = tlsAllocMem(hashAlgo->contextSize);
//Successful memory allocation?
if(hashContext != NULL)
{
//The original hash context must be preserved
osMemcpy(hashContext, context->transcriptHashContext,
hashAlgo->contextSize);
//Finalize hash computation
hashAlgo->final(hashContext, NULL);
//Check whether the computation is performed at client or server side
if(entity == TLS_CONNECTION_END_CLIENT)
{
label = "client finished";
}
else
{
label = "server finished";
}
//Compute the verify data
error = tls12Prf(hashAlgo, context->masterSecret, TLS_MASTER_SECRET_SIZE,
label, hashContext->digest, hashAlgo->digestSize,
verifyData, context->cipherSuite.verifyDataLen);
//Release previously allocated memory
tlsFreeMem(hashContext);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
}
else
{
//Invalid hash algorithm
error = ERROR_FAILURE;
}
}
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)
{
uint8_t *baseKey;
const HashAlgo *hashAlgo;
uint8_t digest[TLS_MAX_HKDF_DIGEST_SIZE];
uint8_t finishedKey[TLS_MAX_HKDF_DIGEST_SIZE];
//The hash function used by HKDF is the cipher suite hash algorithm
hashAlgo = context->cipherSuite.prfHashAlgo;
//Valid hash algorithm?
if(hashAlgo != NULL && context->transcriptHashContext != NULL)
{
//Check whether the computation is performed at client or server side
if(entity == TLS_CONNECTION_END_CLIENT)
{
baseKey = context->clientHsTrafficSecret;
}
else
{
baseKey = context->serverHsTrafficSecret;
}
//The key used to compute the Finished message is computed from the
//base key using HKDF
error = tls13HkdfExpandLabel(hashAlgo, baseKey, hashAlgo->digestSize,
"finished", NULL, 0, finishedKey, hashAlgo->digestSize);
//Check status code
if(!error)
{
//Compute the transcript hash
error = tlsFinalizeTranscriptHash(context, hashAlgo,
context->transcriptHashContext, "", digest);
}
//Check status code
if(!error)
{
//Compute the verify data
error = hmacCompute(hashAlgo, finishedKey, hashAlgo->digestSize,
digest, hashAlgo->digestSize, verifyData);
}
}
else
{
//Invalid hash algorithm
error = ERROR_FAILURE;
}
}
else
#endif
//Invalid TLS version?
{
//Report an error
error = ERROR_INVALID_VERSION;
}
//Check status code
if(!error)
{
//Save the length of the verify data
*verifyDataLen = context->cipherSuite.verifyDataLen;
//Debug message
TRACE_DEBUG("Verify data:\r\n");
TRACE_DEBUG_ARRAY(" ", verifyData, *verifyDataLen);
}
//Return status code
return error;
}
#endif