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