ssl_ticket.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * TLS server tickets callbacks implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_SSL_TICKET_C)
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdlib.h>
  33. #define mbedtls_calloc calloc
  34. #define mbedtls_free free
  35. #endif
  36. #include "mbedtls/ssl_ticket.h"
  37. #include <string.h>
  38. /* Implementation that should never be optimized out by the compiler */
  39. static void mbedtls_zeroize( void *v, size_t n ) {
  40. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  41. }
  42. /*
  43. * Initialze context
  44. */
  45. void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
  46. {
  47. memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
  48. #if defined(MBEDTLS_THREADING_C)
  49. mbedtls_mutex_init( &ctx->mutex );
  50. #endif
  51. }
  52. #define MAX_KEY_BYTES 32 /* 256 bits */
  53. /*
  54. * Generate/update a key
  55. */
  56. static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
  57. unsigned char index )
  58. {
  59. int ret;
  60. unsigned char buf[MAX_KEY_BYTES];
  61. mbedtls_ssl_ticket_key *key = ctx->keys + index;
  62. #if defined(MBEDTLS_HAVE_TIME)
  63. key->generation_time = (uint32_t) mbedtls_time( NULL );
  64. #endif
  65. if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
  66. return( ret );
  67. if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
  68. return( ret );
  69. /* With GCM and CCM, same context can encrypt & decrypt */
  70. ret = mbedtls_cipher_setkey( &key->ctx, buf,
  71. mbedtls_cipher_get_key_bitlen( &key->ctx ),
  72. MBEDTLS_ENCRYPT );
  73. mbedtls_zeroize( buf, sizeof( buf ) );
  74. return( ret );
  75. }
  76. /*
  77. * Rotate/generate keys if necessary
  78. */
  79. static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
  80. {
  81. #if !defined(MBEDTLS_HAVE_TIME)
  82. ((void) ctx);
  83. #else
  84. if( ctx->ticket_lifetime != 0 )
  85. {
  86. uint32_t current_time = (uint32_t) mbedtls_time( NULL );
  87. uint32_t key_time = ctx->keys[ctx->active].generation_time;
  88. if( current_time > key_time &&
  89. current_time - key_time < ctx->ticket_lifetime )
  90. {
  91. return( 0 );
  92. }
  93. ctx->active = 1 - ctx->active;
  94. return( ssl_ticket_gen_key( ctx, ctx->active ) );
  95. }
  96. else
  97. #endif /* MBEDTLS_HAVE_TIME */
  98. return( 0 );
  99. }
  100. /*
  101. * Setup context for actual use
  102. */
  103. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  104. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  105. mbedtls_cipher_type_t cipher,
  106. uint32_t lifetime )
  107. {
  108. int ret;
  109. const mbedtls_cipher_info_t *cipher_info;
  110. ctx->f_rng = f_rng;
  111. ctx->p_rng = p_rng;
  112. ctx->ticket_lifetime = lifetime;
  113. cipher_info = mbedtls_cipher_info_from_type( cipher);
  114. if( cipher_info == NULL )
  115. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  116. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  117. cipher_info->mode != MBEDTLS_MODE_CCM )
  118. {
  119. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  120. }
  121. if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
  122. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  123. if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
  124. ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
  125. {
  126. return( ret );
  127. }
  128. if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
  129. ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
  130. {
  131. return( ret );
  132. }
  133. return( 0 );
  134. }
  135. /*
  136. * Serialize a session in the following format:
  137. * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
  138. * n . n+2 peer_cert length = m (0 if no certificate)
  139. * n+3 . n+2+m peer cert ASN.1
  140. */
  141. static int ssl_save_session( const mbedtls_ssl_session *session,
  142. unsigned char *buf, size_t buf_len,
  143. size_t *olen )
  144. {
  145. unsigned char *p = buf;
  146. size_t left = buf_len;
  147. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  148. size_t cert_len;
  149. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  150. if( left < sizeof( mbedtls_ssl_session ) )
  151. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  152. memcpy( p, session, sizeof( mbedtls_ssl_session ) );
  153. p += sizeof( mbedtls_ssl_session );
  154. left -= sizeof( mbedtls_ssl_session );
  155. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  156. if( session->peer_cert == NULL )
  157. cert_len = 0;
  158. else
  159. cert_len = session->peer_cert->raw.len;
  160. if( left < 3 + cert_len )
  161. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  162. *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
  163. *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
  164. *p++ = (unsigned char)( cert_len & 0xFF );
  165. if( session->peer_cert != NULL )
  166. memcpy( p, session->peer_cert->raw.p, cert_len );
  167. p += cert_len;
  168. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  169. *olen = p - buf;
  170. return( 0 );
  171. }
  172. /*
  173. * Unserialise session, see ssl_save_session()
  174. */
  175. static int ssl_load_session( mbedtls_ssl_session *session,
  176. const unsigned char *buf, size_t len )
  177. {
  178. const unsigned char *p = buf;
  179. const unsigned char * const end = buf + len;
  180. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  181. size_t cert_len;
  182. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  183. if( p + sizeof( mbedtls_ssl_session ) > end )
  184. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  185. memcpy( session, p, sizeof( mbedtls_ssl_session ) );
  186. p += sizeof( mbedtls_ssl_session );
  187. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  188. if( p + 3 > end )
  189. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  190. cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
  191. p += 3;
  192. if( cert_len == 0 )
  193. {
  194. session->peer_cert = NULL;
  195. }
  196. else
  197. {
  198. int ret;
  199. if( p + cert_len > end )
  200. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  201. session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  202. if( session->peer_cert == NULL )
  203. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  204. mbedtls_x509_crt_init( session->peer_cert );
  205. if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
  206. p, cert_len ) ) != 0 )
  207. {
  208. mbedtls_x509_crt_free( session->peer_cert );
  209. mbedtls_free( session->peer_cert );
  210. session->peer_cert = NULL;
  211. return( ret );
  212. }
  213. p += cert_len;
  214. }
  215. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  216. if( p != end )
  217. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  218. return( 0 );
  219. }
  220. /*
  221. * Create session ticket, with the following structure:
  222. *
  223. * struct {
  224. * opaque key_name[4];
  225. * opaque iv[12];
  226. * opaque encrypted_state<0..2^16-1>;
  227. * opaque tag[16];
  228. * } ticket;
  229. *
  230. * The key_name, iv, and length of encrypted_state are the additional
  231. * authenticated data.
  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;
  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 + 4;
  245. unsigned char *state_len_bytes = iv + 12;
  246. unsigned char *state = state_len_bytes + 2;
  247. unsigned char *tag;
  248. size_t clear_len, ciph_len;
  249. *tlen = 0;
  250. if( ctx == NULL || ctx->f_rng == NULL )
  251. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  252. /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
  253. * in addition to session itself, that will be checked when writing it. */
  254. if( end - start < 4 + 12 + 2 + 16 )
  255. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  256. #if defined(MBEDTLS_THREADING_C)
  257. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  258. return( ret );
  259. #endif
  260. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  261. goto cleanup;
  262. key = &ctx->keys[ctx->active];
  263. *ticket_lifetime = ctx->ticket_lifetime;
  264. memcpy( key_name, key->name, 4 );
  265. if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 )
  266. goto cleanup;
  267. /* Dump session state */
  268. if( ( ret = ssl_save_session( session,
  269. state, end - state, &clear_len ) ) != 0 ||
  270. (unsigned long) clear_len > 65535 )
  271. {
  272. goto cleanup;
  273. }
  274. state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
  275. state_len_bytes[1] = ( clear_len ) & 0xff;
  276. /* Encrypt and authenticate */
  277. tag = state + clear_len;
  278. if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
  279. iv, 12, key_name, 4 + 12 + 2,
  280. state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 )
  281. {
  282. goto cleanup;
  283. }
  284. if( ciph_len != clear_len )
  285. {
  286. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  287. goto cleanup;
  288. }
  289. *tlen = 4 + 12 + 2 + 16 + ciph_len;
  290. cleanup:
  291. #if defined(MBEDTLS_THREADING_C)
  292. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  293. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  294. #endif
  295. return( ret );
  296. }
  297. /*
  298. * Select key based on name
  299. */
  300. static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
  301. mbedtls_ssl_ticket_context *ctx,
  302. const unsigned char name[4] )
  303. {
  304. unsigned char i;
  305. for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
  306. if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
  307. return( &ctx->keys[i] );
  308. return( NULL );
  309. }
  310. /*
  311. * Load session ticket (see mbedtls_ssl_ticket_write for structure)
  312. */
  313. int mbedtls_ssl_ticket_parse( void *p_ticket,
  314. mbedtls_ssl_session *session,
  315. unsigned char *buf,
  316. size_t len )
  317. {
  318. int ret;
  319. mbedtls_ssl_ticket_context *ctx = p_ticket;
  320. mbedtls_ssl_ticket_key *key;
  321. unsigned char *key_name = buf;
  322. unsigned char *iv = buf + 4;
  323. unsigned char *enc_len_p = iv + 12;
  324. unsigned char *ticket = enc_len_p + 2;
  325. unsigned char *tag;
  326. size_t enc_len, clear_len;
  327. if( ctx == NULL || ctx->f_rng == NULL )
  328. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  329. /* See mbedtls_ssl_ticket_write() */
  330. if( len < 4 + 12 + 2 + 16 )
  331. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  332. #if defined(MBEDTLS_THREADING_C)
  333. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  334. return( ret );
  335. #endif
  336. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  337. goto cleanup;
  338. enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
  339. tag = ticket + enc_len;
  340. if( len != 4 + 12 + 2 + enc_len + 16 )
  341. {
  342. ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  343. goto cleanup;
  344. }
  345. /* Select key */
  346. if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
  347. {
  348. /* We can't know for sure but this is a likely option unless we're
  349. * under attack - this is only informative anyway */
  350. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  351. goto cleanup;
  352. }
  353. /* Decrypt and authenticate */
  354. if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12,
  355. key_name, 4 + 12 + 2, ticket, enc_len,
  356. ticket, &clear_len, tag, 16 ) ) != 0 )
  357. {
  358. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  359. ret = MBEDTLS_ERR_SSL_INVALID_MAC;
  360. goto cleanup;
  361. }
  362. if( clear_len != enc_len )
  363. {
  364. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  365. goto cleanup;
  366. }
  367. /* Actually load session */
  368. if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
  369. goto cleanup;
  370. #if defined(MBEDTLS_HAVE_TIME)
  371. {
  372. /* Check for expiration */
  373. mbedtls_time_t current_time = mbedtls_time( NULL );
  374. if( current_time < session->start ||
  375. (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
  376. {
  377. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  378. goto cleanup;
  379. }
  380. }
  381. #endif
  382. cleanup:
  383. #if defined(MBEDTLS_THREADING_C)
  384. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  385. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  386. #endif
  387. return( ret );
  388. }
  389. /*
  390. * Free context
  391. */
  392. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
  393. {
  394. mbedtls_cipher_free( &ctx->keys[0].ctx );
  395. mbedtls_cipher_free( &ctx->keys[1].ctx );
  396. #if defined(MBEDTLS_THREADING_C)
  397. mbedtls_mutex_free( &ctx->mutex );
  398. #endif
  399. mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
  400. }
  401. #endif /* MBEDTLS_SSL_TICKET_C */