1
10
13
14
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
126
127
128
129
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
185
186
187
188
189
193
194
195
196
197
198
199
217
230
231
232
233
234
235
236
237
241
242
243
244
245
246
247
248
255
256
257
258
259
260
261
266
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
369
370
371
372
373
374
375
376
377
378
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
418
419
420
421
422
423
424
427
428
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
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
500
501
502
503
504
509
510
511
512
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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
592
593
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
616
617
618
619
620
621
622
623
624
627
628
629
632
633
634
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
712
713
714
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
744
745
746
747
748
749
750
755
762
763
764
765
766
767
768
769
783
784
785
786
787
788
789
790
791
792
793
794
798
799
800
801
802
803
804
811
812
813
817
822
823
824
825
826
827
828
829
830
831
832
833
834
836
837
838
839
840
841
845
846
847
848
849
850
851
852
853
854
855
856
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
884
885
886
887
888
889
890
891
892
893
894
895
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
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#ifdef NX_SECURE_ENABLE_DTLS
#include "nx_secure_dtls.h"
#endif
static UCHAR handshake_hash[64 + 34 + 32];
static UCHAR _nx_secure_padded_signature[600];
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
static const UCHAR _NX_SECURE_OID_SHA256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
#endif
...
...
UINT _nx_secure_tls_send_certificate_verify(NX_SECURE_TLS_SESSION *tls_session,
NX_PACKET *send_packet)
{
UINT length = 0;
UINT data_size = 0;
USHORT signature_algorithm;
UINT signature_length = 0;
UINT i;
UCHAR *current_buffer;
UCHAR *working_ptr;
const NX_CRYPTO_METHOD *public_cipher_method;
const NX_CRYPTO_METHOD *hash_method = NX_NULL;
NX_SECURE_X509_CERT *local_certificate;
NX_SECURE_X509_CRYPTO *crypto_methods;
UINT status;
UINT user_defined_key;
VOID *handler = NX_NULL;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
UINT handshake_hash_length = 0;
CHAR *metadata;
ULONG metadata_size;
const CHAR server_context[] = "TLS 1.3, server CertificateVerify\0";
const CHAR client_context[] = "TLS 1.3, client CertificateVerify\0"; /* ... */
#endif
#ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
const NX_CRYPTO_METHOD *curve_method_cert;
NX_SECURE_EC_PRIVATE_KEY *ec_privkey;
NX_SECURE_EC_PUBLIC_KEY *ec_pubkey;
NX_SECURE_EC_PRIVATE_KEY ec_hardware_privkey;
NX_CRYPTO_EXTENDED_OUTPUT extended_output;/* ... */
#endif
/* ... */
status = _nx_secure_x509_local_device_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store,
NX_NULL, &local_certificate);
/* ... */
if (status)
{
if (status == NX_SECURE_X509_CERTIFICATE_NOT_FOUND)
{
return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
}if (status == NX_SECURE_X509_CERTIFICATE_NOT_FOUND) { ... }
return(status);
}if (status) { ... }
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_1_3)
{
switch (tls_session -> nx_secure_tls_signature_algorithm)
{
case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256:
signature_algorithm = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256;
break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256:
case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384:
signature_algorithm = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384;
break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384:
case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512:
signature_algorithm = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512;
break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512:
default:
return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG);default
}switch (tls_session -> nx_secure_tls_signature_algorithm) { ... }
}if (tls_session -> nx_secure_tls_1_3) { ... }
else
#endif
{
signature_algorithm = NX_SECURE_TLS_X509_TYPE_RSA_SHA_256;
#ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC)
{
signature_algorithm = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256;
}if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... }
/* ... */#endif
}else { ... }
status = _nx_secure_x509_find_certificate_methods(local_certificate, signature_algorithm, &crypto_methods);
if (status != NX_SUCCESS)
{
if (status == NX_SECURE_X509_UNKNOWN_CERT_SIG_ALGORITHM)
{
return(NX_SECURE_TLS_UNKNOWN_CERT_SIG_ALGORITHM);
}if (status == NX_SECURE_X509_UNKNOWN_CERT_SIG_ALGORITHM) { ... }
return(status);
}if (status != NX_SUCCESS) { ... }
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3)
{
/* ... */
UCHAR *transcript_hash = tls_session->nx_secure_tls_key_material.nx_secure_tls_transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_CERTIFICATE];
NX_SECURE_MEMSET(&handshake_hash[0], 0x20, 64);
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT)
{
NX_SECURE_MEMCPY(&handshake_hash[64], client_context, 34);
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) { ... }
else
{
NX_SECURE_MEMCPY(&handshake_hash[64], server_context, 34);
}else { ... }
NX_SECURE_MEMCPY(&handshake_hash[64 + 34], transcript_hash, 32);
handshake_hash_length = 130;
hash_method = crypto_methods -> nx_secure_x509_hash_method;
metadata = tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch;
metadata_size = tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch_size;
if (hash_method -> nx_crypto_init)
{
status = hash_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
&handler,
metadata,
metadata_size);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_init) { ... }
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
NX_NULL,
0,
metadata,
metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
handshake_hash,
handshake_hash_length,
NX_NULL,
NX_NULL,
0,
metadata,
metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
&handshake_hash[0],
sizeof(handshake_hash),
metadata,
metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
handshake_hash_length = (hash_method -> nx_crypto_ICV_size_in_bits) >> 3;
}if (tls_session->nx_secure_tls_1_3) { ... }
else/* ... */
#endif
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_2)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2)
#endif
{
NX_SECURE_HASH_METADATA_CLONE(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size);
hash_method = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_handshake_hash_sha256_method;
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
&handshake_hash[0],
sizeof(handshake_hash),
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size,
NX_NULL,
NX_NULL);
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
...}
/* ... */
#endif
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_0)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1)/* ... */
#endif
{
NX_SECURE_HASH_METADATA_CLONE(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch +
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_md5_metadata,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_md5_metadata_size);
hash_method = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_handshake_hash_md5_method;
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_md5_handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
&handshake_hash[0],
16,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch +
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_md5_metadata_size,
NX_NULL,
NX_NULL);
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch +
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_md5_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
NX_SECURE_HASH_METADATA_CLONE(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size);
hash_method = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_handshake_hash_sha1_method;
if (hash_method -> nx_crypto_operation != NX_NULL)
{
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
&handshake_hash[16],
sizeof(handshake_hash) - 16,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size,
NX_NULL,
NX_NULL);
}if (hash_method -> nx_crypto_operation != NX_NULL) { ... }
NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch,
tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha1_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
...}/* ... */
#endif
if (hash_method == NX_NULL)
{
return(NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION);
}if (hash_method == NX_NULL) { ... }
user_defined_key = NX_FALSE;
if ((local_certificate -> nx_secure_x509_private_key_type & NX_SECURE_X509_KEY_TYPE_USER_DEFINED_MASK) != 0x0)
{
user_defined_key = NX_TRUE;
}if ((local_certificate -> nx_secure_x509_private_key_type & NX_SECURE_X509_KEY_TYPE_USER_DEFINED_MASK) != 0x0) { ... }
public_cipher_method = crypto_methods -> nx_secure_x509_public_cipher_method;
if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA)
{
data_size = local_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length;
if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)) < (4u + data_size))
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
#endif
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)) < (4u + data_size)) { ... }
current_buffer = send_packet -> nx_packet_append_ptr;
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0x0, sizeof(_nx_secure_padded_signature));
length = 0;
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_2)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2)
#endif
{
current_buffer[length] = NX_SECURE_TLS_HASH_ALGORITHM_SHA256;
current_buffer[length + 1] = NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA;
length += 2;
current_buffer[length] = (UCHAR)(data_size >> 8);
current_buffer[length + 1] = (UCHAR)(data_size);
length += 2;
signature_length = 19 + 32;
if (data_size < signature_length)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
#endif
return(NX_SECURE_TLS_INVALID_CERTIFICATE);
}if (data_size < signature_length) { ... }
/* ... */
working_ptr = &_nx_secure_padded_signature[data_size - signature_length];
NX_SECURE_MEMCPY(&working_ptr[0], _NX_SECURE_OID_SHA256, 19);
NX_SECURE_MEMCPY(&working_ptr[19], handshake_hash, 32);
...}/* ... */
#endif
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_0)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1)/* ... */
#endif
{
current_buffer[length] = (UCHAR)(data_size >> 8);
current_buffer[length + 1] = (UCHAR)(data_size);
length += 2;
signature_length = 36;
if (data_size < signature_length)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
#endif
return(NX_SECURE_TLS_INVALID_CERTIFICATE);
}if (data_size < signature_length) { ... }
/* ... */
working_ptr = &_nx_secure_padded_signature[data_size - signature_length];
NX_SECURE_MEMCPY(working_ptr, handshake_hash, 36);
...}/* ... */
#endif
#ifdef NX_SECURE_KEY_CLEAR
/* ... */
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));/* ... */
#endif
/* ... */
_nx_secure_padded_signature[1] = 0x1;
for (i = 2; i < (data_size - signature_length - 1); ++i)
{
_nx_secure_padded_signature[i] = (UCHAR)0xFF;
}for (i = 2; i < (data_size - signature_length - 1); ++i) { ... }
if (user_defined_key)
{
status = public_cipher_method -> nx_crypto_operation(local_certificate -> nx_secure_x509_private_key_type,
NX_NULL,
(NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)local_certificate -> nx_secure_x509_private_key.user_key.key_data,
(NX_CRYPTO_KEY_SIZE)(local_certificate -> nx_secure_x509_private_key.user_key.key_length),
_nx_secure_padded_signature,
length,
NX_NULL,
¤t_buffer[length],
sizeof(_nx_secure_padded_signature),
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (user_defined_key) { ... }
else
{
if (public_cipher_method -> nx_crypto_init != NX_NULL)
{
status = public_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)local_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus,
(NX_CRYPTO_KEY_SIZE)(local_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length << 3),
&handler,
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size);
if(status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_init != NX_NULL) { ... }
if (public_cipher_method -> nx_crypto_operation != NX_NULL)
{
status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
handler,
(NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_exponent,
(NX_CRYPTO_KEY_SIZE)(local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_exponent_length << 3),
_nx_secure_padded_signature,
data_size,
NX_NULL,
¤t_buffer[length],
sizeof(_nx_secure_padded_signature),
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_operation != NX_NULL) { ... }
if (public_cipher_method -> nx_crypto_cleanup)
{
status = public_cipher_method -> nx_crypto_cleanup(local_certificate -> nx_secure_x509_public_cipher_metadata_area);
if(status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_cleanup) { ... }
}else { ... }
length += data_size;
}if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... }
#ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC)
{
current_buffer = send_packet -> nx_packet_append_ptr;
length = 0;
data_size = 0;
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_2)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2)
#endif
{
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_1_3)
{
data_size = handshake_hash_length;
current_buffer[length] = (UCHAR)((tls_session -> nx_secure_tls_signature_algorithm) >> 8);
}if (tls_session -> nx_secure_tls_1_3) { ... }
else
#endif
{
data_size = 32;
current_buffer[length] = NX_SECURE_TLS_HASH_ALGORITHM_SHA256;
}else { ... }
current_buffer[length + 1] = NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA;
length += 2;
...}/* ... */
#endif
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_0)/* ... */
#else
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 ||
tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1)/* ... */
#endif
{
data_size = 36;
...}/* ... */
#endif
if(local_certificate -> nx_secure_x509_private_key_type == NX_SECURE_X509_KEY_TYPE_HARDWARE)
{
ec_hardware_privkey.nx_secure_ec_private_key = local_certificate -> nx_secure_x509_private_key.user_key.key_data;
ec_hardware_privkey.nx_secure_ec_private_key_length = (USHORT)local_certificate -> nx_secure_x509_private_key.user_key.key_length;
ec_hardware_privkey.nx_secure_ec_named_curve = local_certificate -> nx_secure_x509_public_key.ec_public_key.nx_secure_ec_named_curve;
ec_privkey = &ec_hardware_privkey;
}if (local_certificate -> nx_secure_x509_private_key_type == NX_SECURE_X509_KEY_TYPE_HARDWARE) { ... }
else
{
ec_privkey = &local_certificate -> nx_secure_x509_private_key.ec_private_key;
}else { ... }
ec_pubkey = &local_certificate -> nx_secure_x509_public_key.ec_public_key;
status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)(ec_privkey -> nx_secure_ec_named_curve), &curve_method_cert, NX_NULL);
#ifdef NX_SECURE_KEY_CLEAR
if(status != NX_SUCCESS || curve_method_cert == NX_NULL)
{
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
}if (status != NX_SUCCESS || curve_method_cert == NX_NULL) { ... }
/* ... */#endif
if(status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
if (curve_method_cert == NX_NULL)
{
return(NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE);
}if (curve_method_cert == NX_NULL) { ... }
if (public_cipher_method -> nx_crypto_init != NX_NULL)
{
status = public_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)ec_pubkey -> nx_secure_ec_public_key,
(NX_CRYPTO_KEY_SIZE)(ec_pubkey -> nx_secure_ec_public_key_length << 3),
&handler,
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));/* ... */
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_init != NX_NULL) { ... }
if (public_cipher_method -> nx_crypto_operation == NX_NULL)
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}if (public_cipher_method -> nx_crypto_operation == NX_NULL) { ... }
status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler,
(NX_CRYPTO_METHOD*)public_cipher_method, NX_NULL, 0,
(UCHAR *)curve_method_cert, sizeof(NX_CRYPTO_METHOD *), NX_NULL,
NX_NULL, 0,
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));/* ... */
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
extended_output.nx_crypto_extended_output_data = ¤t_buffer[length + 2];
extended_output.nx_crypto_extended_output_length_in_byte =
(ULONG)send_packet -> nx_packet_data_end - (ULONG)¤t_buffer[length + 2];
extended_output.nx_crypto_extended_output_actual_size = 0;
status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, handler,
(NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)ec_privkey -> nx_secure_ec_private_key,
(NX_CRYPTO_KEY_SIZE)(ec_privkey -> nx_secure_ec_private_key_length << 3),
handshake_hash,
data_size, NX_NULL,
(UCHAR *)&extended_output,
sizeof(extended_output),
local_certificate -> nx_secure_x509_public_cipher_metadata_area,
local_certificate -> nx_secure_x509_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));/* ... */
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
if (public_cipher_method -> nx_crypto_cleanup)
{
status = public_cipher_method -> nx_crypto_cleanup(local_certificate -> nx_secure_x509_public_cipher_metadata_area);
if(status != NX_CRYPTO_SUCCESS)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(handshake_hash, 0, sizeof(handshake_hash));
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));/* ... */
#endif
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_cleanup) { ... }
current_buffer[length] = (UCHAR)((extended_output.nx_crypto_extended_output_actual_size & 0xFF00) >> 8);
current_buffer[length + 1] = (UCHAR)(extended_output.nx_crypto_extended_output_actual_size & 0x00FF);
length += extended_output.nx_crypto_extended_output_actual_size + 2;
}else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... }
/* ... */#endif
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_nx_secure_padded_signature, 0, sizeof(_nx_secure_padded_signature));
#endif
send_packet -> nx_packet_append_ptr = send_packet -> nx_packet_append_ptr + (USHORT)(length);
send_packet -> nx_packet_length = send_packet -> nx_packet_length + (USHORT)(length);
return(NX_SECURE_TLS_SUCCESS);
}{ ... }