scorpiox-bot-plugin-webhook
←ssl_tls12_client.cc
3688 lines · 132 KBHEADBlameDownload
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 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 3);
2329
2330 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
2331 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
2332 MBEDTLS_ECP_DP_SECP256R1);
2333
2334 if (exp_tls_id == 0) {
2335 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2336 }
2337
2338 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2339 (read_tls_id != exp_tls_id)) {
2340 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2341 }
2342
2343 p += 3;
2344
2345 if ((ret = mbedtls_psa_ecjpake_read_round(
2346 &ssl->handshake->psa_pake_ctx, p, end - p,
2347 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2348 psa_destroy_key(ssl->handshake->psa_pake_password);
2349 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2350
2351 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
2352 mbedtls_ssl_send_alert_message(
2353 ssl,
2354 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2355 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2356 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
2357 }
2358#else
2359 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2360 p, end - p);
2361 if (ret != 0) {
2362 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
2363 mbedtls_ssl_send_alert_message(
2364 ssl,
2365 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2366 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2367 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
2368 }
2369#endif /* MBEDTLS_USE_PSA_CRYPTO */
2370 } else
2371#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2372 {
2373 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2374 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2375 }
2376
2377#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2378 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
2379 size_t sig_len, hashlen;
2380 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2381
2382 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2383 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2384 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
2385 size_t params_len = (size_t) (p - params);
2386 void *rs_ctx = NULL;
2387
2388 mbedtls_pk_context *peer_pk;
2389
2390#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2391 peer_pk = &ssl->handshake->peer_pubkey;
2392#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2393 if (ssl->session_negotiate->peer_cert == NULL) {
2394 /* Should never happen */
2395 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2396 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2397 }
2398 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2399#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2400
2401 /*
2402 * Handle the digitally-signed structure
2403 */
2404 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2405 uint16_t sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2406 if (ssl_parse_signature_algorithm(ssl, sig_alg, &md_alg, &pk_alg) != 0) {
2407 MBEDTLS_SSL_DEBUG_MSG(1,
2408 ("bad server key exchange message"));
2409 mbedtls_ssl_send_alert_message(
2410 ssl,
2411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2412 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2413 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2414 }
2415 p += 2;
2416
2417 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2418 MBEDTLS_SSL_DEBUG_MSG(1,
2419 ("bad server key exchange message"));
2420 mbedtls_ssl_send_alert_message(
2421 ssl,
2422 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2423 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2424 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2425 }
2426
2427 /*
2428 * Read signature
2429 */
2430
2431 if (p > end - 2) {
2432 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
2433 mbedtls_ssl_send_alert_message(
2434 ssl,
2435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2436 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2437 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2438 }
2439 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
2440 p += 2;
2441
2442 if (p != end - sig_len) {
2443 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
2444 mbedtls_ssl_send_alert_message(
2445 ssl,
2446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2447 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2448 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2449 }
2450
2451 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
2452
2453 /*
2454 * Compute the hash that has been signed
2455 */
2456 if (md_alg != MBEDTLS_MD_NONE) {
2457 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2458 params, params_len,
2459 md_alg);
2460 if (ret != 0) {
2461 return ret;
2462 }
2463 } else {
2464 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2465 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2466 }
2467
2468 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
2469
2470 /*
2471 * Verify signature
2472 */
2473 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2474 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
2475 mbedtls_ssl_send_alert_message(
2476 ssl,
2477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2478 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2479 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
2480 }
2481
2482#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2483 if (ssl->handshake->ecrs_enabled) {
2484 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
2485 }
2486#endif
2487
2488#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2489 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
2490 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2491 rsassa_pss_options.mgf1_hash_id = md_alg;
2492 rsassa_pss_options.expected_salt_len =
2493 mbedtls_md_get_size_from_type(md_alg);
2494 if (rsassa_pss_options.expected_salt_len == 0) {
2495 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2496 }
2497
2498 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2499 peer_pk,
2500 md_alg, hash, hashlen,
2501 p, sig_len);
2502 } else
2503#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2504 ret = mbedtls_pk_verify_restartable(peer_pk,
2505 md_alg, hash, hashlen, p, sig_len, rs_ctx);
2506
2507 if (ret != 0) {
2508 int send_alert_msg = 1;
2509#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2510 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
2511#endif
2512 if (send_alert_msg) {
2513 mbedtls_ssl_send_alert_message(
2514 ssl,
2515 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2516 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2517 }
2518 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
2519#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2520 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2521 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2522 }
2523#endif
2524 return ret;
2525 }
2526
2527#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2528 /* We don't need the peer's public key anymore. Free it,
2529 * so that more RAM is available for upcoming expensive
2530 * operations like ECDHE. */
2531 mbedtls_pk_free(peer_pk);
2532#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2533 }
2534#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2535
2536exit:
2537 mbedtls_ssl_handshake_increment_state(ssl);
2538
2539 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
2540
2541 return 0;
2542}
2543
2544#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
2545MBEDTLS_CHECK_RETURN_CRITICAL
2546static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
2547{
2548 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2549 ssl->handshake->ciphersuite_info;
2550
2551 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
2552
2553 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2554 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
2555 mbedtls_ssl_handshake_increment_state(ssl);
2556 return 0;
2557 }
2558
2559 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2560 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2561}
2562#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2563MBEDTLS_CHECK_RETURN_CRITICAL
2564static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
2565{
2566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2567 unsigned char *buf;
2568 size_t n = 0;
2569 size_t cert_type_len = 0, dn_len = 0;
2570 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2571 ssl->handshake->ciphersuite_info;
2572 size_t sig_alg_len;
2573#if defined(MBEDTLS_DEBUG_C)
2574 unsigned char *sig_alg;
2575 unsigned char *dn;
2576#endif
2577
2578 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
2579
2580 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2581 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
2582 mbedtls_ssl_handshake_increment_state(ssl);
2583 return 0;
2584 }
2585
2586 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2587 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2588 return ret;
2589 }
2590
2591 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2592 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2593 mbedtls_ssl_send_alert_message(
2594 ssl,
2595 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2596 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2597 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
2598 }
2599
2600 mbedtls_ssl_handshake_increment_state(ssl);
2601 ssl->handshake->client_auth =
2602 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
2603
2604 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2605 ssl->handshake->client_auth ? "a" : "no"));
2606
2607 if (ssl->handshake->client_auth == 0) {
2608 /* Current message is probably the ServerHelloDone */
2609 ssl->keep_current_message = 1;
2610 goto exit;
2611 }
2612
2613 /*
2614 * struct {
2615 * ClientCertificateType certificate_types<1..2^8-1>;
2616 * SignatureAndHashAlgorithm
2617 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2618 * DistinguishedName certificate_authorities<0..2^16-1>;
2619 * } CertificateRequest;
2620 *
2621 * Since we only support a single certificate on clients, let's just
2622 * ignore all the information that's supposed to help us pick a
2623 * certificate.
2624 *
2625 * We could check that our certificate matches the request, and bail out
2626 * if it doesn't, but it's simpler to just send the certificate anyway,
2627 * and give the server the opportunity to decide if it should terminate
2628 * the connection when it doesn't like our certificate.
2629 *
2630 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2631 * point we only have one hash available (see comments in
2632 * write_certificate_verify), so let's just use what we have.
2633 *
2634 * However, we still minimally parse the message to check it is at least
2635 * superficially sane.
2636 */
2637 buf = ssl->in_msg;
2638
2639 /* certificate_types */
2640 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2641 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2642 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2643 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2644 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2645 }
2646 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
2647 n = cert_type_len;
2648
2649 /*
2650 * In the subsequent code there are two paths that read from buf:
2651 * * the length of the signature algorithms field (if minor version of
2652 * SSL is 3),
2653 * * distinguished name length otherwise.
2654 * Both reach at most the index:
2655 * ...hdr_len + 2 + n,
2656 * therefore the buffer length at this point must be greater than that
2657 * regardless of the actual code path.
2658 */
2659 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2660 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2661 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2662 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2663 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2664 }
2665
2666 /* supported_signature_algorithms */
2667 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
2668
2669 /*
2670 * The furthest access in buf is in the loop few lines below:
2671 * sig_alg[i + 1],
2672 * where:
2673 * sig_alg = buf + ...hdr_len + 3 + n,
2674 * max(i) = sig_alg_len - 1.
2675 * Therefore the furthest access is:
2676 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2677 * which reduces to:
2678 * buf[...hdr_len + 3 + n + sig_alg_len],
2679 * which is one less than we need the buf to be.
2680 */
2681 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2682 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2683 mbedtls_ssl_send_alert_message(
2684 ssl,
2685 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2686 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2687 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2688 }
2689
2690#if defined(MBEDTLS_DEBUG_C)
2691 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2692 for (size_t i = 0; i < sig_alg_len; i += 2) {
2693 MBEDTLS_SSL_DEBUG_MSG(3,
2694 ("Supported Signature Algorithm found: %02x %02x",
2695 sig_alg[i], sig_alg[i + 1]));
2696 }
2697#endif
2698
2699 n += 2 + sig_alg_len;
2700
2701 /* certificate_authorities */
2702 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
2703
2704 n += dn_len;
2705 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2706 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2707 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2709 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2710 }
2711
2712#if defined(MBEDTLS_DEBUG_C)
2713 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2714 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
2715 unsigned char *p = dn + i + 2;
2716 mbedtls_x509_name name;
2717 size_t asn1_len;
2718 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2719 memset(&name, 0, sizeof(name));
2720 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2721 if (dni_len > dn_len - i - 2 ||
2722 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2723 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2724 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2725 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2726 mbedtls_ssl_send_alert_message(
2727 ssl,
2728 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2730 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2731 }
2732 MBEDTLS_SSL_DEBUG_MSG(3,
2733 ("DN hint: %.*s",
2734 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2735 mbedtls_asn1_free_named_data_list_shallow(name.next);
2736 }
2737#endif
2738
2739exit:
2740 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2741
2742 return 0;
2743}
2744#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2745
2746MBEDTLS_CHECK_RETURN_CRITICAL
2747static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
2748{
2749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2750
2751 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
2752
2753 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2754 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2755 return ret;
2756 }
2757
2758 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2759 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2760 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
2761 }
2762
2763 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2764 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2765 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2766 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2767 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2768 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2769 }
2770
2771 mbedtls_ssl_handshake_increment_state(ssl);
2772
2773#if defined(MBEDTLS_SSL_PROTO_DTLS)
2774 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2775 mbedtls_ssl_recv_flight_completed(ssl);
2776 }
2777#endif
2778
2779 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
2780
2781 return 0;
2782}
2783
2784MBEDTLS_CHECK_RETURN_CRITICAL
2785static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
2786{
2787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2788
2789 size_t header_len;
2790 size_t content_len;
2791 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2792 ssl->handshake->ciphersuite_info;
2793
2794 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
2795
2796#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2797 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
2798 /*
2799 * DHM key exchange -- send G^X mod P
2800 */
2801 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
2802
2803 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
2804 header_len = 6;
2805
2806 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2807 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2808 &ssl->out_msg[header_len], content_len,
2809 ssl->conf->f_rng, ssl->conf->p_rng);
2810 if (ret != 0) {
2811 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2812 return ret;
2813 }
2814
2815 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2816 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
2817
2818 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2819 ssl->handshake->premaster,
2820 MBEDTLS_PREMASTER_SIZE,
2821 &ssl->handshake->pmslen,
2822 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2824 return ret;
2825 }
2826
2827 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2828 } else
2829#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
2830#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2831 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2832 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2833 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2834 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2835 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2836 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2837 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2838#if defined(MBEDTLS_USE_PSA_CRYPTO)
2839 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2840 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2841 psa_key_attributes_t key_attributes;
2842
2843 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2844
2845 header_len = 4;
2846
2847 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation."));
2848
2849 /*
2850 * Generate EC private key for ECDHE exchange.
2851 */
2852
2853 /* The master secret is obtained from the shared ECDH secret by
2854 * applying the TLS 1.2 PRF with a specific salt and label. While
2855 * the PSA Crypto API encourages combining key agreement schemes
2856 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2857 * yet support the provisioning of salt + label to the KDF.
2858 * For the time being, we therefore need to split the computation
2859 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2860 key_attributes = psa_key_attributes_init();
2861 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2862 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2863 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
2864 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
2865
2866 /* Generate ECDH private key. */
2867 status = psa_generate_key(&key_attributes,
2868 &handshake->xxdh_psa_privkey);
2869 if (status != PSA_SUCCESS) {
2870 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2871 }
2872
2873 /* Export the public part of the ECDH private key from PSA.
2874 * The export format is an ECPoint structure as expected by TLS,
2875 * but we just need to add a length byte before that. */
2876 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2877 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2878 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
2879 size_t own_pubkey_len;
2880
2881 status = psa_export_public_key(handshake->xxdh_psa_privkey,
2882 own_pubkey, own_pubkey_max_len,
2883 &own_pubkey_len);
2884 if (status != PSA_SUCCESS) {
2885 psa_destroy_key(handshake->xxdh_psa_privkey);
2886 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
2887 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2888 }
2889
2890 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2891 content_len = own_pubkey_len + 1;
2892
2893 /* The ECDH secret is the premaster secret used for key derivation. */
2894
2895 /* Compute ECDH shared secret. */
2896 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2897 handshake->xxdh_psa_privkey,
2898 handshake->xxdh_psa_peerkey,
2899 handshake->xxdh_psa_peerkey_len,
2900 ssl->handshake->premaster,
2901 sizeof(ssl->handshake->premaster),
2902 &ssl->handshake->pmslen);
2903
2904 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2905 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
2906
2907 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2908 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2909 }
2910#else
2911 /*
2912 * ECDH key exchange -- send client public value
2913 */
2914 header_len = 4;
2915
2916#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2917 if (ssl->handshake->ecrs_enabled) {
2918 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
2919 goto ecdh_calc_secret;
2920 }
2921
2922 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
2923 }
2924#endif
2925
2926 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2927 &content_len,
2928 &ssl->out_msg[header_len], 1000,
2929 ssl->conf->f_rng, ssl->conf->p_rng);
2930 if (ret != 0) {
2931 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
2932#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2933 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2934 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2935 }
2936#endif
2937 return ret;
2938 }
2939
2940 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2941 MBEDTLS_DEBUG_ECDH_Q);
2942
2943#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2944 if (ssl->handshake->ecrs_enabled) {
2945 ssl->handshake->ecrs_n = content_len;
2946 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
2947 }
2948
2949ecdh_calc_secret:
2950 if (ssl->handshake->ecrs_enabled) {
2951 content_len = ssl->handshake->ecrs_n;
2952 }
2953#endif
2954 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2955 &ssl->handshake->pmslen,
2956 ssl->handshake->premaster,
2957 MBEDTLS_PREMASTER_SIZE,
2958 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2959 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2960#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2961 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2962 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2963 }
2964#endif
2965 return ret;
2966 }
2967
2968 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2969 MBEDTLS_DEBUG_ECDH_Z);
2970#endif /* MBEDTLS_USE_PSA_CRYPTO */
2971 } else
2972#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2973 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2974 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2975 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2976#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2977 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2978 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2980 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2981 psa_key_attributes_t key_attributes;
2982
2983 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2984
2985 /*
2986 * opaque psk_identity<0..2^16-1>;
2987 */
2988 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
2989 /* We don't offer PSK suites if we don't have a PSK,
2990 * and we check that the server's choice is among the
2991 * ciphersuites we offered, so this should never happen. */
2992 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2993 }
2994
2995 /* uint16 to store content length */
2996 const size_t content_len_size = 2;
2997 const size_t ecpoint_len_size = 1;
2998 const size_t ecpoint_max_len =
2999 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(handshake->xxdh_psa_type,
3000 handshake->xxdh_psa_bits);
3001
3002 header_len = 4;
3003
3004 if (ecpoint_max_len > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size ||
3005 header_len + content_len_size + ssl->conf->psk_identity_len
3006 > MBEDTLS_SSL_OUT_CONTENT_LEN - ecpoint_len_size - ecpoint_max_len) {
3007 MBEDTLS_SSL_DEBUG_MSG(1,
3008 ("psk identity too long or SSL buffer too short"));
3009 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3010 }
3011
3012 unsigned char *p = ssl->out_msg + header_len;
3013
3014 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
3015 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
3016 header_len += content_len_size;
3017
3018 memcpy(p, ssl->conf->psk_identity,
3019 ssl->conf->psk_identity_len);
3020 p += ssl->conf->psk_identity_len;
3021
3022 header_len += ssl->conf->psk_identity_len;
3023
3024 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation."));
3025
3026 /*
3027 * Generate EC private key for ECDHE exchange.
3028 */
3029
3030 /* The master secret is obtained from the shared ECDH secret by
3031 * applying the TLS 1.2 PRF with a specific salt and label. While
3032 * the PSA Crypto API encourages combining key agreement schemes
3033 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3034 * yet support the provisioning of salt + label to the KDF.
3035 * For the time being, we therefore need to split the computation
3036 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3037 key_attributes = psa_key_attributes_init();
3038 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
3039 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
3040 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
3041 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
3042
3043 /* Generate ECDH private key. */
3044 status = psa_generate_key(&key_attributes,
3045 &handshake->xxdh_psa_privkey);
3046 if (status != PSA_SUCCESS) {
3047 return PSA_TO_MBEDTLS_ERR(status);
3048 }
3049
3050 /* Export the public part of the ECDH private key from PSA.
3051 * The export format is an ECPoint structure as expected by TLS,
3052 * but we just need to add a length byte before that. */
3053 unsigned char *own_pubkey = p + 1;
3054 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3055 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
3056 size_t own_pubkey_len = 0;
3057
3058 status = psa_export_public_key(handshake->xxdh_psa_privkey,
3059 own_pubkey, own_pubkey_max_len,
3060 &own_pubkey_len);
3061 if (status != PSA_SUCCESS) {
3062 psa_destroy_key(handshake->xxdh_psa_privkey);
3063 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3064 return PSA_TO_MBEDTLS_ERR(status);
3065 }
3066
3067 *p = (unsigned char) own_pubkey_len;
3068 content_len = own_pubkey_len + 1;
3069
3070 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3071 * - a uint16 containing the length (in octets) of the ECDH computation
3072 * - the octet string produced by the ECDH computation
3073 * - a uint16 containing the length (in octets) of the PSK
3074 * - the PSK itself
3075 */
3076 unsigned char *pms = ssl->handshake->premaster;
3077 const unsigned char * const pms_end = pms +
3078 sizeof(ssl->handshake->premaster);
3079 /* uint16 to store length (in octets) of the ECDH computation */
3080 const size_t zlen_size = 2;
3081 size_t zlen = 0;
3082
3083 /* Perform ECDH computation after the uint16 reserved for the length */
3084 status = psa_raw_key_agreement(PSA_ALG_ECDH,
3085 handshake->xxdh_psa_privkey,
3086 handshake->xxdh_psa_peerkey,
3087 handshake->xxdh_psa_peerkey_len,
3088 pms + zlen_size,
3089 pms_end - (pms + zlen_size),
3090 &zlen);
3091
3092 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3093 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3094
3095 if (status != PSA_SUCCESS) {
3096 return PSA_TO_MBEDTLS_ERR(status);
3097 } else if (destruction_status != PSA_SUCCESS) {
3098 return PSA_TO_MBEDTLS_ERR(destruction_status);
3099 }
3100
3101 /* Write the ECDH computation length before the ECDH computation */
3102 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
3103 pms += zlen_size + zlen;
3104 } else
3105#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3106 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3107#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
3108 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
3109 /*
3110 * opaque psk_identity<0..2^16-1>;
3111 */
3112 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
3113 /* We don't offer PSK suites if we don't have a PSK,
3114 * and we check that the server's choice is among the
3115 * ciphersuites we offered, so this should never happen. */
3116 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3117 }
3118
3119 header_len = 4;
3120 content_len = ssl->conf->psk_identity_len;
3121
3122 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3123 MBEDTLS_SSL_DEBUG_MSG(1,
3124 ("psk identity too long or SSL buffer too short"));
3125 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3126 }
3127
3128 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3129 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
3130
3131 memcpy(ssl->out_msg + header_len,
3132 ssl->conf->psk_identity,
3133 ssl->conf->psk_identity_len);
3134 header_len += ssl->conf->psk_identity_len;
3135
3136#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3137 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
3138 content_len = 0;
3139 } else
3140#endif
3141#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3142 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3143 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3144 &content_len, 2)) != 0) {
3145 return ret;
3146 }
3147 } else
3148#endif
3149#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3150 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
3151 /*
3152 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3153 */
3154 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
3155
3156 if (header_len + 2 + content_len >
3157 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3158 MBEDTLS_SSL_DEBUG_MSG(1,
3159 ("psk identity or DHM size too long or SSL buffer too short"));
3160 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3161 }
3162
3163 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3164 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
3165
3166 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3167 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3168 &ssl->out_msg[header_len], content_len,
3169 ssl->conf->f_rng, ssl->conf->p_rng);
3170 if (ret != 0) {
3171 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3172 return ret;
3173 }
3174
3175#if defined(MBEDTLS_USE_PSA_CRYPTO)
3176 unsigned char *pms = ssl->handshake->premaster;
3177 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
3178 size_t pms_len;
3179
3180 /* Write length only when we know the actual value */
3181 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3182 pms + 2, pms_end - (pms + 2), &pms_len,
3183 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3184 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3185 return ret;
3186 }
3187 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
3188 pms += 2 + pms_len;
3189
3190 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
3191#endif
3192 } else
3193#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3194#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3195 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3196 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
3197 /*
3198 * ClientECDiffieHellmanPublic public;
3199 */
3200 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3201 &content_len,
3202 &ssl->out_msg[header_len],
3203 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3204 ssl->conf->f_rng, ssl->conf->p_rng);
3205 if (ret != 0) {
3206 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3207 return ret;
3208 }
3209
3210 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3211 MBEDTLS_DEBUG_ECDH_Q);
3212 } else
3213#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3214 {
3215 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3216 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3217 }
3218
3219#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3220 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3221 (mbedtls_key_exchange_type_t) ciphersuite_info->
3222 key_exchange)) != 0) {
3223 MBEDTLS_SSL_DEBUG_RET(1,
3224 "mbedtls_ssl_psk_derive_premaster", ret);
3225 return ret;
3226 }
3227#endif /* !MBEDTLS_USE_PSA_CRYPTO */
3228 } else
3229#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
3230#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3231 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
3232 header_len = 4;
3233 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3234 &content_len, 0)) != 0) {
3235 return ret;
3236 }
3237 } else
3238#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3239#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3240 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
3241 header_len = 4;
3242
3243#if defined(MBEDTLS_USE_PSA_CRYPTO)
3244 unsigned char *out_p = ssl->out_msg + header_len;
3245 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3246 header_len;
3247 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3248 out_p, end_p - out_p, &content_len,
3249 MBEDTLS_ECJPAKE_ROUND_TWO);
3250 if (ret != 0) {
3251 psa_destroy_key(ssl->handshake->psa_pake_password);
3252 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3253 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3254 return ret;
3255 }
3256#else
3257 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3258 ssl->out_msg + header_len,
3259 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3260 &content_len,
3261 ssl->conf->f_rng, ssl->conf->p_rng);
3262 if (ret != 0) {
3263 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3264 return ret;
3265 }
3266
3267 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3268 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3269 ssl->conf->f_rng, ssl->conf->p_rng);
3270 if (ret != 0) {
3271 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3272 return ret;
3273 }
3274#endif /* MBEDTLS_USE_PSA_CRYPTO */
3275 } else
3276#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3277 {
3278 ((void) ciphersuite_info);
3279 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3280 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3281 }
3282
3283 ssl->out_msglen = header_len + content_len;
3284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3285 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
3286
3287 mbedtls_ssl_handshake_increment_state(ssl);
3288
3289 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3290 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3291 return ret;
3292 }
3293
3294 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
3295
3296 return 0;
3297}
3298
3299#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
3300MBEDTLS_CHECK_RETURN_CRITICAL
3301static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
3302{
3303 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
3304 ssl->handshake->ciphersuite_info;
3305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3306
3307 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
3308
3309 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3310 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3311 return ret;
3312 }
3313
3314 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3315 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
3316 mbedtls_ssl_handshake_increment_state(ssl);
3317 return 0;
3318 }
3319
3320 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3321 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3322}
3323#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
3324MBEDTLS_CHECK_RETURN_CRITICAL
3325static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
3326{
3327 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3328 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
3329 ssl->handshake->ciphersuite_info;
3330 size_t n = 0, offset = 0;
3331 unsigned char hash[48];
3332 unsigned char *hash_start = hash;
3333 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3334 size_t hashlen;
3335 void *rs_ctx = NULL;
3336#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3337 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
3338#else
3339 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf);
3340#endif
3341
3342 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
3343
3344#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
3345 if (ssl->handshake->ecrs_enabled &&
3346 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
3347 goto sign;
3348 }
3349#endif
3350
3351 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3352 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3353 return ret;
3354 }
3355
3356 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3357 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
3358 mbedtls_ssl_handshake_increment_state(ssl);
3359 return 0;
3360 }
3361
3362 if (ssl->handshake->client_auth == 0 ||
3363 mbedtls_ssl_own_cert(ssl) == NULL) {
3364 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
3365 mbedtls_ssl_handshake_increment_state(ssl);
3366 return 0;
3367 }
3368
3369 if (mbedtls_ssl_own_key(ssl) == NULL) {
3370 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3371 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
3372 }
3373
3374 /*
3375 * Make a signature of the handshake digests
3376 */
3377#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
3378 if (ssl->handshake->ecrs_enabled) {
3379 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3380 }
3381
3382sign:
3383#endif
3384
3385 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3386 if (0 != ret) {
3387 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3388 return ret;
3389 }
3390
3391 /*
3392 * digitally-signed struct {
3393 * opaque handshake_messages[handshake_messages_length];
3394 * };
3395 *
3396 * Taking shortcut here. We assume that the server always allows the
3397 * PRF Hash function and has sent it in the allowed signature
3398 * algorithms list received in the Certificate Request message.
3399 *
3400 * Until we encounter a server that does not, we will take this
3401 * shortcut.
3402 *
3403 * Reason: Otherwise we should have running hashes for SHA512 and
3404 * SHA224 in order to satisfy 'weird' needs from the server
3405 * side.
3406 */
3407 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
3408 md_alg = MBEDTLS_MD_SHA384;
3409 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
3410 } else {
3411 md_alg = MBEDTLS_MD_SHA256;
3412 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
3413 }
3414 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
3415
3416 /* Info from md_alg will be used instead */
3417 hashlen = 0;
3418 offset = 2;
3419
3420#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
3421 if (ssl->handshake->ecrs_enabled) {
3422 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
3423 }
3424#endif
3425
3426 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3427 md_alg, hash_start, hashlen,
3428 ssl->out_msg + 6 + offset,
3429 out_buf_len - 6 - offset,
3430 &n,
3431 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3432 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
3433#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
3434 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
3435 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3436 }
3437#endif
3438 return ret;
3439 }
3440
3441 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
3442
3443 ssl->out_msglen = 6 + n + offset;
3444 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3445 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
3446
3447 mbedtls_ssl_handshake_increment_state(ssl);
3448
3449 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3450 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3451 return ret;
3452 }
3453
3454 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
3455
3456 return ret;
3457}
3458#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
3459
3460#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3461MBEDTLS_CHECK_RETURN_CRITICAL
3462static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
3463{
3464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3465 uint32_t lifetime;
3466 size_t ticket_len;
3467 unsigned char *ticket;
3468 const unsigned char *msg;
3469
3470 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
3471
3472 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3473 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3474 return ret;
3475 }
3476
3477 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3478 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3479 mbedtls_ssl_send_alert_message(
3480 ssl,
3481 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3482 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3483 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3484 }
3485
3486 /*
3487 * struct {
3488 * uint32 ticket_lifetime_hint;
3489 * opaque ticket<0..2^16-1>;
3490 * } NewSessionTicket;
3491 *
3492 * 0 . 3 ticket_lifetime_hint
3493 * 4 . 5 ticket_len (n)
3494 * 6 . 5+n ticket content
3495 */
3496 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3497 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3498 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3499 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3500 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3501 return MBEDTLS_ERR_SSL_DECODE_ERROR;
3502 }
3503
3504 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
3505
3506 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
3507
3508 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
3509
3510 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3511 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3512 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3514 return MBEDTLS_ERR_SSL_DECODE_ERROR;
3515 }
3516
3517 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
3518
3519 /* We're not waiting for a NewSessionTicket message any more */
3520 ssl->handshake->new_session_ticket = 0;
3521 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
3522
3523 /*
3524 * Zero-length ticket means the server changed his mind and doesn't want
3525 * to send a ticket after all, so just forget it
3526 */
3527 if (ticket_len == 0) {
3528 return 0;
3529 }
3530
3531 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3532 mbedtls_zeroize_and_free(ssl->session->ticket,
3533 ssl->session->ticket_len);
3534 ssl->session->ticket = NULL;
3535 ssl->session->ticket_len = 0;
3536 }
3537
3538 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
3539 ssl->session_negotiate->ticket_len);
3540 ssl->session_negotiate->ticket = NULL;
3541 ssl->session_negotiate->ticket_len = 0;
3542
3543 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3544 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3545 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3546 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3547 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3548 }
3549
3550 memcpy(ticket, msg + 6, ticket_len);
3551
3552 ssl->session_negotiate->ticket = ticket;
3553 ssl->session_negotiate->ticket_len = ticket_len;
3554 ssl->session_negotiate->ticket_lifetime = lifetime;
3555
3556 /*
3557 * RFC 5077 section 3.4:
3558 * "If the client receives a session ticket from the server, then it
3559 * discards any Session ID that was sent in the ServerHello."
3560 */
3561 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
3562 ssl->session_negotiate->id_len = 0;
3563
3564 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
3565
3566 return 0;
3567}
3568#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3569
3570/*
3571 * SSL handshake -- client side -- single step
3572 */
3573int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
3574{
3575 int ret = 0;
3576
3577 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
3578 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
3579#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3580 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3581 ssl->handshake->new_session_ticket != 0) {
3582 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET);
3583 }
3584#endif
3585
3586 switch (ssl->state) {
3587 case MBEDTLS_SSL_HELLO_REQUEST:
3588 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3589 break;
3590
3591 /*
3592 * ==> ClientHello
3593 */
3594 case MBEDTLS_SSL_CLIENT_HELLO:
3595 ret = mbedtls_ssl_write_client_hello(ssl);
3596 break;
3597
3598 /*
3599 * <== ServerHello
3600 * Certificate
3601 * ( ServerKeyExchange )
3602 * ( CertificateRequest )
3603 * ServerHelloDone
3604 */
3605 case MBEDTLS_SSL_SERVER_HELLO:
3606 ret = ssl_parse_server_hello(ssl);
3607 break;
3608
3609 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3610 ret = mbedtls_ssl_parse_certificate(ssl);
3611 break;
3612
3613 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3614 ret = ssl_parse_server_key_exchange(ssl);
3615 break;
3616
3617 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3618 ret = ssl_parse_certificate_request(ssl);
3619 break;
3620
3621 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3622 ret = ssl_parse_server_hello_done(ssl);
3623 break;
3624
3625 /*
3626 * ==> ( Certificate/Alert )
3627 * ClientKeyExchange
3628 * ( CertificateVerify )
3629 * ChangeCipherSpec
3630 * Finished
3631 */
3632 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3633 ret = mbedtls_ssl_write_certificate(ssl);
3634 break;
3635
3636 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3637 ret = ssl_write_client_key_exchange(ssl);
3638 break;
3639
3640 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3641 ret = ssl_write_certificate_verify(ssl);
3642 break;
3643
3644 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3645 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3646 break;
3647
3648 case MBEDTLS_SSL_CLIENT_FINISHED:
3649 ret = mbedtls_ssl_write_finished(ssl);
3650 break;
3651
3652 /*
3653 * <== ( NewSessionTicket )
3654 * ChangeCipherSpec
3655 * Finished
3656 */
3657#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3658 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3659 ret = ssl_parse_new_session_ticket(ssl);
3660 break;
3661#endif
3662
3663 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3664 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3665 break;
3666
3667 case MBEDTLS_SSL_SERVER_FINISHED:
3668 ret = mbedtls_ssl_parse_finished(ssl);
3669 break;
3670
3671 case MBEDTLS_SSL_FLUSH_BUFFERS:
3672 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3673 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3674 break;
3675
3676 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3677 mbedtls_ssl_handshake_wrapup(ssl);
3678 break;
3679
3680 default:
3681 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3682 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3683 }
3684
3685 return ret;
3686}
3687
3688#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */