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 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    _nx_secure_tls_process_handshake_header             PORTABLE C      */
34 /*                                                           6.1.11       */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    Timothy Stapko, Microsoft Corporation                               */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function processes a TLS Handshake record header, which is     */
42 /*    at the beginning of each TLS Handshake message, encapsulated within */
43 /*    the TLS record itself.                                              */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    packet_buffer                         Pointer to incoming packet    */
48 /*    message_type                          Return message type value     */
49 /*    header_size                           Input size of packet buffer   */
50 /*                                            Return size of header       */
51 /*    message_length                        Return length of message      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_secure_tls_client_handshake       TLS client state machine      */
64 /*    _nx_secure_tls_server_handshake       TLS server state machine      */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
71 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*  12-31-2020     Timothy Stapko           Modified comment(s),          */
74 /*                                            improved buffer length      */
75 /*                                            verification,               */
76 /*                                            resulting in version 6.1.3  */
77 /*  04-25-2022     Yuxin Zhou               Modified comment(s),          */
78 /*                                            removed unused code,        */
79 /*                                            resulting in version 6.1.11 */
80 /*                                                                        */
81 /**************************************************************************/
_nx_secure_tls_process_handshake_header(UCHAR * packet_buffer,USHORT * message_type,UINT * header_size,UINT * message_length)82 UINT _nx_secure_tls_process_handshake_header(UCHAR *packet_buffer, USHORT *message_type,
83                                              UINT *header_size, UINT *message_length)
84 {
85 
86     /* Check buffer length. */
87     if (*header_size < 4)
88     {
89         return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH);
90     }
91 
92     /* The message being passed in to this function should already be stripped of the TLS header
93        so the first byte in the packet/record is our handshake message type. */
94     *message_type = packet_buffer[0];
95     packet_buffer++;
96 
97     /* Get the length of the TLS data. */
98     *message_length = (UINT)((packet_buffer[0] << 16) + (packet_buffer[1] << 8) + packet_buffer[2]);
99 
100     /* We have extracted 4 bytes of the header. */
101     *header_size = 4;
102 
103     return(NX_SECURE_TLS_SUCCESS);
104 }
105 
106