選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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