1
10
13
14
20
21
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
109
110
111
112
113
114
115
121
122
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
149
150
159
160
161
162
163
164
165
176
177
178
179
180
181
182
183
188
189
190
191
192
196
206
207
212
213
214
215
216
217
218
222
223
224
225
226
227
228
229
230
231
232
233
237
238
239
240
241
242
247
248
249
253
254
255
256
257
258
259
260
261
262
266
267
268
269
270
271
272
273
274
275
279
280
281
282
283
284
285
286
287
288
289
290
294
295
296
297
298
299
300
301
302
303
304
305
306
307
311
312
313
314
322
323
324
325
326
327
328
329
330
331
336
337
338
339
340
341
351
352
353
354
355
356
361
362
363
364
365
366
367
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
417
418
419
420
421
422
423
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
467
468
469
470
472
473
474
475
476
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
510
511
512
513
514
515
520
521
522
523
524
525
526
527
530
531
532
533
541
542
543
548
549
550
551
552
553
554
555
558
559
560
561
562
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
static UCHAR _nx_secure_client_padded_pre_master[600];
...
...
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
#ifndef NX_SECURE_ENABLE_PSK_CIPHERSUITES
NX_PARAMETER_NOT_USED(id);
#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) { ... }
/* ... */
#ifdef NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE
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
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)
{
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)
{
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
{
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)
{
return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
}if (local_certificate == NX_NULL) { ... }
ec_privkey = &local_certificate -> nx_secure_x509_private_key.ec_private_key;
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;
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)
{
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) { ... }
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
{
length = (USHORT)((packet_buffer[0] << 8) + (USHORT)packet_buffer[1]);
packet_buffer += 2;
if (length > message_length)
{
return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH);
}if (length > message_length) { ... }
encrypted_pre_master_secret = &packet_buffer[0];
if (tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_ciphersuite == TLS_NULL_WITH_NULL_NULL)
{
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);
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) { ... }
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)
{
return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
}if (status) { ... }
public_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_public_cipher;
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)) { ... }
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)
{
if (user_defined_key)
{
status = public_cipher_method -> nx_crypto_operation(local_certificate -> nx_secure_x509_private_key_type,
NX_NULL,
(NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)local_certificate -> nx_secure_x509_private_key.user_key.key_data,
(NX_CRYPTO_KEY_SIZE)(local_certificate -> nx_secure_x509_private_key.user_key.key_length),
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
{
if (public_cipher_method -> nx_crypto_init != NX_NULL)
{
status = public_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)public_cipher_method,
(UCHAR *)local_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus,
(NX_CRYPTO_KEY_SIZE)(local_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length << 3),
&handler,
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)
{
/* ... */
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) { ... }
/* ... */
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
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_cleanup) { ... }
}else { ... }
/* ... */
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)
{
/* ... */
for (i = 0; i < NX_SECURE_TLS_RSA_PREMASTER_SIZE; ++i)
{
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
{
/* ... */
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);
}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) { ... }
else
{
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
#ifdef NX_SECURE_TLS_SERVER_DISABLED
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_ERROR;
return(NX_SECURE_TLS_INVALID_STATE);/* ... */
#else
return(NX_SUCCESS);
#endif
}{ ... }