25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2728 lines
76 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-2007 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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <string.h>
  143. #include <openssl/bytestring.h>
  144. #include <openssl/crypto.h>
  145. #include <openssl/dh.h>
  146. #include <openssl/err.h>
  147. #include <openssl/lhash.h>
  148. #include <openssl/mem.h>
  149. #include <openssl/rand.h>
  150. #include <openssl/x509v3.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  154. * to avoid downstream churn. */
  155. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  156. /* Some error codes are special. Ensure the make_errors.go script never
  157. * regresses this. */
  158. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  159. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  160. ssl_alert_reason_code_mismatch);
  161. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  162. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  163. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  164. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  165. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  166. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  167. int SSL_library_init(void) {
  168. CRYPTO_library_init();
  169. return 1;
  170. }
  171. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  172. const uint8_t *session_id = sess->session_id;
  173. uint8_t tmp_storage[sizeof(uint32_t)];
  174. if (sess->session_id_length < sizeof(tmp_storage)) {
  175. memset(tmp_storage, 0, sizeof(tmp_storage));
  176. memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  177. session_id = tmp_storage;
  178. }
  179. uint32_t hash =
  180. ((uint32_t)session_id[0]) |
  181. ((uint32_t)session_id[1] << 8) |
  182. ((uint32_t)session_id[2] << 16) |
  183. ((uint32_t)session_id[3] << 24);
  184. return hash;
  185. }
  186. /* NB: If this function (or indeed the hash function which uses a sort of
  187. * coarser function than this one) is changed, ensure
  188. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  189. * able to construct an SSL_SESSION that will collide with any existing session
  190. * with a matching session ID. */
  191. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  192. if (a->ssl_version != b->ssl_version) {
  193. return 1;
  194. }
  195. if (a->session_id_length != b->session_id_length) {
  196. return 1;
  197. }
  198. return memcmp(a->session_id, b->session_id, a->session_id_length);
  199. }
  200. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  201. SSL_CTX *ret = NULL;
  202. if (method == NULL) {
  203. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  204. return NULL;
  205. }
  206. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  207. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  208. goto err;
  209. }
  210. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  211. if (ret == NULL) {
  212. goto err;
  213. }
  214. memset(ret, 0, sizeof(SSL_CTX));
  215. ret->method = method->method;
  216. CRYPTO_MUTEX_init(&ret->lock);
  217. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  218. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  219. /* We take the system default */
  220. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  221. ret->references = 1;
  222. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  223. ret->verify_mode = SSL_VERIFY_NONE;
  224. ret->cert = ssl_cert_new();
  225. if (ret->cert == NULL) {
  226. goto err;
  227. }
  228. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  229. if (ret->sessions == NULL) {
  230. goto err;
  231. }
  232. ret->cert_store = X509_STORE_new();
  233. if (ret->cert_store == NULL) {
  234. goto err;
  235. }
  236. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  237. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  238. if (ret->cipher_list == NULL ||
  239. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  240. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  241. goto err2;
  242. }
  243. ret->param = X509_VERIFY_PARAM_new();
  244. if (!ret->param) {
  245. goto err;
  246. }
  247. ret->client_CA = sk_X509_NAME_new_null();
  248. if (ret->client_CA == NULL) {
  249. goto err;
  250. }
  251. CRYPTO_new_ex_data(&ret->ex_data);
  252. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  253. /* Setup RFC4507 ticket keys */
  254. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  255. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  256. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  257. ret->options |= SSL_OP_NO_TICKET;
  258. }
  259. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  260. * uses of SSL_METHOD. */
  261. if (method->version != 0) {
  262. SSL_CTX_set_max_version(ret, method->version);
  263. SSL_CTX_set_min_version(ret, method->version);
  264. }
  265. return ret;
  266. err:
  267. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  268. err2:
  269. SSL_CTX_free(ret);
  270. return NULL;
  271. }
  272. void SSL_CTX_free(SSL_CTX *ctx) {
  273. if (ctx == NULL ||
  274. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  275. return;
  276. }
  277. X509_VERIFY_PARAM_free(ctx->param);
  278. /* Free internal session cache. However: the remove_cb() may reference the
  279. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  280. * sessions were flushed. As the ex_data handling routines might also touch
  281. * the session cache, the most secure solution seems to be: empty (flush) the
  282. * cache, then free ex_data, then finally free the cache. (See ticket
  283. * [openssl.org #212].) */
  284. SSL_CTX_flush_sessions(ctx, 0);
  285. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  286. CRYPTO_MUTEX_cleanup(&ctx->lock);
  287. lh_SSL_SESSION_free(ctx->sessions);
  288. X509_STORE_free(ctx->cert_store);
  289. ssl_cipher_preference_list_free(ctx->cipher_list);
  290. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  291. ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
  292. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  293. ssl_cert_free(ctx->cert);
  294. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  295. SSL_CUSTOM_EXTENSION_free);
  296. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  297. SSL_CUSTOM_EXTENSION_free);
  298. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  299. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  300. OPENSSL_free(ctx->psk_identity_hint);
  301. OPENSSL_free(ctx->tlsext_ellipticcurvelist);
  302. OPENSSL_free(ctx->alpn_client_proto_list);
  303. OPENSSL_free(ctx->ocsp_response);
  304. OPENSSL_free(ctx->signed_cert_timestamp_list);
  305. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  306. OPENSSL_free(ctx);
  307. }
  308. SSL *SSL_new(SSL_CTX *ctx) {
  309. if (ctx == NULL) {
  310. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  311. return NULL;
  312. }
  313. if (ctx->method == NULL) {
  314. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  315. return NULL;
  316. }
  317. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  318. if (ssl == NULL) {
  319. goto err;
  320. }
  321. memset(ssl, 0, sizeof(SSL));
  322. ssl->min_version = ctx->min_version;
  323. ssl->max_version = ctx->max_version;
  324. ssl->options = ctx->options;
  325. ssl->mode = ctx->mode;
  326. ssl->max_cert_list = ctx->max_cert_list;
  327. ssl->cert = ssl_cert_dup(ctx->cert);
  328. if (ssl->cert == NULL) {
  329. goto err;
  330. }
  331. ssl->msg_callback = ctx->msg_callback;
  332. ssl->msg_callback_arg = ctx->msg_callback_arg;
  333. ssl->verify_mode = ctx->verify_mode;
  334. ssl->sid_ctx_length = ctx->sid_ctx_length;
  335. assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
  336. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  337. ssl->verify_callback = ctx->default_verify_callback;
  338. ssl->param = X509_VERIFY_PARAM_new();
  339. if (!ssl->param) {
  340. goto err;
  341. }
  342. X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
  343. ssl->quiet_shutdown = ctx->quiet_shutdown;
  344. ssl->max_send_fragment = ctx->max_send_fragment;
  345. CRYPTO_refcount_inc(&ctx->references);
  346. ssl->ctx = ctx;
  347. CRYPTO_refcount_inc(&ctx->references);
  348. ssl->initial_ctx = ctx;
  349. if (ctx->tlsext_ellipticcurvelist) {
  350. ssl->tlsext_ellipticcurvelist =
  351. BUF_memdup(ctx->tlsext_ellipticcurvelist,
  352. ctx->tlsext_ellipticcurvelist_length * 2);
  353. if (!ssl->tlsext_ellipticcurvelist) {
  354. goto err;
  355. }
  356. ssl->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
  357. }
  358. if (ssl->ctx->alpn_client_proto_list) {
  359. ssl->alpn_client_proto_list = BUF_memdup(
  360. ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
  361. if (ssl->alpn_client_proto_list == NULL) {
  362. goto err;
  363. }
  364. ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
  365. }
  366. ssl->verify_result = X509_V_OK;
  367. ssl->method = ctx->method;
  368. if (!ssl->method->ssl_new(ssl)) {
  369. goto err;
  370. }
  371. ssl->rwstate = SSL_NOTHING;
  372. CRYPTO_new_ex_data(&ssl->ex_data);
  373. ssl->psk_identity_hint = NULL;
  374. if (ctx->psk_identity_hint) {
  375. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  376. if (ssl->psk_identity_hint == NULL) {
  377. goto err;
  378. }
  379. }
  380. ssl->psk_client_callback = ctx->psk_client_callback;
  381. ssl->psk_server_callback = ctx->psk_server_callback;
  382. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  383. if (ctx->tlsext_channel_id_private) {
  384. ssl->tlsext_channel_id_private =
  385. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  386. }
  387. ssl->signed_cert_timestamps_enabled =
  388. ssl->ctx->signed_cert_timestamps_enabled;
  389. ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
  390. return ssl;
  391. err:
  392. SSL_free(ssl);
  393. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  394. return NULL;
  395. }
  396. void SSL_free(SSL *ssl) {
  397. if (ssl == NULL) {
  398. return;
  399. }
  400. X509_VERIFY_PARAM_free(ssl->param);
  401. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  402. if (ssl->bbio != NULL) {
  403. /* If the buffering BIO is in place, pop it off */
  404. if (ssl->bbio == ssl->wbio) {
  405. ssl->wbio = BIO_pop(ssl->wbio);
  406. }
  407. BIO_free(ssl->bbio);
  408. ssl->bbio = NULL;
  409. }
  410. int free_wbio = ssl->wbio != ssl->rbio;
  411. BIO_free_all(ssl->rbio);
  412. if (free_wbio) {
  413. BIO_free_all(ssl->wbio);
  414. }
  415. BUF_MEM_free(ssl->init_buf);
  416. /* add extra stuff */
  417. ssl_cipher_preference_list_free(ssl->cipher_list);
  418. sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
  419. ssl_clear_bad_session(ssl);
  420. SSL_SESSION_free(ssl->session);
  421. ssl_cert_free(ssl->cert);
  422. OPENSSL_free(ssl->tlsext_hostname);
  423. SSL_CTX_free(ssl->initial_ctx);
  424. OPENSSL_free(ssl->tlsext_ellipticcurvelist);
  425. OPENSSL_free(ssl->alpn_client_proto_list);
  426. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  427. OPENSSL_free(ssl->psk_identity_hint);
  428. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  429. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  430. if (ssl->method != NULL) {
  431. ssl->method->ssl_free(ssl);
  432. }
  433. SSL_CTX_free(ssl->ctx);
  434. OPENSSL_free(ssl);
  435. }
  436. void SSL_set_connect_state(SSL *ssl) {
  437. ssl->server = 0;
  438. ssl->shutdown = 0;
  439. ssl->state = SSL_ST_CONNECT;
  440. ssl->handshake_func = ssl->method->ssl_connect;
  441. }
  442. void SSL_set_accept_state(SSL *ssl) {
  443. ssl->server = 1;
  444. ssl->shutdown = 0;
  445. ssl->state = SSL_ST_ACCEPT;
  446. ssl->handshake_func = ssl->method->ssl_accept;
  447. }
  448. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  449. /* If the output buffering BIO is still in place, remove it. */
  450. if (ssl->bbio != NULL) {
  451. if (ssl->wbio == ssl->bbio) {
  452. ssl->wbio = ssl->wbio->next_bio;
  453. ssl->bbio->next_bio = NULL;
  454. }
  455. }
  456. if (ssl->rbio != rbio) {
  457. BIO_free_all(ssl->rbio);
  458. }
  459. if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
  460. BIO_free_all(ssl->wbio);
  461. }
  462. ssl->rbio = rbio;
  463. ssl->wbio = wbio;
  464. }
  465. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  466. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  467. int SSL_do_handshake(SSL *ssl) {
  468. ssl->rwstate = SSL_NOTHING;
  469. /* Functions which use SSL_get_error must clear the error queue on entry. */
  470. ERR_clear_error();
  471. if (ssl->handshake_func == NULL) {
  472. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  473. return -1;
  474. }
  475. if (!SSL_in_init(ssl)) {
  476. return 1;
  477. }
  478. return ssl->handshake_func(ssl);
  479. }
  480. int SSL_connect(SSL *ssl) {
  481. if (ssl->handshake_func == NULL) {
  482. /* Not properly initialized yet */
  483. SSL_set_connect_state(ssl);
  484. }
  485. assert(ssl->handshake_func == ssl->method->ssl_connect);
  486. return SSL_do_handshake(ssl);
  487. }
  488. int SSL_accept(SSL *ssl) {
  489. if (ssl->handshake_func == NULL) {
  490. /* Not properly initialized yet */
  491. SSL_set_accept_state(ssl);
  492. }
  493. assert(ssl->handshake_func == ssl->method->ssl_accept);
  494. return SSL_do_handshake(ssl);
  495. }
  496. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  497. ssl->rwstate = SSL_NOTHING;
  498. /* Functions which use SSL_get_error must clear the error queue on entry. */
  499. ERR_clear_error();
  500. ERR_clear_system_error();
  501. if (ssl->handshake_func == NULL) {
  502. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  503. return -1;
  504. }
  505. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  506. return 0;
  507. }
  508. /* This may require multiple iterations. False Start will cause
  509. * |ssl->handshake_func| to signal success one step early, but the handshake
  510. * must be completely finished before other modes are accepted. */
  511. while (SSL_in_init(ssl)) {
  512. int ret = SSL_do_handshake(ssl);
  513. if (ret < 0) {
  514. return ret;
  515. }
  516. if (ret == 0) {
  517. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  518. return -1;
  519. }
  520. }
  521. return ssl->method->ssl_read_app_data(ssl, buf, num, peek);
  522. }
  523. int SSL_read(SSL *ssl, void *buf, int num) {
  524. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  525. }
  526. int SSL_peek(SSL *ssl, void *buf, int num) {
  527. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  528. }
  529. int SSL_write(SSL *ssl, const void *buf, int num) {
  530. ssl->rwstate = SSL_NOTHING;
  531. /* Functions which use SSL_get_error must clear the error queue on entry. */
  532. ERR_clear_error();
  533. ERR_clear_system_error();
  534. if (ssl->handshake_func == NULL) {
  535. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  536. return -1;
  537. }
  538. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  539. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  540. return -1;
  541. }
  542. /* If necessary, complete the handshake implicitly. */
  543. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  544. int ret = SSL_do_handshake(ssl);
  545. if (ret < 0) {
  546. return ret;
  547. }
  548. if (ret == 0) {
  549. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  550. return -1;
  551. }
  552. }
  553. return ssl->method->ssl_write_app_data(ssl, buf, num);
  554. }
  555. int SSL_shutdown(SSL *ssl) {
  556. ssl->rwstate = SSL_NOTHING;
  557. /* Functions which use SSL_get_error must clear the error queue on entry. */
  558. ERR_clear_error();
  559. /* Note that this function behaves differently from what one might expect.
  560. * Return values are 0 for no success (yet), 1 for success; but calling it
  561. * once is usually not enough, even if blocking I/O is used (see
  562. * ssl3_shutdown). */
  563. if (ssl->handshake_func == NULL) {
  564. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  565. return -1;
  566. }
  567. /* We can't shutdown properly if we are in the middle of a handshake. */
  568. if (SSL_in_init(ssl)) {
  569. OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  570. return -1;
  571. }
  572. /* Do nothing if configured not to send a close_notify. */
  573. if (ssl->quiet_shutdown) {
  574. ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
  575. return 1;
  576. }
  577. if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
  578. ssl->shutdown |= SSL_SENT_SHUTDOWN;
  579. ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
  580. /* our shutdown alert has been sent now, and if it still needs to be
  581. * written, ssl->s3->alert_dispatch will be true */
  582. if (ssl->s3->alert_dispatch) {
  583. return -1; /* return WANT_WRITE */
  584. }
  585. } else if (ssl->s3->alert_dispatch) {
  586. /* resend it if not sent */
  587. int ret = ssl->method->ssl_dispatch_alert(ssl);
  588. if (ret == -1) {
  589. /* we only get to return -1 here the 2nd/Nth invocation, we must have
  590. * already signalled return 0 upon a previous invoation, return
  591. * WANT_WRITE */
  592. return ret;
  593. }
  594. } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  595. /* If we are waiting for a close from our peer, we are closed */
  596. ssl->method->ssl_read_close_notify(ssl);
  597. if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  598. return -1; /* return WANT_READ */
  599. }
  600. }
  601. if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
  602. !ssl->s3->alert_dispatch) {
  603. return 1;
  604. } else {
  605. return 0;
  606. }
  607. }
  608. int SSL_get_error(const SSL *ssl, int ret_code) {
  609. int reason;
  610. uint32_t err;
  611. BIO *bio;
  612. if (ret_code > 0) {
  613. return SSL_ERROR_NONE;
  614. }
  615. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  616. * where we do encode the error */
  617. err = ERR_peek_error();
  618. if (err != 0) {
  619. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  620. return SSL_ERROR_SYSCALL;
  621. }
  622. return SSL_ERROR_SSL;
  623. }
  624. if (ret_code == 0) {
  625. if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) && ssl->s3->clean_shutdown) {
  626. /* The socket was cleanly shut down with a close_notify. */
  627. return SSL_ERROR_ZERO_RETURN;
  628. }
  629. /* An EOF was observed which violates the protocol, and the underlying
  630. * transport does not participate in the error queue. Bubble up to the
  631. * caller. */
  632. return SSL_ERROR_SYSCALL;
  633. }
  634. if (SSL_want_session(ssl)) {
  635. return SSL_ERROR_PENDING_SESSION;
  636. }
  637. if (SSL_want_certificate(ssl)) {
  638. return SSL_ERROR_PENDING_CERTIFICATE;
  639. }
  640. if (SSL_want_read(ssl)) {
  641. bio = SSL_get_rbio(ssl);
  642. if (BIO_should_read(bio)) {
  643. return SSL_ERROR_WANT_READ;
  644. }
  645. if (BIO_should_write(bio)) {
  646. /* This one doesn't make too much sense ... We never try to write to the
  647. * rbio, and an application program where rbio and wbio are separate
  648. * couldn't even know what it should wait for. However if we ever set
  649. * ssl->rwstate incorrectly (so that we have SSL_want_read(ssl) instead of
  650. * SSL_want_write(ssl)) and rbio and wbio *are* the same, this test works
  651. * around that bug; so it might be safer to keep it. */
  652. return SSL_ERROR_WANT_WRITE;
  653. }
  654. if (BIO_should_io_special(bio)) {
  655. reason = BIO_get_retry_reason(bio);
  656. if (reason == BIO_RR_CONNECT) {
  657. return SSL_ERROR_WANT_CONNECT;
  658. }
  659. if (reason == BIO_RR_ACCEPT) {
  660. return SSL_ERROR_WANT_ACCEPT;
  661. }
  662. return SSL_ERROR_SYSCALL; /* unknown */
  663. }
  664. }
  665. if (SSL_want_write(ssl)) {
  666. bio = SSL_get_wbio(ssl);
  667. if (BIO_should_write(bio)) {
  668. return SSL_ERROR_WANT_WRITE;
  669. }
  670. if (BIO_should_read(bio)) {
  671. /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
  672. return SSL_ERROR_WANT_READ;
  673. }
  674. if (BIO_should_io_special(bio)) {
  675. reason = BIO_get_retry_reason(bio);
  676. if (reason == BIO_RR_CONNECT) {
  677. return SSL_ERROR_WANT_CONNECT;
  678. }
  679. if (reason == BIO_RR_ACCEPT) {
  680. return SSL_ERROR_WANT_ACCEPT;
  681. }
  682. return SSL_ERROR_SYSCALL;
  683. }
  684. }
  685. if (SSL_want_x509_lookup(ssl)) {
  686. return SSL_ERROR_WANT_X509_LOOKUP;
  687. }
  688. if (SSL_want_channel_id_lookup(ssl)) {
  689. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  690. }
  691. if (SSL_want_private_key_operation(ssl)) {
  692. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  693. }
  694. return SSL_ERROR_SYSCALL;
  695. }
  696. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  697. ctx->min_version = version;
  698. }
  699. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  700. ctx->max_version = version;
  701. }
  702. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  703. ssl->min_version = version;
  704. }
  705. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  706. ssl->max_version = version;
  707. }
  708. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  709. ctx->options |= options;
  710. return ctx->options;
  711. }
  712. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  713. ctx->options &= ~options;
  714. return ctx->options;
  715. }
  716. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  717. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  718. ssl->options |= options;
  719. return ssl->options;
  720. }
  721. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  722. ssl->options &= ~options;
  723. return ssl->options;
  724. }
  725. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  726. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  727. ctx->mode |= mode;
  728. return ctx->mode;
  729. }
  730. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  731. ctx->mode &= ~mode;
  732. return ctx->mode;
  733. }
  734. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  735. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  736. ssl->mode |= mode;
  737. return ssl->mode;
  738. }
  739. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  740. ssl->mode &= ~mode;
  741. return ssl->mode;
  742. }
  743. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  744. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  745. if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
  746. return NULL;
  747. }
  748. return X509_up_ref(ssl->session->peer);
  749. }
  750. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  751. if (ssl == NULL || ssl->session == NULL) {
  752. return NULL;
  753. }
  754. return ssl->session->cert_chain;
  755. }
  756. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  757. size_t max_out) {
  758. /* The tls-unique value is the first Finished message in the handshake, which
  759. * is the client's in a full handshake and the server's for a resumption. See
  760. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  761. const uint8_t *finished = ssl->s3->previous_client_finished;
  762. size_t finished_len = ssl->s3->previous_client_finished_len;
  763. if (ssl->hit) {
  764. /* tls-unique is broken for resumed sessions unless EMS is used. */
  765. if (!ssl->session->extended_master_secret) {
  766. goto err;
  767. }
  768. finished = ssl->s3->previous_server_finished;
  769. finished_len = ssl->s3->previous_server_finished_len;
  770. }
  771. if (!ssl->s3->initial_handshake_complete ||
  772. ssl->version < TLS1_VERSION) {
  773. goto err;
  774. }
  775. *out_len = finished_len;
  776. if (finished_len > max_out) {
  777. *out_len = max_out;
  778. }
  779. memcpy(out, finished, *out_len);
  780. return 1;
  781. err:
  782. *out_len = 0;
  783. memset(out, 0, max_out);
  784. return 0;
  785. }
  786. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  787. unsigned sid_ctx_len) {
  788. if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
  789. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  790. return 0;
  791. }
  792. ctx->sid_ctx_length = sid_ctx_len;
  793. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  794. return 1;
  795. }
  796. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  797. unsigned sid_ctx_len) {
  798. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  799. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  800. return 0;
  801. }
  802. ssl->sid_ctx_length = sid_ctx_len;
  803. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  804. return 1;
  805. }
  806. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  807. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  808. }
  809. int SSL_set_purpose(SSL *ssl, int purpose) {
  810. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  811. }
  812. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  813. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  814. }
  815. int SSL_set_trust(SSL *ssl, int trust) {
  816. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  817. }
  818. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  819. return X509_VERIFY_PARAM_set1(ctx->param, param);
  820. }
  821. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  822. return X509_VERIFY_PARAM_set1(ssl->param, param);
  823. }
  824. void ssl_cipher_preference_list_free(
  825. struct ssl_cipher_preference_list_st *cipher_list) {
  826. if (cipher_list == NULL) {
  827. return;
  828. }
  829. sk_SSL_CIPHER_free(cipher_list->ciphers);
  830. OPENSSL_free(cipher_list->in_group_flags);
  831. OPENSSL_free(cipher_list);
  832. }
  833. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  834. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  835. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  836. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  837. int SSL_get_rfd(const SSL *ssl) {
  838. int ret = -1;
  839. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  840. if (b != NULL) {
  841. BIO_get_fd(b, &ret);
  842. }
  843. return ret;
  844. }
  845. int SSL_get_wfd(const SSL *ssl) {
  846. int ret = -1;
  847. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  848. if (b != NULL) {
  849. BIO_get_fd(b, &ret);
  850. }
  851. return ret;
  852. }
  853. int SSL_set_fd(SSL *ssl, int fd) {
  854. BIO *bio = BIO_new(BIO_s_socket());
  855. if (bio == NULL) {
  856. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  857. return 0;
  858. }
  859. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  860. SSL_set_bio(ssl, bio, bio);
  861. return 1;
  862. }
  863. int SSL_set_wfd(SSL *ssl, int fd) {
  864. if (ssl->rbio == NULL ||
  865. BIO_method_type(ssl->rbio) != BIO_TYPE_SOCKET ||
  866. BIO_get_fd(ssl->rbio, NULL) != fd) {
  867. BIO *bio = BIO_new(BIO_s_socket());
  868. if (bio == NULL) {
  869. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  870. return 0;
  871. }
  872. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  873. SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
  874. } else {
  875. SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
  876. }
  877. return 1;
  878. }
  879. int SSL_set_rfd(SSL *ssl, int fd) {
  880. if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_SOCKET ||
  881. BIO_get_fd(ssl->wbio, NULL) != fd) {
  882. BIO *bio = BIO_new(BIO_s_socket());
  883. if (bio == NULL) {
  884. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  885. return 0;
  886. }
  887. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  888. SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
  889. } else {
  890. SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
  891. }
  892. return 1;
  893. }
  894. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  895. size_t ret = 0;
  896. if (ssl->s3 != NULL) {
  897. ret = ssl->s3->tmp.finish_md_len;
  898. if (count > ret) {
  899. count = ret;
  900. }
  901. memcpy(buf, ssl->s3->tmp.finish_md, count);
  902. }
  903. return ret;
  904. }
  905. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  906. size_t ret = 0;
  907. if (ssl->s3 != NULL) {
  908. ret = ssl->s3->tmp.peer_finish_md_len;
  909. if (count > ret) {
  910. count = ret;
  911. }
  912. memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
  913. }
  914. return ret;
  915. }
  916. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  917. int SSL_get_verify_depth(const SSL *ssl) {
  918. return X509_VERIFY_PARAM_get_depth(ssl->param);
  919. }
  920. int SSL_get_extms_support(const SSL *ssl) {
  921. return ssl->s3->tmp.extended_master_secret == 1;
  922. }
  923. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  924. return ssl->verify_callback;
  925. }
  926. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  927. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  928. return X509_VERIFY_PARAM_get_depth(ctx->param);
  929. }
  930. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  931. int ok, X509_STORE_CTX *store_ctx) {
  932. return ctx->default_verify_callback;
  933. }
  934. void SSL_set_verify(SSL *ssl, int mode,
  935. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  936. ssl->verify_mode = mode;
  937. if (callback != NULL) {
  938. ssl->verify_callback = callback;
  939. }
  940. }
  941. void SSL_set_verify_depth(SSL *ssl, int depth) {
  942. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  943. }
  944. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  945. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  946. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  947. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  948. int SSL_pending(const SSL *ssl) {
  949. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  950. return 0;
  951. }
  952. return ssl->s3->rrec.length;
  953. }
  954. /* Fix this so it checks all the valid key/cert options */
  955. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  956. if (ctx->cert->x509 == NULL) {
  957. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  958. return 0;
  959. }
  960. if (ctx->cert->privatekey == NULL) {
  961. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  962. return 0;
  963. }
  964. return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
  965. }
  966. /* Fix this function so that it takes an optional type parameter */
  967. int SSL_check_private_key(const SSL *ssl) {
  968. if (ssl->cert->x509 == NULL) {
  969. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  970. return 0;
  971. }
  972. if (ssl->cert->privatekey == NULL) {
  973. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  974. return 0;
  975. }
  976. return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
  977. }
  978. long SSL_get_default_timeout(const SSL *ssl) {
  979. return SSL_DEFAULT_SESSION_TIMEOUT;
  980. }
  981. int SSL_renegotiate(SSL *ssl) {
  982. /* Caller-initiated renegotiation is not supported. */
  983. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  984. return 0;
  985. }
  986. int SSL_renegotiate_pending(SSL *ssl) {
  987. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  988. }
  989. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  990. return ctx->max_cert_list;
  991. }
  992. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  993. if (max_cert_list > kMaxHandshakeSize) {
  994. max_cert_list = kMaxHandshakeSize;
  995. }
  996. ctx->max_cert_list = (uint32_t)max_cert_list;
  997. }
  998. size_t SSL_get_max_cert_list(const SSL *ssl) {
  999. return ssl->max_cert_list;
  1000. }
  1001. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1002. if (max_cert_list > kMaxHandshakeSize) {
  1003. max_cert_list = kMaxHandshakeSize;
  1004. }
  1005. ssl->max_cert_list = (uint32_t)max_cert_list;
  1006. }
  1007. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1008. if (max_send_fragment < 512) {
  1009. max_send_fragment = 512;
  1010. }
  1011. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1012. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1013. }
  1014. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1015. return 1;
  1016. }
  1017. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1018. if (max_send_fragment < 512) {
  1019. max_send_fragment = 512;
  1020. }
  1021. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1022. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1023. }
  1024. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1025. return 1;
  1026. }
  1027. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1028. if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
  1029. return 0;
  1030. }
  1031. ssl->d1->mtu = mtu;
  1032. return 1;
  1033. }
  1034. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1035. return ssl->s3->send_connection_binding;
  1036. }
  1037. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1038. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1039. return lh_SSL_SESSION_num_items(ctx->sessions);
  1040. }
  1041. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1042. unsigned long ret = ctx->session_cache_size;
  1043. ctx->session_cache_size = size;
  1044. return ret;
  1045. }
  1046. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1047. return ctx->session_cache_size;
  1048. }
  1049. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1050. int ret = ctx->session_cache_mode;
  1051. ctx->session_cache_mode = mode;
  1052. return ret;
  1053. }
  1054. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1055. return ctx->session_cache_mode;
  1056. }
  1057. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1058. if (ssl == NULL) {
  1059. return NULL;
  1060. }
  1061. if (ssl->cipher_list != NULL) {
  1062. return ssl->cipher_list->ciphers;
  1063. }
  1064. if (ssl->version >= TLS1_1_VERSION && ssl->ctx->cipher_list_tls11 != NULL) {
  1065. return ssl->ctx->cipher_list_tls11->ciphers;
  1066. }
  1067. if (ssl->version >= TLS1_VERSION && ssl->ctx->cipher_list_tls10 != NULL) {
  1068. return ssl->ctx->cipher_list_tls10->ciphers;
  1069. }
  1070. if (ssl->ctx->cipher_list != NULL) {
  1071. return ssl->ctx->cipher_list->ciphers;
  1072. }
  1073. return NULL;
  1074. }
  1075. /* return a STACK of the ciphers available for the SSL and in order of
  1076. * algorithm id */
  1077. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
  1078. if (ssl == NULL) {
  1079. return NULL;
  1080. }
  1081. if (ssl->cipher_list_by_id != NULL) {
  1082. return ssl->cipher_list_by_id;
  1083. }
  1084. if (ssl->ctx->cipher_list_by_id != NULL) {
  1085. return ssl->ctx->cipher_list_by_id;
  1086. }
  1087. return NULL;
  1088. }
  1089. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1090. const SSL_CIPHER *c;
  1091. STACK_OF(SSL_CIPHER) *sk;
  1092. if (ssl == NULL) {
  1093. return NULL;
  1094. }
  1095. sk = SSL_get_ciphers(ssl);
  1096. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1097. return NULL;
  1098. }
  1099. c = sk_SSL_CIPHER_value(sk, n);
  1100. if (c == NULL) {
  1101. return NULL;
  1102. }
  1103. return c->name;
  1104. }
  1105. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1106. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1107. ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
  1108. if (cipher_list == NULL) {
  1109. return 0;
  1110. }
  1111. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1112. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1113. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1114. return 0;
  1115. }
  1116. return 1;
  1117. }
  1118. int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
  1119. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1120. ctx->method, &ctx->cipher_list_tls10, NULL, str);
  1121. if (cipher_list == NULL) {
  1122. return 0;
  1123. }
  1124. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1125. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1126. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1127. return 0;
  1128. }
  1129. return 1;
  1130. }
  1131. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1132. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1133. ctx->method, &ctx->cipher_list_tls11, NULL, str);
  1134. if (cipher_list == NULL) {
  1135. return 0;
  1136. }
  1137. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1138. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1139. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1140. return 0;
  1141. }
  1142. return 1;
  1143. }
  1144. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1145. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1146. ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
  1147. if (cipher_list == NULL) {
  1148. return 0;
  1149. }
  1150. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1151. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1152. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1153. return 0;
  1154. }
  1155. return 1;
  1156. }
  1157. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
  1158. CBS cipher_suites = *cbs;
  1159. const SSL_CIPHER *c;
  1160. STACK_OF(SSL_CIPHER) *sk;
  1161. if (ssl->s3) {
  1162. ssl->s3->send_connection_binding = 0;
  1163. }
  1164. if (CBS_len(&cipher_suites) % 2 != 0) {
  1165. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1166. return NULL;
  1167. }
  1168. sk = sk_SSL_CIPHER_new_null();
  1169. if (sk == NULL) {
  1170. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1171. goto err;
  1172. }
  1173. while (CBS_len(&cipher_suites) > 0) {
  1174. uint16_t cipher_suite;
  1175. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1176. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1177. goto err;
  1178. }
  1179. /* Check for SCSV. */
  1180. if (ssl->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1181. /* SCSV is fatal if renegotiating. */
  1182. if (ssl->s3->initial_handshake_complete) {
  1183. OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1184. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1185. goto err;
  1186. }
  1187. ssl->s3->send_connection_binding = 1;
  1188. continue;
  1189. }
  1190. /* Check for FALLBACK_SCSV. */
  1191. if (ssl->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1192. uint16_t max_version = ssl3_get_max_server_version(ssl);
  1193. if (SSL_IS_DTLS(ssl) ? (uint16_t)ssl->version > max_version
  1194. : (uint16_t)ssl->version < max_version) {
  1195. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  1196. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1197. goto err;
  1198. }
  1199. continue;
  1200. }
  1201. c = SSL_get_cipher_by_value(cipher_suite);
  1202. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1203. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1204. goto err;
  1205. }
  1206. }
  1207. return sk;
  1208. err:
  1209. sk_SSL_CIPHER_free(sk);
  1210. return NULL;
  1211. }
  1212. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1213. if (type != TLSEXT_NAMETYPE_host_name) {
  1214. return NULL;
  1215. }
  1216. /* Historically, |SSL_get_servername| was also the configuration getter
  1217. * corresponding to |SSL_set_tlsext_host_name|. */
  1218. if (ssl->tlsext_hostname != NULL) {
  1219. return ssl->tlsext_hostname;
  1220. }
  1221. if (ssl->session == NULL) {
  1222. return NULL;
  1223. }
  1224. return ssl->session->tlsext_hostname;
  1225. }
  1226. int SSL_get_servername_type(const SSL *ssl) {
  1227. if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
  1228. return TLSEXT_NAMETYPE_host_name;
  1229. }
  1230. return -1;
  1231. }
  1232. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1233. ctx->signed_cert_timestamps_enabled = 1;
  1234. }
  1235. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1236. ssl->signed_cert_timestamps_enabled = 1;
  1237. return 1;
  1238. }
  1239. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1240. ctx->ocsp_stapling_enabled = 1;
  1241. }
  1242. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1243. ssl->ocsp_stapling_enabled = 1;
  1244. return 1;
  1245. }
  1246. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1247. size_t *out_len) {
  1248. SSL_SESSION *session = ssl->session;
  1249. *out_len = 0;
  1250. *out = NULL;
  1251. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1252. return;
  1253. }
  1254. *out = session->tlsext_signed_cert_timestamp_list;
  1255. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1256. }
  1257. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1258. size_t *out_len) {
  1259. SSL_SESSION *session = ssl->session;
  1260. *out_len = 0;
  1261. *out = NULL;
  1262. if (ssl->server || !session || !session->ocsp_response) {
  1263. return;
  1264. }
  1265. *out = session->ocsp_response;
  1266. *out_len = session->ocsp_response_length;
  1267. }
  1268. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  1269. size_t list_len) {
  1270. OPENSSL_free(ctx->signed_cert_timestamp_list);
  1271. ctx->signed_cert_timestamp_list_length = 0;
  1272. ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
  1273. if (ctx->signed_cert_timestamp_list == NULL) {
  1274. return 0;
  1275. }
  1276. ctx->signed_cert_timestamp_list_length = list_len;
  1277. return 1;
  1278. }
  1279. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  1280. size_t response_len) {
  1281. OPENSSL_free(ctx->ocsp_response);
  1282. ctx->ocsp_response_length = 0;
  1283. ctx->ocsp_response = BUF_memdup(response, response_len);
  1284. if (ctx->ocsp_response == NULL) {
  1285. return 0;
  1286. }
  1287. ctx->ocsp_response_length = response_len;
  1288. return 1;
  1289. }
  1290. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1291. const uint8_t *server, unsigned server_len,
  1292. const uint8_t *client, unsigned client_len) {
  1293. unsigned int i, j;
  1294. const uint8_t *result;
  1295. int status = OPENSSL_NPN_UNSUPPORTED;
  1296. /* For each protocol in server preference order, see if we support it. */
  1297. for (i = 0; i < server_len;) {
  1298. for (j = 0; j < client_len;) {
  1299. if (server[i] == client[j] &&
  1300. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1301. /* We found a match */
  1302. result = &server[i];
  1303. status = OPENSSL_NPN_NEGOTIATED;
  1304. goto found;
  1305. }
  1306. j += client[j];
  1307. j++;
  1308. }
  1309. i += server[i];
  1310. i++;
  1311. }
  1312. /* There's no overlap between our protocols and the server's list. */
  1313. result = client;
  1314. status = OPENSSL_NPN_NO_OVERLAP;
  1315. found:
  1316. *out = (uint8_t *)result + 1;
  1317. *out_len = result[0];
  1318. return status;
  1319. }
  1320. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1321. unsigned *out_len) {
  1322. *out_data = ssl->s3->next_proto_negotiated;
  1323. if (*out_data == NULL) {
  1324. *out_len = 0;
  1325. } else {
  1326. *out_len = ssl->s3->next_proto_negotiated_len;
  1327. }
  1328. }
  1329. void SSL_CTX_set_next_protos_advertised_cb(
  1330. SSL_CTX *ctx,
  1331. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1332. void *arg) {
  1333. ctx->next_protos_advertised_cb = cb;
  1334. ctx->next_protos_advertised_cb_arg = arg;
  1335. }
  1336. void SSL_CTX_set_next_proto_select_cb(
  1337. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1338. const uint8_t *in, unsigned in_len, void *arg),
  1339. void *arg) {
  1340. ctx->next_proto_select_cb = cb;
  1341. ctx->next_proto_select_cb_arg = arg;
  1342. }
  1343. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1344. unsigned protos_len) {
  1345. OPENSSL_free(ctx->alpn_client_proto_list);
  1346. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1347. if (!ctx->alpn_client_proto_list) {
  1348. return 1;
  1349. }
  1350. ctx->alpn_client_proto_list_len = protos_len;
  1351. return 0;
  1352. }
  1353. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1354. OPENSSL_free(ssl->alpn_client_proto_list);
  1355. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1356. if (!ssl->alpn_client_proto_list) {
  1357. return 1;
  1358. }
  1359. ssl->alpn_client_proto_list_len = protos_len;
  1360. return 0;
  1361. }
  1362. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1363. int (*cb)(SSL *ssl, const uint8_t **out,
  1364. uint8_t *out_len, const uint8_t *in,
  1365. unsigned in_len, void *arg),
  1366. void *arg) {
  1367. ctx->alpn_select_cb = cb;
  1368. ctx->alpn_select_cb_arg = arg;
  1369. }
  1370. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1371. unsigned *out_len) {
  1372. *out_data = NULL;
  1373. if (ssl->s3) {
  1374. *out_data = ssl->s3->alpn_selected;
  1375. }
  1376. if (*out_data == NULL) {
  1377. *out_len = 0;
  1378. } else {
  1379. *out_len = ssl->s3->alpn_selected_len;
  1380. }
  1381. }
  1382. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1383. int (*cb)(X509_STORE_CTX *store_ctx,
  1384. void *arg),
  1385. void *arg) {
  1386. ctx->app_verify_callback = cb;
  1387. ctx->app_verify_arg = arg;
  1388. }
  1389. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1390. int (*cb)(int, X509_STORE_CTX *)) {
  1391. ctx->verify_mode = mode;
  1392. ctx->default_verify_callback = cb;
  1393. }
  1394. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1395. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1396. }
  1397. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  1398. void *arg) {
  1399. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  1400. }
  1401. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1402. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  1403. }
  1404. void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
  1405. uint32_t *out_mask_a) {
  1406. CERT *c = ssl->cert;
  1407. int have_rsa_cert = 0, dh_tmp;
  1408. uint32_t mask_k, mask_a;
  1409. int have_ecc_cert = 0, ecdsa_ok;
  1410. X509 *x;
  1411. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1412. if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
  1413. if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
  1414. have_rsa_cert = 1;
  1415. } else if (ssl_private_key_type(ssl) == EVP_PKEY_EC) {
  1416. have_ecc_cert = 1;
  1417. }
  1418. }
  1419. mask_k = 0;
  1420. mask_a = 0;
  1421. if (dh_tmp) {
  1422. mask_k |= SSL_kDHE;
  1423. }
  1424. if (have_rsa_cert) {
  1425. mask_k |= SSL_kRSA;
  1426. mask_a |= SSL_aRSA;
  1427. }
  1428. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1429. * key usage extension and on the client's curve preferences. */
  1430. if (have_ecc_cert) {
  1431. x = c->x509;
  1432. /* This call populates extension flags (ex_flags). */
  1433. X509_check_purpose(x, -1, 0);
  1434. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1435. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1436. : 1;
  1437. if (!tls1_check_ec_cert(ssl, x)) {
  1438. ecdsa_ok = 0;
  1439. }
  1440. if (ecdsa_ok) {
  1441. mask_a |= SSL_aECDSA;
  1442. }
  1443. }
  1444. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1445. * key, check for a shared curve. */
  1446. uint16_t unused;
  1447. if (tls1_get_shared_curve(ssl, &unused)) {
  1448. mask_k |= SSL_kECDHE;
  1449. }
  1450. /* PSK requires a server callback. */
  1451. if (ssl->psk_server_callback != NULL) {
  1452. mask_k |= SSL_kPSK;
  1453. mask_a |= SSL_aPSK;
  1454. }
  1455. *out_mask_k = mask_k;
  1456. *out_mask_a = mask_a;
  1457. }
  1458. void ssl_update_cache(SSL *ssl, int mode) {
  1459. SSL_CTX *ctx = ssl->initial_ctx;
  1460. /* Never cache sessions with empty session IDs. */
  1461. if (ssl->session->session_id_length == 0 ||
  1462. (ctx->session_cache_mode & mode) != mode) {
  1463. return;
  1464. }
  1465. /* Clients never use the internal session cache. */
  1466. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1467. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1468. /* A client may see new sessions on abbreviated handshakes if the server
  1469. * decides to renew the ticket. Once the handshake is completed, it should be
  1470. * inserted into the cache. */
  1471. if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
  1472. if (use_internal_cache) {
  1473. SSL_CTX_add_session(ctx, ssl->session);
  1474. }
  1475. if (ctx->new_session_cb != NULL &&
  1476. !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
  1477. /* |new_session_cb|'s return value signals whether it took ownership. */
  1478. SSL_SESSION_free(ssl->session);
  1479. }
  1480. }
  1481. if (use_internal_cache &&
  1482. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1483. /* Automatically flush the internal session cache every 255 connections. */
  1484. int flush_cache = 0;
  1485. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1486. ctx->handshakes_since_cache_flush++;
  1487. if (ctx->handshakes_since_cache_flush >= 255) {
  1488. flush_cache = 1;
  1489. ctx->handshakes_since_cache_flush = 0;
  1490. }
  1491. CRYPTO_MUTEX_unlock(&ctx->lock);
  1492. if (flush_cache) {
  1493. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1494. }
  1495. }
  1496. }
  1497. static const char *ssl_get_version(int version) {
  1498. switch (version) {
  1499. case TLS1_2_VERSION:
  1500. return "TLSv1.2";
  1501. case TLS1_1_VERSION:
  1502. return "TLSv1.1";
  1503. case TLS1_VERSION:
  1504. return "TLSv1";
  1505. case SSL3_VERSION:
  1506. return "SSLv3";
  1507. case DTLS1_VERSION:
  1508. return "DTLSv1";
  1509. case DTLS1_2_VERSION:
  1510. return "DTLSv1.2";
  1511. default:
  1512. return "unknown";
  1513. }
  1514. }
  1515. const char *SSL_get_version(const SSL *ssl) {
  1516. return ssl_get_version(ssl->version);
  1517. }
  1518. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1519. return ssl_get_version(session->ssl_version);
  1520. }
  1521. X509 *SSL_get_certificate(const SSL *ssl) {
  1522. if (ssl->cert != NULL) {
  1523. return ssl->cert->x509;
  1524. }
  1525. return NULL;
  1526. }
  1527. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1528. if (ssl->cert != NULL) {
  1529. return ssl->cert->privatekey;
  1530. }
  1531. return NULL;
  1532. }
  1533. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1534. if (ctx->cert != NULL) {
  1535. return ctx->cert->x509;
  1536. }
  1537. return NULL;
  1538. }
  1539. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1540. if (ctx->cert != NULL) {
  1541. return ctx->cert->privatekey;
  1542. }
  1543. return NULL;
  1544. }
  1545. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1546. if (ssl->s3->aead_write_ctx == NULL) {
  1547. return NULL;
  1548. }
  1549. return ssl->s3->aead_write_ctx->cipher;
  1550. }
  1551. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1552. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1553. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1554. int ssl_init_wbio_buffer(SSL *ssl, int push) {
  1555. BIO *bbio;
  1556. if (ssl->bbio == NULL) {
  1557. bbio = BIO_new(BIO_f_buffer());
  1558. if (bbio == NULL) {
  1559. return 0;
  1560. }
  1561. ssl->bbio = bbio;
  1562. } else {
  1563. bbio = ssl->bbio;
  1564. if (ssl->bbio == ssl->wbio) {
  1565. ssl->wbio = BIO_pop(ssl->wbio);
  1566. }
  1567. }
  1568. BIO_reset(bbio);
  1569. if (!BIO_set_read_buffer_size(bbio, 1)) {
  1570. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1571. return 0;
  1572. }
  1573. if (push) {
  1574. if (ssl->wbio != bbio) {
  1575. ssl->wbio = BIO_push(bbio, ssl->wbio);
  1576. }
  1577. } else {
  1578. if (ssl->wbio == bbio) {
  1579. ssl->wbio = BIO_pop(bbio);
  1580. }
  1581. }
  1582. return 1;
  1583. }
  1584. void ssl_free_wbio_buffer(SSL *ssl) {
  1585. if (ssl->bbio == NULL) {
  1586. return;
  1587. }
  1588. if (ssl->bbio == ssl->wbio) {
  1589. /* remove buffering */
  1590. ssl->wbio = BIO_pop(ssl->wbio);
  1591. }
  1592. BIO_free(ssl->bbio);
  1593. ssl->bbio = NULL;
  1594. }
  1595. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1596. ctx->quiet_shutdown = (mode != 0);
  1597. }
  1598. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1599. return ctx->quiet_shutdown;
  1600. }
  1601. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1602. ssl->quiet_shutdown = (mode != 0);
  1603. }
  1604. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1605. void SSL_set_shutdown(SSL *ssl, int mode) {
  1606. /* It is an error to clear any bits that have already been set. (We can't try
  1607. * to get a second close_notify or send two.) */
  1608. assert((ssl->shutdown & mode) == ssl->shutdown);
  1609. ssl->shutdown |= mode;
  1610. }
  1611. int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
  1612. int SSL_version(const SSL *ssl) { return ssl->version; }
  1613. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1614. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1615. if (ssl->ctx == ctx) {
  1616. return ssl->ctx;
  1617. }
  1618. if (ctx == NULL) {
  1619. ctx = ssl->initial_ctx;
  1620. }
  1621. ssl_cert_free(ssl->cert);
  1622. ssl->cert = ssl_cert_dup(ctx->cert);
  1623. CRYPTO_refcount_inc(&ctx->references);
  1624. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1625. ssl->ctx = ctx;
  1626. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1627. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1628. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1629. return ssl->ctx;
  1630. }
  1631. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1632. return X509_STORE_set_default_paths(ctx->cert_store);
  1633. }
  1634. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  1635. const char *ca_dir) {
  1636. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  1637. }
  1638. void SSL_set_info_callback(SSL *ssl,
  1639. void (*cb)(const SSL *ssl, int type, int value)) {
  1640. ssl->info_callback = cb;
  1641. }
  1642. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1643. int value) {
  1644. return ssl->info_callback;
  1645. }
  1646. int SSL_state(const SSL *ssl) { return ssl->state; }
  1647. void SSL_set_state(SSL *ssl, int state) { }
  1648. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1649. if (len <= 0) {
  1650. return NULL;
  1651. }
  1652. buf[0] = '\0';
  1653. return buf;
  1654. }
  1655. void SSL_set_verify_result(SSL *ssl, long result) {
  1656. ssl->verify_result = result;
  1657. }
  1658. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  1659. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1660. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1661. int index;
  1662. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1663. dup_func, free_func)) {
  1664. return -1;
  1665. }
  1666. return index;
  1667. }
  1668. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1669. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1670. }
  1671. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1672. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1673. }
  1674. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1675. CRYPTO_EX_dup *dup_func,
  1676. CRYPTO_EX_free *free_func) {
  1677. int index;
  1678. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1679. dup_func, free_func)) {
  1680. return -1;
  1681. }
  1682. return index;
  1683. }
  1684. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1685. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1686. }
  1687. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1688. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1689. }
  1690. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1691. return ctx->cert_store;
  1692. }
  1693. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1694. X509_STORE_free(ctx->cert_store);
  1695. ctx->cert_store = store;
  1696. }
  1697. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1698. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1699. RSA *(*cb)(SSL *ssl, int is_export,
  1700. int keylength)) {
  1701. }
  1702. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1703. int keylength)) {
  1704. }
  1705. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1706. DH *(*callback)(SSL *ssl, int is_export,
  1707. int keylength)) {
  1708. ctx->cert->dh_tmp_cb = callback;
  1709. }
  1710. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1711. int keylength)) {
  1712. ssl->cert->dh_tmp_cb = callback;
  1713. }
  1714. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1715. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1716. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1717. return 0;
  1718. }
  1719. OPENSSL_free(ctx->psk_identity_hint);
  1720. if (identity_hint != NULL) {
  1721. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1722. if (ctx->psk_identity_hint == NULL) {
  1723. return 0;
  1724. }
  1725. } else {
  1726. ctx->psk_identity_hint = NULL;
  1727. }
  1728. return 1;
  1729. }
  1730. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1731. if (ssl == NULL) {
  1732. return 0;
  1733. }
  1734. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1735. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1736. return 0;
  1737. }
  1738. /* Clear currently configured hint, if any. */
  1739. OPENSSL_free(ssl->psk_identity_hint);
  1740. ssl->psk_identity_hint = NULL;
  1741. if (identity_hint != NULL) {
  1742. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1743. if (ssl->psk_identity_hint == NULL) {
  1744. return 0;
  1745. }
  1746. }
  1747. return 1;
  1748. }
  1749. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1750. if (ssl == NULL) {
  1751. return NULL;
  1752. }
  1753. return ssl->psk_identity_hint;
  1754. }
  1755. const char *SSL_get_psk_identity(const SSL *ssl) {
  1756. if (ssl == NULL || ssl->session == NULL) {
  1757. return NULL;
  1758. }
  1759. return ssl->session->psk_identity;
  1760. }
  1761. void SSL_set_psk_client_callback(
  1762. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1763. unsigned max_identity_len, uint8_t *psk,
  1764. unsigned max_psk_len)) {
  1765. ssl->psk_client_callback = cb;
  1766. }
  1767. void SSL_CTX_set_psk_client_callback(
  1768. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1769. unsigned max_identity_len, uint8_t *psk,
  1770. unsigned max_psk_len)) {
  1771. ctx->psk_client_callback = cb;
  1772. }
  1773. void SSL_set_psk_server_callback(
  1774. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1775. unsigned max_psk_len)) {
  1776. ssl->psk_server_callback = cb;
  1777. }
  1778. void SSL_CTX_set_psk_server_callback(
  1779. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1780. uint8_t *psk, unsigned max_psk_len)) {
  1781. ctx->psk_server_callback = cb;
  1782. }
  1783. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1784. void (*cb)(int write_p, int version,
  1785. int content_type, const void *buf,
  1786. size_t len, SSL *ssl, void *arg)) {
  1787. ctx->msg_callback = cb;
  1788. }
  1789. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1790. ctx->msg_callback_arg = arg;
  1791. }
  1792. void SSL_set_msg_callback(SSL *ssl,
  1793. void (*cb)(int write_p, int version, int content_type,
  1794. const void *buf, size_t len, SSL *ssl,
  1795. void *arg)) {
  1796. ssl->msg_callback = cb;
  1797. }
  1798. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1799. ssl->msg_callback_arg = arg;
  1800. }
  1801. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1802. void (*cb)(const SSL *ssl, const char *line)) {
  1803. ctx->keylog_callback = cb;
  1804. }
  1805. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1806. static const char hextable[] = "0123456789abcdef";
  1807. uint8_t *out;
  1808. size_t i;
  1809. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1810. return 0;
  1811. }
  1812. for (i = 0; i < in_len; i++) {
  1813. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1814. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1815. }
  1816. return 1;
  1817. }
  1818. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  1819. const uint8_t *encrypted_premaster,
  1820. size_t encrypted_premaster_len,
  1821. const uint8_t *premaster,
  1822. size_t premaster_len) {
  1823. if (ssl->ctx->keylog_callback == NULL) {
  1824. return 1;
  1825. }
  1826. if (encrypted_premaster_len < 8) {
  1827. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1828. return 0;
  1829. }
  1830. CBB cbb;
  1831. uint8_t *out;
  1832. size_t out_len;
  1833. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
  1834. !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  1835. /* Only the first 8 bytes of the encrypted premaster secret are
  1836. * logged. */
  1837. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  1838. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1839. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  1840. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1841. !CBB_finish(&cbb, &out, &out_len)) {
  1842. CBB_cleanup(&cbb);
  1843. return 0;
  1844. }
  1845. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1846. OPENSSL_free(out);
  1847. return 1;
  1848. }
  1849. int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
  1850. size_t client_random_len, const uint8_t *master,
  1851. size_t master_len) {
  1852. if (ssl->ctx->keylog_callback == NULL) {
  1853. return 1;
  1854. }
  1855. if (client_random_len != 32) {
  1856. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1857. return 0;
  1858. }
  1859. CBB cbb;
  1860. uint8_t *out;
  1861. size_t out_len;
  1862. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
  1863. !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  1864. !cbb_add_hex(&cbb, client_random, 32) ||
  1865. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1866. !cbb_add_hex(&cbb, master, master_len) ||
  1867. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1868. !CBB_finish(&cbb, &out, &out_len)) {
  1869. CBB_cleanup(&cbb);
  1870. return 0;
  1871. }
  1872. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1873. OPENSSL_free(out);
  1874. return 1;
  1875. }
  1876. int SSL_is_init_finished(const SSL *ssl) {
  1877. return ssl->state == SSL_ST_OK;
  1878. }
  1879. int SSL_in_init(const SSL *ssl) {
  1880. return (ssl->state & SSL_ST_INIT) != 0;
  1881. }
  1882. int SSL_in_false_start(const SSL *ssl) {
  1883. return ssl->s3->tmp.in_false_start;
  1884. }
  1885. int SSL_cutthrough_complete(const SSL *ssl) {
  1886. return SSL_in_false_start(ssl);
  1887. }
  1888. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1889. size_t *ssl_session_size) {
  1890. *ssl_size = sizeof(SSL);
  1891. *ssl_ctx_size = sizeof(SSL_CTX);
  1892. *ssl_session_size = sizeof(SSL_SESSION);
  1893. }
  1894. int ssl3_can_false_start(const SSL *ssl) {
  1895. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  1896. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1897. return !SSL_IS_DTLS(ssl) &&
  1898. SSL_version(ssl) >= TLS1_2_VERSION &&
  1899. (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
  1900. cipher != NULL &&
  1901. cipher->algorithm_mkey == SSL_kECDHE &&
  1902. cipher->algorithm_mac == SSL_AEAD;
  1903. }
  1904. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  1905. switch (version) {
  1906. case SSL3_VERSION:
  1907. return &SSLv3_enc_data;
  1908. case TLS1_VERSION:
  1909. case TLS1_1_VERSION:
  1910. case TLS1_2_VERSION:
  1911. case DTLS1_VERSION:
  1912. case DTLS1_2_VERSION:
  1913. return &TLSv1_enc_data;
  1914. default:
  1915. return NULL;
  1916. }
  1917. }
  1918. uint16_t ssl3_get_max_server_version(const SSL *ssl) {
  1919. uint16_t max_version;
  1920. if (SSL_IS_DTLS(ssl)) {
  1921. max_version = (ssl->max_version != 0) ? ssl->max_version : DTLS1_2_VERSION;
  1922. if (!(ssl->options & SSL_OP_NO_DTLSv1_2) &&
  1923. DTLS1_2_VERSION >= max_version) {
  1924. return DTLS1_2_VERSION;
  1925. }
  1926. if (!(ssl->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  1927. return DTLS1_VERSION;
  1928. }
  1929. return 0;
  1930. }
  1931. max_version = (ssl->max_version != 0) ? ssl->max_version : TLS1_2_VERSION;
  1932. if (!(ssl->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  1933. return TLS1_2_VERSION;
  1934. }
  1935. if (!(ssl->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  1936. return TLS1_1_VERSION;
  1937. }
  1938. if (!(ssl->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  1939. return TLS1_VERSION;
  1940. }
  1941. if (!(ssl->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  1942. return SSL3_VERSION;
  1943. }
  1944. return 0;
  1945. }
  1946. uint16_t ssl3_get_mutual_version(SSL *ssl, uint16_t client_version) {
  1947. uint16_t version = 0;
  1948. if (SSL_IS_DTLS(ssl)) {
  1949. /* Clamp client_version to max_version. */
  1950. if (ssl->max_version != 0 && client_version < ssl->max_version) {
  1951. client_version = ssl->max_version;
  1952. }
  1953. if (client_version <= DTLS1_2_VERSION &&
  1954. !(ssl->options & SSL_OP_NO_DTLSv1_2)) {
  1955. version = DTLS1_2_VERSION;
  1956. } else if (client_version <= DTLS1_VERSION &&
  1957. !(ssl->options & SSL_OP_NO_DTLSv1)) {
  1958. version = DTLS1_VERSION;
  1959. }
  1960. /* Check against min_version. */
  1961. if (version != 0 && ssl->min_version != 0 && version > ssl->min_version) {
  1962. return 0;
  1963. }
  1964. return version;
  1965. } else {
  1966. /* Clamp client_version to max_version. */
  1967. if (ssl->max_version != 0 && client_version > ssl->max_version) {
  1968. client_version = ssl->max_version;
  1969. }
  1970. if (client_version >= TLS1_2_VERSION &&
  1971. !(ssl->options & SSL_OP_NO_TLSv1_2)) {
  1972. version = TLS1_2_VERSION;
  1973. } else if (client_version >= TLS1_1_VERSION &&
  1974. !(ssl->options & SSL_OP_NO_TLSv1_1)) {
  1975. version = TLS1_1_VERSION;
  1976. } else if (client_version >= TLS1_VERSION &&
  1977. !(ssl->options & SSL_OP_NO_TLSv1)) {
  1978. version = TLS1_VERSION;
  1979. } else if (client_version >= SSL3_VERSION &&
  1980. !(ssl->options & SSL_OP_NO_SSLv3)) {
  1981. version = SSL3_VERSION;
  1982. }
  1983. /* Check against min_version. */
  1984. if (version != 0 && ssl->min_version != 0 && version < ssl->min_version) {
  1985. return 0;
  1986. }
  1987. return version;
  1988. }
  1989. }
  1990. uint16_t ssl3_get_max_client_version(SSL *ssl) {
  1991. uint32_t options = ssl->options;
  1992. uint16_t version = 0;
  1993. /* OpenSSL's API for controlling versions entails blacklisting individual
  1994. * protocols. This has two problems. First, on the client, the protocol can
  1995. * only express a contiguous range of versions. Second, a library consumer
  1996. * trying to set a maximum version cannot disable protocol versions that get
  1997. * added in a future version of the library.
  1998. *
  1999. * To account for both of these, OpenSSL interprets the client-side bitmask
  2000. * as a min/max range by picking the lowest contiguous non-empty range of
  2001. * enabled protocols. Note that this means it is impossible to set a maximum
  2002. * version of TLS 1.2 in a future-proof way.
  2003. *
  2004. * By this scheme, the maximum version is the lowest version V such that V is
  2005. * enabled and V+1 is disabled or unimplemented. */
  2006. if (SSL_IS_DTLS(ssl)) {
  2007. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  2008. version = DTLS1_2_VERSION;
  2009. }
  2010. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  2011. version = DTLS1_VERSION;
  2012. }
  2013. if (ssl->max_version != 0 && version < ssl->max_version) {
  2014. version = ssl->max_version;
  2015. }
  2016. } else {
  2017. if (!(options & SSL_OP_NO_TLSv1_2)) {
  2018. version = TLS1_2_VERSION;
  2019. }
  2020. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  2021. version = TLS1_1_VERSION;
  2022. }
  2023. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  2024. version = TLS1_VERSION;
  2025. }
  2026. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  2027. version = SSL3_VERSION;
  2028. }
  2029. if (ssl->max_version != 0 && version > ssl->max_version) {
  2030. version = ssl->max_version;
  2031. }
  2032. }
  2033. return version;
  2034. }
  2035. int ssl3_is_version_enabled(SSL *ssl, uint16_t version) {
  2036. if (SSL_IS_DTLS(ssl)) {
  2037. if (ssl->max_version != 0 && version < ssl->max_version) {
  2038. return 0;
  2039. }
  2040. if (ssl->min_version != 0 && version > ssl->min_version) {
  2041. return 0;
  2042. }
  2043. switch (version) {
  2044. case DTLS1_VERSION:
  2045. return !(ssl->options & SSL_OP_NO_DTLSv1);
  2046. case DTLS1_2_VERSION:
  2047. return !(ssl->options & SSL_OP_NO_DTLSv1_2);
  2048. default:
  2049. return 0;
  2050. }
  2051. } else {
  2052. if (ssl->max_version != 0 && version > ssl->max_version) {
  2053. return 0;
  2054. }
  2055. if (ssl->min_version != 0 && version < ssl->min_version) {
  2056. return 0;
  2057. }
  2058. switch (version) {
  2059. case SSL3_VERSION:
  2060. return !(ssl->options & SSL_OP_NO_SSLv3);
  2061. case TLS1_VERSION:
  2062. return !(ssl->options & SSL_OP_NO_TLSv1);
  2063. case TLS1_1_VERSION:
  2064. return !(ssl->options & SSL_OP_NO_TLSv1_1);
  2065. case TLS1_2_VERSION:
  2066. return !(ssl->options & SSL_OP_NO_TLSv1_2);
  2067. default:
  2068. return 0;
  2069. }
  2070. }
  2071. }
  2072. uint16_t ssl3_version_from_wire(const SSL *ssl, uint16_t wire_version) {
  2073. if (!SSL_IS_DTLS(ssl)) {
  2074. return wire_version;
  2075. }
  2076. uint16_t tls_version = ~wire_version;
  2077. uint16_t version = tls_version + 0x0201;
  2078. /* If either component overflowed, clamp it so comparisons still work. */
  2079. if ((version >> 8) < (tls_version >> 8)) {
  2080. version = 0xff00 | (version & 0xff);
  2081. }
  2082. if ((version & 0xff) < (tls_version & 0xff)) {
  2083. version = (version & 0xff00) | 0xff;
  2084. }
  2085. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2086. if (version == TLS1_VERSION) {
  2087. version = TLS1_1_VERSION;
  2088. }
  2089. return version;
  2090. }
  2091. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2092. assert(ssl->s3->have_version);
  2093. return ssl3_version_from_wire(ssl, ssl->version);
  2094. }
  2095. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2096. int SSL_is_server(SSL *ssl) { return ssl->server; }
  2097. void SSL_CTX_set_select_certificate_cb(
  2098. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2099. ctx->select_certificate_cb = cb;
  2100. }
  2101. void SSL_CTX_set_dos_protection_cb(
  2102. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2103. ctx->dos_protection_cb = cb;
  2104. }
  2105. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2106. ssl->renegotiate_mode = mode;
  2107. }
  2108. void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
  2109. SSL_set_renegotiate_mode(
  2110. ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
  2111. }
  2112. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2113. const RC4_KEY **write_key) {
  2114. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2115. return 0;
  2116. }
  2117. return EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_read_ctx->ctx, read_key) &&
  2118. EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_write_ctx->ctx, write_key);
  2119. }
  2120. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2121. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2122. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2123. return 0;
  2124. }
  2125. size_t write_iv_len;
  2126. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2127. out_iv_len) ||
  2128. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2129. &write_iv_len) ||
  2130. *out_iv_len != write_iv_len) {
  2131. return 0;
  2132. }
  2133. return 1;
  2134. }
  2135. static uint64_t be_to_u64(const uint8_t in[8]) {
  2136. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2137. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2138. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2139. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2140. }
  2141. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2142. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2143. if (SSL_IS_DTLS(ssl)) {
  2144. /* max_seq_num already includes the epoch. */
  2145. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2146. return ssl->d1->bitmap.max_seq_num;
  2147. }
  2148. return be_to_u64(ssl->s3->read_sequence);
  2149. }
  2150. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2151. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2152. if (SSL_IS_DTLS(ssl)) {
  2153. assert((ret >> 48) == 0);
  2154. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2155. }
  2156. return ret;
  2157. }
  2158. uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
  2159. return ssl->s3->tmp.server_key_exchange_hash;
  2160. }
  2161. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2162. if (max_out == 0) {
  2163. return sizeof(ssl->s3->client_random);
  2164. }
  2165. if (max_out > sizeof(ssl->s3->client_random)) {
  2166. max_out = sizeof(ssl->s3->client_random);
  2167. }
  2168. memcpy(out, ssl->s3->client_random, max_out);
  2169. return max_out;
  2170. }
  2171. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2172. if (max_out == 0) {
  2173. return sizeof(ssl->s3->server_random);
  2174. }
  2175. if (max_out > sizeof(ssl->s3->server_random)) {
  2176. max_out = sizeof(ssl->s3->server_random);
  2177. }
  2178. memcpy(out, ssl->s3->server_random, max_out);
  2179. return max_out;
  2180. }
  2181. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2182. if (!SSL_in_init(ssl)) {
  2183. return NULL;
  2184. }
  2185. return ssl->s3->tmp.new_cipher;
  2186. }
  2187. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2188. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2189. }
  2190. int SSL_clear(SSL *ssl) {
  2191. if (ssl->method == NULL) {
  2192. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
  2193. return 0;
  2194. }
  2195. if (ssl_clear_bad_session(ssl)) {
  2196. SSL_SESSION_free(ssl->session);
  2197. ssl->session = NULL;
  2198. }
  2199. ssl->hit = 0;
  2200. ssl->shutdown = 0;
  2201. /* SSL_clear may be called before or after the |ssl| is initialized in either
  2202. * accept or connect state. In the latter case, SSL_clear should preserve the
  2203. * half and reset |ssl->state| accordingly. */
  2204. if (ssl->handshake_func != NULL) {
  2205. if (ssl->server) {
  2206. SSL_set_accept_state(ssl);
  2207. } else {
  2208. SSL_set_connect_state(ssl);
  2209. }
  2210. } else {
  2211. assert(ssl->state == 0);
  2212. }
  2213. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2214. * |SSL_clear| because it is per-connection state rather than configuration
  2215. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2216. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2217. * |ssl3_new|. */
  2218. ssl->rwstate = SSL_NOTHING;
  2219. BUF_MEM_free(ssl->init_buf);
  2220. ssl->init_buf = NULL;
  2221. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2222. * clear) and connection-specific state (gets reset).
  2223. *
  2224. * TODO(davidben): Avoid this. */
  2225. unsigned mtu = 0;
  2226. if (ssl->d1 != NULL) {
  2227. mtu = ssl->d1->mtu;
  2228. }
  2229. ssl->method->ssl_free(ssl);
  2230. if (!ssl->method->ssl_new(ssl)) {
  2231. return 0;
  2232. }
  2233. if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2234. ssl->d1->mtu = mtu;
  2235. }
  2236. ssl->client_version = ssl->version;
  2237. return 1;
  2238. }
  2239. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2240. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2241. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2242. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2243. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2244. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2245. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2246. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2247. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2248. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2249. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2250. void ERR_load_SSL_strings(void) {}
  2251. void SSL_load_error_strings(void) {}