You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1148 lines
38 KiB

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