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