ssl_ticket.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * TLS server tickets callbacks implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_SSL_TICKET_C)
  52. #if defined(MBEDTLS_PLATFORM_C)
  53. #include "mbedtls/platform.h"
  54. #else
  55. #include <stdlib.h>
  56. #define mbedtls_calloc calloc
  57. #define mbedtls_free free
  58. #endif
  59. #include "mbedtls/ssl_internal.h"
  60. #include "mbedtls/ssl_ticket.h"
  61. #include "mbedtls/platform_util.h"
  62. #include <string.h>
  63. /*
  64. * Initialze context
  65. */
  66. void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
  67. {
  68. memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
  69. #if defined(MBEDTLS_THREADING_C)
  70. mbedtls_mutex_init( &ctx->mutex );
  71. #endif
  72. }
  73. #define MAX_KEY_BYTES 32 /* 256 bits */
  74. #define TICKET_KEY_NAME_BYTES 4
  75. #define TICKET_IV_BYTES 12
  76. #define TICKET_CRYPT_LEN_BYTES 2
  77. #define TICKET_AUTH_TAG_BYTES 16
  78. #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
  79. TICKET_IV_BYTES + \
  80. TICKET_CRYPT_LEN_BYTES + \
  81. TICKET_AUTH_TAG_BYTES )
  82. #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
  83. TICKET_IV_BYTES + \
  84. TICKET_CRYPT_LEN_BYTES )
  85. /*
  86. * Generate/update a key
  87. */
  88. static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
  89. unsigned char index )
  90. {
  91. int ret;
  92. unsigned char buf[MAX_KEY_BYTES];
  93. mbedtls_ssl_ticket_key *key = ctx->keys + index;
  94. #if defined(MBEDTLS_HAVE_TIME)
  95. key->generation_time = (uint32_t) mbedtls_time( NULL );
  96. #endif
  97. if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
  98. return( ret );
  99. if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
  100. return( ret );
  101. /* With GCM and CCM, same context can encrypt & decrypt */
  102. ret = mbedtls_cipher_setkey( &key->ctx, buf,
  103. mbedtls_cipher_get_key_bitlen( &key->ctx ),
  104. MBEDTLS_ENCRYPT );
  105. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  106. return( ret );
  107. }
  108. /*
  109. * Rotate/generate keys if necessary
  110. */
  111. static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
  112. {
  113. #if !defined(MBEDTLS_HAVE_TIME)
  114. ((void) ctx);
  115. #else
  116. if( ctx->ticket_lifetime != 0 )
  117. {
  118. uint32_t current_time = (uint32_t) mbedtls_time( NULL );
  119. uint32_t key_time = ctx->keys[ctx->active].generation_time;
  120. if( current_time >= key_time &&
  121. current_time - key_time < ctx->ticket_lifetime )
  122. {
  123. return( 0 );
  124. }
  125. ctx->active = 1 - ctx->active;
  126. return( ssl_ticket_gen_key( ctx, ctx->active ) );
  127. }
  128. else
  129. #endif /* MBEDTLS_HAVE_TIME */
  130. return( 0 );
  131. }
  132. /*
  133. * Setup context for actual use
  134. */
  135. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  136. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  137. mbedtls_cipher_type_t cipher,
  138. uint32_t lifetime )
  139. {
  140. int ret;
  141. const mbedtls_cipher_info_t *cipher_info;
  142. ctx->f_rng = f_rng;
  143. ctx->p_rng = p_rng;
  144. ctx->ticket_lifetime = lifetime;
  145. cipher_info = mbedtls_cipher_info_from_type( cipher);
  146. if( cipher_info == NULL )
  147. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  148. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  149. cipher_info->mode != MBEDTLS_MODE_CCM )
  150. {
  151. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  152. }
  153. if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
  154. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  155. if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
  156. ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
  157. {
  158. return( ret );
  159. }
  160. if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
  161. ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
  162. {
  163. return( ret );
  164. }
  165. return( 0 );
  166. }
  167. /*
  168. * Serialize a session in the following format:
  169. * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
  170. * n . n+2 peer_cert length = m (0 if no certificate)
  171. * n+3 . n+2+m peer cert ASN.1
  172. */
  173. static int ssl_save_session( const mbedtls_ssl_session *session,
  174. unsigned char *buf, size_t buf_len,
  175. size_t *olen )
  176. {
  177. unsigned char *p = buf;
  178. size_t left = buf_len;
  179. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  180. size_t cert_len;
  181. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  182. if( left < sizeof( mbedtls_ssl_session ) )
  183. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  184. memcpy( p, session, sizeof( mbedtls_ssl_session ) );
  185. p += sizeof( mbedtls_ssl_session );
  186. left -= sizeof( mbedtls_ssl_session );
  187. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  188. if( session->peer_cert == NULL )
  189. cert_len = 0;
  190. else
  191. cert_len = session->peer_cert->raw.len;
  192. if( left < 3 + cert_len )
  193. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  194. *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
  195. *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
  196. *p++ = (unsigned char)( ( cert_len ) & 0xFF );
  197. if( session->peer_cert != NULL )
  198. memcpy( p, session->peer_cert->raw.p, cert_len );
  199. p += cert_len;
  200. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  201. *olen = p - buf;
  202. return( 0 );
  203. }
  204. /*
  205. * Unserialise session, see ssl_save_session()
  206. */
  207. static int ssl_load_session( mbedtls_ssl_session *session,
  208. const unsigned char *buf, size_t len )
  209. {
  210. const unsigned char *p = buf;
  211. const unsigned char * const end = buf + len;
  212. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  213. size_t cert_len;
  214. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  215. if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) )
  216. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  217. memcpy( session, p, sizeof( mbedtls_ssl_session ) );
  218. p += sizeof( mbedtls_ssl_session );
  219. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  220. if( 3 > (size_t)( end - p ) )
  221. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  222. cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
  223. p += 3;
  224. if( cert_len == 0 )
  225. {
  226. session->peer_cert = NULL;
  227. }
  228. else
  229. {
  230. int ret;
  231. if( cert_len > (size_t)( end - p ) )
  232. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  233. session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  234. if( session->peer_cert == NULL )
  235. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  236. mbedtls_x509_crt_init( session->peer_cert );
  237. if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
  238. p, cert_len ) ) != 0 )
  239. {
  240. mbedtls_x509_crt_free( session->peer_cert );
  241. mbedtls_free( session->peer_cert );
  242. session->peer_cert = NULL;
  243. return( ret );
  244. }
  245. p += cert_len;
  246. }
  247. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  248. if( p != end )
  249. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  250. return( 0 );
  251. }
  252. /*
  253. * Create session ticket, with the following structure:
  254. *
  255. * struct {
  256. * opaque key_name[4];
  257. * opaque iv[12];
  258. * opaque encrypted_state<0..2^16-1>;
  259. * opaque tag[16];
  260. * } ticket;
  261. *
  262. * The key_name, iv, and length of encrypted_state are the additional
  263. * authenticated data.
  264. */
  265. int mbedtls_ssl_ticket_write( void *p_ticket,
  266. const mbedtls_ssl_session *session,
  267. unsigned char *start,
  268. const unsigned char *end,
  269. size_t *tlen,
  270. uint32_t *ticket_lifetime )
  271. {
  272. int ret;
  273. mbedtls_ssl_ticket_context *ctx = p_ticket;
  274. mbedtls_ssl_ticket_key *key;
  275. unsigned char *key_name = start;
  276. unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
  277. unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
  278. unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
  279. unsigned char *tag;
  280. size_t clear_len, ciph_len;
  281. *tlen = 0;
  282. if( ctx == NULL || ctx->f_rng == NULL )
  283. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  284. /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
  285. * in addition to session itself, that will be checked when writing it. */
  286. MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
  287. #if defined(MBEDTLS_THREADING_C)
  288. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  289. return( ret );
  290. #endif
  291. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  292. goto cleanup;
  293. key = &ctx->keys[ctx->active];
  294. *ticket_lifetime = ctx->ticket_lifetime;
  295. memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
  296. if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
  297. goto cleanup;
  298. /* Dump session state */
  299. if( ( ret = ssl_save_session( session,
  300. state, end - state, &clear_len ) ) != 0 ||
  301. (unsigned long) clear_len > 65535 )
  302. {
  303. goto cleanup;
  304. }
  305. state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
  306. state_len_bytes[1] = ( clear_len ) & 0xff;
  307. /* Encrypt and authenticate */
  308. tag = state + clear_len;
  309. if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
  310. iv, TICKET_IV_BYTES,
  311. /* Additional data: key name, IV and length */
  312. key_name, TICKET_ADD_DATA_LEN,
  313. state, clear_len, state, &ciph_len,
  314. tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
  315. {
  316. goto cleanup;
  317. }
  318. if( ciph_len != clear_len )
  319. {
  320. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  321. goto cleanup;
  322. }
  323. *tlen = TICKET_MIN_LEN + ciph_len;
  324. cleanup:
  325. #if defined(MBEDTLS_THREADING_C)
  326. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  327. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  328. #endif
  329. return( ret );
  330. }
  331. /*
  332. * Select key based on name
  333. */
  334. static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
  335. mbedtls_ssl_ticket_context *ctx,
  336. const unsigned char name[4] )
  337. {
  338. unsigned char i;
  339. for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
  340. if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
  341. return( &ctx->keys[i] );
  342. return( NULL );
  343. }
  344. /*
  345. * Load session ticket (see mbedtls_ssl_ticket_write for structure)
  346. */
  347. int mbedtls_ssl_ticket_parse( void *p_ticket,
  348. mbedtls_ssl_session *session,
  349. unsigned char *buf,
  350. size_t len )
  351. {
  352. int ret;
  353. mbedtls_ssl_ticket_context *ctx = p_ticket;
  354. mbedtls_ssl_ticket_key *key;
  355. unsigned char *key_name = buf;
  356. unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
  357. unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
  358. unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
  359. unsigned char *tag;
  360. size_t enc_len, clear_len;
  361. if( ctx == NULL || ctx->f_rng == NULL )
  362. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  363. if( len < TICKET_MIN_LEN )
  364. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  365. #if defined(MBEDTLS_THREADING_C)
  366. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  367. return( ret );
  368. #endif
  369. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  370. goto cleanup;
  371. enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
  372. tag = ticket + enc_len;
  373. if( len != TICKET_MIN_LEN + enc_len )
  374. {
  375. ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  376. goto cleanup;
  377. }
  378. /* Select key */
  379. if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
  380. {
  381. /* We can't know for sure but this is a likely option unless we're
  382. * under attack - this is only informative anyway */
  383. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  384. goto cleanup;
  385. }
  386. /* Decrypt and authenticate */
  387. if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx,
  388. iv, TICKET_IV_BYTES,
  389. /* Additional data: key name, IV and length */
  390. key_name, TICKET_ADD_DATA_LEN,
  391. ticket, enc_len,
  392. ticket, &clear_len,
  393. tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
  394. {
  395. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  396. ret = MBEDTLS_ERR_SSL_INVALID_MAC;
  397. goto cleanup;
  398. }
  399. if( clear_len != enc_len )
  400. {
  401. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  402. goto cleanup;
  403. }
  404. /* Actually load session */
  405. if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
  406. goto cleanup;
  407. #if defined(MBEDTLS_HAVE_TIME)
  408. {
  409. /* Check for expiration */
  410. mbedtls_time_t current_time = mbedtls_time( NULL );
  411. if( current_time < session->start ||
  412. (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
  413. {
  414. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  415. goto cleanup;
  416. }
  417. }
  418. #endif
  419. cleanup:
  420. #if defined(MBEDTLS_THREADING_C)
  421. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  422. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  423. #endif
  424. return( ret );
  425. }
  426. /*
  427. * Free context
  428. */
  429. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
  430. {
  431. mbedtls_cipher_free( &ctx->keys[0].ctx );
  432. mbedtls_cipher_free( &ctx->keys[1].ctx );
  433. #if defined(MBEDTLS_THREADING_C)
  434. mbedtls_mutex_free( &ctx->mutex );
  435. #endif
  436. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
  437. }
  438. #endif /* MBEDTLS_SSL_TICKET_C */