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
123
124
125
126
127
128
129
130
131
132
133
134
138
139
140
141
142
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
165
171
172
173
174
175
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
205
206
207
208
209
215
216
217
223
224
237
238
239
240
241
242
243
244
245
246
251
252
253
258
259
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
281
282
283
285
286
287
288
289
290
291
292
293
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
UINT _nx_secure_tls_session_receive_records(NX_SECURE_TLS_SESSION *tls_session,
NX_PACKET **packet_ptr_ptr, ULONG wait_option)
{
UINT status;
NX_PACKET *packet_ptr;
NX_TCP_SOCKET *tcp_socket;
ULONG bytes_processed = 0;
ULONG packet_fragment_length;
NX_PACKET *send_packet = NX_NULL;
UINT error_number;
UINT alert_number;
UINT alert_level;
NX_PACKET *current_packet;
NX_PACKET *previous_packet;
UCHAR handshake_finished = NX_FALSE;
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
tcp_socket = tls_session -> nx_secure_tls_tcp_socket;
/* ... */
status = NX_CONTINUE;
if (tls_session -> nx_secure_record_queue_header)
{
/* ... */
status = _nx_secure_tls_process_record(tls_session, NX_NULL, &bytes_processed, wait_option);
}if (tls_session -> nx_secure_record_queue_header) { ... }
while (status == NX_CONTINUE)
{
tx_mutex_put(&_nx_secure_tls_protection);
status = nx_tcp_socket_receive(tcp_socket, &packet_ptr, wait_option);
if (status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
/* ... */
status = _nx_secure_tls_process_record(tls_session, packet_ptr, &bytes_processed, wait_option);
}while (status == NX_CONTINUE) { ... }
if (status == NX_SUCCESS || status == NX_SECURE_TLS_POST_HANDSHAKE_RECEIVED)
{
tls_session -> nx_secure_record_queue_header -> nx_packet_length -= bytes_processed;
current_packet = tls_session -> nx_secure_record_queue_header;
previous_packet = NX_NULL;
while (current_packet)
{
packet_fragment_length = (ULONG)(current_packet -> nx_packet_append_ptr) - (ULONG)(current_packet -> nx_packet_prepend_ptr);
if (packet_fragment_length <= bytes_processed)
{
bytes_processed -= packet_fragment_length;
}if (packet_fragment_length <= bytes_processed) { ... }
else
{
current_packet -> nx_packet_prepend_ptr += bytes_processed;
bytes_processed = 0;
break;
}else { ... }
previous_packet = current_packet;
current_packet = current_packet -> nx_packet_next;
}while (current_packet) { ... }
if (!current_packet)
{
nx_secure_tls_packet_release(tls_session -> nx_secure_record_queue_header);
tls_session -> nx_secure_record_queue_header = NX_NULL;
}if (!current_packet) { ... }
else if (previous_packet)
{
previous_packet -> nx_packet_next = NX_NULL;
current_packet -> nx_packet_length = tls_session -> nx_secure_record_queue_header -> nx_packet_length;
current_packet -> nx_packet_last = tls_session -> nx_secure_record_queue_header -> nx_packet_last;
tls_session -> nx_secure_record_queue_header -> nx_packet_last = previous_packet;
nx_secure_tls_packet_release(tls_session -> nx_secure_record_queue_header);
tls_session -> nx_secure_record_queue_header = current_packet;
}else if (previous_packet) { ... }
if (bytes_processed)
{
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_SECURE_TLS_INVALID_PACKET);
}if (bytes_processed) { ... }
#ifndef NX_SECURE_TLS_CLIENT_DISABLED
if ((tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) &&
(tls_session -> nx_secure_tls_client_state == NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED))
{
handshake_finished = NX_TRUE;
}if ((tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) && (tls_session -> nx_secure_tls_client_state == NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED)) { ... }
/* ... */#endif
#ifndef NX_SECURE_TLS_SERVER_DISABLED
if ((tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) &&
(tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED))
{
handshake_finished = NX_TRUE;
}if ((tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) && (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED)) { ... }
/* ... */#endif
if (handshake_finished)
{
if (tls_session -> nx_secure_record_decrypted_packet == NX_NULL)
{
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_SECURE_TLS_INVALID_PACKET);
}if (tls_session -> nx_secure_record_decrypted_packet == NX_NULL) { ... }
*packet_ptr_ptr = tls_session -> nx_secure_record_decrypted_packet;
tls_session -> nx_secure_record_decrypted_packet = NX_NULL;
}if (handshake_finished) { ... }
}if (status == NX_SUCCESS || status == NX_SECURE_TLS_POST_HANDSHAKE_RECEIVED) { ... }
else
{
error_number = status;
_nx_secure_tls_map_error_to_alert(error_number, &alert_number, &alert_level);
#ifndef NX_SECURE_TLS_SERVER_DISABLED
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
tls_session -> nx_secure_tls_server_state = NX_SECURE_TLS_SERVER_STATE_ERROR;
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... }
/* ... */#endif
#ifndef NX_SECURE_TLS_CLIENT_DISABLED
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT)
{
tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_ERROR;
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) { ... }
/* ... */#endif
/* ... */
if (error_number != NX_SECURE_TLS_ALERT_RECEIVED)
{
tx_mutex_put(&_nx_secure_tls_protection);
status = _nx_secure_tls_packet_allocate(tls_session, tls_session -> nx_secure_tls_packet_pool, &send_packet, wait_option);
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
if (status == NX_SUCCESS)
{
_nx_secure_tls_send_alert(tls_session, send_packet, (UCHAR)alert_number, (UCHAR)alert_level);
status = _nx_secure_tls_send_record(tls_session, send_packet, NX_SECURE_TLS_ALERT, wait_option);
if (status != NX_SUCCESS)
{
nx_secure_tls_packet_release(send_packet);
}if (status != NX_SUCCESS) { ... }
}if (status == NX_SUCCESS) { ... }
/* ... */
_nx_secure_tls_session_reset(tls_session);
}if (error_number != NX_SECURE_TLS_ALERT_RECEIVED) { ... }
status = error_number;
}else { ... }
tx_mutex_put(&_nx_secure_tls_protection);
return(status);
}{ ... }