Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
#define NX_SECURE_SOURCE_CODE
_nx_secure_tls_session_keys_set(NX_SECURE_TLS_SESSION *, USHORT)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_session_keys_set.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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) - Generate Session Keys */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_session_keys_set PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sets the session keys for a TLS session following the */ /* sending or receiving of a ChangeCipherSpec message. In */ /* renegotiation handshakes, two separate set of session keys will be */ /* in use simultaneously so we need this to be able to separate which */ /* keys are actually in use. */ /* */ /* Once the keys are set, this function initializes the appropriate */ /* session cipher with the new key set. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* key_set Remote or local keys */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_init] Initialize crypto */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_client_handshake DTLS client state machine */ /* _nx_secure_dtls_server_handshake DTLS server state machine */ /* _nx_secure_tls_client_handshake TLS client state machine */ /* _nx_secure_tls_server_handshake TLS server state machine */ /* _nx_secure_tls_process_changecipherspec */ /* Process ChangeCipherSpec */ /* */ /* 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 */ /* 08-02-2021 Timothy Stapko Modified comment(s), added */ /* cleanup for session cipher, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ #define NX_SECURE_SOURCE_CODE UINT _nx_secure_tls_session_keys_set(NX_SECURE_TLS_SESSION *tls_session, USHORT key_set) { UINT status; UCHAR *key_block; /* Maximum ciphersuite key size - AES_256_CBC_SHA, 2x32 byte keys + 2x20 byte MAC secrets + 2x16 IVs. */ UINT key_size; UINT hash_size; UINT iv_size; UINT key_offset; UINT is_client; const NX_CRYPTO_METHOD *session_cipher_method = NX_NULL; /* The key material should have already been generated by nx_secure_tls_generate_keys once all * key generation data was available. This simply switches the appropriate key data over to the active * key block *//* ... */ if (key_set == NX_SECURE_TLS_KEY_SET_LOCAL) { tls_session -> nx_secure_tls_local_session_active = 1; }if (key_set == NX_SECURE_TLS_KEY_SET_LOCAL) { ... } else { tls_session -> nx_secure_tls_remote_session_active = 1; }else { ... } /* Working pointers into our key material blocks. */ key_block = tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data; /* See if we are setting server or client keys. */ if ((key_set == NX_SECURE_TLS_KEY_SET_REMOTE && tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) || (key_set == NX_SECURE_TLS_KEY_SET_LOCAL && tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)) { /* Setting remote keys for a client or local keys for a server: we are setting server keys. */ is_client = NX_FALSE; }if ((key_set == NX_SECURE_TLS_KEY_SET_REMOTE && tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_CLIENT) || (key_set == NX_SECURE_TLS_KEY_SET_LOCAL && tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)) { ... } else { /* Local client/local keys or local server/remote keys. */ is_client = NX_TRUE; }else { ... } 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 our session cipher method so we can get key sizes. */ session_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_session_cipher; /* Lookup ciphersuite data for key size. We need 2 keys for each session. */ key_size = session_cipher_method -> nx_crypto_key_size_in_bits >> 3; /* Lookup ciphersuite data for hash size - used for the MAC secret. */ hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size; /* Lookup initialization vector size. */ iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; /* Partition the key block into our keys and secrets following the TLS spec.*/ key_offset = 0; if (((hash_size + key_size + iv_size) << 1) > sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data)) { /* No space for key material data. */ return(NX_SECURE_TLS_CRYPTO_KEYS_TOO_LARGE); }if (((hash_size + key_size + iv_size) << 1) > sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data)) { ... } /* First, the mac secrets. Check for non-zero in the (unlikely) event we are using a NULL hash. */ if (hash_size > 0) { /* Copy new client mac secret over if setting client keys. */ if (is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], hash_size); /* Use case of memcpy is verified. */ }if (is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_mac_secret = &key_block[key_offset]; key_offset += hash_size; /* Copy new server mac secret if setting server keys. */ if (!is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], hash_size); /* Use case of memcpy is verified. */ }if (!is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret = &key_block[key_offset]; key_offset += hash_size; }if (hash_size > 0) { ... } /* Now the keys. Check for non-zero size in the event we are using a NULL cipher (usually for debugging). */ if (key_size > 0) { /* Copy new client session key if setting client keys. */ if (is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], key_size); /* Use case of memcpy is verified. */ }if (is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_key = &key_block[key_offset]; key_offset += key_size; /* Copy new server session key if setting server keys. */ if (!is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], key_size); /* Use case of memcpy is verified. */ }if (!is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_key = &key_block[key_offset]; key_offset += key_size; }if (key_size > 0) { ... } /* Finally, the IVs. Many ciphers don't use IV's so the iv_size is often zero. */ if (iv_size > 0) { /* Copy new client IV if setting client keys. */ if (is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], iv_size); /* Use case of memcpy is verified. */ }if (is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_iv = &key_block[key_offset]; key_offset += iv_size; /* Copy new server IV if setting server keys. */ if (!is_client) { NX_SECURE_MEMCPY(&tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data[key_offset], &tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[key_offset], iv_size); /* Use case of memcpy is verified. */ }if (!is_client) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_iv = &key_block[key_offset]; key_offset += iv_size; }if (iv_size > 0) { ... } /* Initialize the crypto method used in the session cipher. */ if (session_cipher_method -> nx_crypto_init != NULL) { /* Set client write key. */ if (is_client) { if (tls_session -> nx_secure_tls_session_cipher_client_initialized && session_cipher_method -> nx_crypto_cleanup) { status = session_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_session_cipher_metadata_area_client); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_client_initialized = 0; }if (tls_session -> nx_secure_tls_session_cipher_client_initialized && session_cipher_method -> nx_crypto_cleanup) { ... } status = session_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)session_cipher_method, tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_key, session_cipher_method -> nx_crypto_key_size_in_bits, &tls_session -> nx_secure_session_cipher_handler_client, tls_session -> nx_secure_session_cipher_metadata_area_client, tls_session -> nx_secure_session_cipher_metadata_size); tls_session -> nx_secure_tls_session_cipher_client_initialized = 1; }if (is_client) { ... } else { if (tls_session -> nx_secure_tls_session_cipher_server_initialized && session_cipher_method -> nx_crypto_cleanup) { status = session_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_session_cipher_metadata_area_server); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_server_initialized = 0; }if (tls_session -> nx_secure_tls_session_cipher_server_initialized && session_cipher_method -> nx_crypto_cleanup) { ... } /* Set server write key. */ status = session_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)session_cipher_method, tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_key, session_cipher_method -> nx_crypto_key_size_in_bits, &tls_session -> nx_secure_session_cipher_handler_server, tls_session -> nx_secure_session_cipher_metadata_area_server, tls_session -> nx_secure_session_cipher_metadata_size); tls_session -> nx_secure_tls_session_cipher_server_initialized = 1; }else { ... } if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (session_cipher_method -> nx_crypto_init != NULL) { ... } return(NX_SECURE_TLS_SUCCESS); }{ ... }