1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
131
132
133
134
140
141
142
143
144
145
146
150
151
152
153
154
155
156
160
164
165
166
167
168
169
170
171
172
173
174
178
179
180
181
182
183
184
185
186
187
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
220
226
227
228
229
230
231
232
233
234
235
239
240
241
242
243
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
269
270
271
272
273
274
282
283
284
285
286
287
291
301
302
307
308
309
310
311
312
313
314
315
316
317
318
322
323
324
325
329
330
333
334
339
344
345
346
347
349
350
351
352
353
354
355
356
357
358
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
385
386
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
470
471
480
481
482
483
484
488
489
492
493
494
495
496
498
499
500
501
502
503
504
505
506
507
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
534
535
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
619
620
629
630
631
632
633
634
642
643
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
670
671
674
675
679
680
681
682
683
684
688
692
696
700
704
708
710
711
712
713
714
718
719
720
721
722
723
724
725
726
727
728
729
730
731
736
737
738
739
740
741
742
743
744
745
746
747
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
773
774
775
776
777
786
787
788
789
790
791
792
793
794
795
799
805
806
807
808
809
810
811
812
813
814
818
819
823
824
825
826
827
828
829
830
831
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
855
856
865
866
867
868
869
870
871
872
878
879
880
881
882
883
884
885
886
887
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#ifdef NX_SECURE_ENABLE_DTLS
#include "nx_secure_dtls.h"
#endif
#ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
static UCHAR hash[64];
static UCHAR _nx_secure_padded_signature[512];
/* ... */
static const UCHAR _NX_CRYPTO_DER_OID_MD5[] = {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10};
static const UCHAR _NX_CRYPTO_DER_OID_SHA_1[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
static const UCHAR _NX_CRYPTO_DER_OID_SHA_224[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c};
static const UCHAR _NX_CRYPTO_DER_OID_SHA_256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
static const UCHAR _NX_CRYPTO_DER_OID_SHA_384[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30};
static const UCHAR _NX_CRYPTO_DER_OID_SHA_512[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
...
UINT _nx_secure_tls_ecc_generate_keys(NX_SECURE_TLS_SESSION *tls_session, UINT ecc_named_curve, USHORT sign_key,
UCHAR *public_key, UINT *public_key_size, NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecc_data)
{
UINT length;
UINT output_size;
UINT status;
NX_CRYPTO_EXTENDED_OUTPUT extended_output;
USHORT signature_length;
UINT signature_offset;
const UCHAR *der_encoding = NX_NULL;
UINT der_encoding_length = 0;
UINT hash_length;
const NX_CRYPTO_METHOD *curve_method;
const NX_CRYPTO_METHOD *curve_method_cert;
const NX_CRYPTO_METHOD *ecdhe_method;
const NX_CRYPTO_METHOD *hash_method;
const NX_CRYPTO_METHOD *auth_method;
VOID *handler = NX_NULL;
NX_SECURE_X509_CERT *certificate;
NX_SECURE_X509_CRYPTO *crypto_methods;
NX_SECURE_EC_PRIVATE_KEY *ec_privkey;
NX_SECURE_EC_PUBLIC_KEY *ec_pubkey;
USHORT signature_algorithm_id;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3)
{
ecdhe_method = tls_session->nx_secure_tls_crypto_table->nx_secure_tls_ecdhe_method;
}if (tls_session->nx_secure_tls_1_3) { ... }
else
#endif
{
if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL)
{
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... }
ecdhe_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher;
}else { ... }
if (ecdhe_method == NX_NULL || ecdhe_method -> nx_crypto_operation == NX_NULL)
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}if (ecdhe_method == NX_NULL || ecdhe_method -> nx_crypto_operation == NX_NULL) { ... }
ecc_data->nx_secure_tls_ecdhe_named_curve = ecc_named_curve;
status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)ecc_named_curve, &curve_method, NX_NULL);
if(status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
if (curve_method == NX_NULL)
{
return(NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE);
}if (curve_method == NX_NULL) { ... }
if (ecdhe_method -> nx_crypto_init != NX_NULL)
{
status = ecdhe_method -> nx_crypto_init((NX_CRYPTO_METHOD*)ecdhe_method,
NX_NULL,
0,
&handler,
tls_session -> nx_secure_public_cipher_metadata_area,
tls_session -> nx_secure_public_cipher_metadata_size);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (ecdhe_method -> nx_crypto_init != NX_NULL) { ... }
status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler,
(NX_CRYPTO_METHOD*)ecdhe_method, NX_NULL, 0,
(UCHAR *)curve_method, sizeof(NX_CRYPTO_METHOD *), NX_NULL,
NX_NULL, 0,
tls_session -> nx_secure_public_cipher_metadata_area,
tls_session -> nx_secure_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
length = 0;
output_size = *public_key_size;
*public_key_size = 0;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(!tls_session->nx_secure_tls_1_3)
#endif
{
public_key[length] = 3;
length += 1;
public_key[length] = (UCHAR)((ecc_data -> nx_secure_tls_ecdhe_named_curve & 0xFF00) >> 8);
public_key[length + 1] = (UCHAR)(ecc_data -> nx_secure_tls_ecdhe_named_curve & 0x00FF);
length += 2;
...}
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3)
{
extended_output.nx_crypto_extended_output_data = &public_key[length];
extended_output.nx_crypto_extended_output_length_in_byte = output_size - length;
}if (tls_session->nx_secure_tls_1_3) { ... }
else
#endif
{
extended_output.nx_crypto_extended_output_data = &public_key[length + 1];
extended_output.nx_crypto_extended_output_length_in_byte = output_size - (length + 1);
}else { ... }
extended_output.nx_crypto_extended_output_actual_size = 0;
status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_DH_SETUP, handler,
(NX_CRYPTO_METHOD*)ecdhe_method, NX_NULL, 0,
NX_NULL, 0, NX_NULL,
(UCHAR *)&extended_output,
sizeof(extended_output),
tls_session -> nx_secure_public_cipher_metadata_area,
tls_session -> nx_secure_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(!tls_session->nx_secure_tls_1_3)
#endif
{
public_key[length] = (UCHAR)extended_output.nx_crypto_extended_output_actual_size;
length += 1;
...}
length += (UINT)(extended_output.nx_crypto_extended_output_actual_size);
extended_output.nx_crypto_extended_output_data = ecc_data -> nx_secure_tls_ecdhe_private_key;
extended_output.nx_crypto_extended_output_length_in_byte =
sizeof(ecc_data -> nx_secure_tls_ecdhe_private_key);
extended_output.nx_crypto_extended_output_actual_size = 0;
status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_DH_PRIVATE_KEY_EXPORT, handler,
(NX_CRYPTO_METHOD*)ecdhe_method, NX_NULL, 0,
NX_NULL, 0, NX_NULL,
(UCHAR *)&extended_output,
sizeof(extended_output),
tls_session -> nx_secure_public_cipher_metadata_area,
tls_session -> nx_secure_public_cipher_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
ecc_data -> nx_secure_tls_ecdhe_private_key_length = (USHORT)extended_output.nx_crypto_extended_output_actual_size;
if (ecdhe_method -> nx_crypto_cleanup)
{
status = ecdhe_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_cipher_metadata_area);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (ecdhe_method -> nx_crypto_cleanup) { ... }
if(sign_key == NX_TRUE)
{
if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL)
{
certificate = tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate;
}if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { ... }
else
{
status = _nx_secure_x509_local_device_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store,
NX_NULL, &certificate);
if (status != NX_SUCCESS)
{
certificate = NX_NULL;
}if (status != NX_SUCCESS) { ... }
}else { ... }
if (certificate == NX_NULL)
{
return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
}if (certificate == NX_NULL) { ... }
_nx_secure_tls_get_signature_algorithm_id((UINT)(ecc_data -> nx_secure_tls_ecdhe_signature_algorithm),
&signature_algorithm_id);
status = _nx_secure_x509_find_certificate_methods(certificate,
signature_algorithm_id,
&crypto_methods);
if (status)
{
return(NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM);
}if (status) { ... }
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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 ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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
{
hash_method = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_handshake_hash_md5_method;
...}
else
#endif
{
hash_method = crypto_methods -> nx_secure_x509_hash_method;
}else { ... }
hash_length = hash_method -> nx_crypto_ICV_size_in_bits >> 3;
/* ... */
if (hash_method -> nx_crypto_init)
{
status = hash_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
&handler,
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_scratch_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,
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_scratch_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) { ... }
else
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}else { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random,
32,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random,
32,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
public_key,
length,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
hash,
hash_length,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
if (hash_method -> nx_crypto_cleanup)
{
status = hash_method -> nx_crypto_cleanup(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_cleanup) { ... }
handler = NX_NULL;
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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 ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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
{
hash_method = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_handshake_hash_sha1_method;;
/* ... */
if (hash_method -> nx_crypto_init)
{
status = hash_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
&handler,
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_scratch_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,
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_scratch_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) { ... }
else
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}else { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random,
32,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random,
32,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
public_key,
length,
NX_NULL,
NX_NULL,
0,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
handler,
(NX_CRYPTO_METHOD*)hash_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
&hash[16],
hash_method -> nx_crypto_ICV_size_in_bits >> 3,
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_scratch_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
if (hash_method -> nx_crypto_cleanup)
{
status = hash_method -> nx_crypto_cleanup(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_cleanup) { ... }
handler = NX_NULL;
...}/* ... */
#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 /* ... */
#endif
{
public_key[length] = (UCHAR)((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF00) >> 8);
public_key[length + 1] = (UCHAR)(ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0x00FF);
length += 2;
...}
auth_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth;
if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_RSA ||
auth_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA))
{
signature_length = certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length;
public_key[length] = (UCHAR)((signature_length & 0xFF00) >> 8);
public_key[length + 1] = (UCHAR)(signature_length & 0x00FF);
length += 2;
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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 ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA &&
(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
{
hash_length += hash_method -> nx_crypto_ICV_size_in_bits >> 3;
der_encoding_length = 0;
...}
else
#endif
{
switch ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF00) >> 8)
{
case NX_SECURE_TLS_HASH_ALGORITHM_MD5:
der_encoding = _NX_CRYPTO_DER_OID_MD5;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_MD5);
break;case NX_SECURE_TLS_HASH_ALGORITHM_MD5:
case NX_SECURE_TLS_HASH_ALGORITHM_SHA1:
der_encoding = _NX_CRYPTO_DER_OID_SHA_1;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_1);
break;case NX_SECURE_TLS_HASH_ALGORITHM_SHA1:
case NX_SECURE_TLS_HASH_ALGORITHM_SHA224:
der_encoding = _NX_CRYPTO_DER_OID_SHA_224;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_224);
break;case NX_SECURE_TLS_HASH_ALGORITHM_SHA224:
case NX_SECURE_TLS_HASH_ALGORITHM_SHA256:
der_encoding = _NX_CRYPTO_DER_OID_SHA_256;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_256);
break;case NX_SECURE_TLS_HASH_ALGORITHM_SHA256:
case NX_SECURE_TLS_HASH_ALGORITHM_SHA384:
der_encoding = _NX_CRYPTO_DER_OID_SHA_384;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_384);
break;case NX_SECURE_TLS_HASH_ALGORITHM_SHA384:
case NX_SECURE_TLS_HASH_ALGORITHM_SHA512:
der_encoding = _NX_CRYPTO_DER_OID_SHA_512;
der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_512);
break;case NX_SECURE_TLS_HASH_ALGORITHM_SHA512:
default:
return(NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM);default
}switch ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF00) >> 8) { ... }
}else { ... }
/* ... */
signature_offset = signature_length - (der_encoding_length + hash_length);
if ((signature_offset > sizeof(_nx_secure_padded_signature)) ||
(signature_length > sizeof(_nx_secure_padded_signature)) ||
(signature_offset == 0))
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if ((signature_offset > sizeof(_nx_secure_padded_signature)) || (signature_length > sizeof(_nx_secure_padded_signature)) || (signature_offset == 0)) { ... }
NX_CRYPTO_MEMSET(_nx_secure_padded_signature, 0xff, signature_offset);
_nx_secure_padded_signature[0] = 0x0;
_nx_secure_padded_signature[1] = 0x1;
_nx_secure_padded_signature[signature_offset - 1] = 0x0;
if (der_encoding_length > 0)
{
NX_CRYPTO_MEMCPY(&_nx_secure_padded_signature[signature_offset], der_encoding, der_encoding_length);
signature_offset += der_encoding_length;
}if (der_encoding_length > 0) { ... }
NX_CRYPTO_MEMCPY(&_nx_secure_padded_signature[signature_offset], hash, hash_length);
if (auth_method -> nx_crypto_init != NX_NULL)
{
status = auth_method -> nx_crypto_init((NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus,
(NX_CRYPTO_KEY_SIZE)(certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length << 3),
&handler,
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (auth_method -> nx_crypto_init != NX_NULL) { ... }
if (auth_method -> nx_crypto_operation != NX_NULL)
{
status = auth_method -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
handler,
(NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_exponent,
(NX_CRYPTO_KEY_SIZE)(certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_exponent_length << 3),
_nx_secure_padded_signature,
signature_length,
NX_NULL,
&public_key[length],
signature_length,
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (auth_method -> nx_crypto_operation != NX_NULL) { ... }
length += signature_length;
if (auth_method -> nx_crypto_cleanup)
{
status = auth_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_auth_metadata_area);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (auth_method -> nx_crypto_cleanup) { ... }
}if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA && (auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_RSA || auth_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA)) { ... }
else if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA &&
auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA)
{
ec_privkey = &certificate -> nx_secure_x509_private_key.ec_private_key;
ec_pubkey = &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);
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 (auth_method -> nx_crypto_init != NX_NULL)
{
status = auth_method -> nx_crypto_init((NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)ec_pubkey -> nx_secure_ec_public_key,
(NX_CRYPTO_KEY_SIZE)(ec_pubkey -> nx_secure_ec_public_key_length << 3),
&handler,
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (auth_method -> nx_crypto_init != NX_NULL) { ... }
if (auth_method -> nx_crypto_operation == NX_NULL)
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}if (auth_method -> nx_crypto_operation == NX_NULL) { ... }
status = auth_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler,
(NX_CRYPTO_METHOD*)auth_method, NX_NULL, 0,
(UCHAR *)curve_method_cert, sizeof(NX_CRYPTO_METHOD *), NX_NULL,
NX_NULL, 0,
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
extended_output.nx_crypto_extended_output_data = &public_key[length + 2];
extended_output.nx_crypto_extended_output_length_in_byte = output_size - length;
extended_output.nx_crypto_extended_output_actual_size = 0;
status = auth_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, handler,
(NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)ec_privkey -> nx_secure_ec_private_key,
(NX_CRYPTO_KEY_SIZE)(ec_privkey -> nx_secure_ec_private_key_length << 3),
hash,
hash_method -> nx_crypto_ICV_size_in_bits >> 3, NX_NULL,
(UCHAR *)&extended_output,
sizeof(extended_output),
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size,
NX_NULL, NX_NULL);
if (status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
if (auth_method -> nx_crypto_cleanup)
{
status = auth_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_auth_metadata_area);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (auth_method -> nx_crypto_cleanup) { ... }
public_key[length] = (UCHAR)((extended_output.nx_crypto_extended_output_actual_size & 0xFF00) >> 8);
public_key[length + 1] = (UCHAR)(extended_output.nx_crypto_extended_output_actual_size & 0x00FF);
length += extended_output.nx_crypto_extended_output_actual_size + 2;
}else if ((ecc_data -> nx_secure_tls_ecdhe_signature_algorithm & 0xFF) == NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA && auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { ... }
else
{
*public_key_size = 0;
return(NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM);
}else { ... }
}if (sign_key == NX_TRUE) { ... }
*public_key_size = length;
return(NX_SUCCESS);
}{ ... }
#endif/* ... */