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_client_padded_pre_master
...
...
_nx_secure_tls_process_client_key_exchange(NX_SECURE_TLS_SESSION *, UCHAR *, UINT, UINT)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_process_client_key_exchange.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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" static UCHAR _nx_secure_client_padded_pre_master[600]; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_process_client_key_exchange PORTABLE C */ /* 6.1.7 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes an incoming ClientKeyExchange message, */ /* which contains the encrypted Pre-Master Secret. This function */ /* decrypts the Pre-Master Secret and saves it in the TLS session */ /* control block for use in generating session key material later. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Pointer to message data */ /* message_length Length of message data (bytes)*/ /* id TLS or DTLS */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tls_generate_premaster_secret */ /* Generate the shared secret */ /* used to generate keys later */ /* _nx_secure_x509_local_device_certificate_get */ /* Get the local certificate */ /* for its keys */ /* _nx_secure_tls_find_curve_method Find named curve used */ /* [nx_crypto_init] Initialize crypto */ /* [nx_crypto_operation] Crypto operation */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_server_handshake DTLS server state machine */ /* _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), update */ /* ECC find curve method, */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* 06-02-2021 Timothy Stapko Modified comment(s), */ /* supported hardware EC */ /* private key, */ /* resulting in version 6.1.7 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_process_client_key_exchange(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT message_length, UINT id) { USHORT length; UINT status; UCHAR *encrypted_pre_master_secret; const NX_CRYPTO_METHOD *public_cipher_method; NX_SECURE_X509_CERT *local_certificate; UINT user_defined_key; VOID *handler = NX_NULL; UCHAR rand_byte; UINT i; #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE NX_SECURE_EC_PRIVATE_KEY *ec_privkey; NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecdhe_data; NX_CRYPTO_EXTENDED_OUTPUT extended_output; const NX_CRYPTO_METHOD *curve_method; const NX_CRYPTO_METHOD *ecdh_method; UCHAR *private_key; UINT private_key_length;/* ... */ #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ #ifndef NX_SECURE_ENABLE_PSK_CIPHERSUITES NX_PARAMETER_NOT_USED(id); #endif /* NX_SECURE_ENABLE_PSK_CIPHERSUITES */ if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Process key material. The contents of the handshake record differ according to the ciphersuite chosen in the Client/Server Hello negotiation. *//* ... */ #ifdef NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE /* Check for ECJ-PAKE ciphersuites and generate the pre-master-secret. */ if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE) { tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = 32; public_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth; status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_ECJPAKE_CLIENT_KEY_EXCHANGE_PROCESS, tls_session -> nx_secure_public_auth_handler, (NX_CRYPTO_METHOD*)public_cipher_method, 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 (public_cipher_method -> nx_crypto_cleanup) { status = public_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_auth_metadata_area); if (status) { return(status); }if (status) { ... } }if (public_cipher_method -> nx_crypto_cleanup) { ... } }if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE) { ... } else #endif { #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES /* Check for PSK ciphersuites and generate the pre-master-secret. */ if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK) { status = _nx_secure_tls_generate_premaster_secret(tls_session, id); if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } }if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK) { ... } else/* ... */ #endif #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDH || tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE) { length = packet_buffer[0]; if (length > message_length) { /* The public key length is larger than the header indicated. */ return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (length > message_length) { ... } if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDH) { /* Get the local certificate. */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { local_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 { /* 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, &local_certificate); if (status != NX_SUCCESS) { local_certificate = NX_NULL; }if (status != NX_SUCCESS) { ... } }else { ... } if (local_certificate == NX_NULL) { /* No certificate found, error! */ return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND); }if (local_certificate == NX_NULL) { ... } ec_privkey = &local_certificate -> nx_secure_x509_private_key.ec_private_key; /* Find out which named curve the local certificate is using. */ status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)(ec_privkey -> nx_secure_ec_named_curve), &curve_method, NX_NULL); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } private_key = (UCHAR *)ec_privkey -> nx_secure_ec_private_key; private_key_length = ec_privkey -> nx_secure_ec_private_key_length; }if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDH) { ... } else { ecdhe_data = (NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *)tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data; /* Find out which named curve the we are using. */ status = _nx_secure_tls_find_curve_method(tls_session, (USHORT)ecdhe_data -> nx_secure_tls_ecdhe_named_curve, &curve_method, NX_NULL); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } private_key = ecdhe_data -> nx_secure_tls_ecdhe_private_key; private_key_length = ecdhe_data -> nx_secure_tls_ecdhe_private_key_length; }else { ... } if (curve_method == NX_NULL) { /* No named curve is selected. */ return(NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE); }if (curve_method == NX_NULL) { ... } ecdh_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher; if (ecdh_method -> nx_crypto_operation == NX_NULL) { return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE); }if (ecdh_method -> nx_crypto_operation == NX_NULL) { ... } if (ecdh_method -> nx_crypto_init != NX_NULL) { status = ecdh_method -> nx_crypto_init((NX_CRYPTO_METHOD*)ecdh_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 (ecdh_method -> nx_crypto_init != NX_NULL) { ... } status = ecdh_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler, (NX_CRYPTO_METHOD*)ecdh_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) { ... } /* Import the private key to the ECDH context. */ status = ecdh_method -> nx_crypto_operation(NX_CRYPTO_DH_KEY_PAIR_IMPORT, handler, (NX_CRYPTO_METHOD*)ecdh_method, private_key, private_key_length << 3, NX_NULL, 0, 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_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 = ecdh_method -> nx_crypto_operation(NX_CRYPTO_DH_CALCULATE, handler, (NX_CRYPTO_METHOD*)ecdh_method, NX_NULL, 0, &packet_buffer[1], 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 (ecdh_method -> nx_crypto_cleanup) { status = ecdh_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 (ecdh_method -> nx_crypto_cleanup) { ... } }else if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDH || tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE) { ... } else #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ { /* Certificate-based authentication. */ /* Get pre-master-secret length. */ length = (USHORT)((packet_buffer[0] << 8) + (USHORT)packet_buffer[1]); packet_buffer += 2; if (length > message_length) { /* The payload is larger than the header indicated. */ return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (length > message_length) { ... } /* Pointer to the encrypted pre-master secret in our packet buffer. */ encrypted_pre_master_secret = &packet_buffer[0]; if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_ciphersuite == TLS_NULL_WITH_NULL_NULL) { /* Special case - NULL ciphersuite. No keys are generated. */ if (length > sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret)) { length = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret); }if (length > sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret)) { ... } NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret, encrypted_pre_master_secret, length); /* Use case of memcpy is verified. */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = length; }if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_ciphersuite == TLS_NULL_WITH_NULL_NULL) { ... } /* 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, &local_certificate); if (status) { /* No certificate found, error! */ return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND); }if (status) { ... } /* Get the public cipher method pointer for this session. */ public_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher; /* Check for user-defined key types. */ user_defined_key = NX_FALSE; if (((local_certificate -> nx_secure_x509_private_key_type & NX_SECURE_X509_KEY_TYPE_USER_DEFINED_MASK) != 0x0) || ( local_certificate -> nx_secure_x509_private_key_type == NX_SECURE_X509_KEY_TYPE_HARDWARE)) { user_defined_key = NX_TRUE; }if (((local_certificate -> nx_secure_x509_private_key_type & NX_SECURE_X509_KEY_TYPE_USER_DEFINED_MASK) != 0x0) || ( local_certificate -> nx_secure_x509_private_key_type == NX_SECURE_X509_KEY_TYPE_HARDWARE)) { ... } /* See if we are using RSA. Separate from other methods (e.g. ECC, DH) for proper handling of padding. */ if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA && local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { /* Check for user-defined keys. */ if (user_defined_key) { /* A user-defined key is passed directly into the crypto routine. */ 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), encrypted_pre_master_secret, length, NX_NULL, _nx_secure_client_padded_pre_master, sizeof(_nx_secure_client_padded_pre_master), 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 (user_defined_key) { ... } else { /* Generic RSA operation, use pre-parsed RSA key data. */ if (public_cipher_method -> nx_crypto_init != NX_NULL) { /* Initialize the crypto method with public key. */ 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, 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 (public_cipher_method -> nx_crypto_init != NX_NULL) { ... } if (public_cipher_method -> nx_crypto_operation != NX_NULL) { /* Check for P and Q in the private key. If they are present, we can use them to speed up RSA using the Chinese Remainder Theorem version of the algorithm. *//* ... */ if (local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_p != NX_NULL && local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_q != NX_NULL) { status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_SET_PRIME_P, handler, (NX_CRYPTO_METHOD*)public_cipher_method, NX_NULL, 0, (VOID *)local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_p, local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_p_length, 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) { ... } status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_SET_PRIME_Q, handler, (NX_CRYPTO_METHOD*)public_cipher_method, NX_NULL, 0, (VOID *)local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_q, local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_q_length, 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) { ... } }if (local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_p != NX_NULL && local_certificate -> nx_secure_x509_private_key.rsa_private_key.nx_secure_rsa_private_prime_q != NX_NULL) { ... } /* Decrypt the pre-master-secret using the private key provided by the user and place the result in the session key material space in our socket. *//* ... */ 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), encrypted_pre_master_secret, length, NX_NULL, _nx_secure_client_padded_pre_master, sizeof(_nx_secure_client_padded_pre_master), 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 (public_cipher_method -> nx_crypto_operation != NX_NULL) { ... } if (public_cipher_method -> nx_crypto_cleanup) { status = public_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_cipher_metadata_area); if(status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_client_padded_pre_master, 0, sizeof(_nx_secure_client_padded_pre_master)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (public_cipher_method -> nx_crypto_cleanup) { ... } }else { ... } /* Check padding - first 2 bytes should be 0x00, 0x02 for PKCS#1 padding. A 0x00 byte should immediately precede the data. *//* ... */ if (_nx_secure_client_padded_pre_master[0] != 0x00 || _nx_secure_client_padded_pre_master[1] != 0x02 || _nx_secure_client_padded_pre_master[length - NX_SECURE_TLS_RSA_PREMASTER_SIZE - 1] != 0x00) { /* Invalid padding. To avoid Bleichenbacher's attack, use random numbers to generate premaster secret and continue the operation (which will be properly taken care of later in _nx_secure_tls_process_finished()). This is described in RFC 5246, section 7.4.7.1, page 58-59. *//* ... */ /* Generate premaster secret using random numbers. */ for (i = 0; i < NX_SECURE_TLS_RSA_PREMASTER_SIZE; ++i) { /* PKCS#1 padding must be random, but CANNOT be 0. */ do { rand_byte = (UCHAR)NX_RAND(); ...} while (rand_byte == 0); tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret[i] = rand_byte; }for (i = 0; i < NX_SECURE_TLS_RSA_PREMASTER_SIZE; ++i) { ... } }if (_nx_secure_client_padded_pre_master[0] != 0x00 || _nx_secure_client_padded_pre_master[1] != 0x02 || _nx_secure_client_padded_pre_master[length - NX_SECURE_TLS_RSA_PREMASTER_SIZE - 1] != 0x00) { ... } else { /* Extract the 48 bytes of the actual pre-master secret from the data we just decrypted, stripping the padding, which comes at the beginning of the decrypted block (the pre-master secret is the last 48 bytes. *//* ... */ NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret, &_nx_secure_client_padded_pre_master[length - NX_SECURE_TLS_RSA_PREMASTER_SIZE], NX_SECURE_TLS_RSA_PREMASTER_SIZE); /* Use case of memcpy is verified. */ }else { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = NX_SECURE_TLS_RSA_PREMASTER_SIZE; }if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA && local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... } /* End RSA-specific section. */ else { /* Unknown or invalid public cipher. */ return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER); }else { ... } }else { ... } }else { ... } #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_client_padded_pre_master, 0, sizeof(_nx_secure_client_padded_pre_master)); #endif /* NX_SECURE_KEY_CLEAR */ #ifdef NX_SECURE_TLS_SERVER_DISABLED /* If TLS Server is disabled and we have processed a ClientKeyExchange, 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 }{ ... }