1
10
13
14
20
21
22
23
24
25
26
27
28
29
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
109
110
111
112
113
114
115
116
119
120
121
122
123
124
125
126
127
128
129
134
135
137
138
139
140
141
142
149
150
151
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
176
177
178
179
180
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
216
217
218
219
220
221
224
225
226
227
228
229
230
231
232
239
240
241
242
243
244
245
246
247
248
249
250
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
285
286
287
288
289
290
291
292
293
294
295
299
300
301
302
303
304
305
306
307
308
309
310
314
315
316
317
318
320
321
322
323
324
326
327
328
329
330
331
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
UINT _nx_secure_tls_send_clienthello_psk_extension(NX_SECURE_TLS_SESSION *tls_session,
UCHAR *packet_buffer, ULONG *packet_offset,
ULONG extension_length_offset, ULONG total_extensions_length,
ULONG *extension_length, ULONG available_size);/* ... */
#endif
#ifndef NX_SECURE_TLS_CLIENT_DISABLED...
...
UINT _nx_secure_tls_send_clienthello(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet)
{
ULONG length;
ULONG extension_offset;
UINT gmt_time;
UINT random_value;
UINT i;
USHORT ciphersuite;
USHORT ciphersuites_length;
UCHAR *ciphersuites_length_ptr;
UCHAR *packet_buffer;
USHORT protocol_version;
USHORT newest_version;
UINT status;
UINT fallback_enabled = NX_FALSE;
const NX_SECURE_TLS_CRYPTO *crypto_table;
ULONG extension_length, total_extensions_length;
/* ... */
packet_buffer = send_packet -> nx_packet_append_ptr;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(!tls_session->nx_secure_tls_1_3)
#endif
{
_nx_secure_tls_handshake_hash_init(tls_session);
...}
length = 0;
_nx_secure_tls_protocol_version_get(tls_session, &protocol_version, NX_SECURE_TLS);
_nx_secure_tls_newest_supported_version(tls_session, &newest_version, NX_SECURE_TLS);
if(protocol_version != newest_version)
{
fallback_enabled = NX_TRUE;
}if (protocol_version != newest_version) { ... }
/* ... */
crypto_table = tls_session -> nx_secure_tls_crypto_table;
ciphersuites_length = (USHORT)((crypto_table -> nx_secure_tls_ciphersuite_lookup_table_size << 1));
#ifdef NX_SECURE_TLS_USE_SCSV_CIPHPERSUITE
/* ... */
if (!tls_session -> nx_secure_tls_renegotiation_handshake && tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_RENEGOTIATING)
{
ciphersuites_length = (USHORT)(ciphersuites_length + 2);
}if (!tls_session -> nx_secure_tls_renegotiation_handshake && tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_RENEGOTIATING) { ... }
/* ... */#endif
if(fallback_enabled)
{
ciphersuites_length = (USHORT)(ciphersuites_length + 2);
}if (fallback_enabled) { ... }
if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)) <
(9u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random) +
tls_session -> nx_secure_tls_session_id_length + ciphersuites_length))
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)) < (9u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random) + tls_session -> nx_secure_tls_session_id_length + ciphersuites_length)) { ... }
packet_buffer[length] = (UCHAR)((protocol_version & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(protocol_version & 0x00FF);
tls_session -> nx_secure_tls_protocol_version = protocol_version;
length += 2;
gmt_time = 0;
if (tls_session -> nx_secure_tls_session_time_function != NX_NULL)
{
gmt_time = tls_session -> nx_secure_tls_session_time_function();
}if (tls_session -> nx_secure_tls_session_time_function != NX_NULL) { ... }
NX_CHANGE_ULONG_ENDIAN(gmt_time);
NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random, (UCHAR *)&gmt_time, sizeof(gmt_time));
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_client_state == NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY)
{
NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random,
sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random));
}if (tls_session -> nx_secure_tls_client_state == NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY) { ... }
else
#endif
{
for (i = 4; i <= 28; i += (UINT)sizeof(random_value))
{
random_value = (UINT)NX_RAND();
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random[i] = (UCHAR)(random_value);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random[i + 1] = (UCHAR)(random_value >> 8);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random[i + 2] = (UCHAR)(random_value >> 16);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random[i + 3] = (UCHAR)(random_value >> 24);
}for (i = 4; i <= 28; i += (UINT)sizeof(random_value)) { ... }
NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random,
sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random));
}else { ... }
length += sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_random);
tls_session -> nx_secure_tls_session_id_length = 0;
packet_buffer[length] = tls_session -> nx_secure_tls_session_id_length;
length++;
if (tls_session -> nx_secure_tls_session_id_length > 0)
{
NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_session_id, tls_session -> nx_secure_tls_session_id_length);
length += tls_session -> nx_secure_tls_session_id_length;
}if (tls_session -> nx_secure_tls_session_id_length > 0) { ... }
ciphersuites_length_ptr = &packet_buffer[length];
length += 2;
/* ... */
ciphersuites_length = 0;
for (i = 0; i < crypto_table -> nx_secure_tls_ciphersuite_lookup_table_size; i++)
{
ciphersuite = crypto_table -> nx_secure_tls_ciphersuite_lookup_table[i].nx_secure_tls_ciphersuite;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if ((tls_session -> nx_secure_tls_1_3 == NX_FALSE) && ((ciphersuite >> 8) == 0x13))
{
continue;
}if ((tls_session -> nx_secure_tls_1_3 == NX_FALSE) && ((ciphersuite >> 8) == 0x13)) { ... }
/* ... */#endif
packet_buffer[length] = (UCHAR)((ciphersuite & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(ciphersuite & 0x00FF);
length += 2;
ciphersuites_length = (USHORT)(ciphersuites_length + 2);
}for (i = 0; i < crypto_table -> nx_secure_tls_ciphersuite_lookup_table_size; i++) { ... }
#ifdef NX_SECURE_TLS_USE_SCSV_CIPHPERSUITE
/* ... */
if (!tls_session -> nx_secure_tls_renegotiation_handshake && tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_RENEGOTIATING)
{
/* ... */
ciphersuite = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
packet_buffer[length] = (UCHAR)((ciphersuite & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(ciphersuite & 0x00FF);
length += 2;
ciphersuites_length = (USHORT)(ciphersuites_length + 2);
}if (!tls_session -> nx_secure_tls_renegotiation_handshake && tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_RENEGOTIATING) { ... }
/* ... */#endif
if(fallback_enabled)
{
ciphersuite = TLS_FALLBACK_NOTIFY_SCSV;
packet_buffer[length] = (UCHAR)((ciphersuite & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(ciphersuite & 0x00FF);
length += 2;
ciphersuites_length = (USHORT)(ciphersuites_length + 2);
}if (fallback_enabled) { ... }
ciphersuites_length_ptr[0] = (UCHAR)((ciphersuites_length & 0xFF00) >> 8);
ciphersuites_length_ptr[1] = (UCHAR)(ciphersuites_length & 0x00FF);
packet_buffer[length] = 0x1;
length++;
packet_buffer[length] = 0x0;
length++;
/* ... */
extension_offset = length;
total_extensions_length = 0;
length += 2;
status = _nx_secure_tls_send_clienthello_extensions(tls_session, packet_buffer, &length, &extension_length,
((ULONG)send_packet -> nx_packet_data_end -
(ULONG)packet_buffer));
if (status)
{
return(status);
}if (status) { ... }
total_extensions_length += extension_length;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3 && tls_session->nx_secure_tls_credentials.nx_secure_tls_psk_count > 0)
{
status = _nx_secure_tls_send_clienthello_psk_extension(tls_session, packet_buffer, &length,
extension_offset, total_extensions_length,
&extension_length,
((ULONG)send_packet -> nx_packet_data_end - (ULONG)packet_buffer));
if(status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
total_extensions_length += extension_length;
}if (tls_session->nx_secure_tls_1_3 && tls_session->nx_secure_tls_credentials.nx_secure_tls_psk_count > 0) { ... }
/* ... */#endif
/* ... */
packet_buffer[extension_offset] = (UCHAR)((total_extensions_length & 0xFF00) >> 8);
packet_buffer[extension_offset + 1] = (UCHAR)(total_extensions_length & 0x00FF);
/* ... */
send_packet -> nx_packet_append_ptr = send_packet -> nx_packet_append_ptr + length;
send_packet -> nx_packet_length = send_packet -> nx_packet_length + length;
return(status);
}{ ... }
#endif... /* ... */