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_delete                      PORTABLE C      */
32 /*                                                           6.1          */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function deletes a DTLS session object, returning any resources*/
40 /*    to the system.                                                      */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    dtls_session                          DTLS session control block    */
45 /*                                                                        */
46 /*  OUTPUT                                                                */
47 /*                                                                        */
48 /*    status                                Completion status             */
49 /*                                                                        */
50 /*  CALLS                                                                 */
51 /*                                                                        */
52 /*    _nx_secure_dtls_session_reset         Clear DTLS control block      */
53 /*    _nx_secure_tls_session_delete         Delete TLS session            */
54 /*    tx_mutex_get                          Get protection mutex          */
55 /*    tx_mutex_put                          Put protection mutex          */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Application Code                                                    */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
66 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*                                                                        */
69 /**************************************************************************/
_nx_secure_dtls_session_delete(NX_SECURE_DTLS_SESSION * dtls_session)70 UINT  _nx_secure_dtls_session_delete(NX_SECURE_DTLS_SESSION *dtls_session)
71 {
72 #ifdef NX_SECURE_ENABLE_DTLS
73 UINT status;
74 
75 
76     /* Reset the DTLS state so this socket can be reused. */
77     _nx_secure_dtls_session_reset(dtls_session);
78 
79     /* Delete TLS session.  */
80     status = _nx_secure_tls_session_delete(&dtls_session -> nx_secure_dtls_tls_session);
81 
82     /* Get the protection. */
83     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
84 
85     /* Remove the DTLS instance from the created list. */
86     /* See if the DTLS instance is the only one on the list. */
87     if (dtls_session == dtls_session -> nx_secure_dtls_created_next)
88     {
89 
90         /* Only created DTLS instance, just set the created list to NULL. */
91         _nx_secure_dtls_created_ptr = NX_NULL;
92     }
93     else
94     {
95 
96         /* Otherwise, not the only created DTLS, link-up the neighbors. */
97         if (dtls_session -> nx_secure_dtls_created_next != NX_NULL)
98         {
99             (dtls_session -> nx_secure_dtls_created_next) -> nx_secure_dtls_created_previous =
100                     dtls_session -> nx_secure_dtls_created_previous;
101         }
102 
103         (dtls_session -> nx_secure_dtls_created_previous) -> nx_secure_dtls_created_next =
104             dtls_session -> nx_secure_dtls_created_next;
105 
106         /* See if we have to update the created list head pointer. */
107         if (_nx_secure_dtls_created_ptr == dtls_session)
108         {
109 
110             /* Yes, move the head pointer to the next link. */
111             _nx_secure_dtls_created_ptr = dtls_session -> nx_secure_dtls_created_next;
112         }
113     }
114     _nx_secure_dtls_created_count--;
115 
116     /* Release the protection. */
117     tx_mutex_put(&_nx_secure_tls_protection);
118 
119     return(status);
120 #else
121     NX_PARAMETER_NOT_USED(dtls_session);
122 
123     return(NX_NOT_SUPPORTED);
124 #endif /* NX_SECURE_ENABLE_DTLS */
125 }
126 
127