ssl_ticket.c 12 KB

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