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