選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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