| 1 | /* |
| 2 | * TLS shared functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | /* |
| 8 | * http://www.ietf.org/rfc/rfc2246.txt |
| 9 | * http://www.ietf.org/rfc/rfc4346.txt |
| 10 | */ |
| 11 | |
| 12 | #include "common.h" |
| 13 | |
| 14 | #if defined(MBEDTLS_SSL_TLS_C) |
| 15 | |
| 16 | #include "mbedtls/platform.h" |
| 17 | |
| 18 | #include "mbedtls/ssl.h" |
| 19 | #include "ssl_client.h" |
| 20 | #include "ssl_debug_helpers.h" |
| 21 | #include "ssl_misc.h" |
| 22 | #include "ssl_tls13_keys.h" |
| 23 | |
| 24 | #include "debug_internal.h" |
| 25 | #include "mbedtls/error.h" |
| 26 | #include "mbedtls/platform_util.h" |
| 27 | #include "mbedtls/version.h" |
| 28 | #include "mbedtls/constant_time.h" |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 33 | #include "mbedtls/psa_util.h" |
| 34 | #include "md_psa.h" |
| 35 | #include "psa_util_internal.h" |
| 36 | #include "psa/crypto.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 40 | #include "mbedtls/oid.h" |
| 41 | #endif |
| 42 | |
| 43 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 44 | /* Define local translating functions to save code size by not using too many |
| 45 | * arguments in each translating place. */ |
| 46 | static int local_err_translation(psa_status_t status) |
| 47 | { |
| 48 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
| 49 | ARRAY_LENGTH(psa_to_ssl_errors), |
| 50 | psa_generic_status_to_mbedtls); |
| 51 | } |
| 52 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
| 53 | #endif |
| 54 | |
| 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
| 59 | const uint8_t *cur, const uint8_t *end, size_t need) |
| 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
| 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void) |
| 67 | { |
| 68 | memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args)); |
| 69 | } |
| 70 | |
| 71 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args) |
| 72 | { |
| 73 | return (chk_buf_ptr_fail_args.cur != args->cur) || |
| 74 | (chk_buf_ptr_fail_args.end != args->end) || |
| 75 | (chk_buf_ptr_fail_args.need != args->need); |
| 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
| 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 80 | |
| 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 82 | /* Top-level Connection ID API */ |
| 83 | |
| 84 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid) |
| 87 | { |
| 88 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) { |
| 89 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 90 | } |
| 91 | |
| 92 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 93 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 94 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
| 98 | conf->cid_len = len; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len) |
| 106 | { |
| 107 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 108 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 109 | } |
| 110 | |
| 111 | ssl->negotiate_cid = enable; |
| 112 | if (enable == MBEDTLS_SSL_CID_DISABLED) { |
| 113 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); |
| 114 | return 0; |
| 115 | } |
| 116 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); |
| 117 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); |
| 118 | |
| 119 | if (own_cid_len != ssl->conf->cid_len) { |
| 120 | MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config", |
| 121 | (unsigned) own_cid_len, |
| 122 | (unsigned) ssl->conf->cid_len)); |
| 123 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 124 | } |
| 125 | |
| 126 | memcpy(ssl->own_cid, own_cid, own_cid_len); |
| 127 | /* Truncation is not an issue here because |
| 128 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 129 | ssl->own_cid_len = (uint8_t) own_cid_len; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl, |
| 135 | int *enabled, |
| 136 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX], |
| 137 | size_t *own_cid_len) |
| 138 | { |
| 139 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 140 | |
| 141 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 142 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 143 | } |
| 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
| 148 | if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | if (own_cid_len != NULL) { |
| 153 | *own_cid_len = ssl->own_cid_len; |
| 154 | if (own_cid != NULL) { |
| 155 | memcpy(own_cid, ssl->own_cid, ssl->own_cid_len); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, |
| 165 | int *enabled, |
| 166 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 167 | size_t *peer_cid_len) |
| 168 | { |
| 169 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 170 | |
| 171 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 172 | mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 173 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 174 | } |
| 175 | |
| 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
| 180 | if (ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0) { |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | if (peer_cid_len != NULL) { |
| 186 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 187 | if (peer_cid != NULL) { |
| 188 | memcpy(peer_cid, ssl->transform_in->out_cid, |
| 189 | ssl->transform_in->out_cid_len); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 198 | |
| 199 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 200 | |
| 201 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 202 | /* |
| 203 | * Convert max_fragment_length codes to length. |
| 204 | * RFC 6066 says: |
| 205 | * enum{ |
| 206 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 207 | * } MaxFragmentLength; |
| 208 | * and we add 0 -> extension unused |
| 209 | */ |
| 210 | static unsigned int ssl_mfl_code_to_length(int mfl) |
| 211 | { |
| 212 | switch (mfl) { |
| 213 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 214 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 215 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 216 | return 512; |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 218 | return 1024; |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 220 | return 2048; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 222 | return 4096; |
| 223 | default: |
| 224 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 225 | } |
| 226 | } |
| 227 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 228 | |
| 229 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, |
| 230 | const mbedtls_ssl_session *src) |
| 231 | { |
| 232 | mbedtls_ssl_session_free(dst); |
| 233 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); |
| 234 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 235 | dst->ticket = NULL; |
| 236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 237 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 238 | dst->hostname = NULL; |
| 239 | #endif |
| 240 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 241 | |
| 242 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 243 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 244 | dst->ticket_alpn = NULL; |
| 245 | #endif |
| 246 | |
| 247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 248 | |
| 249 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 250 | if (src->peer_cert != NULL) { |
| 251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 252 | |
| 253 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 254 | if (dst->peer_cert == NULL) { |
| 255 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 256 | } |
| 257 | |
| 258 | mbedtls_x509_crt_init(dst->peer_cert); |
| 259 | |
| 260 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p, |
| 261 | src->peer_cert->raw.len)) != 0) { |
| 262 | mbedtls_free(dst->peer_cert); |
| 263 | dst->peer_cert = NULL; |
| 264 | return ret; |
| 265 | } |
| 266 | } |
| 267 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 268 | if (src->peer_cert_digest != NULL) { |
| 269 | dst->peer_cert_digest = |
| 270 | mbedtls_calloc(1, src->peer_cert_digest_len); |
| 271 | if (dst->peer_cert_digest == NULL) { |
| 272 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 273 | } |
| 274 | |
| 275 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, |
| 276 | src->peer_cert_digest_len); |
| 277 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
| 278 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
| 279 | } |
| 280 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 281 | |
| 282 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 283 | |
| 284 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 285 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 286 | { |
| 287 | int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn); |
| 288 | if (ret != 0) { |
| 289 | return ret; |
| 290 | } |
| 291 | } |
| 292 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */ |
| 293 | |
| 294 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 295 | if (src->ticket != NULL) { |
| 296 | dst->ticket = mbedtls_calloc(1, src->ticket_len); |
| 297 | if (dst->ticket == NULL) { |
| 298 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 299 | } |
| 300 | |
| 301 | memcpy(dst->ticket, src->ticket, src->ticket_len); |
| 302 | } |
| 303 | |
| 304 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 305 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 306 | if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 307 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 308 | ret = mbedtls_ssl_session_set_hostname(dst, src->hostname); |
| 309 | if (ret != 0) { |
| 310 | return ret; |
| 311 | } |
| 312 | } |
| 313 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 314 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 315 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 321 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 322 | static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old) |
| 323 | { |
| 324 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); |
| 325 | if (resized_buffer == NULL) { |
| 326 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 327 | } |
| 328 | |
| 329 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 330 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 331 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 332 | * lost, are done outside of this function. */ |
| 333 | memcpy(resized_buffer, *buffer, |
| 334 | (len_new < *len_old) ? len_new : *len_old); |
| 335 | mbedtls_zeroize_and_free(*buffer, *len_old); |
| 336 | |
| 337 | *buffer = resized_buffer; |
| 338 | *len_old = len_new; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing, |
| 344 | size_t in_buf_new_len, |
| 345 | size_t out_buf_new_len) |
| 346 | { |
| 347 | int modified = 0; |
| 348 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0; |
| 349 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 350 | if (ssl->in_buf != NULL) { |
| 351 | written_in = ssl->in_msg - ssl->in_buf; |
| 352 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 353 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 354 | hdr_in = ssl->in_hdr - ssl->in_buf; |
| 355 | if (downsizing ? |
| 356 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 357 | ssl->in_buf_len < in_buf_new_len) { |
| 358 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { |
| 359 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); |
| 360 | } else { |
| 361 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 362 | in_buf_new_len)); |
| 363 | modified = 1; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (ssl->out_buf != NULL) { |
| 369 | written_out = ssl->out_msg - ssl->out_buf; |
| 370 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 371 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 372 | if (downsizing ? |
| 373 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 374 | ssl->out_buf_len < out_buf_new_len) { |
| 375 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { |
| 376 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); |
| 377 | } else { |
| 378 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 379 | out_buf_new_len)); |
| 380 | modified = 1; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | if (modified) { |
| 385 | /* Update pointers here to avoid doing it twice. */ |
| 386 | ssl->in_hdr = ssl->in_buf + hdr_in; |
| 387 | mbedtls_ssl_update_in_pointers(ssl); |
| 388 | mbedtls_ssl_reset_out_pointers(ssl); |
| 389 | |
| 390 | /* Fields below might not be properly updated with record |
| 391 | * splitting or with CID, so they are manually updated here. */ |
| 392 | ssl->out_msg = ssl->out_buf + written_out; |
| 393 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 394 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 395 | |
| 396 | ssl->in_msg = ssl->in_buf + written_in; |
| 397 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 398 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 399 | } |
| 400 | } |
| 401 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 402 | |
| 403 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 404 | |
| 405 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 406 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, |
| 407 | const char *label, |
| 408 | const unsigned char *random, size_t rlen, |
| 409 | unsigned char *dstbuf, size_t dlen); |
| 410 | |
| 411 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); |
| 412 | |
| 413 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 414 | |
| 415 | /* Type for the TLS PRF */ |
| 416 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 417 | const unsigned char *, size_t, |
| 418 | unsigned char *, size_t); |
| 419 | |
| 420 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 421 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 422 | int ciphersuite, |
| 423 | const unsigned char master[48], |
| 424 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 425 | int encrypt_then_mac, |
| 426 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 427 | ssl_tls_prf_t tls_prf, |
| 428 | const unsigned char randbytes[64], |
| 429 | mbedtls_ssl_protocol_version tls_version, |
| 430 | unsigned endpoint, |
| 431 | const mbedtls_ssl_context *ssl); |
| 432 | |
| 433 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 434 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 435 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 436 | const char *label, |
| 437 | const unsigned char *random, size_t rlen, |
| 438 | unsigned char *dstbuf, size_t dlen); |
| 439 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 440 | static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int); |
| 441 | |
| 442 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 443 | |
| 444 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 445 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 446 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 447 | const char *label, |
| 448 | const unsigned char *random, size_t rlen, |
| 449 | unsigned char *dstbuf, size_t dlen); |
| 450 | |
| 451 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 452 | static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); |
| 453 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 454 | |
| 455 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 456 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 457 | const unsigned char *buf, |
| 458 | size_t len); |
| 459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 460 | |
| 461 | static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 462 | |
| 463 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 464 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 465 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 466 | |
| 467 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 468 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 469 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 470 | |
| 471 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 472 | const unsigned char *secret, size_t slen, |
| 473 | const char *label, |
| 474 | const unsigned char *random, size_t rlen, |
| 475 | unsigned char *dstbuf, size_t dlen) |
| 476 | { |
| 477 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 478 | |
| 479 | switch (prf) { |
| 480 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 481 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 482 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 483 | tls_prf = tls_prf_sha384; |
| 484 | break; |
| 485 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 486 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 487 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 488 | tls_prf = tls_prf_sha256; |
| 489 | break; |
| 490 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 492 | default: |
| 493 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 494 | } |
| 495 | |
| 496 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
| 497 | } |
| 498 | |
| 499 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 500 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
| 501 | { |
| 502 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 503 | if (session->peer_cert != NULL) { |
| 504 | mbedtls_x509_crt_free(session->peer_cert); |
| 505 | mbedtls_free(session->peer_cert); |
| 506 | session->peer_cert = NULL; |
| 507 | } |
| 508 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 509 | if (session->peer_cert_digest != NULL) { |
| 510 | /* Zeroization is not necessary. */ |
| 511 | mbedtls_free(session->peer_cert_digest); |
| 512 | session->peer_cert_digest = NULL; |
| 513 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 514 | session->peer_cert_digest_len = 0; |
| 515 | } |
| 516 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 517 | } |
| 518 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 519 | |
| 520 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) |
| 521 | { |
| 522 | switch (extension_type) { |
| 523 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 524 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; |
| 525 | |
| 526 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 527 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; |
| 528 | |
| 529 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
| 530 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; |
| 531 | |
| 532 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
| 533 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; |
| 534 | |
| 535 | case MBEDTLS_TLS_EXT_SIG_ALG: |
| 536 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; |
| 537 | |
| 538 | case MBEDTLS_TLS_EXT_USE_SRTP: |
| 539 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; |
| 540 | |
| 541 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
| 542 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; |
| 543 | |
| 544 | case MBEDTLS_TLS_EXT_ALPN: |
| 545 | return MBEDTLS_SSL_EXT_ID_ALPN; |
| 546 | |
| 547 | case MBEDTLS_TLS_EXT_SCT: |
| 548 | return MBEDTLS_SSL_EXT_ID_SCT; |
| 549 | |
| 550 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
| 551 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; |
| 552 | |
| 553 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
| 554 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; |
| 555 | |
| 556 | case MBEDTLS_TLS_EXT_PADDING: |
| 557 | return MBEDTLS_SSL_EXT_ID_PADDING; |
| 558 | |
| 559 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
| 560 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; |
| 561 | |
| 562 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
| 563 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; |
| 564 | |
| 565 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
| 566 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; |
| 567 | |
| 568 | case MBEDTLS_TLS_EXT_COOKIE: |
| 569 | return MBEDTLS_SSL_EXT_ID_COOKIE; |
| 570 | |
| 571 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
| 572 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; |
| 573 | |
| 574 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
| 575 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; |
| 576 | |
| 577 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
| 578 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; |
| 579 | |
| 580 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
| 581 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; |
| 582 | |
| 583 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
| 584 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; |
| 585 | |
| 586 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
| 587 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; |
| 588 | |
| 589 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 590 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; |
| 591 | |
| 592 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 593 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; |
| 594 | |
| 595 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 596 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; |
| 597 | |
| 598 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 599 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; |
| 600 | |
| 601 | case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: |
| 602 | return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT; |
| 603 | |
| 604 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 605 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; |
| 606 | |
| 607 | } |
| 608 | |
| 609 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; |
| 610 | } |
| 611 | |
| 612 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) |
| 613 | { |
| 614 | return 1 << mbedtls_ssl_get_extension_id(extension_type); |
| 615 | } |
| 616 | |
| 617 | #if defined(MBEDTLS_DEBUG_C) |
| 618 | static const char *extension_name_table[] = { |
| 619 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
| 620 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 621 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 622 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 623 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 624 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 625 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 626 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 627 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 628 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 629 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 630 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 631 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 632 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 633 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 634 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 635 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 636 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 637 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 638 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 639 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 640 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 641 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 642 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 643 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 644 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 645 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
| 646 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket", |
| 647 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit" |
| 648 | }; |
| 649 | |
| 650 | static const unsigned int extension_type_table[] = { |
| 651 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 652 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 653 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 654 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 655 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 656 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 657 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 658 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 659 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 660 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 661 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 662 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 663 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 664 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 665 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 666 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 667 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 668 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 669 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 670 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 671 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 672 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 673 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 674 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 675 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 676 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 677 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, |
| 678 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET, |
| 679 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT |
| 680 | }; |
| 681 | |
| 682 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) |
| 683 | { |
| 684 | return extension_name_table[ |
| 685 | mbedtls_ssl_get_extension_id(extension_type)]; |
| 686 | } |
| 687 | |
| 688 | const char *mbedtls_ssl_get_hs_msg_name(int hs_msg_type) |
| 689 | { |
| 690 | switch (hs_msg_type) { |
| 691 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
| 692 | return "ClientHello"; |
| 693 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
| 694 | return "ServerHello"; |
| 695 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
| 696 | return "HelloRetryRequest"; |
| 697 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
| 698 | return "NewSessionTicket"; |
| 699 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
| 700 | return "EncryptedExtensions"; |
| 701 | case MBEDTLS_SSL_HS_CERTIFICATE: |
| 702 | return "Certificate"; |
| 703 | case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: |
| 704 | return "ServerKeyExchange"; |
| 705 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
| 706 | return "CertificateRequest"; |
| 707 | case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: |
| 708 | return "CertificateVerify"; |
| 709 | case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: |
| 710 | return "ClientKeyExchange"; |
| 711 | case MBEDTLS_SSL_HS_FINISHED: |
| 712 | return "Finished"; |
| 713 | } |
| 714 | return "Unknown"; |
| 715 | } |
| 716 | |
| 717 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, |
| 718 | int level, const char *file, int line, |
| 719 | int hs_msg_type, unsigned int extension_type, |
| 720 | const char *extra_msg0, const char *extra_msg1) |
| 721 | { |
| 722 | const char *extra_msg; |
| 723 | if (extra_msg0 && extra_msg1) { |
| 724 | mbedtls_debug_print_msg( |
| 725 | ssl, level, file, line, |
| 726 | "%s: %s(%u) extension %s %s.", |
| 727 | mbedtls_ssl_get_hs_msg_name(hs_msg_type), |
| 728 | mbedtls_ssl_get_extension_name(extension_type), |
| 729 | extension_type, |
| 730 | extra_msg0, extra_msg1); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
| 735 | if (extra_msg) { |
| 736 | mbedtls_debug_print_msg( |
| 737 | ssl, level, file, line, |
| 738 | "%s: %s(%u) extension %s.", mbedtls_ssl_get_hs_msg_name(hs_msg_type), |
| 739 | mbedtls_ssl_get_extension_name(extension_type), extension_type, |
| 740 | extra_msg); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | mbedtls_debug_print_msg( |
| 745 | ssl, level, file, line, |
| 746 | "%s: %s(%u) extension.", mbedtls_ssl_get_hs_msg_name(hs_msg_type), |
| 747 | mbedtls_ssl_get_extension_name(extension_type), extension_type); |
| 748 | } |
| 749 | |
| 750 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, |
| 751 | int level, const char *file, int line, |
| 752 | int hs_msg_type, uint32_t extensions_mask, |
| 753 | const char *extra) |
| 754 | { |
| 755 | |
| 756 | for (unsigned i = 0; |
| 757 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); |
| 758 | i++) { |
| 759 | mbedtls_ssl_print_extension( |
| 760 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
| 761 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 766 | static const char *ticket_flag_name_table[] = |
| 767 | { |
| 768 | [0] = "ALLOW_PSK_RESUMPTION", |
| 769 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", |
| 770 | [3] = "ALLOW_EARLY_DATA", |
| 771 | }; |
| 772 | |
| 773 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, |
| 774 | int level, const char *file, int line, |
| 775 | unsigned int flags) |
| 776 | { |
| 777 | size_t i; |
| 778 | |
| 779 | mbedtls_debug_print_msg(ssl, level, file, line, |
| 780 | "print ticket_flags (0x%02x)", flags); |
| 781 | |
| 782 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; |
| 783 | |
| 784 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { |
| 785 | if ((flags & (1 << i))) { |
| 786 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", |
| 787 | ticket_flag_name_table[i]); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ |
| 792 | |
| 793 | #endif /* MBEDTLS_DEBUG_C */ |
| 794 | |
| 795 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, |
| 796 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
| 797 | { |
| 798 | ((void) ciphersuite_info); |
| 799 | |
| 800 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 801 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 802 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 803 | } else |
| 804 | #endif |
| 805 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 806 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { |
| 807 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 808 | } else |
| 809 | #endif |
| 810 | { |
| 811 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 812 | return; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl, |
| 817 | unsigned hs_type, |
| 818 | size_t total_hs_len) |
| 819 | { |
| 820 | unsigned char hs_hdr[4]; |
| 821 | |
| 822 | /* Build HS header for checksum update. */ |
| 823 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); |
| 824 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); |
| 825 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); |
| 826 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); |
| 827 | |
| 828 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
| 829 | } |
| 830 | |
| 831 | int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl, |
| 832 | unsigned hs_type, |
| 833 | unsigned char const *msg, |
| 834 | size_t msg_len) |
| 835 | { |
| 836 | int ret; |
| 837 | ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len); |
| 838 | if (ret != 0) { |
| 839 | return ret; |
| 840 | } |
| 841 | return ssl->handshake->update_checksum(ssl, msg, msg_len); |
| 842 | } |
| 843 | |
| 844 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
| 845 | { |
| 846 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 847 | defined(MBEDTLS_MD_CAN_SHA384) |
| 848 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 849 | psa_status_t status; |
| 850 | #else |
| 851 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 852 | #endif |
| 853 | #else /* SHA-256 or SHA-384 */ |
| 854 | ((void) ssl); |
| 855 | #endif /* SHA-256 or SHA-384 */ |
| 856 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 858 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 859 | if (status != PSA_SUCCESS) { |
| 860 | return mbedtls_md_error_from_psa(status); |
| 861 | } |
| 862 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 863 | if (status != PSA_SUCCESS) { |
| 864 | return mbedtls_md_error_from_psa(status); |
| 865 | } |
| 866 | #else |
| 867 | mbedtls_md_free(&ssl->handshake->fin_sha256); |
| 868 | mbedtls_md_init(&ssl->handshake->fin_sha256); |
| 869 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, |
| 870 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 871 | 0); |
| 872 | if (ret != 0) { |
| 873 | return ret; |
| 874 | } |
| 875 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); |
| 876 | if (ret != 0) { |
| 877 | return ret; |
| 878 | } |
| 879 | #endif |
| 880 | #endif |
| 881 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 882 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 883 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 884 | if (status != PSA_SUCCESS) { |
| 885 | return mbedtls_md_error_from_psa(status); |
| 886 | } |
| 887 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 888 | if (status != PSA_SUCCESS) { |
| 889 | return mbedtls_md_error_from_psa(status); |
| 890 | } |
| 891 | #else |
| 892 | mbedtls_md_free(&ssl->handshake->fin_sha384); |
| 893 | mbedtls_md_init(&ssl->handshake->fin_sha384); |
| 894 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, |
| 895 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 896 | if (ret != 0) { |
| 897 | return ret; |
| 898 | } |
| 899 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); |
| 900 | if (ret != 0) { |
| 901 | return ret; |
| 902 | } |
| 903 | #endif |
| 904 | #endif |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
| 909 | const unsigned char *buf, size_t len) |
| 910 | { |
| 911 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 912 | defined(MBEDTLS_MD_CAN_SHA384) |
| 913 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 914 | psa_status_t status; |
| 915 | #else |
| 916 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 917 | #endif |
| 918 | #else /* SHA-256 or SHA-384 */ |
| 919 | ((void) ssl); |
| 920 | (void) buf; |
| 921 | (void) len; |
| 922 | #endif /* SHA-256 or SHA-384 */ |
| 923 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 924 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 925 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 926 | if (status != PSA_SUCCESS) { |
| 927 | return mbedtls_md_error_from_psa(status); |
| 928 | } |
| 929 | #else |
| 930 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
| 931 | if (ret != 0) { |
| 932 | return ret; |
| 933 | } |
| 934 | #endif |
| 935 | #endif |
| 936 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 937 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 938 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 939 | if (status != PSA_SUCCESS) { |
| 940 | return mbedtls_md_error_from_psa(status); |
| 941 | } |
| 942 | #else |
| 943 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
| 944 | if (ret != 0) { |
| 945 | return ret; |
| 946 | } |
| 947 | #endif |
| 948 | #endif |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 953 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
| 954 | const unsigned char *buf, size_t len) |
| 955 | { |
| 956 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 957 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 958 | &ssl->handshake->fin_sha256_psa, buf, len)); |
| 959 | #else |
| 960 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
| 961 | #endif |
| 962 | } |
| 963 | #endif |
| 964 | |
| 965 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 966 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
| 967 | const unsigned char *buf, size_t len) |
| 968 | { |
| 969 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 970 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 971 | &ssl->handshake->fin_sha384_psa, buf, len)); |
| 972 | #else |
| 973 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
| 974 | #endif |
| 975 | } |
| 976 | #endif |
| 977 | |
| 978 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
| 979 | { |
| 980 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
| 981 | |
| 982 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 983 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 984 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 985 | #else |
| 986 | mbedtls_md_init(&handshake->fin_sha256); |
| 987 | #endif |
| 988 | #endif |
| 989 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 990 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 991 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 992 | #else |
| 993 | mbedtls_md_init(&handshake->fin_sha384); |
| 994 | #endif |
| 995 | #endif |
| 996 | |
| 997 | handshake->update_checksum = ssl_update_checksum_start; |
| 998 | |
| 999 | #if defined(MBEDTLS_DHM_C) |
| 1000 | mbedtls_dhm_init(&handshake->dhm_ctx); |
| 1001 | #endif |
| 1002 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1003 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
| 1004 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
| 1005 | #endif |
| 1006 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1007 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1008 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 1009 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 1010 | #else |
| 1011 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
| 1012 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1013 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1014 | handshake->ecjpake_cache = NULL; |
| 1015 | handshake->ecjpake_cache_len = 0; |
| 1016 | #endif |
| 1017 | #endif |
| 1018 | |
| 1019 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 1020 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
| 1021 | #endif |
| 1022 | |
| 1023 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1024 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 1025 | #endif |
| 1026 | |
| 1027 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1028 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1029 | mbedtls_pk_init(&handshake->peer_pubkey); |
| 1030 | #endif |
| 1031 | } |
| 1032 | |
| 1033 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
| 1034 | { |
| 1035 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
| 1036 | |
| 1037 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1038 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1039 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1040 | #else |
| 1041 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 1042 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
| 1043 | #endif |
| 1044 | |
| 1045 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1046 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1047 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1048 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1049 | #else |
| 1050 | mbedtls_md_init(&transform->md_ctx_enc); |
| 1051 | mbedtls_md_init(&transform->md_ctx_dec); |
| 1052 | #endif |
| 1053 | #endif |
| 1054 | } |
| 1055 | |
| 1056 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
| 1057 | { |
| 1058 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
| 1059 | /* Set verify_result to -1u to indicate 'result not available'. */ |
| 1060 | session->verify_result = 0xFFFFFFFF; |
| 1061 | } |
| 1062 | |
| 1063 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1064 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
| 1065 | { |
| 1066 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1067 | |
| 1068 | /* Clear old handshake information if present */ |
| 1069 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1070 | if (ssl->transform_negotiate) { |
| 1071 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 1072 | } |
| 1073 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1074 | if (ssl->session_negotiate) { |
| 1075 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 1076 | } |
| 1077 | if (ssl->handshake) { |
| 1078 | mbedtls_ssl_handshake_free(ssl); |
| 1079 | } |
| 1080 | |
| 1081 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1082 | /* |
| 1083 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 1084 | * Now allocate missing structures. |
| 1085 | */ |
| 1086 | if (ssl->transform_negotiate == NULL) { |
| 1087 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
| 1088 | } |
| 1089 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1090 | |
| 1091 | if (ssl->session_negotiate == NULL) { |
| 1092 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
| 1093 | } |
| 1094 | |
| 1095 | if (ssl->handshake == NULL) { |
| 1096 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
| 1097 | } |
| 1098 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1099 | /* If the buffers are too small - reallocate */ |
| 1100 | |
| 1101 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1102 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1103 | #endif |
| 1104 | |
| 1105 | /* All pointers should exist and can be directly freed without issue */ |
| 1106 | if (ssl->handshake == NULL || |
| 1107 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1108 | ssl->transform_negotiate == NULL || |
| 1109 | #endif |
| 1110 | ssl->session_negotiate == NULL) { |
| 1111 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
| 1112 | |
| 1113 | mbedtls_free(ssl->handshake); |
| 1114 | ssl->handshake = NULL; |
| 1115 | |
| 1116 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1117 | mbedtls_free(ssl->transform_negotiate); |
| 1118 | ssl->transform_negotiate = NULL; |
| 1119 | #endif |
| 1120 | |
| 1121 | mbedtls_free(ssl->session_negotiate); |
| 1122 | ssl->session_negotiate = NULL; |
| 1123 | |
| 1124 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1125 | } |
| 1126 | |
| 1127 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1128 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1129 | ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE; |
| 1130 | #endif |
| 1131 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1132 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 1133 | #endif |
| 1134 | ssl->total_early_data_size = 0; |
| 1135 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 1136 | |
| 1137 | /* Initialize structures */ |
| 1138 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1139 | ssl_handshake_params_init(ssl->handshake); |
| 1140 | |
| 1141 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1142 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
| 1143 | #endif |
| 1144 | |
| 1145 | /* Setup handshake checksums */ |
| 1146 | ret = mbedtls_ssl_reset_checksum(ssl); |
| 1147 | if (ret != 0) { |
| 1148 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1153 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1154 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1155 | ssl->handshake->new_session_tickets_count = |
| 1156 | ssl->conf->new_session_tickets_count; |
| 1157 | #endif |
| 1158 | |
| 1159 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1160 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1161 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 1162 | |
| 1163 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 1164 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 1165 | } else { |
| 1166 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 1167 | } |
| 1168 | |
| 1169 | mbedtls_ssl_set_timer(ssl, 0); |
| 1170 | } |
| 1171 | #endif |
| 1172 | |
| 1173 | /* |
| 1174 | * curve_list is translated to IANA TLS group identifiers here because |
| 1175 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1176 | * any error codes. |
| 1177 | */ |
| 1178 | #if defined(MBEDTLS_ECP_C) |
| 1179 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1180 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 1181 | if (ssl->conf->curve_list != NULL) { |
| 1182 | size_t length; |
| 1183 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1184 | |
| 1185 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) { |
| 1186 | } |
| 1187 | |
| 1188 | /* Leave room for zero termination */ |
| 1189 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1190 | if (group_list == NULL) { |
| 1191 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1192 | } |
| 1193 | |
| 1194 | for (size_t i = 0; i < length; i++) { |
| 1195 | uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( |
| 1196 | curve_list[i]); |
| 1197 | if (tls_id == 0) { |
| 1198 | mbedtls_free(group_list); |
| 1199 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1200 | } |
| 1201 | group_list[i] = tls_id; |
| 1202 | } |
| 1203 | |
| 1204 | group_list[length] = 0; |
| 1205 | |
| 1206 | ssl->handshake->group_list = group_list; |
| 1207 | ssl->handshake->group_list_heap_allocated = 1; |
| 1208 | } else { |
| 1209 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1210 | ssl->handshake->group_list_heap_allocated = 0; |
| 1211 | } |
| 1212 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1213 | #endif /* MBEDTLS_ECP_C */ |
| 1214 | |
| 1215 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 1216 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1217 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1218 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1219 | signature algorithms IANA identifiers. */ |
| 1220 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1221 | ssl->conf->sig_hashes != NULL) { |
| 1222 | const int *md; |
| 1223 | const int *sig_hashes = ssl->conf->sig_hashes; |
| 1224 | size_t sig_algs_len = 0; |
| 1225 | uint16_t *p; |
| 1226 | |
| 1227 | MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1228 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1229 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
| 1230 | |
| 1231 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1232 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
| 1233 | continue; |
| 1234 | } |
| 1235 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 1236 | sig_algs_len += sizeof(uint16_t); |
| 1237 | #endif |
| 1238 | |
| 1239 | #if defined(MBEDTLS_RSA_C) |
| 1240 | sig_algs_len += sizeof(uint16_t); |
| 1241 | #endif |
| 1242 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1243 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1248 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1249 | } |
| 1250 | |
| 1251 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1252 | sizeof(uint16_t)); |
| 1253 | if (ssl->handshake->sig_algs == NULL) { |
| 1254 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1255 | } |
| 1256 | |
| 1257 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1258 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1259 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1260 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
| 1261 | continue; |
| 1262 | } |
| 1263 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 1264 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
| 1265 | p++; |
| 1266 | #endif |
| 1267 | #if defined(MBEDTLS_RSA_C) |
| 1268 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
| 1269 | p++; |
| 1270 | #endif |
| 1271 | } |
| 1272 | *p = MBEDTLS_TLS_SIG_NONE; |
| 1273 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 1274 | } else |
| 1275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1276 | { |
| 1277 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1278 | } |
| 1279 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 1280 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 1285 | /* Dummy cookie callbacks for defaults */ |
| 1286 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1287 | static int ssl_cookie_write_dummy(void *ctx, |
| 1288 | unsigned char **p, unsigned char *end, |
| 1289 | const unsigned char *cli_id, size_t cli_id_len) |
| 1290 | { |
| 1291 | ((void) ctx); |
| 1292 | ((void) p); |
| 1293 | ((void) end); |
| 1294 | ((void) cli_id); |
| 1295 | ((void) cli_id_len); |
| 1296 | |
| 1297 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1298 | } |
| 1299 | |
| 1300 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1301 | static int ssl_cookie_check_dummy(void *ctx, |
| 1302 | const unsigned char *cookie, size_t cookie_len, |
| 1303 | const unsigned char *cli_id, size_t cli_id_len) |
| 1304 | { |
| 1305 | ((void) ctx); |
| 1306 | ((void) cookie); |
| 1307 | ((void) cookie_len); |
| 1308 | ((void) cli_id); |
| 1309 | ((void) cli_id_len); |
| 1310 | |
| 1311 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1312 | } |
| 1313 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
| 1314 | |
| 1315 | /* |
| 1316 | * Initialize an SSL context |
| 1317 | */ |
| 1318 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
| 1319 | { |
| 1320 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
| 1321 | } |
| 1322 | |
| 1323 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1324 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
| 1325 | { |
| 1326 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1327 | |
| 1328 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1329 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1330 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1331 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1332 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1333 | } |
| 1334 | |
| 1335 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1336 | return 0; |
| 1337 | } |
| 1338 | #endif |
| 1339 | |
| 1340 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1341 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1342 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | #endif |
| 1346 | |
| 1347 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1348 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1349 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1350 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1351 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1352 | } |
| 1353 | |
| 1354 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | #endif |
| 1358 | |
| 1359 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1360 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1361 | } |
| 1362 | |
| 1363 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1364 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1365 | { |
| 1366 | int ret; |
| 1367 | ret = ssl_conf_version_check(ssl); |
| 1368 | if (ret != 0) { |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | if (ssl->conf->f_rng == NULL) { |
| 1373 | MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided")); |
| 1374 | return MBEDTLS_ERR_SSL_NO_RNG; |
| 1375 | } |
| 1376 | |
| 1377 | /* Space for further checks */ |
| 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | * Setup an SSL context |
| 1384 | */ |
| 1385 | |
| 1386 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1387 | const mbedtls_ssl_config *conf) |
| 1388 | { |
| 1389 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1390 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1391 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1392 | |
| 1393 | ssl->conf = conf; |
| 1394 | |
| 1395 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1396 | return ret; |
| 1397 | } |
| 1398 | ssl->tls_version = ssl->conf->max_tls_version; |
| 1399 | |
| 1400 | /* |
| 1401 | * Prepare base structures |
| 1402 | */ |
| 1403 | |
| 1404 | /* Set to NULL in case of an error condition */ |
| 1405 | ssl->out_buf = NULL; |
| 1406 | |
| 1407 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1408 | ssl->in_buf_len = in_buf_len; |
| 1409 | #endif |
| 1410 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1411 | if (ssl->in_buf == NULL) { |
| 1412 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len)); |
| 1413 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1414 | goto error; |
| 1415 | } |
| 1416 | |
| 1417 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1418 | ssl->out_buf_len = out_buf_len; |
| 1419 | #endif |
| 1420 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1421 | if (ssl->out_buf == NULL) { |
| 1422 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len)); |
| 1423 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1424 | goto error; |
| 1425 | } |
| 1426 | |
| 1427 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1428 | mbedtls_ssl_reset_out_pointers(ssl); |
| 1429 | |
| 1430 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 1431 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
| 1432 | #endif |
| 1433 | |
| 1434 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1435 | goto error; |
| 1436 | } |
| 1437 | |
| 1438 | return 0; |
| 1439 | |
| 1440 | error: |
| 1441 | mbedtls_free(ssl->in_buf); |
| 1442 | mbedtls_free(ssl->out_buf); |
| 1443 | |
| 1444 | ssl->conf = NULL; |
| 1445 | |
| 1446 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1447 | ssl->in_buf_len = 0; |
| 1448 | ssl->out_buf_len = 0; |
| 1449 | #endif |
| 1450 | ssl->in_buf = NULL; |
| 1451 | ssl->out_buf = NULL; |
| 1452 | |
| 1453 | ssl->in_hdr = NULL; |
| 1454 | ssl->in_ctr = NULL; |
| 1455 | ssl->in_len = NULL; |
| 1456 | ssl->in_iv = NULL; |
| 1457 | ssl->in_msg = NULL; |
| 1458 | |
| 1459 | ssl->out_hdr = NULL; |
| 1460 | ssl->out_ctr = NULL; |
| 1461 | ssl->out_len = NULL; |
| 1462 | ssl->out_iv = NULL; |
| 1463 | ssl->out_msg = NULL; |
| 1464 | |
| 1465 | return ret; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Reset an initialized and used SSL context for re-use while retaining |
| 1470 | * all application-set variables, function pointers and data. |
| 1471 | * |
| 1472 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1473 | * (Use when a DTLS client reconnects from the same port.) |
| 1474 | */ |
| 1475 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1476 | int partial) |
| 1477 | { |
| 1478 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1479 | size_t in_buf_len = ssl->in_buf_len; |
| 1480 | size_t out_buf_len = ssl->out_buf_len; |
| 1481 | #else |
| 1482 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1483 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1484 | #endif |
| 1485 | |
| 1486 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1487 | partial = 0; |
| 1488 | #endif |
| 1489 | |
| 1490 | /* Cancel any possibly running timer */ |
| 1491 | mbedtls_ssl_set_timer(ssl, 0); |
| 1492 | |
| 1493 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1494 | mbedtls_ssl_reset_out_pointers(ssl); |
| 1495 | |
| 1496 | /* Reset incoming message parsing */ |
| 1497 | ssl->in_offt = NULL; |
| 1498 | ssl->nb_zero = 0; |
| 1499 | ssl->in_msgtype = 0; |
| 1500 | ssl->in_msglen = 0; |
| 1501 | ssl->in_hslen = 0; |
| 1502 | ssl->keep_current_message = 0; |
| 1503 | ssl->transform_in = NULL; |
| 1504 | |
| 1505 | /* TLS: reset in_hsfraglen, which is part of message parsing. |
| 1506 | * DTLS: on a client reconnect, don't reset badmac_seen. */ |
| 1507 | if (!partial) { |
| 1508 | ssl->badmac_seen_or_in_hsfraglen = 0; |
| 1509 | } |
| 1510 | |
| 1511 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1512 | ssl->next_record_offset = 0; |
| 1513 | ssl->in_epoch = 0; |
| 1514 | #endif |
| 1515 | |
| 1516 | /* Keep current datagram if partial == 1 */ |
| 1517 | if (partial == 0) { |
| 1518 | ssl->in_left = 0; |
| 1519 | memset(ssl->in_buf, 0, in_buf_len); |
| 1520 | } |
| 1521 | |
| 1522 | ssl->send_alert = 0; |
| 1523 | |
| 1524 | /* Reset outgoing message writing */ |
| 1525 | ssl->out_msgtype = 0; |
| 1526 | ssl->out_msglen = 0; |
| 1527 | ssl->out_left = 0; |
| 1528 | memset(ssl->out_buf, 0, out_buf_len); |
| 1529 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
| 1530 | ssl->transform_out = NULL; |
| 1531 | |
| 1532 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1533 | mbedtls_ssl_dtls_replay_reset(ssl); |
| 1534 | #endif |
| 1535 | |
| 1536 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1537 | if (ssl->transform) { |
| 1538 | mbedtls_ssl_transform_free(ssl->transform); |
| 1539 | mbedtls_free(ssl->transform); |
| 1540 | ssl->transform = NULL; |
| 1541 | } |
| 1542 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1543 | |
| 1544 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1545 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1546 | mbedtls_free(ssl->transform_application); |
| 1547 | ssl->transform_application = NULL; |
| 1548 | |
| 1549 | if (ssl->handshake != NULL) { |
| 1550 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1551 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1552 | mbedtls_free(ssl->handshake->transform_earlydata); |
| 1553 | ssl->handshake->transform_earlydata = NULL; |
| 1554 | #endif |
| 1555 | |
| 1556 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1557 | mbedtls_free(ssl->handshake->transform_handshake); |
| 1558 | ssl->handshake->transform_handshake = NULL; |
| 1559 | } |
| 1560 | |
| 1561 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1562 | } |
| 1563 | |
| 1564 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
| 1565 | { |
| 1566 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1567 | |
| 1568 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST); |
| 1569 | ssl->tls_version = ssl->conf->max_tls_version; |
| 1570 | |
| 1571 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
| 1572 | |
| 1573 | /* Reset renegotiation state */ |
| 1574 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1575 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
| 1576 | ssl->renego_records_seen = 0; |
| 1577 | |
| 1578 | ssl->verify_data_len = 0; |
| 1579 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1580 | memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1581 | #endif |
| 1582 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
| 1583 | |
| 1584 | ssl->session_in = NULL; |
| 1585 | ssl->session_out = NULL; |
| 1586 | if (ssl->session) { |
| 1587 | mbedtls_ssl_session_free(ssl->session); |
| 1588 | mbedtls_free(ssl->session); |
| 1589 | ssl->session = NULL; |
| 1590 | } |
| 1591 | |
| 1592 | #if defined(MBEDTLS_SSL_ALPN) |
| 1593 | ssl->alpn_chosen = NULL; |
| 1594 | #endif |
| 1595 | |
| 1596 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 1597 | int free_cli_id = 1; |
| 1598 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
| 1599 | free_cli_id = (partial == 0); |
| 1600 | #endif |
| 1601 | if (free_cli_id) { |
| 1602 | mbedtls_free(ssl->cli_id); |
| 1603 | ssl->cli_id = NULL; |
| 1604 | ssl->cli_id_len = 0; |
| 1605 | } |
| 1606 | #endif |
| 1607 | |
| 1608 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Reset an initialized and used SSL context for re-use while retaining |
| 1617 | * all application-set variables, function pointers and data. |
| 1618 | */ |
| 1619 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
| 1620 | { |
| 1621 | return mbedtls_ssl_session_reset_int(ssl, 0); |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * SSL set accessors |
| 1626 | */ |
| 1627 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
| 1628 | { |
| 1629 | conf->endpoint = endpoint; |
| 1630 | } |
| 1631 | |
| 1632 | void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport) |
| 1633 | { |
| 1634 | conf->transport = transport; |
| 1635 | } |
| 1636 | |
| 1637 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1638 | void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode) |
| 1639 | { |
| 1640 | conf->anti_replay = mode; |
| 1641 | } |
| 1642 | #endif |
| 1643 | |
| 1644 | void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit) |
| 1645 | { |
| 1646 | conf->badmac_limit = limit; |
| 1647 | } |
| 1648 | |
| 1649 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1650 | |
| 1651 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1652 | unsigned allow_packing) |
| 1653 | { |
| 1654 | ssl->disable_datagram_packing = !allow_packing; |
| 1655 | } |
| 1656 | |
| 1657 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1658 | uint32_t min, uint32_t max) |
| 1659 | { |
| 1660 | conf->hs_timeout_min = min; |
| 1661 | conf->hs_timeout_max = max; |
| 1662 | } |
| 1663 | #endif |
| 1664 | |
| 1665 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
| 1666 | { |
| 1667 | conf->authmode = authmode; |
| 1668 | } |
| 1669 | |
| 1670 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1671 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1672 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1673 | void *p_vrfy) |
| 1674 | { |
| 1675 | conf->f_vrfy = f_vrfy; |
| 1676 | conf->p_vrfy = p_vrfy; |
| 1677 | } |
| 1678 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1679 | |
| 1680 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1681 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1682 | void *p_rng) |
| 1683 | { |
| 1684 | conf->f_rng = f_rng; |
| 1685 | conf->p_rng = p_rng; |
| 1686 | } |
| 1687 | |
| 1688 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1689 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1690 | void *p_dbg) |
| 1691 | { |
| 1692 | conf->f_dbg = f_dbg; |
| 1693 | conf->p_dbg = p_dbg; |
| 1694 | } |
| 1695 | |
| 1696 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1697 | void *p_bio, |
| 1698 | mbedtls_ssl_send_t *f_send, |
| 1699 | mbedtls_ssl_recv_t *f_recv, |
| 1700 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
| 1701 | { |
| 1702 | ssl->p_bio = p_bio; |
| 1703 | ssl->f_send = f_send; |
| 1704 | ssl->f_recv = f_recv; |
| 1705 | ssl->f_recv_timeout = f_recv_timeout; |
| 1706 | } |
| 1707 | |
| 1708 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1709 | void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu) |
| 1710 | { |
| 1711 | ssl->mtu = mtu; |
| 1712 | } |
| 1713 | #endif |
| 1714 | |
| 1715 | void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout) |
| 1716 | { |
| 1717 | conf->read_timeout = timeout; |
| 1718 | } |
| 1719 | |
| 1720 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1721 | void *p_timer, |
| 1722 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1723 | mbedtls_ssl_get_timer_t *f_get_timer) |
| 1724 | { |
| 1725 | ssl->p_timer = p_timer; |
| 1726 | ssl->f_set_timer = f_set_timer; |
| 1727 | ssl->f_get_timer = f_get_timer; |
| 1728 | |
| 1729 | /* Make sure we start with no timer running */ |
| 1730 | mbedtls_ssl_set_timer(ssl, 0); |
| 1731 | } |
| 1732 | |
| 1733 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1734 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1735 | void *p_cache, |
| 1736 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1737 | mbedtls_ssl_cache_set_t *f_set_cache) |
| 1738 | { |
| 1739 | conf->p_cache = p_cache; |
| 1740 | conf->f_get_cache = f_get_cache; |
| 1741 | conf->f_set_cache = f_set_cache; |
| 1742 | } |
| 1743 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1744 | |
| 1745 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1746 | int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session) |
| 1747 | { |
| 1748 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1749 | |
| 1750 | if (ssl == NULL || |
| 1751 | session == NULL || |
| 1752 | ssl->session_negotiate == NULL || |
| 1753 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1754 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1755 | } |
| 1756 | |
| 1757 | if (ssl->handshake->resume == 1) { |
| 1758 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1759 | } |
| 1760 | |
| 1761 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1762 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 1763 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1764 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1765 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
| 1766 | |
| 1767 | if (mbedtls_ssl_validate_ciphersuite( |
| 1768 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1769 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1770 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1771 | session->ciphersuite)); |
| 1772 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1773 | } |
| 1774 | #else |
| 1775 | /* |
| 1776 | * If session tickets are not enabled, it is not possible to resume a |
| 1777 | * TLS 1.3 session, thus do not make any change to the SSL context in |
| 1778 | * the first place. |
| 1779 | */ |
| 1780 | return 0; |
| 1781 | #endif |
| 1782 | } |
| 1783 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1784 | |
| 1785 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1786 | session)) != 0) { |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | ssl->handshake->resume = 1; |
| 1791 | |
| 1792 | return 0; |
| 1793 | } |
| 1794 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1795 | |
| 1796 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1797 | const int *ciphersuites) |
| 1798 | { |
| 1799 | conf->ciphersuite_list = ciphersuites; |
| 1800 | } |
| 1801 | |
| 1802 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1803 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1804 | const int kex_modes) |
| 1805 | { |
| 1806 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
| 1807 | } |
| 1808 | |
| 1809 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1810 | void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf, |
| 1811 | int early_data_enabled) |
| 1812 | { |
| 1813 | conf->early_data_enabled = early_data_enabled; |
| 1814 | } |
| 1815 | |
| 1816 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1817 | void mbedtls_ssl_conf_max_early_data_size( |
| 1818 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
| 1819 | { |
| 1820 | conf->max_early_data_size = max_early_data_size; |
| 1821 | } |
| 1822 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1823 | |
| 1824 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 1825 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1826 | |
| 1827 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1828 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1829 | const mbedtls_x509_crt_profile *profile) |
| 1830 | { |
| 1831 | conf->cert_profile = profile; |
| 1832 | } |
| 1833 | |
| 1834 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
| 1835 | { |
| 1836 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1837 | |
| 1838 | while (cur != NULL) { |
| 1839 | next = cur->next; |
| 1840 | mbedtls_free(cur); |
| 1841 | cur = next; |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | /* Append a new keycert entry to a (possibly empty) list */ |
| 1846 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1847 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1848 | mbedtls_x509_crt *cert, |
| 1849 | mbedtls_pk_context *key) |
| 1850 | { |
| 1851 | mbedtls_ssl_key_cert *new_cert; |
| 1852 | |
| 1853 | if (cert == NULL) { |
| 1854 | /* Free list if cert is null */ |
| 1855 | ssl_key_cert_free(*head); |
| 1856 | *head = NULL; |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
| 1860 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1861 | if (new_cert == NULL) { |
| 1862 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1863 | } |
| 1864 | |
| 1865 | new_cert->cert = cert; |
| 1866 | new_cert->key = key; |
| 1867 | new_cert->next = NULL; |
| 1868 | |
| 1869 | /* Update head if the list was null, else add to the end */ |
| 1870 | if (*head == NULL) { |
| 1871 | *head = new_cert; |
| 1872 | } else { |
| 1873 | mbedtls_ssl_key_cert *cur = *head; |
| 1874 | while (cur->next != NULL) { |
| 1875 | cur = cur->next; |
| 1876 | } |
| 1877 | cur->next = new_cert; |
| 1878 | } |
| 1879 | |
| 1880 | return 0; |
| 1881 | } |
| 1882 | |
| 1883 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
| 1884 | mbedtls_x509_crt *own_cert, |
| 1885 | mbedtls_pk_context *pk_key) |
| 1886 | { |
| 1887 | return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key); |
| 1888 | } |
| 1889 | |
| 1890 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
| 1891 | mbedtls_x509_crt *ca_chain, |
| 1892 | mbedtls_x509_crl *ca_crl) |
| 1893 | { |
| 1894 | conf->ca_chain = ca_chain; |
| 1895 | conf->ca_crl = ca_crl; |
| 1896 | |
| 1897 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1898 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1899 | * cannot be used together. */ |
| 1900 | conf->f_ca_cb = NULL; |
| 1901 | conf->p_ca_cb = NULL; |
| 1902 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 1903 | } |
| 1904 | |
| 1905 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1906 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1907 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1908 | void *p_ca_cb) |
| 1909 | { |
| 1910 | conf->f_ca_cb = f_ca_cb; |
| 1911 | conf->p_ca_cb = p_ca_cb; |
| 1912 | |
| 1913 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1914 | * cannot be used together. */ |
| 1915 | conf->ca_chain = NULL; |
| 1916 | conf->ca_crl = NULL; |
| 1917 | } |
| 1918 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 1919 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1920 | |
| 1921 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1922 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1923 | size_t *name_len) |
| 1924 | { |
| 1925 | *name_len = ssl->handshake->sni_name_len; |
| 1926 | return ssl->handshake->sni_name; |
| 1927 | } |
| 1928 | |
| 1929 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1930 | mbedtls_x509_crt *own_cert, |
| 1931 | mbedtls_pk_context *pk_key) |
| 1932 | { |
| 1933 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1934 | own_cert, pk_key); |
| 1935 | } |
| 1936 | |
| 1937 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1938 | mbedtls_x509_crt *ca_chain, |
| 1939 | mbedtls_x509_crl *ca_crl) |
| 1940 | { |
| 1941 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1942 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1943 | } |
| 1944 | |
| 1945 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1946 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1947 | const mbedtls_x509_crt *crt) |
| 1948 | { |
| 1949 | ssl->handshake->dn_hints = crt; |
| 1950 | } |
| 1951 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1952 | |
| 1953 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1954 | int authmode) |
| 1955 | { |
| 1956 | ssl->handshake->sni_authmode = authmode; |
| 1957 | } |
| 1958 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1959 | |
| 1960 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1961 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1962 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1963 | void *p_vrfy) |
| 1964 | { |
| 1965 | ssl->f_vrfy = f_vrfy; |
| 1966 | ssl->p_vrfy = p_vrfy; |
| 1967 | } |
| 1968 | #endif |
| 1969 | |
| 1970 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1971 | |
| 1972 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1973 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; |
| 1974 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; |
| 1975 | |
| 1976 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
| 1977 | mbedtls_ssl_context *ssl, |
| 1978 | mbedtls_svc_key_id_t pwd) |
| 1979 | { |
| 1980 | psa_status_t status; |
| 1981 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1982 | const uint8_t *user = NULL; |
| 1983 | size_t user_len = 0; |
| 1984 | const uint8_t *peer = NULL; |
| 1985 | size_t peer_len = 0; |
| 1986 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1987 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1988 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1989 | PSA_ECC_FAMILY_SECP_R1, |
| 1990 | 256)); |
| 1991 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
| 1992 | |
| 1993 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1994 | if (status != PSA_SUCCESS) { |
| 1995 | return status; |
| 1996 | } |
| 1997 | |
| 1998 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1999 | user = jpake_server_id; |
| 2000 | user_len = sizeof(jpake_server_id); |
| 2001 | peer = jpake_client_id; |
| 2002 | peer_len = sizeof(jpake_client_id); |
| 2003 | } else { |
| 2004 | user = jpake_client_id; |
| 2005 | user_len = sizeof(jpake_client_id); |
| 2006 | peer = jpake_server_id; |
| 2007 | peer_len = sizeof(jpake_server_id); |
| 2008 | } |
| 2009 | |
| 2010 | status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len); |
| 2011 | if (status != PSA_SUCCESS) { |
| 2012 | return status; |
| 2013 | } |
| 2014 | |
| 2015 | status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len); |
| 2016 | if (status != PSA_SUCCESS) { |
| 2017 | return status; |
| 2018 | } |
| 2019 | |
| 2020 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 2021 | if (status != PSA_SUCCESS) { |
| 2022 | return status; |
| 2023 | } |
| 2024 | |
| 2025 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 2026 | |
| 2027 | return PSA_SUCCESS; |
| 2028 | } |
| 2029 | |
| 2030 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2031 | const unsigned char *pw, |
| 2032 | size_t pw_len) |
| 2033 | { |
| 2034 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2035 | psa_status_t status; |
| 2036 | |
| 2037 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2038 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2039 | } |
| 2040 | |
| 2041 | /* Empty password is not valid */ |
| 2042 | if ((pw == NULL) || (pw_len == 0)) { |
| 2043 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2044 | } |
| 2045 | |
| 2046 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 2047 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 2048 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 2049 | |
| 2050 | status = psa_import_key(&attributes, pw, pw_len, |
| 2051 | &ssl->handshake->psa_pake_password); |
| 2052 | if (status != PSA_SUCCESS) { |
| 2053 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2054 | } |
| 2055 | |
| 2056 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 2057 | ssl->handshake->psa_pake_password); |
| 2058 | if (status != PSA_SUCCESS) { |
| 2059 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 2060 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2061 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2062 | } |
| 2063 | |
| 2064 | return 0; |
| 2065 | } |
| 2066 | |
| 2067 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 2068 | mbedtls_svc_key_id_t pwd) |
| 2069 | { |
| 2070 | psa_status_t status; |
| 2071 | |
| 2072 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2073 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2074 | } |
| 2075 | |
| 2076 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 2077 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2078 | } |
| 2079 | |
| 2080 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 2081 | if (status != PSA_SUCCESS) { |
| 2082 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2083 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2084 | } |
| 2085 | |
| 2086 | return 0; |
| 2087 | } |
| 2088 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2089 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2090 | const unsigned char *pw, |
| 2091 | size_t pw_len) |
| 2092 | { |
| 2093 | mbedtls_ecjpake_role role; |
| 2094 | |
| 2095 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2096 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2097 | } |
| 2098 | |
| 2099 | /* Empty password is not valid */ |
| 2100 | if ((pw == NULL) || (pw_len == 0)) { |
| 2101 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2102 | } |
| 2103 | |
| 2104 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2105 | role = MBEDTLS_ECJPAKE_SERVER; |
| 2106 | } else { |
| 2107 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 2108 | } |
| 2109 | |
| 2110 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 2111 | role, |
| 2112 | MBEDTLS_MD_SHA256, |
| 2113 | MBEDTLS_ECP_DP_SECP256R1, |
| 2114 | pw, pw_len); |
| 2115 | } |
| 2116 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2117 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2118 | |
| 2119 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
| 2120 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
| 2121 | { |
| 2122 | if (conf->psk_identity == NULL || |
| 2123 | conf->psk_identity_len == 0) { |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
| 2127 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2128 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2129 | return 1; |
| 2130 | } |
| 2131 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2132 | |
| 2133 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2134 | return 1; |
| 2135 | } |
| 2136 | |
| 2137 | return 0; |
| 2138 | } |
| 2139 | |
| 2140 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
| 2141 | { |
| 2142 | /* Remove reference to existing PSK, if any. */ |
| 2143 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2144 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2145 | /* The maintenance of the PSK key slot is the |
| 2146 | * user's responsibility. */ |
| 2147 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 2148 | } |
| 2149 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2150 | if (conf->psk != NULL) { |
| 2151 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
| 2152 | conf->psk = NULL; |
| 2153 | conf->psk_len = 0; |
| 2154 | } |
| 2155 | |
| 2156 | /* Remove reference to PSK identity, if any. */ |
| 2157 | if (conf->psk_identity != NULL) { |
| 2158 | mbedtls_free(conf->psk_identity); |
| 2159 | conf->psk_identity = NULL; |
| 2160 | conf->psk_identity_len = 0; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2165 | * It checks that the provided identity is well-formed and attempts |
| 2166 | * to make a copy of it in the SSL config. |
| 2167 | * On failure, the PSK identity in the config remains unset. */ |
| 2168 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2169 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2170 | unsigned char const *psk_identity, |
| 2171 | size_t psk_identity_len) |
| 2172 | { |
| 2173 | /* Identity len will be encoded on two bytes */ |
| 2174 | if (psk_identity == NULL || |
| 2175 | psk_identity_len == 0 || |
| 2176 | (psk_identity_len >> 16) != 0 || |
| 2177 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2178 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2179 | } |
| 2180 | |
| 2181 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2182 | if (conf->psk_identity == NULL) { |
| 2183 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2184 | } |
| 2185 | |
| 2186 | conf->psk_identity_len = psk_identity_len; |
| 2187 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
| 2188 | |
| 2189 | return 0; |
| 2190 | } |
| 2191 | |
| 2192 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2193 | const unsigned char *psk, size_t psk_len, |
| 2194 | const unsigned char *psk_identity, size_t psk_identity_len) |
| 2195 | { |
| 2196 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2197 | |
| 2198 | /* We currently only support one PSK, raw or opaque. */ |
| 2199 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2200 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2201 | } |
| 2202 | |
| 2203 | /* Check and set raw PSK */ |
| 2204 | if (psk == NULL) { |
| 2205 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2206 | } |
| 2207 | if (psk_len == 0) { |
| 2208 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2209 | } |
| 2210 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2211 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2212 | } |
| 2213 | |
| 2214 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2215 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2216 | } |
| 2217 | conf->psk_len = psk_len; |
| 2218 | memcpy(conf->psk, psk, conf->psk_len); |
| 2219 | |
| 2220 | /* Check and set PSK Identity */ |
| 2221 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2222 | if (ret != 0) { |
| 2223 | ssl_conf_remove_psk(conf); |
| 2224 | } |
| 2225 | |
| 2226 | return ret; |
| 2227 | } |
| 2228 | |
| 2229 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
| 2230 | { |
| 2231 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2232 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
| 2233 | /* The maintenance of the external PSK key slot is the |
| 2234 | * user's responsibility. */ |
| 2235 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2236 | psa_destroy_key(ssl->handshake->psk_opaque); |
| 2237 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2238 | } |
| 2239 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 2240 | } |
| 2241 | #else |
| 2242 | if (ssl->handshake->psk != NULL) { |
| 2243 | mbedtls_zeroize_and_free(ssl->handshake->psk, |
| 2244 | ssl->handshake->psk_len); |
| 2245 | ssl->handshake->psk_len = 0; |
| 2246 | ssl->handshake->psk = NULL; |
| 2247 | } |
| 2248 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2249 | } |
| 2250 | |
| 2251 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2252 | const unsigned char *psk, size_t psk_len) |
| 2253 | { |
| 2254 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2255 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 2256 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2257 | psa_algorithm_t alg = PSA_ALG_NONE; |
| 2258 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2259 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2260 | |
| 2261 | if (psk == NULL || ssl->handshake == NULL) { |
| 2262 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2263 | } |
| 2264 | |
| 2265 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2266 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2267 | } |
| 2268 | |
| 2269 | ssl_remove_psk(ssl); |
| 2270 | |
| 2271 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2272 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2273 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2274 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2275 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2276 | } else { |
| 2277 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2278 | } |
| 2279 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 2280 | } |
| 2281 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2282 | |
| 2283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 2284 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2285 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2286 | psa_set_key_usage_flags(&key_attributes, |
| 2287 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
| 2288 | } |
| 2289 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2290 | |
| 2291 | psa_set_key_algorithm(&key_attributes, alg); |
| 2292 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
| 2293 | |
| 2294 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2295 | if (status != PSA_SUCCESS) { |
| 2296 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2297 | } |
| 2298 | |
| 2299 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2300 | ssl->handshake->psk_opaque_is_internal = 1; |
| 2301 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
| 2302 | #else |
| 2303 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2304 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2305 | } |
| 2306 | |
| 2307 | ssl->handshake->psk_len = psk_len; |
| 2308 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
| 2309 | |
| 2310 | return 0; |
| 2311 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2312 | } |
| 2313 | |
| 2314 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2315 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2316 | mbedtls_svc_key_id_t psk, |
| 2317 | const unsigned char *psk_identity, |
| 2318 | size_t psk_identity_len) |
| 2319 | { |
| 2320 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2321 | |
| 2322 | /* We currently only support one PSK, raw or opaque. */ |
| 2323 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2324 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2325 | } |
| 2326 | |
| 2327 | /* Check and set opaque PSK */ |
| 2328 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2329 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2330 | } |
| 2331 | conf->psk_opaque = psk; |
| 2332 | |
| 2333 | /* Check and set PSK Identity */ |
| 2334 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2335 | psk_identity_len); |
| 2336 | if (ret != 0) { |
| 2337 | ssl_conf_remove_psk(conf); |
| 2338 | } |
| 2339 | |
| 2340 | return ret; |
| 2341 | } |
| 2342 | |
| 2343 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2344 | mbedtls_svc_key_id_t psk) |
| 2345 | { |
| 2346 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2347 | (ssl->handshake == NULL)) { |
| 2348 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2349 | } |
| 2350 | |
| 2351 | ssl_remove_psk(ssl); |
| 2352 | ssl->handshake->psk_opaque = psk; |
| 2353 | return 0; |
| 2354 | } |
| 2355 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2356 | |
| 2357 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2358 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2359 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2360 | size_t), |
| 2361 | void *p_psk) |
| 2362 | { |
| 2363 | conf->f_psk = f_psk; |
| 2364 | conf->p_psk = p_psk; |
| 2365 | } |
| 2366 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2367 | |
| 2368 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
| 2369 | |
| 2370 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2371 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2372 | psa_algorithm_t alg) |
| 2373 | { |
| 2374 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2375 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2376 | return MBEDTLS_SSL_MODE_CBC; |
| 2377 | } |
| 2378 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2379 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2380 | return MBEDTLS_SSL_MODE_AEAD; |
| 2381 | } |
| 2382 | return MBEDTLS_SSL_MODE_STREAM; |
| 2383 | } |
| 2384 | |
| 2385 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2386 | |
| 2387 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2388 | mbedtls_cipher_mode_t mode) |
| 2389 | { |
| 2390 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2391 | if (mode == MBEDTLS_MODE_CBC) { |
| 2392 | return MBEDTLS_SSL_MODE_CBC; |
| 2393 | } |
| 2394 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2395 | |
| 2396 | #if defined(MBEDTLS_GCM_C) || \ |
| 2397 | defined(MBEDTLS_CCM_C) || \ |
| 2398 | defined(MBEDTLS_CHACHAPOLY_C) |
| 2399 | if (mode == MBEDTLS_MODE_GCM || |
| 2400 | mode == MBEDTLS_MODE_CCM || |
| 2401 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2402 | return MBEDTLS_SSL_MODE_AEAD; |
| 2403 | } |
| 2404 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 2405 | |
| 2406 | return MBEDTLS_SSL_MODE_STREAM; |
| 2407 | } |
| 2408 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2409 | |
| 2410 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2411 | mbedtls_ssl_mode_t base_mode, |
| 2412 | int encrypt_then_mac) |
| 2413 | { |
| 2414 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2415 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2416 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2417 | return MBEDTLS_SSL_MODE_CBC_ETM; |
| 2418 | } |
| 2419 | #else |
| 2420 | (void) encrypt_then_mac; |
| 2421 | #endif |
| 2422 | return base_mode; |
| 2423 | } |
| 2424 | |
| 2425 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
| 2426 | const mbedtls_ssl_transform *transform) |
| 2427 | { |
| 2428 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
| 2429 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2430 | transform->psa_alg |
| 2431 | #else |
| 2432 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
| 2433 | #endif |
| 2434 | ); |
| 2435 | |
| 2436 | int encrypt_then_mac = 0; |
| 2437 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2438 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2439 | #endif |
| 2440 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
| 2441 | } |
| 2442 | |
| 2443 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
| 2444 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2445 | int encrypt_then_mac, |
| 2446 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 2447 | const mbedtls_ssl_ciphersuite_t *suite) |
| 2448 | { |
| 2449 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2450 | |
| 2451 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2452 | psa_status_t status; |
| 2453 | psa_algorithm_t alg; |
| 2454 | psa_key_type_t type; |
| 2455 | size_t size; |
| 2456 | status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher, |
| 2457 | 0, &alg, &type, &size); |
| 2458 | if (status == PSA_SUCCESS) { |
| 2459 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2460 | } |
| 2461 | #else |
| 2462 | const mbedtls_cipher_info_t *cipher = |
| 2463 | mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher); |
| 2464 | if (cipher != NULL) { |
| 2465 | base_mode = |
| 2466 | mbedtls_ssl_get_base_mode( |
| 2467 | mbedtls_cipher_info_get_mode(cipher)); |
| 2468 | } |
| 2469 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2470 | |
| 2471 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2472 | int encrypt_then_mac = 0; |
| 2473 | #endif |
| 2474 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
| 2475 | } |
| 2476 | |
| 2477 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 2478 | |
| 2479 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2480 | size_t taglen, |
| 2481 | psa_algorithm_t *alg, |
| 2482 | psa_key_type_t *key_type, |
| 2483 | size_t *key_size) |
| 2484 | { |
| 2485 | #if !defined(MBEDTLS_SSL_HAVE_CCM) |
| 2486 | (void) taglen; |
| 2487 | #endif |
| 2488 | switch (mbedtls_cipher_type) { |
| 2489 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2490 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2491 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2492 | *key_type = PSA_KEY_TYPE_AES; |
| 2493 | *key_size = 128; |
| 2494 | break; |
| 2495 | #endif |
| 2496 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2497 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2498 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2499 | *key_type = PSA_KEY_TYPE_AES; |
| 2500 | *key_size = 128; |
| 2501 | break; |
| 2502 | #endif |
| 2503 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2504 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2505 | *alg = PSA_ALG_GCM; |
| 2506 | *key_type = PSA_KEY_TYPE_AES; |
| 2507 | *key_size = 128; |
| 2508 | break; |
| 2509 | #endif |
| 2510 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2511 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2512 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2513 | *key_type = PSA_KEY_TYPE_AES; |
| 2514 | *key_size = 192; |
| 2515 | break; |
| 2516 | #endif |
| 2517 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2518 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2519 | *alg = PSA_ALG_GCM; |
| 2520 | *key_type = PSA_KEY_TYPE_AES; |
| 2521 | *key_size = 192; |
| 2522 | break; |
| 2523 | #endif |
| 2524 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2525 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2526 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2527 | *key_type = PSA_KEY_TYPE_AES; |
| 2528 | *key_size = 256; |
| 2529 | break; |
| 2530 | #endif |
| 2531 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2532 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2533 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2534 | *key_type = PSA_KEY_TYPE_AES; |
| 2535 | *key_size = 256; |
| 2536 | break; |
| 2537 | #endif |
| 2538 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2539 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2540 | *alg = PSA_ALG_GCM; |
| 2541 | *key_type = PSA_KEY_TYPE_AES; |
| 2542 | *key_size = 256; |
| 2543 | break; |
| 2544 | #endif |
| 2545 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2546 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2547 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2548 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2549 | *key_size = 128; |
| 2550 | break; |
| 2551 | #endif |
| 2552 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2553 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2554 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2555 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2556 | *key_size = 128; |
| 2557 | break; |
| 2558 | #endif |
| 2559 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2560 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2561 | *alg = PSA_ALG_GCM; |
| 2562 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2563 | *key_size = 128; |
| 2564 | break; |
| 2565 | #endif |
| 2566 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2567 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2568 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2569 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2570 | *key_size = 192; |
| 2571 | break; |
| 2572 | #endif |
| 2573 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2574 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2575 | *alg = PSA_ALG_GCM; |
| 2576 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2577 | *key_size = 192; |
| 2578 | break; |
| 2579 | #endif |
| 2580 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2581 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2582 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2583 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2584 | *key_size = 256; |
| 2585 | break; |
| 2586 | #endif |
| 2587 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2588 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2589 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2590 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2591 | *key_size = 256; |
| 2592 | break; |
| 2593 | #endif |
| 2594 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2595 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2596 | *alg = PSA_ALG_GCM; |
| 2597 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2598 | *key_size = 256; |
| 2599 | break; |
| 2600 | #endif |
| 2601 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2602 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2603 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2604 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2605 | *key_size = 128; |
| 2606 | break; |
| 2607 | #endif |
| 2608 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2609 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2610 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2611 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2612 | *key_size = 128; |
| 2613 | break; |
| 2614 | #endif |
| 2615 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2616 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2617 | *alg = PSA_ALG_GCM; |
| 2618 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2619 | *key_size = 128; |
| 2620 | break; |
| 2621 | #endif |
| 2622 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2623 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2624 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2625 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2626 | *key_size = 192; |
| 2627 | break; |
| 2628 | #endif |
| 2629 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2630 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2631 | *alg = PSA_ALG_GCM; |
| 2632 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2633 | *key_size = 192; |
| 2634 | break; |
| 2635 | #endif |
| 2636 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
| 2637 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2638 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2639 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2640 | *key_size = 256; |
| 2641 | break; |
| 2642 | #endif |
| 2643 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
| 2644 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2645 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; |
| 2646 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2647 | *key_size = 256; |
| 2648 | break; |
| 2649 | #endif |
| 2650 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
| 2651 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2652 | *alg = PSA_ALG_GCM; |
| 2653 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2654 | *key_size = 256; |
| 2655 | break; |
| 2656 | #endif |
| 2657 | #if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY) |
| 2658 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2659 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2660 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2661 | *key_size = 256; |
| 2662 | break; |
| 2663 | #endif |
| 2664 | case MBEDTLS_CIPHER_NULL: |
| 2665 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2666 | *key_type = 0; |
| 2667 | *key_size = 0; |
| 2668 | break; |
| 2669 | default: |
| 2670 | return PSA_ERROR_NOT_SUPPORTED; |
| 2671 | } |
| 2672 | |
| 2673 | return PSA_SUCCESS; |
| 2674 | } |
| 2675 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2676 | |
| 2677 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 2678 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2679 | const unsigned char *dhm_P, size_t P_len, |
| 2680 | const unsigned char *dhm_G, size_t G_len) |
| 2681 | { |
| 2682 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2683 | |
| 2684 | mbedtls_mpi_free(&conf->dhm_P); |
| 2685 | mbedtls_mpi_free(&conf->dhm_G); |
| 2686 | |
| 2687 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2688 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2689 | mbedtls_mpi_free(&conf->dhm_P); |
| 2690 | mbedtls_mpi_free(&conf->dhm_G); |
| 2691 | return ret; |
| 2692 | } |
| 2693 | |
| 2694 | return 0; |
| 2695 | } |
| 2696 | |
| 2697 | int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx) |
| 2698 | { |
| 2699 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2700 | |
| 2701 | mbedtls_mpi_free(&conf->dhm_P); |
| 2702 | mbedtls_mpi_free(&conf->dhm_G); |
| 2703 | |
| 2704 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2705 | &conf->dhm_P)) != 0 || |
| 2706 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2707 | &conf->dhm_G)) != 0) { |
| 2708 | mbedtls_mpi_free(&conf->dhm_P); |
| 2709 | mbedtls_mpi_free(&conf->dhm_G); |
| 2710 | return ret; |
| 2711 | } |
| 2712 | |
| 2713 | return 0; |
| 2714 | } |
| 2715 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
| 2716 | |
| 2717 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2718 | /* |
| 2719 | * Set the minimum length for Diffie-Hellman parameters |
| 2720 | */ |
| 2721 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2722 | unsigned int bitlen) |
| 2723 | { |
| 2724 | conf->dhm_min_bitlen = bitlen; |
| 2725 | } |
| 2726 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2727 | |
| 2728 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 2729 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2730 | /* |
| 2731 | * Set allowed/preferred hashes for handshake signatures |
| 2732 | */ |
| 2733 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2734 | const int *hashes) |
| 2735 | { |
| 2736 | conf->sig_hashes = hashes; |
| 2737 | } |
| 2738 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2739 | |
| 2740 | /* Configure allowed signature algorithms for handshake */ |
| 2741 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2742 | const uint16_t *sig_algs) |
| 2743 | { |
| 2744 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2745 | conf->sig_hashes = NULL; |
| 2746 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2747 | conf->sig_algs = sig_algs; |
| 2748 | } |
| 2749 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 2750 | |
| 2751 | #if defined(MBEDTLS_ECP_C) |
| 2752 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2753 | /* |
| 2754 | * Set the allowed elliptic curves |
| 2755 | * |
| 2756 | * mbedtls_ssl_setup() takes the provided list |
| 2757 | * and translates it to a list of IANA TLS group identifiers, |
| 2758 | * stored in ssl->handshake->group_list. |
| 2759 | * |
| 2760 | */ |
| 2761 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2762 | const mbedtls_ecp_group_id *curve_list) |
| 2763 | { |
| 2764 | conf->curve_list = curve_list; |
| 2765 | conf->group_list = NULL; |
| 2766 | } |
| 2767 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 2768 | #endif /* MBEDTLS_ECP_C */ |
| 2769 | |
| 2770 | /* |
| 2771 | * Set the allowed groups |
| 2772 | */ |
| 2773 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2774 | const uint16_t *group_list) |
| 2775 | { |
| 2776 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2777 | conf->curve_list = NULL; |
| 2778 | #endif |
| 2779 | conf->group_list = group_list; |
| 2780 | } |
| 2781 | |
| 2782 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2783 | |
| 2784 | /* A magic value for `ssl->hostname` indicating that |
| 2785 | * mbedtls_ssl_set_hostname() has been called with `NULL`. |
| 2786 | * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then |
| 2787 | * `ssl->hostname == NULL`. */ |
| 2788 | static const char *const ssl_hostname_skip_cn_verification = ""; |
| 2789 | |
| 2790 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 2791 | /** Whether mbedtls_ssl_set_hostname() has been called. |
| 2792 | * |
| 2793 | * \param[in] ssl SSL context |
| 2794 | * |
| 2795 | * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl |
| 2796 | * (including `mbedtls_ssl_set_hostname(ssl, NULL)`), |
| 2797 | * otherwise \c 0. |
| 2798 | */ |
| 2799 | static int mbedtls_ssl_has_set_hostname_been_called( |
| 2800 | const mbedtls_ssl_context *ssl) |
| 2801 | { |
| 2802 | return ssl->hostname != NULL; |
| 2803 | } |
| 2804 | #endif |
| 2805 | |
| 2806 | /* Micro-optimization: don't export this function if it isn't needed outside |
| 2807 | * of this source file. */ |
| 2808 | #if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2809 | static |
| 2810 | #endif |
| 2811 | const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl) |
| 2812 | { |
| 2813 | if (ssl->hostname == ssl_hostname_skip_cn_verification) { |
| 2814 | return NULL; |
| 2815 | } |
| 2816 | return ssl->hostname; |
| 2817 | } |
| 2818 | |
| 2819 | static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl) |
| 2820 | { |
| 2821 | if (ssl->hostname != NULL && |
| 2822 | ssl->hostname != ssl_hostname_skip_cn_verification) { |
| 2823 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
| 2824 | } |
| 2825 | ssl->hostname = NULL; |
| 2826 | } |
| 2827 | |
| 2828 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
| 2829 | { |
| 2830 | /* Initialize to suppress unnecessary compiler warning */ |
| 2831 | size_t hostname_len = 0; |
| 2832 | |
| 2833 | /* Check if new hostname is valid before |
| 2834 | * making any change to current one */ |
| 2835 | if (hostname != NULL) { |
| 2836 | hostname_len = strlen(hostname); |
| 2837 | |
| 2838 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2839 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | /* Now it's clear that we will overwrite the old hostname, |
| 2844 | * so we can free it safely */ |
| 2845 | mbedtls_ssl_free_hostname(ssl); |
| 2846 | |
| 2847 | if (hostname == NULL) { |
| 2848 | /* Passing NULL as hostname clears the old one, but leaves a |
| 2849 | * special marker to indicate that mbedtls_ssl_set_hostname() |
| 2850 | * has been called. */ |
| 2851 | /* ssl->hostname should be const, but isn't. We won't actually |
| 2852 | * write to the buffer, so it's ok to cast away the const. */ |
| 2853 | ssl->hostname = (char *) ssl_hostname_skip_cn_verification; |
| 2854 | } else { |
| 2855 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2856 | if (ssl->hostname == NULL) { |
| 2857 | /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully. |
| 2858 | * Leave ssl->hostname in the same state as if the function had |
| 2859 | * not been called, i.e. a null pointer. */ |
| 2860 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2861 | } |
| 2862 | |
| 2863 | memcpy(ssl->hostname, hostname, hostname_len); |
| 2864 | |
| 2865 | ssl->hostname[hostname_len] = '\0'; |
| 2866 | } |
| 2867 | |
| 2868 | return 0; |
| 2869 | } |
| 2870 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2871 | |
| 2872 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2873 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2874 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2875 | const unsigned char *, size_t), |
| 2876 | void *p_sni) |
| 2877 | { |
| 2878 | conf->f_sni = f_sni; |
| 2879 | conf->p_sni = p_sni; |
| 2880 | } |
| 2881 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2882 | |
| 2883 | #if defined(MBEDTLS_SSL_ALPN) |
| 2884 | int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos) |
| 2885 | { |
| 2886 | size_t cur_len, tot_len; |
| 2887 | const char **p; |
| 2888 | |
| 2889 | /* |
| 2890 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2891 | * MUST NOT be truncated." |
| 2892 | * We check lengths now rather than later. |
| 2893 | */ |
| 2894 | tot_len = 0; |
| 2895 | for (p = protos; *p != NULL; p++) { |
| 2896 | cur_len = strlen(*p); |
| 2897 | tot_len += cur_len; |
| 2898 | |
| 2899 | if ((cur_len == 0) || |
| 2900 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2901 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2902 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | conf->alpn_list = protos; |
| 2907 | |
| 2908 | return 0; |
| 2909 | } |
| 2910 | |
| 2911 | const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl) |
| 2912 | { |
| 2913 | return ssl->alpn_chosen; |
| 2914 | } |
| 2915 | #endif /* MBEDTLS_SSL_ALPN */ |
| 2916 | |
| 2917 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 2918 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2919 | int support_mki_value) |
| 2920 | { |
| 2921 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2922 | } |
| 2923 | |
| 2924 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2925 | unsigned char *mki_value, |
| 2926 | uint16_t mki_len) |
| 2927 | { |
| 2928 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2929 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2930 | } |
| 2931 | |
| 2932 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2933 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2934 | } |
| 2935 | |
| 2936 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
| 2937 | ssl->dtls_srtp_info.mki_len = mki_len; |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2942 | const mbedtls_ssl_srtp_profile *profiles) |
| 2943 | { |
| 2944 | const mbedtls_ssl_srtp_profile *p; |
| 2945 | size_t list_size = 0; |
| 2946 | |
| 2947 | /* check the profiles list: all entry must be valid, |
| 2948 | * its size cannot be more than the total number of supported profiles, currently 4 */ |
| 2949 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2950 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2951 | p++) { |
| 2952 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
| 2953 | list_size++; |
| 2954 | } else { |
| 2955 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2956 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 2961 | conf->dtls_srtp_profile_list = NULL; |
| 2962 | conf->dtls_srtp_profile_list_len = 0; |
| 2963 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2964 | } |
| 2965 | |
| 2966 | conf->dtls_srtp_profile_list = profiles; |
| 2967 | conf->dtls_srtp_profile_list_len = list_size; |
| 2968 | |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
| 2972 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 2973 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
| 2974 | { |
| 2975 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2976 | /* do not copy the mki value if there is no chosen profile */ |
| 2977 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
| 2978 | dtls_srtp_info->mki_len = 0; |
| 2979 | } else { |
| 2980 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
| 2981 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2982 | ssl->dtls_srtp_info.mki_len); |
| 2983 | } |
| 2984 | } |
| 2985 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2986 | |
| 2987 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2988 | void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor) |
| 2989 | { |
| 2990 | conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
| 2991 | } |
| 2992 | |
| 2993 | void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor) |
| 2994 | { |
| 2995 | conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
| 2996 | } |
| 2997 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 2998 | |
| 2999 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3000 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 3001 | char cert_req_ca_list) |
| 3002 | { |
| 3003 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3004 | } |
| 3005 | #endif |
| 3006 | |
| 3007 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3008 | void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm) |
| 3009 | { |
| 3010 | conf->encrypt_then_mac = etm; |
| 3011 | } |
| 3012 | #endif |
| 3013 | |
| 3014 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 3015 | void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems) |
| 3016 | { |
| 3017 | conf->extended_ms = ems; |
| 3018 | } |
| 3019 | #endif |
| 3020 | |
| 3021 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3022 | int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code) |
| 3023 | { |
| 3024 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3025 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3026 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3027 | } |
| 3028 | |
| 3029 | conf->mfl_code = mfl_code; |
| 3030 | |
| 3031 | return 0; |
| 3032 | } |
| 3033 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3034 | |
| 3035 | void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy) |
| 3036 | { |
| 3037 | conf->allow_legacy_renegotiation = allow_legacy; |
| 3038 | } |
| 3039 | |
| 3040 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3041 | void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation) |
| 3042 | { |
| 3043 | conf->disable_renegotiation = renegotiation; |
| 3044 | } |
| 3045 | |
| 3046 | void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records) |
| 3047 | { |
| 3048 | conf->renego_max_records = max_records; |
| 3049 | } |
| 3050 | |
| 3051 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3052 | const unsigned char period[8]) |
| 3053 | { |
| 3054 | memcpy(conf->renego_period, period, 8); |
| 3055 | } |
| 3056 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 3057 | |
| 3058 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3059 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3060 | |
| 3061 | void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets) |
| 3062 | { |
| 3063 | conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK; |
| 3064 | conf->session_tickets |= (use_tickets != 0) << |
| 3065 | MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT; |
| 3066 | } |
| 3067 | |
| 3068 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3069 | void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets( |
| 3070 | mbedtls_ssl_config *conf, int signal_new_session_tickets) |
| 3071 | { |
| 3072 | conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK; |
| 3073 | conf->session_tickets |= (signal_new_session_tickets != 0) << |
| 3074 | MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT; |
| 3075 | } |
| 3076 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3077 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3078 | |
| 3079 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3080 | |
| 3081 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3082 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3083 | uint16_t num_tickets) |
| 3084 | { |
| 3085 | conf->new_session_tickets_count = num_tickets; |
| 3086 | } |
| 3087 | #endif |
| 3088 | |
| 3089 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3090 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3091 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3092 | void *p_ticket) |
| 3093 | { |
| 3094 | conf->f_ticket_write = f_ticket_write; |
| 3095 | conf->f_ticket_parse = f_ticket_parse; |
| 3096 | conf->p_ticket = p_ticket; |
| 3097 | } |
| 3098 | #endif |
| 3099 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3100 | |
| 3101 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3102 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3103 | void *p_export_keys) |
| 3104 | { |
| 3105 | ssl->f_export_keys = f_export_keys; |
| 3106 | ssl->p_export_keys = p_export_keys; |
| 3107 | } |
| 3108 | |
| 3109 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3110 | void mbedtls_ssl_conf_async_private_cb( |
| 3111 | mbedtls_ssl_config *conf, |
| 3112 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3113 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3114 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3115 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
| 3116 | void *async_config_data) |
| 3117 | { |
| 3118 | conf->f_async_sign_start = f_async_sign; |
| 3119 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3120 | conf->f_async_resume = f_async_resume; |
| 3121 | conf->f_async_cancel = f_async_cancel; |
| 3122 | conf->p_async_config_data = async_config_data; |
| 3123 | } |
| 3124 | |
| 3125 | void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf) |
| 3126 | { |
| 3127 | return conf->p_async_config_data; |
| 3128 | } |
| 3129 | |
| 3130 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
| 3131 | { |
| 3132 | if (ssl->handshake == NULL) { |
| 3133 | return NULL; |
| 3134 | } else { |
| 3135 | return ssl->handshake->user_async_ctx; |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3140 | void *ctx) |
| 3141 | { |
| 3142 | if (ssl->handshake != NULL) { |
| 3143 | ssl->handshake->user_async_ctx = ctx; |
| 3144 | } |
| 3145 | } |
| 3146 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3147 | |
| 3148 | /* |
| 3149 | * SSL get accessors |
| 3150 | */ |
| 3151 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
| 3152 | { |
| 3153 | if (ssl->session != NULL) { |
| 3154 | return ssl->session->verify_result; |
| 3155 | } |
| 3156 | |
| 3157 | if (ssl->session_negotiate != NULL) { |
| 3158 | return ssl->session_negotiate->verify_result; |
| 3159 | } |
| 3160 | |
| 3161 | return 0xFFFFFFFF; |
| 3162 | } |
| 3163 | |
| 3164 | int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl) |
| 3165 | { |
| 3166 | if (ssl == NULL || ssl->session == NULL) { |
| 3167 | return 0; |
| 3168 | } |
| 3169 | |
| 3170 | return ssl->session->ciphersuite; |
| 3171 | } |
| 3172 | |
| 3173 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
| 3174 | { |
| 3175 | if (ssl == NULL || ssl->session == NULL) { |
| 3176 | return NULL; |
| 3177 | } |
| 3178 | |
| 3179 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
| 3180 | } |
| 3181 | |
| 3182 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
| 3183 | { |
| 3184 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3185 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3186 | switch (ssl->tls_version) { |
| 3187 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3188 | return "DTLSv1.2"; |
| 3189 | default: |
| 3190 | return "unknown (DTLS)"; |
| 3191 | } |
| 3192 | } |
| 3193 | #endif |
| 3194 | |
| 3195 | switch (ssl->tls_version) { |
| 3196 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3197 | return "TLSv1.2"; |
| 3198 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3199 | return "TLSv1.3"; |
| 3200 | default: |
| 3201 | return "unknown"; |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3206 | |
| 3207 | size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl) |
| 3208 | { |
| 3209 | const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3210 | size_t record_size_limit = max_len; |
| 3211 | |
| 3212 | if (ssl->session != NULL && |
| 3213 | ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3214 | ssl->session->record_size_limit < max_len) { |
| 3215 | record_size_limit = ssl->session->record_size_limit; |
| 3216 | } |
| 3217 | |
| 3218 | // TODO: this is currently untested |
| 3219 | /* During a handshake, use the value being negotiated */ |
| 3220 | if (ssl->session_negotiate != NULL && |
| 3221 | ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3222 | ssl->session_negotiate->record_size_limit < max_len) { |
| 3223 | record_size_limit = ssl->session_negotiate->record_size_limit; |
| 3224 | } |
| 3225 | |
| 3226 | return record_size_limit; |
| 3227 | } |
| 3228 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3229 | |
| 3230 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3231 | size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl) |
| 3232 | { |
| 3233 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3234 | size_t read_mfl; |
| 3235 | |
| 3236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3237 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 3238 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3239 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3240 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
| 3241 | } |
| 3242 | #endif |
| 3243 | |
| 3244 | /* Check if a smaller max length was negotiated */ |
| 3245 | if (ssl->session_out != NULL) { |
| 3246 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3247 | if (read_mfl < max_len) { |
| 3248 | max_len = read_mfl; |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | /* During a handshake, use the value being negotiated */ |
| 3253 | if (ssl->session_negotiate != NULL) { |
| 3254 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3255 | if (read_mfl < max_len) { |
| 3256 | max_len = read_mfl; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | return max_len; |
| 3261 | } |
| 3262 | |
| 3263 | size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl) |
| 3264 | { |
| 3265 | size_t max_len; |
| 3266 | |
| 3267 | /* |
| 3268 | * Assume mfl_code is correct since it was checked when set |
| 3269 | */ |
| 3270 | max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code); |
| 3271 | |
| 3272 | /* Check if a smaller max length was negotiated */ |
| 3273 | if (ssl->session_out != NULL && |
| 3274 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3275 | max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3276 | } |
| 3277 | |
| 3278 | /* During a handshake, use the value being negotiated */ |
| 3279 | if (ssl->session_negotiate != NULL && |
| 3280 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3281 | max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3282 | } |
| 3283 | |
| 3284 | return max_len; |
| 3285 | } |
| 3286 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3287 | |
| 3288 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3289 | size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl) |
| 3290 | { |
| 3291 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3292 | return ssl->mtu; |
| 3293 | } |
| 3294 | |
| 3295 | if (ssl->mtu == 0) { |
| 3296 | return ssl->handshake->mtu; |
| 3297 | } |
| 3298 | |
| 3299 | return ssl->mtu < ssl->handshake->mtu ? |
| 3300 | ssl->mtu : ssl->handshake->mtu; |
| 3301 | } |
| 3302 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3303 | |
| 3304 | int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl) |
| 3305 | { |
| 3306 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3307 | |
| 3308 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3309 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \ |
| 3310 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3311 | (void) ssl; |
| 3312 | #endif |
| 3313 | |
| 3314 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3315 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
| 3316 | |
| 3317 | if (max_len > mfl) { |
| 3318 | max_len = mfl; |
| 3319 | } |
| 3320 | #endif |
| 3321 | |
| 3322 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3323 | const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl); |
| 3324 | |
| 3325 | if (max_len > record_size_limit) { |
| 3326 | max_len = record_size_limit; |
| 3327 | } |
| 3328 | #endif |
| 3329 | |
| 3330 | if (ssl->transform_out != NULL && |
| 3331 | ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 3332 | /* |
| 3333 | * In TLS 1.3 case, when records are protected, `max_len` as computed |
| 3334 | * above is the maximum length of the TLSInnerPlaintext structure that |
| 3335 | * along the plaintext payload contains the inner content type (one byte) |
| 3336 | * and some zero padding. Given the algorithm used for padding |
| 3337 | * in mbedtls_ssl_encrypt_buf(), compute the maximum length for |
| 3338 | * the plaintext payload. Round down to a multiple of |
| 3339 | * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and |
| 3340 | * subtract 1. |
| 3341 | */ |
| 3342 | max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) * |
| 3343 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1; |
| 3344 | } |
| 3345 | |
| 3346 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3347 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3348 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3349 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
| 3350 | const size_t overhead = (size_t) ret; |
| 3351 | |
| 3352 | if (ret < 0) { |
| 3353 | return ret; |
| 3354 | } |
| 3355 | |
| 3356 | if (mtu <= overhead) { |
| 3357 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3358 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3359 | } |
| 3360 | |
| 3361 | if (max_len > mtu - overhead) { |
| 3362 | max_len = mtu - overhead; |
| 3363 | } |
| 3364 | } |
| 3365 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3366 | |
| 3367 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3368 | !defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 3369 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3370 | ((void) ssl); |
| 3371 | #endif |
| 3372 | |
| 3373 | return (int) max_len; |
| 3374 | } |
| 3375 | |
| 3376 | int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl) |
| 3377 | { |
| 3378 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3379 | |
| 3380 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3381 | (void) ssl; |
| 3382 | #endif |
| 3383 | |
| 3384 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3385 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
| 3386 | |
| 3387 | if (max_len > mfl) { |
| 3388 | max_len = mfl; |
| 3389 | } |
| 3390 | #endif |
| 3391 | |
| 3392 | return (int) max_len; |
| 3393 | } |
| 3394 | |
| 3395 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3396 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl) |
| 3397 | { |
| 3398 | if (ssl == NULL || ssl->session == NULL) { |
| 3399 | return NULL; |
| 3400 | } |
| 3401 | |
| 3402 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3403 | return ssl->session->peer_cert; |
| 3404 | #else |
| 3405 | return NULL; |
| 3406 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3407 | } |
| 3408 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3409 | |
| 3410 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3411 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3412 | mbedtls_ssl_session *dst) |
| 3413 | { |
| 3414 | int ret; |
| 3415 | |
| 3416 | if (ssl == NULL || |
| 3417 | dst == NULL || |
| 3418 | ssl->session == NULL || |
| 3419 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3420 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3421 | } |
| 3422 | |
| 3423 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3424 | * idempotent: Each session can only be exported once. |
| 3425 | * |
| 3426 | * (This is in preparation for TLS 1.3 support where we will |
| 3427 | * need the ability to export multiple sessions (aka tickets), |
| 3428 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3429 | * multiple times until it fails.) |
| 3430 | * |
| 3431 | * Check whether we have already exported the current session, |
| 3432 | * and fail if so. |
| 3433 | */ |
| 3434 | if (ssl->session->exported == 1) { |
| 3435 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3436 | } |
| 3437 | |
| 3438 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3439 | if (ret != 0) { |
| 3440 | return ret; |
| 3441 | } |
| 3442 | |
| 3443 | /* Remember that we've exported the session. */ |
| 3444 | ssl->session->exported = 1; |
| 3445 | return 0; |
| 3446 | } |
| 3447 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3448 | |
| 3449 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3450 | |
| 3451 | /* Serialization of TLS 1.2 sessions |
| 3452 | * |
| 3453 | * For more detail, see the description of ssl_session_save(). |
| 3454 | */ |
| 3455 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 3456 | unsigned char *buf, |
| 3457 | size_t buf_len) |
| 3458 | { |
| 3459 | unsigned char *p = buf; |
| 3460 | size_t used = 0; |
| 3461 | |
| 3462 | #if defined(MBEDTLS_HAVE_TIME) |
| 3463 | uint64_t start; |
| 3464 | #endif |
| 3465 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3466 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3467 | size_t cert_len; |
| 3468 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3469 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3470 | |
| 3471 | /* |
| 3472 | * Time |
| 3473 | */ |
| 3474 | #if defined(MBEDTLS_HAVE_TIME) |
| 3475 | used += 8; |
| 3476 | |
| 3477 | if (used <= buf_len) { |
| 3478 | start = (uint64_t) session->start; |
| 3479 | |
| 3480 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
| 3481 | p += 8; |
| 3482 | } |
| 3483 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3484 | |
| 3485 | /* |
| 3486 | * Basic mandatory fields |
| 3487 | */ |
| 3488 | used += 1 /* id_len */ |
| 3489 | + sizeof(session->id) |
| 3490 | + sizeof(session->master) |
| 3491 | + 4; /* verify_result */ |
| 3492 | |
| 3493 | if (used <= buf_len) { |
| 3494 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 3495 | memcpy(p, session->id, 32); |
| 3496 | p += 32; |
| 3497 | |
| 3498 | memcpy(p, session->master, 48); |
| 3499 | p += 48; |
| 3500 | |
| 3501 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
| 3502 | p += 4; |
| 3503 | } |
| 3504 | |
| 3505 | /* |
| 3506 | * Peer's end-entity certificate |
| 3507 | */ |
| 3508 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3509 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3510 | if (session->peer_cert == NULL) { |
| 3511 | cert_len = 0; |
| 3512 | } else { |
| 3513 | cert_len = session->peer_cert->raw.len; |
| 3514 | } |
| 3515 | |
| 3516 | used += 3 + cert_len; |
| 3517 | |
| 3518 | if (used <= buf_len) { |
| 3519 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 3520 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 3521 | *p++ = MBEDTLS_BYTE_0(cert_len); |
| 3522 | |
| 3523 | if (session->peer_cert != NULL) { |
| 3524 | memcpy(p, session->peer_cert->raw.p, cert_len); |
| 3525 | p += cert_len; |
| 3526 | } |
| 3527 | } |
| 3528 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3529 | if (session->peer_cert_digest != NULL) { |
| 3530 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 3531 | if (used <= buf_len) { |
| 3532 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 3533 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 3534 | memcpy(p, session->peer_cert_digest, |
| 3535 | session->peer_cert_digest_len); |
| 3536 | p += session->peer_cert_digest_len; |
| 3537 | } |
| 3538 | } else { |
| 3539 | used += 2; |
| 3540 | if (used <= buf_len) { |
| 3541 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 3542 | *p++ = 0; |
| 3543 | } |
| 3544 | } |
| 3545 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3546 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3547 | |
| 3548 | /* |
| 3549 | * Session ticket if any, plus associated data |
| 3550 | */ |
| 3551 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3552 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3553 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3554 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 3555 | |
| 3556 | if (used <= buf_len) { |
| 3557 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 3558 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 3559 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
| 3560 | |
| 3561 | if (session->ticket != NULL) { |
| 3562 | memcpy(p, session->ticket, session->ticket_len); |
| 3563 | p += session->ticket_len; |
| 3564 | } |
| 3565 | |
| 3566 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3567 | p += 4; |
| 3568 | } |
| 3569 | } |
| 3570 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3571 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3572 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3573 | used += 8; |
| 3574 | |
| 3575 | if (used <= buf_len) { |
| 3576 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3577 | p += 8; |
| 3578 | } |
| 3579 | } |
| 3580 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3581 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3582 | |
| 3583 | /* |
| 3584 | * Misc extension-related info |
| 3585 | */ |
| 3586 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3587 | used += 1; |
| 3588 | |
| 3589 | if (used <= buf_len) { |
| 3590 | *p++ = session->mfl_code; |
| 3591 | } |
| 3592 | #endif |
| 3593 | |
| 3594 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3595 | used += 1; |
| 3596 | |
| 3597 | if (used <= buf_len) { |
| 3598 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 3599 | } |
| 3600 | #endif |
| 3601 | |
| 3602 | return used; |
| 3603 | } |
| 3604 | |
| 3605 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3606 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 3607 | const unsigned char *buf, |
| 3608 | size_t len) |
| 3609 | { |
| 3610 | #if defined(MBEDTLS_HAVE_TIME) |
| 3611 | uint64_t start; |
| 3612 | #endif |
| 3613 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3614 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3615 | size_t cert_len; |
| 3616 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3617 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3618 | |
| 3619 | const unsigned char *p = buf; |
| 3620 | const unsigned char * const end = buf + len; |
| 3621 | |
| 3622 | /* |
| 3623 | * Time |
| 3624 | */ |
| 3625 | #if defined(MBEDTLS_HAVE_TIME) |
| 3626 | if (8 > (size_t) (end - p)) { |
| 3627 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3628 | } |
| 3629 | |
| 3630 | start = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3631 | p += 8; |
| 3632 | |
| 3633 | session->start = (mbedtls_time_t) start; |
| 3634 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3635 | |
| 3636 | /* |
| 3637 | * Basic mandatory fields |
| 3638 | */ |
| 3639 | if (1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 3640 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3641 | } |
| 3642 | |
| 3643 | session->id_len = *p++; |
| 3644 | memcpy(session->id, p, 32); |
| 3645 | p += 32; |
| 3646 | |
| 3647 | memcpy(session->master, p, 48); |
| 3648 | p += 48; |
| 3649 | |
| 3650 | session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3651 | p += 4; |
| 3652 | |
| 3653 | /* Immediately clear invalid pointer values that have been read, in case |
| 3654 | * we exit early before we replaced them with valid ones. */ |
| 3655 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3656 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3657 | session->peer_cert = NULL; |
| 3658 | #else |
| 3659 | session->peer_cert_digest = NULL; |
| 3660 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3661 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3662 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 3663 | session->ticket = NULL; |
| 3664 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 3665 | |
| 3666 | /* |
| 3667 | * Peer certificate |
| 3668 | */ |
| 3669 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3670 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3671 | /* Deserialize CRT from the end of the ticket. */ |
| 3672 | if (3 > (size_t) (end - p)) { |
| 3673 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3674 | } |
| 3675 | |
| 3676 | cert_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3677 | p += 3; |
| 3678 | |
| 3679 | if (cert_len != 0) { |
| 3680 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3681 | |
| 3682 | if (cert_len > (size_t) (end - p)) { |
| 3683 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3684 | } |
| 3685 | |
| 3686 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 3687 | |
| 3688 | if (session->peer_cert == NULL) { |
| 3689 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3690 | } |
| 3691 | |
| 3692 | mbedtls_x509_crt_init(session->peer_cert); |
| 3693 | |
| 3694 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 3695 | p, cert_len)) != 0) { |
| 3696 | mbedtls_x509_crt_free(session->peer_cert); |
| 3697 | mbedtls_free(session->peer_cert); |
| 3698 | session->peer_cert = NULL; |
| 3699 | return ret; |
| 3700 | } |
| 3701 | |
| 3702 | p += cert_len; |
| 3703 | } |
| 3704 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3705 | /* Deserialize CRT digest from the end of the ticket. */ |
| 3706 | if (2 > (size_t) (end - p)) { |
| 3707 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3708 | } |
| 3709 | |
| 3710 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 3711 | session->peer_cert_digest_len = (size_t) *p++; |
| 3712 | |
| 3713 | if (session->peer_cert_digest_len != 0) { |
| 3714 | const mbedtls_md_info_t *md_info = |
| 3715 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 3716 | if (md_info == NULL) { |
| 3717 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3718 | } |
| 3719 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 3720 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3721 | } |
| 3722 | |
| 3723 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 3724 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3725 | } |
| 3726 | |
| 3727 | session->peer_cert_digest = |
| 3728 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 3729 | if (session->peer_cert_digest == NULL) { |
| 3730 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3731 | } |
| 3732 | |
| 3733 | memcpy(session->peer_cert_digest, p, |
| 3734 | session->peer_cert_digest_len); |
| 3735 | p += session->peer_cert_digest_len; |
| 3736 | } |
| 3737 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3738 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3739 | |
| 3740 | /* |
| 3741 | * Session ticket and associated data |
| 3742 | */ |
| 3743 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3744 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3745 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3746 | if (3 > (size_t) (end - p)) { |
| 3747 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3748 | } |
| 3749 | |
| 3750 | session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3751 | p += 3; |
| 3752 | |
| 3753 | if (session->ticket_len != 0) { |
| 3754 | if (session->ticket_len > (size_t) (end - p)) { |
| 3755 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3756 | } |
| 3757 | |
| 3758 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 3759 | if (session->ticket == NULL) { |
| 3760 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3761 | } |
| 3762 | |
| 3763 | memcpy(session->ticket, p, session->ticket_len); |
| 3764 | p += session->ticket_len; |
| 3765 | } |
| 3766 | |
| 3767 | if (4 > (size_t) (end - p)) { |
| 3768 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3769 | } |
| 3770 | |
| 3771 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3772 | p += 4; |
| 3773 | } |
| 3774 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3775 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3776 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3777 | if (8 > (size_t) (end - p)) { |
| 3778 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3779 | } |
| 3780 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3781 | p += 8; |
| 3782 | } |
| 3783 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3784 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3785 | |
| 3786 | /* |
| 3787 | * Misc extension-related info |
| 3788 | */ |
| 3789 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3790 | if (1 > (size_t) (end - p)) { |
| 3791 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3792 | } |
| 3793 | |
| 3794 | session->mfl_code = *p++; |
| 3795 | #endif |
| 3796 | |
| 3797 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3798 | if (1 > (size_t) (end - p)) { |
| 3799 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3800 | } |
| 3801 | |
| 3802 | session->encrypt_then_mac = *p++; |
| 3803 | #endif |
| 3804 | |
| 3805 | /* Done, should have consumed entire buffer */ |
| 3806 | if (p != end) { |
| 3807 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3808 | } |
| 3809 | |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3814 | |
| 3815 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3816 | /* Serialization of TLS 1.3 sessions: |
| 3817 | * |
| 3818 | * For more detail, see the description of ssl_session_save(). |
| 3819 | */ |
| 3820 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3821 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3822 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 3823 | unsigned char *buf, |
| 3824 | size_t buf_len, |
| 3825 | size_t *olen) |
| 3826 | { |
| 3827 | unsigned char *p = buf; |
| 3828 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3829 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3830 | size_t hostname_len = (session->hostname == NULL) ? |
| 3831 | 0 : strlen(session->hostname) + 1; |
| 3832 | #endif |
| 3833 | |
| 3834 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 3835 | defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
| 3836 | const size_t alpn_len = (session->ticket_alpn == NULL) ? |
| 3837 | 0 : strlen(session->ticket_alpn) + 1; |
| 3838 | #endif |
| 3839 | size_t needed = 4 /* ticket_age_add */ |
| 3840 | + 1 /* ticket_flags */ |
| 3841 | + 1; /* resumption_key length */ |
| 3842 | |
| 3843 | *olen = 0; |
| 3844 | |
| 3845 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 3846 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3847 | } |
| 3848 | needed += session->resumption_key_len; /* resumption_key */ |
| 3849 | |
| 3850 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3851 | needed += 4; /* max_early_data_size */ |
| 3852 | #endif |
| 3853 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3854 | needed += 2; /* record_size_limit */ |
| 3855 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3856 | |
| 3857 | #if defined(MBEDTLS_HAVE_TIME) |
| 3858 | needed += 8; /* ticket_creation_time or ticket_reception_time */ |
| 3859 | #endif |
| 3860 | |
| 3861 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3862 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3863 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
| 3864 | needed += 2 /* alpn_len */ |
| 3865 | + alpn_len; /* alpn */ |
| 3866 | #endif |
| 3867 | } |
| 3868 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3869 | |
| 3870 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3871 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3872 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3873 | needed += 2 /* hostname_len */ |
| 3874 | + hostname_len; /* hostname */ |
| 3875 | #endif |
| 3876 | |
| 3877 | needed += 4 /* ticket_lifetime */ |
| 3878 | + 2; /* ticket_len */ |
| 3879 | |
| 3880 | /* Check size_t overflow */ |
| 3881 | if (session->ticket_len > SIZE_MAX - needed) { |
| 3882 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3883 | } |
| 3884 | |
| 3885 | needed += session->ticket_len; /* ticket */ |
| 3886 | } |
| 3887 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3888 | |
| 3889 | *olen = needed; |
| 3890 | if (needed > buf_len) { |
| 3891 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3892 | } |
| 3893 | |
| 3894 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0); |
| 3895 | p[4] = session->ticket_flags; |
| 3896 | |
| 3897 | /* save resumption_key */ |
| 3898 | p[5] = session->resumption_key_len; |
| 3899 | p += 6; |
| 3900 | memcpy(p, session->resumption_key, session->resumption_key_len); |
| 3901 | p += session->resumption_key_len; |
| 3902 | |
| 3903 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3904 | MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0); |
| 3905 | p += 4; |
| 3906 | #endif |
| 3907 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3908 | MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0); |
| 3909 | p += 2; |
| 3910 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3911 | |
| 3912 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3913 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3914 | #if defined(MBEDTLS_HAVE_TIME) |
| 3915 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3916 | p += 8; |
| 3917 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3918 | |
| 3919 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
| 3920 | MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0); |
| 3921 | p += 2; |
| 3922 | |
| 3923 | if (alpn_len > 0) { |
| 3924 | /* save chosen alpn */ |
| 3925 | memcpy(p, session->ticket_alpn, alpn_len); |
| 3926 | p += alpn_len; |
| 3927 | } |
| 3928 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3929 | } |
| 3930 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3931 | |
| 3932 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3933 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3934 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3935 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
| 3936 | p += 2; |
| 3937 | if (hostname_len > 0) { |
| 3938 | /* save host name */ |
| 3939 | memcpy(p, session->hostname, hostname_len); |
| 3940 | p += hostname_len; |
| 3941 | } |
| 3942 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3943 | |
| 3944 | #if defined(MBEDTLS_HAVE_TIME) |
| 3945 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0); |
| 3946 | p += 8; |
| 3947 | #endif |
| 3948 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3949 | p += 4; |
| 3950 | |
| 3951 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
| 3952 | p += 2; |
| 3953 | |
| 3954 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 3955 | memcpy(p, session->ticket, session->ticket_len); |
| 3956 | p += session->ticket_len; |
| 3957 | } |
| 3958 | } |
| 3959 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3960 | return 0; |
| 3961 | } |
| 3962 | |
| 3963 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3964 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 3965 | const unsigned char *buf, |
| 3966 | size_t len) |
| 3967 | { |
| 3968 | const unsigned char *p = buf; |
| 3969 | const unsigned char *end = buf + len; |
| 3970 | |
| 3971 | if (end - p < 6) { |
| 3972 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3973 | } |
| 3974 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3975 | session->ticket_flags = p[4]; |
| 3976 | |
| 3977 | /* load resumption_key */ |
| 3978 | session->resumption_key_len = p[5]; |
| 3979 | p += 6; |
| 3980 | |
| 3981 | if (end - p < session->resumption_key_len) { |
| 3982 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3983 | } |
| 3984 | |
| 3985 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 3986 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3987 | } |
| 3988 | memcpy(session->resumption_key, p, session->resumption_key_len); |
| 3989 | p += session->resumption_key_len; |
| 3990 | |
| 3991 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3992 | if (end - p < 4) { |
| 3993 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3994 | } |
| 3995 | session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3996 | p += 4; |
| 3997 | #endif |
| 3998 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3999 | if (end - p < 2) { |
| 4000 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4001 | } |
| 4002 | session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4003 | p += 2; |
| 4004 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4005 | |
| 4006 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4007 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4008 | #if defined(MBEDTLS_HAVE_TIME) |
| 4009 | if (end - p < 8) { |
| 4010 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4011 | } |
| 4012 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4013 | p += 8; |
| 4014 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4015 | |
| 4016 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
| 4017 | size_t alpn_len; |
| 4018 | |
| 4019 | if (end - p < 2) { |
| 4020 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4021 | } |
| 4022 | |
| 4023 | alpn_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4024 | p += 2; |
| 4025 | |
| 4026 | if (end - p < (long int) alpn_len) { |
| 4027 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4028 | } |
| 4029 | |
| 4030 | if (alpn_len > 0) { |
| 4031 | int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p); |
| 4032 | if (ret != 0) { |
| 4033 | return ret; |
| 4034 | } |
| 4035 | p += alpn_len; |
| 4036 | } |
| 4037 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 4038 | } |
| 4039 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 4040 | |
| 4041 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4042 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4043 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4044 | size_t hostname_len; |
| 4045 | /* load host name */ |
| 4046 | if (end - p < 2) { |
| 4047 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4048 | } |
| 4049 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4050 | p += 2; |
| 4051 | |
| 4052 | if (end - p < (long int) hostname_len) { |
| 4053 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4054 | } |
| 4055 | if (hostname_len > 0) { |
| 4056 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 4057 | if (session->hostname == NULL) { |
| 4058 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4059 | } |
| 4060 | memcpy(session->hostname, p, hostname_len); |
| 4061 | p += hostname_len; |
| 4062 | } |
| 4063 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4064 | |
| 4065 | #if defined(MBEDTLS_HAVE_TIME) |
| 4066 | if (end - p < 8) { |
| 4067 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4068 | } |
| 4069 | session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4070 | p += 8; |
| 4071 | #endif |
| 4072 | if (end - p < 4) { |
| 4073 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4074 | } |
| 4075 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 4076 | p += 4; |
| 4077 | |
| 4078 | if (end - p < 2) { |
| 4079 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4080 | } |
| 4081 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4082 | p += 2; |
| 4083 | |
| 4084 | if (end - p < (long int) session->ticket_len) { |
| 4085 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4086 | } |
| 4087 | if (session->ticket_len > 0) { |
| 4088 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 4089 | if (session->ticket == NULL) { |
| 4090 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4091 | } |
| 4092 | memcpy(session->ticket, p, session->ticket_len); |
| 4093 | p += session->ticket_len; |
| 4094 | } |
| 4095 | } |
| 4096 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4097 | |
| 4098 | return 0; |
| 4099 | |
| 4100 | } |
| 4101 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4102 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4103 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 4104 | unsigned char *buf, |
| 4105 | size_t buf_len, |
| 4106 | size_t *olen) |
| 4107 | { |
| 4108 | ((void) session); |
| 4109 | ((void) buf); |
| 4110 | ((void) buf_len); |
| 4111 | *olen = 0; |
| 4112 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4113 | } |
| 4114 | |
| 4115 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
| 4116 | const unsigned char *buf, |
| 4117 | size_t buf_len) |
| 4118 | { |
| 4119 | ((void) session); |
| 4120 | ((void) buf); |
| 4121 | ((void) buf_len); |
| 4122 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4123 | } |
| 4124 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
| 4125 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4126 | |
| 4127 | /* |
| 4128 | * Define ticket header determining Mbed TLS version |
| 4129 | * and structure of the ticket. |
| 4130 | */ |
| 4131 | |
| 4132 | /* |
| 4133 | * Define bitflag determining compile-time settings influencing |
| 4134 | * structure of serialized SSL sessions. |
| 4135 | */ |
| 4136 | |
| 4137 | #if defined(MBEDTLS_HAVE_TIME) |
| 4138 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
| 4139 | #else |
| 4140 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
| 4141 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4142 | |
| 4143 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4144 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
| 4145 | #else |
| 4146 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
| 4147 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4148 | |
| 4149 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4150 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1 |
| 4151 | #else |
| 4152 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0 |
| 4153 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4154 | |
| 4155 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4156 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
| 4157 | #else |
| 4158 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
| 4159 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4160 | |
| 4161 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4162 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
| 4163 | #else |
| 4164 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
| 4165 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4166 | |
| 4167 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4168 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
| 4169 | #else |
| 4170 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
| 4171 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4172 | |
| 4173 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4174 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4175 | #else |
| 4176 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4177 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4178 | |
| 4179 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4180 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 1 |
| 4181 | #else |
| 4182 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 0 |
| 4183 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4184 | |
| 4185 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4186 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1 |
| 4187 | #else |
| 4188 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0 |
| 4189 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 4190 | |
| 4191 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4192 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1 |
| 4193 | #else |
| 4194 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0 |
| 4195 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4196 | |
| 4197 | #if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \ |
| 4198 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 4199 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1 |
| 4200 | #else |
| 4201 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0 |
| 4202 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4203 | |
| 4204 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4205 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4206 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4207 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 4208 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4209 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
| 4210 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6 |
| 4211 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7 |
| 4212 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8 |
| 4213 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9 |
| 4214 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10 |
| 4215 | |
| 4216 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
| 4217 | ((uint16_t) ( \ |
| 4218 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 4219 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 4220 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 4221 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 4222 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 4223 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
| 4224 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \ |
| 4225 | (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \ |
| 4226 | SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \ |
| 4227 | (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \ |
| 4228 | (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \ |
| 4229 | SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \ |
| 4230 | (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \ |
| 4231 | SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \ |
| 4232 | (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \ |
| 4233 | SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT))) |
| 4234 | |
| 4235 | static const unsigned char ssl_serialized_session_header[] = { |
| 4236 | MBEDTLS_VERSION_MAJOR, |
| 4237 | MBEDTLS_VERSION_MINOR, |
| 4238 | MBEDTLS_VERSION_PATCH, |
| 4239 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4240 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4241 | }; |
| 4242 | |
| 4243 | /* |
| 4244 | * Serialize a session in the following format: |
| 4245 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4246 | * |
| 4247 | * TLS 1.2 session: |
| 4248 | * |
| 4249 | * struct { |
| 4250 | * #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4251 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4252 | * uint32 ticket_lifetime; |
| 4253 | * #endif |
| 4254 | * } ClientOnlyData; |
| 4255 | * |
| 4256 | * struct { |
| 4257 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4258 | * uint64 start_time; |
| 4259 | * #endif |
| 4260 | * uint8 session_id_len; // at most 32 |
| 4261 | * opaque session_id[32]; |
| 4262 | * opaque master[48]; // fixed length in the standard |
| 4263 | * uint32 verify_result; |
| 4264 | * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE |
| 4265 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4266 | * #else |
| 4267 | * uint8 peer_cert_digest_type; |
| 4268 | * opaque peer_cert_digest<0..2^8-1> |
| 4269 | * #endif |
| 4270 | * select (endpoint) { |
| 4271 | * case client: ClientOnlyData; |
| 4272 | * case server: uint64 ticket_creation_time; |
| 4273 | * }; |
| 4274 | * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4275 | * uint8 mfl_code; // up to 255 according to standard |
| 4276 | * #endif |
| 4277 | * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4278 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4279 | * #endif |
| 4280 | * } serialized_session_tls12; |
| 4281 | * |
| 4282 | * |
| 4283 | * TLS 1.3 Session: |
| 4284 | * |
| 4285 | * struct { |
| 4286 | * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4287 | * opaque hostname<0..2^16-1>; |
| 4288 | * #endif |
| 4289 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4290 | * uint64 ticket_reception_time; |
| 4291 | * #endif |
| 4292 | * uint32 ticket_lifetime; |
| 4293 | * opaque ticket<1..2^16-1>; |
| 4294 | * } ClientOnlyData; |
| 4295 | * |
| 4296 | * struct { |
| 4297 | * uint32 ticket_age_add; |
| 4298 | * uint8 ticket_flags; |
| 4299 | * opaque resumption_key<0..255>; |
| 4300 | * #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4301 | * uint32 max_early_data_size; |
| 4302 | * #endif |
| 4303 | * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4304 | * uint16 record_size_limit; |
| 4305 | * #endif |
| 4306 | * select ( endpoint ) { |
| 4307 | * case client: ClientOnlyData; |
| 4308 | * case server: |
| 4309 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4310 | * uint64 ticket_creation_time; |
| 4311 | * #endif |
| 4312 | * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
| 4313 | * opaque ticket_alpn<0..256>; |
| 4314 | * #endif |
| 4315 | * }; |
| 4316 | * } serialized_session_tls13; |
| 4317 | * |
| 4318 | * |
| 4319 | * SSL session: |
| 4320 | * |
| 4321 | * struct { |
| 4322 | * |
| 4323 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4324 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4325 | * // determining the format of the remaining |
| 4326 | * // serialized data. |
| 4327 | * |
| 4328 | * Note: When updating the format, remember to keep |
| 4329 | * these version+format bytes. |
| 4330 | * |
| 4331 | * // In this version, `session_format` determines |
| 4332 | * // the setting of those compile-time |
| 4333 | * // configuration options which influence |
| 4334 | * // the structure of mbedtls_ssl_session. |
| 4335 | * |
| 4336 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 4337 | * // - TLS 1.2 (0x0303) |
| 4338 | * // - TLS 1.3 (0x0304) |
| 4339 | * uint8_t endpoint; |
| 4340 | * uint16_t ciphersuite; |
| 4341 | * |
| 4342 | * select (serialized_session.tls_version) { |
| 4343 | * |
| 4344 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4345 | * serialized_session_tls12 data; |
| 4346 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4347 | * serialized_session_tls13 data; |
| 4348 | * |
| 4349 | * }; |
| 4350 | * |
| 4351 | * } serialized_session; |
| 4352 | * |
| 4353 | */ |
| 4354 | |
| 4355 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4356 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 4357 | unsigned char omit_header, |
| 4358 | unsigned char *buf, |
| 4359 | size_t buf_len, |
| 4360 | size_t *olen) |
| 4361 | { |
| 4362 | unsigned char *p = buf; |
| 4363 | size_t used = 0; |
| 4364 | size_t remaining_len; |
| 4365 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4366 | size_t out_len; |
| 4367 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4368 | #endif |
| 4369 | if (session == NULL) { |
| 4370 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4371 | } |
| 4372 | |
| 4373 | if (!omit_header) { |
| 4374 | /* |
| 4375 | * Add Mbed TLS version identifier |
| 4376 | */ |
| 4377 | used += sizeof(ssl_serialized_session_header); |
| 4378 | |
| 4379 | if (used <= buf_len) { |
| 4380 | memcpy(p, ssl_serialized_session_header, |
| 4381 | sizeof(ssl_serialized_session_header)); |
| 4382 | p += sizeof(ssl_serialized_session_header); |
| 4383 | } |
| 4384 | } |
| 4385 | |
| 4386 | /* |
| 4387 | * TLS version identifier, endpoint, ciphersuite |
| 4388 | */ |
| 4389 | used += 1 /* TLS version */ |
| 4390 | + 1 /* endpoint */ |
| 4391 | + 2; /* ciphersuite */ |
| 4392 | if (used <= buf_len) { |
| 4393 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
| 4394 | *p++ = session->endpoint; |
| 4395 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
| 4396 | p += 2; |
| 4397 | } |
| 4398 | |
| 4399 | /* Forward to version-specific serialization routine. */ |
| 4400 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
| 4401 | switch (session->tls_version) { |
| 4402 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4403 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4404 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 4405 | break; |
| 4406 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4407 | |
| 4408 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4409 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4410 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 4411 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4412 | return ret; |
| 4413 | } |
| 4414 | used += out_len; |
| 4415 | break; |
| 4416 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4417 | |
| 4418 | default: |
| 4419 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4420 | } |
| 4421 | |
| 4422 | *olen = used; |
| 4423 | if (used > buf_len) { |
| 4424 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4425 | } |
| 4426 | |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
| 4430 | /* |
| 4431 | * Public wrapper for ssl_session_save() |
| 4432 | */ |
| 4433 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 4434 | unsigned char *buf, |
| 4435 | size_t buf_len, |
| 4436 | size_t *olen) |
| 4437 | { |
| 4438 | return ssl_session_save(session, 0, buf, buf_len, olen); |
| 4439 | } |
| 4440 | |
| 4441 | /* |
| 4442 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 4443 | * |
| 4444 | * This internal version is wrapped by a public function that cleans up in |
| 4445 | * case of error, and has an extra option omit_header. |
| 4446 | */ |
| 4447 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4448 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 4449 | unsigned char omit_header, |
| 4450 | const unsigned char *buf, |
| 4451 | size_t len) |
| 4452 | { |
| 4453 | const unsigned char *p = buf; |
| 4454 | const unsigned char * const end = buf + len; |
| 4455 | size_t remaining_len; |
| 4456 | |
| 4457 | |
| 4458 | if (session == NULL) { |
| 4459 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4460 | } |
| 4461 | |
| 4462 | if (!omit_header) { |
| 4463 | /* |
| 4464 | * Check Mbed TLS version identifier |
| 4465 | */ |
| 4466 | |
| 4467 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 4468 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4469 | } |
| 4470 | |
| 4471 | if (memcmp(p, ssl_serialized_session_header, |
| 4472 | sizeof(ssl_serialized_session_header)) != 0) { |
| 4473 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4474 | } |
| 4475 | p += sizeof(ssl_serialized_session_header); |
| 4476 | } |
| 4477 | |
| 4478 | /* |
| 4479 | * TLS version identifier, endpoint, ciphersuite |
| 4480 | */ |
| 4481 | if (4 > (size_t) (end - p)) { |
| 4482 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4483 | } |
| 4484 | session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++); |
| 4485 | session->endpoint = *p++; |
| 4486 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4487 | p += 2; |
| 4488 | |
| 4489 | /* Dispatch according to TLS version. */ |
| 4490 | remaining_len = (size_t) (end - p); |
| 4491 | switch (session->tls_version) { |
| 4492 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4493 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4494 | return ssl_tls12_session_load(session, p, remaining_len); |
| 4495 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4496 | |
| 4497 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4498 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4499 | return ssl_tls13_session_load(session, p, remaining_len); |
| 4500 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4501 | |
| 4502 | default: |
| 4503 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | /* |
| 4508 | * Deserialize session: public wrapper for error cleaning |
| 4509 | */ |
| 4510 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 4511 | const unsigned char *buf, |
| 4512 | size_t len) |
| 4513 | { |
| 4514 | int ret = ssl_session_load(session, 0, buf, len); |
| 4515 | |
| 4516 | if (ret != 0) { |
| 4517 | mbedtls_ssl_session_free(session); |
| 4518 | } |
| 4519 | |
| 4520 | return ret; |
| 4521 | } |
| 4522 | |
| 4523 | /* |
| 4524 | * Perform a single step of the SSL handshake |
| 4525 | */ |
| 4526 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4527 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
| 4528 | { |
| 4529 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4530 | |
| 4531 | /* |
| 4532 | * We may have not been able to send to the peer all the handshake data |
| 4533 | * that were written into the output buffer by the previous handshake step, |
| 4534 | * if the write to the network callback returned with the |
| 4535 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 4536 | * We proceed to the next handshake step only when all data from the |
| 4537 | * previous one have been sent to the peer, thus we make sure that this is |
| 4538 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 4539 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 4540 | * we have to wait before to go ahead. |
| 4541 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 4542 | * peer. Data are only sent here and through |
| 4543 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
| 4544 | * alert occurred. |
| 4545 | */ |
| 4546 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 4547 | return ret; |
| 4548 | } |
| 4549 | |
| 4550 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4551 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4552 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 4553 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 4554 | return ret; |
| 4555 | } |
| 4556 | } |
| 4557 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4558 | |
| 4559 | return ret; |
| 4560 | } |
| 4561 | |
| 4562 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
| 4563 | { |
| 4564 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4565 | |
| 4566 | if (ssl == NULL || |
| 4567 | ssl->conf == NULL || |
| 4568 | ssl->handshake == NULL || |
| 4569 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4570 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4571 | } |
| 4572 | |
| 4573 | ret = ssl_prepare_handshake_step(ssl); |
| 4574 | if (ret != 0) { |
| 4575 | return ret; |
| 4576 | } |
| 4577 | |
| 4578 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 4579 | if (ret != 0) { |
| 4580 | goto cleanup; |
| 4581 | } |
| 4582 | |
| 4583 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 4584 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 4585 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4586 | |
| 4587 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4588 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4589 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
| 4590 | mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state))); |
| 4591 | |
| 4592 | switch (ssl->state) { |
| 4593 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4594 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); |
| 4595 | ret = 0; |
| 4596 | break; |
| 4597 | |
| 4598 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 4599 | ret = mbedtls_ssl_write_client_hello(ssl); |
| 4600 | break; |
| 4601 | |
| 4602 | default: |
| 4603 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4604 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4605 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 4606 | } else { |
| 4607 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4608 | } |
| 4609 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4610 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4611 | #else |
| 4612 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 4613 | #endif |
| 4614 | } |
| 4615 | } |
| 4616 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4617 | |
| 4618 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4619 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4620 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4621 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4622 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
| 4623 | } else { |
| 4624 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4625 | } |
| 4626 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4627 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4628 | #else |
| 4629 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
| 4630 | #endif |
| 4631 | } |
| 4632 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 4633 | |
| 4634 | if (ret != 0) { |
| 4635 | /* handshake_step return error. And it is same |
| 4636 | * with alert_reason. |
| 4637 | */ |
| 4638 | if (ssl->send_alert) { |
| 4639 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 4640 | goto cleanup; |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | cleanup: |
| 4645 | return ret; |
| 4646 | } |
| 4647 | |
| 4648 | /* |
| 4649 | * Perform the SSL handshake |
| 4650 | */ |
| 4651 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
| 4652 | { |
| 4653 | int ret = 0; |
| 4654 | |
| 4655 | /* Sanity checks */ |
| 4656 | |
| 4657 | if (ssl == NULL || ssl->conf == NULL) { |
| 4658 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4659 | } |
| 4660 | |
| 4661 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4662 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4663 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 4664 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 4665 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 4666 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4667 | } |
| 4668 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4669 | |
| 4670 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
| 4671 | |
| 4672 | /* Main handshake loop */ |
| 4673 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4674 | ret = mbedtls_ssl_handshake_step(ssl); |
| 4675 | |
| 4676 | if (ret != 0) { |
| 4677 | break; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
| 4682 | |
| 4683 | return ret; |
| 4684 | } |
| 4685 | |
| 4686 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4687 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4688 | /* |
| 4689 | * Write HelloRequest to request renegotiation on server |
| 4690 | */ |
| 4691 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4692 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
| 4693 | { |
| 4694 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4695 | |
| 4696 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
| 4697 | |
| 4698 | ssl->out_msglen = 4; |
| 4699 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4700 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
| 4701 | |
| 4702 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4703 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4704 | return ret; |
| 4705 | } |
| 4706 | |
| 4707 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
| 4708 | |
| 4709 | return 0; |
| 4710 | } |
| 4711 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 4712 | |
| 4713 | /* |
| 4714 | * Actually renegotiate current connection, triggered by either: |
| 4715 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 4716 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 4717 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after |
| 4718 | * the initial handshake is completed. |
| 4719 | * If the handshake doesn't complete due to waiting for I/O, it will continue |
| 4720 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. |
| 4721 | */ |
| 4722 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
| 4723 | { |
| 4724 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4725 | |
| 4726 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
| 4727 | |
| 4728 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 4729 | return ret; |
| 4730 | } |
| 4731 | |
| 4732 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 4733 | * the ServerHello will have message_seq = 1" */ |
| 4734 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4735 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4736 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4737 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4738 | ssl->handshake->out_msg_seq = 1; |
| 4739 | } else { |
| 4740 | ssl->handshake->in_msg_seq = 1; |
| 4741 | } |
| 4742 | } |
| 4743 | #endif |
| 4744 | |
| 4745 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST); |
| 4746 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
| 4747 | |
| 4748 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4749 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4750 | return ret; |
| 4751 | } |
| 4752 | |
| 4753 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
| 4754 | |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
| 4758 | /* |
| 4759 | * Renegotiate current connection on client, |
| 4760 | * or request renegotiation on server |
| 4761 | */ |
| 4762 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
| 4763 | { |
| 4764 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4765 | |
| 4766 | if (ssl == NULL || ssl->conf == NULL) { |
| 4767 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4768 | } |
| 4769 | |
| 4770 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4771 | /* On server, just send the request */ |
| 4772 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4773 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4774 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4775 | } |
| 4776 | |
| 4777 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 4778 | |
| 4779 | /* Did we already try/start sending HelloRequest? */ |
| 4780 | if (ssl->out_left != 0) { |
| 4781 | return mbedtls_ssl_flush_output(ssl); |
| 4782 | } |
| 4783 | |
| 4784 | return ssl_write_hello_request(ssl); |
| 4785 | } |
| 4786 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 4787 | |
| 4788 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4789 | /* |
| 4790 | * On client, either start the renegotiation process or, |
| 4791 | * if already in progress, continue the handshake |
| 4792 | */ |
| 4793 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 4794 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4795 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4796 | } |
| 4797 | |
| 4798 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 4799 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4800 | return ret; |
| 4801 | } |
| 4802 | } else { |
| 4803 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4804 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4805 | return ret; |
| 4806 | } |
| 4807 | } |
| 4808 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4809 | |
| 4810 | return ret; |
| 4811 | } |
| 4812 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 4813 | |
| 4814 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
| 4815 | { |
| 4816 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 4817 | |
| 4818 | if (handshake == NULL) { |
| 4819 | return; |
| 4820 | } |
| 4821 | |
| 4822 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 4823 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4824 | if (ssl->handshake->group_list_heap_allocated) { |
| 4825 | mbedtls_free((void *) handshake->group_list); |
| 4826 | } |
| 4827 | handshake->group_list = NULL; |
| 4828 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 4829 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 4830 | |
| 4831 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 4832 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4833 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4834 | mbedtls_free((void *) handshake->sig_algs); |
| 4835 | } |
| 4836 | handshake->sig_algs = NULL; |
| 4837 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 4838 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4839 | if (ssl->handshake->certificate_request_context) { |
| 4840 | mbedtls_free((void *) handshake->certificate_request_context); |
| 4841 | } |
| 4842 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4843 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 4844 | |
| 4845 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4846 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4847 | ssl->conf->f_async_cancel(ssl); |
| 4848 | handshake->async_in_progress = 0; |
| 4849 | } |
| 4850 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4851 | |
| 4852 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 4853 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4854 | psa_hash_abort(&handshake->fin_sha256_psa); |
| 4855 | #else |
| 4856 | mbedtls_md_free(&handshake->fin_sha256); |
| 4857 | #endif |
| 4858 | #endif |
| 4859 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 4860 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4861 | psa_hash_abort(&handshake->fin_sha384_psa); |
| 4862 | #else |
| 4863 | mbedtls_md_free(&handshake->fin_sha384); |
| 4864 | #endif |
| 4865 | #endif |
| 4866 | |
| 4867 | #if defined(MBEDTLS_DHM_C) |
| 4868 | mbedtls_dhm_free(&handshake->dhm_ctx); |
| 4869 | #endif |
| 4870 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 4871 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
| 4872 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
| 4873 | #endif |
| 4874 | |
| 4875 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 4876 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4877 | psa_pake_abort(&handshake->psa_pake_ctx); |
| 4878 | /* |
| 4879 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4880 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4881 | * the TLS library and should be destroyed at the same level |
| 4882 | */ |
| 4883 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4884 | psa_destroy_key(handshake->psa_pake_password); |
| 4885 | } |
| 4886 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4887 | #else |
| 4888 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
| 4889 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4890 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4891 | mbedtls_free(handshake->ecjpake_cache); |
| 4892 | handshake->ecjpake_cache = NULL; |
| 4893 | handshake->ecjpake_cache_len = 0; |
| 4894 | #endif |
| 4895 | #endif |
| 4896 | |
| 4897 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \ |
| 4898 | defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \ |
| 4899 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 4900 | /* explicit void pointer cast for buggy MS compiler */ |
| 4901 | mbedtls_free((void *) handshake->curves_tls_id); |
| 4902 | #endif |
| 4903 | |
| 4904 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
| 4905 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4906 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
| 4907 | /* The maintenance of the external PSK key slot is the |
| 4908 | * user's responsibility. */ |
| 4909 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4910 | psa_destroy_key(ssl->handshake->psk_opaque); |
| 4911 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4912 | } |
| 4913 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4914 | } |
| 4915 | #else |
| 4916 | if (handshake->psk != NULL) { |
| 4917 | mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len); |
| 4918 | } |
| 4919 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4920 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
| 4921 | |
| 4922 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4923 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4924 | /* |
| 4925 | * Free only the linked list wrapper, not the keys themselves |
| 4926 | * since the belong to the SNI callback |
| 4927 | */ |
| 4928 | ssl_key_cert_free(handshake->sni_key_cert); |
| 4929 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4930 | |
| 4931 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 4932 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4933 | if (handshake->ecrs_peer_cert != NULL) { |
| 4934 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4935 | mbedtls_free(handshake->ecrs_peer_cert); |
| 4936 | } |
| 4937 | #endif |
| 4938 | |
| 4939 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4940 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4941 | mbedtls_pk_free(&handshake->peer_pubkey); |
| 4942 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4943 | |
| 4944 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 4945 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4946 | mbedtls_free(handshake->cookie); |
| 4947 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4948 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
| 4949 | |
| 4950 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4951 | mbedtls_ssl_flight_free(handshake->flight); |
| 4952 | mbedtls_ssl_buffering_free(ssl); |
| 4953 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4954 | |
| 4955 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) |
| 4956 | if (handshake->xxdh_psa_privkey_is_external == 0) { |
| 4957 | psa_destroy_key(handshake->xxdh_psa_privkey); |
| 4958 | } |
| 4959 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */ |
| 4960 | |
| 4961 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4962 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4963 | mbedtls_free(handshake->transform_handshake); |
| 4964 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4965 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4966 | mbedtls_free(handshake->transform_earlydata); |
| 4967 | #endif |
| 4968 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4969 | |
| 4970 | |
| 4971 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4972 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4973 | * processes datagrams and the fact that a datagram is allowed to have |
| 4974 | * several records in it, it is possible that the I/O buffers are not |
| 4975 | * empty at this stage */ |
| 4976 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4977 | mbedtls_ssl_get_output_buflen(ssl)); |
| 4978 | #endif |
| 4979 | |
| 4980 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 4981 | mbedtls_platform_zeroize(handshake, |
| 4982 | sizeof(mbedtls_ssl_handshake_params)); |
| 4983 | } |
| 4984 | |
| 4985 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
| 4986 | { |
| 4987 | if (session == NULL) { |
| 4988 | return; |
| 4989 | } |
| 4990 | |
| 4991 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4992 | ssl_clear_peer_cert(session); |
| 4993 | #endif |
| 4994 | |
| 4995 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4996 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4997 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4998 | mbedtls_free(session->hostname); |
| 4999 | #endif |
| 5000 | mbedtls_free(session->ticket); |
| 5001 | #endif |
| 5002 | |
| 5003 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \ |
| 5004 | defined(MBEDTLS_SSL_SRV_C) |
| 5005 | mbedtls_free(session->ticket_alpn); |
| 5006 | #endif |
| 5007 | |
| 5008 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
| 5009 | |
| 5010 | /* Set verify_result to -1u to indicate 'result not available'. */ |
| 5011 | session->verify_result = 0xFFFFFFFF; |
| 5012 | } |
| 5013 | |
| 5014 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 5015 | |
| 5016 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5017 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5018 | #else |
| 5019 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5020 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5021 | |
| 5022 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 5023 | |
| 5024 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5025 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5026 | #else |
| 5027 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5028 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5029 | |
| 5030 | #if defined(MBEDTLS_SSL_ALPN) |
| 5031 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5032 | #else |
| 5033 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5034 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5035 | |
| 5036 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5037 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5038 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5039 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5040 | |
| 5041 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5042 | ((uint32_t) ( \ |
| 5043 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 5044 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 5045 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 5046 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 5047 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 5048 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 5049 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 5050 | 0u)) |
| 5051 | |
| 5052 | static const unsigned char ssl_serialized_context_header[] = { |
| 5053 | MBEDTLS_VERSION_MAJOR, |
| 5054 | MBEDTLS_VERSION_MINOR, |
| 5055 | MBEDTLS_VERSION_PATCH, |
| 5056 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5057 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5058 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5059 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5060 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5061 | }; |
| 5062 | |
| 5063 | /* |
| 5064 | * Serialize a full SSL context |
| 5065 | * |
| 5066 | * The format of the serialized data is: |
| 5067 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5068 | * |
| 5069 | * // header |
| 5070 | * opaque mbedtls_version[3]; // major, minor, patch |
| 5071 | * opaque context_format[5]; // version-specific field determining |
| 5072 | * // the format of the remaining |
| 5073 | * // serialized data. |
| 5074 | * Note: When updating the format, remember to keep these |
| 5075 | * version+format bytes. (We may make their size part of the API.) |
| 5076 | * |
| 5077 | * // session sub-structure |
| 5078 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5079 | * // transform sub-structure |
| 5080 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5081 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5082 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5083 | * // fields from ssl_context |
| 5084 | * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC |
| 5085 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5086 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5087 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5088 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5089 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5090 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5091 | * |
| 5092 | * Note that many fields of the ssl_context or sub-structures are not |
| 5093 | * serialized, as they fall in one of the following categories: |
| 5094 | * |
| 5095 | * 1. forced value (eg in_left must be 0) |
| 5096 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5097 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5098 | * 4. value was temporary (eg content of input buffer) |
| 5099 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
| 5100 | */ |
| 5101 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 5102 | unsigned char *buf, |
| 5103 | size_t buf_len, |
| 5104 | size_t *olen) |
| 5105 | { |
| 5106 | unsigned char *p = buf; |
| 5107 | size_t used = 0; |
| 5108 | size_t session_len; |
| 5109 | int ret = 0; |
| 5110 | |
| 5111 | /* |
| 5112 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5113 | * this function's documentation. |
| 5114 | * |
| 5115 | * These are due to assumptions/limitations in the implementation. Some of |
| 5116 | * them are likely to stay (no handshake in progress) some might go away |
| 5117 | * (only DTLS) but are currently used to simplify the implementation. |
| 5118 | */ |
| 5119 | /* The initial handshake must be over */ |
| 5120 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 5121 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 5122 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5123 | } |
| 5124 | if (ssl->handshake != NULL) { |
| 5125 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 5126 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5127 | } |
| 5128 | /* Double-check that sub-structures are indeed ready */ |
| 5129 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 5130 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 5131 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5132 | } |
| 5133 | /* There must be no pending incoming or outgoing data */ |
| 5134 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 5135 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 5136 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5137 | } |
| 5138 | if (ssl->out_left != 0) { |
| 5139 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 5140 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5141 | } |
| 5142 | /* Protocol must be DTLS, not TLS */ |
| 5143 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5144 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 5145 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5146 | } |
| 5147 | /* Version must be 1.2 */ |
| 5148 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 5149 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5150 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5151 | } |
| 5152 | /* We must be using an AEAD ciphersuite */ |
| 5153 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 5154 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 5155 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5156 | } |
| 5157 | /* Renegotiation must not be enabled */ |
| 5158 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5159 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5160 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 5161 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5162 | } |
| 5163 | #endif |
| 5164 | |
| 5165 | /* |
| 5166 | * Version and format identifier |
| 5167 | */ |
| 5168 | used += sizeof(ssl_serialized_context_header); |
| 5169 | |
| 5170 | if (used <= buf_len) { |
| 5171 | memcpy(p, ssl_serialized_context_header, |
| 5172 | sizeof(ssl_serialized_context_header)); |
| 5173 | p += sizeof(ssl_serialized_context_header); |
| 5174 | } |
| 5175 | |
| 5176 | /* |
| 5177 | * Session (length + data) |
| 5178 | */ |
| 5179 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 5180 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 5181 | return ret; |
| 5182 | } |
| 5183 | |
| 5184 | used += 4 + session_len; |
| 5185 | if (used <= buf_len) { |
| 5186 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
| 5187 | p += 4; |
| 5188 | |
| 5189 | ret = ssl_session_save(ssl->session, 1, |
| 5190 | p, session_len, &session_len); |
| 5191 | if (ret != 0) { |
| 5192 | return ret; |
| 5193 | } |
| 5194 | |
| 5195 | p += session_len; |
| 5196 | } |
| 5197 | |
| 5198 | /* |
| 5199 | * Transform |
| 5200 | */ |
| 5201 | used += sizeof(ssl->transform->randbytes); |
| 5202 | if (used <= buf_len) { |
| 5203 | memcpy(p, ssl->transform->randbytes, |
| 5204 | sizeof(ssl->transform->randbytes)); |
| 5205 | p += sizeof(ssl->transform->randbytes); |
| 5206 | } |
| 5207 | |
| 5208 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5209 | used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 5210 | if (used <= buf_len) { |
| 5211 | *p++ = ssl->transform->in_cid_len; |
| 5212 | memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len); |
| 5213 | p += ssl->transform->in_cid_len; |
| 5214 | |
| 5215 | *p++ = ssl->transform->out_cid_len; |
| 5216 | memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len); |
| 5217 | p += ssl->transform->out_cid_len; |
| 5218 | } |
| 5219 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5220 | |
| 5221 | /* |
| 5222 | * Saved fields from top-level ssl_context structure |
| 5223 | */ |
| 5224 | used += 4; |
| 5225 | if (used <= buf_len) { |
| 5226 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0); |
| 5227 | p += 4; |
| 5228 | } |
| 5229 | |
| 5230 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5231 | used += 16; |
| 5232 | if (used <= buf_len) { |
| 5233 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
| 5234 | p += 8; |
| 5235 | |
| 5236 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
| 5237 | p += 8; |
| 5238 | } |
| 5239 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5240 | |
| 5241 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5242 | used += 1; |
| 5243 | if (used <= buf_len) { |
| 5244 | *p++ = ssl->disable_datagram_packing; |
| 5245 | } |
| 5246 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5247 | |
| 5248 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
| 5249 | if (used <= buf_len) { |
| 5250 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
| 5251 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
| 5252 | } |
| 5253 | |
| 5254 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5255 | used += 2; |
| 5256 | if (used <= buf_len) { |
| 5257 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
| 5258 | p += 2; |
| 5259 | } |
| 5260 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5261 | |
| 5262 | #if defined(MBEDTLS_SSL_ALPN) |
| 5263 | { |
| 5264 | const uint8_t alpn_len = ssl->alpn_chosen |
| 5265 | ? (uint8_t) strlen(ssl->alpn_chosen) |
| 5266 | : 0; |
| 5267 | |
| 5268 | used += 1 + alpn_len; |
| 5269 | if (used <= buf_len) { |
| 5270 | *p++ = alpn_len; |
| 5271 | |
| 5272 | if (ssl->alpn_chosen != NULL) { |
| 5273 | memcpy(p, ssl->alpn_chosen, alpn_len); |
| 5274 | p += alpn_len; |
| 5275 | } |
| 5276 | } |
| 5277 | } |
| 5278 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5279 | |
| 5280 | /* |
| 5281 | * Done |
| 5282 | */ |
| 5283 | *olen = used; |
| 5284 | |
| 5285 | if (used > buf_len) { |
| 5286 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5287 | } |
| 5288 | |
| 5289 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
| 5290 | |
| 5291 | return mbedtls_ssl_session_reset_int(ssl, 0); |
| 5292 | } |
| 5293 | |
| 5294 | /* |
| 5295 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
| 5296 | * |
| 5297 | * This internal version is wrapped by a public function that cleans up in |
| 5298 | * case of error. |
| 5299 | */ |
| 5300 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 5301 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 5302 | const unsigned char *buf, |
| 5303 | size_t len) |
| 5304 | { |
| 5305 | const unsigned char *p = buf; |
| 5306 | const unsigned char * const end = buf + len; |
| 5307 | size_t session_len; |
| 5308 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5309 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5310 | tls_prf_fn prf_func = NULL; |
| 5311 | #endif |
| 5312 | |
| 5313 | /* |
| 5314 | * The context should have been freshly setup or reset. |
| 5315 | * Give the user an error in case of obvious misuse. |
| 5316 | * (Checking session is useful because it won't be NULL if we're |
| 5317 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5318 | */ |
| 5319 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 5320 | ssl->session != NULL) { |
| 5321 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5322 | } |
| 5323 | |
| 5324 | /* |
| 5325 | * We can't check that the config matches the initial one, but we can at |
| 5326 | * least check it matches the requirements for serializing. |
| 5327 | */ |
| 5328 | if ( |
| 5329 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5330 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5331 | #endif |
| 5332 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 5333 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 5334 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 |
| 5335 | ) { |
| 5336 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5337 | } |
| 5338 | |
| 5339 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
| 5340 | |
| 5341 | /* |
| 5342 | * Check version identifier |
| 5343 | */ |
| 5344 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 5345 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5346 | } |
| 5347 | |
| 5348 | if (memcmp(p, ssl_serialized_context_header, |
| 5349 | sizeof(ssl_serialized_context_header)) != 0) { |
| 5350 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 5351 | } |
| 5352 | p += sizeof(ssl_serialized_context_header); |
| 5353 | |
| 5354 | /* |
| 5355 | * Session |
| 5356 | */ |
| 5357 | if ((size_t) (end - p) < 4) { |
| 5358 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5359 | } |
| 5360 | |
| 5361 | session_len = MBEDTLS_GET_UINT32_BE(p, 0); |
| 5362 | p += 4; |
| 5363 | |
| 5364 | /* This has been allocated by ssl_handshake_init(), called by |
| 5365 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 5366 | ssl->session = ssl->session_negotiate; |
| 5367 | ssl->session_in = ssl->session; |
| 5368 | ssl->session_out = ssl->session; |
| 5369 | ssl->session_negotiate = NULL; |
| 5370 | |
| 5371 | if ((size_t) (end - p) < session_len) { |
| 5372 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5373 | } |
| 5374 | |
| 5375 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 5376 | if (ret != 0) { |
| 5377 | mbedtls_ssl_session_free(ssl->session); |
| 5378 | return ret; |
| 5379 | } |
| 5380 | |
| 5381 | p += session_len; |
| 5382 | |
| 5383 | /* |
| 5384 | * Transform |
| 5385 | */ |
| 5386 | |
| 5387 | /* This has been allocated by ssl_handshake_init(), called by |
| 5388 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 5389 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5390 | ssl->transform = ssl->transform_negotiate; |
| 5391 | ssl->transform_in = ssl->transform; |
| 5392 | ssl->transform_out = ssl->transform; |
| 5393 | ssl->transform_negotiate = NULL; |
| 5394 | #endif |
| 5395 | |
| 5396 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5397 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 5398 | if (prf_func == NULL) { |
| 5399 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5400 | } |
| 5401 | |
| 5402 | /* Read random bytes and populate structure */ |
| 5403 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 5404 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5405 | } |
| 5406 | |
| 5407 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 5408 | ssl->session->ciphersuite, |
| 5409 | ssl->session->master, |
| 5410 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 5411 | ssl->session->encrypt_then_mac, |
| 5412 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 5413 | prf_func, |
| 5414 | p, /* currently pointing to randbytes */ |
| 5415 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 5416 | ssl->conf->endpoint, |
| 5417 | ssl); |
| 5418 | if (ret != 0) { |
| 5419 | return ret; |
| 5420 | } |
| 5421 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5422 | p += sizeof(ssl->transform->randbytes); |
| 5423 | |
| 5424 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5425 | /* Read connection IDs and store them */ |
| 5426 | if ((size_t) (end - p) < 1) { |
| 5427 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5428 | } |
| 5429 | |
| 5430 | ssl->transform->in_cid_len = *p++; |
| 5431 | |
| 5432 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 5433 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5434 | } |
| 5435 | |
| 5436 | memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len); |
| 5437 | p += ssl->transform->in_cid_len; |
| 5438 | |
| 5439 | ssl->transform->out_cid_len = *p++; |
| 5440 | |
| 5441 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 5442 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5443 | } |
| 5444 | |
| 5445 | memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len); |
| 5446 | p += ssl->transform->out_cid_len; |
| 5447 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5448 | |
| 5449 | /* |
| 5450 | * Saved fields from top-level ssl_context structure |
| 5451 | */ |
| 5452 | if ((size_t) (end - p) < 4) { |
| 5453 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5454 | } |
| 5455 | |
| 5456 | ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0); |
| 5457 | p += 4; |
| 5458 | |
| 5459 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5460 | if ((size_t) (end - p) < 16) { |
| 5461 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5462 | } |
| 5463 | |
| 5464 | ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0); |
| 5465 | p += 8; |
| 5466 | |
| 5467 | ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0); |
| 5468 | p += 8; |
| 5469 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5470 | |
| 5471 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5472 | if ((size_t) (end - p) < 1) { |
| 5473 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5474 | } |
| 5475 | |
| 5476 | ssl->disable_datagram_packing = *p++; |
| 5477 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5478 | |
| 5479 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 5480 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5481 | } |
| 5482 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 5483 | p += sizeof(ssl->cur_out_ctr); |
| 5484 | |
| 5485 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5486 | if ((size_t) (end - p) < 2) { |
| 5487 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5488 | } |
| 5489 | |
| 5490 | ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0); |
| 5491 | p += 2; |
| 5492 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5493 | |
| 5494 | #if defined(MBEDTLS_SSL_ALPN) |
| 5495 | { |
| 5496 | uint8_t alpn_len; |
| 5497 | const char **cur; |
| 5498 | |
| 5499 | if ((size_t) (end - p) < 1) { |
| 5500 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5501 | } |
| 5502 | |
| 5503 | alpn_len = *p++; |
| 5504 | |
| 5505 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
| 5506 | /* alpn_chosen should point to an item in the configured list */ |
| 5507 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 5508 | if (strlen(*cur) == alpn_len && |
| 5509 | memcmp(p, *cur, alpn_len) == 0) { |
| 5510 | ssl->alpn_chosen = *cur; |
| 5511 | break; |
| 5512 | } |
| 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | /* can only happen on conf mismatch */ |
| 5517 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 5518 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5519 | } |
| 5520 | |
| 5521 | p += alpn_len; |
| 5522 | } |
| 5523 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5524 | |
| 5525 | /* |
| 5526 | * Forced fields from top-level ssl_context structure |
| 5527 | * |
| 5528 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 5529 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 5530 | */ |
| 5531 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); |
| 5532 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5533 | |
| 5534 | /* Adjust pointers for header fields of outgoing records to |
| 5535 | * the given transform, accounting for explicit IV and CID. */ |
| 5536 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
| 5537 | |
| 5538 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5539 | ssl->in_epoch = 1; |
| 5540 | #endif |
| 5541 | |
| 5542 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 5543 | * which we don't want - otherwise we'd end up freeing the wrong transform |
| 5544 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 5545 | * inappropriately. */ |
| 5546 | if (ssl->handshake != NULL) { |
| 5547 | mbedtls_ssl_handshake_free(ssl); |
| 5548 | mbedtls_free(ssl->handshake); |
| 5549 | ssl->handshake = NULL; |
| 5550 | } |
| 5551 | |
| 5552 | /* |
| 5553 | * Done - should have consumed entire buffer |
| 5554 | */ |
| 5555 | if (p != end) { |
| 5556 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5557 | } |
| 5558 | |
| 5559 | return 0; |
| 5560 | } |
| 5561 | |
| 5562 | /* |
| 5563 | * Deserialize context: public wrapper for error cleaning |
| 5564 | */ |
| 5565 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 5566 | const unsigned char *buf, |
| 5567 | size_t len) |
| 5568 | { |
| 5569 | int ret = ssl_context_load(context, buf, len); |
| 5570 | |
| 5571 | if (ret != 0) { |
| 5572 | mbedtls_ssl_free(context); |
| 5573 | } |
| 5574 | |
| 5575 | return ret; |
| 5576 | } |
| 5577 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 5578 | |
| 5579 | /* |
| 5580 | * Free an SSL context |
| 5581 | */ |
| 5582 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
| 5583 | { |
| 5584 | if (ssl == NULL) { |
| 5585 | return; |
| 5586 | } |
| 5587 | |
| 5588 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
| 5589 | |
| 5590 | if (ssl->out_buf != NULL) { |
| 5591 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5592 | size_t out_buf_len = ssl->out_buf_len; |
| 5593 | #else |
| 5594 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 5595 | #endif |
| 5596 | |
| 5597 | mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len); |
| 5598 | ssl->out_buf = NULL; |
| 5599 | } |
| 5600 | |
| 5601 | if (ssl->in_buf != NULL) { |
| 5602 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5603 | size_t in_buf_len = ssl->in_buf_len; |
| 5604 | #else |
| 5605 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 5606 | #endif |
| 5607 | |
| 5608 | mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len); |
| 5609 | ssl->in_buf = NULL; |
| 5610 | } |
| 5611 | |
| 5612 | if (ssl->transform) { |
| 5613 | mbedtls_ssl_transform_free(ssl->transform); |
| 5614 | mbedtls_free(ssl->transform); |
| 5615 | } |
| 5616 | |
| 5617 | if (ssl->handshake) { |
| 5618 | mbedtls_ssl_handshake_free(ssl); |
| 5619 | mbedtls_free(ssl->handshake); |
| 5620 | |
| 5621 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5622 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 5623 | mbedtls_free(ssl->transform_negotiate); |
| 5624 | #endif |
| 5625 | |
| 5626 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 5627 | mbedtls_free(ssl->session_negotiate); |
| 5628 | } |
| 5629 | |
| 5630 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5631 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 5632 | mbedtls_free(ssl->transform_application); |
| 5633 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5634 | |
| 5635 | if (ssl->session) { |
| 5636 | mbedtls_ssl_session_free(ssl->session); |
| 5637 | mbedtls_free(ssl->session); |
| 5638 | } |
| 5639 | |
| 5640 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5641 | mbedtls_ssl_free_hostname(ssl); |
| 5642 | #endif |
| 5643 | |
| 5644 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 5645 | mbedtls_free(ssl->cli_id); |
| 5646 | #endif |
| 5647 | |
| 5648 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
| 5649 | |
| 5650 | /* Actually clear after last debug message */ |
| 5651 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
| 5652 | } |
| 5653 | |
| 5654 | /* |
| 5655 | * Initialize mbedtls_ssl_config |
| 5656 | */ |
| 5657 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
| 5658 | { |
| 5659 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
| 5660 | } |
| 5661 | |
| 5662 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 5663 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
| 5664 | * curves with a lower resource usage come first. |
| 5665 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 5666 | * about this list. |
| 5667 | */ |
| 5668 | static const uint16_t ssl_preset_default_groups[] = { |
| 5669 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) |
| 5670 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
| 5671 | #endif |
| 5672 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
| 5673 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5674 | #endif |
| 5675 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
| 5676 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
| 5677 | #endif |
| 5678 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) |
| 5679 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
| 5680 | #endif |
| 5681 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) |
| 5682 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
| 5683 | #endif |
| 5684 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) |
| 5685 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
| 5686 | #endif |
| 5687 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) |
| 5688 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
| 5689 | #endif |
| 5690 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) |
| 5691 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
| 5692 | #endif |
| 5693 | #if defined(PSA_WANT_ALG_FFDH) |
| 5694 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, |
| 5695 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, |
| 5696 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, |
| 5697 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, |
| 5698 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, |
| 5699 | #endif |
| 5700 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
| 5701 | }; |
| 5702 | |
| 5703 | static const int ssl_preset_suiteb_ciphersuites[] = { |
| 5704 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 5705 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 5706 | 0 |
| 5707 | }; |
| 5708 | |
| 5709 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 5710 | |
| 5711 | /* NOTICE: |
| 5712 | * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following |
| 5713 | * rules SHOULD be upheld. |
| 5714 | * - No duplicate entries. |
| 5715 | * - But if there is a good reason, do not change the order of the algorithms. |
| 5716 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
| 5717 | * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes. |
| 5718 | */ |
| 5719 | static const uint16_t ssl_preset_default_sig_algs[] = { |
| 5720 | |
| 5721 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
| 5722 | defined(MBEDTLS_MD_CAN_SHA256) && \ |
| 5723 | defined(PSA_WANT_ECC_SECP_R1_256) |
| 5724 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 5725 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5726 | #endif |
| 5727 | |
| 5728 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
| 5729 | defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 5730 | defined(PSA_WANT_ECC_SECP_R1_384) |
| 5731 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 5732 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5733 | #endif |
| 5734 | |
| 5735 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
| 5736 | defined(MBEDTLS_MD_CAN_SHA512) && \ |
| 5737 | defined(PSA_WANT_ECC_SECP_R1_521) |
| 5738 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 5739 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512) |
| 5740 | #endif |
| 5741 | |
| 5742 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512) |
| 5743 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 5744 | #endif |
| 5745 | |
| 5746 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384) |
| 5747 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 5748 | #endif |
| 5749 | |
| 5750 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256) |
| 5751 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 5752 | #endif |
| 5753 | |
| 5754 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512) |
| 5755 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 5756 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */ |
| 5757 | |
| 5758 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384) |
| 5759 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 5760 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */ |
| 5761 | |
| 5762 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) |
| 5763 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 5764 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */ |
| 5765 | |
| 5766 | MBEDTLS_TLS_SIG_NONE |
| 5767 | }; |
| 5768 | |
| 5769 | /* NOTICE: see above */ |
| 5770 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5771 | static const uint16_t ssl_tls12_preset_default_sig_algs[] = { |
| 5772 | |
| 5773 | #if defined(MBEDTLS_MD_CAN_SHA512) |
| 5774 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 5775 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), |
| 5776 | #endif |
| 5777 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5778 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 5779 | #endif |
| 5780 | #if defined(MBEDTLS_RSA_C) |
| 5781 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), |
| 5782 | #endif |
| 5783 | #endif /* MBEDTLS_MD_CAN_SHA512 */ |
| 5784 | |
| 5785 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 5786 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 5787 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), |
| 5788 | #endif |
| 5789 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5790 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 5791 | #endif |
| 5792 | #if defined(MBEDTLS_RSA_C) |
| 5793 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384), |
| 5794 | #endif |
| 5795 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 5796 | |
| 5797 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 5798 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 5799 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), |
| 5800 | #endif |
| 5801 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5802 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 5803 | #endif |
| 5804 | #if defined(MBEDTLS_RSA_C) |
| 5805 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256), |
| 5806 | #endif |
| 5807 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
| 5808 | |
| 5809 | MBEDTLS_TLS_SIG_NONE |
| 5810 | }; |
| 5811 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5812 | |
| 5813 | /* NOTICE: see above */ |
| 5814 | static const uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 5815 | |
| 5816 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
| 5817 | defined(MBEDTLS_MD_CAN_SHA256) && \ |
| 5818 | defined(MBEDTLS_ECP_HAVE_SECP256R1) |
| 5819 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 5820 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5821 | #endif |
| 5822 | |
| 5823 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
| 5824 | defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 5825 | defined(MBEDTLS_ECP_HAVE_SECP384R1) |
| 5826 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 5827 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5828 | #endif |
| 5829 | |
| 5830 | MBEDTLS_TLS_SIG_NONE |
| 5831 | }; |
| 5832 | |
| 5833 | /* NOTICE: see above */ |
| 5834 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5835 | static const uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
| 5836 | |
| 5837 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 5838 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 5839 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), |
| 5840 | #endif |
| 5841 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
| 5842 | |
| 5843 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 5844 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
| 5845 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), |
| 5846 | #endif |
| 5847 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 5848 | |
| 5849 | MBEDTLS_TLS_SIG_NONE |
| 5850 | }; |
| 5851 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5852 | |
| 5853 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 5854 | |
| 5855 | static const uint16_t ssl_preset_suiteb_groups[] = { |
| 5856 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
| 5857 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5858 | #endif |
| 5859 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
| 5860 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
| 5861 | #endif |
| 5862 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
| 5863 | }; |
| 5864 | |
| 5865 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 5866 | /* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs` |
| 5867 | * to make sure there are no duplicated signature algorithm entries. */ |
| 5868 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 5869 | static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs) |
| 5870 | { |
| 5871 | size_t i, j; |
| 5872 | int ret = 0; |
| 5873 | |
| 5874 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5875 | for (j = 0; j < i; j++) { |
| 5876 | if (sig_algs[i] != sig_algs[j]) { |
| 5877 | continue; |
| 5878 | } |
| 5879 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5880 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5881 | sig_algs[i], j, i); |
| 5882 | ret = -1; |
| 5883 | } |
| 5884 | } |
| 5885 | return ret; |
| 5886 | } |
| 5887 | |
| 5888 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 5889 | |
| 5890 | /* |
| 5891 | * Load default in mbedtls_ssl_config |
| 5892 | */ |
| 5893 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5894 | int endpoint, int transport, int preset) |
| 5895 | { |
| 5896 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 5897 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5898 | #endif |
| 5899 | |
| 5900 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 5901 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5902 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5903 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5904 | } |
| 5905 | |
| 5906 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5907 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5908 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5909 | } |
| 5910 | |
| 5911 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5912 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5913 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5914 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5915 | } |
| 5916 | |
| 5917 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5918 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5919 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5920 | } |
| 5921 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5922 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 5923 | |
| 5924 | /* Use the functions here so that they are covered in tests, |
| 5925 | * but otherwise access member directly for efficiency */ |
| 5926 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5927 | mbedtls_ssl_conf_transport(conf, transport); |
| 5928 | |
| 5929 | /* |
| 5930 | * Things that are common to all presets |
| 5931 | */ |
| 5932 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5933 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 5934 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5935 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5936 | mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED); |
| 5937 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5938 | /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message |
| 5939 | * handling is disabled by default in Mbed TLS 3.6.x for backward |
| 5940 | * compatibility with client applications developed using Mbed TLS 3.5 |
| 5941 | * or earlier with the default configuration. |
| 5942 | * |
| 5943 | * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was |
| 5944 | * disabled, and a Mbed TLS client with the default configuration would |
| 5945 | * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable |
| 5946 | * server. |
| 5947 | * |
| 5948 | * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus |
| 5949 | * an Mbed TLS client with the default configuration establishes a |
| 5950 | * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If |
| 5951 | * following the handshake the TLS 1.3 server sends NewSessionTicket |
| 5952 | * messages and the Mbed TLS client processes them, this results in |
| 5953 | * Mbed TLS high level APIs (mbedtls_ssl_read(), |
| 5954 | * mbedtls_ssl_handshake(), ...) to eventually return an |
| 5955 | * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code |
| 5956 | * (see the documentation of mbedtls_ssl_read() for more information on |
| 5957 | * that error code). Applications unaware of that TLS 1.3 specific non |
| 5958 | * fatal error code are then failing. |
| 5959 | */ |
| 5960 | mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets( |
| 5961 | conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED); |
| 5962 | #endif |
| 5963 | #endif |
| 5964 | } |
| 5965 | #endif |
| 5966 | |
| 5967 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5968 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5969 | #endif |
| 5970 | |
| 5971 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5972 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5973 | #endif |
| 5974 | |
| 5975 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 5976 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5977 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5978 | #endif |
| 5979 | |
| 5980 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5981 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5982 | #endif |
| 5983 | |
| 5984 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5985 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 5986 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
| 5987 | #endif |
| 5988 | |
| 5989 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5990 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5991 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5992 | #endif |
| 5993 | |
| 5994 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5995 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
| 5996 | memset(conf->renego_period, 0x00, 2); |
| 5997 | memset(conf->renego_period + 2, 0xFF, 6); |
| 5998 | #endif |
| 5999 | |
| 6000 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 6001 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 6002 | const unsigned char dhm_p[] = |
| 6003 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6004 | const unsigned char dhm_g[] = |
| 6005 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 6006 | |
| 6007 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 6008 | dhm_p, sizeof(dhm_p), |
| 6009 | dhm_g, sizeof(dhm_g))) != 0) { |
| 6010 | return ret; |
| 6011 | } |
| 6012 | } |
| 6013 | #endif |
| 6014 | |
| 6015 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6016 | |
| 6017 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 6018 | mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
| 6019 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6020 | mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE); |
| 6021 | #endif |
| 6022 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 6023 | |
| 6024 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6025 | mbedtls_ssl_conf_new_session_tickets( |
| 6026 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
| 6027 | #endif |
| 6028 | /* |
| 6029 | * Allow all TLS 1.3 key exchange modes by default. |
| 6030 | */ |
| 6031 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
| 6032 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 6033 | |
| 6034 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 6035 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6036 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6037 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6038 | #else |
| 6039 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6040 | #endif |
| 6041 | } else { |
| 6042 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6043 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6044 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 6045 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6046 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 6047 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 6048 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6049 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6050 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6051 | #else |
| 6052 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6053 | #endif |
| 6054 | } |
| 6055 | |
| 6056 | /* |
| 6057 | * Preset-specific defaults |
| 6058 | */ |
| 6059 | switch (preset) { |
| 6060 | /* |
| 6061 | * NSA Suite B |
| 6062 | */ |
| 6063 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6064 | |
| 6065 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
| 6066 | |
| 6067 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6068 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
| 6069 | #endif |
| 6070 | |
| 6071 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 6072 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6073 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 6074 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 6075 | } else |
| 6076 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6077 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
| 6078 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 6079 | |
| 6080 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6081 | conf->curve_list = NULL; |
| 6082 | #endif |
| 6083 | conf->group_list = ssl_preset_suiteb_groups; |
| 6084 | break; |
| 6085 | |
| 6086 | /* |
| 6087 | * Default |
| 6088 | */ |
| 6089 | default: |
| 6090 | |
| 6091 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
| 6092 | |
| 6093 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6094 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6095 | #endif |
| 6096 | |
| 6097 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 6098 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6099 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 6100 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 6101 | } else |
| 6102 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6103 | conf->sig_algs = ssl_preset_default_sig_algs; |
| 6104 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 6105 | |
| 6106 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6107 | conf->curve_list = NULL; |
| 6108 | #endif |
| 6109 | conf->group_list = ssl_preset_default_groups; |
| 6110 | |
| 6111 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6112 | conf->dhm_min_bitlen = 1024; |
| 6113 | #endif |
| 6114 | } |
| 6115 | |
| 6116 | return 0; |
| 6117 | } |
| 6118 | |
| 6119 | /* |
| 6120 | * Free mbedtls_ssl_config |
| 6121 | */ |
| 6122 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
| 6123 | { |
| 6124 | if (conf == NULL) { |
| 6125 | return; |
| 6126 | } |
| 6127 | |
| 6128 | #if defined(MBEDTLS_DHM_C) |
| 6129 | mbedtls_mpi_free(&conf->dhm_P); |
| 6130 | mbedtls_mpi_free(&conf->dhm_G); |
| 6131 | #endif |
| 6132 | |
| 6133 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
| 6134 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6135 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 6136 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 6137 | } |
| 6138 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6139 | if (conf->psk != NULL) { |
| 6140 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
| 6141 | conf->psk = NULL; |
| 6142 | conf->psk_len = 0; |
| 6143 | } |
| 6144 | |
| 6145 | if (conf->psk_identity != NULL) { |
| 6146 | mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len); |
| 6147 | conf->psk_identity = NULL; |
| 6148 | conf->psk_identity_len = 0; |
| 6149 | } |
| 6150 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
| 6151 | |
| 6152 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6153 | ssl_key_cert_free(conf->key_cert); |
| 6154 | #endif |
| 6155 | |
| 6156 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
| 6157 | } |
| 6158 | |
| 6159 | #if defined(MBEDTLS_PK_C) && \ |
| 6160 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)) |
| 6161 | /* |
| 6162 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
| 6163 | */ |
| 6164 | unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk) |
| 6165 | { |
| 6166 | #if defined(MBEDTLS_RSA_C) |
| 6167 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 6168 | return MBEDTLS_SSL_SIG_RSA; |
| 6169 | } |
| 6170 | #endif |
| 6171 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) |
| 6172 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 6173 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6174 | } |
| 6175 | #endif |
| 6176 | return MBEDTLS_SSL_SIG_ANON; |
| 6177 | } |
| 6178 | |
| 6179 | unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type) |
| 6180 | { |
| 6181 | switch (type) { |
| 6182 | case MBEDTLS_PK_RSA: |
| 6183 | return MBEDTLS_SSL_SIG_RSA; |
| 6184 | case MBEDTLS_PK_ECDSA: |
| 6185 | case MBEDTLS_PK_ECKEY: |
| 6186 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6187 | default: |
| 6188 | return MBEDTLS_SSL_SIG_ANON; |
| 6189 | } |
| 6190 | } |
| 6191 | |
| 6192 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig) |
| 6193 | { |
| 6194 | switch (sig) { |
| 6195 | #if defined(MBEDTLS_RSA_C) |
| 6196 | case MBEDTLS_SSL_SIG_RSA: |
| 6197 | return MBEDTLS_PK_RSA; |
| 6198 | #endif |
| 6199 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) |
| 6200 | case MBEDTLS_SSL_SIG_ECDSA: |
| 6201 | return MBEDTLS_PK_ECDSA; |
| 6202 | #endif |
| 6203 | default: |
| 6204 | return MBEDTLS_PK_NONE; |
| 6205 | } |
| 6206 | } |
| 6207 | #endif /* MBEDTLS_PK_C && |
| 6208 | ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */ |
| 6209 | |
| 6210 | /* |
| 6211 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
| 6212 | */ |
| 6213 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash) |
| 6214 | { |
| 6215 | switch (hash) { |
| 6216 | #if defined(MBEDTLS_MD_CAN_MD5) |
| 6217 | case MBEDTLS_SSL_HASH_MD5: |
| 6218 | return MBEDTLS_MD_MD5; |
| 6219 | #endif |
| 6220 | #if defined(MBEDTLS_MD_CAN_SHA1) |
| 6221 | case MBEDTLS_SSL_HASH_SHA1: |
| 6222 | return MBEDTLS_MD_SHA1; |
| 6223 | #endif |
| 6224 | #if defined(MBEDTLS_MD_CAN_SHA224) |
| 6225 | case MBEDTLS_SSL_HASH_SHA224: |
| 6226 | return MBEDTLS_MD_SHA224; |
| 6227 | #endif |
| 6228 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6229 | case MBEDTLS_SSL_HASH_SHA256: |
| 6230 | return MBEDTLS_MD_SHA256; |
| 6231 | #endif |
| 6232 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6233 | case MBEDTLS_SSL_HASH_SHA384: |
| 6234 | return MBEDTLS_MD_SHA384; |
| 6235 | #endif |
| 6236 | #if defined(MBEDTLS_MD_CAN_SHA512) |
| 6237 | case MBEDTLS_SSL_HASH_SHA512: |
| 6238 | return MBEDTLS_MD_SHA512; |
| 6239 | #endif |
| 6240 | default: |
| 6241 | return MBEDTLS_MD_NONE; |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | /* |
| 6246 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6247 | */ |
| 6248 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
| 6249 | { |
| 6250 | switch (md) { |
| 6251 | #if defined(MBEDTLS_MD_CAN_MD5) |
| 6252 | case MBEDTLS_MD_MD5: |
| 6253 | return MBEDTLS_SSL_HASH_MD5; |
| 6254 | #endif |
| 6255 | #if defined(MBEDTLS_MD_CAN_SHA1) |
| 6256 | case MBEDTLS_MD_SHA1: |
| 6257 | return MBEDTLS_SSL_HASH_SHA1; |
| 6258 | #endif |
| 6259 | #if defined(MBEDTLS_MD_CAN_SHA224) |
| 6260 | case MBEDTLS_MD_SHA224: |
| 6261 | return MBEDTLS_SSL_HASH_SHA224; |
| 6262 | #endif |
| 6263 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6264 | case MBEDTLS_MD_SHA256: |
| 6265 | return MBEDTLS_SSL_HASH_SHA256; |
| 6266 | #endif |
| 6267 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6268 | case MBEDTLS_MD_SHA384: |
| 6269 | return MBEDTLS_SSL_HASH_SHA384; |
| 6270 | #endif |
| 6271 | #if defined(MBEDTLS_MD_CAN_SHA512) |
| 6272 | case MBEDTLS_MD_SHA512: |
| 6273 | return MBEDTLS_SSL_HASH_SHA512; |
| 6274 | #endif |
| 6275 | default: |
| 6276 | return MBEDTLS_SSL_HASH_NONE; |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | /* |
| 6281 | * Check if a curve proposed by the peer is in our list. |
| 6282 | * Return 0 if we're willing to use it, -1 otherwise. |
| 6283 | */ |
| 6284 | int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id) |
| 6285 | { |
| 6286 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
| 6287 | |
| 6288 | if (group_list == NULL) { |
| 6289 | return -1; |
| 6290 | } |
| 6291 | |
| 6292 | for (; *group_list != 0; group_list++) { |
| 6293 | if (*group_list == tls_id) { |
| 6294 | return 0; |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | return -1; |
| 6299 | } |
| 6300 | |
| 6301 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 6302 | /* |
| 6303 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 6304 | */ |
| 6305 | int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id) |
| 6306 | { |
| 6307 | uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id); |
| 6308 | |
| 6309 | if (tls_id == 0) { |
| 6310 | return -1; |
| 6311 | } |
| 6312 | |
| 6313 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
| 6314 | } |
| 6315 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 6316 | |
| 6317 | static const struct { |
| 6318 | uint16_t tls_id; |
| 6319 | mbedtls_ecp_group_id ecp_group_id; |
| 6320 | psa_ecc_family_t psa_family; |
| 6321 | uint16_t bits; |
| 6322 | } tls_id_match_table[] = |
| 6323 | { |
| 6324 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) |
| 6325 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 }, |
| 6326 | #endif |
| 6327 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) |
| 6328 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 }, |
| 6329 | #endif |
| 6330 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
| 6331 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 }, |
| 6332 | #endif |
| 6333 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) |
| 6334 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, |
| 6335 | #endif |
| 6336 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
| 6337 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 }, |
| 6338 | #endif |
| 6339 | #if defined(MBEDTLS_ECP_HAVE_SECP256K1) |
| 6340 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 }, |
| 6341 | #endif |
| 6342 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) |
| 6343 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, |
| 6344 | #endif |
| 6345 | #if defined(MBEDTLS_ECP_HAVE_SECP224R1) |
| 6346 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 }, |
| 6347 | #endif |
| 6348 | #if defined(MBEDTLS_ECP_HAVE_SECP224K1) |
| 6349 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 }, |
| 6350 | #endif |
| 6351 | #if defined(MBEDTLS_ECP_HAVE_SECP192R1) |
| 6352 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 }, |
| 6353 | #endif |
| 6354 | #if defined(MBEDTLS_ECP_HAVE_SECP192K1) |
| 6355 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 }, |
| 6356 | #endif |
| 6357 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) |
| 6358 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 }, |
| 6359 | #endif |
| 6360 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) |
| 6361 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 }, |
| 6362 | #endif |
| 6363 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0 }, |
| 6364 | }; |
| 6365 | |
| 6366 | int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id, |
| 6367 | psa_key_type_t *type, |
| 6368 | size_t *bits) |
| 6369 | { |
| 6370 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6371 | if (tls_id_match_table[i].tls_id == tls_id) { |
| 6372 | if (type != NULL) { |
| 6373 | *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family); |
| 6374 | } |
| 6375 | if (bits != NULL) { |
| 6376 | *bits = tls_id_match_table[i].bits; |
| 6377 | } |
| 6378 | return PSA_SUCCESS; |
| 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | return PSA_ERROR_NOT_SUPPORTED; |
| 6383 | } |
| 6384 | |
| 6385 | mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id) |
| 6386 | { |
| 6387 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6388 | if (tls_id_match_table[i].tls_id == tls_id) { |
| 6389 | return tls_id_match_table[i].ecp_group_id; |
| 6390 | } |
| 6391 | } |
| 6392 | |
| 6393 | return MBEDTLS_ECP_DP_NONE; |
| 6394 | } |
| 6395 | |
| 6396 | uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id) |
| 6397 | { |
| 6398 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 6399 | i++) { |
| 6400 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
| 6401 | return tls_id_match_table[i].tls_id; |
| 6402 | } |
| 6403 | } |
| 6404 | |
| 6405 | return 0; |
| 6406 | } |
| 6407 | |
| 6408 | #if defined(MBEDTLS_DEBUG_C) |
| 6409 | static const struct { |
| 6410 | uint16_t tls_id; |
| 6411 | const char *name; |
| 6412 | } tls_id_curve_name_table[] = |
| 6413 | { |
| 6414 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" }, |
| 6415 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" }, |
| 6416 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" }, |
| 6417 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" }, |
| 6418 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" }, |
| 6419 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" }, |
| 6420 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" }, |
| 6421 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" }, |
| 6422 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" }, |
| 6423 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" }, |
| 6424 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" }, |
| 6425 | { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" }, |
| 6426 | { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" }, |
| 6427 | { 0, NULL }, |
| 6428 | }; |
| 6429 | |
| 6430 | const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id) |
| 6431 | { |
| 6432 | for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) { |
| 6433 | if (tls_id_curve_name_table[i].tls_id == tls_id) { |
| 6434 | return tls_id_curve_name_table[i].name; |
| 6435 | } |
| 6436 | } |
| 6437 | |
| 6438 | return NULL; |
| 6439 | } |
| 6440 | #endif |
| 6441 | |
| 6442 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6443 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6444 | const mbedtls_md_type_t md, |
| 6445 | unsigned char *dst, |
| 6446 | size_t dst_len, |
| 6447 | size_t *olen) |
| 6448 | { |
| 6449 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6450 | psa_hash_operation_t *hash_operation_to_clone; |
| 6451 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 6452 | |
| 6453 | *olen = 0; |
| 6454 | |
| 6455 | switch (md) { |
| 6456 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6457 | case MBEDTLS_MD_SHA384: |
| 6458 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 6459 | break; |
| 6460 | #endif |
| 6461 | |
| 6462 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6463 | case MBEDTLS_MD_SHA256: |
| 6464 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 6465 | break; |
| 6466 | #endif |
| 6467 | |
| 6468 | default: |
| 6469 | goto exit; |
| 6470 | } |
| 6471 | |
| 6472 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 6473 | if (status != PSA_SUCCESS) { |
| 6474 | goto exit; |
| 6475 | } |
| 6476 | |
| 6477 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 6478 | if (status != PSA_SUCCESS) { |
| 6479 | goto exit; |
| 6480 | } |
| 6481 | |
| 6482 | exit: |
| 6483 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 6484 | !defined(MBEDTLS_MD_CAN_SHA256) |
| 6485 | (void) ssl; |
| 6486 | #endif |
| 6487 | return PSA_TO_MBEDTLS_ERR(status); |
| 6488 | } |
| 6489 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6490 | |
| 6491 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6492 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6493 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 6494 | unsigned char *dst, |
| 6495 | size_t dst_len, |
| 6496 | size_t *olen) |
| 6497 | { |
| 6498 | int ret; |
| 6499 | mbedtls_md_context_t sha384; |
| 6500 | |
| 6501 | if (dst_len < 48) { |
| 6502 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6503 | } |
| 6504 | |
| 6505 | mbedtls_md_init(&sha384); |
| 6506 | ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 6507 | if (ret != 0) { |
| 6508 | goto exit; |
| 6509 | } |
| 6510 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
| 6511 | if (ret != 0) { |
| 6512 | goto exit; |
| 6513 | } |
| 6514 | |
| 6515 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { |
| 6516 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
| 6517 | goto exit; |
| 6518 | } |
| 6519 | |
| 6520 | *olen = 48; |
| 6521 | |
| 6522 | exit: |
| 6523 | |
| 6524 | mbedtls_md_free(&sha384); |
| 6525 | return ret; |
| 6526 | } |
| 6527 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 6528 | |
| 6529 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6530 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6531 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 6532 | unsigned char *dst, |
| 6533 | size_t dst_len, |
| 6534 | size_t *olen) |
| 6535 | { |
| 6536 | int ret; |
| 6537 | mbedtls_md_context_t sha256; |
| 6538 | |
| 6539 | if (dst_len < 32) { |
| 6540 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6541 | } |
| 6542 | |
| 6543 | mbedtls_md_init(&sha256); |
| 6544 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 6545 | if (ret != 0) { |
| 6546 | goto exit; |
| 6547 | } |
| 6548 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6549 | if (ret != 0) { |
| 6550 | goto exit; |
| 6551 | } |
| 6552 | |
| 6553 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { |
| 6554 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
| 6555 | goto exit; |
| 6556 | } |
| 6557 | |
| 6558 | *olen = 32; |
| 6559 | |
| 6560 | exit: |
| 6561 | |
| 6562 | mbedtls_md_free(&sha256); |
| 6563 | return ret; |
| 6564 | } |
| 6565 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
| 6566 | |
| 6567 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6568 | const mbedtls_md_type_t md, |
| 6569 | unsigned char *dst, |
| 6570 | size_t dst_len, |
| 6571 | size_t *olen) |
| 6572 | { |
| 6573 | switch (md) { |
| 6574 | |
| 6575 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6576 | case MBEDTLS_MD_SHA384: |
| 6577 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
| 6578 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 6579 | |
| 6580 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6581 | case MBEDTLS_MD_SHA256: |
| 6582 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
| 6583 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 6584 | |
| 6585 | default: |
| 6586 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 6587 | !defined(MBEDTLS_MD_CAN_SHA256) |
| 6588 | (void) ssl; |
| 6589 | (void) dst; |
| 6590 | (void) dst_len; |
| 6591 | (void) olen; |
| 6592 | #endif |
| 6593 | break; |
| 6594 | } |
| 6595 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6596 | } |
| 6597 | |
| 6598 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
| 6599 | |
| 6600 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 6601 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 6602 | * |
| 6603 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 6604 | * value (TLS 1.3 RFC8446): |
| 6605 | * enum { |
| 6606 | * .... |
| 6607 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 6608 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 6609 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 6610 | * .... |
| 6611 | * } SignatureScheme; |
| 6612 | * |
| 6613 | * struct { |
| 6614 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 6615 | * } SignatureSchemeList; |
| 6616 | * |
| 6617 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 6618 | * value (TLS 1.2 RFC5246): |
| 6619 | * enum { |
| 6620 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 6621 | * sha512(6), (255) |
| 6622 | * } HashAlgorithm; |
| 6623 | * |
| 6624 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 6625 | * SignatureAlgorithm; |
| 6626 | * |
| 6627 | * struct { |
| 6628 | * HashAlgorithm hash; |
| 6629 | * SignatureAlgorithm signature; |
| 6630 | * } SignatureAndHashAlgorithm; |
| 6631 | * |
| 6632 | * SignatureAndHashAlgorithm |
| 6633 | * supported_signature_algorithms<2..2^16-2>; |
| 6634 | * |
| 6635 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 6636 | * generalization of the TLS 1.2 signature algorithm extension. |
| 6637 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 6638 | * `SignatureScheme` field of TLS 1.3 |
| 6639 | * |
| 6640 | */ |
| 6641 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 6642 | const unsigned char *buf, |
| 6643 | const unsigned char *end) |
| 6644 | { |
| 6645 | const unsigned char *p = buf; |
| 6646 | size_t supported_sig_algs_len = 0; |
| 6647 | const unsigned char *supported_sig_algs_end; |
| 6648 | uint16_t sig_alg; |
| 6649 | uint32_t common_idx = 0; |
| 6650 | |
| 6651 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 6652 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 6653 | p += 2; |
| 6654 | |
| 6655 | memset(ssl->handshake->received_sig_algs, 0, |
| 6656 | sizeof(ssl->handshake->received_sig_algs)); |
| 6657 | |
| 6658 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
| 6659 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 6660 | while (p < supported_sig_algs_end) { |
| 6661 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 6662 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
| 6663 | p += 2; |
| 6664 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 6665 | sig_alg, |
| 6666 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
| 6667 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6668 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 6669 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 6670 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
| 6671 | continue; |
| 6672 | } |
| 6673 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6674 | |
| 6675 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 6676 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
| 6677 | |
| 6678 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
| 6679 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 6680 | common_idx += 1; |
| 6681 | } |
| 6682 | } |
| 6683 | /* Check that we consumed all the message. */ |
| 6684 | if (p != end) { |
| 6685 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 6686 | ("Signature algorithms extension length misaligned")); |
| 6687 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 6688 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 6689 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 6690 | } |
| 6691 | |
| 6692 | if (common_idx == 0) { |
| 6693 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 6694 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 6695 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 6696 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 6697 | } |
| 6698 | |
| 6699 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
| 6700 | return 0; |
| 6701 | } |
| 6702 | |
| 6703 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 6704 | |
| 6705 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6706 | |
| 6707 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6708 | |
| 6709 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 6710 | mbedtls_svc_key_id_t key, |
| 6711 | psa_algorithm_t alg, |
| 6712 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 6713 | const unsigned char *seed, size_t seed_length, |
| 6714 | const unsigned char *label, size_t label_length, |
| 6715 | const unsigned char *other_secret, |
| 6716 | size_t other_secret_length, |
| 6717 | size_t capacity) |
| 6718 | { |
| 6719 | psa_status_t status; |
| 6720 | |
| 6721 | status = psa_key_derivation_setup(derivation, alg); |
| 6722 | if (status != PSA_SUCCESS) { |
| 6723 | return status; |
| 6724 | } |
| 6725 | |
| 6726 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 6727 | status = psa_key_derivation_input_bytes(derivation, |
| 6728 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 6729 | seed, seed_length); |
| 6730 | if (status != PSA_SUCCESS) { |
| 6731 | return status; |
| 6732 | } |
| 6733 | |
| 6734 | if (other_secret != NULL) { |
| 6735 | status = psa_key_derivation_input_bytes(derivation, |
| 6736 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 6737 | other_secret, other_secret_length); |
| 6738 | if (status != PSA_SUCCESS) { |
| 6739 | return status; |
| 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | if (mbedtls_svc_key_id_is_null(key)) { |
| 6744 | status = psa_key_derivation_input_bytes( |
| 6745 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 6746 | raw_psk, raw_psk_length); |
| 6747 | } else { |
| 6748 | status = psa_key_derivation_input_key( |
| 6749 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
| 6750 | } |
| 6751 | if (status != PSA_SUCCESS) { |
| 6752 | return status; |
| 6753 | } |
| 6754 | |
| 6755 | status = psa_key_derivation_input_bytes(derivation, |
| 6756 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 6757 | label, label_length); |
| 6758 | if (status != PSA_SUCCESS) { |
| 6759 | return status; |
| 6760 | } |
| 6761 | } else { |
| 6762 | return PSA_ERROR_NOT_SUPPORTED; |
| 6763 | } |
| 6764 | |
| 6765 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 6766 | if (status != PSA_SUCCESS) { |
| 6767 | return status; |
| 6768 | } |
| 6769 | |
| 6770 | return PSA_SUCCESS; |
| 6771 | } |
| 6772 | |
| 6773 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 6774 | defined(PSA_WANT_ALG_SHA_256) |
| 6775 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6776 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6777 | const unsigned char *secret, size_t slen, |
| 6778 | const char *label, size_t label_len, |
| 6779 | const unsigned char *random, size_t rlen, |
| 6780 | unsigned char *dstbuf, size_t dlen) |
| 6781 | { |
| 6782 | psa_status_t status; |
| 6783 | psa_algorithm_t alg; |
| 6784 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6785 | psa_key_derivation_operation_t derivation = |
| 6786 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6787 | |
| 6788 | if (md_type == MBEDTLS_MD_SHA384) { |
| 6789 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 6790 | } else { |
| 6791 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 6792 | } |
| 6793 | |
| 6794 | /* Normally a "secret" should be long enough to be impossible to |
| 6795 | * find by brute force, and in particular should not be empty. But |
| 6796 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6797 | * and for this use case it makes sense to have a 0-length "secret". |
| 6798 | * Since the key API doesn't allow importing a key of length 0, |
| 6799 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6800 | * to mean a 0-length "secret" input. */ |
| 6801 | if (slen != 0) { |
| 6802 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 6803 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6804 | psa_set_key_algorithm(&key_attributes, alg); |
| 6805 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
| 6806 | |
| 6807 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6808 | if (status != PSA_SUCCESS) { |
| 6809 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6810 | } |
| 6811 | } |
| 6812 | |
| 6813 | status = setup_psa_key_derivation(&derivation, |
| 6814 | master_key, alg, |
| 6815 | NULL, 0, |
| 6816 | random, rlen, |
| 6817 | (unsigned char const *) label, |
| 6818 | label_len, |
| 6819 | NULL, 0, |
| 6820 | dlen); |
| 6821 | if (status != PSA_SUCCESS) { |
| 6822 | psa_key_derivation_abort(&derivation); |
| 6823 | psa_destroy_key(master_key); |
| 6824 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6825 | } |
| 6826 | |
| 6827 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6828 | if (status != PSA_SUCCESS) { |
| 6829 | psa_key_derivation_abort(&derivation); |
| 6830 | psa_destroy_key(master_key); |
| 6831 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6832 | } |
| 6833 | |
| 6834 | status = psa_key_derivation_abort(&derivation); |
| 6835 | if (status != PSA_SUCCESS) { |
| 6836 | psa_destroy_key(master_key); |
| 6837 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6838 | } |
| 6839 | |
| 6840 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6841 | status = psa_destroy_key(master_key); |
| 6842 | } |
| 6843 | if (status != PSA_SUCCESS) { |
| 6844 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6845 | } |
| 6846 | |
| 6847 | return 0; |
| 6848 | } |
| 6849 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
| 6850 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6851 | |
| 6852 | #if defined(MBEDTLS_MD_C) && \ |
| 6853 | (defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 6854 | defined(MBEDTLS_MD_CAN_SHA384)) |
| 6855 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6856 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6857 | const unsigned char *secret, size_t slen, |
| 6858 | const char *label, size_t label_len, |
| 6859 | const unsigned char *random, size_t rlen, |
| 6860 | unsigned char *dstbuf, size_t dlen) |
| 6861 | { |
| 6862 | size_t nb; |
| 6863 | size_t i, j, k, md_len; |
| 6864 | unsigned char *tmp; |
| 6865 | size_t tmp_len = 0; |
| 6866 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6867 | const mbedtls_md_info_t *md_info; |
| 6868 | mbedtls_md_context_t md_ctx; |
| 6869 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6870 | |
| 6871 | mbedtls_md_init(&md_ctx); |
| 6872 | |
| 6873 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6874 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6875 | } |
| 6876 | |
| 6877 | md_len = mbedtls_md_get_size(md_info); |
| 6878 | |
| 6879 | tmp_len = md_len + label_len + rlen; |
| 6880 | tmp = mbedtls_calloc(1, tmp_len); |
| 6881 | if (tmp == NULL) { |
| 6882 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6883 | goto exit; |
| 6884 | } |
| 6885 | |
| 6886 | nb = label_len; |
| 6887 | memcpy(tmp + md_len, label, nb); |
| 6888 | memcpy(tmp + md_len + nb, random, rlen); |
| 6889 | nb += rlen; |
| 6890 | |
| 6891 | /* |
| 6892 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6893 | */ |
| 6894 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
| 6895 | goto exit; |
| 6896 | } |
| 6897 | |
| 6898 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6899 | if (ret != 0) { |
| 6900 | goto exit; |
| 6901 | } |
| 6902 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6903 | if (ret != 0) { |
| 6904 | goto exit; |
| 6905 | } |
| 6906 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6907 | if (ret != 0) { |
| 6908 | goto exit; |
| 6909 | } |
| 6910 | |
| 6911 | for (i = 0; i < dlen; i += md_len) { |
| 6912 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6913 | if (ret != 0) { |
| 6914 | goto exit; |
| 6915 | } |
| 6916 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6917 | if (ret != 0) { |
| 6918 | goto exit; |
| 6919 | } |
| 6920 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6921 | if (ret != 0) { |
| 6922 | goto exit; |
| 6923 | } |
| 6924 | |
| 6925 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6926 | if (ret != 0) { |
| 6927 | goto exit; |
| 6928 | } |
| 6929 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6930 | if (ret != 0) { |
| 6931 | goto exit; |
| 6932 | } |
| 6933 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6934 | if (ret != 0) { |
| 6935 | goto exit; |
| 6936 | } |
| 6937 | |
| 6938 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
| 6939 | |
| 6940 | for (j = 0; j < k; j++) { |
| 6941 | dstbuf[i + j] = h_i[j]; |
| 6942 | } |
| 6943 | } |
| 6944 | |
| 6945 | exit: |
| 6946 | mbedtls_md_free(&md_ctx); |
| 6947 | |
| 6948 | if (tmp != NULL) { |
| 6949 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6950 | } |
| 6951 | |
| 6952 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
| 6953 | |
| 6954 | mbedtls_free(tmp); |
| 6955 | |
| 6956 | return ret; |
| 6957 | } |
| 6958 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */ |
| 6959 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6960 | |
| 6961 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 6962 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6963 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6964 | const char *label, |
| 6965 | const unsigned char *random, size_t rlen, |
| 6966 | unsigned char *dstbuf, size_t dlen) |
| 6967 | { |
| 6968 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6969 | label, strlen(label), random, rlen, dstbuf, dlen); |
| 6970 | } |
| 6971 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 6972 | |
| 6973 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6974 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6975 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6976 | const char *label, |
| 6977 | const unsigned char *random, size_t rlen, |
| 6978 | unsigned char *dstbuf, size_t dlen) |
| 6979 | { |
| 6980 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6981 | label, strlen(label), random, rlen, dstbuf, dlen); |
| 6982 | } |
| 6983 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 6984 | |
| 6985 | /* |
| 6986 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6987 | * |
| 6988 | * Inputs: |
| 6989 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6990 | * |
| 6991 | * Outputs: |
| 6992 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6993 | */ |
| 6994 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 6995 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6996 | mbedtls_md_type_t hash) |
| 6997 | { |
| 6998 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 6999 | if (hash == MBEDTLS_MD_SHA384) { |
| 7000 | handshake->tls_prf = tls_prf_sha384; |
| 7001 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7002 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 7003 | } else |
| 7004 | #endif |
| 7005 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 7006 | { |
| 7007 | (void) hash; |
| 7008 | handshake->tls_prf = tls_prf_sha256; |
| 7009 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7010 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 7011 | } |
| 7012 | #else |
| 7013 | { |
| 7014 | (void) handshake; |
| 7015 | (void) hash; |
| 7016 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7017 | } |
| 7018 | #endif |
| 7019 | |
| 7020 | return 0; |
| 7021 | } |
| 7022 | |
| 7023 | /* |
| 7024 | * Compute master secret if needed |
| 7025 | * |
| 7026 | * Parameters: |
| 7027 | * [in/out] handshake |
| 7028 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 7029 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 7030 | * [out] premaster (cleared) |
| 7031 | * [out] master |
| 7032 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 7033 | * debug: conf->f_dbg, conf->p_dbg |
| 7034 | * EMS: passed to calc_verify (debug + session_negotiate) |
| 7035 | * PSA-PSA: conf |
| 7036 | */ |
| 7037 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7038 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 7039 | unsigned char *master, |
| 7040 | const mbedtls_ssl_context *ssl) |
| 7041 | { |
| 7042 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7043 | |
| 7044 | /* cf. RFC 5246, Section 8.1: |
| 7045 | * "The master secret is always exactly 48 bytes in length." */ |
| 7046 | size_t const master_secret_len = 48; |
| 7047 | |
| 7048 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 7049 | unsigned char session_hash[48]; |
| 7050 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 7051 | |
| 7052 | /* The label for the KDF used for key expansion. |
| 7053 | * This is either "master secret" or "extended master secret" |
| 7054 | * depending on whether the Extended Master Secret extension |
| 7055 | * is used. */ |
| 7056 | char const *lbl = "master secret"; |
| 7057 | |
| 7058 | /* The seed for the KDF used for key expansion. |
| 7059 | * - If the Extended Master Secret extension is not used, |
| 7060 | * this is ClientHello.Random + ServerHello.Random |
| 7061 | * (see Sect. 8.1 in RFC 5246). |
| 7062 | * - If the Extended Master Secret extension is used, |
| 7063 | * this is the transcript of the handshake so far. |
| 7064 | * (see Sect. 4 in RFC 7627). */ |
| 7065 | unsigned char const *seed = handshake->randbytes; |
| 7066 | size_t seed_len = 64; |
| 7067 | |
| 7068 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7069 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 7070 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7071 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 7072 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 7073 | (void) ssl; |
| 7074 | #endif |
| 7075 | |
| 7076 | if (handshake->resume != 0) { |
| 7077 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 7078 | return 0; |
| 7079 | } |
| 7080 | |
| 7081 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 7082 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
| 7083 | lbl = "extended master secret"; |
| 7084 | seed = session_hash; |
| 7085 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); |
| 7086 | if (ret != 0) { |
| 7087 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); |
| 7088 | } |
| 7089 | |
| 7090 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 7091 | session_hash, seed_len); |
| 7092 | } |
| 7093 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 7094 | |
| 7095 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7096 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 7097 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
| 7098 | /* Perform PSK-to-MS expansion in a single step. */ |
| 7099 | psa_status_t status; |
| 7100 | psa_algorithm_t alg; |
| 7101 | mbedtls_svc_key_id_t psk; |
| 7102 | psa_key_derivation_operation_t derivation = |
| 7103 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7104 | mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac; |
| 7105 | |
| 7106 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
| 7107 | |
| 7108 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
| 7109 | |
| 7110 | if (hash_alg == MBEDTLS_MD_SHA384) { |
| 7111 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 7112 | } else { |
| 7113 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 7114 | } |
| 7115 | |
| 7116 | size_t other_secret_len = 0; |
| 7117 | unsigned char *other_secret = NULL; |
| 7118 | |
| 7119 | switch (handshake->ciphersuite_info->key_exchange) { |
| 7120 | /* Provide other secret. |
| 7121 | * Other secret is stored in premaster, where first 2 bytes hold the |
| 7122 | * length of the other key. |
| 7123 | */ |
| 7124 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
| 7125 | /* For RSA-PSK other key length is always 48 bytes. */ |
| 7126 | other_secret_len = 48; |
| 7127 | other_secret = handshake->premaster + 2; |
| 7128 | break; |
| 7129 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
| 7130 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7131 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 7132 | other_secret = handshake->premaster + 2; |
| 7133 | break; |
| 7134 | default: |
| 7135 | break; |
| 7136 | } |
| 7137 | |
| 7138 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 7139 | ssl->conf->psk, ssl->conf->psk_len, |
| 7140 | seed, seed_len, |
| 7141 | (unsigned char const *) lbl, |
| 7142 | (size_t) strlen(lbl), |
| 7143 | other_secret, other_secret_len, |
| 7144 | master_secret_len); |
| 7145 | if (status != PSA_SUCCESS) { |
| 7146 | psa_key_derivation_abort(&derivation); |
| 7147 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7148 | } |
| 7149 | |
| 7150 | status = psa_key_derivation_output_bytes(&derivation, |
| 7151 | master, |
| 7152 | master_secret_len); |
| 7153 | if (status != PSA_SUCCESS) { |
| 7154 | psa_key_derivation_abort(&derivation); |
| 7155 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7156 | } |
| 7157 | |
| 7158 | status = psa_key_derivation_abort(&derivation); |
| 7159 | if (status != PSA_SUCCESS) { |
| 7160 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7161 | } |
| 7162 | } else |
| 7163 | #endif |
| 7164 | { |
| 7165 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7166 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7167 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
| 7168 | psa_status_t status; |
| 7169 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7170 | psa_key_derivation_operation_t derivation = |
| 7171 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7172 | |
| 7173 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
| 7174 | |
| 7175 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 7176 | |
| 7177 | status = psa_key_derivation_setup(&derivation, alg); |
| 7178 | if (status != PSA_SUCCESS) { |
| 7179 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7180 | } |
| 7181 | |
| 7182 | status = psa_key_derivation_set_capacity(&derivation, |
| 7183 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 7184 | if (status != PSA_SUCCESS) { |
| 7185 | psa_key_derivation_abort(&derivation); |
| 7186 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7187 | } |
| 7188 | |
| 7189 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 7190 | &derivation); |
| 7191 | if (status != PSA_SUCCESS) { |
| 7192 | psa_key_derivation_abort(&derivation); |
| 7193 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7194 | } |
| 7195 | |
| 7196 | status = psa_key_derivation_output_bytes(&derivation, |
| 7197 | handshake->premaster, |
| 7198 | handshake->pmslen); |
| 7199 | if (status != PSA_SUCCESS) { |
| 7200 | psa_key_derivation_abort(&derivation); |
| 7201 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7202 | } |
| 7203 | |
| 7204 | status = psa_key_derivation_abort(&derivation); |
| 7205 | if (status != PSA_SUCCESS) { |
| 7206 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7207 | } |
| 7208 | } |
| 7209 | #endif |
| 7210 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 7211 | lbl, seed, seed_len, |
| 7212 | master, |
| 7213 | master_secret_len); |
| 7214 | if (ret != 0) { |
| 7215 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 7216 | return ret; |
| 7217 | } |
| 7218 | |
| 7219 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 7220 | handshake->premaster, |
| 7221 | handshake->pmslen); |
| 7222 | |
| 7223 | mbedtls_platform_zeroize(handshake->premaster, |
| 7224 | sizeof(handshake->premaster)); |
| 7225 | } |
| 7226 | |
| 7227 | return 0; |
| 7228 | } |
| 7229 | |
| 7230 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
| 7231 | { |
| 7232 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7233 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7234 | ssl->handshake->ciphersuite_info; |
| 7235 | |
| 7236 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
| 7237 | |
| 7238 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 7239 | ret = ssl_set_handshake_prfs(ssl->handshake, |
| 7240 | (mbedtls_md_type_t) ciphersuite_info->mac); |
| 7241 | if (ret != 0) { |
| 7242 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 7243 | return ret; |
| 7244 | } |
| 7245 | |
| 7246 | /* Compute master secret if needed */ |
| 7247 | ret = ssl_compute_master(ssl->handshake, |
| 7248 | ssl->session_negotiate->master, |
| 7249 | ssl); |
| 7250 | if (ret != 0) { |
| 7251 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 7252 | return ret; |
| 7253 | } |
| 7254 | |
| 7255 | /* Swap the client and server random values: |
| 7256 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 7257 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 7258 | { |
| 7259 | unsigned char tmp[64]; |
| 7260 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 7261 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 7262 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 7263 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 7264 | } |
| 7265 | |
| 7266 | /* Populate transform structure */ |
| 7267 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 7268 | ssl->session_negotiate->ciphersuite, |
| 7269 | ssl->session_negotiate->master, |
| 7270 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 7271 | ssl->session_negotiate->encrypt_then_mac, |
| 7272 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 7273 | ssl->handshake->tls_prf, |
| 7274 | ssl->handshake->randbytes, |
| 7275 | ssl->tls_version, |
| 7276 | ssl->conf->endpoint, |
| 7277 | ssl); |
| 7278 | if (ret != 0) { |
| 7279 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 7280 | return ret; |
| 7281 | } |
| 7282 | |
| 7283 | /* We no longer need Server/ClientHello.random values */ |
| 7284 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 7285 | sizeof(ssl->handshake->randbytes)); |
| 7286 | |
| 7287 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
| 7288 | |
| 7289 | return 0; |
| 7290 | } |
| 7291 | |
| 7292 | int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md) |
| 7293 | { |
| 7294 | switch (md) { |
| 7295 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 7296 | case MBEDTLS_SSL_HASH_SHA384: |
| 7297 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7298 | break; |
| 7299 | #endif |
| 7300 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 7301 | case MBEDTLS_SSL_HASH_SHA256: |
| 7302 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7303 | break; |
| 7304 | #endif |
| 7305 | default: |
| 7306 | return -1; |
| 7307 | } |
| 7308 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 7309 | !defined(MBEDTLS_MD_CAN_SHA256) |
| 7310 | (void) ssl; |
| 7311 | #endif |
| 7312 | return 0; |
| 7313 | } |
| 7314 | |
| 7315 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7316 | static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl, |
| 7317 | const psa_hash_operation_t *hs_op, |
| 7318 | size_t buffer_size, |
| 7319 | unsigned char *hash, |
| 7320 | size_t *hlen) |
| 7321 | { |
| 7322 | psa_status_t status; |
| 7323 | psa_hash_operation_t cloned_op = psa_hash_operation_init(); |
| 7324 | |
| 7325 | #if !defined(MBEDTLS_DEBUG_C) |
| 7326 | (void) ssl; |
| 7327 | #endif |
| 7328 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify")); |
| 7329 | status = psa_hash_clone(hs_op, &cloned_op); |
| 7330 | if (status != PSA_SUCCESS) { |
| 7331 | goto exit; |
| 7332 | } |
| 7333 | |
| 7334 | status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen); |
| 7335 | if (status != PSA_SUCCESS) { |
| 7336 | goto exit; |
| 7337 | } |
| 7338 | |
| 7339 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 7340 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 7341 | |
| 7342 | exit: |
| 7343 | psa_hash_abort(&cloned_op); |
| 7344 | return mbedtls_md_error_from_psa(status); |
| 7345 | } |
| 7346 | #else |
| 7347 | static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl, |
| 7348 | const mbedtls_md_context_t *hs_ctx, |
| 7349 | unsigned char *hash, |
| 7350 | size_t *hlen) |
| 7351 | { |
| 7352 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7353 | mbedtls_md_context_t cloned_ctx; |
| 7354 | |
| 7355 | mbedtls_md_init(&cloned_ctx); |
| 7356 | |
| 7357 | #if !defined(MBEDTLS_DEBUG_C) |
| 7358 | (void) ssl; |
| 7359 | #endif |
| 7360 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify")); |
| 7361 | |
| 7362 | ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0); |
| 7363 | if (ret != 0) { |
| 7364 | goto exit; |
| 7365 | } |
| 7366 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
| 7367 | if (ret != 0) { |
| 7368 | goto exit; |
| 7369 | } |
| 7370 | |
| 7371 | ret = mbedtls_md_finish(&cloned_ctx, hash); |
| 7372 | if (ret != 0) { |
| 7373 | goto exit; |
| 7374 | } |
| 7375 | |
| 7376 | *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx)); |
| 7377 | |
| 7378 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 7379 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
| 7380 | |
| 7381 | exit: |
| 7382 | mbedtls_md_free(&cloned_ctx); |
| 7383 | return ret; |
| 7384 | } |
| 7385 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7386 | |
| 7387 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 7388 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 7389 | unsigned char *hash, |
| 7390 | size_t *hlen) |
| 7391 | { |
| 7392 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7393 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32, |
| 7394 | hash, hlen); |
| 7395 | #else |
| 7396 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256, |
| 7397 | hash, hlen); |
| 7398 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7399 | } |
| 7400 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
| 7401 | |
| 7402 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 7403 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 7404 | unsigned char *hash, |
| 7405 | size_t *hlen) |
| 7406 | { |
| 7407 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7408 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48, |
| 7409 | hash, hlen); |
| 7410 | #else |
| 7411 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384, |
| 7412 | hash, hlen); |
| 7413 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7414 | } |
| 7415 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 7416 | |
| 7417 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7418 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 7419 | int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex) |
| 7420 | { |
| 7421 | unsigned char *p = ssl->handshake->premaster; |
| 7422 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
| 7423 | const unsigned char *psk = NULL; |
| 7424 | size_t psk_len = 0; |
| 7425 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
| 7426 | |
| 7427 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
| 7428 | /* |
| 7429 | * This should never happen because the existence of a PSK is always |
| 7430 | * checked before calling this function. |
| 7431 | * |
| 7432 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
| 7433 | * the shared secret without PSK. |
| 7434 | */ |
| 7435 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 7436 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7437 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7438 | } |
| 7439 | } |
| 7440 | |
| 7441 | /* |
| 7442 | * PMS = struct { |
| 7443 | * opaque other_secret<0..2^16-1>; |
| 7444 | * opaque psk<0..2^16-1>; |
| 7445 | * }; |
| 7446 | * with "other_secret" depending on the particular key exchange |
| 7447 | */ |
| 7448 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 7449 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 7450 | if (end - p < 2) { |
| 7451 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7452 | } |
| 7453 | |
| 7454 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
| 7455 | p += 2; |
| 7456 | |
| 7457 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7458 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7459 | } |
| 7460 | |
| 7461 | memset(p, 0, psk_len); |
| 7462 | p += psk_len; |
| 7463 | } else |
| 7464 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 7465 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 7466 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7467 | /* |
| 7468 | * other_secret already set by the ClientKeyExchange message, |
| 7469 | * and is 48 bytes long |
| 7470 | */ |
| 7471 | if (end - p < 2) { |
| 7472 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7473 | } |
| 7474 | |
| 7475 | *p++ = 0; |
| 7476 | *p++ = 48; |
| 7477 | p += 48; |
| 7478 | } else |
| 7479 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 7480 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 7481 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 7482 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7483 | size_t len; |
| 7484 | |
| 7485 | /* Write length only when we know the actual value */ |
| 7486 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
| 7487 | p + 2, (size_t) (end - (p + 2)), &len, |
| 7488 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7489 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 7490 | return ret; |
| 7491 | } |
| 7492 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
| 7493 | p += 2 + len; |
| 7494 | |
| 7495 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 7496 | } else |
| 7497 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 7498 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 7499 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
| 7500 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7501 | size_t zlen; |
| 7502 | |
| 7503 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
| 7504 | p + 2, (size_t) (end - (p + 2)), |
| 7505 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7506 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 7507 | return ret; |
| 7508 | } |
| 7509 | |
| 7510 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
| 7511 | p += 2 + zlen; |
| 7512 | |
| 7513 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 7514 | MBEDTLS_DEBUG_ECDH_Z); |
| 7515 | } else |
| 7516 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 7517 | { |
| 7518 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7519 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7520 | } |
| 7521 | |
| 7522 | /* opaque psk<0..2^16-1>; */ |
| 7523 | if (end - p < 2) { |
| 7524 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7525 | } |
| 7526 | |
| 7527 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
| 7528 | p += 2; |
| 7529 | |
| 7530 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7531 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7532 | } |
| 7533 | |
| 7534 | memcpy(p, psk, psk_len); |
| 7535 | p += psk_len; |
| 7536 | |
| 7537 | ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster); |
| 7538 | |
| 7539 | return 0; |
| 7540 | } |
| 7541 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 7542 | |
| 7543 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7544 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7545 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
| 7546 | |
| 7547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7548 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
| 7549 | { |
| 7550 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 7551 | * timeout if we were using the usual handshake doubling scheme */ |
| 7552 | if (ssl->conf->renego_max_records < 0) { |
| 7553 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 7554 | unsigned char doublings = 1; |
| 7555 | |
| 7556 | while (ratio != 0) { |
| 7557 | ++doublings; |
| 7558 | ratio >>= 1; |
| 7559 | } |
| 7560 | |
| 7561 | if (++ssl->renego_records_seen > doublings) { |
| 7562 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 7563 | return 0; |
| 7564 | } |
| 7565 | } |
| 7566 | |
| 7567 | return ssl_write_hello_request(ssl); |
| 7568 | } |
| 7569 | #endif |
| 7570 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
| 7571 | |
| 7572 | /* |
| 7573 | * Handshake functions |
| 7574 | */ |
| 7575 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7576 | /* No certificate support -> dummy functions */ |
| 7577 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
| 7578 | { |
| 7579 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7580 | ssl->handshake->ciphersuite_info; |
| 7581 | |
| 7582 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
| 7583 | |
| 7584 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7585 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
| 7586 | mbedtls_ssl_handshake_increment_state(ssl); |
| 7587 | return 0; |
| 7588 | } |
| 7589 | |
| 7590 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7591 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7592 | } |
| 7593 | |
| 7594 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
| 7595 | { |
| 7596 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7597 | ssl->handshake->ciphersuite_info; |
| 7598 | |
| 7599 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
| 7600 | |
| 7601 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7602 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
| 7603 | mbedtls_ssl_handshake_increment_state(ssl); |
| 7604 | return 0; |
| 7605 | } |
| 7606 | |
| 7607 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7608 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7609 | } |
| 7610 | |
| 7611 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7612 | /* Some certificate support -> implement write and parse */ |
| 7613 | |
| 7614 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
| 7615 | { |
| 7616 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 7617 | size_t i, n; |
| 7618 | const mbedtls_x509_crt *crt; |
| 7619 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7620 | ssl->handshake->ciphersuite_info; |
| 7621 | |
| 7622 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
| 7623 | |
| 7624 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7625 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
| 7626 | mbedtls_ssl_handshake_increment_state(ssl); |
| 7627 | return 0; |
| 7628 | } |
| 7629 | |
| 7630 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7631 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7632 | if (ssl->handshake->client_auth == 0) { |
| 7633 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
| 7634 | mbedtls_ssl_handshake_increment_state(ssl); |
| 7635 | return 0; |
| 7636 | } |
| 7637 | } |
| 7638 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7639 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7640 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7641 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
| 7642 | /* Should never happen because we shouldn't have picked the |
| 7643 | * ciphersuite if we don't have a certificate. */ |
| 7644 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7645 | } |
| 7646 | } |
| 7647 | #endif |
| 7648 | |
| 7649 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
| 7650 | |
| 7651 | /* |
| 7652 | * 0 . 0 handshake type |
| 7653 | * 1 . 3 handshake length |
| 7654 | * 4 . 6 length of all certs |
| 7655 | * 7 . 9 length of cert. 1 |
| 7656 | * 10 . n-1 peer certificate |
| 7657 | * n . n+2 length of cert. 2 |
| 7658 | * n+3 . ... upper level cert, etc. |
| 7659 | */ |
| 7660 | i = 7; |
| 7661 | crt = mbedtls_ssl_own_cert(ssl); |
| 7662 | |
| 7663 | while (crt != NULL) { |
| 7664 | n = crt->raw.len; |
| 7665 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 7666 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 7667 | " > %" MBEDTLS_PRINTF_SIZET, |
| 7668 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 7669 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 7670 | } |
| 7671 | |
| 7672 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 7673 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 7674 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
| 7675 | |
| 7676 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
| 7677 | i += n; crt = crt->next; |
| 7678 | } |
| 7679 | |
| 7680 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 7681 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 7682 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
| 7683 | |
| 7684 | ssl->out_msglen = i; |
| 7685 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7686 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 7687 | |
| 7688 | mbedtls_ssl_handshake_increment_state(ssl); |
| 7689 | |
| 7690 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7691 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7692 | return ret; |
| 7693 | } |
| 7694 | |
| 7695 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
| 7696 | |
| 7697 | return ret; |
| 7698 | } |
| 7699 | |
| 7700 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7701 | |
| 7702 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7703 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7704 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7705 | unsigned char *crt_buf, |
| 7706 | size_t crt_buf_len) |
| 7707 | { |
| 7708 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 7709 | |
| 7710 | if (peer_crt == NULL) { |
| 7711 | return -1; |
| 7712 | } |
| 7713 | |
| 7714 | if (peer_crt->raw.len != crt_buf_len) { |
| 7715 | return -1; |
| 7716 | } |
| 7717 | |
| 7718 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
| 7719 | } |
| 7720 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7721 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7722 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7723 | unsigned char *crt_buf, |
| 7724 | size_t crt_buf_len) |
| 7725 | { |
| 7726 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7727 | unsigned char const * const peer_cert_digest = |
| 7728 | ssl->session->peer_cert_digest; |
| 7729 | mbedtls_md_type_t const peer_cert_digest_type = |
| 7730 | ssl->session->peer_cert_digest_type; |
| 7731 | mbedtls_md_info_t const * const digest_info = |
| 7732 | mbedtls_md_info_from_type(peer_cert_digest_type); |
| 7733 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 7734 | size_t digest_len; |
| 7735 | |
| 7736 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 7737 | return -1; |
| 7738 | } |
| 7739 | |
| 7740 | digest_len = mbedtls_md_get_size(digest_info); |
| 7741 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 7742 | return -1; |
| 7743 | } |
| 7744 | |
| 7745 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 7746 | if (ret != 0) { |
| 7747 | return -1; |
| 7748 | } |
| 7749 | |
| 7750 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
| 7751 | } |
| 7752 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7753 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7754 | |
| 7755 | /* |
| 7756 | * Once the certificate message is read, parse it into a cert chain and |
| 7757 | * perform basic checks, but leave actual verification to the caller |
| 7758 | */ |
| 7759 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7760 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 7761 | mbedtls_x509_crt *chain) |
| 7762 | { |
| 7763 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7764 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7765 | int crt_cnt = 0; |
| 7766 | #endif |
| 7767 | size_t i, n; |
| 7768 | uint8_t alert; |
| 7769 | |
| 7770 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7771 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7772 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7773 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7774 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7775 | } |
| 7776 | |
| 7777 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 7778 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7779 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7780 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7781 | } |
| 7782 | |
| 7783 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 7784 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7785 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7786 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7787 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7788 | } |
| 7789 | |
| 7790 | i = mbedtls_ssl_hs_hdr_len(ssl); |
| 7791 | |
| 7792 | /* |
| 7793 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 7794 | */ |
| 7795 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
| 7796 | |
| 7797 | if (ssl->in_msg[i] != 0 || |
| 7798 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 7799 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7800 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7801 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7802 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7803 | } |
| 7804 | |
| 7805 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7806 | i += 3; |
| 7807 | |
| 7808 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 7809 | while (i < ssl->in_hslen) { |
| 7810 | /* Check that there's room for the next CRT's length fields. */ |
| 7811 | if (i + 3 > ssl->in_hslen) { |
| 7812 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7813 | mbedtls_ssl_send_alert_message(ssl, |
| 7814 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7815 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7816 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7817 | } |
| 7818 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7819 | * anything beyond 2**16 ~ 64K. */ |
| 7820 | if (ssl->in_msg[i] != 0) { |
| 7821 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7822 | mbedtls_ssl_send_alert_message(ssl, |
| 7823 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7824 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7825 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7826 | } |
| 7827 | |
| 7828 | /* Read length of the next CRT in the chain. */ |
| 7829 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
| 7830 | i += 3; |
| 7831 | |
| 7832 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7833 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7834 | mbedtls_ssl_send_alert_message(ssl, |
| 7835 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7836 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7837 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7838 | } |
| 7839 | |
| 7840 | /* Check if we're handling the first CRT in the chain. */ |
| 7841 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7842 | if (crt_cnt++ == 0 && |
| 7843 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 7844 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 7845 | /* During client-side renegotiation, check that the server's |
| 7846 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7847 | * mitigating the triple handshake attack. On success, reuse |
| 7848 | * the original end-CRT instead of parsing it again. */ |
| 7849 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7850 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7851 | &ssl->in_msg[i], |
| 7852 | n) != 0) { |
| 7853 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7854 | mbedtls_ssl_send_alert_message(ssl, |
| 7855 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7856 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7857 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7858 | } |
| 7859 | |
| 7860 | /* Now we can safely free the original chain. */ |
| 7861 | ssl_clear_peer_cert(ssl->session); |
| 7862 | } |
| 7863 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7864 | |
| 7865 | /* Parse the next certificate in the chain. */ |
| 7866 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7867 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
| 7868 | #else |
| 7869 | /* If we don't need to store the CRT chain permanently, parse |
| 7870 | * it in-place from the input buffer instead of making a copy. */ |
| 7871 | ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n); |
| 7872 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7873 | switch (ret) { |
| 7874 | case 0: /*ok*/ |
| 7875 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7876 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7877 | prior certificate was already trusted. */ |
| 7878 | break; |
| 7879 | |
| 7880 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7881 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7882 | goto crt_parse_der_failed; |
| 7883 | |
| 7884 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7885 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7886 | goto crt_parse_der_failed; |
| 7887 | |
| 7888 | default: |
| 7889 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7890 | crt_parse_der_failed: |
| 7891 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7892 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7893 | return ret; |
| 7894 | } |
| 7895 | |
| 7896 | i += n; |
| 7897 | } |
| 7898 | |
| 7899 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7900 | return 0; |
| 7901 | } |
| 7902 | |
| 7903 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7904 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7905 | static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl) |
| 7906 | { |
| 7907 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7908 | return -1; |
| 7909 | } |
| 7910 | |
| 7911 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
| 7912 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7913 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 7914 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7915 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7916 | return 0; |
| 7917 | } |
| 7918 | return -1; |
| 7919 | } |
| 7920 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7921 | |
| 7922 | /* Check if a certificate message is expected. |
| 7923 | * Return either |
| 7924 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7925 | * - SSL_CERTIFICATE_SKIP |
| 7926 | * indicating whether a Certificate message is expected or not. |
| 7927 | */ |
| 7928 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7929 | #define SSL_CERTIFICATE_SKIP 1 |
| 7930 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7931 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7932 | int authmode) |
| 7933 | { |
| 7934 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7935 | ssl->handshake->ciphersuite_info; |
| 7936 | |
| 7937 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7938 | ssl->session_negotiate->verify_result = 0; |
| 7939 | return SSL_CERTIFICATE_SKIP; |
| 7940 | } |
| 7941 | |
| 7942 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7943 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7944 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7945 | return SSL_CERTIFICATE_SKIP; |
| 7946 | } |
| 7947 | |
| 7948 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 7949 | ssl->session_negotiate->verify_result = |
| 7950 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 7951 | return SSL_CERTIFICATE_SKIP; |
| 7952 | } |
| 7953 | } |
| 7954 | #else |
| 7955 | ((void) authmode); |
| 7956 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7957 | |
| 7958 | return SSL_CERTIFICATE_EXPECTED; |
| 7959 | } |
| 7960 | |
| 7961 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7962 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7963 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7964 | unsigned char *start, size_t len) |
| 7965 | { |
| 7966 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7967 | /* Remember digest of the peer's end-CRT. */ |
| 7968 | ssl->session_negotiate->peer_cert_digest = |
| 7969 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7970 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7971 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7972 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7973 | mbedtls_ssl_send_alert_message(ssl, |
| 7974 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7975 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 7976 | |
| 7977 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7978 | } |
| 7979 | |
| 7980 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7981 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7982 | start, len, |
| 7983 | ssl->session_negotiate->peer_cert_digest); |
| 7984 | |
| 7985 | ssl->session_negotiate->peer_cert_digest_type = |
| 7986 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7987 | ssl->session_negotiate->peer_cert_digest_len = |
| 7988 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7989 | |
| 7990 | return ret; |
| 7991 | } |
| 7992 | |
| 7993 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 7994 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7995 | unsigned char *start, size_t len) |
| 7996 | { |
| 7997 | unsigned char *end = start + len; |
| 7998 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7999 | |
| 8000 | /* Make a copy of the peer's raw public key. */ |
| 8001 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 8002 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 8003 | &ssl->handshake->peer_pubkey); |
| 8004 | if (ret != 0) { |
| 8005 | /* We should have parsed the public key before. */ |
| 8006 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8007 | } |
| 8008 | |
| 8009 | return 0; |
| 8010 | } |
| 8011 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8012 | |
| 8013 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
| 8014 | { |
| 8015 | int ret = 0; |
| 8016 | int crt_expected; |
| 8017 | /* Authmode: precedence order is SNI if used else configuration */ |
| 8018 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8019 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 8020 | ? ssl->handshake->sni_authmode |
| 8021 | : ssl->conf->authmode; |
| 8022 | #else |
| 8023 | const int authmode = ssl->conf->authmode; |
| 8024 | #endif |
| 8025 | void *rs_ctx = NULL; |
| 8026 | mbedtls_x509_crt *chain = NULL; |
| 8027 | |
| 8028 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
| 8029 | |
| 8030 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 8031 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 8032 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
| 8033 | goto exit; |
| 8034 | } |
| 8035 | |
| 8036 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 8037 | if (ssl->handshake->ecrs_enabled && |
| 8038 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
| 8039 | chain = ssl->handshake->ecrs_peer_cert; |
| 8040 | ssl->handshake->ecrs_peer_cert = NULL; |
| 8041 | goto crt_verify; |
| 8042 | } |
| 8043 | #endif |
| 8044 | |
| 8045 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8046 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 8047 | let it decide whether to alert. */ |
| 8048 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 8049 | goto exit; |
| 8050 | } |
| 8051 | |
| 8052 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8053 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
| 8054 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 8055 | |
| 8056 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
| 8057 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 8058 | } |
| 8059 | |
| 8060 | goto exit; |
| 8061 | } |
| 8062 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8063 | |
| 8064 | /* Clear existing peer CRT structure in case we tried to |
| 8065 | * reuse a session but it failed, and allocate a new one. */ |
| 8066 | ssl_clear_peer_cert(ssl->session_negotiate); |
| 8067 | |
| 8068 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 8069 | if (chain == NULL) { |
| 8070 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 8071 | sizeof(mbedtls_x509_crt))); |
| 8072 | mbedtls_ssl_send_alert_message(ssl, |
| 8073 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8074 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8075 | |
| 8076 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 8077 | goto exit; |
| 8078 | } |
| 8079 | mbedtls_x509_crt_init(chain); |
| 8080 | |
| 8081 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 8082 | if (ret != 0) { |
| 8083 | goto exit; |
| 8084 | } |
| 8085 | |
| 8086 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 8087 | if (ssl->handshake->ecrs_enabled) { |
| 8088 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 8089 | } |
| 8090 | |
| 8091 | crt_verify: |
| 8092 | if (ssl->handshake->ecrs_enabled) { |
| 8093 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 8094 | } |
| 8095 | #endif |
| 8096 | |
| 8097 | ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain, |
| 8098 | ssl->handshake->ciphersuite_info, |
| 8099 | rs_ctx); |
| 8100 | if (ret != 0) { |
| 8101 | goto exit; |
| 8102 | } |
| 8103 | |
| 8104 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8105 | { |
| 8106 | unsigned char *crt_start, *pk_start; |
| 8107 | size_t crt_len, pk_len; |
| 8108 | |
| 8109 | /* We parse the CRT chain without copying, so |
| 8110 | * these pointers point into the input buffer, |
| 8111 | * and are hence still valid after freeing the |
| 8112 | * CRT chain. */ |
| 8113 | |
| 8114 | crt_start = chain->raw.p; |
| 8115 | crt_len = chain->raw.len; |
| 8116 | |
| 8117 | pk_start = chain->pk_raw.p; |
| 8118 | pk_len = chain->pk_raw.len; |
| 8119 | |
| 8120 | /* Free the CRT structures before computing |
| 8121 | * digest and copying the peer's public key. */ |
| 8122 | mbedtls_x509_crt_free(chain); |
| 8123 | mbedtls_free(chain); |
| 8124 | chain = NULL; |
| 8125 | |
| 8126 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 8127 | if (ret != 0) { |
| 8128 | goto exit; |
| 8129 | } |
| 8130 | |
| 8131 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 8132 | if (ret != 0) { |
| 8133 | goto exit; |
| 8134 | } |
| 8135 | } |
| 8136 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8137 | /* Pass ownership to session structure. */ |
| 8138 | ssl->session_negotiate->peer_cert = chain; |
| 8139 | chain = NULL; |
| 8140 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8141 | |
| 8142 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
| 8143 | |
| 8144 | exit: |
| 8145 | |
| 8146 | if (ret == 0) { |
| 8147 | mbedtls_ssl_handshake_increment_state(ssl); |
| 8148 | } |
| 8149 | |
| 8150 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 8151 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
| 8152 | ssl->handshake->ecrs_peer_cert = chain; |
| 8153 | chain = NULL; |
| 8154 | } |
| 8155 | #endif |
| 8156 | |
| 8157 | if (chain != NULL) { |
| 8158 | mbedtls_x509_crt_free(chain); |
| 8159 | mbedtls_free(chain); |
| 8160 | } |
| 8161 | |
| 8162 | return ret; |
| 8163 | } |
| 8164 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8165 | |
| 8166 | static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx, |
| 8167 | unsigned char *padbuf, size_t hlen, |
| 8168 | unsigned char *buf, int from) |
| 8169 | { |
| 8170 | unsigned int len = 12; |
| 8171 | const char *sender; |
| 8172 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8173 | psa_status_t status; |
| 8174 | psa_hash_operation_t *hs_op = ctx; |
| 8175 | psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT; |
| 8176 | size_t hash_size; |
| 8177 | #else |
| 8178 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8179 | mbedtls_md_context_t *hs_ctx = ctx; |
| 8180 | mbedtls_md_context_t cloned_ctx; |
| 8181 | mbedtls_md_init(&cloned_ctx); |
| 8182 | #endif |
| 8183 | |
| 8184 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 8185 | if (!session) { |
| 8186 | session = ssl->session; |
| 8187 | } |
| 8188 | |
| 8189 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
| 8190 | ? "client finished" |
| 8191 | : "server finished"; |
| 8192 | |
| 8193 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8194 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls")); |
| 8195 | |
| 8196 | status = psa_hash_clone(hs_op, &cloned_op); |
| 8197 | if (status != PSA_SUCCESS) { |
| 8198 | goto exit; |
| 8199 | } |
| 8200 | |
| 8201 | status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size); |
| 8202 | if (status != PSA_SUCCESS) { |
| 8203 | goto exit; |
| 8204 | } |
| 8205 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen); |
| 8206 | #else |
| 8207 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls")); |
| 8208 | |
| 8209 | ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0); |
| 8210 | if (ret != 0) { |
| 8211 | goto exit; |
| 8212 | } |
| 8213 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
| 8214 | if (ret != 0) { |
| 8215 | goto exit; |
| 8216 | } |
| 8217 | |
| 8218 | ret = mbedtls_md_finish(&cloned_ctx, padbuf); |
| 8219 | if (ret != 0) { |
| 8220 | goto exit; |
| 8221 | } |
| 8222 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8223 | |
| 8224 | MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen); |
| 8225 | |
| 8226 | /* |
| 8227 | * TLSv1.2: |
| 8228 | * hash = PRF( master, finished_label, |
| 8229 | * Hash( handshake ) )[0.11] |
| 8230 | */ |
| 8231 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 8232 | padbuf, hlen, buf, len); |
| 8233 | |
| 8234 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
| 8235 | |
| 8236 | mbedtls_platform_zeroize(padbuf, hlen); |
| 8237 | |
| 8238 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
| 8239 | |
| 8240 | exit: |
| 8241 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8242 | psa_hash_abort(&cloned_op); |
| 8243 | return mbedtls_md_error_from_psa(status); |
| 8244 | #else |
| 8245 | mbedtls_md_free(&cloned_ctx); |
| 8246 | return ret; |
| 8247 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8248 | } |
| 8249 | |
| 8250 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 8251 | static int ssl_calc_finished_tls_sha256( |
| 8252 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
| 8253 | { |
| 8254 | unsigned char padbuf[32]; |
| 8255 | return ssl_calc_finished_tls_generic(ssl, |
| 8256 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8257 | &ssl->handshake->fin_sha256_psa, |
| 8258 | #else |
| 8259 | &ssl->handshake->fin_sha256, |
| 8260 | #endif |
| 8261 | padbuf, sizeof(padbuf), |
| 8262 | buf, from); |
| 8263 | } |
| 8264 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
| 8265 | |
| 8266 | |
| 8267 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 8268 | static int ssl_calc_finished_tls_sha384( |
| 8269 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
| 8270 | { |
| 8271 | unsigned char padbuf[48]; |
| 8272 | return ssl_calc_finished_tls_generic(ssl, |
| 8273 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8274 | &ssl->handshake->fin_sha384_psa, |
| 8275 | #else |
| 8276 | &ssl->handshake->fin_sha384, |
| 8277 | #endif |
| 8278 | padbuf, sizeof(padbuf), |
| 8279 | buf, from); |
| 8280 | } |
| 8281 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 8282 | |
| 8283 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
| 8284 | { |
| 8285 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
| 8286 | |
| 8287 | /* |
| 8288 | * Free our handshake params |
| 8289 | */ |
| 8290 | mbedtls_ssl_handshake_free(ssl); |
| 8291 | mbedtls_free(ssl->handshake); |
| 8292 | ssl->handshake = NULL; |
| 8293 | |
| 8294 | /* |
| 8295 | * Free the previous transform and switch in the current one |
| 8296 | */ |
| 8297 | if (ssl->transform) { |
| 8298 | mbedtls_ssl_transform_free(ssl->transform); |
| 8299 | mbedtls_free(ssl->transform); |
| 8300 | } |
| 8301 | ssl->transform = ssl->transform_negotiate; |
| 8302 | ssl->transform_negotiate = NULL; |
| 8303 | |
| 8304 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
| 8305 | } |
| 8306 | |
| 8307 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
| 8308 | { |
| 8309 | int resume = ssl->handshake->resume; |
| 8310 | |
| 8311 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
| 8312 | |
| 8313 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8314 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 8315 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 8316 | ssl->renego_records_seen = 0; |
| 8317 | } |
| 8318 | #endif |
| 8319 | |
| 8320 | /* |
| 8321 | * Free the previous session and switch in the current one |
| 8322 | */ |
| 8323 | if (ssl->session) { |
| 8324 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8325 | /* RFC 7366 3.1: keep the EtM state */ |
| 8326 | ssl->session_negotiate->encrypt_then_mac = |
| 8327 | ssl->session->encrypt_then_mac; |
| 8328 | #endif |
| 8329 | |
| 8330 | mbedtls_ssl_session_free(ssl->session); |
| 8331 | mbedtls_free(ssl->session); |
| 8332 | } |
| 8333 | ssl->session = ssl->session_negotiate; |
| 8334 | ssl->session_negotiate = NULL; |
| 8335 | |
| 8336 | /* |
| 8337 | * Add cache entry |
| 8338 | */ |
| 8339 | if (ssl->conf->f_set_cache != NULL && |
| 8340 | ssl->session->id_len != 0 && |
| 8341 | resume == 0) { |
| 8342 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 8343 | ssl->session->id, |
| 8344 | ssl->session->id_len, |
| 8345 | ssl->session) != 0) { |
| 8346 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 8347 | } |
| 8348 | } |
| 8349 | |
| 8350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8351 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8352 | ssl->handshake->flight != NULL) { |
| 8353 | /* Cancel handshake timer */ |
| 8354 | mbedtls_ssl_set_timer(ssl, 0); |
| 8355 | |
| 8356 | /* Keep last flight around in case we need to resend it: |
| 8357 | * we need the handshake and transform structures for that */ |
| 8358 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 8359 | } else |
| 8360 | #endif |
| 8361 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
| 8362 | |
| 8363 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); |
| 8364 | |
| 8365 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
| 8366 | } |
| 8367 | |
| 8368 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
| 8369 | { |
| 8370 | int ret; |
| 8371 | unsigned int hash_len; |
| 8372 | |
| 8373 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
| 8374 | |
| 8375 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
| 8376 | |
| 8377 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
| 8378 | if (ret != 0) { |
| 8379 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8380 | return ret; |
| 8381 | } |
| 8382 | |
| 8383 | /* |
| 8384 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8385 | * may define some other value. Currently (early 2016), no defined |
| 8386 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8387 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8388 | */ |
| 8389 | hash_len = 12; |
| 8390 | |
| 8391 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8392 | ssl->verify_data_len = hash_len; |
| 8393 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
| 8394 | #endif |
| 8395 | |
| 8396 | ssl->out_msglen = 4 + hash_len; |
| 8397 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8398 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 8399 | |
| 8400 | /* |
| 8401 | * In case of session resuming, invert the client and server |
| 8402 | * ChangeCipherSpec messages order. |
| 8403 | */ |
| 8404 | if (ssl->handshake->resume != 0) { |
| 8405 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8406 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 8407 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); |
| 8408 | } |
| 8409 | #endif |
| 8410 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8411 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 8412 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC); |
| 8413 | } |
| 8414 | #endif |
| 8415 | } else { |
| 8416 | mbedtls_ssl_handshake_increment_state(ssl); |
| 8417 | } |
| 8418 | |
| 8419 | /* |
| 8420 | * Switch to our negotiated transform and session parameters for outbound |
| 8421 | * data. |
| 8422 | */ |
| 8423 | MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data")); |
| 8424 | |
| 8425 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8426 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8427 | unsigned char i; |
| 8428 | |
| 8429 | /* Remember current epoch settings for resending */ |
| 8430 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 8431 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 8432 | sizeof(ssl->handshake->alt_out_ctr)); |
| 8433 | |
| 8434 | /* Set sequence_number to zero */ |
| 8435 | memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2); |
| 8436 | |
| 8437 | |
| 8438 | /* Increment epoch */ |
| 8439 | for (i = 2; i > 0; i--) { |
| 8440 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
| 8441 | break; |
| 8442 | } |
| 8443 | } |
| 8444 | |
| 8445 | /* The loop goes to its end iff the counter is wrapping */ |
| 8446 | if (i == 0) { |
| 8447 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 8448 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
| 8449 | } |
| 8450 | } else |
| 8451 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8452 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
| 8453 | |
| 8454 | ssl->transform_out = ssl->transform_negotiate; |
| 8455 | ssl->session_out = ssl->session_negotiate; |
| 8456 | |
| 8457 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8458 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8459 | mbedtls_ssl_send_flight_completed(ssl); |
| 8460 | } |
| 8461 | #endif |
| 8462 | |
| 8463 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 8464 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 8465 | return ret; |
| 8466 | } |
| 8467 | |
| 8468 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8469 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8470 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 8471 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 8472 | return ret; |
| 8473 | } |
| 8474 | #endif |
| 8475 | |
| 8476 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
| 8477 | |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
| 8481 | #define SSL_MAX_HASH_LEN 12 |
| 8482 | |
| 8483 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
| 8484 | { |
| 8485 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8486 | unsigned int hash_len = 12; |
| 8487 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 8488 | |
| 8489 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
| 8490 | |
| 8491 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
| 8492 | if (ret != 0) { |
| 8493 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8494 | return ret; |
| 8495 | } |
| 8496 | |
| 8497 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8498 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 8499 | goto exit; |
| 8500 | } |
| 8501 | |
| 8502 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 8503 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8504 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8505 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 8506 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8507 | goto exit; |
| 8508 | } |
| 8509 | |
| 8510 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 8511 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8512 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 8513 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8514 | goto exit; |
| 8515 | } |
| 8516 | |
| 8517 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 8518 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8519 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8520 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 8521 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 8522 | goto exit; |
| 8523 | } |
| 8524 | |
| 8525 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 8526 | buf, hash_len) != 0) { |
| 8527 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8528 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8529 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
| 8530 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8531 | goto exit; |
| 8532 | } |
| 8533 | |
| 8534 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8535 | ssl->verify_data_len = hash_len; |
| 8536 | memcpy(ssl->peer_verify_data, buf, hash_len); |
| 8537 | #endif |
| 8538 | |
| 8539 | if (ssl->handshake->resume != 0) { |
| 8540 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8541 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 8542 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC); |
| 8543 | } |
| 8544 | #endif |
| 8545 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8546 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 8547 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); |
| 8548 | } |
| 8549 | #endif |
| 8550 | } else { |
| 8551 | mbedtls_ssl_handshake_increment_state(ssl); |
| 8552 | } |
| 8553 | |
| 8554 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8555 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8556 | mbedtls_ssl_recv_flight_completed(ssl); |
| 8557 | } |
| 8558 | #endif |
| 8559 | |
| 8560 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
| 8561 | |
| 8562 | exit: |
| 8563 | mbedtls_platform_zeroize(buf, hash_len); |
| 8564 | return ret; |
| 8565 | } |
| 8566 | |
| 8567 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8568 | /* |
| 8569 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 8570 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 8571 | */ |
| 8572 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
| 8573 | { |
| 8574 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 8575 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
| 8576 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 8577 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 8578 | return tls_prf_sha384; |
| 8579 | } else |
| 8580 | #endif |
| 8581 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 8582 | { |
| 8583 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 8584 | return tls_prf_sha256; |
| 8585 | } |
| 8586 | } |
| 8587 | #endif |
| 8588 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 8589 | !defined(MBEDTLS_MD_CAN_SHA256) |
| 8590 | (void) ciphersuite_info; |
| 8591 | #endif |
| 8592 | |
| 8593 | return NULL; |
| 8594 | } |
| 8595 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 8596 | |
| 8597 | static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf) |
| 8598 | { |
| 8599 | ((void) tls_prf); |
| 8600 | #if defined(MBEDTLS_MD_CAN_SHA384) |
| 8601 | if (tls_prf == tls_prf_sha384) { |
| 8602 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8603 | } else |
| 8604 | #endif |
| 8605 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 8606 | if (tls_prf == tls_prf_sha256) { |
| 8607 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8608 | } else |
| 8609 | #endif |
| 8610 | return MBEDTLS_SSL_TLS_PRF_NONE; |
| 8611 | } |
| 8612 | |
| 8613 | /* |
| 8614 | * Populate a transform structure with session keys and all the other |
| 8615 | * necessary information. |
| 8616 | * |
| 8617 | * Parameters: |
| 8618 | * - [in/out]: transform: structure to populate |
| 8619 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8620 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8621 | * - [in] ciphersuite |
| 8622 | * - [in] master |
| 8623 | * - [in] encrypt_then_mac |
| 8624 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8625 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
| 8626 | * - [in] tls_version: TLS version |
| 8627 | * - [in] endpoint: client or server |
| 8628 | * - [in] ssl: used for: |
| 8629 | * - ssl->conf->{f,p}_export_keys |
| 8630 | * [in] optionally used for: |
| 8631 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8632 | */ |
| 8633 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 8634 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8635 | int ciphersuite, |
| 8636 | const unsigned char master[48], |
| 8637 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8638 | int encrypt_then_mac, |
| 8639 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 8640 | ssl_tls_prf_t tls_prf, |
| 8641 | const unsigned char randbytes[64], |
| 8642 | mbedtls_ssl_protocol_version tls_version, |
| 8643 | unsigned endpoint, |
| 8644 | const mbedtls_ssl_context *ssl) |
| 8645 | { |
| 8646 | int ret = 0; |
| 8647 | unsigned char keyblk[256]; |
| 8648 | unsigned char *key1; |
| 8649 | unsigned char *key2; |
| 8650 | unsigned char *mac_enc; |
| 8651 | unsigned char *mac_dec; |
| 8652 | size_t mac_key_len = 0; |
| 8653 | size_t iv_copy_len; |
| 8654 | size_t keylen; |
| 8655 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 8656 | mbedtls_ssl_mode_t ssl_mode; |
| 8657 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8658 | const mbedtls_cipher_info_t *cipher_info; |
| 8659 | const mbedtls_md_info_t *md_info; |
| 8660 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
| 8661 | |
| 8662 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8663 | psa_key_type_t key_type; |
| 8664 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8665 | psa_algorithm_t alg; |
| 8666 | psa_algorithm_t mac_alg = 0; |
| 8667 | size_t key_bits; |
| 8668 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8669 | #endif |
| 8670 | |
| 8671 | /* |
| 8672 | * Some data just needs copying into the structure |
| 8673 | */ |
| 8674 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8675 | transform->encrypt_then_mac = encrypt_then_mac; |
| 8676 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 8677 | transform->tls_version = tls_version; |
| 8678 | |
| 8679 | #if defined(MBEDTLS_SSL_KEEP_RANDBYTES) |
| 8680 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
| 8681 | #endif |
| 8682 | |
| 8683 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8684 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 8685 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8686 | * generation separate. This should never happen. */ |
| 8687 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8688 | } |
| 8689 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8690 | |
| 8691 | /* |
| 8692 | * Get various info structures |
| 8693 | */ |
| 8694 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8695 | if (ciphersuite_info == NULL) { |
| 8696 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8697 | ciphersuite)); |
| 8698 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8699 | } |
| 8700 | |
| 8701 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
| 8702 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8703 | encrypt_then_mac, |
| 8704 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
| 8705 | ciphersuite_info); |
| 8706 | |
| 8707 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
| 8708 | transform->taglen = |
| 8709 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 8710 | } |
| 8711 | |
| 8712 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8713 | if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, |
| 8714 | transform->taglen, |
| 8715 | &alg, |
| 8716 | &key_type, |
| 8717 | &key_bits)) != PSA_SUCCESS) { |
| 8718 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 8719 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
| 8720 | goto end; |
| 8721 | } |
| 8722 | #else |
| 8723 | cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher); |
| 8724 | if (cipher_info == NULL) { |
| 8725 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8726 | ciphersuite_info->cipher)); |
| 8727 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8728 | } |
| 8729 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8730 | |
| 8731 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8732 | mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac); |
| 8733 | if (mac_alg == 0) { |
| 8734 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found", |
| 8735 | (unsigned) ciphersuite_info->mac)); |
| 8736 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8737 | } |
| 8738 | #else |
| 8739 | md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac); |
| 8740 | if (md_info == NULL) { |
| 8741 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8742 | (unsigned) ciphersuite_info->mac)); |
| 8743 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8744 | } |
| 8745 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8746 | |
| 8747 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8748 | /* Copy own and peer's CID if the use of the CID |
| 8749 | * extension has been negotiated. */ |
| 8750 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8751 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
| 8752 | |
| 8753 | transform->in_cid_len = ssl->own_cid_len; |
| 8754 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8755 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8756 | transform->in_cid_len); |
| 8757 | |
| 8758 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 8759 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8760 | ssl->handshake->peer_cid_len); |
| 8761 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8762 | transform->out_cid_len); |
| 8763 | } |
| 8764 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8765 | |
| 8766 | /* |
| 8767 | * Compute key block using the PRF |
| 8768 | */ |
| 8769 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8770 | if (ret != 0) { |
| 8771 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8772 | return ret; |
| 8773 | } |
| 8774 | |
| 8775 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8776 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8777 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8778 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8779 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
| 8780 | |
| 8781 | /* |
| 8782 | * Determine the appropriate key, IV and MAC length. |
| 8783 | */ |
| 8784 | |
| 8785 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8786 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8787 | #else |
| 8788 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
| 8789 | #endif |
| 8790 | |
| 8791 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
| 8792 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
| 8793 | size_t explicit_ivlen; |
| 8794 | |
| 8795 | transform->maclen = 0; |
| 8796 | mac_key_len = 0; |
| 8797 | |
| 8798 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8799 | * with mode and version: |
| 8800 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8801 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8802 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8803 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8804 | * sequence number). |
| 8805 | */ |
| 8806 | transform->ivlen = 12; |
| 8807 | |
| 8808 | int is_chachapoly = 0; |
| 8809 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8810 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
| 8811 | #else |
| 8812 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8813 | == MBEDTLS_MODE_CHACHAPOLY); |
| 8814 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8815 | |
| 8816 | if (is_chachapoly) { |
| 8817 | transform->fixed_ivlen = 12; |
| 8818 | } else { |
| 8819 | transform->fixed_ivlen = 4; |
| 8820 | } |
| 8821 | |
| 8822 | /* Minimum length of encrypted record */ |
| 8823 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8824 | transform->minlen = explicit_ivlen + transform->taglen; |
| 8825 | } else |
| 8826 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
| 8827 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8828 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 8829 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 8830 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
| 8831 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8832 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
| 8833 | #else |
| 8834 | size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info); |
| 8835 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8836 | |
| 8837 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8838 | /* Get MAC length */ |
| 8839 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8840 | #else |
| 8841 | /* Initialize HMAC contexts */ |
| 8842 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8843 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8844 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
| 8845 | goto end; |
| 8846 | } |
| 8847 | |
| 8848 | /* Get MAC length */ |
| 8849 | mac_key_len = mbedtls_md_get_size(md_info); |
| 8850 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8851 | transform->maclen = mac_key_len; |
| 8852 | |
| 8853 | /* IV length */ |
| 8854 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8855 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
| 8856 | #else |
| 8857 | transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info); |
| 8858 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8859 | |
| 8860 | /* Minimum length */ |
| 8861 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
| 8862 | transform->minlen = transform->maclen; |
| 8863 | } else { |
| 8864 | /* |
| 8865 | * GenericBlockCipher: |
| 8866 | * 1. if EtM is in use: one block plus MAC |
| 8867 | * otherwise: * first multiple of blocklen greater than maclen |
| 8868 | * 2. IV |
| 8869 | */ |
| 8870 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8871 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
| 8872 | transform->minlen = transform->maclen |
| 8873 | + block_size; |
| 8874 | } else |
| 8875 | #endif |
| 8876 | { |
| 8877 | transform->minlen = transform->maclen |
| 8878 | + block_size |
| 8879 | - transform->maclen % block_size; |
| 8880 | } |
| 8881 | |
| 8882 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 8883 | transform->minlen += transform->ivlen; |
| 8884 | } else { |
| 8885 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8886 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8887 | goto end; |
| 8888 | } |
| 8889 | } |
| 8890 | } else |
| 8891 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8892 | { |
| 8893 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8894 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8895 | } |
| 8896 | |
| 8897 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8898 | (unsigned) keylen, |
| 8899 | (unsigned) transform->minlen, |
| 8900 | (unsigned) transform->ivlen, |
| 8901 | (unsigned) transform->maclen)); |
| 8902 | |
| 8903 | /* |
| 8904 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8905 | */ |
| 8906 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8907 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 8908 | key1 = keyblk + mac_key_len * 2; |
| 8909 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8910 | |
| 8911 | mac_enc = keyblk; |
| 8912 | mac_dec = keyblk + mac_key_len; |
| 8913 | |
| 8914 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8915 | transform->fixed_ivlen : transform->ivlen; |
| 8916 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8917 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8918 | iv_copy_len); |
| 8919 | } else |
| 8920 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8921 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8922 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 8923 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8924 | key2 = keyblk + mac_key_len * 2; |
| 8925 | |
| 8926 | mac_enc = keyblk + mac_key_len; |
| 8927 | mac_dec = keyblk; |
| 8928 | |
| 8929 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8930 | transform->fixed_ivlen : transform->ivlen; |
| 8931 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8932 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8933 | iv_copy_len); |
| 8934 | } else |
| 8935 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8936 | { |
| 8937 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8938 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8939 | goto end; |
| 8940 | } |
| 8941 | |
| 8942 | if (ssl->f_export_keys != NULL) { |
| 8943 | ssl->f_export_keys(ssl->p_export_keys, |
| 8944 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8945 | master, 48, |
| 8946 | randbytes + 32, |
| 8947 | randbytes, |
| 8948 | tls_prf_get_type(tls_prf)); |
| 8949 | } |
| 8950 | |
| 8951 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8952 | transform->psa_alg = alg; |
| 8953 | |
| 8954 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8955 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8956 | psa_set_key_algorithm(&attributes, alg); |
| 8957 | psa_set_key_type(&attributes, key_type); |
| 8958 | |
| 8959 | if ((status = psa_import_key(&attributes, |
| 8960 | key1, |
| 8961 | PSA_BITS_TO_BYTES(key_bits), |
| 8962 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8963 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
| 8964 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 8965 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
| 8966 | goto end; |
| 8967 | } |
| 8968 | |
| 8969 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8970 | |
| 8971 | if ((status = psa_import_key(&attributes, |
| 8972 | key2, |
| 8973 | PSA_BITS_TO_BYTES(key_bits), |
| 8974 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
| 8975 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 8976 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
| 8977 | goto end; |
| 8978 | } |
| 8979 | } |
| 8980 | #else |
| 8981 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8982 | cipher_info)) != 0) { |
| 8983 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
| 8984 | goto end; |
| 8985 | } |
| 8986 | |
| 8987 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8988 | cipher_info)) != 0) { |
| 8989 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
| 8990 | goto end; |
| 8991 | } |
| 8992 | |
| 8993 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8994 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8995 | MBEDTLS_ENCRYPT)) != 0) { |
| 8996 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
| 8997 | goto end; |
| 8998 | } |
| 8999 | |
| 9000 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 9001 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 9002 | MBEDTLS_DECRYPT)) != 0) { |
| 9003 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
| 9004 | goto end; |
| 9005 | } |
| 9006 | |
| 9007 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 9008 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 9009 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 9010 | MBEDTLS_PADDING_NONE)) != 0) { |
| 9011 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
| 9012 | goto end; |
| 9013 | } |
| 9014 | |
| 9015 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 9016 | MBEDTLS_PADDING_NONE)) != 0) { |
| 9017 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
| 9018 | goto end; |
| 9019 | } |
| 9020 | } |
| 9021 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 9022 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9023 | |
| 9024 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 9025 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 9026 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 9027 | if (mac_key_len != 0) { |
| 9028 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9029 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
| 9030 | |
| 9031 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 9032 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 9033 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 9034 | |
| 9035 | if ((status = psa_import_key(&attributes, |
| 9036 | mac_enc, mac_key_len, |
| 9037 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
| 9038 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 9039 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
| 9040 | goto end; |
| 9041 | } |
| 9042 | |
| 9043 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 9044 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
| 9045 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 9046 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
| 9047 | #endif |
| 9048 | )) { |
| 9049 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 9050 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 9051 | PSA_KEY_USAGE_VERIFY_HASH); |
| 9052 | } else { |
| 9053 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 9054 | } |
| 9055 | |
| 9056 | if ((status = psa_import_key(&attributes, |
| 9057 | mac_dec, mac_key_len, |
| 9058 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
| 9059 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 9060 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
| 9061 | goto end; |
| 9062 | } |
| 9063 | #else |
| 9064 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 9065 | if (ret != 0) { |
| 9066 | goto end; |
| 9067 | } |
| 9068 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 9069 | if (ret != 0) { |
| 9070 | goto end; |
| 9071 | } |
| 9072 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9073 | } |
| 9074 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 9075 | |
| 9076 | ((void) mac_dec); |
| 9077 | ((void) mac_enc); |
| 9078 | |
| 9079 | end: |
| 9080 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 9081 | return ret; |
| 9082 | } |
| 9083 | |
| 9084 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 9085 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9086 | int mbedtls_psa_ecjpake_read_round( |
| 9087 | psa_pake_operation_t *pake_ctx, |
| 9088 | const unsigned char *buf, |
| 9089 | size_t len, mbedtls_ecjpake_rounds_t round) |
| 9090 | { |
| 9091 | psa_status_t status; |
| 9092 | size_t input_offset = 0; |
| 9093 | /* |
| 9094 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9095 | * At round two perform a single cycle |
| 9096 | */ |
| 9097 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
| 9098 | |
| 9099 | for (; remaining_steps > 0; remaining_steps--) { |
| 9100 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 9101 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 9102 | ++step) { |
| 9103 | /* Length is stored at the first byte */ |
| 9104 | size_t length = buf[input_offset]; |
| 9105 | input_offset += 1; |
| 9106 | |
| 9107 | if (input_offset + length > len) { |
| 9108 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 9109 | } |
| 9110 | |
| 9111 | status = psa_pake_input(pake_ctx, step, |
| 9112 | buf + input_offset, length); |
| 9113 | if (status != PSA_SUCCESS) { |
| 9114 | return PSA_TO_MBEDTLS_ERR(status); |
| 9115 | } |
| 9116 | |
| 9117 | input_offset += length; |
| 9118 | } |
| 9119 | } |
| 9120 | |
| 9121 | if (input_offset != len) { |
| 9122 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 9123 | } |
| 9124 | |
| 9125 | return 0; |
| 9126 | } |
| 9127 | |
| 9128 | int mbedtls_psa_ecjpake_write_round( |
| 9129 | psa_pake_operation_t *pake_ctx, |
| 9130 | unsigned char *buf, |
| 9131 | size_t len, size_t *olen, |
| 9132 | mbedtls_ecjpake_rounds_t round) |
| 9133 | { |
| 9134 | psa_status_t status; |
| 9135 | size_t output_offset = 0; |
| 9136 | size_t output_len; |
| 9137 | /* |
| 9138 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9139 | * At round two perform a single cycle |
| 9140 | */ |
| 9141 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
| 9142 | |
| 9143 | for (; remaining_steps > 0; remaining_steps--) { |
| 9144 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 9145 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 9146 | ++step) { |
| 9147 | /* |
| 9148 | * For each step, prepend 1 byte with the length of the data as |
| 9149 | * given by psa_pake_output(). |
| 9150 | */ |
| 9151 | status = psa_pake_output(pake_ctx, step, |
| 9152 | buf + output_offset + 1, |
| 9153 | len - output_offset - 1, |
| 9154 | &output_len); |
| 9155 | if (status != PSA_SUCCESS) { |
| 9156 | return PSA_TO_MBEDTLS_ERR(status); |
| 9157 | } |
| 9158 | |
| 9159 | *(buf + output_offset) = (uint8_t) output_len; |
| 9160 | |
| 9161 | output_offset += output_len + 1; |
| 9162 | } |
| 9163 | } |
| 9164 | |
| 9165 | *olen = output_offset; |
| 9166 | |
| 9167 | return 0; |
| 9168 | } |
| 9169 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 9170 | |
| 9171 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9172 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9173 | unsigned char *hash, size_t *hashlen, |
| 9174 | unsigned char *data, size_t data_len, |
| 9175 | mbedtls_md_type_t md_alg) |
| 9176 | { |
| 9177 | psa_status_t status; |
| 9178 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 9179 | psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg); |
| 9180 | |
| 9181 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange")); |
| 9182 | |
| 9183 | if ((status = psa_hash_setup(&hash_operation, |
| 9184 | hash_alg)) != PSA_SUCCESS) { |
| 9185 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
| 9186 | goto exit; |
| 9187 | } |
| 9188 | |
| 9189 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 9190 | 64)) != PSA_SUCCESS) { |
| 9191 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
| 9192 | goto exit; |
| 9193 | } |
| 9194 | |
| 9195 | if ((status = psa_hash_update(&hash_operation, |
| 9196 | data, data_len)) != PSA_SUCCESS) { |
| 9197 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
| 9198 | goto exit; |
| 9199 | } |
| 9200 | |
| 9201 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 9202 | hashlen)) != PSA_SUCCESS) { |
| 9203 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 9204 | goto exit; |
| 9205 | } |
| 9206 | |
| 9207 | exit: |
| 9208 | if (status != PSA_SUCCESS) { |
| 9209 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9210 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9211 | switch (status) { |
| 9212 | case PSA_ERROR_NOT_SUPPORTED: |
| 9213 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
| 9214 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 9215 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 9216 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 9217 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 9218 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
| 9219 | default: |
| 9220 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 9221 | } |
| 9222 | } |
| 9223 | return 0; |
| 9224 | } |
| 9225 | |
| 9226 | #else |
| 9227 | |
| 9228 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9229 | unsigned char *hash, size_t *hashlen, |
| 9230 | unsigned char *data, size_t data_len, |
| 9231 | mbedtls_md_type_t md_alg) |
| 9232 | { |
| 9233 | int ret = 0; |
| 9234 | mbedtls_md_context_t ctx; |
| 9235 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 9236 | *hashlen = mbedtls_md_get_size(md_info); |
| 9237 | |
| 9238 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange")); |
| 9239 | |
| 9240 | mbedtls_md_init(&ctx); |
| 9241 | |
| 9242 | /* |
| 9243 | * digitally-signed struct { |
| 9244 | * opaque client_random[32]; |
| 9245 | * opaque server_random[32]; |
| 9246 | * ServerDHParams params; |
| 9247 | * }; |
| 9248 | */ |
| 9249 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 9250 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
| 9251 | goto exit; |
| 9252 | } |
| 9253 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 9254 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
| 9255 | goto exit; |
| 9256 | } |
| 9257 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 9258 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
| 9259 | goto exit; |
| 9260 | } |
| 9261 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 9262 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
| 9263 | goto exit; |
| 9264 | } |
| 9265 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 9266 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
| 9267 | goto exit; |
| 9268 | } |
| 9269 | |
| 9270 | exit: |
| 9271 | mbedtls_md_free(&ctx); |
| 9272 | |
| 9273 | if (ret != 0) { |
| 9274 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9275 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9276 | } |
| 9277 | |
| 9278 | return ret; |
| 9279 | } |
| 9280 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9281 | |
| 9282 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 9283 | |
| 9284 | /* Find the preferred hash for a given signature algorithm. */ |
| 9285 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 9286 | mbedtls_ssl_context *ssl, |
| 9287 | unsigned int sig_alg) |
| 9288 | { |
| 9289 | unsigned int i; |
| 9290 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
| 9291 | |
| 9292 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 9293 | return MBEDTLS_SSL_HASH_NONE; |
| 9294 | } |
| 9295 | |
| 9296 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 9297 | unsigned int hash_alg_received = |
| 9298 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 9299 | received_sig_algs[i]); |
| 9300 | unsigned int sig_alg_received = |
| 9301 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 9302 | received_sig_algs[i]); |
| 9303 | |
| 9304 | mbedtls_md_type_t md_alg = |
| 9305 | mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received); |
| 9306 | if (md_alg == MBEDTLS_MD_NONE) { |
| 9307 | continue; |
| 9308 | } |
| 9309 | |
| 9310 | if (sig_alg == sig_alg_received) { |
| 9311 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9312 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
| 9313 | psa_algorithm_t psa_hash_alg = |
| 9314 | mbedtls_md_psa_alg_from_type(md_alg); |
| 9315 | |
| 9316 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 9317 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9318 | PSA_ALG_ECDSA(psa_hash_alg), |
| 9319 | PSA_KEY_USAGE_SIGN_HASH)) { |
| 9320 | continue; |
| 9321 | } |
| 9322 | |
| 9323 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 9324 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9325 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 9326 | psa_hash_alg), |
| 9327 | PSA_KEY_USAGE_SIGN_HASH)) { |
| 9328 | continue; |
| 9329 | } |
| 9330 | } |
| 9331 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9332 | |
| 9333 | return hash_alg_received; |
| 9334 | } |
| 9335 | } |
| 9336 | |
| 9337 | return MBEDTLS_SSL_HASH_NONE; |
| 9338 | } |
| 9339 | |
| 9340 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 9341 | |
| 9342 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9343 | |
| 9344 | int mbedtls_ssl_validate_ciphersuite( |
| 9345 | const mbedtls_ssl_context *ssl, |
| 9346 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9347 | mbedtls_ssl_protocol_version min_tls_version, |
| 9348 | mbedtls_ssl_protocol_version max_tls_version) |
| 9349 | { |
| 9350 | (void) ssl; |
| 9351 | |
| 9352 | if (suite_info == NULL) { |
| 9353 | return -1; |
| 9354 | } |
| 9355 | |
| 9356 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9357 | (suite_info->max_tls_version < min_tls_version)) { |
| 9358 | return -1; |
| 9359 | } |
| 9360 | |
| 9361 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
| 9362 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 9363 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9364 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9365 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
| 9366 | #else |
| 9367 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9368 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
| 9369 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9370 | { |
| 9371 | return -1; |
| 9372 | } |
| 9373 | #endif |
| 9374 | |
| 9375 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9376 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 9377 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9378 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9379 | return -1; |
| 9380 | } |
| 9381 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9382 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9383 | |
| 9384 | return 0; |
| 9385 | } |
| 9386 | |
| 9387 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 9388 | /* |
| 9389 | * Function for writing a signature algorithm extension. |
| 9390 | * |
| 9391 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9392 | * value (TLS 1.3 RFC8446): |
| 9393 | * enum { |
| 9394 | * .... |
| 9395 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9396 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9397 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9398 | * .... |
| 9399 | * } SignatureScheme; |
| 9400 | * |
| 9401 | * struct { |
| 9402 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9403 | * } SignatureSchemeList; |
| 9404 | * |
| 9405 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9406 | * value (TLS 1.2 RFC5246): |
| 9407 | * enum { |
| 9408 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9409 | * sha512(6), (255) |
| 9410 | * } HashAlgorithm; |
| 9411 | * |
| 9412 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9413 | * SignatureAlgorithm; |
| 9414 | * |
| 9415 | * struct { |
| 9416 | * HashAlgorithm hash; |
| 9417 | * SignatureAlgorithm signature; |
| 9418 | * } SignatureAndHashAlgorithm; |
| 9419 | * |
| 9420 | * SignatureAndHashAlgorithm |
| 9421 | * supported_signature_algorithms<2..2^16-2>; |
| 9422 | * |
| 9423 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9424 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9425 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9426 | * `SignatureScheme` field of TLS 1.3 |
| 9427 | * |
| 9428 | */ |
| 9429 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9430 | const unsigned char *end, size_t *out_len) |
| 9431 | { |
| 9432 | unsigned char *p = buf; |
| 9433 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9434 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9435 | |
| 9436 | *out_len = 0; |
| 9437 | |
| 9438 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
| 9439 | |
| 9440 | /* Check if we have space for header and length field: |
| 9441 | * - extension_type (2 bytes) |
| 9442 | * - extension_data_length (2 bytes) |
| 9443 | * - supported_signature_algorithms_length (2 bytes) |
| 9444 | */ |
| 9445 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
| 9446 | p += 6; |
| 9447 | |
| 9448 | /* |
| 9449 | * Write supported_signature_algorithms |
| 9450 | */ |
| 9451 | supported_sig_alg = p; |
| 9452 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9453 | if (sig_alg == NULL) { |
| 9454 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9455 | } |
| 9456 | |
| 9457 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9458 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9459 | *sig_alg, |
| 9460 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9461 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
| 9462 | continue; |
| 9463 | } |
| 9464 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9465 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
| 9466 | p += 2; |
| 9467 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9468 | *sig_alg, |
| 9469 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9470 | } |
| 9471 | |
| 9472 | /* Length of supported_signature_algorithms */ |
| 9473 | supported_sig_alg_len = (size_t) (p - supported_sig_alg); |
| 9474 | if (supported_sig_alg_len == 0) { |
| 9475 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9476 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 9477 | } |
| 9478 | |
| 9479 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9480 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9481 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
| 9482 | |
| 9483 | *out_len = (size_t) (p - buf); |
| 9484 | |
| 9485 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 9486 | mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG); |
| 9487 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 9488 | |
| 9489 | return 0; |
| 9490 | } |
| 9491 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 9492 | |
| 9493 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9494 | /* |
| 9495 | * mbedtls_ssl_parse_server_name_ext |
| 9496 | * |
| 9497 | * Structure of server_name extension: |
| 9498 | * |
| 9499 | * enum { |
| 9500 | * host_name(0), (255) |
| 9501 | * } NameType; |
| 9502 | * opaque HostName<1..2^16-1>; |
| 9503 | * |
| 9504 | * struct { |
| 9505 | * NameType name_type; |
| 9506 | * select (name_type) { |
| 9507 | * case host_name: HostName; |
| 9508 | * } name; |
| 9509 | * } ServerName; |
| 9510 | * struct { |
| 9511 | * ServerName server_name_list<1..2^16-1> |
| 9512 | * } ServerNameList; |
| 9513 | */ |
| 9514 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 9515 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9516 | const unsigned char *buf, |
| 9517 | const unsigned char *end) |
| 9518 | { |
| 9519 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9520 | const unsigned char *p = buf; |
| 9521 | size_t server_name_list_len, hostname_len; |
| 9522 | const unsigned char *server_name_list_end; |
| 9523 | |
| 9524 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
| 9525 | |
| 9526 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9527 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 9528 | p += 2; |
| 9529 | |
| 9530 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
| 9531 | server_name_list_end = p + server_name_list_len; |
| 9532 | while (p < server_name_list_end) { |
| 9533 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9534 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9535 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9536 | hostname_len + 3); |
| 9537 | |
| 9538 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
| 9539 | /* sni_name is intended to be used only during the parsing of the |
| 9540 | * ClientHello message (it is reset to NULL before the end of |
| 9541 | * the message parsing). Thus it is ok to just point to the |
| 9542 | * reception buffer and not make a copy of it. |
| 9543 | */ |
| 9544 | ssl->handshake->sni_name = p + 3; |
| 9545 | ssl->handshake->sni_name_len = hostname_len; |
| 9546 | if (ssl->conf->f_sni == NULL) { |
| 9547 | return 0; |
| 9548 | } |
| 9549 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9550 | ssl, p + 3, hostname_len); |
| 9551 | if (ret != 0) { |
| 9552 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9553 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9554 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9555 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9556 | } |
| 9557 | return 0; |
| 9558 | } |
| 9559 | |
| 9560 | p += hostname_len + 3; |
| 9561 | } |
| 9562 | |
| 9563 | return 0; |
| 9564 | } |
| 9565 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9566 | |
| 9567 | #if defined(MBEDTLS_SSL_ALPN) |
| 9568 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 9569 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9570 | const unsigned char *buf, |
| 9571 | const unsigned char *end) |
| 9572 | { |
| 9573 | const unsigned char *p = buf; |
| 9574 | size_t protocol_name_list_len; |
| 9575 | const unsigned char *protocol_name_list; |
| 9576 | const unsigned char *protocol_name_list_end; |
| 9577 | size_t protocol_name_len; |
| 9578 | |
| 9579 | /* If ALPN not configured, just ignore the extension */ |
| 9580 | if (ssl->conf->alpn_list == NULL) { |
| 9581 | return 0; |
| 9582 | } |
| 9583 | |
| 9584 | /* |
| 9585 | * RFC7301, section 3.1 |
| 9586 | * opaque ProtocolName<1..2^8-1>; |
| 9587 | * |
| 9588 | * struct { |
| 9589 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9590 | * } ProtocolNameList; |
| 9591 | */ |
| 9592 | |
| 9593 | /* |
| 9594 | * protocol_name_list_len 2 bytes |
| 9595 | * protocol_name_len 1 bytes |
| 9596 | * protocol_name >=1 byte |
| 9597 | */ |
| 9598 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
| 9599 | |
| 9600 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 9601 | p += 2; |
| 9602 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
| 9603 | protocol_name_list = p; |
| 9604 | protocol_name_list_end = p + protocol_name_list_len; |
| 9605 | |
| 9606 | /* Validate peer's list (lengths) */ |
| 9607 | while (p < protocol_name_list_end) { |
| 9608 | protocol_name_len = *p++; |
| 9609 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9610 | protocol_name_len); |
| 9611 | if (protocol_name_len == 0) { |
| 9612 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9613 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 9614 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9615 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
| 9616 | } |
| 9617 | |
| 9618 | p += protocol_name_len; |
| 9619 | } |
| 9620 | |
| 9621 | /* Use our order of preference */ |
| 9622 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9623 | size_t const alpn_len = strlen(*alpn); |
| 9624 | p = protocol_name_list; |
| 9625 | while (p < protocol_name_list_end) { |
| 9626 | protocol_name_len = *p++; |
| 9627 | if (protocol_name_len == alpn_len && |
| 9628 | memcmp(p, *alpn, alpn_len) == 0) { |
| 9629 | ssl->alpn_chosen = *alpn; |
| 9630 | return 0; |
| 9631 | } |
| 9632 | |
| 9633 | p += protocol_name_len; |
| 9634 | } |
| 9635 | } |
| 9636 | |
| 9637 | /* If we get here, no match was found */ |
| 9638 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9639 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9640 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9641 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
| 9642 | } |
| 9643 | |
| 9644 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9645 | unsigned char *buf, |
| 9646 | unsigned char *end, |
| 9647 | size_t *out_len) |
| 9648 | { |
| 9649 | unsigned char *p = buf; |
| 9650 | size_t protocol_name_len; |
| 9651 | *out_len = 0; |
| 9652 | |
| 9653 | if (ssl->alpn_chosen == NULL) { |
| 9654 | return 0; |
| 9655 | } |
| 9656 | |
| 9657 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9658 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
| 9659 | |
| 9660 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
| 9661 | /* |
| 9662 | * 0 . 1 ext identifier |
| 9663 | * 2 . 3 ext length |
| 9664 | * 4 . 5 protocol list length |
| 9665 | * 6 . 6 protocol name length |
| 9666 | * 7 . 7+n protocol name |
| 9667 | */ |
| 9668 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
| 9669 | |
| 9670 | *out_len = 7 + protocol_name_len; |
| 9671 | |
| 9672 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9673 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
| 9674 | /* Note: the length of the chosen protocol has been checked to be less |
| 9675 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9676 | */ |
| 9677 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
| 9678 | |
| 9679 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
| 9680 | |
| 9681 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 9682 | mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN); |
| 9683 | #endif |
| 9684 | |
| 9685 | return 0; |
| 9686 | } |
| 9687 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9688 | |
| 9689 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 9690 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
| 9691 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 9692 | defined(MBEDTLS_SSL_CLI_C) |
| 9693 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9694 | const char *hostname) |
| 9695 | { |
| 9696 | /* Initialize to suppress unnecessary compiler warning */ |
| 9697 | size_t hostname_len = 0; |
| 9698 | |
| 9699 | /* Check if new hostname is valid before |
| 9700 | * making any change to current one */ |
| 9701 | if (hostname != NULL) { |
| 9702 | hostname_len = strlen(hostname); |
| 9703 | |
| 9704 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9705 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9706 | } |
| 9707 | } |
| 9708 | |
| 9709 | /* Now it's clear that we will overwrite the old hostname, |
| 9710 | * so we can free it safely */ |
| 9711 | if (session->hostname != NULL) { |
| 9712 | mbedtls_zeroize_and_free(session->hostname, |
| 9713 | strlen(session->hostname)); |
| 9714 | } |
| 9715 | |
| 9716 | /* Passing NULL as hostname shall clear the old one */ |
| 9717 | if (hostname == NULL) { |
| 9718 | session->hostname = NULL; |
| 9719 | } else { |
| 9720 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9721 | if (session->hostname == NULL) { |
| 9722 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9723 | } |
| 9724 | |
| 9725 | memcpy(session->hostname, hostname, hostname_len); |
| 9726 | } |
| 9727 | |
| 9728 | return 0; |
| 9729 | } |
| 9730 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9731 | MBEDTLS_SSL_SESSION_TICKETS && |
| 9732 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 9733 | MBEDTLS_SSL_CLI_C */ |
| 9734 | |
| 9735 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \ |
| 9736 | defined(MBEDTLS_SSL_ALPN) |
| 9737 | int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session, |
| 9738 | const char *alpn) |
| 9739 | { |
| 9740 | size_t alpn_len = 0; |
| 9741 | |
| 9742 | if (alpn != NULL) { |
| 9743 | alpn_len = strlen(alpn); |
| 9744 | |
| 9745 | if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) { |
| 9746 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9747 | } |
| 9748 | } |
| 9749 | |
| 9750 | if (session->ticket_alpn != NULL) { |
| 9751 | mbedtls_zeroize_and_free(session->ticket_alpn, |
| 9752 | strlen(session->ticket_alpn)); |
| 9753 | session->ticket_alpn = NULL; |
| 9754 | } |
| 9755 | |
| 9756 | if (alpn != NULL) { |
| 9757 | session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1); |
| 9758 | if (session->ticket_alpn == NULL) { |
| 9759 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9760 | } |
| 9761 | memcpy(session->ticket_alpn, alpn, alpn_len); |
| 9762 | } |
| 9763 | |
| 9764 | return 0; |
| 9765 | } |
| 9766 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 9767 | |
| 9768 | /* |
| 9769 | * The following functions are used by 1.2 and 1.3, client and server. |
| 9770 | */ |
| 9771 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 9772 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 9773 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 9774 | int recv_endpoint, |
| 9775 | mbedtls_ssl_protocol_version tls_version, |
| 9776 | uint32_t *flags) |
| 9777 | { |
| 9778 | int ret = 0; |
| 9779 | unsigned int usage = 0; |
| 9780 | const char *ext_oid; |
| 9781 | size_t ext_len; |
| 9782 | |
| 9783 | /* |
| 9784 | * keyUsage |
| 9785 | */ |
| 9786 | |
| 9787 | /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants |
| 9788 | * to check what a compliant client will think while choosing which cert |
| 9789 | * to send to the client. */ |
| 9790 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9791 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9792 | recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9793 | /* TLS 1.2 server part of the key exchange */ |
| 9794 | switch (ciphersuite->key_exchange) { |
| 9795 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 9796 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
| 9797 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
| 9798 | break; |
| 9799 | |
| 9800 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 9801 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 9802 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 9803 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9804 | break; |
| 9805 | |
| 9806 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 9807 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
| 9808 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
| 9809 | break; |
| 9810 | |
| 9811 | /* Don't use default: we want warnings when adding new values */ |
| 9812 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 9813 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 9814 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 9815 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
| 9816 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
| 9817 | usage = 0; |
| 9818 | } |
| 9819 | } else |
| 9820 | #endif |
| 9821 | { |
| 9822 | /* This is either TLS 1.3 authentication, which always uses signatures, |
| 9823 | * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only |
| 9824 | * options we implement, both using signatures. */ |
| 9825 | (void) tls_version; |
| 9826 | (void) ciphersuite; |
| 9827 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9828 | } |
| 9829 | |
| 9830 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
| 9831 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
| 9832 | ret = -1; |
| 9833 | } |
| 9834 | |
| 9835 | /* |
| 9836 | * extKeyUsage |
| 9837 | */ |
| 9838 | |
| 9839 | if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9840 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 9841 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 9842 | } else { |
| 9843 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 9844 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
| 9845 | } |
| 9846 | |
| 9847 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { |
| 9848 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
| 9849 | ret = -1; |
| 9850 | } |
| 9851 | |
| 9852 | return ret; |
| 9853 | } |
| 9854 | |
| 9855 | static int get_hostname_for_verification(mbedtls_ssl_context *ssl, |
| 9856 | const char **hostname) |
| 9857 | { |
| 9858 | if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) { |
| 9859 | MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname")); |
| 9860 | #if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME) |
| 9861 | if (mbedtls_ssl_conf_get_endpoint(ssl->conf) == MBEDTLS_SSL_IS_CLIENT && |
| 9862 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 9863 | return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME; |
| 9864 | } |
| 9865 | #endif |
| 9866 | } |
| 9867 | |
| 9868 | *hostname = mbedtls_ssl_get_hostname_pointer(ssl); |
| 9869 | if (*hostname == NULL) { |
| 9870 | MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification")); |
| 9871 | } |
| 9872 | |
| 9873 | return 0; |
| 9874 | } |
| 9875 | |
| 9876 | int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl, |
| 9877 | int authmode, |
| 9878 | mbedtls_x509_crt *chain, |
| 9879 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info, |
| 9880 | void *rs_ctx) |
| 9881 | { |
| 9882 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 9883 | ssl->session_negotiate->verify_result = 0; |
| 9884 | return 0; |
| 9885 | } |
| 9886 | |
| 9887 | /* |
| 9888 | * Primary check: use the appropriate X.509 verification function |
| 9889 | */ |
| 9890 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 9891 | void *p_vrfy; |
| 9892 | if (ssl->f_vrfy != NULL) { |
| 9893 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
| 9894 | f_vrfy = ssl->f_vrfy; |
| 9895 | p_vrfy = ssl->p_vrfy; |
| 9896 | } else { |
| 9897 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
| 9898 | f_vrfy = ssl->conf->f_vrfy; |
| 9899 | p_vrfy = ssl->conf->p_vrfy; |
| 9900 | } |
| 9901 | |
| 9902 | const char *hostname = ""; |
| 9903 | int ret = get_hostname_for_verification(ssl, &hostname); |
| 9904 | if (ret != 0) { |
| 9905 | MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret); |
| 9906 | return ret; |
| 9907 | } |
| 9908 | |
| 9909 | int have_ca_chain_or_callback = 0; |
| 9910 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9911 | if (ssl->conf->f_ca_cb != NULL) { |
| 9912 | ((void) rs_ctx); |
| 9913 | have_ca_chain_or_callback = 1; |
| 9914 | |
| 9915 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); |
| 9916 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 9917 | chain, |
| 9918 | ssl->conf->f_ca_cb, |
| 9919 | ssl->conf->p_ca_cb, |
| 9920 | ssl->conf->cert_profile, |
| 9921 | hostname, |
| 9922 | &ssl->session_negotiate->verify_result, |
| 9923 | f_vrfy, p_vrfy); |
| 9924 | } else |
| 9925 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 9926 | { |
| 9927 | mbedtls_x509_crt *ca_chain; |
| 9928 | mbedtls_x509_crl *ca_crl; |
| 9929 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9930 | if (ssl->handshake->sni_ca_chain != NULL) { |
| 9931 | ca_chain = ssl->handshake->sni_ca_chain; |
| 9932 | ca_crl = ssl->handshake->sni_ca_crl; |
| 9933 | } else |
| 9934 | #endif |
| 9935 | { |
| 9936 | ca_chain = ssl->conf->ca_chain; |
| 9937 | ca_crl = ssl->conf->ca_crl; |
| 9938 | } |
| 9939 | |
| 9940 | if (ca_chain != NULL) { |
| 9941 | have_ca_chain_or_callback = 1; |
| 9942 | } |
| 9943 | |
| 9944 | ret = mbedtls_x509_crt_verify_restartable( |
| 9945 | chain, |
| 9946 | ca_chain, ca_crl, |
| 9947 | ssl->conf->cert_profile, |
| 9948 | hostname, |
| 9949 | &ssl->session_negotiate->verify_result, |
| 9950 | f_vrfy, p_vrfy, rs_ctx); |
| 9951 | } |
| 9952 | |
| 9953 | if (ret != 0) { |
| 9954 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
| 9955 | } |
| 9956 | |
| 9957 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 9958 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 9959 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 9960 | } |
| 9961 | #endif |
| 9962 | |
| 9963 | /* |
| 9964 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 9965 | */ |
| 9966 | |
| 9967 | /* With TLS 1.2 and ECC certs, check that the curve used by the |
| 9968 | * certificate is on our list of acceptable curves. |
| 9969 | * |
| 9970 | * With TLS 1.3 this is not needed because the curve is part of the |
| 9971 | * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when |
| 9972 | * we validate the signature made with the key associated to this cert. |
| 9973 | */ |
| 9974 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 9975 | defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 9976 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9977 | mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) { |
| 9978 | if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) { |
| 9979 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 9980 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 9981 | if (ret == 0) { |
| 9982 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9983 | } |
| 9984 | } |
| 9985 | } |
| 9986 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 9987 | |
| 9988 | /* Check X.509 usage extensions (keyUsage, extKeyUsage) */ |
| 9989 | if (mbedtls_ssl_check_cert_usage(chain, |
| 9990 | ciphersuite_info, |
| 9991 | ssl->conf->endpoint, |
| 9992 | ssl->tls_version, |
| 9993 | &ssl->session_negotiate->verify_result) != 0) { |
| 9994 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 9995 | if (ret == 0) { |
| 9996 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9997 | } |
| 9998 | } |
| 9999 | |
| 10000 | /* With authmode optional, we want to keep going if the certificate was |
| 10001 | * unacceptable, but still fail on other errors (out of memory etc), |
| 10002 | * including fatal errors from the f_vrfy callback. |
| 10003 | * |
| 10004 | * The only acceptable errors are: |
| 10005 | * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check; |
| 10006 | * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks. |
| 10007 | * Anything else is a fatal error. */ |
| 10008 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 10009 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 10010 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
| 10011 | ret = 0; |
| 10012 | } |
| 10013 | |
| 10014 | /* Return a specific error as this is a user error: inconsistent |
| 10015 | * configuration - can't verify without trust anchors. */ |
| 10016 | if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 10017 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
| 10018 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 10019 | } |
| 10020 | |
| 10021 | if (ret != 0) { |
| 10022 | uint8_t alert; |
| 10023 | |
| 10024 | /* The certificate may have been rejected for several reasons. |
| 10025 | Pick one and send the corresponding alert. Which alert to send |
| 10026 | may be a subject of debate in some cases. */ |
| 10027 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
| 10028 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 10029 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
| 10030 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 10031 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
| 10032 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10033 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { |
| 10034 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10035 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
| 10036 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10037 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
| 10038 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10039 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
| 10040 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 10041 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
| 10042 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 10043 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
| 10044 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 10045 | } else { |
| 10046 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 10047 | } |
| 10048 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10049 | alert); |
| 10050 | } |
| 10051 | |
| 10052 | #if defined(MBEDTLS_DEBUG_C) |
| 10053 | if (ssl->session_negotiate->verify_result != 0) { |
| 10054 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 10055 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 10056 | } else { |
| 10057 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
| 10058 | } |
| 10059 | #endif /* MBEDTLS_DEBUG_C */ |
| 10060 | |
| 10061 | return ret; |
| 10062 | } |
| 10063 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 10064 | |
| 10065 | #if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) |
| 10066 | |
| 10067 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10068 | static int mbedtls_ssl_tls12_export_keying_material(const mbedtls_ssl_context *ssl, |
| 10069 | const mbedtls_md_type_t hash_alg, |
| 10070 | uint8_t *out, |
| 10071 | const size_t key_len, |
| 10072 | const char *label, |
| 10073 | const size_t label_len, |
| 10074 | const unsigned char *context, |
| 10075 | const size_t context_len, |
| 10076 | const int use_context) |
| 10077 | { |
| 10078 | int ret = 0; |
| 10079 | unsigned char *prf_input = NULL; |
| 10080 | |
| 10081 | /* The input to the PRF is client_random, then server_random. |
| 10082 | * If a context is provided, this is then followed by the context length |
| 10083 | * as a 16-bit big-endian integer, and then the context itself. */ |
| 10084 | const size_t randbytes_len = MBEDTLS_CLIENT_HELLO_RANDOM_LEN + MBEDTLS_SERVER_HELLO_RANDOM_LEN; |
| 10085 | size_t prf_input_len = randbytes_len; |
| 10086 | if (use_context) { |
| 10087 | if (context_len > UINT16_MAX) { |
| 10088 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10089 | } |
| 10090 | |
| 10091 | /* This does not overflow a 32-bit size_t because the current value of |
| 10092 | * prf_input_len is 64 (length of client_random + server_random) and |
| 10093 | * context_len fits into two bytes (checked above). */ |
| 10094 | prf_input_len += sizeof(uint16_t) + context_len; |
| 10095 | } |
| 10096 | |
| 10097 | prf_input = mbedtls_calloc(prf_input_len, sizeof(unsigned char)); |
| 10098 | if (prf_input == NULL) { |
| 10099 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 10100 | } |
| 10101 | |
| 10102 | memcpy(prf_input, |
| 10103 | ssl->transform->randbytes + MBEDTLS_SERVER_HELLO_RANDOM_LEN, |
| 10104 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN); |
| 10105 | memcpy(prf_input + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
| 10106 | ssl->transform->randbytes, |
| 10107 | MBEDTLS_SERVER_HELLO_RANDOM_LEN); |
| 10108 | if (use_context) { |
| 10109 | MBEDTLS_PUT_UINT16_BE(context_len, prf_input, randbytes_len); |
| 10110 | memcpy(prf_input + randbytes_len + sizeof(uint16_t), context, context_len); |
| 10111 | } |
| 10112 | ret = tls_prf_generic(hash_alg, ssl->session->master, sizeof(ssl->session->master), |
| 10113 | label, label_len, |
| 10114 | prf_input, prf_input_len, |
| 10115 | out, key_len); |
| 10116 | mbedtls_free(prf_input); |
| 10117 | return ret; |
| 10118 | } |
| 10119 | #endif /* defined(MBEDTLS_SSL_PROTO_TLS1_2) */ |
| 10120 | |
| 10121 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 10122 | static int mbedtls_ssl_tls13_export_keying_material(mbedtls_ssl_context *ssl, |
| 10123 | const mbedtls_md_type_t hash_alg, |
| 10124 | uint8_t *out, |
| 10125 | const size_t key_len, |
| 10126 | const char *label, |
| 10127 | const size_t label_len, |
| 10128 | const unsigned char *context, |
| 10129 | const size_t context_len) |
| 10130 | { |
| 10131 | const psa_algorithm_t psa_hash_alg = mbedtls_md_psa_alg_from_type(hash_alg); |
| 10132 | const size_t hash_len = PSA_HASH_LENGTH(psa_hash_alg); |
| 10133 | const unsigned char *secret = ssl->session->app_secrets.exporter_master_secret; |
| 10134 | |
| 10135 | /* The length of the label must be at most 249 bytes to fit into the HkdfLabel |
| 10136 | * struct as defined in RFC 8446, Section 7.1. |
| 10137 | * |
| 10138 | * The length of the context is unlimited even though the context field in the |
| 10139 | * struct can only hold up to 255 bytes. This is because we place a *hash* of |
| 10140 | * the context in the field. */ |
| 10141 | if (label_len > 249) { |
| 10142 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10143 | } |
| 10144 | |
| 10145 | return mbedtls_ssl_tls13_exporter(psa_hash_alg, secret, hash_len, |
| 10146 | (const unsigned char *) label, label_len, |
| 10147 | context, context_len, out, key_len); |
| 10148 | } |
| 10149 | #endif /* defined(MBEDTLS_SSL_PROTO_TLS1_3) */ |
| 10150 | |
| 10151 | int mbedtls_ssl_export_keying_material(mbedtls_ssl_context *ssl, |
| 10152 | uint8_t *out, const size_t key_len, |
| 10153 | const char *label, const size_t label_len, |
| 10154 | const unsigned char *context, const size_t context_len, |
| 10155 | const int use_context) |
| 10156 | { |
| 10157 | if (!mbedtls_ssl_is_handshake_over(ssl)) { |
| 10158 | /* TODO: Change this to a more appropriate error code when one is available. */ |
| 10159 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10160 | } |
| 10161 | |
| 10162 | if (key_len > MBEDTLS_SSL_EXPORT_MAX_KEY_LEN) { |
| 10163 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10164 | } |
| 10165 | |
| 10166 | int ciphersuite_id = mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl); |
| 10167 | const mbedtls_ssl_ciphersuite_t *ciphersuite = mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
| 10168 | const mbedtls_md_type_t hash_alg = ciphersuite->mac; |
| 10169 | |
| 10170 | switch (mbedtls_ssl_get_version_number(ssl)) { |
| 10171 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10172 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 10173 | return mbedtls_ssl_tls12_export_keying_material(ssl, hash_alg, out, key_len, |
| 10174 | label, label_len, |
| 10175 | context, context_len, use_context); |
| 10176 | #endif |
| 10177 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 10178 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 10179 | return mbedtls_ssl_tls13_export_keying_material(ssl, |
| 10180 | hash_alg, |
| 10181 | out, |
| 10182 | key_len, |
| 10183 | label, |
| 10184 | label_len, |
| 10185 | use_context ? context : NULL, |
| 10186 | use_context ? context_len : 0); |
| 10187 | #endif |
| 10188 | default: |
| 10189 | return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
| 10190 | } |
| 10191 | } |
| 10192 | |
| 10193 | #endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */ |
| 10194 | |
| 10195 | #endif /* MBEDTLS_SSL_TLS_C */ |