0.2.0-dev Download ZIP
453 lines ·12.8 KB Raw
| 1 | /* |
| 2 | * TLS server tickets callbacks implementation |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "ssl_misc.h" |
| 9 | |
| 10 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 11 | |
| 12 | #include "mbedtls/platform.h" |
| 13 | |
| 14 | #include "mbedtls/ssl_ticket.h" |
| 15 | #include "mbedtls/error.h" |
| 16 | #include "mbedtls/platform_util.h" |
| 17 | |
| 18 | #include <string.h> |
| 19 | |
| 20 | /* Define a local translating function to save code size by not using too many |
| 21 | * arguments in each translating place. */ |
| 22 | static int local_err_translation(psa_status_t status) |
| 23 | { |
| 24 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
| 25 | ARRAY_LENGTH(psa_to_ssl_errors), |
| 26 | psa_generic_status_to_mbedtls); |
| 27 | } |
| 28 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
| 29 | |
| 30 | /* |
| 31 | * Initialize context |
| 32 | */ |
| 33 | void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx) |
| 34 | { |
| 35 | memset(ctx, 0, sizeof(mbedtls_ssl_ticket_context)); |
| 36 | |
| 37 | #if defined(MBEDTLS_THREADING_C) |
| 38 | mbedtls_mutex_init(&ctx->mutex); |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | #define MAX_KEY_BYTES MBEDTLS_SSL_TICKET_MAX_KEY_BYTES |
| 43 | |
| 44 | #define TICKET_KEY_NAME_BYTES MBEDTLS_SSL_TICKET_KEY_NAME_BYTES |
| 45 | #define TICKET_IV_BYTES 12 |
| 46 | #define TICKET_CRYPT_LEN_BYTES 2 |
| 47 | #define TICKET_AUTH_TAG_BYTES 16 |
| 48 | |
| 49 | #define TICKET_MIN_LEN (TICKET_KEY_NAME_BYTES + \ |
| 50 | TICKET_IV_BYTES + \ |
| 51 | TICKET_CRYPT_LEN_BYTES + \ |
| 52 | TICKET_AUTH_TAG_BYTES) |
| 53 | #define TICKET_ADD_DATA_LEN (TICKET_KEY_NAME_BYTES + \ |
| 54 | TICKET_IV_BYTES + \ |
| 55 | TICKET_CRYPT_LEN_BYTES) |
| 56 | |
| 57 | /* |
| 58 | * Generate/update a key |
| 59 | */ |
| 60 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 61 | static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx, |
| 62 | unsigned char index) |
| 63 | { |
| 64 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 65 | unsigned char buf[MAX_KEY_BYTES] = { 0 }; |
| 66 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 67 | |
| 68 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 69 | |
| 70 | #if defined(MBEDTLS_HAVE_TIME) |
| 71 | key->generation_time = mbedtls_time(NULL); |
| 72 | #endif |
| 73 | /* The lifetime of a key is the configured lifetime of the tickets when |
| 74 | * the key is created. |
| 75 | */ |
| 76 | key->lifetime = ctx->ticket_lifetime; |
| 77 | |
| 78 | if ((ret = psa_generate_random(key->name, sizeof(key->name))) != 0) { |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | if ((ret = psa_generate_random(buf, sizeof(buf))) != 0) { |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | psa_set_key_usage_flags(&attributes, |
| 87 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 88 | psa_set_key_algorithm(&attributes, key->alg); |
| 89 | psa_set_key_type(&attributes, key->key_type); |
| 90 | psa_set_key_bits(&attributes, key->key_bits); |
| 91 | |
| 92 | ret = PSA_TO_MBEDTLS_ERR( |
| 93 | psa_import_key(&attributes, buf, |
| 94 | PSA_BITS_TO_BYTES(key->key_bits), |
| 95 | &key->key)); |
| 96 | |
| 97 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Rotate/generate keys if necessary |
| 104 | */ |
| 105 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 106 | static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx) |
| 107 | { |
| 108 | #if !defined(MBEDTLS_HAVE_TIME) |
| 109 | ((void) ctx); |
| 110 | #else |
| 111 | mbedtls_ssl_ticket_key * const key = ctx->keys + ctx->active; |
| 112 | if (key->lifetime != 0) { |
| 113 | mbedtls_time_t current_time = mbedtls_time(NULL); |
| 114 | mbedtls_time_t key_time = key->generation_time; |
| 115 | |
| 116 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 117 | |
| 118 | if (current_time >= key_time && |
| 119 | (uint64_t) (current_time - key_time) < key->lifetime) { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | ctx->active = 1 - ctx->active; |
| 124 | |
| 125 | if ((status = psa_destroy_key(ctx->keys[ctx->active].key)) != PSA_SUCCESS) { |
| 126 | return PSA_TO_MBEDTLS_ERR(status); |
| 127 | } |
| 128 | |
| 129 | return ssl_ticket_gen_key(ctx, ctx->active); |
| 130 | } else |
| 131 | #endif /* MBEDTLS_HAVE_TIME */ |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Rotate active session ticket encryption key |
| 137 | */ |
| 138 | int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx, |
| 139 | const unsigned char *name, size_t nlength, |
| 140 | const unsigned char *k, size_t klength, |
| 141 | uint32_t lifetime) |
| 142 | { |
| 143 | const unsigned char idx = 1 - ctx->active; |
| 144 | mbedtls_ssl_ticket_key * const key = ctx->keys + idx; |
| 145 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 146 | |
| 147 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 149 | const size_t bitlen = key->key_bits; |
| 150 | |
| 151 | if (nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t) bitlen) { |
| 152 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 153 | } |
| 154 | |
| 155 | if ((status = psa_destroy_key(key->key)) != PSA_SUCCESS) { |
| 156 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | psa_set_key_usage_flags(&attributes, |
| 161 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 162 | psa_set_key_algorithm(&attributes, key->alg); |
| 163 | psa_set_key_type(&attributes, key->key_type); |
| 164 | psa_set_key_bits(&attributes, key->key_bits); |
| 165 | |
| 166 | if ((status = psa_import_key(&attributes, k, |
| 167 | PSA_BITS_TO_BYTES(key->key_bits), |
| 168 | &key->key)) != PSA_SUCCESS) { |
| 169 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | ctx->active = idx; |
| 174 | ctx->ticket_lifetime = lifetime; |
| 175 | memcpy(key->name, name, TICKET_KEY_NAME_BYTES); |
| 176 | #if defined(MBEDTLS_HAVE_TIME) |
| 177 | key->generation_time = mbedtls_time(NULL); |
| 178 | #endif |
| 179 | key->lifetime = lifetime; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Setup context for actual use |
| 186 | */ |
| 187 | int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx, |
| 188 | psa_algorithm_t alg, psa_key_type_t key_type, psa_key_bits_t key_bits, |
| 189 | uint32_t lifetime) |
| 190 | { |
| 191 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 192 | |
| 193 | if (PSA_ALG_IS_AEAD(alg) == 0) { |
| 194 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 195 | } |
| 196 | |
| 197 | if (key_bits > 8 * MAX_KEY_BYTES) { |
| 198 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 199 | } |
| 200 | |
| 201 | ctx->ticket_lifetime = lifetime; |
| 202 | |
| 203 | ctx->keys[0].alg = alg; |
| 204 | ctx->keys[0].key_type = key_type; |
| 205 | ctx->keys[0].key_bits = key_bits; |
| 206 | |
| 207 | ctx->keys[1].alg = alg; |
| 208 | ctx->keys[1].key_type = key_type; |
| 209 | ctx->keys[1].key_bits = key_bits; |
| 210 | |
| 211 | if ((ret = ssl_ticket_gen_key(ctx, 0)) != 0 || |
| 212 | (ret = ssl_ticket_gen_key(ctx, 1)) != 0) { |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Create session ticket, with the following structure: |
| 221 | * |
| 222 | * struct { |
| 223 | * opaque key_name[4]; |
| 224 | * opaque iv[12]; |
| 225 | * opaque encrypted_state<0..2^16-1>; |
| 226 | * opaque tag[16]; |
| 227 | * } ticket; |
| 228 | * |
| 229 | * The key_name, iv, and length of encrypted_state are the additional |
| 230 | * authenticated data. |
| 231 | */ |
| 232 | |
| 233 | int mbedtls_ssl_ticket_write(void *p_ticket, |
| 234 | const mbedtls_ssl_session *session, |
| 235 | unsigned char *start, |
| 236 | const unsigned char *end, |
| 237 | size_t *tlen, |
| 238 | uint32_t *ticket_lifetime) |
| 239 | { |
| 240 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 241 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
| 242 | mbedtls_ssl_ticket_key *key; |
| 243 | unsigned char *key_name = start; |
| 244 | unsigned char *iv = start + TICKET_KEY_NAME_BYTES; |
| 245 | unsigned char *state_len_bytes = iv + TICKET_IV_BYTES; |
| 246 | unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES; |
| 247 | size_t clear_len, ciph_len; |
| 248 | |
| 249 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 250 | |
| 251 | *tlen = 0; |
| 252 | |
| 253 | if (ctx == NULL) { |
| 254 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 255 | } |
| 256 | |
| 257 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 258 | * in addition to session itself, that will be checked when writing it. */ |
| 259 | MBEDTLS_SSL_CHK_BUF_PTR(start, end, TICKET_MIN_LEN); |
| 260 | |
| 261 | #if defined(MBEDTLS_THREADING_C) |
| 262 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 263 | return ret; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | if ((ret = ssl_ticket_update_keys(ctx)) != 0) { |
| 268 | goto cleanup; |
| 269 | } |
| 270 | |
| 271 | key = &ctx->keys[ctx->active]; |
| 272 | |
| 273 | *ticket_lifetime = key->lifetime; |
| 274 | |
| 275 | memcpy(key_name, key->name, TICKET_KEY_NAME_BYTES); |
| 276 | |
| 277 | if ((ret = psa_generate_random(iv, TICKET_IV_BYTES)) != 0) { |
| 278 | goto cleanup; |
| 279 | } |
| 280 | |
| 281 | /* Dump session state */ |
| 282 | if ((ret = mbedtls_ssl_session_save(session, |
| 283 | state, (size_t) (end - state), |
| 284 | &clear_len)) != 0 || |
| 285 | (unsigned long) clear_len > 65535) { |
| 286 | goto cleanup; |
| 287 | } |
| 288 | MBEDTLS_PUT_UINT16_BE(clear_len, state_len_bytes, 0); |
| 289 | |
| 290 | /* Encrypt and authenticate */ |
| 291 | if ((status = psa_aead_encrypt(key->key, key->alg, iv, TICKET_IV_BYTES, |
| 292 | key_name, TICKET_ADD_DATA_LEN, |
| 293 | state, clear_len, |
| 294 | state, end - state, |
| 295 | &ciph_len)) != PSA_SUCCESS) { |
| 296 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 297 | goto cleanup; |
| 298 | } |
| 299 | |
| 300 | if (ciph_len != clear_len + TICKET_AUTH_TAG_BYTES) { |
| 301 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 302 | goto cleanup; |
| 303 | } |
| 304 | |
| 305 | *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES; |
| 306 | |
| 307 | cleanup: |
| 308 | #if defined(MBEDTLS_THREADING_C) |
| 309 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 310 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 311 | } |
| 312 | #endif |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Select key based on name |
| 319 | */ |
| 320 | static mbedtls_ssl_ticket_key *ssl_ticket_select_key( |
| 321 | mbedtls_ssl_ticket_context *ctx, |
| 322 | const unsigned char name[4]) |
| 323 | { |
| 324 | unsigned char i; |
| 325 | |
| 326 | for (i = 0; i < sizeof(ctx->keys) / sizeof(*ctx->keys); i++) { |
| 327 | if (memcmp(name, ctx->keys[i].name, 4) == 0) { |
| 328 | return &ctx->keys[i]; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 337 | */ |
| 338 | int mbedtls_ssl_ticket_parse(void *p_ticket, |
| 339 | mbedtls_ssl_session *session, |
| 340 | unsigned char *buf, |
| 341 | size_t len) |
| 342 | { |
| 343 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 344 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
| 345 | mbedtls_ssl_ticket_key *key; |
| 346 | unsigned char *key_name = buf; |
| 347 | unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; |
| 348 | unsigned char *enc_len_p = iv + TICKET_IV_BYTES; |
| 349 | unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; |
| 350 | size_t enc_len, clear_len; |
| 351 | |
| 352 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 353 | |
| 354 | if (ctx == NULL) { |
| 355 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 356 | } |
| 357 | |
| 358 | if (len < TICKET_MIN_LEN) { |
| 359 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 360 | } |
| 361 | |
| 362 | #if defined(MBEDTLS_THREADING_C) |
| 363 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 364 | return ret; |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | if ((ret = ssl_ticket_update_keys(ctx)) != 0) { |
| 369 | goto cleanup; |
| 370 | } |
| 371 | |
| 372 | enc_len = MBEDTLS_GET_UINT16_BE(enc_len_p, 0); |
| 373 | |
| 374 | if (len != TICKET_MIN_LEN + enc_len) { |
| 375 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 376 | goto cleanup; |
| 377 | } |
| 378 | |
| 379 | /* Select key */ |
| 380 | if ((key = ssl_ticket_select_key(ctx, key_name)) == NULL) { |
| 381 | /* We can't know for sure but this is a likely option unless we're |
| 382 | * under attack - this is only informative anyway */ |
| 383 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 384 | goto cleanup; |
| 385 | } |
| 386 | |
| 387 | /* Decrypt and authenticate */ |
| 388 | if ((status = psa_aead_decrypt(key->key, key->alg, iv, TICKET_IV_BYTES, |
| 389 | key_name, TICKET_ADD_DATA_LEN, |
| 390 | ticket, enc_len + TICKET_AUTH_TAG_BYTES, |
| 391 | ticket, enc_len, &clear_len)) != PSA_SUCCESS) { |
| 392 | ret = PSA_TO_MBEDTLS_ERR(status); |
| 393 | goto cleanup; |
| 394 | } |
| 395 | |
| 396 | if (clear_len != enc_len) { |
| 397 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 398 | goto cleanup; |
| 399 | } |
| 400 | |
| 401 | /* Actually load session */ |
| 402 | if ((ret = mbedtls_ssl_session_load(session, ticket, clear_len)) != 0) { |
| 403 | goto cleanup; |
| 404 | } |
| 405 | |
| 406 | #if defined(MBEDTLS_HAVE_TIME) |
| 407 | mbedtls_ms_time_t ticket_creation_time, ticket_age; |
| 408 | mbedtls_ms_time_t ticket_lifetime = |
| 409 | (mbedtls_ms_time_t) key->lifetime * 1000; |
| 410 | |
| 411 | ret = mbedtls_ssl_session_get_ticket_creation_time(session, |
| 412 | &ticket_creation_time); |
| 413 | if (ret != 0) { |
| 414 | goto cleanup; |
| 415 | } |
| 416 | |
| 417 | ticket_age = mbedtls_ms_time() - ticket_creation_time; |
| 418 | if (ticket_age < 0 || ticket_age > ticket_lifetime) { |
| 419 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 420 | goto cleanup; |
| 421 | } |
| 422 | #endif |
| 423 | |
| 424 | cleanup: |
| 425 | #if defined(MBEDTLS_THREADING_C) |
| 426 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 427 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Free context |
| 436 | */ |
| 437 | void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx) |
| 438 | { |
| 439 | if (ctx == NULL) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | psa_destroy_key(ctx->keys[0].key); |
| 444 | psa_destroy_key(ctx->keys[1].key); |
| 445 | |
| 446 | #if defined(MBEDTLS_THREADING_C) |
| 447 | mbedtls_mutex_free(&ctx->mutex); |
| 448 | #endif |
| 449 | |
| 450 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_ticket_context)); |
| 451 | } |
| 452 | |
| 453 | #endif /* MBEDTLS_SSL_TICKET_C */ |