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_allocate_handshake_packet            PORTABLE C      */
31 /*                                                           6.1          */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Timothy Stapko, Microsoft Corporation                               */
35 /*                                                                        */
36 /*  DESCRIPTION                                                           */
37 /*                                                                        */
38 /*    This function allocates a packet, positions the prepend_ptr and     */
39 /*    append_ptr suitable for TLS handshake packets.                      */
40 /*                                                                        */
41 /*  INPUT                                                                 */
42 /*                                                                        */
43 /*    tls_session                           TLS control block             */
44 /*    packet_pool                           The pool to allocate from     */
45 /*    packet_ptr                            Pointer to the allocated      */
46 /*                                            packet                      */
47 /*    wait_option                           Controls timeout actions      */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    status                                Completion status             */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    _nx_secure_tls_packet_allocate        Allocate internal TLS packet  */
56 /*    tx_mutex_get                          Get protection mutex          */
57 /*    tx_mutex_put                          Put protection mutex          */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    _nx_secure_tls_server_handshake       TLS Server state machine      */
62 /*    _nx_secure_tls_client_handshake       TLS Client state machine      */
63 /*    _nx_secure_tls_session_renegotiate    Renegotiate TLS session       */
64 /*    _nx_secure_tls_session_start          Start TLS 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 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_secure_tls_allocate_handshake_packet(NX_SECURE_TLS_SESSION * tls_session,NX_PACKET_POOL * packet_pool,NX_PACKET ** packet_ptr,ULONG wait_option)75 UINT _nx_secure_tls_allocate_handshake_packet(NX_SECURE_TLS_SESSION *tls_session,
76                                               NX_PACKET_POOL *packet_pool,
77                                               NX_PACKET **packet_ptr, ULONG wait_option)
78 {
79 UINT status;
80 
81     /* Release the protection before suspending on nx_packet_allocate. */
82     tx_mutex_put(&_nx_secure_tls_protection);
83 
84     status = _nx_secure_tls_packet_allocate(tls_session, packet_pool, packet_ptr, wait_option);
85 
86     /* Get the protection after nx_packet_allocate. */
87     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
88 
89     if (status != NX_SECURE_TLS_SUCCESS)
90     {
91         return(NX_SECURE_TLS_ALLOCATE_PACKET_FAILED);
92     }
93 
94     if (((ULONG)((*packet_ptr) -> nx_packet_data_end) - (ULONG)((*packet_ptr) -> nx_packet_prepend_ptr)) <
95         NX_SECURE_TLS_HANDSHAKE_HEADER_SIZE)
96     {
97 
98         /* Packet buffer is too small. */
99         nx_packet_release(*packet_ptr);
100         return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
101     }
102 
103     /* Allocate space for the handshake header. */
104     (*packet_ptr) -> nx_packet_prepend_ptr += NX_SECURE_TLS_HANDSHAKE_HEADER_SIZE;
105     (*packet_ptr) -> nx_packet_append_ptr = (*packet_ptr) -> nx_packet_prepend_ptr;
106 
107 
108     return(NX_SECURE_TLS_SUCCESS);
109 }
110 
111