Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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