ssl_session.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 <openssl/err.h>
  140. #include <openssl/lhash.h>
  141. #include <openssl/mem.h>
  142. #include <openssl/rand.h>
  143. #include "internal.h"
  144. #include "../crypto/internal.h"
  145. /* The address of this is a magic value, a pointer to which is returned by
  146. * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  147. * that it needs to asynchronously fetch session information. */
  148. static const char g_pending_session_magic = 0;
  149. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  150. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  151. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
  152. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
  153. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
  154. SSL_SESSION *ssl_session_new(const SSL_X509_METHOD *x509_method) {
  155. SSL_SESSION *session = OPENSSL_malloc(sizeof(SSL_SESSION));
  156. if (session == NULL) {
  157. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  158. return 0;
  159. }
  160. OPENSSL_memset(session, 0, sizeof(SSL_SESSION));
  161. session->x509_method = x509_method;
  162. session->verify_result = X509_V_ERR_INVALID_CALL;
  163. session->references = 1;
  164. session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  165. session->auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  166. session->time = (long)time(NULL);
  167. CRYPTO_new_ex_data(&session->ex_data);
  168. return session;
  169. }
  170. SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) {
  171. return ssl_session_new(ctx->x509_method);
  172. }
  173. SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) {
  174. SSL_SESSION *new_session = ssl_session_new(session->x509_method);
  175. if (new_session == NULL) {
  176. goto err;
  177. }
  178. new_session->is_server = session->is_server;
  179. new_session->ssl_version = session->ssl_version;
  180. new_session->sid_ctx_length = session->sid_ctx_length;
  181. OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length);
  182. /* Copy the key material. */
  183. new_session->master_key_length = session->master_key_length;
  184. OPENSSL_memcpy(new_session->master_key, session->master_key,
  185. session->master_key_length);
  186. new_session->cipher = session->cipher;
  187. /* Copy authentication state. */
  188. if (session->psk_identity != NULL) {
  189. new_session->psk_identity = BUF_strdup(session->psk_identity);
  190. if (new_session->psk_identity == NULL) {
  191. goto err;
  192. }
  193. }
  194. if (session->certs != NULL) {
  195. new_session->certs = sk_CRYPTO_BUFFER_new_null();
  196. if (new_session->certs == NULL) {
  197. goto err;
  198. }
  199. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(session->certs); i++) {
  200. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(session->certs, i);
  201. if (!sk_CRYPTO_BUFFER_push(new_session->certs, buffer)) {
  202. goto err;
  203. }
  204. CRYPTO_BUFFER_up_ref(buffer);
  205. }
  206. }
  207. if (!session->x509_method->session_dup(new_session, session)) {
  208. goto err;
  209. }
  210. new_session->verify_result = session->verify_result;
  211. new_session->ocsp_response_length = session->ocsp_response_length;
  212. if (session->ocsp_response != NULL) {
  213. new_session->ocsp_response = BUF_memdup(session->ocsp_response,
  214. session->ocsp_response_length);
  215. if (new_session->ocsp_response == NULL) {
  216. goto err;
  217. }
  218. }
  219. new_session->tlsext_signed_cert_timestamp_list_length =
  220. session->tlsext_signed_cert_timestamp_list_length;
  221. if (session->tlsext_signed_cert_timestamp_list != NULL) {
  222. new_session->tlsext_signed_cert_timestamp_list =
  223. BUF_memdup(session->tlsext_signed_cert_timestamp_list,
  224. session->tlsext_signed_cert_timestamp_list_length);
  225. if (new_session->tlsext_signed_cert_timestamp_list == NULL) {
  226. goto err;
  227. }
  228. }
  229. OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256,
  230. SHA256_DIGEST_LENGTH);
  231. new_session->peer_sha256_valid = session->peer_sha256_valid;
  232. if (session->tlsext_hostname != NULL) {
  233. new_session->tlsext_hostname = BUF_strdup(session->tlsext_hostname);
  234. if (new_session->tlsext_hostname == NULL) {
  235. goto err;
  236. }
  237. }
  238. new_session->peer_signature_algorithm = session->peer_signature_algorithm;
  239. new_session->timeout = session->timeout;
  240. new_session->auth_timeout = session->auth_timeout;
  241. new_session->time = session->time;
  242. /* Copy non-authentication connection properties. */
  243. if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) {
  244. new_session->session_id_length = session->session_id_length;
  245. OPENSSL_memcpy(new_session->session_id, session->session_id,
  246. session->session_id_length);
  247. new_session->group_id = session->group_id;
  248. OPENSSL_memcpy(new_session->original_handshake_hash,
  249. session->original_handshake_hash,
  250. session->original_handshake_hash_len);
  251. new_session->original_handshake_hash_len =
  252. session->original_handshake_hash_len;
  253. new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint;
  254. new_session->ticket_age_add = session->ticket_age_add;
  255. new_session->ticket_max_early_data = session->ticket_max_early_data;
  256. new_session->extended_master_secret = session->extended_master_secret;
  257. if (session->early_alpn != NULL) {
  258. new_session->early_alpn =
  259. BUF_memdup(session->early_alpn, session->early_alpn_len);
  260. if (new_session->early_alpn == NULL) {
  261. goto err;
  262. }
  263. }
  264. new_session->early_alpn_len = session->early_alpn_len;
  265. }
  266. /* Copy the ticket. */
  267. if (dup_flags & SSL_SESSION_INCLUDE_TICKET) {
  268. if (session->tlsext_tick != NULL) {
  269. new_session->tlsext_tick =
  270. BUF_memdup(session->tlsext_tick, session->tlsext_ticklen);
  271. if (new_session->tlsext_tick == NULL) {
  272. goto err;
  273. }
  274. }
  275. new_session->tlsext_ticklen = session->tlsext_ticklen;
  276. }
  277. /* The new_session does not get a copy of the ex_data. */
  278. new_session->not_resumable = 1;
  279. return new_session;
  280. err:
  281. SSL_SESSION_free(new_session);
  282. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  283. return 0;
  284. }
  285. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) {
  286. struct timeval now;
  287. ssl_get_current_time(ssl, &now);
  288. /* To avoid overflows and underflows, if we've gone back in time or any value
  289. * is negative, update the time, but mark the session expired. */
  290. if (session->time > now.tv_sec ||
  291. session->time < 0 ||
  292. now.tv_sec < 0) {
  293. session->time = now.tv_sec;
  294. session->timeout = 0;
  295. session->auth_timeout = 0;
  296. return;
  297. }
  298. /* Adjust the session time and timeouts. If the session has already expired,
  299. * clamp the timeouts at zero. */
  300. long delta = now.tv_sec - session->time;
  301. session->time = now.tv_sec;
  302. if (session->timeout < delta) {
  303. session->timeout = 0;
  304. } else {
  305. session->timeout -= delta;
  306. }
  307. if (session->auth_timeout < delta) {
  308. session->auth_timeout = 0;
  309. } else {
  310. session->auth_timeout -= delta;
  311. }
  312. }
  313. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, long timeout) {
  314. /* Rebase the timestamp relative to the current time so |timeout| is measured
  315. * correctly. */
  316. ssl_session_rebase_time(ssl, session);
  317. if (session->timeout > timeout) {
  318. return;
  319. }
  320. session->timeout = timeout;
  321. if (session->timeout > session->auth_timeout) {
  322. session->timeout = session->auth_timeout;
  323. }
  324. }
  325. int SSL_SESSION_up_ref(SSL_SESSION *session) {
  326. CRYPTO_refcount_inc(&session->references);
  327. return 1;
  328. }
  329. void SSL_SESSION_free(SSL_SESSION *session) {
  330. if (session == NULL ||
  331. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  332. return;
  333. }
  334. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  335. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  336. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  337. sk_CRYPTO_BUFFER_pop_free(session->certs, CRYPTO_BUFFER_free);
  338. session->x509_method->session_clear(session);
  339. OPENSSL_free(session->tlsext_hostname);
  340. OPENSSL_free(session->tlsext_tick);
  341. OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
  342. OPENSSL_free(session->ocsp_response);
  343. OPENSSL_free(session->psk_identity);
  344. OPENSSL_free(session->early_alpn);
  345. OPENSSL_cleanse(session, sizeof(*session));
  346. OPENSSL_free(session);
  347. }
  348. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
  349. unsigned *out_len) {
  350. if (out_len != NULL) {
  351. *out_len = session->session_id_length;
  352. }
  353. return session->session_id;
  354. }
  355. long SSL_SESSION_get_timeout(const SSL_SESSION *session) {
  356. return session->timeout;
  357. }
  358. long SSL_SESSION_get_time(const SSL_SESSION *session) {
  359. if (session == NULL) {
  360. /* NULL should crash, but silently accept it here for compatibility. */
  361. return 0;
  362. }
  363. return session->time;
  364. }
  365. X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
  366. return session->x509_peer;
  367. }
  368. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out,
  369. size_t max_out) {
  370. /* TODO(davidben): Fix master_key_length's type and remove these casts. */
  371. if (max_out == 0) {
  372. return (size_t)session->master_key_length;
  373. }
  374. if (max_out > (size_t)session->master_key_length) {
  375. max_out = (size_t)session->master_key_length;
  376. }
  377. OPENSSL_memcpy(out, session->master_key, max_out);
  378. return max_out;
  379. }
  380. long SSL_SESSION_set_time(SSL_SESSION *session, long time) {
  381. if (session == NULL) {
  382. return 0;
  383. }
  384. session->time = time;
  385. return time;
  386. }
  387. long SSL_SESSION_set_timeout(SSL_SESSION *session, long timeout) {
  388. if (session == NULL) {
  389. return 0;
  390. }
  391. session->timeout = timeout;
  392. session->auth_timeout = timeout;
  393. return 1;
  394. }
  395. int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
  396. size_t sid_ctx_len) {
  397. if (sid_ctx_len > sizeof(session->sid_ctx)) {
  398. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  399. return 0;
  400. }
  401. assert(sizeof(session->sid_ctx) < 256);
  402. session->sid_ctx_length = (uint8_t)sid_ctx_len;
  403. OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
  404. return 1;
  405. }
  406. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  407. return (SSL_SESSION *)&g_pending_session_magic;
  408. }
  409. SSL_SESSION *SSL_get_session(const SSL *ssl) {
  410. /* Once the handshake completes we return the established session. Otherwise
  411. * we return the intermediate session, either |session| (for resumption) or
  412. * |new_session| if doing a full handshake. */
  413. if (!SSL_in_init(ssl)) {
  414. return ssl->s3->established_session;
  415. }
  416. if (ssl->s3->hs->new_session != NULL) {
  417. return ssl->s3->hs->new_session;
  418. }
  419. return ssl->session;
  420. }
  421. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  422. SSL_SESSION *ret = SSL_get_session(ssl);
  423. if (ret != NULL) {
  424. SSL_SESSION_up_ref(ret);
  425. }
  426. return ret;
  427. }
  428. int SSL_SESSION_get_ex_new_index(long argl, void *argp,
  429. CRYPTO_EX_unused *unused,
  430. CRYPTO_EX_dup *dup_func,
  431. CRYPTO_EX_free *free_func) {
  432. int index;
  433. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  434. free_func)) {
  435. return -1;
  436. }
  437. return index;
  438. }
  439. int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
  440. return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
  441. }
  442. void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
  443. return CRYPTO_get_ex_data(&session->ex_data, idx);
  444. }
  445. const EVP_MD *SSL_SESSION_get_digest(const SSL_SESSION *session,
  446. const SSL *ssl) {
  447. uint16_t version;
  448. if (!ssl->method->version_from_wire(&version, session->ssl_version)) {
  449. return NULL;
  450. }
  451. return ssl_get_handshake_digest(session->cipher->algorithm_prf, version);
  452. }
  453. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
  454. SSL *const ssl = hs->ssl;
  455. if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
  456. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  457. return 0;
  458. }
  459. SSL_SESSION *session = ssl_session_new(ssl->ctx->x509_method);
  460. if (session == NULL) {
  461. return 0;
  462. }
  463. session->is_server = is_server;
  464. session->ssl_version = ssl->version;
  465. /* Fill in the time from the |SSL_CTX|'s clock. */
  466. struct timeval now;
  467. ssl_get_current_time(ssl, &now);
  468. session->time = now.tv_sec;
  469. uint16_t version = ssl3_protocol_version(ssl);
  470. if (version >= TLS1_3_VERSION) {
  471. /* TLS 1.3 uses tickets as authenticators, so we are willing to use them for
  472. * longer. */
  473. session->timeout = ssl->initial_ctx->session_psk_dhe_timeout;
  474. session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT;
  475. } else {
  476. /* TLS 1.2 resumption does not incorporate new key material, so we use a
  477. * much shorter timeout. */
  478. session->timeout = ssl->initial_ctx->session_timeout;
  479. session->auth_timeout = ssl->initial_ctx->session_timeout;
  480. }
  481. if (is_server) {
  482. if (hs->ticket_expected || version >= TLS1_3_VERSION) {
  483. /* Don't set session IDs for sessions resumed with tickets. This will keep
  484. * them out of the session cache. */
  485. session->session_id_length = 0;
  486. } else {
  487. session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  488. if (!RAND_bytes(session->session_id, session->session_id_length)) {
  489. goto err;
  490. }
  491. }
  492. } else {
  493. session->session_id_length = 0;
  494. }
  495. if (ssl->cert->sid_ctx_length > sizeof(session->sid_ctx)) {
  496. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  497. goto err;
  498. }
  499. OPENSSL_memcpy(session->sid_ctx, ssl->cert->sid_ctx,
  500. ssl->cert->sid_ctx_length);
  501. session->sid_ctx_length = ssl->cert->sid_ctx_length;
  502. /* The session is marked not resumable until it is completely filled in. */
  503. session->not_resumable = 1;
  504. session->verify_result = X509_V_ERR_INVALID_CALL;
  505. SSL_SESSION_free(hs->new_session);
  506. hs->new_session = session;
  507. ssl_set_session(ssl, NULL);
  508. return 1;
  509. err:
  510. SSL_SESSION_free(session);
  511. return 0;
  512. }
  513. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session) {
  514. int ret = 0;
  515. /* Serialize the SSL_SESSION to be encoded into the ticket. */
  516. uint8_t *session_buf = NULL;
  517. size_t session_len;
  518. if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) {
  519. return -1;
  520. }
  521. EVP_CIPHER_CTX ctx;
  522. EVP_CIPHER_CTX_init(&ctx);
  523. HMAC_CTX hctx;
  524. HMAC_CTX_init(&hctx);
  525. /* If the session is too long, emit a dummy value rather than abort the
  526. * connection. */
  527. static const size_t kMaxTicketOverhead =
  528. 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
  529. if (session_len > 0xffff - kMaxTicketOverhead) {
  530. static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
  531. if (CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder,
  532. strlen(kTicketPlaceholder))) {
  533. ret = 1;
  534. }
  535. goto err;
  536. }
  537. /* Initialize HMAC and cipher contexts. If callback present it does all the
  538. * work otherwise use generated values from parent ctx. */
  539. SSL_CTX *tctx = ssl->initial_ctx;
  540. uint8_t iv[EVP_MAX_IV_LENGTH];
  541. uint8_t key_name[16];
  542. if (tctx->tlsext_ticket_key_cb != NULL) {
  543. if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
  544. 1 /* encrypt */) < 0) {
  545. goto err;
  546. }
  547. } else {
  548. if (!RAND_bytes(iv, 16) ||
  549. !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  550. tctx->tlsext_tick_aes_key, iv) ||
  551. !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
  552. NULL)) {
  553. goto err;
  554. }
  555. OPENSSL_memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  556. }
  557. uint8_t *ptr;
  558. if (!CBB_add_bytes(out, key_name, 16) ||
  559. !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(&ctx)) ||
  560. !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) {
  561. goto err;
  562. }
  563. size_t total = 0;
  564. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  565. OPENSSL_memcpy(ptr, session_buf, session_len);
  566. total = session_len;
  567. #else
  568. int len;
  569. if (!EVP_EncryptUpdate(&ctx, ptr + total, &len, session_buf, session_len)) {
  570. goto err;
  571. }
  572. total += len;
  573. if (!EVP_EncryptFinal_ex(&ctx, ptr + total, &len)) {
  574. goto err;
  575. }
  576. total += len;
  577. #endif
  578. if (!CBB_did_write(out, total)) {
  579. goto err;
  580. }
  581. unsigned hlen;
  582. if (!HMAC_Update(&hctx, CBB_data(out), CBB_len(out)) ||
  583. !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) ||
  584. !HMAC_Final(&hctx, ptr, &hlen) ||
  585. !CBB_did_write(out, hlen)) {
  586. goto err;
  587. }
  588. ret = 1;
  589. err:
  590. OPENSSL_free(session_buf);
  591. EVP_CIPHER_CTX_cleanup(&ctx);
  592. HMAC_CTX_cleanup(&hctx);
  593. return ret;
  594. }
  595. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session) {
  596. if (session == NULL) {
  597. return 0;
  598. }
  599. return session->sid_ctx_length == ssl->cert->sid_ctx_length &&
  600. OPENSSL_memcmp(session->sid_ctx, ssl->cert->sid_ctx,
  601. ssl->cert->sid_ctx_length) == 0;
  602. }
  603. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) {
  604. if (session == NULL) {
  605. return 0;
  606. }
  607. struct timeval now;
  608. ssl_get_current_time(ssl, &now);
  609. /* Reject tickets from the future to avoid underflow. */
  610. if ((long)now.tv_sec < session->time) {
  611. return 0;
  612. }
  613. return session->timeout > (long)now.tv_sec - session->time;
  614. }
  615. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  616. const SSL_SESSION *session) {
  617. const SSL *const ssl = hs->ssl;
  618. return ssl_session_is_context_valid(ssl, session) &&
  619. /* The session must have been created by the same type of end point as
  620. * we're now using it with. */
  621. ssl->server == session->is_server &&
  622. /* The session must not be expired. */
  623. ssl_session_is_time_valid(ssl, session) &&
  624. /* Only resume if the session's version matches the negotiated
  625. * version. */
  626. ssl->version == session->ssl_version &&
  627. /* Only resume if the session's cipher matches the negotiated one. */
  628. hs->new_cipher == session->cipher &&
  629. /* If the session contains a client certificate (either the full
  630. * certificate or just the hash) then require that the form of the
  631. * certificate matches the current configuration. */
  632. ((sk_CRYPTO_BUFFER_num(session->certs) == 0 &&
  633. !session->peer_sha256_valid) ||
  634. session->peer_sha256_valid ==
  635. ssl->retain_only_sha256_of_client_certs);
  636. }
  637. /* ssl_lookup_session looks up |session_id| in the session cache and sets
  638. * |*out_session| to an |SSL_SESSION| object if found. The caller takes
  639. * ownership of the result. */
  640. static enum ssl_session_result_t ssl_lookup_session(
  641. SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id,
  642. size_t session_id_len) {
  643. *out_session = NULL;
  644. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  645. return ssl_session_success;
  646. }
  647. SSL_SESSION *session = NULL;
  648. /* Try the internal cache, if it exists. */
  649. if (!(ssl->initial_ctx->session_cache_mode &
  650. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  651. SSL_SESSION data;
  652. data.ssl_version = ssl->version;
  653. data.session_id_length = session_id_len;
  654. OPENSSL_memcpy(data.session_id, session_id, session_id_len);
  655. CRYPTO_MUTEX_lock_read(&ssl->initial_ctx->lock);
  656. session = lh_SSL_SESSION_retrieve(ssl->initial_ctx->sessions, &data);
  657. if (session != NULL) {
  658. SSL_SESSION_up_ref(session);
  659. }
  660. /* TODO(davidben): This should probably move it to the front of the list. */
  661. CRYPTO_MUTEX_unlock_read(&ssl->initial_ctx->lock);
  662. }
  663. /* Fall back to the external cache, if it exists. */
  664. if (session == NULL &&
  665. ssl->initial_ctx->get_session_cb != NULL) {
  666. int copy = 1;
  667. session = ssl->initial_ctx->get_session_cb(ssl, (uint8_t *)session_id,
  668. session_id_len, &copy);
  669. if (session == NULL) {
  670. return ssl_session_success;
  671. }
  672. if (session == SSL_magic_pending_session_ptr()) {
  673. return ssl_session_retry;
  674. }
  675. /* Increment reference count now if the session callback asks us to do so
  676. * (note that if the session structures returned by the callback are shared
  677. * between threads, it must handle the reference count itself [i.e. copy ==
  678. * 0], or things won't be thread-safe). */
  679. if (copy) {
  680. SSL_SESSION_up_ref(session);
  681. }
  682. /* Add the externally cached session to the internal cache if necessary. */
  683. if (!(ssl->initial_ctx->session_cache_mode &
  684. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  685. SSL_CTX_add_session(ssl->initial_ctx, session);
  686. }
  687. }
  688. if (session != NULL &&
  689. !ssl_session_is_time_valid(ssl, session)) {
  690. /* The session was from the cache, so remove it. */
  691. SSL_CTX_remove_session(ssl->initial_ctx, session);
  692. SSL_SESSION_free(session);
  693. session = NULL;
  694. }
  695. *out_session = session;
  696. return ssl_session_success;
  697. }
  698. enum ssl_session_result_t ssl_get_prev_session(
  699. SSL *ssl, SSL_SESSION **out_session, int *out_tickets_supported,
  700. int *out_renew_ticket, const SSL_CLIENT_HELLO *client_hello) {
  701. /* This is used only by servers. */
  702. assert(ssl->server);
  703. SSL_SESSION *session = NULL;
  704. int renew_ticket = 0;
  705. /* If tickets are disabled, always behave as if no tickets are present. */
  706. const uint8_t *ticket = NULL;
  707. size_t ticket_len = 0;
  708. const int tickets_supported =
  709. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  710. ssl->version > SSL3_VERSION &&
  711. SSL_early_callback_ctx_extension_get(
  712. client_hello, TLSEXT_TYPE_session_ticket, &ticket, &ticket_len);
  713. if (tickets_supported && ticket_len > 0) {
  714. if (!tls_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
  715. client_hello->session_id,
  716. client_hello->session_id_len)) {
  717. return ssl_session_error;
  718. }
  719. } else {
  720. /* The client didn't send a ticket, so the session ID is a real ID. */
  721. enum ssl_session_result_t lookup_ret = ssl_lookup_session(
  722. ssl, &session, client_hello->session_id, client_hello->session_id_len);
  723. if (lookup_ret != ssl_session_success) {
  724. return lookup_ret;
  725. }
  726. }
  727. *out_session = session;
  728. *out_tickets_supported = tickets_supported;
  729. *out_renew_ticket = renew_ticket;
  730. return ssl_session_success;
  731. }
  732. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
  733. /* Although |session| is inserted into two structures (a doubly-linked list
  734. * and the hash table), |ctx| only takes one reference. */
  735. SSL_SESSION_up_ref(session);
  736. SSL_SESSION *old_session;
  737. CRYPTO_MUTEX_lock_write(&ctx->lock);
  738. if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
  739. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  740. SSL_SESSION_free(session);
  741. return 0;
  742. }
  743. if (old_session != NULL) {
  744. if (old_session == session) {
  745. /* |session| was already in the cache. */
  746. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  747. SSL_SESSION_free(old_session);
  748. return 0;
  749. }
  750. /* There was a session ID collision. |old_session| must be removed from
  751. * the linked list and released. */
  752. SSL_SESSION_list_remove(ctx, old_session);
  753. SSL_SESSION_free(old_session);
  754. }
  755. SSL_SESSION_list_add(ctx, session);
  756. /* Enforce any cache size limits. */
  757. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  758. while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
  759. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  760. break;
  761. }
  762. }
  763. }
  764. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  765. return 1;
  766. }
  767. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
  768. return remove_session_lock(ctx, session, 1);
  769. }
  770. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
  771. int ret = 0;
  772. if (session != NULL && session->session_id_length != 0) {
  773. if (lock) {
  774. CRYPTO_MUTEX_lock_write(&ctx->lock);
  775. }
  776. SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
  777. session);
  778. if (found_session == session) {
  779. ret = 1;
  780. found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
  781. SSL_SESSION_list_remove(ctx, session);
  782. }
  783. if (lock) {
  784. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  785. }
  786. if (ret) {
  787. found_session->not_resumable = 1;
  788. if (ctx->remove_session_cb != NULL) {
  789. ctx->remove_session_cb(ctx, found_session);
  790. }
  791. SSL_SESSION_free(found_session);
  792. }
  793. }
  794. return ret;
  795. }
  796. int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
  797. /* SSL_set_session may only be called before the handshake has started. */
  798. if (ssl->s3->initial_handshake_complete ||
  799. ssl->s3->hs == NULL ||
  800. ssl->s3->hs->state != SSL_ST_INIT) {
  801. abort();
  802. }
  803. ssl_set_session(ssl, session);
  804. return 1;
  805. }
  806. void ssl_set_session(SSL *ssl, SSL_SESSION *session) {
  807. if (ssl->session == session) {
  808. return;
  809. }
  810. SSL_SESSION_free(ssl->session);
  811. ssl->session = session;
  812. if (session != NULL) {
  813. SSL_SESSION_up_ref(session);
  814. }
  815. }
  816. long SSL_CTX_set_timeout(SSL_CTX *ctx, long timeout) {
  817. if (ctx == NULL) {
  818. return 0;
  819. }
  820. /* Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|. */
  821. if (timeout == 0) {
  822. timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  823. }
  824. long old_timeout = ctx->session_timeout;
  825. ctx->session_timeout = timeout;
  826. return old_timeout;
  827. }
  828. long SSL_CTX_get_timeout(const SSL_CTX *ctx) {
  829. if (ctx == NULL) {
  830. return 0;
  831. }
  832. return ctx->session_timeout;
  833. }
  834. void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, long timeout) {
  835. ctx->session_psk_dhe_timeout = timeout;
  836. }
  837. typedef struct timeout_param_st {
  838. SSL_CTX *ctx;
  839. long time;
  840. LHASH_OF(SSL_SESSION) *cache;
  841. } TIMEOUT_PARAM;
  842. static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
  843. TIMEOUT_PARAM *param = void_param;
  844. if (param->time == 0 ||
  845. param->time > (session->time + session->timeout)) {
  846. /* timeout */
  847. /* The reason we don't call SSL_CTX_remove_session() is to
  848. * save on locking overhead */
  849. (void) lh_SSL_SESSION_delete(param->cache, session);
  850. SSL_SESSION_list_remove(param->ctx, session);
  851. session->not_resumable = 1;
  852. if (param->ctx->remove_session_cb != NULL) {
  853. param->ctx->remove_session_cb(param->ctx, session);
  854. }
  855. SSL_SESSION_free(session);
  856. }
  857. }
  858. void SSL_CTX_flush_sessions(SSL_CTX *ctx, long time) {
  859. TIMEOUT_PARAM tp;
  860. tp.ctx = ctx;
  861. tp.cache = ctx->sessions;
  862. if (tp.cache == NULL) {
  863. return;
  864. }
  865. tp.time = time;
  866. CRYPTO_MUTEX_lock_write(&ctx->lock);
  867. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  868. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  869. }
  870. /* locked by SSL_CTX in the calling function */
  871. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
  872. if (session->next == NULL || session->prev == NULL) {
  873. return;
  874. }
  875. if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  876. /* last element in list */
  877. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  878. /* only one element in list */
  879. ctx->session_cache_head = NULL;
  880. ctx->session_cache_tail = NULL;
  881. } else {
  882. ctx->session_cache_tail = session->prev;
  883. session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  884. }
  885. } else {
  886. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  887. /* first element in list */
  888. ctx->session_cache_head = session->next;
  889. session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  890. } else { /* middle of list */
  891. session->next->prev = session->prev;
  892. session->prev->next = session->next;
  893. }
  894. }
  895. session->prev = session->next = NULL;
  896. }
  897. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
  898. if (session->next != NULL && session->prev != NULL) {
  899. SSL_SESSION_list_remove(ctx, session);
  900. }
  901. if (ctx->session_cache_head == NULL) {
  902. ctx->session_cache_head = session;
  903. ctx->session_cache_tail = session;
  904. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  905. session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  906. } else {
  907. session->next = ctx->session_cache_head;
  908. session->next->prev = session;
  909. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  910. ctx->session_cache_head = session;
  911. }
  912. }
  913. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  914. int (*cb)(SSL *ssl, SSL_SESSION *session)) {
  915. ctx->new_session_cb = cb;
  916. }
  917. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
  918. return ctx->new_session_cb;
  919. }
  920. void SSL_CTX_sess_set_remove_cb(
  921. SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
  922. ctx->remove_session_cb = cb;
  923. }
  924. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  925. SSL_SESSION *session) {
  926. return ctx->remove_session_cb;
  927. }
  928. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  929. SSL_SESSION *(*cb)(SSL *ssl,
  930. uint8_t *id, int id_len,
  931. int *out_copy)) {
  932. ctx->get_session_cb = cb;
  933. }
  934. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(
  935. SSL *ssl, uint8_t *id, int id_len, int *out_copy) {
  936. return ctx->get_session_cb;
  937. }
  938. void SSL_CTX_set_info_callback(
  939. SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
  940. ctx->info_callback = cb;
  941. }
  942. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  943. int value) {
  944. return ctx->info_callback;
  945. }
  946. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  947. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  948. ctx->channel_id_cb = cb;
  949. }
  950. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  951. return ctx->channel_id_cb;
  952. }