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

2723 Zeilen
78 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 <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/dh.h>
  147. #include <openssl/err.h>
  148. #include <openssl/lhash.h>
  149. #include <openssl/mem.h>
  150. #include <openssl/rand.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. #if defined(OPENSSL_WINDOWS)
  154. #include <sys/timeb.h>
  155. #else
  156. #include <sys/socket.h>
  157. #include <sys/time.h>
  158. #endif
  159. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  160. * to avoid downstream churn. */
  161. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  162. /* The following errors are no longer emitted, but are used in nginx without
  163. * #ifdefs. */
  164. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  165. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  166. /* Some error codes are special. Ensure the make_errors.go script never
  167. * regresses this. */
  168. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. ssl_alert_reason_code_mismatch);
  171. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  172. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  173. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  174. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  175. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  176. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  177. int SSL_library_init(void) {
  178. CRYPTO_library_init();
  179. return 1;
  180. }
  181. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  182. const uint8_t *session_id = sess->session_id;
  183. uint8_t tmp_storage[sizeof(uint32_t)];
  184. if (sess->session_id_length < sizeof(tmp_storage)) {
  185. OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
  186. OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  187. session_id = tmp_storage;
  188. }
  189. uint32_t hash =
  190. ((uint32_t)session_id[0]) |
  191. ((uint32_t)session_id[1] << 8) |
  192. ((uint32_t)session_id[2] << 16) |
  193. ((uint32_t)session_id[3] << 24);
  194. return hash;
  195. }
  196. /* NB: If this function (or indeed the hash function which uses a sort of
  197. * coarser function than this one) is changed, ensure
  198. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  199. * able to construct an SSL_SESSION that will collide with any existing session
  200. * with a matching session ID. */
  201. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  202. if (a->ssl_version != b->ssl_version) {
  203. return 1;
  204. }
  205. if (a->session_id_length != b->session_id_length) {
  206. return 1;
  207. }
  208. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  209. }
  210. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  211. SSL_CTX *ret = NULL;
  212. if (method == NULL) {
  213. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  214. return NULL;
  215. }
  216. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  217. if (ret == NULL) {
  218. goto err;
  219. }
  220. OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
  221. ret->method = method->method;
  222. ret->x509_method = method->x509_method;
  223. CRYPTO_MUTEX_init(&ret->lock);
  224. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  225. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  226. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  227. ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
  228. ret->references = 1;
  229. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  230. ret->verify_mode = SSL_VERIFY_NONE;
  231. ret->cert = ssl_cert_new(method->x509_method);
  232. if (ret->cert == NULL) {
  233. goto err;
  234. }
  235. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  236. if (ret->sessions == NULL) {
  237. goto err;
  238. }
  239. if (!ret->x509_method->ssl_ctx_new(ret)) {
  240. goto err;
  241. }
  242. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  243. SSL_DEFAULT_CIPHER_LIST, 1 /* strict */);
  244. if (ret->cipher_list == NULL ||
  245. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  246. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  247. goto err2;
  248. }
  249. ret->client_CA = sk_CRYPTO_BUFFER_new_null();
  250. if (ret->client_CA == NULL) {
  251. goto err;
  252. }
  253. CRYPTO_new_ex_data(&ret->ex_data);
  254. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  255. /* Setup RFC4507 ticket keys */
  256. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  257. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  258. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  259. ret->options |= SSL_OP_NO_TICKET;
  260. }
  261. /* Disable the auto-chaining feature by default. Once this has stuck without
  262. * problems, the feature will be removed entirely. */
  263. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  264. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  265. * uses of SSL_METHOD. */
  266. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  267. !SSL_CTX_set_min_proto_version(ret, method->version)) {
  268. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  269. goto err2;
  270. }
  271. return ret;
  272. err:
  273. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  274. err2:
  275. SSL_CTX_free(ret);
  276. return NULL;
  277. }
  278. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  279. CRYPTO_refcount_inc(&ctx->references);
  280. return 1;
  281. }
  282. void SSL_CTX_free(SSL_CTX *ctx) {
  283. if (ctx == NULL ||
  284. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  285. return;
  286. }
  287. /* Free internal session cache. However: the remove_cb() may reference the
  288. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  289. * sessions were flushed. As the ex_data handling routines might also touch
  290. * the session cache, the most secure solution seems to be: empty (flush) the
  291. * cache, then free ex_data, then finally free the cache. (See ticket
  292. * [openssl.org #212].) */
  293. SSL_CTX_flush_sessions(ctx, 0);
  294. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  295. CRYPTO_MUTEX_cleanup(&ctx->lock);
  296. lh_SSL_SESSION_free(ctx->sessions);
  297. ssl_cipher_preference_list_free(ctx->cipher_list);
  298. ssl_cert_free(ctx->cert);
  299. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  300. SSL_CUSTOM_EXTENSION_free);
  301. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  302. SSL_CUSTOM_EXTENSION_free);
  303. sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
  304. ctx->x509_method->ssl_ctx_free(ctx);
  305. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  306. OPENSSL_free(ctx->psk_identity_hint);
  307. OPENSSL_free(ctx->supported_group_list);
  308. OPENSSL_free(ctx->alpn_client_proto_list);
  309. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  310. OPENSSL_free(ctx);
  311. }
  312. SSL *SSL_new(SSL_CTX *ctx) {
  313. if (ctx == NULL) {
  314. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  315. return NULL;
  316. }
  317. if (ctx->method == NULL) {
  318. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  319. return NULL;
  320. }
  321. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  322. if (ssl == NULL) {
  323. goto err;
  324. }
  325. OPENSSL_memset(ssl, 0, sizeof(SSL));
  326. ssl->min_version = ctx->min_version;
  327. ssl->max_version = ctx->max_version;
  328. /* RFC 6347 states that implementations SHOULD use an initial timer value of
  329. * 1 second. */
  330. ssl->initial_timeout_duration_ms = 1000;
  331. ssl->options = ctx->options;
  332. ssl->mode = ctx->mode;
  333. ssl->max_cert_list = ctx->max_cert_list;
  334. ssl->cert = ssl_cert_dup(ctx->cert);
  335. if (ssl->cert == NULL) {
  336. goto err;
  337. }
  338. ssl->msg_callback = ctx->msg_callback;
  339. ssl->msg_callback_arg = ctx->msg_callback_arg;
  340. ssl->verify_mode = ctx->verify_mode;
  341. ssl->verify_callback = ctx->default_verify_callback;
  342. ssl->retain_only_sha256_of_client_certs =
  343. ctx->retain_only_sha256_of_client_certs;
  344. ssl->quiet_shutdown = ctx->quiet_shutdown;
  345. ssl->max_send_fragment = ctx->max_send_fragment;
  346. SSL_CTX_up_ref(ctx);
  347. ssl->ctx = ctx;
  348. SSL_CTX_up_ref(ctx);
  349. ssl->initial_ctx = ctx;
  350. if (!ssl->ctx->x509_method->ssl_new(ssl)) {
  351. goto err;
  352. }
  353. if (ctx->supported_group_list) {
  354. ssl->supported_group_list = BUF_memdup(ctx->supported_group_list,
  355. ctx->supported_group_list_len * 2);
  356. if (!ssl->supported_group_list) {
  357. goto err;
  358. }
  359. ssl->supported_group_list_len = ctx->supported_group_list_len;
  360. }
  361. if (ctx->alpn_client_proto_list) {
  362. ssl->alpn_client_proto_list = BUF_memdup(ctx->alpn_client_proto_list,
  363. ctx->alpn_client_proto_list_len);
  364. if (ssl->alpn_client_proto_list == NULL) {
  365. goto err;
  366. }
  367. ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
  368. }
  369. ssl->method = ctx->method;
  370. if (!ssl->method->ssl_new(ssl)) {
  371. goto err;
  372. }
  373. ssl->rwstate = SSL_NOTHING;
  374. CRYPTO_new_ex_data(&ssl->ex_data);
  375. ssl->psk_identity_hint = NULL;
  376. if (ctx->psk_identity_hint) {
  377. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  378. if (ssl->psk_identity_hint == NULL) {
  379. goto err;
  380. }
  381. }
  382. ssl->psk_client_callback = ctx->psk_client_callback;
  383. ssl->psk_server_callback = ctx->psk_server_callback;
  384. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  385. if (ctx->tlsext_channel_id_private) {
  386. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  387. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  388. }
  389. ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
  390. ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  391. return ssl;
  392. err:
  393. SSL_free(ssl);
  394. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  395. return NULL;
  396. }
  397. void SSL_free(SSL *ssl) {
  398. if (ssl == NULL) {
  399. return;
  400. }
  401. ssl->ctx->x509_method->ssl_free(ssl);
  402. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  403. BIO_free_all(ssl->rbio);
  404. BIO_free_all(ssl->wbio);
  405. BUF_MEM_free(ssl->init_buf);
  406. /* add extra stuff */
  407. ssl_cipher_preference_list_free(ssl->cipher_list);
  408. SSL_SESSION_free(ssl->session);
  409. ssl_cert_free(ssl->cert);
  410. OPENSSL_free(ssl->tlsext_hostname);
  411. SSL_CTX_free(ssl->initial_ctx);
  412. OPENSSL_free(ssl->supported_group_list);
  413. OPENSSL_free(ssl->alpn_client_proto_list);
  414. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  415. OPENSSL_free(ssl->psk_identity_hint);
  416. sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
  417. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  418. if (ssl->method != NULL) {
  419. ssl->method->ssl_free(ssl);
  420. }
  421. SSL_CTX_free(ssl->ctx);
  422. OPENSSL_free(ssl);
  423. }
  424. void SSL_set_connect_state(SSL *ssl) {
  425. ssl->server = 0;
  426. ssl->handshake_func = ssl3_connect;
  427. }
  428. void SSL_set_accept_state(SSL *ssl) {
  429. ssl->server = 1;
  430. ssl->handshake_func = ssl3_accept;
  431. }
  432. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  433. BIO_free_all(ssl->rbio);
  434. ssl->rbio = rbio;
  435. }
  436. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  437. BIO_free_all(ssl->wbio);
  438. ssl->wbio = wbio;
  439. }
  440. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  441. /* For historical reasons, this function has many different cases in ownership
  442. * handling. */
  443. /* If nothing has changed, do nothing */
  444. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  445. return;
  446. }
  447. /* If the two arguments are equal, one fewer reference is granted than
  448. * taken. */
  449. if (rbio != NULL && rbio == wbio) {
  450. BIO_up_ref(rbio);
  451. }
  452. /* If only the wbio is changed, adopt only one reference. */
  453. if (rbio == SSL_get_rbio(ssl)) {
  454. SSL_set0_wbio(ssl, wbio);
  455. return;
  456. }
  457. /* There is an asymmetry here for historical reasons. If only the rbio is
  458. * changed AND the rbio and wbio were originally different, then we only adopt
  459. * one reference. */
  460. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  461. SSL_set0_rbio(ssl, rbio);
  462. return;
  463. }
  464. /* Otherwise, adopt both references. */
  465. SSL_set0_rbio(ssl, rbio);
  466. SSL_set0_wbio(ssl, wbio);
  467. }
  468. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  469. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  470. void ssl_reset_error_state(SSL *ssl) {
  471. /* Functions which use |SSL_get_error| must reset I/O and error state on
  472. * entry. */
  473. ssl->rwstate = SSL_NOTHING;
  474. ERR_clear_error();
  475. ERR_clear_system_error();
  476. }
  477. int SSL_do_handshake(SSL *ssl) {
  478. ssl_reset_error_state(ssl);
  479. if (ssl->handshake_func == NULL) {
  480. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  481. return -1;
  482. }
  483. if (!SSL_in_init(ssl)) {
  484. return 1;
  485. }
  486. if (ssl->s3->hs == NULL) {
  487. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  488. return -1;
  489. }
  490. /* Run the handshake. */
  491. assert(ssl->s3->hs != NULL);
  492. int ret = ssl->handshake_func(ssl->s3->hs);
  493. if (ret <= 0) {
  494. return ret;
  495. }
  496. /* Destroy the handshake object if the handshake has completely finished. */
  497. if (!SSL_in_init(ssl)) {
  498. ssl_handshake_free(ssl->s3->hs);
  499. ssl->s3->hs = NULL;
  500. }
  501. return 1;
  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_do_renegotiate(SSL *ssl) {
  518. /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  519. * removed entirely in the future and requires retaining more data for
  520. * renegotiation_info. */
  521. if (ssl->server || ssl->version == SSL3_VERSION) {
  522. goto no_renegotiation;
  523. }
  524. if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
  525. ssl->init_num != 0) {
  526. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  527. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  528. return 0;
  529. }
  530. switch (ssl->renegotiate_mode) {
  531. case ssl_renegotiate_ignore:
  532. /* Ignore the HelloRequest. */
  533. return 1;
  534. case ssl_renegotiate_once:
  535. if (ssl->s3->total_renegotiations != 0) {
  536. goto no_renegotiation;
  537. }
  538. break;
  539. case ssl_renegotiate_never:
  540. goto no_renegotiation;
  541. case ssl_renegotiate_freely:
  542. break;
  543. }
  544. /* Renegotiation is only supported at quiescent points in the application
  545. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  546. * the record-layer be idle and avoid complexities of sending a handshake
  547. * record while an application_data record is being written. */
  548. if (ssl_write_buffer_is_pending(ssl)) {
  549. goto no_renegotiation;
  550. }
  551. /* Begin a new handshake. */
  552. if (ssl->s3->hs != NULL) {
  553. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  554. return 0;
  555. }
  556. ssl->s3->hs = ssl_handshake_new(ssl);
  557. if (ssl->s3->hs == NULL) {
  558. return 0;
  559. }
  560. ssl->s3->total_renegotiations++;
  561. return 1;
  562. no_renegotiation:
  563. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  564. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  565. return 0;
  566. }
  567. static int ssl_do_post_handshake(SSL *ssl) {
  568. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  569. return ssl_do_renegotiate(ssl);
  570. }
  571. return tls13_post_handshake(ssl);
  572. }
  573. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  574. ssl_reset_error_state(ssl);
  575. if (ssl->handshake_func == NULL) {
  576. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  577. return -1;
  578. }
  579. for (;;) {
  580. /* Complete the current handshake, if any. False Start will cause
  581. * |SSL_do_handshake| to return mid-handshake, so this may require multiple
  582. * iterations. */
  583. while (SSL_in_init(ssl)) {
  584. int ret = SSL_do_handshake(ssl);
  585. if (ret < 0) {
  586. return ret;
  587. }
  588. if (ret == 0) {
  589. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  590. return -1;
  591. }
  592. }
  593. int got_handshake;
  594. int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
  595. if (ret > 0 || !got_handshake) {
  596. ssl->s3->key_update_count = 0;
  597. return ret;
  598. }
  599. /* Handle the post-handshake message and try again. */
  600. if (!ssl_do_post_handshake(ssl)) {
  601. return -1;
  602. }
  603. ssl->method->release_current_message(ssl, 1 /* free buffer */);
  604. }
  605. }
  606. int SSL_read(SSL *ssl, void *buf, int num) {
  607. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  608. }
  609. int SSL_peek(SSL *ssl, void *buf, int num) {
  610. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  611. }
  612. int SSL_write(SSL *ssl, const void *buf, int num) {
  613. ssl_reset_error_state(ssl);
  614. if (ssl->handshake_func == NULL) {
  615. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  616. return -1;
  617. }
  618. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  619. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  620. return -1;
  621. }
  622. /* If necessary, complete the handshake implicitly. */
  623. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  624. int ret = SSL_do_handshake(ssl);
  625. if (ret < 0) {
  626. return ret;
  627. }
  628. if (ret == 0) {
  629. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  630. return -1;
  631. }
  632. }
  633. return ssl->method->write_app_data(ssl, buf, num);
  634. }
  635. int SSL_shutdown(SSL *ssl) {
  636. ssl_reset_error_state(ssl);
  637. if (ssl->handshake_func == NULL) {
  638. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  639. return -1;
  640. }
  641. /* If we are in the middle of a handshake, silently succeed. Consumers often
  642. * call this function before |SSL_free|, whether the handshake succeeded or
  643. * not. We assume the caller has already handled failed handshakes. */
  644. if (SSL_in_init(ssl)) {
  645. return 1;
  646. }
  647. if (ssl->quiet_shutdown) {
  648. /* Do nothing if configured not to send a close_notify. */
  649. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  650. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  651. return 1;
  652. }
  653. /* This function completes in two stages. It sends a close_notify and then it
  654. * waits for a close_notify to come in. Perform exactly one action and return
  655. * whether or not it succeeds. */
  656. if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
  657. /* Send a close_notify. */
  658. if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  659. return -1;
  660. }
  661. } else if (ssl->s3->alert_dispatch) {
  662. /* Finish sending the close_notify. */
  663. if (ssl->method->dispatch_alert(ssl) <= 0) {
  664. return -1;
  665. }
  666. } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  667. /* Wait for the peer's close_notify. */
  668. ssl->method->read_close_notify(ssl);
  669. if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  670. return -1;
  671. }
  672. }
  673. /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
  674. return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
  675. }
  676. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  677. if (ssl->s3->alert_dispatch) {
  678. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  679. ssl->s3->send_alert[1] != alert) {
  680. /* We are already attempting to write a different alert. */
  681. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  682. return -1;
  683. }
  684. return ssl->method->dispatch_alert(ssl);
  685. }
  686. return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  687. }
  688. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  689. ctx->enable_early_data = !!enabled;
  690. }
  691. static int bio_retry_reason_to_error(int reason) {
  692. switch (reason) {
  693. case BIO_RR_CONNECT:
  694. return SSL_ERROR_WANT_CONNECT;
  695. case BIO_RR_ACCEPT:
  696. return SSL_ERROR_WANT_ACCEPT;
  697. default:
  698. return SSL_ERROR_SYSCALL;
  699. }
  700. }
  701. int SSL_get_error(const SSL *ssl, int ret_code) {
  702. if (ret_code > 0) {
  703. return SSL_ERROR_NONE;
  704. }
  705. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  706. * where we do encode the error */
  707. uint32_t err = ERR_peek_error();
  708. if (err != 0) {
  709. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  710. return SSL_ERROR_SYSCALL;
  711. }
  712. return SSL_ERROR_SSL;
  713. }
  714. if (ret_code == 0) {
  715. if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
  716. return SSL_ERROR_ZERO_RETURN;
  717. }
  718. /* An EOF was observed which violates the protocol, and the underlying
  719. * transport does not participate in the error queue. Bubble up to the
  720. * caller. */
  721. return SSL_ERROR_SYSCALL;
  722. }
  723. switch (ssl->rwstate) {
  724. case SSL_PENDING_SESSION:
  725. return SSL_ERROR_PENDING_SESSION;
  726. case SSL_CERTIFICATE_SELECTION_PENDING:
  727. return SSL_ERROR_PENDING_CERTIFICATE;
  728. case SSL_READING: {
  729. BIO *bio = SSL_get_rbio(ssl);
  730. if (BIO_should_read(bio)) {
  731. return SSL_ERROR_WANT_READ;
  732. }
  733. if (BIO_should_write(bio)) {
  734. /* TODO(davidben): OpenSSL historically checked for writes on the read
  735. * BIO. Can this be removed? */
  736. return SSL_ERROR_WANT_WRITE;
  737. }
  738. if (BIO_should_io_special(bio)) {
  739. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  740. }
  741. break;
  742. }
  743. case SSL_WRITING: {
  744. BIO *bio = SSL_get_wbio(ssl);
  745. if (BIO_should_write(bio)) {
  746. return SSL_ERROR_WANT_WRITE;
  747. }
  748. if (BIO_should_read(bio)) {
  749. /* TODO(davidben): OpenSSL historically checked for reads on the write
  750. * BIO. Can this be removed? */
  751. return SSL_ERROR_WANT_READ;
  752. }
  753. if (BIO_should_io_special(bio)) {
  754. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  755. }
  756. break;
  757. }
  758. case SSL_X509_LOOKUP:
  759. return SSL_ERROR_WANT_X509_LOOKUP;
  760. case SSL_CHANNEL_ID_LOOKUP:
  761. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  762. case SSL_PRIVATE_KEY_OPERATION:
  763. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  764. }
  765. return SSL_ERROR_SYSCALL;
  766. }
  767. static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  768. uint16_t version) {
  769. if (version == 0) {
  770. *out = method->min_version;
  771. return 1;
  772. }
  773. if (version == TLS1_3_VERSION) {
  774. version = TLS1_3_DRAFT_VERSION;
  775. }
  776. return method->version_from_wire(out, version);
  777. }
  778. static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  779. uint16_t version) {
  780. if (version == 0) {
  781. *out = method->max_version;
  782. /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
  783. if (*out > TLS1_2_VERSION) {
  784. *out = TLS1_2_VERSION;
  785. }
  786. return 1;
  787. }
  788. if (version == TLS1_3_VERSION) {
  789. version = TLS1_3_DRAFT_VERSION;
  790. }
  791. return method->version_from_wire(out, version);
  792. }
  793. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  794. return set_min_version(ctx->method, &ctx->min_version, version);
  795. }
  796. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  797. return set_max_version(ctx->method, &ctx->max_version, version);
  798. }
  799. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  800. return set_min_version(ssl->method, &ssl->min_version, version);
  801. }
  802. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  803. return set_max_version(ssl->method, &ssl->max_version, version);
  804. }
  805. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  806. ctx->options |= options;
  807. return ctx->options;
  808. }
  809. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  810. ctx->options &= ~options;
  811. return ctx->options;
  812. }
  813. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  814. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  815. ssl->options |= options;
  816. return ssl->options;
  817. }
  818. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  819. ssl->options &= ~options;
  820. return ssl->options;
  821. }
  822. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  823. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  824. ctx->mode |= mode;
  825. return ctx->mode;
  826. }
  827. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  828. ctx->mode &= ~mode;
  829. return ctx->mode;
  830. }
  831. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  832. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  833. ssl->mode |= mode;
  834. return ssl->mode;
  835. }
  836. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  837. ssl->mode &= ~mode;
  838. return ssl->mode;
  839. }
  840. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  841. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  842. ctx->pool = pool;
  843. }
  844. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  845. size_t max_out) {
  846. /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
  847. if (!ssl->s3->initial_handshake_complete ||
  848. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  849. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  850. goto err;
  851. }
  852. /* The tls-unique value is the first Finished message in the handshake, which
  853. * is the client's in a full handshake and the server's for a resumption. See
  854. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  855. const uint8_t *finished = ssl->s3->previous_client_finished;
  856. size_t finished_len = ssl->s3->previous_client_finished_len;
  857. if (ssl->session != NULL) {
  858. /* tls-unique is broken for resumed sessions unless EMS is used. */
  859. if (!ssl->session->extended_master_secret) {
  860. goto err;
  861. }
  862. finished = ssl->s3->previous_server_finished;
  863. finished_len = ssl->s3->previous_server_finished_len;
  864. }
  865. *out_len = finished_len;
  866. if (finished_len > max_out) {
  867. *out_len = max_out;
  868. }
  869. OPENSSL_memcpy(out, finished, *out_len);
  870. return 1;
  871. err:
  872. *out_len = 0;
  873. OPENSSL_memset(out, 0, max_out);
  874. return 0;
  875. }
  876. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  877. size_t sid_ctx_len) {
  878. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  879. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  880. return 0;
  881. }
  882. OPENSSL_COMPILE_ASSERT(sizeof(cert->sid_ctx) < 256, sid_ctx_too_large);
  883. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  884. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  885. return 1;
  886. }
  887. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  888. size_t sid_ctx_len) {
  889. return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
  890. }
  891. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  892. size_t sid_ctx_len) {
  893. return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
  894. }
  895. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  896. *out_len = ssl->cert->sid_ctx_length;
  897. return ssl->cert->sid_ctx;
  898. }
  899. void ssl_cipher_preference_list_free(
  900. struct ssl_cipher_preference_list_st *cipher_list) {
  901. if (cipher_list == NULL) {
  902. return;
  903. }
  904. sk_SSL_CIPHER_free(cipher_list->ciphers);
  905. OPENSSL_free(cipher_list->in_group_flags);
  906. OPENSSL_free(cipher_list);
  907. }
  908. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  909. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  910. int SSL_get_rfd(const SSL *ssl) {
  911. int ret = -1;
  912. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  913. if (b != NULL) {
  914. BIO_get_fd(b, &ret);
  915. }
  916. return ret;
  917. }
  918. int SSL_get_wfd(const SSL *ssl) {
  919. int ret = -1;
  920. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  921. if (b != NULL) {
  922. BIO_get_fd(b, &ret);
  923. }
  924. return ret;
  925. }
  926. int SSL_set_fd(SSL *ssl, int fd) {
  927. BIO *bio = BIO_new(BIO_s_socket());
  928. if (bio == NULL) {
  929. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  930. return 0;
  931. }
  932. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  933. SSL_set_bio(ssl, bio, bio);
  934. return 1;
  935. }
  936. int SSL_set_wfd(SSL *ssl, int fd) {
  937. BIO *rbio = SSL_get_rbio(ssl);
  938. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  939. BIO_get_fd(rbio, NULL) != fd) {
  940. BIO *bio = BIO_new(BIO_s_socket());
  941. if (bio == NULL) {
  942. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  943. return 0;
  944. }
  945. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  946. SSL_set0_wbio(ssl, bio);
  947. } else {
  948. /* Copy the rbio over to the wbio. */
  949. BIO_up_ref(rbio);
  950. SSL_set0_wbio(ssl, rbio);
  951. }
  952. return 1;
  953. }
  954. int SSL_set_rfd(SSL *ssl, int fd) {
  955. BIO *wbio = SSL_get_wbio(ssl);
  956. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  957. BIO_get_fd(wbio, NULL) != fd) {
  958. BIO *bio = BIO_new(BIO_s_socket());
  959. if (bio == NULL) {
  960. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  961. return 0;
  962. }
  963. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  964. SSL_set0_rbio(ssl, bio);
  965. } else {
  966. /* Copy the wbio over to the rbio. */
  967. BIO_up_ref(wbio);
  968. SSL_set0_rbio(ssl, wbio);
  969. }
  970. return 1;
  971. }
  972. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  973. size_t in_len) {
  974. if (out_len > in_len) {
  975. out_len = in_len;
  976. }
  977. OPENSSL_memcpy(out, in, out_len);
  978. return in_len;
  979. }
  980. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  981. if (!ssl->s3->initial_handshake_complete ||
  982. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  983. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  984. return 0;
  985. }
  986. if (ssl->server) {
  987. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  988. ssl->s3->previous_server_finished_len);
  989. }
  990. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  991. ssl->s3->previous_client_finished_len);
  992. }
  993. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  994. if (!ssl->s3->initial_handshake_complete ||
  995. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  996. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  997. return 0;
  998. }
  999. if (ssl->server) {
  1000. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1001. ssl->s3->previous_client_finished_len);
  1002. }
  1003. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1004. ssl->s3->previous_server_finished_len);
  1005. }
  1006. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1007. int SSL_get_extms_support(const SSL *ssl) {
  1008. /* TLS 1.3 does not require extended master secret and always reports as
  1009. * supporting it. */
  1010. if (!ssl->s3->have_version) {
  1011. return 0;
  1012. }
  1013. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1014. return 1;
  1015. }
  1016. /* If the initial handshake completed, query the established session. */
  1017. if (ssl->s3->established_session != NULL) {
  1018. return ssl->s3->established_session->extended_master_secret;
  1019. }
  1020. /* Otherwise, query the in-progress handshake. */
  1021. if (ssl->s3->hs != NULL) {
  1022. return ssl->s3->hs->extended_master_secret;
  1023. }
  1024. assert(0);
  1025. return 0;
  1026. }
  1027. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1028. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1029. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1030. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1031. int SSL_pending(const SSL *ssl) {
  1032. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  1033. return 0;
  1034. }
  1035. return ssl->s3->rrec.length;
  1036. }
  1037. /* Fix this so it checks all the valid key/cert options */
  1038. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1039. return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
  1040. }
  1041. /* Fix this function so that it takes an optional type parameter */
  1042. int SSL_check_private_key(const SSL *ssl) {
  1043. return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
  1044. }
  1045. long SSL_get_default_timeout(const SSL *ssl) {
  1046. return SSL_DEFAULT_SESSION_TIMEOUT;
  1047. }
  1048. int SSL_renegotiate(SSL *ssl) {
  1049. /* Caller-initiated renegotiation is not supported. */
  1050. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1051. return 0;
  1052. }
  1053. int SSL_renegotiate_pending(SSL *ssl) {
  1054. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1055. }
  1056. int SSL_total_renegotiations(const SSL *ssl) {
  1057. return ssl->s3->total_renegotiations;
  1058. }
  1059. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1060. return ctx->max_cert_list;
  1061. }
  1062. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1063. if (max_cert_list > kMaxHandshakeSize) {
  1064. max_cert_list = kMaxHandshakeSize;
  1065. }
  1066. ctx->max_cert_list = (uint32_t)max_cert_list;
  1067. }
  1068. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1069. return ssl->max_cert_list;
  1070. }
  1071. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1072. if (max_cert_list > kMaxHandshakeSize) {
  1073. max_cert_list = kMaxHandshakeSize;
  1074. }
  1075. ssl->max_cert_list = (uint32_t)max_cert_list;
  1076. }
  1077. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1078. if (max_send_fragment < 512) {
  1079. max_send_fragment = 512;
  1080. }
  1081. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1082. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1083. }
  1084. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1085. return 1;
  1086. }
  1087. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1088. if (max_send_fragment < 512) {
  1089. max_send_fragment = 512;
  1090. }
  1091. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1092. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1093. }
  1094. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1095. return 1;
  1096. }
  1097. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1098. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1099. return 0;
  1100. }
  1101. ssl->d1->mtu = mtu;
  1102. return 1;
  1103. }
  1104. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1105. if (!ssl->s3->have_version) {
  1106. return 0;
  1107. }
  1108. return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1109. ssl->s3->send_connection_binding;
  1110. }
  1111. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1112. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1113. return lh_SSL_SESSION_num_items(ctx->sessions);
  1114. }
  1115. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1116. unsigned long ret = ctx->session_cache_size;
  1117. ctx->session_cache_size = size;
  1118. return ret;
  1119. }
  1120. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1121. return ctx->session_cache_size;
  1122. }
  1123. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1124. int ret = ctx->session_cache_mode;
  1125. ctx->session_cache_mode = mode;
  1126. return ret;
  1127. }
  1128. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1129. return ctx->session_cache_mode;
  1130. }
  1131. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1132. if (out == NULL) {
  1133. return 48;
  1134. }
  1135. if (len != 48) {
  1136. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1137. return 0;
  1138. }
  1139. uint8_t *out_bytes = out;
  1140. OPENSSL_memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
  1141. OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
  1142. OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
  1143. return 1;
  1144. }
  1145. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1146. if (in == NULL) {
  1147. return 48;
  1148. }
  1149. if (len != 48) {
  1150. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1151. return 0;
  1152. }
  1153. const uint8_t *in_bytes = in;
  1154. OPENSSL_memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
  1155. OPENSSL_memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
  1156. OPENSSL_memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
  1157. return 1;
  1158. }
  1159. int SSL_CTX_set_tlsext_ticket_key_cb(
  1160. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1161. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1162. int encrypt)) {
  1163. ctx->tlsext_ticket_key_cb = callback;
  1164. return 1;
  1165. }
  1166. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1167. return tls1_set_curves(&ctx->supported_group_list,
  1168. &ctx->supported_group_list_len, curves,
  1169. curves_len);
  1170. }
  1171. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1172. return tls1_set_curves(&ssl->supported_group_list,
  1173. &ssl->supported_group_list_len, curves,
  1174. curves_len);
  1175. }
  1176. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1177. return tls1_set_curves_list(&ctx->supported_group_list,
  1178. &ctx->supported_group_list_len, curves);
  1179. }
  1180. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1181. return tls1_set_curves_list(&ssl->supported_group_list,
  1182. &ssl->supported_group_list_len, curves);
  1183. }
  1184. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1185. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1186. * progress. */
  1187. SSL_SESSION *session = SSL_get_session(ssl);
  1188. if (session == NULL) {
  1189. return 0;
  1190. }
  1191. return session->group_id;
  1192. }
  1193. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1194. DH_free(ctx->cert->dh_tmp);
  1195. ctx->cert->dh_tmp = DHparams_dup(dh);
  1196. if (ctx->cert->dh_tmp == NULL) {
  1197. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1198. return 0;
  1199. }
  1200. return 1;
  1201. }
  1202. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1203. DH_free(ssl->cert->dh_tmp);
  1204. ssl->cert->dh_tmp = DHparams_dup(dh);
  1205. if (ssl->cert->dh_tmp == NULL) {
  1206. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1207. return 0;
  1208. }
  1209. return 1;
  1210. }
  1211. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1212. if (ssl == NULL) {
  1213. return NULL;
  1214. }
  1215. const struct ssl_cipher_preference_list_st *prefs =
  1216. ssl_get_cipher_preferences(ssl);
  1217. if (prefs == NULL) {
  1218. return NULL;
  1219. }
  1220. return prefs->ciphers;
  1221. }
  1222. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1223. const SSL_CIPHER *c;
  1224. STACK_OF(SSL_CIPHER) *sk;
  1225. if (ssl == NULL) {
  1226. return NULL;
  1227. }
  1228. sk = SSL_get_ciphers(ssl);
  1229. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1230. return NULL;
  1231. }
  1232. c = sk_SSL_CIPHER_value(sk, n);
  1233. if (c == NULL) {
  1234. return NULL;
  1235. }
  1236. return c->name;
  1237. }
  1238. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1239. STACK_OF(SSL_CIPHER) *cipher_list =
  1240. ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1241. 0 /* not strict */);
  1242. if (cipher_list == NULL) {
  1243. return 0;
  1244. }
  1245. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1246. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1247. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1248. return 0;
  1249. }
  1250. return 1;
  1251. }
  1252. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1253. STACK_OF(SSL_CIPHER) *cipher_list =
  1254. ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1255. 1 /* strict */);
  1256. if (cipher_list == NULL) {
  1257. return 0;
  1258. }
  1259. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1260. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1261. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1262. return 0;
  1263. }
  1264. return 1;
  1265. }
  1266. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1267. STACK_OF(SSL_CIPHER) *cipher_list =
  1268. ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1269. 0 /* not strict */);
  1270. if (cipher_list == NULL) {
  1271. return 0;
  1272. }
  1273. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1274. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1275. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1276. return 0;
  1277. }
  1278. return 1;
  1279. }
  1280. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1281. STACK_OF(SSL_CIPHER) *cipher_list =
  1282. ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1283. 1 /* strict */);
  1284. if (cipher_list == NULL) {
  1285. return 0;
  1286. }
  1287. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1288. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1289. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1290. return 0;
  1291. }
  1292. return 1;
  1293. }
  1294. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1295. if (type != TLSEXT_NAMETYPE_host_name) {
  1296. return NULL;
  1297. }
  1298. /* Historically, |SSL_get_servername| was also the configuration getter
  1299. * corresponding to |SSL_set_tlsext_host_name|. */
  1300. if (ssl->tlsext_hostname != NULL) {
  1301. return ssl->tlsext_hostname;
  1302. }
  1303. /* During the handshake, report the handshake value. */
  1304. if (ssl->s3->hs != NULL) {
  1305. return ssl->s3->hs->hostname;
  1306. }
  1307. /* SSL_get_servername may also be called after the handshake to look up the
  1308. * SNI value.
  1309. *
  1310. * TODO(davidben): This is almost unused. Can we remove it? */
  1311. SSL_SESSION *session = SSL_get_session(ssl);
  1312. if (session == NULL) {
  1313. return NULL;
  1314. }
  1315. return session->tlsext_hostname;
  1316. }
  1317. int SSL_get_servername_type(const SSL *ssl) {
  1318. SSL_SESSION *session = SSL_get_session(ssl);
  1319. if (session == NULL || session->tlsext_hostname == NULL) {
  1320. return -1;
  1321. }
  1322. return TLSEXT_NAMETYPE_host_name;
  1323. }
  1324. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1325. ctx->signed_cert_timestamps_enabled = 1;
  1326. }
  1327. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1328. ssl->signed_cert_timestamps_enabled = 1;
  1329. }
  1330. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1331. ctx->ocsp_stapling_enabled = 1;
  1332. }
  1333. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1334. ssl->ocsp_stapling_enabled = 1;
  1335. }
  1336. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1337. size_t *out_len) {
  1338. SSL_SESSION *session = SSL_get_session(ssl);
  1339. *out_len = 0;
  1340. *out = NULL;
  1341. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1342. return;
  1343. }
  1344. *out = session->tlsext_signed_cert_timestamp_list;
  1345. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1346. }
  1347. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1348. size_t *out_len) {
  1349. SSL_SESSION *session = SSL_get_session(ssl);
  1350. *out_len = 0;
  1351. *out = NULL;
  1352. if (ssl->server || !session || !session->ocsp_response) {
  1353. return;
  1354. }
  1355. *out = session->ocsp_response;
  1356. *out_len = session->ocsp_response_length;
  1357. }
  1358. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1359. OPENSSL_free(ssl->tlsext_hostname);
  1360. ssl->tlsext_hostname = NULL;
  1361. if (name == NULL) {
  1362. return 1;
  1363. }
  1364. size_t len = strlen(name);
  1365. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1366. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1367. return 0;
  1368. }
  1369. ssl->tlsext_hostname = BUF_strdup(name);
  1370. if (ssl->tlsext_hostname == NULL) {
  1371. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1372. return 0;
  1373. }
  1374. return 1;
  1375. }
  1376. int SSL_CTX_set_tlsext_servername_callback(
  1377. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1378. ctx->tlsext_servername_callback = callback;
  1379. return 1;
  1380. }
  1381. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1382. ctx->tlsext_servername_arg = arg;
  1383. return 1;
  1384. }
  1385. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1386. const uint8_t *server, unsigned server_len,
  1387. const uint8_t *client, unsigned client_len) {
  1388. unsigned int i, j;
  1389. const uint8_t *result;
  1390. int status = OPENSSL_NPN_UNSUPPORTED;
  1391. /* For each protocol in server preference order, see if we support it. */
  1392. for (i = 0; i < server_len;) {
  1393. for (j = 0; j < client_len;) {
  1394. if (server[i] == client[j] &&
  1395. OPENSSL_memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1396. /* We found a match */
  1397. result = &server[i];
  1398. status = OPENSSL_NPN_NEGOTIATED;
  1399. goto found;
  1400. }
  1401. j += client[j];
  1402. j++;
  1403. }
  1404. i += server[i];
  1405. i++;
  1406. }
  1407. /* There's no overlap between our protocols and the server's list. */
  1408. result = client;
  1409. status = OPENSSL_NPN_NO_OVERLAP;
  1410. found:
  1411. *out = (uint8_t *)result + 1;
  1412. *out_len = result[0];
  1413. return status;
  1414. }
  1415. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1416. unsigned *out_len) {
  1417. *out_data = ssl->s3->next_proto_negotiated;
  1418. if (*out_data == NULL) {
  1419. *out_len = 0;
  1420. } else {
  1421. *out_len = ssl->s3->next_proto_negotiated_len;
  1422. }
  1423. }
  1424. void SSL_CTX_set_next_protos_advertised_cb(
  1425. SSL_CTX *ctx,
  1426. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1427. void *arg) {
  1428. ctx->next_protos_advertised_cb = cb;
  1429. ctx->next_protos_advertised_cb_arg = arg;
  1430. }
  1431. void SSL_CTX_set_next_proto_select_cb(
  1432. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1433. const uint8_t *in, unsigned in_len, void *arg),
  1434. void *arg) {
  1435. ctx->next_proto_select_cb = cb;
  1436. ctx->next_proto_select_cb_arg = arg;
  1437. }
  1438. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1439. unsigned protos_len) {
  1440. OPENSSL_free(ctx->alpn_client_proto_list);
  1441. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1442. if (!ctx->alpn_client_proto_list) {
  1443. return 1;
  1444. }
  1445. ctx->alpn_client_proto_list_len = protos_len;
  1446. return 0;
  1447. }
  1448. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1449. OPENSSL_free(ssl->alpn_client_proto_list);
  1450. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1451. if (!ssl->alpn_client_proto_list) {
  1452. return 1;
  1453. }
  1454. ssl->alpn_client_proto_list_len = protos_len;
  1455. return 0;
  1456. }
  1457. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1458. int (*cb)(SSL *ssl, const uint8_t **out,
  1459. uint8_t *out_len, const uint8_t *in,
  1460. unsigned in_len, void *arg),
  1461. void *arg) {
  1462. ctx->alpn_select_cb = cb;
  1463. ctx->alpn_select_cb_arg = arg;
  1464. }
  1465. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1466. unsigned *out_len) {
  1467. *out_data = NULL;
  1468. if (ssl->s3) {
  1469. *out_data = ssl->s3->alpn_selected;
  1470. }
  1471. if (*out_data == NULL) {
  1472. *out_len = 0;
  1473. } else {
  1474. *out_len = ssl->s3->alpn_selected_len;
  1475. }
  1476. }
  1477. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1478. ctx->tlsext_channel_id_enabled = !!enabled;
  1479. }
  1480. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1481. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1482. return 1;
  1483. }
  1484. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1485. ssl->tlsext_channel_id_enabled = !!enabled;
  1486. }
  1487. int SSL_enable_tls_channel_id(SSL *ssl) {
  1488. SSL_set_tls_channel_id_enabled(ssl, 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. EVP_PKEY_up_ref(private_key);
  1504. ctx->tlsext_channel_id_private = private_key;
  1505. ctx->tlsext_channel_id_enabled = 1;
  1506. return 1;
  1507. }
  1508. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1509. if (!is_p256_key(private_key)) {
  1510. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1511. return 0;
  1512. }
  1513. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1514. EVP_PKEY_up_ref(private_key);
  1515. ssl->tlsext_channel_id_private = private_key;
  1516. ssl->tlsext_channel_id_enabled = 1;
  1517. return 1;
  1518. }
  1519. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1520. if (!ssl->s3->tlsext_channel_id_valid) {
  1521. return 0;
  1522. }
  1523. OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
  1524. (max_out < 64) ? max_out : 64);
  1525. return 64;
  1526. }
  1527. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1528. if (ssl->server || ssl->s3->hs == NULL) {
  1529. *out_types = NULL;
  1530. return 0;
  1531. }
  1532. *out_types = ssl->s3->hs->certificate_types;
  1533. return ssl->s3->hs->num_certificate_types;
  1534. }
  1535. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  1536. SSL *const ssl = hs->ssl;
  1537. SSL_CTX *ctx = ssl->initial_ctx;
  1538. /* Never cache sessions with empty session IDs. */
  1539. if (ssl->s3->established_session->session_id_length == 0 ||
  1540. (ctx->session_cache_mode & mode) != mode) {
  1541. return;
  1542. }
  1543. /* Clients never use the internal session cache. */
  1544. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1545. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1546. /* A client may see new sessions on abbreviated handshakes if the server
  1547. * decides to renew the ticket. Once the handshake is completed, it should be
  1548. * inserted into the cache. */
  1549. if (ssl->s3->established_session != ssl->session ||
  1550. (!ssl->server && hs->ticket_expected)) {
  1551. if (use_internal_cache) {
  1552. SSL_CTX_add_session(ctx, ssl->s3->established_session);
  1553. }
  1554. if (ctx->new_session_cb != NULL) {
  1555. SSL_SESSION_up_ref(ssl->s3->established_session);
  1556. if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
  1557. /* |new_session_cb|'s return value signals whether it took ownership. */
  1558. SSL_SESSION_free(ssl->s3->established_session);
  1559. }
  1560. }
  1561. }
  1562. if (use_internal_cache &&
  1563. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1564. /* Automatically flush the internal session cache every 255 connections. */
  1565. int flush_cache = 0;
  1566. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1567. ctx->handshakes_since_cache_flush++;
  1568. if (ctx->handshakes_since_cache_flush >= 255) {
  1569. flush_cache = 1;
  1570. ctx->handshakes_since_cache_flush = 0;
  1571. }
  1572. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  1573. if (flush_cache) {
  1574. struct timeval now;
  1575. ssl_get_current_time(ssl, &now);
  1576. SSL_CTX_flush_sessions(ctx, (long)now.tv_sec);
  1577. }
  1578. }
  1579. }
  1580. static const char *ssl_get_version(int version) {
  1581. switch (version) {
  1582. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1583. case TLS1_3_DRAFT_VERSION:
  1584. return "TLSv1.3";
  1585. case TLS1_2_VERSION:
  1586. return "TLSv1.2";
  1587. case TLS1_1_VERSION:
  1588. return "TLSv1.1";
  1589. case TLS1_VERSION:
  1590. return "TLSv1";
  1591. case SSL3_VERSION:
  1592. return "SSLv3";
  1593. case DTLS1_VERSION:
  1594. return "DTLSv1";
  1595. case DTLS1_2_VERSION:
  1596. return "DTLSv1.2";
  1597. default:
  1598. return "unknown";
  1599. }
  1600. }
  1601. const char *SSL_get_version(const SSL *ssl) {
  1602. return ssl_get_version(ssl->version);
  1603. }
  1604. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1605. return ssl_get_version(session->ssl_version);
  1606. }
  1607. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1608. if (ssl->cert != NULL) {
  1609. return ssl->cert->privatekey;
  1610. }
  1611. return NULL;
  1612. }
  1613. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1614. if (ctx->cert != NULL) {
  1615. return ctx->cert->privatekey;
  1616. }
  1617. return NULL;
  1618. }
  1619. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1620. if (ssl->s3->aead_write_ctx == NULL) {
  1621. return NULL;
  1622. }
  1623. return ssl->s3->aead_write_ctx->cipher;
  1624. }
  1625. int SSL_session_reused(const SSL *ssl) {
  1626. return ssl->s3->session_reused;
  1627. }
  1628. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1629. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1630. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1631. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1632. ctx->quiet_shutdown = (mode != 0);
  1633. }
  1634. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1635. return ctx->quiet_shutdown;
  1636. }
  1637. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1638. ssl->quiet_shutdown = (mode != 0);
  1639. }
  1640. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1641. void SSL_set_shutdown(SSL *ssl, int mode) {
  1642. /* It is an error to clear any bits that have already been set. (We can't try
  1643. * to get a second close_notify or send two.) */
  1644. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1645. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1646. ssl->s3->recv_shutdown == ssl_shutdown_none) {
  1647. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  1648. }
  1649. if (mode & SSL_SENT_SHUTDOWN &&
  1650. ssl->s3->send_shutdown == ssl_shutdown_none) {
  1651. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  1652. }
  1653. }
  1654. int SSL_get_shutdown(const SSL *ssl) {
  1655. int ret = 0;
  1656. if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
  1657. /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1658. * and fatal alert. */
  1659. ret |= SSL_RECEIVED_SHUTDOWN;
  1660. }
  1661. if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
  1662. /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
  1663. ret |= SSL_SENT_SHUTDOWN;
  1664. }
  1665. return ret;
  1666. }
  1667. int SSL_version(const SSL *ssl) {
  1668. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1669. if (ssl->version == TLS1_3_DRAFT_VERSION) {
  1670. return TLS1_3_VERSION;
  1671. }
  1672. return ssl->version;
  1673. }
  1674. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1675. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1676. if (ssl->ctx == ctx) {
  1677. return ssl->ctx;
  1678. }
  1679. /* One cannot change the X.509 callbacks during a connection. */
  1680. if (ssl->ctx->x509_method != ctx->x509_method) {
  1681. assert(0);
  1682. return NULL;
  1683. }
  1684. if (ctx == NULL) {
  1685. ctx = ssl->initial_ctx;
  1686. }
  1687. ssl_cert_free(ssl->cert);
  1688. ssl->cert = ssl_cert_dup(ctx->cert);
  1689. SSL_CTX_up_ref(ctx);
  1690. SSL_CTX_free(ssl->ctx);
  1691. ssl->ctx = ctx;
  1692. return ssl->ctx;
  1693. }
  1694. void SSL_set_info_callback(SSL *ssl,
  1695. void (*cb)(const SSL *ssl, int type, int value)) {
  1696. ssl->info_callback = cb;
  1697. }
  1698. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1699. int value) {
  1700. return ssl->info_callback;
  1701. }
  1702. int SSL_state(const SSL *ssl) {
  1703. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  1704. }
  1705. void SSL_set_state(SSL *ssl, int state) { }
  1706. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1707. if (len <= 0) {
  1708. return NULL;
  1709. }
  1710. buf[0] = '\0';
  1711. return buf;
  1712. }
  1713. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1714. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1715. int index;
  1716. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1717. dup_func, free_func)) {
  1718. return -1;
  1719. }
  1720. return index;
  1721. }
  1722. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1723. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1724. }
  1725. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1726. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1727. }
  1728. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1729. CRYPTO_EX_dup *dup_func,
  1730. CRYPTO_EX_free *free_func) {
  1731. int index;
  1732. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1733. dup_func, free_func)) {
  1734. return -1;
  1735. }
  1736. return index;
  1737. }
  1738. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1739. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1740. }
  1741. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1742. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1743. }
  1744. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1745. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1746. RSA *(*cb)(SSL *ssl, int is_export,
  1747. int keylength)) {
  1748. }
  1749. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1750. int keylength)) {
  1751. }
  1752. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1753. DH *(*callback)(SSL *ssl, int is_export,
  1754. int keylength)) {
  1755. ctx->cert->dh_tmp_cb = callback;
  1756. }
  1757. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1758. int keylength)) {
  1759. ssl->cert->dh_tmp_cb = callback;
  1760. }
  1761. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1762. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1763. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1764. return 0;
  1765. }
  1766. OPENSSL_free(ctx->psk_identity_hint);
  1767. if (identity_hint != NULL) {
  1768. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1769. if (ctx->psk_identity_hint == NULL) {
  1770. return 0;
  1771. }
  1772. } else {
  1773. ctx->psk_identity_hint = NULL;
  1774. }
  1775. return 1;
  1776. }
  1777. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1778. if (ssl == NULL) {
  1779. return 0;
  1780. }
  1781. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1782. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1783. return 0;
  1784. }
  1785. /* Clear currently configured hint, if any. */
  1786. OPENSSL_free(ssl->psk_identity_hint);
  1787. ssl->psk_identity_hint = NULL;
  1788. /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
  1789. * send either no hint (omit ServerKeyExchange) or an empty hint, while
  1790. * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  1791. * so we interpret empty and missing as identical. */
  1792. if (identity_hint != NULL && identity_hint[0] != '\0') {
  1793. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1794. if (ssl->psk_identity_hint == NULL) {
  1795. return 0;
  1796. }
  1797. }
  1798. return 1;
  1799. }
  1800. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1801. if (ssl == NULL) {
  1802. return NULL;
  1803. }
  1804. return ssl->psk_identity_hint;
  1805. }
  1806. const char *SSL_get_psk_identity(const SSL *ssl) {
  1807. if (ssl == NULL) {
  1808. return NULL;
  1809. }
  1810. SSL_SESSION *session = SSL_get_session(ssl);
  1811. if (session == NULL) {
  1812. return NULL;
  1813. }
  1814. return session->psk_identity;
  1815. }
  1816. void SSL_set_psk_client_callback(
  1817. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1818. unsigned max_identity_len, uint8_t *psk,
  1819. unsigned max_psk_len)) {
  1820. ssl->psk_client_callback = cb;
  1821. }
  1822. void SSL_CTX_set_psk_client_callback(
  1823. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1824. unsigned max_identity_len, uint8_t *psk,
  1825. unsigned max_psk_len)) {
  1826. ctx->psk_client_callback = cb;
  1827. }
  1828. void SSL_set_psk_server_callback(
  1829. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1830. unsigned max_psk_len)) {
  1831. ssl->psk_server_callback = cb;
  1832. }
  1833. void SSL_CTX_set_psk_server_callback(
  1834. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1835. uint8_t *psk, unsigned max_psk_len)) {
  1836. ctx->psk_server_callback = cb;
  1837. }
  1838. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1839. void (*cb)(int write_p, int version,
  1840. int content_type, const void *buf,
  1841. size_t len, SSL *ssl, void *arg)) {
  1842. ctx->msg_callback = cb;
  1843. }
  1844. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1845. ctx->msg_callback_arg = arg;
  1846. }
  1847. void SSL_set_msg_callback(SSL *ssl,
  1848. void (*cb)(int write_p, int version, int content_type,
  1849. const void *buf, size_t len, SSL *ssl,
  1850. void *arg)) {
  1851. ssl->msg_callback = cb;
  1852. }
  1853. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1854. ssl->msg_callback_arg = arg;
  1855. }
  1856. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1857. void (*cb)(const SSL *ssl, const char *line)) {
  1858. ctx->keylog_callback = cb;
  1859. }
  1860. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  1861. const char *line) {
  1862. return ctx->keylog_callback;
  1863. }
  1864. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  1865. void (*cb)(const SSL *ssl,
  1866. struct timeval *out_clock)) {
  1867. ctx->current_time_cb = cb;
  1868. }
  1869. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1870. static const char hextable[] = "0123456789abcdef";
  1871. uint8_t *out;
  1872. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1873. return 0;
  1874. }
  1875. for (size_t i = 0; i < in_len; i++) {
  1876. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1877. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1878. }
  1879. return 1;
  1880. }
  1881. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  1882. size_t secret_len) {
  1883. if (ssl->ctx->keylog_callback == NULL) {
  1884. return 1;
  1885. }
  1886. CBB cbb;
  1887. uint8_t *out;
  1888. size_t out_len;
  1889. if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  1890. secret_len * 2 + 1) ||
  1891. !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
  1892. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1893. !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  1894. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1895. !cbb_add_hex(&cbb, secret, secret_len) ||
  1896. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1897. !CBB_finish(&cbb, &out, &out_len)) {
  1898. CBB_cleanup(&cbb);
  1899. return 0;
  1900. }
  1901. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1902. OPENSSL_free(out);
  1903. return 1;
  1904. }
  1905. int SSL_is_init_finished(const SSL *ssl) {
  1906. return !SSL_in_init(ssl);
  1907. }
  1908. int SSL_in_init(const SSL *ssl) {
  1909. SSL_HANDSHAKE *hs = ssl->s3->hs;
  1910. return hs != NULL && hs->state != SSL_ST_OK;
  1911. }
  1912. int SSL_in_false_start(const SSL *ssl) {
  1913. if (ssl->s3->hs == NULL) {
  1914. return 0;
  1915. }
  1916. return ssl->s3->hs->in_false_start;
  1917. }
  1918. int SSL_cutthrough_complete(const SSL *ssl) {
  1919. return SSL_in_false_start(ssl);
  1920. }
  1921. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1922. size_t *ssl_session_size) {
  1923. *ssl_size = sizeof(SSL);
  1924. *ssl_ctx_size = sizeof(SSL_CTX);
  1925. *ssl_session_size = sizeof(SSL_SESSION);
  1926. }
  1927. int ssl3_can_false_start(const SSL *ssl) {
  1928. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  1929. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1930. return !SSL_is_dtls(ssl) &&
  1931. SSL_version(ssl) == TLS1_2_VERSION &&
  1932. (ssl->s3->alpn_selected != NULL ||
  1933. ssl->s3->next_proto_negotiated != NULL) &&
  1934. cipher != NULL &&
  1935. cipher->algorithm_mkey == SSL_kECDHE &&
  1936. cipher->algorithm_mac == SSL_AEAD;
  1937. }
  1938. const struct {
  1939. uint16_t version;
  1940. uint32_t flag;
  1941. } kVersions[] = {
  1942. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  1943. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  1944. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  1945. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  1946. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  1947. };
  1948. static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
  1949. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  1950. uint16_t *out_max_version) {
  1951. /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  1952. * DTLS 1.0 should be mapped to TLS 1.1. */
  1953. uint32_t options = ssl->options;
  1954. if (SSL_is_dtls(ssl)) {
  1955. options &= ~SSL_OP_NO_TLSv1_1;
  1956. if (options & SSL_OP_NO_DTLSv1) {
  1957. options |= SSL_OP_NO_TLSv1_1;
  1958. }
  1959. }
  1960. uint16_t min_version = ssl->min_version;
  1961. uint16_t max_version = ssl->max_version;
  1962. /* Bound the range to only those implemented in this protocol. */
  1963. if (min_version < ssl->method->min_version) {
  1964. min_version = ssl->method->min_version;
  1965. }
  1966. if (max_version > ssl->method->max_version) {
  1967. max_version = ssl->method->max_version;
  1968. }
  1969. /* OpenSSL's API for controlling versions entails blacklisting individual
  1970. * protocols. This has two problems. First, on the client, the protocol can
  1971. * only express a contiguous range of versions. Second, a library consumer
  1972. * trying to set a maximum version cannot disable protocol versions that get
  1973. * added in a future version of the library.
  1974. *
  1975. * To account for both of these, OpenSSL interprets the client-side bitmask
  1976. * as a min/max range by picking the lowest contiguous non-empty range of
  1977. * enabled protocols. Note that this means it is impossible to set a maximum
  1978. * version of the higest supported TLS version in a future-proof way. */
  1979. int any_enabled = 0;
  1980. for (size_t i = 0; i < kVersionsLen; i++) {
  1981. /* Only look at the versions already enabled. */
  1982. if (min_version > kVersions[i].version) {
  1983. continue;
  1984. }
  1985. if (max_version < kVersions[i].version) {
  1986. break;
  1987. }
  1988. if (!(options & kVersions[i].flag)) {
  1989. /* The minimum version is the first enabled version. */
  1990. if (!any_enabled) {
  1991. any_enabled = 1;
  1992. min_version = kVersions[i].version;
  1993. }
  1994. continue;
  1995. }
  1996. /* If there is a disabled version after the first enabled one, all versions
  1997. * after it are implicitly disabled. */
  1998. if (any_enabled) {
  1999. max_version = kVersions[i-1].version;
  2000. break;
  2001. }
  2002. }
  2003. if (!any_enabled) {
  2004. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  2005. return 0;
  2006. }
  2007. *out_min_version = min_version;
  2008. *out_max_version = max_version;
  2009. return 1;
  2010. }
  2011. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2012. assert(ssl->s3->have_version);
  2013. uint16_t version;
  2014. if (!ssl->method->version_from_wire(&version, ssl->version)) {
  2015. /* TODO(davidben): Use the internal version representation for ssl->version
  2016. * and map to the public API representation at API boundaries. */
  2017. assert(0);
  2018. return 0;
  2019. }
  2020. return version;
  2021. }
  2022. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2023. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2024. void SSL_CTX_set_select_certificate_cb(SSL_CTX *ctx,
  2025. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2026. ctx->select_certificate_cb = cb;
  2027. }
  2028. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2029. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2030. ctx->dos_protection_cb = cb;
  2031. }
  2032. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2033. ssl->renegotiate_mode = mode;
  2034. }
  2035. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2036. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2037. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2038. return 0;
  2039. }
  2040. size_t write_iv_len;
  2041. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2042. out_iv_len) ||
  2043. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2044. &write_iv_len) ||
  2045. *out_iv_len != write_iv_len) {
  2046. return 0;
  2047. }
  2048. return 1;
  2049. }
  2050. static uint64_t be_to_u64(const uint8_t in[8]) {
  2051. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2052. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2053. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2054. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2055. }
  2056. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2057. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2058. if (SSL_is_dtls(ssl)) {
  2059. /* max_seq_num already includes the epoch. */
  2060. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2061. return ssl->d1->bitmap.max_seq_num;
  2062. }
  2063. return be_to_u64(ssl->s3->read_sequence);
  2064. }
  2065. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2066. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2067. if (SSL_is_dtls(ssl)) {
  2068. assert((ret >> 48) == 0);
  2069. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2070. }
  2071. return ret;
  2072. }
  2073. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2074. /* TODO(davidben): This checks the wrong session if there is a renegotiation
  2075. * in progress. */
  2076. SSL_SESSION *session = SSL_get_session(ssl);
  2077. if (session == NULL) {
  2078. return 0;
  2079. }
  2080. return session->peer_signature_algorithm;
  2081. }
  2082. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2083. if (max_out == 0) {
  2084. return sizeof(ssl->s3->client_random);
  2085. }
  2086. if (max_out > sizeof(ssl->s3->client_random)) {
  2087. max_out = sizeof(ssl->s3->client_random);
  2088. }
  2089. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2090. return max_out;
  2091. }
  2092. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2093. if (max_out == 0) {
  2094. return sizeof(ssl->s3->server_random);
  2095. }
  2096. if (max_out > sizeof(ssl->s3->server_random)) {
  2097. max_out = sizeof(ssl->s3->server_random);
  2098. }
  2099. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2100. return max_out;
  2101. }
  2102. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2103. SSL_HANDSHAKE *hs = ssl->s3->hs;
  2104. if (hs == NULL) {
  2105. return NULL;
  2106. }
  2107. return hs->new_cipher;
  2108. }
  2109. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2110. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2111. }
  2112. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2113. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2114. }
  2115. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2116. ctx->grease_enabled = !!enabled;
  2117. }
  2118. void SSL_CTX_set_short_header_enabled(SSL_CTX *ctx, int enabled) {
  2119. ctx->short_header_enabled = !!enabled;
  2120. }
  2121. int SSL_clear(SSL *ssl) {
  2122. /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2123. * established session to be offered the next time around. wpa_supplicant
  2124. * depends on this behavior, so emulate it. */
  2125. SSL_SESSION *session = NULL;
  2126. if (!ssl->server && ssl->s3->established_session != NULL) {
  2127. session = ssl->s3->established_session;
  2128. SSL_SESSION_up_ref(session);
  2129. }
  2130. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2131. * |SSL_clear| because it is per-connection state rather than configuration
  2132. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2133. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2134. * |ssl3_new|. */
  2135. ssl->rwstate = SSL_NOTHING;
  2136. BUF_MEM_free(ssl->init_buf);
  2137. ssl->init_buf = NULL;
  2138. ssl->init_msg = NULL;
  2139. ssl->init_num = 0;
  2140. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2141. * clear) and connection-specific state (gets reset).
  2142. *
  2143. * TODO(davidben): Avoid this. */
  2144. unsigned mtu = 0;
  2145. if (ssl->d1 != NULL) {
  2146. mtu = ssl->d1->mtu;
  2147. }
  2148. ssl->method->ssl_free(ssl);
  2149. if (!ssl->method->ssl_new(ssl)) {
  2150. SSL_SESSION_free(session);
  2151. return 0;
  2152. }
  2153. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2154. ssl->d1->mtu = mtu;
  2155. }
  2156. if (session != NULL) {
  2157. SSL_set_session(ssl, session);
  2158. SSL_SESSION_free(session);
  2159. }
  2160. return 1;
  2161. }
  2162. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  2163. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  2164. if (ssl->info_callback != NULL) {
  2165. cb = ssl->info_callback;
  2166. } else if (ssl->ctx->info_callback != NULL) {
  2167. cb = ssl->ctx->info_callback;
  2168. }
  2169. if (cb != NULL) {
  2170. cb(ssl, type, value);
  2171. }
  2172. }
  2173. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  2174. const void *buf, size_t len) {
  2175. if (ssl->msg_callback == NULL) {
  2176. return;
  2177. }
  2178. /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  2179. * a V2ClientHello. */
  2180. int version;
  2181. switch (content_type) {
  2182. case 0:
  2183. /* V2ClientHello */
  2184. version = SSL2_VERSION;
  2185. break;
  2186. case SSL3_RT_HEADER:
  2187. version = 0;
  2188. break;
  2189. default:
  2190. version = SSL_version(ssl);
  2191. }
  2192. ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
  2193. ssl->msg_callback_arg);
  2194. }
  2195. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2196. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2197. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2198. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2199. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2200. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2201. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2202. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2203. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2204. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2205. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2206. int SSL_num_renegotiations(const SSL *ssl) {
  2207. return SSL_total_renegotiations(ssl);
  2208. }
  2209. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2210. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2211. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2212. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2213. void ERR_load_SSL_strings(void) {}
  2214. void SSL_load_error_strings(void) {}
  2215. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2216. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2217. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2218. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2219. return 0;
  2220. }
  2221. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2222. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2223. }
  2224. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2225. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2226. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2227. return 0;
  2228. }
  2229. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2230. return SSL_set1_curves(ssl, &nid, 1);
  2231. }
  2232. void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock) {
  2233. if (ssl->ctx->current_time_cb != NULL) {
  2234. ssl->ctx->current_time_cb(ssl, out_clock);
  2235. return;
  2236. }
  2237. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  2238. out_clock->tv_sec = 1234;
  2239. out_clock->tv_usec = 1234;
  2240. #elif defined(OPENSSL_WINDOWS)
  2241. struct _timeb time;
  2242. _ftime(&time);
  2243. out_clock->tv_sec = time.time;
  2244. out_clock->tv_usec = time.millitm * 1000;
  2245. #else
  2246. gettimeofday(out_clock, NULL);
  2247. #endif
  2248. }
  2249. int SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2250. return SSL_CTX_set_min_proto_version(ctx, version);
  2251. }
  2252. int SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2253. return SSL_CTX_set_max_proto_version(ctx, version);
  2254. }
  2255. int SSL_set_min_version(SSL *ssl, uint16_t version) {
  2256. return SSL_set_min_proto_version(ssl, version);
  2257. }
  2258. int SSL_set_max_version(SSL *ssl, uint16_t version) {
  2259. return SSL_set_max_proto_version(ssl, version);
  2260. }