forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls13_signature.c
615 lines (561 loc) · 20.2 KB
/
tls13_signature.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
/**
* @file tls13_signature.c
* @brief RSA/DSA/ECDSA/EdDSA signature generation and verification
*
* @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_signature.h"
#include "tls_transcript_hash.h"
#include "tls_misc.h"
#include "pkix/pem_import.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
/**
* @brief Digital signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[out] p Buffer where to store the digitally-signed element
* @param[out] length Length of the digitally-signed element
* @return Error code
**/
error_t tls13GenerateSignature(TlsContext *context, uint8_t *p,
size_t *length)
{
error_t error;
size_t n;
uint8_t *buffer;
Tls13DigitalSignature *signature;
const HashAlgo *hashAlgo;
//Point to the digitally-signed element
signature = (Tls13DigitalSignature *) p;
//The hash function used by HKDF is the cipher suite hash algorithm
hashAlgo = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hashAlgo == NULL)
return ERROR_FAILURE;
//Calculate the length of the content covered by the digital signature
n = hashAlgo->digestSize + 98;
//Allocate a memory buffer
buffer = tlsAllocMem(n);
//Failed to allocate memory?
if(buffer == NULL)
return ERROR_OUT_OF_MEMORY;
//Form a string that consists of octet 32 (0x20) repeated 64 times
osMemset(buffer, ' ', 64);
//Append the context string. It is used to provide separation between
//signatures made in different contexts, helping against potential
//cross-protocol attacks
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
osMemcpy(buffer + 64, "TLS 1.3, client CertificateVerify", 33);
}
else
{
osMemcpy(buffer + 64, "TLS 1.3, server CertificateVerify", 33);
}
//Append a single 0 byte which serves as the separator
buffer[97] = 0x00;
//Compute the transcript hash
error = tlsFinalizeTranscriptHash(context, hashAlgo,
context->transcriptHashContext, "", buffer + 98);
//Check status code
if(!error)
{
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSA-PSS signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384 ||
context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
RsaPrivateKey privateKey;
//Initialize RSA private key
rsaInitPrivateKey(&privateKey);
//The algorithm field specifies the signature scheme and the
//corresponding hash algorithm
if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA256)
{
//Select rsa_pss_rsae_sha256 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA384)
{
//Select rsa_pss_rsae_sha384 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_RSAE_SHA512)
{
//Select rsa_pss_rsae_sha512 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA256)
{
//Select rsa_pss_pss_sha256 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA384)
{
//Select rsa_pss_pss_sha384 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signAlgo == TLS_SIGN_ALGO_RSA_PSS_PSS_SHA512)
{
//Select rsa_pss_pss_sha512 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature algorithm
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(buffer, n, context->clientVerifyData);
}
else
{
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
//Check status code
if(!error)
{
//Retrieve the RSA private key corresponding to the certificate sent
//in the previous message
error = pemImportRsaPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, &privateKey);
}
//Check status code
if(!error)
{
//RSA signatures must use an RSASSA-PSS algorithm, regardless of
//whether RSASSA-PKCS1-v1_5 algorithms appear in SignatureAlgorithms
error = rsassaPssSign(context->prngAlgo, context->prngContext,
&privateKey, hashAlgo, hashAlgo->digestSize,
context->clientVerifyData, signature->value, length);
}
//Release previously allocated resources
rsaFreePrivateKey(&privateKey);
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_ECDSA)
{
//The algorithm field specifies the signature scheme, the corresponding
//curve and the corresponding hash algorithm
if(context->cert->namedCurve == TLS_GROUP_SECP256R1 &&
context->signHashAlgo == TLS_HASH_ALGO_SHA256)
{
//Select ecdsa_secp256r1_sha256 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->cert->namedCurve == TLS_GROUP_SECP384R1 &&
context->signHashAlgo == TLS_HASH_ALGO_SHA384)
{
//Select ecdsa_secp384r1_sha384 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->cert->namedCurve == TLS_GROUP_SECP521R1 &&
context->signHashAlgo == TLS_HASH_ALGO_SHA512)
{
//Select ecdsa_secp521r1_sha512 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512);
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature algorithm
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(buffer, n, context->clientVerifyData);
}
else
{
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
//Check status code
if(!error)
{
//Generate an ECDSA signature
error = tlsGenerateEcdsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, length);
}
}
else
#endif
#if (TLS_EDDSA_SIGN_SUPPORT == ENABLED)
//EdDSA signature scheme?
if(context->signAlgo == TLS_SIGN_ALGO_ED25519 ||
context->signAlgo == TLS_SIGN_ALGO_ED448)
{
//The algorithm field specifies the signature algorithm used
if(context->signAlgo == TLS_SIGN_ALGO_ED25519)
{
//Select ed25519 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_ED25519);
}
else if(context->signAlgo == TLS_SIGN_ALGO_ED448)
{
//Select ed448 signature algorithm
signature->algorithm = HTONS(TLS_SIGN_SCHEME_ED448);
}
else
{
//Invalid signature algorithm
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//Check status code
if(!error)
{
EddsaMessageChunk messageChunks[2];
//Data to be signed is run through the EdDSA algorithm without
//pre-hashing
messageChunks[0].buffer = buffer;
messageChunks[0].length = n;
messageChunks[1].buffer = NULL;
messageChunks[1].length = 0;
//Generate a signature in PureEdDSA mode
error = tlsGenerateEddsaSignature(context, messageChunks,
signature->value, length);
}
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
//Release memory buffer
tlsFreeMem(buffer);
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(*length);
//Total length of the digitally-signed element
*length += sizeof(Tls13DigitalSignature);
}
//Return status code
return error;
}
/**
* @brief Digital signature verification (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] p Pointer to the digitally-signed element to be verified
* @param[in] length Length of the digitally-signed element
* @return Error code
**/
error_t tls13VerifySignature(TlsContext *context, const uint8_t *p,
size_t length)
{
error_t error;
size_t n;
uint8_t *buffer;
Tls13SignatureScheme signAlgo;
const Tls13DigitalSignature *signature;
const HashAlgo *hashAlgo;
//Point to the digitally-signed element
signature = (Tls13DigitalSignature *) p;
//Malformed CertificateVerify message?
if(length < sizeof(Tls13DigitalSignature))
return ERROR_DECODING_FAILED;
if(length != (sizeof(Tls13DigitalSignature) + ntohs(signature->length)))
return ERROR_DECODING_FAILED;
//The hash function used by HKDF is the cipher suite hash algorithm
hashAlgo = context->cipherSuite.prfHashAlgo;
//Make sure the hash algorithm is valid
if(hashAlgo == NULL)
return ERROR_FAILURE;
//Calculate the length of the content covered by the digital signature
n = hashAlgo->digestSize + 98;
//Allocate a memory buffer
buffer = tlsAllocMem(n);
//Failed to allocate memory?
if(buffer == NULL)
return ERROR_OUT_OF_MEMORY;
//Form a string that consists of octet 32 (0x20) repeated 64 times
osMemset(buffer, ' ', 64);
//Append the context string. It is used to provide separation between
//signatures made in different contexts, helping against potential
//cross-protocol attacks
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
osMemcpy(buffer + 64, "TLS 1.3, server CertificateVerify", 33);
}
else
{
osMemcpy(buffer + 64, "TLS 1.3, client CertificateVerify", 33);
}
//Append a single 0 byte which serves as the separator
buffer[97] = 0x00;
//Compute the transcript hash
error = tlsFinalizeTranscriptHash(context, hashAlgo,
context->transcriptHashContext, "", buffer + 98);
//Check status code
if(!error)
{
//The algorithm field specifies the signature scheme
signAlgo = (Tls13SignatureScheme) ntohs(signature->algorithm);
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
//RSASSA-PSS signature scheme?
if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256 ||
signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384 ||
signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_RSA_SIGN)
{
//Retrieve the hash algorithm used for signing
if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature scheme
hashAlgo = NULL;
}
}
else if(context->peerCertType == TLS_CERT_RSA_PSS_SIGN)
{
//Retrieve the hash algorithm used for signing
if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(signAlgo == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature scheme
hashAlgo = NULL;
}
}
else
{
//Invalid certificate
hashAlgo = NULL;
}
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(buffer, n, context->clientVerifyData);
}
else
{
error = ERROR_ILLEGAL_PARAMETER;
}
//Check status code
if(!error)
{
//Verify RSASSA-PSS signature
error = rsassaPssVerify(&context->peerRsaPublicKey, hashAlgo,
hashAlgo->digestSize, context->clientVerifyData,
signature->value, ntohs(signature->length));
}
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256 ||
signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384 ||
signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_ECDSA_SIGN)
{
//Retrieve the hash algorithm used for signing
if(context->peerEcParams.name == NULL)
{
//Invalid signature scheme
hashAlgo = NULL;
}
else if(signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256 &&
osStrcmp(context->peerEcParams.name, "secp256r1") == 0)
{
//Select SHA-256 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384 &&
osStrcmp(context->peerEcParams.name, "secp384r1") == 0)
{
//Select SHA-384 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(signAlgo == TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512 &&
osStrcmp(context->peerEcParams.name, "secp521r1") == 0)
{
//Select SHA-512 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature scheme
hashAlgo = NULL;
}
}
else
{
//Invalid certificate
hashAlgo = NULL;
}
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(buffer, n, context->clientVerifyData);
}
else
{
error = ERROR_ILLEGAL_PARAMETER;
}
//Check status code
if(!error)
{
//Verify ECDSA signature
error = tlsVerifyEcdsaSignature(context, context->clientVerifyData,
hashAlgo->digestSize, signature->value, ntohs(signature->length));
}
}
else
#endif
#if (TLS_EDDSA_SIGN_SUPPORT == ENABLED && TLS_ED25519_SUPPORT == ENABLED)
//Ed25519 signature scheme?
if(signAlgo == TLS_SIGN_SCHEME_ED25519)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_ED25519_SIGN)
{
EddsaMessageChunk messageChunks[2];
//Data to be verified is run through the EdDSA algorithm without
//pre-hashing
messageChunks[0].buffer = buffer;
messageChunks[0].length = n;
messageChunks[1].buffer = NULL;
messageChunks[1].length = 0;
//Verify EdDSA signature (PureEdDSA mode)
error = tlsVerifyEddsaSignature(context, messageChunks,
signature->value, ntohs(signature->length));
}
else
{
//Invalid certificate
error = ERROR_ILLEGAL_PARAMETER;
}
}
else
#endif
#if (TLS_EDDSA_SIGN_SUPPORT == ENABLED && TLS_ED448_SUPPORT == ENABLED)
//Ed448 signature scheme?
if(signAlgo == TLS_SIGN_SCHEME_ED448)
{
//Enforce the type of the certificate provided by the peer
if(context->peerCertType == TLS_CERT_ED448_SIGN)
{
EddsaMessageChunk messageChunks[2];
//Data to be verified is run through the EdDSA algorithm without
//pre-hashing
messageChunks[0].buffer = buffer;
messageChunks[0].length = n;
messageChunks[1].buffer = NULL;
messageChunks[1].length = 0;
//Verify EdDSA signature (PureEdDSA mode)
error = tlsVerifyEddsaSignature(context, messageChunks,
signature->value, ntohs(signature->length));
}
else
{
//Invalid certificate
error = ERROR_ILLEGAL_PARAMETER;
}
}
else
#endif
//Unknown signature scheme?
{
//Report an error
error = ERROR_ILLEGAL_PARAMETER;
}
}
//Release memory buffer
tlsFreeMem(buffer);
//Return status code
return error;
}
#endif