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