ssl_ticket.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * TLS server tickets callbacks implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_SSL_TICKET_C)
  21. #if defined(MBEDTLS_PLATFORM_C)
  22. #include "mbedtls/platform.h"
  23. #else
  24. #include <stdlib.h>
  25. #define mbedtls_calloc calloc
  26. #define mbedtls_free free
  27. #endif
  28. #include "mbedtls/ssl_internal.h"
  29. #include "mbedtls/ssl_ticket.h"
  30. #include "mbedtls/error.h"
  31. #include "mbedtls/platform_util.h"
  32. #include <string.h>
  33. /*
  34. * Initialize context
  35. */
  36. void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
  37. {
  38. memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
  39. #if defined(MBEDTLS_THREADING_C)
  40. mbedtls_mutex_init( &ctx->mutex );
  41. #endif
  42. }
  43. #define MAX_KEY_BYTES 32 /* 256 bits */
  44. #define TICKET_KEY_NAME_BYTES 4
  45. #define TICKET_IV_BYTES 12
  46. #define TICKET_CRYPT_LEN_BYTES 2
  47. #define TICKET_AUTH_TAG_BYTES 16
  48. #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
  49. TICKET_IV_BYTES + \
  50. TICKET_CRYPT_LEN_BYTES + \
  51. TICKET_AUTH_TAG_BYTES )
  52. #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
  53. TICKET_IV_BYTES + \
  54. TICKET_CRYPT_LEN_BYTES )
  55. /*
  56. * Generate/update a key
  57. */
  58. MBEDTLS_CHECK_RETURN_CRITICAL
  59. static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
  60. unsigned char index )
  61. {
  62. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  63. unsigned char buf[MAX_KEY_BYTES];
  64. mbedtls_ssl_ticket_key *key = ctx->keys + index;
  65. #if defined(MBEDTLS_HAVE_TIME)
  66. key->generation_time = (uint32_t) mbedtls_time( NULL );
  67. #endif
  68. if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
  69. return( ret );
  70. if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
  71. return( ret );
  72. /* With GCM and CCM, same context can encrypt & decrypt */
  73. ret = mbedtls_cipher_setkey( &key->ctx, buf,
  74. mbedtls_cipher_get_key_bitlen( &key->ctx ),
  75. MBEDTLS_ENCRYPT );
  76. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  77. return( ret );
  78. }
  79. /*
  80. * Rotate/generate keys if necessary
  81. */
  82. MBEDTLS_CHECK_RETURN_CRITICAL
  83. static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
  84. {
  85. #if !defined(MBEDTLS_HAVE_TIME)
  86. ((void) ctx);
  87. #else
  88. if( ctx->ticket_lifetime != 0 )
  89. {
  90. uint32_t current_time = (uint32_t) mbedtls_time( NULL );
  91. uint32_t key_time = ctx->keys[ctx->active].generation_time;
  92. if( current_time >= key_time &&
  93. current_time - key_time < ctx->ticket_lifetime )
  94. {
  95. return( 0 );
  96. }
  97. ctx->active = 1 - ctx->active;
  98. return( ssl_ticket_gen_key( ctx, ctx->active ) );
  99. }
  100. else
  101. #endif /* MBEDTLS_HAVE_TIME */
  102. return( 0 );
  103. }
  104. /*
  105. * Setup context for actual use
  106. */
  107. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  108. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  109. mbedtls_cipher_type_t cipher,
  110. uint32_t lifetime )
  111. {
  112. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  113. const mbedtls_cipher_info_t *cipher_info;
  114. ctx->f_rng = f_rng;
  115. ctx->p_rng = p_rng;
  116. ctx->ticket_lifetime = lifetime;
  117. cipher_info = mbedtls_cipher_info_from_type( cipher);
  118. if( cipher_info == NULL )
  119. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  120. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  121. cipher_info->mode != MBEDTLS_MODE_CCM )
  122. {
  123. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  124. }
  125. if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
  126. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  127. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  128. ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx,
  129. cipher_info, TICKET_AUTH_TAG_BYTES );
  130. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  131. return( ret );
  132. /* We don't yet expect to support all ciphers through PSA,
  133. * so allow fallback to ordinary mbedtls_cipher_setup(). */
  134. if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  135. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  136. if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
  137. return( ret );
  138. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  139. ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx,
  140. cipher_info, TICKET_AUTH_TAG_BYTES );
  141. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  142. return( ret );
  143. if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  144. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  145. if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
  146. return( ret );
  147. if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
  148. ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
  149. {
  150. return( ret );
  151. }
  152. return( 0 );
  153. }
  154. /*
  155. * Create session ticket, with the following structure:
  156. *
  157. * struct {
  158. * opaque key_name[4];
  159. * opaque iv[12];
  160. * opaque encrypted_state<0..2^16-1>;
  161. * opaque tag[16];
  162. * } ticket;
  163. *
  164. * The key_name, iv, and length of encrypted_state are the additional
  165. * authenticated data.
  166. */
  167. int mbedtls_ssl_ticket_write( void *p_ticket,
  168. const mbedtls_ssl_session *session,
  169. unsigned char *start,
  170. const unsigned char *end,
  171. size_t *tlen,
  172. uint32_t *ticket_lifetime )
  173. {
  174. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  175. mbedtls_ssl_ticket_context *ctx = p_ticket;
  176. mbedtls_ssl_ticket_key *key;
  177. unsigned char *key_name = start;
  178. unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
  179. unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
  180. unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
  181. size_t clear_len, ciph_len;
  182. *tlen = 0;
  183. if( ctx == NULL || ctx->f_rng == NULL )
  184. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  185. /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
  186. * in addition to session itself, that will be checked when writing it. */
  187. MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
  188. #if defined(MBEDTLS_THREADING_C)
  189. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  190. return( ret );
  191. #endif
  192. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  193. goto cleanup;
  194. key = &ctx->keys[ctx->active];
  195. *ticket_lifetime = ctx->ticket_lifetime;
  196. memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
  197. if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
  198. goto cleanup;
  199. /* Dump session state */
  200. if( ( ret = mbedtls_ssl_session_save( session,
  201. state, end - state,
  202. &clear_len ) ) != 0 ||
  203. (unsigned long) clear_len > 65535 )
  204. {
  205. goto cleanup;
  206. }
  207. MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 );
  208. /* Encrypt and authenticate */
  209. if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
  210. iv, TICKET_IV_BYTES,
  211. /* Additional data: key name, IV and length */
  212. key_name, TICKET_ADD_DATA_LEN,
  213. state, clear_len,
  214. state, end - state, &ciph_len,
  215. TICKET_AUTH_TAG_BYTES ) ) != 0 )
  216. {
  217. goto cleanup;
  218. }
  219. if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
  220. {
  221. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  222. goto cleanup;
  223. }
  224. *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
  225. cleanup:
  226. #if defined(MBEDTLS_THREADING_C)
  227. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  228. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  229. #endif
  230. return( ret );
  231. }
  232. /*
  233. * Select key based on name
  234. */
  235. static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
  236. mbedtls_ssl_ticket_context *ctx,
  237. const unsigned char name[4] )
  238. {
  239. unsigned char i;
  240. for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
  241. if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
  242. return( &ctx->keys[i] );
  243. return( NULL );
  244. }
  245. /*
  246. * Load session ticket (see mbedtls_ssl_ticket_write for structure)
  247. */
  248. int mbedtls_ssl_ticket_parse( void *p_ticket,
  249. mbedtls_ssl_session *session,
  250. unsigned char *buf,
  251. size_t len )
  252. {
  253. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  254. mbedtls_ssl_ticket_context *ctx = p_ticket;
  255. mbedtls_ssl_ticket_key *key;
  256. unsigned char *key_name = buf;
  257. unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
  258. unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
  259. unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
  260. size_t enc_len, clear_len;
  261. if( ctx == NULL || ctx->f_rng == NULL )
  262. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  263. if( len < TICKET_MIN_LEN )
  264. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  265. #if defined(MBEDTLS_THREADING_C)
  266. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  267. return( ret );
  268. #endif
  269. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  270. goto cleanup;
  271. enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
  272. if( len != TICKET_MIN_LEN + enc_len )
  273. {
  274. ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  275. goto cleanup;
  276. }
  277. /* Select key */
  278. if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
  279. {
  280. /* We can't know for sure but this is a likely option unless we're
  281. * under attack - this is only informative anyway */
  282. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  283. goto cleanup;
  284. }
  285. /* Decrypt and authenticate */
  286. if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
  287. iv, TICKET_IV_BYTES,
  288. /* Additional data: key name, IV and length */
  289. key_name, TICKET_ADD_DATA_LEN,
  290. ticket, enc_len + TICKET_AUTH_TAG_BYTES,
  291. ticket, enc_len, &clear_len,
  292. TICKET_AUTH_TAG_BYTES ) ) != 0 )
  293. {
  294. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  295. ret = MBEDTLS_ERR_SSL_INVALID_MAC;
  296. goto cleanup;
  297. }
  298. if( clear_len != enc_len )
  299. {
  300. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  301. goto cleanup;
  302. }
  303. /* Actually load session */
  304. if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
  305. goto cleanup;
  306. #if defined(MBEDTLS_HAVE_TIME)
  307. {
  308. /* Check for expiration */
  309. mbedtls_time_t current_time = mbedtls_time( NULL );
  310. if( current_time < session->start ||
  311. (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
  312. {
  313. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  314. goto cleanup;
  315. }
  316. }
  317. #endif
  318. cleanup:
  319. #if defined(MBEDTLS_THREADING_C)
  320. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  321. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  322. #endif
  323. return( ret );
  324. }
  325. /*
  326. * Free context
  327. */
  328. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
  329. {
  330. mbedtls_cipher_free( &ctx->keys[0].ctx );
  331. mbedtls_cipher_free( &ctx->keys[1].ctx );
  332. #if defined(MBEDTLS_THREADING_C)
  333. mbedtls_mutex_free( &ctx->mutex );
  334. #endif
  335. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
  336. }
  337. #endif /* MBEDTLS_SSL_TICKET_C */