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 #include "nx_secure_dtls.h"
25 
26 #ifdef NX_SECURE_ENABLE_DTLS
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_dtls_process_handshake_header            PORTABLE C      */
32 /*                                                           6.1.3        */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function processes a DTLS Handshake record header, which is    */
40 /*    at the beginning of each DTLS Handshake message, encapsulated       */
41 /*    within the DTLS record itself.                                      */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    packet_buffer                         Pointer to incoming packet    */
46 /*    message_type                          Return message type value     */
47 /*    header_size                           Input size of packet buffer   */
48 /*                                            Return size of header       */
49 /*    message_length                        Return length of message      */
50 /*    message_seq                           Return sequence of message    */
51 /*    fragment_offset                       Return offset of fragment     */
52 /*    fragment_length                       Return length of fragment     */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    None                                                                */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _nx_secure_dtls_client_handshake      DTLS client state machine     */
65 /*    _nx_secure_dtls_server_handshake      DTLS server state machine     */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
72 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  12-31-2020     Timothy Stapko           Modified comment(s),          */
75 /*                                            improved buffer length      */
76 /*                                            verification,               */
77 /*                                            resulting in version 6.1.3  */
78 /*                                                                        */
79 /**************************************************************************/
_nx_secure_dtls_process_handshake_header(UCHAR * packet_buffer,USHORT * message_type,UINT * header_size,UINT * message_length,UINT * message_seq,UINT * fragment_offset,UINT * fragment_length)80 UINT _nx_secure_dtls_process_handshake_header(UCHAR *packet_buffer, USHORT *message_type,
81                                               UINT *header_size, UINT *message_length,
82                                               UINT *message_seq, UINT *fragment_offset,
83                                               UINT *fragment_length)
84 {
85 
86     /* Check buffer length. */
87     if (*header_size < NX_SECURE_DTLS_HANDSHAKE_HEADER_SIZE)
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     packet_buffer += 3;
100 
101     /* Extract message sequence number. */
102     *message_seq = (UINT)((packet_buffer[0] << 8) + packet_buffer[1]);
103     packet_buffer += 2;
104 
105     /* Extract fragment offset. */
106     *fragment_offset = (UINT)((packet_buffer[0] << 16) + (packet_buffer[1] << 8) + packet_buffer[2]);
107     packet_buffer += 3;
108 
109     /* Extract fragment length. */
110     *fragment_length = (UINT)((packet_buffer[0] << 16) + (packet_buffer[1] << 8) + packet_buffer[2]);
111 
112     /* We have extracted the DTLS header. */
113     *header_size = NX_SECURE_DTLS_HANDSHAKE_HEADER_SIZE;
114 
115     return(NX_SECURE_TLS_SUCCESS);
116 }
117 #endif /* NX_SECURE_ENABLE_DTLS */
118 
119