ssl_session.cc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE. */
  135. #include <openssl/ssl.h>
  136. #include <assert.h>
  137. #include <stdlib.h>
  138. #include <string.h>
  139. #include <utility>
  140. #include <openssl/err.h>
  141. #include <openssl/hmac.h>
  142. #include <openssl/lhash.h>
  143. #include <openssl/mem.h>
  144. #include <openssl/rand.h>
  145. #include "internal.h"
  146. #include "../crypto/internal.h"
  147. namespace bssl {
  148. // The address of this is a magic value, a pointer to which is returned by
  149. // SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  150. // that it needs to asynchronously fetch session information.
  151. static const char g_pending_session_magic = 0;
  152. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  153. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  154. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
  155. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
  156. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
  157. UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method) {
  158. UniquePtr<SSL_SESSION> session(
  159. (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)));
  160. if (!session) {
  161. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  162. return 0;
  163. }
  164. OPENSSL_memset(session.get(), 0, sizeof(SSL_SESSION));
  165. session->x509_method = x509_method;
  166. session->verify_result = X509_V_ERR_INVALID_CALL;
  167. session->references = 1;
  168. session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  169. session->auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  170. session->time = time(NULL);
  171. CRYPTO_new_ex_data(&session->ex_data);
  172. return session;
  173. }
  174. UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) {
  175. UniquePtr<SSL_SESSION> new_session = ssl_session_new(session->x509_method);
  176. if (!new_session) {
  177. return nullptr;
  178. }
  179. new_session->is_server = session->is_server;
  180. new_session->ssl_version = session->ssl_version;
  181. new_session->sid_ctx_length = session->sid_ctx_length;
  182. OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length);
  183. // Copy the key material.
  184. new_session->master_key_length = session->master_key_length;
  185. OPENSSL_memcpy(new_session->master_key, session->master_key,
  186. session->master_key_length);
  187. new_session->cipher = session->cipher;
  188. // Copy authentication state.
  189. if (session->psk_identity != NULL) {
  190. new_session->psk_identity = BUF_strdup(session->psk_identity);
  191. if (new_session->psk_identity == NULL) {
  192. return nullptr;
  193. }
  194. }
  195. if (session->certs != NULL) {
  196. new_session->certs = sk_CRYPTO_BUFFER_new_null();
  197. if (new_session->certs == NULL) {
  198. return nullptr;
  199. }
  200. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(session->certs); i++) {
  201. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(session->certs, i);
  202. if (!sk_CRYPTO_BUFFER_push(new_session->certs, buffer)) {
  203. return nullptr;
  204. }
  205. CRYPTO_BUFFER_up_ref(buffer);
  206. }
  207. }
  208. if (!session->x509_method->session_dup(new_session.get(), session)) {
  209. return nullptr;
  210. }
  211. new_session->verify_result = session->verify_result;
  212. if (session->ocsp_response != NULL) {
  213. new_session->ocsp_response = session->ocsp_response;
  214. CRYPTO_BUFFER_up_ref(new_session->ocsp_response);
  215. }
  216. if (session->signed_cert_timestamp_list != NULL) {
  217. new_session->signed_cert_timestamp_list =
  218. session->signed_cert_timestamp_list;
  219. CRYPTO_BUFFER_up_ref(new_session->signed_cert_timestamp_list);
  220. }
  221. OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256,
  222. SHA256_DIGEST_LENGTH);
  223. new_session->peer_sha256_valid = session->peer_sha256_valid;
  224. new_session->peer_signature_algorithm = session->peer_signature_algorithm;
  225. new_session->timeout = session->timeout;
  226. new_session->auth_timeout = session->auth_timeout;
  227. new_session->time = session->time;
  228. // Copy non-authentication connection properties.
  229. if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) {
  230. new_session->session_id_length = session->session_id_length;
  231. OPENSSL_memcpy(new_session->session_id, session->session_id,
  232. session->session_id_length);
  233. new_session->group_id = session->group_id;
  234. OPENSSL_memcpy(new_session->original_handshake_hash,
  235. session->original_handshake_hash,
  236. session->original_handshake_hash_len);
  237. new_session->original_handshake_hash_len =
  238. session->original_handshake_hash_len;
  239. new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint;
  240. new_session->ticket_age_add = session->ticket_age_add;
  241. new_session->ticket_max_early_data = session->ticket_max_early_data;
  242. new_session->extended_master_secret = session->extended_master_secret;
  243. if (session->early_alpn != NULL) {
  244. new_session->early_alpn =
  245. (uint8_t *)BUF_memdup(session->early_alpn, session->early_alpn_len);
  246. if (new_session->early_alpn == NULL) {
  247. return nullptr;
  248. }
  249. }
  250. new_session->early_alpn_len = session->early_alpn_len;
  251. }
  252. // Copy the ticket.
  253. if (dup_flags & SSL_SESSION_INCLUDE_TICKET) {
  254. if (session->tlsext_tick != NULL) {
  255. new_session->tlsext_tick =
  256. (uint8_t *)BUF_memdup(session->tlsext_tick, session->tlsext_ticklen);
  257. if (new_session->tlsext_tick == NULL) {
  258. return nullptr;
  259. }
  260. }
  261. new_session->tlsext_ticklen = session->tlsext_ticklen;
  262. }
  263. // The new_session does not get a copy of the ex_data.
  264. new_session->not_resumable = 1;
  265. return new_session;
  266. }
  267. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) {
  268. struct OPENSSL_timeval now;
  269. ssl_get_current_time(ssl, &now);
  270. // To avoid overflows and underflows, if we've gone back in time, update the
  271. // time, but mark the session expired.
  272. if (session->time > now.tv_sec) {
  273. session->time = now.tv_sec;
  274. session->timeout = 0;
  275. session->auth_timeout = 0;
  276. return;
  277. }
  278. // Adjust the session time and timeouts. If the session has already expired,
  279. // clamp the timeouts at zero.
  280. uint64_t delta = now.tv_sec - session->time;
  281. session->time = now.tv_sec;
  282. if (session->timeout < delta) {
  283. session->timeout = 0;
  284. } else {
  285. session->timeout -= delta;
  286. }
  287. if (session->auth_timeout < delta) {
  288. session->auth_timeout = 0;
  289. } else {
  290. session->auth_timeout -= delta;
  291. }
  292. }
  293. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
  294. uint32_t timeout) {
  295. // Rebase the timestamp relative to the current time so |timeout| is measured
  296. // correctly.
  297. ssl_session_rebase_time(ssl, session);
  298. if (session->timeout > timeout) {
  299. return;
  300. }
  301. session->timeout = timeout;
  302. if (session->timeout > session->auth_timeout) {
  303. session->timeout = session->auth_timeout;
  304. }
  305. }
  306. uint16_t ssl_session_protocol_version(const SSL_SESSION *session) {
  307. uint16_t ret;
  308. if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) {
  309. // An |SSL_SESSION| will never have an invalid version. This is enforced by
  310. // the parser.
  311. assert(0);
  312. return 0;
  313. }
  314. return ret;
  315. }
  316. const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) {
  317. return ssl_get_handshake_digest(ssl_session_protocol_version(session),
  318. session->cipher);
  319. }
  320. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
  321. SSL *const ssl = hs->ssl;
  322. if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
  323. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  324. return 0;
  325. }
  326. UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method);
  327. if (session == NULL) {
  328. return 0;
  329. }
  330. session->is_server = is_server;
  331. session->ssl_version = ssl->version;
  332. // Fill in the time from the |SSL_CTX|'s clock.
  333. struct OPENSSL_timeval now;
  334. ssl_get_current_time(ssl, &now);
  335. session->time = now.tv_sec;
  336. uint16_t version = ssl_protocol_version(ssl);
  337. if (version >= TLS1_3_VERSION) {
  338. // TLS 1.3 uses tickets as authenticators, so we are willing to use them for
  339. // longer.
  340. session->timeout = ssl->session_ctx->session_psk_dhe_timeout;
  341. session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT;
  342. } else {
  343. // TLS 1.2 resumption does not incorporate new key material, so we use a
  344. // much shorter timeout.
  345. session->timeout = ssl->session_ctx->session_timeout;
  346. session->auth_timeout = ssl->session_ctx->session_timeout;
  347. }
  348. if (is_server) {
  349. if (hs->ticket_expected || version >= TLS1_3_VERSION) {
  350. // Don't set session IDs for sessions resumed with tickets. This will keep
  351. // them out of the session cache.
  352. session->session_id_length = 0;
  353. } else {
  354. session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  355. if (!RAND_bytes(session->session_id, session->session_id_length)) {
  356. return 0;
  357. }
  358. }
  359. } else {
  360. session->session_id_length = 0;
  361. }
  362. if (ssl->cert->sid_ctx_length > sizeof(session->sid_ctx)) {
  363. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  364. return 0;
  365. }
  366. OPENSSL_memcpy(session->sid_ctx, ssl->cert->sid_ctx,
  367. ssl->cert->sid_ctx_length);
  368. session->sid_ctx_length = ssl->cert->sid_ctx_length;
  369. // The session is marked not resumable until it is completely filled in.
  370. session->not_resumable = 1;
  371. session->verify_result = X509_V_ERR_INVALID_CALL;
  372. hs->new_session = std::move(session);
  373. ssl_set_session(ssl, NULL);
  374. return 1;
  375. }
  376. int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) {
  377. OPENSSL_timeval now;
  378. ssl_ctx_get_current_time(ctx, &now);
  379. {
  380. // Avoid acquiring a write lock in the common case (i.e. a non-default key
  381. // is used or the default keys have not expired yet).
  382. MutexReadLock lock(&ctx->lock);
  383. if (ctx->tlsext_ticket_key_current &&
  384. (ctx->tlsext_ticket_key_current->next_rotation_tv_sec == 0 ||
  385. ctx->tlsext_ticket_key_current->next_rotation_tv_sec > now.tv_sec) &&
  386. (!ctx->tlsext_ticket_key_prev ||
  387. ctx->tlsext_ticket_key_prev->next_rotation_tv_sec > now.tv_sec)) {
  388. return 1;
  389. }
  390. }
  391. MutexWriteLock lock(&ctx->lock);
  392. if (!ctx->tlsext_ticket_key_current ||
  393. (ctx->tlsext_ticket_key_current->next_rotation_tv_sec != 0 &&
  394. ctx->tlsext_ticket_key_current->next_rotation_tv_sec <= now.tv_sec)) {
  395. // The current key has not been initialized or it is expired.
  396. auto new_key = bssl::MakeUnique<struct tlsext_ticket_key>();
  397. if (!new_key) {
  398. return 0;
  399. }
  400. OPENSSL_memset(new_key.get(), 0, sizeof(struct tlsext_ticket_key));
  401. if (ctx->tlsext_ticket_key_current) {
  402. // The current key expired. Rotate it to prev and bump up its rotation
  403. // timestamp. Note that even with the new rotation time it may still be
  404. // expired and get droppped below.
  405. ctx->tlsext_ticket_key_current->next_rotation_tv_sec +=
  406. SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL;
  407. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  408. ctx->tlsext_ticket_key_prev = ctx->tlsext_ticket_key_current;
  409. }
  410. ctx->tlsext_ticket_key_current = new_key.release();
  411. RAND_bytes(ctx->tlsext_ticket_key_current->name, 16);
  412. RAND_bytes(ctx->tlsext_ticket_key_current->hmac_key, 16);
  413. RAND_bytes(ctx->tlsext_ticket_key_current->aes_key, 16);
  414. ctx->tlsext_ticket_key_current->next_rotation_tv_sec =
  415. now.tv_sec + SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL;
  416. }
  417. // Drop an expired prev key.
  418. if (ctx->tlsext_ticket_key_prev &&
  419. ctx->tlsext_ticket_key_prev->next_rotation_tv_sec <= now.tv_sec) {
  420. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  421. ctx->tlsext_ticket_key_prev = nullptr;
  422. }
  423. return 1;
  424. }
  425. static int ssl_encrypt_ticket_with_cipher_ctx(SSL *ssl, CBB *out,
  426. const uint8_t *session_buf,
  427. size_t session_len) {
  428. ScopedEVP_CIPHER_CTX ctx;
  429. ScopedHMAC_CTX hctx;
  430. // If the session is too long, emit a dummy value rather than abort the
  431. // connection.
  432. static const size_t kMaxTicketOverhead =
  433. 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
  434. if (session_len > 0xffff - kMaxTicketOverhead) {
  435. static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
  436. return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder,
  437. strlen(kTicketPlaceholder));
  438. }
  439. // Initialize HMAC and cipher contexts. If callback present it does all the
  440. // work otherwise use generated values from parent ctx.
  441. SSL_CTX *tctx = ssl->session_ctx;
  442. uint8_t iv[EVP_MAX_IV_LENGTH];
  443. uint8_t key_name[16];
  444. if (tctx->tlsext_ticket_key_cb != NULL) {
  445. if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, ctx.get(), hctx.get(),
  446. 1 /* encrypt */) < 0) {
  447. return 0;
  448. }
  449. } else {
  450. // Rotate ticket key if necessary.
  451. if (!ssl_ctx_rotate_ticket_encryption_key(tctx)) {
  452. return 0;
  453. }
  454. MutexReadLock lock(&tctx->lock);
  455. if (!RAND_bytes(iv, 16) ||
  456. !EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL,
  457. tctx->tlsext_ticket_key_current->aes_key, iv) ||
  458. !HMAC_Init_ex(hctx.get(), tctx->tlsext_ticket_key_current->hmac_key, 16,
  459. tlsext_tick_md(), NULL)) {
  460. return 0;
  461. }
  462. OPENSSL_memcpy(key_name, tctx->tlsext_ticket_key_current->name, 16);
  463. }
  464. uint8_t *ptr;
  465. if (!CBB_add_bytes(out, key_name, 16) ||
  466. !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) ||
  467. !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) {
  468. return 0;
  469. }
  470. size_t total = 0;
  471. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  472. OPENSSL_memcpy(ptr, session_buf, session_len);
  473. total = session_len;
  474. #else
  475. int len;
  476. if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) {
  477. return 0;
  478. }
  479. total += len;
  480. if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) {
  481. return 0;
  482. }
  483. total += len;
  484. #endif
  485. if (!CBB_did_write(out, total)) {
  486. return 0;
  487. }
  488. unsigned hlen;
  489. if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) ||
  490. !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) ||
  491. !HMAC_Final(hctx.get(), ptr, &hlen) ||
  492. !CBB_did_write(out, hlen)) {
  493. return 0;
  494. }
  495. return 1;
  496. }
  497. static int ssl_encrypt_ticket_with_method(SSL *ssl, CBB *out,
  498. const uint8_t *session_buf,
  499. size_t session_len) {
  500. const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method;
  501. const size_t max_overhead = method->max_overhead(ssl);
  502. const size_t max_out = session_len + max_overhead;
  503. if (max_out < max_overhead) {
  504. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  505. return 0;
  506. }
  507. uint8_t *ptr;
  508. if (!CBB_reserve(out, &ptr, max_out)) {
  509. return 0;
  510. }
  511. size_t out_len;
  512. if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, session_len)) {
  513. OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED);
  514. return 0;
  515. }
  516. if (!CBB_did_write(out, out_len)) {
  517. return 0;
  518. }
  519. return 1;
  520. }
  521. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session) {
  522. // Serialize the SSL_SESSION to be encoded into the ticket.
  523. uint8_t *session_buf = NULL;
  524. size_t session_len;
  525. if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) {
  526. return -1;
  527. }
  528. int ret = 0;
  529. if (ssl->session_ctx->ticket_aead_method) {
  530. ret = ssl_encrypt_ticket_with_method(ssl, out, session_buf, session_len);
  531. } else {
  532. ret =
  533. ssl_encrypt_ticket_with_cipher_ctx(ssl, out, session_buf, session_len);
  534. }
  535. OPENSSL_free(session_buf);
  536. return ret;
  537. }
  538. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session) {
  539. if (session == NULL) {
  540. return 0;
  541. }
  542. return session->sid_ctx_length == ssl->cert->sid_ctx_length &&
  543. OPENSSL_memcmp(session->sid_ctx, ssl->cert->sid_ctx,
  544. ssl->cert->sid_ctx_length) == 0;
  545. }
  546. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) {
  547. if (session == NULL) {
  548. return 0;
  549. }
  550. struct OPENSSL_timeval now;
  551. ssl_get_current_time(ssl, &now);
  552. // Reject tickets from the future to avoid underflow.
  553. if (now.tv_sec < session->time) {
  554. return 0;
  555. }
  556. return session->timeout > now.tv_sec - session->time;
  557. }
  558. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  559. const SSL_SESSION *session) {
  560. const SSL *const ssl = hs->ssl;
  561. return ssl_session_is_context_valid(ssl, session) &&
  562. // The session must have been created by the same type of end point as
  563. // we're now using it with.
  564. ssl->server == session->is_server &&
  565. // The session must not be expired.
  566. ssl_session_is_time_valid(ssl, session) &&
  567. /* Only resume if the session's version matches the negotiated
  568. * version. */
  569. ssl->version == session->ssl_version &&
  570. // Only resume if the session's cipher matches the negotiated one.
  571. hs->new_cipher == session->cipher &&
  572. // If the session contains a client certificate (either the full
  573. // certificate or just the hash) then require that the form of the
  574. // certificate matches the current configuration.
  575. ((sk_CRYPTO_BUFFER_num(session->certs) == 0 &&
  576. !session->peer_sha256_valid) ||
  577. session->peer_sha256_valid ==
  578. ssl->retain_only_sha256_of_client_certs);
  579. }
  580. // ssl_lookup_session looks up |session_id| in the session cache and sets
  581. // |*out_session| to an |SSL_SESSION| object if found.
  582. static enum ssl_hs_wait_t ssl_lookup_session(
  583. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, const uint8_t *session_id,
  584. size_t session_id_len) {
  585. out_session->reset();
  586. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  587. return ssl_hs_ok;
  588. }
  589. UniquePtr<SSL_SESSION> session;
  590. // Try the internal cache, if it exists.
  591. if (!(ssl->session_ctx->session_cache_mode &
  592. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  593. SSL_SESSION data;
  594. data.ssl_version = ssl->version;
  595. data.session_id_length = session_id_len;
  596. OPENSSL_memcpy(data.session_id, session_id, session_id_len);
  597. MutexReadLock lock(&ssl->session_ctx->lock);
  598. session.reset(lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &data));
  599. if (session) {
  600. // |lh_SSL_SESSION_retrieve| returns a non-owning pointer.
  601. SSL_SESSION_up_ref(session.get());
  602. }
  603. // TODO(davidben): This should probably move it to the front of the list.
  604. }
  605. // Fall back to the external cache, if it exists.
  606. if (!session && (ssl->session_ctx->get_session_cb != nullptr ||
  607. ssl->session_ctx->get_session_cb_legacy != nullptr)) {
  608. int copy = 1;
  609. if (ssl->session_ctx->get_session_cb != nullptr) {
  610. session.reset(ssl->session_ctx->get_session_cb(ssl, session_id,
  611. session_id_len, &copy));
  612. } else {
  613. session.reset(ssl->session_ctx->get_session_cb_legacy(
  614. ssl, const_cast<uint8_t *>(session_id), session_id_len, &copy));
  615. }
  616. if (!session) {
  617. return ssl_hs_ok;
  618. }
  619. if (session.get() == SSL_magic_pending_session_ptr()) {
  620. session.release(); // This pointer is not actually owned.
  621. return ssl_hs_pending_session;
  622. }
  623. // Increment reference count now if the session callback asks us to do so
  624. // (note that if the session structures returned by the callback are shared
  625. // between threads, it must handle the reference count itself [i.e. copy ==
  626. // 0], or things won't be thread-safe).
  627. if (copy) {
  628. SSL_SESSION_up_ref(session.get());
  629. }
  630. // Add the externally cached session to the internal cache if necessary.
  631. if (!(ssl->session_ctx->session_cache_mode &
  632. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  633. SSL_CTX_add_session(ssl->session_ctx, session.get());
  634. }
  635. }
  636. if (session && !ssl_session_is_time_valid(ssl, session.get())) {
  637. // The session was from the cache, so remove it.
  638. SSL_CTX_remove_session(ssl->session_ctx, session.get());
  639. session.reset();
  640. }
  641. *out_session = std::move(session);
  642. return ssl_hs_ok;
  643. }
  644. enum ssl_hs_wait_t ssl_get_prev_session(SSL *ssl,
  645. UniquePtr<SSL_SESSION> *out_session,
  646. bool *out_tickets_supported,
  647. bool *out_renew_ticket,
  648. const SSL_CLIENT_HELLO *client_hello) {
  649. // This is used only by servers.
  650. assert(ssl->server);
  651. UniquePtr<SSL_SESSION> session;
  652. bool renew_ticket = false;
  653. // If tickets are disabled, always behave as if no tickets are present.
  654. const uint8_t *ticket = NULL;
  655. size_t ticket_len = 0;
  656. const bool tickets_supported =
  657. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  658. ssl->version > SSL3_VERSION &&
  659. SSL_early_callback_ctx_extension_get(
  660. client_hello, TLSEXT_TYPE_session_ticket, &ticket, &ticket_len);
  661. if (tickets_supported && ticket_len > 0) {
  662. switch (ssl_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
  663. client_hello->session_id,
  664. client_hello->session_id_len)) {
  665. case ssl_ticket_aead_success:
  666. break;
  667. case ssl_ticket_aead_ignore_ticket:
  668. assert(!session);
  669. break;
  670. case ssl_ticket_aead_error:
  671. return ssl_hs_error;
  672. case ssl_ticket_aead_retry:
  673. return ssl_hs_pending_ticket;
  674. }
  675. } else {
  676. // The client didn't send a ticket, so the session ID is a real ID.
  677. enum ssl_hs_wait_t lookup_ret = ssl_lookup_session(
  678. ssl, &session, client_hello->session_id, client_hello->session_id_len);
  679. if (lookup_ret != ssl_hs_ok) {
  680. return lookup_ret;
  681. }
  682. }
  683. *out_session = std::move(session);
  684. *out_tickets_supported = tickets_supported;
  685. *out_renew_ticket = renew_ticket;
  686. return ssl_hs_ok;
  687. }
  688. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
  689. int ret = 0;
  690. if (session != NULL && session->session_id_length != 0) {
  691. if (lock) {
  692. CRYPTO_MUTEX_lock_write(&ctx->lock);
  693. }
  694. SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
  695. session);
  696. if (found_session == session) {
  697. ret = 1;
  698. found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
  699. SSL_SESSION_list_remove(ctx, session);
  700. }
  701. if (lock) {
  702. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  703. }
  704. if (ret) {
  705. if (ctx->remove_session_cb != NULL) {
  706. ctx->remove_session_cb(ctx, found_session);
  707. }
  708. SSL_SESSION_free(found_session);
  709. }
  710. }
  711. return ret;
  712. }
  713. void ssl_set_session(SSL *ssl, SSL_SESSION *session) {
  714. if (ssl->session == session) {
  715. return;
  716. }
  717. SSL_SESSION_free(ssl->session);
  718. ssl->session = session;
  719. if (session != NULL) {
  720. SSL_SESSION_up_ref(session);
  721. }
  722. }
  723. // locked by SSL_CTX in the calling function
  724. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
  725. if (session->next == NULL || session->prev == NULL) {
  726. return;
  727. }
  728. if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  729. // last element in list
  730. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  731. // only one element in list
  732. ctx->session_cache_head = NULL;
  733. ctx->session_cache_tail = NULL;
  734. } else {
  735. ctx->session_cache_tail = session->prev;
  736. session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  737. }
  738. } else {
  739. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  740. // first element in list
  741. ctx->session_cache_head = session->next;
  742. session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  743. } else { // middle of list
  744. session->next->prev = session->prev;
  745. session->prev->next = session->next;
  746. }
  747. }
  748. session->prev = session->next = NULL;
  749. }
  750. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
  751. if (session->next != NULL && session->prev != NULL) {
  752. SSL_SESSION_list_remove(ctx, session);
  753. }
  754. if (ctx->session_cache_head == NULL) {
  755. ctx->session_cache_head = session;
  756. ctx->session_cache_tail = session;
  757. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  758. session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  759. } else {
  760. session->next = ctx->session_cache_head;
  761. session->next->prev = session;
  762. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  763. ctx->session_cache_head = session;
  764. }
  765. }
  766. } // namespace bssl
  767. using namespace bssl;
  768. SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) {
  769. return ssl_session_new(ctx->x509_method).release();
  770. }
  771. int SSL_SESSION_up_ref(SSL_SESSION *session) {
  772. CRYPTO_refcount_inc(&session->references);
  773. return 1;
  774. }
  775. void SSL_SESSION_free(SSL_SESSION *session) {
  776. if (session == NULL ||
  777. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  778. return;
  779. }
  780. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  781. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  782. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  783. sk_CRYPTO_BUFFER_pop_free(session->certs, CRYPTO_BUFFER_free);
  784. session->x509_method->session_clear(session);
  785. OPENSSL_free(session->tlsext_tick);
  786. CRYPTO_BUFFER_free(session->signed_cert_timestamp_list);
  787. CRYPTO_BUFFER_free(session->ocsp_response);
  788. OPENSSL_free(session->psk_identity);
  789. OPENSSL_free(session->early_alpn);
  790. OPENSSL_free(session);
  791. }
  792. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
  793. unsigned *out_len) {
  794. if (out_len != NULL) {
  795. *out_len = session->session_id_length;
  796. }
  797. return session->session_id;
  798. }
  799. uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) {
  800. return session->timeout;
  801. }
  802. uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) {
  803. if (session == NULL) {
  804. // NULL should crash, but silently accept it here for compatibility.
  805. return 0;
  806. }
  807. return session->time;
  808. }
  809. X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
  810. return session->x509_peer;
  811. }
  812. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out,
  813. size_t max_out) {
  814. // TODO(davidben): Fix master_key_length's type and remove these casts.
  815. if (max_out == 0) {
  816. return (size_t)session->master_key_length;
  817. }
  818. if (max_out > (size_t)session->master_key_length) {
  819. max_out = (size_t)session->master_key_length;
  820. }
  821. OPENSSL_memcpy(out, session->master_key, max_out);
  822. return max_out;
  823. }
  824. uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) {
  825. if (session == NULL) {
  826. return 0;
  827. }
  828. session->time = time;
  829. return time;
  830. }
  831. uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) {
  832. if (session == NULL) {
  833. return 0;
  834. }
  835. session->timeout = timeout;
  836. session->auth_timeout = timeout;
  837. return 1;
  838. }
  839. int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
  840. size_t sid_ctx_len) {
  841. if (sid_ctx_len > sizeof(session->sid_ctx)) {
  842. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  843. return 0;
  844. }
  845. static_assert(sizeof(session->sid_ctx) < 256, "sid_ctx_len does not fit");
  846. session->sid_ctx_length = (uint8_t)sid_ctx_len;
  847. OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
  848. return 1;
  849. }
  850. int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) {
  851. return ssl_session_protocol_version(session) >= TLS1_3_VERSION;
  852. }
  853. int SSL_SESSION_is_resumable(const SSL_SESSION *session) {
  854. return !session->not_resumable;
  855. }
  856. int SSL_SESSION_has_ticket(const SSL_SESSION *session) {
  857. return session->tlsext_ticklen > 0;
  858. }
  859. void SSL_SESSION_get0_ticket(const SSL_SESSION *session,
  860. const uint8_t **out_ticket, size_t *out_len) {
  861. if (out_ticket != nullptr) {
  862. *out_ticket = session->tlsext_tick;
  863. }
  864. *out_len = session->tlsext_ticklen;
  865. }
  866. uint32_t SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *session) {
  867. return session->tlsext_tick_lifetime_hint;
  868. }
  869. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  870. return (SSL_SESSION *)&g_pending_session_magic;
  871. }
  872. SSL_SESSION *SSL_get_session(const SSL *ssl) {
  873. // Once the handshake completes we return the established session. Otherwise
  874. // we return the intermediate session, either |session| (for resumption) or
  875. // |new_session| if doing a full handshake.
  876. if (!SSL_in_init(ssl)) {
  877. return ssl->s3->established_session.get();
  878. }
  879. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  880. if (hs->early_session) {
  881. return hs->early_session.get();
  882. }
  883. if (hs->new_session) {
  884. return hs->new_session.get();
  885. }
  886. return ssl->session;
  887. }
  888. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  889. SSL_SESSION *ret = SSL_get_session(ssl);
  890. if (ret != NULL) {
  891. SSL_SESSION_up_ref(ret);
  892. }
  893. return ret;
  894. }
  895. int SSL_SESSION_get_ex_new_index(long argl, void *argp,
  896. CRYPTO_EX_unused *unused,
  897. CRYPTO_EX_dup *dup_unused,
  898. CRYPTO_EX_free *free_func) {
  899. int index;
  900. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  901. free_func)) {
  902. return -1;
  903. }
  904. return index;
  905. }
  906. int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
  907. return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
  908. }
  909. void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
  910. return CRYPTO_get_ex_data(&session->ex_data, idx);
  911. }
  912. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
  913. // Although |session| is inserted into two structures (a doubly-linked list
  914. // and the hash table), |ctx| only takes one reference.
  915. SSL_SESSION_up_ref(session);
  916. UniquePtr<SSL_SESSION> owned_session(session);
  917. SSL_SESSION *old_session;
  918. MutexWriteLock lock(&ctx->lock);
  919. if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
  920. return 0;
  921. }
  922. // |ctx->sessions| took ownership of |session| and gave us back a reference to
  923. // |old_session|. (|old_session| may be the same as |session|, in which case
  924. // we traded identical references with |ctx->sessions|.)
  925. owned_session.release();
  926. owned_session.reset(old_session);
  927. if (old_session != NULL) {
  928. if (old_session == session) {
  929. // |session| was already in the cache. There are no linked list pointers
  930. // to update.
  931. return 0;
  932. }
  933. // There was a session ID collision. |old_session| was replaced with
  934. // |session| in the hash table, so |old_session| must be removed from the
  935. // linked list to match.
  936. SSL_SESSION_list_remove(ctx, old_session);
  937. }
  938. SSL_SESSION_list_add(ctx, session);
  939. // Enforce any cache size limits.
  940. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  941. while (lh_SSL_SESSION_num_items(ctx->sessions) >
  942. SSL_CTX_sess_get_cache_size(ctx)) {
  943. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  944. break;
  945. }
  946. }
  947. }
  948. return 1;
  949. }
  950. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
  951. return remove_session_lock(ctx, session, 1);
  952. }
  953. int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
  954. // SSL_set_session may only be called before the handshake has started.
  955. if (ssl->s3->initial_handshake_complete ||
  956. ssl->s3->hs == NULL ||
  957. ssl->s3->hs->state != 0) {
  958. abort();
  959. }
  960. ssl_set_session(ssl, session);
  961. return 1;
  962. }
  963. uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) {
  964. if (ctx == NULL) {
  965. return 0;
  966. }
  967. // Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|.
  968. if (timeout == 0) {
  969. timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  970. }
  971. uint32_t old_timeout = ctx->session_timeout;
  972. ctx->session_timeout = timeout;
  973. return old_timeout;
  974. }
  975. uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) {
  976. if (ctx == NULL) {
  977. return 0;
  978. }
  979. return ctx->session_timeout;
  980. }
  981. void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) {
  982. ctx->session_psk_dhe_timeout = timeout;
  983. }
  984. typedef struct timeout_param_st {
  985. SSL_CTX *ctx;
  986. uint64_t time;
  987. LHASH_OF(SSL_SESSION) *cache;
  988. } TIMEOUT_PARAM;
  989. static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
  990. TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param);
  991. if (param->time == 0 ||
  992. session->time + session->timeout < session->time ||
  993. param->time > (session->time + session->timeout)) {
  994. // The reason we don't call SSL_CTX_remove_session() is to
  995. // save on locking overhead
  996. (void) lh_SSL_SESSION_delete(param->cache, session);
  997. SSL_SESSION_list_remove(param->ctx, session);
  998. if (param->ctx->remove_session_cb != NULL) {
  999. param->ctx->remove_session_cb(param->ctx, session);
  1000. }
  1001. SSL_SESSION_free(session);
  1002. }
  1003. }
  1004. void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) {
  1005. TIMEOUT_PARAM tp;
  1006. tp.ctx = ctx;
  1007. tp.cache = ctx->sessions;
  1008. if (tp.cache == NULL) {
  1009. return;
  1010. }
  1011. tp.time = time;
  1012. MutexWriteLock lock(&ctx->lock);
  1013. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  1014. }
  1015. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  1016. int (*cb)(SSL *ssl, SSL_SESSION *session)) {
  1017. ctx->new_session_cb = cb;
  1018. }
  1019. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
  1020. return ctx->new_session_cb;
  1021. }
  1022. void SSL_CTX_sess_set_remove_cb(
  1023. SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
  1024. ctx->remove_session_cb = cb;
  1025. }
  1026. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  1027. SSL_SESSION *session) {
  1028. return ctx->remove_session_cb;
  1029. }
  1030. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  1031. SSL_SESSION *(*cb)(SSL *ssl, const uint8_t *id,
  1032. int id_len, int *out_copy)) {
  1033. ctx->get_session_cb = cb;
  1034. }
  1035. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  1036. SSL_SESSION *(*cb)(SSL *ssl, uint8_t *id,
  1037. int id_len, int *out_copy)) {
  1038. ctx->get_session_cb_legacy = cb;
  1039. }
  1040. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
  1041. const uint8_t *id,
  1042. int id_len,
  1043. int *out_copy) {
  1044. return ctx->get_session_cb;
  1045. }
  1046. void SSL_CTX_set_info_callback(
  1047. SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
  1048. ctx->info_callback = cb;
  1049. }
  1050. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  1051. int value) {
  1052. return ctx->info_callback;
  1053. }
  1054. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  1055. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  1056. ctx->channel_id_cb = cb;
  1057. }
  1058. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  1059. return ctx->channel_id_cb;
  1060. }