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

2959 行
83 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 <assert.h>
  141. #include <stdio.h>
  142. #include <string.h>
  143. #include <openssl/bytestring.h>
  144. #include <openssl/dh.h>
  145. #include <openssl/err.h>
  146. #include <openssl/lhash.h>
  147. #include <openssl/mem.h>
  148. #include <openssl/obj.h>
  149. #include <openssl/rand.h>
  150. #include <openssl/x509v3.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. /* Some error codes are special. Ensure the make_errors.go script never
  154. * regresses this. */
  155. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  156. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  157. ssl_alert_reason_code_mismatch);
  158. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  159. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  160. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
  161. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
  162. int SSL_clear(SSL *ssl) {
  163. if (ssl->method == NULL) {
  164. OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
  165. return 0;
  166. }
  167. if (ssl_clear_bad_session(ssl)) {
  168. SSL_SESSION_free(ssl->session);
  169. ssl->session = NULL;
  170. }
  171. ssl->hit = 0;
  172. ssl->shutdown = 0;
  173. /* SSL_clear may be called before or after the |ssl| is initialized in either
  174. * accept or connect state. In the latter case, SSL_clear should preserve the
  175. * half and reset |ssl->state| accordingly. */
  176. if (ssl->handshake_func != NULL) {
  177. if (ssl->server) {
  178. SSL_set_accept_state(ssl);
  179. } else {
  180. SSL_set_connect_state(ssl);
  181. }
  182. } else {
  183. assert(ssl->state == 0);
  184. }
  185. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  186. * |SSL_clear| because it is per-connection state rather than configuration
  187. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  188. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  189. * |ssl3_new|. */
  190. ssl->rwstate = SSL_NOTHING;
  191. ssl->rstate = SSL_ST_READ_HEADER;
  192. BUF_MEM_free(ssl->init_buf);
  193. ssl->init_buf = NULL;
  194. ssl->packet = NULL;
  195. ssl->packet_length = 0;
  196. ssl_clear_cipher_ctx(ssl);
  197. OPENSSL_free(ssl->next_proto_negotiated);
  198. ssl->next_proto_negotiated = NULL;
  199. ssl->next_proto_negotiated_len = 0;
  200. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  201. * clear) and connection-specific state (gets reset).
  202. *
  203. * TODO(davidben): Avoid this. */
  204. unsigned mtu = 0;
  205. if (ssl->d1 != NULL) {
  206. mtu = ssl->d1->mtu;
  207. }
  208. ssl->method->ssl_free(ssl);
  209. if (!ssl->method->ssl_new(ssl)) {
  210. return 0;
  211. }
  212. ssl->enc_method = ssl3_get_enc_method(ssl->version);
  213. assert(ssl->enc_method != NULL);
  214. if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  215. ssl->d1->mtu = mtu;
  216. }
  217. ssl->client_version = ssl->version;
  218. return 1;
  219. }
  220. SSL *SSL_new(SSL_CTX *ctx) {
  221. SSL *s;
  222. if (ctx == NULL) {
  223. OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
  224. return NULL;
  225. }
  226. if (ctx->method == NULL) {
  227. OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  228. return NULL;
  229. }
  230. s = (SSL *)OPENSSL_malloc(sizeof(SSL));
  231. if (s == NULL) {
  232. goto err;
  233. }
  234. memset(s, 0, sizeof(SSL));
  235. s->min_version = ctx->min_version;
  236. s->max_version = ctx->max_version;
  237. s->options = ctx->options;
  238. s->mode = ctx->mode;
  239. s->max_cert_list = ctx->max_cert_list;
  240. s->cert = ssl_cert_dup(ctx->cert);
  241. if (s->cert == NULL) {
  242. goto err;
  243. }
  244. s->msg_callback = ctx->msg_callback;
  245. s->msg_callback_arg = ctx->msg_callback_arg;
  246. s->verify_mode = ctx->verify_mode;
  247. s->sid_ctx_length = ctx->sid_ctx_length;
  248. assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  249. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  250. s->verify_callback = ctx->default_verify_callback;
  251. s->generate_session_id = ctx->generate_session_id;
  252. s->param = X509_VERIFY_PARAM_new();
  253. if (!s->param) {
  254. goto err;
  255. }
  256. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  257. s->quiet_shutdown = ctx->quiet_shutdown;
  258. s->max_send_fragment = ctx->max_send_fragment;
  259. CRYPTO_refcount_inc(&ctx->references);
  260. s->ctx = ctx;
  261. s->tlsext_ticket_expected = 0;
  262. CRYPTO_refcount_inc(&ctx->references);
  263. s->initial_ctx = ctx;
  264. if (ctx->tlsext_ecpointformatlist) {
  265. s->tlsext_ecpointformatlist = BUF_memdup(
  266. ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
  267. if (!s->tlsext_ecpointformatlist) {
  268. goto err;
  269. }
  270. s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
  271. }
  272. if (ctx->tlsext_ellipticcurvelist) {
  273. s->tlsext_ellipticcurvelist =
  274. BUF_memdup(ctx->tlsext_ellipticcurvelist,
  275. ctx->tlsext_ellipticcurvelist_length * 2);
  276. if (!s->tlsext_ellipticcurvelist) {
  277. goto err;
  278. }
  279. s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
  280. }
  281. s->next_proto_negotiated = NULL;
  282. if (s->ctx->alpn_client_proto_list) {
  283. s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
  284. s->ctx->alpn_client_proto_list_len);
  285. if (s->alpn_client_proto_list == NULL) {
  286. goto err;
  287. }
  288. s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
  289. }
  290. s->verify_result = X509_V_OK;
  291. s->method = ctx->method;
  292. if (!s->method->ssl_new(s)) {
  293. goto err;
  294. }
  295. s->enc_method = ssl3_get_enc_method(s->version);
  296. assert(s->enc_method != NULL);
  297. s->rwstate = SSL_NOTHING;
  298. s->rstate = SSL_ST_READ_HEADER;
  299. CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
  300. s->psk_identity_hint = NULL;
  301. if (ctx->psk_identity_hint) {
  302. s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  303. if (s->psk_identity_hint == NULL) {
  304. goto err;
  305. }
  306. }
  307. s->psk_client_callback = ctx->psk_client_callback;
  308. s->psk_server_callback = ctx->psk_server_callback;
  309. s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  310. if (ctx->tlsext_channel_id_private) {
  311. s->tlsext_channel_id_private =
  312. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  313. }
  314. s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
  315. s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
  316. return s;
  317. err:
  318. SSL_free(s);
  319. OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
  320. return NULL;
  321. }
  322. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  323. unsigned int sid_ctx_len) {
  324. if (sid_ctx_len > sizeof ctx->sid_ctx) {
  325. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
  326. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  327. return 0;
  328. }
  329. ctx->sid_ctx_length = sid_ctx_len;
  330. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  331. return 1;
  332. }
  333. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  334. unsigned int sid_ctx_len) {
  335. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  336. OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
  337. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  338. return 0;
  339. }
  340. ssl->sid_ctx_length = sid_ctx_len;
  341. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  342. return 1;
  343. }
  344. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
  345. ctx->generate_session_id = cb;
  346. return 1;
  347. }
  348. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
  349. ssl->generate_session_id = cb;
  350. return 1;
  351. }
  352. int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
  353. unsigned int id_len) {
  354. /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
  355. * can "construct" a session to give us the desired check - ie. to find if
  356. * there's a session in the hash table that would conflict with any new
  357. * session built out of this id/id_len and the ssl_version in use by this
  358. * SSL. */
  359. SSL_SESSION r, *p;
  360. if (id_len > sizeof r.session_id) {
  361. return 0;
  362. }
  363. r.ssl_version = ssl->version;
  364. r.session_id_length = id_len;
  365. memcpy(r.session_id, id, id_len);
  366. CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
  367. p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
  368. CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
  369. return p != NULL;
  370. }
  371. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
  372. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  373. }
  374. int SSL_set_purpose(SSL *s, int purpose) {
  375. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  376. }
  377. int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
  378. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  379. }
  380. int SSL_set_trust(SSL *s, int trust) {
  381. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  382. }
  383. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
  384. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  385. }
  386. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
  387. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  388. }
  389. void ssl_cipher_preference_list_free(
  390. struct ssl_cipher_preference_list_st *cipher_list) {
  391. if (cipher_list == NULL) {
  392. return;
  393. }
  394. sk_SSL_CIPHER_free(cipher_list->ciphers);
  395. OPENSSL_free(cipher_list->in_group_flags);
  396. OPENSSL_free(cipher_list);
  397. }
  398. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
  399. struct ssl_cipher_preference_list_st *cipher_list) {
  400. struct ssl_cipher_preference_list_st *ret = NULL;
  401. size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
  402. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  403. if (!ret) {
  404. goto err;
  405. }
  406. ret->ciphers = NULL;
  407. ret->in_group_flags = NULL;
  408. ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
  409. if (!ret->ciphers) {
  410. goto err;
  411. }
  412. ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
  413. if (!ret->in_group_flags) {
  414. goto err;
  415. }
  416. return ret;
  417. err:
  418. ssl_cipher_preference_list_free(ret);
  419. return NULL;
  420. }
  421. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
  422. STACK_OF(SSL_CIPHER) *ciphers) {
  423. struct ssl_cipher_preference_list_st *ret = NULL;
  424. size_t n = sk_SSL_CIPHER_num(ciphers);
  425. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  426. if (!ret) {
  427. goto err;
  428. }
  429. ret->ciphers = NULL;
  430. ret->in_group_flags = NULL;
  431. ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
  432. if (!ret->ciphers) {
  433. goto err;
  434. }
  435. ret->in_group_flags = OPENSSL_malloc(n);
  436. if (!ret->in_group_flags) {
  437. goto err;
  438. }
  439. memset(ret->in_group_flags, 0, n);
  440. return ret;
  441. err:
  442. ssl_cipher_preference_list_free(ret);
  443. return NULL;
  444. }
  445. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  446. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  447. void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
  448. void SSL_free(SSL *ssl) {
  449. if (ssl == NULL) {
  450. return;
  451. }
  452. X509_VERIFY_PARAM_free(ssl->param);
  453. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  454. if (ssl->bbio != NULL) {
  455. /* If the buffering BIO is in place, pop it off */
  456. if (ssl->bbio == ssl->wbio) {
  457. ssl->wbio = BIO_pop(ssl->wbio);
  458. }
  459. BIO_free(ssl->bbio);
  460. ssl->bbio = NULL;
  461. }
  462. int free_wbio = ssl->wbio != ssl->rbio;
  463. BIO_free_all(ssl->rbio);
  464. if (free_wbio) {
  465. BIO_free_all(ssl->wbio);
  466. }
  467. BUF_MEM_free(ssl->init_buf);
  468. /* add extra stuff */
  469. ssl_cipher_preference_list_free(ssl->cipher_list);
  470. sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
  471. ssl_clear_bad_session(ssl);
  472. SSL_SESSION_free(ssl->session);
  473. ssl_clear_cipher_ctx(ssl);
  474. ssl_cert_free(ssl->cert);
  475. OPENSSL_free(ssl->tlsext_hostname);
  476. SSL_CTX_free(ssl->initial_ctx);
  477. OPENSSL_free(ssl->tlsext_ecpointformatlist);
  478. OPENSSL_free(ssl->tlsext_ellipticcurvelist);
  479. OPENSSL_free(ssl->alpn_client_proto_list);
  480. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  481. OPENSSL_free(ssl->psk_identity_hint);
  482. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  483. OPENSSL_free(ssl->next_proto_negotiated);
  484. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  485. if (ssl->method != NULL) {
  486. ssl->method->ssl_free(ssl);
  487. }
  488. SSL_CTX_free(ssl->ctx);
  489. OPENSSL_free(ssl);
  490. }
  491. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
  492. /* If the output buffering BIO is still in place, remove it. */
  493. if (s->bbio != NULL) {
  494. if (s->wbio == s->bbio) {
  495. s->wbio = s->wbio->next_bio;
  496. s->bbio->next_bio = NULL;
  497. }
  498. }
  499. if (s->rbio != rbio) {
  500. BIO_free_all(s->rbio);
  501. }
  502. if (s->wbio != wbio && s->rbio != s->wbio) {
  503. BIO_free_all(s->wbio);
  504. }
  505. s->rbio = rbio;
  506. s->wbio = wbio;
  507. }
  508. BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
  509. BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
  510. int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
  511. int SSL_get_rfd(const SSL *s) {
  512. int ret = -1;
  513. BIO *b, *r;
  514. b = SSL_get_rbio(s);
  515. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  516. if (r != NULL) {
  517. BIO_get_fd(r, &ret);
  518. }
  519. return ret;
  520. }
  521. int SSL_get_wfd(const SSL *s) {
  522. int ret = -1;
  523. BIO *b, *r;
  524. b = SSL_get_wbio(s);
  525. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  526. if (r != NULL) {
  527. BIO_get_fd(r, &ret);
  528. }
  529. return ret;
  530. }
  531. int SSL_set_fd(SSL *s, int fd) {
  532. int ret = 0;
  533. BIO *bio = NULL;
  534. bio = BIO_new(BIO_s_fd());
  535. if (bio == NULL) {
  536. OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
  537. goto err;
  538. }
  539. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  540. SSL_set_bio(s, bio, bio);
  541. ret = 1;
  542. err:
  543. return ret;
  544. }
  545. int SSL_set_wfd(SSL *s, int fd) {
  546. int ret = 0;
  547. BIO *bio = NULL;
  548. if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
  549. (int)BIO_get_fd(s->rbio, NULL) != fd) {
  550. bio = BIO_new(BIO_s_fd());
  551. if (bio == NULL) {
  552. OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
  553. goto err;
  554. }
  555. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  556. SSL_set_bio(s, SSL_get_rbio(s), bio);
  557. } else {
  558. SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
  559. }
  560. ret = 1;
  561. err:
  562. return ret;
  563. }
  564. int SSL_set_rfd(SSL *s, int fd) {
  565. int ret = 0;
  566. BIO *bio = NULL;
  567. if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
  568. (int)BIO_get_fd(s->wbio, NULL) != fd) {
  569. bio = BIO_new(BIO_s_fd());
  570. if (bio == NULL) {
  571. OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
  572. goto err;
  573. }
  574. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  575. SSL_set_bio(s, bio, SSL_get_wbio(s));
  576. } else {
  577. SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
  578. }
  579. ret = 1;
  580. err:
  581. return ret;
  582. }
  583. /* return length of latest Finished message we sent, copy to 'buf' */
  584. size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
  585. size_t ret = 0;
  586. if (s->s3 != NULL) {
  587. ret = s->s3->tmp.finish_md_len;
  588. if (count > ret) {
  589. count = ret;
  590. }
  591. memcpy(buf, s->s3->tmp.finish_md, count);
  592. }
  593. return ret;
  594. }
  595. /* return length of latest Finished message we expected, copy to 'buf' */
  596. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
  597. size_t ret = 0;
  598. if (s->s3 != NULL) {
  599. ret = s->s3->tmp.peer_finish_md_len;
  600. if (count > ret) {
  601. count = ret;
  602. }
  603. memcpy(buf, s->s3->tmp.peer_finish_md, count);
  604. }
  605. return ret;
  606. }
  607. int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
  608. int SSL_get_verify_depth(const SSL *s) {
  609. return X509_VERIFY_PARAM_get_depth(s->param);
  610. }
  611. int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
  612. return s->verify_callback;
  613. }
  614. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  615. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  616. return X509_VERIFY_PARAM_get_depth(ctx->param);
  617. }
  618. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
  619. return ctx->default_verify_callback;
  620. }
  621. void SSL_set_verify(SSL *s, int mode,
  622. int (*callback)(int ok, X509_STORE_CTX *ctx)) {
  623. s->verify_mode = mode;
  624. if (callback != NULL) {
  625. s->verify_callback = callback;
  626. }
  627. }
  628. void SSL_set_verify_depth(SSL *s, int depth) {
  629. X509_VERIFY_PARAM_set_depth(s->param, depth);
  630. }
  631. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  632. int SSL_get_read_ahead(const SSL *s) { return 0; }
  633. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  634. void SSL_set_read_ahead(SSL *s, int yes) { }
  635. int SSL_pending(const SSL *s) {
  636. if (s->rstate == SSL_ST_READ_BODY) {
  637. return 0;
  638. }
  639. return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
  640. : 0;
  641. }
  642. X509 *SSL_get_peer_certificate(const SSL *s) {
  643. X509 *r;
  644. if (s == NULL || s->session == NULL) {
  645. r = NULL;
  646. } else {
  647. r = s->session->peer;
  648. }
  649. if (r == NULL) {
  650. return NULL;
  651. }
  652. return X509_up_ref(r);
  653. }
  654. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
  655. STACK_OF(X509) *r;
  656. if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
  657. r = NULL;
  658. } else {
  659. r = s->session->sess_cert->cert_chain;
  660. }
  661. /* If we are a client, cert_chain includes the peer's own certificate; if we
  662. * are a server, it does not. */
  663. return r;
  664. }
  665. /* Fix this so it checks all the valid key/cert options */
  666. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  667. if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
  668. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  669. SSL_R_NO_CERTIFICATE_ASSIGNED);
  670. return 0;
  671. }
  672. if (ctx->cert->key->privatekey == NULL) {
  673. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  674. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  675. return 0;
  676. }
  677. return X509_check_private_key(ctx->cert->key->x509,
  678. ctx->cert->key->privatekey);
  679. }
  680. /* Fix this function so that it takes an optional type parameter */
  681. int SSL_check_private_key(const SSL *ssl) {
  682. if (ssl == NULL) {
  683. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
  684. return 0;
  685. }
  686. if (ssl->cert == NULL) {
  687. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  688. SSL_R_NO_CERTIFICATE_ASSIGNED);
  689. return 0;
  690. }
  691. if (ssl->cert->key->x509 == NULL) {
  692. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  693. SSL_R_NO_CERTIFICATE_ASSIGNED);
  694. return 0;
  695. }
  696. if (ssl->cert->key->privatekey == NULL) {
  697. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  698. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  699. return 0;
  700. }
  701. return X509_check_private_key(ssl->cert->key->x509,
  702. ssl->cert->key->privatekey);
  703. }
  704. int SSL_accept(SSL *s) {
  705. if (s->handshake_func == 0) {
  706. /* Not properly initialized yet */
  707. SSL_set_accept_state(s);
  708. }
  709. if (s->handshake_func != s->method->ssl_accept) {
  710. OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
  711. return -1;
  712. }
  713. return s->handshake_func(s);
  714. }
  715. int SSL_connect(SSL *s) {
  716. if (s->handshake_func == 0) {
  717. /* Not properly initialized yet */
  718. SSL_set_connect_state(s);
  719. }
  720. if (s->handshake_func != s->method->ssl_connect) {
  721. OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
  722. return -1;
  723. }
  724. return s->handshake_func(s);
  725. }
  726. long SSL_get_default_timeout(const SSL *s) {
  727. return SSL_DEFAULT_SESSION_TIMEOUT;
  728. }
  729. int SSL_read(SSL *s, void *buf, int num) {
  730. if (s->handshake_func == 0) {
  731. OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
  732. return -1;
  733. }
  734. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  735. s->rwstate = SSL_NOTHING;
  736. return 0;
  737. }
  738. ERR_clear_system_error();
  739. return s->method->ssl_read_app_data(s, buf, num, 0);
  740. }
  741. int SSL_peek(SSL *s, void *buf, int num) {
  742. if (s->handshake_func == 0) {
  743. OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
  744. return -1;
  745. }
  746. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  747. return 0;
  748. }
  749. ERR_clear_system_error();
  750. return s->method->ssl_read_app_data(s, buf, num, 1);
  751. }
  752. int SSL_write(SSL *s, const void *buf, int num) {
  753. if (s->handshake_func == 0) {
  754. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
  755. return -1;
  756. }
  757. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  758. s->rwstate = SSL_NOTHING;
  759. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
  760. return -1;
  761. }
  762. ERR_clear_system_error();
  763. return s->method->ssl_write_app_data(s, buf, num);
  764. }
  765. int SSL_shutdown(SSL *s) {
  766. /* Note that this function behaves differently from what one might expect.
  767. * Return values are 0 for no success (yet), 1 for success; but calling it
  768. * once is usually not enough, even if blocking I/O is used (see
  769. * ssl3_shutdown). */
  770. if (s->handshake_func == 0) {
  771. OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
  772. return -1;
  773. }
  774. if (SSL_in_init(s)) {
  775. return 1;
  776. }
  777. /* Do nothing if configured not to send a close_notify. */
  778. if (s->quiet_shutdown) {
  779. s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
  780. return 1;
  781. }
  782. if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
  783. s->shutdown |= SSL_SENT_SHUTDOWN;
  784. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
  785. /* our shutdown alert has been sent now, and if it still needs to be
  786. * written, s->s3->alert_dispatch will be true */
  787. if (s->s3->alert_dispatch) {
  788. return -1; /* return WANT_WRITE */
  789. }
  790. } else if (s->s3->alert_dispatch) {
  791. /* resend it if not sent */
  792. int ret = s->method->ssl_dispatch_alert(s);
  793. if (ret == -1) {
  794. /* we only get to return -1 here the 2nd/Nth invocation, we must have
  795. * already signalled return 0 upon a previous invoation, return
  796. * WANT_WRITE */
  797. return ret;
  798. }
  799. } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  800. /* If we are waiting for a close from our peer, we are closed */
  801. s->method->ssl_read_close_notify(s);
  802. if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  803. return -1; /* return WANT_READ */
  804. }
  805. }
  806. if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
  807. !s->s3->alert_dispatch) {
  808. return 1;
  809. } else {
  810. return 0;
  811. }
  812. }
  813. int SSL_renegotiate(SSL *ssl) {
  814. /* Caller-initiated renegotiation is not supported. */
  815. OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  816. return 0;
  817. }
  818. int SSL_renegotiate_pending(SSL *ssl) {
  819. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  820. }
  821. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  822. ctx->options |= options;
  823. return ctx->options;
  824. }
  825. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  826. ssl->options |= options;
  827. return ssl->options;
  828. }
  829. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  830. ctx->options &= ~options;
  831. return ctx->options;
  832. }
  833. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  834. ssl->options &= ~options;
  835. return ssl->options;
  836. }
  837. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  838. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  839. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  840. ctx->mode |= mode;
  841. return ctx->mode;
  842. }
  843. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  844. ssl->mode |= mode;
  845. return ssl->mode;
  846. }
  847. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  848. ctx->mode &= ~mode;
  849. return ctx->mode;
  850. }
  851. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  852. ssl->mode &= ~mode;
  853. return ssl->mode;
  854. }
  855. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  856. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  857. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  858. return ctx->max_cert_list;
  859. }
  860. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  861. if (max_cert_list > kMaxHandshakeSize) {
  862. max_cert_list = kMaxHandshakeSize;
  863. }
  864. ctx->max_cert_list = (uint32_t)max_cert_list;
  865. }
  866. size_t SSL_get_max_cert_list(const SSL *ssl) {
  867. return ssl->max_cert_list;
  868. }
  869. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  870. if (max_cert_list > kMaxHandshakeSize) {
  871. max_cert_list = kMaxHandshakeSize;
  872. }
  873. ssl->max_cert_list = (uint32_t)max_cert_list;
  874. }
  875. void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  876. if (max_send_fragment < 512) {
  877. max_send_fragment = 512;
  878. }
  879. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  880. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  881. }
  882. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  883. }
  884. void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  885. if (max_send_fragment < 512) {
  886. max_send_fragment = 512;
  887. }
  888. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  889. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  890. }
  891. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  892. }
  893. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  894. if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
  895. return 0;
  896. }
  897. ssl->d1->mtu = mtu;
  898. return 1;
  899. }
  900. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  901. return ssl->s3->send_connection_binding;
  902. }
  903. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
  904. return s->method->ssl_ctrl(s, cmd, larg, parg);
  905. }
  906. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  907. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  908. return lh_SSL_SESSION_num_items(ctx->sessions);
  909. }
  910. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  911. unsigned long ret = ctx->session_cache_size;
  912. ctx->session_cache_size = size;
  913. return ret;
  914. }
  915. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  916. return ctx->session_cache_size;
  917. }
  918. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  919. int ret = ctx->session_cache_mode;
  920. ctx->session_cache_mode = mode;
  921. return ret;
  922. }
  923. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  924. return ctx->session_cache_mode;
  925. }
  926. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
  927. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  928. }
  929. /* return a STACK of the ciphers available for the SSL and in order of
  930. * preference */
  931. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
  932. if (s == NULL) {
  933. return NULL;
  934. }
  935. if (s->cipher_list != NULL) {
  936. return s->cipher_list->ciphers;
  937. }
  938. if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
  939. s->ctx->cipher_list_tls11 != NULL) {
  940. return s->ctx->cipher_list_tls11->ciphers;
  941. }
  942. if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
  943. return s->ctx->cipher_list->ciphers;
  944. }
  945. return NULL;
  946. }
  947. /* return a STACK of the ciphers available for the SSL and in order of
  948. * algorithm id */
  949. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
  950. if (s == NULL) {
  951. return NULL;
  952. }
  953. if (s->cipher_list_by_id != NULL) {
  954. return s->cipher_list_by_id;
  955. }
  956. if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
  957. return s->ctx->cipher_list_by_id;
  958. }
  959. return NULL;
  960. }
  961. /* The old interface to get the same thing as SSL_get_ciphers() */
  962. const char *SSL_get_cipher_list(const SSL *s, int n) {
  963. const SSL_CIPHER *c;
  964. STACK_OF(SSL_CIPHER) *sk;
  965. if (s == NULL) {
  966. return NULL;
  967. }
  968. sk = SSL_get_ciphers(s);
  969. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  970. return NULL;
  971. }
  972. c = sk_SSL_CIPHER_value(sk, n);
  973. if (c == NULL) {
  974. return NULL;
  975. }
  976. return c->name;
  977. }
  978. /* specify the ciphers to be used by default by the SSL_CTX */
  979. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  980. STACK_OF(SSL_CIPHER) *sk;
  981. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
  982. &ctx->cipher_list_by_id, str);
  983. /* ssl_create_cipher_list may return an empty stack if it was unable to find
  984. * a cipher matching the given rule string (for example if the rule string
  985. * specifies a cipher which has been disabled). This is not an error as far
  986. * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
  987. * ctx->cipher_list_by_id has been updated. */
  988. if (sk == NULL) {
  989. return 0;
  990. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  991. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  992. return 0;
  993. }
  994. return 1;
  995. }
  996. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  997. STACK_OF(SSL_CIPHER) *sk;
  998. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
  999. if (sk == NULL) {
  1000. return 0;
  1001. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
  1003. SSL_R_NO_CIPHER_MATCH);
  1004. return 0;
  1005. }
  1006. return 1;
  1007. }
  1008. /* specify the ciphers to be used by the SSL */
  1009. int SSL_set_cipher_list(SSL *s, const char *str) {
  1010. STACK_OF(SSL_CIPHER) *sk;
  1011. sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
  1012. &s->cipher_list_by_id, str);
  1013. /* see comment in SSL_CTX_set_cipher_list */
  1014. if (sk == NULL) {
  1015. return 0;
  1016. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1017. OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  1018. return 0;
  1019. }
  1020. return 1;
  1021. }
  1022. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
  1023. size_t i;
  1024. const SSL_CIPHER *c;
  1025. CERT *ct = s->cert;
  1026. uint8_t *q;
  1027. /* Set disabled masks for this session */
  1028. ssl_set_client_disabled(s);
  1029. if (sk == NULL) {
  1030. return 0;
  1031. }
  1032. q = p;
  1033. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  1034. c = sk_SSL_CIPHER_value(sk, i);
  1035. /* Skip disabled ciphers */
  1036. if (c->algorithm_ssl & ct->mask_ssl ||
  1037. c->algorithm_mkey & ct->mask_k ||
  1038. c->algorithm_auth & ct->mask_a) {
  1039. continue;
  1040. }
  1041. s2n(ssl_cipher_get_value(c), p);
  1042. }
  1043. /* If all ciphers were disabled, return the error to the caller. */
  1044. if (p == q) {
  1045. return 0;
  1046. }
  1047. /* Add SCSVs. */
  1048. if (!s->s3->initial_handshake_complete) {
  1049. s2n(SSL3_CK_SCSV & 0xffff, p);
  1050. }
  1051. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  1052. s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
  1053. }
  1054. return p - q;
  1055. }
  1056. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
  1057. CBS cipher_suites = *cbs;
  1058. const SSL_CIPHER *c;
  1059. STACK_OF(SSL_CIPHER) *sk;
  1060. if (s->s3) {
  1061. s->s3->send_connection_binding = 0;
  1062. }
  1063. if (CBS_len(&cipher_suites) % 2 != 0) {
  1064. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1065. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1066. return NULL;
  1067. }
  1068. sk = sk_SSL_CIPHER_new_null();
  1069. if (sk == NULL) {
  1070. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1071. goto err;
  1072. }
  1073. while (CBS_len(&cipher_suites) > 0) {
  1074. uint16_t cipher_suite;
  1075. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1076. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
  1077. goto err;
  1078. }
  1079. /* Check for SCSV. */
  1080. if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1081. /* SCSV is fatal if renegotiating. */
  1082. if (s->s3->initial_handshake_complete) {
  1083. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1084. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1085. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1086. goto err;
  1087. }
  1088. s->s3->send_connection_binding = 1;
  1089. continue;
  1090. }
  1091. /* Check for FALLBACK_SCSV. */
  1092. if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1093. uint16_t max_version = ssl3_get_max_server_version(s);
  1094. if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
  1095. : (uint16_t)s->version < max_version) {
  1096. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1097. SSL_R_INAPPROPRIATE_FALLBACK);
  1098. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1099. goto err;
  1100. }
  1101. continue;
  1102. }
  1103. c = SSL_get_cipher_by_value(cipher_suite);
  1104. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1105. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1106. goto err;
  1107. }
  1108. }
  1109. return sk;
  1110. err:
  1111. sk_SSL_CIPHER_free(sk);
  1112. return NULL;
  1113. }
  1114. /* return a servername extension value if provided in Client Hello, or NULL. So
  1115. * far, only host_name types are defined (RFC 3546). */
  1116. const char *SSL_get_servername(const SSL *s, const int type) {
  1117. if (type != TLSEXT_NAMETYPE_host_name) {
  1118. return NULL;
  1119. }
  1120. return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
  1121. : s->tlsext_hostname;
  1122. }
  1123. int SSL_get_servername_type(const SSL *s) {
  1124. if (s->session &&
  1125. (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
  1126. return TLSEXT_NAMETYPE_host_name;
  1127. }
  1128. return -1;
  1129. }
  1130. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1131. ctx->signed_cert_timestamps_enabled = 1;
  1132. }
  1133. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1134. ssl->signed_cert_timestamps_enabled = 1;
  1135. return 1;
  1136. }
  1137. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1138. ctx->ocsp_stapling_enabled = 1;
  1139. }
  1140. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1141. ssl->ocsp_stapling_enabled = 1;
  1142. return 1;
  1143. }
  1144. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1145. size_t *out_len) {
  1146. SSL_SESSION *session = ssl->session;
  1147. *out_len = 0;
  1148. *out = NULL;
  1149. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1150. return;
  1151. }
  1152. *out = session->tlsext_signed_cert_timestamp_list;
  1153. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1154. }
  1155. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1156. size_t *out_len) {
  1157. SSL_SESSION *session = ssl->session;
  1158. *out_len = 0;
  1159. *out = NULL;
  1160. if (ssl->server || !session || !session->ocsp_response) {
  1161. return;
  1162. }
  1163. *out = session->ocsp_response;
  1164. *out_len = session->ocsp_response_length;
  1165. }
  1166. /* SSL_select_next_proto implements the standard protocol selection. It is
  1167. * expected that this function is called from the callback set by
  1168. * SSL_CTX_set_next_proto_select_cb.
  1169. *
  1170. * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
  1171. * strings. The length byte itself is not included in the length. A byte
  1172. * string of length 0 is invalid. No byte string may be truncated.
  1173. *
  1174. * The current, but experimental algorithm for selecting the protocol is:
  1175. *
  1176. * 1) If the server doesn't support NPN then this is indicated to the
  1177. * callback. In this case, the client application has to abort the connection
  1178. * or have a default application level protocol.
  1179. *
  1180. * 2) If the server supports NPN, but advertises an empty list then the
  1181. * client selects the first protcol in its list, but indicates via the
  1182. * API that this fallback case was enacted.
  1183. *
  1184. * 3) Otherwise, the client finds the first protocol in the server's list
  1185. * that it supports and selects this protocol. This is because it's
  1186. * assumed that the server has better information about which protocol
  1187. * a client should use.
  1188. *
  1189. * 4) If the client doesn't support any of the server's advertised
  1190. * protocols, then this is treated the same as case 2.
  1191. *
  1192. * It returns either
  1193. * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
  1194. * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  1195. */
  1196. int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
  1197. unsigned int server_len, const uint8_t *client,
  1198. unsigned int client_len) {
  1199. unsigned int i, j;
  1200. const uint8_t *result;
  1201. int status = OPENSSL_NPN_UNSUPPORTED;
  1202. /* For each protocol in server preference order, see if we support it. */
  1203. for (i = 0; i < server_len;) {
  1204. for (j = 0; j < client_len;) {
  1205. if (server[i] == client[j] &&
  1206. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1207. /* We found a match */
  1208. result = &server[i];
  1209. status = OPENSSL_NPN_NEGOTIATED;
  1210. goto found;
  1211. }
  1212. j += client[j];
  1213. j++;
  1214. }
  1215. i += server[i];
  1216. i++;
  1217. }
  1218. /* There's no overlap between our protocols and the server's list. */
  1219. result = client;
  1220. status = OPENSSL_NPN_NO_OVERLAP;
  1221. found:
  1222. *out = (uint8_t *)result + 1;
  1223. *outlen = result[0];
  1224. return status;
  1225. }
  1226. /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
  1227. * requested protocol for this connection and returns 0. If the client didn't
  1228. * request any protocol, then *data is set to NULL.
  1229. *
  1230. * Note that the client can request any protocol it chooses. The value returned
  1231. * from this function need not be a member of the list of supported protocols
  1232. * provided by the callback. */
  1233. void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
  1234. unsigned *len) {
  1235. *data = s->next_proto_negotiated;
  1236. if (!*data) {
  1237. *len = 0;
  1238. } else {
  1239. *len = s->next_proto_negotiated_len;
  1240. }
  1241. }
  1242. /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
  1243. * TLS server needs a list of supported protocols for Next Protocol
  1244. * Negotiation. The returned list must be in wire format. The list is returned
  1245. * by setting |out| to point to it and |outlen| to its length. This memory will
  1246. * not be modified, but one should assume that the SSL* keeps a reference to
  1247. * it.
  1248. *
  1249. * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
  1250. * Otherwise, no such extension will be included in the ServerHello. */
  1251. void SSL_CTX_set_next_protos_advertised_cb(
  1252. SSL_CTX *ctx,
  1253. int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
  1254. void *arg) {
  1255. ctx->next_protos_advertised_cb = cb;
  1256. ctx->next_protos_advertised_cb_arg = arg;
  1257. }
  1258. /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  1259. * client needs to select a protocol from the server's provided list. |out|
  1260. * must be set to point to the selected protocol (which may be within |in|).
  1261. * The length of the protocol name must be written into |outlen|. The server's
  1262. * advertised protocols are provided in |in| and |inlen|. The callback can
  1263. * assume that |in| is syntactically valid.
  1264. *
  1265. * The client must select a protocol. It is fatal to the connection if this
  1266. * callback returns a value other than SSL_TLSEXT_ERR_OK.
  1267. */
  1268. void SSL_CTX_set_next_proto_select_cb(
  1269. SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
  1270. const uint8_t *in, unsigned int inlen, void *arg),
  1271. void *arg) {
  1272. ctx->next_proto_select_cb = cb;
  1273. ctx->next_proto_select_cb_arg = arg;
  1274. }
  1275. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1276. unsigned protos_len) {
  1277. OPENSSL_free(ctx->alpn_client_proto_list);
  1278. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1279. if (!ctx->alpn_client_proto_list) {
  1280. return 1;
  1281. }
  1282. ctx->alpn_client_proto_list_len = protos_len;
  1283. return 0;
  1284. }
  1285. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1286. OPENSSL_free(ssl->alpn_client_proto_list);
  1287. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1288. if (!ssl->alpn_client_proto_list) {
  1289. return 1;
  1290. }
  1291. ssl->alpn_client_proto_list_len = protos_len;
  1292. return 0;
  1293. }
  1294. /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
  1295. * during ClientHello processing in order to select an ALPN protocol from the
  1296. * client's list of offered protocols. */
  1297. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1298. int (*cb)(SSL *ssl, const uint8_t **out,
  1299. uint8_t *outlen, const uint8_t *in,
  1300. unsigned int inlen, void *arg),
  1301. void *arg) {
  1302. ctx->alpn_select_cb = cb;
  1303. ctx->alpn_select_cb_arg = arg;
  1304. }
  1305. /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  1306. * On return it sets |*data| to point to |*len| bytes of protocol name (not
  1307. * including the leading length-prefix byte). If the server didn't respond with
  1308. * a negotiated protocol then |*len| will be zero. */
  1309. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
  1310. unsigned *len) {
  1311. *data = NULL;
  1312. if (ssl->s3) {
  1313. *data = ssl->s3->alpn_selected;
  1314. }
  1315. if (*data == NULL) {
  1316. *len = 0;
  1317. } else {
  1318. *len = ssl->s3->alpn_selected_len;
  1319. }
  1320. }
  1321. int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  1322. const char *label, size_t label_len,
  1323. const uint8_t *context, size_t context_len,
  1324. int use_context) {
  1325. if (s->version < TLS1_VERSION) {
  1326. return 0;
  1327. }
  1328. return s->enc_method->export_keying_material(
  1329. s, out, out_len, label, label_len, context, context_len, use_context);
  1330. }
  1331. static uint32_t ssl_session_hash(const SSL_SESSION *a) {
  1332. uint32_t hash =
  1333. ((uint32_t)a->session_id[0]) ||
  1334. ((uint32_t)a->session_id[1] << 8) ||
  1335. ((uint32_t)a->session_id[2] << 16) ||
  1336. ((uint32_t)a->session_id[3] << 24);
  1337. return hash;
  1338. }
  1339. /* NB: If this function (or indeed the hash function which uses a sort of
  1340. * coarser function than this one) is changed, ensure
  1341. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  1342. * able to construct an SSL_SESSION that will collide with any existing session
  1343. * with a matching session ID. */
  1344. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  1345. if (a->ssl_version != b->ssl_version) {
  1346. return 1;
  1347. }
  1348. if (a->session_id_length != b->session_id_length) {
  1349. return 1;
  1350. }
  1351. return memcmp(a->session_id, b->session_id, a->session_id_length);
  1352. }
  1353. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  1354. SSL_CTX *ret = NULL;
  1355. if (method == NULL) {
  1356. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
  1357. return NULL;
  1358. }
  1359. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  1360. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1361. goto err;
  1362. }
  1363. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  1364. if (ret == NULL) {
  1365. goto err;
  1366. }
  1367. memset(ret, 0, sizeof(SSL_CTX));
  1368. ret->method = method->method;
  1369. CRYPTO_MUTEX_init(&ret->lock);
  1370. ret->cert_store = NULL;
  1371. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  1372. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1373. ret->session_cache_head = NULL;
  1374. ret->session_cache_tail = NULL;
  1375. /* We take the system default */
  1376. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  1377. ret->new_session_cb = 0;
  1378. ret->remove_session_cb = 0;
  1379. ret->get_session_cb = 0;
  1380. ret->generate_session_id = 0;
  1381. ret->references = 1;
  1382. ret->quiet_shutdown = 0;
  1383. ret->info_callback = NULL;
  1384. ret->app_verify_callback = 0;
  1385. ret->app_verify_arg = NULL;
  1386. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  1387. ret->msg_callback = 0;
  1388. ret->msg_callback_arg = NULL;
  1389. ret->verify_mode = SSL_VERIFY_NONE;
  1390. ret->sid_ctx_length = 0;
  1391. ret->default_verify_callback = NULL;
  1392. ret->cert = ssl_cert_new();
  1393. if (ret->cert == NULL) {
  1394. goto err;
  1395. }
  1396. ret->default_passwd_callback = 0;
  1397. ret->default_passwd_callback_userdata = NULL;
  1398. ret->client_cert_cb = 0;
  1399. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  1400. if (ret->sessions == NULL) {
  1401. goto err;
  1402. }
  1403. ret->cert_store = X509_STORE_new();
  1404. if (ret->cert_store == NULL) {
  1405. goto err;
  1406. }
  1407. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  1408. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  1409. if (ret->cipher_list == NULL ||
  1410. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  1411. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1412. goto err2;
  1413. }
  1414. ret->param = X509_VERIFY_PARAM_new();
  1415. if (!ret->param) {
  1416. goto err;
  1417. }
  1418. ret->client_CA = sk_X509_NAME_new_null();
  1419. if (ret->client_CA == NULL) {
  1420. goto err;
  1421. }
  1422. CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
  1423. ret->extra_certs = NULL;
  1424. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1425. ret->tlsext_servername_callback = 0;
  1426. ret->tlsext_servername_arg = NULL;
  1427. /* Setup RFC4507 ticket keys */
  1428. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  1429. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  1430. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  1431. ret->options |= SSL_OP_NO_TICKET;
  1432. }
  1433. ret->next_protos_advertised_cb = 0;
  1434. ret->next_proto_select_cb = 0;
  1435. ret->psk_identity_hint = NULL;
  1436. ret->psk_client_callback = NULL;
  1437. ret->psk_server_callback = NULL;
  1438. /* Default is to connect to non-RI servers. When RI is more widely deployed
  1439. * might change this. */
  1440. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  1441. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  1442. * uses of SSL_METHOD. */
  1443. if (method->version != 0) {
  1444. SSL_CTX_set_max_version(ret, method->version);
  1445. SSL_CTX_set_min_version(ret, method->version);
  1446. }
  1447. return ret;
  1448. err:
  1449. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
  1450. err2:
  1451. SSL_CTX_free(ret);
  1452. return NULL;
  1453. }
  1454. void SSL_CTX_free(SSL_CTX *ctx) {
  1455. if (ctx == NULL ||
  1456. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  1457. return;
  1458. }
  1459. X509_VERIFY_PARAM_free(ctx->param);
  1460. /* Free internal session cache. However: the remove_cb() may reference the
  1461. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  1462. * sessions were flushed. As the ex_data handling routines might also touch
  1463. * the session cache, the most secure solution seems to be: empty (flush) the
  1464. * cache, then free ex_data, then finally free the cache. (See ticket
  1465. * [openssl.org #212].) */
  1466. SSL_CTX_flush_sessions(ctx, 0);
  1467. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  1468. CRYPTO_MUTEX_cleanup(&ctx->lock);
  1469. lh_SSL_SESSION_free(ctx->sessions);
  1470. X509_STORE_free(ctx->cert_store);
  1471. ssl_cipher_preference_list_free(ctx->cipher_list);
  1472. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  1473. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  1474. ssl_cert_free(ctx->cert);
  1475. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  1476. sk_X509_pop_free(ctx->extra_certs, X509_free);
  1477. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  1478. OPENSSL_free(ctx->psk_identity_hint);
  1479. OPENSSL_free(ctx->tlsext_ecpointformatlist);
  1480. OPENSSL_free(ctx->tlsext_ellipticcurvelist);
  1481. OPENSSL_free(ctx->alpn_client_proto_list);
  1482. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1483. BIO_free(ctx->keylog_bio);
  1484. OPENSSL_free(ctx);
  1485. }
  1486. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
  1487. ctx->default_passwd_callback = cb;
  1488. }
  1489. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
  1490. ctx->default_passwd_callback_userdata = u;
  1491. }
  1492. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1493. int (*cb)(X509_STORE_CTX *, void *),
  1494. void *arg) {
  1495. ctx->app_verify_callback = cb;
  1496. ctx->app_verify_arg = arg;
  1497. }
  1498. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1499. int (*cb)(int, X509_STORE_CTX *)) {
  1500. ctx->verify_mode = mode;
  1501. ctx->default_verify_callback = cb;
  1502. }
  1503. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1504. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1505. }
  1506. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
  1507. void *arg) {
  1508. ssl_cert_set_cert_cb(c->cert, cb, arg);
  1509. }
  1510. void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1511. ssl_cert_set_cert_cb(s->cert, cb, arg);
  1512. }
  1513. static int ssl_has_key(SSL *s, size_t idx) {
  1514. CERT_PKEY *cpk = &s->cert->pkeys[idx];
  1515. return cpk->x509 && cpk->privatekey;
  1516. }
  1517. void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
  1518. uint32_t *out_mask_a) {
  1519. CERT *c = s->cert;
  1520. int rsa_enc, rsa_sign, dh_tmp;
  1521. uint32_t mask_k, mask_a;
  1522. int have_ecc_cert, ecdsa_ok;
  1523. X509 *x;
  1524. if (c == NULL) {
  1525. /* TODO(davidben): Is this codepath possible? */
  1526. *out_mask_k = 0;
  1527. *out_mask_a = 0;
  1528. return;
  1529. }
  1530. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1531. rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
  1532. rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
  1533. have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
  1534. mask_k = 0;
  1535. mask_a = 0;
  1536. if (rsa_enc) {
  1537. mask_k |= SSL_kRSA;
  1538. }
  1539. if (dh_tmp) {
  1540. mask_k |= SSL_kDHE;
  1541. }
  1542. if (rsa_enc || rsa_sign) {
  1543. mask_a |= SSL_aRSA;
  1544. }
  1545. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1546. * key usage extension and on the client's curve preferences. */
  1547. if (have_ecc_cert) {
  1548. x = c->pkeys[SSL_PKEY_ECC].x509;
  1549. /* This call populates extension flags (ex_flags). */
  1550. X509_check_purpose(x, -1, 0);
  1551. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1552. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1553. : 1;
  1554. if (!tls1_check_ec_cert(s, x)) {
  1555. ecdsa_ok = 0;
  1556. }
  1557. if (ecdsa_ok) {
  1558. mask_a |= SSL_aECDSA;
  1559. }
  1560. }
  1561. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1562. * key, check it. */
  1563. if (tls1_check_ec_tmp_key(s)) {
  1564. mask_k |= SSL_kECDHE;
  1565. }
  1566. /* PSK requires a server callback. */
  1567. if (s->psk_server_callback != NULL) {
  1568. mask_k |= SSL_kPSK;
  1569. mask_a |= SSL_aPSK;
  1570. }
  1571. *out_mask_k = mask_k;
  1572. *out_mask_a = mask_a;
  1573. }
  1574. static int ssl_get_server_cert_index(const SSL *s) {
  1575. int idx;
  1576. idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1577. if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
  1578. idx = SSL_PKEY_RSA_SIGN;
  1579. }
  1580. if (idx == -1) {
  1581. OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
  1582. }
  1583. return idx;
  1584. }
  1585. CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
  1586. int i = ssl_get_server_cert_index(s);
  1587. /* This may or may not be an error. */
  1588. if (i < 0) {
  1589. return NULL;
  1590. }
  1591. /* May be NULL. */
  1592. return &s->cert->pkeys[i];
  1593. }
  1594. EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
  1595. uint32_t alg_a = cipher->algorithm_auth;
  1596. CERT *c = s->cert;
  1597. int idx = -1;
  1598. if (alg_a & SSL_aRSA) {
  1599. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
  1600. idx = SSL_PKEY_RSA_SIGN;
  1601. } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
  1602. idx = SSL_PKEY_RSA_ENC;
  1603. }
  1604. } else if ((alg_a & SSL_aECDSA) &&
  1605. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
  1606. idx = SSL_PKEY_ECC;
  1607. }
  1608. if (idx == -1) {
  1609. OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
  1610. return NULL;
  1611. }
  1612. return c->pkeys[idx].privatekey;
  1613. }
  1614. void ssl_update_cache(SSL *s, int mode) {
  1615. /* Never cache sessions with empty session IDs. */
  1616. if (s->session->session_id_length == 0) {
  1617. return;
  1618. }
  1619. int has_new_session = !s->hit;
  1620. if (!s->server && s->tlsext_ticket_expected) {
  1621. /* A client may see new sessions on abbreviated handshakes if the server
  1622. * decides to renew the ticket. Once the handshake is completed, it should
  1623. * be inserted into the cache. */
  1624. has_new_session = 1;
  1625. }
  1626. SSL_CTX *ctx = s->initial_ctx;
  1627. if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
  1628. ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
  1629. SSL_CTX_add_session(ctx, s->session)) &&
  1630. ctx->new_session_cb != NULL) {
  1631. /* Note: |new_session_cb| is called whether the internal session cache is
  1632. * used or not. */
  1633. if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
  1634. SSL_SESSION_free(s->session);
  1635. }
  1636. }
  1637. if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
  1638. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
  1639. (ctx->session_cache_mode & mode) == mode) {
  1640. /* Automatically flush the internal session cache every 255 connections. */
  1641. int flush_cache = 0;
  1642. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1643. ctx->handshakes_since_cache_flush++;
  1644. if (ctx->handshakes_since_cache_flush >= 255) {
  1645. flush_cache = 1;
  1646. ctx->handshakes_since_cache_flush = 0;
  1647. }
  1648. CRYPTO_MUTEX_unlock(&ctx->lock);
  1649. if (flush_cache) {
  1650. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1651. }
  1652. }
  1653. }
  1654. int SSL_get_error(const SSL *s, int ret_code) {
  1655. int reason;
  1656. uint32_t err;
  1657. BIO *bio;
  1658. if (ret_code > 0) {
  1659. return SSL_ERROR_NONE;
  1660. }
  1661. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1662. * where we do encode the error */
  1663. err = ERR_peek_error();
  1664. if (err != 0) {
  1665. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1666. return SSL_ERROR_SYSCALL;
  1667. }
  1668. return SSL_ERROR_SSL;
  1669. }
  1670. if (ret_code == 0) {
  1671. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  1672. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
  1673. /* The socket was cleanly shut down with a close_notify. */
  1674. return SSL_ERROR_ZERO_RETURN;
  1675. }
  1676. /* An EOF was observed which violates the protocol, and the underlying
  1677. * transport does not participate in the error queue. Bubble up to the
  1678. * caller. */
  1679. return SSL_ERROR_SYSCALL;
  1680. }
  1681. if (SSL_want_session(s)) {
  1682. return SSL_ERROR_PENDING_SESSION;
  1683. }
  1684. if (SSL_want_certificate(s)) {
  1685. return SSL_ERROR_PENDING_CERTIFICATE;
  1686. }
  1687. if (SSL_want_read(s)) {
  1688. bio = SSL_get_rbio(s);
  1689. if (BIO_should_read(bio)) {
  1690. return SSL_ERROR_WANT_READ;
  1691. }
  1692. if (BIO_should_write(bio)) {
  1693. /* This one doesn't make too much sense ... We never try to write to the
  1694. * rbio, and an application program where rbio and wbio are separate
  1695. * couldn't even know what it should wait for. However if we ever set
  1696. * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
  1697. * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
  1698. * around that bug; so it might be safer to keep it. */
  1699. return SSL_ERROR_WANT_WRITE;
  1700. }
  1701. if (BIO_should_io_special(bio)) {
  1702. reason = BIO_get_retry_reason(bio);
  1703. if (reason == BIO_RR_CONNECT) {
  1704. return SSL_ERROR_WANT_CONNECT;
  1705. }
  1706. if (reason == BIO_RR_ACCEPT) {
  1707. return SSL_ERROR_WANT_ACCEPT;
  1708. }
  1709. return SSL_ERROR_SYSCALL; /* unknown */
  1710. }
  1711. }
  1712. if (SSL_want_write(s)) {
  1713. bio = SSL_get_wbio(s);
  1714. if (BIO_should_write(bio)) {
  1715. return SSL_ERROR_WANT_WRITE;
  1716. }
  1717. if (BIO_should_read(bio)) {
  1718. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  1719. return SSL_ERROR_WANT_READ;
  1720. }
  1721. if (BIO_should_io_special(bio)) {
  1722. reason = BIO_get_retry_reason(bio);
  1723. if (reason == BIO_RR_CONNECT) {
  1724. return SSL_ERROR_WANT_CONNECT;
  1725. }
  1726. if (reason == BIO_RR_ACCEPT) {
  1727. return SSL_ERROR_WANT_ACCEPT;
  1728. }
  1729. return SSL_ERROR_SYSCALL;
  1730. }
  1731. }
  1732. if (SSL_want_x509_lookup(s)) {
  1733. return SSL_ERROR_WANT_X509_LOOKUP;
  1734. }
  1735. if (SSL_want_channel_id_lookup(s)) {
  1736. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1737. }
  1738. if (SSL_want_private_key_operation(s)) {
  1739. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  1740. }
  1741. return SSL_ERROR_SYSCALL;
  1742. }
  1743. int SSL_do_handshake(SSL *s) {
  1744. int ret = 1;
  1745. if (s->handshake_func == NULL) {
  1746. OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
  1747. return -1;
  1748. }
  1749. if (SSL_in_init(s)) {
  1750. ret = s->handshake_func(s);
  1751. }
  1752. return ret;
  1753. }
  1754. void SSL_set_accept_state(SSL *ssl) {
  1755. ssl->server = 1;
  1756. ssl->shutdown = 0;
  1757. ssl->state = SSL_ST_ACCEPT;
  1758. ssl->handshake_func = ssl->method->ssl_accept;
  1759. /* clear the current cipher */
  1760. ssl_clear_cipher_ctx(ssl);
  1761. }
  1762. void SSL_set_connect_state(SSL *ssl) {
  1763. ssl->server = 0;
  1764. ssl->shutdown = 0;
  1765. ssl->state = SSL_ST_CONNECT;
  1766. ssl->handshake_func = ssl->method->ssl_connect;
  1767. /* clear the current cipher */
  1768. ssl_clear_cipher_ctx(ssl);
  1769. }
  1770. static const char *ssl_get_version(int version) {
  1771. switch (version) {
  1772. case TLS1_2_VERSION:
  1773. return "TLSv1.2";
  1774. case TLS1_1_VERSION:
  1775. return "TLSv1.1";
  1776. case TLS1_VERSION:
  1777. return "TLSv1";
  1778. case SSL3_VERSION:
  1779. return "SSLv3";
  1780. case DTLS1_VERSION:
  1781. return "DTLSv1";
  1782. case DTLS1_2_VERSION:
  1783. return "DTLSv1.2";
  1784. default:
  1785. return "unknown";
  1786. }
  1787. }
  1788. const char *SSL_get_version(const SSL *s) {
  1789. return ssl_get_version(s->version);
  1790. }
  1791. const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
  1792. return ssl_get_version(sess->ssl_version);
  1793. }
  1794. void ssl_clear_cipher_ctx(SSL *s) {
  1795. SSL_AEAD_CTX_free(s->aead_read_ctx);
  1796. s->aead_read_ctx = NULL;
  1797. SSL_AEAD_CTX_free(s->aead_write_ctx);
  1798. s->aead_write_ctx = NULL;
  1799. }
  1800. X509 *SSL_get_certificate(const SSL *s) {
  1801. if (s->cert != NULL) {
  1802. return s->cert->key->x509;
  1803. }
  1804. return NULL;
  1805. }
  1806. EVP_PKEY *SSL_get_privatekey(const SSL *s) {
  1807. if (s->cert != NULL) {
  1808. return s->cert->key->privatekey;
  1809. }
  1810. return NULL;
  1811. }
  1812. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1813. if (ctx->cert != NULL) {
  1814. return ctx->cert->key->x509;
  1815. }
  1816. return NULL;
  1817. }
  1818. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1819. if (ctx->cert != NULL) {
  1820. return ctx->cert->key->privatekey;
  1821. }
  1822. return NULL;
  1823. }
  1824. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
  1825. if (s->aead_write_ctx == NULL) {
  1826. return NULL;
  1827. }
  1828. return s->aead_write_ctx->cipher;
  1829. }
  1830. const void *SSL_get_current_compression(SSL *s) { return NULL; }
  1831. const void *SSL_get_current_expansion(SSL *s) { return NULL; }
  1832. int ssl_init_wbio_buffer(SSL *s, int push) {
  1833. BIO *bbio;
  1834. if (s->bbio == NULL) {
  1835. bbio = BIO_new(BIO_f_buffer());
  1836. if (bbio == NULL) {
  1837. return 0;
  1838. }
  1839. s->bbio = bbio;
  1840. } else {
  1841. bbio = s->bbio;
  1842. if (s->bbio == s->wbio) {
  1843. s->wbio = BIO_pop(s->wbio);
  1844. }
  1845. }
  1846. BIO_reset(bbio);
  1847. if (!BIO_set_read_buffer_size(bbio, 1)) {
  1848. OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
  1849. return 0;
  1850. }
  1851. if (push) {
  1852. if (s->wbio != bbio) {
  1853. s->wbio = BIO_push(bbio, s->wbio);
  1854. }
  1855. } else {
  1856. if (s->wbio == bbio) {
  1857. s->wbio = BIO_pop(bbio);
  1858. }
  1859. }
  1860. return 1;
  1861. }
  1862. void ssl_free_wbio_buffer(SSL *s) {
  1863. if (s->bbio == NULL) {
  1864. return;
  1865. }
  1866. if (s->bbio == s->wbio) {
  1867. /* remove buffering */
  1868. s->wbio = BIO_pop(s->wbio);
  1869. }
  1870. BIO_free(s->bbio);
  1871. s->bbio = NULL;
  1872. }
  1873. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1874. ctx->quiet_shutdown = mode;
  1875. }
  1876. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1877. return ctx->quiet_shutdown;
  1878. }
  1879. void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
  1880. int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
  1881. void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
  1882. int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
  1883. int SSL_version(const SSL *s) { return s->version; }
  1884. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1885. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1886. if (ssl->ctx == ctx) {
  1887. return ssl->ctx;
  1888. }
  1889. if (ctx == NULL) {
  1890. ctx = ssl->initial_ctx;
  1891. }
  1892. ssl_cert_free(ssl->cert);
  1893. ssl->cert = ssl_cert_dup(ctx->cert);
  1894. CRYPTO_refcount_inc(&ctx->references);
  1895. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1896. ssl->ctx = ctx;
  1897. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1898. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1899. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1900. return ssl->ctx;
  1901. }
  1902. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1903. return X509_STORE_set_default_paths(ctx->cert_store);
  1904. }
  1905. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  1906. const char *CApath) {
  1907. return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
  1908. }
  1909. void SSL_set_info_callback(SSL *ssl,
  1910. void (*cb)(const SSL *ssl, int type, int val)) {
  1911. ssl->info_callback = cb;
  1912. }
  1913. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
  1914. int /*val*/) {
  1915. return ssl->info_callback;
  1916. }
  1917. int SSL_state(const SSL *ssl) { return ssl->state; }
  1918. void SSL_set_state(SSL *ssl, int state) { }
  1919. void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
  1920. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  1921. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1922. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1923. int index;
  1924. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1925. new_func, dup_func, free_func)) {
  1926. return -1;
  1927. }
  1928. return index;
  1929. }
  1930. int SSL_set_ex_data(SSL *s, int idx, void *arg) {
  1931. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  1932. }
  1933. void *SSL_get_ex_data(const SSL *s, int idx) {
  1934. return CRYPTO_get_ex_data(&s->ex_data, idx);
  1935. }
  1936. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1937. CRYPTO_EX_dup *dup_func,
  1938. CRYPTO_EX_free *free_func) {
  1939. int index;
  1940. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1941. new_func, dup_func, free_func)) {
  1942. return -1;
  1943. }
  1944. return index;
  1945. }
  1946. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
  1947. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  1948. }
  1949. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
  1950. return CRYPTO_get_ex_data(&s->ex_data, idx);
  1951. }
  1952. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1953. return ctx->cert_store;
  1954. }
  1955. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1956. X509_STORE_free(ctx->cert_store);
  1957. ctx->cert_store = store;
  1958. }
  1959. int SSL_want(const SSL *s) { return s->rwstate; }
  1960. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1961. RSA *(*cb)(SSL *ssl, int is_export,
  1962. int keylength)) {
  1963. }
  1964. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1965. int keylength)) {
  1966. }
  1967. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1968. DH *(*callback)(SSL *ssl, int is_export,
  1969. int keylength)) {
  1970. ctx->cert->dh_tmp_cb = callback;
  1971. }
  1972. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1973. int keylength)) {
  1974. ssl->cert->dh_tmp_cb = callback;
  1975. }
  1976. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
  1977. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1978. int keylength)) {
  1979. ctx->cert->ecdh_tmp_cb = callback;
  1980. }
  1981. void SSL_set_tmp_ecdh_callback(SSL *ssl,
  1982. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1983. int keylength)) {
  1984. ssl->cert->ecdh_tmp_cb = callback;
  1985. }
  1986. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1987. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1988. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
  1989. SSL_R_DATA_LENGTH_TOO_LONG);
  1990. return 0;
  1991. }
  1992. OPENSSL_free(ctx->psk_identity_hint);
  1993. if (identity_hint != NULL) {
  1994. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1995. if (ctx->psk_identity_hint == NULL) {
  1996. return 0;
  1997. }
  1998. } else {
  1999. ctx->psk_identity_hint = NULL;
  2000. }
  2001. return 1;
  2002. }
  2003. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
  2004. if (s == NULL) {
  2005. return 0;
  2006. }
  2007. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2008. OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
  2009. SSL_R_DATA_LENGTH_TOO_LONG);
  2010. return 0;
  2011. }
  2012. /* Clear currently configured hint, if any. */
  2013. OPENSSL_free(s->psk_identity_hint);
  2014. s->psk_identity_hint = NULL;
  2015. if (identity_hint != NULL) {
  2016. s->psk_identity_hint = BUF_strdup(identity_hint);
  2017. if (s->psk_identity_hint == NULL) {
  2018. return 0;
  2019. }
  2020. }
  2021. return 1;
  2022. }
  2023. const char *SSL_get_psk_identity_hint(const SSL *s) {
  2024. if (s == NULL) {
  2025. return NULL;
  2026. }
  2027. return s->psk_identity_hint;
  2028. }
  2029. const char *SSL_get_psk_identity(const SSL *s) {
  2030. if (s == NULL || s->session == NULL) {
  2031. return NULL;
  2032. }
  2033. return s->session->psk_identity;
  2034. }
  2035. void SSL_set_psk_client_callback(
  2036. SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2037. unsigned int max_identity_len, uint8_t *psk,
  2038. unsigned int max_psk_len)) {
  2039. s->psk_client_callback = cb;
  2040. }
  2041. void SSL_CTX_set_psk_client_callback(
  2042. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2043. unsigned int max_identity_len,
  2044. uint8_t *psk, unsigned int max_psk_len)) {
  2045. ctx->psk_client_callback = cb;
  2046. }
  2047. void SSL_set_psk_server_callback(
  2048. SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2049. unsigned int max_psk_len)) {
  2050. s->psk_server_callback = cb;
  2051. }
  2052. void SSL_CTX_set_psk_server_callback(
  2053. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
  2054. uint8_t *psk, unsigned int max_psk_len)) {
  2055. ctx->psk_server_callback = cb;
  2056. }
  2057. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2058. ctx->min_version = version;
  2059. }
  2060. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2061. ctx->max_version = version;
  2062. }
  2063. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  2064. ssl->min_version = version;
  2065. }
  2066. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  2067. ssl->max_version = version;
  2068. }
  2069. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2070. void (*cb)(int write_p, int version,
  2071. int content_type, const void *buf,
  2072. size_t len, SSL *ssl, void *arg)) {
  2073. ctx->msg_callback = cb;
  2074. }
  2075. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2076. ctx->msg_callback_arg = arg;
  2077. }
  2078. void SSL_set_msg_callback(SSL *ssl,
  2079. void (*cb)(int write_p, int version, int content_type,
  2080. const void *buf, size_t len, SSL *ssl,
  2081. void *arg)) {
  2082. ssl->msg_callback = cb;
  2083. }
  2084. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2085. ssl->msg_callback_arg = arg;
  2086. }
  2087. void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
  2088. BIO_free(ctx->keylog_bio);
  2089. ctx->keylog_bio = keylog_bio;
  2090. }
  2091. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  2092. static const char hextable[] = "0123456789abcdef";
  2093. uint8_t *out;
  2094. size_t i;
  2095. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  2096. return 0;
  2097. }
  2098. for (i = 0; i < in_len; i++) {
  2099. *(out++) = (uint8_t)hextable[in[i] >> 4];
  2100. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  2101. }
  2102. return 1;
  2103. }
  2104. int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
  2105. const uint8_t *encrypted_premaster,
  2106. size_t encrypted_premaster_len,
  2107. const uint8_t *premaster,
  2108. size_t premaster_len) {
  2109. BIO *bio = ctx->keylog_bio;
  2110. CBB cbb;
  2111. uint8_t *out;
  2112. size_t out_len;
  2113. int ret;
  2114. if (bio == NULL) {
  2115. return 1;
  2116. }
  2117. if (encrypted_premaster_len < 8) {
  2118. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
  2119. ERR_R_INTERNAL_ERROR);
  2120. return 0;
  2121. }
  2122. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
  2123. return 0;
  2124. }
  2125. if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  2126. /* Only the first 8 bytes of the encrypted premaster secret are
  2127. * logged. */
  2128. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  2129. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2130. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  2131. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2132. !CBB_finish(&cbb, &out, &out_len)) {
  2133. CBB_cleanup(&cbb);
  2134. return 0;
  2135. }
  2136. CRYPTO_MUTEX_lock_write(&ctx->lock);
  2137. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2138. CRYPTO_MUTEX_unlock(&ctx->lock);
  2139. OPENSSL_free(out);
  2140. return ret;
  2141. }
  2142. int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
  2143. size_t client_random_len, const uint8_t *master,
  2144. size_t master_len) {
  2145. BIO *bio = ctx->keylog_bio;
  2146. CBB cbb;
  2147. uint8_t *out;
  2148. size_t out_len;
  2149. int ret;
  2150. if (bio == NULL) {
  2151. return 1;
  2152. }
  2153. if (client_random_len != 32) {
  2154. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
  2155. return 0;
  2156. }
  2157. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
  2158. return 0;
  2159. }
  2160. if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  2161. !cbb_add_hex(&cbb, client_random, 32) ||
  2162. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2163. !cbb_add_hex(&cbb, master, master_len) ||
  2164. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2165. !CBB_finish(&cbb, &out, &out_len)) {
  2166. CBB_cleanup(&cbb);
  2167. return 0;
  2168. }
  2169. CRYPTO_MUTEX_lock_write(&ctx->lock);
  2170. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2171. CRYPTO_MUTEX_unlock(&ctx->lock);
  2172. OPENSSL_free(out);
  2173. return ret;
  2174. }
  2175. int SSL_in_false_start(const SSL *s) {
  2176. return s->s3->tmp.in_false_start;
  2177. }
  2178. int SSL_cutthrough_complete(const SSL *s) {
  2179. return SSL_in_false_start(s);
  2180. }
  2181. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2182. size_t *ssl_session_size) {
  2183. *ssl_size = sizeof(SSL);
  2184. *ssl_ctx_size = sizeof(SSL_CTX);
  2185. *ssl_session_size = sizeof(SSL_SESSION);
  2186. }
  2187. int ssl3_can_false_start(const SSL *s) {
  2188. const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
  2189. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  2190. return !SSL_IS_DTLS(s) &&
  2191. SSL_version(s) >= TLS1_2_VERSION &&
  2192. (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
  2193. cipher != NULL &&
  2194. cipher->algorithm_mkey == SSL_kECDHE &&
  2195. (cipher->algorithm_enc == SSL_AES128GCM ||
  2196. cipher->algorithm_enc == SSL_AES256GCM ||
  2197. cipher->algorithm_enc == SSL_CHACHA20POLY1305);
  2198. }
  2199. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  2200. switch (version) {
  2201. case SSL3_VERSION:
  2202. return &SSLv3_enc_data;
  2203. case TLS1_VERSION:
  2204. return &TLSv1_enc_data;
  2205. case DTLS1_VERSION:
  2206. case TLS1_1_VERSION:
  2207. return &TLSv1_1_enc_data;
  2208. case DTLS1_2_VERSION:
  2209. case TLS1_2_VERSION:
  2210. return &TLSv1_2_enc_data;
  2211. default:
  2212. return NULL;
  2213. }
  2214. }
  2215. uint16_t ssl3_get_max_server_version(const SSL *s) {
  2216. uint16_t max_version;
  2217. if (SSL_IS_DTLS(s)) {
  2218. max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
  2219. if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
  2220. return DTLS1_2_VERSION;
  2221. }
  2222. if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  2223. return DTLS1_VERSION;
  2224. }
  2225. return 0;
  2226. }
  2227. max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
  2228. if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  2229. return TLS1_2_VERSION;
  2230. }
  2231. if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  2232. return TLS1_1_VERSION;
  2233. }
  2234. if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  2235. return TLS1_VERSION;
  2236. }
  2237. if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  2238. return SSL3_VERSION;
  2239. }
  2240. return 0;
  2241. }
  2242. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
  2243. uint16_t version = 0;
  2244. if (SSL_IS_DTLS(s)) {
  2245. /* Clamp client_version to max_version. */
  2246. if (s->max_version != 0 && client_version < s->max_version) {
  2247. client_version = s->max_version;
  2248. }
  2249. if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
  2250. version = DTLS1_2_VERSION;
  2251. } else if (client_version <= DTLS1_VERSION &&
  2252. !(s->options & SSL_OP_NO_DTLSv1)) {
  2253. version = DTLS1_VERSION;
  2254. }
  2255. /* Check against min_version. */
  2256. if (version != 0 && s->min_version != 0 && version > s->min_version) {
  2257. return 0;
  2258. }
  2259. return version;
  2260. } else {
  2261. /* Clamp client_version to max_version. */
  2262. if (s->max_version != 0 && client_version > s->max_version) {
  2263. client_version = s->max_version;
  2264. }
  2265. if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
  2266. version = TLS1_2_VERSION;
  2267. } else if (client_version >= TLS1_1_VERSION &&
  2268. !(s->options & SSL_OP_NO_TLSv1_1)) {
  2269. version = TLS1_1_VERSION;
  2270. } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
  2271. version = TLS1_VERSION;
  2272. } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
  2273. version = SSL3_VERSION;
  2274. }
  2275. /* Check against min_version. */
  2276. if (version != 0 && s->min_version != 0 && version < s->min_version) {
  2277. return 0;
  2278. }
  2279. return version;
  2280. }
  2281. }
  2282. uint16_t ssl3_get_max_client_version(SSL *s) {
  2283. uint32_t options = s->options;
  2284. uint16_t version = 0;
  2285. /* OpenSSL's API for controlling versions entails blacklisting individual
  2286. * protocols. This has two problems. First, on the client, the protocol can
  2287. * only express a contiguous range of versions. Second, a library consumer
  2288. * trying to set a maximum version cannot disable protocol versions that get
  2289. * added in a future version of the library.
  2290. *
  2291. * To account for both of these, OpenSSL interprets the client-side bitmask
  2292. * as a min/max range by picking the lowest contiguous non-empty range of
  2293. * enabled protocols. Note that this means it is impossible to set a maximum
  2294. * version of TLS 1.2 in a future-proof way.
  2295. *
  2296. * By this scheme, the maximum version is the lowest version V such that V is
  2297. * enabled and V+1 is disabled or unimplemented. */
  2298. if (SSL_IS_DTLS(s)) {
  2299. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  2300. version = DTLS1_2_VERSION;
  2301. }
  2302. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  2303. version = DTLS1_VERSION;
  2304. }
  2305. if (s->max_version != 0 && version < s->max_version) {
  2306. version = s->max_version;
  2307. }
  2308. } else {
  2309. if (!(options & SSL_OP_NO_TLSv1_2)) {
  2310. version = TLS1_2_VERSION;
  2311. }
  2312. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  2313. version = TLS1_1_VERSION;
  2314. }
  2315. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  2316. version = TLS1_VERSION;
  2317. }
  2318. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  2319. version = SSL3_VERSION;
  2320. }
  2321. if (s->max_version != 0 && version > s->max_version) {
  2322. version = s->max_version;
  2323. }
  2324. }
  2325. return version;
  2326. }
  2327. int ssl3_is_version_enabled(SSL *s, uint16_t version) {
  2328. if (SSL_IS_DTLS(s)) {
  2329. if (s->max_version != 0 && version < s->max_version) {
  2330. return 0;
  2331. }
  2332. if (s->min_version != 0 && version > s->min_version) {
  2333. return 0;
  2334. }
  2335. switch (version) {
  2336. case DTLS1_VERSION:
  2337. return !(s->options & SSL_OP_NO_DTLSv1);
  2338. case DTLS1_2_VERSION:
  2339. return !(s->options & SSL_OP_NO_DTLSv1_2);
  2340. default:
  2341. return 0;
  2342. }
  2343. } else {
  2344. if (s->max_version != 0 && version > s->max_version) {
  2345. return 0;
  2346. }
  2347. if (s->min_version != 0 && version < s->min_version) {
  2348. return 0;
  2349. }
  2350. switch (version) {
  2351. case SSL3_VERSION:
  2352. return !(s->options & SSL_OP_NO_SSLv3);
  2353. case TLS1_VERSION:
  2354. return !(s->options & SSL_OP_NO_TLSv1);
  2355. case TLS1_1_VERSION:
  2356. return !(s->options & SSL_OP_NO_TLSv1_1);
  2357. case TLS1_2_VERSION:
  2358. return !(s->options & SSL_OP_NO_TLSv1_2);
  2359. default:
  2360. return 0;
  2361. }
  2362. }
  2363. }
  2364. uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
  2365. if (!SSL_IS_DTLS(s)) {
  2366. return wire_version;
  2367. }
  2368. uint16_t tls_version = ~wire_version;
  2369. uint16_t version = tls_version + 0x0201;
  2370. /* If either component overflowed, clamp it so comparisons still work. */
  2371. if ((version >> 8) < (tls_version >> 8)) {
  2372. version = 0xff00 | (version & 0xff);
  2373. }
  2374. if ((version & 0xff) < (tls_version & 0xff)) {
  2375. version = (version & 0xff00) | 0xff;
  2376. }
  2377. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2378. if (version == TLS1_VERSION) {
  2379. version = TLS1_1_VERSION;
  2380. }
  2381. return version;
  2382. }
  2383. int SSL_cache_hit(SSL *s) { return s->hit; }
  2384. int SSL_is_server(SSL *s) { return s->server; }
  2385. void SSL_CTX_set_dos_protection_cb(
  2386. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2387. ctx->dos_protection_cb = cb;
  2388. }
  2389. void SSL_enable_fastradio_padding(SSL *s, char on_off) {
  2390. s->fastradio_padding = on_off;
  2391. }
  2392. void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
  2393. s->accept_peer_renegotiations = !reject;
  2394. }
  2395. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2396. const RC4_KEY **write_key) {
  2397. if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
  2398. return 0;
  2399. }
  2400. return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
  2401. EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
  2402. }
  2403. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  2404. size_t max_out) {
  2405. /* The tls-unique value is the first Finished message in the handshake, which
  2406. * is the client's in a full handshake and the server's for a resumption. See
  2407. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  2408. const uint8_t *finished = ssl->s3->previous_client_finished;
  2409. size_t finished_len = ssl->s3->previous_client_finished_len;
  2410. if (ssl->hit) {
  2411. /* tls-unique is broken for resumed sessions unless EMS is used. */
  2412. if (!ssl->session->extended_master_secret) {
  2413. goto err;
  2414. }
  2415. finished = ssl->s3->previous_server_finished;
  2416. finished_len = ssl->s3->previous_server_finished_len;
  2417. }
  2418. if (!ssl->s3->initial_handshake_complete ||
  2419. ssl->version < TLS1_VERSION) {
  2420. goto err;
  2421. }
  2422. *out_len = finished_len;
  2423. if (finished_len > max_out) {
  2424. *out_len = max_out;
  2425. }
  2426. memcpy(out, finished, *out_len);
  2427. return 1;
  2428. err:
  2429. *out_len = 0;
  2430. memset(out, 0, max_out);
  2431. return 0;
  2432. }
  2433. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2434. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2435. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2436. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2437. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2438. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2439. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2440. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2441. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2442. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2443. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }