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
114
115
116
117
118
119
120
121
122
123
124
129
134
135
136
137
138
139
140
141
142
143
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
178
179
180
181
187
188
189
195
196
202
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
225
226
230
231
236
237
238
239
240
241
242
243
249
250
251
252
253
254
255
256
257
258
259
265
266
267
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_dtls.h"
...
...
UINT _nx_secure_dtls_session_start(NX_SECURE_DTLS_SESSION *dtls_session, NX_UDP_SOCKET *udp_socket,
UINT is_client, UINT wait_option)
{
#ifdef NX_SECURE_ENABLE_DTLS
UINT status = NX_NOT_SUCCESSFUL;
NX_PACKET *incoming_packet;
NX_SECURE_TLS_SESSION *tls_session;
UINT minimum_wait_option;
#ifndef NX_SECURE_TLS_CLIENT_DISABLED
NX_PACKET *send_packet;
#endif
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
tls_session = &dtls_session -> nx_secure_dtls_tls_session;
tls_session -> nx_secure_tls_packet_pool = udp_socket -> nx_udp_socket_ip_ptr -> nx_ip_default_packet_pool;
tls_session -> nx_secure_tls_received_remote_credentials = NX_FALSE;
#ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
tls_session -> nx_secure_tls_renegotation_enabled = NX_FALSE;/* ... */
#endif
dtls_session -> nx_secure_dtls_udp_socket = udp_socket;
tls_session -> nx_secure_record_queue_header = NX_NULL;
tls_session -> nx_secure_record_decrypted_packet = NX_NULL;
if (is_client)
{
tls_session -> nx_secure_tls_socket_type = NX_SECURE_TLS_SESSION_TYPE_CLIENT;
}if (is_client) { ... }
else
{
tls_session -> nx_secure_tls_socket_type = NX_SECURE_TLS_SESSION_TYPE_SERVER;
}else { ... }
#ifndef NX_SECURE_TLS_CLIENT_DISABLED
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT)
{
status = _nx_secure_dtls_allocate_handshake_packet(dtls_session, tls_session -> nx_secure_tls_packet_pool, &send_packet, wait_option);
if (status != NX_SUCCESS)
{
tx_mutex_put(&_nx_secure_tls_protection);
return(status);
}if (status != NX_SUCCESS) { ... }
status = _nx_secure_dtls_send_clienthello(dtls_session, send_packet);
if (status == NX_SUCCESS)
{
status = _nx_secure_dtls_send_handshake_record(dtls_session, send_packet, NX_SECURE_TLS_CLIENT_HELLO, wait_option, 0);
tls_session->nx_secure_tls_protocol_version = 0;
}if (status == NX_SUCCESS) { ... }
if (status != NX_SUCCESS)
{
tx_mutex_put(&_nx_secure_tls_protection);
nx_secure_tls_packet_release(send_packet);
return(status);
}if (status != NX_SUCCESS) { ... }
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) { ... }
/* ... */#endif
/* ... */
for (;;)
{
#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))
{
break;
}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))
{
break;
}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 ((dtls_session -> nx_secure_dtls_handshake_timeout > wait_option) ||
(dtls_session -> nx_secure_dtls_handshake_timeout == 0) ||
(dtls_session -> nx_secure_dtls_transmit_sent_head == NX_NULL))
{
minimum_wait_option = wait_option;
}if ((dtls_session -> nx_secure_dtls_handshake_timeout > wait_option) || (dtls_session -> nx_secure_dtls_handshake_timeout == 0) || (dtls_session -> nx_secure_dtls_transmit_sent_head == NX_NULL)) { ... }
else
{
minimum_wait_option = dtls_session -> nx_secure_dtls_handshake_timeout;
}else { ... }
tx_mutex_put(&_nx_secure_tls_protection);
status = _nx_secure_dtls_session_receive(dtls_session, &incoming_packet,
minimum_wait_option);
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
if (status != NX_SUCCESS)
{
if (status != NX_NO_PACKET && status != NX_CONTINUE)
{
break;
}if (status != NX_NO_PACKET && status != NX_CONTINUE) { ... }
if (minimum_wait_option == wait_option)
{
break;
}if (minimum_wait_option == wait_option) { ... }
if (dtls_session -> nx_secure_dtls_timeout_retries >=
NX_SECURE_DTLS_MAXIMUM_RETRANSMIT_RETRIES)
{
break;
}if (dtls_session -> nx_secure_dtls_timeout_retries >= NX_SECURE_DTLS_MAXIMUM_RETRANSMIT_RETRIES) { ... }
_nx_secure_dtls_retransmit(dtls_session);
wait_option -= minimum_wait_option;
}if (status != NX_SUCCESS) { ... }
else
{
nx_secure_tls_packet_release(incoming_packet);
}else { ... }
}for (;;) { ... }
_nx_secure_dtls_retransmit_queue_flush(dtls_session);
tx_mutex_put(&_nx_secure_tls_protection);
return(status);/* ... */
#else
NX_PARAMETER_NOT_USED(dtls_session);
NX_PARAMETER_NOT_USED(udp_socket);
NX_PARAMETER_NOT_USED(is_client);
NX_PARAMETER_NOT_USED(wait_option);
return(NX_NOT_SUPPORTED);/* ... */
#endif
}{ ... }