You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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