ssl_ticket.c 12 KB

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