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

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