1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
146
147
148
149
150
151
152
153
154
155
160
161
163
164
165
166
167
168
169
170
171
172
173
174
175
179
180
181
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
205
206
210
211
212
213
214
215
216
217
218
219
223
224
225
226
227
231
232
233
234
235
239
240
245
246
247
248
249
250
251
256
257
258
259
260
261
262
263
264
267
268
270
271
282
289
290
291
292
293
294
295
296
297
298
299
303
304
305
306
310
311
314
315
320
325
326
327
329
330
331
332
333
334
335
336
337
338
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
365
366
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
449
450
459
460
461
462
463
467
468
471
472
473
474
475
477
478
479
480
481
482
483
484
485
486
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
513
514
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
598
599
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
658
659
660
668
669
670
671
672
675
676
678
679
680
684
685
686
691
692
693
701
702
703
707
708
709
710
711
712
713
714
715
716
717
721
722
726
727
728
729
730
731
732
734
735
736
740
741
742
743
744
745
746
747
748
749
750
751
755
756
761
762
763
764
765
766
767
768
769
770
774
775
779
780
781
782
783
784
785
786
787
791
792
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
814
815
819
820
828
829
834
835
836
840
841
842
843
844
845
846
847
848
849
853
854
855
856
857
858
859
860
861
862
866
867
868
869
870
871
872
873
874
875
876
877
878
879
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
902
903
904
905
909
910
911
912
913
914
915
916
917
918
919
926
927
928
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#ifdef NX_SECURE_ENABLE_DTLS
#include "nx_secure_dtls.h"
#endif
#if defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) && !defined(NX_SECURE_TLS_CLIENT_DISABLED)
static UCHAR hash[64];
static UCHAR decrypted_signature[512];/* ... */
#endif
...
...
UINT _nx_secure_tls_process_server_key_exchange(NX_SECURE_TLS_SESSION *tls_session,
UCHAR *packet_buffer, UINT message_length)
{
#if !defined(NX_SECURE_TLS_CLIENT_DISABLED)
#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
USHORT length;
#endif
#if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || \
(defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE))
UINT status;
const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite;/* ... */
#endif
#if defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE)
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;
UCHAR pubkey_length;
UCHAR *pubkey;
USHORT signature_length;
NX_SECURE_X509_CERT *server_certificate;
NX_SECURE_X509_CRYPTO *crypto_methods;
UINT sig_oid_length;
const UCHAR *sig_oid;
UINT decrypted_hash_length;
const UCHAR *decrypted_hash;
UINT compare_result;
NX_SECURE_EC_PUBLIC_KEY *ec_pubkey;
NX_CRYPTO_EXTENDED_OUTPUT extended_output;
UCHAR *current_buffer;
UCHAR hash_algorithm;
UCHAR signature_algorithm;
USHORT signature_algorithm_id;
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
UINT i;
#endif /* ... */
#endif
NX_PARAMETER_NOT_USED(tls_session);
NX_PARAMETER_NOT_USED(packet_buffer);
NX_PARAMETER_NOT_USED(message_length);
#if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || \
(defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE))
ciphersuite = tls_session -> nx_secure_tls_session_ciphersuite;
if (ciphersuite == NX_NULL)
{
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (ciphersuite == NX_NULL) { ... }
/* ... */#endif
#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK)
{
length = (USHORT)((packet_buffer[0] << 8) + (USHORT)packet_buffer[1]);
packet_buffer += 2;
if (length > message_length || length > NX_SECURE_TLS_MAX_PSK_ID_SIZE)
{
return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH);
}if (length > message_length || length > NX_SECURE_TLS_MAX_PSK_ID_SIZE) { ... }
/* ... */
NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_credentials.nx_secure_tls_remote_psk_id, &packet_buffer[0], length);
tls_session -> nx_secure_tls_credentials.nx_secure_tls_remote_psk_id_size = length;
return(NX_SECURE_TLS_SUCCESS);
}if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK) { ... }
/* ... */#endif
#ifdef NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE)
{
if (packet_buffer[0] != 3)
{
return(NX_SECURE_TLS_UNSUPPORTED_ECC_FORMAT);
}if (packet_buffer[0] != 3) { ... }
if (packet_buffer[1] != 0 || packet_buffer[2] != 23)
{
return(NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE);
}if (packet_buffer[1] != 0 || packet_buffer[2] != 23) { ... }
packet_buffer += 3;
tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = 32;
status = ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_operation(NX_CRYPTO_ECJPAKE_SERVER_KEY_EXCHANGE_PROCESS,
tls_session -> nx_secure_public_auth_handler,
(NX_CRYPTO_METHOD*)ciphersuite -> nx_secure_tls_public_auth,
NX_NULL, 0,
packet_buffer,
message_length,
NX_NULL,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret,
tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size,
tls_session -> nx_secure_public_auth_metadata_area,
tls_session -> nx_secure_public_auth_metadata_size,
NX_NULL, NX_NULL);
if (status)
{
return(status);
}if (status) { ... }
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_cleanup)
{
status = ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_cleanup(tls_session -> nx_secure_public_auth_metadata_area);
}if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_cleanup) { ... }
return(status);
}if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE) { ... }
/* ... */#endif
#if defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE)
if (ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE)
{
if (tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_SERVER_CERTIFICATE)
{
return(NX_SECURE_TLS_UNEXPECTED_MESSAGE);
}if (tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_SERVER_CERTIFICATE) { ... }
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_SERVER_KEY_EXCHANGE;
if (packet_buffer[0] != 3)
{
return(NX_SECURE_TLS_UNSUPPORTED_ECC_FORMAT);
}if (packet_buffer[0] != 3) { ... }
status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)((packet_buffer[1] << 8) + packet_buffer[2]), &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) { ... }
current_buffer = &packet_buffer[3];
status = _nx_secure_x509_remote_endpoint_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store,
&server_certificate);
if (status || server_certificate == NX_NULL)
{
return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
}if (status || server_certificate == NX_NULL) { ... }
pubkey_length = current_buffer[0];
pubkey = ¤t_buffer[1];
current_buffer += pubkey_length + 1;
#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
{
hash_algorithm = NX_SECURE_TLS_HASH_ALGORITHM_SHA1;
if (server_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC)
{
signature_algorithm = NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA;
}if (server_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... }
else
{
signature_algorithm = NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA;
}else { ... }
...}
else
#endif
{
hash_algorithm = current_buffer[0];
signature_algorithm = current_buffer[1];
current_buffer += 2;
}else { ... }
_nx_secure_tls_get_signature_algorithm_id(((UINT)(hash_algorithm << 8) + signature_algorithm),
&signature_algorithm_id);
status = _nx_secure_x509_find_certificate_methods(server_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 (signature_algorithm == 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 (signature_algorithm == 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 { ... }
/* ... */
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,
packet_buffer,
(ULONG)(4 + pubkey_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_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;
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
#ifdef NX_SECURE_ENABLE_DTLS
if (signature_algorithm == 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 (signature_algorithm == 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,
packet_buffer,
(ULONG)(4 + pubkey_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
signature_length = (USHORT)((current_buffer[0] << 8) + current_buffer[1]);
current_buffer += 2;
auth_method = ciphersuite -> nx_secure_tls_public_auth;
if (signature_algorithm == 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))
{
if (auth_method -> nx_crypto_init != NX_NULL)
{
status = auth_method -> nx_crypto_init((NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)server_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus,
(NX_CRYPTO_KEY_SIZE)(server_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_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA)
{
status = auth_method -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
handler,
(NX_CRYPTO_METHOD*)auth_method,
(UCHAR *)server_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent,
(NX_CRYPTO_KEY_SIZE)(server_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent_length << 3),
current_buffer,
signature_length,
NX_NULL,
decrypted_signature,
sizeof(decrypted_signature),
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_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA) { ... }
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) { ... }
handler = NX_NULL;
#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
{
if (signature_length < 39)
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (signature_length < 39) { ... }
if (decrypted_signature[0] != 0x0 && decrypted_signature[1] != 0x1)
{
return(NX_SECURE_TLS_PADDING_CHECK_FAILED);
}if (decrypted_signature[0] != 0x0 && decrypted_signature[1] != 0x1) { ... }
for (i = 2; i < (UINT)(signature_length - 37); ++i)
{
if (decrypted_signature[i] != (UCHAR)0xFF)
{
return(NX_SECURE_TLS_PADDING_CHECK_FAILED);
}if (decrypted_signature[i] != (UCHAR)0xFF) { ... }
}for (i = 2; i < (UINT)(signature_length - 37); ++i) { ... }
if (decrypted_signature[i] != 0x00)
{
return(NX_SECURE_TLS_PADDING_CHECK_FAILED);
}if (decrypted_signature[i] != 0x00) { ... }
decrypted_hash = &decrypted_signature[i + 1];
decrypted_hash_length = 36;
...}
else
#endif
{
status = _nx_secure_x509_pkcs7_decode(decrypted_signature, signature_length, &sig_oid, &sig_oid_length,
&decrypted_hash, &decrypted_hash_length);
if (status != NX_SUCCESS)
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (status != NX_SUCCESS) { ... }
if (decrypted_hash_length != (hash_method -> nx_crypto_ICV_size_in_bits >> 3))
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (decrypted_hash_length != (hash_method -> nx_crypto_ICV_size_in_bits >> 3)) { ... }
}else { ... }
compare_result = (UINT)NX_SECURE_MEMCMP(hash, decrypted_hash, decrypted_hash_length);
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(hash, 0, sizeof(hash));
NX_SECURE_MEMSET(decrypted_signature, 0, sizeof(decrypted_signature));/* ... */
#endif
if (compare_result != 0)
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (compare_result != 0) { ... }
}if (signature_algorithm == 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 (signature_algorithm == NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA &&
auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA)
{
ec_pubkey = &server_certificate -> nx_secure_x509_public_key.ec_public_key;
status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)(ec_pubkey -> 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) { ... }
if (packet_buffer + message_length < current_buffer + signature_length)
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (packet_buffer + message_length < current_buffer + signature_length) { ... }
status = auth_method -> nx_crypto_operation(NX_CRYPTO_VERIFY, handler,
(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),
hash,
hash_method -> nx_crypto_ICV_size_in_bits >> 3,
NX_NULL,
current_buffer,
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_AUTHENTICATION_FAILED)
{
return(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR);
}if (status == NX_CRYPTO_AUTHENTICATION_FAILED) { ... }
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) { ... }
}else if (signature_algorithm == NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA && auth_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { ... }
else
{
return(NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM);
}else { ... }
ecdhe_method = ciphersuite -> nx_secure_tls_public_cipher;
if (ecdhe_method -> nx_crypto_operation == NX_NULL)
{
return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
}if (ecdhe_method -> nx_crypto_operation == 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) { ... }
extended_output.nx_crypto_extended_output_data = &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[1];
extended_output.nx_crypto_extended_output_length_in_byte = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data) - 1;
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) { ... }
tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[0] = (UCHAR)extended_output.nx_crypto_extended_output_actual_size;
extended_output.nx_crypto_extended_output_data = tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret;
extended_output.nx_crypto_extended_output_length_in_byte = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret);
extended_output.nx_crypto_extended_output_actual_size = 0;
status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_DH_CALCULATE, handler,
(NX_CRYPTO_METHOD*)ecdhe_method, NX_NULL, 0,
pubkey, pubkey_length, 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) { ... }
tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = 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 (ecdhe_method -> nx_crypto_cleanup) { ... }
return(status);
}if (ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE) { ... }
/* ... */
#endif
return(NX_SECURE_TLS_UNEXPECTED_MESSAGE);
/* ... */
#else
NX_PARAMETER_NOT_USED(tls_session);
NX_PARAMETER_NOT_USED(packet_buffer);
NX_PARAMETER_NOT_USED(message_length);
return(NX_SECURE_TLS_UNEXPECTED_MESSAGE);/* ... */
#endif
}{ ... }