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.
 
 
 
 
 
 

2689 lines
78 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/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. } // namespace bssl
  426. using namespace bssl;
  427. int SSL_library_init(void) {
  428. CRYPTO_library_init();
  429. return 1;
  430. }
  431. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  432. CRYPTO_library_init();
  433. return 1;
  434. }
  435. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  436. const uint8_t *session_id = sess->session_id;
  437. uint8_t tmp_storage[sizeof(uint32_t)];
  438. if (sess->session_id_length < sizeof(tmp_storage)) {
  439. OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
  440. OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  441. session_id = tmp_storage;
  442. }
  443. uint32_t hash =
  444. ((uint32_t)session_id[0]) |
  445. ((uint32_t)session_id[1] << 8) |
  446. ((uint32_t)session_id[2] << 16) |
  447. ((uint32_t)session_id[3] << 24);
  448. return hash;
  449. }
  450. // NB: If this function (or indeed the hash function which uses a sort of
  451. // coarser function than this one) is changed, ensure
  452. // SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  453. // able to construct an SSL_SESSION that will collide with any existing session
  454. // with a matching session ID.
  455. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  456. if (a->ssl_version != b->ssl_version) {
  457. return 1;
  458. }
  459. if (a->session_id_length != b->session_id_length) {
  460. return 1;
  461. }
  462. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  463. }
  464. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  465. SSL_CTX *ret = NULL;
  466. if (method == NULL) {
  467. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  468. return NULL;
  469. }
  470. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  471. if (ret == NULL) {
  472. goto err;
  473. }
  474. OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
  475. ret->method = method->method;
  476. ret->x509_method = method->x509_method;
  477. CRYPTO_MUTEX_init(&ret->lock);
  478. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  479. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  480. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  481. ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
  482. ret->references = 1;
  483. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  484. ret->verify_mode = SSL_VERIFY_NONE;
  485. ret->cert = ssl_cert_new(method->x509_method);
  486. if (ret->cert == NULL) {
  487. goto err;
  488. }
  489. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  490. if (ret->sessions == NULL) {
  491. goto err;
  492. }
  493. if (!ret->x509_method->ssl_ctx_new(ret)) {
  494. goto err;
  495. }
  496. if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
  497. goto err2;
  498. }
  499. ret->client_CA = sk_CRYPTO_BUFFER_new_null();
  500. if (ret->client_CA == NULL) {
  501. goto err;
  502. }
  503. CRYPTO_new_ex_data(&ret->ex_data);
  504. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  505. // Disable the auto-chaining feature by default. Once this has stuck without
  506. // problems, the feature will be removed entirely.
  507. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  508. // Lock the SSL_CTX to the specified version, for compatibility with legacy
  509. // uses of SSL_METHOD, but we do not set the minimum version for
  510. // |SSLv3_method|.
  511. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  512. !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
  513. ? 0 // default
  514. : 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. return ssl;
  643. err:
  644. SSL_free(ssl);
  645. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  646. return NULL;
  647. }
  648. void SSL_free(SSL *ssl) {
  649. if (ssl == NULL) {
  650. return;
  651. }
  652. if (ssl->ctx != NULL) {
  653. ssl->ctx->x509_method->ssl_free(ssl);
  654. }
  655. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  656. BIO_free_all(ssl->rbio);
  657. BIO_free_all(ssl->wbio);
  658. // add extra stuff
  659. ssl_cipher_preference_list_free(ssl->cipher_list);
  660. SSL_SESSION_free(ssl->session);
  661. ssl_cert_free(ssl->cert);
  662. OPENSSL_free(ssl->tlsext_hostname);
  663. SSL_CTX_free(ssl->session_ctx);
  664. OPENSSL_free(ssl->supported_group_list);
  665. OPENSSL_free(ssl->alpn_client_proto_list);
  666. OPENSSL_free(ssl->token_binding_params);
  667. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  668. OPENSSL_free(ssl->psk_identity_hint);
  669. sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
  670. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  671. if (ssl->method != NULL) {
  672. ssl->method->ssl_free(ssl);
  673. }
  674. SSL_CTX_free(ssl->ctx);
  675. OPENSSL_free(ssl);
  676. }
  677. void SSL_set_connect_state(SSL *ssl) {
  678. ssl->server = false;
  679. ssl->do_handshake = ssl_client_handshake;
  680. }
  681. void SSL_set_accept_state(SSL *ssl) {
  682. ssl->server = true;
  683. ssl->do_handshake = ssl_server_handshake;
  684. }
  685. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  686. BIO_free_all(ssl->rbio);
  687. ssl->rbio = rbio;
  688. }
  689. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  690. BIO_free_all(ssl->wbio);
  691. ssl->wbio = wbio;
  692. }
  693. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  694. // For historical reasons, this function has many different cases in ownership
  695. // handling.
  696. // If nothing has changed, do nothing
  697. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  698. return;
  699. }
  700. // If the two arguments are equal, one fewer reference is granted than
  701. // taken.
  702. if (rbio != NULL && rbio == wbio) {
  703. BIO_up_ref(rbio);
  704. }
  705. // If only the wbio is changed, adopt only one reference.
  706. if (rbio == SSL_get_rbio(ssl)) {
  707. SSL_set0_wbio(ssl, wbio);
  708. return;
  709. }
  710. // There is an asymmetry here for historical reasons. If only the rbio is
  711. // changed AND the rbio and wbio were originally different, then we only adopt
  712. // one reference.
  713. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  714. SSL_set0_rbio(ssl, rbio);
  715. return;
  716. }
  717. // Otherwise, adopt both references.
  718. SSL_set0_rbio(ssl, rbio);
  719. SSL_set0_wbio(ssl, wbio);
  720. }
  721. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  722. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  723. int SSL_do_handshake(SSL *ssl) {
  724. ssl_reset_error_state(ssl);
  725. if (ssl->do_handshake == NULL) {
  726. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  727. return -1;
  728. }
  729. if (!SSL_in_init(ssl)) {
  730. return 1;
  731. }
  732. // Run the handshake.
  733. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  734. bool early_return = false;
  735. int ret = ssl_run_handshake(hs, &early_return);
  736. ssl_do_info_callback(
  737. ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
  738. if (ret <= 0) {
  739. return ret;
  740. }
  741. // Destroy the handshake object if the handshake has completely finished.
  742. if (!early_return) {
  743. ssl->s3->hs.reset();
  744. }
  745. return 1;
  746. }
  747. int SSL_connect(SSL *ssl) {
  748. if (ssl->do_handshake == NULL) {
  749. // Not properly initialized yet
  750. SSL_set_connect_state(ssl);
  751. }
  752. return SSL_do_handshake(ssl);
  753. }
  754. int SSL_accept(SSL *ssl) {
  755. if (ssl->do_handshake == NULL) {
  756. // Not properly initialized yet
  757. SSL_set_accept_state(ssl);
  758. }
  759. return SSL_do_handshake(ssl);
  760. }
  761. static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
  762. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  763. return tls13_post_handshake(ssl, msg);
  764. }
  765. // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  766. // removed entirely in the future and requires retaining more data for
  767. // renegotiation_info.
  768. if (ssl->server || ssl->version == SSL3_VERSION) {
  769. goto no_renegotiation;
  770. }
  771. if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
  772. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  773. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  774. return 0;
  775. }
  776. switch (ssl->renegotiate_mode) {
  777. case ssl_renegotiate_ignore:
  778. // Ignore the HelloRequest.
  779. return 1;
  780. case ssl_renegotiate_once:
  781. if (ssl->s3->total_renegotiations != 0) {
  782. goto no_renegotiation;
  783. }
  784. break;
  785. case ssl_renegotiate_never:
  786. goto no_renegotiation;
  787. case ssl_renegotiate_freely:
  788. break;
  789. }
  790. // Renegotiation is only supported at quiescent points in the application
  791. // protocol, namely in HTTPS, just before reading the HTTP response. Require
  792. // the record-layer be idle and avoid complexities of sending a handshake
  793. // record while an application_data record is being written.
  794. if (!ssl->s3->write_buffer.empty() ||
  795. ssl->s3->write_shutdown != ssl_shutdown_none) {
  796. goto no_renegotiation;
  797. }
  798. // Begin a new handshake.
  799. if (ssl->s3->hs != nullptr) {
  800. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  801. return 0;
  802. }
  803. ssl->s3->hs = ssl_handshake_new(ssl);
  804. if (ssl->s3->hs == nullptr) {
  805. return 0;
  806. }
  807. ssl->s3->total_renegotiations++;
  808. return 1;
  809. no_renegotiation:
  810. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  811. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  812. return 0;
  813. }
  814. static int ssl_read_impl(SSL *ssl) {
  815. ssl_reset_error_state(ssl);
  816. if (ssl->do_handshake == NULL) {
  817. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  818. return -1;
  819. }
  820. // Replay post-handshake message errors.
  821. if (!check_read_error(ssl)) {
  822. return -1;
  823. }
  824. while (ssl->s3->pending_app_data.empty()) {
  825. // Complete the current handshake, if any. False Start will cause
  826. // |SSL_do_handshake| to return mid-handshake, so this may require multiple
  827. // iterations.
  828. while (!ssl_can_read(ssl)) {
  829. int ret = SSL_do_handshake(ssl);
  830. if (ret < 0) {
  831. return ret;
  832. }
  833. if (ret == 0) {
  834. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  835. return -1;
  836. }
  837. }
  838. // Process any buffered post-handshake messages.
  839. SSLMessage msg;
  840. if (ssl->method->get_message(ssl, &msg)) {
  841. // If we received an interrupt in early read (EndOfEarlyData), loop again
  842. // for the handshake to process it.
  843. if (SSL_in_init(ssl)) {
  844. ssl->s3->hs->can_early_read = false;
  845. continue;
  846. }
  847. // Handle the post-handshake message and try again.
  848. if (!ssl_do_post_handshake(ssl, msg)) {
  849. ssl_set_read_error(ssl);
  850. return -1;
  851. }
  852. ssl->method->next_message(ssl);
  853. continue; // Loop again. We may have begun a new handshake.
  854. }
  855. uint8_t alert = SSL_AD_DECODE_ERROR;
  856. size_t consumed = 0;
  857. auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
  858. &alert, ssl->s3->read_buffer.span());
  859. bool retry;
  860. int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
  861. if (bio_ret <= 0) {
  862. return bio_ret;
  863. }
  864. if (!retry) {
  865. assert(!ssl->s3->pending_app_data.empty());
  866. ssl->s3->key_update_count = 0;
  867. }
  868. }
  869. return 1;
  870. }
  871. int SSL_read(SSL *ssl, void *buf, int num) {
  872. int ret = SSL_peek(ssl, buf, num);
  873. if (ret <= 0) {
  874. return ret;
  875. }
  876. // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
  877. // is not a stream. See https://crbug.com/boringssl/65.
  878. ssl->s3->pending_app_data =
  879. ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
  880. if (ssl->s3->pending_app_data.empty()) {
  881. ssl->s3->read_buffer.DiscardConsumed();
  882. }
  883. return ret;
  884. }
  885. int SSL_peek(SSL *ssl, void *buf, int num) {
  886. int ret = ssl_read_impl(ssl);
  887. if (ret <= 0) {
  888. return ret;
  889. }
  890. if (num <= 0) {
  891. return num;
  892. }
  893. size_t todo =
  894. std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
  895. OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
  896. return static_cast<int>(todo);
  897. }
  898. int SSL_write(SSL *ssl, const void *buf, int num) {
  899. ssl_reset_error_state(ssl);
  900. if (ssl->do_handshake == NULL) {
  901. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  902. return -1;
  903. }
  904. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  905. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  906. return -1;
  907. }
  908. int ret = 0;
  909. bool needs_handshake = false;
  910. do {
  911. // If necessary, complete the handshake implicitly.
  912. if (!ssl_can_write(ssl)) {
  913. ret = SSL_do_handshake(ssl);
  914. if (ret < 0) {
  915. return ret;
  916. }
  917. if (ret == 0) {
  918. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  919. return -1;
  920. }
  921. }
  922. ret = ssl->method->write_app_data(ssl, &needs_handshake,
  923. (const uint8_t *)buf, num);
  924. } while (needs_handshake);
  925. return ret;
  926. }
  927. int SSL_shutdown(SSL *ssl) {
  928. ssl_reset_error_state(ssl);
  929. if (ssl->do_handshake == NULL) {
  930. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  931. return -1;
  932. }
  933. // If we are in the middle of a handshake, silently succeed. Consumers often
  934. // call this function before |SSL_free|, whether the handshake succeeded or
  935. // not. We assume the caller has already handled failed handshakes.
  936. if (SSL_in_init(ssl)) {
  937. return 1;
  938. }
  939. if (ssl->quiet_shutdown) {
  940. // Do nothing if configured not to send a close_notify.
  941. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  942. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  943. return 1;
  944. }
  945. // This function completes in two stages. It sends a close_notify and then it
  946. // waits for a close_notify to come in. Perform exactly one action and return
  947. // whether or not it succeeds.
  948. if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
  949. // Send a close_notify.
  950. if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  951. return -1;
  952. }
  953. } else if (ssl->s3->alert_dispatch) {
  954. // Finish sending the close_notify.
  955. if (ssl->method->dispatch_alert(ssl) <= 0) {
  956. return -1;
  957. }
  958. } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  959. if (SSL_is_dtls(ssl)) {
  960. // Bidirectional shutdown doesn't make sense for an unordered
  961. // transport. DTLS alerts also aren't delivered reliably, so we may even
  962. // time out because the peer never received our close_notify. Report to
  963. // the caller that the channel has fully shut down.
  964. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  965. ERR_restore_state(ssl->s3->read_error.get());
  966. return -1;
  967. }
  968. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  969. } else {
  970. // Keep discarding data until we see a close_notify.
  971. for (;;) {
  972. ssl->s3->pending_app_data = Span<uint8_t>();
  973. int ret = ssl_read_impl(ssl);
  974. if (ret <= 0) {
  975. break;
  976. }
  977. }
  978. if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  979. return -1;
  980. }
  981. }
  982. }
  983. // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
  984. return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
  985. }
  986. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  987. if (ssl->s3->alert_dispatch) {
  988. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  989. ssl->s3->send_alert[1] != alert) {
  990. // We are already attempting to write a different alert.
  991. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  992. return -1;
  993. }
  994. return ssl->method->dispatch_alert(ssl);
  995. }
  996. return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  997. }
  998. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  999. ctx->cert->enable_early_data = !!enabled;
  1000. }
  1001. void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
  1002. ctx->tls13_variant = variant;
  1003. }
  1004. void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
  1005. ssl->tls13_variant = variant;
  1006. }
  1007. void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
  1008. ssl->cert->enable_early_data = !!enabled;
  1009. }
  1010. int SSL_in_early_data(const SSL *ssl) {
  1011. if (ssl->s3->hs == NULL) {
  1012. return 0;
  1013. }
  1014. return ssl->s3->hs->in_early_data;
  1015. }
  1016. int SSL_early_data_accepted(const SSL *ssl) {
  1017. return ssl->s3->early_data_accepted;
  1018. }
  1019. void SSL_reset_early_data_reject(SSL *ssl) {
  1020. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  1021. if (hs == NULL ||
  1022. hs->wait != ssl_hs_early_data_rejected) {
  1023. abort();
  1024. }
  1025. hs->wait = ssl_hs_ok;
  1026. hs->in_early_data = false;
  1027. hs->early_session.reset();
  1028. // Discard any unfinished writes from the perspective of |SSL_write|'s
  1029. // retry. The handshake will transparently flush out the pending record
  1030. // (discarded by the server) to keep the framing correct.
  1031. ssl->s3->wpend_pending = false;
  1032. }
  1033. static int bio_retry_reason_to_error(int reason) {
  1034. switch (reason) {
  1035. case BIO_RR_CONNECT:
  1036. return SSL_ERROR_WANT_CONNECT;
  1037. case BIO_RR_ACCEPT:
  1038. return SSL_ERROR_WANT_ACCEPT;
  1039. default:
  1040. return SSL_ERROR_SYSCALL;
  1041. }
  1042. }
  1043. int SSL_get_error(const SSL *ssl, int ret_code) {
  1044. if (ret_code > 0) {
  1045. return SSL_ERROR_NONE;
  1046. }
  1047. // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1048. // where we do encode the error
  1049. uint32_t err = ERR_peek_error();
  1050. if (err != 0) {
  1051. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1052. return SSL_ERROR_SYSCALL;
  1053. }
  1054. return SSL_ERROR_SSL;
  1055. }
  1056. if (ret_code == 0) {
  1057. if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
  1058. return SSL_ERROR_ZERO_RETURN;
  1059. }
  1060. // An EOF was observed which violates the protocol, and the underlying
  1061. // transport does not participate in the error queue. Bubble up to the
  1062. // caller.
  1063. return SSL_ERROR_SYSCALL;
  1064. }
  1065. switch (ssl->s3->rwstate) {
  1066. case SSL_PENDING_SESSION:
  1067. return SSL_ERROR_PENDING_SESSION;
  1068. case SSL_CERTIFICATE_SELECTION_PENDING:
  1069. return SSL_ERROR_PENDING_CERTIFICATE;
  1070. case SSL_READING: {
  1071. BIO *bio = SSL_get_rbio(ssl);
  1072. if (BIO_should_read(bio)) {
  1073. return SSL_ERROR_WANT_READ;
  1074. }
  1075. if (BIO_should_write(bio)) {
  1076. // TODO(davidben): OpenSSL historically checked for writes on the read
  1077. // BIO. Can this be removed?
  1078. return SSL_ERROR_WANT_WRITE;
  1079. }
  1080. if (BIO_should_io_special(bio)) {
  1081. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1082. }
  1083. break;
  1084. }
  1085. case SSL_WRITING: {
  1086. BIO *bio = SSL_get_wbio(ssl);
  1087. if (BIO_should_write(bio)) {
  1088. return SSL_ERROR_WANT_WRITE;
  1089. }
  1090. if (BIO_should_read(bio)) {
  1091. // TODO(davidben): OpenSSL historically checked for reads on the write
  1092. // BIO. Can this be removed?
  1093. return SSL_ERROR_WANT_READ;
  1094. }
  1095. if (BIO_should_io_special(bio)) {
  1096. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1097. }
  1098. break;
  1099. }
  1100. case SSL_X509_LOOKUP:
  1101. return SSL_ERROR_WANT_X509_LOOKUP;
  1102. case SSL_CHANNEL_ID_LOOKUP:
  1103. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1104. case SSL_PRIVATE_KEY_OPERATION:
  1105. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  1106. case SSL_PENDING_TICKET:
  1107. return SSL_ERROR_PENDING_TICKET;
  1108. case SSL_EARLY_DATA_REJECTED:
  1109. return SSL_ERROR_EARLY_DATA_REJECTED;
  1110. case SSL_CERTIFICATE_VERIFY:
  1111. return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
  1112. }
  1113. return SSL_ERROR_SYSCALL;
  1114. }
  1115. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  1116. ctx->options |= options;
  1117. return ctx->options;
  1118. }
  1119. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  1120. ctx->options &= ~options;
  1121. return ctx->options;
  1122. }
  1123. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  1124. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  1125. ssl->options |= options;
  1126. return ssl->options;
  1127. }
  1128. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  1129. ssl->options &= ~options;
  1130. return ssl->options;
  1131. }
  1132. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  1133. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  1134. ctx->mode |= mode;
  1135. return ctx->mode;
  1136. }
  1137. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  1138. ctx->mode &= ~mode;
  1139. return ctx->mode;
  1140. }
  1141. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  1142. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  1143. ssl->mode |= mode;
  1144. return ssl->mode;
  1145. }
  1146. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  1147. ssl->mode &= ~mode;
  1148. return ssl->mode;
  1149. }
  1150. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  1151. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  1152. ctx->pool = pool;
  1153. }
  1154. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  1155. size_t max_out) {
  1156. *out_len = 0;
  1157. OPENSSL_memset(out, 0, max_out);
  1158. // tls-unique is not defined for SSL 3.0 or TLS 1.3.
  1159. if (!ssl->s3->initial_handshake_complete ||
  1160. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1161. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1162. return 0;
  1163. }
  1164. // The tls-unique value is the first Finished message in the handshake, which
  1165. // is the client's in a full handshake and the server's for a resumption. See
  1166. // https://tools.ietf.org/html/rfc5929#section-3.1.
  1167. const uint8_t *finished = ssl->s3->previous_client_finished;
  1168. size_t finished_len = ssl->s3->previous_client_finished_len;
  1169. if (ssl->session != NULL) {
  1170. // tls-unique is broken for resumed sessions unless EMS is used.
  1171. if (!ssl->session->extended_master_secret) {
  1172. return 0;
  1173. }
  1174. finished = ssl->s3->previous_server_finished;
  1175. finished_len = ssl->s3->previous_server_finished_len;
  1176. }
  1177. *out_len = finished_len;
  1178. if (finished_len > max_out) {
  1179. *out_len = max_out;
  1180. }
  1181. OPENSSL_memcpy(out, finished, *out_len);
  1182. return 1;
  1183. }
  1184. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  1185. size_t sid_ctx_len) {
  1186. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  1187. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  1188. return 0;
  1189. }
  1190. static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
  1191. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  1192. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  1193. return 1;
  1194. }
  1195. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  1196. size_t sid_ctx_len) {
  1197. return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
  1198. }
  1199. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  1200. size_t sid_ctx_len) {
  1201. return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
  1202. }
  1203. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  1204. *out_len = ssl->cert->sid_ctx_length;
  1205. return ssl->cert->sid_ctx;
  1206. }
  1207. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  1208. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  1209. int SSL_get_rfd(const SSL *ssl) {
  1210. int ret = -1;
  1211. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  1212. if (b != NULL) {
  1213. BIO_get_fd(b, &ret);
  1214. }
  1215. return ret;
  1216. }
  1217. int SSL_get_wfd(const SSL *ssl) {
  1218. int ret = -1;
  1219. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  1220. if (b != NULL) {
  1221. BIO_get_fd(b, &ret);
  1222. }
  1223. return ret;
  1224. }
  1225. int SSL_set_fd(SSL *ssl, int fd) {
  1226. BIO *bio = BIO_new(BIO_s_socket());
  1227. if (bio == NULL) {
  1228. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1229. return 0;
  1230. }
  1231. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1232. SSL_set_bio(ssl, bio, bio);
  1233. return 1;
  1234. }
  1235. int SSL_set_wfd(SSL *ssl, int fd) {
  1236. BIO *rbio = SSL_get_rbio(ssl);
  1237. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  1238. BIO_get_fd(rbio, NULL) != fd) {
  1239. BIO *bio = BIO_new(BIO_s_socket());
  1240. if (bio == NULL) {
  1241. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1242. return 0;
  1243. }
  1244. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1245. SSL_set0_wbio(ssl, bio);
  1246. } else {
  1247. // Copy the rbio over to the wbio.
  1248. BIO_up_ref(rbio);
  1249. SSL_set0_wbio(ssl, rbio);
  1250. }
  1251. return 1;
  1252. }
  1253. int SSL_set_rfd(SSL *ssl, int fd) {
  1254. BIO *wbio = SSL_get_wbio(ssl);
  1255. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  1256. BIO_get_fd(wbio, NULL) != fd) {
  1257. BIO *bio = BIO_new(BIO_s_socket());
  1258. if (bio == NULL) {
  1259. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1260. return 0;
  1261. }
  1262. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1263. SSL_set0_rbio(ssl, bio);
  1264. } else {
  1265. // Copy the wbio over to the rbio.
  1266. BIO_up_ref(wbio);
  1267. SSL_set0_rbio(ssl, wbio);
  1268. }
  1269. return 1;
  1270. }
  1271. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  1272. size_t in_len) {
  1273. if (out_len > in_len) {
  1274. out_len = in_len;
  1275. }
  1276. OPENSSL_memcpy(out, in, out_len);
  1277. return in_len;
  1278. }
  1279. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1280. if (!ssl->s3->initial_handshake_complete ||
  1281. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1282. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1283. return 0;
  1284. }
  1285. if (ssl->server) {
  1286. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1287. ssl->s3->previous_server_finished_len);
  1288. }
  1289. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1290. ssl->s3->previous_client_finished_len);
  1291. }
  1292. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1293. if (!ssl->s3->initial_handshake_complete ||
  1294. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1295. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1296. return 0;
  1297. }
  1298. if (ssl->server) {
  1299. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1300. ssl->s3->previous_client_finished_len);
  1301. }
  1302. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1303. ssl->s3->previous_server_finished_len);
  1304. }
  1305. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1306. int SSL_get_extms_support(const SSL *ssl) {
  1307. // TLS 1.3 does not require extended master secret and always reports as
  1308. // supporting it.
  1309. if (!ssl->s3->have_version) {
  1310. return 0;
  1311. }
  1312. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1313. return 1;
  1314. }
  1315. // If the initial handshake completed, query the established session.
  1316. if (ssl->s3->established_session != NULL) {
  1317. return ssl->s3->established_session->extended_master_secret;
  1318. }
  1319. // Otherwise, query the in-progress handshake.
  1320. if (ssl->s3->hs != NULL) {
  1321. return ssl->s3->hs->extended_master_secret;
  1322. }
  1323. assert(0);
  1324. return 0;
  1325. }
  1326. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1327. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1328. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1329. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1330. int SSL_pending(const SSL *ssl) {
  1331. return static_cast<int>(ssl->s3->pending_app_data.size());
  1332. }
  1333. // Fix this so it checks all the valid key/cert options
  1334. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1335. return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
  1336. }
  1337. // Fix this function so that it takes an optional type parameter
  1338. int SSL_check_private_key(const SSL *ssl) {
  1339. return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
  1340. }
  1341. long SSL_get_default_timeout(const SSL *ssl) {
  1342. return SSL_DEFAULT_SESSION_TIMEOUT;
  1343. }
  1344. int SSL_renegotiate(SSL *ssl) {
  1345. // Caller-initiated renegotiation is not supported.
  1346. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1347. return 0;
  1348. }
  1349. int SSL_renegotiate_pending(SSL *ssl) {
  1350. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1351. }
  1352. int SSL_total_renegotiations(const SSL *ssl) {
  1353. return ssl->s3->total_renegotiations;
  1354. }
  1355. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1356. return ctx->max_cert_list;
  1357. }
  1358. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1359. if (max_cert_list > kMaxHandshakeSize) {
  1360. max_cert_list = kMaxHandshakeSize;
  1361. }
  1362. ctx->max_cert_list = (uint32_t)max_cert_list;
  1363. }
  1364. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1365. return ssl->max_cert_list;
  1366. }
  1367. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1368. if (max_cert_list > kMaxHandshakeSize) {
  1369. max_cert_list = kMaxHandshakeSize;
  1370. }
  1371. ssl->max_cert_list = (uint32_t)max_cert_list;
  1372. }
  1373. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1374. if (max_send_fragment < 512) {
  1375. max_send_fragment = 512;
  1376. }
  1377. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1378. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1379. }
  1380. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1381. return 1;
  1382. }
  1383. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1384. if (max_send_fragment < 512) {
  1385. max_send_fragment = 512;
  1386. }
  1387. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1388. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1389. }
  1390. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1391. return 1;
  1392. }
  1393. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1394. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1395. return 0;
  1396. }
  1397. ssl->d1->mtu = mtu;
  1398. return 1;
  1399. }
  1400. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1401. if (!ssl->s3->have_version) {
  1402. return 0;
  1403. }
  1404. return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  1405. ssl->s3->send_connection_binding;
  1406. }
  1407. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1408. MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
  1409. return lh_SSL_SESSION_num_items(ctx->sessions);
  1410. }
  1411. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1412. unsigned long ret = ctx->session_cache_size;
  1413. ctx->session_cache_size = size;
  1414. return ret;
  1415. }
  1416. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1417. return ctx->session_cache_size;
  1418. }
  1419. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1420. int ret = ctx->session_cache_mode;
  1421. ctx->session_cache_mode = mode;
  1422. return ret;
  1423. }
  1424. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1425. return ctx->session_cache_mode;
  1426. }
  1427. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1428. if (out == NULL) {
  1429. return 48;
  1430. }
  1431. if (len != 48) {
  1432. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1433. return 0;
  1434. }
  1435. // The default ticket keys are initialized lazily. Trigger a key
  1436. // rotation to initialize them.
  1437. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  1438. return 0;
  1439. }
  1440. uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
  1441. MutexReadLock lock(&ctx->lock);
  1442. OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
  1443. OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
  1444. OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
  1445. return 1;
  1446. }
  1447. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1448. if (in == NULL) {
  1449. return 48;
  1450. }
  1451. if (len != 48) {
  1452. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1453. return 0;
  1454. }
  1455. if (!ctx->tlsext_ticket_key_current) {
  1456. ctx->tlsext_ticket_key_current =
  1457. (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
  1458. if (!ctx->tlsext_ticket_key_current) {
  1459. return 0;
  1460. }
  1461. }
  1462. OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
  1463. const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
  1464. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
  1465. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
  1466. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
  1467. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  1468. ctx->tlsext_ticket_key_prev = nullptr;
  1469. // Disable automatic key rotation.
  1470. ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
  1471. return 1;
  1472. }
  1473. int SSL_CTX_set_tlsext_ticket_key_cb(
  1474. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1475. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1476. int encrypt)) {
  1477. ctx->tlsext_ticket_key_cb = callback;
  1478. return 1;
  1479. }
  1480. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1481. return tls1_set_curves(&ctx->supported_group_list,
  1482. &ctx->supported_group_list_len, curves,
  1483. curves_len);
  1484. }
  1485. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1486. return tls1_set_curves(&ssl->supported_group_list,
  1487. &ssl->supported_group_list_len, curves,
  1488. curves_len);
  1489. }
  1490. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1491. return tls1_set_curves_list(&ctx->supported_group_list,
  1492. &ctx->supported_group_list_len, curves);
  1493. }
  1494. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1495. return tls1_set_curves_list(&ssl->supported_group_list,
  1496. &ssl->supported_group_list_len, curves);
  1497. }
  1498. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1499. // TODO(davidben): This checks the wrong session if there is a renegotiation
  1500. // in progress.
  1501. SSL_SESSION *session = SSL_get_session(ssl);
  1502. if (session == NULL) {
  1503. return 0;
  1504. }
  1505. return session->group_id;
  1506. }
  1507. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1508. return 1;
  1509. }
  1510. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1511. return 1;
  1512. }
  1513. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
  1514. return ctx->cipher_list->ciphers;
  1515. }
  1516. int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
  1517. if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) {
  1518. return 0;
  1519. }
  1520. return ctx->cipher_list->in_group_flags[i];
  1521. }
  1522. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1523. if (ssl == NULL) {
  1524. return NULL;
  1525. }
  1526. const struct ssl_cipher_preference_list_st *prefs =
  1527. ssl_get_cipher_preferences(ssl);
  1528. if (prefs == NULL) {
  1529. return NULL;
  1530. }
  1531. return prefs->ciphers;
  1532. }
  1533. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1534. if (ssl == NULL) {
  1535. return NULL;
  1536. }
  1537. STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
  1538. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1539. return NULL;
  1540. }
  1541. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
  1542. if (c == NULL) {
  1543. return NULL;
  1544. }
  1545. return c->name;
  1546. }
  1547. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1548. return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
  1549. }
  1550. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1551. return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
  1552. }
  1553. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1554. return ssl_create_cipher_list(&ssl->cipher_list, str, false /* not strict */);
  1555. }
  1556. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1557. return ssl_create_cipher_list(&ssl->cipher_list, str, true /* strict */);
  1558. }
  1559. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1560. if (type != TLSEXT_NAMETYPE_host_name) {
  1561. return NULL;
  1562. }
  1563. // Historically, |SSL_get_servername| was also the configuration getter
  1564. // corresponding to |SSL_set_tlsext_host_name|.
  1565. if (ssl->tlsext_hostname != NULL) {
  1566. return ssl->tlsext_hostname;
  1567. }
  1568. return ssl->s3->hostname.get();
  1569. }
  1570. int SSL_get_servername_type(const SSL *ssl) {
  1571. if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
  1572. return -1;
  1573. }
  1574. return TLSEXT_NAMETYPE_host_name;
  1575. }
  1576. void SSL_CTX_set_custom_verify(
  1577. SSL_CTX *ctx, int mode,
  1578. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1579. ctx->verify_mode = mode;
  1580. ctx->custom_verify_callback = callback;
  1581. }
  1582. void SSL_set_custom_verify(
  1583. SSL *ssl, int mode,
  1584. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1585. ssl->verify_mode = mode;
  1586. ssl->custom_verify_callback = callback;
  1587. }
  1588. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1589. ctx->signed_cert_timestamps_enabled = true;
  1590. }
  1591. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1592. ssl->signed_cert_timestamps_enabled = true;
  1593. }
  1594. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1595. ctx->ocsp_stapling_enabled = true;
  1596. }
  1597. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1598. ssl->ocsp_stapling_enabled = true;
  1599. }
  1600. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1601. size_t *out_len) {
  1602. SSL_SESSION *session = SSL_get_session(ssl);
  1603. if (ssl->server || !session || !session->signed_cert_timestamp_list) {
  1604. *out_len = 0;
  1605. *out = NULL;
  1606. return;
  1607. }
  1608. *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
  1609. *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
  1610. }
  1611. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1612. size_t *out_len) {
  1613. SSL_SESSION *session = SSL_get_session(ssl);
  1614. if (ssl->server || !session || !session->ocsp_response) {
  1615. *out_len = 0;
  1616. *out = NULL;
  1617. return;
  1618. }
  1619. *out = CRYPTO_BUFFER_data(session->ocsp_response);
  1620. *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
  1621. }
  1622. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1623. OPENSSL_free(ssl->tlsext_hostname);
  1624. ssl->tlsext_hostname = NULL;
  1625. if (name == NULL) {
  1626. return 1;
  1627. }
  1628. size_t len = strlen(name);
  1629. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1630. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1631. return 0;
  1632. }
  1633. ssl->tlsext_hostname = BUF_strdup(name);
  1634. if (ssl->tlsext_hostname == NULL) {
  1635. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1636. return 0;
  1637. }
  1638. return 1;
  1639. }
  1640. int SSL_CTX_set_tlsext_servername_callback(
  1641. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1642. ctx->tlsext_servername_callback = callback;
  1643. return 1;
  1644. }
  1645. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1646. ctx->tlsext_servername_arg = arg;
  1647. return 1;
  1648. }
  1649. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
  1650. unsigned peer_len, const uint8_t *supported,
  1651. unsigned supported_len) {
  1652. const uint8_t *result;
  1653. int status;
  1654. // For each protocol in peer preference order, see if we support it.
  1655. for (unsigned i = 0; i < peer_len;) {
  1656. for (unsigned j = 0; j < supported_len;) {
  1657. if (peer[i] == supported[j] &&
  1658. OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
  1659. // We found a match
  1660. result = &peer[i];
  1661. status = OPENSSL_NPN_NEGOTIATED;
  1662. goto found;
  1663. }
  1664. j += supported[j];
  1665. j++;
  1666. }
  1667. i += peer[i];
  1668. i++;
  1669. }
  1670. // There's no overlap between our protocols and the peer's list.
  1671. result = supported;
  1672. status = OPENSSL_NPN_NO_OVERLAP;
  1673. found:
  1674. *out = (uint8_t *)result + 1;
  1675. *out_len = result[0];
  1676. return status;
  1677. }
  1678. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1679. unsigned *out_len) {
  1680. *out_data = ssl->s3->next_proto_negotiated.data();
  1681. *out_len = ssl->s3->next_proto_negotiated.size();
  1682. }
  1683. void SSL_CTX_set_next_protos_advertised_cb(
  1684. SSL_CTX *ctx,
  1685. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1686. void *arg) {
  1687. ctx->next_protos_advertised_cb = cb;
  1688. ctx->next_protos_advertised_cb_arg = arg;
  1689. }
  1690. void SSL_CTX_set_next_proto_select_cb(
  1691. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1692. const uint8_t *in, unsigned in_len, void *arg),
  1693. void *arg) {
  1694. ctx->next_proto_select_cb = cb;
  1695. ctx->next_proto_select_cb_arg = arg;
  1696. }
  1697. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1698. unsigned protos_len) {
  1699. OPENSSL_free(ctx->alpn_client_proto_list);
  1700. ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
  1701. if (!ctx->alpn_client_proto_list) {
  1702. return 1;
  1703. }
  1704. ctx->alpn_client_proto_list_len = protos_len;
  1705. return 0;
  1706. }
  1707. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1708. OPENSSL_free(ssl->alpn_client_proto_list);
  1709. ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
  1710. if (!ssl->alpn_client_proto_list) {
  1711. return 1;
  1712. }
  1713. ssl->alpn_client_proto_list_len = protos_len;
  1714. return 0;
  1715. }
  1716. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1717. int (*cb)(SSL *ssl, const uint8_t **out,
  1718. uint8_t *out_len, const uint8_t *in,
  1719. unsigned in_len, void *arg),
  1720. void *arg) {
  1721. ctx->alpn_select_cb = cb;
  1722. ctx->alpn_select_cb_arg = arg;
  1723. }
  1724. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1725. unsigned *out_len) {
  1726. if (SSL_in_early_data(ssl) && !ssl->server) {
  1727. *out_data = ssl->s3->hs->early_session->early_alpn;
  1728. *out_len = ssl->s3->hs->early_session->early_alpn_len;
  1729. } else {
  1730. *out_data = ssl->s3->alpn_selected.data();
  1731. *out_len = ssl->s3->alpn_selected.size();
  1732. }
  1733. }
  1734. void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
  1735. ctx->allow_unknown_alpn_protos = !!enabled;
  1736. }
  1737. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1738. ctx->tlsext_channel_id_enabled = !!enabled;
  1739. }
  1740. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1741. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1742. return 1;
  1743. }
  1744. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1745. ssl->tlsext_channel_id_enabled = !!enabled;
  1746. }
  1747. int SSL_enable_tls_channel_id(SSL *ssl) {
  1748. SSL_set_tls_channel_id_enabled(ssl, 1);
  1749. return 1;
  1750. }
  1751. static int is_p256_key(EVP_PKEY *private_key) {
  1752. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1753. return ec_key != NULL &&
  1754. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1755. NID_X9_62_prime256v1;
  1756. }
  1757. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1758. if (!is_p256_key(private_key)) {
  1759. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1760. return 0;
  1761. }
  1762. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1763. EVP_PKEY_up_ref(private_key);
  1764. ctx->tlsext_channel_id_private = private_key;
  1765. ctx->tlsext_channel_id_enabled = true;
  1766. return 1;
  1767. }
  1768. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1769. if (!is_p256_key(private_key)) {
  1770. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1771. return 0;
  1772. }
  1773. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1774. EVP_PKEY_up_ref(private_key);
  1775. ssl->tlsext_channel_id_private = private_key;
  1776. ssl->tlsext_channel_id_enabled = true;
  1777. return 1;
  1778. }
  1779. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1780. if (!ssl->s3->tlsext_channel_id_valid) {
  1781. return 0;
  1782. }
  1783. OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
  1784. (max_out < 64) ? max_out : 64);
  1785. return 64;
  1786. }
  1787. int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
  1788. if (len > 256) {
  1789. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  1790. return 0;
  1791. }
  1792. OPENSSL_free(ssl->token_binding_params);
  1793. ssl->token_binding_params = (uint8_t *)BUF_memdup(params, len);
  1794. if (!ssl->token_binding_params) {
  1795. return 0;
  1796. }
  1797. ssl->token_binding_params_len = len;
  1798. return 1;
  1799. }
  1800. int SSL_is_token_binding_negotiated(const SSL *ssl) {
  1801. return ssl->token_binding_negotiated;
  1802. }
  1803. uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
  1804. return ssl->negotiated_token_binding_param;
  1805. }
  1806. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1807. if (ssl->server || ssl->s3->hs == NULL) {
  1808. *out_types = NULL;
  1809. return 0;
  1810. }
  1811. *out_types = ssl->s3->hs->certificate_types.data();
  1812. return ssl->s3->hs->certificate_types.size();
  1813. }
  1814. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1815. if (ssl->cert != NULL) {
  1816. return ssl->cert->privatekey;
  1817. }
  1818. return NULL;
  1819. }
  1820. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1821. if (ctx->cert != NULL) {
  1822. return ctx->cert->privatekey;
  1823. }
  1824. return NULL;
  1825. }
  1826. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1827. return ssl->s3->aead_write_ctx->cipher();
  1828. }
  1829. int SSL_session_reused(const SSL *ssl) {
  1830. return ssl->s3->session_reused || SSL_in_early_data(ssl);
  1831. }
  1832. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1833. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1834. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1835. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1836. ctx->quiet_shutdown = (mode != 0);
  1837. }
  1838. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1839. return ctx->quiet_shutdown;
  1840. }
  1841. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1842. ssl->quiet_shutdown = (mode != 0);
  1843. }
  1844. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1845. void SSL_set_shutdown(SSL *ssl, int mode) {
  1846. // It is an error to clear any bits that have already been set. (We can't try
  1847. // to get a second close_notify or send two.)
  1848. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1849. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1850. ssl->s3->read_shutdown == ssl_shutdown_none) {
  1851. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1852. }
  1853. if (mode & SSL_SENT_SHUTDOWN &&
  1854. ssl->s3->write_shutdown == ssl_shutdown_none) {
  1855. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  1856. }
  1857. }
  1858. int SSL_get_shutdown(const SSL *ssl) {
  1859. int ret = 0;
  1860. if (ssl->s3->read_shutdown != ssl_shutdown_none) {
  1861. // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1862. // and fatal alert.
  1863. ret |= SSL_RECEIVED_SHUTDOWN;
  1864. }
  1865. if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
  1866. // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
  1867. ret |= SSL_SENT_SHUTDOWN;
  1868. }
  1869. return ret;
  1870. }
  1871. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1872. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1873. if (ssl->ctx == ctx) {
  1874. return ssl->ctx;
  1875. }
  1876. // One cannot change the X.509 callbacks during a connection.
  1877. if (ssl->ctx->x509_method != ctx->x509_method) {
  1878. assert(0);
  1879. return NULL;
  1880. }
  1881. if (ctx == NULL) {
  1882. ctx = ssl->session_ctx;
  1883. }
  1884. ssl_cert_free(ssl->cert);
  1885. ssl->cert = ssl_cert_dup(ctx->cert);
  1886. SSL_CTX_up_ref(ctx);
  1887. SSL_CTX_free(ssl->ctx);
  1888. ssl->ctx = ctx;
  1889. return ssl->ctx;
  1890. }
  1891. void SSL_set_info_callback(SSL *ssl,
  1892. void (*cb)(const SSL *ssl, int type, int value)) {
  1893. ssl->info_callback = cb;
  1894. }
  1895. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1896. int value) {
  1897. return ssl->info_callback;
  1898. }
  1899. int SSL_state(const SSL *ssl) {
  1900. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  1901. }
  1902. void SSL_set_state(SSL *ssl, int state) { }
  1903. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1904. if (len <= 0) {
  1905. return NULL;
  1906. }
  1907. buf[0] = '\0';
  1908. return buf;
  1909. }
  1910. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1911. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  1912. int index;
  1913. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1914. free_func)) {
  1915. return -1;
  1916. }
  1917. return index;
  1918. }
  1919. int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
  1920. return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
  1921. }
  1922. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1923. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1924. }
  1925. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1926. CRYPTO_EX_dup *dup_unused,
  1927. CRYPTO_EX_free *free_func) {
  1928. int index;
  1929. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1930. free_func)) {
  1931. return -1;
  1932. }
  1933. return index;
  1934. }
  1935. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
  1936. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1937. }
  1938. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1939. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1940. }
  1941. int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
  1942. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1943. RSA *(*cb)(SSL *ssl, int is_export,
  1944. int keylength)) {}
  1945. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1946. int keylength)) {}
  1947. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1948. DH *(*cb)(SSL *ssl, int is_export,
  1949. int keylength)) {}
  1950. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
  1951. int keylength)) {}
  1952. static int use_psk_identity_hint(char **out, const char *identity_hint) {
  1953. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1954. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1955. return 0;
  1956. }
  1957. // Clear currently configured hint, if any.
  1958. OPENSSL_free(*out);
  1959. *out = NULL;
  1960. // Treat the empty hint as not supplying one. Plain PSK makes it possible to
  1961. // send either no hint (omit ServerKeyExchange) or an empty hint, while
  1962. // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  1963. // so we interpret empty and missing as identical.
  1964. if (identity_hint != NULL && identity_hint[0] != '\0') {
  1965. *out = BUF_strdup(identity_hint);
  1966. if (*out == NULL) {
  1967. return 0;
  1968. }
  1969. }
  1970. return 1;
  1971. }
  1972. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1973. return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
  1974. }
  1975. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1976. return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
  1977. }
  1978. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1979. if (ssl == NULL) {
  1980. return NULL;
  1981. }
  1982. return ssl->psk_identity_hint;
  1983. }
  1984. const char *SSL_get_psk_identity(const SSL *ssl) {
  1985. if (ssl == NULL) {
  1986. return NULL;
  1987. }
  1988. SSL_SESSION *session = SSL_get_session(ssl);
  1989. if (session == NULL) {
  1990. return NULL;
  1991. }
  1992. return session->psk_identity;
  1993. }
  1994. void SSL_set_psk_client_callback(
  1995. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1996. unsigned max_identity_len, uint8_t *psk,
  1997. unsigned max_psk_len)) {
  1998. ssl->psk_client_callback = cb;
  1999. }
  2000. void SSL_CTX_set_psk_client_callback(
  2001. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2002. unsigned max_identity_len, uint8_t *psk,
  2003. unsigned max_psk_len)) {
  2004. ctx->psk_client_callback = cb;
  2005. }
  2006. void SSL_set_psk_server_callback(
  2007. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2008. unsigned max_psk_len)) {
  2009. ssl->psk_server_callback = cb;
  2010. }
  2011. void SSL_CTX_set_psk_server_callback(
  2012. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  2013. uint8_t *psk, unsigned max_psk_len)) {
  2014. ctx->psk_server_callback = cb;
  2015. }
  2016. int SSL_set_dummy_pq_padding_size(SSL *ssl, size_t num_bytes) {
  2017. if (num_bytes > 0xffff) {
  2018. return 0;
  2019. }
  2020. ssl->dummy_pq_padding_len = num_bytes;
  2021. return 1;
  2022. }
  2023. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2024. void (*cb)(int write_p, int version,
  2025. int content_type, const void *buf,
  2026. size_t len, SSL *ssl, void *arg)) {
  2027. ctx->msg_callback = cb;
  2028. }
  2029. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2030. ctx->msg_callback_arg = arg;
  2031. }
  2032. void SSL_set_msg_callback(SSL *ssl,
  2033. void (*cb)(int write_p, int version, int content_type,
  2034. const void *buf, size_t len, SSL *ssl,
  2035. void *arg)) {
  2036. ssl->msg_callback = cb;
  2037. }
  2038. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2039. ssl->msg_callback_arg = arg;
  2040. }
  2041. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2042. void (*cb)(const SSL *ssl, const char *line)) {
  2043. ctx->keylog_callback = cb;
  2044. }
  2045. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2046. const char *line) {
  2047. return ctx->keylog_callback;
  2048. }
  2049. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2050. void (*cb)(const SSL *ssl,
  2051. struct timeval *out_clock)) {
  2052. ctx->current_time_cb = cb;
  2053. }
  2054. int SSL_is_init_finished(const SSL *ssl) {
  2055. return !SSL_in_init(ssl);
  2056. }
  2057. int SSL_in_init(const SSL *ssl) {
  2058. // This returns false once all the handshake state has been finalized, to
  2059. // allow callbacks and getters based on SSL_in_init to return the correct
  2060. // values.
  2061. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2062. return hs != nullptr && !hs->handshake_finalized;
  2063. }
  2064. int SSL_in_false_start(const SSL *ssl) {
  2065. if (ssl->s3->hs == NULL) {
  2066. return 0;
  2067. }
  2068. return ssl->s3->hs->in_false_start;
  2069. }
  2070. int SSL_cutthrough_complete(const SSL *ssl) {
  2071. return SSL_in_false_start(ssl);
  2072. }
  2073. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2074. size_t *ssl_session_size) {
  2075. *ssl_size = sizeof(SSL);
  2076. *ssl_ctx_size = sizeof(SSL_CTX);
  2077. *ssl_session_size = sizeof(SSL_SESSION);
  2078. }
  2079. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2080. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2081. void SSL_CTX_set_select_certificate_cb(
  2082. SSL_CTX *ctx,
  2083. enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
  2084. ctx->select_certificate_cb = cb;
  2085. }
  2086. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2087. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2088. ctx->dos_protection_cb = cb;
  2089. }
  2090. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2091. ssl->renegotiate_mode = mode;
  2092. }
  2093. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2094. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2095. size_t write_iv_len;
  2096. if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
  2097. !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
  2098. *out_iv_len != write_iv_len) {
  2099. return 0;
  2100. }
  2101. return 1;
  2102. }
  2103. static uint64_t be_to_u64(const uint8_t in[8]) {
  2104. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2105. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2106. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2107. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2108. }
  2109. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2110. // TODO(davidben): Internally represent sequence numbers as uint64_t.
  2111. if (SSL_is_dtls(ssl)) {
  2112. // max_seq_num already includes the epoch.
  2113. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2114. return ssl->d1->bitmap.max_seq_num;
  2115. }
  2116. return be_to_u64(ssl->s3->read_sequence);
  2117. }
  2118. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2119. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2120. if (SSL_is_dtls(ssl)) {
  2121. assert((ret >> 48) == 0);
  2122. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2123. }
  2124. return ret;
  2125. }
  2126. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2127. // TODO(davidben): This checks the wrong session if there is a renegotiation
  2128. // in progress.
  2129. SSL_SESSION *session = SSL_get_session(ssl);
  2130. if (session == NULL) {
  2131. return 0;
  2132. }
  2133. return session->peer_signature_algorithm;
  2134. }
  2135. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2136. if (max_out == 0) {
  2137. return sizeof(ssl->s3->client_random);
  2138. }
  2139. if (max_out > sizeof(ssl->s3->client_random)) {
  2140. max_out = sizeof(ssl->s3->client_random);
  2141. }
  2142. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2143. return max_out;
  2144. }
  2145. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2146. if (max_out == 0) {
  2147. return sizeof(ssl->s3->server_random);
  2148. }
  2149. if (max_out > sizeof(ssl->s3->server_random)) {
  2150. max_out = sizeof(ssl->s3->server_random);
  2151. }
  2152. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2153. return max_out;
  2154. }
  2155. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2156. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2157. if (hs == NULL) {
  2158. return NULL;
  2159. }
  2160. return hs->new_cipher;
  2161. }
  2162. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2163. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2164. }
  2165. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2166. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2167. }
  2168. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2169. ctx->grease_enabled = !!enabled;
  2170. }
  2171. int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
  2172. return ssl->s3->ticket_age_skew;
  2173. }
  2174. void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
  2175. ctx->false_start_allowed_without_alpn = !!allowed;
  2176. }
  2177. int SSL_is_draft_downgrade(const SSL *ssl) { return ssl->s3->draft_downgrade; }
  2178. int SSL_clear(SSL *ssl) {
  2179. // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2180. // established session to be offered the next time around. wpa_supplicant
  2181. // depends on this behavior, so emulate it.
  2182. UniquePtr<SSL_SESSION> session;
  2183. if (!ssl->server && ssl->s3->established_session != NULL) {
  2184. session.reset(ssl->s3->established_session.get());
  2185. SSL_SESSION_up_ref(session.get());
  2186. }
  2187. // The ssl->d1->mtu is simultaneously configuration (preserved across
  2188. // clear) and connection-specific state (gets reset).
  2189. //
  2190. // TODO(davidben): Avoid this.
  2191. unsigned mtu = 0;
  2192. if (ssl->d1 != NULL) {
  2193. mtu = ssl->d1->mtu;
  2194. }
  2195. ssl->method->ssl_free(ssl);
  2196. if (!ssl->method->ssl_new(ssl)) {
  2197. return 0;
  2198. }
  2199. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2200. ssl->d1->mtu = mtu;
  2201. }
  2202. if (session != nullptr) {
  2203. SSL_set_session(ssl, session.get());
  2204. }
  2205. return 1;
  2206. }
  2207. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2208. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2209. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2210. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2211. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2212. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2213. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2214. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2215. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2216. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2217. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2218. int SSL_num_renegotiations(const SSL *ssl) {
  2219. return SSL_total_renegotiations(ssl);
  2220. }
  2221. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2222. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2223. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2224. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2225. void ERR_load_SSL_strings(void) {}
  2226. void SSL_load_error_strings(void) {}
  2227. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2228. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2229. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2230. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2231. return 0;
  2232. }
  2233. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2234. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2235. }
  2236. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2237. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2238. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2239. return 0;
  2240. }
  2241. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2242. return SSL_set1_curves(ssl, &nid, 1);
  2243. }
  2244. void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
  2245. const SSL_TICKET_AEAD_METHOD *aead_method) {
  2246. ctx->ticket_aead_method = aead_method;
  2247. }