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.
 
 
 
 
 
 

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