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 /**    Transport Layer Security (TLS)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 #include "nx_secure_tls.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_tls_session_delete                       PORTABLE C      */
32 /*                                                           6.1          */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function deletes a TLS session object, returning any resources */
40 /*    to the system.                                                      */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    tls_session                           TLS control block             */
45 /*                                                                        */
46 /*  OUTPUT                                                                */
47 /*                                                                        */
48 /*    status                                Completion status             */
49 /*                                                                        */
50 /*  CALLS                                                                 */
51 /*                                                                        */
52 /*    _nx_secure_tls_session_reset          Clear TLS control block       */
53 /*    tx_mutex_get                          Get protection mutex          */
54 /*    tx_mutex_put                          Put protection mutex          */
55 /*                                                                        */
56 /*  CALLED BY                                                             */
57 /*                                                                        */
58 /*    Application Code                                                    */
59 /*    _nx_secure_dtls_session_delete        Delete the DTLS session       */
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), and      */
67 /*                                            fixed race condition for    */
68 /*                                            multithread transmission,   */
69 /*                                            fixed underflow issue,      */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_nx_secure_tls_session_delete(NX_SECURE_TLS_SESSION * tls_session)73 UINT  _nx_secure_tls_session_delete(NX_SECURE_TLS_SESSION *tls_session)
74 {
75 UINT status;
76 
77     /* Reset the TLS state so this socket can be reused. */
78     status = _nx_secure_tls_session_reset(tls_session);
79 
80     /* Get the protection. */
81     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
82 
83     /* Remove the TLS instance from the created list. */
84     /* See if the TLS instance is the only one on the list. */
85     if (tls_session == tls_session -> nx_secure_tls_created_next)
86     {
87 
88         /* Only created TLS instance, just set the created list to NULL. */
89         _nx_secure_tls_created_ptr = NX_NULL;
90     }
91     else
92     {
93 
94         /* Otherwise, not the only created TLS, link-up the neighbors. */
95         if(tls_session -> nx_secure_tls_created_next != NX_NULL)
96         {
97             (tls_session -> nx_secure_tls_created_next) -> nx_secure_tls_created_previous =
98                     tls_session -> nx_secure_tls_created_previous;
99         }
100 
101         (tls_session -> nx_secure_tls_created_previous) -> nx_secure_tls_created_next =
102             tls_session -> nx_secure_tls_created_next;
103 
104         /* See if we have to update the created list head pointer. */
105         if (_nx_secure_tls_created_ptr == tls_session)
106         {
107 
108             /* Yes, move the head pointer to the next link. */
109             _nx_secure_tls_created_ptr = tls_session -> nx_secure_tls_created_next;
110         }
111     }
112 
113     /* We shouldn't need this conditional but occasionally automated code may
114        call delete after all sessions have already been deleted. */
115     if(_nx_secure_tls_created_count > 0)
116     {
117         _nx_secure_tls_created_count--;
118     }
119 
120     /* Make sure the session is completely reset - set ID to zero for error checking. */
121     tls_session -> nx_secure_tls_id = 0;
122 
123     /* Delete the mutex used for TLS session while transmitting packets. */
124     tx_mutex_delete(&(tls_session -> nx_secure_tls_session_transmit_mutex));
125 
126     /* Release the protection. */
127     tx_mutex_put(&_nx_secure_tls_protection);
128 
129     return(status);
130 }
131 
132