Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
_nx_secure_tls_send_serverhello(NX_SECURE_TLS_SESSION *, NX_PACKET *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_send_serverhello.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Transport Layer Security (TLS) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" #if (NX_SECURE_TLS_TLS_1_3_ENABLED) const UCHAR _nx_secure_tls_hello_retry_request_random[32] = { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C ...}; const UCHAR _nx_secure_tls_1_2_random[8] = { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01 ...}; const UCHAR _nx_secure_tls_1_1_random[8] = { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x00 ...};/* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_send_serverhello PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function generates a ServerHello message, which is used to */ /* respond to an incoming ClientHello message and provide the remote */ /* TLS Client with the chosen ciphersuite and key generation data. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* send_packet Packet used to send message */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tls_send_serverhello_extensions */ /* Send TLS ServerHello extension*/ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_server_handshake TLS server state machine */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), improved */ /* buffer length verification, */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), fixed */ /* compilation issue with */ /* TLS 1.3 and disabling TLS */ /* server, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_send_serverhello(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet) { #ifndef NX_SECURE_TLS_SERVER_DISABLED ULONG length; UINT gmt_time; UINT random_value; UINT i; USHORT ciphersuite; UCHAR *packet_buffer; USHORT protocol_version; UINT status; /* Build up the ServerHello message. * Structure: * | 2 | 4 + 28 | 1 | <SID len> | 2 | 1 | 2 | <Ext. Len> | * | TLS version | Random (time + random) | SID length | Session ID | Ciphersuite | Compression | Ext. Len | Extensions | *//* ... */ if ((6u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random)) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { /* Packet buffer is too small to hold random. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if ((6u + sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random)) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { ... } packet_buffer = send_packet -> nx_packet_append_ptr; /* Use our length as an index into the buffer. */ length = 0; /* First two bytes of the hello following the header are the TLS major and minor version numbers. */ protocol_version = tls_session -> nx_secure_tls_protocol_version; packet_buffer[length] = (UCHAR)((protocol_version & 0xFF00) >> 8); packet_buffer[length + 1] = (UCHAR)(protocol_version & 0x00FF); length += 2; /* Set the Server random data, used in key generation. First 4 bytes is GMT time. */ gmt_time = 0; if (tls_session -> nx_secure_tls_session_time_function != NX_NULL) { gmt_time = tls_session -> nx_secure_tls_session_time_function(); }if (tls_session -> nx_secure_tls_session_time_function != NX_NULL) { ... } NX_CHANGE_ULONG_ENDIAN(gmt_time); NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random, (UCHAR *)&gmt_time, sizeof(gmt_time)); /* Use case of memcpy is verified. */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY) { NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random, _nx_secure_tls_hello_retry_request_random, sizeof(_nx_secure_tls_hello_retry_request_random)); /* Use case of memcpy is verified. */ }if (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY) { ... } else if (!(tls_session -> nx_secure_tls_1_3) && !(tls_session -> nx_secure_tls_protocol_version_override)) { /* Next 20 bytes is random data. */ for (i = 4; i <= 20; i += (UINT)sizeof(random_value)) { random_value = (UINT)NX_RAND(); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i] = (UCHAR)(random_value); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 1] = (UCHAR)(random_value >> 8); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 2] = (UCHAR)(random_value >> 16); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 3] = (UCHAR)(random_value >> 24); }for (i = 4; i <= 20; i += (UINT)sizeof(random_value)) { ... } /* RFC 8446 4.1.3: TLS 1.3 servers which negotiate TLS 1.2 or below in response to a ClientHello MUST set the last 8 bytes of their Random value specially in their ServerHello. *//* ... */ if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) { NX_SECURE_MEMCPY(&(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[24]), _nx_secure_tls_1_2_random, sizeof(_nx_secure_tls_1_2_random)); /* Use case of memcpy is verified. */ }if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) { ... } else { NX_SECURE_MEMCPY(&(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[24]), _nx_secure_tls_1_1_random, sizeof(_nx_secure_tls_1_1_random)); /* Use case of memcpy is verified. */ }else { ... } }else if (!(tls_session -> nx_secure_tls_1_3) && !(tls_session -> nx_secure_tls_protocol_version_override)) { ... } else #endif { /* Next 28 bytes is random data. */ for (i = 4; i <= 28; i += (UINT)sizeof(random_value)) { random_value = (UINT)NX_RAND(); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i] = (UCHAR)(random_value); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 1] = (UCHAR)(random_value >> 8); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 2] = (UCHAR)(random_value >> 16); tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random[i + 3] = (UCHAR)(random_value >> 24); }for (i = 4; i <= 28; i += (UINT)sizeof(random_value)) { ... } }else { ... } /* Copy the random data into the packet. */ NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random, sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random)); /* Use case of memcpy is verified. */ length += sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_random); /* Session ID length is one byte. Session ID data follows if we ever implement session resumption. */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if(tls_session->nx_secure_tls_1_3) { /* TLS 1.3 doesn't use the session ID, but the Server must echo the server ID provided in the ClientHello back to the client or the client will abort the handshake (RFC 8446, Section 4.1.3). *//* ... */ packet_buffer[length] = tls_session->nx_secure_tls_session_id_length; length++; if ((length + tls_session->nx_secure_tls_session_id_length + 3) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { /* Packet buffer is too small to hold random. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if ((length + tls_session->nx_secure_tls_session_id_length + 3) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr))) { ... } NX_SECURE_MEMCPY(&packet_buffer[length], tls_session -> nx_secure_tls_session_id, tls_session->nx_secure_tls_session_id_length); /* Use case of memcpy is verified. */ length += tls_session->nx_secure_tls_session_id_length; }if (tls_session->nx_secure_tls_1_3) { ... } else #endif { packet_buffer[length] = 0; length++; }else { ... } /* Finally, our chosen ciphersuite - this is selected when we receive the Client Hello. */ ciphersuite = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_ciphersuite; packet_buffer[length] = (UCHAR)(ciphersuite >> 8); packet_buffer[length + 1] = (UCHAR)ciphersuite; length += 2; /* Compression method - for now this is NULL. */ packet_buffer[length] = 0x0; length++; /* ============ TLS ServerHello extensions. ============= */ status = _nx_secure_tls_send_serverhello_extensions(tls_session, packet_buffer, &length, ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)packet_buffer)); if (status) { return(status); }if (status) { ... } /* Finally, we have a complete length and can put it into the buffer. Before that, save off and return the number of bytes we wrote and need to send. *//* ... */ send_packet -> nx_packet_append_ptr = send_packet -> nx_packet_append_ptr + length; send_packet -> nx_packet_length = send_packet -> nx_packet_length + length; return(status); /* ... */ #else NX_PARAMETER_NOT_USED(send_packet); /* If TLS Server is disabled and we have processed a ClientKeyExchange, something is wrong... */ tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_ERROR; /* Server is disabled, we shouldn't be sending a ServerHello - error! */ return(NX_SECURE_TLS_INVALID_STATE);/* ... */ #endif }{ ... }