Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2960 linhas
83 KiB

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