Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
_nx_secure_tls_process_clienthello(NX_SECURE_TLS_SESSION *, UCHAR *, UINT)
...
...
_nx_secure_tls_check_ciphersuite(const NX_SECURE_TLS_CIPHERSUITE_INFO *, NX_SECURE_X509_CERT *, UINT, UINT)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_process_clienthello.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Secure Component */ /** */ /** Transport Layer Security (TLS) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE static UINT _nx_secure_tls_check_ciphersuite(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite_info, NX_SECURE_X509_CERT *cert, UINT selected_curve, UINT cert_curve_supported); /* ... */ #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_process_clienthello PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes an incoming ClientHello message from a */ /* remote host, kicking off a TLS handshake. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Pointer to message data */ /* message_length Length of message data (bytes)*/ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tls_ciphersuite_lookup Lookup current ciphersuite */ /* _nx_secure_tls_check_protocol_version Check incoming TLS version */ /* _nx_secure_tls_newest_supported_version */ /* Get newest TLS version */ /* _nx_secure_tls_process_clienthello_extensions */ /* Process ClientHello extensions*/ /* _nx_secure_tls_proc_clienthello_sec_sa_extension */ /* Process ECC extensions */ /* _nx_secure_tls_check_ciphersuite Check if ECC suite is usable */ /* _nx_secure_tls_remote_certificate_free_all */ /* Free all remote certificates */ /* [_nx_secure_tls_session_renegotiate_callback_set] */ /* Renegotiation callback */ /* [_nx_secure_tls_session_server_callback_set] */ /* Server session callback */ /* _nx_secure_x509_local_device_certificate_get */ /* Get the local certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_server_handshake TLS Server state machine */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), added */ /* priority ciphersuite logic, */ /* verified memcpy use cases, */ /* fixed renegotiation bug, */ /* improved negotiation logic, */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), added */ /* ability to disable client */ /* initiated renegotiation, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_process_clienthello(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT message_length) { UINT length; USHORT ciphersuite_list_length; UCHAR compression_methods_length; USHORT cipher_entry; UCHAR session_id_length; UINT i; UINT status; USHORT protocol_version; USHORT newest_version; UINT total_extensions_length; const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite_info; USHORT ciphersuite_priority; USHORT new_ciphersuite_priority = 0; NX_SECURE_TLS_HELLO_EXTENSION extension_data[NX_SECURE_TLS_HELLO_EXTENSIONS_MAX]; UINT num_extensions = NX_SECURE_TLS_HELLO_EXTENSIONS_MAX; UCHAR *ciphersuite_list; #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE NX_SECURE_X509_CERT *cert; UINT selected_curve; UINT cert_curve; UINT cert_curve_supported; NX_SECURE_EC_PUBLIC_KEY *ec_pubkey; NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecdhe_data;/* ... */ #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) USHORT tls_1_3 = tls_session -> nx_secure_tls_1_3; #endif /* Structure of ClientHello: * | 2 | 4 + 28 | 1 | <SID len> | 2 | <CS Len> | 1 | <Comp Len> | 2 | <Ext. Len> | * | TLS version | Random (time + random) | SID length | Session ID | CS Len | Ciphersuites |Comp Len | Compression |Ext. Len | Extensions | *//* ... */ if (message_length < 38) { /* Message was not the minimum required size for a ClientHello. */ return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (message_length < 38) { ... } /* Use our length as an index into the buffer. */ length = 0; /* If we are currently in a session, we have a renegotiation handshake. */ if (tls_session -> nx_secure_tls_local_session_active) { #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session->nx_secure_tls_1_3 == NX_TRUE) { /* RFC 8446, section 4.1.2, page 27. * Server has negotiated TLS 1.3 and receives a ClientHello again. * Send an unexpected message alert. *//* ... */ return(NX_SECURE_TLS_UNEXPECTED_CLIENTHELLO); }if (tls_session->nx_secure_tls_1_3 == NX_TRUE) { ... } /* ... */#endif #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) /* If Client initiated renegotiation is enabled, handle the renegotiation handshake if appropriate. Otherwise return "no renegotiation allowed" error. *//* ... */ if (tls_session -> nx_secure_tls_renegotation_enabled && tls_session -> nx_secure_tls_secure_renegotiation #if defined(NX_SECURE_TLS_DISABLE_CLIENT_INITIATED_RENEGOTIATION) /* If client initiated renegotiation is disabled for TLS servers, only allow server-initiated renegotiations requested by the server itself. *//* ... */ && tls_session -> nx_secure_tls_server_renegotiation_requested/* ... */ #endif ) { tls_session -> nx_secure_tls_renegotiation_handshake = NX_TRUE; /* On a session resumption free all certificates for the new session. * SESSION RESUMPTION: if session resumption is enabled, don't free!! *//* ... */ status = _nx_secure_tls_remote_certificate_free_all(tls_session); if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* Invoke user callback to notify application of renegotiation request. */ if (tls_session -> nx_secure_tls_session_renegotiation_callback != NX_NULL) { status = tls_session -> nx_secure_tls_session_renegotiation_callback(tls_session); if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } }if (tls_session -> nx_secure_tls_session_renegotiation_callback != NX_NULL) { ... } /* We don't want any more clienthellos to come in after this - only the first one in response to our original request. *//* ... */ tls_session -> nx_secure_tls_server_renegotiation_requested = NX_FALSE; }if (tls_session -> nx_secure_tls_renegotation_enabled && tls_session -> nx_secure_tls_secure_renegotiation #if defined(NX_SECURE_TLS_DISABLE_CLIENT_INITIATED_RENEGOTIATION) /* If client initiated renegotiation is disabled for TLS servers, only allow server-initiated renegotiations requested by the server itself. */ && tls_session -> nx_secure_tls_server_renegotiation_requested #endif) { ... } else #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ { /* Session renegotiation is disabled, so this is an error! */ return(NX_SECURE_TLS_NO_RENEGOTIATION_ERROR); }else { ... } }if (tls_session -> nx_secure_tls_local_session_active) { ... } /* Client is establishing a TLS session with our server. */ /* Extract the protocol version - only part of the ClientHello message. */ protocol_version = (USHORT)((packet_buffer[length] << 8) | packet_buffer[length + 1]); length += 2; #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session->nx_secure_tls_1_3 == NX_FALSE) #endif { /* Check protocol version provided by client. */ status = _nx_secure_tls_check_protocol_version(tls_session, protocol_version, NX_SECURE_TLS); if (status != NX_SECURE_TLS_SUCCESS) { /* If we have an active session, this is a renegotiation attempt, treat the protocol error as if we are starting a new session. *//* ... */ if (status == NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION || tls_session -> nx_secure_tls_local_session_active) { /* If the version isn't supported, it's not an issue - TLS is backward-compatible, * so negotiate the highest supported version. If the version isn't recognized, * flag an error. *//* ... */ _nx_secure_tls_highest_supported_version_negotiate(tls_session, &protocol_version, NX_SECURE_TLS); if (protocol_version == 0x0) { /* Error, no versions enabled. */ return(NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION); }if (protocol_version == 0x0) { ... } }if (status == NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION || tls_session -> nx_secure_tls_local_session_active) { ... } else { /* Protocol version unknown (not TLS or SSL!), return status. */ return(status); }else { ... } }if (status != NX_SECURE_TLS_SUCCESS) { ... } ...} /* Assign our protocol version to our socket. This is used for all further communications * in this session. *//* ... */ tls_session -> nx_secure_tls_protocol_version = protocol_version; /* Save off the random value for key generation later. */ NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random, &packet_buffer[length], NX_SECURE_TLS_RANDOM_SIZE); /* Use case of memcpy is verified. */ length += NX_SECURE_TLS_RANDOM_SIZE; /* Extract the session ID if there is one. */ session_id_length = packet_buffer[length]; length++; if ((length + session_id_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((length + session_id_length) > message_length) { ... } /* If there is a session ID, copy it into our TLS socket structure. */ tls_session -> nx_secure_tls_session_id_length = session_id_length; if (session_id_length > 0) { NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_session_id, &packet_buffer[length], session_id_length); /* Use case of memcpy is verified. */ length += session_id_length; }if (session_id_length > 0) { ... } /* Negotiate the ciphersuite we want to use. */ ciphersuite_list_length = (USHORT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length += 2; /* Make sure the list length makes sense. */ if (ciphersuite_list_length < 2 || (length + ciphersuite_list_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (ciphersuite_list_length < 2 || (length + ciphersuite_list_length) > message_length) { ... } ciphersuite_list = &packet_buffer[length]; length += ciphersuite_list_length; /* Compression methods length - one byte. For now we only support the NULL method. */ compression_methods_length = packet_buffer[length]; length++; /* Message length overflow. */ if ((length + compression_methods_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((length + compression_methods_length) > message_length) { ... } /* Make sure NULL compression method is supported. */ status = NX_SECURE_TLS_BAD_COMPRESSION_METHOD; for (i = 0; i < compression_methods_length; ++i) { if (packet_buffer[length + i] == 0x0) { status = NX_SUCCESS; break; }if (packet_buffer[length + i] == 0x0) { ... } }for (i = 0; i < compression_methods_length; ++i) { ... } /* Check for error. */ if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } length += compression_methods_length; /* Padding data? */ if (message_length >= (length + 2)) { /* TLS Extensions come next. Get the total length of all extensions first. */ total_extensions_length = (USHORT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length += 2; /* Message length overflow. */ if ((length + total_extensions_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((length + total_extensions_length) > message_length) { ... } if (total_extensions_length > 0) { /* Process serverhello extensions. */ status = _nx_secure_tls_process_clienthello_extensions(tls_session, &packet_buffer[length], total_extensions_length, extension_data, &num_extensions, packet_buffer, message_length); /* Check for error. */ if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session -> nx_secure_tls_1_3 != tls_1_3) { /* Negotiate a version of TLS prior to TLS 1.3. */ return(status); }if (tls_session -> nx_secure_tls_1_3 != tls_1_3) { ... } /* ... */#endif /* If the server callback is set, invoke it now with the extensions that require application input. */ if (tls_session -> nx_secure_tls_session_server_callback != NX_NULL) { status = tls_session -> nx_secure_tls_session_server_callback(tls_session, extension_data, num_extensions); /* Check for error. */ if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } }if (tls_session -> nx_secure_tls_session_server_callback != NX_NULL) { ... } }if (total_extensions_length > 0) { ... } }if (message_length >= (length + 2)) { ... } #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) if ((tls_session -> nx_secure_tls_renegotiation_handshake) && (!tls_session -> nx_secure_tls_secure_renegotiation_verified)) { /* The server did not receive the "renegotiation_info" extension, the handshake must be aborted. */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if ((tls_session -> nx_secure_tls_renegotiation_handshake) && (!tls_session -> nx_secure_tls_secure_renegotiation_verified)) { ... } tls_session -> nx_secure_tls_secure_renegotiation_verified = NX_FALSE;/* ... */ #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE /* Get the local certificate. */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { cert = 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 { /* Get reference to local device certificate. NX_NULL is passed for name to get default entry. */ status = _nx_secure_x509_local_device_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store, NX_NULL, &cert); if (status != NX_SUCCESS) { cert = NX_NULL; }if (status != NX_SUCCESS) { ... } }else { ... } if (cert != NX_NULL && cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ec_pubkey = &cert -> nx_secure_x509_public_key.ec_public_key; cert_curve = ec_pubkey -> nx_secure_ec_named_curve; }if (cert != NX_NULL && cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... } else { cert_curve = 0; }else { ... } ecdhe_data = (NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *)tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data; /* Parse the ECC extension of supported curve. */ status = _nx_secure_tls_proc_clienthello_sec_sa_extension(tls_session, extension_data, num_extensions, &selected_curve, (USHORT)cert_curve, &cert_curve_supported, &ecdhe_data -> nx_secure_tls_ecdhe_signature_algorithm, cert); /* Check for error. */ if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } ecdhe_data -> nx_secure_tls_ecdhe_named_curve = selected_curve; /* Check if certificate curve is supported. */ if (cert != NX_NULL && cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { if (cert_curve_supported == NX_FALSE) { /* The named curve in our server certificate is not supported by the client. */ return(NX_SECURE_TLS_NO_SUPPORTED_CIPHERS); }if (cert_curve_supported == NX_FALSE) { ... } }if (cert != NX_NULL && cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ /* Set our initial priority to the maximum value - the size of our ciphersuite crypto table. */ ciphersuite_priority = (USHORT)(0xFFFFFFFF); for (i = 0; i < ciphersuite_list_length; i += 2) { /* Loop through list of acceptable ciphersuites. */ cipher_entry = (USHORT)((ciphersuite_list[i] << 8) + ciphersuite_list[i + 1]); #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if ((tls_session -> nx_secure_tls_1_3 == NX_TRUE) ^ (ciphersuite_list[i] == 0x13)) { /* Ciphersuites for TLS 1.3 can not be used by TLS 1.2. */ continue; }if ((tls_session -> nx_secure_tls_1_3 == NX_TRUE) ^ (ciphersuite_list[i] == 0x13)) { ... } /* ... */#endif /* Look up the ciphersuite in our crypto table, then check the priority of the chosen ciphersuite. */ status = _nx_secure_tls_ciphersuite_lookup(tls_session, cipher_entry, &ciphersuite_info, &new_ciphersuite_priority); /* Save the first ciphersuite we find - assume cipher table is in priority order. */ if ((status == NX_SUCCESS) && (new_ciphersuite_priority < ciphersuite_priority)) { #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE if (NX_SUCCESS == _nx_secure_tls_check_ciphersuite(ciphersuite_info, cert, selected_curve, cert_curve_supported)) #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ { /* Save the ciphersuite and its priority but continue processing the entire list to see if there is a better ciphersuite available. *//* ... */ tls_session -> nx_secure_tls_session_ciphersuite = ciphersuite_info; ciphersuite_priority = new_ciphersuite_priority; ...} }if ((status == NX_SUCCESS) && (new_ciphersuite_priority < ciphersuite_priority)) { ... } #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) if (cipher_entry == TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { if (tls_session -> nx_secure_tls_renegotiation_handshake) { /* When secure renegotiating, the server MUST verify that it does not contain the TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV. *//* ... */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if (tls_session -> nx_secure_tls_renegotiation_handshake) { ... } else { /* Secure Renegotiation signalling ciphersuite value was encountered. This indicates that the Client supports secure renegotiation. *//* ... */ tls_session -> nx_secure_tls_secure_renegotiation = NX_TRUE; }else { ... } }if (cipher_entry == TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { ... } /* ... */#endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ /* Check for the fallback notification SCSV. */ if (cipher_entry == TLS_FALLBACK_NOTIFY_SCSV) { /* A fallback is indicated by the Client, check the TLS version. */ _nx_secure_tls_newest_supported_version(tls_session, &newest_version, NX_SECURE_TLS); if (protocol_version != newest_version) { return(NX_SECURE_TLS_INAPPROPRIATE_FALLBACK); }if (protocol_version != newest_version) { ... } }if (cipher_entry == TLS_FALLBACK_NOTIFY_SCSV) { ... } /* Continue searching client ciphersuite list to get highest-priority selection. */ }for (i = 0; i < ciphersuite_list_length; i += 2) { ... } #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) && defined(NX_SECURE_TLS_REQUIRE_RENEGOTIATION_EXT) #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (!tls_session -> nx_secure_tls_1_3) #endif /* NX_SECURE_TLS_TLS_1_3_ENABLED */ { if ((tls_session -> nx_secure_tls_renegotation_enabled) && (!tls_session -> nx_secure_tls_secure_renegotiation)) { /* No "renegotiation_info" extension present, some servers may want to terminate the handshake. */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if ((tls_session -> nx_secure_tls_renegotation_enabled) && (!tls_session -> nx_secure_tls_secure_renegotiation)) { ... } ...}/* ... */ #endif /* !NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION && NX_SECURE_TLS_REQUIRE_RENEGOTIATION_EXT */ /* See if we found an acceptable ciphersuite. */ if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* No supported ciphersuites found. */ return(NX_SECURE_TLS_NO_SUPPORTED_CIPHERS); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } #ifdef NX_SECURE_TLS_SERVER_DISABLED /* If TLS Server is disabled and we have processed a ClientHello, something is wrong... */ tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_ERROR; return(NX_SECURE_TLS_INVALID_STATE);/* ... */ #else return(NX_SUCCESS); #endif }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_check_ciphersuite PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function checks whether the specified ciphersuite is */ /* suitable for the server certificate, the curve in the certificate */ /* and the common shared curve. */ /* */ /* INPUT */ /* */ /* ciphersuite_info The specified cipher suite */ /* cert Local server certificate */ /* selected_curve Curve selected for ECC */ /* cert_curve_supported If cert curve is supported */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_server_handshake TLS Server state machine */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE static UINT _nx_secure_tls_check_ciphersuite(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite_info, NX_SECURE_X509_CERT *cert, UINT selected_curve, UINT cert_curve_supported) { if (ciphersuite_info -> nx_secure_tls_public_cipher == NX_NULL) { return(NX_SUCCESS); }if (ciphersuite_info -> nx_secure_tls_public_cipher == NX_NULL) { ... } if (ciphersuite_info -> nx_secure_tls_public_auth == NX_NULL) { return(NX_SUCCESS); }if (ciphersuite_info -> nx_secure_tls_public_auth == NX_NULL) { ... } switch (ciphersuite_info -> nx_secure_tls_public_cipher -> nx_crypto_algorithm) { case NX_CRYPTO_KEY_EXCHANGE_ECDHE: if (selected_curve == 0 || cert == NX_NULL) { /* No common named curve supported for ECDHE. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (selected_curve == 0 || cert == NX_NULL) { ... } if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { /* ECDSA auth requires EC certificate. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { ... } }if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { ... } else { if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { /* RSA auth requires RSA certificate. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { ... } }else { ... } break; case NX_CRYPTO_KEY_EXCHANGE_ECDHE: case NX_CRYPTO_KEY_EXCHANGE_ECDH: if (selected_curve == 0) { /* No common named curve supported supported. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (selected_curve == 0) { ... } /* Check for ECDH_anon. */ if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm != NX_CRYPTO_DIGITAL_SIGNATURE_ANONYMOUS) { /* ECDH key exchange requires an EC certificate. */ if (cert == NX_NULL || cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert == NX_NULL || cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { ... } if (cert_curve_supported == NX_FALSE) { return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert_curve_supported == NX_FALSE) { ... } /* Check the signatureAlgorithm of the certificate to determine the public auth algorithm. */ if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { if (cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_1 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_224 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512) { return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_1 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_224 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512) { ... } }if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { ... } else { if (cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_MD5 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_1 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_256 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_384 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_512) { return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_MD5 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_1 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_256 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_384 && cert -> nx_secure_x509_signature_algorithm != NX_SECURE_TLS_X509_TYPE_RSA_SHA_512) { ... } }else { ... } }if (ciphersuite_info -> nx_secure_tls_public_auth -> nx_crypto_algorithm != NX_CRYPTO_DIGITAL_SIGNATURE_ANONYMOUS) { ... } break; case NX_CRYPTO_KEY_EXCHANGE_ECDH: case NX_CRYPTO_KEY_EXCHANGE_RSA: if (cert == NX_NULL || cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { /* RSA key exchange requires RSA certificate. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }if (cert == NX_NULL || cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { ... } break; case NX_CRYPTO_KEY_EXCHANGE_RSA: default: break;default }switch (ciphersuite_info -> nx_secure_tls_public_cipher -> nx_crypto_algorithm) { ... } return(NX_SUCCESS); }{ ... } /* ... */#endif... /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */