Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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