1
10
13
14
20
21
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
89
90
91
92
95
96
97
98
99
100
101
102
106
107
108
109
114
115
116
117
119
120
121
122
123
124
125
126
127
128
130
131
132
133
134
135
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
221
222
223
224
225
226
227
228
229
230
231
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
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
#if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE)
UCHAR *packet_buffer;
#endif
#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
#ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecdhe_data;
#endif
/* ... */
ciphersuite = tls_session -> nx_secure_tls_session_ciphersuite;
if (ciphersuite == NX_NULL)
{
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (ciphersuite == NX_NULL) { ... }
length = 0;
/* ... */
#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
packet_buffer = send_packet -> nx_packet_append_ptr;
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK)
{
/* ... */
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))
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if ((identity_length + 2u) > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)packet_buffer)) { ... }
packet_buffer[length] = (UCHAR)((identity_length & 0xFF00) >> 8);
packet_buffer[length + 1] = (UCHAR)(identity_length & 0x00FF);
length = (USHORT)(length + 2);
NX_SECURE_MEMCPY(&packet_buffer[length], identity, identity_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
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_ECJPAKE)
{
packet_buffer = send_packet -> nx_packet_append_ptr;
if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(&packet_buffer[length])) < 3)
{
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}if (((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(&packet_buffer[length])) < 3) { ... }
packet_buffer[length] = 3;
length += 1;
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
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
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);
}{ ... }