1
10
13
14
20
21
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
98
99
100
101
102
103
108
113
114
115
116
117
118
119
120
121
125
126
127
128
133
134
135
136
137
138
139
140
141
142
143
144
145
146
150
151
152
153
154
155
156
157
158
159
160
161
166
167
168
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
199
203
204
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
static UCHAR _generated_hash[NX_SECURE_TLS_MAX_HASH_SIZE];
static UCHAR _received_hash[NX_SECURE_TLS_MAX_HASH_SIZE];
...
...
UINT _nx_secure_tls_verify_mac(NX_SECURE_TLS_SESSION *tls_session, UCHAR *header_data,
USHORT header_length, NX_PACKET *packet_ptr, ULONG offset, UINT *length)
{
UCHAR *mac_secret;
USHORT hash_size;
INT compare_result;
USHORT data_length;
UINT hash_length;
UCHAR header[6];
ULONG bytes_copied;
if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL)
{
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... }
hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size;
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
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
{
mac_secret = tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret;
}else { ... }
if (hash_size >= *length)
{
if (header_data[0] == (UCHAR)(NX_SECURE_TLS_APPLICATION_DATA) &&
*length == hash_size)
{
/* ... */
*length = 0;
if ((tls_session -> nx_secure_tls_remote_sequence_number[0] + 1) == 0)
{
tls_session -> nx_secure_tls_remote_sequence_number[1]++;
}if ((tls_session -> nx_secure_tls_remote_sequence_number[0] + 1) == 0) { ... }
tls_session -> nx_secure_tls_remote_sequence_number[0]++;
return(NX_SUCCESS);
}if (header_data[0] == (UCHAR)(NX_SECURE_TLS_APPLICATION_DATA) && *length == hash_size) { ... }
return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE);
}if (hash_size >= *length) { ... }
data_length = (USHORT)(*length - hash_size);
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);
header[3] = (UCHAR)((data_length >> 8) & 0x00FF);
header[4] = (UCHAR)(data_length & 0x00FF);
_nx_secure_tls_hash_record(tls_session, tls_session -> nx_secure_tls_remote_sequence_number, header, header_length,
packet_ptr, offset, data_length, _generated_hash, &hash_length, mac_secret);
if ((tls_session -> nx_secure_tls_remote_sequence_number[0] + 1) == 0)
{
tls_session -> nx_secure_tls_remote_sequence_number[1]++;
}if ((tls_session -> nx_secure_tls_remote_sequence_number[0] + 1) == 0) { ... }
tls_session -> nx_secure_tls_remote_sequence_number[0]++;
if (hash_size == 0)
{
return(NX_SECURE_TLS_SUCCESS);
}if (hash_size == 0) { ... }
if (nx_packet_data_extract_offset(packet_ptr,
offset + data_length,
_received_hash, hash_size, &bytes_copied) ||
(bytes_copied != hash_size))
{
return(NX_SECURE_TLS_PADDING_CHECK_FAILED);
}if (nx_packet_data_extract_offset(packet_ptr, offset + data_length, _received_hash, hash_size, &bytes_copied) || (bytes_copied != hash_size)) { ... }
compare_result = NX_SECURE_MEMCMP(_received_hash, _generated_hash, hash_size);
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(_generated_hash, 0, sizeof(_generated_hash));
#endif
*length = data_length;
if (compare_result == 0)
{
return(NX_SECURE_TLS_SUCCESS);
}if (compare_result == 0) { ... }
else
{
return(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE);
}else { ... }
}{ ... }