1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Secure Component                                                 */
17 /**                                                                       */
18 /**    Datagram Transport Layer Security (DTLS)                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 #include "nx_secure_dtls.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_dtls_session_reset                       PORTABLE C      */
32 /*                                                           6.1.10       */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function resets a DTLS session object, clearing out all data   */
40 /*    for initialization or re-use.                                       */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    dtls_session                          DTLS session control block    */
45 /*                                                                        */
46 /*  OUTPUT                                                                */
47 /*                                                                        */
48 /*    status                                Completion status             */
49 /*                                                                        */
50 /*  CALLS                                                                 */
51 /*                                                                        */
52 /*    tx_thread_wait_abort                  Abort wait process            */
53 /*    tx_mutex_get                          Get protection mutex          */
54 /*    tx_mutex_put                          Put protection mutex          */
55 /*    _nx_secure_tls_session_reset          Clear out the session         */
56 /*    nx_secure_tls_packet_release          Release packet                */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Application                                                         */
61 /*    _nx_secure_dtls_server_stop           Stop DTLS server              */
62 /*    _nx_secure_dtls_session_delete        Delete the DTLS session       */
63 /*    _nx_secure_dtls_session_end           End of a session              */
64 /*    nx_secure_dtls_session_cache_delete   Delete a session              */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
71 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
72 /*                                            released packet securely,   */
73 /*                                            resulting in version 6.1    */
74 /*  01-31-2022     Timothy Stapko           Modified comment(s),          */
75 /*                                            updated cookie handling,    */
76 /*                                            resulting in version 6.1.10 */
77 /*                                                                        */
78 /**************************************************************************/
_nx_secure_dtls_session_reset(NX_SECURE_DTLS_SESSION * dtls_session)79 UINT _nx_secure_dtls_session_reset(NX_SECURE_DTLS_SESSION *dtls_session)
80 {
81 #ifdef NX_SECURE_ENABLE_DTLS
82 NX_PACKET *packet_ptr = NX_NULL;
83 NX_PACKET *next_packet_ptr;
84 
85     /* Get the protection. */
86     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
87 
88     /* UDP doesn't have a persistent state like TCP, so save off IP address index and Port. */
89     dtls_session -> nx_secure_dtls_local_ip_address_index = 0xffffffff;
90     dtls_session -> nx_secure_dtls_local_port = 0;
91 
92     /* Reset remote port and address. */
93     dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version = 0;
94     dtls_session -> nx_secure_dtls_remote_port = 0;
95 
96     /* Reset session state. */
97     dtls_session -> nx_secure_dtls_session_in_use = NX_FALSE;
98 
99     /* Reset the cookie. */
100     dtls_session -> nx_secure_dtls_cookie_length = 0;
101     NX_SECURE_MEMSET(dtls_session -> nx_secure_dtls_cookie, 0, sizeof(dtls_session -> nx_secure_dtls_cookie));
102     dtls_session -> nx_secure_dtls_client_cookie_ptr = NX_NULL;
103 
104     /* Reset the fragment length. */
105     dtls_session -> nx_secure_dtls_fragment_length = 0;
106 
107     /* Reset the handshake sequence numbers. */
108     dtls_session -> nx_secure_dtls_local_handshake_sequence = 0;
109     dtls_session -> nx_secure_dtls_remote_handshake_sequence = 0;
110     dtls_session -> nx_secure_dtls_expected_handshake_sequence = 0;
111 
112     /* Reset the DTLS epoch. */
113     dtls_session -> nx_secure_dtls_local_epoch = 0;
114     dtls_session -> nx_secure_dtls_remote_epoch = 0;
115 
116     /* Is there any thread waiting for packet? */
117     if (dtls_session -> nx_secure_dtls_thread_suspended)
118     {
119 
120         /* Yes. Just abort it. */
121         tx_thread_wait_abort(dtls_session -> nx_secure_dtls_thread_suspended);
122         dtls_session -> nx_secure_dtls_thread_suspended = NX_NULL;
123     }
124 
125     /* Reset the receive queue.  */
126     if (dtls_session -> nx_secure_dtls_receive_queue_head)
127     {
128         packet_ptr = dtls_session -> nx_secure_dtls_receive_queue_head;
129         dtls_session -> nx_secure_dtls_receive_queue_head = NX_NULL;
130     }
131 
132     /* Reset the internal TLS session state. */
133     _nx_secure_tls_session_reset(&dtls_session -> nx_secure_dtls_tls_session);
134 
135     /* Release the protection. */
136     tx_mutex_put(&_nx_secure_tls_protection);
137 
138     /* Release the queued packets.  */
139     while (packet_ptr)
140     {
141         next_packet_ptr = packet_ptr -> nx_packet_queue_next;
142         nx_secure_tls_packet_release(packet_ptr);
143         packet_ptr = next_packet_ptr;
144     }
145 
146     return(NX_SUCCESS);
147 #else
148     NX_PARAMETER_NOT_USED(dtls_session);
149 
150     return(NX_NOT_SUPPORTED);
151 #endif /* NX_SECURE_ENABLE_DTLS */
152 }
153 
154