25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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