ssl_ticket.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * Initialze 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. static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
  59. unsigned char index )
  60. {
  61. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  62. unsigned char buf[MAX_KEY_BYTES];
  63. mbedtls_ssl_ticket_key *key = ctx->keys + index;
  64. #if defined(MBEDTLS_HAVE_TIME)
  65. key->generation_time = (uint32_t) mbedtls_time( NULL );
  66. #endif
  67. if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
  68. return( ret );
  69. if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
  70. return( ret );
  71. /* With GCM and CCM, same context can encrypt & decrypt */
  72. ret = mbedtls_cipher_setkey( &key->ctx, buf,
  73. mbedtls_cipher_get_key_bitlen( &key->ctx ),
  74. MBEDTLS_ENCRYPT );
  75. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  76. return( ret );
  77. }
  78. /*
  79. * Rotate/generate keys if necessary
  80. */
  81. static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
  82. {
  83. #if !defined(MBEDTLS_HAVE_TIME)
  84. ((void) ctx);
  85. #else
  86. if( ctx->ticket_lifetime != 0 )
  87. {
  88. uint32_t current_time = (uint32_t) mbedtls_time( NULL );
  89. uint32_t key_time = ctx->keys[ctx->active].generation_time;
  90. if( current_time >= key_time &&
  91. current_time - key_time < ctx->ticket_lifetime )
  92. {
  93. return( 0 );
  94. }
  95. ctx->active = 1 - ctx->active;
  96. return( ssl_ticket_gen_key( ctx, ctx->active ) );
  97. }
  98. else
  99. #endif /* MBEDTLS_HAVE_TIME */
  100. return( 0 );
  101. }
  102. /*
  103. * Setup context for actual use
  104. */
  105. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  106. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  107. mbedtls_cipher_type_t cipher,
  108. uint32_t lifetime )
  109. {
  110. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  111. const mbedtls_cipher_info_t *cipher_info;
  112. ctx->f_rng = f_rng;
  113. ctx->p_rng = p_rng;
  114. ctx->ticket_lifetime = lifetime;
  115. cipher_info = mbedtls_cipher_info_from_type( cipher);
  116. if( cipher_info == NULL )
  117. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  118. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  119. cipher_info->mode != MBEDTLS_MODE_CCM )
  120. {
  121. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  122. }
  123. if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
  124. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  125. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  126. ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx,
  127. cipher_info, TICKET_AUTH_TAG_BYTES );
  128. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  129. return( ret );
  130. /* We don't yet expect to support all ciphers through PSA,
  131. * so allow fallback to ordinary mbedtls_cipher_setup(). */
  132. if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  133. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  134. if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
  135. return( ret );
  136. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  137. ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx,
  138. cipher_info, TICKET_AUTH_TAG_BYTES );
  139. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  140. return( ret );
  141. if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  142. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  143. if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
  144. return( ret );
  145. if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
  146. ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
  147. {
  148. return( ret );
  149. }
  150. return( 0 );
  151. }
  152. /*
  153. * Create session ticket, with the following structure:
  154. *
  155. * struct {
  156. * opaque key_name[4];
  157. * opaque iv[12];
  158. * opaque encrypted_state<0..2^16-1>;
  159. * opaque tag[16];
  160. * } ticket;
  161. *
  162. * The key_name, iv, and length of encrypted_state are the additional
  163. * authenticated data.
  164. */
  165. int mbedtls_ssl_ticket_write( void *p_ticket,
  166. const mbedtls_ssl_session *session,
  167. unsigned char *start,
  168. const unsigned char *end,
  169. size_t *tlen,
  170. uint32_t *ticket_lifetime )
  171. {
  172. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  173. mbedtls_ssl_ticket_context *ctx = p_ticket;
  174. mbedtls_ssl_ticket_key *key;
  175. unsigned char *key_name = start;
  176. unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
  177. unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
  178. unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
  179. size_t clear_len, ciph_len;
  180. *tlen = 0;
  181. if( ctx == NULL || ctx->f_rng == NULL )
  182. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  183. /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
  184. * in addition to session itself, that will be checked when writing it. */
  185. MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
  186. #if defined(MBEDTLS_THREADING_C)
  187. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  188. return( ret );
  189. #endif
  190. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  191. goto cleanup;
  192. key = &ctx->keys[ctx->active];
  193. *ticket_lifetime = ctx->ticket_lifetime;
  194. memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
  195. if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
  196. goto cleanup;
  197. /* Dump session state */
  198. if( ( ret = mbedtls_ssl_session_save( session,
  199. state, end - state,
  200. &clear_len ) ) != 0 ||
  201. (unsigned long) clear_len > 65535 )
  202. {
  203. goto cleanup;
  204. }
  205. state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
  206. state_len_bytes[1] = ( clear_len ) & 0xff;
  207. /* Encrypt and authenticate */
  208. if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
  209. iv, TICKET_IV_BYTES,
  210. /* Additional data: key name, IV and length */
  211. key_name, TICKET_ADD_DATA_LEN,
  212. state, clear_len,
  213. state, end - state, &ciph_len,
  214. TICKET_AUTH_TAG_BYTES ) ) != 0 )
  215. {
  216. goto cleanup;
  217. }
  218. if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
  219. {
  220. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  221. goto cleanup;
  222. }
  223. *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
  224. cleanup:
  225. #if defined(MBEDTLS_THREADING_C)
  226. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  227. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  228. #endif
  229. return( ret );
  230. }
  231. /*
  232. * Select key based on name
  233. */
  234. static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
  235. mbedtls_ssl_ticket_context *ctx,
  236. const unsigned char name[4] )
  237. {
  238. unsigned char i;
  239. for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
  240. if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
  241. return( &ctx->keys[i] );
  242. return( NULL );
  243. }
  244. /*
  245. * Load session ticket (see mbedtls_ssl_ticket_write for structure)
  246. */
  247. int mbedtls_ssl_ticket_parse( void *p_ticket,
  248. mbedtls_ssl_session *session,
  249. unsigned char *buf,
  250. size_t len )
  251. {
  252. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  253. mbedtls_ssl_ticket_context *ctx = p_ticket;
  254. mbedtls_ssl_ticket_key *key;
  255. unsigned char *key_name = buf;
  256. unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
  257. unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
  258. unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
  259. size_t enc_len, clear_len;
  260. if( ctx == NULL || ctx->f_rng == NULL )
  261. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  262. if( len < TICKET_MIN_LEN )
  263. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  264. #if defined(MBEDTLS_THREADING_C)
  265. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  266. return( ret );
  267. #endif
  268. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  269. goto cleanup;
  270. enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
  271. if( len != TICKET_MIN_LEN + enc_len )
  272. {
  273. ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  274. goto cleanup;
  275. }
  276. /* Select key */
  277. if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
  278. {
  279. /* We can't know for sure but this is a likely option unless we're
  280. * under attack - this is only informative anyway */
  281. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  282. goto cleanup;
  283. }
  284. /* Decrypt and authenticate */
  285. if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
  286. iv, TICKET_IV_BYTES,
  287. /* Additional data: key name, IV and length */
  288. key_name, TICKET_ADD_DATA_LEN,
  289. ticket, enc_len + TICKET_AUTH_TAG_BYTES,
  290. ticket, enc_len, &clear_len,
  291. TICKET_AUTH_TAG_BYTES ) ) != 0 )
  292. {
  293. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  294. ret = MBEDTLS_ERR_SSL_INVALID_MAC;
  295. goto cleanup;
  296. }
  297. if( clear_len != enc_len )
  298. {
  299. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  300. goto cleanup;
  301. }
  302. /* Actually load session */
  303. if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
  304. goto cleanup;
  305. #if defined(MBEDTLS_HAVE_TIME)
  306. {
  307. /* Check for expiration */
  308. mbedtls_time_t current_time = mbedtls_time( NULL );
  309. if( current_time < session->start ||
  310. (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
  311. {
  312. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  313. goto cleanup;
  314. }
  315. }
  316. #endif
  317. cleanup:
  318. #if defined(MBEDTLS_THREADING_C)
  319. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  320. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  321. #endif
  322. return( ret );
  323. }
  324. /*
  325. * Free context
  326. */
  327. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
  328. {
  329. mbedtls_cipher_free( &ctx->keys[0].ctx );
  330. mbedtls_cipher_free( &ctx->keys[1].ctx );
  331. #if defined(MBEDTLS_THREADING_C)
  332. mbedtls_mutex_free( &ctx->mutex );
  333. #endif
  334. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
  335. }
  336. #endif /* MBEDTLS_SSL_TICKET_C */