1 /* 2 * TLS server tickets callbacks implementation 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6 * 7 * This file is provided under the Apache License 2.0, or the 8 * GNU General Public License v2.0 or later. 9 * 10 * ********** 11 * Apache License 2.0: 12 * 13 * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 * not use this file except in compliance with the License. 15 * You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 * 25 * ********** 26 * 27 * ********** 28 * GNU General Public License v2.0 or later: 29 * 30 * This program is free software; you can redistribute it and/or modify 31 * it under the terms of the GNU General Public License as published by 32 * the Free Software Foundation; either version 2 of the License, or 33 * (at your option) any later version. 34 * 35 * This program is distributed in the hope that it will be useful, 36 * but WITHOUT ANY WARRANTY; without even the implied warranty of 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 38 * GNU General Public License for more details. 39 * 40 * You should have received a copy of the GNU General Public License along 41 * with this program; if not, write to the Free Software Foundation, Inc., 42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 43 * 44 * ********** 45 * 46 * This file is part of mbed TLS (https://tls.mbed.org) 47 */ 48 49 #if !defined(MBEDTLS_CONFIG_FILE) 50 #include "mbedtls/config.h" 51 #else 52 #include MBEDTLS_CONFIG_FILE 53 #endif 54 55 #if defined(MBEDTLS_SSL_TICKET_C) 56 57 #if defined(MBEDTLS_PLATFORM_C) 58 #include "mbedtls/platform.h" 59 #else 60 #include <stdlib.h> 61 #define mbedtls_calloc calloc 62 #define mbedtls_free free 63 #endif 64 65 #include "mbedtls/ssl_internal.h" 66 #include "mbedtls/ssl_ticket.h" 67 68 #include <string.h> 69 70 /* Implementation that should never be optimized out by the compiler */ 71 static void mbedtls_zeroize( void *v, size_t n ) { 72 volatile unsigned char *p = v; while( n-- ) *p++ = 0; 73 } 74 75 /* 76 * Initialze context 77 */ 78 void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) 79 { 80 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); 81 82 #if defined(MBEDTLS_THREADING_C) 83 mbedtls_mutex_init( &ctx->mutex ); 84 #endif 85 } 86 87 #define MAX_KEY_BYTES 32 /* 256 bits */ 88 89 #define TICKET_KEY_NAME_BYTES 4 90 #define TICKET_IV_BYTES 12 91 #define TICKET_CRYPT_LEN_BYTES 2 92 #define TICKET_AUTH_TAG_BYTES 16 93 94 #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \ 95 TICKET_IV_BYTES + \ 96 TICKET_CRYPT_LEN_BYTES + \ 97 TICKET_AUTH_TAG_BYTES ) 98 #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \ 99 TICKET_IV_BYTES + \ 100 TICKET_CRYPT_LEN_BYTES ) 101 102 /* 103 * Generate/update a key 104 */ 105 static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, 106 unsigned char index ) 107 { 108 int ret; 109 unsigned char buf[MAX_KEY_BYTES]; 110 mbedtls_ssl_ticket_key *key = ctx->keys + index; 111 112 #if defined(MBEDTLS_HAVE_TIME) 113 key->generation_time = (uint32_t) mbedtls_time( NULL ); 114 #endif 115 116 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) 117 return( ret ); 118 119 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) 120 return( ret ); 121 122 /* With GCM and CCM, same context can encrypt & decrypt */ 123 ret = mbedtls_cipher_setkey( &key->ctx, buf, 124 mbedtls_cipher_get_key_bitlen( &key->ctx ), 125 MBEDTLS_ENCRYPT ); 126 127 mbedtls_zeroize( buf, sizeof( buf ) ); 128 129 return( ret ); 130 } 131 132 /* 133 * Rotate/generate keys if necessary 134 */ 135 static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx ) 136 { 137 #if !defined(MBEDTLS_HAVE_TIME) 138 ((void) ctx); 139 #else 140 if( ctx->ticket_lifetime != 0 ) 141 { 142 uint32_t current_time = (uint32_t) mbedtls_time( NULL ); 143 uint32_t key_time = ctx->keys[ctx->active].generation_time; 144 145 if( current_time >= key_time && 146 current_time - key_time < ctx->ticket_lifetime ) 147 { 148 return( 0 ); 149 } 150 151 ctx->active = 1 - ctx->active; 152 153 return( ssl_ticket_gen_key( ctx, ctx->active ) ); 154 } 155 else 156 #endif /* MBEDTLS_HAVE_TIME */ 157 return( 0 ); 158 } 159 160 /* 161 * Setup context for actual use 162 */ 163 int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, 164 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 165 mbedtls_cipher_type_t cipher, 166 uint32_t lifetime ) 167 { 168 int ret; 169 const mbedtls_cipher_info_t *cipher_info; 170 171 ctx->f_rng = f_rng; 172 ctx->p_rng = p_rng; 173 174 ctx->ticket_lifetime = lifetime; 175 176 cipher_info = mbedtls_cipher_info_from_type( cipher); 177 if( cipher_info == NULL ) 178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 179 180 if( cipher_info->mode != MBEDTLS_MODE_GCM && 181 cipher_info->mode != MBEDTLS_MODE_CCM ) 182 { 183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 184 } 185 186 if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES ) 187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 188 189 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 || 190 ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) 191 { 192 return( ret ); 193 } 194 195 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || 196 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) 197 { 198 return( ret ); 199 } 200 201 return( 0 ); 202 } 203 204 /* 205 * Serialize a session in the following format: 206 * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) 207 * n . n+2 peer_cert length = m (0 if no certificate) 208 * n+3 . n+2+m peer cert ASN.1 209 */ 210 static int ssl_save_session( const mbedtls_ssl_session *session, 211 unsigned char *buf, size_t buf_len, 212 size_t *olen ) 213 { 214 unsigned char *p = buf; 215 size_t left = buf_len; 216 #if defined(MBEDTLS_X509_CRT_PARSE_C) 217 size_t cert_len; 218 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 219 220 if( left < sizeof( mbedtls_ssl_session ) ) 221 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 222 223 memcpy( p, session, sizeof( mbedtls_ssl_session ) ); 224 p += sizeof( mbedtls_ssl_session ); 225 left -= sizeof( mbedtls_ssl_session ); 226 227 #if defined(MBEDTLS_X509_CRT_PARSE_C) 228 if( session->peer_cert == NULL ) 229 cert_len = 0; 230 else 231 cert_len = session->peer_cert->raw.len; 232 233 if( left < 3 + cert_len ) 234 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 235 236 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); 237 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); 238 *p++ = (unsigned char)( ( cert_len ) & 0xFF ); 239 240 if( session->peer_cert != NULL ) 241 memcpy( p, session->peer_cert->raw.p, cert_len ); 242 243 p += cert_len; 244 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 245 246 *olen = p - buf; 247 248 return( 0 ); 249 } 250 251 /* 252 * Unserialise session, see ssl_save_session() 253 */ 254 static int ssl_load_session( mbedtls_ssl_session *session, 255 const unsigned char *buf, size_t len ) 256 { 257 const unsigned char *p = buf; 258 const unsigned char * const end = buf + len; 259 #if defined(MBEDTLS_X509_CRT_PARSE_C) 260 size_t cert_len; 261 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 262 263 if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) ) 264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 265 266 memcpy( session, p, sizeof( mbedtls_ssl_session ) ); 267 p += sizeof( mbedtls_ssl_session ); 268 269 #if defined(MBEDTLS_X509_CRT_PARSE_C) 270 if( 3 > (size_t)( end - p ) ) 271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 272 273 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; 274 p += 3; 275 276 if( cert_len == 0 ) 277 { 278 session->peer_cert = NULL; 279 } 280 else 281 { 282 int ret; 283 284 if( cert_len > (size_t)( end - p ) ) 285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 286 287 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); 288 289 if( session->peer_cert == NULL ) 290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); 291 292 mbedtls_x509_crt_init( session->peer_cert ); 293 294 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, 295 p, cert_len ) ) != 0 ) 296 { 297 mbedtls_x509_crt_free( session->peer_cert ); 298 mbedtls_free( session->peer_cert ); 299 session->peer_cert = NULL; 300 return( ret ); 301 } 302 303 p += cert_len; 304 } 305 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 306 307 if( p != end ) 308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 309 310 return( 0 ); 311 } 312 313 /* 314 * Create session ticket, with the following structure: 315 * 316 * struct { 317 * opaque key_name[4]; 318 * opaque iv[12]; 319 * opaque encrypted_state<0..2^16-1>; 320 * opaque tag[16]; 321 * } ticket; 322 * 323 * The key_name, iv, and length of encrypted_state are the additional 324 * authenticated data. 325 */ 326 327 int mbedtls_ssl_ticket_write( void *p_ticket, 328 const mbedtls_ssl_session *session, 329 unsigned char *start, 330 const unsigned char *end, 331 size_t *tlen, 332 uint32_t *ticket_lifetime ) 333 { 334 int ret; 335 mbedtls_ssl_ticket_context *ctx = p_ticket; 336 mbedtls_ssl_ticket_key *key; 337 unsigned char *key_name = start; 338 unsigned char *iv = start + TICKET_KEY_NAME_BYTES; 339 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES; 340 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES; 341 unsigned char *tag; 342 size_t clear_len, ciph_len; 343 344 *tlen = 0; 345 346 if( ctx == NULL || ctx->f_rng == NULL ) 347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 348 349 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, 350 * in addition to session itself, that will be checked when writing it. */ 351 MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN ); 352 353 #if defined(MBEDTLS_THREADING_C) 354 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) 355 return( ret ); 356 #endif 357 358 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) 359 goto cleanup; 360 361 key = &ctx->keys[ctx->active]; 362 363 *ticket_lifetime = ctx->ticket_lifetime; 364 365 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES ); 366 367 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 ) 368 goto cleanup; 369 370 /* Dump session state */ 371 if( ( ret = ssl_save_session( session, 372 state, end - state, &clear_len ) ) != 0 || 373 (unsigned long) clear_len > 65535 ) 374 { 375 goto cleanup; 376 } 377 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; 378 state_len_bytes[1] = ( clear_len ) & 0xff; 379 380 /* Encrypt and authenticate */ 381 tag = state + clear_len; 382 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx, 383 iv, TICKET_IV_BYTES, 384 /* Additional data: key name, IV and length */ 385 key_name, TICKET_ADD_DATA_LEN, 386 state, clear_len, state, &ciph_len, 387 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) 388 { 389 goto cleanup; 390 } 391 if( ciph_len != clear_len ) 392 { 393 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; 394 goto cleanup; 395 } 396 397 *tlen = TICKET_MIN_LEN + ciph_len; 398 399 cleanup: 400 #if defined(MBEDTLS_THREADING_C) 401 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) 402 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 403 #endif 404 405 return( ret ); 406 } 407 408 /* 409 * Select key based on name 410 */ 411 static mbedtls_ssl_ticket_key *ssl_ticket_select_key( 412 mbedtls_ssl_ticket_context *ctx, 413 const unsigned char name[4] ) 414 { 415 unsigned char i; 416 417 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ ) 418 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 ) 419 return( &ctx->keys[i] ); 420 421 return( NULL ); 422 } 423 424 /* 425 * Load session ticket (see mbedtls_ssl_ticket_write for structure) 426 */ 427 int mbedtls_ssl_ticket_parse( void *p_ticket, 428 mbedtls_ssl_session *session, 429 unsigned char *buf, 430 size_t len ) 431 { 432 int ret; 433 mbedtls_ssl_ticket_context *ctx = p_ticket; 434 mbedtls_ssl_ticket_key *key; 435 unsigned char *key_name = buf; 436 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; 437 unsigned char *enc_len_p = iv + TICKET_IV_BYTES; 438 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; 439 unsigned char *tag; 440 size_t enc_len, clear_len; 441 442 if( ctx == NULL || ctx->f_rng == NULL ) 443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 444 445 if( len < TICKET_MIN_LEN ) 446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 447 448 #if defined(MBEDTLS_THREADING_C) 449 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) 450 return( ret ); 451 #endif 452 453 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) 454 goto cleanup; 455 456 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; 457 tag = ticket + enc_len; 458 459 if( len != TICKET_MIN_LEN + enc_len ) 460 { 461 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 462 goto cleanup; 463 } 464 465 /* Select key */ 466 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL ) 467 { 468 /* We can't know for sure but this is a likely option unless we're 469 * under attack - this is only informative anyway */ 470 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; 471 goto cleanup; 472 } 473 474 /* Decrypt and authenticate */ 475 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, 476 iv, TICKET_IV_BYTES, 477 /* Additional data: key name, IV and length */ 478 key_name, TICKET_ADD_DATA_LEN, 479 ticket, enc_len, 480 ticket, &clear_len, 481 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) 482 { 483 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) 484 ret = MBEDTLS_ERR_SSL_INVALID_MAC; 485 486 goto cleanup; 487 } 488 if( clear_len != enc_len ) 489 { 490 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; 491 goto cleanup; 492 } 493 494 /* Actually load session */ 495 if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) 496 goto cleanup; 497 498 #if defined(MBEDTLS_HAVE_TIME) 499 { 500 /* Check for expiration */ 501 mbedtls_time_t current_time = mbedtls_time( NULL ); 502 503 if( current_time < session->start || 504 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) 505 { 506 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; 507 goto cleanup; 508 } 509 } 510 #endif 511 512 cleanup: 513 #if defined(MBEDTLS_THREADING_C) 514 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) 515 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 516 #endif 517 518 return( ret ); 519 } 520 521 /* 522 * Free context 523 */ 524 void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) 525 { 526 mbedtls_cipher_free( &ctx->keys[0].ctx ); 527 mbedtls_cipher_free( &ctx->keys[1].ctx ); 528 529 #if defined(MBEDTLS_THREADING_C) 530 mbedtls_mutex_free( &ctx->mutex ); 531 #endif 532 533 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); 534 } 535 536 #endif /* MBEDTLS_SSL_TICKET_C */ 537