/src/mbedtls/library/ssl_tls12_client.c
Line | Count | Source |
1 | | /* |
2 | | * TLS client-side functions |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
6 | | */ |
7 | | |
8 | | #include "common.h" |
9 | | |
10 | | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
11 | | |
12 | | #include "mbedtls/platform.h" |
13 | | |
14 | | #include "mbedtls/ssl.h" |
15 | | #include "ssl_client.h" |
16 | | #include "ssl_debug_helpers.h" |
17 | | #include "ssl_misc.h" |
18 | | #include "debug_internal.h" |
19 | | #include "mbedtls/error.h" |
20 | | #include "mbedtls/constant_time.h" |
21 | | |
22 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
23 | | #include "psa_util_internal.h" |
24 | | #include "psa/crypto.h" |
25 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
26 | | /* Define a local translating function to save code size by not using too many |
27 | | * arguments in each translating place. */ |
28 | | static int local_err_translation(psa_status_t status) |
29 | 0 | { |
30 | 0 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
31 | 0 | ARRAY_LENGTH(psa_to_ssl_errors), |
32 | 0 | psa_generic_status_to_mbedtls); |
33 | 0 | } |
34 | 0 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
35 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
36 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
37 | | |
38 | | #include <string.h> |
39 | | |
40 | | #include <stdint.h> |
41 | | |
42 | | #if defined(MBEDTLS_HAVE_TIME) |
43 | | #include "mbedtls/platform_time.h" |
44 | | #endif |
45 | | |
46 | | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
47 | | #include "mbedtls/platform_util.h" |
48 | | #endif |
49 | | |
50 | | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
51 | | MBEDTLS_CHECK_RETURN_CRITICAL |
52 | | static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl, |
53 | | unsigned char *buf, |
54 | | const unsigned char *end, |
55 | | size_t *olen) |
56 | 5.84k | { |
57 | 5.84k | unsigned char *p = buf; |
58 | | |
59 | 5.84k | *olen = 0; |
60 | | |
61 | | /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the |
62 | | * initial ClientHello, in which case also adding the renegotiation |
63 | | * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */ |
64 | 5.84k | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
65 | 5.84k | return 0; |
66 | 5.84k | } |
67 | | |
68 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, |
69 | 0 | ("client hello, adding renegotiation extension")); |
70 | |
|
71 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len); |
72 | | |
73 | | /* |
74 | | * Secure renegotiation |
75 | | */ |
76 | 0 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0); |
77 | 0 | p += 2; |
78 | |
|
79 | 0 | *p++ = 0x00; |
80 | 0 | *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1); |
81 | 0 | *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len); |
82 | |
|
83 | 0 | memcpy(p, ssl->own_verify_data, ssl->verify_data_len); |
84 | |
|
85 | 0 | *olen = 5 + ssl->verify_data_len; |
86 | |
|
87 | 0 | return 0; |
88 | 0 | } |
89 | | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
90 | | |
91 | | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \ |
92 | | defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \ |
93 | | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
94 | | |
95 | | MBEDTLS_CHECK_RETURN_CRITICAL |
96 | | static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl, |
97 | | unsigned char *buf, |
98 | | const unsigned char *end, |
99 | | size_t *olen) |
100 | 5.84k | { |
101 | 5.84k | unsigned char *p = buf; |
102 | 5.84k | (void) ssl; /* ssl used for debugging only */ |
103 | | |
104 | 5.84k | *olen = 0; |
105 | | |
106 | 5.84k | MBEDTLS_SSL_DEBUG_MSG(3, |
107 | 5.84k | ("client hello, adding supported_point_formats extension")); |
108 | 5.84k | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
109 | | |
110 | 5.84k | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0); |
111 | 5.84k | p += 2; |
112 | | |
113 | 5.84k | *p++ = 0x00; |
114 | 5.84k | *p++ = 2; |
115 | | |
116 | 5.84k | *p++ = 1; |
117 | 5.84k | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
118 | | |
119 | 5.84k | *olen = 6; |
120 | | |
121 | 5.84k | return 0; |
122 | 5.84k | } |
123 | | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED || |
124 | | MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED || |
125 | | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
126 | | |
127 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
128 | | MBEDTLS_CHECK_RETURN_CRITICAL |
129 | | static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl, |
130 | | unsigned char *buf, |
131 | | const unsigned char *end, |
132 | | size_t *olen) |
133 | 5.84k | { |
134 | 5.84k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
135 | 5.84k | unsigned char *p = buf; |
136 | 5.84k | size_t kkpp_len = 0; |
137 | | |
138 | 5.84k | *olen = 0; |
139 | | |
140 | | /* Skip costly extension if we can't use EC J-PAKE anyway */ |
141 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
142 | | if (ssl->handshake->psa_pake_ctx_is_ok != 1) { |
143 | | return 0; |
144 | | } |
145 | | #else |
146 | 5.84k | if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) { |
147 | 5.84k | return 0; |
148 | 5.84k | } |
149 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
150 | | |
151 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, |
152 | 0 | ("client hello, adding ecjpake_kkpp extension")); |
153 | |
|
154 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4); |
155 | | |
156 | 0 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0); |
157 | 0 | p += 2; |
158 | | |
159 | | /* |
160 | | * We may need to send ClientHello multiple times for Hello verification. |
161 | | * We don't want to compute fresh values every time (both for performance |
162 | | * and consistency reasons), so cache the extension content. |
163 | | */ |
164 | 0 | if (ssl->handshake->ecjpake_cache == NULL || |
165 | 0 | ssl->handshake->ecjpake_cache_len == 0) { |
166 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters")); |
167 | |
|
168 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
169 | | ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx, |
170 | | p + 2, end - p - 2, &kkpp_len, |
171 | | MBEDTLS_ECJPAKE_ROUND_ONE); |
172 | | if (ret != 0) { |
173 | | psa_destroy_key(ssl->handshake->psa_pake_password); |
174 | | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
175 | | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret); |
176 | | return ret; |
177 | | } |
178 | | #else |
179 | 0 | ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx, |
180 | 0 | p + 2, end - p - 2, &kkpp_len, |
181 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); |
182 | 0 | if (ret != 0) { |
183 | 0 | MBEDTLS_SSL_DEBUG_RET(1, |
184 | 0 | "mbedtls_ecjpake_write_round_one", ret); |
185 | 0 | return ret; |
186 | 0 | } |
187 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
188 | | |
189 | 0 | ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len); |
190 | 0 | if (ssl->handshake->ecjpake_cache == NULL) { |
191 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed")); |
192 | 0 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
193 | 0 | } |
194 | | |
195 | 0 | memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len); |
196 | 0 | ssl->handshake->ecjpake_cache_len = kkpp_len; |
197 | 0 | } else { |
198 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters")); |
199 | |
|
200 | 0 | kkpp_len = ssl->handshake->ecjpake_cache_len; |
201 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len); |
202 | | |
203 | 0 | memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len); |
204 | 0 | } |
205 | | |
206 | 0 | MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0); |
207 | 0 | p += 2; |
208 | |
|
209 | 0 | *olen = kkpp_len + 4; |
210 | |
|
211 | 0 | return 0; |
212 | 0 | } |
213 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
214 | | |
215 | | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
216 | | MBEDTLS_CHECK_RETURN_CRITICAL |
217 | | static int ssl_write_cid_ext(mbedtls_ssl_context *ssl, |
218 | | unsigned char *buf, |
219 | | const unsigned char *end, |
220 | | size_t *olen) |
221 | 5.84k | { |
222 | 5.84k | unsigned char *p = buf; |
223 | 5.84k | size_t ext_len; |
224 | | |
225 | | /* |
226 | | * struct { |
227 | | * opaque cid<0..2^8-1>; |
228 | | * } ConnectionId; |
229 | | */ |
230 | | |
231 | 5.84k | *olen = 0; |
232 | 5.84k | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
233 | 5.84k | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
234 | 5.84k | return 0; |
235 | 5.84k | } |
236 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension")); |
237 | | |
238 | | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
239 | | * which is at most 255, so the increment cannot overflow. */ |
240 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5)); |
241 | | |
242 | | /* Add extension ID + size */ |
243 | 0 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0); |
244 | 0 | p += 2; |
245 | 0 | ext_len = (size_t) ssl->own_cid_len + 1; |
246 | 0 | MBEDTLS_PUT_UINT16_BE(ext_len, p, 0); |
247 | 0 | p += 2; |
248 | |
|
249 | 0 | *p++ = (uint8_t) ssl->own_cid_len; |
250 | 0 | memcpy(p, ssl->own_cid, ssl->own_cid_len); |
251 | |
|
252 | 0 | *olen = ssl->own_cid_len + 5; |
253 | |
|
254 | 0 | return 0; |
255 | 0 | } |
256 | | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
257 | | |
258 | | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
259 | | MBEDTLS_CHECK_RETURN_CRITICAL |
260 | | static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl, |
261 | | unsigned char *buf, |
262 | | const unsigned char *end, |
263 | | size_t *olen) |
264 | 5.84k | { |
265 | 5.84k | unsigned char *p = buf; |
266 | | |
267 | 5.84k | *olen = 0; |
268 | | |
269 | 5.84k | if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) { |
270 | 5.84k | return 0; |
271 | 5.84k | } |
272 | | |
273 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, |
274 | 0 | ("client hello, adding max_fragment_length extension")); |
275 | |
|
276 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5); |
277 | | |
278 | 0 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0); |
279 | 0 | p += 2; |
280 | |
|
281 | 0 | *p++ = 0x00; |
282 | 0 | *p++ = 1; |
283 | |
|
284 | 0 | *p++ = ssl->conf->mfl_code; |
285 | |
|
286 | 0 | *olen = 5; |
287 | |
|
288 | 0 | return 0; |
289 | 0 | } |
290 | | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
291 | | |
292 | | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
293 | | MBEDTLS_CHECK_RETURN_CRITICAL |
294 | | static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl, |
295 | | unsigned char *buf, |
296 | | const unsigned char *end, |
297 | | size_t *olen) |
298 | 5.84k | { |
299 | 5.84k | unsigned char *p = buf; |
300 | | |
301 | 5.84k | *olen = 0; |
302 | | |
303 | 5.84k | if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) { |
304 | 0 | return 0; |
305 | 0 | } |
306 | | |
307 | 5.84k | MBEDTLS_SSL_DEBUG_MSG(3, |
308 | 5.84k | ("client hello, adding encrypt_then_mac extension")); |
309 | | |
310 | 5.84k | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4); |
311 | | |
312 | 5.84k | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0); |
313 | 5.84k | p += 2; |
314 | | |
315 | 5.84k | *p++ = 0x00; |
316 | 5.84k | *p++ = 0x00; |
317 | | |
318 | 5.84k | *olen = 4; |
319 | | |
320 | 5.84k | return 0; |
321 | 5.84k | } |
322 | | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
323 | | |
324 | | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
325 | | MBEDTLS_CHECK_RETURN_CRITICAL |
326 | | static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl, |
327 | | unsigned char *buf, |
328 | | const unsigned char *end, |
329 | | size_t *olen) |
330 | 5.84k | { |
331 | 5.84k | unsigned char *p = buf; |
332 | | |
333 | 5.84k | *olen = 0; |
334 | | |
335 | 5.84k | if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) { |
336 | 0 | return 0; |
337 | 0 | } |
338 | | |
339 | 5.84k | MBEDTLS_SSL_DEBUG_MSG(3, |
340 | 5.84k | ("client hello, adding extended_master_secret extension")); |
341 | | |
342 | 5.84k | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4); |
343 | | |
344 | 5.84k | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0); |
345 | 5.84k | p += 2; |
346 | | |
347 | 5.84k | *p++ = 0x00; |
348 | 5.84k | *p++ = 0x00; |
349 | | |
350 | 5.84k | *olen = 4; |
351 | | |
352 | 5.84k | return 0; |
353 | 5.84k | } |
354 | | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
355 | | |
356 | | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
357 | | MBEDTLS_CHECK_RETURN_CRITICAL |
358 | | static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl, |
359 | | unsigned char *buf, |
360 | | const unsigned char *end, |
361 | | size_t *olen) |
362 | 5.84k | { |
363 | 5.84k | unsigned char *p = buf; |
364 | 5.84k | size_t tlen = ssl->session_negotiate->ticket_len; |
365 | | |
366 | 5.84k | *olen = 0; |
367 | | |
368 | 5.84k | if (mbedtls_ssl_conf_get_session_tickets(ssl->conf) == |
369 | 5.84k | MBEDTLS_SSL_SESSION_TICKETS_DISABLED) { |
370 | 0 | return 0; |
371 | 0 | } |
372 | | |
373 | 5.84k | MBEDTLS_SSL_DEBUG_MSG(3, |
374 | 5.84k | ("client hello, adding session ticket extension")); |
375 | | |
376 | | /* The addition is safe here since the ticket length is 16 bit. */ |
377 | 5.84k | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen); |
378 | | |
379 | 5.84k | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0); |
380 | 5.84k | p += 2; |
381 | | |
382 | 5.84k | MBEDTLS_PUT_UINT16_BE(tlen, p, 0); |
383 | 5.84k | p += 2; |
384 | | |
385 | 5.84k | *olen = 4; |
386 | | |
387 | 5.84k | if (ssl->session_negotiate->ticket == NULL || tlen == 0) { |
388 | 5.84k | return 0; |
389 | 5.84k | } |
390 | | |
391 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, |
392 | 0 | ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen)); |
393 | |
|
394 | 0 | memcpy(p, ssl->session_negotiate->ticket, tlen); |
395 | |
|
396 | 0 | *olen += tlen; |
397 | |
|
398 | 0 | return 0; |
399 | 5.84k | } |
400 | | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
401 | | |
402 | | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
403 | | MBEDTLS_CHECK_RETURN_CRITICAL |
404 | | static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl, |
405 | | unsigned char *buf, |
406 | | const unsigned char *end, |
407 | | size_t *olen) |
408 | 5.84k | { |
409 | 5.84k | unsigned char *p = buf; |
410 | 5.84k | size_t protection_profiles_index = 0, ext_len = 0; |
411 | 5.84k | uint16_t mki_len = 0, profile_value = 0; |
412 | | |
413 | 5.84k | *olen = 0; |
414 | | |
415 | 5.84k | if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) || |
416 | 5.84k | (ssl->conf->dtls_srtp_profile_list == NULL) || |
417 | 5.84k | (ssl->conf->dtls_srtp_profile_list_len == 0)) { |
418 | 5.84k | return 0; |
419 | 5.84k | } |
420 | | |
421 | | /* RFC 5764 section 4.1.1 |
422 | | * uint8 SRTPProtectionProfile[2]; |
423 | | * |
424 | | * struct { |
425 | | * SRTPProtectionProfiles SRTPProtectionProfiles; |
426 | | * opaque srtp_mki<0..255>; |
427 | | * } UseSRTPData; |
428 | | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; |
429 | | */ |
430 | 0 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) { |
431 | 0 | mki_len = ssl->dtls_srtp_info.mki_len; |
432 | 0 | } |
433 | | /* Extension length = 2 bytes for profiles length, |
434 | | * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ), |
435 | | * 1 byte for srtp_mki vector length and the mki_len value |
436 | | */ |
437 | 0 | ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len; |
438 | |
|
439 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension")); |
440 | | |
441 | | /* Check there is room in the buffer for the extension + 4 bytes |
442 | | * - the extension tag (2 bytes) |
443 | | * - the extension length (2 bytes) |
444 | | */ |
445 | 0 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4); |
446 | | |
447 | 0 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0); |
448 | 0 | p += 2; |
449 | |
|
450 | 0 | MBEDTLS_PUT_UINT16_BE(ext_len, p, 0); |
451 | 0 | p += 2; |
452 | | |
453 | | /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ |
454 | | /* micro-optimization: |
455 | | * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH |
456 | | * which is lower than 127, so the upper byte of the length is always 0 |
457 | | * For the documentation, the more generic code is left in comments |
458 | | * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len ) |
459 | | * >> 8 ) & 0xFF ); |
460 | | */ |
461 | 0 | *p++ = 0; |
462 | 0 | *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len); |
463 | |
|
464 | 0 | for (protection_profiles_index = 0; |
465 | 0 | protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; |
466 | 0 | protection_profiles_index++) { |
467 | 0 | profile_value = mbedtls_ssl_check_srtp_profile_value |
468 | 0 | (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]); |
469 | 0 | if (profile_value != MBEDTLS_TLS_SRTP_UNSET) { |
470 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x", |
471 | 0 | profile_value)); |
472 | 0 | MBEDTLS_PUT_UINT16_BE(profile_value, p, 0); |
473 | 0 | p += 2; |
474 | 0 | } else { |
475 | | /* |
476 | | * Note: we shall never arrive here as protection profiles |
477 | | * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function |
478 | | */ |
479 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, |
480 | 0 | ("client hello, " |
481 | 0 | "illegal DTLS-SRTP protection profile %d", |
482 | 0 | ssl->conf->dtls_srtp_profile_list[protection_profiles_index] |
483 | 0 | )); |
484 | 0 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | 0 | *p++ = mki_len & 0xFF; |
489 | |
|
490 | 0 | if (mki_len != 0) { |
491 | 0 | memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len); |
492 | | /* |
493 | | * Increment p to point to the current position. |
494 | | */ |
495 | 0 | p += mki_len; |
496 | 0 | MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value, |
497 | 0 | ssl->dtls_srtp_info.mki_len); |
498 | 0 | } |
499 | | |
500 | | /* |
501 | | * total extension length: extension type (2 bytes) |
502 | | * + extension length (2 bytes) |
503 | | * + protection profile length (2 bytes) |
504 | | * + 2 * number of protection profiles |
505 | | * + srtp_mki vector length(1 byte) |
506 | | * + mki value |
507 | | */ |
508 | 0 | *olen = p - buf; |
509 | |
|
510 | 0 | return 0; |
511 | 0 | } |
512 | | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
513 | | |
514 | | int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl, |
515 | | unsigned char *buf, |
516 | | const unsigned char *end, |
517 | | int uses_ec, |
518 | | size_t *out_len) |
519 | 5.84k | { |
520 | 5.84k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
521 | 5.84k | unsigned char *p = buf; |
522 | 5.84k | size_t ext_len = 0; |
523 | | |
524 | 5.84k | (void) ssl; |
525 | 5.84k | (void) end; |
526 | 5.84k | (void) uses_ec; |
527 | 5.84k | (void) ret; |
528 | 5.84k | (void) ext_len; |
529 | | |
530 | 5.84k | *out_len = 0; |
531 | | |
532 | | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added |
533 | | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ |
534 | 5.84k | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
535 | 5.84k | if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) { |
536 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret); |
537 | 0 | return ret; |
538 | 0 | } |
539 | 5.84k | p += ext_len; |
540 | 5.84k | #endif |
541 | | |
542 | 5.84k | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \ |
543 | 5.84k | defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \ |
544 | 5.84k | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
545 | 5.84k | if (uses_ec) { |
546 | 5.84k | if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end, |
547 | 5.84k | &ext_len)) != 0) { |
548 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret); |
549 | 0 | return ret; |
550 | 0 | } |
551 | 5.84k | p += ext_len; |
552 | 5.84k | } |
553 | 5.84k | #endif |
554 | | |
555 | 5.84k | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
556 | 5.84k | if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) { |
557 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret); |
558 | 0 | return ret; |
559 | 0 | } |
560 | 5.84k | p += ext_len; |
561 | 5.84k | #endif |
562 | | |
563 | 5.84k | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
564 | 5.84k | if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) { |
565 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret); |
566 | 0 | return ret; |
567 | 0 | } |
568 | 5.84k | p += ext_len; |
569 | 5.84k | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
570 | | |
571 | 5.84k | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
572 | 5.84k | if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end, |
573 | 5.84k | &ext_len)) != 0) { |
574 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret); |
575 | 0 | return ret; |
576 | 0 | } |
577 | 5.84k | p += ext_len; |
578 | 5.84k | #endif |
579 | | |
580 | 5.84k | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
581 | 5.84k | if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) { |
582 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret); |
583 | 0 | return ret; |
584 | 0 | } |
585 | 5.84k | p += ext_len; |
586 | 5.84k | #endif |
587 | | |
588 | 5.84k | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
589 | 5.84k | if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) { |
590 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret); |
591 | 0 | return ret; |
592 | 0 | } |
593 | 5.84k | p += ext_len; |
594 | 5.84k | #endif |
595 | | |
596 | 5.84k | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
597 | 5.84k | if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) { |
598 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret); |
599 | 0 | return ret; |
600 | 0 | } |
601 | 5.84k | p += ext_len; |
602 | 5.84k | #endif |
603 | | |
604 | 5.84k | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
605 | 5.84k | if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) { |
606 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret); |
607 | 0 | return ret; |
608 | 0 | } |
609 | 5.84k | p += ext_len; |
610 | 5.84k | #endif |
611 | | |
612 | 5.84k | *out_len = (size_t) (p - buf); |
613 | | |
614 | 5.84k | return 0; |
615 | 5.84k | } |
616 | | |
617 | | MBEDTLS_CHECK_RETURN_CRITICAL |
618 | | static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl, |
619 | | const unsigned char *buf, |
620 | | size_t len) |
621 | 1.43k | { |
622 | 1.43k | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
623 | 1.43k | if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) { |
624 | | /* Check verify-data in constant-time. The length OTOH is no secret */ |
625 | 0 | if (len != 1 + ssl->verify_data_len * 2 || |
626 | 0 | buf[0] != ssl->verify_data_len * 2 || |
627 | 0 | mbedtls_ct_memcmp(buf + 1, |
628 | 0 | ssl->own_verify_data, ssl->verify_data_len) != 0 || |
629 | 0 | mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len, |
630 | 0 | ssl->peer_verify_data, ssl->verify_data_len) != 0) { |
631 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info")); |
632 | 0 | mbedtls_ssl_send_alert_message( |
633 | 0 | ssl, |
634 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
635 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
636 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
637 | 0 | } |
638 | 0 | } else |
639 | 1.43k | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
640 | 1.43k | { |
641 | 1.43k | if (len != 1 || buf[0] != 0x00) { |
642 | 10 | MBEDTLS_SSL_DEBUG_MSG(1, |
643 | 10 | ("non-zero length renegotiation info")); |
644 | 10 | mbedtls_ssl_send_alert_message( |
645 | 10 | ssl, |
646 | 10 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
647 | 10 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
648 | 10 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
649 | 10 | } |
650 | | |
651 | 1.42k | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
652 | 1.42k | } |
653 | | |
654 | 1.42k | return 0; |
655 | 1.43k | } |
656 | | |
657 | | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
658 | | MBEDTLS_CHECK_RETURN_CRITICAL |
659 | | static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl, |
660 | | const unsigned char *buf, |
661 | | size_t len) |
662 | 5 | { |
663 | | /* |
664 | | * server should use the extension only if we did, |
665 | | * and if so the server's value should match ours (and len is always 1) |
666 | | */ |
667 | 5 | if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE || |
668 | 0 | len != 1 || |
669 | 5 | buf[0] != ssl->conf->mfl_code) { |
670 | 5 | MBEDTLS_SSL_DEBUG_MSG(1, |
671 | 5 | ("non-matching max fragment length extension")); |
672 | 5 | mbedtls_ssl_send_alert_message( |
673 | 5 | ssl, |
674 | 5 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
675 | 5 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
676 | 5 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
677 | 5 | } |
678 | | |
679 | 0 | return 0; |
680 | 5 | } |
681 | | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
682 | | |
683 | | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
684 | | MBEDTLS_CHECK_RETURN_CRITICAL |
685 | | static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl, |
686 | | const unsigned char *buf, |
687 | | size_t len) |
688 | 1 | { |
689 | 1 | size_t peer_cid_len; |
690 | | |
691 | 1 | if ( /* CID extension only makes sense in DTLS */ |
692 | 1 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
693 | | /* The server must only send the CID extension if we have offered it. */ |
694 | 1 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
695 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected")); |
696 | 1 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
697 | 1 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT); |
698 | 1 | return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION; |
699 | 1 | } |
700 | | |
701 | 0 | if (len == 0) { |
702 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid")); |
703 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
704 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
705 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
706 | 0 | } |
707 | | |
708 | 0 | peer_cid_len = *buf++; |
709 | 0 | len--; |
710 | |
|
711 | 0 | if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) { |
712 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid")); |
713 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
714 | 0 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
715 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
716 | 0 | } |
717 | | |
718 | 0 | if (len != peer_cid_len) { |
719 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid")); |
720 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
721 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
722 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
723 | 0 | } |
724 | | |
725 | 0 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
726 | 0 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
727 | 0 | memcpy(ssl->handshake->peer_cid, buf, peer_cid_len); |
728 | |
|
729 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated")); |
730 | 0 | MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len); |
731 | |
|
732 | 0 | return 0; |
733 | 0 | } |
734 | | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
735 | | |
736 | | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
737 | | MBEDTLS_CHECK_RETURN_CRITICAL |
738 | | static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl, |
739 | | const unsigned char *buf, |
740 | | size_t len) |
741 | 439 | { |
742 | 439 | if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
743 | 439 | len != 0) { |
744 | 4 | MBEDTLS_SSL_DEBUG_MSG(1, |
745 | 4 | ("non-matching encrypt-then-MAC extension")); |
746 | 4 | mbedtls_ssl_send_alert_message( |
747 | 4 | ssl, |
748 | 4 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
749 | 4 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT); |
750 | 4 | return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION; |
751 | 4 | } |
752 | | |
753 | 435 | ((void) buf); |
754 | | |
755 | 435 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
756 | | |
757 | 435 | return 0; |
758 | 439 | } |
759 | | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
760 | | |
761 | | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
762 | | MBEDTLS_CHECK_RETURN_CRITICAL |
763 | | static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl, |
764 | | const unsigned char *buf, |
765 | | size_t len) |
766 | 782 | { |
767 | 782 | if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
768 | 782 | len != 0) { |
769 | 4 | MBEDTLS_SSL_DEBUG_MSG(1, |
770 | 4 | ("non-matching extended master secret extension")); |
771 | 4 | mbedtls_ssl_send_alert_message( |
772 | 4 | ssl, |
773 | 4 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
774 | 4 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT); |
775 | 4 | return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION; |
776 | 4 | } |
777 | | |
778 | 778 | ((void) buf); |
779 | | |
780 | 778 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
781 | | |
782 | 778 | return 0; |
783 | 782 | } |
784 | | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
785 | | |
786 | | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
787 | | MBEDTLS_CHECK_RETURN_CRITICAL |
788 | | static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl, |
789 | | const unsigned char *buf, |
790 | | size_t len) |
791 | 568 | { |
792 | 568 | if ((mbedtls_ssl_conf_get_session_tickets(ssl->conf) == |
793 | 568 | MBEDTLS_SSL_SESSION_TICKETS_DISABLED) || |
794 | 568 | len != 0) { |
795 | 3 | MBEDTLS_SSL_DEBUG_MSG(1, |
796 | 3 | ("non-matching session ticket extension")); |
797 | 3 | mbedtls_ssl_send_alert_message( |
798 | 3 | ssl, |
799 | 3 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
800 | 3 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT); |
801 | 3 | return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION; |
802 | 3 | } |
803 | | |
804 | 565 | ((void) buf); |
805 | | |
806 | 565 | ssl->handshake->new_session_ticket = 1; |
807 | | |
808 | 565 | return 0; |
809 | 568 | } |
810 | | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
811 | | |
812 | | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \ |
813 | | defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \ |
814 | | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
815 | | MBEDTLS_CHECK_RETURN_CRITICAL |
816 | | static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl, |
817 | | const unsigned char *buf, |
818 | | size_t len) |
819 | 891 | { |
820 | 891 | size_t list_size; |
821 | 891 | const unsigned char *p; |
822 | | |
823 | 891 | if (len == 0 || (size_t) (buf[0] + 1) != len) { |
824 | 16 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
825 | 16 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
826 | 16 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
827 | 16 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
828 | 16 | } |
829 | 875 | list_size = buf[0]; |
830 | | |
831 | 875 | p = buf + 1; |
832 | 1.24k | while (list_size > 0) { |
833 | 1.23k | if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
834 | 870 | p[0] == MBEDTLS_ECP_PF_COMPRESSED) { |
835 | 870 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
836 | 870 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
837 | 870 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
838 | 870 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */ |
839 | 870 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
840 | 870 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
841 | 870 | mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx, |
842 | 870 | p[0]); |
843 | 870 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
844 | 870 | MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0])); |
845 | 870 | return 0; |
846 | 870 | } |
847 | | |
848 | 368 | list_size--; |
849 | 368 | p++; |
850 | 368 | } |
851 | | |
852 | 5 | MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common")); |
853 | 5 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
854 | 5 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
855 | 5 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
856 | 875 | } |
857 | | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED || |
858 | | MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED || |
859 | | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
860 | | |
861 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
862 | | MBEDTLS_CHECK_RETURN_CRITICAL |
863 | | static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl, |
864 | | const unsigned char *buf, |
865 | | size_t len) |
866 | 257 | { |
867 | 257 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
868 | | |
869 | 257 | if (ssl->handshake->ciphersuite_info->key_exchange != |
870 | 257 | MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
871 | 257 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension")); |
872 | 257 | return 0; |
873 | 257 | } |
874 | | |
875 | | /* If we got here, we no longer need our cached extension */ |
876 | 0 | mbedtls_free(ssl->handshake->ecjpake_cache); |
877 | 0 | ssl->handshake->ecjpake_cache = NULL; |
878 | 0 | ssl->handshake->ecjpake_cache_len = 0; |
879 | |
|
880 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
881 | | if ((ret = mbedtls_psa_ecjpake_read_round( |
882 | | &ssl->handshake->psa_pake_ctx, buf, len, |
883 | | MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) { |
884 | | psa_destroy_key(ssl->handshake->psa_pake_password); |
885 | | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
886 | | |
887 | | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret); |
888 | | mbedtls_ssl_send_alert_message( |
889 | | ssl, |
890 | | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
891 | | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
892 | | return ret; |
893 | | } |
894 | | |
895 | | return 0; |
896 | | #else |
897 | 0 | if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx, |
898 | 0 | buf, len)) != 0) { |
899 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret); |
900 | 0 | mbedtls_ssl_send_alert_message( |
901 | 0 | ssl, |
902 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
903 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
904 | 0 | return ret; |
905 | 0 | } |
906 | | |
907 | 0 | return 0; |
908 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
909 | 0 | } |
910 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
911 | | |
912 | | #if defined(MBEDTLS_SSL_ALPN) |
913 | | MBEDTLS_CHECK_RETURN_CRITICAL |
914 | | static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
915 | | const unsigned char *buf, size_t len) |
916 | 4 | { |
917 | 4 | size_t list_len, name_len; |
918 | 4 | const char **p; |
919 | | |
920 | | /* If we didn't send it, the server shouldn't send it */ |
921 | 4 | if (ssl->conf->alpn_list == NULL) { |
922 | 4 | MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension")); |
923 | 4 | mbedtls_ssl_send_alert_message( |
924 | 4 | ssl, |
925 | 4 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
926 | 4 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT); |
927 | 4 | return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION; |
928 | 4 | } |
929 | | |
930 | | /* |
931 | | * opaque ProtocolName<1..2^8-1>; |
932 | | * |
933 | | * struct { |
934 | | * ProtocolName protocol_name_list<2..2^16-1> |
935 | | * } ProtocolNameList; |
936 | | * |
937 | | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" |
938 | | */ |
939 | | |
940 | | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
941 | 0 | if (len < 4) { |
942 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
943 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
944 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
945 | 0 | } |
946 | | |
947 | 0 | list_len = MBEDTLS_GET_UINT16_BE(buf, 0); |
948 | 0 | if (list_len != len - 2) { |
949 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
950 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
951 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
952 | 0 | } |
953 | | |
954 | 0 | name_len = buf[2]; |
955 | 0 | if (name_len != list_len - 1) { |
956 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
957 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
958 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
959 | 0 | } |
960 | | |
961 | | /* Check that the server chosen protocol was in our list and save it */ |
962 | 0 | for (p = ssl->conf->alpn_list; *p != NULL; p++) { |
963 | 0 | if (name_len == strlen(*p) && |
964 | 0 | memcmp(buf + 3, *p, name_len) == 0) { |
965 | 0 | ssl->alpn_chosen = *p; |
966 | 0 | return 0; |
967 | 0 | } |
968 | 0 | } |
969 | | |
970 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol")); |
971 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
972 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
973 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
974 | 0 | } |
975 | | #endif /* MBEDTLS_SSL_ALPN */ |
976 | | |
977 | | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
978 | | MBEDTLS_CHECK_RETURN_CRITICAL |
979 | | static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl, |
980 | | const unsigned char *buf, |
981 | | size_t len) |
982 | 106 | { |
983 | 106 | mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET; |
984 | 106 | size_t i, mki_len = 0; |
985 | 106 | uint16_t server_protection_profile_value = 0; |
986 | | |
987 | | /* If use_srtp is not configured, just ignore the extension */ |
988 | 106 | if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) || |
989 | 106 | (ssl->conf->dtls_srtp_profile_list == NULL) || |
990 | 106 | (ssl->conf->dtls_srtp_profile_list_len == 0)) { |
991 | 106 | return 0; |
992 | 106 | } |
993 | | |
994 | | /* RFC 5764 section 4.1.1 |
995 | | * uint8 SRTPProtectionProfile[2]; |
996 | | * |
997 | | * struct { |
998 | | * SRTPProtectionProfiles SRTPProtectionProfiles; |
999 | | * opaque srtp_mki<0..255>; |
1000 | | * } UseSRTPData; |
1001 | | |
1002 | | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; |
1003 | | * |
1004 | | */ |
1005 | 0 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) { |
1006 | 0 | mki_len = ssl->dtls_srtp_info.mki_len; |
1007 | 0 | } |
1008 | | |
1009 | | /* |
1010 | | * Length is 5 + optional mki_value : one protection profile length (2 bytes) |
1011 | | * + protection profile (2 bytes) |
1012 | | * + mki_len(1 byte) |
1013 | | * and optional srtp_mki |
1014 | | */ |
1015 | 0 | if ((len < 5) || (len != (buf[4] + 5u))) { |
1016 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1017 | 0 | } |
1018 | | |
1019 | | /* |
1020 | | * get the server protection profile |
1021 | | */ |
1022 | | |
1023 | | /* |
1024 | | * protection profile length must be 0x0002 as we must have only |
1025 | | * one protection profile in server Hello |
1026 | | */ |
1027 | 0 | if ((buf[0] != 0) || (buf[1] != 2)) { |
1028 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1029 | 0 | } |
1030 | | |
1031 | 0 | server_protection_profile_value = (buf[2] << 8) | buf[3]; |
1032 | 0 | server_protection = mbedtls_ssl_check_srtp_profile_value( |
1033 | 0 | server_protection_profile_value); |
1034 | 0 | if (server_protection != MBEDTLS_TLS_SRTP_UNSET) { |
1035 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s", |
1036 | 0 | mbedtls_ssl_get_srtp_profile_as_string( |
1037 | 0 | server_protection))); |
1038 | 0 | } |
1039 | |
|
1040 | 0 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; |
1041 | | |
1042 | | /* |
1043 | | * Check we have the server profile in our list |
1044 | | */ |
1045 | 0 | for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) { |
1046 | 0 | if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) { |
1047 | 0 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; |
1048 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s", |
1049 | 0 | mbedtls_ssl_get_srtp_profile_as_string( |
1050 | 0 | server_protection))); |
1051 | 0 | break; |
1052 | 0 | } |
1053 | 0 | } |
1054 | | |
1055 | | /* If no match was found : server problem, it shall never answer with incompatible profile */ |
1056 | 0 | if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
1057 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1058 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
1059 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1060 | 0 | } |
1061 | | |
1062 | | /* If server does not use mki in its reply, make sure the client won't keep |
1063 | | * one as negotiated */ |
1064 | 0 | if (len == 5) { |
1065 | 0 | ssl->dtls_srtp_info.mki_len = 0; |
1066 | 0 | } |
1067 | | |
1068 | | /* |
1069 | | * RFC5764: |
1070 | | * If the client detects a nonzero-length MKI in the server's response |
1071 | | * that is different than the one the client offered, then the client |
1072 | | * MUST abort the handshake and SHOULD send an invalid_parameter alert. |
1073 | | */ |
1074 | 0 | if (len > 5 && (buf[4] != mki_len || |
1075 | 0 | (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) { |
1076 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1077 | 0 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
1078 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
1079 | 0 | } |
1080 | 0 | #if defined(MBEDTLS_DEBUG_C) |
1081 | 0 | if (len > 5) { |
1082 | 0 | MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value, |
1083 | 0 | ssl->dtls_srtp_info.mki_len); |
1084 | 0 | } |
1085 | 0 | #endif |
1086 | 0 | return 0; |
1087 | 0 | } |
1088 | | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
1089 | | |
1090 | | /* |
1091 | | * Parse HelloVerifyRequest. Only called after verifying the HS type. |
1092 | | */ |
1093 | | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
1094 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1095 | | static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl) |
1096 | 592 | { |
1097 | 592 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
1098 | 592 | const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); |
1099 | 592 | uint16_t dtls_legacy_version; |
1100 | | |
1101 | | #if !defined(MBEDTLS_SSL_PROTO_TLS1_3) |
1102 | | uint8_t cookie_len; |
1103 | | #else |
1104 | 592 | uint16_t cookie_len; |
1105 | 592 | #endif |
1106 | | |
1107 | 592 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request")); |
1108 | | |
1109 | | /* Check that there is enough room for: |
1110 | | * - 2 bytes of version |
1111 | | * - 1 byte of cookie_len |
1112 | | */ |
1113 | 592 | if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) { |
1114 | 7 | MBEDTLS_SSL_DEBUG_MSG(1, |
1115 | 7 | ("incoming HelloVerifyRequest message is too short")); |
1116 | 7 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1117 | 7 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1118 | 7 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1119 | 7 | } |
1120 | | |
1121 | | /* |
1122 | | * struct { |
1123 | | * ProtocolVersion server_version; |
1124 | | * opaque cookie<0..2^8-1>; |
1125 | | * } HelloVerifyRequest; |
1126 | | */ |
1127 | 585 | MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2); |
1128 | 585 | dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0); |
1129 | 585 | p += 2; |
1130 | | |
1131 | | /* |
1132 | | * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff) |
1133 | | * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to |
1134 | | * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2) |
1135 | | */ |
1136 | 585 | if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) { |
1137 | 41 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version")); |
1138 | | |
1139 | 41 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1140 | 41 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION); |
1141 | | |
1142 | 41 | return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
1143 | 41 | } |
1144 | | |
1145 | 544 | cookie_len = *p++; |
1146 | 544 | if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) { |
1147 | 2 | MBEDTLS_SSL_DEBUG_MSG(1, |
1148 | 2 | ("cookie length does not match incoming message size")); |
1149 | 2 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1150 | 2 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1151 | 2 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1152 | 2 | } |
1153 | 542 | MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len); |
1154 | | |
1155 | 542 | mbedtls_free(ssl->handshake->cookie); |
1156 | | |
1157 | 542 | ssl->handshake->cookie = mbedtls_calloc(1, cookie_len); |
1158 | 542 | if (ssl->handshake->cookie == NULL) { |
1159 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len)); |
1160 | 0 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
1161 | 0 | } |
1162 | | |
1163 | 542 | memcpy(ssl->handshake->cookie, p, cookie_len); |
1164 | 542 | ssl->handshake->cookie_len = cookie_len; |
1165 | | |
1166 | | /* Start over at ClientHello */ |
1167 | 542 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); |
1168 | 542 | ret = mbedtls_ssl_reset_checksum(ssl); |
1169 | 542 | if (0 != ret) { |
1170 | 0 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret); |
1171 | 0 | return ret; |
1172 | 0 | } |
1173 | | |
1174 | 542 | mbedtls_ssl_recv_flight_completed(ssl); |
1175 | | |
1176 | 542 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request")); |
1177 | | |
1178 | 542 | return 0; |
1179 | 542 | } |
1180 | | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
1181 | | |
1182 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1183 | | static int ssl_parse_server_hello(mbedtls_ssl_context *ssl) |
1184 | 5.84k | { |
1185 | 5.84k | int ret, i; |
1186 | 5.84k | size_t n; |
1187 | 5.84k | size_t ext_len; |
1188 | 5.84k | unsigned char *buf, *ext; |
1189 | 5.84k | unsigned char comp; |
1190 | 5.84k | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1191 | 5.84k | int renegotiation_info_seen = 0; |
1192 | 5.84k | #endif |
1193 | 5.84k | int handshake_failure = 0; |
1194 | 5.84k | const mbedtls_ssl_ciphersuite_t *suite_info; |
1195 | | |
1196 | 5.84k | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello")); |
1197 | | |
1198 | 5.84k | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
1199 | | /* No alert on a read error. */ |
1200 | 1.11k | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
1201 | 1.11k | return ret; |
1202 | 1.11k | } |
1203 | | |
1204 | 4.73k | buf = ssl->in_msg; |
1205 | | |
1206 | 4.73k | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
1207 | 1 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1208 | 1 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
1209 | 0 | ssl->renego_records_seen++; |
1210 | |
|
1211 | 0 | if (ssl->conf->renego_max_records >= 0 && |
1212 | 0 | ssl->renego_records_seen > ssl->conf->renego_max_records) { |
1213 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1214 | 0 | ("renegotiation requested, but not honored by server")); |
1215 | 0 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
1216 | 0 | } |
1217 | | |
1218 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1219 | 0 | ("non-handshake message during renegotiation")); |
1220 | |
|
1221 | 0 | ssl->keep_current_message = 1; |
1222 | 0 | return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO; |
1223 | 0 | } |
1224 | 1 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
1225 | | |
1226 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1227 | 1 | mbedtls_ssl_send_alert_message( |
1228 | 1 | ssl, |
1229 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1230 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
1231 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
1232 | 1 | } |
1233 | | |
1234 | 4.72k | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
1235 | 4.72k | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
1236 | 4.72k | if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { |
1237 | 592 | MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request")); |
1238 | 592 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello")); |
1239 | 592 | return ssl_parse_hello_verify_request(ssl); |
1240 | 4.13k | } else { |
1241 | | /* We made it through the verification process */ |
1242 | 4.13k | mbedtls_free(ssl->handshake->cookie); |
1243 | 4.13k | ssl->handshake->cookie = NULL; |
1244 | 4.13k | ssl->handshake->cookie_len = 0; |
1245 | 4.13k | } |
1246 | 4.72k | } |
1247 | 4.13k | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
1248 | | |
1249 | 4.13k | if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) || |
1250 | 4.11k | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) { |
1251 | 41 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1252 | 41 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1253 | 41 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1254 | 41 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1255 | 41 | } |
1256 | | |
1257 | | /* |
1258 | | * 0 . 1 server_version |
1259 | | * 2 . 33 random (maybe including 4 bytes of Unix time) |
1260 | | * 34 . 34 session_id length = n |
1261 | | * 35 . 34+n session_id |
1262 | | * 35+n . 36+n cipher_suite |
1263 | | * 37+n . 37+n compression_method |
1264 | | * |
1265 | | * 38+n . 39+n extensions length (optional) |
1266 | | * 40+n . .. extensions |
1267 | | */ |
1268 | 4.09k | buf += mbedtls_ssl_hs_hdr_len(ssl); |
1269 | | |
1270 | 4.09k | MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2); |
1271 | 4.09k | ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf, |
1272 | 4.09k | ssl->conf->transport); |
1273 | 4.09k | ssl->session_negotiate->tls_version = ssl->tls_version; |
1274 | 4.09k | ssl->session_negotiate->endpoint = ssl->conf->endpoint; |
1275 | | |
1276 | 4.09k | if (ssl->tls_version < ssl->conf->min_tls_version || |
1277 | 4.08k | ssl->tls_version > ssl->conf->max_tls_version) { |
1278 | 38 | MBEDTLS_SSL_DEBUG_MSG(1, |
1279 | 38 | ( |
1280 | 38 | "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]", |
1281 | 38 | (unsigned) ssl->conf->min_tls_version, |
1282 | 38 | (unsigned) ssl->tls_version, |
1283 | 38 | (unsigned) ssl->conf->max_tls_version)); |
1284 | | |
1285 | 38 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1286 | 38 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION); |
1287 | | |
1288 | 38 | return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
1289 | 38 | } |
1290 | | |
1291 | 4.05k | MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu", |
1292 | 4.05k | ((unsigned long) buf[2] << 24) | |
1293 | 4.05k | ((unsigned long) buf[3] << 16) | |
1294 | 4.05k | ((unsigned long) buf[4] << 8) | |
1295 | 4.05k | ((unsigned long) buf[5]))); |
1296 | | |
1297 | 4.05k | memcpy(ssl->handshake->randbytes + 32, buf + 2, 32); |
1298 | | |
1299 | 4.05k | n = buf[34]; |
1300 | | |
1301 | 4.05k | MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32); |
1302 | | |
1303 | 4.05k | if (n > 32) { |
1304 | 3 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1305 | 3 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1306 | 3 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1307 | 3 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1308 | 3 | } |
1309 | | |
1310 | 4.05k | if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) { |
1311 | 4.02k | ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n); |
1312 | | |
1313 | 4.02k | if ((ext_len > 0 && ext_len < 4) || |
1314 | 4.02k | ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) { |
1315 | 40 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1316 | 40 | mbedtls_ssl_send_alert_message( |
1317 | 40 | ssl, |
1318 | 40 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1319 | 40 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1320 | 40 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1321 | 40 | } |
1322 | 4.02k | } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) { |
1323 | 24 | ext_len = 0; |
1324 | 24 | } else { |
1325 | 9 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1326 | 9 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1327 | 9 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1328 | 9 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1329 | 9 | } |
1330 | | |
1331 | | /* ciphersuite (used later) */ |
1332 | 4.00k | i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35); |
1333 | | |
1334 | | /* |
1335 | | * Read and check compression |
1336 | | */ |
1337 | 4.00k | comp = buf[37 + n]; |
1338 | | |
1339 | 4.00k | if (comp != MBEDTLS_SSL_COMPRESS_NULL) { |
1340 | 8 | MBEDTLS_SSL_DEBUG_MSG(1, |
1341 | 8 | ("server hello, bad compression: %d", comp)); |
1342 | 8 | mbedtls_ssl_send_alert_message( |
1343 | 8 | ssl, |
1344 | 8 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1345 | 8 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
1346 | 8 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
1347 | 8 | } |
1348 | | |
1349 | | /* |
1350 | | * Initialize update checksum functions |
1351 | | */ |
1352 | 3.99k | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i); |
1353 | 3.99k | if (ssl->handshake->ciphersuite_info == NULL) { |
1354 | 7 | MBEDTLS_SSL_DEBUG_MSG(1, |
1355 | 7 | ("ciphersuite info for %04x not found", (unsigned int) i)); |
1356 | 7 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1357 | 7 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
1358 | 7 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
1359 | 7 | } |
1360 | | |
1361 | 3.99k | mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info); |
1362 | | |
1363 | 3.99k | MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n)); |
1364 | 3.99k | MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n); |
1365 | | |
1366 | | /* |
1367 | | * Check if the session can be resumed |
1368 | | */ |
1369 | 3.99k | if (ssl->handshake->resume == 0 || n == 0 || |
1370 | 0 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1371 | 0 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
1372 | 0 | #endif |
1373 | 0 | ssl->session_negotiate->ciphersuite != i || |
1374 | 0 | ssl->session_negotiate->id_len != n || |
1375 | 3.99k | memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) { |
1376 | 3.99k | mbedtls_ssl_handshake_increment_state(ssl); |
1377 | 3.99k | ssl->handshake->resume = 0; |
1378 | 3.99k | #if defined(MBEDTLS_HAVE_TIME) |
1379 | 3.99k | ssl->session_negotiate->start = mbedtls_time(NULL); |
1380 | 3.99k | #endif |
1381 | 3.99k | ssl->session_negotiate->ciphersuite = i; |
1382 | 3.99k | ssl->session_negotiate->id_len = n; |
1383 | 3.99k | memcpy(ssl->session_negotiate->id, buf + 35, n); |
1384 | 3.99k | } else { |
1385 | 0 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC); |
1386 | 0 | } |
1387 | | |
1388 | 3.99k | MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed", |
1389 | 3.99k | ssl->handshake->resume ? "a" : "no")); |
1390 | | |
1391 | 3.99k | MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i)); |
1392 | 3.99k | MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d", |
1393 | 3.99k | buf[37 + n])); |
1394 | | |
1395 | | /* |
1396 | | * Perform cipher suite validation in same way as in ssl_write_client_hello. |
1397 | | */ |
1398 | 3.99k | i = 0; |
1399 | 355k | while (1) { |
1400 | 355k | if (ssl->conf->ciphersuite_list[i] == 0) { |
1401 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1402 | 0 | mbedtls_ssl_send_alert_message( |
1403 | 0 | ssl, |
1404 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1405 | 0 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
1406 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
1407 | 0 | } |
1408 | | |
1409 | 355k | if (ssl->conf->ciphersuite_list[i++] == |
1410 | 355k | ssl->session_negotiate->ciphersuite) { |
1411 | 3.99k | break; |
1412 | 3.99k | } |
1413 | 355k | } |
1414 | | |
1415 | 3.99k | suite_info = mbedtls_ssl_ciphersuite_from_id( |
1416 | 3.99k | ssl->session_negotiate->ciphersuite); |
1417 | 3.99k | if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version, |
1418 | 3.99k | ssl->tls_version) != 0) { |
1419 | 11 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1420 | 11 | mbedtls_ssl_send_alert_message( |
1421 | 11 | ssl, |
1422 | 11 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1423 | 11 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
1424 | 11 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1425 | 11 | } |
1426 | | |
1427 | 3.98k | MBEDTLS_SSL_DEBUG_MSG(3, |
1428 | 3.98k | ("server hello, chosen ciphersuite: %s", suite_info->name)); |
1429 | | |
1430 | 3.98k | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
1431 | 3.98k | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && |
1432 | 318 | ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
1433 | 318 | ssl->handshake->ecrs_enabled = 1; |
1434 | 318 | } |
1435 | 3.98k | #endif |
1436 | | |
1437 | 3.98k | if (comp != MBEDTLS_SSL_COMPRESS_NULL) { |
1438 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1439 | 0 | mbedtls_ssl_send_alert_message( |
1440 | 0 | ssl, |
1441 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1442 | 0 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
1443 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
1444 | 0 | } |
1445 | | |
1446 | 3.98k | ext = buf + 40 + n; |
1447 | | |
1448 | 3.98k | MBEDTLS_SSL_DEBUG_MSG(2, |
1449 | 3.98k | ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, |
1450 | 3.98k | ext_len)); |
1451 | | |
1452 | 20.7k | while (ext_len) { |
1453 | 16.8k | unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0); |
1454 | 16.8k | unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2); |
1455 | | |
1456 | 16.8k | if (ext_size + 4 > ext_len) { |
1457 | 39 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1458 | 39 | mbedtls_ssl_send_alert_message( |
1459 | 39 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1460 | 39 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
1461 | 39 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1462 | 39 | } |
1463 | | |
1464 | 16.8k | switch (ext_id) { |
1465 | 1.43k | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
1466 | 1.43k | MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension")); |
1467 | 1.43k | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1468 | 1.43k | renegotiation_info_seen = 1; |
1469 | 1.43k | #endif |
1470 | | |
1471 | 1.43k | if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4, |
1472 | 1.43k | ext_size)) != 0) { |
1473 | 10 | return ret; |
1474 | 10 | } |
1475 | | |
1476 | 1.42k | break; |
1477 | | |
1478 | 1.42k | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
1479 | 1.42k | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
1480 | 5 | MBEDTLS_SSL_DEBUG_MSG(3, |
1481 | 5 | ("found max_fragment_length extension")); |
1482 | | |
1483 | 5 | if ((ret = ssl_parse_max_fragment_length_ext(ssl, |
1484 | 5 | ext + 4, ext_size)) != 0) { |
1485 | 5 | return ret; |
1486 | 5 | } |
1487 | | |
1488 | 0 | break; |
1489 | 0 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
1490 | | |
1491 | 0 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
1492 | 1 | case MBEDTLS_TLS_EXT_CID: |
1493 | 1 | MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension")); |
1494 | | |
1495 | 1 | if ((ret = ssl_parse_cid_ext(ssl, |
1496 | 1 | ext + 4, |
1497 | 1 | ext_size)) != 0) { |
1498 | 1 | return ret; |
1499 | 1 | } |
1500 | | |
1501 | 0 | break; |
1502 | 0 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
1503 | | |
1504 | 0 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
1505 | 439 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
1506 | 439 | MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension")); |
1507 | | |
1508 | 439 | if ((ret = ssl_parse_encrypt_then_mac_ext(ssl, |
1509 | 439 | ext + 4, ext_size)) != 0) { |
1510 | 4 | return ret; |
1511 | 4 | } |
1512 | | |
1513 | 435 | break; |
1514 | 435 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
1515 | | |
1516 | 435 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
1517 | 782 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
1518 | 782 | MBEDTLS_SSL_DEBUG_MSG(3, |
1519 | 782 | ("found extended_master_secret extension")); |
1520 | | |
1521 | 782 | if ((ret = ssl_parse_extended_ms_ext(ssl, |
1522 | 782 | ext + 4, ext_size)) != 0) { |
1523 | 4 | return ret; |
1524 | 4 | } |
1525 | | |
1526 | 778 | break; |
1527 | 778 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
1528 | | |
1529 | 778 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
1530 | 778 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
1531 | 568 | MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension")); |
1532 | | |
1533 | 568 | if ((ret = ssl_parse_session_ticket_ext(ssl, |
1534 | 568 | ext + 4, ext_size)) != 0) { |
1535 | 3 | return ret; |
1536 | 3 | } |
1537 | | |
1538 | 565 | break; |
1539 | 565 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
1540 | | |
1541 | 565 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \ |
1542 | 565 | defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \ |
1543 | 565 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
1544 | 891 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
1545 | 891 | MBEDTLS_SSL_DEBUG_MSG(3, |
1546 | 891 | ("found supported_point_formats extension")); |
1547 | | |
1548 | 891 | if ((ret = ssl_parse_supported_point_formats_ext(ssl, |
1549 | 891 | ext + 4, ext_size)) != 0) { |
1550 | 21 | return ret; |
1551 | 21 | } |
1552 | | |
1553 | 870 | break; |
1554 | 870 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED || |
1555 | | MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED || |
1556 | | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
1557 | | |
1558 | 870 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
1559 | 870 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
1560 | 257 | MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension")); |
1561 | | |
1562 | 257 | if ((ret = ssl_parse_ecjpake_kkpp(ssl, |
1563 | 257 | ext + 4, ext_size)) != 0) { |
1564 | 0 | return ret; |
1565 | 0 | } |
1566 | | |
1567 | 257 | break; |
1568 | 257 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
1569 | | |
1570 | 257 | #if defined(MBEDTLS_SSL_ALPN) |
1571 | 257 | case MBEDTLS_TLS_EXT_ALPN: |
1572 | 4 | MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension")); |
1573 | | |
1574 | 4 | if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) { |
1575 | 4 | return ret; |
1576 | 4 | } |
1577 | | |
1578 | 0 | break; |
1579 | 0 | #endif /* MBEDTLS_SSL_ALPN */ |
1580 | | |
1581 | 0 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
1582 | 106 | case MBEDTLS_TLS_EXT_USE_SRTP: |
1583 | 106 | MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension")); |
1584 | | |
1585 | 106 | if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) { |
1586 | 0 | return ret; |
1587 | 0 | } |
1588 | | |
1589 | 106 | break; |
1590 | 106 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
1591 | | |
1592 | 12.3k | default: |
1593 | 12.3k | MBEDTLS_SSL_DEBUG_MSG(3, |
1594 | 16.8k | ("unknown extension found: %u (ignoring)", ext_id)); |
1595 | 16.8k | } |
1596 | | |
1597 | 16.7k | ext_len -= 4 + ext_size; |
1598 | 16.7k | ext += 4 + ext_size; |
1599 | | |
1600 | 16.7k | if (ext_len > 0 && ext_len < 4) { |
1601 | 14 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message")); |
1602 | 14 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1603 | 14 | } |
1604 | 16.7k | } |
1605 | | |
1606 | | /* |
1607 | | * mbedtls_ssl_derive_keys() has to be called after the parsing of the |
1608 | | * extensions. It sets the transform data for the resumed session which in |
1609 | | * case of DTLS includes the server CID extracted from the CID extension. |
1610 | | */ |
1611 | 3.87k | if (ssl->handshake->resume) { |
1612 | 0 | if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) { |
1613 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret); |
1614 | 0 | mbedtls_ssl_send_alert_message( |
1615 | 0 | ssl, |
1616 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1617 | 0 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
1618 | 0 | return ret; |
1619 | 0 | } |
1620 | 0 | } |
1621 | | |
1622 | | /* |
1623 | | * Renegotiation security checks |
1624 | | */ |
1625 | 3.87k | if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
1626 | 2.65k | ssl->conf->allow_legacy_renegotiation == |
1627 | 2.65k | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) { |
1628 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1629 | 0 | ("legacy renegotiation, breaking off handshake")); |
1630 | 0 | handshake_failure = 1; |
1631 | 0 | } |
1632 | 3.87k | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1633 | 3.87k | else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
1634 | 0 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
1635 | 0 | renegotiation_info_seen == 0) { |
1636 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1637 | 0 | ("renegotiation_info extension missing (secure)")); |
1638 | 0 | handshake_failure = 1; |
1639 | 3.87k | } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
1640 | 0 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
1641 | 0 | ssl->conf->allow_legacy_renegotiation == |
1642 | 0 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) { |
1643 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed")); |
1644 | 0 | handshake_failure = 1; |
1645 | 3.87k | } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
1646 | 0 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
1647 | 0 | renegotiation_info_seen == 1) { |
1648 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1649 | 0 | ("renegotiation_info extension present (legacy)")); |
1650 | 0 | handshake_failure = 1; |
1651 | 0 | } |
1652 | 3.87k | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
1653 | | |
1654 | 3.87k | if (handshake_failure == 1) { |
1655 | 0 | mbedtls_ssl_send_alert_message( |
1656 | 0 | ssl, |
1657 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
1658 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
1659 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1660 | 0 | } |
1661 | | |
1662 | 3.87k | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello")); |
1663 | | |
1664 | 3.87k | return 0; |
1665 | 3.87k | } |
1666 | | |
1667 | | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
1668 | | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
1669 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1670 | | static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl, |
1671 | | unsigned char **p, |
1672 | | unsigned char *end) |
1673 | 109 | { |
1674 | 109 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
1675 | 109 | size_t dhm_actual_bitlen; |
1676 | | |
1677 | | /* |
1678 | | * Ephemeral DH parameters: |
1679 | | * |
1680 | | * struct { |
1681 | | * opaque dh_p<1..2^16-1>; |
1682 | | * opaque dh_g<1..2^16-1>; |
1683 | | * opaque dh_Ys<1..2^16-1>; |
1684 | | * } ServerDHParams; |
1685 | | */ |
1686 | 109 | if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx, |
1687 | 109 | p, end)) != 0) { |
1688 | 59 | MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret); |
1689 | 59 | return ret; |
1690 | 59 | } |
1691 | | |
1692 | 50 | dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx); |
1693 | 50 | if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) { |
1694 | 33 | MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u", |
1695 | 33 | dhm_actual_bitlen, |
1696 | 33 | ssl->conf->dhm_min_bitlen)); |
1697 | 33 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1698 | 33 | } |
1699 | | |
1700 | 17 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P); |
1701 | 17 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G); |
1702 | 17 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY); |
1703 | | |
1704 | 17 | return ret; |
1705 | 50 | } |
1706 | | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
1707 | | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
1708 | | |
1709 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1710 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
1711 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
1712 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
1713 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1714 | | static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl, |
1715 | | unsigned char **p, |
1716 | | unsigned char *end) |
1717 | | { |
1718 | | uint16_t tls_id; |
1719 | | size_t ecpoint_len; |
1720 | | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
1721 | | psa_key_type_t key_type = PSA_KEY_TYPE_NONE; |
1722 | | size_t ec_bits = 0; |
1723 | | |
1724 | | /* |
1725 | | * struct { |
1726 | | * ECParameters curve_params; |
1727 | | * ECPoint public; |
1728 | | * } ServerECDHParams; |
1729 | | * |
1730 | | * 1 curve_type (must be "named_curve") |
1731 | | * 2..3 NamedCurve |
1732 | | * 4 ECPoint.len |
1733 | | * 5+ ECPoint contents |
1734 | | */ |
1735 | | if (end - *p < 4) { |
1736 | | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1737 | | } |
1738 | | |
1739 | | /* First byte is curve_type; only named_curve is handled */ |
1740 | | if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) { |
1741 | | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1742 | | } |
1743 | | |
1744 | | /* Next two bytes are the namedcurve value */ |
1745 | | tls_id = MBEDTLS_GET_UINT16_BE(*p, 0); |
1746 | | *p += 2; |
1747 | | |
1748 | | /* Check it's a curve we offered */ |
1749 | | if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) { |
1750 | | MBEDTLS_SSL_DEBUG_MSG(2, |
1751 | | ("bad server key exchange message (ECDHE curve): %u", |
1752 | | (unsigned) tls_id)); |
1753 | | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1754 | | } |
1755 | | |
1756 | | /* Convert EC's TLS ID to PSA key type. */ |
1757 | | if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type, |
1758 | | &ec_bits) == PSA_ERROR_NOT_SUPPORTED) { |
1759 | | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1760 | | } |
1761 | | handshake->xxdh_psa_type = key_type; |
1762 | | handshake->xxdh_psa_bits = ec_bits; |
1763 | | |
1764 | | /* Keep a copy of the peer's public key */ |
1765 | | ecpoint_len = *(*p)++; |
1766 | | if ((size_t) (end - *p) < ecpoint_len) { |
1767 | | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1768 | | } |
1769 | | |
1770 | | if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) { |
1771 | | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1772 | | } |
1773 | | |
1774 | | memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len); |
1775 | | handshake->xxdh_psa_peerkey_len = ecpoint_len; |
1776 | | *p += ecpoint_len; |
1777 | | |
1778 | | return 0; |
1779 | | } |
1780 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
1781 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
1782 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
1783 | | #else |
1784 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
1785 | | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
1786 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
1787 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
1788 | | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
1789 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1790 | | static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl) |
1791 | 972 | { |
1792 | 972 | uint16_t tls_id; |
1793 | 972 | mbedtls_ecp_group_id grp_id; |
1794 | 972 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
1795 | 972 | grp_id = ssl->handshake->ecdh_ctx.grp.id; |
1796 | | #else |
1797 | | grp_id = ssl->handshake->ecdh_ctx.grp_id; |
1798 | | #endif |
1799 | | |
1800 | 972 | tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id); |
1801 | 972 | if (tls_id == 0) { |
1802 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
1803 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
1804 | 0 | } |
1805 | | |
1806 | 972 | MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s", |
1807 | 972 | mbedtls_ssl_get_curve_name_from_tls_id(tls_id))); |
1808 | | |
1809 | 972 | if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) { |
1810 | 36 | return -1; |
1811 | 36 | } |
1812 | | |
1813 | 936 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
1814 | 936 | MBEDTLS_DEBUG_ECDH_QP); |
1815 | | |
1816 | 936 | return 0; |
1817 | 972 | } |
1818 | | |
1819 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
1820 | | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
1821 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
1822 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
1823 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
1824 | | |
1825 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
1826 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
1827 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
1828 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1829 | | static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl, |
1830 | | unsigned char **p, |
1831 | | unsigned char *end) |
1832 | 615 | { |
1833 | 615 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
1834 | | |
1835 | | /* |
1836 | | * Ephemeral ECDH parameters: |
1837 | | * |
1838 | | * struct { |
1839 | | * ECParameters curve_params; |
1840 | | * ECPoint public; |
1841 | | * } ServerECDHParams; |
1842 | | */ |
1843 | 615 | if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx, |
1844 | 615 | (const unsigned char **) p, end)) != 0) { |
1845 | 35 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret); |
1846 | 35 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
1847 | 35 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
1848 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
1849 | 0 | } |
1850 | 35 | #endif |
1851 | 35 | return ret; |
1852 | 35 | } |
1853 | | |
1854 | 580 | if (ssl_check_server_ecdh_params(ssl) != 0) { |
1855 | 36 | MBEDTLS_SSL_DEBUG_MSG(1, |
1856 | 36 | ("bad server key exchange message (ECDHE curve)")); |
1857 | 36 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
1858 | 36 | } |
1859 | | |
1860 | 544 | return ret; |
1861 | 580 | } |
1862 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \ |
1863 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \ |
1864 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
1865 | | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
1866 | | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
1867 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1868 | | static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl, |
1869 | | unsigned char **p, |
1870 | | unsigned char *end) |
1871 | 0 | { |
1872 | 0 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
1873 | 0 | uint16_t len; |
1874 | 0 | ((void) ssl); |
1875 | | |
1876 | | /* |
1877 | | * PSK parameters: |
1878 | | * |
1879 | | * opaque psk_identity_hint<0..2^16-1>; |
1880 | | */ |
1881 | 0 | if (end - (*p) < 2) { |
1882 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1883 | 0 | ("bad server key exchange message (psk_identity_hint length)")); |
1884 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1885 | 0 | } |
1886 | 0 | len = MBEDTLS_GET_UINT16_BE(*p, 0); |
1887 | 0 | *p += 2; |
1888 | |
|
1889 | 0 | if (end - (*p) < len) { |
1890 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
1891 | 0 | ("bad server key exchange message (psk_identity_hint length)")); |
1892 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
1893 | 0 | } |
1894 | | |
1895 | | /* |
1896 | | * Note: we currently ignore the PSK identity hint, as we only allow one |
1897 | | * PSK to be provisioned on the client. This could be changed later if |
1898 | | * someone needs that feature. |
1899 | | */ |
1900 | 0 | *p += len; |
1901 | 0 | ret = 0; |
1902 | |
|
1903 | 0 | return ret; |
1904 | 0 | } |
1905 | | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
1906 | | |
1907 | | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
1908 | | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
1909 | | /* |
1910 | | * Generate a pre-master secret and encrypt it with the server's RSA key |
1911 | | */ |
1912 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1913 | | static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl, |
1914 | | size_t offset, size_t *olen, |
1915 | | size_t pms_offset) |
1916 | 588 | { |
1917 | 588 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1918 | 588 | size_t len_bytes = 2; |
1919 | 588 | unsigned char *p = ssl->handshake->premaster + pms_offset; |
1920 | 588 | mbedtls_pk_context *peer_pk; |
1921 | | |
1922 | 588 | if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
1923 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms")); |
1924 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
1925 | 0 | } |
1926 | | |
1927 | | /* |
1928 | | * Generate (part of) the pre-master as |
1929 | | * struct { |
1930 | | * ProtocolVersion client_version; |
1931 | | * opaque random[46]; |
1932 | | * } PreMasterSecret; |
1933 | | */ |
1934 | 588 | mbedtls_ssl_write_version(p, ssl->conf->transport, |
1935 | 588 | MBEDTLS_SSL_VERSION_TLS1_2); |
1936 | | |
1937 | 588 | if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) { |
1938 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret); |
1939 | 0 | return ret; |
1940 | 0 | } |
1941 | | |
1942 | 588 | ssl->handshake->pmslen = 48; |
1943 | | |
1944 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
1945 | | peer_pk = &ssl->handshake->peer_pubkey; |
1946 | | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
1947 | 588 | if (ssl->session_negotiate->peer_cert == NULL) { |
1948 | | /* Should never happen */ |
1949 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
1950 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
1951 | 0 | } |
1952 | 588 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
1953 | 588 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
1954 | | |
1955 | | /* |
1956 | | * Now write it out, encrypted |
1957 | | */ |
1958 | 588 | if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) { |
1959 | 2 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch")); |
1960 | 2 | return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH; |
1961 | 2 | } |
1962 | | |
1963 | 586 | if ((ret = mbedtls_pk_encrypt(peer_pk, |
1964 | 586 | p, ssl->handshake->pmslen, |
1965 | 586 | ssl->out_msg + offset + len_bytes, olen, |
1966 | 586 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, |
1967 | 586 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
1968 | 1 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_encrypt", ret); |
1969 | 1 | return ret; |
1970 | 1 | } |
1971 | | |
1972 | 585 | if (len_bytes == 2) { |
1973 | 585 | MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset); |
1974 | 585 | *olen += 2; |
1975 | 585 | } |
1976 | | |
1977 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
1978 | | /* We don't need the peer's public key anymore. Free it. */ |
1979 | | mbedtls_pk_free(peer_pk); |
1980 | | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
1981 | 585 | return 0; |
1982 | 586 | } |
1983 | | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
1984 | | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
1985 | | |
1986 | | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
1987 | | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
1988 | | MBEDTLS_CHECK_RETURN_CRITICAL |
1989 | | static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl) |
1990 | 398 | { |
1991 | 398 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1992 | 398 | mbedtls_pk_context *peer_pk; |
1993 | | |
1994 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
1995 | | peer_pk = &ssl->handshake->peer_pubkey; |
1996 | | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
1997 | 398 | if (ssl->session_negotiate->peer_cert == NULL) { |
1998 | | /* Should never happen */ |
1999 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
2000 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2001 | 0 | } |
2002 | 398 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
2003 | 398 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
2004 | | |
2005 | | /* This is a public key, so it can't be opaque, so can_do() is a good |
2006 | | * enough check to ensure pk_ec() is safe to use below. */ |
2007 | 398 | if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) { |
2008 | 6 | MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable")); |
2009 | 6 | return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH; |
2010 | 6 | } |
2011 | | |
2012 | 392 | #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
2013 | 392 | const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk); |
2014 | 392 | #endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */ |
2015 | | |
2016 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
2017 | | uint16_t tls_id = 0; |
2018 | 0 | psa_key_type_t key_type = PSA_KEY_TYPE_NONE; |
2019 | | mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk); |
2020 | | |
2021 | 0 | if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) { |
2022 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)")); |
2023 | 0 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
2024 | 0 | } |
2025 | | |
2026 | 0 | tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id); |
2027 | 0 | if (tls_id == 0) { |
2028 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not supported", |
2029 | 0 | grp_id)); |
2030 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2031 | 0 | } |
2032 | | |
2033 | | /* If the above conversion to TLS ID was fine, then also this one will be, |
2034 | | so there is no need to check the return value here */ |
2035 | 0 | mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type, |
2036 | 0 | &ssl->handshake->xxdh_psa_bits); |
2037 | |
|
2038 | 0 | ssl->handshake->xxdh_psa_type = key_type; |
2039 | | |
2040 | | /* Store peer's public key in psa format. */ |
2041 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
2042 | | memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len); |
2043 | | ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len; |
2044 | | ret = 0; |
2045 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
2046 | 0 | size_t olen = 0; |
2047 | 0 | ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q, |
2048 | 0 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, |
2049 | 0 | ssl->handshake->xxdh_psa_peerkey, |
2050 | 0 | sizeof(ssl->handshake->xxdh_psa_peerkey)); |
2051 | |
|
2052 | 0 | if (ret != 0) { |
2053 | 0 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret); |
2054 | 0 | return ret; |
2055 | 0 | } |
2056 | 0 | ssl->handshake->xxdh_psa_peerkey_len = olen; |
2057 | 0 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
2058 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
2059 | 392 | if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key, |
2060 | 392 | MBEDTLS_ECDH_THEIRS)) != 0) { |
2061 | 0 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret); |
2062 | 0 | return ret; |
2063 | 0 | } |
2064 | | |
2065 | 392 | if (ssl_check_server_ecdh_params(ssl) != 0) { |
2066 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)")); |
2067 | 0 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
2068 | 0 | } |
2069 | 392 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
2070 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
2071 | | /* We don't need the peer's public key anymore. Free it, |
2072 | | * so that more RAM is available for upcoming expensive |
2073 | | * operations like ECDHE. */ |
2074 | | mbedtls_pk_free(peer_pk); |
2075 | | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
2076 | | |
2077 | 392 | return ret; |
2078 | 392 | } ssl_tls12_client.c:ssl_get_ecdh_params_from_cert Line | Count | Source | 1990 | 398 | { | 1991 | 398 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 1992 | 398 | mbedtls_pk_context *peer_pk; | 1993 | | | 1994 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 1995 | | peer_pk = &ssl->handshake->peer_pubkey; | 1996 | | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 1997 | 398 | if (ssl->session_negotiate->peer_cert == NULL) { | 1998 | | /* Should never happen */ | 1999 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 2000 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2001 | 0 | } | 2002 | 398 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 2003 | 398 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 2004 | | | 2005 | | /* This is a public key, so it can't be opaque, so can_do() is a good | 2006 | | * enough check to ensure pk_ec() is safe to use below. */ | 2007 | 398 | if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) { | 2008 | 6 | MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable")); | 2009 | 6 | return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH; | 2010 | 6 | } | 2011 | | | 2012 | 392 | #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA) | 2013 | 392 | const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk); | 2014 | 392 | #endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */ | 2015 | | | 2016 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 2017 | | uint16_t tls_id = 0; | 2018 | | psa_key_type_t key_type = PSA_KEY_TYPE_NONE; | 2019 | | mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk); | 2020 | | | 2021 | | if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) { | 2022 | | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)")); | 2023 | | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 2024 | | } | 2025 | | | 2026 | | tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id); | 2027 | | if (tls_id == 0) { | 2028 | | MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not supported", | 2029 | | grp_id)); | 2030 | | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2031 | | } | 2032 | | | 2033 | | /* If the above conversion to TLS ID was fine, then also this one will be, | 2034 | | so there is no need to check the return value here */ | 2035 | | mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type, | 2036 | | &ssl->handshake->xxdh_psa_bits); | 2037 | | | 2038 | | ssl->handshake->xxdh_psa_type = key_type; | 2039 | | | 2040 | | /* Store peer's public key in psa format. */ | 2041 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) | 2042 | | memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len); | 2043 | | ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len; | 2044 | | ret = 0; | 2045 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ | 2046 | | size_t olen = 0; | 2047 | | ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q, | 2048 | | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, | 2049 | | ssl->handshake->xxdh_psa_peerkey, | 2050 | | sizeof(ssl->handshake->xxdh_psa_peerkey)); | 2051 | | | 2052 | | if (ret != 0) { | 2053 | | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret); | 2054 | | return ret; | 2055 | | } | 2056 | | ssl->handshake->xxdh_psa_peerkey_len = olen; | 2057 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ | 2058 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 2059 | 392 | if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key, | 2060 | 392 | MBEDTLS_ECDH_THEIRS)) != 0) { | 2061 | 0 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret); | 2062 | 0 | return ret; | 2063 | 0 | } | 2064 | | | 2065 | 392 | if (ssl_check_server_ecdh_params(ssl) != 0) { | 2066 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)")); | 2067 | 0 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 2068 | 0 | } | 2069 | 392 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 2070 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 2071 | | /* We don't need the peer's public key anymore. Free it, | 2072 | | * so that more RAM is available for upcoming expensive | 2073 | | * operations like ECDHE. */ | 2074 | | mbedtls_pk_free(peer_pk); | 2075 | | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 2076 | | | 2077 | 392 | return ret; | 2078 | 392 | } |
Unexecuted instantiation: ssl_tls12_client.c:ssl_get_ecdh_params_from_cert |
2079 | | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
2080 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
2081 | | |
2082 | | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) |
2083 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2084 | | static int ssl_parse_signature_algorithm(mbedtls_ssl_context *ssl, |
2085 | | uint16_t sig_alg, |
2086 | | mbedtls_md_type_t *md_alg, |
2087 | | mbedtls_pk_type_t *pk_alg) |
2088 | 555 | { |
2089 | 555 | if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(sig_alg, pk_alg, md_alg) != 0) { |
2090 | 256 | MBEDTLS_SSL_DEBUG_MSG(1, |
2091 | 256 | ("Server used unsupported %s signature algorithm", |
2092 | 256 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
2093 | 256 | return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER; |
2094 | 256 | } |
2095 | | |
2096 | | /* |
2097 | | * mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg() understands |
2098 | | * signature algorithm code points from both TLS 1.2 and TLS 1.3. Make sure |
2099 | | * that the selected signature algorithm is acceptable when TLS 1.2 is |
2100 | | * negotiated. |
2101 | | * |
2102 | | * In TLS 1.2, RSA-PSS signature algorithms (rsa_pss_rsae_*) are not |
2103 | | * defined by RFC 5246. However, RFC 8446 Section 4.2.3 requires that |
2104 | | * implementations which advertise support for RSASSA-PSS must be |
2105 | | * prepared to accept such signatures even when TLS 1.2 is negotiated, |
2106 | | * provided they were offered in the signature_algorithms extension. |
2107 | | * |
2108 | | * Therefore, we allow rsa_pss_rsae_* here if: |
2109 | | * - the implementation supports them, and |
2110 | | * - they were offered in the signature_algorithms extension (checked by |
2111 | | * `mbedtls_ssl_sig_alg_is_offered()` below). |
2112 | | * |
2113 | | * If we were to add full support for rsa_pss_rsae_* signature algorithms |
2114 | | * in TLS 1.2, we should then integrate RSA-PSS into the TLS 1.2 signature |
2115 | | * algorithm support logic (`mbedtls_ssl_tls12_sig_alg_is_supported()`) |
2116 | | * instead of handling it as a special case here. |
2117 | | */ |
2118 | 299 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) { |
2119 | 45 | switch (sig_alg) { |
2120 | 0 | #if defined(PSA_WANT_ALG_RSA_PSS) |
2121 | 0 | #if defined(PSA_WANT_ALG_SHA_256) |
2122 | 21 | case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: |
2123 | 21 | #endif |
2124 | 21 | #if defined(PSA_WANT_ALG_SHA_384) |
2125 | 35 | case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: |
2126 | 35 | #endif |
2127 | 35 | #if defined(PSA_WANT_ALG_SHA_512) |
2128 | 45 | case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512: |
2129 | 45 | #endif |
2130 | 45 | #if defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA_512) |
2131 | 45 | MBEDTLS_SSL_DEBUG_MSG(3, |
2132 | 45 | ( |
2133 | 45 | "Accepting TLS 1.2 RSA-PSS signature algorithm %s via compatibility exception", |
2134 | 45 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
2135 | 45 | break; |
2136 | 0 | #endif |
2137 | 0 | #endif /* PSA_WANT_ALG_RSA_PSS */ |
2138 | 0 | default: |
2139 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
2140 | 0 | ("Server used unsupported %s signature algorithm", |
2141 | 0 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
2142 | 0 | return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER; |
2143 | 45 | } |
2144 | 45 | } |
2145 | | |
2146 | | /* |
2147 | | * Check if the signature algorithm is acceptable |
2148 | | */ |
2149 | 299 | if (!mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)) { |
2150 | 4 | MBEDTLS_SSL_DEBUG_MSG(1, |
2151 | 4 | ("Server used the signature algorithm %s that was not offered", |
2152 | 4 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
2153 | 4 | return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER; |
2154 | 4 | } |
2155 | | |
2156 | 295 | MBEDTLS_SSL_DEBUG_MSG(2, ("Server used the signature algorithm %s", |
2157 | 295 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
2158 | | |
2159 | 295 | return 0; |
2160 | 299 | } |
2161 | | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) */ |
2162 | | |
2163 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2164 | | static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl) |
2165 | 1.92k | { |
2166 | 1.92k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2167 | 1.92k | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
2168 | 1.92k | ssl->handshake->ciphersuite_info; |
2169 | 1.92k | unsigned char *p = NULL, *end = NULL; |
2170 | | |
2171 | 1.92k | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange")); |
2172 | | |
2173 | 1.92k | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
2174 | 1.92k | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) { |
2175 | 789 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange")); |
2176 | 789 | mbedtls_ssl_handshake_increment_state(ssl); |
2177 | 789 | return 0; |
2178 | 789 | } |
2179 | 1.13k | ((void) p); |
2180 | 1.13k | ((void) end); |
2181 | 1.13k | #endif |
2182 | | |
2183 | 1.13k | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
2184 | 1.13k | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
2185 | 1.13k | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
2186 | 935 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) { |
2187 | 398 | if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) { |
2188 | 6 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret); |
2189 | 6 | mbedtls_ssl_send_alert_message( |
2190 | 6 | ssl, |
2191 | 6 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2192 | 6 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
2193 | 6 | return ret; |
2194 | 6 | } |
2195 | | |
2196 | 392 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange")); |
2197 | 392 | mbedtls_ssl_handshake_increment_state(ssl); |
2198 | 392 | return 0; |
2199 | 398 | } |
2200 | 738 | ((void) p); |
2201 | 738 | ((void) end); |
2202 | 738 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
2203 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
2204 | | |
2205 | 738 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2206 | 738 | if (ssl->handshake->ecrs_enabled && |
2207 | 263 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) { |
2208 | 0 | goto start_processing; |
2209 | 0 | } |
2210 | 738 | #endif |
2211 | | |
2212 | 738 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
2213 | 12 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
2214 | 12 | return ret; |
2215 | 12 | } |
2216 | | |
2217 | 726 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
2218 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2219 | 1 | mbedtls_ssl_send_alert_message( |
2220 | 1 | ssl, |
2221 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2222 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
2223 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
2224 | 1 | } |
2225 | | |
2226 | | /* |
2227 | | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server |
2228 | | * doesn't use a psk_identity_hint |
2229 | | */ |
2230 | 725 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) { |
2231 | 1 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
2232 | 1 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
2233 | | /* Current message is probably either |
2234 | | * CertificateRequest or ServerHelloDone */ |
2235 | 0 | ssl->keep_current_message = 1; |
2236 | 0 | goto exit; |
2237 | 0 | } |
2238 | | |
2239 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, |
2240 | 1 | ("server key exchange message must not be skipped")); |
2241 | 1 | mbedtls_ssl_send_alert_message( |
2242 | 1 | ssl, |
2243 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2244 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
2245 | | |
2246 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
2247 | 1 | } |
2248 | | |
2249 | 724 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2250 | 724 | if (ssl->handshake->ecrs_enabled) { |
2251 | 260 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; |
2252 | 260 | } |
2253 | | |
2254 | 724 | start_processing: |
2255 | 724 | #endif |
2256 | 724 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); |
2257 | 724 | end = ssl->in_msg + ssl->in_hslen; |
2258 | 724 | MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p)); |
2259 | | |
2260 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
2261 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
2262 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
2263 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
2264 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
2265 | 0 | if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) { |
2266 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2267 | 0 | mbedtls_ssl_send_alert_message( |
2268 | 0 | ssl, |
2269 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2270 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2271 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2272 | 0 | } |
2273 | 0 | } /* FALLTHROUGH */ |
2274 | 724 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
2275 | | |
2276 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ |
2277 | 724 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
2278 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
2279 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
2280 | 0 | ; /* nothing more to do */ |
2281 | 0 | } else |
2282 | 724 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || |
2283 | | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
2284 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
2285 | 724 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
2286 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || |
2287 | 615 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
2288 | 109 | if (ssl_parse_server_dh_params(ssl, &p, end) != 0) { |
2289 | 92 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2290 | 92 | mbedtls_ssl_send_alert_message( |
2291 | 92 | ssl, |
2292 | 92 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2293 | 92 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
2294 | 92 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2295 | 92 | } |
2296 | 109 | } else |
2297 | 615 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
2298 | | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
2299 | 615 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
2300 | 615 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
2301 | 615 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
2302 | 615 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
2303 | 260 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
2304 | 615 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) { |
2305 | 615 | if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) { |
2306 | 71 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2307 | 71 | mbedtls_ssl_send_alert_message( |
2308 | 71 | ssl, |
2309 | 71 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2310 | 71 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
2311 | 71 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2312 | 71 | } |
2313 | 615 | } else |
2314 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
2315 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
2316 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
2317 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
2318 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
2319 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
2320 | | /* |
2321 | | * The first 3 bytes are: |
2322 | | * [0] MBEDTLS_ECP_TLS_NAMED_CURVE |
2323 | | * [1, 2] elliptic curve's TLS ID |
2324 | | * |
2325 | | * However since we only support secp256r1 for now, we check only |
2326 | | * that TLS ID here |
2327 | | */ |
2328 | 0 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 3); |
2329 | | |
2330 | 0 | uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1); |
2331 | 0 | uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( |
2332 | 0 | MBEDTLS_ECP_DP_SECP256R1); |
2333 | |
|
2334 | 0 | if (exp_tls_id == 0) { |
2335 | 0 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
2336 | 0 | } |
2337 | | |
2338 | 0 | if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) || |
2339 | 0 | (read_tls_id != exp_tls_id)) { |
2340 | 0 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2341 | 0 | } |
2342 | | |
2343 | 0 | p += 3; |
2344 | |
|
2345 | 0 | if ((ret = mbedtls_psa_ecjpake_read_round( |
2346 | 0 | &ssl->handshake->psa_pake_ctx, p, end - p, |
2347 | 0 | MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) { |
2348 | 0 | psa_destroy_key(ssl->handshake->psa_pake_password); |
2349 | 0 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
2350 | |
|
2351 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret); |
2352 | 0 | mbedtls_ssl_send_alert_message( |
2353 | 0 | ssl, |
2354 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2355 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
2356 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
2357 | 0 | } |
2358 | | #else |
2359 | | ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx, |
2360 | | p, end - p); |
2361 | 0 | if (ret != 0) { |
2362 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret); |
2363 | 0 | mbedtls_ssl_send_alert_message( |
2364 | 0 | ssl, |
2365 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2366 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
2367 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
2368 | 0 | } |
2369 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
2370 | 0 | } else |
2371 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
2372 | 0 | { |
2373 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
2374 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2375 | 0 | } |
2376 | | |
2377 | 561 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) |
2378 | 561 | if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) { |
2379 | 561 | size_t sig_len, hashlen; |
2380 | 561 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
2381 | | |
2382 | 561 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
2383 | 561 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
2384 | 561 | unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); |
2385 | 561 | size_t params_len = (size_t) (p - params); |
2386 | 561 | void *rs_ctx = NULL; |
2387 | | |
2388 | 561 | mbedtls_pk_context *peer_pk; |
2389 | | |
2390 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
2391 | | peer_pk = &ssl->handshake->peer_pubkey; |
2392 | | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
2393 | 561 | if (ssl->session_negotiate->peer_cert == NULL) { |
2394 | | /* Should never happen */ |
2395 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
2396 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2397 | 0 | } |
2398 | 561 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
2399 | 561 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
2400 | | |
2401 | | /* |
2402 | | * Handle the digitally-signed structure |
2403 | | */ |
2404 | 561 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
2405 | 555 | uint16_t sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
2406 | 555 | if (ssl_parse_signature_algorithm(ssl, sig_alg, &md_alg, &pk_alg) != 0) { |
2407 | 260 | MBEDTLS_SSL_DEBUG_MSG(1, |
2408 | 260 | ("bad server key exchange message")); |
2409 | 260 | mbedtls_ssl_send_alert_message( |
2410 | 260 | ssl, |
2411 | 260 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2412 | 260 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
2413 | 260 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2414 | 260 | } |
2415 | 295 | p += 2; |
2416 | | |
2417 | 295 | if (!mbedtls_pk_can_do(peer_pk, pk_alg)) { |
2418 | 11 | MBEDTLS_SSL_DEBUG_MSG(1, |
2419 | 11 | ("bad server key exchange message")); |
2420 | 11 | mbedtls_ssl_send_alert_message( |
2421 | 11 | ssl, |
2422 | 11 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2423 | 11 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); |
2424 | 11 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
2425 | 11 | } |
2426 | | |
2427 | | /* |
2428 | | * Read signature |
2429 | | */ |
2430 | | |
2431 | 284 | if (p > end - 2) { |
2432 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2433 | 1 | mbedtls_ssl_send_alert_message( |
2434 | 1 | ssl, |
2435 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2436 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2437 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2438 | 1 | } |
2439 | 283 | sig_len = MBEDTLS_GET_UINT16_BE(p, 0); |
2440 | 283 | p += 2; |
2441 | | |
2442 | 283 | if (p != end - sig_len) { |
2443 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2444 | 1 | mbedtls_ssl_send_alert_message( |
2445 | 1 | ssl, |
2446 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2447 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2448 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2449 | 1 | } |
2450 | | |
2451 | 282 | MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len); |
2452 | | |
2453 | | /* |
2454 | | * Compute the hash that has been signed |
2455 | | */ |
2456 | 282 | if (md_alg != MBEDTLS_MD_NONE) { |
2457 | 282 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen, |
2458 | 282 | params, params_len, |
2459 | 282 | md_alg); |
2460 | 282 | if (ret != 0) { |
2461 | 0 | return ret; |
2462 | 0 | } |
2463 | 282 | } else { |
2464 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
2465 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2466 | 0 | } |
2467 | | |
2468 | 282 | MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen); |
2469 | | |
2470 | | /* |
2471 | | * Verify signature |
2472 | | */ |
2473 | 282 | if (!mbedtls_pk_can_do(peer_pk, pk_alg)) { |
2474 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); |
2475 | 0 | mbedtls_ssl_send_alert_message( |
2476 | 0 | ssl, |
2477 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2478 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
2479 | 0 | return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH; |
2480 | 0 | } |
2481 | | |
2482 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2483 | 282 | if (ssl->handshake->ecrs_enabled) { |
2484 | 158 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
2485 | 158 | } |
2486 | 282 | #endif |
2487 | | |
2488 | 282 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
2489 | 282 | if (pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
2490 | 41 | mbedtls_pk_rsassa_pss_options rsassa_pss_options; |
2491 | 41 | rsassa_pss_options.mgf1_hash_id = md_alg; |
2492 | 41 | rsassa_pss_options.expected_salt_len = |
2493 | 41 | mbedtls_md_get_size_from_type(md_alg); |
2494 | 41 | if (rsassa_pss_options.expected_salt_len == 0) { |
2495 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2496 | 0 | } |
2497 | | |
2498 | 41 | ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options, |
2499 | 41 | peer_pk, |
2500 | 41 | md_alg, hash, hashlen, |
2501 | 41 | p, sig_len); |
2502 | 41 | } else |
2503 | 241 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
2504 | 241 | ret = mbedtls_pk_verify_restartable(peer_pk, |
2505 | 241 | md_alg, hash, hashlen, p, sig_len, rs_ctx); |
2506 | | |
2507 | 282 | if (ret != 0) { |
2508 | 282 | int send_alert_msg = 1; |
2509 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2510 | 282 | send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS); |
2511 | 282 | #endif |
2512 | 282 | if (send_alert_msg) { |
2513 | 282 | mbedtls_ssl_send_alert_message( |
2514 | 282 | ssl, |
2515 | 282 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2516 | 282 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
2517 | 282 | } |
2518 | 282 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret); |
2519 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2520 | 282 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
2521 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
2522 | 0 | } |
2523 | 282 | #endif |
2524 | 282 | return ret; |
2525 | 282 | } |
2526 | | |
2527 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
2528 | | /* We don't need the peer's public key anymore. Free it, |
2529 | | * so that more RAM is available for upcoming expensive |
2530 | | * operations like ECDHE. */ |
2531 | | mbedtls_pk_free(peer_pk); |
2532 | | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
2533 | 282 | } |
2534 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ |
2535 | | |
2536 | 0 | exit: |
2537 | 0 | mbedtls_ssl_handshake_increment_state(ssl); |
2538 | |
|
2539 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange")); |
2540 | |
|
2541 | 0 | return 0; |
2542 | 561 | } ssl_tls12_client.c:ssl_parse_server_key_exchange Line | Count | Source | 2165 | 1.92k | { | 2166 | 1.92k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 2167 | 1.92k | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 2168 | 1.92k | ssl->handshake->ciphersuite_info; | 2169 | 1.92k | unsigned char *p = NULL, *end = NULL; | 2170 | | | 2171 | 1.92k | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange")); | 2172 | | | 2173 | 1.92k | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 2174 | 1.92k | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) { | 2175 | 789 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange")); | 2176 | 789 | mbedtls_ssl_handshake_increment_state(ssl); | 2177 | 789 | return 0; | 2178 | 789 | } | 2179 | 1.13k | ((void) p); | 2180 | 1.13k | ((void) end); | 2181 | 1.13k | #endif | 2182 | | | 2183 | 1.13k | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 2184 | 1.13k | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 2185 | 1.13k | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 2186 | 935 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) { | 2187 | 398 | if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) { | 2188 | 6 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret); | 2189 | 6 | mbedtls_ssl_send_alert_message( | 2190 | 6 | ssl, | 2191 | 6 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2192 | 6 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); | 2193 | 6 | return ret; | 2194 | 6 | } | 2195 | | | 2196 | 392 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange")); | 2197 | 392 | mbedtls_ssl_handshake_increment_state(ssl); | 2198 | 392 | return 0; | 2199 | 398 | } | 2200 | 738 | ((void) p); | 2201 | 738 | ((void) end); | 2202 | 738 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 2203 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 2204 | | | 2205 | 738 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2206 | 738 | if (ssl->handshake->ecrs_enabled && | 2207 | 263 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) { | 2208 | 0 | goto start_processing; | 2209 | 0 | } | 2210 | 738 | #endif | 2211 | | | 2212 | 738 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { | 2213 | 12 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); | 2214 | 12 | return ret; | 2215 | 12 | } | 2216 | | | 2217 | 726 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { | 2218 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2219 | 1 | mbedtls_ssl_send_alert_message( | 2220 | 1 | ssl, | 2221 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2222 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 2223 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 2224 | 1 | } | 2225 | | | 2226 | | /* | 2227 | | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server | 2228 | | * doesn't use a psk_identity_hint | 2229 | | */ | 2230 | 725 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) { | 2231 | 1 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 2232 | 1 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { | 2233 | | /* Current message is probably either | 2234 | | * CertificateRequest or ServerHelloDone */ | 2235 | 0 | ssl->keep_current_message = 1; | 2236 | 0 | goto exit; | 2237 | 0 | } | 2238 | | | 2239 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, | 2240 | 1 | ("server key exchange message must not be skipped")); | 2241 | 1 | mbedtls_ssl_send_alert_message( | 2242 | 1 | ssl, | 2243 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2244 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 2245 | | | 2246 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 2247 | 1 | } | 2248 | | | 2249 | 724 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2250 | 724 | if (ssl->handshake->ecrs_enabled) { | 2251 | 260 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; | 2252 | 260 | } | 2253 | | | 2254 | 724 | start_processing: | 2255 | 724 | #endif | 2256 | 724 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); | 2257 | 724 | end = ssl->in_msg + ssl->in_hslen; | 2258 | 724 | MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p)); | 2259 | | | 2260 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 2261 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 2262 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 2263 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 2264 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { | 2265 | 0 | if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) { | 2266 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2267 | 0 | mbedtls_ssl_send_alert_message( | 2268 | 0 | ssl, | 2269 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2270 | 0 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 2271 | 0 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 2272 | 0 | } | 2273 | 0 | } /* FALLTHROUGH */ | 2274 | 724 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 2275 | | | 2276 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ | 2277 | 724 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 2278 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 2279 | 724 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { | 2280 | 0 | ; /* nothing more to do */ | 2281 | 0 | } else | 2282 | 724 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || | 2283 | | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 2284 | 724 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ | 2285 | 724 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 2286 | 724 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || | 2287 | 615 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { | 2288 | 109 | if (ssl_parse_server_dh_params(ssl, &p, end) != 0) { | 2289 | 92 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2290 | 92 | mbedtls_ssl_send_alert_message( | 2291 | 92 | ssl, | 2292 | 92 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2293 | 92 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); | 2294 | 92 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2295 | 92 | } | 2296 | 109 | } else | 2297 | 615 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 2298 | | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 2299 | 615 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ | 2300 | 615 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ | 2301 | 615 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 2302 | 615 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 2303 | 260 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 2304 | 615 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) { | 2305 | 615 | if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) { | 2306 | 71 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2307 | 71 | mbedtls_ssl_send_alert_message( | 2308 | 71 | ssl, | 2309 | 71 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2310 | 71 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); | 2311 | 71 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2312 | 71 | } | 2313 | 615 | } else | 2314 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 2315 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || | 2316 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 2317 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 2318 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { | 2319 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 2320 | | /* | 2321 | | * The first 3 bytes are: | 2322 | | * [0] MBEDTLS_ECP_TLS_NAMED_CURVE | 2323 | | * [1, 2] elliptic curve's TLS ID | 2324 | | * | 2325 | | * However since we only support secp256r1 for now, we check only | 2326 | | * that TLS ID here | 2327 | | */ | 2328 | | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 3); | 2329 | | | 2330 | | uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1); | 2331 | | uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( | 2332 | | MBEDTLS_ECP_DP_SECP256R1); | 2333 | | | 2334 | | if (exp_tls_id == 0) { | 2335 | | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 2336 | | } | 2337 | | | 2338 | | if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) || | 2339 | | (read_tls_id != exp_tls_id)) { | 2340 | | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2341 | | } | 2342 | | | 2343 | | p += 3; | 2344 | | | 2345 | | if ((ret = mbedtls_psa_ecjpake_read_round( | 2346 | | &ssl->handshake->psa_pake_ctx, p, end - p, | 2347 | | MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) { | 2348 | | psa_destroy_key(ssl->handshake->psa_pake_password); | 2349 | | psa_pake_abort(&ssl->handshake->psa_pake_ctx); | 2350 | | | 2351 | | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret); | 2352 | | mbedtls_ssl_send_alert_message( | 2353 | | ssl, | 2354 | | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2355 | | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); | 2356 | | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 2357 | | } | 2358 | | #else | 2359 | 0 | ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx, | 2360 | 0 | p, end - p); | 2361 | 0 | if (ret != 0) { | 2362 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret); | 2363 | 0 | mbedtls_ssl_send_alert_message( | 2364 | 0 | ssl, | 2365 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2366 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); | 2367 | 0 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 2368 | 0 | } | 2369 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 2370 | 0 | } else | 2371 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 2372 | 0 | { | 2373 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 2374 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2375 | 0 | } | 2376 | | | 2377 | 561 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 2378 | 561 | if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) { | 2379 | 561 | size_t sig_len, hashlen; | 2380 | 561 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 2381 | | | 2382 | 561 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 2383 | 561 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; | 2384 | 561 | unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); | 2385 | 561 | size_t params_len = (size_t) (p - params); | 2386 | 561 | void *rs_ctx = NULL; | 2387 | | | 2388 | 561 | mbedtls_pk_context *peer_pk; | 2389 | | | 2390 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 2391 | | peer_pk = &ssl->handshake->peer_pubkey; | 2392 | | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 2393 | 561 | if (ssl->session_negotiate->peer_cert == NULL) { | 2394 | | /* Should never happen */ | 2395 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 2396 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2397 | 0 | } | 2398 | 561 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 2399 | 561 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 2400 | | | 2401 | | /* | 2402 | | * Handle the digitally-signed structure | 2403 | | */ | 2404 | 561 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); | 2405 | 555 | uint16_t sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); | 2406 | 555 | if (ssl_parse_signature_algorithm(ssl, sig_alg, &md_alg, &pk_alg) != 0) { | 2407 | 260 | MBEDTLS_SSL_DEBUG_MSG(1, | 2408 | 260 | ("bad server key exchange message")); | 2409 | 260 | mbedtls_ssl_send_alert_message( | 2410 | 260 | ssl, | 2411 | 260 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2412 | 260 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); | 2413 | 260 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2414 | 260 | } | 2415 | 295 | p += 2; | 2416 | | | 2417 | 295 | if (!mbedtls_pk_can_do(peer_pk, pk_alg)) { | 2418 | 11 | MBEDTLS_SSL_DEBUG_MSG(1, | 2419 | 11 | ("bad server key exchange message")); | 2420 | 11 | mbedtls_ssl_send_alert_message( | 2421 | 11 | ssl, | 2422 | 11 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2423 | 11 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER); | 2424 | 11 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 2425 | 11 | } | 2426 | | | 2427 | | /* | 2428 | | * Read signature | 2429 | | */ | 2430 | | | 2431 | 284 | if (p > end - 2) { | 2432 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2433 | 1 | mbedtls_ssl_send_alert_message( | 2434 | 1 | ssl, | 2435 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2436 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 2437 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 2438 | 1 | } | 2439 | 283 | sig_len = MBEDTLS_GET_UINT16_BE(p, 0); | 2440 | 283 | p += 2; | 2441 | | | 2442 | 283 | if (p != end - sig_len) { | 2443 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2444 | 1 | mbedtls_ssl_send_alert_message( | 2445 | 1 | ssl, | 2446 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2447 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 2448 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 2449 | 1 | } | 2450 | | | 2451 | 282 | MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len); | 2452 | | | 2453 | | /* | 2454 | | * Compute the hash that has been signed | 2455 | | */ | 2456 | 282 | if (md_alg != MBEDTLS_MD_NONE) { | 2457 | 282 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen, | 2458 | 282 | params, params_len, | 2459 | 282 | md_alg); | 2460 | 282 | if (ret != 0) { | 2461 | 0 | return ret; | 2462 | 0 | } | 2463 | 282 | } else { | 2464 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 2465 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2466 | 0 | } | 2467 | | | 2468 | 282 | MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen); | 2469 | | | 2470 | | /* | 2471 | | * Verify signature | 2472 | | */ | 2473 | 282 | if (!mbedtls_pk_can_do(peer_pk, pk_alg)) { | 2474 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message")); | 2475 | 0 | mbedtls_ssl_send_alert_message( | 2476 | 0 | ssl, | 2477 | 0 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2478 | 0 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); | 2479 | 0 | return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH; | 2480 | 0 | } | 2481 | | | 2482 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2483 | 282 | if (ssl->handshake->ecrs_enabled) { | 2484 | 158 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; | 2485 | 158 | } | 2486 | 282 | #endif | 2487 | | | 2488 | 282 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) | 2489 | 282 | if (pk_alg == MBEDTLS_PK_RSASSA_PSS) { | 2490 | 41 | mbedtls_pk_rsassa_pss_options rsassa_pss_options; | 2491 | 41 | rsassa_pss_options.mgf1_hash_id = md_alg; | 2492 | 41 | rsassa_pss_options.expected_salt_len = | 2493 | 41 | mbedtls_md_get_size_from_type(md_alg); | 2494 | 41 | if (rsassa_pss_options.expected_salt_len == 0) { | 2495 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2496 | 0 | } | 2497 | | | 2498 | 41 | ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options, | 2499 | 41 | peer_pk, | 2500 | 41 | md_alg, hash, hashlen, | 2501 | 41 | p, sig_len); | 2502 | 41 | } else | 2503 | 241 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | 2504 | 241 | ret = mbedtls_pk_verify_restartable(peer_pk, | 2505 | 241 | md_alg, hash, hashlen, p, sig_len, rs_ctx); | 2506 | | | 2507 | 282 | if (ret != 0) { | 2508 | 282 | int send_alert_msg = 1; | 2509 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2510 | 282 | send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS); | 2511 | 282 | #endif | 2512 | 282 | if (send_alert_msg) { | 2513 | 282 | mbedtls_ssl_send_alert_message( | 2514 | 282 | ssl, | 2515 | 282 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 2516 | 282 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); | 2517 | 282 | } | 2518 | 282 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret); | 2519 | 282 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2520 | 282 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { | 2521 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 2522 | 0 | } | 2523 | 282 | #endif | 2524 | 282 | return ret; | 2525 | 282 | } | 2526 | | | 2527 | | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 2528 | | /* We don't need the peer's public key anymore. Free it, | 2529 | | * so that more RAM is available for upcoming expensive | 2530 | | * operations like ECDHE. */ | 2531 | | mbedtls_pk_free(peer_pk); | 2532 | | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 2533 | 282 | } | 2534 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 2535 | | | 2536 | 0 | exit: | 2537 | 0 | mbedtls_ssl_handshake_increment_state(ssl); | 2538 | |
| 2539 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange")); | 2540 | |
| 2541 | 0 | return 0; | 2542 | 561 | } |
Unexecuted instantiation: ssl_tls12_client.c:ssl_parse_server_key_exchange |
2543 | | |
2544 | | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
2545 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2546 | | static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl) |
2547 | | { |
2548 | | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
2549 | | ssl->handshake->ciphersuite_info; |
2550 | | |
2551 | | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request")); |
2552 | | |
2553 | | if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) { |
2554 | | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request")); |
2555 | | mbedtls_ssl_handshake_increment_state(ssl); |
2556 | | return 0; |
2557 | | } |
2558 | | |
2559 | | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
2560 | | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2561 | | } |
2562 | | #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
2563 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2564 | | static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl) |
2565 | 1.18k | { |
2566 | 1.18k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2567 | 1.18k | unsigned char *buf; |
2568 | 1.18k | size_t n = 0; |
2569 | 1.18k | size_t cert_type_len = 0, dn_len = 0; |
2570 | 1.18k | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
2571 | 1.18k | ssl->handshake->ciphersuite_info; |
2572 | 1.18k | size_t sig_alg_len; |
2573 | 1.18k | #if defined(MBEDTLS_DEBUG_C) |
2574 | 1.18k | unsigned char *sig_alg; |
2575 | 1.18k | unsigned char *dn; |
2576 | 1.18k | #endif |
2577 | | |
2578 | 1.18k | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request")); |
2579 | | |
2580 | 1.18k | if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) { |
2581 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request")); |
2582 | 0 | mbedtls_ssl_handshake_increment_state(ssl); |
2583 | 0 | return 0; |
2584 | 0 | } |
2585 | | |
2586 | 1.18k | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
2587 | 13 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
2588 | 13 | return ret; |
2589 | 13 | } |
2590 | | |
2591 | 1.16k | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
2592 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2593 | 1 | mbedtls_ssl_send_alert_message( |
2594 | 1 | ssl, |
2595 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2596 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
2597 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
2598 | 1 | } |
2599 | | |
2600 | 1.16k | mbedtls_ssl_handshake_increment_state(ssl); |
2601 | 1.16k | ssl->handshake->client_auth = |
2602 | 1.16k | (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST); |
2603 | | |
2604 | 1.16k | MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request", |
2605 | 1.16k | ssl->handshake->client_auth ? "a" : "no")); |
2606 | | |
2607 | 1.16k | if (ssl->handshake->client_auth == 0) { |
2608 | | /* Current message is probably the ServerHelloDone */ |
2609 | 984 | ssl->keep_current_message = 1; |
2610 | 984 | goto exit; |
2611 | 984 | } |
2612 | | |
2613 | | /* |
2614 | | * struct { |
2615 | | * ClientCertificateType certificate_types<1..2^8-1>; |
2616 | | * SignatureAndHashAlgorithm |
2617 | | * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only |
2618 | | * DistinguishedName certificate_authorities<0..2^16-1>; |
2619 | | * } CertificateRequest; |
2620 | | * |
2621 | | * Since we only support a single certificate on clients, let's just |
2622 | | * ignore all the information that's supposed to help us pick a |
2623 | | * certificate. |
2624 | | * |
2625 | | * We could check that our certificate matches the request, and bail out |
2626 | | * if it doesn't, but it's simpler to just send the certificate anyway, |
2627 | | * and give the server the opportunity to decide if it should terminate |
2628 | | * the connection when it doesn't like our certificate. |
2629 | | * |
2630 | | * Same goes for the hash in TLS 1.2's signature_algorithms: at this |
2631 | | * point we only have one hash available (see comments in |
2632 | | * write_certificate_verify), so let's just use what we have. |
2633 | | * |
2634 | | * However, we still minimally parse the message to check it is at least |
2635 | | * superficially sane. |
2636 | | */ |
2637 | 183 | buf = ssl->in_msg; |
2638 | | |
2639 | | /* certificate_types */ |
2640 | 183 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1) { |
2641 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2642 | 1 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2643 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2644 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2645 | 1 | } |
2646 | 182 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)]; |
2647 | 182 | n = cert_type_len; |
2648 | | |
2649 | | /* |
2650 | | * The two-byte supported_signature_algorithms length is read next, |
2651 | | * at offset ...hdr_len + 1 + n. |
2652 | | */ |
2653 | 182 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1 + n + 2) { |
2654 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2655 | 1 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2656 | 1 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2657 | 1 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2658 | 1 | } |
2659 | | |
2660 | | /* supported_signature_algorithms */ |
2661 | 181 | sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n); |
2662 | | |
2663 | | /* |
2664 | | * The list is a sequence of two-byte values, so its length must be |
2665 | | * even. The message must have room for the list itself, plus the |
2666 | | * two-byte certificate_authorities length that follows it and is |
2667 | | * read below. |
2668 | | */ |
2669 | 181 | if (sig_alg_len % 2 != 0 || |
2670 | 180 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1 + n + 2 + sig_alg_len + 2) { |
2671 | 15 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2672 | 15 | mbedtls_ssl_send_alert_message( |
2673 | 15 | ssl, |
2674 | 15 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2675 | 15 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2676 | 15 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2677 | 15 | } |
2678 | | |
2679 | 166 | #if defined(MBEDTLS_DEBUG_C) |
2680 | 166 | sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n; |
2681 | 7.68k | for (size_t i = 0; i < sig_alg_len; i += 2) { |
2682 | 7.52k | MBEDTLS_SSL_DEBUG_MSG(3, |
2683 | 7.52k | ("Supported Signature Algorithm found: %02x %02x", |
2684 | 7.52k | sig_alg[i], sig_alg[i + 1])); |
2685 | 7.52k | } |
2686 | 166 | #endif |
2687 | | |
2688 | 166 | n += 2 + sig_alg_len; |
2689 | | |
2690 | | /* certificate_authorities */ |
2691 | 166 | dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n); |
2692 | | |
2693 | 166 | n += dn_len; |
2694 | 166 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) { |
2695 | 23 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2696 | 23 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2697 | 23 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2698 | 23 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2699 | 23 | } |
2700 | | |
2701 | 143 | #if defined(MBEDTLS_DEBUG_C) |
2702 | 143 | dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len; |
2703 | 265 | for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) { |
2704 | 150 | unsigned char *p = dn + i + 2; |
2705 | 150 | mbedtls_x509_name name; |
2706 | 150 | size_t asn1_len; |
2707 | 150 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
2708 | 150 | memset(&name, 0, sizeof(name)); |
2709 | 150 | dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0); |
2710 | 150 | if (dni_len > dn_len - i - 2 || |
2711 | 142 | mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len, |
2712 | 142 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 || |
2713 | 132 | mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) { |
2714 | 28 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message")); |
2715 | 28 | mbedtls_ssl_send_alert_message( |
2716 | 28 | ssl, |
2717 | 28 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2718 | 28 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2719 | 28 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2720 | 28 | } |
2721 | 122 | MBEDTLS_SSL_DEBUG_MSG(3, |
2722 | 122 | ("DN hint: %.*s", |
2723 | 122 | mbedtls_x509_dn_gets(s, sizeof(s), &name), s)); |
2724 | 122 | mbedtls_asn1_free_named_data_list_shallow(name.next); |
2725 | 122 | } |
2726 | 115 | #endif |
2727 | | |
2728 | 1.09k | exit: |
2729 | 1.09k | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request")); |
2730 | | |
2731 | 1.09k | return 0; |
2732 | 143 | } |
2733 | | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
2734 | | |
2735 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2736 | | static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl) |
2737 | 1.09k | { |
2738 | 1.09k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2739 | | |
2740 | 1.09k | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done")); |
2741 | | |
2742 | 1.09k | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
2743 | 111 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
2744 | 111 | return ret; |
2745 | 111 | } |
2746 | | |
2747 | 988 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
2748 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message")); |
2749 | 0 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
2750 | 0 | } |
2751 | | |
2752 | 988 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) || |
2753 | 977 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) { |
2754 | 17 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message")); |
2755 | 17 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
2756 | 17 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
2757 | 17 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
2758 | 17 | } |
2759 | | |
2760 | 971 | mbedtls_ssl_handshake_increment_state(ssl); |
2761 | | |
2762 | 971 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
2763 | 971 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
2764 | 971 | mbedtls_ssl_recv_flight_completed(ssl); |
2765 | 971 | } |
2766 | 971 | #endif |
2767 | | |
2768 | 971 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done")); |
2769 | | |
2770 | 971 | return 0; |
2771 | 988 | } |
2772 | | |
2773 | | MBEDTLS_CHECK_RETURN_CRITICAL |
2774 | | static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl) |
2775 | 971 | { |
2776 | 971 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2777 | | |
2778 | 971 | size_t header_len; |
2779 | 971 | size_t content_len; |
2780 | 971 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
2781 | 971 | ssl->handshake->ciphersuite_info; |
2782 | | |
2783 | 971 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange")); |
2784 | | |
2785 | 971 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
2786 | 971 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) { |
2787 | | /* |
2788 | | * DHM key exchange -- send G^X mod P |
2789 | | */ |
2790 | 0 | content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx); |
2791 | |
|
2792 | 0 | MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4); |
2793 | 0 | header_len = 6; |
2794 | |
|
2795 | 0 | ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx, |
2796 | 0 | (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx), |
2797 | 0 | &ssl->out_msg[header_len], content_len, |
2798 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); |
2799 | 0 | if (ret != 0) { |
2800 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret); |
2801 | 0 | return ret; |
2802 | 0 | } |
2803 | | |
2804 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X); |
2805 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX); |
2806 | |
|
2807 | 0 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
2808 | 0 | ssl->handshake->premaster, |
2809 | 0 | MBEDTLS_PREMASTER_SIZE, |
2810 | 0 | &ssl->handshake->pmslen, |
2811 | 0 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
2812 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
2813 | 0 | return ret; |
2814 | 0 | } |
2815 | | |
2816 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
2817 | 0 | } else |
2818 | 971 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
2819 | 971 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
2820 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
2821 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
2822 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
2823 | 971 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
2824 | 971 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
2825 | 971 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
2826 | 777 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) { |
2827 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
2828 | 0 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
2829 | 0 | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; |
2830 | | psa_key_attributes_t key_attributes; |
2831 | | |
2832 | | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
2833 | | |
2834 | | header_len = 4; |
2835 | | |
2836 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation.")); |
2837 | | |
2838 | | /* |
2839 | | * Generate EC private key for ECDHE exchange. |
2840 | | */ |
2841 | | |
2842 | | /* The master secret is obtained from the shared ECDH secret by |
2843 | | * applying the TLS 1.2 PRF with a specific salt and label. While |
2844 | | * the PSA Crypto API encourages combining key agreement schemes |
2845 | | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not |
2846 | | * yet support the provisioning of salt + label to the KDF. |
2847 | | * For the time being, we therefore need to split the computation |
2848 | | * of the ECDH secret and the application of the TLS 1.2 PRF. */ |
2849 | | key_attributes = psa_key_attributes_init(); |
2850 | 0 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
2851 | 0 | psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH); |
2852 | | psa_set_key_type(&key_attributes, handshake->xxdh_psa_type); |
2853 | | psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits); |
2854 | | |
2855 | | /* Generate ECDH private key. */ |
2856 | | status = psa_generate_key(&key_attributes, |
2857 | | &handshake->xxdh_psa_privkey); |
2858 | 0 | if (status != PSA_SUCCESS) { |
2859 | 0 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
2860 | 0 | } |
2861 | | |
2862 | | /* Export the public part of the ECDH private key from PSA. |
2863 | | * The export format is an ECPoint structure as expected by TLS, |
2864 | | * but we just need to add a length byte before that. */ |
2865 | 0 | unsigned char *own_pubkey = ssl->out_msg + header_len + 1; |
2866 | 0 | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
2867 | 0 | size_t own_pubkey_max_len = (size_t) (end - own_pubkey); |
2868 | 0 | size_t own_pubkey_len; |
2869 | |
|
2870 | 0 | status = psa_export_public_key(handshake->xxdh_psa_privkey, |
2871 | 0 | own_pubkey, own_pubkey_max_len, |
2872 | 0 | &own_pubkey_len); |
2873 | 0 | if (status != PSA_SUCCESS) { |
2874 | 0 | psa_destroy_key(handshake->xxdh_psa_privkey); |
2875 | 0 | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
2876 | 0 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
2877 | 0 | } |
2878 | | |
2879 | 0 | ssl->out_msg[header_len] = (unsigned char) own_pubkey_len; |
2880 | 0 | content_len = own_pubkey_len + 1; |
2881 | | |
2882 | | /* The ECDH secret is the premaster secret used for key derivation. */ |
2883 | | |
2884 | | /* Compute ECDH shared secret. */ |
2885 | 0 | status = psa_raw_key_agreement(PSA_ALG_ECDH, |
2886 | 0 | handshake->xxdh_psa_privkey, |
2887 | 0 | handshake->xxdh_psa_peerkey, |
2888 | 0 | handshake->xxdh_psa_peerkey_len, |
2889 | 0 | ssl->handshake->premaster, |
2890 | 0 | sizeof(ssl->handshake->premaster), |
2891 | 0 | &ssl->handshake->pmslen); |
2892 | |
|
2893 | 0 | destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey); |
2894 | 0 | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
2895 | |
|
2896 | 0 | if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) { |
2897 | 0 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
2898 | 0 | } |
2899 | | #else |
2900 | | /* |
2901 | | * ECDH key exchange -- send client public value |
2902 | | */ |
2903 | | header_len = 4; |
2904 | | |
2905 | | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2906 | 383 | if (ssl->handshake->ecrs_enabled) { |
2907 | 0 | if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) { |
2908 | 0 | goto ecdh_calc_secret; |
2909 | 0 | } |
2910 | | |
2911 | 0 | mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx); |
2912 | 0 | } |
2913 | 383 | #endif |
2914 | | |
2915 | 383 | ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx, |
2916 | 383 | &content_len, |
2917 | 383 | &ssl->out_msg[header_len], 1000, |
2918 | 383 | ssl->conf->f_rng, ssl->conf->p_rng); |
2919 | 383 | if (ret != 0) { |
2920 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret); |
2921 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2922 | 0 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
2923 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
2924 | 0 | } |
2925 | 0 | #endif |
2926 | 0 | return ret; |
2927 | 0 | } |
2928 | | |
2929 | 383 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
2930 | 383 | MBEDTLS_DEBUG_ECDH_Q); |
2931 | | |
2932 | 383 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2933 | 383 | if (ssl->handshake->ecrs_enabled) { |
2934 | 0 | ssl->handshake->ecrs_n = content_len; |
2935 | 0 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; |
2936 | 0 | } |
2937 | | |
2938 | 383 | ecdh_calc_secret: |
2939 | 383 | if (ssl->handshake->ecrs_enabled) { |
2940 | 0 | content_len = ssl->handshake->ecrs_n; |
2941 | 0 | } |
2942 | 383 | #endif |
2943 | 383 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, |
2944 | 383 | &ssl->handshake->pmslen, |
2945 | 383 | ssl->handshake->premaster, |
2946 | 383 | MBEDTLS_PREMASTER_SIZE, |
2947 | 383 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
2948 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
2949 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
2950 | 0 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
2951 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
2952 | 0 | } |
2953 | 0 | #endif |
2954 | 0 | return ret; |
2955 | 0 | } |
2956 | | |
2957 | 383 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
2958 | 383 | MBEDTLS_DEBUG_ECDH_Z); |
2959 | 383 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
2960 | 383 | } else |
2961 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
2962 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
2963 | | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
2964 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
2965 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
2966 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
2967 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
2968 | 0 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
2969 | 0 | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; |
2970 | 0 | psa_key_attributes_t key_attributes; |
2971 | | |
2972 | | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
2973 | | |
2974 | | /* |
2975 | | * opaque psk_identity<0..2^16-1>; |
2976 | | */ |
2977 | 0 | if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
2978 | | /* We don't offer PSK suites if we don't have a PSK, |
2979 | | * and we check that the server's choice is among the |
2980 | | * ciphersuites we offered, so this should never happen. */ |
2981 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
2982 | 0 | } |
2983 | | |
2984 | | /* uint16 to store content length */ |
2985 | 0 | const size_t content_len_size = 2; |
2986 | 0 | const size_t ecpoint_len_size = 1; |
2987 | 0 | const size_t ecpoint_max_len = |
2988 | 0 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(handshake->xxdh_psa_type, |
2989 | 0 | handshake->xxdh_psa_bits); |
2990 | |
|
2991 | 0 | header_len = 4; |
2992 | |
|
2993 | 0 | if (ecpoint_max_len > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size || |
2994 | 0 | header_len + content_len_size + ssl->conf->psk_identity_len |
2995 | 0 | > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size - ecpoint_max_len) { |
2996 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
2997 | 0 | ("psk identity too long or SSL buffer too short")); |
2998 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
2999 | 0 | } |
3000 | | |
3001 | 0 | unsigned char *p = ssl->out_msg + header_len; |
3002 | |
|
3003 | 0 | *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len); |
3004 | 0 | *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len); |
3005 | 0 | header_len += content_len_size; |
3006 | |
|
3007 | 0 | memcpy(p, ssl->conf->psk_identity, |
3008 | 0 | ssl->conf->psk_identity_len); |
3009 | 0 | p += ssl->conf->psk_identity_len; |
3010 | |
|
3011 | 0 | header_len += ssl->conf->psk_identity_len; |
3012 | |
|
3013 | 0 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation.")); |
3014 | | |
3015 | | /* |
3016 | | * Generate EC private key for ECDHE exchange. |
3017 | | */ |
3018 | | |
3019 | | /* The master secret is obtained from the shared ECDH secret by |
3020 | | * applying the TLS 1.2 PRF with a specific salt and label. While |
3021 | | * the PSA Crypto API encourages combining key agreement schemes |
3022 | | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not |
3023 | | * yet support the provisioning of salt + label to the KDF. |
3024 | | * For the time being, we therefore need to split the computation |
3025 | | * of the ECDH secret and the application of the TLS 1.2 PRF. */ |
3026 | 0 | key_attributes = psa_key_attributes_init(); |
3027 | 0 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
3028 | 0 | psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH); |
3029 | 0 | psa_set_key_type(&key_attributes, handshake->xxdh_psa_type); |
3030 | 0 | psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits); |
3031 | | |
3032 | | /* Generate ECDH private key. */ |
3033 | 0 | status = psa_generate_key(&key_attributes, |
3034 | 0 | &handshake->xxdh_psa_privkey); |
3035 | 0 | if (status != PSA_SUCCESS) { |
3036 | 0 | return PSA_TO_MBEDTLS_ERR(status); |
3037 | 0 | } |
3038 | | |
3039 | | /* Export the public part of the ECDH private key from PSA. |
3040 | | * The export format is an ECPoint structure as expected by TLS, |
3041 | | * but we just need to add a length byte before that. */ |
3042 | 0 | unsigned char *own_pubkey = p + 1; |
3043 | 0 | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
3044 | 0 | size_t own_pubkey_max_len = (size_t) (end - own_pubkey); |
3045 | 0 | size_t own_pubkey_len = 0; |
3046 | |
|
3047 | 0 | status = psa_export_public_key(handshake->xxdh_psa_privkey, |
3048 | 0 | own_pubkey, own_pubkey_max_len, |
3049 | 0 | &own_pubkey_len); |
3050 | 0 | if (status != PSA_SUCCESS) { |
3051 | 0 | psa_destroy_key(handshake->xxdh_psa_privkey); |
3052 | 0 | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
3053 | 0 | return PSA_TO_MBEDTLS_ERR(status); |
3054 | 0 | } |
3055 | | |
3056 | 0 | *p = (unsigned char) own_pubkey_len; |
3057 | 0 | content_len = own_pubkey_len + 1; |
3058 | | |
3059 | | /* As RFC 5489 section 2, the premaster secret is formed as follows: |
3060 | | * - a uint16 containing the length (in octets) of the ECDH computation |
3061 | | * - the octet string produced by the ECDH computation |
3062 | | * - a uint16 containing the length (in octets) of the PSK |
3063 | | * - the PSK itself |
3064 | | */ |
3065 | 0 | unsigned char *pms = ssl->handshake->premaster; |
3066 | 0 | const unsigned char * const pms_end = pms + |
3067 | 0 | sizeof(ssl->handshake->premaster); |
3068 | | /* uint16 to store length (in octets) of the ECDH computation */ |
3069 | 0 | const size_t zlen_size = 2; |
3070 | 0 | size_t zlen = 0; |
3071 | | |
3072 | | /* Perform ECDH computation after the uint16 reserved for the length */ |
3073 | 0 | status = psa_raw_key_agreement(PSA_ALG_ECDH, |
3074 | 0 | handshake->xxdh_psa_privkey, |
3075 | 0 | handshake->xxdh_psa_peerkey, |
3076 | 0 | handshake->xxdh_psa_peerkey_len, |
3077 | 0 | pms + zlen_size, |
3078 | 0 | pms_end - (pms + zlen_size), |
3079 | 0 | &zlen); |
3080 | |
|
3081 | 0 | destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey); |
3082 | 0 | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
3083 | |
|
3084 | 0 | if (status != PSA_SUCCESS) { |
3085 | 0 | return PSA_TO_MBEDTLS_ERR(status); |
3086 | 0 | } else if (destruction_status != PSA_SUCCESS) { |
3087 | 0 | return PSA_TO_MBEDTLS_ERR(destruction_status); |
3088 | 0 | } |
3089 | | |
3090 | | /* Write the ECDH computation length before the ECDH computation */ |
3091 | 0 | MBEDTLS_PUT_UINT16_BE(zlen, pms, 0); |
3092 | 0 | pms += zlen_size + zlen; |
3093 | 0 | } else |
3094 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
3095 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
3096 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
3097 | 588 | if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) { |
3098 | | /* |
3099 | | * opaque psk_identity<0..2^16-1>; |
3100 | | */ |
3101 | 0 | if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
3102 | | /* We don't offer PSK suites if we don't have a PSK, |
3103 | | * and we check that the server's choice is among the |
3104 | | * ciphersuites we offered, so this should never happen. */ |
3105 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
3106 | 0 | } |
3107 | | |
3108 | 0 | header_len = 4; |
3109 | 0 | content_len = ssl->conf->psk_identity_len; |
3110 | |
|
3111 | 0 | if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
3112 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
3113 | 0 | ("psk identity too long or SSL buffer too short")); |
3114 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
3115 | 0 | } |
3116 | | |
3117 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len); |
3118 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len); |
3119 | |
|
3120 | 0 | memcpy(ssl->out_msg + header_len, |
3121 | 0 | ssl->conf->psk_identity, |
3122 | 0 | ssl->conf->psk_identity_len); |
3123 | 0 | header_len += ssl->conf->psk_identity_len; |
3124 | |
|
3125 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
3126 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) { |
3127 | 0 | content_len = 0; |
3128 | 0 | } else |
3129 | 0 | #endif |
3130 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
3131 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
3132 | 0 | if ((ret = ssl_write_encrypted_pms(ssl, header_len, |
3133 | 0 | &content_len, 2)) != 0) { |
3134 | 0 | return ret; |
3135 | 0 | } |
3136 | 0 | } else |
3137 | 0 | #endif |
3138 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
3139 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
3140 | | /* |
3141 | | * ClientDiffieHellmanPublic public (DHM send G^X mod P) |
3142 | | */ |
3143 | 0 | content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx); |
3144 | |
|
3145 | 0 | if (header_len + 2 + content_len > |
3146 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN) { |
3147 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, |
3148 | 0 | ("psk identity or DHM size too long or SSL buffer too short")); |
3149 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
3150 | 0 | } |
3151 | | |
3152 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len); |
3153 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len); |
3154 | |
|
3155 | 0 | ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx, |
3156 | 0 | (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx), |
3157 | 0 | &ssl->out_msg[header_len], content_len, |
3158 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); |
3159 | 0 | if (ret != 0) { |
3160 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret); |
3161 | 0 | return ret; |
3162 | 0 | } |
3163 | | |
3164 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
3165 | 0 | unsigned char *pms = ssl->handshake->premaster; |
3166 | 0 | unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster); |
3167 | 0 | size_t pms_len; |
3168 | | |
3169 | | /* Write length only when we know the actual value */ |
3170 | 0 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
3171 | 0 | pms + 2, pms_end - (pms + 2), &pms_len, |
3172 | 0 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
3173 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
3174 | 0 | return ret; |
3175 | 0 | } |
3176 | 0 | MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0); |
3177 | 0 | pms += 2 + pms_len; |
3178 | |
|
3179 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
3180 | | #endif |
3181 | 0 | } else |
3182 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
3183 | | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
3184 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
3185 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
3186 | | /* |
3187 | | * ClientECDiffieHellmanPublic public; |
3188 | | */ |
3189 | 0 | ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx, |
3190 | 0 | &content_len, |
3191 | 0 | &ssl->out_msg[header_len], |
3192 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
3193 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); |
3194 | 0 | if (ret != 0) { |
3195 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret); |
3196 | 0 | return ret; |
3197 | 0 | } |
3198 | | |
3199 | 0 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
3200 | 0 | MBEDTLS_DEBUG_ECDH_Q); |
3201 | 0 | } else |
3202 | 0 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
3203 | 0 | { |
3204 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
3205 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
3206 | 0 | } |
3207 | | |
3208 | | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
3209 | 0 | if ((ret = mbedtls_ssl_psk_derive_premaster(ssl, |
3210 | 0 | (mbedtls_key_exchange_type_t) ciphersuite_info-> |
3211 | 0 | key_exchange)) != 0) { |
3212 | 0 | MBEDTLS_SSL_DEBUG_RET(1, |
3213 | 0 | "mbedtls_ssl_psk_derive_premaster", ret); |
3214 | 0 | return ret; |
3215 | 0 | } |
3216 | | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
3217 | 0 | } else |
3218 | 588 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
3219 | 588 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
3220 | 588 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) { |
3221 | 588 | header_len = 4; |
3222 | 588 | if ((ret = ssl_write_encrypted_pms(ssl, header_len, |
3223 | 588 | &content_len, 0)) != 0) { |
3224 | 3 | return ret; |
3225 | 3 | } |
3226 | 588 | } else |
3227 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
3228 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
3229 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
3230 | 0 | header_len = 4; |
3231 | |
|
3232 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
3233 | | unsigned char *out_p = ssl->out_msg + header_len; |
3234 | 0 | unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN - |
3235 | | header_len; |
3236 | | ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx, |
3237 | | out_p, end_p - out_p, &content_len, |
3238 | | MBEDTLS_ECJPAKE_ROUND_TWO); |
3239 | 0 | if (ret != 0) { |
3240 | 0 | psa_destroy_key(ssl->handshake->psa_pake_password); |
3241 | 0 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
3242 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret); |
3243 | 0 | return ret; |
3244 | 0 | } |
3245 | | #else |
3246 | | ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx, |
3247 | | ssl->out_msg + header_len, |
3248 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
3249 | | &content_len, |
3250 | | ssl->conf->f_rng, ssl->conf->p_rng); |
3251 | 0 | if (ret != 0) { |
3252 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret); |
3253 | 0 | return ret; |
3254 | 0 | } |
3255 | | |
3256 | 0 | ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx, |
3257 | 0 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
3258 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); |
3259 | 0 | if (ret != 0) { |
3260 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret); |
3261 | 0 | return ret; |
3262 | 0 | } |
3263 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
3264 | 0 | } else |
3265 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
3266 | 0 | { |
3267 | 0 | ((void) ciphersuite_info); |
3268 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
3269 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
3270 | 0 | } |
3271 | | |
3272 | 968 | ssl->out_msglen = header_len + content_len; |
3273 | 968 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
3274 | 968 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; |
3275 | | |
3276 | 968 | mbedtls_ssl_handshake_increment_state(ssl); |
3277 | | |
3278 | 968 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
3279 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
3280 | 0 | return ret; |
3281 | 0 | } |
3282 | | |
3283 | 968 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange")); |
3284 | | |
3285 | 968 | return 0; |
3286 | 968 | } ssl_tls12_client.c:ssl_write_client_key_exchange Line | Count | Source | 2775 | 971 | { | 2776 | 971 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 2777 | | | 2778 | 971 | size_t header_len; | 2779 | 971 | size_t content_len; | 2780 | 971 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 2781 | 971 | ssl->handshake->ciphersuite_info; | 2782 | | | 2783 | 971 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange")); | 2784 | | | 2785 | 971 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 2786 | 971 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) { | 2787 | | /* | 2788 | | * DHM key exchange -- send G^X mod P | 2789 | | */ | 2790 | 0 | content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx); | 2791 | |
| 2792 | 0 | MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4); | 2793 | 0 | header_len = 6; | 2794 | |
| 2795 | 0 | ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx, | 2796 | 0 | (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx), | 2797 | 0 | &ssl->out_msg[header_len], content_len, | 2798 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); | 2799 | 0 | if (ret != 0) { | 2800 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret); | 2801 | 0 | return ret; | 2802 | 0 | } | 2803 | | | 2804 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X); | 2805 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX); | 2806 | |
| 2807 | 0 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, | 2808 | 0 | ssl->handshake->premaster, | 2809 | 0 | MBEDTLS_PREMASTER_SIZE, | 2810 | 0 | &ssl->handshake->pmslen, | 2811 | 0 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { | 2812 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); | 2813 | 0 | return ret; | 2814 | 0 | } | 2815 | | | 2816 | 0 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); | 2817 | 0 | } else | 2818 | 971 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 2819 | 971 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ | 2820 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ | 2821 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 2822 | 971 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 2823 | 971 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 2824 | 971 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || | 2825 | 971 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 2826 | 777 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) { | 2827 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 2828 | | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 2829 | | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; | 2830 | | psa_key_attributes_t key_attributes; | 2831 | | | 2832 | | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 2833 | | | 2834 | | header_len = 4; | 2835 | | | 2836 | | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation.")); | 2837 | | | 2838 | | /* | 2839 | | * Generate EC private key for ECDHE exchange. | 2840 | | */ | 2841 | | | 2842 | | /* The master secret is obtained from the shared ECDH secret by | 2843 | | * applying the TLS 1.2 PRF with a specific salt and label. While | 2844 | | * the PSA Crypto API encourages combining key agreement schemes | 2845 | | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not | 2846 | | * yet support the provisioning of salt + label to the KDF. | 2847 | | * For the time being, we therefore need to split the computation | 2848 | | * of the ECDH secret and the application of the TLS 1.2 PRF. */ | 2849 | | key_attributes = psa_key_attributes_init(); | 2850 | | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); | 2851 | | psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH); | 2852 | | psa_set_key_type(&key_attributes, handshake->xxdh_psa_type); | 2853 | | psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits); | 2854 | | | 2855 | | /* Generate ECDH private key. */ | 2856 | | status = psa_generate_key(&key_attributes, | 2857 | | &handshake->xxdh_psa_privkey); | 2858 | | if (status != PSA_SUCCESS) { | 2859 | | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 2860 | | } | 2861 | | | 2862 | | /* Export the public part of the ECDH private key from PSA. | 2863 | | * The export format is an ECPoint structure as expected by TLS, | 2864 | | * but we just need to add a length byte before that. */ | 2865 | | unsigned char *own_pubkey = ssl->out_msg + header_len + 1; | 2866 | | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 2867 | | size_t own_pubkey_max_len = (size_t) (end - own_pubkey); | 2868 | | size_t own_pubkey_len; | 2869 | | | 2870 | | status = psa_export_public_key(handshake->xxdh_psa_privkey, | 2871 | | own_pubkey, own_pubkey_max_len, | 2872 | | &own_pubkey_len); | 2873 | | if (status != PSA_SUCCESS) { | 2874 | | psa_destroy_key(handshake->xxdh_psa_privkey); | 2875 | | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 2876 | | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 2877 | | } | 2878 | | | 2879 | | ssl->out_msg[header_len] = (unsigned char) own_pubkey_len; | 2880 | | content_len = own_pubkey_len + 1; | 2881 | | | 2882 | | /* The ECDH secret is the premaster secret used for key derivation. */ | 2883 | | | 2884 | | /* Compute ECDH shared secret. */ | 2885 | | status = psa_raw_key_agreement(PSA_ALG_ECDH, | 2886 | | handshake->xxdh_psa_privkey, | 2887 | | handshake->xxdh_psa_peerkey, | 2888 | | handshake->xxdh_psa_peerkey_len, | 2889 | | ssl->handshake->premaster, | 2890 | | sizeof(ssl->handshake->premaster), | 2891 | | &ssl->handshake->pmslen); | 2892 | | | 2893 | | destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey); | 2894 | | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 2895 | | | 2896 | | if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) { | 2897 | | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 2898 | | } | 2899 | | #else | 2900 | | /* | 2901 | | * ECDH key exchange -- send client public value | 2902 | | */ | 2903 | 383 | header_len = 4; | 2904 | | | 2905 | 383 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2906 | 383 | if (ssl->handshake->ecrs_enabled) { | 2907 | 0 | if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) { | 2908 | 0 | goto ecdh_calc_secret; | 2909 | 0 | } | 2910 | | | 2911 | 0 | mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx); | 2912 | 0 | } | 2913 | 383 | #endif | 2914 | | | 2915 | 383 | ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx, | 2916 | 383 | &content_len, | 2917 | 383 | &ssl->out_msg[header_len], 1000, | 2918 | 383 | ssl->conf->f_rng, ssl->conf->p_rng); | 2919 | 383 | if (ret != 0) { | 2920 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret); | 2921 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2922 | 0 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { | 2923 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 2924 | 0 | } | 2925 | 0 | #endif | 2926 | 0 | return ret; | 2927 | 0 | } | 2928 | | | 2929 | 383 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, | 2930 | 383 | MBEDTLS_DEBUG_ECDH_Q); | 2931 | | | 2932 | 383 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2933 | 383 | if (ssl->handshake->ecrs_enabled) { | 2934 | 0 | ssl->handshake->ecrs_n = content_len; | 2935 | 0 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; | 2936 | 0 | } | 2937 | | | 2938 | 383 | ecdh_calc_secret: | 2939 | 383 | if (ssl->handshake->ecrs_enabled) { | 2940 | 0 | content_len = ssl->handshake->ecrs_n; | 2941 | 0 | } | 2942 | 383 | #endif | 2943 | 383 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, | 2944 | 383 | &ssl->handshake->pmslen, | 2945 | 383 | ssl->handshake->premaster, | 2946 | 383 | MBEDTLS_PREMASTER_SIZE, | 2947 | 383 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { | 2948 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); | 2949 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 2950 | 0 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { | 2951 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 2952 | 0 | } | 2953 | 0 | #endif | 2954 | 0 | return ret; | 2955 | 0 | } | 2956 | | | 2957 | 383 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, | 2958 | 383 | MBEDTLS_DEBUG_ECDH_Z); | 2959 | 383 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 2960 | 383 | } else | 2961 | 588 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 2962 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 2963 | | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 2964 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 2965 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ | 2966 | | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 2967 | | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { | 2968 | | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 2969 | | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; | 2970 | | psa_key_attributes_t key_attributes; | 2971 | | | 2972 | | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 2973 | | | 2974 | | /* | 2975 | | * opaque psk_identity<0..2^16-1>; | 2976 | | */ | 2977 | | if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { | 2978 | | /* We don't offer PSK suites if we don't have a PSK, | 2979 | | * and we check that the server's choice is among the | 2980 | | * ciphersuites we offered, so this should never happen. */ | 2981 | | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 2982 | | } | 2983 | | | 2984 | | /* uint16 to store content length */ | 2985 | | const size_t content_len_size = 2; | 2986 | | const size_t ecpoint_len_size = 1; | 2987 | | const size_t ecpoint_max_len = | 2988 | | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(handshake->xxdh_psa_type, | 2989 | | handshake->xxdh_psa_bits); | 2990 | | | 2991 | | header_len = 4; | 2992 | | | 2993 | | if (ecpoint_max_len > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size || | 2994 | | header_len + content_len_size + ssl->conf->psk_identity_len | 2995 | | > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size - ecpoint_max_len) { | 2996 | | MBEDTLS_SSL_DEBUG_MSG(1, | 2997 | | ("psk identity too long or SSL buffer too short")); | 2998 | | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 2999 | | } | 3000 | | | 3001 | | unsigned char *p = ssl->out_msg + header_len; | 3002 | | | 3003 | | *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len); | 3004 | | *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len); | 3005 | | header_len += content_len_size; | 3006 | | | 3007 | | memcpy(p, ssl->conf->psk_identity, | 3008 | | ssl->conf->psk_identity_len); | 3009 | | p += ssl->conf->psk_identity_len; | 3010 | | | 3011 | | header_len += ssl->conf->psk_identity_len; | 3012 | | | 3013 | | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation.")); | 3014 | | | 3015 | | /* | 3016 | | * Generate EC private key for ECDHE exchange. | 3017 | | */ | 3018 | | | 3019 | | /* The master secret is obtained from the shared ECDH secret by | 3020 | | * applying the TLS 1.2 PRF with a specific salt and label. While | 3021 | | * the PSA Crypto API encourages combining key agreement schemes | 3022 | | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not | 3023 | | * yet support the provisioning of salt + label to the KDF. | 3024 | | * For the time being, we therefore need to split the computation | 3025 | | * of the ECDH secret and the application of the TLS 1.2 PRF. */ | 3026 | | key_attributes = psa_key_attributes_init(); | 3027 | | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); | 3028 | | psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH); | 3029 | | psa_set_key_type(&key_attributes, handshake->xxdh_psa_type); | 3030 | | psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits); | 3031 | | | 3032 | | /* Generate ECDH private key. */ | 3033 | | status = psa_generate_key(&key_attributes, | 3034 | | &handshake->xxdh_psa_privkey); | 3035 | | if (status != PSA_SUCCESS) { | 3036 | | return PSA_TO_MBEDTLS_ERR(status); | 3037 | | } | 3038 | | | 3039 | | /* Export the public part of the ECDH private key from PSA. | 3040 | | * The export format is an ECPoint structure as expected by TLS, | 3041 | | * but we just need to add a length byte before that. */ | 3042 | | unsigned char *own_pubkey = p + 1; | 3043 | | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 3044 | | size_t own_pubkey_max_len = (size_t) (end - own_pubkey); | 3045 | | size_t own_pubkey_len = 0; | 3046 | | | 3047 | | status = psa_export_public_key(handshake->xxdh_psa_privkey, | 3048 | | own_pubkey, own_pubkey_max_len, | 3049 | | &own_pubkey_len); | 3050 | | if (status != PSA_SUCCESS) { | 3051 | | psa_destroy_key(handshake->xxdh_psa_privkey); | 3052 | | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 3053 | | return PSA_TO_MBEDTLS_ERR(status); | 3054 | | } | 3055 | | | 3056 | | *p = (unsigned char) own_pubkey_len; | 3057 | | content_len = own_pubkey_len + 1; | 3058 | | | 3059 | | /* As RFC 5489 section 2, the premaster secret is formed as follows: | 3060 | | * - a uint16 containing the length (in octets) of the ECDH computation | 3061 | | * - the octet string produced by the ECDH computation | 3062 | | * - a uint16 containing the length (in octets) of the PSK | 3063 | | * - the PSK itself | 3064 | | */ | 3065 | | unsigned char *pms = ssl->handshake->premaster; | 3066 | | const unsigned char * const pms_end = pms + | 3067 | | sizeof(ssl->handshake->premaster); | 3068 | | /* uint16 to store length (in octets) of the ECDH computation */ | 3069 | | const size_t zlen_size = 2; | 3070 | | size_t zlen = 0; | 3071 | | | 3072 | | /* Perform ECDH computation after the uint16 reserved for the length */ | 3073 | | status = psa_raw_key_agreement(PSA_ALG_ECDH, | 3074 | | handshake->xxdh_psa_privkey, | 3075 | | handshake->xxdh_psa_peerkey, | 3076 | | handshake->xxdh_psa_peerkey_len, | 3077 | | pms + zlen_size, | 3078 | | pms_end - (pms + zlen_size), | 3079 | | &zlen); | 3080 | | | 3081 | | destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey); | 3082 | | handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 3083 | | | 3084 | | if (status != PSA_SUCCESS) { | 3085 | | return PSA_TO_MBEDTLS_ERR(status); | 3086 | | } else if (destruction_status != PSA_SUCCESS) { | 3087 | | return PSA_TO_MBEDTLS_ERR(destruction_status); | 3088 | | } | 3089 | | | 3090 | | /* Write the ECDH computation length before the ECDH computation */ | 3091 | | MBEDTLS_PUT_UINT16_BE(zlen, pms, 0); | 3092 | | pms += zlen_size + zlen; | 3093 | | } else | 3094 | | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 3095 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 3096 | 588 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 3097 | 588 | if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) { | 3098 | | /* | 3099 | | * opaque psk_identity<0..2^16-1>; | 3100 | | */ | 3101 | 0 | if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { | 3102 | | /* We don't offer PSK suites if we don't have a PSK, | 3103 | | * and we check that the server's choice is among the | 3104 | | * ciphersuites we offered, so this should never happen. */ | 3105 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 3106 | 0 | } | 3107 | | | 3108 | 0 | header_len = 4; | 3109 | 0 | content_len = ssl->conf->psk_identity_len; | 3110 | |
| 3111 | 0 | if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { | 3112 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, | 3113 | 0 | ("psk identity too long or SSL buffer too short")); | 3114 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 3115 | 0 | } | 3116 | | | 3117 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len); | 3118 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len); | 3119 | |
| 3120 | 0 | memcpy(ssl->out_msg + header_len, | 3121 | 0 | ssl->conf->psk_identity, | 3122 | 0 | ssl->conf->psk_identity_len); | 3123 | 0 | header_len += ssl->conf->psk_identity_len; | 3124 | |
| 3125 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 3126 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) { | 3127 | 0 | content_len = 0; | 3128 | 0 | } else | 3129 | 0 | #endif | 3130 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 3131 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { | 3132 | 0 | if ((ret = ssl_write_encrypted_pms(ssl, header_len, | 3133 | 0 | &content_len, 2)) != 0) { | 3134 | 0 | return ret; | 3135 | 0 | } | 3136 | 0 | } else | 3137 | 0 | #endif | 3138 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 3139 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { | 3140 | | /* | 3141 | | * ClientDiffieHellmanPublic public (DHM send G^X mod P) | 3142 | | */ | 3143 | 0 | content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx); | 3144 | |
| 3145 | 0 | if (header_len + 2 + content_len > | 3146 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN) { | 3147 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, | 3148 | 0 | ("psk identity or DHM size too long or SSL buffer too short")); | 3149 | 0 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 3150 | 0 | } | 3151 | | | 3152 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len); | 3153 | 0 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len); | 3154 | |
| 3155 | 0 | ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx, | 3156 | 0 | (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx), | 3157 | 0 | &ssl->out_msg[header_len], content_len, | 3158 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); | 3159 | 0 | if (ret != 0) { | 3160 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret); | 3161 | 0 | return ret; | 3162 | 0 | } | 3163 | |
| 3164 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 3165 | | unsigned char *pms = ssl->handshake->premaster; | 3166 | | unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster); | 3167 | | size_t pms_len; | 3168 | | | 3169 | | /* Write length only when we know the actual value */ | 3170 | | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, | 3171 | | pms + 2, pms_end - (pms + 2), &pms_len, | 3172 | | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { | 3173 | | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); | 3174 | | return ret; | 3175 | | } | 3176 | | MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0); | 3177 | | pms += 2 + pms_len; | 3178 | | | 3179 | | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); | 3180 | | #endif | 3181 | 0 | } else | 3182 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 3183 | 0 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ | 3184 | 0 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 3185 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { | 3186 | | /* | 3187 | | * ClientECDiffieHellmanPublic public; | 3188 | | */ | 3189 | 0 | ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx, | 3190 | 0 | &content_len, | 3191 | 0 | &ssl->out_msg[header_len], | 3192 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 3193 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); | 3194 | 0 | if (ret != 0) { | 3195 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret); | 3196 | 0 | return ret; | 3197 | 0 | } | 3198 | | | 3199 | 0 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, | 3200 | 0 | MBEDTLS_DEBUG_ECDH_Q); | 3201 | 0 | } else | 3202 | 0 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 3203 | 0 | { | 3204 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 3205 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 3206 | 0 | } | 3207 | | | 3208 | 0 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) | 3209 | 0 | if ((ret = mbedtls_ssl_psk_derive_premaster(ssl, | 3210 | 0 | (mbedtls_key_exchange_type_t) ciphersuite_info-> | 3211 | 0 | key_exchange)) != 0) { | 3212 | 0 | MBEDTLS_SSL_DEBUG_RET(1, | 3213 | 0 | "mbedtls_ssl_psk_derive_premaster", ret); | 3214 | 0 | return ret; | 3215 | 0 | } | 3216 | 0 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 3217 | 0 | } else | 3218 | 588 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 3219 | 588 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 3220 | 588 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) { | 3221 | 588 | header_len = 4; | 3222 | 588 | if ((ret = ssl_write_encrypted_pms(ssl, header_len, | 3223 | 588 | &content_len, 0)) != 0) { | 3224 | 3 | return ret; | 3225 | 3 | } | 3226 | 588 | } else | 3227 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 3228 | 0 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 3229 | 0 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { | 3230 | 0 | header_len = 4; | 3231 | |
| 3232 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 3233 | | unsigned char *out_p = ssl->out_msg + header_len; | 3234 | | unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN - | 3235 | | header_len; | 3236 | | ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx, | 3237 | | out_p, end_p - out_p, &content_len, | 3238 | | MBEDTLS_ECJPAKE_ROUND_TWO); | 3239 | | if (ret != 0) { | 3240 | | psa_destroy_key(ssl->handshake->psa_pake_password); | 3241 | | psa_pake_abort(&ssl->handshake->psa_pake_ctx); | 3242 | | MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret); | 3243 | | return ret; | 3244 | | } | 3245 | | #else | 3246 | 0 | ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx, | 3247 | 0 | ssl->out_msg + header_len, | 3248 | 0 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 3249 | 0 | &content_len, | 3250 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); | 3251 | 0 | if (ret != 0) { | 3252 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret); | 3253 | 0 | return ret; | 3254 | 0 | } | 3255 | | | 3256 | 0 | ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx, | 3257 | 0 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 3258 | 0 | ssl->conf->f_rng, ssl->conf->p_rng); | 3259 | 0 | if (ret != 0) { | 3260 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret); | 3261 | 0 | return ret; | 3262 | 0 | } | 3263 | 0 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 3264 | 0 | } else | 3265 | 0 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 3266 | 0 | { | 3267 | 0 | ((void) ciphersuite_info); | 3268 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 3269 | 0 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 3270 | 0 | } | 3271 | | | 3272 | 968 | ssl->out_msglen = header_len + content_len; | 3273 | 968 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 3274 | 968 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; | 3275 | | | 3276 | 968 | mbedtls_ssl_handshake_increment_state(ssl); | 3277 | | | 3278 | 968 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { | 3279 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); | 3280 | 0 | return ret; | 3281 | 0 | } | 3282 | | | 3283 | 968 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange")); | 3284 | | | 3285 | 968 | return 0; | 3286 | 968 | } |
Unexecuted instantiation: ssl_tls12_client.c:ssl_write_client_key_exchange |
3287 | | |
3288 | | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
3289 | | MBEDTLS_CHECK_RETURN_CRITICAL |
3290 | | static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl) |
3291 | | { |
3292 | | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
3293 | | ssl->handshake->ciphersuite_info; |
3294 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
3295 | | |
3296 | | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify")); |
3297 | | |
3298 | | if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) { |
3299 | | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret); |
3300 | | return ret; |
3301 | | } |
3302 | | |
3303 | | if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) { |
3304 | | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify")); |
3305 | | mbedtls_ssl_handshake_increment_state(ssl); |
3306 | | return 0; |
3307 | | } |
3308 | | |
3309 | | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
3310 | | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
3311 | | } |
3312 | | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
3313 | | MBEDTLS_CHECK_RETURN_CRITICAL |
3314 | | static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl) |
3315 | 968 | { |
3316 | 968 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
3317 | 968 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
3318 | 968 | ssl->handshake->ciphersuite_info; |
3319 | 968 | size_t n = 0, offset = 0; |
3320 | 968 | unsigned char hash[48]; |
3321 | 968 | unsigned char *hash_start = hash; |
3322 | 968 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
3323 | 968 | size_t hashlen; |
3324 | 968 | void *rs_ctx = NULL; |
3325 | 968 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
3326 | 968 | size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf); |
3327 | | #else |
3328 | | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf); |
3329 | | #endif |
3330 | | |
3331 | 968 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify")); |
3332 | | |
3333 | 968 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
3334 | 968 | if (ssl->handshake->ecrs_enabled && |
3335 | 0 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) { |
3336 | 0 | goto sign; |
3337 | 0 | } |
3338 | 968 | #endif |
3339 | | |
3340 | 968 | if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) { |
3341 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret); |
3342 | 0 | return ret; |
3343 | 0 | } |
3344 | | |
3345 | 968 | if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) { |
3346 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify")); |
3347 | 0 | mbedtls_ssl_handshake_increment_state(ssl); |
3348 | 0 | return 0; |
3349 | 0 | } |
3350 | | |
3351 | 968 | if (ssl->handshake->client_auth == 0 || |
3352 | 968 | mbedtls_ssl_own_cert(ssl) == NULL) { |
3353 | 968 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify")); |
3354 | 968 | mbedtls_ssl_handshake_increment_state(ssl); |
3355 | 968 | return 0; |
3356 | 968 | } |
3357 | | |
3358 | 0 | if (mbedtls_ssl_own_key(ssl) == NULL) { |
3359 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate")); |
3360 | 0 | return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED; |
3361 | 0 | } |
3362 | | |
3363 | | /* |
3364 | | * Make a signature of the handshake digests |
3365 | | */ |
3366 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
3367 | 0 | if (ssl->handshake->ecrs_enabled) { |
3368 | 0 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; |
3369 | 0 | } |
3370 | |
|
3371 | 0 | sign: |
3372 | 0 | #endif |
3373 | |
|
3374 | 0 | ret = ssl->handshake->calc_verify(ssl, hash, &hashlen); |
3375 | 0 | if (0 != ret) { |
3376 | 0 | MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret); |
3377 | 0 | return ret; |
3378 | 0 | } |
3379 | | |
3380 | | /* |
3381 | | * digitally-signed struct { |
3382 | | * opaque handshake_messages[handshake_messages_length]; |
3383 | | * }; |
3384 | | * |
3385 | | * Taking shortcut here. We assume that the server always allows the |
3386 | | * PRF Hash function and has sent it in the allowed signature |
3387 | | * algorithms list received in the Certificate Request message. |
3388 | | * |
3389 | | * Until we encounter a server that does not, we will take this |
3390 | | * shortcut. |
3391 | | * |
3392 | | * Reason: Otherwise we should have running hashes for SHA512 and |
3393 | | * SHA224 in order to satisfy 'weird' needs from the server |
3394 | | * side. |
3395 | | */ |
3396 | 0 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
3397 | 0 | md_alg = MBEDTLS_MD_SHA384; |
3398 | 0 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; |
3399 | 0 | } else { |
3400 | 0 | md_alg = MBEDTLS_MD_SHA256; |
3401 | 0 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; |
3402 | 0 | } |
3403 | 0 | ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl)); |
3404 | | |
3405 | | /* Info from md_alg will be used instead */ |
3406 | 0 | hashlen = 0; |
3407 | 0 | offset = 2; |
3408 | |
|
3409 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
3410 | 0 | if (ssl->handshake->ecrs_enabled) { |
3411 | 0 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
3412 | 0 | } |
3413 | 0 | #endif |
3414 | |
|
3415 | 0 | if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl), |
3416 | 0 | md_alg, hash_start, hashlen, |
3417 | 0 | ssl->out_msg + 6 + offset, |
3418 | 0 | out_buf_len - 6 - offset, |
3419 | 0 | &n, |
3420 | 0 | ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) { |
3421 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret); |
3422 | 0 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
3423 | 0 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
3424 | 0 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
3425 | 0 | } |
3426 | 0 | #endif |
3427 | 0 | return ret; |
3428 | 0 | } |
3429 | | |
3430 | 0 | MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4); |
3431 | |
|
3432 | 0 | ssl->out_msglen = 6 + n + offset; |
3433 | 0 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
3434 | 0 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; |
3435 | |
|
3436 | 0 | mbedtls_ssl_handshake_increment_state(ssl); |
3437 | |
|
3438 | 0 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
3439 | 0 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
3440 | 0 | return ret; |
3441 | 0 | } |
3442 | | |
3443 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify")); |
3444 | |
|
3445 | 0 | return ret; |
3446 | 0 | } |
3447 | | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
3448 | | |
3449 | | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
3450 | | MBEDTLS_CHECK_RETURN_CRITICAL |
3451 | | static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl) |
3452 | 183 | { |
3453 | 183 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
3454 | 183 | uint32_t lifetime; |
3455 | 183 | size_t ticket_len; |
3456 | 183 | unsigned char *ticket; |
3457 | 183 | const unsigned char *msg; |
3458 | | |
3459 | 183 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket")); |
3460 | | |
3461 | 183 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
3462 | 161 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
3463 | 161 | return ret; |
3464 | 161 | } |
3465 | | |
3466 | 22 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
3467 | 1 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message")); |
3468 | 1 | mbedtls_ssl_send_alert_message( |
3469 | 1 | ssl, |
3470 | 1 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
3471 | 1 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
3472 | 1 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
3473 | 1 | } |
3474 | | |
3475 | | /* |
3476 | | * struct { |
3477 | | * uint32 ticket_lifetime_hint; |
3478 | | * opaque ticket<0..2^16-1>; |
3479 | | * } NewSessionTicket; |
3480 | | * |
3481 | | * 0 . 3 ticket_lifetime_hint |
3482 | | * 4 . 5 ticket_len (n) |
3483 | | * 6 . 5+n ticket content |
3484 | | */ |
3485 | 21 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || |
3486 | 14 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) { |
3487 | 8 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message")); |
3488 | 8 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
3489 | 8 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
3490 | 8 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
3491 | 8 | } |
3492 | | |
3493 | 13 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl); |
3494 | | |
3495 | 13 | lifetime = MBEDTLS_GET_UINT32_BE(msg, 0); |
3496 | | |
3497 | 13 | ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4); |
3498 | | |
3499 | 13 | if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) { |
3500 | 12 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message")); |
3501 | 12 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
3502 | 12 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
3503 | 12 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
3504 | 12 | } |
3505 | | |
3506 | 1 | MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len)); |
3507 | | |
3508 | | /* We're not waiting for a NewSessionTicket message any more */ |
3509 | 1 | ssl->handshake->new_session_ticket = 0; |
3510 | 1 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC); |
3511 | | |
3512 | | /* |
3513 | | * Zero-length ticket means the server changed his mind and doesn't want |
3514 | | * to send a ticket after all, so just forget it |
3515 | | */ |
3516 | 1 | if (ticket_len == 0) { |
3517 | 0 | return 0; |
3518 | 0 | } |
3519 | | |
3520 | 1 | if (ssl->session != NULL && ssl->session->ticket != NULL) { |
3521 | 0 | mbedtls_zeroize_and_free(ssl->session->ticket, |
3522 | 0 | ssl->session->ticket_len); |
3523 | 0 | ssl->session->ticket = NULL; |
3524 | 0 | ssl->session->ticket_len = 0; |
3525 | 0 | } |
3526 | | |
3527 | 1 | mbedtls_zeroize_and_free(ssl->session_negotiate->ticket, |
3528 | 1 | ssl->session_negotiate->ticket_len); |
3529 | 1 | ssl->session_negotiate->ticket = NULL; |
3530 | 1 | ssl->session_negotiate->ticket_len = 0; |
3531 | | |
3532 | 1 | if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) { |
3533 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed")); |
3534 | 0 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
3535 | 0 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
3536 | 0 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
3537 | 0 | } |
3538 | | |
3539 | 1 | memcpy(ticket, msg + 6, ticket_len); |
3540 | | |
3541 | 1 | ssl->session_negotiate->ticket = ticket; |
3542 | 1 | ssl->session_negotiate->ticket_len = ticket_len; |
3543 | 1 | ssl->session_negotiate->ticket_lifetime = lifetime; |
3544 | | |
3545 | | /* |
3546 | | * RFC 5077 section 3.4: |
3547 | | * "If the client receives a session ticket from the server, then it |
3548 | | * discards any Session ID that was sent in the ServerHello." |
3549 | | */ |
3550 | 1 | MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id")); |
3551 | 1 | ssl->session_negotiate->id_len = 0; |
3552 | | |
3553 | 1 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket")); |
3554 | | |
3555 | 1 | return 0; |
3556 | 1 | } |
3557 | | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
3558 | | |
3559 | | /* |
3560 | | * SSL handshake -- client side -- single step |
3561 | | */ |
3562 | | int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl) |
3563 | 20.1k | { |
3564 | 20.1k | int ret = 0; |
3565 | | |
3566 | | /* Change state now, so that it is right in mbedtls_ssl_read_record(), used |
3567 | | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ |
3568 | 20.1k | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
3569 | 20.1k | if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && |
3570 | 969 | ssl->handshake->new_session_ticket != 0) { |
3571 | 183 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET); |
3572 | 183 | } |
3573 | 20.1k | #endif |
3574 | | |
3575 | 20.1k | switch (ssl->state) { |
3576 | 0 | case MBEDTLS_SSL_HELLO_REQUEST: |
3577 | 0 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); |
3578 | 0 | break; |
3579 | | |
3580 | | /* |
3581 | | * ==> ClientHello |
3582 | | */ |
3583 | 0 | case MBEDTLS_SSL_CLIENT_HELLO: |
3584 | 0 | ret = mbedtls_ssl_write_client_hello(ssl); |
3585 | 0 | break; |
3586 | | |
3587 | | /* |
3588 | | * <== ServerHello |
3589 | | * Certificate |
3590 | | * ( ServerKeyExchange ) |
3591 | | * ( CertificateRequest ) |
3592 | | * ServerHelloDone |
3593 | | */ |
3594 | 5.84k | case MBEDTLS_SSL_SERVER_HELLO: |
3595 | 5.84k | ret = ssl_parse_server_hello(ssl); |
3596 | 5.84k | break; |
3597 | | |
3598 | 3.87k | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
3599 | 3.87k | ret = mbedtls_ssl_parse_certificate(ssl); |
3600 | 3.87k | break; |
3601 | | |
3602 | 1.92k | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
3603 | 1.92k | ret = ssl_parse_server_key_exchange(ssl); |
3604 | 1.92k | break; |
3605 | | |
3606 | 1.18k | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
3607 | 1.18k | ret = ssl_parse_certificate_request(ssl); |
3608 | 1.18k | break; |
3609 | | |
3610 | 1.09k | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
3611 | 1.09k | ret = ssl_parse_server_hello_done(ssl); |
3612 | 1.09k | break; |
3613 | | |
3614 | | /* |
3615 | | * ==> ( Certificate/Alert ) |
3616 | | * ClientKeyExchange |
3617 | | * ( CertificateVerify ) |
3618 | | * ChangeCipherSpec |
3619 | | * Finished |
3620 | | */ |
3621 | 971 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
3622 | 971 | ret = mbedtls_ssl_write_certificate(ssl); |
3623 | 971 | break; |
3624 | | |
3625 | 971 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
3626 | 971 | ret = ssl_write_client_key_exchange(ssl); |
3627 | 971 | break; |
3628 | | |
3629 | 968 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
3630 | 968 | ret = ssl_write_certificate_verify(ssl); |
3631 | 968 | break; |
3632 | | |
3633 | 968 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
3634 | 968 | ret = mbedtls_ssl_write_change_cipher_spec(ssl); |
3635 | 968 | break; |
3636 | | |
3637 | 968 | case MBEDTLS_SSL_CLIENT_FINISHED: |
3638 | 968 | ret = mbedtls_ssl_write_finished(ssl); |
3639 | 968 | break; |
3640 | | |
3641 | | /* |
3642 | | * <== ( NewSessionTicket ) |
3643 | | * ChangeCipherSpec |
3644 | | * Finished |
3645 | | */ |
3646 | 0 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
3647 | 183 | case MBEDTLS_SSL_NEW_SESSION_TICKET: |
3648 | 183 | ret = ssl_parse_new_session_ticket(ssl); |
3649 | 183 | break; |
3650 | 0 | #endif |
3651 | | |
3652 | 786 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
3653 | 786 | ret = mbedtls_ssl_parse_change_cipher_spec(ssl); |
3654 | 786 | break; |
3655 | | |
3656 | 405 | case MBEDTLS_SSL_SERVER_FINISHED: |
3657 | 405 | ret = mbedtls_ssl_parse_finished(ssl); |
3658 | 405 | break; |
3659 | | |
3660 | 0 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
3661 | 0 | MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done")); |
3662 | 0 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); |
3663 | 0 | break; |
3664 | | |
3665 | 0 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
3666 | 0 | mbedtls_ssl_handshake_wrapup(ssl); |
3667 | 0 | break; |
3668 | | |
3669 | 0 | default: |
3670 | 0 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state)); |
3671 | 0 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
3672 | 20.1k | } |
3673 | | |
3674 | 20.1k | return ret; |
3675 | 20.1k | } |
3676 | | |
3677 | | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */ |