| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "common.h" |
| 21 |
|
| 22 |
#if defined(MBEDTLS_SSL_CLI_C) |
| 23 |
|
| 24 |
#if defined(MBEDTLS_PLATFORM_C) |
| 25 |
#include "mbedtls/platform.h" |
| 26 |
#else |
| 27 |
#include <stdlib.h> |
| 28 |
#define mbedtls_calloc calloc |
| 29 |
#define mbedtls_free free |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "mbedtls/ssl.h" |
| 33 |
#include "mbedtls/ssl_internal.h" |
| 34 |
#include "mbedtls/debug.h" |
| 35 |
#include "mbedtls/error.h" |
| 36 |
#include "mbedtls/constant_time.h" |
| 37 |
|
| 38 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 39 |
#include "mbedtls/psa_util.h" |
| 40 |
#include "psa/crypto.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#include <stdint.h> |
| 46 |
|
| 47 |
#if defined(MBEDTLS_HAVE_TIME) |
| 48 |
#include "mbedtls/platform_time.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 52 |
#include "mbedtls/platform_util.h" |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 56 |
static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
| 57 |
{ |
| 58 |
if( conf->psk_identity == NULL || |
| 59 |
conf->psk_identity_len == 0 ) |
| 60 |
{ |
| 61 |
return( 0 ); |
| 62 |
} |
| 63 |
|
| 64 |
if( conf->psk != NULL && conf->psk_len != 0 ) |
| 65 |
return( 1 ); |
| 66 |
|
| 67 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 68 |
if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 69 |
return( 1 ); |
| 70 |
#endif |
| 71 |
|
| 72 |
return( 0 ); |
| 73 |
} |
| 74 |
|
| 75 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 76 |
static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf ) |
| 77 |
{ |
| 78 |
if( conf->psk_identity == NULL || |
| 79 |
conf->psk_identity_len == 0 ) |
| 80 |
{ |
| 81 |
return( 0 ); |
| 82 |
} |
| 83 |
|
| 84 |
if( conf->psk != NULL && conf->psk_len != 0 ) |
| 85 |
return( 1 ); |
| 86 |
|
| 87 |
return( 0 ); |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
#endif |
| 92 |
|
| 93 |
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 94 |
static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
| 95 |
unsigned char *buf, |
| 96 |
const unsigned char *end, |
| 97 |
size_t *olen ) |
| 98 |
{ |
| 99 |
unsigned char *p = buf; |
| 100 |
size_t hostname_len; |
| 101 |
|
| 102 |
*olen = 0; |
| 103 |
|
| 104 |
if( ssl->hostname == NULL ) |
| 105 |
return( 0 ); |
| 106 |
|
| 107 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 108 |
( "client hello, adding server name extension: %s", |
| 109 |
ssl->hostname ) ); |
| 110 |
|
| 111 |
hostname_len = strlen( ssl->hostname ); |
| 112 |
|
| 113 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); |
| 142 |
p += 2; |
| 143 |
|
| 144 |
MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); |
| 145 |
p += 2; |
| 146 |
|
| 147 |
MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); |
| 148 |
p += 2; |
| 149 |
|
| 150 |
*p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); |
| 151 |
|
| 152 |
MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 153 |
p += 2; |
| 154 |
|
| 155 |
memcpy( p, ssl->hostname, hostname_len ); |
| 156 |
|
| 157 |
*olen = hostname_len + 9; |
| 158 |
|
| 159 |
return( 0 ); |
| 160 |
} |
| 161 |
#endif |
| 162 |
|
| 163 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 164 |
static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
| 165 |
unsigned char *buf, |
| 166 |
const unsigned char *end, |
| 167 |
size_t *olen ) |
| 168 |
{ |
| 169 |
unsigned char *p = buf; |
| 170 |
|
| 171 |
*olen = 0; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 177 |
return( 0 ); |
| 178 |
|
| 179 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 180 |
( "client hello, adding renegotiation extension" ) ); |
| 181 |
|
| 182 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len ); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 ); |
| 188 |
p += 2; |
| 189 |
|
| 190 |
*p++ = 0x00; |
| 191 |
*p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 ); |
| 192 |
*p++ = MBEDTLS_BYTE_0( ssl->verify_data_len ); |
| 193 |
|
| 194 |
memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 195 |
|
| 196 |
*olen = 5 + ssl->verify_data_len; |
| 197 |
|
| 198 |
return( 0 ); |
| 199 |
} |
| 200 |
#endif |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 206 |
defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 207 |
static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
| 208 |
unsigned char *buf, |
| 209 |
const unsigned char *end, |
| 210 |
size_t *olen ) |
| 211 |
{ |
| 212 |
unsigned char *p = buf; |
| 213 |
size_t sig_alg_len = 0; |
| 214 |
const int *md; |
| 215 |
|
| 216 |
#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) |
| 217 |
unsigned char *sig_alg_list = buf + 6; |
| 218 |
#endif |
| 219 |
|
| 220 |
*olen = 0; |
| 221 |
|
| 222 |
if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 223 |
return( 0 ); |
| 224 |
|
| 225 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 226 |
( "client hello, adding signature_algorithms extension" ) ); |
| 227 |
|
| 228 |
if( ssl->conf->sig_hashes == NULL ) |
| 229 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 230 |
|
| 231 |
for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 232 |
{ |
| 233 |
#if defined(MBEDTLS_ECDSA_C) |
| 234 |
sig_alg_len += 2; |
| 235 |
#endif |
| 236 |
#if defined(MBEDTLS_RSA_C) |
| 237 |
sig_alg_len += 2; |
| 238 |
#endif |
| 239 |
if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN ) |
| 240 |
{ |
| 241 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 242 |
( "length in bytes of sig-hash-alg extension too big" ) ); |
| 243 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
if( sig_alg_len == 0 ) |
| 249 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 250 |
|
| 251 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 ); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
sig_alg_len = 0; |
| 257 |
|
| 258 |
for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 259 |
{ |
| 260 |
#if defined(MBEDTLS_ECDSA_C) |
| 261 |
sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 262 |
sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA; |
| 263 |
#endif |
| 264 |
#if defined(MBEDTLS_RSA_C) |
| 265 |
sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 266 |
sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA; |
| 267 |
#endif |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, p, 0 ); |
| 288 |
p += 2; |
| 289 |
|
| 290 |
MBEDTLS_PUT_UINT16_BE( sig_alg_len + 2, p, 0 ); |
| 291 |
p += 2; |
| 292 |
|
| 293 |
MBEDTLS_PUT_UINT16_BE( sig_alg_len, p, 0 ); |
| 294 |
p += 2; |
| 295 |
|
| 296 |
*olen = 6 + sig_alg_len; |
| 297 |
|
| 298 |
return( 0 ); |
| 299 |
} |
| 300 |
#endif |
| 301 |
|
| 302 |
|
| 303 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 304 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 305 |
static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, |
| 306 |
unsigned char *buf, |
| 307 |
const unsigned char *end, |
| 308 |
size_t *olen ) |
| 309 |
{ |
| 310 |
unsigned char *p = buf; |
| 311 |
unsigned char *elliptic_curve_list = p + 6; |
| 312 |
size_t elliptic_curve_len = 0; |
| 313 |
const mbedtls_ecp_curve_info *info; |
| 314 |
const mbedtls_ecp_group_id *grp_id; |
| 315 |
|
| 316 |
*olen = 0; |
| 317 |
|
| 318 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 319 |
( "client hello, adding supported_elliptic_curves extension" ) ); |
| 320 |
|
| 321 |
if( ssl->conf->curve_list == NULL ) |
| 322 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 323 |
|
| 324 |
for( grp_id = ssl->conf->curve_list; |
| 325 |
*grp_id != MBEDTLS_ECP_DP_NONE; |
| 326 |
grp_id++ ) |
| 327 |
{ |
| 328 |
info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
| 329 |
if( info == NULL ) |
| 330 |
{ |
| 331 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 332 |
( "invalid curve in ssl configuration" ) ); |
| 333 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 334 |
} |
| 335 |
elliptic_curve_len += 2; |
| 336 |
|
| 337 |
if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN ) |
| 338 |
{ |
| 339 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 340 |
( "malformed supported_elliptic_curves extension in config" ) ); |
| 341 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
if( elliptic_curve_len == 0 ) |
| 347 |
return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 348 |
|
| 349 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + elliptic_curve_len ); |
| 350 |
|
| 351 |
elliptic_curve_len = 0; |
| 352 |
|
| 353 |
for( grp_id = ssl->conf->curve_list; |
| 354 |
*grp_id != MBEDTLS_ECP_DP_NONE; |
| 355 |
grp_id++ ) |
| 356 |
{ |
| 357 |
info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
| 358 |
elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_1( info->tls_id ); |
| 359 |
elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0( info->tls_id ); |
| 360 |
} |
| 361 |
|
| 362 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0 ); |
| 363 |
p += 2; |
| 364 |
|
| 365 |
MBEDTLS_PUT_UINT16_BE( elliptic_curve_len + 2, p, 0 ); |
| 366 |
p += 2; |
| 367 |
|
| 368 |
MBEDTLS_PUT_UINT16_BE( elliptic_curve_len, p, 0 ); |
| 369 |
p += 2; |
| 370 |
|
| 371 |
*olen = 6 + elliptic_curve_len; |
| 372 |
|
| 373 |
return( 0 ); |
| 374 |
} |
| 375 |
|
| 376 |
static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
| 377 |
unsigned char *buf, |
| 378 |
const unsigned char *end, |
| 379 |
size_t *olen ) |
| 380 |
{ |
| 381 |
unsigned char *p = buf; |
| 382 |
(void) ssl; |
| 383 |
|
| 384 |
*olen = 0; |
| 385 |
|
| 386 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 387 |
( "client hello, adding supported_point_formats extension" ) ); |
| 388 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 389 |
|
| 390 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 ); |
| 391 |
p += 2; |
| 392 |
|
| 393 |
*p++ = 0x00; |
| 394 |
*p++ = 2; |
| 395 |
|
| 396 |
*p++ = 1; |
| 397 |
*p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 398 |
|
| 399 |
*olen = 6; |
| 400 |
|
| 401 |
return( 0 ); |
| 402 |
} |
| 403 |
#endif |
| 404 |
|
| 405 |
|
| 406 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 407 |
static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 408 |
unsigned char *buf, |
| 409 |
const unsigned char *end, |
| 410 |
size_t *olen ) |
| 411 |
{ |
| 412 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 413 |
unsigned char *p = buf; |
| 414 |
size_t kkpp_len; |
| 415 |
|
| 416 |
*olen = 0; |
| 417 |
|
| 418 |
|
| 419 |
if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 420 |
return( 0 ); |
| 421 |
|
| 422 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 423 |
( "client hello, adding ecjpake_kkpp extension" ) ); |
| 424 |
|
| 425 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
| 426 |
|
| 427 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 ); |
| 428 |
p += 2; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if( ssl->handshake->ecjpake_cache == NULL || |
| 436 |
ssl->handshake->ecjpake_cache_len == 0 ) |
| 437 |
{ |
| 438 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) ); |
| 439 |
|
| 440 |
ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 441 |
p + 2, end - p - 2, &kkpp_len, |
| 442 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 443 |
if( ret != 0 ) |
| 444 |
{ |
| 445 |
MBEDTLS_SSL_DEBUG_RET( 1 , |
| 446 |
"mbedtls_ecjpake_write_round_one", ret ); |
| 447 |
return( ret ); |
| 448 |
} |
| 449 |
|
| 450 |
ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len ); |
| 451 |
if( ssl->handshake->ecjpake_cache == NULL ) |
| 452 |
{ |
| 453 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) ); |
| 454 |
return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 455 |
} |
| 456 |
|
| 457 |
memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len ); |
| 458 |
ssl->handshake->ecjpake_cache_len = kkpp_len; |
| 459 |
} |
| 460 |
else |
| 461 |
{ |
| 462 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) ); |
| 463 |
|
| 464 |
kkpp_len = ssl->handshake->ecjpake_cache_len; |
| 465 |
MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len ); |
| 466 |
|
| 467 |
memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); |
| 468 |
} |
| 469 |
|
| 470 |
MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 ); |
| 471 |
p += 2; |
| 472 |
|
| 473 |
*olen = kkpp_len + 4; |
| 474 |
|
| 475 |
return( 0 ); |
| 476 |
} |
| 477 |
#endif |
| 478 |
|
| 479 |
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 480 |
static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 481 |
unsigned char *buf, |
| 482 |
const unsigned char *end, |
| 483 |
size_t *olen ) |
| 484 |
{ |
| 485 |
unsigned char *p = buf; |
| 486 |
size_t ext_len; |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
*olen = 0; |
| 498 |
if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 499 |
ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 500 |
{ |
| 501 |
return( 0 ); |
| 502 |
} |
| 503 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) ); |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); |
| 508 |
|
| 509 |
|
| 510 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 ); |
| 511 |
p += 2; |
| 512 |
ext_len = (size_t) ssl->own_cid_len + 1; |
| 513 |
MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); |
| 514 |
p += 2; |
| 515 |
|
| 516 |
*p++ = (uint8_t) ssl->own_cid_len; |
| 517 |
memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 518 |
|
| 519 |
*olen = ssl->own_cid_len + 5; |
| 520 |
|
| 521 |
return( 0 ); |
| 522 |
} |
| 523 |
#endif |
| 524 |
|
| 525 |
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 526 |
static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
| 527 |
unsigned char *buf, |
| 528 |
const unsigned char *end, |
| 529 |
size_t *olen ) |
| 530 |
{ |
| 531 |
unsigned char *p = buf; |
| 532 |
|
| 533 |
*olen = 0; |
| 534 |
|
| 535 |
if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) |
| 536 |
return( 0 ); |
| 537 |
|
| 538 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 539 |
( "client hello, adding max_fragment_length extension" ) ); |
| 540 |
|
| 541 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); |
| 542 |
|
| 543 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 ); |
| 544 |
p += 2; |
| 545 |
|
| 546 |
*p++ = 0x00; |
| 547 |
*p++ = 1; |
| 548 |
|
| 549 |
*p++ = ssl->conf->mfl_code; |
| 550 |
|
| 551 |
*olen = 5; |
| 552 |
|
| 553 |
return( 0 ); |
| 554 |
} |
| 555 |
#endif |
| 556 |
|
| 557 |
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 558 |
static int ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl, |
| 559 |
unsigned char *buf, |
| 560 |
const unsigned char *end, |
| 561 |
size_t *olen ) |
| 562 |
{ |
| 563 |
unsigned char *p = buf; |
| 564 |
|
| 565 |
*olen = 0; |
| 566 |
|
| 567 |
if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ) |
| 568 |
return( 0 ); |
| 569 |
|
| 570 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 571 |
( "client hello, adding truncated_hmac extension" ) ); |
| 572 |
|
| 573 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
| 574 |
|
| 575 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 ); |
| 576 |
p += 2; |
| 577 |
|
| 578 |
*p++ = 0x00; |
| 579 |
*p++ = 0x00; |
| 580 |
|
| 581 |
*olen = 4; |
| 582 |
|
| 583 |
return( 0 ); |
| 584 |
} |
| 585 |
#endif |
| 586 |
|
| 587 |
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 588 |
static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
| 589 |
unsigned char *buf, |
| 590 |
const unsigned char *end, |
| 591 |
size_t *olen ) |
| 592 |
{ |
| 593 |
unsigned char *p = buf; |
| 594 |
|
| 595 |
*olen = 0; |
| 596 |
|
| 597 |
if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
| 598 |
ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 599 |
return( 0 ); |
| 600 |
|
| 601 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 602 |
( "client hello, adding encrypt_then_mac extension" ) ); |
| 603 |
|
| 604 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
| 605 |
|
| 606 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 ); |
| 607 |
p += 2; |
| 608 |
|
| 609 |
*p++ = 0x00; |
| 610 |
*p++ = 0x00; |
| 611 |
|
| 612 |
*olen = 4; |
| 613 |
|
| 614 |
return( 0 ); |
| 615 |
} |
| 616 |
#endif |
| 617 |
|
| 618 |
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 619 |
static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, |
| 620 |
unsigned char *buf, |
| 621 |
const unsigned char *end, |
| 622 |
size_t *olen ) |
| 623 |
{ |
| 624 |
unsigned char *p = buf; |
| 625 |
|
| 626 |
*olen = 0; |
| 627 |
|
| 628 |
if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
| 629 |
ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 630 |
return( 0 ); |
| 631 |
|
| 632 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 633 |
( "client hello, adding extended_master_secret extension" ) ); |
| 634 |
|
| 635 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
| 636 |
|
| 637 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 ); |
| 638 |
p += 2; |
| 639 |
|
| 640 |
*p++ = 0x00; |
| 641 |
*p++ = 0x00; |
| 642 |
|
| 643 |
*olen = 4; |
| 644 |
|
| 645 |
return( 0 ); |
| 646 |
} |
| 647 |
#endif |
| 648 |
|
| 649 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 650 |
static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, |
| 651 |
unsigned char *buf, |
| 652 |
const unsigned char *end, |
| 653 |
size_t *olen ) |
| 654 |
{ |
| 655 |
unsigned char *p = buf; |
| 656 |
size_t tlen = ssl->session_negotiate->ticket_len; |
| 657 |
|
| 658 |
*olen = 0; |
| 659 |
|
| 660 |
if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ) |
| 661 |
return( 0 ); |
| 662 |
|
| 663 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 664 |
( "client hello, adding session ticket extension" ) ); |
| 665 |
|
| 666 |
|
| 667 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); |
| 668 |
|
| 669 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 ); |
| 670 |
p += 2; |
| 671 |
|
| 672 |
MBEDTLS_PUT_UINT16_BE( tlen, p, 0 ); |
| 673 |
p += 2; |
| 674 |
|
| 675 |
*olen = 4; |
| 676 |
|
| 677 |
if( ssl->session_negotiate->ticket == NULL || tlen == 0 ) |
| 678 |
return( 0 ); |
| 679 |
|
| 680 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 681 |
( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) ); |
| 682 |
|
| 683 |
memcpy( p, ssl->session_negotiate->ticket, tlen ); |
| 684 |
|
| 685 |
*olen += tlen; |
| 686 |
|
| 687 |
return( 0 ); |
| 688 |
} |
| 689 |
#endif |
| 690 |
|
| 691 |
#if defined(MBEDTLS_SSL_ALPN) |
| 692 |
static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 693 |
unsigned char *buf, |
| 694 |
const unsigned char *end, |
| 695 |
size_t *olen ) |
| 696 |
{ |
| 697 |
unsigned char *p = buf; |
| 698 |
size_t alpnlen = 0; |
| 699 |
const char **cur; |
| 700 |
|
| 701 |
*olen = 0; |
| 702 |
|
| 703 |
if( ssl->conf->alpn_list == NULL ) |
| 704 |
return( 0 ); |
| 705 |
|
| 706 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
| 707 |
|
| 708 |
for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 709 |
alpnlen += strlen( *cur ) + 1; |
| 710 |
|
| 711 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen ); |
| 712 |
|
| 713 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 714 |
p += 2; |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
p += 4; |
| 726 |
|
| 727 |
for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 728 |
{ |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
*p = (unsigned char)strlen( *cur ); |
| 734 |
memcpy( p + 1, *cur, *p ); |
| 735 |
p += 1 + *p; |
| 736 |
} |
| 737 |
|
| 738 |
*olen = p - buf; |
| 739 |
|
| 740 |
|
| 741 |
MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 ); |
| 742 |
|
| 743 |
|
| 744 |
MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 ); |
| 745 |
|
| 746 |
return( 0 ); |
| 747 |
} |
| 748 |
#endif |
| 749 |
|
| 750 |
#if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 751 |
static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, |
| 752 |
unsigned char *buf, |
| 753 |
const unsigned char *end, |
| 754 |
size_t *olen ) |
| 755 |
{ |
| 756 |
unsigned char *p = buf; |
| 757 |
size_t protection_profiles_index = 0, ext_len = 0; |
| 758 |
uint16_t mki_len = 0, profile_value = 0; |
| 759 |
|
| 760 |
*olen = 0; |
| 761 |
|
| 762 |
if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 763 |
( ssl->conf->dtls_srtp_profile_list == NULL ) || |
| 764 |
( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) |
| 765 |
{ |
| 766 |
return( 0 ); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) |
| 779 |
{ |
| 780 |
mki_len = ssl->dtls_srtp_info.mki_len; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len; |
| 787 |
|
| 788 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) ); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); |
| 795 |
|
| 796 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 ); |
| 797 |
p += 2; |
| 798 |
|
| 799 |
MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); |
| 800 |
p += 2; |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
*p++ = 0; |
| 811 |
*p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len ); |
| 812 |
|
| 813 |
for( protection_profiles_index=0; |
| 814 |
protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; |
| 815 |
protection_profiles_index++ ) |
| 816 |
{ |
| 817 |
profile_value = mbedtls_ssl_check_srtp_profile_value |
| 818 |
( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] ); |
| 819 |
if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) |
| 820 |
{ |
| 821 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x", |
| 822 |
profile_value ) ); |
| 823 |
MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 ); |
| 824 |
p += 2; |
| 825 |
} |
| 826 |
else |
| 827 |
{ |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 833 |
( "client hello, " |
| 834 |
"illegal DTLS-SRTP protection profile %d", |
| 835 |
ssl->conf->dtls_srtp_profile_list[protection_profiles_index] |
| 836 |
) ); |
| 837 |
return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
*p++ = mki_len & 0xFF; |
| 842 |
|
| 843 |
if( mki_len != 0 ) |
| 844 |
{ |
| 845 |
memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len ); |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
p += mki_len; |
| 850 |
MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value, |
| 851 |
ssl->dtls_srtp_info.mki_len ); |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
*olen = p - buf; |
| 863 |
|
| 864 |
return( 0 ); |
| 865 |
} |
| 866 |
#endif |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
| 872 |
{ |
| 873 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 874 |
unsigned char *p = ssl->handshake->randbytes; |
| 875 |
#if defined(MBEDTLS_HAVE_TIME) |
| 876 |
mbedtls_time_t t; |
| 877 |
#endif |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 883 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 884 |
ssl->handshake->verify_cookie != NULL ) |
| 885 |
{ |
| 886 |
return( 0 ); |
| 887 |
} |
| 888 |
#endif |
| 889 |
|
| 890 |
#if defined(MBEDTLS_HAVE_TIME) |
| 891 |
t = mbedtls_time( NULL ); |
| 892 |
MBEDTLS_PUT_UINT32_BE( t, p, 0 ); |
| 893 |
p += 4; |
| 894 |
|
| 895 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, |
| 896 |
(long long) t ) ); |
| 897 |
#else |
| 898 |
if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 ) |
| 899 |
return( ret ); |
| 900 |
|
| 901 |
p += 4; |
| 902 |
#endif |
| 903 |
|
| 904 |
if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 ) |
| 905 |
return( ret ); |
| 906 |
|
| 907 |
return( 0 ); |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
static int ssl_validate_ciphersuite( |
| 921 |
const mbedtls_ssl_ciphersuite_t * suite_info, |
| 922 |
const mbedtls_ssl_context * ssl, |
| 923 |
int min_minor_ver, int max_minor_ver ) |
| 924 |
{ |
| 925 |
(void) ssl; |
| 926 |
if( suite_info == NULL ) |
| 927 |
return( 1 ); |
| 928 |
|
| 929 |
if( suite_info->min_minor_ver > max_minor_ver || |
| 930 |
suite_info->max_minor_ver < min_minor_ver ) |
| 931 |
return( 1 ); |
| 932 |
|
| 933 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 934 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 935 |
( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 936 |
return( 1 ); |
| 937 |
#endif |
| 938 |
|
| 939 |
#if defined(MBEDTLS_ARC4_C) |
| 940 |
if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
| 941 |
suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
| 942 |
return( 1 ); |
| 943 |
#endif |
| 944 |
|
| 945 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 946 |
if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 947 |
mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 948 |
return( 1 ); |
| 949 |
#endif |
| 950 |
|
| 951 |
|
| 952 |
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 953 |
if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 954 |
ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 955 |
{ |
| 956 |
return( 1 ); |
| 957 |
} |
| 958 |
#endif |
| 959 |
|
| 960 |
return( 0 ); |
| 961 |
} |
| 962 |
|
| 963 |
static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 964 |
{ |
| 965 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 966 |
size_t i, n, olen, ext_len = 0; |
| 967 |
|
| 968 |
unsigned char *buf; |
| 969 |
unsigned char *p, *q; |
| 970 |
const unsigned char *end; |
| 971 |
|
| 972 |
unsigned char offer_compress; |
| 973 |
const int *ciphersuites; |
| 974 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 975 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 976 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 977 |
int uses_ec = 0; |
| 978 |
#endif |
| 979 |
|
| 980 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 981 |
|
| 982 |
if( ssl->conf->f_rng == NULL ) |
| 983 |
{ |
| 984 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 985 |
return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 986 |
} |
| 987 |
|
| 988 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 989 |
if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 990 |
#endif |
| 991 |
{ |
| 992 |
ssl->major_ver = ssl->conf->min_major_ver; |
| 993 |
ssl->minor_ver = ssl->conf->min_minor_ver; |
| 994 |
} |
| 995 |
|
| 996 |
if( ssl->conf->max_major_ver == 0 ) |
| 997 |
{ |
| 998 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 999 |
( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) ); |
| 1000 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1001 |
} |
| 1002 |
|
| 1003 |
buf = ssl->out_msg; |
| 1004 |
end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 ); |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
p = buf + 4; |
| 1030 |
mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 1031 |
ssl->conf->max_minor_ver, |
| 1032 |
ssl->conf->transport, p ); |
| 1033 |
p += 2; |
| 1034 |
|
| 1035 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", |
| 1036 |
buf[4], buf[5] ) ); |
| 1037 |
|
| 1038 |
if( ( ret = ssl_generate_random( ssl ) ) != 0 ) |
| 1039 |
{ |
| 1040 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); |
| 1041 |
return( ret ); |
| 1042 |
} |
| 1043 |
|
| 1044 |
memcpy( p, ssl->handshake->randbytes, 32 ); |
| 1045 |
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 ); |
| 1046 |
p += 32; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
n = ssl->session_negotiate->id_len; |
| 1061 |
|
| 1062 |
if( n < 16 || n > 32 || |
| 1063 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1064 |
ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
| 1065 |
#endif |
| 1066 |
ssl->handshake->resume == 0 ) |
| 1067 |
{ |
| 1068 |
n = 0; |
| 1069 |
} |
| 1070 |
|
| 1071 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1077 |
if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 1078 |
#endif |
| 1079 |
{ |
| 1080 |
if( ssl->session_negotiate->ticket != NULL && |
| 1081 |
ssl->session_negotiate->ticket_len != 0 ) |
| 1082 |
{ |
| 1083 |
ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 1084 |
ssl->session_negotiate->id, 32 ); |
| 1085 |
|
| 1086 |
if( ret != 0 ) |
| 1087 |
return( ret ); |
| 1088 |
|
| 1089 |
ssl->session_negotiate->id_len = n = 32; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
#endif |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
*p++ = (unsigned char) n; |
| 1101 |
|
| 1102 |
for( i = 0; i < n; i++ ) |
| 1103 |
*p++ = ssl->session_negotiate->id[i]; |
| 1104 |
|
| 1105 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); |
| 1106 |
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n ); |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1125 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1126 |
{ |
| 1127 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); |
| 1128 |
|
| 1129 |
if( ssl->handshake->verify_cookie == NULL ) |
| 1130 |
{ |
| 1131 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) ); |
| 1132 |
*p++ = 0; |
| 1133 |
} |
| 1134 |
else |
| 1135 |
{ |
| 1136 |
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
| 1137 |
ssl->handshake->verify_cookie, |
| 1138 |
ssl->handshake->verify_cookie_len ); |
| 1139 |
|
| 1140 |
*p++ = ssl->handshake->verify_cookie_len; |
| 1141 |
|
| 1142 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, |
| 1143 |
ssl->handshake->verify_cookie_len ); |
| 1144 |
memcpy( p, ssl->handshake->verify_cookie, |
| 1145 |
ssl->handshake->verify_cookie_len ); |
| 1146 |
p += ssl->handshake->verify_cookie_len; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
#endif |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; |
| 1155 |
|
| 1156 |
|
| 1157 |
n = 0; |
| 1158 |
q = p; |
| 1159 |
|
| 1160 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1161 |
p += 2; |
| 1162 |
|
| 1163 |
for( i = 0; ciphersuites[i] != 0; i++ ) |
| 1164 |
{ |
| 1165 |
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); |
| 1166 |
|
| 1167 |
if( ssl_validate_ciphersuite( ciphersuite_info, ssl, |
| 1168 |
ssl->conf->min_minor_ver, |
| 1169 |
ssl->conf->max_minor_ver ) != 0 ) |
| 1170 |
continue; |
| 1171 |
|
| 1172 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)", |
| 1173 |
(unsigned int)ciphersuites[i], ciphersuite_info->name ) ); |
| 1174 |
|
| 1175 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 1176 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1177 |
uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); |
| 1178 |
#endif |
| 1179 |
|
| 1180 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1181 |
|
| 1182 |
n++; |
| 1183 |
MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 ); |
| 1184 |
p += 2; |
| 1185 |
} |
| 1186 |
|
| 1187 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1188 |
( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) ); |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1194 |
if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 1195 |
#endif |
| 1196 |
{ |
| 1197 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); |
| 1198 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1199 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 ); |
| 1200 |
p += 2; |
| 1201 |
n++; |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
#if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
| 1206 |
if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK ) |
| 1207 |
{ |
| 1208 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); |
| 1209 |
|
| 1210 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1211 |
MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_FALLBACK_SCSV_VALUE, p, 0 ); |
| 1212 |
p += 2; |
| 1213 |
n++; |
| 1214 |
} |
| 1215 |
#endif |
| 1216 |
|
| 1217 |
*q++ = (unsigned char)( n >> 7 ); |
| 1218 |
*q++ = (unsigned char)( n << 1 ); |
| 1219 |
|
| 1220 |
#if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1221 |
offer_compress = 1; |
| 1222 |
#else |
| 1223 |
offer_compress = 0; |
| 1224 |
#endif |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1233 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1234 |
offer_compress = 0; |
| 1235 |
#endif |
| 1236 |
|
| 1237 |
if( offer_compress ) |
| 1238 |
{ |
| 1239 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) ); |
| 1240 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d", |
| 1241 |
MBEDTLS_SSL_COMPRESS_DEFLATE, |
| 1242 |
MBEDTLS_SSL_COMPRESS_NULL ) ); |
| 1243 |
|
| 1244 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 ); |
| 1245 |
*p++ = 2; |
| 1246 |
*p++ = MBEDTLS_SSL_COMPRESS_DEFLATE; |
| 1247 |
*p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 1248 |
} |
| 1249 |
else |
| 1250 |
{ |
| 1251 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); |
| 1252 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", |
| 1253 |
MBEDTLS_SSL_COMPRESS_NULL ) ); |
| 1254 |
|
| 1255 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1256 |
*p++ = 1; |
| 1257 |
*p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 1263 |
|
| 1264 |
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1265 |
if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len, |
| 1266 |
end, &olen ) ) != 0 ) |
| 1267 |
{ |
| 1268 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret ); |
| 1269 |
return( ret ); |
| 1270 |
} |
| 1271 |
ext_len += olen; |
| 1272 |
#endif |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1277 |
if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, |
| 1278 |
end, &olen ) ) != 0 ) |
| 1279 |
{ |
| 1280 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret ); |
| 1281 |
return( ret ); |
| 1282 |
} |
| 1283 |
ext_len += olen; |
| 1284 |
#endif |
| 1285 |
|
| 1286 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1287 |
defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 1288 |
if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, |
| 1289 |
end, &olen ) ) != 0 ) |
| 1290 |
{ |
| 1291 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret ); |
| 1292 |
return( ret ); |
| 1293 |
} |
| 1294 |
ext_len += olen; |
| 1295 |
#endif |
| 1296 |
|
| 1297 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 1298 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1299 |
if( uses_ec ) |
| 1300 |
{ |
| 1301 |
if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, |
| 1302 |
end, &olen ) ) != 0 ) |
| 1303 |
{ |
| 1304 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret ); |
| 1305 |
return( ret ); |
| 1306 |
} |
| 1307 |
ext_len += olen; |
| 1308 |
|
| 1309 |
if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, |
| 1310 |
end, &olen ) ) != 0 ) |
| 1311 |
{ |
| 1312 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret ); |
| 1313 |
return( ret ); |
| 1314 |
} |
| 1315 |
ext_len += olen; |
| 1316 |
} |
| 1317 |
#endif |
| 1318 |
|
| 1319 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1320 |
if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, |
| 1321 |
end, &olen ) ) != 0 ) |
| 1322 |
{ |
| 1323 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret ); |
| 1324 |
return( ret ); |
| 1325 |
} |
| 1326 |
ext_len += olen; |
| 1327 |
#endif |
| 1328 |
|
| 1329 |
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1330 |
if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 ) |
| 1331 |
{ |
| 1332 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret ); |
| 1333 |
return( ret ); |
| 1334 |
} |
| 1335 |
ext_len += olen; |
| 1336 |
#endif |
| 1337 |
|
| 1338 |
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1339 |
if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, |
| 1340 |
end, &olen ) ) != 0 ) |
| 1341 |
{ |
| 1342 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); |
| 1343 |
return( ret ); |
| 1344 |
} |
| 1345 |
ext_len += olen; |
| 1346 |
#endif |
| 1347 |
|
| 1348 |
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1349 |
if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, |
| 1350 |
end, &olen ) ) != 0 ) |
| 1351 |
{ |
| 1352 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret ); |
| 1353 |
return( ret ); |
| 1354 |
} |
| 1355 |
ext_len += olen; |
| 1356 |
#endif |
| 1357 |
|
| 1358 |
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1359 |
if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, |
| 1360 |
end, &olen ) ) != 0 ) |
| 1361 |
{ |
| 1362 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret ); |
| 1363 |
return( ret ); |
| 1364 |
} |
| 1365 |
ext_len += olen; |
| 1366 |
#endif |
| 1367 |
|
| 1368 |
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1369 |
if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, |
| 1370 |
end, &olen ) ) != 0 ) |
| 1371 |
{ |
| 1372 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret ); |
| 1373 |
return( ret ); |
| 1374 |
} |
| 1375 |
ext_len += olen; |
| 1376 |
#endif |
| 1377 |
|
| 1378 |
#if defined(MBEDTLS_SSL_ALPN) |
| 1379 |
if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len, |
| 1380 |
end, &olen ) ) != 0 ) |
| 1381 |
{ |
| 1382 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret ); |
| 1383 |
return( ret ); |
| 1384 |
} |
| 1385 |
ext_len += olen; |
| 1386 |
#endif |
| 1387 |
|
| 1388 |
#if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 1389 |
if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, |
| 1390 |
end, &olen ) ) != 0 ) |
| 1391 |
{ |
| 1392 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret ); |
| 1393 |
return( ret ); |
| 1394 |
} |
| 1395 |
ext_len += olen; |
| 1396 |
#endif |
| 1397 |
|
| 1398 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1399 |
if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, |
| 1400 |
end, &olen ) ) != 0 ) |
| 1401 |
{ |
| 1402 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret ); |
| 1403 |
return( ret ); |
| 1404 |
} |
| 1405 |
ext_len += olen; |
| 1406 |
#endif |
| 1407 |
|
| 1408 |
|
| 1409 |
((void) olen); |
| 1410 |
|
| 1411 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET, |
| 1412 |
ext_len ) ); |
| 1413 |
|
| 1414 |
if( ext_len > 0 ) |
| 1415 |
{ |
| 1416 |
|
| 1417 |
|
| 1418 |
MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); |
| 1419 |
p += 2 + ext_len; |
| 1420 |
} |
| 1421 |
|
| 1422 |
ssl->out_msglen = p - buf; |
| 1423 |
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1424 |
ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO; |
| 1425 |
|
| 1426 |
ssl->state++; |
| 1427 |
|
| 1428 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1429 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1430 |
mbedtls_ssl_send_flight_completed( ssl ); |
| 1431 |
#endif |
| 1432 |
|
| 1433 |
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 1434 |
{ |
| 1435 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 1436 |
return( ret ); |
| 1437 |
} |
| 1438 |
|
| 1439 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1440 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 1441 |
( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 1442 |
{ |
| 1443 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 1444 |
return( ret ); |
| 1445 |
} |
| 1446 |
#endif |
| 1447 |
|
| 1448 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 1449 |
|
| 1450 |
return( 0 ); |
| 1451 |
} |
| 1452 |
|
| 1453 |
static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
| 1454 |
const unsigned char *buf, |
| 1455 |
size_t len ) |
| 1456 |
{ |
| 1457 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1458 |
if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 1459 |
{ |
| 1460 |
|
| 1461 |
if( len != 1 + ssl->verify_data_len * 2 || |
| 1462 |
buf[0] != ssl->verify_data_len * 2 || |
| 1463 |
mbedtls_ct_memcmp( buf + 1, |
| 1464 |
ssl->own_verify_data, ssl->verify_data_len ) != 0 || |
| 1465 |
mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len, |
| 1466 |
ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) |
| 1467 |
{ |
| 1468 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
| 1469 |
mbedtls_ssl_send_alert_message( |
| 1470 |
ssl, |
| 1471 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1472 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1473 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
else |
| 1477 |
#endif |
| 1478 |
{ |
| 1479 |
if( len != 1 || buf[0] != 0x00 ) |
| 1480 |
{ |
| 1481 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1482 |
( "non-zero length renegotiation info" ) ); |
| 1483 |
mbedtls_ssl_send_alert_message( |
| 1484 |
ssl, |
| 1485 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1486 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1487 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1488 |
} |
| 1489 |
|
| 1490 |
ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
| 1491 |
} |
| 1492 |
|
| 1493 |
return( 0 ); |
| 1494 |
} |
| 1495 |
|
| 1496 |
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1497 |
static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
| 1498 |
const unsigned char *buf, |
| 1499 |
size_t len ) |
| 1500 |
{ |
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE || |
| 1506 |
len != 1 || |
| 1507 |
buf[0] != ssl->conf->mfl_code ) |
| 1508 |
{ |
| 1509 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1510 |
( "non-matching max fragment length extension" ) ); |
| 1511 |
mbedtls_ssl_send_alert_message( |
| 1512 |
ssl, |
| 1513 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1514 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 1515 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1516 |
} |
| 1517 |
|
| 1518 |
return( 0 ); |
| 1519 |
} |
| 1520 |
#endif |
| 1521 |
|
| 1522 |
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1523 |
static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl, |
| 1524 |
const unsigned char *buf, |
| 1525 |
size_t len ) |
| 1526 |
{ |
| 1527 |
if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED || |
| 1528 |
len != 0 ) |
| 1529 |
{ |
| 1530 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1531 |
( "non-matching truncated HMAC extension" ) ); |
| 1532 |
mbedtls_ssl_send_alert_message( |
| 1533 |
ssl, |
| 1534 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1535 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1536 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1537 |
} |
| 1538 |
|
| 1539 |
((void) buf); |
| 1540 |
|
| 1541 |
ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
| 1542 |
|
| 1543 |
return( 0 ); |
| 1544 |
} |
| 1545 |
#endif |
| 1546 |
|
| 1547 |
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1548 |
static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 1549 |
const unsigned char *buf, |
| 1550 |
size_t len ) |
| 1551 |
{ |
| 1552 |
size_t peer_cid_len; |
| 1553 |
|
| 1554 |
if( |
| 1555 |
ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 1556 |
|
| 1557 |
ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 1558 |
{ |
| 1559 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); |
| 1560 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1561 |
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 1562 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1563 |
} |
| 1564 |
|
| 1565 |
if( len == 0 ) |
| 1566 |
{ |
| 1567 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 1568 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1569 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1570 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1571 |
} |
| 1572 |
|
| 1573 |
peer_cid_len = *buf++; |
| 1574 |
len--; |
| 1575 |
|
| 1576 |
if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 1577 |
{ |
| 1578 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 1579 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1580 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1581 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1582 |
} |
| 1583 |
|
| 1584 |
if( len != peer_cid_len ) |
| 1585 |
{ |
| 1586 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 1587 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1588 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 1589 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1590 |
} |
| 1591 |
|
| 1592 |
ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
| 1593 |
ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 1594 |
memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 1595 |
|
| 1596 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 1597 |
MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); |
| 1598 |
|
| 1599 |
return( 0 ); |
| 1600 |
} |
| 1601 |
#endif |
| 1602 |
|
| 1603 |
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1604 |
static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
| 1605 |
const unsigned char *buf, |
| 1606 |
size_t len ) |
| 1607 |
{ |
| 1608 |
if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
| 1609 |
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1610 |
len != 0 ) |
| 1611 |
{ |
| 1612 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1613 |
( "non-matching encrypt-then-MAC extension" ) ); |
| 1614 |
mbedtls_ssl_send_alert_message( |
| 1615 |
ssl, |
| 1616 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1617 |
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 1618 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1619 |
} |
| 1620 |
|
| 1621 |
((void) buf); |
| 1622 |
|
| 1623 |
ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 1624 |
|
| 1625 |
return( 0 ); |
| 1626 |
} |
| 1627 |
#endif |
| 1628 |
|
| 1629 |
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1630 |
static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl, |
| 1631 |
const unsigned char *buf, |
| 1632 |
size_t len ) |
| 1633 |
{ |
| 1634 |
if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
| 1635 |
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1636 |
len != 0 ) |
| 1637 |
{ |
| 1638 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1639 |
( "non-matching extended master secret extension" ) ); |
| 1640 |
mbedtls_ssl_send_alert_message( |
| 1641 |
ssl, |
| 1642 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1643 |
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 1644 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1645 |
} |
| 1646 |
|
| 1647 |
((void) buf); |
| 1648 |
|
| 1649 |
ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 1650 |
|
| 1651 |
return( 0 ); |
| 1652 |
} |
| 1653 |
#endif |
| 1654 |
|
| 1655 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1656 |
static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl, |
| 1657 |
const unsigned char *buf, |
| 1658 |
size_t len ) |
| 1659 |
{ |
| 1660 |
if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || |
| 1661 |
len != 0 ) |
| 1662 |
{ |
| 1663 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1664 |
( "non-matching session ticket extension" ) ); |
| 1665 |
mbedtls_ssl_send_alert_message( |
| 1666 |
ssl, |
| 1667 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1668 |
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 1669 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1670 |
} |
| 1671 |
|
| 1672 |
((void) buf); |
| 1673 |
|
| 1674 |
ssl->handshake->new_session_ticket = 1; |
| 1675 |
|
| 1676 |
return( 0 ); |
| 1677 |
} |
| 1678 |
#endif |
| 1679 |
|
| 1680 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 1681 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1682 |
static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
| 1683 |
const unsigned char *buf, |
| 1684 |
size_t len ) |
| 1685 |
{ |
| 1686 |
size_t list_size; |
| 1687 |
const unsigned char *p; |
| 1688 |
|
| 1689 |
if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
| 1690 |
{ |
| 1691 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 1692 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1693 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1694 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1695 |
} |
| 1696 |
list_size = buf[0]; |
| 1697 |
|
| 1698 |
p = buf + 1; |
| 1699 |
while( list_size > 0 ) |
| 1700 |
{ |
| 1701 |
if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 1702 |
p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
| 1703 |
{ |
| 1704 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
| 1705 |
ssl->handshake->ecdh_ctx.point_format = p[0]; |
| 1706 |
#endif |
| 1707 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1708 |
ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 1709 |
#endif |
| 1710 |
MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
| 1711 |
return( 0 ); |
| 1712 |
} |
| 1713 |
|
| 1714 |
list_size--; |
| 1715 |
p++; |
| 1716 |
} |
| 1717 |
|
| 1718 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); |
| 1719 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1720 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1721 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1722 |
} |
| 1723 |
#endif |
| 1724 |
|
| 1725 |
|
| 1726 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1727 |
static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 1728 |
const unsigned char *buf, |
| 1729 |
size_t len ) |
| 1730 |
{ |
| 1731 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1732 |
|
| 1733 |
if( ssl->handshake->ciphersuite_info->key_exchange != |
| 1734 |
MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 1735 |
{ |
| 1736 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 1737 |
return( 0 ); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
mbedtls_free( ssl->handshake->ecjpake_cache ); |
| 1742 |
ssl->handshake->ecjpake_cache = NULL; |
| 1743 |
ssl->handshake->ecjpake_cache_len = 0; |
| 1744 |
|
| 1745 |
if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 1746 |
buf, len ) ) != 0 ) |
| 1747 |
{ |
| 1748 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
| 1749 |
mbedtls_ssl_send_alert_message( |
| 1750 |
ssl, |
| 1751 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1752 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1753 |
return( ret ); |
| 1754 |
} |
| 1755 |
|
| 1756 |
return( 0 ); |
| 1757 |
} |
| 1758 |
#endif |
| 1759 |
|
| 1760 |
#if defined(MBEDTLS_SSL_ALPN) |
| 1761 |
static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 1762 |
const unsigned char *buf, size_t len ) |
| 1763 |
{ |
| 1764 |
size_t list_len, name_len; |
| 1765 |
const char **p; |
| 1766 |
|
| 1767 |
|
| 1768 |
if( ssl->conf->alpn_list == NULL ) |
| 1769 |
{ |
| 1770 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); |
| 1771 |
mbedtls_ssl_send_alert_message( |
| 1772 |
ssl, |
| 1773 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1774 |
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 1775 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
if( len < 4 ) |
| 1790 |
{ |
| 1791 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1792 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1793 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1794 |
} |
| 1795 |
|
| 1796 |
list_len = ( buf[0] << 8 ) | buf[1]; |
| 1797 |
if( list_len != len - 2 ) |
| 1798 |
{ |
| 1799 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1800 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1801 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1802 |
} |
| 1803 |
|
| 1804 |
name_len = buf[2]; |
| 1805 |
if( name_len != list_len - 1 ) |
| 1806 |
{ |
| 1807 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1808 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1809 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
for( p = ssl->conf->alpn_list; *p != NULL; p++ ) |
| 1814 |
{ |
| 1815 |
if( name_len == strlen( *p ) && |
| 1816 |
memcmp( buf + 3, *p, name_len ) == 0 ) |
| 1817 |
{ |
| 1818 |
ssl->alpn_chosen = *p; |
| 1819 |
return( 0 ); |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); |
| 1824 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1825 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1826 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1827 |
} |
| 1828 |
#endif |
| 1829 |
|
| 1830 |
#if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 1831 |
static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, |
| 1832 |
const unsigned char *buf, |
| 1833 |
size_t len ) |
| 1834 |
{ |
| 1835 |
mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET; |
| 1836 |
size_t i, mki_len = 0; |
| 1837 |
uint16_t server_protection_profile_value = 0; |
| 1838 |
|
| 1839 |
|
| 1840 |
if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 1841 |
( ssl->conf->dtls_srtp_profile_list == NULL ) || |
| 1842 |
( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) |
| 1843 |
return( 0 ); |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) |
| 1857 |
{ |
| 1858 |
mki_len = ssl->dtls_srtp_info.mki_len; |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) ) |
| 1868 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
if( ( buf[0] != 0 ) || ( buf[1] != 2 ) ) |
| 1879 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1880 |
|
| 1881 |
server_protection_profile_value = ( buf[2] << 8 ) | buf[3]; |
| 1882 |
server_protection = mbedtls_ssl_check_srtp_profile_value( |
| 1883 |
server_protection_profile_value ); |
| 1884 |
if( server_protection != MBEDTLS_TLS_SRTP_UNSET ) |
| 1885 |
{ |
| 1886 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", |
| 1887 |
mbedtls_ssl_get_srtp_profile_as_string( |
| 1888 |
server_protection ) ) ); |
| 1889 |
} |
| 1890 |
|
| 1891 |
ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; |
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) |
| 1897 |
{ |
| 1898 |
if( server_protection == ssl->conf->dtls_srtp_profile_list[i] ) |
| 1899 |
{ |
| 1900 |
ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; |
| 1901 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", |
| 1902 |
mbedtls_ssl_get_srtp_profile_as_string( |
| 1903 |
server_protection ) ) ); |
| 1904 |
break; |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
| 1910 |
{ |
| 1911 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1912 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1913 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1914 |
} |
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
if( len == 5 ) |
| 1919 |
{ |
| 1920 |
ssl->dtls_srtp_info.mki_len = 0; |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
if( len > 5 && ( buf[4] != mki_len || |
| 1930 |
( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) ) |
| 1931 |
{ |
| 1932 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1933 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 1934 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1935 |
} |
| 1936 |
#if defined (MBEDTLS_DEBUG_C) |
| 1937 |
if( len > 5 ) |
| 1938 |
{ |
| 1939 |
MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value, |
| 1940 |
ssl->dtls_srtp_info.mki_len ); |
| 1941 |
} |
| 1942 |
#endif |
| 1943 |
return( 0 ); |
| 1944 |
} |
| 1945 |
#endif |
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1951 |
static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl ) |
| 1952 |
{ |
| 1953 |
const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
| 1954 |
int major_ver, minor_ver; |
| 1955 |
unsigned char cookie_len; |
| 1956 |
|
| 1957 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); |
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen ) |
| 1964 |
{ |
| 1965 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1966 |
( "incoming HelloVerifyRequest message is too short" ) ); |
| 1967 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1968 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1969 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1970 |
} |
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
| 1979 |
mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p ); |
| 1980 |
p += 2; |
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 1987 |
minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1988 |
major_ver > ssl->conf->max_major_ver || |
| 1989 |
minor_ver > ssl->conf->max_minor_ver ) |
| 1990 |
{ |
| 1991 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); |
| 1992 |
|
| 1993 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1994 |
MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
| 1995 |
|
| 1996 |
return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1997 |
} |
| 1998 |
|
| 1999 |
cookie_len = *p++; |
| 2000 |
if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) |
| 2001 |
{ |
| 2002 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2003 |
( "cookie length does not match incoming message size" ) ); |
| 2004 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2005 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2006 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2007 |
} |
| 2008 |
MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); |
| 2009 |
|
| 2010 |
mbedtls_free( ssl->handshake->verify_cookie ); |
| 2011 |
|
| 2012 |
ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len ); |
| 2013 |
if( ssl->handshake->verify_cookie == NULL ) |
| 2014 |
{ |
| 2015 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) ); |
| 2016 |
return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2017 |
} |
| 2018 |
|
| 2019 |
memcpy( ssl->handshake->verify_cookie, p, cookie_len ); |
| 2020 |
ssl->handshake->verify_cookie_len = cookie_len; |
| 2021 |
|
| 2022 |
|
| 2023 |
ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 2024 |
mbedtls_ssl_reset_checksum( ssl ); |
| 2025 |
|
| 2026 |
mbedtls_ssl_recv_flight_completed( ssl ); |
| 2027 |
|
| 2028 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); |
| 2029 |
|
| 2030 |
return( 0 ); |
| 2031 |
} |
| 2032 |
#endif |
| 2033 |
|
| 2034 |
static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) |
| 2035 |
{ |
| 2036 |
int ret, i; |
| 2037 |
size_t n; |
| 2038 |
size_t ext_len; |
| 2039 |
unsigned char *buf, *ext; |
| 2040 |
unsigned char comp; |
| 2041 |
#if defined(MBEDTLS_ZLIB_SUPPORT) |
| 2042 |
int accept_comp; |
| 2043 |
#endif |
| 2044 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2045 |
int renegotiation_info_seen = 0; |
| 2046 |
#endif |
| 2047 |
int handshake_failure = 0; |
| 2048 |
const mbedtls_ssl_ciphersuite_t *suite_info; |
| 2049 |
|
| 2050 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); |
| 2051 |
|
| 2052 |
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 2053 |
{ |
| 2054 |
|
| 2055 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 2056 |
return( ret ); |
| 2057 |
} |
| 2058 |
|
| 2059 |
buf = ssl->in_msg; |
| 2060 |
|
| 2061 |
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 2062 |
{ |
| 2063 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2064 |
if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 2065 |
{ |
| 2066 |
ssl->renego_records_seen++; |
| 2067 |
|
| 2068 |
if( ssl->conf->renego_max_records >= 0 && |
| 2069 |
ssl->renego_records_seen > ssl->conf->renego_max_records ) |
| 2070 |
{ |
| 2071 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2072 |
( "renegotiation requested, but not honored by server" ) ); |
| 2073 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2074 |
} |
| 2075 |
|
| 2076 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2077 |
( "non-handshake message during renegotiation" ) ); |
| 2078 |
|
| 2079 |
ssl->keep_current_message = 1; |
| 2080 |
return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); |
| 2081 |
} |
| 2082 |
#endif |
| 2083 |
|
| 2084 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2085 |
mbedtls_ssl_send_alert_message( |
| 2086 |
ssl, |
| 2087 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2088 |
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 2089 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2090 |
} |
| 2091 |
|
| 2092 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2093 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2094 |
{ |
| 2095 |
if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
| 2096 |
{ |
| 2097 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); |
| 2098 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
| 2099 |
return( ssl_parse_hello_verify_request( ssl ) ); |
| 2100 |
} |
| 2101 |
else |
| 2102 |
{ |
| 2103 |
|
| 2104 |
mbedtls_free( ssl->handshake->verify_cookie ); |
| 2105 |
ssl->handshake->verify_cookie = NULL; |
| 2106 |
ssl->handshake->verify_cookie_len = 0; |
| 2107 |
} |
| 2108 |
} |
| 2109 |
#endif |
| 2110 |
|
| 2111 |
if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || |
| 2112 |
buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) |
| 2113 |
{ |
| 2114 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2115 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2116 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2117 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2118 |
} |
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
buf += mbedtls_ssl_hs_hdr_len( ssl ); |
| 2132 |
|
| 2133 |
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 ); |
| 2134 |
mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, |
| 2135 |
ssl->conf->transport, buf + 0 ); |
| 2136 |
|
| 2137 |
if( ssl->major_ver < ssl->conf->min_major_ver || |
| 2138 |
ssl->minor_ver < ssl->conf->min_minor_ver || |
| 2139 |
ssl->major_ver > ssl->conf->max_major_ver || |
| 2140 |
ssl->minor_ver > ssl->conf->max_minor_ver ) |
| 2141 |
{ |
| 2142 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2143 |
( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]", |
| 2144 |
ssl->conf->min_major_ver, |
| 2145 |
ssl->conf->min_minor_ver, |
| 2146 |
ssl->major_ver, ssl->minor_ver, |
| 2147 |
ssl->conf->max_major_ver, |
| 2148 |
ssl->conf->max_minor_ver ) ); |
| 2149 |
|
| 2150 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2151 |
MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
| 2152 |
|
| 2153 |
return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 2154 |
} |
| 2155 |
|
| 2156 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", |
| 2157 |
( (unsigned long) buf[2] << 24 ) | |
| 2158 |
( (unsigned long) buf[3] << 16 ) | |
| 2159 |
( (unsigned long) buf[4] << 8 ) | |
| 2160 |
( (unsigned long) buf[5] ) ) ); |
| 2161 |
|
| 2162 |
memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); |
| 2163 |
|
| 2164 |
n = buf[34]; |
| 2165 |
|
| 2166 |
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 ); |
| 2167 |
|
| 2168 |
if( n > 32 ) |
| 2169 |
{ |
| 2170 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2171 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2172 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2173 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2174 |
} |
| 2175 |
|
| 2176 |
if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) |
| 2177 |
{ |
| 2178 |
ext_len = ( ( buf[38 + n] << 8 ) |
| 2179 |
| ( buf[39 + n] ) ); |
| 2180 |
|
| 2181 |
if( ( ext_len > 0 && ext_len < 4 ) || |
| 2182 |
ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len ) |
| 2183 |
{ |
| 2184 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2185 |
mbedtls_ssl_send_alert_message( |
| 2186 |
ssl, |
| 2187 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2188 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2189 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2190 |
} |
| 2191 |
} |
| 2192 |
else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n ) |
| 2193 |
{ |
| 2194 |
ext_len = 0; |
| 2195 |
} |
| 2196 |
else |
| 2197 |
{ |
| 2198 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2199 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2200 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2201 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2202 |
} |
| 2203 |
|
| 2204 |
|
| 2205 |
i = ( buf[35 + n] << 8 ) | buf[36 + n]; |
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
comp = buf[37 + n]; |
| 2211 |
|
| 2212 |
#if defined(MBEDTLS_ZLIB_SUPPORT) |
| 2213 |
|
| 2214 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2215 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2216 |
accept_comp = 0; |
| 2217 |
else |
| 2218 |
#endif |
| 2219 |
accept_comp = 1; |
| 2220 |
|
| 2221 |
if( comp != MBEDTLS_SSL_COMPRESS_NULL && |
| 2222 |
( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) ) |
| 2223 |
#else |
| 2224 |
if( comp != MBEDTLS_SSL_COMPRESS_NULL ) |
| 2225 |
#endif |
| 2226 |
{ |
| 2227 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2228 |
( "server hello, bad compression: %d", comp ) ); |
| 2229 |
mbedtls_ssl_send_alert_message( |
| 2230 |
ssl, |
| 2231 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2232 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2233 |
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2234 |
} |
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); |
| 2240 |
if( ssl->handshake->ciphersuite_info == NULL ) |
| 2241 |
{ |
| 2242 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2243 |
( "ciphersuite info for %04x not found", (unsigned int)i ) ); |
| 2244 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2245 |
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2246 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2247 |
} |
| 2248 |
|
| 2249 |
mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); |
| 2250 |
|
| 2251 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); |
| 2252 |
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n ); |
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
if( ssl->handshake->resume == 0 || n == 0 || |
| 2258 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2259 |
ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
| 2260 |
#endif |
| 2261 |
ssl->session_negotiate->ciphersuite != i || |
| 2262 |
ssl->session_negotiate->compression != comp || |
| 2263 |
ssl->session_negotiate->id_len != n || |
| 2264 |
memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) |
| 2265 |
{ |
| 2266 |
ssl->state++; |
| 2267 |
ssl->handshake->resume = 0; |
| 2268 |
#if defined(MBEDTLS_HAVE_TIME) |
| 2269 |
ssl->session_negotiate->start = mbedtls_time( NULL ); |
| 2270 |
#endif |
| 2271 |
ssl->session_negotiate->ciphersuite = i; |
| 2272 |
ssl->session_negotiate->compression = comp; |
| 2273 |
ssl->session_negotiate->id_len = n; |
| 2274 |
memcpy( ssl->session_negotiate->id, buf + 35, n ); |
| 2275 |
} |
| 2276 |
else |
| 2277 |
{ |
| 2278 |
ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 2279 |
|
| 2280 |
if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 2281 |
{ |
| 2282 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 2283 |
mbedtls_ssl_send_alert_message( |
| 2284 |
ssl, |
| 2285 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2286 |
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2287 |
return( ret ); |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
| 2292 |
ssl->handshake->resume ? "a" : "no" ) ); |
| 2293 |
|
| 2294 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) ); |
| 2295 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", |
| 2296 |
buf[37 + n] ) ); |
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
i = 0; |
| 2302 |
while( 1 ) |
| 2303 |
{ |
| 2304 |
if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) |
| 2305 |
{ |
| 2306 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2307 |
mbedtls_ssl_send_alert_message( |
| 2308 |
ssl, |
| 2309 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2310 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2311 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2312 |
} |
| 2313 |
|
| 2314 |
if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == |
| 2315 |
ssl->session_negotiate->ciphersuite ) |
| 2316 |
{ |
| 2317 |
break; |
| 2318 |
} |
| 2319 |
} |
| 2320 |
|
| 2321 |
suite_info = mbedtls_ssl_ciphersuite_from_id( |
| 2322 |
ssl->session_negotiate->ciphersuite ); |
| 2323 |
if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, |
| 2324 |
ssl->minor_ver ) != 0 ) |
| 2325 |
{ |
| 2326 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2327 |
mbedtls_ssl_send_alert_message( |
| 2328 |
ssl, |
| 2329 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2330 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2331 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2332 |
} |
| 2333 |
|
| 2334 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2335 |
( "server hello, chosen ciphersuite: %s", suite_info->name ) ); |
| 2336 |
|
| 2337 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2338 |
if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && |
| 2339 |
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 2340 |
{ |
| 2341 |
ssl->handshake->ecrs_enabled = 1; |
| 2342 |
} |
| 2343 |
#endif |
| 2344 |
|
| 2345 |
if( comp != MBEDTLS_SSL_COMPRESS_NULL |
| 2346 |
#if defined(MBEDTLS_ZLIB_SUPPORT) |
| 2347 |
&& comp != MBEDTLS_SSL_COMPRESS_DEFLATE |
| 2348 |
#endif |
| 2349 |
) |
| 2350 |
{ |
| 2351 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2352 |
mbedtls_ssl_send_alert_message( |
| 2353 |
ssl, |
| 2354 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2355 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2356 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2357 |
} |
| 2358 |
ssl->session_negotiate->compression = comp; |
| 2359 |
|
| 2360 |
ext = buf + 40 + n; |
| 2361 |
|
| 2362 |
MBEDTLS_SSL_DEBUG_MSG( 2, |
| 2363 |
( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) ); |
| 2364 |
|
| 2365 |
while( ext_len ) |
| 2366 |
{ |
| 2367 |
unsigned int ext_id = ( ( ext[0] << 8 ) |
| 2368 |
| ( ext[1] ) ); |
| 2369 |
unsigned int ext_size = ( ( ext[2] << 8 ) |
| 2370 |
| ( ext[3] ) ); |
| 2371 |
|
| 2372 |
if( ext_size + 4 > ext_len ) |
| 2373 |
{ |
| 2374 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2375 |
mbedtls_ssl_send_alert_message( |
| 2376 |
ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2377 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2378 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2379 |
} |
| 2380 |
|
| 2381 |
switch( ext_id ) |
| 2382 |
{ |
| 2383 |
case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 2384 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
| 2385 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2386 |
renegotiation_info_seen = 1; |
| 2387 |
#endif |
| 2388 |
|
| 2389 |
if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, |
| 2390 |
ext_size ) ) != 0 ) |
| 2391 |
return( ret ); |
| 2392 |
|
| 2393 |
break; |
| 2394 |
|
| 2395 |
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2396 |
case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 2397 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2398 |
( "found max_fragment_length extension" ) ); |
| 2399 |
|
| 2400 |
if( ( ret = ssl_parse_max_fragment_length_ext( ssl, |
| 2401 |
ext + 4, ext_size ) ) != 0 ) |
| 2402 |
{ |
| 2403 |
return( ret ); |
| 2404 |
} |
| 2405 |
|
| 2406 |
break; |
| 2407 |
#endif |
| 2408 |
|
| 2409 |
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2410 |
case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 2411 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) ); |
| 2412 |
|
| 2413 |
if( ( ret = ssl_parse_truncated_hmac_ext( ssl, |
| 2414 |
ext + 4, ext_size ) ) != 0 ) |
| 2415 |
{ |
| 2416 |
return( ret ); |
| 2417 |
} |
| 2418 |
|
| 2419 |
break; |
| 2420 |
#endif |
| 2421 |
|
| 2422 |
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2423 |
case MBEDTLS_TLS_EXT_CID: |
| 2424 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 2425 |
|
| 2426 |
if( ( ret = ssl_parse_cid_ext( ssl, |
| 2427 |
ext + 4, |
| 2428 |
ext_size ) ) != 0 ) |
| 2429 |
{ |
| 2430 |
return( ret ); |
| 2431 |
} |
| 2432 |
|
| 2433 |
break; |
| 2434 |
#endif |
| 2435 |
|
| 2436 |
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2437 |
case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 2438 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); |
| 2439 |
|
| 2440 |
if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, |
| 2441 |
ext + 4, ext_size ) ) != 0 ) |
| 2442 |
{ |
| 2443 |
return( ret ); |
| 2444 |
} |
| 2445 |
|
| 2446 |
break; |
| 2447 |
#endif |
| 2448 |
|
| 2449 |
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2450 |
case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 2451 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2452 |
( "found extended_master_secret extension" ) ); |
| 2453 |
|
| 2454 |
if( ( ret = ssl_parse_extended_ms_ext( ssl, |
| 2455 |
ext + 4, ext_size ) ) != 0 ) |
| 2456 |
{ |
| 2457 |
return( ret ); |
| 2458 |
} |
| 2459 |
|
| 2460 |
break; |
| 2461 |
#endif |
| 2462 |
|
| 2463 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2464 |
case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 2465 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); |
| 2466 |
|
| 2467 |
if( ( ret = ssl_parse_session_ticket_ext( ssl, |
| 2468 |
ext + 4, ext_size ) ) != 0 ) |
| 2469 |
{ |
| 2470 |
return( ret ); |
| 2471 |
} |
| 2472 |
|
| 2473 |
break; |
| 2474 |
#endif |
| 2475 |
|
| 2476 |
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 2477 |
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2478 |
case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 2479 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2480 |
( "found supported_point_formats extension" ) ); |
| 2481 |
|
| 2482 |
if( ( ret = ssl_parse_supported_point_formats_ext( ssl, |
| 2483 |
ext + 4, ext_size ) ) != 0 ) |
| 2484 |
{ |
| 2485 |
return( ret ); |
| 2486 |
} |
| 2487 |
|
| 2488 |
break; |
| 2489 |
#endif |
| 2490 |
|
| 2491 |
|
| 2492 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2493 |
case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 2494 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); |
| 2495 |
|
| 2496 |
if( ( ret = ssl_parse_ecjpake_kkpp( ssl, |
| 2497 |
ext + 4, ext_size ) ) != 0 ) |
| 2498 |
{ |
| 2499 |
return( ret ); |
| 2500 |
} |
| 2501 |
|
| 2502 |
break; |
| 2503 |
#endif |
| 2504 |
|
| 2505 |
#if defined(MBEDTLS_SSL_ALPN) |
| 2506 |
case MBEDTLS_TLS_EXT_ALPN: |
| 2507 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
| 2508 |
|
| 2509 |
if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 2510 |
return( ret ); |
| 2511 |
|
| 2512 |
break; |
| 2513 |
#endif |
| 2514 |
|
| 2515 |
#if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 2516 |
case MBEDTLS_TLS_EXT_USE_SRTP: |
| 2517 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) ); |
| 2518 |
|
| 2519 |
if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 2520 |
return( ret ); |
| 2521 |
|
| 2522 |
break; |
| 2523 |
#endif |
| 2524 |
|
| 2525 |
default: |
| 2526 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2527 |
( "unknown extension found: %u (ignoring)", ext_id ) ); |
| 2528 |
} |
| 2529 |
|
| 2530 |
ext_len -= 4 + ext_size; |
| 2531 |
ext += 4 + ext_size; |
| 2532 |
|
| 2533 |
if( ext_len > 0 && ext_len < 4 ) |
| 2534 |
{ |
| 2535 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2536 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 2542 |
|
| 2543 |
if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 2544 |
ssl->conf->allow_legacy_renegotiation == |
| 2545 |
MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
| 2546 |
{ |
| 2547 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2548 |
( "legacy renegotiation, breaking off handshake" ) ); |
| 2549 |
handshake_failure = 1; |
| 2550 |
} |
| 2551 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2552 |
else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2553 |
ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
| 2554 |
renegotiation_info_seen == 0 ) |
| 2555 |
{ |
| 2556 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2557 |
( "renegotiation_info extension missing (secure)" ) ); |
| 2558 |
handshake_failure = 1; |
| 2559 |
} |
| 2560 |
else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2561 |
ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 2562 |
ssl->conf->allow_legacy_renegotiation == |
| 2563 |
MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
| 2564 |
{ |
| 2565 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
| 2566 |
handshake_failure = 1; |
| 2567 |
} |
| 2568 |
else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2569 |
ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 2570 |
renegotiation_info_seen == 1 ) |
| 2571 |
{ |
| 2572 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2573 |
( "renegotiation_info extension present (legacy)" ) ); |
| 2574 |
handshake_failure = 1; |
| 2575 |
} |
| 2576 |
#endif |
| 2577 |
|
| 2578 |
if( handshake_failure == 1 ) |
| 2579 |
{ |
| 2580 |
mbedtls_ssl_send_alert_message( |
| 2581 |
ssl, |
| 2582 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2583 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 2584 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 2585 |
} |
| 2586 |
|
| 2587 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
| 2588 |
|
| 2589 |
return( 0 ); |
| 2590 |
} |
| 2591 |
|
| 2592 |
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2593 |
defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2594 |
static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, |
| 2595 |
unsigned char **p, |
| 2596 |
unsigned char *end ) |
| 2597 |
{ |
| 2598 |
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2599 |
size_t dhm_actual_bitlen; |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, |
| 2611 |
p, end ) ) != 0 ) |
| 2612 |
{ |
| 2613 |
MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); |
| 2614 |
return( ret ); |
| 2615 |
} |
| 2616 |
|
| 2617 |
dhm_actual_bitlen = mbedtls_mpi_bitlen( &ssl->handshake->dhm_ctx.P ); |
| 2618 |
if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen ) |
| 2619 |
{ |
| 2620 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u", |
| 2621 |
dhm_actual_bitlen, |
| 2622 |
ssl->conf->dhm_min_bitlen ) ); |
| 2623 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2624 |
} |
| 2625 |
|
| 2626 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 2627 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 2628 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
| 2629 |
|
| 2630 |
return( ret ); |
| 2631 |
} |
| 2632 |
#endif |
| 2633 |
|
| 2634 |
|
| 2635 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2636 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2637 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 2638 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2639 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2640 |
static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl ) |
| 2641 |
{ |
| 2642 |
const mbedtls_ecp_curve_info *curve_info; |
| 2643 |
mbedtls_ecp_group_id grp_id; |
| 2644 |
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 2645 |
grp_id = ssl->handshake->ecdh_ctx.grp.id; |
| 2646 |
#else |
| 2647 |
grp_id = ssl->handshake->ecdh_ctx.grp_id; |
| 2648 |
#endif |
| 2649 |
|
| 2650 |
curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 2651 |
if( curve_info == NULL ) |
| 2652 |
{ |
| 2653 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2654 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2655 |
} |
| 2656 |
|
| 2657 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); |
| 2658 |
|
| 2659 |
#if defined(MBEDTLS_ECP_C) |
| 2660 |
if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) |
| 2661 |
#else |
| 2662 |
if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || |
| 2663 |
ssl->handshake->ecdh_ctx.grp.nbits > 521 ) |
| 2664 |
#endif |
| 2665 |
return( -1 ); |
| 2666 |
|
| 2667 |
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2668 |
MBEDTLS_DEBUG_ECDH_QP ); |
| 2669 |
|
| 2670 |
return( 0 ); |
| 2671 |
} |
| 2672 |
#endif |
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 2679 |
( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2680 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 2681 |
static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, |
| 2682 |
unsigned char **p, |
| 2683 |
unsigned char *end ) |
| 2684 |
{ |
| 2685 |
uint16_t tls_id; |
| 2686 |
size_t ecdh_bits = 0; |
| 2687 |
uint8_t ecpoint_len; |
| 2688 |
mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
if( end - *p < 4 ) |
| 2695 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2696 |
|
| 2697 |
|
| 2698 |
if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) |
| 2699 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2700 |
|
| 2701 |
|
| 2702 |
tls_id = *(*p)++; |
| 2703 |
tls_id <<= 8; |
| 2704 |
tls_id |= *(*p)++; |
| 2705 |
|
| 2706 |
|
| 2707 |
if( ( handshake->ecdh_psa_type = |
| 2708 |
mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) |
| 2709 |
{ |
| 2710 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2711 |
} |
| 2712 |
if( ecdh_bits > 0xffff ) |
| 2713 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2714 |
handshake->ecdh_bits = (uint16_t) ecdh_bits; |
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
ecpoint_len = *(*p)++; |
| 2721 |
if( (size_t)( end - *p ) < ecpoint_len ) |
| 2722 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2723 |
|
| 2724 |
if( mbedtls_psa_tls_ecpoint_to_psa_ec( |
| 2725 |
*p, ecpoint_len, |
| 2726 |
handshake->ecdh_psa_peerkey, |
| 2727 |
sizeof( handshake->ecdh_psa_peerkey ), |
| 2728 |
&handshake->ecdh_psa_peerkey_len ) != 0 ) |
| 2729 |
{ |
| 2730 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2731 |
} |
| 2732 |
|
| 2733 |
*p += ecpoint_len; |
| 2734 |
return( 0 ); |
| 2735 |
} |
| 2736 |
#endif |
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2741 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2742 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2743 |
static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, |
| 2744 |
unsigned char **p, |
| 2745 |
unsigned char *end ) |
| 2746 |
{ |
| 2747 |
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, |
| 2758 |
(const unsigned char **) p, end ) ) != 0 ) |
| 2759 |
{ |
| 2760 |
MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); |
| 2761 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2762 |
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2763 |
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2764 |
#endif |
| 2765 |
return( ret ); |
| 2766 |
} |
| 2767 |
|
| 2768 |
if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
| 2769 |
{ |
| 2770 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2771 |
( "bad server key exchange message (ECDHE curve)" ) ); |
| 2772 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2773 |
} |
| 2774 |
|
| 2775 |
return( ret ); |
| 2776 |
} |
| 2777 |
#endif |
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 2782 |
static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, |
| 2783 |
unsigned char **p, |
| 2784 |
unsigned char *end ) |
| 2785 |
{ |
| 2786 |
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2787 |
uint16_t len; |
| 2788 |
((void) ssl); |
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
if( end - (*p) < 2 ) |
| 2796 |
{ |
| 2797 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2798 |
( "bad server key exchange message (psk_identity_hint length)" ) ); |
| 2799 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2800 |
} |
| 2801 |
len = (*p)[0] << 8 | (*p)[1]; |
| 2802 |
*p += 2; |
| 2803 |
|
| 2804 |
if( end - (*p) < len ) |
| 2805 |
{ |
| 2806 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2807 |
( "bad server key exchange message (psk_identity_hint length)" ) ); |
| 2808 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2809 |
} |
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
*p += len; |
| 2817 |
ret = 0; |
| 2818 |
|
| 2819 |
return( ret ); |
| 2820 |
} |
| 2821 |
#endif |
| 2822 |
|
| 2823 |
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 2824 |
defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, |
| 2829 |
size_t offset, size_t *olen, |
| 2830 |
size_t pms_offset ) |
| 2831 |
{ |
| 2832 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2833 |
size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2; |
| 2834 |
unsigned char *p = ssl->handshake->premaster + pms_offset; |
| 2835 |
mbedtls_pk_context * peer_pk; |
| 2836 |
|
| 2837 |
if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2838 |
{ |
| 2839 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); |
| 2840 |
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2841 |
} |
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 2851 |
ssl->conf->max_minor_ver, |
| 2852 |
ssl->conf->transport, p ); |
| 2853 |
|
| 2854 |
if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 ) |
| 2855 |
{ |
| 2856 |
MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
| 2857 |
return( ret ); |
| 2858 |
} |
| 2859 |
|
| 2860 |
ssl->handshake->pmslen = 48; |
| 2861 |
|
| 2862 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2863 |
peer_pk = &ssl->handshake->peer_pubkey; |
| 2864 |
#else |
| 2865 |
if( ssl->session_negotiate->peer_cert == NULL ) |
| 2866 |
{ |
| 2867 |
|
| 2868 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2869 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2870 |
} |
| 2871 |
peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2872 |
#endif |
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) |
| 2878 |
{ |
| 2879 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); |
| 2880 |
return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
| 2881 |
} |
| 2882 |
|
| 2883 |
if( ( ret = mbedtls_pk_encrypt( peer_pk, |
| 2884 |
p, ssl->handshake->pmslen, |
| 2885 |
ssl->out_msg + offset + len_bytes, olen, |
| 2886 |
MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, |
| 2887 |
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 2888 |
{ |
| 2889 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); |
| 2890 |
return( ret ); |
| 2891 |
} |
| 2892 |
|
| 2893 |
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2894 |
defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2895 |
if( len_bytes == 2 ) |
| 2896 |
{ |
| 2897 |
MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset ); |
| 2898 |
*olen += 2; |
| 2899 |
} |
| 2900 |
#endif |
| 2901 |
|
| 2902 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2903 |
|
| 2904 |
mbedtls_pk_free( peer_pk ); |
| 2905 |
#endif |
| 2906 |
return( 0 ); |
| 2907 |
} |
| 2908 |
#endif |
| 2909 |
|
| 2910 |
|
| 2911 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2912 |
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2913 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2914 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 2915 |
static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl, |
| 2916 |
unsigned char **p, |
| 2917 |
unsigned char *end, |
| 2918 |
mbedtls_md_type_t *md_alg, |
| 2919 |
mbedtls_pk_type_t *pk_alg ) |
| 2920 |
{ |
| 2921 |
((void) ssl); |
| 2922 |
*md_alg = MBEDTLS_MD_NONE; |
| 2923 |
*pk_alg = MBEDTLS_PK_NONE; |
| 2924 |
|
| 2925 |
|
| 2926 |
if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 2927 |
{ |
| 2928 |
return( 0 ); |
| 2929 |
} |
| 2930 |
|
| 2931 |
if( (*p) + 2 > end ) |
| 2932 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) |
| 2938 |
== MBEDTLS_MD_NONE ) |
| 2939 |
{ |
| 2940 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2941 |
( "Server used unsupported HashAlgorithm %d", *(p)[0] ) ); |
| 2942 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2943 |
} |
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) |
| 2949 |
== MBEDTLS_PK_NONE ) |
| 2950 |
{ |
| 2951 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2952 |
( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) ); |
| 2953 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2954 |
} |
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 ) |
| 2960 |
{ |
| 2961 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2962 |
( "server used HashAlgorithm %d that was not offered", *(p)[0] ) ); |
| 2963 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2964 |
} |
| 2965 |
|
| 2966 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", |
| 2967 |
(*p)[1] ) ); |
| 2968 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", |
| 2969 |
(*p)[0] ) ); |
| 2970 |
*p += 2; |
| 2971 |
|
| 2972 |
return( 0 ); |
| 2973 |
} |
| 2974 |
#endif |
| 2975 |
|
| 2976 |
|
| 2977 |
#endif |
| 2978 |
|
| 2979 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2980 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2981 |
static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
| 2982 |
{ |
| 2983 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2984 |
const mbedtls_ecp_keypair *peer_key; |
| 2985 |
mbedtls_pk_context * peer_pk; |
| 2986 |
|
| 2987 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2988 |
peer_pk = &ssl->handshake->peer_pubkey; |
| 2989 |
#else |
| 2990 |
if( ssl->session_negotiate->peer_cert == NULL ) |
| 2991 |
{ |
| 2992 |
|
| 2993 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2994 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2995 |
} |
| 2996 |
peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2997 |
#endif |
| 2998 |
|
| 2999 |
if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) |
| 3000 |
{ |
| 3001 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3002 |
return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
| 3003 |
} |
| 3004 |
|
| 3005 |
peer_key = mbedtls_pk_ec( *peer_pk ); |
| 3006 |
|
| 3007 |
if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, |
| 3008 |
MBEDTLS_ECDH_THEIRS ) ) != 0 ) |
| 3009 |
{ |
| 3010 |
MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
| 3011 |
return( ret ); |
| 3012 |
} |
| 3013 |
|
| 3014 |
if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
| 3015 |
{ |
| 3016 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); |
| 3017 |
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 3018 |
} |
| 3019 |
|
| 3020 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3021 |
|
| 3022 |
|
| 3023 |
|
| 3024 |
mbedtls_pk_free( peer_pk ); |
| 3025 |
#endif |
| 3026 |
|
| 3027 |
return( ret ); |
| 3028 |
} |
| 3029 |
#endif |
| 3030 |
|
| 3031 |
|
| 3032 |
static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) |
| 3033 |
{ |
| 3034 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3035 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3036 |
ssl->handshake->ciphersuite_info; |
| 3037 |
unsigned char *p = NULL, *end = NULL; |
| 3038 |
|
| 3039 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); |
| 3040 |
|
| 3041 |
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 3042 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
| 3043 |
{ |
| 3044 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
| 3045 |
ssl->state++; |
| 3046 |
return( 0 ); |
| 3047 |
} |
| 3048 |
((void) p); |
| 3049 |
((void) end); |
| 3050 |
#endif |
| 3051 |
|
| 3052 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3053 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3054 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 3055 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
| 3056 |
{ |
| 3057 |
if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) |
| 3058 |
{ |
| 3059 |
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); |
| 3060 |
mbedtls_ssl_send_alert_message( |
| 3061 |
ssl, |
| 3062 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3063 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 3064 |
return( ret ); |
| 3065 |
} |
| 3066 |
|
| 3067 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
| 3068 |
ssl->state++; |
| 3069 |
return( 0 ); |
| 3070 |
} |
| 3071 |
((void) p); |
| 3072 |
((void) end); |
| 3073 |
#endif |
| 3074 |
|
| 3075 |
|
| 3076 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3077 |
if( ssl->handshake->ecrs_enabled && |
| 3078 |
ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) |
| 3079 |
{ |
| 3080 |
goto start_processing; |
| 3081 |
} |
| 3082 |
#endif |
| 3083 |
|
| 3084 |
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 3085 |
{ |
| 3086 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3087 |
return( ret ); |
| 3088 |
} |
| 3089 |
|
| 3090 |
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3091 |
{ |
| 3092 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3093 |
mbedtls_ssl_send_alert_message( |
| 3094 |
ssl, |
| 3095 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3096 |
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 3097 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3098 |
} |
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) |
| 3105 |
{ |
| 3106 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3107 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 3108 |
{ |
| 3109 |
|
| 3110 |
|
| 3111 |
ssl->keep_current_message = 1; |
| 3112 |
goto exit; |
| 3113 |
} |
| 3114 |
|
| 3115 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3116 |
( "server key exchange message must not be skipped" ) ); |
| 3117 |
mbedtls_ssl_send_alert_message( |
| 3118 |
ssl, |
| 3119 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3120 |
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 3121 |
|
| 3122 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3123 |
} |
| 3124 |
|
| 3125 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3126 |
if( ssl->handshake->ecrs_enabled ) |
| 3127 |
ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; |
| 3128 |
|
| 3129 |
start_processing: |
| 3130 |
#endif |
| 3131 |
p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
| 3132 |
end = ssl->in_msg + ssl->in_hslen; |
| 3133 |
MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p ); |
| 3134 |
|
| 3135 |
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 3136 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3137 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 3138 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3139 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 3140 |
{ |
| 3141 |
if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) |
| 3142 |
{ |
| 3143 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3144 |
mbedtls_ssl_send_alert_message( |
| 3145 |
ssl, |
| 3146 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3147 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3148 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3149 |
} |
| 3150 |
} |
| 3151 |
#endif |
| 3152 |
|
| 3153 |
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ |
| 3154 |
defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3155 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3156 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 3157 |
; |
| 3158 |
else |
| 3159 |
#endif |
| 3160 |
|
| 3161 |
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 3162 |
defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3163 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || |
| 3164 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 3165 |
{ |
| 3166 |
if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) |
| 3167 |
{ |
| 3168 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3169 |
mbedtls_ssl_send_alert_message( |
| 3170 |
ssl, |
| 3171 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3172 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3173 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3174 |
} |
| 3175 |
} |
| 3176 |
else |
| 3177 |
#endif |
| 3178 |
|
| 3179 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3180 |
( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3181 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 3182 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3183 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 3184 |
{ |
| 3185 |
if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 ) |
| 3186 |
{ |
| 3187 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3188 |
mbedtls_ssl_send_alert_message( |
| 3189 |
ssl, |
| 3190 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3191 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3192 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3193 |
} |
| 3194 |
} |
| 3195 |
else |
| 3196 |
#endif |
| 3197 |
|
| 3198 |
|
| 3199 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3200 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 3201 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 3202 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3203 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 3204 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 3205 |
{ |
| 3206 |
if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) |
| 3207 |
{ |
| 3208 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3209 |
mbedtls_ssl_send_alert_message( |
| 3210 |
ssl, |
| 3211 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3212 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3213 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3214 |
} |
| 3215 |
} |
| 3216 |
else |
| 3217 |
#endif |
| 3218 |
|
| 3219 |
|
| 3220 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 3221 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 3222 |
{ |
| 3223 |
ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 3224 |
p, end - p ); |
| 3225 |
if( ret != 0 ) |
| 3226 |
{ |
| 3227 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
| 3228 |
mbedtls_ssl_send_alert_message( |
| 3229 |
ssl, |
| 3230 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3231 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3232 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3233 |
} |
| 3234 |
} |
| 3235 |
else |
| 3236 |
#endif |
| 3237 |
{ |
| 3238 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3239 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3240 |
} |
| 3241 |
|
| 3242 |
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) |
| 3243 |
if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
| 3244 |
{ |
| 3245 |
size_t sig_len, hashlen; |
| 3246 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3247 |
unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3248 |
#else |
| 3249 |
unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 3250 |
#endif |
| 3251 |
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
| 3252 |
mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 3253 |
unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
| 3254 |
size_t params_len = p - params; |
| 3255 |
void *rs_ctx = NULL; |
| 3256 |
|
| 3257 |
mbedtls_pk_context * peer_pk; |
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3263 |
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 3264 |
{ |
| 3265 |
if( ssl_parse_signature_algorithm( ssl, &p, end, |
| 3266 |
&md_alg, &pk_alg ) != 0 ) |
| 3267 |
{ |
| 3268 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3269 |
( "bad server key exchange message" ) ); |
| 3270 |
mbedtls_ssl_send_alert_message( |
| 3271 |
ssl, |
| 3272 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3273 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3274 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3275 |
} |
| 3276 |
|
| 3277 |
if( pk_alg != |
| 3278 |
mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) ) |
| 3279 |
{ |
| 3280 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3281 |
( "bad server key exchange message" ) ); |
| 3282 |
mbedtls_ssl_send_alert_message( |
| 3283 |
ssl, |
| 3284 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3285 |
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 3286 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3287 |
} |
| 3288 |
} |
| 3289 |
else |
| 3290 |
#endif |
| 3291 |
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3292 |
defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3293 |
if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 3294 |
{ |
| 3295 |
pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
| 3296 |
|
| 3297 |
|
| 3298 |
if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE ) |
| 3299 |
md_alg = MBEDTLS_MD_SHA1; |
| 3300 |
} |
| 3301 |
else |
| 3302 |
#endif |
| 3303 |
{ |
| 3304 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3305 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3306 |
} |
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
|
| 3312 |
if( p > end - 2 ) |
| 3313 |
{ |
| 3314 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3315 |
mbedtls_ssl_send_alert_message( |
| 3316 |
ssl, |
| 3317 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3318 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3319 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3320 |
} |
| 3321 |
sig_len = ( p[0] << 8 ) | p[1]; |
| 3322 |
p += 2; |
| 3323 |
|
| 3324 |
if( p != end - sig_len ) |
| 3325 |
{ |
| 3326 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3327 |
mbedtls_ssl_send_alert_message( |
| 3328 |
ssl, |
| 3329 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3330 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3331 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3332 |
} |
| 3333 |
|
| 3334 |
MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); |
| 3335 |
|
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3340 |
defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3341 |
if( md_alg == MBEDTLS_MD_NONE ) |
| 3342 |
{ |
| 3343 |
hashlen = 36; |
| 3344 |
ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params, |
| 3345 |
params_len ); |
| 3346 |
if( ret != 0 ) |
| 3347 |
return( ret ); |
| 3348 |
} |
| 3349 |
else |
| 3350 |
#endif |
| 3351 |
|
| 3352 |
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3353 |
defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3354 |
if( md_alg != MBEDTLS_MD_NONE ) |
| 3355 |
{ |
| 3356 |
ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, |
| 3357 |
params, params_len, |
| 3358 |
md_alg ); |
| 3359 |
if( ret != 0 ) |
| 3360 |
return( ret ); |
| 3361 |
} |
| 3362 |
else |
| 3363 |
#endif |
| 3364 |
|
| 3365 |
{ |
| 3366 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3367 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3368 |
} |
| 3369 |
|
| 3370 |
MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
| 3371 |
|
| 3372 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3373 |
peer_pk = &ssl->handshake->peer_pubkey; |
| 3374 |
#else |
| 3375 |
if( ssl->session_negotiate->peer_cert == NULL ) |
| 3376 |
{ |
| 3377 |
|
| 3378 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3379 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3380 |
} |
| 3381 |
peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 3382 |
#endif |
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
|
| 3387 |
if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
| 3388 |
{ |
| 3389 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 3390 |
mbedtls_ssl_send_alert_message( |
| 3391 |
ssl, |
| 3392 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3393 |
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 3394 |
return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
| 3395 |
} |
| 3396 |
|
| 3397 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3398 |
if( ssl->handshake->ecrs_enabled ) |
| 3399 |
rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
| 3400 |
#endif |
| 3401 |
|
| 3402 |
if( ( ret = mbedtls_pk_verify_restartable( peer_pk, |
| 3403 |
md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 ) |
| 3404 |
{ |
| 3405 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3406 |
if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3407 |
#endif |
| 3408 |
mbedtls_ssl_send_alert_message( |
| 3409 |
ssl, |
| 3410 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3411 |
MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 3412 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
| 3413 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3414 |
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3415 |
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3416 |
#endif |
| 3417 |
return( ret ); |
| 3418 |
} |
| 3419 |
|
| 3420 |
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3421 |
|
| 3422 |
|
| 3423 |
|
| 3424 |
mbedtls_pk_free( peer_pk ); |
| 3425 |
#endif |
| 3426 |
} |
| 3427 |
#endif |
| 3428 |
|
| 3429 |
exit: |
| 3430 |
ssl->state++; |
| 3431 |
|
| 3432 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
| 3433 |
|
| 3434 |
return( 0 ); |
| 3435 |
} |
| 3436 |
|
| 3437 |
#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 3438 |
static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
| 3439 |
{ |
| 3440 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3441 |
ssl->handshake->ciphersuite_info; |
| 3442 |
|
| 3443 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
| 3444 |
|
| 3445 |
if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
| 3446 |
{ |
| 3447 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
| 3448 |
ssl->state++; |
| 3449 |
return( 0 ); |
| 3450 |
} |
| 3451 |
|
| 3452 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3453 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3454 |
} |
| 3455 |
#else |
| 3456 |
static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
| 3457 |
{ |
| 3458 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3459 |
unsigned char *buf; |
| 3460 |
size_t n = 0; |
| 3461 |
size_t cert_type_len = 0, dn_len = 0; |
| 3462 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3463 |
ssl->handshake->ciphersuite_info; |
| 3464 |
|
| 3465 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
| 3466 |
|
| 3467 |
if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
| 3468 |
{ |
| 3469 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
| 3470 |
ssl->state++; |
| 3471 |
return( 0 ); |
| 3472 |
} |
| 3473 |
|
| 3474 |
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 3475 |
{ |
| 3476 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3477 |
return( ret ); |
| 3478 |
} |
| 3479 |
|
| 3480 |
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3481 |
{ |
| 3482 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3483 |
mbedtls_ssl_send_alert_message( |
| 3484 |
ssl, |
| 3485 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3486 |
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 3487 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3488 |
} |
| 3489 |
|
| 3490 |
ssl->state++; |
| 3491 |
ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); |
| 3492 |
|
| 3493 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
| 3494 |
ssl->client_auth ? "a" : "no" ) ); |
| 3495 |
|
| 3496 |
if( ssl->client_auth == 0 ) |
| 3497 |
{ |
| 3498 |
|
| 3499 |
ssl->keep_current_message = 1; |
| 3500 |
goto exit; |
| 3501 |
} |
| 3502 |
|
| 3503 |
|
| 3504 |
|
| 3505 |
|
| 3506 |
|
| 3507 |
|
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
|
| 3512 |
|
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
|
| 3522 |
|
| 3523 |
|
| 3524 |
|
| 3525 |
|
| 3526 |
|
| 3527 |
buf = ssl->in_msg; |
| 3528 |
|
| 3529 |
|
| 3530 |
if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 3531 |
{ |
| 3532 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3533 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3534 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3535 |
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3536 |
} |
| 3537 |
cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; |
| 3538 |
n = cert_type_len; |
| 3539 |
|
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
|
| 3545 |
|
| 3546 |
|
| 3547 |
|
| 3548 |
|
| 3549 |
|
| 3550 |
if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) |
| 3551 |
{ |
| 3552 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3553 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3554 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3555 |
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3556 |
} |
| 3557 |
|
| 3558 |
|
| 3559 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3560 |
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 3561 |
{ |
| 3562 |
size_t sig_alg_len = |
| 3563 |
( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 3564 |
| ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
| 3565 |
#if defined(MBEDTLS_DEBUG_C) |
| 3566 |
unsigned char* sig_alg; |
| 3567 |
size_t i; |
| 3568 |
#endif |
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
|
| 3581 |
|
| 3582 |
if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) |
| 3583 |
+ 3 + n + sig_alg_len ) |
| 3584 |
{ |
| 3585 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3586 |
mbedtls_ssl_send_alert_message( |
| 3587 |
ssl, |
| 3588 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3589 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3590 |
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3591 |
} |
| 3592 |
|
| 3593 |
#if defined(MBEDTLS_DEBUG_C) |
| 3594 |
sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; |
| 3595 |
for( i = 0; i < sig_alg_len; i += 2 ) |
| 3596 |
{ |
| 3597 |
MBEDTLS_SSL_DEBUG_MSG( 3, |
| 3598 |
( "Supported Signature Algorithm found: %d,%d", |
| 3599 |
sig_alg[i], sig_alg[i + 1] ) ); |
| 3600 |
} |
| 3601 |
#endif |
| 3602 |
|
| 3603 |
n += 2 + sig_alg_len; |
| 3604 |
} |
| 3605 |
#endif |
| 3606 |
|
| 3607 |
|
| 3608 |
dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 3609 |
| ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
| 3610 |
|
| 3611 |
n += dn_len; |
| 3612 |
if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) |
| 3613 |
{ |
| 3614 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3615 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3616 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3617 |
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3618 |
} |
| 3619 |
|
| 3620 |
exit: |
| 3621 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
| 3622 |
|
| 3623 |
return( 0 ); |
| 3624 |
} |
| 3625 |
#endif |
| 3626 |
|
| 3627 |
static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) |
| 3628 |
{ |
| 3629 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3630 |
|
| 3631 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
| 3632 |
|
| 3633 |
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 3634 |
{ |
| 3635 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3636 |
return( ret ); |
| 3637 |
} |
| 3638 |
|
| 3639 |
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3640 |
{ |
| 3641 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
| 3642 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3643 |
} |
| 3644 |
|
| 3645 |
if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) || |
| 3646 |
ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) |
| 3647 |
{ |
| 3648 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
| 3649 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3650 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3651 |
return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); |
| 3652 |
} |
| 3653 |
|
| 3654 |
ssl->state++; |
| 3655 |
|
| 3656 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3657 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3658 |
mbedtls_ssl_recv_flight_completed( ssl ); |
| 3659 |
#endif |
| 3660 |
|
| 3661 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
| 3662 |
|
| 3663 |
return( 0 ); |
| 3664 |
} |
| 3665 |
|
| 3666 |
static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) |
| 3667 |
{ |
| 3668 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3669 |
|
| 3670 |
size_t header_len; |
| 3671 |
size_t content_len; |
| 3672 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3673 |
ssl->handshake->ciphersuite_info; |
| 3674 |
|
| 3675 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
| 3676 |
|
| 3677 |
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 3678 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
| 3679 |
{ |
| 3680 |
|
| 3681 |
|
| 3682 |
|
| 3683 |
content_len = ssl->handshake->dhm_ctx.len; |
| 3684 |
|
| 3685 |
MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 ); |
| 3686 |
header_len = 6; |
| 3687 |
|
| 3688 |
ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
| 3689 |
(int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3690 |
&ssl->out_msg[header_len], content_len, |
| 3691 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3692 |
if( ret != 0 ) |
| 3693 |
{ |
| 3694 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
| 3695 |
return( ret ); |
| 3696 |
} |
| 3697 |
|
| 3698 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3699 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
| 3700 |
|
| 3701 |
if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 3702 |
ssl->handshake->premaster, |
| 3703 |
MBEDTLS_PREMASTER_SIZE, |
| 3704 |
&ssl->handshake->pmslen, |
| 3705 |
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 3706 |
{ |
| 3707 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 3708 |
return( ret ); |
| 3709 |
} |
| 3710 |
|
| 3711 |
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 3712 |
} |
| 3713 |
else |
| 3714 |
#endif |
| 3715 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3716 |
( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3717 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 3718 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3719 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 3720 |
{ |
| 3721 |
psa_status_t status; |
| 3722 |
psa_key_attributes_t key_attributes; |
| 3723 |
|
| 3724 |
mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3725 |
|
| 3726 |
unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 3727 |
size_t own_pubkey_len; |
| 3728 |
unsigned char *own_pubkey_ecpoint; |
| 3729 |
size_t own_pubkey_ecpoint_len; |
| 3730 |
|
| 3731 |
header_len = 4; |
| 3732 |
|
| 3733 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); |
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
|
| 3738 |
|
| 3739 |
|
| 3740 |
|
| 3741 |
|
| 3742 |
|
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
key_attributes = psa_key_attributes_init(); |
| 3747 |
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 3748 |
psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); |
| 3749 |
psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); |
| 3750 |
psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); |
| 3751 |
|
| 3752 |
|
| 3753 |
status = psa_generate_key( &key_attributes, |
| 3754 |
&handshake->ecdh_psa_privkey ); |
| 3755 |
if( status != PSA_SUCCESS ) |
| 3756 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3757 |
|
| 3758 |
|
| 3759 |
|
| 3760 |
status = psa_export_public_key( handshake->ecdh_psa_privkey, |
| 3761 |
own_pubkey, sizeof( own_pubkey ), |
| 3762 |
&own_pubkey_len ); |
| 3763 |
if( status != PSA_SUCCESS ) |
| 3764 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3765 |
|
| 3766 |
if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey, |
| 3767 |
own_pubkey_len, |
| 3768 |
&own_pubkey_ecpoint, |
| 3769 |
&own_pubkey_ecpoint_len ) != 0 ) |
| 3770 |
{ |
| 3771 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3772 |
} |
| 3773 |
|
| 3774 |
|
| 3775 |
ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len; |
| 3776 |
memcpy( ssl->out_msg + header_len + 1, |
| 3777 |
own_pubkey_ecpoint, own_pubkey_ecpoint_len ); |
| 3778 |
content_len = own_pubkey_ecpoint_len + 1; |
| 3779 |
|
| 3780 |
|
| 3781 |
|
| 3782 |
|
| 3783 |
status = psa_raw_key_agreement( PSA_ALG_ECDH, |
| 3784 |
handshake->ecdh_psa_privkey, |
| 3785 |
handshake->ecdh_psa_peerkey, |
| 3786 |
handshake->ecdh_psa_peerkey_len, |
| 3787 |
ssl->handshake->premaster, |
| 3788 |
sizeof( ssl->handshake->premaster ), |
| 3789 |
&ssl->handshake->pmslen ); |
| 3790 |
if( status != PSA_SUCCESS ) |
| 3791 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3792 |
|
| 3793 |
status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 3794 |
if( status != PSA_SUCCESS ) |
| 3795 |
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3796 |
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
| 3797 |
} |
| 3798 |
else |
| 3799 |
#endif |
| 3800 |
|
| 3801 |
|
| 3802 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3803 |
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 3804 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3805 |
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3806 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3807 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3808 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 3809 |
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
| 3810 |
{ |
| 3811 |
|
| 3812 |
|
| 3813 |
|
| 3814 |
header_len = 4; |
| 3815 |
|
| 3816 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3817 |
if( ssl->handshake->ecrs_enabled ) |
| 3818 |
{ |
| 3819 |
if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret ) |
| 3820 |
goto ecdh_calc_secret; |
| 3821 |
|
| 3822 |
mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); |
| 3823 |
} |
| 3824 |
#endif |
| 3825 |
|
| 3826 |
ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
| 3827 |
&content_len, |
| 3828 |
&ssl->out_msg[header_len], 1000, |
| 3829 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3830 |
if( ret != 0 ) |
| 3831 |
{ |
| 3832 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
| 3833 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3834 |
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3835 |
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3836 |
#endif |
| 3837 |
return( ret ); |
| 3838 |
} |
| 3839 |
|
| 3840 |
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3841 |
MBEDTLS_DEBUG_ECDH_Q ); |
| 3842 |
|
| 3843 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3844 |
if( ssl->handshake->ecrs_enabled ) |
| 3845 |
{ |
| 3846 |
ssl->handshake->ecrs_n = content_len; |
| 3847 |
ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; |
| 3848 |
} |
| 3849 |
|
| 3850 |
ecdh_calc_secret: |
| 3851 |
if( ssl->handshake->ecrs_enabled ) |
| 3852 |
content_len = ssl->handshake->ecrs_n; |
| 3853 |
#endif |
| 3854 |
if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
| 3855 |
&ssl->handshake->pmslen, |
| 3856 |
ssl->handshake->premaster, |
| 3857 |
MBEDTLS_MPI_MAX_SIZE, |
| 3858 |
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 3859 |
{ |
| 3860 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 3861 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 3862 |
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3863 |
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3864 |
#endif |
| 3865 |
return( ret ); |
| 3866 |
} |
| 3867 |
|
| 3868 |
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3869 |
MBEDTLS_DEBUG_ECDH_Z ); |
| 3870 |
} |
| 3871 |
else |
| 3872 |
#endif |
| 3873 |
|
| 3874 |
|
| 3875 |
|
| 3876 |
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 3877 |
if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
| 3878 |
{ |
| 3879 |
|
| 3880 |
|
| 3881 |
|
| 3882 |
if( ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 3883 |
{ |
| 3884 |
|
| 3885 |
|
| 3886 |
|
| 3887 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3888 |
} |
| 3889 |
|
| 3890 |
header_len = 4; |
| 3891 |
content_len = ssl->conf->psk_identity_len; |
| 3892 |
|
| 3893 |
if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3894 |
{ |
| 3895 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3896 |
( "psk identity too long or SSL buffer too short" ) ); |
| 3897 |
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3898 |
} |
| 3899 |
|
| 3900 |
ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); |
| 3901 |
ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); |
| 3902 |
|
| 3903 |
memcpy( ssl->out_msg + header_len, |
| 3904 |
ssl->conf->psk_identity, |
| 3905 |
ssl->conf->psk_identity_len ); |
| 3906 |
header_len += ssl->conf->psk_identity_len; |
| 3907 |
|
| 3908 |
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3909 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 3910 |
{ |
| 3911 |
content_len = 0; |
| 3912 |
} |
| 3913 |
else |
| 3914 |
#endif |
| 3915 |
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3916 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 3917 |
{ |
| 3918 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3919 |
|
| 3920 |
if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
| 3921 |
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3922 |
#endif |
| 3923 |
|
| 3924 |
if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 3925 |
&content_len, 2 ) ) != 0 ) |
| 3926 |
return( ret ); |
| 3927 |
} |
| 3928 |
else |
| 3929 |
#endif |
| 3930 |
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3931 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 3932 |
{ |
| 3933 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3934 |
|
| 3935 |
if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
| 3936 |
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3937 |
#endif |
| 3938 |
|
| 3939 |
|
| 3940 |
|
| 3941 |
|
| 3942 |
content_len = ssl->handshake->dhm_ctx.len; |
| 3943 |
|
| 3944 |
if( header_len + 2 + content_len > |
| 3945 |
MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3946 |
{ |
| 3947 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3948 |
( "psk identity or DHM size too long or SSL buffer too short" ) ); |
| 3949 |
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3950 |
} |
| 3951 |
|
| 3952 |
ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); |
| 3953 |
ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); |
| 3954 |
|
| 3955 |
ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
| 3956 |
(int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3957 |
&ssl->out_msg[header_len], content_len, |
| 3958 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3959 |
if( ret != 0 ) |
| 3960 |
{ |
| 3961 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
| 3962 |
return( ret ); |
| 3963 |
} |
| 3964 |
} |
| 3965 |
else |
| 3966 |
#endif |
| 3967 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3968 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 3969 |
{ |
| 3970 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3971 |
|
| 3972 |
if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
| 3973 |
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3974 |
#endif |
| 3975 |
|
| 3976 |
|
| 3977 |
|
| 3978 |
|
| 3979 |
ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
| 3980 |
&content_len, |
| 3981 |
&ssl->out_msg[header_len], |
| 3982 |
MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
| 3983 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3984 |
if( ret != 0 ) |
| 3985 |
{ |
| 3986 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
| 3987 |
return( ret ); |
| 3988 |
} |
| 3989 |
|
| 3990 |
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3991 |
MBEDTLS_DEBUG_ECDH_Q ); |
| 3992 |
} |
| 3993 |
else |
| 3994 |
#endif |
| 3995 |
{ |
| 3996 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3997 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3998 |
} |
| 3999 |
|
| 4000 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 4001 |
defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 4002 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
| 4003 |
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 4004 |
ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
| 4005 |
{ |
| 4006 |
MBEDTLS_SSL_DEBUG_MSG( 1, |
| 4007 |
( "skip PMS generation for opaque PSK" ) ); |
| 4008 |
} |
| 4009 |
else |
| 4010 |
#endif |
| 4011 |
|
| 4012 |
if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
| 4013 |
ciphersuite_info->key_exchange ) ) != 0 ) |
| 4014 |
{ |
| 4015 |
MBEDTLS_SSL_DEBUG_RET( 1, |
| 4016 |
"mbedtls_ssl_psk_derive_premaster", ret ); |
| 4017 |
return( ret ); |
| 4018 |
} |
| 4019 |
} |
| 4020 |
else |
| 4021 |
#endif |
| 4022 |
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 4023 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
| 4024 |
{ |
| 4025 |
header_len = 4; |
| 4026 |
if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 4027 |
&content_len, 0 ) ) != 0 ) |
| 4028 |
return( ret ); |
| 4029 |
} |
| 4030 |
else |
| 4031 |
#endif |
| 4032 |
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 4033 |
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 4034 |
{ |
| 4035 |
header_len = 4; |
| 4036 |
|
| 4037 |
ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, |
| 4038 |
ssl->out_msg + header_len, |
| 4039 |
MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
| 4040 |
&content_len, |
| 4041 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 4042 |
if( ret != 0 ) |
| 4043 |
{ |
| 4044 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 4045 |
return( ret ); |
| 4046 |
} |
| 4047 |
|
| 4048 |
ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 4049 |
ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 4050 |
ssl->conf->f_rng, ssl->conf->p_rng ); |
| 4051 |
if( ret != 0 ) |
| 4052 |
{ |
| 4053 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 4054 |
return( ret ); |
| 4055 |
} |
| 4056 |
} |
| 4057 |
else |
| 4058 |
#endif |
| 4059 |
{ |
| 4060 |
((void) ciphersuite_info); |
| 4061 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4062 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4063 |
} |
| 4064 |
|
| 4065 |
ssl->out_msglen = header_len + content_len; |
| 4066 |
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4067 |
ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; |
| 4068 |
|
| 4069 |
ssl->state++; |
| 4070 |
|
| 4071 |
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 4072 |
{ |
| 4073 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 4074 |
return( ret ); |
| 4075 |
} |
| 4076 |
|
| 4077 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
| 4078 |
|
| 4079 |
return( 0 ); |
| 4080 |
} |
| 4081 |
|
| 4082 |
#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 4083 |
static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
| 4084 |
{ |
| 4085 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 4086 |
ssl->handshake->ciphersuite_info; |
| 4087 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4088 |
|
| 4089 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
| 4090 |
|
| 4091 |
if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 4092 |
{ |
| 4093 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 4094 |
return( ret ); |
| 4095 |
} |
| 4096 |
|
| 4097 |
if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
| 4098 |
{ |
| 4099 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 4100 |
ssl->state++; |
| 4101 |
return( 0 ); |
| 4102 |
} |
| 4103 |
|
| 4104 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4105 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4106 |
} |
| 4107 |
#else |
| 4108 |
static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
| 4109 |
{ |
| 4110 |
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4111 |
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 4112 |
ssl->handshake->ciphersuite_info; |
| 4113 |
size_t n = 0, offset = 0; |
| 4114 |
unsigned char hash[48]; |
| 4115 |
unsigned char *hash_start = hash; |
| 4116 |
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
| 4117 |
size_t hashlen; |
| 4118 |
void *rs_ctx = NULL; |
| 4119 |
|
| 4120 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
| 4121 |
|
| 4122 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 4123 |
if( ssl->handshake->ecrs_enabled && |
| 4124 |
ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) |
| 4125 |
{ |
| 4126 |
goto sign; |
| 4127 |
} |
| 4128 |
#endif |
| 4129 |
|
| 4130 |
if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 4131 |
{ |
| 4132 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 4133 |
return( ret ); |
| 4134 |
} |
| 4135 |
|
| 4136 |
if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
| 4137 |
{ |
| 4138 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 4139 |
ssl->state++; |
| 4140 |
return( 0 ); |
| 4141 |
} |
| 4142 |
|
| 4143 |
if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 4144 |
{ |
| 4145 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 4146 |
ssl->state++; |
| 4147 |
return( 0 ); |
| 4148 |
} |
| 4149 |
|
| 4150 |
if( mbedtls_ssl_own_key( ssl ) == NULL ) |
| 4151 |
{ |
| 4152 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) ); |
| 4153 |
return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 4154 |
} |
| 4155 |
|
| 4156 |
|
| 4157 |
|
| 4158 |
|
| 4159 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 4160 |
if( ssl->handshake->ecrs_enabled ) |
| 4161 |
ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; |
| 4162 |
|
| 4163 |
sign: |
| 4164 |
#endif |
| 4165 |
|
| 4166 |
ssl->handshake->calc_verify( ssl, hash, &hashlen ); |
| 4167 |
|
| 4168 |
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4169 |
defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4170 |
if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 4171 |
{ |
| 4172 |
|
| 4173 |
|
| 4174 |
|
| 4175 |
|
| 4176 |
|
| 4177 |
|
| 4178 |
|
| 4179 |
|
| 4180 |
|
| 4181 |
|
| 4182 |
|
| 4183 |
|
| 4184 |
md_alg = MBEDTLS_MD_NONE; |
| 4185 |
|
| 4186 |
|
| 4187 |
|
| 4188 |
|
| 4189 |
if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) ) |
| 4190 |
{ |
| 4191 |
hash_start += 16; |
| 4192 |
hashlen -= 16; |
| 4193 |
md_alg = MBEDTLS_MD_SHA1; |
| 4194 |
} |
| 4195 |
} |
| 4196 |
else |
| 4197 |
#endif |
| 4198 |
|
| 4199 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4200 |
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 4201 |
{ |
| 4202 |
|
| 4203 |
|
| 4204 |
|
| 4205 |
|
| 4206 |
|
| 4207 |
|
| 4208 |
|
| 4209 |
|
| 4210 |
|
| 4211 |
|
| 4212 |
|
| 4213 |
|
| 4214 |
|
| 4215 |
|
| 4216 |
|
| 4217 |
|
| 4218 |
if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 4219 |
{ |
| 4220 |
md_alg = MBEDTLS_MD_SHA384; |
| 4221 |
ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; |
| 4222 |
} |
| 4223 |
else |
| 4224 |
{ |
| 4225 |
md_alg = MBEDTLS_MD_SHA256; |
| 4226 |
ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; |
| 4227 |
} |
| 4228 |
ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) ); |
| 4229 |
|
| 4230 |
|
| 4231 |
hashlen = 0; |
| 4232 |
offset = 2; |
| 4233 |
} |
| 4234 |
else |
| 4235 |
#endif |
| 4236 |
{ |
| 4237 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4238 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4239 |
} |
| 4240 |
|
| 4241 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 4242 |
if( ssl->handshake->ecrs_enabled ) |
| 4243 |
rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
| 4244 |
#endif |
| 4245 |
|
| 4246 |
if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), |
| 4247 |
md_alg, hash_start, hashlen, |
| 4248 |
ssl->out_msg + 6 + offset, &n, |
| 4249 |
ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 ) |
| 4250 |
{ |
| 4251 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
| 4252 |
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 4253 |
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 4254 |
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 4255 |
#endif |
| 4256 |
return( ret ); |
| 4257 |
} |
| 4258 |
|
| 4259 |
MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 ); |
| 4260 |
|
| 4261 |
ssl->out_msglen = 6 + n + offset; |
| 4262 |
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4263 |
ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; |
| 4264 |
|
| 4265 |
ssl->state++; |
| 4266 |
|
| 4267 |
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 4268 |
{ |
| 4269 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 4270 |
return( ret ); |
| 4271 |
} |
| 4272 |
|
| 4273 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
| 4274 |
|
| 4275 |
return( ret ); |
| 4276 |
} |
| 4277 |
#endif |
| 4278 |
|
| 4279 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4280 |
static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl ) |
| 4281 |
{ |
| 4282 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4283 |
uint32_t lifetime; |
| 4284 |
size_t ticket_len; |
| 4285 |
unsigned char *ticket; |
| 4286 |
const unsigned char *msg; |
| 4287 |
|
| 4288 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); |
| 4289 |
|
| 4290 |
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 4291 |
{ |
| 4292 |
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 4293 |
return( ret ); |
| 4294 |
} |
| 4295 |
|
| 4296 |
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 4297 |
{ |
| 4298 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
| 4299 |
mbedtls_ssl_send_alert_message( |
| 4300 |
ssl, |
| 4301 |
MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4302 |
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4303 |
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 4304 |
} |
| 4305 |
|
| 4306 |
|
| 4307 |
|
| 4308 |
|
| 4309 |
|
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
|
| 4314 |
|
| 4315 |
|
| 4316 |
if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || |
| 4317 |
ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 4318 |
{ |
| 4319 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
| 4320 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4321 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 4322 |
return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
| 4323 |
} |
| 4324 |
|
| 4325 |
msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
| 4326 |
|
| 4327 |
lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | |
| 4328 |
( msg[2] << 8 ) | ( msg[3] ); |
| 4329 |
|
| 4330 |
ticket_len = ( msg[4] << 8 ) | ( msg[5] ); |
| 4331 |
|
| 4332 |
if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen ) |
| 4333 |
{ |
| 4334 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
| 4335 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4336 |
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 4337 |
return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
| 4338 |
} |
| 4339 |
|
| 4340 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) ); |
| 4341 |
|
| 4342 |
|
| 4343 |
ssl->handshake->new_session_ticket = 0; |
| 4344 |
ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 4345 |
|
| 4346 |
|
| 4347 |
|
| 4348 |
|
| 4349 |
|
| 4350 |
if( ticket_len == 0 ) |
| 4351 |
return( 0 ); |
| 4352 |
|
| 4353 |
if( ssl->session != NULL && ssl->session->ticket != NULL ) |
| 4354 |
{ |
| 4355 |
mbedtls_platform_zeroize( ssl->session->ticket, |
| 4356 |
ssl->session->ticket_len ); |
| 4357 |
mbedtls_free( ssl->session->ticket ); |
| 4358 |
ssl->session->ticket = NULL; |
| 4359 |
ssl->session->ticket_len = 0; |
| 4360 |
} |
| 4361 |
|
| 4362 |
mbedtls_platform_zeroize( ssl->session_negotiate->ticket, |
| 4363 |
ssl->session_negotiate->ticket_len ); |
| 4364 |
mbedtls_free( ssl->session_negotiate->ticket ); |
| 4365 |
ssl->session_negotiate->ticket = NULL; |
| 4366 |
ssl->session_negotiate->ticket_len = 0; |
| 4367 |
|
| 4368 |
if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) |
| 4369 |
{ |
| 4370 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); |
| 4371 |
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4372 |
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 4373 |
return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 4374 |
} |
| 4375 |
|
| 4376 |
memcpy( ticket, msg + 6, ticket_len ); |
| 4377 |
|
| 4378 |
ssl->session_negotiate->ticket = ticket; |
| 4379 |
ssl->session_negotiate->ticket_len = ticket_len; |
| 4380 |
ssl->session_negotiate->ticket_lifetime = lifetime; |
| 4381 |
|
| 4382 |
|
| 4383 |
|
| 4384 |
|
| 4385 |
|
| 4386 |
|
| 4387 |
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) ); |
| 4388 |
ssl->session_negotiate->id_len = 0; |
| 4389 |
|
| 4390 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); |
| 4391 |
|
| 4392 |
return( 0 ); |
| 4393 |
} |
| 4394 |
#endif |
| 4395 |
|
| 4396 |
|
| 4397 |
|
| 4398 |
|
| 4399 |
int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) |
| 4400 |
{ |
| 4401 |
int ret = 0; |
| 4402 |
|
| 4403 |
if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
| 4404 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4405 |
|
| 4406 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 4407 |
|
| 4408 |
if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4409 |
return( ret ); |
| 4410 |
|
| 4411 |
#if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4412 |
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4413 |
ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 4414 |
{ |
| 4415 |
if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 4416 |
return( ret ); |
| 4417 |
} |
| 4418 |
#endif |
| 4419 |
|
| 4420 |
|
| 4421 |
|
| 4422 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4423 |
if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && |
| 4424 |
ssl->handshake->new_session_ticket != 0 ) |
| 4425 |
{ |
| 4426 |
ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET; |
| 4427 |
} |
| 4428 |
#endif |
| 4429 |
|
| 4430 |
switch( ssl->state ) |
| 4431 |
{ |
| 4432 |
case MBEDTLS_SSL_HELLO_REQUEST: |
| 4433 |
ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 4434 |
break; |
| 4435 |
|
| 4436 |
|
| 4437 |
|
| 4438 |
|
| 4439 |
case MBEDTLS_SSL_CLIENT_HELLO: |
| 4440 |
ret = ssl_write_client_hello( ssl ); |
| 4441 |
break; |
| 4442 |
|
| 4443 |
|
| 4444 |
|
| 4445 |
|
| 4446 |
|
| 4447 |
|
| 4448 |
|
| 4449 |
|
| 4450 |
case MBEDTLS_SSL_SERVER_HELLO: |
| 4451 |
ret = ssl_parse_server_hello( ssl ); |
| 4452 |
break; |
| 4453 |
|
| 4454 |
case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4455 |
ret = mbedtls_ssl_parse_certificate( ssl ); |
| 4456 |
break; |
| 4457 |
|
| 4458 |
case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
| 4459 |
ret = ssl_parse_server_key_exchange( ssl ); |
| 4460 |
break; |
| 4461 |
|
| 4462 |
case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
| 4463 |
ret = ssl_parse_certificate_request( ssl ); |
| 4464 |
break; |
| 4465 |
|
| 4466 |
case MBEDTLS_SSL_SERVER_HELLO_DONE: |
| 4467 |
ret = ssl_parse_server_hello_done( ssl ); |
| 4468 |
break; |
| 4469 |
|
| 4470 |
|
| 4471 |
|
| 4472 |
|
| 4473 |
|
| 4474 |
|
| 4475 |
|
| 4476 |
|
| 4477 |
case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4478 |
ret = mbedtls_ssl_write_certificate( ssl ); |
| 4479 |
break; |
| 4480 |
|
| 4481 |
case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
| 4482 |
ret = ssl_write_client_key_exchange( ssl ); |
| 4483 |
break; |
| 4484 |
|
| 4485 |
case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
| 4486 |
ret = ssl_write_certificate_verify( ssl ); |
| 4487 |
break; |
| 4488 |
|
| 4489 |
case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4490 |
ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
| 4491 |
break; |
| 4492 |
|
| 4493 |
case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4494 |
ret = mbedtls_ssl_write_finished( ssl ); |
| 4495 |
break; |
| 4496 |
|
| 4497 |
|
| 4498 |
|
| 4499 |
|
| 4500 |
|
| 4501 |
|
| 4502 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4503 |
case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: |
| 4504 |
ret = ssl_parse_new_session_ticket( ssl ); |
| 4505 |
break; |
| 4506 |
#endif |
| 4507 |
|
| 4508 |
case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4509 |
ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
| 4510 |
break; |
| 4511 |
|
| 4512 |
case MBEDTLS_SSL_SERVER_FINISHED: |
| 4513 |
ret = mbedtls_ssl_parse_finished( ssl ); |
| 4514 |
break; |
| 4515 |
|
| 4516 |
case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4517 |
MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4518 |
ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 4519 |
break; |
| 4520 |
|
| 4521 |
case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 4522 |
mbedtls_ssl_handshake_wrapup( ssl ); |
| 4523 |
break; |
| 4524 |
|
| 4525 |
default: |
| 4526 |
MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4527 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4528 |
} |
| 4529 |
|
| 4530 |
return( ret ); |
| 4531 |
} |
| 4532 |
#endif |