Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_dtls.h"
...
Files
loading (3/7)...
SourceVuSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_dtls_verify_mac.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Secure Component */ /** */ /** Datagram Transport Layer Security (DTLS) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_dtls.h" #ifdef NX_SECURE_ENABLE_DTLS static UCHAR _generated_hash[NX_SECURE_TLS_MAX_HASH_SIZE]; /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_dtls_verify_mac PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function verifies the Message Authentication Code (MAC) that */ /* is included in encrypted DTLS records. It hashes the incoming */ /* message data and then compares it to the MAC in the received */ /* record. If there is a mismatch, then the record has been corrupted */ /* in transit and represents a possible security breach. */ /* */ /* INPUT */ /* */ /* dtls_session DTLS control block */ /* header_data DTLS record header data */ /* header_length Length of header data */ /* data DTLS record payload data */ /* length Length of payload data */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_dtls_hash_record Generate payload data hash */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_process_record Process DTLS record data */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_secure_dtls_verify_mac(NX_SECURE_DTLS_SESSION *dtls_session, UCHAR *header_data, USHORT header_length, UCHAR *data, UINT *length) { UCHAR *mac_secret; USHORT hash_size; UINT status; INT compare_result; USHORT data_length; UCHAR *received_hash; UINT hash_length; UCHAR header[20]; NX_SECURE_TLS_SESSION *tls_session; tls_session = &dtls_session -> nx_secure_dtls_tls_session; if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get the hash size and MAC secret for our current session. */ hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size; /* Select our proper MAC secret for hashing. */ if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { /* If we are a server, we need to use the client's MAC secret. */ mac_secret = tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_mac_secret; }if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... } else { /* We are a client, so use the server's MAC secret. */ mac_secret = tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret; }else { ... } if (hash_size >= *length) { /* The record data was smaller than the selected hash... Error. */ return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE); }if (hash_size >= *length) { ... } /* Adjust our length so we only hash the record data, not the hash as well. */ data_length = (USHORT)(*length - hash_size); /* Copy the header data into our local buffer so we can change it if we need to. */ if (header_length > sizeof(header)) { return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE); }if (header_length > sizeof(header)) { ... } NX_SECURE_MEMCPY(header, header_data, header_length); /* Use case of memcpy is verified. */ /* Adjust the length in the header to match data without hash. */ /* In DTLS, the length is at offset 11. */ header[11] = (UCHAR)((data_length >> 8) & 0x00FF); header[12] = (UCHAR)(data_length & 0x00FF); /* Generate the hash on the plaintext data. */ status = _nx_secure_dtls_hash_record(dtls_session, tls_session -> nx_secure_tls_remote_sequence_number, header, header_length, data, (USHORT)(data_length), _generated_hash, &hash_length, mac_secret); if (status != NX_SUCCESS) { /* The hash operation failed for some reason. */ return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE); }if (status != NX_SUCCESS) { ... } /* In DTLS, the sequence number is explicit in the record. In TLS the sequence number would be incremented here. */ /* Now, compare the hash we generated to the one we received. */ received_hash = &data[data_length]; compare_result = NX_SECURE_MEMCMP(received_hash, _generated_hash, hash_size); /* Before we return, adjust our data size so the caller will only see data, not the hash. */ *length = data_length; /* If the hashes match, we are all good. Otherwise we have a problem. */ if (compare_result == 0) { return(NX_SECURE_TLS_SUCCESS); }if (compare_result == 0) { ... } else { return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE); }else { ... } }_nx_secure_dtls_verify_mac (NX_SECURE_DTLS_SESSION *dtls_session, UCHAR *header_data, USHORT header_length, UCHAR *data, UINT *length) { ... } /* ... */#endif /* NX_SECURE_ENABLE_DTLS */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.