1
10
13
14
20
21
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
135
136
138
139
140
141
142
143
144
145
146
147
148
153
154
155
162
163
165
166
167
168
169
170
177
178
179
180
181
182
183
184
185
186
187
188
192
193
194
195
196
198
199
200
201
202
209
210
211
212
213
214
215
216
217
218
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
245
250
251
255
256
257
258
259
260
261
262
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
318
319
320
321
326
327
328
329
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
360
361
362
363
364
365
366
367
368
369
370
371
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
UINT _nx_secure_tls_send_record(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet,
UCHAR record_type, ULONG wait_option)
{
UINT status;
UINT message_length;
UCHAR *mac_secret;
UCHAR *record_header;
UCHAR record_hash[NX_SECURE_TLS_MAX_HASH_SIZE];
UINT hash_length;
UCHAR *hash_data;
ULONG hash_data_length;
ULONG length;
USHORT iv_size = 0;
NX_PACKET *current_packet;
length = send_packet -> nx_packet_length;
if ((tls_session -> nx_secure_tls_tcp_socket) &&
(tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_ip_ptr) &&
(tx_thread_identify() == &(tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_ip_ptr -> nx_ip_thread)))
{
wait_option = 0;
}if ((tls_session -> nx_secure_tls_tcp_socket) && (tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_ip_ptr) && (tx_thread_identify() == &(tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_ip_ptr -> nx_ip_thread))) { ... }
tx_mutex_put(&_nx_secure_tls_protection);
status = tx_mutex_get(&(tls_session -> nx_secure_tls_session_transmit_mutex), TX_WAIT_FOREVER);
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
if (status)
{
return(NX_SECURE_TLS_TRANSMIT_LOCKED);
}if (status) { ... }
/* ... */
if (tls_session -> nx_secure_tls_local_session_active
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
&& !tls_session->nx_secure_tls_1_3
#endif
)
{
status = _nx_secure_tls_session_iv_size_get(tls_session, &iv_size);
if (status != NX_SUCCESS)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(status);
}if (status != NX_SUCCESS) { ... }
if ((ULONG)(send_packet -> nx_packet_prepend_ptr - send_packet -> nx_packet_data_start) < iv_size)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(NX_SECURE_TLS_INVALID_PACKET);
}if ((ULONG)(send_packet -> nx_packet_prepend_ptr - send_packet -> nx_packet_data_start) < iv_size) { ... }
/* ... */
send_packet -> nx_packet_prepend_ptr -= iv_size;
send_packet -> nx_packet_length += iv_size;
}if (tls_session -> nx_secure_tls_local_session_active #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !tls_session->nx_secure_tls_1_3 #endif) { ... }
if ((ULONG)(send_packet -> nx_packet_prepend_ptr - send_packet -> nx_packet_data_start) < NX_SECURE_TLS_RECORD_HEADER_SIZE)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(NX_SECURE_TLS_INVALID_PACKET);
}if ((ULONG)(send_packet -> nx_packet_prepend_ptr - send_packet -> nx_packet_data_start) < NX_SECURE_TLS_RECORD_HEADER_SIZE) { ... }
record_header = send_packet -> nx_packet_prepend_ptr - NX_SECURE_TLS_RECORD_HEADER_SIZE;
record_header[0] = record_type;
record_header[1] = (UCHAR)((tls_session -> nx_secure_tls_protocol_version & 0xFF00) >> 8);
record_header[2] = (UCHAR)(tls_session -> nx_secure_tls_protocol_version & 0x00FF);
/* ... */
message_length = length;
record_header[3] = (UCHAR)((length & 0xFF00) >> 8);
record_header[4] = (UCHAR)(length & 0x00FF);
/* ... */
if (tls_session -> nx_secure_tls_local_session_active)
{
if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... }
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
if(tls_session->nx_secure_tls_1_3)
{
status = nx_packet_data_append(send_packet, (UCHAR*)(&record_type), 1,
tls_session -> nx_secure_tls_packet_pool, wait_option);
if(status != NX_SUCCESS)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(status);
}if (status != NX_SUCCESS) { ... }
record_type = NX_SECURE_TLS_APPLICATION_DATA;
record_header[0] = record_type;
}if (tls_session->nx_secure_tls_1_3) { ... }
/* ... */#endif
if (
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
!tls_session->nx_secure_tls_1_3 &&
#endif
tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash -> nx_crypto_operation)
{
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
mac_secret = tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret;
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... }
else
{
mac_secret = tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_mac_secret;
}else { ... }
/* ... */
current_packet = send_packet;
status = _nx_secure_tls_record_hash_initialize(tls_session, tls_session -> nx_secure_tls_local_sequence_number,
record_header, 5, &hash_length, mac_secret);
if (status != NX_SUCCESS)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(status);
}if (status != NX_SUCCESS) { ... }
hash_data = current_packet -> nx_packet_prepend_ptr + iv_size;
hash_data_length = (ULONG)(current_packet -> nx_packet_append_ptr - current_packet -> nx_packet_prepend_ptr) - iv_size;
do
{
status = _nx_secure_tls_record_hash_update(tls_session, hash_data,
(UINT)hash_data_length);
current_packet = current_packet -> nx_packet_next;
if (current_packet != NX_NULL)
{
hash_data = current_packet -> nx_packet_prepend_ptr;
hash_data_length = (ULONG)(current_packet -> nx_packet_append_ptr - current_packet -> nx_packet_prepend_ptr);
}if (current_packet != NX_NULL) { ... }
...} while (current_packet != NX_NULL);
_nx_secure_tls_record_hash_calculate(tls_session, record_hash, &hash_length);
tx_mutex_put(&_nx_secure_tls_protection);
status = nx_packet_data_append(send_packet, record_hash, hash_length,
tls_session -> nx_secure_tls_packet_pool, wait_option);
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(record_hash, 0, sizeof(record_hash));
#endif
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
}if (#if (NX_SECURE_TLS_TLS_1_3_ENABLED) !tls_session->nx_secure_tls_1_3 && #endif tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash -> nx_crypto_operation) { ... }
...
status = _nx_secure_tls_record_payload_encrypt(tls_session, send_packet, tls_session -> nx_secure_tls_local_sequence_number, record_type);
if (status != NX_SUCCESS)
{
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(status);
}if (status != NX_SUCCESS) { ... }
...
if ((tls_session -> nx_secure_tls_local_sequence_number[0] + 1) == 0)
{
tls_session -> nx_secure_tls_local_sequence_number[1]++;
}if ((tls_session -> nx_secure_tls_local_sequence_number[0] + 1) == 0) { ... }
tls_session -> nx_secure_tls_local_sequence_number[0]++;
}if (tls_session -> nx_secure_tls_local_session_active) { ... }
/* ... */
message_length = send_packet -> nx_packet_length;
record_header[3] = (UCHAR)((message_length & 0xFF00) >> 8);
record_header[4] = (UCHAR)(message_length & 0x00FF);
send_packet -> nx_packet_prepend_ptr -= NX_SECURE_TLS_RECORD_HEADER_SIZE;
send_packet -> nx_packet_length += NX_SECURE_TLS_RECORD_HEADER_SIZE;
tx_mutex_put(&_nx_secure_tls_protection);
status = nx_tcp_socket_send(tls_session -> nx_secure_tls_tcp_socket, send_packet, wait_option);
#ifdef NX_SECURE_KEY_CLEAR
if (tls_session -> nx_secure_tls_local_session_active)
{
current_packet = send_packet;
while (current_packet)
{
NX_SECURE_MEMSET(current_packet -> nx_packet_prepend_ptr, 0,
(ULONG)current_packet -> nx_packet_append_ptr -
(ULONG)current_packet -> nx_packet_prepend_ptr);
current_packet = current_packet -> nx_packet_next;
}while (current_packet) { ... }
}if (tls_session -> nx_secure_tls_local_session_active) { ... }
/* ... */#endif
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
tx_mutex_put(&(tls_session -> nx_secure_tls_session_transmit_mutex));
return(status);
}{ ... }