-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls13_sign_generate.c
503 lines (443 loc) · 14.7 KB
/
tls13_sign_generate.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
/**
* @file tls13_sign_generate.c
* @brief RSA/DSA/ECDSA/SM2/EdDSA signature generation (TLS 1.3)
*
* @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.
*
* @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_sign_generate.h"
#include "tls_transcript_hash.h"
#include "tls_misc.h"
#include "tls13_sign_generate.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 algorithm field specifies the signature scheme
signature->algorithm = htons(context->signScheme);
//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);
//Successful memory allocation?
if(buffer != NULL)
{
//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->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Generate an RSA-PSS signature
error = tls13GenerateRsaPssSignature(context, buffer, n, signature);
}
else
#endif
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
//ECDSA signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP256R1_TLS13_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP384R1_TLS13_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP512R1_TLS13_SHA512)
{
//Generate an ECDSA signature
error = tls13GenerateEcdsaSignature(context, buffer, n, signature);
}
else
#endif
#if (TLS_SM2_SIGN_SUPPORT == ENABLED)
//SM2 signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_SM2SIG_SM3)
{
//Generate an SM2 signature
error = tls13GenerateSm2Signature(context, buffer, n, signature);
}
else
#endif
#if (TLS_ED25519_SIGN_SUPPORT == ENABLED)
//Ed25519 signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_ED25519)
{
//Generate an Ed25519 signature
error = tls13GenerateEd25519Signature(context, buffer, n, signature);
}
else
#endif
#if (TLS_ED448_SIGN_SUPPORT == ENABLED)
//Ed448 signature scheme?
if(context->signScheme == TLS_SIGN_SCHEME_ED448)
{
//Generate an Ed448 signature
error = tls13GenerateEd448Signature(context, buffer, n, signature);
}
else
#endif
//Invalid signature scheme?
{
//Report an error
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
}
//Release memory buffer
tlsFreeMem(buffer);
}
else
{
//Failed to allocate memory
error = ERROR_OUT_OF_MEMORY;
}
//Check status code
if(!error)
{
//Total length of the digitally-signed element
*length = sizeof(Tls13DigitalSignature) + ntohs(signature->length);
}
//Return status code
return error;
}
/**
* @brief RSA-PSS signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the message to be signed
* @param[in] length Length of the message, in bytes
* @param[out] signature Buffer where to store the digital signature
* @return Error code
**/
error_t tls13GenerateRsaPssSignature(TlsContext *context, const uint8_t *message,
size_t length, Tls13DigitalSignature *signature)
{
#if (TLS_RSA_PSS_SIGN_SUPPORT == ENABLED)
error_t error;
size_t n;
const HashAlgo *hashAlgo;
//Retrieve the hash algorithm used for signing
if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_RSAE_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_RSA_PSS_PSS_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature scheme
hashAlgo = NULL;
}
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(message, length, context->clientVerifyData);
}
else
{
error = ERROR_UNSUPPORTED_SIGNATURE_ALGO;
}
//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 = tlsGenerateRsaPssSignature(context, hashAlgo,
context->clientVerifyData, signature->value, &n);
}
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
}
//Return status code
return error;
#else
//RSA-PSS signature algorithm not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief ECDSA signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the message to be signed
* @param[in] length Length of the message, in bytes
* @param[out] signature Buffer where to store the digital signature
* @return Error code
**/
error_t tls13GenerateEcdsaSignature(TlsContext *context, const uint8_t *message,
size_t length, Tls13DigitalSignature *signature)
{
#if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
error_t error;
size_t n;
const HashAlgo *hashAlgo;
//Retrieve the hash algorithm used for signing
if(context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP256R1_SHA256 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP256R1_TLS13_SHA256)
{
//Select SHA-256 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA256);
}
else if(context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP384R1_SHA384 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP384R1_TLS13_SHA384)
{
//Select SHA-384 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA384);
}
else if(context->signScheme == TLS_SIGN_SCHEME_ECDSA_SECP521R1_SHA512 ||
context->signScheme == TLS_SIGN_SCHEME_ECDSA_BP512R1_TLS13_SHA512)
{
//Select SHA-512 hash algorithm
hashAlgo = tlsGetHashAlgo(TLS_HASH_ALGO_SHA512);
}
else
{
//Invalid signature scheme
hashAlgo = NULL;
}
//Pre-hash the content covered by the digital signature
if(hashAlgo != NULL)
{
error = hashAlgo->compute(message, length, 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, &n);
}
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
}
//Return status code
return error;
#else
//ECDSA signature algorithm not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief SM2 signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the message to be signed
* @param[in] length Length of the message, in bytes
* @param[out] signature Buffer where to store the digital signature
* @return Error code
**/
error_t tls13GenerateSm2Signature(TlsContext *context, const uint8_t *message,
size_t length, Tls13DigitalSignature *signature)
{
#if (TLS_SM2_SIGN_SUPPORT == ENABLED)
error_t error;
size_t n;
EcDomainParameters params;
EcPrivateKey privateKey;
EcdsaSignature sm2Signature;
//Initialize EC domain parameters
ecInitDomainParameters(¶ms);
//Initialize EC private key
ecInitPrivateKey(&privateKey);
//Initialize SM2 signature
ecdsaInitSignature(&sm2Signature);
//Decode the PEM structure that holds the EC domain parameters
error = pemImportEcParameters(context->cert->privateKey,
context->cert->privateKeyLen, ¶ms);
//Check status code
if(!error)
{
//Decode the PEM structure that holds the EC private key
error = pemImportEcPrivateKey(context->cert->privateKey,
context->cert->privateKeyLen, context->cert->password, &privateKey);
}
//Check status code
if(!error)
{
//Generate SM2 signature
error = sm2GenerateSignature(context->prngAlgo, context->prngContext,
¶ms, &privateKey, SM3_HASH_ALGO, SM2_TLS13_ID,
osStrlen(SM2_TLS13_ID), message, length, &sm2Signature);
}
//Check status code
if(!error)
{
//Encode the resulting (R, S) integer pair using ASN.1
error = ecdsaWriteSignature(&sm2Signature, signature->value, &n);
}
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
}
//Release previously allocated resources
ecdsaFreeSignature(&sm2Signature);
//Return status code
return error;
#else
//SM2 signature algorithm not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Ed25519 signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the message to be signed
* @param[in] length Length of the message, in bytes
* @param[out] signature Buffer where to store the digital signature
* @return Error code
**/
error_t tls13GenerateEd25519Signature(TlsContext *context, const uint8_t *message,
size_t length, Tls13DigitalSignature *signature)
{
#if (TLS_ED25519_SIGN_SUPPORT == ENABLED)
error_t error;
size_t n;
DataChunk messageChunks[2];
//Data to be signed is run through the EdDSA algorithm without pre-hashing
messageChunks[0].buffer = message;
messageChunks[0].length = length;
messageChunks[1].buffer = NULL;
messageChunks[1].length = 0;
//Generate Ed25519 signature in PureEdDSA mode
error = tlsGenerateEd25519Signature(context, messageChunks,
signature->value, &n);
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
}
//Return status code
return error;
#else
//Ed25519 signature algorithm not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
/**
* @brief Ed448 signature generation (TLS 1.3)
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the message to be signed
* @param[in] length Length of the message, in bytes
* @param[out] signature Buffer where to store the digital signature
* @return Error code
**/
error_t tls13GenerateEd448Signature(TlsContext *context, const uint8_t *message,
size_t length, Tls13DigitalSignature *signature)
{
#if (TLS_ED448_SIGN_SUPPORT == ENABLED)
error_t error;
size_t n;
DataChunk messageChunks[2];
//Data to be signed is run through the EdDSA algorithm without pre-hashing
messageChunks[0].buffer = message;
messageChunks[0].length = length;
messageChunks[1].buffer = NULL;
messageChunks[1].length = 0;
//Generate Ed448 signature in PureEdDSA mode
error = tlsGenerateEd448Signature(context, messageChunks,
signature->value, &n);
//Check status code
if(!error)
{
//The signature is preceded by a 2-byte length field
signature->length = htons(n);
}
//Return status code
return error;
#else
//Ed448 signature algorithm not implemented
return ERROR_NOT_IMPLEMENTED;
#endif
}
#endif