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