Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2882 Zeilen
81 KiB

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