1
10
13
14
20
21
22
23
24
25
26
27
28
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
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
141
142
143
144
145
146
152
153
154
155
156
157
158
159
160
161
162
163
164
167
173
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
206
207
208
209
210
217
218
219
220
221
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
246
247
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
const UCHAR _nx_secure_tls_hello_retry_request_random[32] =
{
0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
...};
const UCHAR _nx_secure_tls_1_2_random[8] =
{
0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01
...};
const UCHAR _nx_secure_tls_1_1_random[8] =
{
0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x00
...};/* ... */
#endif
...
...
UINT _nx_secure_tls_send_serverhello(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet)
{
#ifndef NX_SECURE_TLS_SERVER_DISABLED
ULONG length;
UINT gmt_time;
UINT random_value;
UINT i;
USHORT ciphersuite;
UCHAR *packet_buffer;
USHORT protocol_version;
UINT status;
/* ... */
if ((6u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random)) >
((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)))
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if ((6u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random)) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { ... }
packet_buffer = send_packet -> nx_packet_append_ptr;
length = 0;
protocol_version = tls_session -> nx_secure_tls_protocol_version;
packet_buffer[length] = (UCHAR)((protocol_version & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(protocol_version & 0x00FF);
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_server_random, (UCHAR *)&gmt_time, sizeof(gmt_time));
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY)
{
NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random,
_nx_secure_tls_hello_retry_request_random,
sizeof(_nx_secure_tls_hello_retry_request_random));
}if (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY) { ... }
else if (!(tls_session -> nx_secure_tls_1_3) && !(tls_session -> nx_secure_tls_protocol_version_override))
{
for (i = 4; i <= 20; i += (UINT)sizeof(random_value))
{
random_value = (UINT)NX_RAND();
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i] = (UCHAR)(random_value);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 1] = (UCHAR)(random_value >> 8);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 2] = (UCHAR)(random_value >> 16);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 3] = (UCHAR)(random_value >> 24);
}for (i = 4; i <= 20; i += (UINT)sizeof(random_value)) { ... }
/* ... */
if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2)
{
NX_SECURE_MEMCPY(&(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[24]),
_nx_secure_tls_1_2_random,
sizeof(_nx_secure_tls_1_2_random));
}if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) { ... }
else
{
NX_SECURE_MEMCPY(&(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[24]),
_nx_secure_tls_1_1_random,
sizeof(_nx_secure_tls_1_1_random));
}else { ... }
}else if (!(tls_session -> nx_secure_tls_1_3) && !(tls_session -> nx_secure_tls_protocol_version_override)) { ... }
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_server_random[i] = (UCHAR)(random_value);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 1] = (UCHAR)(random_value >> 8);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 2] = (UCHAR)(random_value >> 16);
tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 3] = (UCHAR)(random_value >> 24);
}for (i = 4; i <= 28; i += (UINT)sizeof(random_value)) { ... }
}else { ... }
NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random,
sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random));
length += sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random);
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3)
{
/* ... */
packet_buffer[length] = tls_session->nx_secure_tls_session_id_length;
length++;
if ((length + tls_session->nx_secure_tls_session_id_length + 3) >
((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)))
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if ((length + tls_session->nx_secure_tls_session_id_length + 3) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { ... }
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_1_3) { ... }
else
#endif
{
packet_buffer[length] = 0;
length++;
}else { ... }
ciphersuite = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_ciphersuite;
packet_buffer[length] = (UCHAR)(ciphersuite >> 8);
packet_buffer[length + 1] = (UCHAR)ciphersuite;
length += 2;
packet_buffer[length] = 0x0;
length++;
status = _nx_secure_tls_send_serverhello_extensions(tls_session, packet_buffer, &length,
((ULONG)(send_packet -> nx_packet_data_end) -
(ULONG)packet_buffer));
if (status)
{
return(status);
}if (status) { ... }
/* ... */
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); /* ... */
#else
NX_PARAMETER_NOT_USED(send_packet);
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_ERROR;
return(NX_SECURE_TLS_INVALID_STATE);/* ... */
#endif
}{ ... }