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