ssl_ticket.c 13 KB

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