1
10
13
14
20
21
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
103
104
105
110
111
116
117
118
119
120
121
129
130
131
132
133
134
135
139
140
142
143
144
145
146
147
148
149
150
156
160
161
168
169
170
171
172
173
174
178
179
180
185
186
187
188
189
190
191
192
203
204
205
206
207
208
212
213
214
215
216
217
218
219
220
221
222
223
227
228
229
230
231
232
233
234
235
239
240
241
248
249
250
260
261
262
263
264
286
287
288
289
293
294
295
296
297
298
299
300
301
302
306
307
308
309
310
315
316
317
318
319
325
326
327
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
extern const UCHAR _nx_secure_tls_hello_retry_request_random[32];/* ... */
#endif
...
...
UINT _nx_secure_tls_process_serverhello(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
UINT message_length)
{
#ifndef NX_SECURE_TLS_CLIENT_DISABLED
UINT length;
UCHAR compression_method;
USHORT version, total_extensions_length;
UINT status;
USHORT ciphersuite;
USHORT ciphersuite_priority;
NX_SECURE_TLS_HELLO_EXTENSION extension_data[NX_SECURE_TLS_HELLO_EXTENSIONS_MAX];
UINT num_extensions;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
USHORT tls_1_3 = tls_session -> nx_secure_tls_1_3;
NX_SECURE_TLS_SERVER_STATE old_client_state = tls_session -> nx_secure_tls_client_state;
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_IDLE;/* ... */
#endif
/* ... */
if (message_length < 38)
{
return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH);
}if (message_length < 38) { ... }
length = 0;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_1_3 && tls_session -> nx_secure_tls_local_session_active)
{
/* ... */
return(NX_SECURE_TLS_UNEXPECTED_MESSAGE);
}if (tls_session -> nx_secure_tls_1_3 && tls_session -> nx_secure_tls_local_session_active) { ... }
/* ... */#endif
version = (USHORT)((packet_buffer[length] << 8) + packet_buffer[length + 1]);
length += 2;
status = _nx_secure_tls_check_protocol_version(tls_session, version, NX_SECURE_TLS);
if (status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
/* ... */
tls_session -> nx_secure_tls_protocol_version = version;
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_1_3 &&
(NX_SECURE_MEMCMP(_nx_secure_tls_hello_retry_request_random, &packet_buffer[length], NX_SECURE_TLS_RANDOM_SIZE) == 0))
{
if (old_client_state == NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY)
{
return(NX_SECURE_TLS_UNRECOGNIZED_MESSAGE_TYPE);
}if (old_client_state == NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY) { ... }
else
{
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY;
}else { ... }
}if (tls_session -> nx_secure_tls_1_3 && (NX_SECURE_MEMCMP(_nx_secure_tls_hello_retry_request_random, &packet_buffer[length], NX_SECURE_TLS_RANDOM_SIZE) == 0)) { ... }
else
#endif
{
NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[0], &packet_buffer[length], NX_SECURE_TLS_RANDOM_SIZE);
}else { ... }
length += NX_SECURE_TLS_RANDOM_SIZE;
tls_session -> nx_secure_tls_session_id_length = packet_buffer[length];
length++;
if ((length + tls_session -> nx_secure_tls_session_id_length) > message_length)
{
return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH);
}if ((length + tls_session -> nx_secure_tls_session_id_length) > message_length) { ... }
if (tls_session -> nx_secure_tls_session_id_length > 0)
{
NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_session_id, &packet_buffer[length], 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) { ... }
ciphersuite = (USHORT)((packet_buffer[length] << 8) + packet_buffer[length + 1]);
length += 2;
status = _nx_secure_tls_ciphersuite_lookup(tls_session, ciphersuite, &tls_session -> nx_secure_tls_session_ciphersuite, &ciphersuite_priority);
if (status != NX_SUCCESS)
{
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_1_3)
{
return(NX_SECURE_TLS_1_3_UNKNOWN_CIPHERSUITE);
}if (tls_session -> nx_secure_tls_1_3) { ... }
/* ... */#endif
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (status != NX_SUCCESS) { ... }
compression_method = packet_buffer[length];
if (compression_method != 0x00)
{
return(NX_SECURE_TLS_BAD_COMPRESSION_METHOD);
}if (compression_method != 0x00) { ... }
length++;
if (message_length >= (length + 2))
{
total_extensions_length = (USHORT)((packet_buffer[length] << 8) + packet_buffer[length + 1]);
length += 2;
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)
{
status = _nx_secure_tls_process_serverhello_extensions(tls_session, &packet_buffer[length], total_extensions_length, extension_data, &num_extensions);
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)
{
return(status);
}if (tls_session -> nx_secure_tls_1_3 != tls_1_3) { ... }
/* ... */#endif
if (tls_session -> nx_secure_tls_session_client_callback != NX_NULL)
{
status = tls_session -> nx_secure_tls_session_client_callback(tls_session, extension_data, num_extensions);
if (status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
}if (tls_session -> nx_secure_tls_session_client_callback != NX_NULL) { ... }
}if (total_extensions_length > 0) { ... }
}if (message_length >= (length + 2)) { ... }
#ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
#ifdef NX_SECURE_TLS_REQUIRE_RENEGOTIATION_EXT
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (!tls_session -> nx_secure_tls_1_3)
#endif
{
if ((tls_session -> nx_secure_tls_renegotation_enabled) && (!tls_session -> nx_secure_tls_secure_renegotiation))
{
return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR);
}if ((tls_session -> nx_secure_tls_renegotation_enabled) && (!tls_session -> nx_secure_tls_secure_renegotiation)) { ... }
...}/* ... */
#endif
if ((tls_session -> nx_secure_tls_local_session_active) && (!tls_session -> nx_secure_tls_secure_renegotiation_verified))
{
return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR);
}if ((tls_session -> nx_secure_tls_local_session_active) && (!tls_session -> nx_secure_tls_secure_renegotiation_verified)) { ... }
tls_session -> nx_secure_tls_secure_renegotiation_verified = NX_FALSE;/* ... */
#endif
#ifdef NX_SECURE_TLS_CLIENT_DISABLED
tls_session -> nx_secure_tls_server_state = NX_SECURE_TLS_SERVER_STATE_ERROR;
return(NX_SECURE_TLS_INVALID_STATE);/* ... */
#else
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if ((tls_session -> nx_secure_tls_1_3) && (old_client_state == NX_SECURE_TLS_CLIENT_STATE_IDLE))
{
status = _nx_secure_tls_handshake_hash_init(tls_session);
if(status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
}if ((tls_session -> nx_secure_tls_1_3) && (old_client_state == NX_SECURE_TLS_CLIENT_STATE_IDLE)) { ... }
if (tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY)/* ... */
#endif
{
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_SERVERHELLO;
...}
return(NX_SUCCESS);/* ... */
#endif/* ... */
#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
}{ ... }