Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

2938 rindas
82 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. return s->method->ssl_pending(s);
  637. }
  638. X509 *SSL_get_peer_certificate(const SSL *s) {
  639. X509 *r;
  640. if (s == NULL || s->session == NULL) {
  641. r = NULL;
  642. } else {
  643. r = s->session->peer;
  644. }
  645. if (r == NULL) {
  646. return NULL;
  647. }
  648. return X509_up_ref(r);
  649. }
  650. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
  651. STACK_OF(X509) *r;
  652. if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
  653. r = NULL;
  654. } else {
  655. r = s->session->sess_cert->cert_chain;
  656. }
  657. /* If we are a client, cert_chain includes the peer's own certificate; if we
  658. * are a server, it does not. */
  659. return r;
  660. }
  661. /* Fix this so it checks all the valid key/cert options */
  662. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  663. if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
  664. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  665. SSL_R_NO_CERTIFICATE_ASSIGNED);
  666. return 0;
  667. }
  668. if (ctx->cert->key->privatekey == NULL) {
  669. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  670. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  671. return 0;
  672. }
  673. return X509_check_private_key(ctx->cert->key->x509,
  674. ctx->cert->key->privatekey);
  675. }
  676. /* Fix this function so that it takes an optional type parameter */
  677. int SSL_check_private_key(const SSL *ssl) {
  678. if (ssl == NULL) {
  679. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
  680. return 0;
  681. }
  682. if (ssl->cert == NULL) {
  683. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  684. SSL_R_NO_CERTIFICATE_ASSIGNED);
  685. return 0;
  686. }
  687. if (ssl->cert->key->x509 == NULL) {
  688. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  689. SSL_R_NO_CERTIFICATE_ASSIGNED);
  690. return 0;
  691. }
  692. if (ssl->cert->key->privatekey == NULL) {
  693. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  694. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  695. return 0;
  696. }
  697. return X509_check_private_key(ssl->cert->key->x509,
  698. ssl->cert->key->privatekey);
  699. }
  700. int SSL_accept(SSL *s) {
  701. if (s->handshake_func == 0) {
  702. /* Not properly initialized yet */
  703. SSL_set_accept_state(s);
  704. }
  705. if (s->handshake_func != s->method->ssl_accept) {
  706. OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
  707. return -1;
  708. }
  709. return s->handshake_func(s);
  710. }
  711. int SSL_connect(SSL *s) {
  712. if (s->handshake_func == 0) {
  713. /* Not properly initialized yet */
  714. SSL_set_connect_state(s);
  715. }
  716. if (s->handshake_func != s->method->ssl_connect) {
  717. OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
  718. return -1;
  719. }
  720. return s->handshake_func(s);
  721. }
  722. long SSL_get_default_timeout(const SSL *s) {
  723. return SSL_DEFAULT_SESSION_TIMEOUT;
  724. }
  725. int SSL_read(SSL *s, void *buf, int num) {
  726. if (s->handshake_func == 0) {
  727. OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
  728. return -1;
  729. }
  730. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  731. s->rwstate = SSL_NOTHING;
  732. return 0;
  733. }
  734. return s->method->ssl_read(s, buf, num);
  735. }
  736. int SSL_peek(SSL *s, void *buf, int num) {
  737. if (s->handshake_func == 0) {
  738. OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
  739. return -1;
  740. }
  741. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  742. return 0;
  743. }
  744. return s->method->ssl_peek(s, buf, num);
  745. }
  746. int SSL_write(SSL *s, const void *buf, int num) {
  747. if (s->handshake_func == 0) {
  748. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
  749. return -1;
  750. }
  751. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  752. s->rwstate = SSL_NOTHING;
  753. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
  754. return -1;
  755. }
  756. return s->method->ssl_write(s, buf, num);
  757. }
  758. int SSL_shutdown(SSL *s) {
  759. /* Note that this function behaves differently from what one might expect.
  760. * Return values are 0 for no success (yet), 1 for success; but calling it
  761. * once is usually not enough, even if blocking I/O is used (see
  762. * ssl3_shutdown). */
  763. if (s->handshake_func == 0) {
  764. OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
  765. return -1;
  766. }
  767. if (!SSL_in_init(s)) {
  768. return s->method->ssl_shutdown(s);
  769. }
  770. return 1;
  771. }
  772. int SSL_renegotiate(SSL *s) {
  773. if (SSL_IS_DTLS(s)) {
  774. /* Renegotiation is not supported for DTLS. */
  775. OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  776. return 0;
  777. }
  778. if (s->renegotiate == 0) {
  779. s->renegotiate = 1;
  780. }
  781. return s->method->ssl_renegotiate(s);
  782. }
  783. int SSL_renegotiate_pending(SSL *s) {
  784. /* becomes true when negotiation is requested; false again once a handshake
  785. * has finished */
  786. return s->renegotiate != 0;
  787. }
  788. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  789. ctx->options |= options;
  790. return ctx->options;
  791. }
  792. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  793. ssl->options |= options;
  794. return ssl->options;
  795. }
  796. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  797. ctx->options &= ~options;
  798. return ctx->options;
  799. }
  800. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  801. ssl->options &= ~options;
  802. return ssl->options;
  803. }
  804. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  805. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  806. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  807. ctx->mode |= mode;
  808. return ctx->mode;
  809. }
  810. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  811. ssl->mode |= mode;
  812. return ssl->mode;
  813. }
  814. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  815. ctx->mode &= ~mode;
  816. return ctx->mode;
  817. }
  818. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  819. ssl->mode &= ~mode;
  820. return ssl->mode;
  821. }
  822. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  823. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  824. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  825. return ctx->max_cert_list;
  826. }
  827. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  828. if (max_cert_list > kMaxHandshakeSize) {
  829. max_cert_list = kMaxHandshakeSize;
  830. }
  831. ctx->max_cert_list = (uint32_t)max_cert_list;
  832. }
  833. size_t SSL_get_max_cert_list(const SSL *ssl) {
  834. return ssl->max_cert_list;
  835. }
  836. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  837. if (max_cert_list > kMaxHandshakeSize) {
  838. max_cert_list = kMaxHandshakeSize;
  839. }
  840. ssl->max_cert_list = (uint32_t)max_cert_list;
  841. }
  842. void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  843. if (max_send_fragment < 512) {
  844. max_send_fragment = 512;
  845. }
  846. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  847. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  848. }
  849. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  850. }
  851. void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  852. if (max_send_fragment < 512) {
  853. max_send_fragment = 512;
  854. }
  855. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  856. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  857. }
  858. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  859. }
  860. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  861. if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
  862. return 0;
  863. }
  864. ssl->d1->mtu = mtu;
  865. return 1;
  866. }
  867. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  868. return ssl->s3->send_connection_binding;
  869. }
  870. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
  871. return s->method->ssl_ctrl(s, cmd, larg, parg);
  872. }
  873. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  874. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  875. return lh_SSL_SESSION_num_items(ctx->sessions);
  876. }
  877. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  878. unsigned long ret = ctx->session_cache_size;
  879. ctx->session_cache_size = size;
  880. return ret;
  881. }
  882. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  883. return ctx->session_cache_size;
  884. }
  885. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  886. int ret = ctx->session_cache_mode;
  887. ctx->session_cache_mode = mode;
  888. return ret;
  889. }
  890. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  891. return ctx->session_cache_mode;
  892. }
  893. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
  894. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  895. }
  896. int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  897. long l;
  898. const SSL_CIPHER *a = in_a;
  899. const SSL_CIPHER *b = in_b;
  900. const long a_id = a->id;
  901. const long b_id = b->id;
  902. l = a_id - b_id;
  903. if (l == 0L) {
  904. return 0;
  905. } else {
  906. return (l > 0) ? 1 : -1;
  907. }
  908. }
  909. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
  910. long l;
  911. const long a_id = (*ap)->id;
  912. const long b_id = (*bp)->id;
  913. l = a_id - b_id;
  914. if (l == 0) {
  915. return 0;
  916. } else {
  917. return (l > 0) ? 1 : -1;
  918. }
  919. }
  920. /* return a STACK of the ciphers available for the SSL and in order of
  921. * preference */
  922. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
  923. if (s == NULL) {
  924. return NULL;
  925. }
  926. if (s->cipher_list != NULL) {
  927. return s->cipher_list->ciphers;
  928. }
  929. if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
  930. s->ctx->cipher_list_tls11 != NULL) {
  931. return s->ctx->cipher_list_tls11->ciphers;
  932. }
  933. if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
  934. return s->ctx->cipher_list->ciphers;
  935. }
  936. return NULL;
  937. }
  938. /* return a STACK of the ciphers available for the SSL and in order of
  939. * algorithm id */
  940. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
  941. if (s == NULL) {
  942. return NULL;
  943. }
  944. if (s->cipher_list_by_id != NULL) {
  945. return s->cipher_list_by_id;
  946. }
  947. if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
  948. return s->ctx->cipher_list_by_id;
  949. }
  950. return NULL;
  951. }
  952. /* The old interface to get the same thing as SSL_get_ciphers() */
  953. const char *SSL_get_cipher_list(const SSL *s, int n) {
  954. const SSL_CIPHER *c;
  955. STACK_OF(SSL_CIPHER) *sk;
  956. if (s == NULL) {
  957. return NULL;
  958. }
  959. sk = SSL_get_ciphers(s);
  960. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  961. return NULL;
  962. }
  963. c = sk_SSL_CIPHER_value(sk, n);
  964. if (c == NULL) {
  965. return NULL;
  966. }
  967. return c->name;
  968. }
  969. /* specify the ciphers to be used by default by the SSL_CTX */
  970. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  971. STACK_OF(SSL_CIPHER) *sk;
  972. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
  973. &ctx->cipher_list_by_id, str);
  974. /* ssl_create_cipher_list may return an empty stack if it was unable to find
  975. * a cipher matching the given rule string (for example if the rule string
  976. * specifies a cipher which has been disabled). This is not an error as far
  977. * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
  978. * ctx->cipher_list_by_id has been updated. */
  979. if (sk == NULL) {
  980. return 0;
  981. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  982. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  983. return 0;
  984. }
  985. return 1;
  986. }
  987. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  988. STACK_OF(SSL_CIPHER) *sk;
  989. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
  990. if (sk == NULL) {
  991. return 0;
  992. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  993. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
  994. SSL_R_NO_CIPHER_MATCH);
  995. return 0;
  996. }
  997. return 1;
  998. }
  999. /* specify the ciphers to be used by the SSL */
  1000. int SSL_set_cipher_list(SSL *s, const char *str) {
  1001. STACK_OF(SSL_CIPHER) *sk;
  1002. sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
  1003. &s->cipher_list_by_id, str);
  1004. /* see comment in SSL_CTX_set_cipher_list */
  1005. if (sk == NULL) {
  1006. return 0;
  1007. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1008. OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  1009. return 0;
  1010. }
  1011. return 1;
  1012. }
  1013. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
  1014. size_t i;
  1015. const SSL_CIPHER *c;
  1016. CERT *ct = s->cert;
  1017. uint8_t *q;
  1018. /* Set disabled masks for this session */
  1019. ssl_set_client_disabled(s);
  1020. if (sk == NULL) {
  1021. return 0;
  1022. }
  1023. q = p;
  1024. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  1025. c = sk_SSL_CIPHER_value(sk, i);
  1026. /* Skip disabled ciphers */
  1027. if (c->algorithm_ssl & ct->mask_ssl ||
  1028. c->algorithm_mkey & ct->mask_k ||
  1029. c->algorithm_auth & ct->mask_a) {
  1030. continue;
  1031. }
  1032. s2n(ssl3_get_cipher_value(c), p);
  1033. }
  1034. /* If all ciphers were disabled, return the error to the caller. */
  1035. if (p == q) {
  1036. return 0;
  1037. }
  1038. /* Add SCSVs. */
  1039. if (!s->s3->initial_handshake_complete) {
  1040. s2n(SSL3_CK_SCSV & 0xffff, p);
  1041. }
  1042. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  1043. s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
  1044. }
  1045. return p - q;
  1046. }
  1047. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
  1048. CBS cipher_suites = *cbs;
  1049. const SSL_CIPHER *c;
  1050. STACK_OF(SSL_CIPHER) *sk;
  1051. if (s->s3) {
  1052. s->s3->send_connection_binding = 0;
  1053. }
  1054. if (CBS_len(&cipher_suites) % 2 != 0) {
  1055. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1056. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1057. return NULL;
  1058. }
  1059. sk = sk_SSL_CIPHER_new_null();
  1060. if (sk == NULL) {
  1061. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1062. goto err;
  1063. }
  1064. while (CBS_len(&cipher_suites) > 0) {
  1065. uint16_t cipher_suite;
  1066. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1067. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
  1068. goto err;
  1069. }
  1070. /* Check for SCSV. */
  1071. if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1072. /* SCSV is fatal if renegotiating. */
  1073. if (s->s3->initial_handshake_complete) {
  1074. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1075. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1076. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1077. goto err;
  1078. }
  1079. s->s3->send_connection_binding = 1;
  1080. continue;
  1081. }
  1082. /* Check for FALLBACK_SCSV. */
  1083. if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1084. uint16_t max_version = ssl3_get_max_server_version(s);
  1085. if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
  1086. : (uint16_t)s->version < max_version) {
  1087. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1088. SSL_R_INAPPROPRIATE_FALLBACK);
  1089. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1090. goto err;
  1091. }
  1092. continue;
  1093. }
  1094. c = ssl3_get_cipher_by_value(cipher_suite);
  1095. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1096. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1097. goto err;
  1098. }
  1099. }
  1100. return sk;
  1101. err:
  1102. sk_SSL_CIPHER_free(sk);
  1103. return NULL;
  1104. }
  1105. /* return a servername extension value if provided in Client Hello, or NULL. So
  1106. * far, only host_name types are defined (RFC 3546). */
  1107. const char *SSL_get_servername(const SSL *s, const int type) {
  1108. if (type != TLSEXT_NAMETYPE_host_name) {
  1109. return NULL;
  1110. }
  1111. return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
  1112. : s->tlsext_hostname;
  1113. }
  1114. int SSL_get_servername_type(const SSL *s) {
  1115. if (s->session &&
  1116. (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
  1117. return TLSEXT_NAMETYPE_host_name;
  1118. }
  1119. return -1;
  1120. }
  1121. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1122. ctx->signed_cert_timestamps_enabled = 1;
  1123. }
  1124. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1125. ssl->signed_cert_timestamps_enabled = 1;
  1126. return 1;
  1127. }
  1128. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1129. ctx->ocsp_stapling_enabled = 1;
  1130. }
  1131. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1132. ssl->ocsp_stapling_enabled = 1;
  1133. return 1;
  1134. }
  1135. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1136. size_t *out_len) {
  1137. SSL_SESSION *session = ssl->session;
  1138. *out_len = 0;
  1139. *out = NULL;
  1140. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1141. return;
  1142. }
  1143. *out = session->tlsext_signed_cert_timestamp_list;
  1144. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1145. }
  1146. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1147. size_t *out_len) {
  1148. SSL_SESSION *session = ssl->session;
  1149. *out_len = 0;
  1150. *out = NULL;
  1151. if (ssl->server || !session || !session->ocsp_response) {
  1152. return;
  1153. }
  1154. *out = session->ocsp_response;
  1155. *out_len = session->ocsp_response_length;
  1156. }
  1157. /* SSL_select_next_proto implements the standard protocol selection. It is
  1158. * expected that this function is called from the callback set by
  1159. * SSL_CTX_set_next_proto_select_cb.
  1160. *
  1161. * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
  1162. * strings. The length byte itself is not included in the length. A byte
  1163. * string of length 0 is invalid. No byte string may be truncated.
  1164. *
  1165. * The current, but experimental algorithm for selecting the protocol is:
  1166. *
  1167. * 1) If the server doesn't support NPN then this is indicated to the
  1168. * callback. In this case, the client application has to abort the connection
  1169. * or have a default application level protocol.
  1170. *
  1171. * 2) If the server supports NPN, but advertises an empty list then the
  1172. * client selects the first protcol in its list, but indicates via the
  1173. * API that this fallback case was enacted.
  1174. *
  1175. * 3) Otherwise, the client finds the first protocol in the server's list
  1176. * that it supports and selects this protocol. This is because it's
  1177. * assumed that the server has better information about which protocol
  1178. * a client should use.
  1179. *
  1180. * 4) If the client doesn't support any of the server's advertised
  1181. * protocols, then this is treated the same as case 2.
  1182. *
  1183. * It returns either
  1184. * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
  1185. * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  1186. */
  1187. int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
  1188. unsigned int server_len, const uint8_t *client,
  1189. unsigned int client_len) {
  1190. unsigned int i, j;
  1191. const uint8_t *result;
  1192. int status = OPENSSL_NPN_UNSUPPORTED;
  1193. /* For each protocol in server preference order, see if we support it. */
  1194. for (i = 0; i < server_len;) {
  1195. for (j = 0; j < client_len;) {
  1196. if (server[i] == client[j] &&
  1197. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1198. /* We found a match */
  1199. result = &server[i];
  1200. status = OPENSSL_NPN_NEGOTIATED;
  1201. goto found;
  1202. }
  1203. j += client[j];
  1204. j++;
  1205. }
  1206. i += server[i];
  1207. i++;
  1208. }
  1209. /* There's no overlap between our protocols and the server's list. */
  1210. result = client;
  1211. status = OPENSSL_NPN_NO_OVERLAP;
  1212. found:
  1213. *out = (uint8_t *)result + 1;
  1214. *outlen = result[0];
  1215. return status;
  1216. }
  1217. /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
  1218. * requested protocol for this connection and returns 0. If the client didn't
  1219. * request any protocol, then *data is set to NULL.
  1220. *
  1221. * Note that the client can request any protocol it chooses. The value returned
  1222. * from this function need not be a member of the list of supported protocols
  1223. * provided by the callback. */
  1224. void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
  1225. unsigned *len) {
  1226. *data = s->next_proto_negotiated;
  1227. if (!*data) {
  1228. *len = 0;
  1229. } else {
  1230. *len = s->next_proto_negotiated_len;
  1231. }
  1232. }
  1233. /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
  1234. * TLS server needs a list of supported protocols for Next Protocol
  1235. * Negotiation. The returned list must be in wire format. The list is returned
  1236. * by setting |out| to point to it and |outlen| to its length. This memory will
  1237. * not be modified, but one should assume that the SSL* keeps a reference to
  1238. * it.
  1239. *
  1240. * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
  1241. * Otherwise, no such extension will be included in the ServerHello. */
  1242. void SSL_CTX_set_next_protos_advertised_cb(
  1243. SSL_CTX *ctx,
  1244. int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
  1245. void *arg) {
  1246. ctx->next_protos_advertised_cb = cb;
  1247. ctx->next_protos_advertised_cb_arg = arg;
  1248. }
  1249. /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  1250. * client needs to select a protocol from the server's provided list. |out|
  1251. * must be set to point to the selected protocol (which may be within |in|).
  1252. * The length of the protocol name must be written into |outlen|. The server's
  1253. * advertised protocols are provided in |in| and |inlen|. The callback can
  1254. * assume that |in| is syntactically valid.
  1255. *
  1256. * The client must select a protocol. It is fatal to the connection if this
  1257. * callback returns a value other than SSL_TLSEXT_ERR_OK.
  1258. */
  1259. void SSL_CTX_set_next_proto_select_cb(
  1260. SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
  1261. const uint8_t *in, unsigned int inlen, void *arg),
  1262. void *arg) {
  1263. ctx->next_proto_select_cb = cb;
  1264. ctx->next_proto_select_cb_arg = arg;
  1265. }
  1266. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1267. unsigned protos_len) {
  1268. OPENSSL_free(ctx->alpn_client_proto_list);
  1269. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1270. if (!ctx->alpn_client_proto_list) {
  1271. return 1;
  1272. }
  1273. ctx->alpn_client_proto_list_len = protos_len;
  1274. return 0;
  1275. }
  1276. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1277. OPENSSL_free(ssl->alpn_client_proto_list);
  1278. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1279. if (!ssl->alpn_client_proto_list) {
  1280. return 1;
  1281. }
  1282. ssl->alpn_client_proto_list_len = protos_len;
  1283. return 0;
  1284. }
  1285. /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
  1286. * during ClientHello processing in order to select an ALPN protocol from the
  1287. * client's list of offered protocols. */
  1288. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1289. int (*cb)(SSL *ssl, const uint8_t **out,
  1290. uint8_t *outlen, const uint8_t *in,
  1291. unsigned int inlen, void *arg),
  1292. void *arg) {
  1293. ctx->alpn_select_cb = cb;
  1294. ctx->alpn_select_cb_arg = arg;
  1295. }
  1296. /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  1297. * On return it sets |*data| to point to |*len| bytes of protocol name (not
  1298. * including the leading length-prefix byte). If the server didn't respond with
  1299. * a negotiated protocol then |*len| will be zero. */
  1300. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
  1301. unsigned *len) {
  1302. *data = NULL;
  1303. if (ssl->s3) {
  1304. *data = ssl->s3->alpn_selected;
  1305. }
  1306. if (*data == NULL) {
  1307. *len = 0;
  1308. } else {
  1309. *len = ssl->s3->alpn_selected_len;
  1310. }
  1311. }
  1312. int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  1313. const char *label, size_t label_len,
  1314. const uint8_t *context, size_t context_len,
  1315. int use_context) {
  1316. if (s->version < TLS1_VERSION) {
  1317. return 0;
  1318. }
  1319. return s->enc_method->export_keying_material(
  1320. s, out, out_len, label, label_len, context, context_len, use_context);
  1321. }
  1322. static uint32_t ssl_session_hash(const SSL_SESSION *a) {
  1323. uint32_t hash =
  1324. ((uint32_t)a->session_id[0]) ||
  1325. ((uint32_t)a->session_id[1] << 8) ||
  1326. ((uint32_t)a->session_id[2] << 16) ||
  1327. ((uint32_t)a->session_id[3] << 24);
  1328. return hash;
  1329. }
  1330. /* NB: If this function (or indeed the hash function which uses a sort of
  1331. * coarser function than this one) is changed, ensure
  1332. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  1333. * able to construct an SSL_SESSION that will collide with any existing session
  1334. * with a matching session ID. */
  1335. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  1336. if (a->ssl_version != b->ssl_version) {
  1337. return 1;
  1338. }
  1339. if (a->session_id_length != b->session_id_length) {
  1340. return 1;
  1341. }
  1342. return memcmp(a->session_id, b->session_id, a->session_id_length);
  1343. }
  1344. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  1345. SSL_CTX *ret = NULL;
  1346. if (method == NULL) {
  1347. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
  1348. return NULL;
  1349. }
  1350. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  1351. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1352. goto err;
  1353. }
  1354. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  1355. if (ret == NULL) {
  1356. goto err;
  1357. }
  1358. memset(ret, 0, sizeof(SSL_CTX));
  1359. ret->method = method->method;
  1360. CRYPTO_MUTEX_init(&ret->lock);
  1361. ret->cert_store = NULL;
  1362. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  1363. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1364. ret->session_cache_head = NULL;
  1365. ret->session_cache_tail = NULL;
  1366. /* We take the system default */
  1367. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  1368. ret->new_session_cb = 0;
  1369. ret->remove_session_cb = 0;
  1370. ret->get_session_cb = 0;
  1371. ret->generate_session_id = 0;
  1372. ret->references = 1;
  1373. ret->quiet_shutdown = 0;
  1374. ret->info_callback = NULL;
  1375. ret->app_verify_callback = 0;
  1376. ret->app_verify_arg = NULL;
  1377. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  1378. ret->msg_callback = 0;
  1379. ret->msg_callback_arg = NULL;
  1380. ret->verify_mode = SSL_VERIFY_NONE;
  1381. ret->sid_ctx_length = 0;
  1382. ret->default_verify_callback = NULL;
  1383. ret->cert = ssl_cert_new();
  1384. if (ret->cert == NULL) {
  1385. goto err;
  1386. }
  1387. ret->default_passwd_callback = 0;
  1388. ret->default_passwd_callback_userdata = NULL;
  1389. ret->client_cert_cb = 0;
  1390. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  1391. if (ret->sessions == NULL) {
  1392. goto err;
  1393. }
  1394. ret->cert_store = X509_STORE_new();
  1395. if (ret->cert_store == NULL) {
  1396. goto err;
  1397. }
  1398. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  1399. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  1400. if (ret->cipher_list == NULL ||
  1401. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  1402. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1403. goto err2;
  1404. }
  1405. ret->param = X509_VERIFY_PARAM_new();
  1406. if (!ret->param) {
  1407. goto err;
  1408. }
  1409. ret->client_CA = sk_X509_NAME_new_null();
  1410. if (ret->client_CA == NULL) {
  1411. goto err;
  1412. }
  1413. CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
  1414. ret->extra_certs = NULL;
  1415. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1416. ret->tlsext_servername_callback = 0;
  1417. ret->tlsext_servername_arg = NULL;
  1418. /* Setup RFC4507 ticket keys */
  1419. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  1420. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  1421. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  1422. ret->options |= SSL_OP_NO_TICKET;
  1423. }
  1424. ret->next_protos_advertised_cb = 0;
  1425. ret->next_proto_select_cb = 0;
  1426. ret->psk_identity_hint = NULL;
  1427. ret->psk_client_callback = NULL;
  1428. ret->psk_server_callback = NULL;
  1429. /* Default is to connect to non-RI servers. When RI is more widely deployed
  1430. * might change this. */
  1431. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  1432. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  1433. * uses of SSL_METHOD. */
  1434. if (method->version != 0) {
  1435. SSL_CTX_set_max_version(ret, method->version);
  1436. SSL_CTX_set_min_version(ret, method->version);
  1437. }
  1438. return ret;
  1439. err:
  1440. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
  1441. err2:
  1442. SSL_CTX_free(ret);
  1443. return NULL;
  1444. }
  1445. void SSL_CTX_free(SSL_CTX *ctx) {
  1446. if (ctx == NULL ||
  1447. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  1448. return;
  1449. }
  1450. X509_VERIFY_PARAM_free(ctx->param);
  1451. /* Free internal session cache. However: the remove_cb() may reference the
  1452. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  1453. * sessions were flushed. As the ex_data handling routines might also touch
  1454. * the session cache, the most secure solution seems to be: empty (flush) the
  1455. * cache, then free ex_data, then finally free the cache. (See ticket
  1456. * [openssl.org #212].) */
  1457. SSL_CTX_flush_sessions(ctx, 0);
  1458. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  1459. CRYPTO_MUTEX_cleanup(&ctx->lock);
  1460. lh_SSL_SESSION_free(ctx->sessions);
  1461. X509_STORE_free(ctx->cert_store);
  1462. ssl_cipher_preference_list_free(ctx->cipher_list);
  1463. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  1464. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  1465. ssl_cert_free(ctx->cert);
  1466. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  1467. sk_X509_pop_free(ctx->extra_certs, X509_free);
  1468. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  1469. OPENSSL_free(ctx->psk_identity_hint);
  1470. OPENSSL_free(ctx->tlsext_ecpointformatlist);
  1471. OPENSSL_free(ctx->tlsext_ellipticcurvelist);
  1472. OPENSSL_free(ctx->alpn_client_proto_list);
  1473. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1474. BIO_free(ctx->keylog_bio);
  1475. OPENSSL_free(ctx);
  1476. }
  1477. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
  1478. ctx->default_passwd_callback = cb;
  1479. }
  1480. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
  1481. ctx->default_passwd_callback_userdata = u;
  1482. }
  1483. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1484. int (*cb)(X509_STORE_CTX *, void *),
  1485. void *arg) {
  1486. ctx->app_verify_callback = cb;
  1487. ctx->app_verify_arg = arg;
  1488. }
  1489. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1490. int (*cb)(int, X509_STORE_CTX *)) {
  1491. ctx->verify_mode = mode;
  1492. ctx->default_verify_callback = cb;
  1493. }
  1494. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1495. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1496. }
  1497. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
  1498. void *arg) {
  1499. ssl_cert_set_cert_cb(c->cert, cb, arg);
  1500. }
  1501. void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1502. ssl_cert_set_cert_cb(s->cert, cb, arg);
  1503. }
  1504. static int ssl_has_key(SSL *s, size_t idx) {
  1505. CERT_PKEY *cpk = &s->cert->pkeys[idx];
  1506. return cpk->x509 && cpk->privatekey;
  1507. }
  1508. void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
  1509. uint32_t *out_mask_a) {
  1510. CERT *c = s->cert;
  1511. int rsa_enc, rsa_sign, dh_tmp;
  1512. uint32_t mask_k, mask_a;
  1513. int have_ecc_cert, ecdsa_ok;
  1514. X509 *x;
  1515. if (c == NULL) {
  1516. /* TODO(davidben): Is this codepath possible? */
  1517. *out_mask_k = 0;
  1518. *out_mask_a = 0;
  1519. return;
  1520. }
  1521. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1522. rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
  1523. rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
  1524. have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
  1525. mask_k = 0;
  1526. mask_a = 0;
  1527. if (rsa_enc) {
  1528. mask_k |= SSL_kRSA;
  1529. }
  1530. if (dh_tmp) {
  1531. mask_k |= SSL_kDHE;
  1532. }
  1533. if (rsa_enc || rsa_sign) {
  1534. mask_a |= SSL_aRSA;
  1535. }
  1536. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1537. * key usage extension and on the client's curve preferences. */
  1538. if (have_ecc_cert) {
  1539. x = c->pkeys[SSL_PKEY_ECC].x509;
  1540. /* This call populates extension flags (ex_flags). */
  1541. X509_check_purpose(x, -1, 0);
  1542. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1543. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1544. : 1;
  1545. if (!tls1_check_ec_cert(s, x)) {
  1546. ecdsa_ok = 0;
  1547. }
  1548. if (ecdsa_ok) {
  1549. mask_a |= SSL_aECDSA;
  1550. }
  1551. }
  1552. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1553. * key, check it. */
  1554. if (tls1_check_ec_tmp_key(s)) {
  1555. mask_k |= SSL_kECDHE;
  1556. }
  1557. /* PSK requires a server callback. */
  1558. if (s->psk_server_callback != NULL) {
  1559. mask_k |= SSL_kPSK;
  1560. mask_a |= SSL_aPSK;
  1561. }
  1562. *out_mask_k = mask_k;
  1563. *out_mask_a = mask_a;
  1564. }
  1565. /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
  1566. #define ku_reject(x, usage) \
  1567. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  1568. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
  1569. const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
  1570. uint32_t alg_a = cs->algorithm_auth;
  1571. int signature_nid = 0, md_nid = 0, pk_nid = 0;
  1572. /* This call populates the ex_flags field correctly */
  1573. X509_check_purpose(x, -1, 0);
  1574. if (x->sig_alg && x->sig_alg->algorithm) {
  1575. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1576. OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
  1577. }
  1578. if (alg_a & SSL_aECDSA) {
  1579. /* key usage, if present, must allow signing */
  1580. if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
  1581. OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
  1582. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  1583. return 0;
  1584. }
  1585. }
  1586. return 1; /* all checks are ok */
  1587. }
  1588. static int ssl_get_server_cert_index(const SSL *s) {
  1589. int idx;
  1590. idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1591. if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
  1592. idx = SSL_PKEY_RSA_SIGN;
  1593. }
  1594. if (idx == -1) {
  1595. OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
  1596. }
  1597. return idx;
  1598. }
  1599. CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
  1600. int i = ssl_get_server_cert_index(s);
  1601. /* This may or may not be an error. */
  1602. if (i < 0) {
  1603. return NULL;
  1604. }
  1605. /* May be NULL. */
  1606. return &s->cert->pkeys[i];
  1607. }
  1608. EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
  1609. uint32_t alg_a = cipher->algorithm_auth;
  1610. CERT *c = s->cert;
  1611. int idx = -1;
  1612. if (alg_a & SSL_aRSA) {
  1613. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
  1614. idx = SSL_PKEY_RSA_SIGN;
  1615. } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
  1616. idx = SSL_PKEY_RSA_ENC;
  1617. }
  1618. } else if ((alg_a & SSL_aECDSA) &&
  1619. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
  1620. idx = SSL_PKEY_ECC;
  1621. }
  1622. if (idx == -1) {
  1623. OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
  1624. return NULL;
  1625. }
  1626. return c->pkeys[idx].privatekey;
  1627. }
  1628. void ssl_update_cache(SSL *s, int mode) {
  1629. /* Never cache sessions with empty session IDs. */
  1630. if (s->session->session_id_length == 0) {
  1631. return;
  1632. }
  1633. SSL_CTX *ctx = s->initial_ctx;
  1634. if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
  1635. ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
  1636. SSL_CTX_add_session(ctx, s->session)) &&
  1637. ctx->new_session_cb != NULL) {
  1638. /* Note: |new_session_cb| is called whether the internal session cache is
  1639. * used or not. */
  1640. if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
  1641. SSL_SESSION_free(s->session);
  1642. }
  1643. }
  1644. if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
  1645. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
  1646. (ctx->session_cache_mode & mode) == mode) {
  1647. /* Automatically flush the internal session cache every 255 connections. */
  1648. int flush_cache = 0;
  1649. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1650. ctx->handshakes_since_cache_flush++;
  1651. if (ctx->handshakes_since_cache_flush >= 255) {
  1652. flush_cache = 1;
  1653. ctx->handshakes_since_cache_flush = 0;
  1654. }
  1655. CRYPTO_MUTEX_unlock(&ctx->lock);
  1656. if (flush_cache) {
  1657. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1658. }
  1659. }
  1660. }
  1661. int SSL_get_error(const SSL *s, int ret_code) {
  1662. int reason;
  1663. uint32_t err;
  1664. BIO *bio;
  1665. if (ret_code > 0) {
  1666. return SSL_ERROR_NONE;
  1667. }
  1668. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1669. * where we do encode the error */
  1670. err = ERR_peek_error();
  1671. if (err != 0) {
  1672. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1673. return SSL_ERROR_SYSCALL;
  1674. }
  1675. return SSL_ERROR_SSL;
  1676. }
  1677. if (ret_code == 0) {
  1678. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  1679. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
  1680. /* The socket was cleanly shut down with a close_notify. */
  1681. return SSL_ERROR_ZERO_RETURN;
  1682. }
  1683. /* An EOF was observed which violates the protocol, and the underlying
  1684. * transport does not participate in the error queue. Bubble up to the
  1685. * caller. */
  1686. return SSL_ERROR_SYSCALL;
  1687. }
  1688. if (SSL_want_session(s)) {
  1689. return SSL_ERROR_PENDING_SESSION;
  1690. }
  1691. if (SSL_want_certificate(s)) {
  1692. return SSL_ERROR_PENDING_CERTIFICATE;
  1693. }
  1694. if (SSL_want_read(s)) {
  1695. bio = SSL_get_rbio(s);
  1696. if (BIO_should_read(bio)) {
  1697. return SSL_ERROR_WANT_READ;
  1698. }
  1699. if (BIO_should_write(bio)) {
  1700. /* This one doesn't make too much sense ... We never try to write to the
  1701. * rbio, and an application program where rbio and wbio are separate
  1702. * couldn't even know what it should wait for. However if we ever set
  1703. * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
  1704. * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
  1705. * around that bug; so it might be safer to keep it. */
  1706. return SSL_ERROR_WANT_WRITE;
  1707. }
  1708. if (BIO_should_io_special(bio)) {
  1709. reason = BIO_get_retry_reason(bio);
  1710. if (reason == BIO_RR_CONNECT) {
  1711. return SSL_ERROR_WANT_CONNECT;
  1712. }
  1713. if (reason == BIO_RR_ACCEPT) {
  1714. return SSL_ERROR_WANT_ACCEPT;
  1715. }
  1716. return SSL_ERROR_SYSCALL; /* unknown */
  1717. }
  1718. }
  1719. if (SSL_want_write(s)) {
  1720. bio = SSL_get_wbio(s);
  1721. if (BIO_should_write(bio)) {
  1722. return SSL_ERROR_WANT_WRITE;
  1723. }
  1724. if (BIO_should_read(bio)) {
  1725. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  1726. return SSL_ERROR_WANT_READ;
  1727. }
  1728. if (BIO_should_io_special(bio)) {
  1729. reason = BIO_get_retry_reason(bio);
  1730. if (reason == BIO_RR_CONNECT) {
  1731. return SSL_ERROR_WANT_CONNECT;
  1732. }
  1733. if (reason == BIO_RR_ACCEPT) {
  1734. return SSL_ERROR_WANT_ACCEPT;
  1735. }
  1736. return SSL_ERROR_SYSCALL;
  1737. }
  1738. }
  1739. if (SSL_want_x509_lookup(s)) {
  1740. return SSL_ERROR_WANT_X509_LOOKUP;
  1741. }
  1742. if (SSL_want_channel_id_lookup(s)) {
  1743. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1744. }
  1745. return SSL_ERROR_SYSCALL;
  1746. }
  1747. int SSL_do_handshake(SSL *s) {
  1748. int ret = 1;
  1749. if (s->handshake_func == NULL) {
  1750. OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
  1751. return -1;
  1752. }
  1753. s->method->ssl_renegotiate_check(s);
  1754. if (SSL_in_init(s)) {
  1755. ret = s->handshake_func(s);
  1756. }
  1757. return ret;
  1758. }
  1759. void SSL_set_accept_state(SSL *ssl) {
  1760. ssl->server = 1;
  1761. ssl->shutdown = 0;
  1762. ssl->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
  1763. ssl->handshake_func = ssl->method->ssl_accept;
  1764. /* clear the current cipher */
  1765. ssl_clear_cipher_ctx(ssl);
  1766. }
  1767. void SSL_set_connect_state(SSL *ssl) {
  1768. ssl->server = 0;
  1769. ssl->shutdown = 0;
  1770. ssl->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
  1771. ssl->handshake_func = ssl->method->ssl_connect;
  1772. /* clear the current cipher */
  1773. ssl_clear_cipher_ctx(ssl);
  1774. }
  1775. static const char *ssl_get_version(int version) {
  1776. switch (version) {
  1777. case TLS1_2_VERSION:
  1778. return "TLSv1.2";
  1779. case TLS1_1_VERSION:
  1780. return "TLSv1.1";
  1781. case TLS1_VERSION:
  1782. return "TLSv1";
  1783. case SSL3_VERSION:
  1784. return "SSLv3";
  1785. case DTLS1_VERSION:
  1786. return "DTLSv1";
  1787. case DTLS1_2_VERSION:
  1788. return "DTLSv1.2";
  1789. default:
  1790. return "unknown";
  1791. }
  1792. }
  1793. const char *SSL_get_version(const SSL *s) {
  1794. return ssl_get_version(s->version);
  1795. }
  1796. const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
  1797. return ssl_get_version(sess->ssl_version);
  1798. }
  1799. void ssl_clear_cipher_ctx(SSL *s) {
  1800. SSL_AEAD_CTX_free(s->aead_read_ctx);
  1801. s->aead_read_ctx = NULL;
  1802. SSL_AEAD_CTX_free(s->aead_write_ctx);
  1803. s->aead_write_ctx = NULL;
  1804. }
  1805. X509 *SSL_get_certificate(const SSL *s) {
  1806. if (s->cert != NULL) {
  1807. return s->cert->key->x509;
  1808. }
  1809. return NULL;
  1810. }
  1811. EVP_PKEY *SSL_get_privatekey(const SSL *s) {
  1812. if (s->cert != NULL) {
  1813. return s->cert->key->privatekey;
  1814. }
  1815. return NULL;
  1816. }
  1817. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1818. if (ctx->cert != NULL) {
  1819. return ctx->cert->key->x509;
  1820. }
  1821. return NULL;
  1822. }
  1823. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1824. if (ctx->cert != NULL) {
  1825. return ctx->cert->key->privatekey;
  1826. }
  1827. return NULL;
  1828. }
  1829. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
  1830. if (s->aead_write_ctx == NULL) {
  1831. return NULL;
  1832. }
  1833. return s->aead_write_ctx->cipher;
  1834. }
  1835. const void *SSL_get_current_compression(SSL *s) { return NULL; }
  1836. const void *SSL_get_current_expansion(SSL *s) { return NULL; }
  1837. int ssl_init_wbio_buffer(SSL *s, int push) {
  1838. BIO *bbio;
  1839. if (s->bbio == NULL) {
  1840. bbio = BIO_new(BIO_f_buffer());
  1841. if (bbio == NULL) {
  1842. return 0;
  1843. }
  1844. s->bbio = bbio;
  1845. } else {
  1846. bbio = s->bbio;
  1847. if (s->bbio == s->wbio) {
  1848. s->wbio = BIO_pop(s->wbio);
  1849. }
  1850. }
  1851. BIO_reset(bbio);
  1852. if (!BIO_set_read_buffer_size(bbio, 1)) {
  1853. OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
  1854. return 0;
  1855. }
  1856. if (push) {
  1857. if (s->wbio != bbio) {
  1858. s->wbio = BIO_push(bbio, s->wbio);
  1859. }
  1860. } else {
  1861. if (s->wbio == bbio) {
  1862. s->wbio = BIO_pop(bbio);
  1863. }
  1864. }
  1865. return 1;
  1866. }
  1867. void ssl_free_wbio_buffer(SSL *s) {
  1868. if (s->bbio == NULL) {
  1869. return;
  1870. }
  1871. if (s->bbio == s->wbio) {
  1872. /* remove buffering */
  1873. s->wbio = BIO_pop(s->wbio);
  1874. }
  1875. BIO_free(s->bbio);
  1876. s->bbio = NULL;
  1877. }
  1878. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1879. ctx->quiet_shutdown = mode;
  1880. }
  1881. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1882. return ctx->quiet_shutdown;
  1883. }
  1884. void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
  1885. int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
  1886. void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
  1887. int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
  1888. int SSL_version(const SSL *s) { return s->version; }
  1889. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1890. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1891. if (ssl->ctx == ctx) {
  1892. return ssl->ctx;
  1893. }
  1894. if (ctx == NULL) {
  1895. ctx = ssl->initial_ctx;
  1896. }
  1897. ssl_cert_free(ssl->cert);
  1898. ssl->cert = ssl_cert_dup(ctx->cert);
  1899. CRYPTO_refcount_inc(&ctx->references);
  1900. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1901. ssl->ctx = ctx;
  1902. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1903. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1904. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1905. return ssl->ctx;
  1906. }
  1907. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1908. return X509_STORE_set_default_paths(ctx->cert_store);
  1909. }
  1910. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  1911. const char *CApath) {
  1912. return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
  1913. }
  1914. void SSL_set_info_callback(SSL *ssl,
  1915. void (*cb)(const SSL *ssl, int type, int val)) {
  1916. ssl->info_callback = cb;
  1917. }
  1918. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
  1919. int /*val*/) {
  1920. return ssl->info_callback;
  1921. }
  1922. int SSL_state(const SSL *ssl) { return ssl->state; }
  1923. void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
  1924. void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
  1925. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  1926. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1927. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1928. int index;
  1929. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1930. new_func, dup_func, free_func)) {
  1931. return -1;
  1932. }
  1933. return index;
  1934. }
  1935. int SSL_set_ex_data(SSL *s, int idx, void *arg) {
  1936. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  1937. }
  1938. void *SSL_get_ex_data(const SSL *s, int idx) {
  1939. return CRYPTO_get_ex_data(&s->ex_data, idx);
  1940. }
  1941. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1942. CRYPTO_EX_dup *dup_func,
  1943. CRYPTO_EX_free *free_func) {
  1944. int index;
  1945. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1946. new_func, dup_func, free_func)) {
  1947. return -1;
  1948. }
  1949. return index;
  1950. }
  1951. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
  1952. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  1953. }
  1954. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
  1955. return CRYPTO_get_ex_data(&s->ex_data, idx);
  1956. }
  1957. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1958. return ctx->cert_store;
  1959. }
  1960. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1961. X509_STORE_free(ctx->cert_store);
  1962. ctx->cert_store = store;
  1963. }
  1964. int SSL_want(const SSL *s) { return s->rwstate; }
  1965. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1966. RSA *(*cb)(SSL *ssl, int is_export,
  1967. int keylength)) {
  1968. }
  1969. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1970. int keylength)) {
  1971. }
  1972. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1973. DH *(*callback)(SSL *ssl, int is_export,
  1974. int keylength)) {
  1975. ctx->cert->dh_tmp_cb = callback;
  1976. }
  1977. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1978. int keylength)) {
  1979. ssl->cert->dh_tmp_cb = callback;
  1980. }
  1981. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
  1982. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1983. int keylength)) {
  1984. ctx->cert->ecdh_tmp_cb = callback;
  1985. }
  1986. void SSL_set_tmp_ecdh_callback(SSL *ssl,
  1987. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1988. int keylength)) {
  1989. ssl->cert->ecdh_tmp_cb = callback;
  1990. }
  1991. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1992. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1993. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
  1994. SSL_R_DATA_LENGTH_TOO_LONG);
  1995. return 0;
  1996. }
  1997. OPENSSL_free(ctx->psk_identity_hint);
  1998. if (identity_hint != NULL) {
  1999. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  2000. if (ctx->psk_identity_hint == NULL) {
  2001. return 0;
  2002. }
  2003. } else {
  2004. ctx->psk_identity_hint = NULL;
  2005. }
  2006. return 1;
  2007. }
  2008. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
  2009. if (s == NULL) {
  2010. return 0;
  2011. }
  2012. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2013. OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
  2014. SSL_R_DATA_LENGTH_TOO_LONG);
  2015. return 0;
  2016. }
  2017. /* Clear currently configured hint, if any. */
  2018. OPENSSL_free(s->psk_identity_hint);
  2019. s->psk_identity_hint = NULL;
  2020. if (identity_hint != NULL) {
  2021. s->psk_identity_hint = BUF_strdup(identity_hint);
  2022. if (s->psk_identity_hint == NULL) {
  2023. return 0;
  2024. }
  2025. }
  2026. return 1;
  2027. }
  2028. const char *SSL_get_psk_identity_hint(const SSL *s) {
  2029. if (s == NULL) {
  2030. return NULL;
  2031. }
  2032. return s->psk_identity_hint;
  2033. }
  2034. const char *SSL_get_psk_identity(const SSL *s) {
  2035. if (s == NULL || s->session == NULL) {
  2036. return NULL;
  2037. }
  2038. return s->session->psk_identity;
  2039. }
  2040. void SSL_set_psk_client_callback(
  2041. SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2042. unsigned int max_identity_len, uint8_t *psk,
  2043. unsigned int max_psk_len)) {
  2044. s->psk_client_callback = cb;
  2045. }
  2046. void SSL_CTX_set_psk_client_callback(
  2047. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2048. unsigned int max_identity_len,
  2049. uint8_t *psk, unsigned int max_psk_len)) {
  2050. ctx->psk_client_callback = cb;
  2051. }
  2052. void SSL_set_psk_server_callback(
  2053. SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2054. unsigned int max_psk_len)) {
  2055. s->psk_server_callback = cb;
  2056. }
  2057. void SSL_CTX_set_psk_server_callback(
  2058. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
  2059. uint8_t *psk, unsigned int max_psk_len)) {
  2060. ctx->psk_server_callback = cb;
  2061. }
  2062. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2063. ctx->min_version = version;
  2064. }
  2065. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2066. ctx->max_version = version;
  2067. }
  2068. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  2069. ssl->min_version = version;
  2070. }
  2071. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  2072. ssl->max_version = version;
  2073. }
  2074. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2075. void (*cb)(int write_p, int version,
  2076. int content_type, const void *buf,
  2077. size_t len, SSL *ssl, void *arg)) {
  2078. ctx->msg_callback = cb;
  2079. }
  2080. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2081. ctx->msg_callback_arg = arg;
  2082. }
  2083. void SSL_set_msg_callback(SSL *ssl,
  2084. void (*cb)(int write_p, int version, int content_type,
  2085. const void *buf, size_t len, SSL *ssl,
  2086. void *arg)) {
  2087. ssl->msg_callback = cb;
  2088. }
  2089. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2090. ssl->msg_callback_arg = arg;
  2091. }
  2092. void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
  2093. BIO_free(ctx->keylog_bio);
  2094. ctx->keylog_bio = keylog_bio;
  2095. }
  2096. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  2097. static const char hextable[] = "0123456789abcdef";
  2098. uint8_t *out;
  2099. size_t i;
  2100. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  2101. return 0;
  2102. }
  2103. for (i = 0; i < in_len; i++) {
  2104. *(out++) = (uint8_t)hextable[in[i] >> 4];
  2105. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  2106. }
  2107. return 1;
  2108. }
  2109. int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
  2110. const uint8_t *encrypted_premaster,
  2111. size_t encrypted_premaster_len,
  2112. const uint8_t *premaster,
  2113. size_t premaster_len) {
  2114. BIO *bio = ctx->keylog_bio;
  2115. CBB cbb;
  2116. uint8_t *out;
  2117. size_t out_len;
  2118. int ret;
  2119. if (bio == NULL) {
  2120. return 1;
  2121. }
  2122. if (encrypted_premaster_len < 8) {
  2123. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
  2124. ERR_R_INTERNAL_ERROR);
  2125. return 0;
  2126. }
  2127. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
  2128. return 0;
  2129. }
  2130. if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  2131. /* Only the first 8 bytes of the encrypted premaster secret are
  2132. * logged. */
  2133. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  2134. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2135. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  2136. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2137. !CBB_finish(&cbb, &out, &out_len)) {
  2138. CBB_cleanup(&cbb);
  2139. return 0;
  2140. }
  2141. CRYPTO_MUTEX_lock_write(&ctx->lock);
  2142. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2143. CRYPTO_MUTEX_unlock(&ctx->lock);
  2144. OPENSSL_free(out);
  2145. return ret;
  2146. }
  2147. int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
  2148. size_t client_random_len, const uint8_t *master,
  2149. size_t master_len) {
  2150. BIO *bio = ctx->keylog_bio;
  2151. CBB cbb;
  2152. uint8_t *out;
  2153. size_t out_len;
  2154. int ret;
  2155. if (bio == NULL) {
  2156. return 1;
  2157. }
  2158. if (client_random_len != 32) {
  2159. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
  2160. return 0;
  2161. }
  2162. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
  2163. return 0;
  2164. }
  2165. if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  2166. !cbb_add_hex(&cbb, client_random, 32) ||
  2167. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2168. !cbb_add_hex(&cbb, master, master_len) ||
  2169. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2170. !CBB_finish(&cbb, &out, &out_len)) {
  2171. CBB_cleanup(&cbb);
  2172. return 0;
  2173. }
  2174. CRYPTO_MUTEX_lock_write(&ctx->lock);
  2175. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2176. CRYPTO_MUTEX_unlock(&ctx->lock);
  2177. OPENSSL_free(out);
  2178. return ret;
  2179. }
  2180. int SSL_in_false_start(const SSL *s) {
  2181. return s->s3->tmp.in_false_start;
  2182. }
  2183. int SSL_cutthrough_complete(const SSL *s) {
  2184. return SSL_in_false_start(s);
  2185. }
  2186. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2187. size_t *ssl_session_size) {
  2188. *ssl_size = sizeof(SSL);
  2189. *ssl_ctx_size = sizeof(SSL_CTX);
  2190. *ssl_session_size = sizeof(SSL_SESSION);
  2191. }
  2192. int ssl3_can_false_start(const SSL *s) {
  2193. const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
  2194. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  2195. return !SSL_IS_DTLS(s) &&
  2196. SSL_version(s) >= TLS1_2_VERSION &&
  2197. (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
  2198. cipher != NULL &&
  2199. cipher->algorithm_mkey == SSL_kECDHE &&
  2200. (cipher->algorithm_enc == SSL_AES128GCM ||
  2201. cipher->algorithm_enc == SSL_AES256GCM ||
  2202. cipher->algorithm_enc == SSL_CHACHA20POLY1305);
  2203. }
  2204. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  2205. switch (version) {
  2206. case SSL3_VERSION:
  2207. return &SSLv3_enc_data;
  2208. case TLS1_VERSION:
  2209. return &TLSv1_enc_data;
  2210. case DTLS1_VERSION:
  2211. case TLS1_1_VERSION:
  2212. return &TLSv1_1_enc_data;
  2213. case DTLS1_2_VERSION:
  2214. case TLS1_2_VERSION:
  2215. return &TLSv1_2_enc_data;
  2216. default:
  2217. return NULL;
  2218. }
  2219. }
  2220. uint16_t ssl3_get_max_server_version(const SSL *s) {
  2221. uint16_t max_version;
  2222. if (SSL_IS_DTLS(s)) {
  2223. max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
  2224. if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
  2225. return DTLS1_2_VERSION;
  2226. }
  2227. if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  2228. return DTLS1_VERSION;
  2229. }
  2230. return 0;
  2231. }
  2232. max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
  2233. if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  2234. return TLS1_2_VERSION;
  2235. }
  2236. if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  2237. return TLS1_1_VERSION;
  2238. }
  2239. if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  2240. return TLS1_VERSION;
  2241. }
  2242. if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  2243. return SSL3_VERSION;
  2244. }
  2245. return 0;
  2246. }
  2247. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
  2248. uint16_t version = 0;
  2249. if (SSL_IS_DTLS(s)) {
  2250. /* Clamp client_version to max_version. */
  2251. if (s->max_version != 0 && client_version < s->max_version) {
  2252. client_version = s->max_version;
  2253. }
  2254. if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
  2255. version = DTLS1_2_VERSION;
  2256. } else if (client_version <= DTLS1_VERSION &&
  2257. !(s->options & SSL_OP_NO_DTLSv1)) {
  2258. version = DTLS1_VERSION;
  2259. }
  2260. /* Check against min_version. */
  2261. if (version != 0 && s->min_version != 0 && version > s->min_version) {
  2262. return 0;
  2263. }
  2264. return version;
  2265. } else {
  2266. /* Clamp client_version to max_version. */
  2267. if (s->max_version != 0 && client_version > s->max_version) {
  2268. client_version = s->max_version;
  2269. }
  2270. if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
  2271. version = TLS1_2_VERSION;
  2272. } else if (client_version >= TLS1_1_VERSION &&
  2273. !(s->options & SSL_OP_NO_TLSv1_1)) {
  2274. version = TLS1_1_VERSION;
  2275. } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
  2276. version = TLS1_VERSION;
  2277. } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
  2278. version = SSL3_VERSION;
  2279. }
  2280. /* Check against min_version. */
  2281. if (version != 0 && s->min_version != 0 && version < s->min_version) {
  2282. return 0;
  2283. }
  2284. return version;
  2285. }
  2286. }
  2287. uint16_t ssl3_get_max_client_version(SSL *s) {
  2288. uint32_t options = s->options;
  2289. uint16_t version = 0;
  2290. /* OpenSSL's API for controlling versions entails blacklisting individual
  2291. * protocols. This has two problems. First, on the client, the protocol can
  2292. * only express a contiguous range of versions. Second, a library consumer
  2293. * trying to set a maximum version cannot disable protocol versions that get
  2294. * added in a future version of the library.
  2295. *
  2296. * To account for both of these, OpenSSL interprets the client-side bitmask
  2297. * as a min/max range by picking the lowest contiguous non-empty range of
  2298. * enabled protocols. Note that this means it is impossible to set a maximum
  2299. * version of TLS 1.2 in a future-proof way.
  2300. *
  2301. * By this scheme, the maximum version is the lowest version V such that V is
  2302. * enabled and V+1 is disabled or unimplemented. */
  2303. if (SSL_IS_DTLS(s)) {
  2304. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  2305. version = DTLS1_2_VERSION;
  2306. }
  2307. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  2308. version = DTLS1_VERSION;
  2309. }
  2310. if (s->max_version != 0 && version < s->max_version) {
  2311. version = s->max_version;
  2312. }
  2313. } else {
  2314. if (!(options & SSL_OP_NO_TLSv1_2)) {
  2315. version = TLS1_2_VERSION;
  2316. }
  2317. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  2318. version = TLS1_1_VERSION;
  2319. }
  2320. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  2321. version = TLS1_VERSION;
  2322. }
  2323. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  2324. version = SSL3_VERSION;
  2325. }
  2326. if (s->max_version != 0 && version > s->max_version) {
  2327. version = s->max_version;
  2328. }
  2329. }
  2330. return version;
  2331. }
  2332. int ssl3_is_version_enabled(SSL *s, uint16_t version) {
  2333. if (SSL_IS_DTLS(s)) {
  2334. if (s->max_version != 0 && version < s->max_version) {
  2335. return 0;
  2336. }
  2337. if (s->min_version != 0 && version > s->min_version) {
  2338. return 0;
  2339. }
  2340. switch (version) {
  2341. case DTLS1_VERSION:
  2342. return !(s->options & SSL_OP_NO_DTLSv1);
  2343. case DTLS1_2_VERSION:
  2344. return !(s->options & SSL_OP_NO_DTLSv1_2);
  2345. default:
  2346. return 0;
  2347. }
  2348. } else {
  2349. if (s->max_version != 0 && version > s->max_version) {
  2350. return 0;
  2351. }
  2352. if (s->min_version != 0 && version < s->min_version) {
  2353. return 0;
  2354. }
  2355. switch (version) {
  2356. case SSL3_VERSION:
  2357. return !(s->options & SSL_OP_NO_SSLv3);
  2358. case TLS1_VERSION:
  2359. return !(s->options & SSL_OP_NO_TLSv1);
  2360. case TLS1_1_VERSION:
  2361. return !(s->options & SSL_OP_NO_TLSv1_1);
  2362. case TLS1_2_VERSION:
  2363. return !(s->options & SSL_OP_NO_TLSv1_2);
  2364. default:
  2365. return 0;
  2366. }
  2367. }
  2368. }
  2369. uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
  2370. if (!SSL_IS_DTLS(s)) {
  2371. return wire_version;
  2372. }
  2373. uint16_t tls_version = ~wire_version;
  2374. uint16_t version = tls_version + 0x0201;
  2375. /* If either component overflowed, clamp it so comparisons still work. */
  2376. if ((version >> 8) < (tls_version >> 8)) {
  2377. version = 0xff00 | (version & 0xff);
  2378. }
  2379. if ((version & 0xff) < (tls_version & 0xff)) {
  2380. version = (version & 0xff00) | 0xff;
  2381. }
  2382. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2383. if (version == TLS1_VERSION) {
  2384. version = TLS1_1_VERSION;
  2385. }
  2386. return version;
  2387. }
  2388. int SSL_cache_hit(SSL *s) { return s->hit; }
  2389. int SSL_is_server(SSL *s) { return s->server; }
  2390. void SSL_CTX_set_dos_protection_cb(
  2391. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2392. ctx->dos_protection_cb = cb;
  2393. }
  2394. void SSL_enable_fastradio_padding(SSL *s, char on_off) {
  2395. s->fastradio_padding = on_off;
  2396. }
  2397. void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
  2398. s->accept_peer_renegotiations = !reject;
  2399. }
  2400. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  2401. return ssl3_get_cipher_by_value(value);
  2402. }
  2403. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2404. const RC4_KEY **write_key) {
  2405. if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
  2406. return 0;
  2407. }
  2408. return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
  2409. EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
  2410. }
  2411. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2412. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2413. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2414. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2415. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2416. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2417. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2418. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2419. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2420. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2421. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }