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

3241 line
81 KiB

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