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