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_handshake_process                    PORTABLE C      */
32 /*                                                           6.1          */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function processes a TLS handshake, whether at the beginning   */
40 /*    of a new TLS connection or during a session re-negotiation. The     */
41 /*    handshake state machine is implemented for each of TLS Client and   */
42 /*    Server in their own functions, this function is simply the entry    */
43 /*    point for handling the handshake messages.                          */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    tls_session                           TLS control block             */
48 /*    wait_option                           Suspension option             */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Completion status             */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _nx_secure_tls_session_receive_records                              */
57 /*                                          Receive TLS records           */
58 /*    nx_secure_tls_packet_release          Release packet                */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    _nx_secure_tls_session_start          Start TLS session             */
63 /*    _nx_secure_tls_session_receive        Receive TCP data              */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
70 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
71 /*                                            released packet securely,   */
72 /*                                            fixed compiler warnings,    */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_nx_secure_tls_handshake_process(NX_SECURE_TLS_SESSION * tls_session,UINT wait_option)76 UINT _nx_secure_tls_handshake_process(NX_SECURE_TLS_SESSION *tls_session, UINT wait_option)
77 {
78 UINT       status = NX_NOT_SUCCESSFUL;
79 NX_PACKET *incoming_packet = NX_NULL;
80 
81     /* Process the handshake depending on the TLS session type. */
82 #ifndef NX_SECURE_TLS_CLIENT_DISABLED
83     if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT)
84     {
85 
86         /* Handle our incoming handshake messages. Continue processing until the handshake is complete
87          * or an error/timeout occurs. */
88         while (tls_session -> nx_secure_tls_client_state != NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED)
89         {
90             status = _nx_secure_tls_session_receive_records(tls_session, &incoming_packet, wait_option);
91 
92             /* Make sure we didn't have an error during the receive. */
93             if (status != NX_SUCCESS)
94             {
95                 break;
96             }
97         }
98 
99         if (tls_session -> nx_secure_tls_client_state == NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED)
100         {
101 
102             /* Release the incoming packet if we do receive it. */
103             nx_secure_tls_packet_release(incoming_packet);
104         }
105     }
106 #endif
107 
108 #ifndef NX_SECURE_TLS_SERVER_DISABLED
109     if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
110     {
111         /* Session is a TLS Server type. */
112         /* The client socket connection has already been accepted at this point, process the handshake.  */
113 
114         /* Now handle our incoming handshake messages. Continue processing until the handshake is complete
115          * or an error/timeout occurs. */
116         while (tls_session -> nx_secure_tls_server_state != NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED)
117         {
118             status = _nx_secure_tls_session_receive_records(tls_session, &incoming_packet, wait_option);
119 
120             /* Make sure we didn't have an error during the receive. */
121             if (status != NX_SUCCESS)
122             {
123                 break;
124             }
125         }
126 
127         if (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED)
128         {
129 
130             /* Release the incoming packet if we do receive it. */
131             nx_secure_tls_packet_release(incoming_packet);
132         }
133     }
134 #endif
135 
136     if ((status == NX_NO_PACKET) && (wait_option == 0))
137     {
138 
139         /* It is non blocking mode. */
140         status = NX_CONTINUE;
141     }
142 
143     return(status);
144 }
145 
146