2 * SSLv3/TLSv1 server-side functions
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #if defined(MBEDTLS_SSL_SRV_C)
24 #if defined(MBEDTLS_PLATFORM_C)
25 #include "mbedtls/platform.h"
28 #define mbedtls_calloc calloc
29 #define mbedtls_free free
32 #include "mbedtls/ssl.h"
33 #include "mbedtls/ssl_internal.h"
34 #include "mbedtls/debug.h"
35 #include "mbedtls/error.h"
36 #include "mbedtls/platform_util.h"
40 #if defined(MBEDTLS_ECP_C)
41 #include "mbedtls/ecp.h"
44 #if defined(MBEDTLS_HAVE_TIME)
45 #include "mbedtls/platform_time.h"
48 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
49 int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context
*ssl
,
50 const unsigned char *info
,
52 if (ssl
->conf
->endpoint
!= MBEDTLS_SSL_IS_SERVER
)
53 return (MBEDTLS_ERR_SSL_BAD_INPUT_DATA
);
55 mbedtls_free(ssl
->cli_id
);
57 if ((ssl
->cli_id
= mbedtls_calloc(1, ilen
)) == NULL
)
58 return (MBEDTLS_ERR_SSL_ALLOC_FAILED
);
60 memcpy(ssl
->cli_id
, info
, ilen
);
61 ssl
->cli_id_len
= ilen
;
66 void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config
*conf
,
67 mbedtls_ssl_cookie_write_t
*f_cookie_write
,
68 mbedtls_ssl_cookie_check_t
*f_cookie_check
,
70 conf
->f_cookie_write
= f_cookie_write
;
71 conf
->f_cookie_check
= f_cookie_check
;
72 conf
->p_cookie
= p_cookie
;
74 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
76 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
77 static int ssl_parse_servername_ext(mbedtls_ssl_context
*ssl
,
78 const unsigned char *buf
,
80 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
81 size_t servername_list_size
, hostname_len
;
82 const unsigned char *p
;
84 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
87 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
88 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
89 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
90 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
92 servername_list_size
= ((buf
[0] << 8) | (buf
[1]));
93 if (servername_list_size
+ 2 != len
) {
94 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
95 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
96 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
97 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
101 while (servername_list_size
> 2) {
102 hostname_len
= ((p
[1] << 8) | p
[2]);
103 if (hostname_len
+ 3 > servername_list_size
) {
104 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
105 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
106 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
107 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
110 if (p
[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME
) {
111 ret
= ssl
->conf
->f_sni(ssl
->conf
->p_sni
,
112 ssl
, p
+ 3, hostname_len
);
114 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret
);
115 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
116 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME
);
117 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
122 servername_list_size
-= hostname_len
+ 3;
123 p
+= hostname_len
+ 3;
126 if (servername_list_size
!= 0) {
127 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
128 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
129 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
130 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
135 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
137 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
138 static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config
const *conf
) {
139 if (conf
->f_psk
!= NULL
)
142 if (conf
->psk_identity_len
== 0 || conf
->psk_identity
== NULL
)
145 if (conf
->psk
!= NULL
&& conf
->psk_len
!= 0)
148 #if defined(MBEDTLS_USE_PSA_CRYPTO)
149 if (! mbedtls_svc_key_id_is_null(conf
->psk_opaque
))
151 #endif /* MBEDTLS_USE_PSA_CRYPTO */
156 #if defined(MBEDTLS_USE_PSA_CRYPTO)
157 static int ssl_use_opaque_psk(mbedtls_ssl_context
const *ssl
) {
158 if (ssl
->conf
->f_psk
!= NULL
) {
159 /* If we've used a callback to select the PSK,
160 * the static configuration is irrelevant. */
162 if (! mbedtls_svc_key_id_is_null(ssl
->handshake
->psk_opaque
))
168 if (! mbedtls_svc_key_id_is_null(ssl
->conf
->psk_opaque
))
173 #endif /* MBEDTLS_USE_PSA_CRYPTO */
174 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
176 static int ssl_parse_renegotiation_info(mbedtls_ssl_context
*ssl
,
177 const unsigned char *buf
,
179 #if defined(MBEDTLS_SSL_RENEGOTIATION)
180 if (ssl
->renego_status
!= MBEDTLS_SSL_INITIAL_HANDSHAKE
) {
181 /* Check verify-data in constant-time. The length OTOH is no secret */
182 if (len
!= 1 + ssl
->verify_data_len
||
183 buf
[0] != ssl
->verify_data_len
||
184 mbedtls_ssl_safer_memcmp(buf
+ 1, ssl
->peer_verify_data
,
185 ssl
->verify_data_len
) != 0) {
186 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
187 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
188 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
189 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
192 #endif /* MBEDTLS_SSL_RENEGOTIATION */
194 if (len
!= 1 || buf
[0] != 0x0) {
195 MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
196 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
197 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
198 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
201 ssl
->secure_renegotiation
= MBEDTLS_SSL_SECURE_RENEGOTIATION
;
207 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
208 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
211 * Status of the implementation of signature-algorithms extension:
213 * Currently, we are only considering the signature-algorithm extension
214 * to pick a ciphersuite which allows us to send the ServerKeyExchange
215 * message with a signature-hash combination that the user allows.
217 * We do *not* check whether all certificates in our certificate
218 * chain are signed with an allowed signature-hash pair.
219 * This needs to be done at a later stage.
222 static int ssl_parse_signature_algorithms_ext(mbedtls_ssl_context
*ssl
,
223 const unsigned char *buf
,
225 size_t sig_alg_list_size
;
227 const unsigned char *p
;
228 const unsigned char *end
= buf
+ len
;
230 mbedtls_md_type_t md_cur
;
231 mbedtls_pk_type_t sig_cur
;
234 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
235 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
236 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
237 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
239 sig_alg_list_size
= ((buf
[0] << 8) | (buf
[1]));
240 if (sig_alg_list_size
+ 2 != len
||
241 sig_alg_list_size
% 2 != 0) {
242 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
243 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
244 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
245 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
248 /* Currently we only guarantee signing the ServerKeyExchange message according
249 * to the constraints specified in this extension (see above), so it suffices
250 * to remember only one suitable hash for each possible signature algorithm.
252 * This will change when we also consider certificate signatures,
253 * in which case we will need to remember the whole signature-hash
254 * pair list from the extension.
257 for (p
= buf
+ 2; p
< end
; p
+= 2) {
258 /* Silently ignore unknown signature or hash algorithms. */
260 if ((sig_cur
= mbedtls_ssl_pk_alg_from_sig(p
[1])) == MBEDTLS_PK_NONE
) {
261 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext"
262 " unknown sig alg encoding %d", p
[1]));
266 /* Check if we support the hash the user proposes */
267 md_cur
= mbedtls_ssl_md_alg_from_hash(p
[0]);
268 if (md_cur
== MBEDTLS_MD_NONE
) {
269 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
270 " unknown hash alg encoding %d", p
[0]));
274 if (mbedtls_ssl_check_sig_hash(ssl
, md_cur
) == 0) {
275 mbedtls_ssl_sig_hash_set_add(&ssl
->handshake
->hash_algs
, sig_cur
, md_cur
);
276 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
277 " match sig %u and hash %u",
278 (unsigned) sig_cur
, (unsigned) md_cur
));
280 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: "
281 "hash alg %u not supported", (unsigned) md_cur
));
287 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
288 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
290 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
291 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
292 static int ssl_parse_supported_elliptic_curves(mbedtls_ssl_context
*ssl
,
293 const unsigned char *buf
,
295 size_t list_size
, our_size
;
296 const unsigned char *p
;
297 const mbedtls_ecp_curve_info
*curve_info
, **curves
;
300 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
301 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
303 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
305 list_size
= ((buf
[0] << 8) | (buf
[1]));
306 if (list_size
+ 2 != len
||
307 list_size
% 2 != 0) {
308 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
309 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
310 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
311 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
314 /* Should never happen unless client duplicates the extension */
315 if (ssl
->handshake
->curves
!= NULL
) {
316 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
317 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
319 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
322 /* Don't allow our peer to make us allocate too much memory,
323 * and leave room for a final 0 */
324 our_size
= list_size
/ 2 + 1;
325 if (our_size
> MBEDTLS_ECP_DP_MAX
)
326 our_size
= MBEDTLS_ECP_DP_MAX
;
328 if ((curves
= mbedtls_calloc(our_size
, sizeof(*curves
))) == NULL
) {
329 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
330 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR
);
331 return (MBEDTLS_ERR_SSL_ALLOC_FAILED
);
334 ssl
->handshake
->curves
= curves
;
337 while (list_size
> 0 && our_size
> 1) {
338 curve_info
= mbedtls_ecp_curve_info_from_tls_id((p
[0] << 8) | p
[1]);
340 if (curve_info
!= NULL
) {
341 *curves
++ = curve_info
;
352 static int ssl_parse_supported_point_formats(mbedtls_ssl_context
*ssl
,
353 const unsigned char *buf
,
356 const unsigned char *p
;
358 if (len
== 0 || (size_t)(buf
[0] + 1) != len
) {
359 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
360 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
361 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
362 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
367 while (list_size
> 0) {
368 if (p
[0] == MBEDTLS_ECP_PF_UNCOMPRESSED
||
369 p
[0] == MBEDTLS_ECP_PF_COMPRESSED
) {
370 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
371 ssl
->handshake
->ecdh_ctx
.point_format
= p
[0];
373 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
374 ssl
->handshake
->ecjpake_ctx
.point_format
= p
[0];
376 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p
[0]));
386 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
387 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
389 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
390 static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context
*ssl
,
391 const unsigned char *buf
,
393 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
395 if (mbedtls_ecjpake_check(&ssl
->handshake
->ecjpake_ctx
) != 0) {
396 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
400 if ((ret
= mbedtls_ecjpake_read_round_one(&ssl
->handshake
->ecjpake_ctx
,
402 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret
);
403 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
404 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
408 /* Only mark the extension as OK when we're sure it is */
409 ssl
->handshake
->cli_exts
|= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK
;
413 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
415 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
416 static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context
*ssl
,
417 const unsigned char *buf
,
419 if (len
!= 1 || buf
[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID
) {
420 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
421 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
422 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
423 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
426 ssl
->session_negotiate
->mfl_code
= buf
[0];
430 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
432 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
433 static int ssl_parse_cid_ext(mbedtls_ssl_context
*ssl
,
434 const unsigned char *buf
,
438 /* CID extension only makes sense in DTLS */
439 if (ssl
->conf
->transport
!= MBEDTLS_SSL_TRANSPORT_DATAGRAM
) {
440 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
441 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
442 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
443 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
447 * Quoting draft-ietf-tls-dtls-connection-id-05
448 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
451 * opaque cid<0..2^8-1>;
456 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
457 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
458 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
459 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
462 peer_cid_len
= *buf
++;
465 if (len
!= peer_cid_len
) {
466 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
467 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
468 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
469 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
472 /* Ignore CID if the user has disabled its use. */
473 if (ssl
->negotiate_cid
== MBEDTLS_SSL_CID_DISABLED
) {
474 /* Leave ssl->handshake->cid_in_use in its default
475 * value of MBEDTLS_SSL_CID_DISABLED. */
476 MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
480 if (peer_cid_len
> MBEDTLS_SSL_CID_OUT_LEN_MAX
) {
481 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
482 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
483 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
484 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
487 ssl
->handshake
->cid_in_use
= MBEDTLS_SSL_CID_ENABLED
;
488 ssl
->handshake
->peer_cid_len
= (uint8_t) peer_cid_len
;
489 memcpy(ssl
->handshake
->peer_cid
, buf
, peer_cid_len
);
491 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
492 MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf
, peer_cid_len
);
496 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
498 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
499 static int ssl_parse_truncated_hmac_ext(mbedtls_ssl_context
*ssl
,
500 const unsigned char *buf
,
503 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
504 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
505 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
506 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
511 if (ssl
->conf
->trunc_hmac
== MBEDTLS_SSL_TRUNC_HMAC_ENABLED
)
512 ssl
->session_negotiate
->trunc_hmac
= MBEDTLS_SSL_TRUNC_HMAC_ENABLED
;
516 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
518 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
519 static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context
*ssl
,
520 const unsigned char *buf
,
523 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
524 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
526 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
531 if (ssl
->conf
->encrypt_then_mac
== MBEDTLS_SSL_ETM_ENABLED
&&
532 ssl
->minor_ver
!= MBEDTLS_SSL_MINOR_VERSION_0
) {
533 ssl
->session_negotiate
->encrypt_then_mac
= MBEDTLS_SSL_ETM_ENABLED
;
538 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
540 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
541 static int ssl_parse_extended_ms_ext(mbedtls_ssl_context
*ssl
,
542 const unsigned char *buf
,
545 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
546 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
547 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
548 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
553 if (ssl
->conf
->extended_ms
== MBEDTLS_SSL_EXTENDED_MS_ENABLED
&&
554 ssl
->minor_ver
!= MBEDTLS_SSL_MINOR_VERSION_0
) {
555 ssl
->handshake
->extended_ms
= MBEDTLS_SSL_EXTENDED_MS_ENABLED
;
560 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
562 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
563 static int ssl_parse_session_ticket_ext(mbedtls_ssl_context
*ssl
,
566 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
567 mbedtls_ssl_session session
;
569 mbedtls_ssl_session_init(&session
);
571 if (ssl
->conf
->f_ticket_parse
== NULL
||
572 ssl
->conf
->f_ticket_write
== NULL
) {
576 /* Remember the client asked us to send a new ticket */
577 ssl
->handshake
->new_session_ticket
= 1;
579 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET
, len
));
584 #if defined(MBEDTLS_SSL_RENEGOTIATION)
585 if (ssl
->renego_status
!= MBEDTLS_SSL_INITIAL_HANDSHAKE
) {
586 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
589 #endif /* MBEDTLS_SSL_RENEGOTIATION */
592 * Failures are ok: just ignore the ticket and proceed.
594 if ((ret
= ssl
->conf
->f_ticket_parse(ssl
->conf
->p_ticket
, &session
,
596 mbedtls_ssl_session_free(&session
);
598 if (ret
== MBEDTLS_ERR_SSL_INVALID_MAC
)
599 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
600 else if (ret
== MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED
)
601 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
603 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret
);
609 * Keep the session ID sent by the client, since we MUST send it back to
610 * inform them we're accepting the ticket (RFC 5077 section 3.4)
612 session
.id_len
= ssl
->session_negotiate
->id_len
;
613 memcpy(&session
.id
, ssl
->session_negotiate
->id
, session
.id_len
);
615 mbedtls_ssl_session_free(ssl
->session_negotiate
);
616 memcpy(ssl
->session_negotiate
, &session
, sizeof(mbedtls_ssl_session
));
618 /* Zeroize instead of free as we copied the content */
619 mbedtls_platform_zeroize(&session
, sizeof(mbedtls_ssl_session
));
621 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
623 ssl
->handshake
->resume
= 1;
625 /* Don't send a new ticket after all, this one is OK */
626 ssl
->handshake
->new_session_ticket
= 0;
630 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
632 #if defined(MBEDTLS_SSL_ALPN)
633 static int ssl_parse_alpn_ext(mbedtls_ssl_context
*ssl
,
634 const unsigned char *buf
, size_t len
) {
635 size_t list_len
, cur_len
, ours_len
;
636 const unsigned char *theirs
, *start
, *end
;
639 /* If ALPN not configured, just ignore the extension */
640 if (ssl
->conf
->alpn_list
== NULL
)
644 * opaque ProtocolName<1..2^8-1>;
647 * ProtocolName protocol_name_list<2..2^16-1>
648 * } ProtocolNameList;
651 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
653 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
654 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
655 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
658 list_len
= (buf
[0] << 8) | buf
[1];
659 if (list_len
!= len
- 2) {
660 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
661 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
662 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
666 * Validate peer's list (lengths)
670 for (theirs
= start
; theirs
!= end
; theirs
+= cur_len
) {
673 /* Current identifier must fit in list */
674 if (cur_len
> (size_t)(end
- theirs
)) {
675 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
676 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
677 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
680 /* Empty strings MUST NOT be included */
682 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
683 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER
);
684 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
689 * Use our order of preference
691 for (ours
= ssl
->conf
->alpn_list
; *ours
!= NULL
; ours
++) {
692 ours_len
= strlen(*ours
);
693 for (theirs
= start
; theirs
!= end
; theirs
+= cur_len
) {
696 if (cur_len
== ours_len
&&
697 memcmp(theirs
, *ours
, cur_len
) == 0) {
698 ssl
->alpn_chosen
= *ours
;
704 /* If we get there, no match was found */
705 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
706 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL
);
707 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
709 #endif /* MBEDTLS_SSL_ALPN */
711 #if defined(MBEDTLS_SSL_DTLS_SRTP)
712 static int ssl_parse_use_srtp_ext(mbedtls_ssl_context
*ssl
,
713 const unsigned char *buf
,
715 mbedtls_ssl_srtp_profile client_protection
= MBEDTLS_TLS_SRTP_UNSET
;
717 size_t profile_length
;
719 /*! 2 bytes for profile length and 1 byte for mki len */
720 const size_t size_of_lengths
= 3;
722 /* If use_srtp is not configured, just ignore the extension */
723 if ((ssl
->conf
->transport
!= MBEDTLS_SSL_TRANSPORT_DATAGRAM
) ||
724 (ssl
->conf
->dtls_srtp_profile_list
== NULL
) ||
725 (ssl
->conf
->dtls_srtp_profile_list_len
== 0)) {
729 /* RFC5764 section 4.1.1
730 * uint8 SRTPProtectionProfile[2];
733 * SRTPProtectionProfiles SRTPProtectionProfiles;
734 * opaque srtp_mki<0..255>;
737 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
741 * Min length is 5: at least one protection profile(2 bytes)
742 * and length(2 bytes) + srtp_mki length(1 byte)
743 * Check here that we have at least 2 bytes of protection profiles length
744 * and one of srtp_mki length
746 if (len
< size_of_lengths
) {
747 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
748 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
749 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
752 ssl
->dtls_srtp_info
.chosen_dtls_srtp_profile
= MBEDTLS_TLS_SRTP_UNSET
;
754 /* first 2 bytes are protection profile length(in bytes) */
755 profile_length
= (buf
[0] << 8) | buf
[1];
758 /* The profile length cannot be bigger than input buffer size - lengths fields */
759 if (profile_length
> len
- size_of_lengths
||
760 profile_length
% 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
761 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
762 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
763 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
766 * parse the extension list values are defined in
767 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
769 for (j
= 0; j
< profile_length
; j
+= 2) {
770 uint16_t protection_profile_value
= buf
[j
] << 8 | buf
[j
+ 1];
771 client_protection
= mbedtls_ssl_check_srtp_profile_value(protection_profile_value
);
773 if (client_protection
!= MBEDTLS_TLS_SRTP_UNSET
) {
774 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
775 mbedtls_ssl_get_srtp_profile_as_string(
776 client_protection
)));
780 /* check if suggested profile is in our list */
781 for (i
= 0; i
< ssl
->conf
->dtls_srtp_profile_list_len
; i
++) {
782 if (client_protection
== ssl
->conf
->dtls_srtp_profile_list
[i
]) {
783 ssl
->dtls_srtp_info
.chosen_dtls_srtp_profile
= ssl
->conf
->dtls_srtp_profile_list
[i
];
784 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
785 mbedtls_ssl_get_srtp_profile_as_string(
786 client_protection
)));
790 if (ssl
->dtls_srtp_info
.chosen_dtls_srtp_profile
!= MBEDTLS_TLS_SRTP_UNSET
)
793 buf
+= profile_length
; /* buf points to the mki length */
797 if (mki_length
> MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH
||
798 mki_length
+ profile_length
+ size_of_lengths
!= len
) {
799 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
800 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
801 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
804 /* Parse the mki only if present and mki is supported locally */
805 if (ssl
->conf
->dtls_srtp_mki_support
== MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED
&&
807 ssl
->dtls_srtp_info
.mki_len
= mki_length
;
809 memcpy(ssl
->dtls_srtp_info
.mki_value
, buf
, mki_length
);
811 MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl
->dtls_srtp_info
.mki_value
,
812 ssl
->dtls_srtp_info
.mki_len
);
817 #endif /* MBEDTLS_SSL_DTLS_SRTP */
820 * Auxiliary functions for ServerHello parsing and related actions
823 #if defined(MBEDTLS_X509_CRT_PARSE_C)
825 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
827 #if defined(MBEDTLS_ECDSA_C)
828 static int ssl_check_key_curve(mbedtls_pk_context
*pk
,
829 const mbedtls_ecp_curve_info
**curves
) {
830 const mbedtls_ecp_curve_info
**crv
= curves
;
831 mbedtls_ecp_group_id grp_id
= mbedtls_pk_ec(*pk
)->grp
.id
;
833 while (*crv
!= NULL
) {
834 if ((*crv
)->grp_id
== grp_id
)
841 #endif /* MBEDTLS_ECDSA_C */
844 * Try picking a certificate for this ciphersuite,
845 * return 0 on success and -1 on failure.
847 static int ssl_pick_cert(mbedtls_ssl_context
*ssl
,
848 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
) {
849 mbedtls_ssl_key_cert
*cur
, *list
, *fallback
= NULL
;
850 mbedtls_pk_type_t pk_alg
=
851 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info
);
854 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
855 if (ssl
->handshake
->sni_key_cert
!= NULL
)
856 list
= ssl
->handshake
->sni_key_cert
;
859 list
= ssl
->conf
->key_cert
;
861 if (pk_alg
== MBEDTLS_PK_NONE
)
864 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
867 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
871 for (cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
873 MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
876 if (! mbedtls_pk_can_do(&cur
->cert
->pk
, pk_alg
)) {
877 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
882 * This avoids sending the client a cert it'll reject based on
883 * keyUsage or other extensions.
885 * It also allows the user to provision different certificates for
886 * different uses based on keyUsage, eg if they want to avoid signing
887 * and decrypting with the same RSA key.
889 if (mbedtls_ssl_check_cert_usage(cur
->cert
, ciphersuite_info
,
890 MBEDTLS_SSL_IS_SERVER
, &flags
) != 0) {
891 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
892 "(extended) key usage extension"));
896 #if defined(MBEDTLS_ECDSA_C)
897 if (pk_alg
== MBEDTLS_PK_ECDSA
&&
898 ssl_check_key_curve(&cur
->cert
->pk
, ssl
->handshake
->curves
) != 0) {
899 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
905 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
906 * present them a SHA-higher cert rather than failing if it's the only
907 * one we got that satisfies the other conditions.
909 if (ssl
->minor_ver
< MBEDTLS_SSL_MINOR_VERSION_3
&&
910 cur
->cert
->sig_md
!= MBEDTLS_MD_SHA1
) {
911 if (fallback
== NULL
)
914 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate not preferred: "
915 "sha-2 with pre-TLS 1.2 client"));
920 /* If we get there, we got a winner */
927 /* Do not update ssl->handshake->key_cert unless there is a match */
929 ssl
->handshake
->key_cert
= cur
;
930 MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
931 ssl
->handshake
->key_cert
->cert
);
937 #endif /* MBEDTLS_X509_CRT_PARSE_C */
940 * Check if a given ciphersuite is suitable for use with our config/keys/etc
941 * Sets ciphersuite_info only if the suite matches.
943 static int ssl_ciphersuite_match(mbedtls_ssl_context
*ssl
, int suite_id
,
944 const mbedtls_ssl_ciphersuite_t
**ciphersuite_info
) {
945 const mbedtls_ssl_ciphersuite_t
*suite_info
;
947 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
948 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
949 mbedtls_pk_type_t sig_type
;
952 suite_info
= mbedtls_ssl_ciphersuite_from_id(suite_id
);
953 if (suite_info
== NULL
) {
954 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
955 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
958 MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
959 (unsigned int) suite_id
, suite_info
->name
));
961 if (suite_info
->min_minor_ver
> ssl
->minor_ver
||
962 suite_info
->max_minor_ver
< ssl
->minor_ver
) {
963 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
967 #if defined(MBEDTLS_SSL_PROTO_DTLS)
968 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
&&
969 (suite_info
->flags
& MBEDTLS_CIPHERSUITE_NODTLS
))
973 #if defined(MBEDTLS_ARC4_C)
974 if (ssl
->conf
->arc4_disabled
== MBEDTLS_SSL_ARC4_DISABLED
&&
975 suite_info
->cipher
== MBEDTLS_CIPHER_ARC4_128
) {
976 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: rc4"));
981 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
982 if (suite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECJPAKE
&&
983 (ssl
->handshake
->cli_exts
& MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK
) == 0) {
984 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
985 "not configured or ext missing"));
991 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
992 if (mbedtls_ssl_ciphersuite_uses_ec(suite_info
) &&
993 (ssl
->handshake
->curves
== NULL
||
994 ssl
->handshake
->curves
[0] == NULL
)) {
995 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
996 "no common elliptic curve"));
1001 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1002 /* If the ciphersuite requires a pre-shared key and we don't
1003 * have one, skip it now rather than failing later */
1004 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info
) &&
1005 ssl_conf_has_psk_or_cb(ssl
->conf
) == 0) {
1006 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
1011 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1012 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1013 /* If the ciphersuite requires signing, check whether
1014 * a suitable hash algorithm is present. */
1015 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
1016 sig_type
= mbedtls_ssl_get_ciphersuite_sig_alg(suite_info
);
1017 if (sig_type
!= MBEDTLS_PK_NONE
&&
1018 mbedtls_ssl_sig_hash_set_find(&ssl
->handshake
->hash_algs
, sig_type
) == MBEDTLS_MD_NONE
) {
1019 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
1020 "for signature algorithm %u", (unsigned) sig_type
));
1025 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1026 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1028 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1030 * Final check: if ciphersuite requires us to have a
1031 * certificate/key of a particular type:
1032 * - select the appropriate certificate if we have one, or
1033 * - try the next ciphersuite if we don't
1034 * This must be done last since we modify the key_cert list.
1036 if (ssl_pick_cert(ssl
, suite_info
) != 0) {
1037 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
1038 "no suitable certificate"));
1043 *ciphersuite_info
= suite_info
;
1047 #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1048 static int ssl_parse_client_hello_v2(mbedtls_ssl_context
*ssl
) {
1049 int ret
, got_common_suite
;
1052 unsigned int ciph_len
, sess_len
, chal_len
;
1053 unsigned char *buf
, *p
;
1054 const int *ciphersuites
;
1055 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
;
1057 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello v2"));
1059 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1060 if (ssl
->renego_status
!= MBEDTLS_SSL_INITIAL_HANDSHAKE
) {
1061 MBEDTLS_SSL_DEBUG_MSG(1, ("client hello v2 illegal for renegotiation"));
1062 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1063 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
1064 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1066 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1070 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf
, 5);
1072 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message type: %d",
1074 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message len.: %d",
1075 ((buf
[0] & 0x7F) << 8) | buf
[1]));
1076 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, max. version: [%d:%d]",
1080 * SSLv2 Client Hello
1083 * 0 . 1 message length
1086 * 2 . 2 message type
1087 * 3 . 4 protocol version
1089 if (buf
[2] != MBEDTLS_SSL_HS_CLIENT_HELLO
||
1090 buf
[3] != MBEDTLS_SSL_MAJOR_VERSION_3
) {
1091 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1092 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1095 n
= ((buf
[0] << 8) | buf
[1]) & 0x7FFF;
1097 if (n
< 17 || n
> 512) {
1098 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1099 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1102 ssl
->major_ver
= MBEDTLS_SSL_MAJOR_VERSION_3
;
1103 ssl
->minor_ver
= (buf
[4] <= ssl
->conf
->max_minor_ver
)
1104 ? buf
[4] : ssl
->conf
->max_minor_ver
;
1106 if (ssl
->minor_ver
< ssl
->conf
->min_minor_ver
) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1108 " [%d:%d] < [%d:%d]",
1109 ssl
->major_ver
, ssl
->minor_ver
,
1110 ssl
->conf
->min_major_ver
, ssl
->conf
->min_minor_ver
));
1112 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1113 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION
);
1114 return (MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION
);
1117 ssl
->handshake
->max_major_ver
= buf
[3];
1118 ssl
->handshake
->max_minor_ver
= buf
[4];
1120 if ((ret
= mbedtls_ssl_fetch_input(ssl
, 2 + n
)) != 0) {
1121 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret
);
1125 ssl
->handshake
->update_checksum(ssl
, buf
+ 2, n
);
1128 n
= ssl
->in_left
- 5;
1131 * 0 . 1 ciphersuitelist length
1132 * 2 . 3 session id length
1133 * 4 . 5 challenge length
1134 * 6 . .. ciphersuitelist
1135 * .. . .. session id
1138 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf
, n
);
1140 ciph_len
= (buf
[0] << 8) | buf
[1];
1141 sess_len
= (buf
[2] << 8) | buf
[3];
1142 chal_len
= (buf
[4] << 8) | buf
[5];
1144 MBEDTLS_SSL_DEBUG_MSG(3, ("ciph_len: %u, sess_len: %u, chal_len: %u",
1145 ciph_len
, sess_len
, chal_len
));
1148 * Make sure each parameter length is valid
1150 if (ciph_len
< 3 || (ciph_len
% 3) != 0) {
1151 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1152 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1155 if (sess_len
> 32) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1157 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1160 if (chal_len
< 8 || chal_len
> 32) {
1161 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1162 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1165 if (n
!= 6 + ciph_len
+ sess_len
+ chal_len
) {
1166 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1167 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1170 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1172 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1173 buf
+ 6 + ciph_len
, sess_len
);
1174 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, challenge",
1175 buf
+ 6 + ciph_len
+ sess_len
, chal_len
);
1177 p
= buf
+ 6 + ciph_len
;
1178 ssl
->session_negotiate
->id_len
= sess_len
;
1179 memset(ssl
->session_negotiate
->id
, 0,
1180 sizeof(ssl
->session_negotiate
->id
));
1181 memcpy(ssl
->session_negotiate
->id
, p
, ssl
->session_negotiate
->id_len
);
1184 memset(ssl
->handshake
->randbytes
, 0, 64);
1185 memcpy(ssl
->handshake
->randbytes
+ 32 - chal_len
, p
, chal_len
);
1188 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1190 for (i
= 0, p
= buf
+ 6; i
< ciph_len
; i
+= 3, p
+= 3) {
1191 if (p
[0] == 0 && p
[1] == 0 && p
[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO
) {
1192 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
1193 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1194 if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
) {
1195 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
1196 "during renegotiation"));
1198 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1199 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
1200 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1202 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1203 ssl
->secure_renegotiation
= MBEDTLS_SSL_SECURE_RENEGOTIATION
;
1208 #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1209 for (i
= 0, p
= buf
+ 6; i
< ciph_len
; i
+= 3, p
+= 3) {
1211 p
[1] == (unsigned char)((MBEDTLS_SSL_FALLBACK_SCSV_VALUE
>> 8) & 0xff) &&
1212 p
[2] == (unsigned char)((MBEDTLS_SSL_FALLBACK_SCSV_VALUE
) & 0xff)) {
1213 MBEDTLS_SSL_DEBUG_MSG(3, ("received FALLBACK_SCSV"));
1215 if (ssl
->minor_ver
< ssl
->conf
->max_minor_ver
) {
1216 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
1218 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1219 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK
);
1221 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1227 #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1229 got_common_suite
= 0;
1230 ciphersuites
= ssl
->conf
->ciphersuite_list
[ssl
->minor_ver
];
1231 ciphersuite_info
= NULL
;
1232 #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1233 for (j
= 0, p
= buf
+ 6; j
< ciph_len
; j
+= 3, p
+= 3)
1234 for (i
= 0; ciphersuites
[i
] != 0; i
++)
1236 for (i
= 0; ciphersuites
[i
] != 0; i
++)
1237 for (j
= 0, p
= buf
+ 6; j
< ciph_len
; j
+= 3, p
+= 3)
1241 p
[1] != ((ciphersuites
[i
] >> 8) & 0xFF) ||
1242 p
[2] != ((ciphersuites
[i
]) & 0xFF))
1245 got_common_suite
= 1;
1247 if ((ret
= ssl_ciphersuite_match(ssl
, ciphersuites
[i
],
1248 &ciphersuite_info
)) != 0)
1251 if (ciphersuite_info
!= NULL
)
1252 goto have_ciphersuite_v2
;
1255 if (got_common_suite
) {
1256 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
1257 "but none of them usable"));
1258 return (MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE
);
1260 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
1261 return (MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN
);
1264 have_ciphersuite_v2
:
1265 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info
->name
));
1267 ssl
->session_negotiate
->ciphersuite
= ciphersuites
[i
];
1268 ssl
->handshake
->ciphersuite_info
= ciphersuite_info
;
1271 * SSLv2 Client Hello relevant renegotiation security checks
1273 if (ssl
->secure_renegotiation
== MBEDTLS_SSL_LEGACY_RENEGOTIATION
&&
1274 ssl
->conf
->allow_legacy_renegotiation
== MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE
) {
1275 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
1276 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1277 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
1278 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1284 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello v2"));
1288 #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1290 /* This function doesn't alert on errors that happen early during
1291 ClientHello parsing because they might indicate that the client is
1292 not talking SSL/TLS at all and would not understand our alert. */
1293 static int ssl_parse_client_hello(mbedtls_ssl_context
*ssl
) {
1294 int ret
, got_common_suite
;
1296 size_t ciph_offset
, comp_offset
, ext_offset
;
1297 size_t msg_len
, ciph_len
, sess_len
, comp_len
, ext_len
;
1298 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1299 size_t cookie_offset
, cookie_len
;
1301 unsigned char *buf
, *p
, *ext
;
1302 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1303 int renegotiation_info_seen
= 0;
1305 int handshake_failure
= 0;
1306 const int *ciphersuites
;
1307 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
;
1310 /* If there is no signature-algorithm extension present,
1311 * we need to fall back to the default values for allowed
1312 * signature-hash pairs. */
1313 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1314 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1315 int sig_hash_alg_ext_present
= 0;
1316 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1317 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1319 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
1321 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1325 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
1326 * otherwise read it ourselves manually in order to support SSLv2
1327 * ClientHello, which doesn't use the same record layer format.
1329 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1330 if (ssl
->renego_status
== MBEDTLS_SSL_INITIAL_HANDSHAKE
)
1333 if ((ret
= mbedtls_ssl_fetch_input(ssl
, 5)) != 0) {
1334 /* No alert on a read error. */
1335 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret
);
1342 #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1343 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1344 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_STREAM
)
1346 if ((buf
[0] & 0x80) != 0)
1347 return (ssl_parse_client_hello_v2(ssl
));
1350 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf
, mbedtls_ssl_in_hdr_len(ssl
));
1353 * SSLv3/TLS Client Hello
1356 * 0 . 0 message type
1357 * 1 . 2 protocol version
1358 * 3 . 11 DTLS: epoch + record sequence number
1359 * 3 . 4 message length
1361 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message type: %d",
1364 if (buf
[0] != MBEDTLS_SSL_MSG_HANDSHAKE
) {
1365 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1366 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1369 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message len.: %d",
1370 (ssl
->in_len
[0] << 8) | ssl
->in_len
[1]));
1372 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, protocol version: [%d:%d]",
1375 mbedtls_ssl_read_version(&major
, &minor
, ssl
->conf
->transport
, buf
+ 1);
1377 /* According to RFC 5246 Appendix E.1, the version here is typically
1378 * "{03,00}, the lowest version number supported by the client, [or] the
1379 * value of ClientHello.client_version", so the only meaningful check here
1380 * is the major version shouldn't be less than 3 */
1381 if (major
< MBEDTLS_SSL_MAJOR_VERSION_3
) {
1382 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1383 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1386 /* For DTLS if this is the initial handshake, remember the client sequence
1387 * number to use it in our next message (RFC 6347 4.2.1) */
1388 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1389 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
1390 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1391 && ssl
->renego_status
== MBEDTLS_SSL_INITIAL_HANDSHAKE
1394 /* Epoch should be 0 for initial handshakes */
1395 if (ssl
->in_ctr
[0] != 0 || ssl
->in_ctr
[1] != 0) {
1396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1397 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1400 memcpy(ssl
->cur_out_ctr
+ 2, ssl
->in_ctr
+ 2, 6);
1402 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1403 if (mbedtls_ssl_dtls_replay_check(ssl
) != 0) {
1404 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record, discarding"));
1405 ssl
->next_record_offset
= 0;
1407 goto read_record_header
;
1410 /* No MAC to check yet, so we can update right now */
1411 mbedtls_ssl_dtls_replay_update(ssl
);
1414 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1416 msg_len
= (ssl
->in_len
[0] << 8) | ssl
->in_len
[1];
1418 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1419 if (ssl
->renego_status
!= MBEDTLS_SSL_INITIAL_HANDSHAKE
) {
1420 /* Set by mbedtls_ssl_read_record() */
1421 msg_len
= ssl
->in_hslen
;
1425 if (msg_len
> MBEDTLS_SSL_IN_CONTENT_LEN
) {
1426 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1427 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1430 if ((ret
= mbedtls_ssl_fetch_input(ssl
,
1431 mbedtls_ssl_in_hdr_len(ssl
) + msg_len
)) != 0) {
1432 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret
);
1436 /* Done reading this record, get ready for the next one */
1437 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1438 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
)
1439 ssl
->next_record_offset
= msg_len
+ mbedtls_ssl_in_hdr_len(ssl
);
1447 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf
, msg_len
);
1449 ssl
->handshake
->update_checksum(ssl
, buf
, msg_len
);
1453 * 0 . 0 handshake type
1454 * 1 . 3 handshake length
1455 * 4 . 5 DTLS only: message seqence number
1456 * 6 . 8 DTLS only: fragment offset
1457 * 9 . 11 DTLS only: fragment length
1459 if (msg_len
< mbedtls_ssl_hs_hdr_len(ssl
)) {
1460 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1461 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1464 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake type: %d", buf
[0]));
1466 if (buf
[0] != MBEDTLS_SSL_HS_CLIENT_HELLO
) {
1467 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1468 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1471 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %d",
1472 (buf
[1] << 16) | (buf
[2] << 8) | buf
[3]));
1474 /* We don't support fragmentation of ClientHello (yet?) */
1476 msg_len
!= mbedtls_ssl_hs_hdr_len(ssl
) + ((buf
[2] << 8) | buf
[3])) {
1477 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1478 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1481 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1482 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
) {
1484 * Copy the client's handshake message_seq on initial handshakes,
1485 * check sequence number on renego.
1487 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1488 if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
) {
1489 /* This couldn't be done in ssl_prepare_handshake_record() */
1490 unsigned int cli_msg_seq
= (ssl
->in_msg
[4] << 8) |
1493 if (cli_msg_seq
!= ssl
->handshake
->in_msg_seq
) {
1494 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message_seq: "
1495 "%u (expected %u)", cli_msg_seq
,
1496 ssl
->handshake
->in_msg_seq
));
1497 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1500 ssl
->handshake
->in_msg_seq
++;
1504 unsigned int cli_msg_seq
= (ssl
->in_msg
[4] << 8) |
1506 ssl
->handshake
->out_msg_seq
= cli_msg_seq
;
1507 ssl
->handshake
->in_msg_seq
= cli_msg_seq
+ 1;
1511 * For now we don't support fragmentation, so make sure
1512 * fragment_offset == 0 and fragment_length == length
1514 if (ssl
->in_msg
[6] != 0 || ssl
->in_msg
[7] != 0 || ssl
->in_msg
[8] != 0 ||
1515 memcmp(ssl
->in_msg
+ 1, ssl
->in_msg
+ 9, 3) != 0) {
1516 MBEDTLS_SSL_DEBUG_MSG(1, ("ClientHello fragmentation not supported"));
1517 return (MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
);
1520 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1522 buf
+= mbedtls_ssl_hs_hdr_len(ssl
);
1523 msg_len
-= mbedtls_ssl_hs_hdr_len(ssl
);
1526 * ClientHello layer:
1527 * 0 . 1 protocol version
1528 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1529 * 34 . 35 session id length (1 byte)
1530 * 35 . 34+x session id
1531 * 35+x . 35+x DTLS only: cookie length (1 byte)
1532 * 36+x . .. DTLS only: cookie
1533 * .. . .. ciphersuite list length (2 bytes)
1534 * .. . .. ciphersuite list
1535 * .. . .. compression alg. list length (1 byte)
1536 * .. . .. compression alg. list
1537 * .. . .. extensions length (2 bytes, optional)
1538 * .. . .. extensions (optional)
1542 * Minimal length (with everything empty and extensions omitted) is
1543 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1544 * read at least up to session id length without worrying.
1547 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1548 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1552 * Check and save the protocol version
1554 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf
, 2);
1556 mbedtls_ssl_read_version(&ssl
->major_ver
, &ssl
->minor_ver
,
1557 ssl
->conf
->transport
, buf
);
1559 ssl
->handshake
->max_major_ver
= ssl
->major_ver
;
1560 ssl
->handshake
->max_minor_ver
= ssl
->minor_ver
;
1562 if (ssl
->major_ver
< ssl
->conf
->min_major_ver
||
1563 ssl
->minor_ver
< ssl
->conf
->min_minor_ver
) {
1564 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1565 " [%d:%d] < [%d:%d]",
1566 ssl
->major_ver
, ssl
->minor_ver
,
1567 ssl
->conf
->min_major_ver
, ssl
->conf
->min_minor_ver
));
1568 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1569 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION
);
1570 return (MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION
);
1573 if (ssl
->major_ver
> ssl
->conf
->max_major_ver
) {
1574 ssl
->major_ver
= ssl
->conf
->max_major_ver
;
1575 ssl
->minor_ver
= ssl
->conf
->max_minor_ver
;
1576 } else if (ssl
->minor_ver
> ssl
->conf
->max_minor_ver
)
1577 ssl
->minor_ver
= ssl
->conf
->max_minor_ver
;
1580 * Save client random (inc. Unix time)
1582 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf
+ 2, 32);
1584 memcpy(ssl
->handshake
->randbytes
, buf
+ 2, 32);
1587 * Check the session ID length and save session ID
1591 if (sess_len
> sizeof(ssl
->session_negotiate
->id
) ||
1592 sess_len
+ 34 + 2 > msg_len
) { /* 2 for cipherlist length field */
1593 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1594 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1595 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1596 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1599 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf
+ 35, sess_len
);
1601 ssl
->session_negotiate
->id_len
= sess_len
;
1602 memset(ssl
->session_negotiate
->id
, 0,
1603 sizeof(ssl
->session_negotiate
->id
));
1604 memcpy(ssl
->session_negotiate
->id
, buf
+ 35,
1605 ssl
->session_negotiate
->id_len
);
1608 * Check the cookie length and content
1610 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1611 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
) {
1612 cookie_offset
= 35 + sess_len
;
1613 cookie_len
= buf
[cookie_offset
];
1615 if (cookie_offset
+ 1 + cookie_len
+ 2 > msg_len
) {
1616 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1617 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1618 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION
);
1619 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1622 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
1623 buf
+ cookie_offset
+ 1, cookie_len
);
1625 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1626 if (ssl
->conf
->f_cookie_check
!= NULL
1627 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1628 && ssl
->renego_status
== MBEDTLS_SSL_INITIAL_HANDSHAKE
1631 if (ssl
->conf
->f_cookie_check(ssl
->conf
->p_cookie
,
1632 buf
+ cookie_offset
+ 1, cookie_len
,
1633 ssl
->cli_id
, ssl
->cli_id_len
) != 0) {
1634 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
1635 ssl
->handshake
->verify_cookie_len
= 1;
1637 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
1638 ssl
->handshake
->verify_cookie_len
= 0;
1641 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1643 /* We know we didn't send a cookie, so it should be empty */
1644 if (cookie_len
!= 0) {
1645 /* This may be an attacker's probe, so don't send an alert */
1646 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1647 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1650 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
1654 * Check the ciphersuitelist length (will be parsed later)
1656 ciph_offset
= cookie_offset
+ 1 + cookie_len
;
1658 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1659 ciph_offset
= 35 + sess_len
;
1661 ciph_len
= (buf
[ciph_offset
+ 0] << 8)
1662 | (buf
[ciph_offset
+ 1]);
1665 ciph_len
+ 2 + ciph_offset
+ 1 > msg_len
|| /* 1 for comp. alg. len */
1666 (ciph_len
% 2) != 0) {
1667 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1668 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1669 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1670 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1673 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1674 buf
+ ciph_offset
+ 2, ciph_len
);
1677 * Check the compression algorithms length and pick one
1679 comp_offset
= ciph_offset
+ 2 + ciph_len
;
1681 comp_len
= buf
[comp_offset
];
1685 comp_len
+ comp_offset
+ 1 > msg_len
) {
1686 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1687 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1688 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1689 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1692 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
1693 buf
+ comp_offset
+ 1, comp_len
);
1695 ssl
->session_negotiate
->compression
= MBEDTLS_SSL_COMPRESS_NULL
;
1696 #if defined(MBEDTLS_ZLIB_SUPPORT)
1697 for (i
= 0; i
< comp_len
; ++i
) {
1698 if (buf
[comp_offset
+ 1 + i
] == MBEDTLS_SSL_COMPRESS_DEFLATE
) {
1699 ssl
->session_negotiate
->compression
= MBEDTLS_SSL_COMPRESS_DEFLATE
;
1705 /* See comments in ssl_write_client_hello() */
1706 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1707 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
)
1708 ssl
->session_negotiate
->compression
= MBEDTLS_SSL_COMPRESS_NULL
;
1711 /* Do not parse the extensions if the protocol is SSLv3 */
1712 #if defined(MBEDTLS_SSL_PROTO_SSL3)
1713 if ((ssl
->major_ver
!= 3) || (ssl
->minor_ver
!= 0)) {
1716 * Check the extension length
1718 ext_offset
= comp_offset
+ 1 + comp_len
;
1719 if (msg_len
> ext_offset
) {
1720 if (msg_len
< ext_offset
+ 2) {
1721 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1722 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1724 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1727 ext_len
= (buf
[ext_offset
+ 0] << 8)
1728 | (buf
[ext_offset
+ 1]);
1730 if ((ext_len
> 0 && ext_len
< 4) ||
1731 msg_len
!= ext_offset
+ 2 + ext_len
) {
1732 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1733 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1734 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1735 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1740 ext
= buf
+ ext_offset
+ 2;
1741 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext
, ext_len
);
1743 while (ext_len
!= 0) {
1744 unsigned int ext_id
;
1745 unsigned int ext_size
;
1747 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1748 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1749 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1750 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1752 ext_id
= ((ext
[0] << 8) | (ext
[1]));
1753 ext_size
= ((ext
[2] << 8) | (ext
[3]));
1755 if (ext_size
+ 4 > ext_len
) {
1756 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1757 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1758 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1759 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1762 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1763 case MBEDTLS_TLS_EXT_SERVERNAME
:
1764 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1765 if (ssl
->conf
->f_sni
== NULL
)
1768 ret
= ssl_parse_servername_ext(ssl
, ext
+ 4, ext_size
);
1772 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1774 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO
:
1775 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
1776 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1777 renegotiation_info_seen
= 1;
1780 ret
= ssl_parse_renegotiation_info(ssl
, ext
+ 4, ext_size
);
1785 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1786 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1787 case MBEDTLS_TLS_EXT_SIG_ALG
:
1788 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
1790 ret
= ssl_parse_signature_algorithms_ext(ssl
, ext
+ 4, ext_size
);
1794 sig_hash_alg_ext_present
= 1;
1796 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1797 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1799 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1800 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1801 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES
:
1802 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
1804 ret
= ssl_parse_supported_elliptic_curves(ssl
, ext
+ 4, ext_size
);
1809 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS
:
1810 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
1811 ssl
->handshake
->cli_exts
|= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT
;
1813 ret
= ssl_parse_supported_point_formats(ssl
, ext
+ 4, ext_size
);
1817 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1818 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1820 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1821 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP
:
1822 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
1824 ret
= ssl_parse_ecjpake_kkpp(ssl
, ext
+ 4, ext_size
);
1828 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1830 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1831 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH
:
1832 MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
1834 ret
= ssl_parse_max_fragment_length_ext(ssl
, ext
+ 4, ext_size
);
1838 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1840 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1841 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC
:
1842 MBEDTLS_SSL_DEBUG_MSG(3, ("found truncated hmac extension"));
1844 ret
= ssl_parse_truncated_hmac_ext(ssl
, ext
+ 4, ext_size
);
1848 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1850 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1851 case MBEDTLS_TLS_EXT_CID
:
1852 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
1854 ret
= ssl_parse_cid_ext(ssl
, ext
+ 4, ext_size
);
1858 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1860 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1861 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC
:
1862 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
1864 ret
= ssl_parse_encrypt_then_mac_ext(ssl
, ext
+ 4, ext_size
);
1868 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1870 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1871 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET
:
1872 MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
1874 ret
= ssl_parse_extended_ms_ext(ssl
, ext
+ 4, ext_size
);
1878 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1880 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1881 case MBEDTLS_TLS_EXT_SESSION_TICKET
:
1882 MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
1884 ret
= ssl_parse_session_ticket_ext(ssl
, ext
+ 4, ext_size
);
1888 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1890 #if defined(MBEDTLS_SSL_ALPN)
1891 case MBEDTLS_TLS_EXT_ALPN
:
1892 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
1894 ret
= ssl_parse_alpn_ext(ssl
, ext
+ 4, ext_size
);
1898 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1900 #if defined(MBEDTLS_SSL_DTLS_SRTP)
1901 case MBEDTLS_TLS_EXT_USE_SRTP
:
1902 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
1904 ret
= ssl_parse_use_srtp_ext(ssl
, ext
+ 4, ext_size
);
1908 #endif /* MBEDTLS_SSL_DTLS_SRTP */
1911 MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
1915 ext_len
-= 4 + ext_size
;
1916 ext
+= 4 + ext_size
;
1918 if (ext_len
> 0 && ext_len
< 4) {
1919 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1920 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR
);
1922 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1925 #if defined(MBEDTLS_SSL_PROTO_SSL3)
1929 #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1930 for (i
= 0, p
= buf
+ ciph_offset
+ 2; i
< ciph_len
; i
+= 2, p
+= 2) {
1931 if (p
[0] == (unsigned char)((MBEDTLS_SSL_FALLBACK_SCSV_VALUE
>> 8) & 0xff) &&
1932 p
[1] == (unsigned char)((MBEDTLS_SSL_FALLBACK_SCSV_VALUE
) & 0xff)) {
1933 MBEDTLS_SSL_DEBUG_MSG(2, ("received FALLBACK_SCSV"));
1935 if (ssl
->minor_ver
< ssl
->conf
->max_minor_ver
) {
1936 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
1938 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1939 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK
);
1941 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1947 #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1949 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1950 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1953 * Try to fall back to default hash SHA1 if the client
1954 * hasn't provided any preferred signature-hash combinations.
1956 if (sig_hash_alg_ext_present
== 0) {
1957 mbedtls_md_type_t md_default
= MBEDTLS_MD_SHA1
;
1959 if (mbedtls_ssl_check_sig_hash(ssl
, md_default
) != 0)
1960 md_default
= MBEDTLS_MD_NONE
;
1962 mbedtls_ssl_sig_hash_set_const_hash(&ssl
->handshake
->hash_algs
, md_default
);
1965 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1966 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1969 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1971 for (i
= 0, p
= buf
+ ciph_offset
+ 2; i
< ciph_len
; i
+= 2, p
+= 2) {
1972 if (p
[0] == 0 && p
[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO
) {
1973 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
1974 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1975 if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
) {
1976 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
1977 "during renegotiation"));
1978 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
1979 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
1980 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1983 ssl
->secure_renegotiation
= MBEDTLS_SSL_SECURE_RENEGOTIATION
;
1989 * Renegotiation security checks
1991 if (ssl
->secure_renegotiation
!= MBEDTLS_SSL_SECURE_RENEGOTIATION
&&
1992 ssl
->conf
->allow_legacy_renegotiation
== MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE
) {
1993 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
1994 handshake_failure
= 1;
1996 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1997 else if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
&&
1998 ssl
->secure_renegotiation
== MBEDTLS_SSL_SECURE_RENEGOTIATION
&&
1999 renegotiation_info_seen
== 0) {
2000 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
2001 handshake_failure
= 1;
2002 } else if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
&&
2003 ssl
->secure_renegotiation
== MBEDTLS_SSL_LEGACY_RENEGOTIATION
&&
2004 ssl
->conf
->allow_legacy_renegotiation
== MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION
) {
2005 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
2006 handshake_failure
= 1;
2007 } else if (ssl
->renego_status
== MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS
&&
2008 ssl
->secure_renegotiation
== MBEDTLS_SSL_LEGACY_RENEGOTIATION
&&
2009 renegotiation_info_seen
== 1) {
2010 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
2011 handshake_failure
= 1;
2013 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2015 if (handshake_failure
== 1) {
2016 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
2017 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
2018 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO
);
2022 * Search for a matching ciphersuite
2023 * (At the end because we need information from the EC-based extensions
2024 * and certificate from the SNI callback triggered by the SNI extension.)
2026 got_common_suite
= 0;
2027 ciphersuites
= ssl
->conf
->ciphersuite_list
[ssl
->minor_ver
];
2028 ciphersuite_info
= NULL
;
2029 #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
2030 for (j
= 0, p
= buf
+ ciph_offset
+ 2; j
< ciph_len
; j
+= 2, p
+= 2)
2031 for (i
= 0; ciphersuites
[i
] != 0; i
++)
2033 for (i
= 0; ciphersuites
[i
] != 0; i
++)
2034 for (j
= 0, p
= buf
+ ciph_offset
+ 2; j
< ciph_len
; j
+= 2, p
+= 2)
2037 if (p
[0] != ((ciphersuites
[i
] >> 8) & 0xFF) ||
2038 p
[1] != ((ciphersuites
[i
]) & 0xFF))
2041 got_common_suite
= 1;
2043 if ((ret
= ssl_ciphersuite_match(ssl
, ciphersuites
[i
],
2044 &ciphersuite_info
)) != 0)
2047 if (ciphersuite_info
!= NULL
)
2048 goto have_ciphersuite
;
2051 if (got_common_suite
) {
2052 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
2053 "but none of them usable"));
2054 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
2055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
2056 return (MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE
);
2058 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
2059 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
2060 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE
);
2061 return (MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN
);
2065 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info
->name
));
2067 ssl
->session_negotiate
->ciphersuite
= ciphersuites
[i
];
2068 ssl
->handshake
->ciphersuite_info
= ciphersuite_info
;
2072 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2073 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
)
2074 mbedtls_ssl_recv_flight_completed(ssl
);
2077 /* Debugging-only output for testsuite */
2078 #if defined(MBEDTLS_DEBUG_C) && \
2079 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
2080 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
2081 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
2082 mbedtls_pk_type_t sig_alg
= mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info
);
2083 if (sig_alg
!= MBEDTLS_PK_NONE
) {
2084 mbedtls_md_type_t md_alg
= mbedtls_ssl_sig_hash_set_find(&ssl
->handshake
->hash_algs
,
2086 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %d",
2087 mbedtls_ssl_hash_from_md_alg(md_alg
)));
2089 MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
2090 "%u - should not happen", (unsigned) sig_alg
));
2095 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
2100 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2101 static void ssl_write_truncated_hmac_ext(mbedtls_ssl_context
*ssl
,
2104 unsigned char *p
= buf
;
2106 if (ssl
->session_negotiate
->trunc_hmac
== MBEDTLS_SSL_TRUNC_HMAC_DISABLED
) {
2111 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding truncated hmac extension"));
2113 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_TRUNCATED_HMAC
>> 8) & 0xFF);
2114 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_TRUNCATED_HMAC
) & 0xFF);
2121 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2123 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2124 static void ssl_write_cid_ext(mbedtls_ssl_context
*ssl
,
2127 unsigned char *p
= buf
;
2129 const unsigned char *end
= ssl
->out_msg
+ MBEDTLS_SSL_OUT_CONTENT_LEN
;
2133 /* Skip writing the extension if we don't want to use it or if
2134 * the client hasn't offered it. */
2135 if (ssl
->handshake
->cid_in_use
== MBEDTLS_SSL_CID_DISABLED
)
2138 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2139 * which is at most 255, so the increment cannot overflow. */
2140 if (end
< p
|| (size_t)(end
- p
) < (unsigned)(ssl
->own_cid_len
+ 5)) {
2141 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2145 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
2148 * Quoting draft-ietf-tls-dtls-connection-id-05
2149 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
2152 * opaque cid<0..2^8-1>;
2156 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_CID
>> 8) & 0xFF);
2157 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_CID
) & 0xFF);
2158 ext_len
= (size_t) ssl
->own_cid_len
+ 1;
2159 *p
++ = (unsigned char)((ext_len
>> 8) & 0xFF);
2160 *p
++ = (unsigned char)((ext_len
) & 0xFF);
2162 *p
++ = (uint8_t) ssl
->own_cid_len
;
2163 memcpy(p
, ssl
->own_cid
, ssl
->own_cid_len
);
2165 *olen
= ssl
->own_cid_len
+ 5;
2167 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2169 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2170 static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context
*ssl
,
2173 unsigned char *p
= buf
;
2174 const mbedtls_ssl_ciphersuite_t
*suite
= NULL
;
2175 const mbedtls_cipher_info_t
*cipher
= NULL
;
2177 if (ssl
->session_negotiate
->encrypt_then_mac
== MBEDTLS_SSL_ETM_DISABLED
||
2178 ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_0
) {
2184 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2185 * from a client and then selects a stream or Authenticated Encryption
2186 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2187 * encrypt-then-MAC response extension back to the client."
2189 if ((suite
= mbedtls_ssl_ciphersuite_from_id(
2190 ssl
->session_negotiate
->ciphersuite
)) == NULL
||
2191 (cipher
= mbedtls_cipher_info_from_type(suite
->cipher
)) == NULL
||
2192 cipher
->mode
!= MBEDTLS_MODE_CBC
) {
2197 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
2199 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC
>> 8) & 0xFF);
2200 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC
) & 0xFF);
2207 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2209 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2210 static void ssl_write_extended_ms_ext(mbedtls_ssl_context
*ssl
,
2213 unsigned char *p
= buf
;
2215 if (ssl
->handshake
->extended_ms
== MBEDTLS_SSL_EXTENDED_MS_DISABLED
||
2216 ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_0
) {
2221 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
2224 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET
>> 8) & 0xFF);
2225 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET
) & 0xFF);
2232 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2234 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2235 static void ssl_write_session_ticket_ext(mbedtls_ssl_context
*ssl
,
2238 unsigned char *p
= buf
;
2240 if (ssl
->handshake
->new_session_ticket
== 0) {
2245 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
2247 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_SESSION_TICKET
>> 8) & 0xFF);
2248 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_SESSION_TICKET
) & 0xFF);
2255 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
2257 static void ssl_write_renegotiation_ext(mbedtls_ssl_context
*ssl
,
2260 unsigned char *p
= buf
;
2262 if (ssl
->secure_renegotiation
!= MBEDTLS_SSL_SECURE_RENEGOTIATION
) {
2267 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
2269 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_RENEGOTIATION_INFO
>> 8) & 0xFF);
2270 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_RENEGOTIATION_INFO
) & 0xFF);
2272 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2273 if (ssl
->renego_status
!= MBEDTLS_SSL_INITIAL_HANDSHAKE
) {
2275 *p
++ = (ssl
->verify_data_len
* 2 + 1) & 0xFF;
2276 *p
++ = ssl
->verify_data_len
* 2 & 0xFF;
2278 memcpy(p
, ssl
->peer_verify_data
, ssl
->verify_data_len
);
2279 p
+= ssl
->verify_data_len
;
2280 memcpy(p
, ssl
->own_verify_data
, ssl
->verify_data_len
);
2281 p
+= ssl
->verify_data_len
;
2283 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2293 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2294 static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context
*ssl
,
2297 unsigned char *p
= buf
;
2299 if (ssl
->session_negotiate
->mfl_code
== MBEDTLS_SSL_MAX_FRAG_LEN_NONE
) {
2304 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
2306 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH
>> 8) & 0xFF);
2307 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH
) & 0xFF);
2312 *p
++ = ssl
->session_negotiate
->mfl_code
;
2316 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2318 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2319 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2320 static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context
*ssl
,
2323 unsigned char *p
= buf
;
2326 if ((ssl
->handshake
->cli_exts
&
2327 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT
) == 0) {
2332 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
2334 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS
>> 8) & 0xFF);
2335 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS
) & 0xFF);
2341 *p
++ = MBEDTLS_ECP_PF_UNCOMPRESSED
;
2345 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2347 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2348 static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context
*ssl
,
2351 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
2352 unsigned char *p
= buf
;
2353 const unsigned char *end
= ssl
->out_msg
+ MBEDTLS_SSL_OUT_CONTENT_LEN
;
2358 /* Skip costly computation if not needed */
2359 if (ssl
->handshake
->ciphersuite_info
->key_exchange
!=
2360 MBEDTLS_KEY_EXCHANGE_ECJPAKE
)
2363 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
2366 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2370 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_ECJPAKE_KKPP
>> 8) & 0xFF);
2371 *p
++ = (unsigned char)((MBEDTLS_TLS_EXT_ECJPAKE_KKPP
) & 0xFF);
2373 ret
= mbedtls_ecjpake_write_round_one(&ssl
->handshake
->ecjpake_ctx
,
2374 p
+ 2, end
- p
- 2, &kkpp_len
,
2375 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
);
2377 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret
);
2381 *p
++ = (unsigned char)((kkpp_len
>> 8) & 0xFF);
2382 *p
++ = (unsigned char)((kkpp_len
) & 0xFF);
2384 *olen
= kkpp_len
+ 4;
2386 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2388 #if defined(MBEDTLS_SSL_ALPN )
2389 static void ssl_write_alpn_ext(mbedtls_ssl_context
*ssl
,
2390 unsigned char *buf
, size_t *olen
) {
2391 if (ssl
->alpn_chosen
== NULL
) {
2396 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding alpn extension"));
2399 * 0 . 1 ext identifier
2401 * 4 . 5 protocol list length
2402 * 6 . 6 protocol name length
2403 * 7 . 7+n protocol name
2405 buf
[0] = (unsigned char)((MBEDTLS_TLS_EXT_ALPN
>> 8) & 0xFF);
2406 buf
[1] = (unsigned char)((MBEDTLS_TLS_EXT_ALPN
) & 0xFF);
2408 *olen
= 7 + strlen(ssl
->alpn_chosen
);
2410 buf
[2] = (unsigned char)(((*olen
- 4) >> 8) & 0xFF);
2411 buf
[3] = (unsigned char)(((*olen
- 4)) & 0xFF);
2413 buf
[4] = (unsigned char)(((*olen
- 6) >> 8) & 0xFF);
2414 buf
[5] = (unsigned char)(((*olen
- 6)) & 0xFF);
2416 buf
[6] = (unsigned char)(((*olen
- 7)) & 0xFF);
2418 memcpy(buf
+ 7, ssl
->alpn_chosen
, *olen
- 7);
2420 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
2422 #if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
2423 static void ssl_write_use_srtp_ext(mbedtls_ssl_context
*ssl
,
2426 size_t mki_len
= 0, ext_len
= 0;
2427 uint16_t profile_value
= 0;
2428 const unsigned char *end
= ssl
->out_msg
+ MBEDTLS_SSL_OUT_CONTENT_LEN
;
2432 if ((ssl
->conf
->transport
!= MBEDTLS_SSL_TRANSPORT_DATAGRAM
) ||
2433 (ssl
->dtls_srtp_info
.chosen_dtls_srtp_profile
== MBEDTLS_TLS_SRTP_UNSET
)) {
2437 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
2439 if (ssl
->conf
->dtls_srtp_mki_support
== MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED
) {
2440 mki_len
= ssl
->dtls_srtp_info
.mki_len
;
2443 /* The extension total size is 9 bytes :
2444 * - 2 bytes for the extension tag
2445 * - 2 bytes for the total size
2446 * - 2 bytes for the protection profile length
2447 * - 2 bytes for the protection profile
2448 * - 1 byte for the mki length
2449 * + the actual mki length
2450 * Check we have enough room in the output buffer */
2451 if ((size_t)(end
- buf
) < mki_len
+ 9) {
2452 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2457 buf
[0] = (unsigned char)((MBEDTLS_TLS_EXT_USE_SRTP
>> 8) & 0xFF);
2458 buf
[1] = (unsigned char)((MBEDTLS_TLS_EXT_USE_SRTP
) & 0xFF);
2460 * total length 5 and mki value: only one profile(2 bytes)
2461 * and length(2 bytes) and srtp_mki )
2463 ext_len
= 5 + mki_len
;
2464 buf
[2] = (unsigned char)((ext_len
>> 8) & 0xFF);
2465 buf
[3] = (unsigned char)(ext_len
& 0xFF);
2467 /* protection profile length: 2 */
2470 profile_value
= mbedtls_ssl_check_srtp_profile_value(
2471 ssl
->dtls_srtp_info
.chosen_dtls_srtp_profile
);
2472 if (profile_value
!= MBEDTLS_TLS_SRTP_UNSET
) {
2473 buf
[6] = (unsigned char)((profile_value
>> 8) & 0xFF);
2474 buf
[7] = (unsigned char)(profile_value
& 0xFF);
2476 MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
2480 buf
[8] = mki_len
& 0xFF;
2481 memcpy(&buf
[9], ssl
->dtls_srtp_info
.mki_value
, mki_len
);
2483 *olen
= 9 + mki_len
;
2485 #endif /* MBEDTLS_SSL_DTLS_SRTP */
2487 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2488 static int ssl_write_hello_verify_request(mbedtls_ssl_context
*ssl
) {
2489 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
2490 unsigned char *p
= ssl
->out_msg
+ 4;
2491 unsigned char *cookie_len_byte
;
2493 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
2497 * ProtocolVersion server_version;
2498 * opaque cookie<0..2^8-1>;
2499 * } HelloVerifyRequest;
2502 /* The RFC is not clear on this point, but sending the actual negotiated
2503 * version looks like the most interoperable thing to do. */
2504 mbedtls_ssl_write_version(ssl
->major_ver
, ssl
->minor_ver
,
2505 ssl
->conf
->transport
, p
);
2506 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p
, 2);
2509 /* If we get here, f_cookie_check is not null */
2510 if (ssl
->conf
->f_cookie_write
== NULL
) {
2511 MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
2512 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
2515 /* Skip length byte until we know the length */
2516 cookie_len_byte
= p
++;
2518 if ((ret
= ssl
->conf
->f_cookie_write(ssl
->conf
->p_cookie
,
2519 &p
, ssl
->out_buf
+ MBEDTLS_SSL_OUT_BUFFER_LEN
,
2520 ssl
->cli_id
, ssl
->cli_id_len
)) != 0) {
2521 MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret
);
2525 *cookie_len_byte
= (unsigned char)(p
- (cookie_len_byte
+ 1));
2527 MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte
+ 1, *cookie_len_byte
);
2529 ssl
->out_msglen
= p
- ssl
->out_msg
;
2530 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
2531 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST
;
2533 ssl
->state
= MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT
;
2535 if ((ret
= mbedtls_ssl_write_handshake_msg(ssl
)) != 0) {
2536 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret
);
2540 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2541 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
&&
2542 (ret
= mbedtls_ssl_flight_transmit(ssl
)) != 0) {
2543 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret
);
2546 #endif /* MBEDTLS_SSL_PROTO_DTLS */
2548 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
2552 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2554 static int ssl_write_server_hello(mbedtls_ssl_context
*ssl
) {
2555 #if defined(MBEDTLS_HAVE_TIME)
2558 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
2559 size_t olen
, ext_len
= 0, n
;
2560 unsigned char *buf
, *p
;
2562 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
2564 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2565 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
&&
2566 ssl
->handshake
->verify_cookie_len
!= 0) {
2567 MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
2568 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2570 return (ssl_write_hello_verify_request(ssl
));
2572 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2574 if (ssl
->conf
->f_rng
== NULL
) {
2575 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
2576 return (MBEDTLS_ERR_SSL_NO_RNG
);
2580 * 0 . 0 handshake type
2581 * 1 . 3 handshake length
2582 * 4 . 5 protocol version
2584 * 10 . 37 random bytes
2589 mbedtls_ssl_write_version(ssl
->major_ver
, ssl
->minor_ver
,
2590 ssl
->conf
->transport
, p
);
2593 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
2596 #if defined(MBEDTLS_HAVE_TIME)
2597 t
= mbedtls_time(NULL
);
2598 *p
++ = (unsigned char)(t
>> 24);
2599 *p
++ = (unsigned char)(t
>> 16);
2600 *p
++ = (unsigned char)(t
>> 8);
2601 *p
++ = (unsigned char)(t
);
2603 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG
,
2606 if ((ret
= ssl
->conf
->f_rng(ssl
->conf
->p_rng
, p
, 4)) != 0)
2610 #endif /* MBEDTLS_HAVE_TIME */
2612 if ((ret
= ssl
->conf
->f_rng(ssl
->conf
->p_rng
, p
, 28)) != 0)
2617 memcpy(ssl
->handshake
->randbytes
+ 32, buf
+ 6, 32);
2619 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf
+ 6, 32);
2622 * Resume is 0 by default, see ssl_handshake_init().
2623 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2624 * If not, try looking up session ID in our cache.
2626 if (ssl
->handshake
->resume
== 0 &&
2627 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2628 ssl
->renego_status
== MBEDTLS_SSL_INITIAL_HANDSHAKE
&&
2630 ssl
->session_negotiate
->id_len
!= 0 &&
2631 ssl
->conf
->f_get_cache
!= NULL
&&
2632 ssl
->conf
->f_get_cache(ssl
->conf
->p_cache
, ssl
->session_negotiate
) == 0) {
2633 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
2634 ssl
->handshake
->resume
= 1;
2637 if (ssl
->handshake
->resume
== 0) {
2639 * New session, create a new session id,
2640 * unless we're about to issue a session ticket
2644 #if defined(MBEDTLS_HAVE_TIME)
2645 ssl
->session_negotiate
->start
= mbedtls_time(NULL
);
2648 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2649 if (ssl
->handshake
->new_session_ticket
!= 0) {
2650 ssl
->session_negotiate
->id_len
= n
= 0;
2651 memset(ssl
->session_negotiate
->id
, 0, 32);
2653 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
2655 ssl
->session_negotiate
->id_len
= n
= 32;
2656 if ((ret
= ssl
->conf
->f_rng(ssl
->conf
->p_rng
, ssl
->session_negotiate
->id
,
2662 * Resuming a session
2664 n
= ssl
->session_negotiate
->id_len
;
2665 ssl
->state
= MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC
;
2667 if ((ret
= mbedtls_ssl_derive_keys(ssl
)) != 0) {
2668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret
);
2674 * 38 . 38 session id length
2675 * 39 . 38+n session id
2676 * 39+n . 40+n chosen ciphersuite
2677 * 41+n . 41+n chosen compression alg.
2678 * 42+n . 43+n extensions length
2679 * 44+n . 43+n+m extensions
2681 *p
++ = (unsigned char) ssl
->session_negotiate
->id_len
;
2682 memcpy(p
, ssl
->session_negotiate
->id
, ssl
->session_negotiate
->id_len
);
2683 p
+= ssl
->session_negotiate
->id_len
;
2685 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET
, n
));
2686 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf
+ 39, n
);
2687 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
2688 ssl
->handshake
->resume
? "a" : "no"));
2690 *p
++ = (unsigned char)(ssl
->session_negotiate
->ciphersuite
>> 8);
2691 *p
++ = (unsigned char)(ssl
->session_negotiate
->ciphersuite
);
2692 *p
++ = (unsigned char)(ssl
->session_negotiate
->compression
);
2694 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
2695 mbedtls_ssl_get_ciphersuite_name(ssl
->session_negotiate
->ciphersuite
)));
2696 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
2697 (unsigned int) ssl
->session_negotiate
->compression
));
2699 /* Do not write the extensions if the protocol is SSLv3 */
2700 #if defined(MBEDTLS_SSL_PROTO_SSL3)
2701 if ((ssl
->major_ver
!= 3) || (ssl
->minor_ver
!= 0)) {
2705 * First write extensions, then the total length
2707 ssl_write_renegotiation_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2710 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2711 ssl_write_max_fragment_length_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2715 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2716 ssl_write_truncated_hmac_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2720 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2721 ssl_write_cid_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2725 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2726 ssl_write_encrypt_then_mac_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2730 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2731 ssl_write_extended_ms_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2735 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2736 ssl_write_session_ticket_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2740 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2741 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2742 if (mbedtls_ssl_ciphersuite_uses_ec(
2743 mbedtls_ssl_ciphersuite_from_id(ssl
->session_negotiate
->ciphersuite
))) {
2744 ssl_write_supported_point_formats_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2749 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2750 ssl_write_ecjpake_kkpp_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2754 #if defined(MBEDTLS_SSL_ALPN)
2755 ssl_write_alpn_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2759 #if defined(MBEDTLS_SSL_DTLS_SRTP)
2760 ssl_write_use_srtp_ext(ssl
, p
+ 2 + ext_len
, &olen
);
2764 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET
,
2768 *p
++ = (unsigned char)((ext_len
>> 8) & 0xFF);
2769 *p
++ = (unsigned char)((ext_len
) & 0xFF);
2773 #if defined(MBEDTLS_SSL_PROTO_SSL3)
2777 ssl
->out_msglen
= p
- buf
;
2778 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
2779 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_SERVER_HELLO
;
2781 ret
= mbedtls_ssl_write_handshake_msg(ssl
);
2783 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2788 #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
2789 static int ssl_write_certificate_request(mbedtls_ssl_context
*ssl
) {
2790 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
2791 ssl
->handshake
->ciphersuite_info
;
2793 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
2795 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info
)) {
2796 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
2801 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2802 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
2804 #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2805 static int ssl_write_certificate_request(mbedtls_ssl_context
*ssl
) {
2806 int ret
= MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
;
2807 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
2808 ssl
->handshake
->ciphersuite_info
;
2809 uint16_t dn_size
, total_dn_size
; /* excluding length bytes */
2810 size_t ct_len
, sa_len
; /* including length bytes */
2811 unsigned char *buf
, *p
;
2812 const unsigned char *const end
= ssl
->out_msg
+ MBEDTLS_SSL_OUT_CONTENT_LEN
;
2813 const mbedtls_x509_crt
*crt
;
2816 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
2820 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2821 if (ssl
->handshake
->sni_authmode
!= MBEDTLS_SSL_VERIFY_UNSET
)
2822 authmode
= ssl
->handshake
->sni_authmode
;
2825 authmode
= ssl
->conf
->authmode
;
2827 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info
) ||
2828 authmode
== MBEDTLS_SSL_VERIFY_NONE
) {
2829 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
2834 * 0 . 0 handshake type
2835 * 1 . 3 handshake length
2836 * 4 . 4 cert type count
2837 * 5 .. m-1 cert types
2838 * m .. m+1 sig alg length (TLS 1.2 only)
2839 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
2840 * n .. n+1 length of all DNs
2841 * n+2 .. n+3 length of DN 1
2842 * n+4 .. ... Distinguished Name #1
2843 * ... .. ... length of DN 2, etc.
2849 * Supported certificate types
2851 * ClientCertificateType certificate_types<1..2^8-1>;
2852 * enum { (255) } ClientCertificateType;
2856 #if defined(MBEDTLS_RSA_C)
2857 p
[1 + ct_len
++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN
;
2859 #if defined(MBEDTLS_ECDSA_C)
2860 p
[1 + ct_len
++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN
;
2863 p
[0] = (unsigned char) ct_len
++;
2867 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2869 * Add signature_algorithms for verify (TLS 1.2)
2871 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2874 * HashAlgorithm hash;
2875 * SignatureAlgorithm signature;
2876 * } SignatureAndHashAlgorithm;
2878 * enum { (255) } HashAlgorithm;
2879 * enum { (255) } SignatureAlgorithm;
2881 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
2885 * Supported signature algorithms
2887 for (cur
= ssl
->conf
->sig_hashes
; *cur
!= MBEDTLS_MD_NONE
; cur
++) {
2888 unsigned char hash
= mbedtls_ssl_hash_from_md_alg(*cur
);
2890 if (MBEDTLS_SSL_HASH_NONE
== hash
|| mbedtls_ssl_set_calc_verify_md(ssl
, hash
))
2893 #if defined(MBEDTLS_RSA_C)
2894 p
[2 + sa_len
++] = hash
;
2895 p
[2 + sa_len
++] = MBEDTLS_SSL_SIG_RSA
;
2897 #if defined(MBEDTLS_ECDSA_C)
2898 p
[2 + sa_len
++] = hash
;
2899 p
[2 + sa_len
++] = MBEDTLS_SSL_SIG_ECDSA
;
2903 p
[0] = (unsigned char)(sa_len
>> 8);
2904 p
[1] = (unsigned char)(sa_len
);
2908 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2911 * DistinguishedName certificate_authorities<0..2^16-1>;
2912 * opaque DistinguishedName<1..2^16-1>;
2918 if (ssl
->conf
->cert_req_ca_list
== MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
) {
2919 /* NOTE: If trusted certificates are provisioned
2920 * via a CA callback (configured through
2921 * `mbedtls_ssl_conf_ca_cb()`, then the
2922 * CertificateRequest is currently left empty. */
2924 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2925 if (ssl
->handshake
->sni_ca_chain
!= NULL
)
2926 crt
= ssl
->handshake
->sni_ca_chain
;
2929 crt
= ssl
->conf
->ca_chain
;
2931 while (crt
!= NULL
&& crt
->version
!= 0) {
2932 /* It follows from RFC 5280 A.1 that this length
2933 * can be represented in at most 11 bits. */
2934 dn_size
= (uint16_t) crt
->subject_raw
.len
;
2936 if (end
< p
|| (size_t)(end
- p
) < 2 + (size_t) dn_size
) {
2937 MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
2941 *p
++ = (unsigned char)(dn_size
>> 8);
2942 *p
++ = (unsigned char)(dn_size
);
2943 memcpy(p
, crt
->subject_raw
.p
, dn_size
);
2946 MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p
- dn_size
, dn_size
);
2948 total_dn_size
+= 2 + dn_size
;
2953 ssl
->out_msglen
= p
- buf
;
2954 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
2955 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST
;
2956 ssl
->out_msg
[4 + ct_len
+ sa_len
] = (unsigned char)(total_dn_size
>> 8);
2957 ssl
->out_msg
[5 + ct_len
+ sa_len
] = (unsigned char)(total_dn_size
);
2959 ret
= mbedtls_ssl_write_handshake_msg(ssl
);
2961 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2965 #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2967 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2968 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2969 static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context
*ssl
) {
2970 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
2972 if (! mbedtls_pk_can_do(mbedtls_ssl_own_key(ssl
), MBEDTLS_PK_ECKEY
)) {
2973 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2974 return (MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH
);
2977 if ((ret
= mbedtls_ecdh_get_params(&ssl
->handshake
->ecdh_ctx
,
2978 mbedtls_pk_ec(*mbedtls_ssl_own_key(ssl
)),
2979 MBEDTLS_ECDH_OURS
)) != 0) {
2980 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret
);
2986 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2987 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2989 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
2990 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2991 static int ssl_resume_server_key_exchange(mbedtls_ssl_context
*ssl
,
2992 size_t *signature_len
) {
2993 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2994 * signature length which will be added in ssl_write_server_key_exchange
2995 * after the call to ssl_prepare_server_key_exchange.
2996 * ssl_write_server_key_exchange also takes care of incrementing
2997 * ssl->out_msglen. */
2998 unsigned char *sig_start
= ssl
->out_msg
+ ssl
->out_msglen
+ 2;
2999 size_t sig_max_len
= (ssl
->out_buf
+ MBEDTLS_SSL_OUT_CONTENT_LEN
3001 int ret
= ssl
->conf
->f_async_resume(ssl
,
3002 sig_start
, signature_len
, sig_max_len
);
3003 if (ret
!= MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
) {
3004 ssl
->handshake
->async_in_progress
= 0;
3005 mbedtls_ssl_set_async_operation_data(ssl
, NULL
);
3007 MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret
);
3010 #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
3011 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3013 /* Prepare the ServerKeyExchange message, up to and including
3014 * calculating the signature if any, but excluding formatting the
3015 * signature and sending the message. */
3016 static int ssl_prepare_server_key_exchange(mbedtls_ssl_context
*ssl
,
3017 size_t *signature_len
) {
3018 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
3019 ssl
->handshake
->ciphersuite_info
;
3021 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3022 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3023 unsigned char *dig_signed
= NULL
;
3024 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3025 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
3027 (void) ciphersuite_info
; /* unused in some configurations */
3028 #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3029 (void) signature_len
;
3030 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3032 ssl
->out_msglen
= 4; /* header (type:1, length:3) to be written later */
3036 * Part 1: Provide key exchange parameters for chosen ciphersuite.
3041 * - ECJPAKE key exchanges
3043 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3044 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECJPAKE
) {
3045 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3048 ret
= mbedtls_ecjpake_write_round_two(
3049 &ssl
->handshake
->ecjpake_ctx
,
3050 ssl
->out_msg
+ ssl
->out_msglen
,
3051 MBEDTLS_SSL_OUT_CONTENT_LEN
- ssl
->out_msglen
, &len
,
3052 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
);
3054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret
);
3058 ssl
->out_msglen
+= len
;
3060 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3063 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3064 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3065 * we use empty support identity hints here.
3067 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
3068 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3069 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_DHE_PSK
||
3070 ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDHE_PSK
) {
3071 ssl
->out_msg
[ssl
->out_msglen
++] = 0x00;
3072 ssl
->out_msg
[ssl
->out_msglen
++] = 0x00;
3074 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3075 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3078 * - DHE key exchanges
3080 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
3081 if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info
)) {
3082 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3085 if (ssl
->conf
->dhm_P
.p
== NULL
|| ssl
->conf
->dhm_G
.p
== NULL
) {
3086 MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
3087 return (MBEDTLS_ERR_SSL_BAD_INPUT_DATA
);
3091 * Ephemeral DH parameters:
3094 * opaque dh_p<1..2^16-1>;
3095 * opaque dh_g<1..2^16-1>;
3096 * opaque dh_Ys<1..2^16-1>;
3099 if ((ret
= mbedtls_dhm_set_group(&ssl
->handshake
->dhm_ctx
,
3101 &ssl
->conf
->dhm_G
)) != 0) {
3102 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret
);
3106 if ((ret
= mbedtls_dhm_make_params(
3107 &ssl
->handshake
->dhm_ctx
,
3108 (int) mbedtls_mpi_size(&ssl
->handshake
->dhm_ctx
.P
),
3109 ssl
->out_msg
+ ssl
->out_msglen
, &len
,
3110 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
)) != 0) {
3111 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret
);
3115 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3116 dig_signed
= ssl
->out_msg
+ ssl
->out_msglen
;
3119 ssl
->out_msglen
+= len
;
3121 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl
->handshake
->dhm_ctx
.X
);
3122 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl
->handshake
->dhm_ctx
.P
);
3123 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl
->handshake
->dhm_ctx
.G
);
3124 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl
->handshake
->dhm_ctx
.GX
);
3126 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
3129 * - ECDHE key exchanges
3131 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
3132 if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info
)) {
3134 * Ephemeral ECDH parameters:
3137 * ECParameters curve_params;
3139 * } ServerECDHParams;
3141 const mbedtls_ecp_curve_info
**curve
= NULL
;
3142 const mbedtls_ecp_group_id
*gid
;
3143 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3146 /* Match our preference list against the offered curves */
3147 for (gid
= ssl
->conf
->curve_list
; *gid
!= MBEDTLS_ECP_DP_NONE
; gid
++)
3148 for (curve
= ssl
->handshake
->curves
; *curve
!= NULL
; curve
++)
3149 if ((*curve
)->grp_id
== *gid
)
3150 goto curve_matching_done
;
3152 curve_matching_done
:
3153 if (curve
== NULL
|| *curve
== NULL
) {
3154 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
3155 return (MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN
);
3158 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s", (*curve
)->name
));
3160 if ((ret
= mbedtls_ecdh_setup(&ssl
->handshake
->ecdh_ctx
,
3161 (*curve
)->grp_id
)) != 0) {
3162 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret
);
3166 if ((ret
= mbedtls_ecdh_make_params(
3167 &ssl
->handshake
->ecdh_ctx
, &len
,
3168 ssl
->out_msg
+ ssl
->out_msglen
,
3169 MBEDTLS_SSL_OUT_CONTENT_LEN
- ssl
->out_msglen
,
3170 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
)) != 0) {
3171 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret
);
3175 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3176 dig_signed
= ssl
->out_msg
+ ssl
->out_msglen
;
3179 ssl
->out_msglen
+= len
;
3181 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl
->handshake
->ecdh_ctx
,
3182 MBEDTLS_DEBUG_ECDH_Q
);
3184 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
3188 * Part 2: For key exchanges involving the server signing the
3189 * exchange parameters, compute and add the signature here.
3192 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3193 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info
)) {
3194 size_t dig_signed_len
= ssl
->out_msg
+ ssl
->out_msglen
- dig_signed
;
3196 unsigned char hash
[MBEDTLS_MD_MAX_SIZE
];
3197 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3200 * 2.1: Choose hash algorithm:
3201 * A: For TLS 1.2, obey signature-hash-algorithm extension
3202 * to choose appropriate hash.
3203 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3204 * (RFC 4492, Sec. 5.4)
3205 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
3208 mbedtls_md_type_t md_alg
;
3210 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3211 mbedtls_pk_type_t sig_alg
=
3212 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info
);
3213 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
3214 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3215 * (RFC 5246, Sec. 7.4.1.4.1). */
3216 if (sig_alg
== MBEDTLS_PK_NONE
||
3217 (md_alg
= mbedtls_ssl_sig_hash_set_find(&ssl
->handshake
->hash_algs
,
3218 sig_alg
)) == MBEDTLS_MD_NONE
) {
3219 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3220 /* (... because we choose a cipher suite
3221 * only if there is a matching hash.) */
3222 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
3225 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3226 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3227 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3228 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA
) {
3229 /* B: Default hash SHA1 */
3230 md_alg
= MBEDTLS_MD_SHA1
;
3232 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3233 MBEDTLS_SSL_PROTO_TLS1_1 */
3236 md_alg
= MBEDTLS_MD_NONE
;
3239 MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg
));
3242 * 2.2: Compute the hash to be signed
3244 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3246 if (md_alg
== MBEDTLS_MD_NONE
) {
3248 ret
= mbedtls_ssl_get_key_exchange_md_ssl_tls(ssl
, hash
,
3254 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3255 MBEDTLS_SSL_PROTO_TLS1_1 */
3256 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3257 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3258 if (md_alg
!= MBEDTLS_MD_NONE
) {
3259 ret
= mbedtls_ssl_get_key_exchange_md_tls1_2(ssl
, hash
, &hashlen
,
3266 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3267 MBEDTLS_SSL_PROTO_TLS1_2 */
3269 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3270 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
3273 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash
, hashlen
);
3276 * 2.3: Compute and add the signature
3278 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3279 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
3281 * For TLS 1.2, we need to specify signature and hash algorithm
3282 * explicitly through a prefix to the signature.
3285 * HashAlgorithm hash;
3286 * SignatureAlgorithm signature;
3287 * } SignatureAndHashAlgorithm;
3290 * SignatureAndHashAlgorithm algorithm;
3291 * opaque signature<0..2^16-1>;
3292 * } DigitallySigned;
3296 ssl
->out_msg
[ssl
->out_msglen
++] =
3297 mbedtls_ssl_hash_from_md_alg(md_alg
);
3298 ssl
->out_msg
[ssl
->out_msglen
++] =
3299 mbedtls_ssl_sig_from_pk_alg(sig_alg
);
3301 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3303 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3304 if (ssl
->conf
->f_async_sign_start
!= NULL
) {
3305 ret
= ssl
->conf
->f_async_sign_start(ssl
,
3306 mbedtls_ssl_own_cert(ssl
),
3307 md_alg
, hash
, hashlen
);
3309 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH
:
3310 /* act as if f_async_sign was null */
3313 ssl
->handshake
->async_in_progress
= 1;
3314 return (ssl_resume_server_key_exchange(ssl
, signature_len
));
3315 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
:
3316 ssl
->handshake
->async_in_progress
= 1;
3317 return (MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
);
3319 MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret
);
3323 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3325 if (mbedtls_ssl_own_key(ssl
) == NULL
) {
3326 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
3327 return (MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED
);
3330 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3331 * signature length which will be added in ssl_write_server_key_exchange
3332 * after the call to ssl_prepare_server_key_exchange.
3333 * ssl_write_server_key_exchange also takes care of incrementing
3334 * ssl->out_msglen. */
3335 if ((ret
= mbedtls_pk_sign(mbedtls_ssl_own_key(ssl
),
3336 md_alg
, hash
, hashlen
,
3337 ssl
->out_msg
+ ssl
->out_msglen
+ 2,
3340 ssl
->conf
->p_rng
)) != 0) {
3341 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret
);
3345 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3350 /* Prepare the ServerKeyExchange message and send it. For ciphersuites
3351 * that do not include a ServerKeyExchange message, do nothing. Either
3352 * way, if successful, move on to the next step in the SSL state
3354 static int ssl_write_server_key_exchange(mbedtls_ssl_context
*ssl
) {
3355 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3356 size_t signature_len
= 0;
3357 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
3358 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
3359 ssl
->handshake
->ciphersuite_info
;
3360 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
3362 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
3364 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
3365 /* Extract static ECDH parameters and abort if ServerKeyExchange
3367 if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info
)) {
3368 /* For suites involving ECDH, extract DH parameters
3369 * from certificate at this point. */
3370 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
3371 if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info
)) {
3372 ssl_get_ecdh_params_from_cert(ssl
);
3374 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
3376 /* Key exchanges not involving ephemeral keys don't use
3377 * ServerKeyExchange, so end here. */
3378 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
3382 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
3384 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
3385 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3386 /* If we have already prepared the message and there is an ongoing
3387 * signature operation, resume signing. */
3388 if (ssl
->handshake
->async_in_progress
!= 0) {
3389 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
3390 ret
= ssl_resume_server_key_exchange(ssl
, &signature_len
);
3392 #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
3393 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3395 /* ServerKeyExchange is needed. Prepare the message. */
3396 ret
= ssl_prepare_server_key_exchange(ssl
, &signature_len
);
3400 /* If we're starting to write a new message, set ssl->out_msglen
3401 * to 0. But if we're resuming after an asynchronous message,
3402 * out_msglen is the amount of data written so far and mst be
3404 if (ret
== MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
)
3405 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
3407 ssl
->out_msglen
= 0;
3411 /* If there is a signature, write its length.
3412 * ssl_prepare_server_key_exchange already wrote the signature
3413 * itself at its proper place in the output buffer. */
3414 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3415 if (signature_len
!= 0) {
3416 ssl
->out_msg
[ssl
->out_msglen
++] = (unsigned char)(signature_len
>> 8);
3417 ssl
->out_msg
[ssl
->out_msglen
++] = (unsigned char)(signature_len
);
3419 MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
3420 ssl
->out_msg
+ ssl
->out_msglen
,
3423 /* Skip over the already-written signature */
3424 ssl
->out_msglen
+= signature_len
;
3426 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3428 /* Add header and send. */
3429 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
3430 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE
;
3434 if ((ret
= mbedtls_ssl_write_handshake_msg(ssl
)) != 0) {
3435 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret
);
3439 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
3443 static int ssl_write_server_hello_done(mbedtls_ssl_context
*ssl
) {
3444 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3446 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
3448 ssl
->out_msglen
= 4;
3449 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
3450 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE
;
3454 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3455 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
)
3456 mbedtls_ssl_send_flight_completed(ssl
);
3459 if ((ret
= mbedtls_ssl_write_handshake_msg(ssl
)) != 0) {
3460 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret
);
3464 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3465 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
&&
3466 (ret
= mbedtls_ssl_flight_transmit(ssl
)) != 0) {
3467 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret
);
3470 #endif /* MBEDTLS_SSL_PROTO_DTLS */
3472 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
3477 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3478 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3479 static int ssl_parse_client_dh_public(mbedtls_ssl_context
*ssl
, unsigned char **p
,
3480 const unsigned char *end
) {
3481 int ret
= MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
;
3485 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3488 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3489 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3492 n
= ((*p
)[0] << 8) | (*p
)[1];
3496 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3497 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3500 if ((ret
= mbedtls_dhm_read_public(&ssl
->handshake
->dhm_ctx
, *p
, n
)) != 0) {
3501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret
);
3502 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
3507 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl
->handshake
->dhm_ctx
.GY
);
3511 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3512 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3514 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3515 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3517 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3518 static int ssl_resume_decrypt_pms(mbedtls_ssl_context
*ssl
,
3519 unsigned char *peer_pms
,
3520 size_t *peer_pmslen
,
3521 size_t peer_pmssize
) {
3522 int ret
= ssl
->conf
->f_async_resume(ssl
,
3523 peer_pms
, peer_pmslen
, peer_pmssize
);
3524 if (ret
!= MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
) {
3525 ssl
->handshake
->async_in_progress
= 0;
3526 mbedtls_ssl_set_async_operation_data(ssl
, NULL
);
3528 MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret
);
3531 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3533 static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context
*ssl
,
3534 const unsigned char *p
,
3535 const unsigned char *end
,
3536 unsigned char *peer_pms
,
3537 size_t *peer_pmslen
,
3538 size_t peer_pmssize
) {
3539 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3540 mbedtls_pk_context
*private_key
= mbedtls_ssl_own_key(ssl
);
3541 mbedtls_pk_context
*public_key
= &mbedtls_ssl_own_cert(ssl
)->pk
;
3542 size_t len
= mbedtls_pk_get_len(public_key
);
3544 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3545 /* If we have already started decoding the message and there is an ongoing
3546 * decryption operation, resume signing. */
3547 if (ssl
->handshake
->async_in_progress
!= 0) {
3548 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
3549 return (ssl_resume_decrypt_pms(ssl
,
3550 peer_pms
, peer_pmslen
, peer_pmssize
));
3552 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3555 * Prepare to decrypt the premaster using own private RSA key
3557 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3558 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3559 if (ssl
->minor_ver
!= MBEDTLS_SSL_MINOR_VERSION_0
) {
3561 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3562 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3564 if (*p
++ != ((len
>> 8) & 0xFF) ||
3565 *p
++ != ((len
) & 0xFF)) {
3566 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3567 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3572 if (p
+ len
!= end
) {
3573 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3574 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3578 * Decrypt the premaster secret
3580 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3581 if (ssl
->conf
->f_async_decrypt_start
!= NULL
) {
3582 ret
= ssl
->conf
->f_async_decrypt_start(ssl
,
3583 mbedtls_ssl_own_cert(ssl
),
3586 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH
:
3587 /* act as if f_async_decrypt_start was null */
3590 ssl
->handshake
->async_in_progress
= 1;
3591 return (ssl_resume_decrypt_pms(ssl
,
3595 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
:
3596 ssl
->handshake
->async_in_progress
= 1;
3597 return (MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
);
3599 MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret
);
3603 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3605 if (! mbedtls_pk_can_do(private_key
, MBEDTLS_PK_RSA
)) {
3606 MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
3607 return (MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED
);
3610 ret
= mbedtls_pk_decrypt(private_key
, p
, len
,
3611 peer_pms
, peer_pmslen
, peer_pmssize
,
3612 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
);
3616 static int ssl_parse_encrypted_pms(mbedtls_ssl_context
*ssl
,
3617 const unsigned char *p
,
3618 const unsigned char *end
,
3619 size_t pms_offset
) {
3620 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3621 unsigned char *pms
= ssl
->handshake
->premaster
+ pms_offset
;
3622 unsigned char ver
[2];
3623 unsigned char fake_pms
[48], peer_pms
[48];
3625 size_t i
, peer_pmslen
;
3628 /* In case of a failure in decryption, the decryption may write less than
3629 * 2 bytes of output, but we always read the first two bytes. It doesn't
3630 * matter in the end because diff will be nonzero in that case due to
3631 * ret being nonzero, and we only care whether diff is 0.
3632 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3633 * also makes memory analyzers happy (don't access uninitialized memory,
3634 * even if it's an unsigned char). */
3635 peer_pms
[0] = peer_pms
[1] = ~0;
3638 ret
= ssl_decrypt_encrypted_pms(ssl
, p
, end
,
3643 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3644 if (ret
== MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS
)
3646 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3648 mbedtls_ssl_write_version(ssl
->handshake
->max_major_ver
,
3649 ssl
->handshake
->max_minor_ver
,
3650 ssl
->conf
->transport
, ver
);
3652 /* Avoid data-dependent branches while checking for invalid
3653 * padding, to protect against timing-based Bleichenbacher-type
3655 diff
= (unsigned int) ret
;
3656 diff
|= peer_pmslen
^ 48;
3657 diff
|= peer_pms
[0] ^ ver
[0];
3658 diff
|= peer_pms
[1] ^ ver
[1];
3660 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3661 /* MSVC has a warning about unary minus on unsigned, but this is
3662 * well-defined and precisely what we want to do here */
3663 #if defined(_MSC_VER)
3664 #pragma warning( push )
3665 #pragma warning( disable : 4146 )
3667 mask
= - ((diff
| - diff
) >> (sizeof(unsigned int) * 8 - 1));
3668 #if defined(_MSC_VER)
3669 #pragma warning( pop )
3673 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3674 * must not cause the connection to end immediately; instead, send a
3675 * bad_record_mac later in the handshake.
3676 * To protect against timing-based variants of the attack, we must
3677 * not have any branch that depends on whether the decryption was
3678 * successful. In particular, always generate the fake premaster secret,
3679 * regardless of whether it will ultimately influence the output or not.
3681 ret
= ssl
->conf
->f_rng(ssl
->conf
->p_rng
, fake_pms
, sizeof(fake_pms
));
3683 /* It's ok to abort on an RNG failure, since this does not reveal
3684 * anything about the RSA decryption. */
3688 #if defined(MBEDTLS_SSL_DEBUG_ALL)
3690 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3693 if (sizeof(ssl
->handshake
->premaster
) < pms_offset
||
3694 sizeof(ssl
->handshake
->premaster
) - pms_offset
< 48) {
3695 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3696 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
3698 ssl
->handshake
->pmslen
= 48;
3700 /* Set pms to either the true or the fake PMS, without
3701 * data-dependent branches. */
3702 for (i
= 0; i
< ssl
->handshake
->pmslen
; i
++)
3703 pms
[i
] = (mask
& fake_pms
[i
]) | ((~mask
) & peer_pms
[i
]);
3707 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3708 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3710 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
3711 static int ssl_parse_client_psk_identity(mbedtls_ssl_context
*ssl
, unsigned char **p
,
3712 const unsigned char *end
) {
3716 if (ssl_conf_has_psk_or_cb(ssl
->conf
) == 0) {
3717 MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
3718 return (MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED
);
3722 * Receive client pre-shared key identity name
3725 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3726 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3729 n
= ((*p
)[0] << 8) | (*p
)[1];
3732 if (n
== 0 || n
> end
- *p
) {
3733 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3734 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3737 if (ssl
->conf
->f_psk
!= NULL
) {
3738 if (ssl
->conf
->f_psk(ssl
->conf
->p_psk
, ssl
, *p
, n
) != 0)
3739 ret
= MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY
;
3741 /* Identity is not a big secret since clients send it in the clear,
3742 * but treat it carefully anyway, just in case */
3743 if (n
!= ssl
->conf
->psk_identity_len
||
3744 mbedtls_ssl_safer_memcmp(ssl
->conf
->psk_identity
, *p
, n
) != 0) {
3745 ret
= MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY
;
3749 if (ret
== MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY
) {
3750 MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p
, n
);
3751 mbedtls_ssl_send_alert_message(ssl
, MBEDTLS_SSL_ALERT_LEVEL_FATAL
,
3752 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY
);
3753 return (MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY
);
3760 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
3762 static int ssl_parse_client_key_exchange(mbedtls_ssl_context
*ssl
) {
3763 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
3764 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
;
3765 unsigned char *p
, *end
;
3767 ciphersuite_info
= ssl
->handshake
->ciphersuite_info
;
3769 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
3771 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
3772 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3773 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3774 if ((ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_RSA_PSK
||
3775 ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_RSA
) &&
3776 (ssl
->handshake
->async_in_progress
!= 0)) {
3777 /* We've already read a record and there is an asynchronous
3778 * operation in progress to decrypt it. So skip reading the
3780 MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
3783 if ((ret
= mbedtls_ssl_read_record(ssl
, 1)) != 0) {
3784 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret
);
3788 p
= ssl
->in_msg
+ mbedtls_ssl_hs_hdr_len(ssl
);
3789 end
= ssl
->in_msg
+ ssl
->in_hslen
;
3791 if (ssl
->in_msgtype
!= MBEDTLS_SSL_MSG_HANDSHAKE
) {
3792 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3793 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3796 if (ssl
->in_msg
[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE
) {
3797 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3798 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3801 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3802 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_DHE_RSA
) {
3803 if ((ret
= ssl_parse_client_dh_public(ssl
, &p
, end
)) != 0) {
3804 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret
);
3809 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3810 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3813 if ((ret
= mbedtls_dhm_calc_secret(&ssl
->handshake
->dhm_ctx
,
3814 ssl
->handshake
->premaster
,
3815 MBEDTLS_PREMASTER_SIZE
,
3816 &ssl
->handshake
->pmslen
,
3817 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
)) != 0) {
3818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret
);
3819 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS
);
3822 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl
->handshake
->dhm_ctx
.K
);
3824 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3825 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3826 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3827 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3828 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3829 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDHE_RSA
||
3830 ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA
||
3831 ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDH_RSA
||
3832 ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA
) {
3833 if ((ret
= mbedtls_ecdh_read_public(&ssl
->handshake
->ecdh_ctx
,
3834 p
, end
- p
)) != 0) {
3835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret
);
3836 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
3839 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl
->handshake
->ecdh_ctx
,
3840 MBEDTLS_DEBUG_ECDH_QP
);
3842 if ((ret
= mbedtls_ecdh_calc_secret(&ssl
->handshake
->ecdh_ctx
,
3843 &ssl
->handshake
->pmslen
,
3844 ssl
->handshake
->premaster
,
3845 MBEDTLS_MPI_MAX_SIZE
,
3846 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
)) != 0) {
3847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret
);
3848 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS
);
3851 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl
->handshake
->ecdh_ctx
,
3852 MBEDTLS_DEBUG_ECDH_Z
);
3854 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3855 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3856 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3857 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3858 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3859 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_PSK
) {
3860 if ((ret
= ssl_parse_client_psk_identity(ssl
, &p
, end
)) != 0) {
3861 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret
);
3866 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3867 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3870 #if defined(MBEDTLS_USE_PSA_CRYPTO)
3871 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3872 * and skip the intermediate PMS. */
3873 if (ssl_use_opaque_psk(ssl
) == 1)
3874 MBEDTLS_SSL_DEBUG_MSG(1, ("skip PMS generation for opaque PSK"));
3876 #endif /* MBEDTLS_USE_PSA_CRYPTO */
3877 if ((ret
= mbedtls_ssl_psk_derive_premaster(ssl
,
3878 ciphersuite_info
->key_exchange
)) != 0) {
3879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret
);
3883 #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3884 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3885 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_RSA_PSK
) {
3886 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3887 if (ssl
->handshake
->async_in_progress
!= 0) {
3888 /* There is an asynchronous operation in progress to
3889 * decrypt the encrypted premaster secret, so skip
3890 * directly to resuming this operation. */
3891 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
3892 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3893 * won't actually use it, but maintain p anyway for robustness. */
3894 p
+= ssl
->conf
->psk_identity_len
+ 2;
3896 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3897 if ((ret
= ssl_parse_client_psk_identity(ssl
, &p
, end
)) != 0) {
3898 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret
);
3902 #if defined(MBEDTLS_USE_PSA_CRYPTO)
3903 /* Opaque PSKs are currently only supported for PSK-only. */
3904 if (ssl_use_opaque_psk(ssl
) == 1)
3905 return (MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
);
3908 if ((ret
= ssl_parse_encrypted_pms(ssl
, p
, end
, 2)) != 0) {
3909 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret
);
3913 if ((ret
= mbedtls_ssl_psk_derive_premaster(ssl
,
3914 ciphersuite_info
->key_exchange
)) != 0) {
3915 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret
);
3919 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3920 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3921 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_DHE_PSK
) {
3922 if ((ret
= ssl_parse_client_psk_identity(ssl
, &p
, end
)) != 0) {
3923 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret
);
3926 if ((ret
= ssl_parse_client_dh_public(ssl
, &p
, end
)) != 0) {
3927 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret
);
3931 #if defined(MBEDTLS_USE_PSA_CRYPTO)
3932 /* Opaque PSKs are currently only supported for PSK-only. */
3933 if (ssl_use_opaque_psk(ssl
) == 1)
3934 return (MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
);
3938 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3939 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3942 if ((ret
= mbedtls_ssl_psk_derive_premaster(ssl
,
3943 ciphersuite_info
->key_exchange
)) != 0) {
3944 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret
);
3948 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3949 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3950 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECDHE_PSK
) {
3951 if ((ret
= ssl_parse_client_psk_identity(ssl
, &p
, end
)) != 0) {
3952 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret
);
3956 if ((ret
= mbedtls_ecdh_read_public(&ssl
->handshake
->ecdh_ctx
,
3957 p
, end
- p
)) != 0) {
3958 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret
);
3959 return (MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
3962 #if defined(MBEDTLS_USE_PSA_CRYPTO)
3963 /* Opaque PSKs are currently only supported for PSK-only. */
3964 if (ssl_use_opaque_psk(ssl
) == 1)
3965 return (MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
);
3968 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl
->handshake
->ecdh_ctx
,
3969 MBEDTLS_DEBUG_ECDH_QP
);
3971 if ((ret
= mbedtls_ssl_psk_derive_premaster(ssl
,
3972 ciphersuite_info
->key_exchange
)) != 0) {
3973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret
);
3977 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3978 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3979 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_RSA
) {
3980 if ((ret
= ssl_parse_encrypted_pms(ssl
, p
, end
, 0)) != 0) {
3981 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret
);
3985 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3986 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3987 if (ciphersuite_info
->key_exchange
== MBEDTLS_KEY_EXCHANGE_ECJPAKE
) {
3988 ret
= mbedtls_ecjpake_read_round_two(&ssl
->handshake
->ecjpake_ctx
,
3991 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret
);
3992 return (MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE
);
3995 ret
= mbedtls_ecjpake_derive_secret(&ssl
->handshake
->ecjpake_ctx
,
3996 ssl
->handshake
->premaster
, 32, &ssl
->handshake
->pmslen
,
3997 ssl
->conf
->f_rng
, ssl
->conf
->p_rng
);
3999 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret
);
4003 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
4005 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4006 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
4009 if ((ret
= mbedtls_ssl_derive_keys(ssl
)) != 0) {
4010 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret
);
4016 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
4021 #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
4022 static int ssl_parse_certificate_verify(mbedtls_ssl_context
*ssl
) {
4023 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
4024 ssl
->handshake
->ciphersuite_info
;
4026 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
4028 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info
)) {
4029 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4034 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4035 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
4037 #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
4038 static int ssl_parse_certificate_verify(mbedtls_ssl_context
*ssl
) {
4039 int ret
= MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
;
4041 unsigned char hash
[48];
4042 unsigned char *hash_start
= hash
;
4044 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4045 mbedtls_pk_type_t pk_alg
;
4047 mbedtls_md_type_t md_alg
;
4048 const mbedtls_ssl_ciphersuite_t
*ciphersuite_info
=
4049 ssl
->handshake
->ciphersuite_info
;
4050 mbedtls_pk_context
*peer_pk
;
4052 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
4054 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info
)) {
4055 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4060 #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4061 if (ssl
->session_negotiate
->peer_cert
== NULL
) {
4062 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4066 #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4067 if (ssl
->session_negotiate
->peer_cert_digest
== NULL
) {
4068 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4072 #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4074 /* Read the message without adding it to the checksum */
4075 ret
= mbedtls_ssl_read_record(ssl
, 0 /* no checksum update */);
4077 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret
);
4083 /* Process the message contents */
4084 if (ssl
->in_msgtype
!= MBEDTLS_SSL_MSG_HANDSHAKE
||
4085 ssl
->in_msg
[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY
) {
4086 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4087 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4090 i
= mbedtls_ssl_hs_hdr_len(ssl
);
4092 #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4093 peer_pk
= &ssl
->handshake
->peer_pubkey
;
4094 #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4095 if (ssl
->session_negotiate
->peer_cert
== NULL
) {
4096 /* Should never happen */
4097 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
4099 peer_pk
= &ssl
->session_negotiate
->peer_cert
->pk
;
4100 #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4104 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4105 * opaque signature<0..2^16-1>;
4106 * } DigitallySigned;
4108 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4109 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4110 if (ssl
->minor_ver
!= MBEDTLS_SSL_MINOR_VERSION_3
) {
4111 md_alg
= MBEDTLS_MD_NONE
;
4114 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
4115 if (mbedtls_pk_can_do(peer_pk
, MBEDTLS_PK_ECDSA
)) {
4118 md_alg
= MBEDTLS_MD_SHA1
;
4121 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4122 MBEDTLS_SSL_PROTO_TLS1_1 */
4123 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4124 if (ssl
->minor_ver
== MBEDTLS_SSL_MINOR_VERSION_3
) {
4125 if (i
+ 2 > ssl
->in_hslen
) {
4126 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4127 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4133 md_alg
= mbedtls_ssl_md_alg_from_hash(ssl
->in_msg
[i
]);
4135 if (md_alg
== MBEDTLS_MD_NONE
|| mbedtls_ssl_set_calc_verify_md(ssl
, ssl
->in_msg
[i
])) {
4136 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4137 " for verify message"));
4138 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4141 #if !defined(MBEDTLS_MD_SHA1)
4142 if (MBEDTLS_MD_SHA1
== md_alg
)
4146 /* Info from md_alg will be used instead */
4154 if ((pk_alg
= mbedtls_ssl_pk_alg_from_sig(ssl
->in_msg
[i
]))
4155 == MBEDTLS_PK_NONE
) {
4156 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4157 " for verify message"));
4158 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4162 * Check the certificate's key type matches the signature alg
4164 if (!mbedtls_pk_can_do(peer_pk
, pk_alg
)) {
4165 MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
4166 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4171 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4173 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4174 return (MBEDTLS_ERR_SSL_INTERNAL_ERROR
);
4177 if (i
+ 2 > ssl
->in_hslen
) {
4178 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4179 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4182 sig_len
= (ssl
->in_msg
[i
] << 8) | ssl
->in_msg
[i
+ 1];
4185 if (i
+ sig_len
!= ssl
->in_hslen
) {
4186 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4187 return (MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
4190 /* Calculate hash and verify signature */
4193 ssl
->handshake
->calc_verify(ssl
, hash
, &dummy_hlen
);
4196 if ((ret
= mbedtls_pk_verify(peer_pk
,
4197 md_alg
, hash_start
, hashlen
,
4198 ssl
->in_msg
+ i
, sig_len
)) != 0) {
4199 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret
);
4203 mbedtls_ssl_update_handshake_status(ssl
);
4205 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
4209 #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
4211 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4212 static int ssl_write_new_session_ticket(mbedtls_ssl_context
*ssl
) {
4213 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
4217 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
4219 ssl
->out_msgtype
= MBEDTLS_SSL_MSG_HANDSHAKE
;
4220 ssl
->out_msg
[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET
;
4224 * uint32 ticket_lifetime_hint;
4225 * opaque ticket<0..2^16-1>;
4226 * } NewSessionTicket;
4228 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4229 * 8 . 9 ticket_len (n)
4230 * 10 . 9+n ticket content
4233 if ((ret
= ssl
->conf
->f_ticket_write(ssl
->conf
->p_ticket
,
4234 ssl
->session_negotiate
,
4236 ssl
->out_msg
+ MBEDTLS_SSL_OUT_CONTENT_LEN
,
4237 &tlen
, &lifetime
)) != 0) {
4238 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret
);
4242 ssl
->out_msg
[4] = (lifetime
>> 24) & 0xFF;
4243 ssl
->out_msg
[5] = (lifetime
>> 16) & 0xFF;
4244 ssl
->out_msg
[6] = (lifetime
>> 8) & 0xFF;
4245 ssl
->out_msg
[7] = (lifetime
) & 0xFF;
4247 ssl
->out_msg
[8] = (unsigned char)((tlen
>> 8) & 0xFF);
4248 ssl
->out_msg
[9] = (unsigned char)((tlen
) & 0xFF);
4250 ssl
->out_msglen
= 10 + tlen
;
4253 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4254 * ChangeCipherSpec share the same state.
4256 ssl
->handshake
->new_session_ticket
= 0;
4258 if ((ret
= mbedtls_ssl_write_handshake_msg(ssl
)) != 0) {
4259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret
);
4263 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
4267 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
4270 * SSL handshake -- server side -- single step
4272 int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context
*ssl
) {
4275 if (ssl
->state
== MBEDTLS_SSL_HANDSHAKE_OVER
|| ssl
->handshake
== NULL
)
4276 return (MBEDTLS_ERR_SSL_BAD_INPUT_DATA
);
4278 MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl
->state
));
4280 if ((ret
= mbedtls_ssl_flush_output(ssl
)) != 0)
4283 #if defined(MBEDTLS_SSL_PROTO_DTLS)
4284 if (ssl
->conf
->transport
== MBEDTLS_SSL_TRANSPORT_DATAGRAM
&&
4285 ssl
->handshake
->retransmit_state
== MBEDTLS_SSL_RETRANS_SENDING
) {
4286 if ((ret
= mbedtls_ssl_flight_transmit(ssl
)) != 0)
4289 #endif /* MBEDTLS_SSL_PROTO_DTLS */
4291 switch (ssl
->state
) {
4292 case MBEDTLS_SSL_HELLO_REQUEST
:
4293 ssl
->state
= MBEDTLS_SSL_CLIENT_HELLO
;
4299 case MBEDTLS_SSL_CLIENT_HELLO
:
4300 ret
= ssl_parse_client_hello(ssl
);
4303 #if defined(MBEDTLS_SSL_PROTO_DTLS)
4304 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT
:
4305 return (MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
);
4311 * ( ServerKeyExchange )
4312 * ( CertificateRequest )
4315 case MBEDTLS_SSL_SERVER_HELLO
:
4316 ret
= ssl_write_server_hello(ssl
);
4319 case MBEDTLS_SSL_SERVER_CERTIFICATE
:
4320 ret
= mbedtls_ssl_write_certificate(ssl
);
4323 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE
:
4324 ret
= ssl_write_server_key_exchange(ssl
);
4327 case MBEDTLS_SSL_CERTIFICATE_REQUEST
:
4328 ret
= ssl_write_certificate_request(ssl
);
4331 case MBEDTLS_SSL_SERVER_HELLO_DONE
:
4332 ret
= ssl_write_server_hello_done(ssl
);
4336 * <== ( Certificate/Alert )
4338 * ( CertificateVerify )
4342 case MBEDTLS_SSL_CLIENT_CERTIFICATE
:
4343 ret
= mbedtls_ssl_parse_certificate(ssl
);
4346 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE
:
4347 ret
= ssl_parse_client_key_exchange(ssl
);
4350 case MBEDTLS_SSL_CERTIFICATE_VERIFY
:
4351 ret
= ssl_parse_certificate_verify(ssl
);
4354 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC
:
4355 ret
= mbedtls_ssl_parse_change_cipher_spec(ssl
);
4358 case MBEDTLS_SSL_CLIENT_FINISHED
:
4359 ret
= mbedtls_ssl_parse_finished(ssl
);
4363 * ==> ( NewSessionTicket )
4367 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC
:
4368 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4369 if (ssl
->handshake
->new_session_ticket
!= 0)
4370 ret
= ssl_write_new_session_ticket(ssl
);
4373 ret
= mbedtls_ssl_write_change_cipher_spec(ssl
);
4376 case MBEDTLS_SSL_SERVER_FINISHED
:
4377 ret
= mbedtls_ssl_write_finished(ssl
);
4380 case MBEDTLS_SSL_FLUSH_BUFFERS
:
4381 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
4382 ssl
->state
= MBEDTLS_SSL_HANDSHAKE_WRAPUP
;
4385 case MBEDTLS_SSL_HANDSHAKE_WRAPUP
:
4386 mbedtls_ssl_handshake_wrapup(ssl
);
4390 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl
->state
));
4391 return (MBEDTLS_ERR_SSL_BAD_INPUT_DATA
);
4396 #endif /* MBEDTLS_SSL_SRV_C */