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 necessary system files.  */
26 
27 #include "nx_secure_dtls.h"
28 
29 /* Bring in externs for caller checking code.  */
30 
31 NX_SECURE_CALLER_CHECKING_EXTERNS
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nxe_secure_dtls_session_create                     PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Timothy Stapko, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function checks for errors in the DTLS session create call.    */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    session_ptr                           DTLS session control block    */
50 /*    crypto_table                          Crypto table                  */
51 /*    metadata_buffer                       Encryption metadata buffer    */
52 /*    metadata_size                         Encryption metadata size      */
53 /*    packet_reassembly_buffer              DTLS reassembly buffer        */
54 /*    packet_reassembly_buffer_size         Size of reassembly buffer     */
55 /*    certs_number                          Number of certs               */
56 /*    remote_certificate_buffer             Remote certificate buffer     */
57 /*    remote_certificate_buffer_size        Remote certificate buffer size*/
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    status                                Completion status             */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _nx_secure_dtls_session_create        Actual DTLS session create    */
66 /*                                            call                        */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    Application Code                                                    */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
77 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
_nxe_secure_dtls_session_create(NX_SECURE_DTLS_SESSION * session_ptr,const NX_SECURE_TLS_CRYPTO * crypto_table,VOID * metadata_buffer,ULONG metadata_size,UCHAR * packet_reassembly_buffer,UINT packet_reassembly_buffer_size,UINT certs_number,UCHAR * remote_certificate_buffer,ULONG remote_certificate_buffer_size)81 UINT _nxe_secure_dtls_session_create(NX_SECURE_DTLS_SESSION *session_ptr,
82                                     const NX_SECURE_TLS_CRYPTO *crypto_table,
83                                     VOID *metadata_buffer, ULONG metadata_size,
84                                     UCHAR *packet_reassembly_buffer, UINT packet_reassembly_buffer_size,
85                                     UINT certs_number,
86                                     UCHAR *remote_certificate_buffer, ULONG remote_certificate_buffer_size)
87 {
88 #ifdef NX_SECURE_ENABLE_DTLS
89 UINT status;
90 NX_SECURE_DTLS_SESSION *created_dtls_session;
91 ULONG created_count;
92 
93     /* Check pointers. NOTE: Remote certificates number can be zero, so buffer can be NULL! */
94     if ((session_ptr == NX_NULL) || (crypto_table == NX_NULL) ||
95         (metadata_buffer == NX_NULL) ||
96         ((packet_reassembly_buffer == NX_NULL) && (packet_reassembly_buffer_size != 0)))
97     {
98         return(NX_PTR_ERROR);
99     }
100 
101     /* Loop to check for the DTLS session already created.  */
102     created_dtls_session = _nx_secure_dtls_created_ptr;
103     created_count = _nx_secure_dtls_created_count;
104     while (created_count--)
105     {
106 
107         /* Is the new DTLS already created?  */
108         if (session_ptr == created_dtls_session)
109         {
110 
111             /* Duplicate DTLS session created, return an error!  */
112             return(NX_PTR_ERROR);
113         }
114 
115         /* Move to next entry.  */
116         created_dtls_session = created_dtls_session -> nx_secure_dtls_created_next;
117     }
118 
119     status = _nx_secure_dtls_session_create(session_ptr, crypto_table, metadata_buffer, metadata_size,
120                                             packet_reassembly_buffer, packet_reassembly_buffer_size,
121                                             certs_number, remote_certificate_buffer, remote_certificate_buffer_size);
122 
123     /* Return completion status.  */
124     return(status);
125 #else
126     NX_PARAMETER_NOT_USED(session_ptr);
127     NX_PARAMETER_NOT_USED(crypto_table);
128     NX_PARAMETER_NOT_USED(metadata_buffer);
129     NX_PARAMETER_NOT_USED(metadata_size);
130     NX_PARAMETER_NOT_USED(packet_reassembly_buffer);
131     NX_PARAMETER_NOT_USED(packet_reassembly_buffer_size);
132     NX_PARAMETER_NOT_USED(certs_number);
133     NX_PARAMETER_NOT_USED(remote_certificate_buffer);
134     NX_PARAMETER_NOT_USED(remote_certificate_buffer_size);
135 
136     return(NX_NOT_SUPPORTED);
137 #endif /* NX_SECURE_ENABLE_DTLS */
138 }
139 
140