您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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