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 /** Datagram Transport Layer Security (DTLS) */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define NX_SECURE_SOURCE_CODE
23
24
25 #include "nx_secure_dtls.h"
26
27 #ifdef NX_SECURE_ENABLE_DTLS
28 /**************************************************************************/
29 /* */
30 /* FUNCTION RELEASE */
31 /* */
32 /* _nx_secure_dtls_allocate_handshake_packet PORTABLE C */
33 /* 6.1 */
34 /* AUTHOR */
35 /* */
36 /* Timothy Stapko, Microsoft Corporation */
37 /* */
38 /* DESCRIPTION */
39 /* */
40 /* This function allocates a packet, positions the prepend_ptr and */
41 /* append_ptr suitable for DTLS handshake packets. */
42 /* */
43 /* INPUT */
44 /* */
45 /* dtls_session DTLS control block */
46 /* packet_pool The pool to allocate from */
47 /* packet_ptr Pointer to the allocated */
48 /* packet */
49 /* wait_option Controls timeout actions */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* status Completion status */
54 /* */
55 /* CALLS */
56 /* */
57 /* _nx_secure_dtls_packet_allocate Allocate DTLS packet */
58 /* tx_mutex_get Get protection mutex */
59 /* tx_mutex_put Put protection mutex */
60 /* */
61 /* CALLED BY */
62 /* */
63 /* _nx_secure_dtls_client_handshake DTLS client state machine */
64 /* _nx_secure_dtls_server_handshake DTLS server state machine */
65 /* _nx_secure_dtls_session_start Actual DTLS session start call*/
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_dtls_allocate_handshake_packet(NX_SECURE_DTLS_SESSION * dtls_session,NX_PACKET_POOL * packet_pool,NX_PACKET ** packet_ptr,ULONG wait_option)76 UINT _nx_secure_dtls_allocate_handshake_packet(NX_SECURE_DTLS_SESSION *dtls_session,
77 NX_PACKET_POOL *packet_pool, NX_PACKET **packet_ptr,
78 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_dtls_packet_allocate(dtls_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_DTLS_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_DTLS_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 #endif /* NX_SECURE_ENABLE_DTLS */
112
113