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 /**    Datagram Transport Layer Security (DTLS)                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 
26 #include "nx_secure_dtls.h"
27 
28 #ifdef NX_SECURE_ENABLE_DTLS
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    _nx_secure_dtls_allocate_handshake_packet           PORTABLE C      */
34 /*                                                           6.1          */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    Timothy Stapko, Microsoft Corporation                               */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function allocates a packet, positions the prepend_ptr and     */
42 /*    append_ptr suitable for DTLS handshake packets.                     */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    dtls_session                          DTLS control block            */
47 /*    packet_pool                           The pool to allocate from     */
48 /*    packet_ptr                            Pointer to the allocated      */
49 /*                                            packet                      */
50 /*    wait_option                           Controls timeout actions      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _nx_secure_dtls_packet_allocate       Allocate DTLS packet          */
59 /*    tx_mutex_get                          Get protection mutex          */
60 /*    tx_mutex_put                          Put protection mutex          */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _nx_secure_dtls_client_handshake      DTLS client state machine     */
65 /*    _nx_secure_dtls_server_handshake      DTLS server state machine     */
66 /*    _nx_secure_dtls_session_start         Actual DTLS session start call*/
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
73 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_secure_dtls_allocate_handshake_packet(NX_SECURE_DTLS_SESSION * dtls_session,NX_PACKET_POOL * packet_pool,NX_PACKET ** packet_ptr,ULONG wait_option)77 UINT _nx_secure_dtls_allocate_handshake_packet(NX_SECURE_DTLS_SESSION *dtls_session,
78                                                NX_PACKET_POOL *packet_pool, NX_PACKET **packet_ptr,
79                                                ULONG wait_option)
80 {
81 UINT status;
82 
83     /* Release the protection before suspending on nx_packet_allocate. */
84     tx_mutex_put(&_nx_secure_tls_protection);
85 
86     status = _nx_secure_dtls_packet_allocate(dtls_session, packet_pool, packet_ptr, wait_option);
87 
88     /* Get the protection after nx_packet_allocate. */
89     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
90 
91     if (status != NX_SECURE_TLS_SUCCESS)
92     {
93         return(NX_SECURE_TLS_ALLOCATE_PACKET_FAILED);
94     }
95 
96     if (((ULONG)((*packet_ptr) -> nx_packet_data_end) - (ULONG)((*packet_ptr) -> nx_packet_prepend_ptr)) <
97         NX_SECURE_DTLS_HANDSHAKE_HEADER_SIZE)
98     {
99 
100         /* Packet buffer is too small. */
101         nx_packet_release(*packet_ptr);
102         return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
103     }
104 
105     /* Allocate space for the handshake header. */
106     (*packet_ptr) -> nx_packet_prepend_ptr += NX_SECURE_DTLS_HANDSHAKE_HEADER_SIZE;
107     (*packet_ptr) -> nx_packet_append_ptr = (*packet_ptr) -> nx_packet_prepend_ptr;
108 
109 
110     return(NX_SECURE_TLS_SUCCESS);
111 }
112 #endif /* NX_SECURE_ENABLE_DTLS */
113 
114