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.
 
 
 
 
 
 

3294 rivejä
82 KiB

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