Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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