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.
 
 
 
 
 
 

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