summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_dtls_receive_callback.c
blob: 58b19498d99c45f1c53c28e595f8563306ed8091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
177
178
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    Datagram Transport Layer Security (DTLS)                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_dtls.h"
#include "nx_udp.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_dtls_receive_callback                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function serves as the notification callback provided to NetX  */
/*    that is invoked when a UDP packet has been received. It checks the  */
/*    DTLS session cache for a matching client (based on IP address and   */
/*    port) and then invokes the application callback with the            */
/*    appropriate session object. If a previous session was not found, a  */
/*    new session is allocated (if available) and the DTLS handshake is   */
/*    initiated. Once the handshake is complete the user callback is      */
/*    invoked as above.                                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            UDP socket                    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    N/A                                                                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nxd_udp_source_extract               Extract IP address and port   */
/*    _nx_udp_socket_port_get               Get local port                */
/*    nx_secure_dtls_session_cache_find     Check DTLS session cache      */
/*    nx_secure_dtls_session_cache_get_new  Get new DTLS session          */
/*    nx_secure_tls_packet_release          Release packet                */
/*    nx_packet_allocate                    Allocate packet               */
/*    _nxd_udp_socket_send                  Send UDP packet               */
/*    nx_udp_socket_receive                 Receive packet                */
/*    tx_thread_wait_abort                  Abort wait process            */
/*    tx_mutex_get                          Get protection mutex          */
/*    tx_mutex_put                          Put protection mutex          */
/*    [nx_secure_dtls_error_notify]         Notify application of error   */
/*    [nx_secure_dtls_receive_notify]       Notify application of packet  */
/*                                            receive                     */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
VOID _nx_secure_dtls_receive_callback(NX_UDP_SOCKET *socket_ptr)
{
#ifdef NX_SECURE_ENABLE_DTLS
TX_INTERRUPT_SAVE_AREA

NXD_ADDRESS ip_address;
UINT remote_port;
UINT local_port;
UINT status;
ULONG packet_type;
NX_PACKET *packet_ptr;
UCHAR *send_data;
NX_PACKET *temp_packet_ptr;
NX_SECURE_DTLS_SERVER *dtls_server;
NX_SECURE_DTLS_SESSION *dtls_session;

    if(socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
    {
      return;
    }

    /* Extract IP address and port from received packet. */
    socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr += sizeof(NX_UDP_HEADER);
    status =  _nxd_udp_source_extract(socket_ptr->nx_udp_socket_receive_head, &ip_address, &remote_port);
    socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr -= sizeof(NX_UDP_HEADER);
    
    if(status != NX_SUCCESS)
    {
        return;
    }

    /* Extract local port from socket. */
    status = _nx_udp_socket_port_get(socket_ptr, &local_port);

    if(status != NX_SUCCESS)
    {
        return;
    }

    /* Get the DTLS server from our UDP socket so we can access the session cache. */
    dtls_server = (NX_SECURE_DTLS_SERVER*)(socket_ptr -> nx_udp_socket_reserved_ptr);

    /* Check session cache for existing DTLS session. */
    status = nx_secure_dtls_session_cache_find(dtls_server, &dtls_session, &ip_address, remote_port, local_port);

    /* Do we have an established session? */
    if(status == NX_SECURE_DTLS_SESSION_NOT_FOUND)
    {

        /* If received packet is ALERT, just drop it.  */
        if ((socket_ptr -> nx_udp_socket_receive_head -> nx_packet_length < NX_SECURE_DTLS_RECORD_HEADER_SIZE) ||
            (socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr[8] == NX_SECURE_TLS_ALERT))
        {

            /* Lockout interrupts.  */
            TX_DISABLE

            /* Remove the header packet from the queue.  */
            packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
            socket_ptr -> nx_udp_socket_receive_head = packet_ptr -> nx_packet_queue_next;
            packet_ptr -> nx_packet_queue_next = NX_NULL;
            
            /* If this was the last packet, set the tail pointer to NULL.  */
            if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
            {
                socket_ptr -> nx_udp_socket_receive_tail =  NX_NULL;
            }

            /* Decrease the queued packet count.  */
            socket_ptr -> nx_udp_socket_receive_count--;

            /* Restore interrupts.  */
            TX_RESTORE

            /* Release the packet.  */
            nx_secure_tls_packet_release(packet_ptr);

            return;
        }

        /* Get a new session. */
        status = nx_secure_dtls_session_cache_get_new(dtls_server, &dtls_session, &ip_address, remote_port, local_port);

        /* Make sure we got a session. */
        if(status == NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS)
        {
            /* No session? Drop the connection with an internal error alert.
               See RFC 5246 Sec. 7.2.2 - internal error is used for memory allocation failures.

               We don't have a DTLS session, so build a simple DTLS alert record to send. */

            /* Lockout interrupts.  */
            TX_DISABLE

            /* Remove the header packet from the queue.  */
            packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
            socket_ptr -> nx_udp_socket_receive_head = packet_ptr -> nx_packet_queue_next;
            packet_ptr -> nx_packet_queue_next = NX_NULL;
            
            /* If this was the last packet, set the tail pointer to NULL.  */
            if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
            {
                socket_ptr -> nx_udp_socket_receive_tail =  NX_NULL;
            }

            /* Decrease the queued packet count.  */
            socket_ptr -> nx_udp_socket_receive_count--;

            /* Restore interrupts.  */
            TX_RESTORE

            /* Release the packet.  */
            nx_secure_tls_packet_release(packet_ptr);

            /* Get a packet to send the alert to the client. */
            if (ip_address.nxd_ip_version == NX_IP_VERSION_V4)
            {
                packet_type = NX_IPv4_UDP_PACKET;
            }
            else
            {
                packet_type = NX_IPv6_UDP_PACKET;
            }

            status =  nx_packet_allocate(socket_ptr->nx_udp_socket_ip_ptr->nx_ip_default_packet_pool,
                                         &packet_ptr, packet_type, NX_NO_WAIT);
            if(status != NX_SUCCESS)
            {
                return;
            }

            if (((ULONG)(packet_ptr -> nx_packet_data_end) - (ULONG)(packet_ptr -> nx_packet_append_ptr)) < 15)
            {

                /* Packet buffer too small. */
                nx_secure_tls_packet_release(packet_ptr);
                return;
            }

            send_data = packet_ptr -> nx_packet_append_ptr;

            /* Build the DTLS record header. */
            send_data[0] = NX_SECURE_TLS_ALERT;

            /* Set the version number - use DTLS 1.2. */
            send_data[1] = (UCHAR)(NX_SECURE_DTLS_VERSION_MAJOR);
            send_data[2] = (UCHAR)(NX_SECURE_DTLS_VERSION_MINOR_1_2);

            /* DTLS Epoch counter. */
            send_data[3] = 0;
            send_data[4] = 0;

            /* DTLS sequence number. */
            send_data[5]  = 0;
            send_data[6]  = 0;
            send_data[7]  = 0;
            send_data[8]  = 0;
            send_data[9]  = 0;
            send_data[10] = 0;

            /* DTLS message length - 2 bytes for the alert. */
            send_data[11] = 0;
            send_data[12] = 2;

            /* Populate the record with the alert level and alert number to send to the remote host. */
            send_data[13] = (UCHAR)(NX_SECURE_TLS_ALERT_LEVEL_FATAL);
            send_data[14] = (UCHAR)(NX_SECURE_TLS_ALERT_INTERNAL_ERROR);

            /* Make sure the caller has the right length of data to send. */
            packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_append_ptr + 15;
            packet_ptr -> nx_packet_length = 15;

            /* Send the UDP packet containing our record. */
            status = _nxd_udp_socket_send(socket_ptr, packet_ptr, &ip_address, remote_port);

            /* Notify the application that we had to drop an incoming connection. */
            if(dtls_server->nx_secure_dtls_error_notify != NX_NULL)
            {
                dtls_server->nx_secure_dtls_error_notify(NX_NULL, NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS);
            }

            return;
        }

        /* Receive our packet. */
        status =  nx_udp_socket_receive(socket_ptr, &packet_ptr, NX_NO_WAIT);
        if (status)
        {
            return;
        }

        /* New session, retrieve the UDP packet and place in our session receive queue. */
        dtls_session -> nx_secure_dtls_receive_queue_head = packet_ptr;

        /* Make sure the queue pointer is cleared. */
        packet_ptr -> nx_packet_queue_next = NX_NULL;

        /* Notify the application of a new connection - it is up to the application to call
           nx_secure_dtls_session_start to kick off the handshake! */
        dtls_server -> nx_secure_dtls_connect_notify(dtls_session, &ip_address, remote_port);
    }
    else
    {

        /* Receive our packet. */
        status =  nx_udp_socket_receive(socket_ptr, &packet_ptr, NX_NO_WAIT);
        if (status)
        {
            return;
        }

        /* Get the protection before modifying the queue pointer. */
        tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);

        if (!dtls_session -> nx_secure_dtls_session_in_use)
        {

            /* Session is not in use.  */
            tx_mutex_put(&_nx_secure_tls_protection);
            return;
        }

        /* Established session, make sure that we append to the end of the receive queue. */
        if (dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL)
        {
            dtls_session -> nx_secure_dtls_receive_queue_head = packet_ptr;
        }
        else
        {
            temp_packet_ptr = dtls_session -> nx_secure_dtls_receive_queue_head;
            while(temp_packet_ptr -> nx_packet_queue_next != NX_NULL)
            {
                temp_packet_ptr = temp_packet_ptr -> nx_packet_queue_next;
            }

            temp_packet_ptr -> nx_packet_queue_next = packet_ptr;
        }

        /* Make sure the queue pointer is cleared. */
        packet_ptr -> nx_packet_queue_next = NX_NULL;

        /* Is there any thread waiting for packet? */
        if (dtls_session -> nx_secure_dtls_thread_suspended)
        {
            /* Yes. Just abort it. */
            tx_thread_wait_abort(dtls_session -> nx_secure_dtls_thread_suspended);
            dtls_session -> nx_secure_dtls_thread_suspended = NX_NULL;
        }

        /* If the handshake isn't finished, don't notify application. */
        if (dtls_session -> nx_secure_dtls_tls_session.nx_secure_tls_server_state < NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED)
        {

            /* Release the protection. */
            tx_mutex_put(&_nx_secure_tls_protection);
            return;
        }

        /* Release the protection. */
        tx_mutex_put(&_nx_secure_tls_protection);

        /* Invoke the session callback to notify application of packet receive. */
        dtls_server -> nx_secure_dtls_receive_notify(dtls_session);
    }
#else
    NX_PARAMETER_NOT_USED(socket_ptr);

    return;
#endif /* NX_SECURE_ENABLE_DTLS */
}