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_server_key_exchange(NX_SECURE_TLS_SESSION *, NX_PACKET *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_send_server_key_exchange.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_send_server_key_exchange PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function generates a ServerKeyExchange message, which is used */ /* when the chosen ciphersuite requires additional information for key */ /* generation, such as when using Diffie-Hellman ciphers. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* send_packet Packet used to send message */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tlx_ecc_generate_keys Generate keys for ECC exchange*/ /* [nx_crypto_init] Initialize Crypto Method */ /* [nx_crypto_operation] Crypto operation */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_server_handshake DTLS server state machine */ /* _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), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_send_server_key_exchange(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet) { UINT length; const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite; #if defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) UINT status; #endif /* defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) */ #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) UCHAR *packet_buffer; #endif /* defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) */ #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES USHORT identity_length; UCHAR *identity;/* ... */ #endif #if defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) USHORT named_curve; NX_CRYPTO_METHOD *crypto_method; NX_CRYPTO_EXTENDED_OUTPUT extended_output;/* ... */ #endif /* defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) */ #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecdhe_data; #endif /* Build up the server key exchange message. Structure: * | 2 | <key data length> | * | Key data length | Key data (opaque) | *//* ... */ /* Figure out which ciphersuite we are using. */ ciphersuite = tls_session -> nx_secure_tls_session_ciphersuite; if (ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (ciphersuite == NX_NULL) { ... } length = 0; /* In the future, any Diffie-Hellman-based ciphersuites will populate this message with key data. RSA ciphersuites do not use this message. *//* ... */ #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES /* Get a pointer to fill our packet. */ packet_buffer = send_packet -> nx_packet_append_ptr; /* Check for PSK ciphersuites. */ if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK) { /* If PSK is being used, the server sends an identity value to the client so the client can respond with the appropriate PSK. *//* ... */ /* Get identity hint and length. */ identity = tls_session -> nx_secure_tls_credentials.nx_secure_tls_psk_store[0].nx_secure_tls_psk_id_hint; identity_length = (USHORT)tls_session -> nx_secure_tls_credentials.nx_secure_tls_psk_store[0].nx_secure_tls_psk_id_hint_size; if ((identity_length + 2u) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)packet_buffer)) { /* Packet buffer too small. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if ((identity_length + 2u) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)packet_buffer)) { ... } /* Use identity hint length for key data length. */ packet_buffer[length] = (UCHAR)((identity_length & 0xFF00) >> 8); packet_buffer[length + 1] = (UCHAR)(identity_length & 0x00FF); length = (USHORT)(length + 2); /* Extract the identity hint and put it into the packet buffer. */ NX_SECURE_MEMCPY(&packet_buffer[length], identity, identity_length); /* Use case of memcpy is verified. */ /* Advance our total length. */ length = (USHORT)(length + identity_length); }if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK) { ... } /* ... */#endif #ifdef NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE /* Check for ECJ-PAKE ciphersuites. */ if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE) { /* Get a pointer to fill our packet. */ packet_buffer = send_packet -> nx_packet_append_ptr; if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(&packet_buffer[length])) < 3) { /* Packet buffer too small. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(&packet_buffer[length])) < 3) { ... } /* ECCurveType: named_curve (3). */ packet_buffer[length] = 3; length += 1; /* NamedCurve: secp256r1 (23) */ named_curve = (USHORT)NX_CRYPTO_EC_SECP256R1; packet_buffer[length] = (UCHAR)((named_curve >> 8) & 0xFF); packet_buffer[length + 1] = (UCHAR)(named_curve & 0xFF); length += 2; extended_output.nx_crypto_extended_output_data = &packet_buffer[length]; extended_output.nx_crypto_extended_output_length_in_byte = (ULONG)send_packet -> nx_packet_data_end - (ULONG)&packet_buffer[length]; extended_output.nx_crypto_extended_output_actual_size = 0; crypto_method = (NX_CRYPTO_METHOD*)ciphersuite -> nx_secure_tls_public_auth; status = crypto_method -> nx_crypto_operation(NX_CRYPTO_ECJPAKE_SERVER_KEY_EXCHANGE_GENERATE, &tls_session -> nx_secure_public_auth_handler, crypto_method, NX_NULL, 0, NX_NULL, 0, NX_NULL, (UCHAR *)&extended_output, sizeof(extended_output), tls_session -> nx_secure_public_auth_metadata_area, tls_session -> nx_secure_public_auth_metadata_size, NX_NULL, NX_NULL); if (status) { return(status); }if (status) { ... } length += extended_output.nx_crypto_extended_output_actual_size; }if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE) { ... } /* ... */#endif #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE /* Check for ECDHE ciphersuites. */ if (ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE) { ecdhe_data = (NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *)tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data; packet_buffer = send_packet -> nx_packet_append_ptr; length = (UINT)(send_packet->nx_packet_data_end - send_packet->nx_packet_append_ptr); status = _nx_secure_tls_ecc_generate_keys(tls_session, ecdhe_data -> nx_secure_tls_ecdhe_named_curve, NX_TRUE, packet_buffer, &length, ecdhe_data); if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } }if (ciphersuite -> nx_secure_tls_public_cipher -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECDHE) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ /* Finally, we have a complete length and can adjust our packet accordingly. */ 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(NX_SECURE_TLS_SUCCESS); }{ ... }