You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2969 lines
85 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/err.h>
  147. #include <openssl/lhash.h>
  148. #include <openssl/mem.h>
  149. #include <openssl/rand.h>
  150. #include "internal.h"
  151. #include "../crypto/internal.h"
  152. #if defined(OPENSSL_WINDOWS)
  153. #include <sys/timeb.h>
  154. #else
  155. #include <sys/socket.h>
  156. #include <sys/time.h>
  157. #endif
  158. BSSL_NAMESPACE_BEGIN
  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. static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. "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. bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) {
  178. uint8_t *ptr;
  179. size_t len;
  180. if (!CBB_finish(cbb, &ptr, &len)) {
  181. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  182. return false;
  183. }
  184. out->Reset(ptr, len);
  185. return true;
  186. }
  187. void ssl_reset_error_state(SSL *ssl) {
  188. // Functions which use |SSL_get_error| must reset I/O and error state on
  189. // entry.
  190. ssl->s3->rwstate = SSL_NOTHING;
  191. ERR_clear_error();
  192. ERR_clear_system_error();
  193. }
  194. void ssl_set_read_error(SSL* ssl) {
  195. ssl->s3->read_shutdown = ssl_shutdown_error;
  196. ssl->s3->read_error.reset(ERR_save_state());
  197. }
  198. static bool check_read_error(const SSL *ssl) {
  199. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  200. ERR_restore_state(ssl->s3->read_error.get());
  201. return false;
  202. }
  203. return true;
  204. }
  205. bool ssl_can_write(const SSL *ssl) {
  206. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
  207. }
  208. bool ssl_can_read(const SSL *ssl) {
  209. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
  210. }
  211. ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
  212. uint8_t *out_alert, Span<uint8_t> in) {
  213. *out_consumed = 0;
  214. if (!check_read_error(ssl)) {
  215. *out_alert = 0;
  216. return ssl_open_record_error;
  217. }
  218. auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in);
  219. if (ret == ssl_open_record_error) {
  220. ssl_set_read_error(ssl);
  221. }
  222. return ret;
  223. }
  224. ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  225. uint8_t *out_alert,
  226. Span<uint8_t> in) {
  227. *out_consumed = 0;
  228. if (!check_read_error(ssl)) {
  229. *out_alert = 0;
  230. return ssl_open_record_error;
  231. }
  232. auto ret =
  233. ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in);
  234. if (ret == ssl_open_record_error) {
  235. ssl_set_read_error(ssl);
  236. }
  237. return ret;
  238. }
  239. ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
  240. size_t *out_consumed, uint8_t *out_alert,
  241. Span<uint8_t> in) {
  242. *out_consumed = 0;
  243. if (!check_read_error(ssl)) {
  244. *out_alert = 0;
  245. return ssl_open_record_error;
  246. }
  247. auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in);
  248. if (ret == ssl_open_record_error) {
  249. ssl_set_read_error(ssl);
  250. }
  251. return ret;
  252. }
  253. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  254. SSL *const ssl = hs->ssl;
  255. SSL_CTX *ctx = ssl->session_ctx.get();
  256. // Never cache sessions with empty session IDs.
  257. if (ssl->s3->established_session->session_id_length == 0 ||
  258. ssl->s3->established_session->not_resumable ||
  259. (ctx->session_cache_mode & mode) != mode) {
  260. return;
  261. }
  262. // Clients never use the internal session cache.
  263. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  264. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  265. // A client may see new sessions on abbreviated handshakes if the server
  266. // decides to renew the ticket. Once the handshake is completed, it should be
  267. // inserted into the cache.
  268. if (ssl->s3->established_session.get() != ssl->session.get() ||
  269. (!ssl->server && hs->ticket_expected)) {
  270. if (use_internal_cache) {
  271. SSL_CTX_add_session(ctx, ssl->s3->established_session.get());
  272. }
  273. if (ctx->new_session_cb != NULL) {
  274. UniquePtr<SSL_SESSION> ref = UpRef(ssl->s3->established_session);
  275. if (ctx->new_session_cb(ssl, ref.get())) {
  276. // |new_session_cb|'s return value signals whether it took ownership.
  277. ref.release();
  278. }
  279. }
  280. }
  281. if (use_internal_cache &&
  282. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  283. // Automatically flush the internal session cache every 255 connections.
  284. int flush_cache = 0;
  285. CRYPTO_MUTEX_lock_write(&ctx->lock);
  286. ctx->handshakes_since_cache_flush++;
  287. if (ctx->handshakes_since_cache_flush >= 255) {
  288. flush_cache = 1;
  289. ctx->handshakes_since_cache_flush = 0;
  290. }
  291. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  292. if (flush_cache) {
  293. struct OPENSSL_timeval now;
  294. ssl_get_current_time(ssl, &now);
  295. SSL_CTX_flush_sessions(ctx, now.tv_sec);
  296. }
  297. }
  298. }
  299. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  300. static const char hextable[] = "0123456789abcdef";
  301. uint8_t *out;
  302. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  303. return 0;
  304. }
  305. for (size_t i = 0; i < in_len; i++) {
  306. *(out++) = (uint8_t)hextable[in[i] >> 4];
  307. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  308. }
  309. return 1;
  310. }
  311. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  312. size_t secret_len) {
  313. if (ssl->ctx->keylog_callback == NULL) {
  314. return 1;
  315. }
  316. ScopedCBB cbb;
  317. uint8_t *out;
  318. size_t out_len;
  319. if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  320. secret_len * 2 + 1) ||
  321. !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
  322. !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
  323. !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  324. !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
  325. !cbb_add_hex(cbb.get(), secret, secret_len) ||
  326. !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
  327. !CBB_finish(cbb.get(), &out, &out_len)) {
  328. return 0;
  329. }
  330. ssl->ctx->keylog_callback(ssl, (const char *)out);
  331. OPENSSL_free(out);
  332. return 1;
  333. }
  334. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  335. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  336. if (ssl->info_callback != NULL) {
  337. cb = ssl->info_callback;
  338. } else if (ssl->ctx->info_callback != NULL) {
  339. cb = ssl->ctx->info_callback;
  340. }
  341. if (cb != NULL) {
  342. cb(ssl, type, value);
  343. }
  344. }
  345. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  346. Span<const uint8_t> in) {
  347. if (ssl->msg_callback == NULL) {
  348. return;
  349. }
  350. // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  351. // a V2ClientHello.
  352. int version;
  353. switch (content_type) {
  354. case 0:
  355. // V2ClientHello
  356. version = SSL2_VERSION;
  357. break;
  358. case SSL3_RT_HEADER:
  359. version = 0;
  360. break;
  361. default:
  362. version = SSL_version(ssl);
  363. }
  364. ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), ssl,
  365. ssl->msg_callback_arg);
  366. }
  367. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
  368. // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
  369. // |ssl| arg from |current_time_cb| if possible.
  370. ssl_ctx_get_current_time(ssl->ctx.get(), out_clock);
  371. }
  372. void ssl_ctx_get_current_time(const SSL_CTX *ctx,
  373. struct OPENSSL_timeval *out_clock) {
  374. if (ctx->current_time_cb != NULL) {
  375. // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
  376. // https://crbug.com/boringssl/155.
  377. struct timeval clock;
  378. ctx->current_time_cb(nullptr /* ssl */, &clock);
  379. if (clock.tv_sec < 0) {
  380. assert(0);
  381. out_clock->tv_sec = 0;
  382. out_clock->tv_usec = 0;
  383. } else {
  384. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  385. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  386. }
  387. return;
  388. }
  389. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  390. out_clock->tv_sec = 1234;
  391. out_clock->tv_usec = 1234;
  392. #elif defined(OPENSSL_WINDOWS)
  393. struct _timeb time;
  394. _ftime(&time);
  395. if (time.time < 0) {
  396. assert(0);
  397. out_clock->tv_sec = 0;
  398. out_clock->tv_usec = 0;
  399. } else {
  400. out_clock->tv_sec = time.time;
  401. out_clock->tv_usec = time.millitm * 1000;
  402. }
  403. #else
  404. struct timeval clock;
  405. gettimeofday(&clock, NULL);
  406. if (clock.tv_sec < 0) {
  407. assert(0);
  408. out_clock->tv_sec = 0;
  409. out_clock->tv_usec = 0;
  410. } else {
  411. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  412. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  413. }
  414. #endif
  415. }
  416. void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) {
  417. ctx->handoff = on;
  418. }
  419. static bool ssl_can_renegotiate(const SSL *ssl) {
  420. if (ssl->server || SSL_is_dtls(ssl)) {
  421. return false;
  422. }
  423. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  424. return false;
  425. }
  426. // The config has already been shed.
  427. if (!ssl->config) {
  428. return false;
  429. }
  430. switch (ssl->renegotiate_mode) {
  431. case ssl_renegotiate_ignore:
  432. case ssl_renegotiate_never:
  433. return false;
  434. case ssl_renegotiate_freely:
  435. return true;
  436. case ssl_renegotiate_once:
  437. return ssl->s3->total_renegotiations == 0;
  438. }
  439. assert(0);
  440. return false;
  441. }
  442. static void ssl_maybe_shed_handshake_config(SSL *ssl) {
  443. if (ssl->s3->hs != nullptr ||
  444. ssl->config == nullptr ||
  445. !ssl->config->shed_handshake_config ||
  446. ssl_can_renegotiate(ssl)) {
  447. return;
  448. }
  449. ssl->config.reset();
  450. }
  451. void SSL_set_handoff_mode(SSL *ssl, bool on) {
  452. if (!ssl->config) {
  453. return;
  454. }
  455. ssl->config->handoff = on;
  456. }
  457. bool SSL_get_traffic_secrets(const SSL *ssl,
  458. Span<const uint8_t> *out_read_traffic_secret,
  459. Span<const uint8_t> *out_write_traffic_secret) {
  460. if (SSL_version(ssl) < TLS1_3_VERSION) {
  461. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  462. return false;
  463. }
  464. if (!ssl->s3->initial_handshake_complete) {
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
  466. return false;
  467. }
  468. *out_read_traffic_secret = Span<const uint8_t>(
  469. ssl->s3->read_traffic_secret, ssl->s3->read_traffic_secret_len);
  470. *out_write_traffic_secret = Span<const uint8_t>(
  471. ssl->s3->write_traffic_secret, ssl->s3->write_traffic_secret_len);
  472. return true;
  473. }
  474. BSSL_NAMESPACE_END
  475. using namespace bssl;
  476. int SSL_library_init(void) {
  477. CRYPTO_library_init();
  478. return 1;
  479. }
  480. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  481. CRYPTO_library_init();
  482. return 1;
  483. }
  484. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  485. return ssl_hash_session_id(
  486. MakeConstSpan(sess->session_id, sess->session_id_length));
  487. }
  488. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  489. if (a->session_id_length != b->session_id_length) {
  490. return 1;
  491. }
  492. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  493. }
  494. ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method)
  495. : method(ssl_method->method),
  496. x509_method(ssl_method->x509_method),
  497. retain_only_sha256_of_client_certs(false),
  498. quiet_shutdown(false),
  499. ocsp_stapling_enabled(false),
  500. signed_cert_timestamps_enabled(false),
  501. channel_id_enabled(false),
  502. grease_enabled(false),
  503. allow_unknown_alpn_protos(false),
  504. ed25519_enabled(false),
  505. rsa_pss_rsae_certs_enabled(true),
  506. false_start_allowed_without_alpn(false),
  507. ignore_tls13_downgrade(false),
  508. handoff(false),
  509. enable_early_data(false) {
  510. CRYPTO_MUTEX_init(&lock);
  511. CRYPTO_new_ex_data(&ex_data);
  512. }
  513. ssl_ctx_st::~ssl_ctx_st() {
  514. // Free the internal session cache. Note that this calls the caller-supplied
  515. // remove callback, so we must do it before clearing ex_data. (See ticket
  516. // [openssl.org #212].)
  517. SSL_CTX_flush_sessions(this, 0);
  518. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, this, &ex_data);
  519. CRYPTO_MUTEX_cleanup(&lock);
  520. lh_SSL_SESSION_free(sessions);
  521. x509_method->ssl_ctx_free(this);
  522. }
  523. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  524. if (method == NULL) {
  525. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  526. return nullptr;
  527. }
  528. UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method);
  529. if (!ret) {
  530. return nullptr;
  531. }
  532. ret->cert = MakeUnique<CERT>(method->x509_method);
  533. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  534. ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null());
  535. if (ret->cert == nullptr ||
  536. ret->sessions == nullptr ||
  537. ret->client_CA == nullptr ||
  538. !ret->x509_method->ssl_ctx_new(ret.get())) {
  539. return nullptr;
  540. }
  541. if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) ||
  542. // Lock the SSL_CTX to the specified version, for compatibility with
  543. // legacy uses of SSL_METHOD.
  544. !SSL_CTX_set_max_proto_version(ret.get(), method->version) ||
  545. !SSL_CTX_set_min_proto_version(ret.get(), method->version)) {
  546. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  547. return nullptr;
  548. }
  549. return ret.release();
  550. }
  551. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  552. CRYPTO_refcount_inc(&ctx->references);
  553. return 1;
  554. }
  555. void SSL_CTX_free(SSL_CTX *ctx) {
  556. if (ctx == NULL ||
  557. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  558. return;
  559. }
  560. ctx->~ssl_ctx_st();
  561. OPENSSL_free(ctx);
  562. }
  563. ssl_st::ssl_st(SSL_CTX *ctx_arg)
  564. : method(ctx_arg->method),
  565. max_send_fragment(ctx_arg->max_send_fragment),
  566. msg_callback(ctx_arg->msg_callback),
  567. msg_callback_arg(ctx_arg->msg_callback_arg),
  568. ctx(UpRef(ctx_arg)),
  569. session_ctx(UpRef(ctx_arg)),
  570. options(ctx->options),
  571. mode(ctx->mode),
  572. max_cert_list(ctx->max_cert_list),
  573. server(false),
  574. quiet_shutdown(ctx->quiet_shutdown),
  575. enable_early_data(ctx->enable_early_data) {
  576. CRYPTO_new_ex_data(&ex_data);
  577. }
  578. ssl_st::~ssl_st() {
  579. CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data);
  580. // |config| refers to |this|, so we must release it earlier.
  581. config.reset();
  582. if (method != NULL) {
  583. method->ssl_free(this);
  584. }
  585. }
  586. SSL *SSL_new(SSL_CTX *ctx) {
  587. if (ctx == nullptr) {
  588. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  589. return nullptr;
  590. }
  591. UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx);
  592. if (ssl == nullptr) {
  593. return nullptr;
  594. }
  595. ssl->config = MakeUnique<SSL_CONFIG>(ssl.get());
  596. if (ssl->config == nullptr) {
  597. return nullptr;
  598. }
  599. ssl->config->conf_min_version = ctx->conf_min_version;
  600. ssl->config->conf_max_version = ctx->conf_max_version;
  601. ssl->config->cert = ssl_cert_dup(ctx->cert.get());
  602. if (ssl->config->cert == nullptr) {
  603. return nullptr;
  604. }
  605. ssl->config->verify_mode = ctx->verify_mode;
  606. ssl->config->verify_callback = ctx->default_verify_callback;
  607. ssl->config->custom_verify_callback = ctx->custom_verify_callback;
  608. ssl->config->retain_only_sha256_of_client_certs =
  609. ctx->retain_only_sha256_of_client_certs;
  610. if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) ||
  611. !ssl->config->alpn_client_proto_list.CopyFrom(
  612. ctx->alpn_client_proto_list) ||
  613. !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) {
  614. return nullptr;
  615. }
  616. if (ctx->psk_identity_hint) {
  617. ssl->config->psk_identity_hint.reset(
  618. BUF_strdup(ctx->psk_identity_hint.get()));
  619. if (ssl->config->psk_identity_hint == nullptr) {
  620. return nullptr;
  621. }
  622. }
  623. ssl->config->psk_client_callback = ctx->psk_client_callback;
  624. ssl->config->psk_server_callback = ctx->psk_server_callback;
  625. ssl->config->channel_id_enabled = ctx->channel_id_enabled;
  626. ssl->config->channel_id_private = UpRef(ctx->channel_id_private);
  627. ssl->config->signed_cert_timestamps_enabled =
  628. ctx->signed_cert_timestamps_enabled;
  629. ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  630. ssl->config->handoff = ctx->handoff;
  631. ssl->config->ignore_tls13_downgrade = ctx->ignore_tls13_downgrade;
  632. ssl->quic_method = ctx->quic_method;
  633. if (!ssl->method->ssl_new(ssl.get()) ||
  634. !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
  635. return nullptr;
  636. }
  637. return ssl.release();
  638. }
  639. SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
  640. : ssl(ssl_arg),
  641. signed_cert_timestamps_enabled(false),
  642. ocsp_stapling_enabled(false),
  643. channel_id_enabled(false),
  644. enforce_rsa_key_usage(false),
  645. retain_only_sha256_of_client_certs(false),
  646. handoff(false),
  647. shed_handshake_config(false),
  648. ignore_tls13_downgrade(false),
  649. jdk11_workaround(false) {
  650. assert(ssl);
  651. }
  652. SSL_CONFIG::~SSL_CONFIG() {
  653. if (ssl->ctx != nullptr) {
  654. ssl->ctx->x509_method->ssl_config_free(this);
  655. }
  656. }
  657. void SSL_free(SSL *ssl) {
  658. Delete(ssl);
  659. }
  660. void SSL_set_connect_state(SSL *ssl) {
  661. ssl->server = false;
  662. ssl->do_handshake = ssl_client_handshake;
  663. }
  664. void SSL_set_accept_state(SSL *ssl) {
  665. ssl->server = true;
  666. ssl->do_handshake = ssl_server_handshake;
  667. }
  668. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  669. ssl->rbio.reset(rbio);
  670. }
  671. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  672. ssl->wbio.reset(wbio);
  673. }
  674. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  675. // For historical reasons, this function has many different cases in ownership
  676. // handling.
  677. // If nothing has changed, do nothing
  678. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  679. return;
  680. }
  681. // If the two arguments are equal, one fewer reference is granted than
  682. // taken.
  683. if (rbio != NULL && rbio == wbio) {
  684. BIO_up_ref(rbio);
  685. }
  686. // If only the wbio is changed, adopt only one reference.
  687. if (rbio == SSL_get_rbio(ssl)) {
  688. SSL_set0_wbio(ssl, wbio);
  689. return;
  690. }
  691. // There is an asymmetry here for historical reasons. If only the rbio is
  692. // changed AND the rbio and wbio were originally different, then we only adopt
  693. // one reference.
  694. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  695. SSL_set0_rbio(ssl, rbio);
  696. return;
  697. }
  698. // Otherwise, adopt both references.
  699. SSL_set0_rbio(ssl, rbio);
  700. SSL_set0_wbio(ssl, wbio);
  701. }
  702. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); }
  703. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); }
  704. size_t SSL_quic_max_handshake_flight_len(const SSL *ssl,
  705. enum ssl_encryption_level_t level) {
  706. // Limits flights to 16K by default when there are no large
  707. // (certificate-carrying) messages.
  708. static const size_t kDefaultLimit = 16384;
  709. switch (level) {
  710. case ssl_encryption_initial:
  711. return kDefaultLimit;
  712. case ssl_encryption_early_data:
  713. // QUIC does not send EndOfEarlyData.
  714. return 0;
  715. case ssl_encryption_handshake:
  716. if (ssl->server) {
  717. // Servers may receive Certificate message if configured to request
  718. // client certificates.
  719. if (!!(ssl->config->verify_mode & SSL_VERIFY_PEER) &&
  720. ssl->max_cert_list > kDefaultLimit) {
  721. return ssl->max_cert_list;
  722. }
  723. } else {
  724. // Clients may receive both Certificate message and a CertificateRequest
  725. // message.
  726. if (2*ssl->max_cert_list > kDefaultLimit) {
  727. return 2*ssl->max_cert_list;
  728. }
  729. }
  730. return kDefaultLimit;
  731. case ssl_encryption_application:
  732. // Note there is not actually a bound on the number of NewSessionTickets
  733. // one may send in a row. This level may need more involved flow
  734. // control. See https://github.com/quicwg/base-drafts/issues/1834.
  735. return kDefaultLimit;
  736. }
  737. return 0;
  738. }
  739. enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) {
  740. return ssl->s3->read_level;
  741. }
  742. enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) {
  743. return ssl->s3->write_level;
  744. }
  745. int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
  746. const uint8_t *data, size_t len) {
  747. if (ssl->quic_method == nullptr) {
  748. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  749. return 0;
  750. }
  751. if (level != ssl->s3->read_level) {
  752. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
  753. return 0;
  754. }
  755. size_t new_len = (ssl->s3->hs_buf ? ssl->s3->hs_buf->length : 0) + len;
  756. if (new_len < len ||
  757. new_len > SSL_quic_max_handshake_flight_len(ssl, level)) {
  758. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  759. return 0;
  760. }
  761. return tls_append_handshake_data(ssl, MakeConstSpan(data, len));
  762. }
  763. int SSL_do_handshake(SSL *ssl) {
  764. ssl_reset_error_state(ssl);
  765. if (ssl->do_handshake == NULL) {
  766. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  767. return -1;
  768. }
  769. if (!SSL_in_init(ssl)) {
  770. return 1;
  771. }
  772. // Run the handshake.
  773. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  774. bool early_return = false;
  775. int ret = ssl_run_handshake(hs, &early_return);
  776. ssl_do_info_callback(
  777. ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
  778. if (ret <= 0) {
  779. return ret;
  780. }
  781. // Destroy the handshake object if the handshake has completely finished.
  782. if (!early_return) {
  783. ssl->s3->hs.reset();
  784. ssl_maybe_shed_handshake_config(ssl);
  785. }
  786. return 1;
  787. }
  788. int SSL_connect(SSL *ssl) {
  789. if (ssl->do_handshake == NULL) {
  790. // Not properly initialized yet
  791. SSL_set_connect_state(ssl);
  792. }
  793. return SSL_do_handshake(ssl);
  794. }
  795. int SSL_accept(SSL *ssl) {
  796. if (ssl->do_handshake == NULL) {
  797. // Not properly initialized yet
  798. SSL_set_accept_state(ssl);
  799. }
  800. return SSL_do_handshake(ssl);
  801. }
  802. static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
  803. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  804. return tls13_post_handshake(ssl, msg);
  805. }
  806. // Check for renegotiation on the server before parsing to use the correct
  807. // error. Renegotiation is triggered by a different message for servers.
  808. if (ssl->server) {
  809. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  810. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  811. return 0;
  812. }
  813. if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
  814. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  815. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  816. return 0;
  817. }
  818. if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
  819. return 1; // Ignore the HelloRequest.
  820. }
  821. if (!ssl_can_renegotiate(ssl) ||
  822. // Renegotiation is only supported at quiescent points in the application
  823. // protocol, namely in HTTPS, just before reading the HTTP response.
  824. // Require the record-layer be idle and avoid complexities of sending a
  825. // handshake record while an application_data record is being written.
  826. !ssl->s3->write_buffer.empty() ||
  827. ssl->s3->write_shutdown != ssl_shutdown_none) {
  828. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  829. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  830. return 0;
  831. }
  832. // Begin a new handshake.
  833. if (ssl->s3->hs != nullptr) {
  834. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  835. return 0;
  836. }
  837. ssl->s3->hs = ssl_handshake_new(ssl);
  838. if (ssl->s3->hs == nullptr) {
  839. return 0;
  840. }
  841. ssl->s3->total_renegotiations++;
  842. return 1;
  843. }
  844. int SSL_process_quic_post_handshake(SSL *ssl) {
  845. ssl_reset_error_state(ssl);
  846. if (SSL_in_init(ssl)) {
  847. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  848. return 0;
  849. }
  850. // Replay post-handshake message errors.
  851. if (!check_read_error(ssl)) {
  852. return 0;
  853. }
  854. // Process any buffered post-handshake messages.
  855. SSLMessage msg;
  856. while (ssl->method->get_message(ssl, &msg)) {
  857. // Handle the post-handshake message and try again.
  858. if (!ssl_do_post_handshake(ssl, msg)) {
  859. ssl_set_read_error(ssl);
  860. return 0;
  861. }
  862. ssl->method->next_message(ssl);
  863. }
  864. return 1;
  865. }
  866. static int ssl_read_impl(SSL *ssl) {
  867. ssl_reset_error_state(ssl);
  868. if (ssl->do_handshake == NULL) {
  869. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  870. return -1;
  871. }
  872. // Replay post-handshake message errors.
  873. if (!check_read_error(ssl)) {
  874. return -1;
  875. }
  876. while (ssl->s3->pending_app_data.empty()) {
  877. // Complete the current handshake, if any. False Start will cause
  878. // |SSL_do_handshake| to return mid-handshake, so this may require multiple
  879. // iterations.
  880. while (!ssl_can_read(ssl)) {
  881. int ret = SSL_do_handshake(ssl);
  882. if (ret < 0) {
  883. return ret;
  884. }
  885. if (ret == 0) {
  886. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  887. return -1;
  888. }
  889. }
  890. // Process any buffered post-handshake messages.
  891. SSLMessage msg;
  892. if (ssl->method->get_message(ssl, &msg)) {
  893. // If we received an interrupt in early read (EndOfEarlyData), loop again
  894. // for the handshake to process it.
  895. if (SSL_in_init(ssl)) {
  896. ssl->s3->hs->can_early_read = false;
  897. continue;
  898. }
  899. // Handle the post-handshake message and try again.
  900. if (!ssl_do_post_handshake(ssl, msg)) {
  901. ssl_set_read_error(ssl);
  902. return -1;
  903. }
  904. ssl->method->next_message(ssl);
  905. continue; // Loop again. We may have begun a new handshake.
  906. }
  907. uint8_t alert = SSL_AD_DECODE_ERROR;
  908. size_t consumed = 0;
  909. auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
  910. &alert, ssl->s3->read_buffer.span());
  911. bool retry;
  912. int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
  913. if (bio_ret <= 0) {
  914. return bio_ret;
  915. }
  916. if (!retry) {
  917. assert(!ssl->s3->pending_app_data.empty());
  918. ssl->s3->key_update_count = 0;
  919. }
  920. }
  921. return 1;
  922. }
  923. int SSL_read(SSL *ssl, void *buf, int num) {
  924. int ret = SSL_peek(ssl, buf, num);
  925. if (ret <= 0) {
  926. return ret;
  927. }
  928. // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
  929. // is not a stream. See https://crbug.com/boringssl/65.
  930. ssl->s3->pending_app_data =
  931. ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
  932. if (ssl->s3->pending_app_data.empty()) {
  933. ssl->s3->read_buffer.DiscardConsumed();
  934. }
  935. return ret;
  936. }
  937. int SSL_peek(SSL *ssl, void *buf, int num) {
  938. if (ssl->quic_method != nullptr) {
  939. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  940. return 0;
  941. }
  942. int ret = ssl_read_impl(ssl);
  943. if (ret <= 0) {
  944. return ret;
  945. }
  946. if (num <= 0) {
  947. return num;
  948. }
  949. size_t todo =
  950. std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
  951. OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
  952. return static_cast<int>(todo);
  953. }
  954. int SSL_write(SSL *ssl, const void *buf, int num) {
  955. ssl_reset_error_state(ssl);
  956. if (ssl->quic_method != nullptr) {
  957. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  958. return 0;
  959. }
  960. if (ssl->do_handshake == NULL) {
  961. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  962. return -1;
  963. }
  964. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  965. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  966. return -1;
  967. }
  968. int ret = 0;
  969. bool needs_handshake = false;
  970. do {
  971. // If necessary, complete the handshake implicitly.
  972. if (!ssl_can_write(ssl)) {
  973. ret = SSL_do_handshake(ssl);
  974. if (ret < 0) {
  975. return ret;
  976. }
  977. if (ret == 0) {
  978. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  979. return -1;
  980. }
  981. }
  982. ret = ssl->method->write_app_data(ssl, &needs_handshake,
  983. (const uint8_t *)buf, num);
  984. } while (needs_handshake);
  985. return ret;
  986. }
  987. int SSL_key_update(SSL *ssl, int request_type) {
  988. ssl_reset_error_state(ssl);
  989. if (ssl->do_handshake == NULL) {
  990. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  991. return 0;
  992. }
  993. if (ssl->ctx->quic_method != nullptr) {
  994. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  995. return 0;
  996. }
  997. if (!ssl->s3->initial_handshake_complete) {
  998. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
  999. return 0;
  1000. }
  1001. if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  1003. return 0;
  1004. }
  1005. if (!ssl->s3->key_update_pending &&
  1006. !tls13_add_key_update(ssl, request_type)) {
  1007. return 0;
  1008. }
  1009. return 1;
  1010. }
  1011. int SSL_shutdown(SSL *ssl) {
  1012. ssl_reset_error_state(ssl);
  1013. if (ssl->do_handshake == NULL) {
  1014. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  1015. return -1;
  1016. }
  1017. // If we are in the middle of a handshake, silently succeed. Consumers often
  1018. // call this function before |SSL_free|, whether the handshake succeeded or
  1019. // not. We assume the caller has already handled failed handshakes.
  1020. if (SSL_in_init(ssl)) {
  1021. return 1;
  1022. }
  1023. if (ssl->quiet_shutdown) {
  1024. // Do nothing if configured not to send a close_notify.
  1025. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  1026. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1027. return 1;
  1028. }
  1029. // This function completes in two stages. It sends a close_notify and then it
  1030. // waits for a close_notify to come in. Perform exactly one action and return
  1031. // whether or not it succeeds.
  1032. if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
  1033. // Send a close_notify.
  1034. if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  1035. return -1;
  1036. }
  1037. } else if (ssl->s3->alert_dispatch) {
  1038. // Finish sending the close_notify.
  1039. if (ssl->method->dispatch_alert(ssl) <= 0) {
  1040. return -1;
  1041. }
  1042. } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  1043. if (SSL_is_dtls(ssl)) {
  1044. // Bidirectional shutdown doesn't make sense for an unordered
  1045. // transport. DTLS alerts also aren't delivered reliably, so we may even
  1046. // time out because the peer never received our close_notify. Report to
  1047. // the caller that the channel has fully shut down.
  1048. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  1049. ERR_restore_state(ssl->s3->read_error.get());
  1050. return -1;
  1051. }
  1052. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1053. } else {
  1054. // Process records until an error, close_notify, or application data.
  1055. if (ssl_read_impl(ssl) > 0) {
  1056. // We received some unexpected application data.
  1057. OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN);
  1058. return -1;
  1059. }
  1060. if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  1061. return -1;
  1062. }
  1063. }
  1064. }
  1065. // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
  1066. return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
  1067. }
  1068. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  1069. if (ssl->s3->alert_dispatch) {
  1070. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  1071. ssl->s3->send_alert[1] != alert) {
  1072. // We are already attempting to write a different alert.
  1073. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1074. return -1;
  1075. }
  1076. return ssl->method->dispatch_alert(ssl);
  1077. }
  1078. return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1079. }
  1080. int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
  1081. size_t params_len) {
  1082. return ssl->config && ssl->config->quic_transport_params.CopyFrom(
  1083. MakeConstSpan(params, params_len));
  1084. }
  1085. void SSL_get_peer_quic_transport_params(const SSL *ssl,
  1086. const uint8_t **out_params,
  1087. size_t *out_params_len) {
  1088. *out_params = ssl->s3->peer_quic_transport_params.data();
  1089. *out_params_len = ssl->s3->peer_quic_transport_params.size();
  1090. }
  1091. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  1092. ctx->enable_early_data = !!enabled;
  1093. }
  1094. void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
  1095. ssl->enable_early_data = !!enabled;
  1096. }
  1097. int SSL_in_early_data(const SSL *ssl) {
  1098. if (ssl->s3->hs == NULL) {
  1099. return 0;
  1100. }
  1101. return ssl->s3->hs->in_early_data;
  1102. }
  1103. int SSL_early_data_accepted(const SSL *ssl) {
  1104. return ssl->s3->early_data_accepted;
  1105. }
  1106. void SSL_reset_early_data_reject(SSL *ssl) {
  1107. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  1108. if (hs == NULL ||
  1109. hs->wait != ssl_hs_early_data_rejected) {
  1110. abort();
  1111. }
  1112. hs->wait = ssl_hs_ok;
  1113. hs->in_early_data = false;
  1114. hs->early_session.reset();
  1115. // Discard any unfinished writes from the perspective of |SSL_write|'s
  1116. // retry. The handshake will transparently flush out the pending record
  1117. // (discarded by the server) to keep the framing correct.
  1118. ssl->s3->wpend_pending = false;
  1119. }
  1120. static int bio_retry_reason_to_error(int reason) {
  1121. switch (reason) {
  1122. case BIO_RR_CONNECT:
  1123. return SSL_ERROR_WANT_CONNECT;
  1124. case BIO_RR_ACCEPT:
  1125. return SSL_ERROR_WANT_ACCEPT;
  1126. default:
  1127. return SSL_ERROR_SYSCALL;
  1128. }
  1129. }
  1130. int SSL_get_error(const SSL *ssl, int ret_code) {
  1131. if (ret_code > 0) {
  1132. return SSL_ERROR_NONE;
  1133. }
  1134. // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1135. // where we do encode the error
  1136. uint32_t err = ERR_peek_error();
  1137. if (err != 0) {
  1138. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1139. return SSL_ERROR_SYSCALL;
  1140. }
  1141. return SSL_ERROR_SSL;
  1142. }
  1143. if (ret_code == 0) {
  1144. if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
  1145. return SSL_ERROR_ZERO_RETURN;
  1146. }
  1147. // An EOF was observed which violates the protocol, and the underlying
  1148. // transport does not participate in the error queue. Bubble up to the
  1149. // caller.
  1150. return SSL_ERROR_SYSCALL;
  1151. }
  1152. switch (ssl->s3->rwstate) {
  1153. case SSL_PENDING_SESSION:
  1154. return SSL_ERROR_PENDING_SESSION;
  1155. case SSL_CERTIFICATE_SELECTION_PENDING:
  1156. return SSL_ERROR_PENDING_CERTIFICATE;
  1157. case SSL_HANDOFF:
  1158. return SSL_ERROR_HANDOFF;
  1159. case SSL_HANDBACK:
  1160. return SSL_ERROR_HANDBACK;
  1161. case SSL_READING: {
  1162. if (ssl->quic_method) {
  1163. return SSL_ERROR_WANT_READ;
  1164. }
  1165. BIO *bio = SSL_get_rbio(ssl);
  1166. if (BIO_should_read(bio)) {
  1167. return SSL_ERROR_WANT_READ;
  1168. }
  1169. if (BIO_should_write(bio)) {
  1170. // TODO(davidben): OpenSSL historically checked for writes on the read
  1171. // BIO. Can this be removed?
  1172. return SSL_ERROR_WANT_WRITE;
  1173. }
  1174. if (BIO_should_io_special(bio)) {
  1175. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1176. }
  1177. break;
  1178. }
  1179. case SSL_WRITING: {
  1180. BIO *bio = SSL_get_wbio(ssl);
  1181. if (BIO_should_write(bio)) {
  1182. return SSL_ERROR_WANT_WRITE;
  1183. }
  1184. if (BIO_should_read(bio)) {
  1185. // TODO(davidben): OpenSSL historically checked for reads on the write
  1186. // BIO. Can this be removed?
  1187. return SSL_ERROR_WANT_READ;
  1188. }
  1189. if (BIO_should_io_special(bio)) {
  1190. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1191. }
  1192. break;
  1193. }
  1194. case SSL_X509_LOOKUP:
  1195. return SSL_ERROR_WANT_X509_LOOKUP;
  1196. case SSL_CHANNEL_ID_LOOKUP:
  1197. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1198. case SSL_PRIVATE_KEY_OPERATION:
  1199. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  1200. case SSL_PENDING_TICKET:
  1201. return SSL_ERROR_PENDING_TICKET;
  1202. case SSL_EARLY_DATA_REJECTED:
  1203. return SSL_ERROR_EARLY_DATA_REJECTED;
  1204. case SSL_CERTIFICATE_VERIFY:
  1205. return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
  1206. }
  1207. return SSL_ERROR_SYSCALL;
  1208. }
  1209. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  1210. ctx->options |= options;
  1211. return ctx->options;
  1212. }
  1213. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  1214. ctx->options &= ~options;
  1215. return ctx->options;
  1216. }
  1217. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  1218. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  1219. ssl->options |= options;
  1220. return ssl->options;
  1221. }
  1222. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  1223. ssl->options &= ~options;
  1224. return ssl->options;
  1225. }
  1226. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  1227. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  1228. ctx->mode |= mode;
  1229. return ctx->mode;
  1230. }
  1231. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  1232. ctx->mode &= ~mode;
  1233. return ctx->mode;
  1234. }
  1235. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  1236. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  1237. ssl->mode |= mode;
  1238. return ssl->mode;
  1239. }
  1240. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  1241. ssl->mode &= ~mode;
  1242. return ssl->mode;
  1243. }
  1244. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  1245. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  1246. ctx->pool = pool;
  1247. }
  1248. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  1249. size_t max_out) {
  1250. *out_len = 0;
  1251. OPENSSL_memset(out, 0, max_out);
  1252. // tls-unique is not defined for TLS 1.3.
  1253. if (!ssl->s3->initial_handshake_complete ||
  1254. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1255. return 0;
  1256. }
  1257. // The tls-unique value is the first Finished message in the handshake, which
  1258. // is the client's in a full handshake and the server's for a resumption. See
  1259. // https://tools.ietf.org/html/rfc5929#section-3.1.
  1260. const uint8_t *finished = ssl->s3->previous_client_finished;
  1261. size_t finished_len = ssl->s3->previous_client_finished_len;
  1262. if (ssl->session != NULL) {
  1263. // tls-unique is broken for resumed sessions unless EMS is used.
  1264. if (!ssl->session->extended_master_secret) {
  1265. return 0;
  1266. }
  1267. finished = ssl->s3->previous_server_finished;
  1268. finished_len = ssl->s3->previous_server_finished_len;
  1269. }
  1270. *out_len = finished_len;
  1271. if (finished_len > max_out) {
  1272. *out_len = max_out;
  1273. }
  1274. OPENSSL_memcpy(out, finished, *out_len);
  1275. return 1;
  1276. }
  1277. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  1278. size_t sid_ctx_len) {
  1279. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  1280. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  1281. return 0;
  1282. }
  1283. static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
  1284. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  1285. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  1286. return 1;
  1287. }
  1288. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  1289. size_t sid_ctx_len) {
  1290. return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len);
  1291. }
  1292. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  1293. size_t sid_ctx_len) {
  1294. if (!ssl->config) {
  1295. return 0;
  1296. }
  1297. return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len);
  1298. }
  1299. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  1300. if (!ssl->config) {
  1301. assert(ssl->config);
  1302. *out_len = 0;
  1303. return NULL;
  1304. }
  1305. *out_len = ssl->config->cert->sid_ctx_length;
  1306. return ssl->config->cert->sid_ctx;
  1307. }
  1308. void SSL_certs_clear(SSL *ssl) {
  1309. if (!ssl->config) {
  1310. return;
  1311. }
  1312. ssl_cert_clear_certs(ssl->config->cert.get());
  1313. }
  1314. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  1315. int SSL_get_rfd(const SSL *ssl) {
  1316. int ret = -1;
  1317. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  1318. if (b != NULL) {
  1319. BIO_get_fd(b, &ret);
  1320. }
  1321. return ret;
  1322. }
  1323. int SSL_get_wfd(const SSL *ssl) {
  1324. int ret = -1;
  1325. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  1326. if (b != NULL) {
  1327. BIO_get_fd(b, &ret);
  1328. }
  1329. return ret;
  1330. }
  1331. int SSL_set_fd(SSL *ssl, int fd) {
  1332. BIO *bio = BIO_new(BIO_s_socket());
  1333. if (bio == NULL) {
  1334. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1335. return 0;
  1336. }
  1337. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1338. SSL_set_bio(ssl, bio, bio);
  1339. return 1;
  1340. }
  1341. int SSL_set_wfd(SSL *ssl, int fd) {
  1342. BIO *rbio = SSL_get_rbio(ssl);
  1343. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  1344. BIO_get_fd(rbio, NULL) != fd) {
  1345. BIO *bio = BIO_new(BIO_s_socket());
  1346. if (bio == NULL) {
  1347. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1348. return 0;
  1349. }
  1350. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1351. SSL_set0_wbio(ssl, bio);
  1352. } else {
  1353. // Copy the rbio over to the wbio.
  1354. BIO_up_ref(rbio);
  1355. SSL_set0_wbio(ssl, rbio);
  1356. }
  1357. return 1;
  1358. }
  1359. int SSL_set_rfd(SSL *ssl, int fd) {
  1360. BIO *wbio = SSL_get_wbio(ssl);
  1361. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  1362. BIO_get_fd(wbio, NULL) != fd) {
  1363. BIO *bio = BIO_new(BIO_s_socket());
  1364. if (bio == NULL) {
  1365. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1366. return 0;
  1367. }
  1368. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1369. SSL_set0_rbio(ssl, bio);
  1370. } else {
  1371. // Copy the wbio over to the rbio.
  1372. BIO_up_ref(wbio);
  1373. SSL_set0_rbio(ssl, wbio);
  1374. }
  1375. return 1;
  1376. }
  1377. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  1378. size_t in_len) {
  1379. if (out_len > in_len) {
  1380. out_len = in_len;
  1381. }
  1382. OPENSSL_memcpy(out, in, out_len);
  1383. return in_len;
  1384. }
  1385. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1386. if (!ssl->s3->initial_handshake_complete ||
  1387. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1388. return 0;
  1389. }
  1390. if (ssl->server) {
  1391. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1392. ssl->s3->previous_server_finished_len);
  1393. }
  1394. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1395. ssl->s3->previous_client_finished_len);
  1396. }
  1397. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1398. if (!ssl->s3->initial_handshake_complete ||
  1399. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1400. return 0;
  1401. }
  1402. if (ssl->server) {
  1403. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1404. ssl->s3->previous_client_finished_len);
  1405. }
  1406. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1407. ssl->s3->previous_server_finished_len);
  1408. }
  1409. int SSL_get_verify_mode(const SSL *ssl) {
  1410. if (!ssl->config) {
  1411. assert(ssl->config);
  1412. return -1;
  1413. }
  1414. return ssl->config->verify_mode;
  1415. }
  1416. int SSL_get_extms_support(const SSL *ssl) {
  1417. // TLS 1.3 does not require extended master secret and always reports as
  1418. // supporting it.
  1419. if (!ssl->s3->have_version) {
  1420. return 0;
  1421. }
  1422. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1423. return 1;
  1424. }
  1425. // If the initial handshake completed, query the established session.
  1426. if (ssl->s3->established_session != NULL) {
  1427. return ssl->s3->established_session->extended_master_secret;
  1428. }
  1429. // Otherwise, query the in-progress handshake.
  1430. if (ssl->s3->hs != NULL) {
  1431. return ssl->s3->hs->extended_master_secret;
  1432. }
  1433. assert(0);
  1434. return 0;
  1435. }
  1436. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1437. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1438. int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; }
  1439. int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; }
  1440. int SSL_pending(const SSL *ssl) {
  1441. return static_cast<int>(ssl->s3->pending_app_data.size());
  1442. }
  1443. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1444. return ssl_cert_check_private_key(ctx->cert.get(),
  1445. ctx->cert->privatekey.get());
  1446. }
  1447. int SSL_check_private_key(const SSL *ssl) {
  1448. if (!ssl->config) {
  1449. return 0;
  1450. }
  1451. return ssl_cert_check_private_key(ssl->config->cert.get(),
  1452. ssl->config->cert->privatekey.get());
  1453. }
  1454. long SSL_get_default_timeout(const SSL *ssl) {
  1455. return SSL_DEFAULT_SESSION_TIMEOUT;
  1456. }
  1457. int SSL_renegotiate(SSL *ssl) {
  1458. // Caller-initiated renegotiation is not supported.
  1459. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1460. return 0;
  1461. }
  1462. int SSL_renegotiate_pending(SSL *ssl) {
  1463. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1464. }
  1465. int SSL_total_renegotiations(const SSL *ssl) {
  1466. return ssl->s3->total_renegotiations;
  1467. }
  1468. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1469. return ctx->max_cert_list;
  1470. }
  1471. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1472. if (max_cert_list > kMaxHandshakeSize) {
  1473. max_cert_list = kMaxHandshakeSize;
  1474. }
  1475. ctx->max_cert_list = (uint32_t)max_cert_list;
  1476. }
  1477. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1478. return ssl->max_cert_list;
  1479. }
  1480. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1481. if (max_cert_list > kMaxHandshakeSize) {
  1482. max_cert_list = kMaxHandshakeSize;
  1483. }
  1484. ssl->max_cert_list = (uint32_t)max_cert_list;
  1485. }
  1486. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1487. if (max_send_fragment < 512) {
  1488. max_send_fragment = 512;
  1489. }
  1490. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1491. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1492. }
  1493. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1494. return 1;
  1495. }
  1496. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1497. if (max_send_fragment < 512) {
  1498. max_send_fragment = 512;
  1499. }
  1500. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1501. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1502. }
  1503. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1504. return 1;
  1505. }
  1506. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1507. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1508. return 0;
  1509. }
  1510. ssl->d1->mtu = mtu;
  1511. return 1;
  1512. }
  1513. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1514. if (!ssl->s3->have_version) {
  1515. return 0;
  1516. }
  1517. return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  1518. ssl->s3->send_connection_binding;
  1519. }
  1520. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1521. MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
  1522. return lh_SSL_SESSION_num_items(ctx->sessions);
  1523. }
  1524. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1525. unsigned long ret = ctx->session_cache_size;
  1526. ctx->session_cache_size = size;
  1527. return ret;
  1528. }
  1529. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1530. return ctx->session_cache_size;
  1531. }
  1532. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1533. int ret = ctx->session_cache_mode;
  1534. ctx->session_cache_mode = mode;
  1535. return ret;
  1536. }
  1537. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1538. return ctx->session_cache_mode;
  1539. }
  1540. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1541. if (out == NULL) {
  1542. return 48;
  1543. }
  1544. if (len != 48) {
  1545. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1546. return 0;
  1547. }
  1548. // The default ticket keys are initialized lazily. Trigger a key
  1549. // rotation to initialize them.
  1550. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  1551. return 0;
  1552. }
  1553. uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
  1554. MutexReadLock lock(&ctx->lock);
  1555. OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16);
  1556. OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16);
  1557. OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16);
  1558. return 1;
  1559. }
  1560. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1561. if (in == NULL) {
  1562. return 48;
  1563. }
  1564. if (len != 48) {
  1565. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1566. return 0;
  1567. }
  1568. auto key = MakeUnique<TicketKey>();
  1569. if (!key) {
  1570. return 0;
  1571. }
  1572. const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
  1573. OPENSSL_memcpy(key->name, in_bytes, 16);
  1574. OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16);
  1575. OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16);
  1576. // Disable automatic key rotation for manually-configured keys. This is now
  1577. // the caller's responsibility.
  1578. key->next_rotation_tv_sec = 0;
  1579. ctx->ticket_key_current = std::move(key);
  1580. ctx->ticket_key_prev.reset();
  1581. return 1;
  1582. }
  1583. int SSL_CTX_set_tlsext_ticket_key_cb(
  1584. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1585. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1586. int encrypt)) {
  1587. ctx->ticket_key_cb = callback;
  1588. return 1;
  1589. }
  1590. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1591. return tls1_set_curves(&ctx->supported_group_list,
  1592. MakeConstSpan(curves, curves_len));
  1593. }
  1594. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1595. if (!ssl->config) {
  1596. return 0;
  1597. }
  1598. return tls1_set_curves(&ssl->config->supported_group_list,
  1599. MakeConstSpan(curves, curves_len));
  1600. }
  1601. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1602. return tls1_set_curves_list(&ctx->supported_group_list, curves);
  1603. }
  1604. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1605. if (!ssl->config) {
  1606. return 0;
  1607. }
  1608. return tls1_set_curves_list(&ssl->config->supported_group_list, curves);
  1609. }
  1610. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1611. // TODO(davidben): This checks the wrong session if there is a renegotiation
  1612. // in progress.
  1613. SSL_SESSION *session = SSL_get_session(ssl);
  1614. if (session == NULL) {
  1615. return 0;
  1616. }
  1617. return session->group_id;
  1618. }
  1619. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1620. return 1;
  1621. }
  1622. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1623. return 1;
  1624. }
  1625. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
  1626. return ctx->cipher_list->ciphers.get();
  1627. }
  1628. int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
  1629. if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
  1630. return 0;
  1631. }
  1632. return ctx->cipher_list->in_group_flags[i];
  1633. }
  1634. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1635. if (ssl == NULL) {
  1636. return NULL;
  1637. }
  1638. if (ssl->config == NULL) {
  1639. assert(ssl->config);
  1640. return NULL;
  1641. }
  1642. return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
  1643. : ssl->ctx->cipher_list->ciphers.get();
  1644. }
  1645. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1646. if (ssl == NULL) {
  1647. return NULL;
  1648. }
  1649. STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
  1650. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1651. return NULL;
  1652. }
  1653. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
  1654. if (c == NULL) {
  1655. return NULL;
  1656. }
  1657. return c->name;
  1658. }
  1659. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1660. return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
  1661. }
  1662. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1663. return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
  1664. }
  1665. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1666. if (!ssl->config) {
  1667. return 0;
  1668. }
  1669. return ssl_create_cipher_list(&ssl->config->cipher_list, str,
  1670. false /* not strict */);
  1671. }
  1672. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1673. if (!ssl->config) {
  1674. return 0;
  1675. }
  1676. return ssl_create_cipher_list(&ssl->config->cipher_list, str,
  1677. true /* strict */);
  1678. }
  1679. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1680. if (type != TLSEXT_NAMETYPE_host_name) {
  1681. return NULL;
  1682. }
  1683. // Historically, |SSL_get_servername| was also the configuration getter
  1684. // corresponding to |SSL_set_tlsext_host_name|.
  1685. if (ssl->hostname != nullptr) {
  1686. return ssl->hostname.get();
  1687. }
  1688. return ssl->s3->hostname.get();
  1689. }
  1690. int SSL_get_servername_type(const SSL *ssl) {
  1691. if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
  1692. return -1;
  1693. }
  1694. return TLSEXT_NAMETYPE_host_name;
  1695. }
  1696. void SSL_CTX_set_custom_verify(
  1697. SSL_CTX *ctx, int mode,
  1698. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1699. ctx->verify_mode = mode;
  1700. ctx->custom_verify_callback = callback;
  1701. }
  1702. void SSL_set_custom_verify(
  1703. SSL *ssl, int mode,
  1704. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1705. if (!ssl->config) {
  1706. return;
  1707. }
  1708. ssl->config->verify_mode = mode;
  1709. ssl->config->custom_verify_callback = callback;
  1710. }
  1711. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1712. ctx->signed_cert_timestamps_enabled = true;
  1713. }
  1714. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1715. if (!ssl->config) {
  1716. return;
  1717. }
  1718. ssl->config->signed_cert_timestamps_enabled = true;
  1719. }
  1720. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1721. ctx->ocsp_stapling_enabled = true;
  1722. }
  1723. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1724. if (!ssl->config) {
  1725. return;
  1726. }
  1727. ssl->config->ocsp_stapling_enabled = true;
  1728. }
  1729. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1730. size_t *out_len) {
  1731. SSL_SESSION *session = SSL_get_session(ssl);
  1732. if (ssl->server || !session || !session->signed_cert_timestamp_list) {
  1733. *out_len = 0;
  1734. *out = NULL;
  1735. return;
  1736. }
  1737. *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get());
  1738. *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get());
  1739. }
  1740. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1741. size_t *out_len) {
  1742. SSL_SESSION *session = SSL_get_session(ssl);
  1743. if (ssl->server || !session || !session->ocsp_response) {
  1744. *out_len = 0;
  1745. *out = NULL;
  1746. return;
  1747. }
  1748. *out = CRYPTO_BUFFER_data(session->ocsp_response.get());
  1749. *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get());
  1750. }
  1751. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1752. ssl->hostname.reset();
  1753. if (name == nullptr) {
  1754. return 1;
  1755. }
  1756. size_t len = strlen(name);
  1757. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1758. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1759. return 0;
  1760. }
  1761. ssl->hostname.reset(BUF_strdup(name));
  1762. if (ssl->hostname == nullptr) {
  1763. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1764. return 0;
  1765. }
  1766. return 1;
  1767. }
  1768. int SSL_CTX_set_tlsext_servername_callback(
  1769. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1770. ctx->servername_callback = callback;
  1771. return 1;
  1772. }
  1773. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1774. ctx->servername_arg = arg;
  1775. return 1;
  1776. }
  1777. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
  1778. unsigned peer_len, const uint8_t *supported,
  1779. unsigned supported_len) {
  1780. const uint8_t *result;
  1781. int status;
  1782. // For each protocol in peer preference order, see if we support it.
  1783. for (unsigned i = 0; i < peer_len;) {
  1784. for (unsigned j = 0; j < supported_len;) {
  1785. if (peer[i] == supported[j] &&
  1786. OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
  1787. // We found a match
  1788. result = &peer[i];
  1789. status = OPENSSL_NPN_NEGOTIATED;
  1790. goto found;
  1791. }
  1792. j += supported[j];
  1793. j++;
  1794. }
  1795. i += peer[i];
  1796. i++;
  1797. }
  1798. // There's no overlap between our protocols and the peer's list.
  1799. result = supported;
  1800. status = OPENSSL_NPN_NO_OVERLAP;
  1801. found:
  1802. *out = (uint8_t *)result + 1;
  1803. *out_len = result[0];
  1804. return status;
  1805. }
  1806. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1807. unsigned *out_len) {
  1808. *out_data = ssl->s3->next_proto_negotiated.data();
  1809. *out_len = ssl->s3->next_proto_negotiated.size();
  1810. }
  1811. void SSL_CTX_set_next_protos_advertised_cb(
  1812. SSL_CTX *ctx,
  1813. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1814. void *arg) {
  1815. ctx->next_protos_advertised_cb = cb;
  1816. ctx->next_protos_advertised_cb_arg = arg;
  1817. }
  1818. void SSL_CTX_set_next_proto_select_cb(
  1819. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1820. const uint8_t *in, unsigned in_len, void *arg),
  1821. void *arg) {
  1822. ctx->next_proto_select_cb = cb;
  1823. ctx->next_proto_select_cb_arg = arg;
  1824. }
  1825. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1826. unsigned protos_len) {
  1827. // Note this function's calling convention is backwards.
  1828. return ctx->alpn_client_proto_list.CopyFrom(MakeConstSpan(protos, protos_len))
  1829. ? 0
  1830. : 1;
  1831. }
  1832. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1833. // Note this function's calling convention is backwards.
  1834. if (!ssl->config) {
  1835. return 1;
  1836. }
  1837. return ssl->config->alpn_client_proto_list.CopyFrom(
  1838. MakeConstSpan(protos, protos_len))
  1839. ? 0
  1840. : 1;
  1841. }
  1842. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1843. int (*cb)(SSL *ssl, const uint8_t **out,
  1844. uint8_t *out_len, const uint8_t *in,
  1845. unsigned in_len, void *arg),
  1846. void *arg) {
  1847. ctx->alpn_select_cb = cb;
  1848. ctx->alpn_select_cb_arg = arg;
  1849. }
  1850. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1851. unsigned *out_len) {
  1852. if (SSL_in_early_data(ssl) && !ssl->server) {
  1853. *out_data = ssl->s3->hs->early_session->early_alpn.data();
  1854. *out_len = ssl->s3->hs->early_session->early_alpn.size();
  1855. } else {
  1856. *out_data = ssl->s3->alpn_selected.data();
  1857. *out_len = ssl->s3->alpn_selected.size();
  1858. }
  1859. }
  1860. void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
  1861. ctx->allow_unknown_alpn_protos = !!enabled;
  1862. }
  1863. int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id,
  1864. ssl_cert_compression_func_t compress,
  1865. ssl_cert_decompression_func_t decompress) {
  1866. assert(compress != nullptr || decompress != nullptr);
  1867. for (const auto *alg : ctx->cert_compression_algs.get()) {
  1868. if (alg->alg_id == alg_id) {
  1869. return 0;
  1870. }
  1871. }
  1872. UniquePtr<CertCompressionAlg> alg = MakeUnique<CertCompressionAlg>();
  1873. if (alg == nullptr) {
  1874. return 0;
  1875. }
  1876. alg->alg_id = alg_id;
  1877. alg->compress = compress;
  1878. alg->decompress = decompress;
  1879. if (ctx->cert_compression_algs == nullptr) {
  1880. ctx->cert_compression_algs.reset(sk_CertCompressionAlg_new_null());
  1881. if (ctx->cert_compression_algs == nullptr) {
  1882. return 0;
  1883. }
  1884. }
  1885. if (!PushToStack(ctx->cert_compression_algs.get(), std::move(alg))) {
  1886. if (sk_CertCompressionAlg_num(ctx->cert_compression_algs.get()) == 0) {
  1887. ctx->cert_compression_algs.reset();
  1888. }
  1889. return 0;
  1890. }
  1891. return 1;
  1892. }
  1893. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1894. ctx->channel_id_enabled = !!enabled;
  1895. }
  1896. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1897. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1898. return 1;
  1899. }
  1900. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1901. if (!ssl->config) {
  1902. return;
  1903. }
  1904. ssl->config->channel_id_enabled = !!enabled;
  1905. }
  1906. int SSL_enable_tls_channel_id(SSL *ssl) {
  1907. SSL_set_tls_channel_id_enabled(ssl, 1);
  1908. return 1;
  1909. }
  1910. static int is_p256_key(EVP_PKEY *private_key) {
  1911. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1912. return ec_key != NULL &&
  1913. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1914. NID_X9_62_prime256v1;
  1915. }
  1916. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1917. if (!is_p256_key(private_key)) {
  1918. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1919. return 0;
  1920. }
  1921. ctx->channel_id_private = UpRef(private_key);
  1922. ctx->channel_id_enabled = true;
  1923. return 1;
  1924. }
  1925. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1926. if (!ssl->config) {
  1927. return 0;
  1928. }
  1929. if (!is_p256_key(private_key)) {
  1930. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1931. return 0;
  1932. }
  1933. ssl->config->channel_id_private = UpRef(private_key);
  1934. ssl->config->channel_id_enabled = true;
  1935. return 1;
  1936. }
  1937. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1938. if (!ssl->s3->channel_id_valid) {
  1939. return 0;
  1940. }
  1941. OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64);
  1942. return 64;
  1943. }
  1944. int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
  1945. if (!ssl->config) {
  1946. return 0;
  1947. }
  1948. if (len > 256) {
  1949. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  1950. return 0;
  1951. }
  1952. return ssl->config->token_binding_params.CopyFrom(MakeConstSpan(params, len));
  1953. }
  1954. int SSL_is_token_binding_negotiated(const SSL *ssl) {
  1955. return ssl->s3->token_binding_negotiated;
  1956. }
  1957. uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
  1958. return ssl->s3->negotiated_token_binding_param;
  1959. }
  1960. size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) {
  1961. Span<const uint8_t> types;
  1962. if (!ssl->server && ssl->s3->hs != nullptr) {
  1963. types = ssl->s3->hs->certificate_types;
  1964. }
  1965. *out_types = types.data();
  1966. return types.size();
  1967. }
  1968. size_t SSL_get0_peer_verify_algorithms(const SSL *ssl,
  1969. const uint16_t **out_sigalgs) {
  1970. Span<const uint16_t> sigalgs;
  1971. if (ssl->s3->hs != nullptr) {
  1972. sigalgs = ssl->s3->hs->peer_sigalgs;
  1973. }
  1974. *out_sigalgs = sigalgs.data();
  1975. return sigalgs.size();
  1976. }
  1977. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1978. if (!ssl->config) {
  1979. assert(ssl->config);
  1980. return NULL;
  1981. }
  1982. if (ssl->config->cert != NULL) {
  1983. return ssl->config->cert->privatekey.get();
  1984. }
  1985. return NULL;
  1986. }
  1987. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1988. if (ctx->cert != NULL) {
  1989. return ctx->cert->privatekey.get();
  1990. }
  1991. return NULL;
  1992. }
  1993. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1994. const SSL_SESSION *session = SSL_get_session(ssl);
  1995. return session == nullptr ? nullptr : session->cipher;
  1996. }
  1997. int SSL_session_reused(const SSL *ssl) {
  1998. return ssl->s3->session_reused || SSL_in_early_data(ssl);
  1999. }
  2000. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  2001. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  2002. int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  2003. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  2004. ctx->quiet_shutdown = (mode != 0);
  2005. }
  2006. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  2007. return ctx->quiet_shutdown;
  2008. }
  2009. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  2010. ssl->quiet_shutdown = (mode != 0);
  2011. }
  2012. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  2013. void SSL_set_shutdown(SSL *ssl, int mode) {
  2014. // It is an error to clear any bits that have already been set. (We can't try
  2015. // to get a second close_notify or send two.)
  2016. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  2017. if (mode & SSL_RECEIVED_SHUTDOWN &&
  2018. ssl->s3->read_shutdown == ssl_shutdown_none) {
  2019. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  2020. }
  2021. if (mode & SSL_SENT_SHUTDOWN &&
  2022. ssl->s3->write_shutdown == ssl_shutdown_none) {
  2023. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  2024. }
  2025. }
  2026. int SSL_get_shutdown(const SSL *ssl) {
  2027. int ret = 0;
  2028. if (ssl->s3->read_shutdown != ssl_shutdown_none) {
  2029. // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  2030. // and fatal alert.
  2031. ret |= SSL_RECEIVED_SHUTDOWN;
  2032. }
  2033. if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
  2034. // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
  2035. ret |= SSL_SENT_SHUTDOWN;
  2036. }
  2037. return ret;
  2038. }
  2039. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); }
  2040. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  2041. if (!ssl->config) {
  2042. return NULL;
  2043. }
  2044. if (ssl->ctx.get() == ctx) {
  2045. return ssl->ctx.get();
  2046. }
  2047. // One cannot change the X.509 callbacks during a connection.
  2048. if (ssl->ctx->x509_method != ctx->x509_method) {
  2049. assert(0);
  2050. return NULL;
  2051. }
  2052. UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get());
  2053. if (!new_cert) {
  2054. return nullptr;
  2055. }
  2056. ssl->config->cert = std::move(new_cert);
  2057. ssl->ctx = UpRef(ctx);
  2058. ssl->enable_early_data = ssl->ctx->enable_early_data;
  2059. return ssl->ctx.get();
  2060. }
  2061. void SSL_set_info_callback(SSL *ssl,
  2062. void (*cb)(const SSL *ssl, int type, int value)) {
  2063. ssl->info_callback = cb;
  2064. }
  2065. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  2066. int value) {
  2067. return ssl->info_callback;
  2068. }
  2069. int SSL_state(const SSL *ssl) {
  2070. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  2071. }
  2072. void SSL_set_state(SSL *ssl, int state) { }
  2073. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  2074. if (len <= 0) {
  2075. return NULL;
  2076. }
  2077. buf[0] = '\0';
  2078. return buf;
  2079. }
  2080. int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) {
  2081. if (ctx->method->is_dtls) {
  2082. return 0;
  2083. }
  2084. ctx->quic_method = quic_method;
  2085. return 1;
  2086. }
  2087. int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) {
  2088. if (ssl->method->is_dtls) {
  2089. return 0;
  2090. }
  2091. ssl->quic_method = quic_method;
  2092. return 1;
  2093. }
  2094. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  2095. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  2096. int index;
  2097. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  2098. free_func)) {
  2099. return -1;
  2100. }
  2101. return index;
  2102. }
  2103. int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
  2104. return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
  2105. }
  2106. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  2107. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  2108. }
  2109. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  2110. CRYPTO_EX_dup *dup_unused,
  2111. CRYPTO_EX_free *free_func) {
  2112. int index;
  2113. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  2114. free_func)) {
  2115. return -1;
  2116. }
  2117. return index;
  2118. }
  2119. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
  2120. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  2121. }
  2122. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  2123. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  2124. }
  2125. int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
  2126. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  2127. RSA *(*cb)(SSL *ssl, int is_export,
  2128. int keylength)) {}
  2129. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  2130. int keylength)) {}
  2131. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  2132. DH *(*cb)(SSL *ssl, int is_export,
  2133. int keylength)) {}
  2134. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
  2135. int keylength)) {}
  2136. static int use_psk_identity_hint(UniquePtr<char> *out,
  2137. const char *identity_hint) {
  2138. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2139. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  2140. return 0;
  2141. }
  2142. // Clear currently configured hint, if any.
  2143. out->reset();
  2144. // Treat the empty hint as not supplying one. Plain PSK makes it possible to
  2145. // send either no hint (omit ServerKeyExchange) or an empty hint, while
  2146. // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  2147. // so we interpret empty and missing as identical.
  2148. if (identity_hint != NULL && identity_hint[0] != '\0') {
  2149. out->reset(BUF_strdup(identity_hint));
  2150. if (*out == nullptr) {
  2151. return 0;
  2152. }
  2153. }
  2154. return 1;
  2155. }
  2156. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  2157. return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
  2158. }
  2159. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  2160. if (!ssl->config) {
  2161. return 0;
  2162. }
  2163. return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
  2164. }
  2165. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  2166. if (ssl == NULL) {
  2167. return NULL;
  2168. }
  2169. if (ssl->config == NULL) {
  2170. assert(ssl->config);
  2171. return NULL;
  2172. }
  2173. return ssl->config->psk_identity_hint.get();
  2174. }
  2175. const char *SSL_get_psk_identity(const SSL *ssl) {
  2176. if (ssl == NULL) {
  2177. return NULL;
  2178. }
  2179. SSL_SESSION *session = SSL_get_session(ssl);
  2180. if (session == NULL) {
  2181. return NULL;
  2182. }
  2183. return session->psk_identity.get();
  2184. }
  2185. void SSL_set_psk_client_callback(
  2186. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2187. unsigned max_identity_len, uint8_t *psk,
  2188. unsigned max_psk_len)) {
  2189. if (!ssl->config) {
  2190. return;
  2191. }
  2192. ssl->config->psk_client_callback = cb;
  2193. }
  2194. void SSL_CTX_set_psk_client_callback(
  2195. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2196. unsigned max_identity_len, uint8_t *psk,
  2197. unsigned max_psk_len)) {
  2198. ctx->psk_client_callback = cb;
  2199. }
  2200. void SSL_set_psk_server_callback(
  2201. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2202. unsigned max_psk_len)) {
  2203. if (!ssl->config) {
  2204. return;
  2205. }
  2206. ssl->config->psk_server_callback = cb;
  2207. }
  2208. void SSL_CTX_set_psk_server_callback(
  2209. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  2210. uint8_t *psk, unsigned max_psk_len)) {
  2211. ctx->psk_server_callback = cb;
  2212. }
  2213. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2214. void (*cb)(int write_p, int version,
  2215. int content_type, const void *buf,
  2216. size_t len, SSL *ssl, void *arg)) {
  2217. ctx->msg_callback = cb;
  2218. }
  2219. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2220. ctx->msg_callback_arg = arg;
  2221. }
  2222. void SSL_set_msg_callback(SSL *ssl,
  2223. void (*cb)(int write_p, int version, int content_type,
  2224. const void *buf, size_t len, SSL *ssl,
  2225. void *arg)) {
  2226. ssl->msg_callback = cb;
  2227. }
  2228. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2229. ssl->msg_callback_arg = arg;
  2230. }
  2231. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2232. void (*cb)(const SSL *ssl, const char *line)) {
  2233. ctx->keylog_callback = cb;
  2234. }
  2235. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2236. const char *line) {
  2237. return ctx->keylog_callback;
  2238. }
  2239. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2240. void (*cb)(const SSL *ssl,
  2241. struct timeval *out_clock)) {
  2242. ctx->current_time_cb = cb;
  2243. }
  2244. int SSL_is_init_finished(const SSL *ssl) {
  2245. return !SSL_in_init(ssl);
  2246. }
  2247. int SSL_in_init(const SSL *ssl) {
  2248. // This returns false once all the handshake state has been finalized, to
  2249. // allow callbacks and getters based on SSL_in_init to return the correct
  2250. // values.
  2251. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2252. return hs != nullptr && !hs->handshake_finalized;
  2253. }
  2254. int SSL_in_false_start(const SSL *ssl) {
  2255. if (ssl->s3->hs == NULL) {
  2256. return 0;
  2257. }
  2258. return ssl->s3->hs->in_false_start;
  2259. }
  2260. int SSL_cutthrough_complete(const SSL *ssl) {
  2261. return SSL_in_false_start(ssl);
  2262. }
  2263. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2264. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2265. void SSL_CTX_set_select_certificate_cb(
  2266. SSL_CTX *ctx,
  2267. enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
  2268. ctx->select_certificate_cb = cb;
  2269. }
  2270. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2271. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2272. ctx->dos_protection_cb = cb;
  2273. }
  2274. void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) {
  2275. ctx->reverify_on_resume = !!enabled;
  2276. }
  2277. void SSL_set_enforce_rsa_key_usage(SSL *ssl, int enabled) {
  2278. if (!ssl->config) {
  2279. return;
  2280. }
  2281. ssl->config->enforce_rsa_key_usage = !!enabled;
  2282. }
  2283. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2284. ssl->renegotiate_mode = mode;
  2285. // Check if |ssl_can_renegotiate| has changed and the configuration may now be
  2286. // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then
  2287. // disable after the handshake once the ALPN protocol is known to be HTTP/2.
  2288. ssl_maybe_shed_handshake_config(ssl);
  2289. }
  2290. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2291. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2292. size_t write_iv_len;
  2293. if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
  2294. !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
  2295. *out_iv_len != write_iv_len) {
  2296. return 0;
  2297. }
  2298. return 1;
  2299. }
  2300. static uint64_t be_to_u64(const uint8_t in[8]) {
  2301. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2302. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2303. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2304. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2305. }
  2306. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2307. // TODO(davidben): Internally represent sequence numbers as uint64_t.
  2308. if (SSL_is_dtls(ssl)) {
  2309. // max_seq_num already includes the epoch.
  2310. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2311. return ssl->d1->bitmap.max_seq_num;
  2312. }
  2313. return be_to_u64(ssl->s3->read_sequence);
  2314. }
  2315. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2316. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2317. if (SSL_is_dtls(ssl)) {
  2318. assert((ret >> 48) == 0);
  2319. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2320. }
  2321. return ret;
  2322. }
  2323. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2324. // TODO(davidben): This checks the wrong session if there is a renegotiation
  2325. // in progress.
  2326. SSL_SESSION *session = SSL_get_session(ssl);
  2327. if (session == NULL) {
  2328. return 0;
  2329. }
  2330. return session->peer_signature_algorithm;
  2331. }
  2332. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2333. if (max_out == 0) {
  2334. return sizeof(ssl->s3->client_random);
  2335. }
  2336. if (max_out > sizeof(ssl->s3->client_random)) {
  2337. max_out = sizeof(ssl->s3->client_random);
  2338. }
  2339. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2340. return max_out;
  2341. }
  2342. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2343. if (max_out == 0) {
  2344. return sizeof(ssl->s3->server_random);
  2345. }
  2346. if (max_out > sizeof(ssl->s3->server_random)) {
  2347. max_out = sizeof(ssl->s3->server_random);
  2348. }
  2349. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2350. return max_out;
  2351. }
  2352. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2353. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2354. if (hs == NULL) {
  2355. return NULL;
  2356. }
  2357. return hs->new_cipher;
  2358. }
  2359. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2360. if (!ssl->config) {
  2361. return;
  2362. }
  2363. ssl->config->retain_only_sha256_of_client_certs = !!enabled;
  2364. }
  2365. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2366. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2367. }
  2368. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2369. ctx->grease_enabled = !!enabled;
  2370. }
  2371. int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
  2372. return ssl->s3->ticket_age_skew;
  2373. }
  2374. void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
  2375. ctx->false_start_allowed_without_alpn = !!allowed;
  2376. }
  2377. int SSL_is_tls13_downgrade(const SSL *ssl) { return ssl->s3->tls13_downgrade; }
  2378. void SSL_CTX_set_ignore_tls13_downgrade(SSL_CTX *ctx, int ignore) {
  2379. ctx->ignore_tls13_downgrade = !!ignore;
  2380. }
  2381. void SSL_set_ignore_tls13_downgrade(SSL *ssl, int ignore) {
  2382. if (!ssl->config) {
  2383. return;
  2384. }
  2385. ssl->config->ignore_tls13_downgrade = !!ignore;
  2386. }
  2387. void SSL_set_shed_handshake_config(SSL *ssl, int enable) {
  2388. if (!ssl->config) {
  2389. return;
  2390. }
  2391. ssl->config->shed_handshake_config = !!enable;
  2392. }
  2393. void SSL_set_jdk11_workaround(SSL *ssl, int enable) {
  2394. if (!ssl->config) {
  2395. return;
  2396. }
  2397. ssl->config->jdk11_workaround = !!enable;
  2398. }
  2399. int SSL_clear(SSL *ssl) {
  2400. if (!ssl->config) {
  2401. return 0; // SSL_clear may not be used after shedding config.
  2402. }
  2403. // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2404. // established session to be offered the next time around. wpa_supplicant
  2405. // depends on this behavior, so emulate it.
  2406. UniquePtr<SSL_SESSION> session;
  2407. if (!ssl->server && ssl->s3->established_session != NULL) {
  2408. session = UpRef(ssl->s3->established_session);
  2409. }
  2410. // The ssl->d1->mtu is simultaneously configuration (preserved across
  2411. // clear) and connection-specific state (gets reset).
  2412. //
  2413. // TODO(davidben): Avoid this.
  2414. unsigned mtu = 0;
  2415. if (ssl->d1 != NULL) {
  2416. mtu = ssl->d1->mtu;
  2417. }
  2418. ssl->method->ssl_free(ssl);
  2419. if (!ssl->method->ssl_new(ssl)) {
  2420. return 0;
  2421. }
  2422. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2423. ssl->d1->mtu = mtu;
  2424. }
  2425. if (session != nullptr) {
  2426. SSL_set_session(ssl, session.get());
  2427. }
  2428. return 1;
  2429. }
  2430. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2431. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2432. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2433. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2434. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2435. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2436. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2437. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2438. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2439. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2440. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2441. int SSL_num_renegotiations(const SSL *ssl) {
  2442. return SSL_total_renegotiations(ssl);
  2443. }
  2444. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2445. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2446. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2447. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2448. void ERR_load_SSL_strings(void) {}
  2449. void SSL_load_error_strings(void) {}
  2450. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2451. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2452. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2453. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2454. return 0;
  2455. }
  2456. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2457. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2458. }
  2459. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2460. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2461. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2462. return 0;
  2463. }
  2464. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2465. return SSL_set1_curves(ssl, &nid, 1);
  2466. }
  2467. void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
  2468. const SSL_TICKET_AEAD_METHOD *aead_method) {
  2469. ctx->ticket_aead_method = aead_method;
  2470. }
  2471. int SSL_set_tlsext_status_type(SSL *ssl, int type) {
  2472. if (!ssl->config) {
  2473. return 0;
  2474. }
  2475. ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp;
  2476. return 1;
  2477. }
  2478. int SSL_get_tlsext_status_type(const SSL *ssl) {
  2479. if (ssl->server) {
  2480. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2481. return hs != nullptr && hs->ocsp_stapling_requested
  2482. ? TLSEXT_STATUSTYPE_ocsp
  2483. : TLSEXT_STATUSTYPE_nothing;
  2484. }
  2485. return ssl->config != nullptr && ssl->config->ocsp_stapling_enabled
  2486. ? TLSEXT_STATUSTYPE_ocsp
  2487. : TLSEXT_STATUSTYPE_nothing;
  2488. }
  2489. int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) {
  2490. if (SSL_set_ocsp_response(ssl, resp, resp_len)) {
  2491. OPENSSL_free(resp);
  2492. return 1;
  2493. }
  2494. return 0;
  2495. }
  2496. size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) {
  2497. size_t ret;
  2498. SSL_get0_ocsp_response(ssl, out, &ret);
  2499. return ret;
  2500. }
  2501. int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx,
  2502. int (*callback)(SSL *ssl, void *arg)) {
  2503. ctx->legacy_ocsp_callback = callback;
  2504. return 1;
  2505. }
  2506. int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) {
  2507. ctx->legacy_ocsp_callback_arg = arg;
  2508. return 1;
  2509. }