Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

2959 рядки
82 KiB

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