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 
25 #include "nx_secure_tls.h"
26 
27 /* Bring in externs for caller checking code.  */
28 
29 NX_SECURE_CALLER_CHECKING_EXTERNS
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxe_secure_tls_packet_allocate                     PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Timothy Stapko, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for errors in TLS packet allocate call.        */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    tls_session                           TLS control block             */
48 /*    pool_ptr                              Pool to allocate packet from  */
49 /*    packet_ptr                            Pointer to place allocated    */
50 /*                                            packet pointer              */
51 /*    wait_option                           Suspension option             */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _nx_secure_tls_packet_allocate        Actual packet allocate call.  */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
70 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_nxe_secure_tls_packet_allocate(NX_SECURE_TLS_SESSION * tls_session,NX_PACKET_POOL * pool_ptr,NX_PACKET ** packet_ptr,ULONG wait_option)74 UINT _nxe_secure_tls_packet_allocate(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET_POOL *pool_ptr,
75                                      NX_PACKET **packet_ptr, ULONG wait_option)
76 {
77 UINT status;
78 
79     /* Verify tls_session is valid. */
80 
81     /* Make sure the session is initialized. */
82     if(tls_session -> nx_secure_tls_id != NX_SECURE_TLS_ID)
83     {
84         return(NX_SECURE_TLS_SESSION_UNINITIALIZED);
85     }
86 
87     /* Verify the TCP socket valid and is connected. */
88     if (tls_session -> nx_secure_tls_tcp_socket == NX_NULL)
89     {
90         return(NX_SECURE_TLS_SESSION_UNINITIALIZED);
91     }
92 
93     if ((tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_connect_ip.nxd_ip_version !=  NX_IP_VERSION_V4) &&
94         (tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_connect_ip.nxd_ip_version !=  NX_IP_VERSION_V6))
95     {
96         return(NX_SECURE_TLS_SESSION_UNINITIALIZED);
97     }
98 
99     if (tls_session -> nx_secure_tls_tcp_socket -> nx_tcp_socket_state != NX_TCP_ESTABLISHED)
100     {
101         return(NX_SECURE_TLS_SESSION_UNINITIALIZED);
102     }
103 
104     /* Check for appropriate caller.  */
105     NX_THREADS_ONLY_CALLER_CHECKING
106 
107     status =  _nx_secure_tls_packet_allocate(tls_session, pool_ptr, packet_ptr, wait_option);
108 
109     return(status);
110 }
111 
112