PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/openssl/openssl/apps/s_server.c

http://github.com/joyent/node
C | 3011 lines | 2551 code | 230 blank | 230 comment | 738 complexity | 07d8ebaee6179bc8068d25f8c3c7d334 MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, ISC, Apache-2.0, MIT, AGPL-3.0
  1. /* apps/s_server.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. /* ====================================================================
  117. * Copyright 2005 Nokia. All rights reserved.
  118. *
  119. * The portions of the attached software ("Contribution") is developed by
  120. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  121. * license.
  122. *
  123. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  124. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  125. * support (see RFC 4279) to OpenSSL.
  126. *
  127. * No patent licenses or other rights except those expressly stated in
  128. * the OpenSSL open source license shall be deemed granted or received
  129. * expressly, by implication, estoppel, or otherwise.
  130. *
  131. * No assurances are provided by Nokia that the Contribution does not
  132. * infringe the patent or other intellectual property rights of any third
  133. * party or that the license provides you with all the necessary rights
  134. * to make use of the Contribution.
  135. *
  136. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  137. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  138. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  139. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  140. * OTHERWISE.
  141. */
  142. /* Until the key-gen callbacks are modified to use newer prototypes, we allow
  143. * deprecated functions for openssl-internal code */
  144. #ifdef OPENSSL_NO_DEPRECATED
  145. #undef OPENSSL_NO_DEPRECATED
  146. #endif
  147. #include <assert.h>
  148. #include <ctype.h>
  149. #include <stdio.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #include <openssl/e_os2.h>
  153. #ifdef OPENSSL_NO_STDIO
  154. #define APPS_WIN16
  155. #endif
  156. #if !defined(OPENSSL_SYS_NETWARE) /* conflicts with winsock2 stuff on netware */
  157. #include <sys/types.h>
  158. #endif
  159. /* With IPv6, it looks like Digital has mixed up the proper order of
  160. recursive header file inclusion, resulting in the compiler complaining
  161. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  162. is needed to have fileno() declared correctly... So let's define u_int */
  163. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  164. #define __U_INT
  165. typedef unsigned int u_int;
  166. #endif
  167. #include <openssl/lhash.h>
  168. #include <openssl/bn.h>
  169. #define USE_SOCKETS
  170. #include "apps.h"
  171. #include <openssl/err.h>
  172. #include <openssl/pem.h>
  173. #include <openssl/x509.h>
  174. #include <openssl/ssl.h>
  175. #include <openssl/rand.h>
  176. #include <openssl/ocsp.h>
  177. #ifndef OPENSSL_NO_DH
  178. #include <openssl/dh.h>
  179. #endif
  180. #ifndef OPENSSL_NO_RSA
  181. #include <openssl/rsa.h>
  182. #endif
  183. #ifndef OPENSSL_NO_SRP
  184. #include <openssl/srp.h>
  185. #endif
  186. #include "s_apps.h"
  187. #include "timeouts.h"
  188. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  189. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  190. #undef FIONBIO
  191. #endif
  192. #if defined(OPENSSL_SYS_BEOS_R5)
  193. #include <fcntl.h>
  194. #endif
  195. #ifndef OPENSSL_NO_RSA
  196. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  197. #endif
  198. static int sv_body(char *hostname, int s, unsigned char *context);
  199. static int www_body(char *hostname, int s, unsigned char *context);
  200. static void close_accept_socket(void );
  201. static void sv_usage(void);
  202. static int init_ssl_connection(SSL *s);
  203. static void print_stats(BIO *bp,SSL_CTX *ctx);
  204. static int generate_session_id(const SSL *ssl, unsigned char *id,
  205. unsigned int *id_len);
  206. #ifndef OPENSSL_NO_DH
  207. static DH *load_dh_param(const char *dhfile);
  208. static DH *get_dh512(void);
  209. #endif
  210. #ifdef MONOLITH
  211. static void s_server_init(void);
  212. #endif
  213. #ifndef OPENSSL_NO_DH
  214. static unsigned char dh512_p[]={
  215. 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
  216. 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
  217. 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
  218. 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
  219. 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
  220. 0x47,0x74,0xE8,0x33,
  221. };
  222. static unsigned char dh512_g[]={
  223. 0x02,
  224. };
  225. static DH *get_dh512(void)
  226. {
  227. DH *dh=NULL;
  228. if ((dh=DH_new()) == NULL) return(NULL);
  229. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  230. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  231. if ((dh->p == NULL) || (dh->g == NULL))
  232. return(NULL);
  233. return(dh);
  234. }
  235. #endif
  236. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  237. #undef BUFSIZZ
  238. #define BUFSIZZ 16*1024
  239. static int bufsize=BUFSIZZ;
  240. static int accept_socket= -1;
  241. #define TEST_CERT "server.pem"
  242. #ifndef OPENSSL_NO_TLSEXT
  243. #define TEST_CERT2 "server2.pem"
  244. #endif
  245. #undef PROG
  246. #define PROG s_server_main
  247. extern int verify_depth, verify_return_error;
  248. static char *cipher=NULL;
  249. static int s_server_verify=SSL_VERIFY_NONE;
  250. static int s_server_session_id_context = 1; /* anything will do */
  251. static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
  252. #ifndef OPENSSL_NO_TLSEXT
  253. static const char *s_cert_file2=TEST_CERT2,*s_key_file2=NULL;
  254. #endif
  255. static char *s_dcert_file=NULL,*s_dkey_file=NULL;
  256. #ifdef FIONBIO
  257. static int s_nbio=0;
  258. #endif
  259. static int s_nbio_test=0;
  260. int s_crlf=0;
  261. static SSL_CTX *ctx=NULL;
  262. #ifndef OPENSSL_NO_TLSEXT
  263. static SSL_CTX *ctx2=NULL;
  264. #endif
  265. static int www=0;
  266. static BIO *bio_s_out=NULL;
  267. static int s_debug=0;
  268. #ifndef OPENSSL_NO_TLSEXT
  269. static int s_tlsextdebug=0;
  270. static int s_tlsextstatus=0;
  271. static int cert_status_cb(SSL *s, void *arg);
  272. #endif
  273. static int s_msg=0;
  274. static int s_quiet=0;
  275. static char *keymatexportlabel=NULL;
  276. static int keymatexportlen=20;
  277. static int hack=0;
  278. #ifndef OPENSSL_NO_ENGINE
  279. static char *engine_id=NULL;
  280. #endif
  281. static const char *session_id_prefix=NULL;
  282. static int enable_timeouts = 0;
  283. static long socket_mtu;
  284. #ifndef OPENSSL_NO_DTLS1
  285. static int cert_chain = 0;
  286. #endif
  287. #ifndef OPENSSL_NO_PSK
  288. static char *psk_identity="Client_identity";
  289. char *psk_key=NULL; /* by default PSK is not used */
  290. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  291. unsigned char *psk, unsigned int max_psk_len)
  292. {
  293. unsigned int psk_len = 0;
  294. int ret;
  295. BIGNUM *bn = NULL;
  296. if (s_debug)
  297. BIO_printf(bio_s_out,"psk_server_cb\n");
  298. if (!identity)
  299. {
  300. BIO_printf(bio_err,"Error: client did not send PSK identity\n");
  301. goto out_err;
  302. }
  303. if (s_debug)
  304. BIO_printf(bio_s_out,"identity_len=%d identity=%s\n",
  305. identity ? (int)strlen(identity) : 0, identity);
  306. /* here we could lookup the given identity e.g. from a database */
  307. if (strcmp(identity, psk_identity) != 0)
  308. {
  309. BIO_printf(bio_s_out, "PSK error: client identity not found"
  310. " (got '%s' expected '%s')\n", identity,
  311. psk_identity);
  312. goto out_err;
  313. }
  314. if (s_debug)
  315. BIO_printf(bio_s_out, "PSK client identity found\n");
  316. /* convert the PSK key to binary */
  317. ret = BN_hex2bn(&bn, psk_key);
  318. if (!ret)
  319. {
  320. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  321. if (bn)
  322. BN_free(bn);
  323. return 0;
  324. }
  325. if (BN_num_bytes(bn) > (int)max_psk_len)
  326. {
  327. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  328. max_psk_len, BN_num_bytes(bn));
  329. BN_free(bn);
  330. return 0;
  331. }
  332. ret = BN_bn2bin(bn, psk);
  333. BN_free(bn);
  334. if (ret < 0)
  335. goto out_err;
  336. psk_len = (unsigned int)ret;
  337. if (s_debug)
  338. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  339. return psk_len;
  340. out_err:
  341. if (s_debug)
  342. BIO_printf(bio_err, "Error in PSK server callback\n");
  343. return 0;
  344. }
  345. #endif
  346. #ifndef OPENSSL_NO_SRP
  347. /* This is a context that we pass to callbacks */
  348. typedef struct srpsrvparm_st
  349. {
  350. char *login;
  351. SRP_VBASE *vb;
  352. SRP_user_pwd *user;
  353. } srpsrvparm;
  354. /* This callback pretends to require some asynchronous logic in order to obtain
  355. a verifier. When the callback is called for a new connection we return
  356. with a negative value. This will provoke the accept etc to return with
  357. an LOOKUP_X509. The main logic of the reinvokes the suspended call
  358. (which would normally occur after a worker has finished) and we
  359. set the user parameters.
  360. */
  361. static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  362. {
  363. srpsrvparm *p = (srpsrvparm *)arg;
  364. if (p->login == NULL && p->user == NULL )
  365. {
  366. p->login = SSL_get_srp_username(s);
  367. BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
  368. return (-1) ;
  369. }
  370. if (p->user == NULL)
  371. {
  372. BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
  373. return SSL3_AL_FATAL;
  374. }
  375. if (SSL_set_srp_server_param(s, p->user->N, p->user->g, p->user->s, p->user->v,
  376. p->user->info) < 0)
  377. {
  378. *ad = SSL_AD_INTERNAL_ERROR;
  379. return SSL3_AL_FATAL;
  380. }
  381. BIO_printf(bio_err, "SRP parameters set: username = \"%s\" info=\"%s\" \n", p->login,p->user->info);
  382. /* need to check whether there are memory leaks */
  383. p->user = NULL;
  384. p->login = NULL;
  385. return SSL_ERROR_NONE;
  386. }
  387. #endif
  388. #ifdef MONOLITH
  389. static void s_server_init(void)
  390. {
  391. accept_socket=-1;
  392. cipher=NULL;
  393. s_server_verify=SSL_VERIFY_NONE;
  394. s_dcert_file=NULL;
  395. s_dkey_file=NULL;
  396. s_cert_file=TEST_CERT;
  397. s_key_file=NULL;
  398. #ifndef OPENSSL_NO_TLSEXT
  399. s_cert_file2=TEST_CERT2;
  400. s_key_file2=NULL;
  401. ctx2=NULL;
  402. #endif
  403. #ifdef FIONBIO
  404. s_nbio=0;
  405. #endif
  406. s_nbio_test=0;
  407. ctx=NULL;
  408. www=0;
  409. bio_s_out=NULL;
  410. s_debug=0;
  411. s_msg=0;
  412. s_quiet=0;
  413. hack=0;
  414. #ifndef OPENSSL_NO_ENGINE
  415. engine_id=NULL;
  416. #endif
  417. }
  418. #endif
  419. static void sv_usage(void)
  420. {
  421. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  422. BIO_printf(bio_err,"\n");
  423. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  424. BIO_printf(bio_err," -context arg - set session ID context\n");
  425. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  426. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  427. BIO_printf(bio_err," -cert arg - certificate file to use\n");
  428. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  429. BIO_printf(bio_err," -crl_check - check the peer certificate has not been revoked by its CA.\n" \
  430. " The CRL(s) are appended to the certificate file\n");
  431. BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
  432. " or any other CRL in the CA chain. CRL(s) are appened to the\n" \
  433. " the certificate file.\n");
  434. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  435. BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n");
  436. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  437. BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  438. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  439. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  440. BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n");
  441. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  442. BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  443. BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n");
  444. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  445. BIO_printf(bio_err," or a default set of parameters is used\n");
  446. #ifndef OPENSSL_NO_ECDH
  447. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  448. " Use \"openssl ecparam -list_curves\" for all names\n" \
  449. " (default is nistp256).\n");
  450. #endif
  451. #ifdef FIONBIO
  452. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  453. #endif
  454. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  455. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  456. BIO_printf(bio_err," -debug - Print more output\n");
  457. BIO_printf(bio_err," -msg - Show protocol messages\n");
  458. BIO_printf(bio_err," -state - Print the SSL states\n");
  459. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  460. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  461. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  462. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  463. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  464. BIO_printf(bio_err," -quiet - No server output\n");
  465. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  466. #ifndef OPENSSL_NO_PSK
  467. BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n");
  468. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  469. # ifndef OPENSSL_NO_JPAKE
  470. BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
  471. # endif
  472. #endif
  473. #ifndef OPENSSL_NO_SRP
  474. BIO_printf(bio_err," -srpvfile file - The verifier file for SRP\n");
  475. BIO_printf(bio_err," -srpuserseed string - A seed string for a default user salt.\n");
  476. #endif
  477. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  478. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  479. BIO_printf(bio_err," -tls1_2 - Just talk TLSv1.2\n");
  480. BIO_printf(bio_err," -tls1_1 - Just talk TLSv1.1\n");
  481. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  482. BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n");
  483. BIO_printf(bio_err," -timeout - Enable timeouts\n");
  484. BIO_printf(bio_err," -mtu - Set link layer MTU\n");
  485. BIO_printf(bio_err," -chain - Read a certificate chain\n");
  486. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  487. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  488. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  489. BIO_printf(bio_err," -no_tls1_1 - Just disable TLSv1.1\n");
  490. BIO_printf(bio_err," -no_tls1_2 - Just disable TLSv1.2\n");
  491. #ifndef OPENSSL_NO_DH
  492. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  493. #endif
  494. #ifndef OPENSSL_NO_ECDH
  495. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  496. #endif
  497. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  498. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  499. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  500. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  501. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  502. #ifndef OPENSSL_NO_ENGINE
  503. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  504. #endif
  505. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  506. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  507. #ifndef OPENSSL_NO_TLSEXT
  508. BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n");
  509. BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  510. BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n");
  511. BIO_printf(bio_err," (default is %s)\n",TEST_CERT2);
  512. BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n");
  513. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2);
  514. BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
  515. BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
  516. BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
  517. # ifndef OPENSSL_NO_NEXTPROTONEG
  518. BIO_printf(bio_err," -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
  519. # endif
  520. # ifndef OPENSSL_NO_SRTP
  521. BIO_printf(bio_err," -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
  522. # endif
  523. #endif
  524. BIO_printf(bio_err," -keymatexport label - Export keying material using label\n");
  525. BIO_printf(bio_err," -keymatexportlen len - Export len bytes of keying material (default 20)\n");
  526. }
  527. static int local_argc=0;
  528. static char **local_argv;
  529. #ifdef CHARSET_EBCDIC
  530. static int ebcdic_new(BIO *bi);
  531. static int ebcdic_free(BIO *a);
  532. static int ebcdic_read(BIO *b, char *out, int outl);
  533. static int ebcdic_write(BIO *b, const char *in, int inl);
  534. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  535. static int ebcdic_gets(BIO *bp, char *buf, int size);
  536. static int ebcdic_puts(BIO *bp, const char *str);
  537. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  538. static BIO_METHOD methods_ebcdic=
  539. {
  540. BIO_TYPE_EBCDIC_FILTER,
  541. "EBCDIC/ASCII filter",
  542. ebcdic_write,
  543. ebcdic_read,
  544. ebcdic_puts,
  545. ebcdic_gets,
  546. ebcdic_ctrl,
  547. ebcdic_new,
  548. ebcdic_free,
  549. };
  550. typedef struct
  551. {
  552. size_t alloced;
  553. char buff[1];
  554. } EBCDIC_OUTBUFF;
  555. BIO_METHOD *BIO_f_ebcdic_filter()
  556. {
  557. return(&methods_ebcdic);
  558. }
  559. static int ebcdic_new(BIO *bi)
  560. {
  561. EBCDIC_OUTBUFF *wbuf;
  562. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  563. wbuf->alloced = 1024;
  564. wbuf->buff[0] = '\0';
  565. bi->ptr=(char *)wbuf;
  566. bi->init=1;
  567. bi->flags=0;
  568. return(1);
  569. }
  570. static int ebcdic_free(BIO *a)
  571. {
  572. if (a == NULL) return(0);
  573. if (a->ptr != NULL)
  574. OPENSSL_free(a->ptr);
  575. a->ptr=NULL;
  576. a->init=0;
  577. a->flags=0;
  578. return(1);
  579. }
  580. static int ebcdic_read(BIO *b, char *out, int outl)
  581. {
  582. int ret=0;
  583. if (out == NULL || outl == 0) return(0);
  584. if (b->next_bio == NULL) return(0);
  585. ret=BIO_read(b->next_bio,out,outl);
  586. if (ret > 0)
  587. ascii2ebcdic(out,out,ret);
  588. return(ret);
  589. }
  590. static int ebcdic_write(BIO *b, const char *in, int inl)
  591. {
  592. EBCDIC_OUTBUFF *wbuf;
  593. int ret=0;
  594. int num;
  595. unsigned char n;
  596. if ((in == NULL) || (inl <= 0)) return(0);
  597. if (b->next_bio == NULL) return(0);
  598. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  599. if (inl > (num = wbuf->alloced))
  600. {
  601. num = num + num; /* double the size */
  602. if (num < inl)
  603. num = inl;
  604. OPENSSL_free(wbuf);
  605. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  606. wbuf->alloced = num;
  607. wbuf->buff[0] = '\0';
  608. b->ptr=(char *)wbuf;
  609. }
  610. ebcdic2ascii(wbuf->buff, in, inl);
  611. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  612. return(ret);
  613. }
  614. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  615. {
  616. long ret;
  617. if (b->next_bio == NULL) return(0);
  618. switch (cmd)
  619. {
  620. case BIO_CTRL_DUP:
  621. ret=0L;
  622. break;
  623. default:
  624. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  625. break;
  626. }
  627. return(ret);
  628. }
  629. static int ebcdic_gets(BIO *bp, char *buf, int size)
  630. {
  631. int i, ret=0;
  632. if (bp->next_bio == NULL) return(0);
  633. /* return(BIO_gets(bp->next_bio,buf,size));*/
  634. for (i=0; i<size-1; ++i)
  635. {
  636. ret = ebcdic_read(bp,&buf[i],1);
  637. if (ret <= 0)
  638. break;
  639. else if (buf[i] == '\n')
  640. {
  641. ++i;
  642. break;
  643. }
  644. }
  645. if (i < size)
  646. buf[i] = '\0';
  647. return (ret < 0 && i == 0) ? ret : i;
  648. }
  649. static int ebcdic_puts(BIO *bp, const char *str)
  650. {
  651. if (bp->next_bio == NULL) return(0);
  652. return ebcdic_write(bp, str, strlen(str));
  653. }
  654. #endif
  655. #ifndef OPENSSL_NO_TLSEXT
  656. /* This is a context that we pass to callbacks */
  657. typedef struct tlsextctx_st {
  658. char * servername;
  659. BIO * biodebug;
  660. int extension_error;
  661. } tlsextctx;
  662. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  663. {
  664. tlsextctx * p = (tlsextctx *) arg;
  665. const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  666. if (servername && p->biodebug)
  667. BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername);
  668. if (!p->servername)
  669. return SSL_TLSEXT_ERR_NOACK;
  670. if (servername)
  671. {
  672. if (strcmp(servername,p->servername))
  673. return p->extension_error;
  674. if (ctx2)
  675. {
  676. BIO_printf(p->biodebug,"Switching server context.\n");
  677. SSL_set_SSL_CTX(s,ctx2);
  678. }
  679. }
  680. return SSL_TLSEXT_ERR_OK;
  681. }
  682. /* Structure passed to cert status callback */
  683. typedef struct tlsextstatusctx_st {
  684. /* Default responder to use */
  685. char *host, *path, *port;
  686. int use_ssl;
  687. int timeout;
  688. BIO *err;
  689. int verbose;
  690. } tlsextstatusctx;
  691. static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
  692. /* Certificate Status callback. This is called when a client includes a
  693. * certificate status request extension.
  694. *
  695. * This is a simplified version. It examines certificates each time and
  696. * makes one OCSP responder query for each request.
  697. *
  698. * A full version would store details such as the OCSP certificate IDs and
  699. * minimise the number of OCSP responses by caching them until they were
  700. * considered "expired".
  701. */
  702. static int cert_status_cb(SSL *s, void *arg)
  703. {
  704. tlsextstatusctx *srctx = arg;
  705. BIO *err = srctx->err;
  706. char *host, *port, *path;
  707. int use_ssl;
  708. unsigned char *rspder = NULL;
  709. int rspderlen;
  710. STACK_OF(OPENSSL_STRING) *aia = NULL;
  711. X509 *x = NULL;
  712. X509_STORE_CTX inctx;
  713. X509_OBJECT obj;
  714. OCSP_REQUEST *req = NULL;
  715. OCSP_RESPONSE *resp = NULL;
  716. OCSP_CERTID *id = NULL;
  717. STACK_OF(X509_EXTENSION) *exts;
  718. int ret = SSL_TLSEXT_ERR_NOACK;
  719. int i;
  720. #if 0
  721. STACK_OF(OCSP_RESPID) *ids;
  722. SSL_get_tlsext_status_ids(s, &ids);
  723. BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids));
  724. #endif
  725. if (srctx->verbose)
  726. BIO_puts(err, "cert_status: callback called\n");
  727. /* Build up OCSP query from server certificate */
  728. x = SSL_get_certificate(s);
  729. aia = X509_get1_ocsp(x);
  730. if (aia)
  731. {
  732. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  733. &host, &port, &path, &use_ssl))
  734. {
  735. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  736. goto err;
  737. }
  738. if (srctx->verbose)
  739. BIO_printf(err, "cert_status: AIA URL: %s\n",
  740. sk_OPENSSL_STRING_value(aia, 0));
  741. }
  742. else
  743. {
  744. if (!srctx->host)
  745. {
  746. BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
  747. goto done;
  748. }
  749. host = srctx->host;
  750. path = srctx->path;
  751. port = srctx->port;
  752. use_ssl = srctx->use_ssl;
  753. }
  754. if (!X509_STORE_CTX_init(&inctx,
  755. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  756. NULL, NULL))
  757. goto err;
  758. if (X509_STORE_get_by_subject(&inctx,X509_LU_X509,
  759. X509_get_issuer_name(x),&obj) <= 0)
  760. {
  761. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  762. X509_STORE_CTX_cleanup(&inctx);
  763. goto done;
  764. }
  765. req = OCSP_REQUEST_new();
  766. if (!req)
  767. goto err;
  768. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  769. X509_free(obj.data.x509);
  770. X509_STORE_CTX_cleanup(&inctx);
  771. if (!id)
  772. goto err;
  773. if (!OCSP_request_add0_id(req, id))
  774. goto err;
  775. id = NULL;
  776. /* Add any extensions to the request */
  777. SSL_get_tlsext_status_exts(s, &exts);
  778. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++)
  779. {
  780. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  781. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  782. goto err;
  783. }
  784. resp = process_responder(err, req, host, path, port, use_ssl, NULL,
  785. srctx->timeout);
  786. if (!resp)
  787. {
  788. BIO_puts(err, "cert_status: error querying responder\n");
  789. goto done;
  790. }
  791. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  792. if (rspderlen <= 0)
  793. goto err;
  794. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  795. if (srctx->verbose)
  796. {
  797. BIO_puts(err, "cert_status: ocsp response sent:\n");
  798. OCSP_RESPONSE_print(err, resp, 2);
  799. }
  800. ret = SSL_TLSEXT_ERR_OK;
  801. done:
  802. if (ret != SSL_TLSEXT_ERR_OK)
  803. ERR_print_errors(err);
  804. if (aia)
  805. {
  806. OPENSSL_free(host);
  807. OPENSSL_free(path);
  808. OPENSSL_free(port);
  809. X509_email_free(aia);
  810. }
  811. if (id)
  812. OCSP_CERTID_free(id);
  813. if (req)
  814. OCSP_REQUEST_free(req);
  815. if (resp)
  816. OCSP_RESPONSE_free(resp);
  817. return ret;
  818. err:
  819. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  820. goto done;
  821. }
  822. # ifndef OPENSSL_NO_NEXTPROTONEG
  823. /* This is the context that we pass to next_proto_cb */
  824. typedef struct tlsextnextprotoctx_st {
  825. unsigned char *data;
  826. unsigned int len;
  827. } tlsextnextprotoctx;
  828. static int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
  829. {
  830. tlsextnextprotoctx *next_proto = arg;
  831. *data = next_proto->data;
  832. *len = next_proto->len;
  833. return SSL_TLSEXT_ERR_OK;
  834. }
  835. # endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  836. #endif
  837. int MAIN(int, char **);
  838. #ifndef OPENSSL_NO_JPAKE
  839. static char *jpake_secret = NULL;
  840. #endif
  841. #ifndef OPENSSL_NO_SRP
  842. static srpsrvparm srp_callback_parm;
  843. #endif
  844. #ifndef OPENSSL_NO_SRTP
  845. static char *srtp_profiles = NULL;
  846. #endif
  847. int MAIN(int argc, char *argv[])
  848. {
  849. X509_VERIFY_PARAM *vpm = NULL;
  850. int badarg = 0;
  851. short port=PORT;
  852. char *CApath=NULL,*CAfile=NULL;
  853. unsigned char *context = NULL;
  854. char *dhfile = NULL;
  855. #ifndef OPENSSL_NO_ECDH
  856. char *named_curve = NULL;
  857. #endif
  858. int badop=0,bugs=0;
  859. int ret=1;
  860. int off=0;
  861. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  862. int state=0;
  863. const SSL_METHOD *meth=NULL;
  864. int socket_type=SOCK_STREAM;
  865. ENGINE *e=NULL;
  866. char *inrand=NULL;
  867. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  868. char *passarg = NULL, *pass = NULL;
  869. char *dpassarg = NULL, *dpass = NULL;
  870. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  871. X509 *s_cert = NULL, *s_dcert = NULL;
  872. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  873. int no_cache = 0;
  874. #ifndef OPENSSL_NO_TLSEXT
  875. EVP_PKEY *s_key2 = NULL;
  876. X509 *s_cert2 = NULL;
  877. tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
  878. # ifndef OPENSSL_NO_NEXTPROTONEG
  879. const char *next_proto_neg_in = NULL;
  880. tlsextnextprotoctx next_proto;
  881. # endif
  882. #endif
  883. #ifndef OPENSSL_NO_PSK
  884. /* by default do not send a PSK identity hint */
  885. static char *psk_identity_hint=NULL;
  886. #endif
  887. #ifndef OPENSSL_NO_SRP
  888. char *srpuserseed = NULL;
  889. char *srp_verifier_file = NULL;
  890. #endif
  891. meth=SSLv23_server_method();
  892. local_argc=argc;
  893. local_argv=argv;
  894. apps_startup();
  895. #ifdef MONOLITH
  896. s_server_init();
  897. #endif
  898. if (bio_err == NULL)
  899. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  900. if (!load_config(bio_err, NULL))
  901. goto end;
  902. verify_depth=0;
  903. #ifdef FIONBIO
  904. s_nbio=0;
  905. #endif
  906. s_nbio_test=0;
  907. argc--;
  908. argv++;
  909. while (argc >= 1)
  910. {
  911. if ((strcmp(*argv,"-port") == 0) ||
  912. (strcmp(*argv,"-accept") == 0))
  913. {
  914. if (--argc < 1) goto bad;
  915. if (!extract_port(*(++argv),&port))
  916. goto bad;
  917. }
  918. else if (strcmp(*argv,"-verify") == 0)
  919. {
  920. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  921. if (--argc < 1) goto bad;
  922. verify_depth=atoi(*(++argv));
  923. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  924. }
  925. else if (strcmp(*argv,"-Verify") == 0)
  926. {
  927. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  928. SSL_VERIFY_CLIENT_ONCE;
  929. if (--argc < 1) goto bad;
  930. verify_depth=atoi(*(++argv));
  931. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  932. }
  933. else if (strcmp(*argv,"-context") == 0)
  934. {
  935. if (--argc < 1) goto bad;
  936. context= (unsigned char *)*(++argv);
  937. }
  938. else if (strcmp(*argv,"-cert") == 0)
  939. {
  940. if (--argc < 1) goto bad;
  941. s_cert_file= *(++argv);
  942. }
  943. else if (strcmp(*argv,"-certform") == 0)
  944. {
  945. if (--argc < 1) goto bad;
  946. s_cert_format = str2fmt(*(++argv));
  947. }
  948. else if (strcmp(*argv,"-key") == 0)
  949. {
  950. if (--argc < 1) goto bad;
  951. s_key_file= *(++argv);
  952. }
  953. else if (strcmp(*argv,"-keyform") == 0)
  954. {
  955. if (--argc < 1) goto bad;
  956. s_key_format = str2fmt(*(++argv));
  957. }
  958. else if (strcmp(*argv,"-pass") == 0)
  959. {
  960. if (--argc < 1) goto bad;
  961. passarg = *(++argv);
  962. }
  963. else if (strcmp(*argv,"-dhparam") == 0)
  964. {
  965. if (--argc < 1) goto bad;
  966. dhfile = *(++argv);
  967. }
  968. #ifndef OPENSSL_NO_ECDH
  969. else if (strcmp(*argv,"-named_curve") == 0)
  970. {
  971. if (--argc < 1) goto bad;
  972. named_curve = *(++argv);
  973. }
  974. #endif
  975. else if (strcmp(*argv,"-dcertform") == 0)
  976. {
  977. if (--argc < 1) goto bad;
  978. s_dcert_format = str2fmt(*(++argv));
  979. }
  980. else if (strcmp(*argv,"-dcert") == 0)
  981. {
  982. if (--argc < 1) goto bad;
  983. s_dcert_file= *(++argv);
  984. }
  985. else if (strcmp(*argv,"-dkeyform") == 0)
  986. {
  987. if (--argc < 1) goto bad;
  988. s_dkey_format = str2fmt(*(++argv));
  989. }
  990. else if (strcmp(*argv,"-dpass") == 0)
  991. {
  992. if (--argc < 1) goto bad;
  993. dpassarg = *(++argv);
  994. }
  995. else if (strcmp(*argv,"-dkey") == 0)
  996. {
  997. if (--argc < 1) goto bad;
  998. s_dkey_file= *(++argv);
  999. }
  1000. else if (strcmp(*argv,"-nocert") == 0)
  1001. {
  1002. nocert=1;
  1003. }
  1004. else if (strcmp(*argv,"-CApath") == 0)
  1005. {
  1006. if (--argc < 1) goto bad;
  1007. CApath= *(++argv);
  1008. }
  1009. else if (strcmp(*argv,"-no_cache") == 0)
  1010. no_cache = 1;
  1011. else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
  1012. {
  1013. if (badarg)
  1014. goto bad;
  1015. continue;
  1016. }
  1017. else if (strcmp(*argv,"-verify_return_error") == 0)
  1018. verify_return_error = 1;
  1019. else if (strcmp(*argv,"-serverpref") == 0)
  1020. { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
  1021. else if (strcmp(*argv,"-legacy_renegotiation") == 0)
  1022. off|=SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  1023. else if (strcmp(*argv,"-cipher") == 0)
  1024. {
  1025. if (--argc < 1) goto bad;
  1026. cipher= *(++argv);
  1027. }
  1028. else if (strcmp(*argv,"-CAfile") == 0)
  1029. {
  1030. if (--argc < 1) goto bad;
  1031. CAfile= *(++argv);
  1032. }
  1033. #ifdef FIONBIO
  1034. else if (strcmp(*argv,"-nbio") == 0)
  1035. { s_nbio=1; }
  1036. #endif
  1037. else if (strcmp(*argv,"-nbio_test") == 0)
  1038. {
  1039. #ifdef FIONBIO
  1040. s_nbio=1;
  1041. #endif
  1042. s_nbio_test=1;
  1043. }
  1044. else if (strcmp(*argv,"-debug") == 0)
  1045. { s_debug=1; }
  1046. #ifndef OPENSSL_NO_TLSEXT
  1047. else if (strcmp(*argv,"-tlsextdebug") == 0)
  1048. s_tlsextdebug=1;
  1049. else if (strcmp(*argv,"-status") == 0)
  1050. s_tlsextstatus=1;
  1051. else if (strcmp(*argv,"-status_verbose") == 0)
  1052. {
  1053. s_tlsextstatus=1;
  1054. tlscstatp.verbose = 1;
  1055. }
  1056. else if (!strcmp(*argv, "-status_timeout"))
  1057. {
  1058. s_tlsextstatus=1;
  1059. if (--argc < 1) goto bad;
  1060. tlscstatp.timeout = atoi(*(++argv));
  1061. }
  1062. else if (!strcmp(*argv, "-status_url"))
  1063. {
  1064. s_tlsextstatus=1;
  1065. if (--argc < 1) goto bad;
  1066. if (!OCSP_parse_url(*(++argv),
  1067. &tlscstatp.host,
  1068. &tlscstatp.port,
  1069. &tlscstatp.path,
  1070. &tlscstatp.use_ssl))
  1071. {
  1072. BIO_printf(bio_err, "Error parsing URL\n");
  1073. goto bad;
  1074. }
  1075. }
  1076. #endif
  1077. else if (strcmp(*argv,"-msg") == 0)
  1078. { s_msg=1; }
  1079. else if (strcmp(*argv,"-hack") == 0)
  1080. { hack=1; }
  1081. else if (strcmp(*argv,"-state") == 0)
  1082. { state=1; }
  1083. else if (strcmp(*argv,"-crlf") == 0)
  1084. { s_crlf=1; }
  1085. else if (strcmp(*argv,"-quiet") == 0)
  1086. { s_quiet=1; }
  1087. else if (strcmp(*argv,"-bugs") == 0)
  1088. { bugs=1; }
  1089. else if (strcmp(*argv,"-no_tmp_rsa") == 0)
  1090. { no_tmp_rsa=1; }
  1091. else if (strcmp(*argv,"-no_dhe") == 0)
  1092. { no_dhe=1; }
  1093. else if (strcmp(*argv,"-no_ecdhe") == 0)
  1094. { no_ecdhe=1; }
  1095. #ifndef OPENSSL_NO_PSK
  1096. else if (strcmp(*argv,"-psk_hint") == 0)
  1097. {
  1098. if (--argc < 1) goto bad;
  1099. psk_identity_hint= *(++argv);
  1100. }
  1101. else if (strcmp(*argv,"-psk") == 0)
  1102. {
  1103. size_t i;
  1104. if (--argc < 1) goto bad;
  1105. psk_key=*(++argv);
  1106. for (i=0; i<strlen(psk_key); i++)
  1107. {
  1108. if (isxdigit((unsigned char)psk_key[i]))
  1109. continue;
  1110. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  1111. goto bad;
  1112. }
  1113. }
  1114. #endif
  1115. #ifndef OPENSSL_NO_SRP
  1116. else if (strcmp(*argv, "-srpvfile") == 0)
  1117. {
  1118. if (--argc < 1) goto bad;
  1119. srp_verifier_file = *(++argv);
  1120. meth = TLSv1_server_method();
  1121. }
  1122. else if (strcmp(*argv, "-srpuserseed") == 0)
  1123. {
  1124. if (--argc < 1) goto bad;
  1125. srpuserseed = *(++argv);
  1126. meth = TLSv1_server_method();
  1127. }
  1128. #endif
  1129. else if (strcmp(*argv,"-www") == 0)
  1130. { www=1; }
  1131. else if (strcmp(*argv,"-WWW") == 0)
  1132. { www=2; }
  1133. else if (strcmp(*argv,"-HTTP") == 0)
  1134. { www=3; }
  1135. else if (strcmp(*argv,"-no_ssl2") == 0)
  1136. { off|=SSL_OP_NO_SSLv2; }
  1137. else if (strcmp(*argv,"-no_ssl3") == 0)
  1138. { off|=SSL_OP_NO_SSLv3; }
  1139. else if (strcmp(*argv,"-no_tls1") == 0)
  1140. { off|=SSL_OP_NO_TLSv1; }
  1141. else if (strcmp(*argv,"-no_tls1_1") == 0)
  1142. { off|=SSL_OP_NO_TLSv1_1; }
  1143. else if (strcmp(*argv,"-no_tls1_2") == 0)
  1144. { off|=SSL_OP_NO_TLSv1_2; }
  1145. else if (strcmp(*argv,"-no_comp") == 0)
  1146. { off|=SSL_OP_NO_COMPRESSION; }
  1147. #ifndef OPENSSL_NO_TLSEXT
  1148. else if (strcmp(*argv,"-no_ticket") == 0)
  1149. { off|=SSL_OP_NO_TICKET; }
  1150. #endif
  1151. #ifndef OPENSSL_NO_SSL2
  1152. else if (strcmp(*argv,"-ssl2") == 0)
  1153. { meth=SSLv2_server_method(); }
  1154. #endif
  1155. #ifndef OPENSSL_NO_SSL3
  1156. else if (strcmp(*argv,"-ssl3") == 0)
  1157. { meth=SSLv3_server_method(); }
  1158. #endif
  1159. #ifndef OPENSSL_NO_TLS1
  1160. else if (strcmp(*argv,"-tls1") == 0)
  1161. { meth=TLSv1_server_method(); }
  1162. else if (strcmp(*argv,"-tls1_1") == 0)
  1163. { meth=TLSv1_1_server_method(); }
  1164. else if (strcmp(*argv,"-tls1_2") == 0)
  1165. { meth=TLSv1_2_server_method(); }
  1166. #endif
  1167. #ifndef OPENSSL_NO_DTLS1
  1168. else if (strcmp(*argv,"-dtls1") == 0)
  1169. {
  1170. meth=DTLSv1_server_method();
  1171. socket_type = SOCK_DGRAM;
  1172. }
  1173. else if (strcmp(*argv,"-timeout") == 0)
  1174. enable_timeouts = 1;
  1175. else if (strcmp(*argv,"-mtu") == 0)
  1176. {
  1177. if (--argc < 1) goto bad;
  1178. socket_mtu = atol(*(++argv));
  1179. }
  1180. else if (strcmp(*argv, "-chain") == 0)
  1181. cert_chain = 1;
  1182. #endif
  1183. else if (strcmp(*argv, "-id_prefix") == 0)
  1184. {
  1185. if (--argc < 1) goto bad;
  1186. session_id_prefix = *(++argv);
  1187. }
  1188. #ifndef OPENSSL_NO_ENGINE
  1189. else if (strcmp(*argv,"-engine") == 0)
  1190. {
  1191. if (--argc < 1) goto bad;
  1192. engine_id= *(++argv);
  1193. }
  1194. #endif
  1195. else if (strcmp(*argv,"-rand") == 0)
  1196. {
  1197. if (--argc < 1) goto bad;
  1198. inrand= *(++argv);
  1199. }
  1200. #ifndef OPENSSL_NO_TLSEXT
  1201. else if (strcmp(*argv,"-servername") == 0)
  1202. {
  1203. if (--argc < 1) goto bad;
  1204. tlsextcbp.servername= *(++argv);
  1205. }
  1206. else if (strcmp(*argv,"-servername_fatal") == 0)
  1207. { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; }
  1208. else if (strcmp(*argv,"-cert2") == 0)
  1209. {
  1210. if (--argc < 1) goto bad;
  1211. s_cert_file2= *(++argv);
  1212. }
  1213. else if (strcmp(*argv,"-key2") == 0)
  1214. {
  1215. if (--argc < 1) goto bad;
  1216. s_key_file2= *(++argv);
  1217. }
  1218. # ifndef OPENSSL_NO_NEXTPROTONEG
  1219. else if (strcmp(*argv,"-nextprotoneg") == 0)
  1220. {
  1221. if (--argc < 1) goto bad;
  1222. next_proto_neg_in = *(++argv);
  1223. }
  1224. # endif
  1225. #endif
  1226. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1227. else if (strcmp(*argv,"-jpake") == 0)
  1228. {
  1229. if (--argc < 1) goto bad;
  1230. jpake_secret = *(++argv);
  1231. }
  1232. #endif
  1233. #ifndef OPENSSL_NO_SRTP
  1234. else if (strcmp(*argv,"-use_srtp") == 0)
  1235. {
  1236. if (--argc < 1) goto bad;
  1237. srtp_profiles = *(++argv);
  1238. }
  1239. #endif
  1240. else if (strcmp(*argv,"-keymatexport") == 0)
  1241. {
  1242. if (--argc < 1) goto bad;
  1243. keymatexportlabel= *(++argv);
  1244. }
  1245. else if (strcmp(*argv,"-keymatexportlen") == 0)
  1246. {
  1247. if (--argc < 1) goto bad;
  1248. keymatexportlen=atoi(*(++argv));
  1249. if (keymatexportlen == 0) goto bad;
  1250. }
  1251. else
  1252. {
  1253. BIO_printf(bio_err,"unknown option %s\n",*argv);
  1254. badop=1;
  1255. break;
  1256. }
  1257. argc--;
  1258. argv++;
  1259. }
  1260. if (badop)
  1261. {
  1262. bad:
  1263. sv_usage();
  1264. goto end;
  1265. }
  1266. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1267. if (jpake_secret)
  1268. {
  1269. if (psk_key)
  1270. {
  1271. BIO_printf(bio_err,
  1272. "Can't use JPAKE and PSK together\n");
  1273. goto end;
  1274. }
  1275. psk_identity = "JPAKE";
  1276. if (cipher)
  1277. {
  1278. BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
  1279. goto end;
  1280. }
  1281. cipher = "PSK";
  1282. }
  1283. #endif
  1284. SSL_load_error_strings();
  1285. OpenSSL_add_ssl_algorithms();
  1286. #ifndef OPENSSL_NO_ENGINE
  1287. e = setup_engine(bio_err, engine_id, 1);
  1288. #endif
  1289. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
  1290. {
  1291. BIO_printf(bio_err, "Error getting password\n");
  1292. goto end;
  1293. }
  1294. if (s_key_file == NULL)
  1295. s_key_file = s_cert_file;
  1296. #ifndef OPENSSL_NO_TLSEXT
  1297. if (s_key_file2 == NULL)
  1298. s_key_file2 = s_cert_file2;
  1299. #endif
  1300. if (nocert == 0)
  1301. {
  1302. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  1303. "server certificate private key file");
  1304. if (!s_key)
  1305. {
  1306. ERR_print_errors(bio_err);
  1307. goto end;
  1308. }
  1309. s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
  1310. NULL, e, "server certificate file");
  1311. if (!s_cert)
  1312. {
  1313. ERR_print_errors(bio_err);
  1314. goto end;
  1315. }
  1316. #ifndef OPENSSL_NO_TLSEXT
  1317. if (tlsextcbp.servername)
  1318. {
  1319. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  1320. "second server certificate private key file");
  1321. if (!s_key2)
  1322. {
  1323. ERR_print_errors(bio_err);
  1324. goto end;
  1325. }
  1326. s_cert2 = load_cert(bio_err,s_cert_file2,s_cert_format,
  1327. NULL, e, "second server certificate file");
  1328. if (!s_cert2)
  1329. {
  1330. ERR_print_errors(bio_err);
  1331. goto end;
  1332. }
  1333. }
  1334. #endif
  1335. }
  1336. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  1337. if (next_proto_neg_in)
  1338. {
  1339. unsigned short len;
  1340. next_proto.data = next_protos_parse(&len, next_proto_neg_in);
  1341. if (next_proto.data == NULL)
  1342. goto end;
  1343. next_proto.len = len;
  1344. }
  1345. else
  1346. {
  1347. next_proto.data = NULL;
  1348. }
  1349. #endif
  1350. if (s_dcert_file)
  1351. {
  1352. if (s_dkey_file == NULL)
  1353. s_dkey_file = s_dcert_file;
  1354. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  1355. 0, dpass, e,
  1356. "second certificate private key file");
  1357. if (!s_dkey)
  1358. {
  1359. ERR_print_errors(bio_err);
  1360. goto end;
  1361. }
  1362. s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
  1363. NULL, e, "second server certificate file");
  1364. if (!s_dcert)
  1365. {
  1366. ERR_print_errors(bio_err);
  1367. goto end;
  1368. }
  1369. }
  1370. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1371. && !RAND_status())
  1372. {
  1373. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  1374. }
  1375. if (inrand != NULL)
  1376. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  1377. app_RAND_load_files(inrand));
  1378. if (bio_s_out == NULL)
  1379. {
  1380. if (s_quiet && !s_debug && !s_msg)
  1381. {
  1382. bio_s_out=BIO_new(BIO_s_null());
  1383. }
  1384. else
  1385. {
  1386. if (bio_s_out == NULL)
  1387. bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  1388. }
  1389. }
  1390. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1391. if (nocert)
  1392. #endif
  1393. {
  1394. s_cert_file=NULL;
  1395. s_key_file=NULL;
  1396. s_dcert_file=NULL;
  1397. s_dkey_file=NULL;
  1398. #ifndef OPENSSL_NO_TLSEXT
  1399. s_cert_file2=NULL;
  1400. s_key_file2=NULL;
  1401. #endif
  1402. }
  1403. ctx=SSL_CTX_new(meth);
  1404. if (ctx == NULL)
  1405. {
  1406. ERR_print_errors(bio_err);
  1407. goto end;
  1408. }
  1409. if (session_id_prefix)
  1410. {
  1411. if(strlen(session_id_prefix) >= 32)
  1412. BIO_printf(bio_err,
  1413. "warning: id_prefix is too long, only one new session will be possible\n");
  1414. else if(strlen(session_id_prefix) >= 16)
  1415. BIO_printf(bio_err,
  1416. "warning: id_prefix is too long if you use SSLv2\n");
  1417. if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
  1418. {
  1419. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1420. ERR_print_errors(bio_err);
  1421. goto end;
  1422. }
  1423. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1424. }
  1425. SSL_CTX_set_quiet_shutdown(ctx,1);
  1426. if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
  1427. if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1428. SSL_CTX_set_options(ctx,off);
  1429. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1430. * Setting read ahead solves this problem.
  1431. */
  1432. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  1433. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  1434. if (no_cache)
  1435. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  1436. else
  1437. SSL_CTX_sess_set_cache_size(ctx,128);
  1438. #ifndef OPENSSL_NO_SRTP
  1439. if (srtp_profiles != NULL)
  1440. SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
  1441. #endif
  1442. #if 0
  1443. if (cipher == NULL) cipher=getenv("SSL_CIPHER");
  1444. #endif
  1445. #if 0
  1446. if (s_cert_file == NULL)
  1447. {
  1448. BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
  1449. goto end;
  1450. }
  1451. #endif
  1452. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  1453. (!SSL_CTX_set_default_verify_paths(ctx)))
  1454. {
  1455. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1456. ERR_print_errors(bio_err);
  1457. /* goto end; */
  1458. }
  1459. if (vpm)
  1460. SSL_CTX_set1_param(ctx, vpm);
  1461. #ifndef OPENSSL_NO_TLSEXT
  1462. if (s_cert2)
  1463. {
  1464. ctx2=SSL_CTX_new(meth);
  1465. if (ctx2 == NULL)
  1466. {
  1467. ERR_print_errors(bio_err);
  1468. goto end;
  1469. }
  1470. }
  1471. if (ctx2)
  1472. {
  1473. BIO_printf(bio_s_out,"Setting secondary ctx parameters\n");
  1474. if (session_id_prefix)
  1475. {
  1476. if(strlen(session_id_prefix) >= 32)
  1477. BIO_printf(bio_err,
  1478. "warning: id_prefix is too long, only one new session will be possible\n");
  1479. else if(strlen(session_id_prefix) >= 16)
  1480. BIO_printf(bio_err,
  1481. "warning: id_prefix is too long if you use SSLv2\n");
  1482. if(!SSL_CTX_set_generate_session_id(ctx2, generate_session_id))
  1483. {
  1484. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1485. ERR_print_errors(bio_err);
  1486. goto end;
  1487. }
  1488. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1489. }
  1490. SSL_CTX_set_quiet_shutdown(ctx2,1);
  1491. if (bugs) SSL_CTX_set_options(ctx2,SSL_OP_ALL);
  1492. if (hack) SSL_CTX_set_options(ctx2,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1493. SSL_CTX_set_options(ctx2,off);
  1494. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1495. * Setting read ahead solves this problem.
  1496. */
  1497. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx2, 1);
  1498. if (state) SSL_CTX_set_info_callback(ctx2,apps_ssl_info_callback);
  1499. if (no_cache)
  1500. SSL_CTX_set_session_cache_mode(ctx2,SSL_SESS_CACHE_OFF);
  1501. else
  1502. SSL_CTX_sess_set_cache_size(ctx2,128);
  1503. if ((!SSL_CTX_load_verify_locations(ctx2,CAfile,CApath)) ||
  1504. (!SSL_CTX_set_default_verify_paths(ctx2)))
  1505. {
  1506. ERR_print_errors(bio_err);
  1507. }
  1508. if (vpm)
  1509. SSL_CTX_set1_param(ctx2, vpm);
  1510. }
  1511. # ifndef OPENSSL_NO_NEXTPROTONEG
  1512. if (next_proto.data)
  1513. SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto);
  1514. # endif
  1515. #endif
  1516. #ifndef OPENSSL_NO_DH
  1517. if (!no_dhe)
  1518. {
  1519. DH *dh=NULL;
  1520. if (dhfile)
  1521. dh = load_dh_param(dhfile);
  1522. else if (s_cert_file)
  1523. dh = load_dh_param(s_cert_file);
  1524. if (dh != NULL)
  1525. {
  1526. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1527. }
  1528. else
  1529. {
  1530. BIO_printf(bio_s_out,"Using default temp DH parameters\n");
  1531. dh=get_dh512();
  1532. }
  1533. (void)BIO_flush(bio_s_out);
  1534. SSL_CTX_set_tmp_dh(ctx,dh);
  1535. #ifndef OPENSSL_NO_TLSEXT
  1536. if (ctx2)
  1537. {
  1538. if (!dhfile)
  1539. {
  1540. DH *dh2=load_dh_param(s_cert_file2);
  1541. if (dh2 != NULL)
  1542. {
  1543. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1544. (void)BIO_flush(bio_s_out);
  1545. DH_free(dh);
  1546. dh = dh2;
  1547. }
  1548. }
  1549. SSL_CTX_set_tmp_dh(ctx2,dh);
  1550. }
  1551. #endif
  1552. DH_free(dh);
  1553. }
  1554. #endif
  1555. #ifndef OPENSSL_NO_ECDH
  1556. if (!no_ecdhe)
  1557. {
  1558. EC_KEY *ecdh=NULL;
  1559. if (named_curve)
  1560. {
  1561. int nid = OBJ_sn2nid(named_curve);
  1562. if (nid == 0)
  1563. {
  1564. BIO_printf(bio_err, "unknown curve name (%s)\n",
  1565. named_curve);
  1566. goto end;
  1567. }
  1568. ecdh = EC_KEY_new_by_curve_name(nid);
  1569. if (ecdh == NULL)
  1570. {
  1571. BIO_printf(bio_err, "unable to create curve (%s)\n",
  1572. named_curve);
  1573. goto end;
  1574. }
  1575. }
  1576. if (ecdh != NULL)
  1577. {
  1578. BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
  1579. }
  1580. else
  1581. {
  1582. BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
  1583. ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  1584. if (ecdh == NULL)
  1585. {
  1586. BIO_printf(bio_err, "unable to create curve (nistp256)\n");
  1587. goto end;
  1588. }
  1589. }
  1590. (void)BIO_flush(bio_s_out);
  1591. SSL_CTX_set_tmp_ecdh(ctx,ecdh);
  1592. #ifndef OPENSSL_NO_TLSEXT
  1593. if (ctx2)
  1594. SSL_CTX_set_tmp_ecdh(ctx2,ecdh);
  1595. #endif
  1596. EC_KEY_free(ecdh);
  1597. }
  1598. #endif
  1599. if (!set_cert_key_stuff(ctx, s_cert, s_key))
  1600. goto end;
  1601. #ifndef OPENSSL_NO_TLSEXT
  1602. if (ctx2 && !set_cert_key_stuff(ctx2,s_cert2,s_key2))
  1603. goto end;
  1604. #endif
  1605. if (s_dcert != NULL)
  1606. {
  1607. if (!set_cert_key_stuff(ctx, s_dcert, s_dkey))
  1608. goto end;
  1609. }
  1610. #ifndef OPENSSL_NO_RSA
  1611. #if 1
  1612. if (!no_tmp_rsa)
  1613. {
  1614. SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
  1615. #ifndef OPENSSL_NO_TLSEXT
  1616. if (ctx2)
  1617. SSL_CTX_set_tmp_rsa_callback(ctx2,tmp_rsa_cb);
  1618. #endif
  1619. }
  1620. #else
  1621. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
  1622. {
  1623. RSA *rsa;
  1624. BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
  1625. BIO_flush(bio_s_out);
  1626. rsa=RSA_generate_key(512,RSA_F4,NULL);
  1627. if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
  1628. {
  1629. ERR_print_errors(bio_err);
  1630. goto end;
  1631. }
  1632. #ifndef OPENSSL_NO_TLSEXT
  1633. if (ctx2)
  1634. {
  1635. if (!SSL_CTX_set_tmp_rsa(ctx2,rsa))
  1636. {
  1637. ERR_print_errors(bio_err);
  1638. goto end;
  1639. }
  1640. }
  1641. #endif
  1642. RSA_free(rsa);
  1643. BIO_printf(bio_s_out,"\n");
  1644. }
  1645. #endif
  1646. #endif
  1647. #ifndef OPENSSL_NO_PSK
  1648. #ifdef OPENSSL_NO_JPAKE
  1649. if (psk_key != NULL)
  1650. #else
  1651. if (psk_key != NULL || jpake_secret)
  1652. #endif
  1653. {
  1654. if (s_debug)
  1655. BIO_printf(bio_s_out, "PSK key given or JPAKE in use, setting server callback\n");
  1656. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1657. }
  1658. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint))
  1659. {
  1660. BIO_printf(bio_err,"error setting PSK identity hint to context\n");
  1661. ERR_print_errors(bio_err);
  1662. goto end;
  1663. }
  1664. #endif
  1665. if (cipher != NULL)
  1666. {
  1667. if(!SSL_CTX_set_cipher_list(ctx,cipher))
  1668. {
  1669. BIO_printf(bio_err,"error setting cipher list\n");
  1670. ERR_print_errors(bio_err);
  1671. goto end;
  1672. }
  1673. #ifndef OPENSSL_NO_TLSEXT
  1674. if (ctx2 && !SSL_CTX_set_cipher_list(ctx2,cipher))
  1675. {
  1676. BIO_printf(bio_err,"error setting cipher list\n");
  1677. ERR_print_errors(bio_err);
  1678. goto end;
  1679. }
  1680. #endif
  1681. }
  1682. SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
  1683. SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
  1684. sizeof s_server_session_id_context);
  1685. /* Set DTLS cookie generation and verification callbacks */
  1686. SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
  1687. SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
  1688. #ifndef OPENSSL_NO_TLSEXT
  1689. if (ctx2)
  1690. {
  1691. SSL_CTX_set_verify(ctx2,s_server_verify,verify_callback);
  1692. SSL_CTX_set_session_id_context(ctx2,(void*)&s_server_session_id_context,
  1693. sizeof s_server_session_id_context);
  1694. tlsextcbp.biodebug = bio_s_out;
  1695. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1696. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1697. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1698. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1699. }
  1700. #endif
  1701. #ifndef OPENSSL_NO_SRP
  1702. if (srp_verifier_file != NULL)
  1703. {
  1704. srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
  1705. srp_callback_parm.user = NULL;
  1706. srp_callback_parm.login = NULL;
  1707. if ((ret = SRP_VBASE_init(srp_callback_parm.vb, srp_verifier_file)) != SRP_NO_ERROR)
  1708. {
  1709. BIO_printf(bio_err,
  1710. "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
  1711. srp_verifier_file, ret);
  1712. goto end;
  1713. }
  1714. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE,verify_callback);
  1715. SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
  1716. SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
  1717. }
  1718. else
  1719. #endif
  1720. if (CAfile != NULL)
  1721. {
  1722. SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
  1723. #ifndef OPENSSL_NO_TLSEXT
  1724. if (ctx2)
  1725. SSL_CTX_set_client_CA_list(ctx2,SSL_load_client_CA_file(CAfile));
  1726. #endif
  1727. }
  1728. BIO_printf(bio_s_out,"ACCEPT\n");
  1729. (void)BIO_flush(bio_s_out);
  1730. if (www)
  1731. do_server(port,socket_type,&accept_socket,www_body, context);
  1732. else
  1733. do_server(port,socket_type,&accept_socket,sv_body, context);
  1734. print_stats(bio_s_out,ctx);
  1735. ret=0;
  1736. end:
  1737. if (ctx != NULL) SSL_CTX_free(ctx);
  1738. if (s_cert)
  1739. X509_free(s_cert);
  1740. if (s_dcert)
  1741. X509_free(s_dcert);
  1742. if (s_key)
  1743. EVP_PKEY_free(s_key);
  1744. if (s_dkey)
  1745. EVP_PKEY_free(s_dkey);
  1746. if (pass)
  1747. OPENSSL_free(pass);
  1748. if (dpass)
  1749. OPENSSL_free(dpass);
  1750. if (vpm)
  1751. X509_VERIFY_PARAM_free(vpm);
  1752. #ifndef OPENSSL_NO_TLSEXT
  1753. if (tlscstatp.host)
  1754. OPENSSL_free(tlscstatp.host);
  1755. if (tlscstatp.port)
  1756. OPENSSL_free(tlscstatp.port);
  1757. if (tlscstatp.path)
  1758. OPENSSL_free(tlscstatp.path);
  1759. if (ctx2 != NULL) SSL_CTX_free(ctx2);
  1760. if (s_cert2)
  1761. X509_free(s_cert2);
  1762. if (s_key2)
  1763. EVP_PKEY_free(s_key2);
  1764. #endif
  1765. if (bio_s_out != NULL)
  1766. {
  1767. BIO_free(bio_s_out);
  1768. bio_s_out=NULL;
  1769. }
  1770. apps_shutdown();
  1771. OPENSSL_EXIT(ret);
  1772. }
  1773. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1774. {
  1775. BIO_printf(bio,"%4ld items in the session cache\n",
  1776. SSL_CTX_sess_number(ssl_ctx));
  1777. BIO_printf(bio,"%4ld client connects (SSL_connect())\n",
  1778. SSL_CTX_sess_connect(ssl_ctx));
  1779. BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n",
  1780. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1781. BIO_printf(bio,"%4ld client connects that finished\n",
  1782. SSL_CTX_sess_connect_good(ssl_ctx));
  1783. BIO_printf(bio,"%4ld server accepts (SSL_accept())\n",
  1784. SSL_CTX_sess_accept(ssl_ctx));
  1785. BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n",
  1786. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1787. BIO_printf(bio,"%4ld server accepts that finished\n",
  1788. SSL_CTX_sess_accept_good(ssl_ctx));
  1789. BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
  1790. BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
  1791. BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
  1792. BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
  1793. BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n",
  1794. SSL_CTX_sess_cache_full(ssl_ctx),
  1795. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1796. }
  1797. static int sv_body(char *hostname, int s, unsigned char *context)
  1798. {
  1799. char *buf=NULL;
  1800. fd_set readfds;
  1801. int ret=1,width;
  1802. int k,i;
  1803. unsigned long l;
  1804. SSL *con=NULL;
  1805. BIO *sbio;
  1806. #ifndef OPENSSL_NO_KRB5
  1807. KSSL_CTX *kctx;
  1808. #endif
  1809. struct timeval timeout;
  1810. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1811. struct timeval tv;
  1812. #else
  1813. struct timeval *timeoutp;
  1814. #endif
  1815. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1816. {
  1817. BIO_printf(bio_err,"out of memory\n");
  1818. goto err;
  1819. }
  1820. #ifdef FIONBIO
  1821. if (s_nbio)
  1822. {
  1823. unsigned long sl=1;
  1824. if (!s_quiet)
  1825. BIO_printf(bio_err,"turning on non blocking io\n");
  1826. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1827. ERR_print_errors(bio_err);
  1828. }
  1829. #endif
  1830. if (con == NULL) {
  1831. con=SSL_new(ctx);
  1832. #ifndef OPENSSL_NO_TLSEXT
  1833. if (s_tlsextdebug)
  1834. {
  1835. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1836. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1837. }
  1838. if (s_tlsextstatus)
  1839. {
  1840. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1841. tlscstatp.err = bio_err;
  1842. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1843. }
  1844. #endif
  1845. #ifndef OPENSSL_NO_KRB5
  1846. if ((kctx = kssl_ctx_new()) != NULL)
  1847. {
  1848. SSL_set0_kssl_ctx(con, kctx);
  1849. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  1850. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  1851. }
  1852. #endif /* OPENSSL_NO_KRB5 */
  1853. if(context)
  1854. SSL_set_session_id_context(con, context,
  1855. strlen((char *)context));
  1856. }
  1857. SSL_clear(con);
  1858. #if 0
  1859. #ifdef TLSEXT_TYPE_opaque_prf_input
  1860. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  1861. #endif
  1862. #endif
  1863. if (SSL_version(con) == DTLS1_VERSION)
  1864. {
  1865. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1866. if (enable_timeouts)
  1867. {
  1868. timeout.tv_sec = 0;
  1869. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1870. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1871. timeout.tv_sec = 0;
  1872. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1873. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1874. }
  1875. if (socket_mtu > 28)
  1876. {
  1877. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1878. SSL_set_mtu(con, socket_mtu - 28);
  1879. }
  1880. else
  1881. /* want to do MTU discovery */
  1882. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1883. /* turn on cookie exchange */
  1884. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1885. }
  1886. else
  1887. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1888. if (s_nbio_test)
  1889. {
  1890. BIO *test;
  1891. test=BIO_new(BIO_f_nbio_test());
  1892. sbio=BIO_push(test,sbio);
  1893. }
  1894. #ifndef OPENSSL_NO_JPAKE
  1895. if(jpake_secret)
  1896. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  1897. #endif
  1898. SSL_set_bio(con,sbio,sbio);
  1899. SSL_set_accept_state(con);
  1900. /* SSL_set_fd(con,s); */
  1901. if (s_debug)
  1902. {
  1903. SSL_set_debug(con, 1);
  1904. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1905. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  1906. }
  1907. if (s_msg)
  1908. {
  1909. SSL_set_msg_callback(con, msg_cb);
  1910. SSL_set_msg_callback_arg(con, bio_s_out);
  1911. }
  1912. #ifndef OPENSSL_NO_TLSEXT
  1913. if (s_tlsextdebug)
  1914. {
  1915. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1916. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1917. }
  1918. #endif
  1919. width=s+1;
  1920. for (;;)
  1921. {
  1922. int read_from_terminal;
  1923. int read_from_sslcon;
  1924. read_from_terminal = 0;
  1925. read_from_sslcon = SSL_pending(con);
  1926. if (!read_from_sslcon)
  1927. {
  1928. FD_ZERO(&readfds);
  1929. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1930. openssl_fdset(fileno(stdin),&readfds);
  1931. #endif
  1932. openssl_fdset(s,&readfds);
  1933. /* Note: under VMS with SOCKETSHR the second parameter is
  1934. * currently of type (int *) whereas under other systems
  1935. * it is (void *) if you don't have a cast it will choke
  1936. * the compiler: if you do have a cast then you can either
  1937. * go for (int *) or (void *).
  1938. */
  1939. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1940. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1941. * on sockets. As a workaround we timeout the select every
  1942. * second and check for any keypress. In a proper Windows
  1943. * application we wouldn't do this because it is inefficient.
  1944. */
  1945. tv.tv_sec = 1;
  1946. tv.tv_usec = 0;
  1947. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1948. if((i < 0) || (!i && !_kbhit() ) )continue;
  1949. if(_kbhit())
  1950. read_from_terminal = 1;
  1951. #elif defined(OPENSSL_SYS_BEOS_R5)
  1952. /* Under BeOS-R5 the situation is similar to DOS */
  1953. tv.tv_sec = 1;
  1954. tv.tv_usec = 0;
  1955. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1956. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1957. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1958. continue;
  1959. if (read(fileno(stdin), buf, 0) >= 0)
  1960. read_from_terminal = 1;
  1961. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1962. #else
  1963. if ((SSL_version(con) == DTLS1_VERSION) &&
  1964. DTLSv1_get_timeout(con, &timeout))
  1965. timeoutp = &timeout;
  1966. else
  1967. timeoutp = NULL;
  1968. i=select(width,(void *)&readfds,NULL,NULL,timeoutp);
  1969. if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
  1970. {
  1971. BIO_printf(bio_err,"TIMEOUT occured\n");
  1972. }
  1973. if (i <= 0) continue;
  1974. if (FD_ISSET(fileno(stdin),&readfds))
  1975. read_from_terminal = 1;
  1976. #endif
  1977. if (FD_ISSET(s,&readfds))
  1978. read_from_sslcon = 1;
  1979. }
  1980. if (read_from_terminal)
  1981. {
  1982. if (s_crlf)
  1983. {
  1984. int j, lf_num;
  1985. i=raw_read_stdin(buf, bufsize/2);
  1986. lf_num = 0;
  1987. /* both loops are skipped when i <= 0 */
  1988. for (j = 0; j < i; j++)
  1989. if (buf[j] == '\n')
  1990. lf_num++;
  1991. for (j = i-1; j >= 0; j--)
  1992. {
  1993. buf[j+lf_num] = buf[j];
  1994. if (buf[j] == '\n')
  1995. {
  1996. lf_num--;
  1997. i++;
  1998. buf[j+lf_num] = '\r';
  1999. }
  2000. }
  2001. assert(lf_num == 0);
  2002. }
  2003. else
  2004. i=raw_read_stdin(buf,bufsize);
  2005. if (!s_quiet)
  2006. {
  2007. if ((i <= 0) || (buf[0] == 'Q'))
  2008. {
  2009. BIO_printf(bio_s_out,"DONE\n");
  2010. SHUTDOWN(s);
  2011. close_accept_socket();
  2012. ret= -11;
  2013. goto err;
  2014. }
  2015. if ((i <= 0) || (buf[0] == 'q'))
  2016. {
  2017. BIO_printf(bio_s_out,"DONE\n");
  2018. if (SSL_version(con) != DTLS1_VERSION)
  2019. SHUTDOWN(s);
  2020. /* close_accept_socket();
  2021. ret= -11;*/
  2022. goto err;
  2023. }
  2024. #ifndef OPENSSL_NO_HEARTBEATS
  2025. if ((buf[0] == 'B') &&
  2026. ((buf[1] == '\n') || (buf[1] == '\r')))
  2027. {
  2028. BIO_printf(bio_err,"HEARTBEATING\n");
  2029. SSL_heartbeat(con);
  2030. i=0;
  2031. continue;
  2032. }
  2033. #endif
  2034. if ((buf[0] == 'r') &&
  2035. ((buf[1] == '\n') || (buf[1] == '\r')))
  2036. {
  2037. SSL_renegotiate(con);
  2038. i=SSL_do_handshake(con);
  2039. printf("SSL_do_handshake -> %d\n",i);
  2040. i=0; /*13; */
  2041. continue;
  2042. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  2043. }
  2044. if ((buf[0] == 'R') &&
  2045. ((buf[1] == '\n') || (buf[1] == '\r')))
  2046. {
  2047. SSL_set_verify(con,
  2048. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  2049. SSL_renegotiate(con);
  2050. i=SSL_do_handshake(con);
  2051. printf("SSL_do_handshake -> %d\n",i);
  2052. i=0; /* 13; */
  2053. continue;
  2054. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  2055. }
  2056. if (buf[0] == 'P')
  2057. {
  2058. static const char *str="Lets print some clear text\n";
  2059. BIO_write(SSL_get_wbio(con),str,strlen(str));
  2060. }
  2061. if (buf[0] == 'S')
  2062. {
  2063. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  2064. }
  2065. }
  2066. #ifdef CHARSET_EBCDIC
  2067. ebcdic2ascii(buf,buf,i);
  2068. #endif
  2069. l=k=0;
  2070. for (;;)
  2071. {
  2072. /* should do a select for the write */
  2073. #ifdef RENEG
  2074. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  2075. #endif
  2076. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  2077. #ifndef OPENSSL_NO_SRP
  2078. while (SSL_get_error(con,k) == SSL_ERROR_WANT_X509_LOOKUP)
  2079. {
  2080. BIO_printf(bio_s_out,"LOOKUP renego during write\n");
  2081. srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login);
  2082. if (srp_callback_parm.user)
  2083. BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info);
  2084. else
  2085. BIO_printf(bio_s_out,"LOOKUP not successful\n");
  2086. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  2087. }
  2088. #endif
  2089. switch (SSL_get_error(con,k))
  2090. {
  2091. case SSL_ERROR_NONE:
  2092. break;
  2093. case SSL_ERROR_WANT_WRITE:
  2094. case SSL_ERROR_WANT_READ:
  2095. case SSL_ERROR_WANT_X509_LOOKUP:
  2096. BIO_printf(bio_s_out,"Write BLOCK\n");
  2097. break;
  2098. case SSL_ERROR_SYSCALL:
  2099. case SSL_ERROR_SSL:
  2100. BIO_printf(bio_s_out,"ERROR\n");
  2101. ERR_print_errors(bio_err);
  2102. ret=1;
  2103. goto err;
  2104. /* break; */
  2105. case SSL_ERROR_ZERO_RETURN:
  2106. BIO_printf(bio_s_out,"DONE\n");
  2107. ret=1;
  2108. goto err;
  2109. }
  2110. l+=k;
  2111. i-=k;
  2112. if (i <= 0) break;
  2113. }
  2114. }
  2115. if (read_from_sslcon)
  2116. {
  2117. if (!SSL_is_init_finished(con))
  2118. {
  2119. i=init_ssl_connection(con);
  2120. if (i < 0)
  2121. {
  2122. ret=0;
  2123. goto err;
  2124. }
  2125. else if (i == 0)
  2126. {
  2127. ret=1;
  2128. goto err;
  2129. }
  2130. }
  2131. else
  2132. {
  2133. again:
  2134. i=SSL_read(con,(char *)buf,bufsize);
  2135. #ifndef OPENSSL_NO_SRP
  2136. while (SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP)
  2137. {
  2138. BIO_printf(bio_s_out,"LOOKUP renego during read\n");
  2139. srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login);
  2140. if (srp_callback_parm.user)
  2141. BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info);
  2142. else
  2143. BIO_printf(bio_s_out,"LOOKUP not successful\n");
  2144. i=SSL_read(con,(char *)buf,bufsize);
  2145. }
  2146. #endif
  2147. switch (SSL_get_error(con,i))
  2148. {
  2149. case SSL_ERROR_NONE:
  2150. #ifdef CHARSET_EBCDIC
  2151. ascii2ebcdic(buf,buf,i);
  2152. #endif
  2153. raw_write_stdout(buf,
  2154. (unsigned int)i);
  2155. if (SSL_pending(con)) goto again;
  2156. break;
  2157. case SSL_ERROR_WANT_WRITE:
  2158. case SSL_ERROR_WANT_READ:
  2159. BIO_printf(bio_s_out,"Read BLOCK\n");
  2160. break;
  2161. case SSL_ERROR_SYSCALL:
  2162. case SSL_ERROR_SSL:
  2163. BIO_printf(bio_s_out,"ERROR\n");
  2164. ERR_print_errors(bio_err);
  2165. ret=1;
  2166. goto err;
  2167. case SSL_ERROR_ZERO_RETURN:
  2168. BIO_printf(bio_s_out,"DONE\n");
  2169. ret=1;
  2170. goto err;
  2171. }
  2172. }
  2173. }
  2174. }
  2175. err:
  2176. if (con != NULL)
  2177. {
  2178. BIO_printf(bio_s_out,"shutting down SSL\n");
  2179. #if 1
  2180. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2181. #else
  2182. SSL_shutdown(con);
  2183. #endif
  2184. SSL_free(con);
  2185. }
  2186. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  2187. if (buf != NULL)
  2188. {
  2189. OPENSSL_cleanse(buf,bufsize);
  2190. OPENSSL_free(buf);
  2191. }
  2192. if (ret >= 0)
  2193. BIO_printf(bio_s_out,"ACCEPT\n");
  2194. return(ret);
  2195. }
  2196. static void close_accept_socket(void)
  2197. {
  2198. BIO_printf(bio_err,"shutdown accept socket\n");
  2199. if (accept_socket >= 0)
  2200. {
  2201. SHUTDOWN2(accept_socket);
  2202. }
  2203. }
  2204. static int init_ssl_connection(SSL *con)
  2205. {
  2206. int i;
  2207. const char *str;
  2208. X509 *peer;
  2209. long verify_error;
  2210. MS_STATIC char buf[BUFSIZ];
  2211. #ifndef OPENSSL_NO_KRB5
  2212. char *client_princ;
  2213. #endif
  2214. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2215. const unsigned char *next_proto_neg;
  2216. unsigned next_proto_neg_len;
  2217. #endif
  2218. unsigned char *exportedkeymat;
  2219. i=SSL_accept(con);
  2220. #ifndef OPENSSL_NO_SRP
  2221. while (i <= 0 && SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP)
  2222. {
  2223. BIO_printf(bio_s_out,"LOOKUP during accept %s\n",srp_callback_parm.login);
  2224. srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login);
  2225. if (srp_callback_parm.user)
  2226. BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info);
  2227. else
  2228. BIO_printf(bio_s_out,"LOOKUP not successful\n");
  2229. i=SSL_accept(con);
  2230. }
  2231. #endif
  2232. if (i <= 0)
  2233. {
  2234. if (BIO_sock_should_retry(i))
  2235. {
  2236. BIO_printf(bio_s_out,"DELAY\n");
  2237. return(1);
  2238. }
  2239. BIO_printf(bio_err,"ERROR\n");
  2240. verify_error=SSL_get_verify_result(con);
  2241. if (verify_error != X509_V_OK)
  2242. {
  2243. BIO_printf(bio_err,"verify error:%s\n",
  2244. X509_verify_cert_error_string(verify_error));
  2245. }
  2246. else
  2247. ERR_print_errors(bio_err);
  2248. return(0);
  2249. }
  2250. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  2251. peer=SSL_get_peer_certificate(con);
  2252. if (peer != NULL)
  2253. {
  2254. BIO_printf(bio_s_out,"Client certificate\n");
  2255. PEM_write_bio_X509(bio_s_out,peer);
  2256. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  2257. BIO_printf(bio_s_out,"subject=%s\n",buf);
  2258. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  2259. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  2260. X509_free(peer);
  2261. }
  2262. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  2263. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  2264. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2265. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  2266. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2267. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2268. if (next_proto_neg)
  2269. {
  2270. BIO_printf(bio_s_out,"NEXTPROTO is ");
  2271. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2272. BIO_printf(bio_s_out, "\n");
  2273. }
  2274. #endif
  2275. #ifndef OPENSSL_NO_SRTP
  2276. {
  2277. SRTP_PROTECTION_PROFILE *srtp_profile
  2278. = SSL_get_selected_srtp_profile(con);
  2279. if(srtp_profile)
  2280. BIO_printf(bio_s_out,"SRTP Extension negotiated, profile=%s\n",
  2281. srtp_profile->name);
  2282. }
  2283. #endif
  2284. if (SSL_cache_hit(con)) BIO_printf(bio_s_out,"Reused session-id\n");
  2285. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  2286. TLS1_FLAGS_TLS_PADDING_BUG)
  2287. BIO_printf(bio_s_out,
  2288. "Peer has incorrect TLSv1 block padding\n");
  2289. #ifndef OPENSSL_NO_KRB5
  2290. client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con));
  2291. if (client_princ != NULL)
  2292. {
  2293. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  2294. client_princ);
  2295. }
  2296. #endif /* OPENSSL_NO_KRB5 */
  2297. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2298. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2299. if (keymatexportlabel != NULL)
  2300. {
  2301. BIO_printf(bio_s_out, "Keying material exporter:\n");
  2302. BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
  2303. BIO_printf(bio_s_out, " Length: %i bytes\n",
  2304. keymatexportlen);
  2305. exportedkeymat = OPENSSL_malloc(keymatexportlen);
  2306. if (exportedkeymat != NULL)
  2307. {
  2308. if (!SSL_export_keying_material(con, exportedkeymat,
  2309. keymatexportlen,
  2310. keymatexportlabel,
  2311. strlen(keymatexportlabel),
  2312. NULL, 0, 0))
  2313. {
  2314. BIO_printf(bio_s_out, " Error\n");
  2315. }
  2316. else
  2317. {
  2318. BIO_printf(bio_s_out, " Keying material: ");
  2319. for (i=0; i<keymatexportlen; i++)
  2320. BIO_printf(bio_s_out, "%02X",
  2321. exportedkeymat[i]);
  2322. BIO_printf(bio_s_out, "\n");
  2323. }
  2324. OPENSSL_free(exportedkeymat);
  2325. }
  2326. }
  2327. return(1);
  2328. }
  2329. #ifndef OPENSSL_NO_DH
  2330. static DH *load_dh_param(const char *dhfile)
  2331. {
  2332. DH *ret=NULL;
  2333. BIO *bio;
  2334. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  2335. goto err;
  2336. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  2337. err:
  2338. if (bio != NULL) BIO_free(bio);
  2339. return(ret);
  2340. }
  2341. #endif
  2342. #ifndef OPENSSL_NO_KRB5
  2343. char *client_princ;
  2344. #endif
  2345. #if 0
  2346. static int load_CA(SSL_CTX *ctx, char *file)
  2347. {
  2348. FILE *in;
  2349. X509 *x=NULL;
  2350. if ((in=fopen(file,"r")) == NULL)
  2351. return(0);
  2352. for (;;)
  2353. {
  2354. if (PEM_read_X509(in,&x,NULL) == NULL)
  2355. break;
  2356. SSL_CTX_add_client_CA(ctx,x);
  2357. }
  2358. if (x != NULL) X509_free(x);
  2359. fclose(in);
  2360. return(1);
  2361. }
  2362. #endif
  2363. static int www_body(char *hostname, int s, unsigned char *context)
  2364. {
  2365. char *buf=NULL;
  2366. int ret=1;
  2367. int i,j,k,dot;
  2368. SSL *con;
  2369. const SSL_CIPHER *c;
  2370. BIO *io,*ssl_bio,*sbio;
  2371. #ifndef OPENSSL_NO_KRB5
  2372. KSSL_CTX *kctx;
  2373. #endif
  2374. buf=OPENSSL_malloc(bufsize);
  2375. if (buf == NULL) return(0);
  2376. io=BIO_new(BIO_f_buffer());
  2377. ssl_bio=BIO_new(BIO_f_ssl());
  2378. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  2379. #ifdef FIONBIO
  2380. if (s_nbio)
  2381. {
  2382. unsigned long sl=1;
  2383. if (!s_quiet)
  2384. BIO_printf(bio_err,"turning on non blocking io\n");
  2385. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  2386. ERR_print_errors(bio_err);
  2387. }
  2388. #endif
  2389. /* lets make the output buffer a reasonable size */
  2390. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  2391. if ((con=SSL_new(ctx)) == NULL) goto err;
  2392. #ifndef OPENSSL_NO_TLSEXT
  2393. if (s_tlsextdebug)
  2394. {
  2395. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2396. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2397. }
  2398. #endif
  2399. #ifndef OPENSSL_NO_KRB5
  2400. if ((kctx = kssl_ctx_new()) != NULL)
  2401. {
  2402. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2403. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2404. }
  2405. #endif /* OPENSSL_NO_KRB5 */
  2406. if(context) SSL_set_session_id_context(con, context,
  2407. strlen((char *)context));
  2408. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  2409. if (s_nbio_test)
  2410. {
  2411. BIO *test;
  2412. test=BIO_new(BIO_f_nbio_test());
  2413. sbio=BIO_push(test,sbio);
  2414. }
  2415. SSL_set_bio(con,sbio,sbio);
  2416. SSL_set_accept_state(con);
  2417. /* SSL_set_fd(con,s); */
  2418. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  2419. BIO_push(io,ssl_bio);
  2420. #ifdef CHARSET_EBCDIC
  2421. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  2422. #endif
  2423. if (s_debug)
  2424. {
  2425. SSL_set_debug(con, 1);
  2426. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  2427. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  2428. }
  2429. if (s_msg)
  2430. {
  2431. SSL_set_msg_callback(con, msg_cb);
  2432. SSL_set_msg_callback_arg(con, bio_s_out);
  2433. }
  2434. for (;;)
  2435. {
  2436. if (hack)
  2437. {
  2438. i=SSL_accept(con);
  2439. #ifndef OPENSSL_NO_SRP
  2440. while (i <= 0 && SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP)
  2441. {
  2442. BIO_printf(bio_s_out,"LOOKUP during accept %s\n",srp_callback_parm.login);
  2443. srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login);
  2444. if (srp_callback_parm.user)
  2445. BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info);
  2446. else
  2447. BIO_printf(bio_s_out,"LOOKUP not successful\n");
  2448. i=SSL_accept(con);
  2449. }
  2450. #endif
  2451. switch (SSL_get_error(con,i))
  2452. {
  2453. case SSL_ERROR_NONE:
  2454. break;
  2455. case SSL_ERROR_WANT_WRITE:
  2456. case SSL_ERROR_WANT_READ:
  2457. case SSL_ERROR_WANT_X509_LOOKUP:
  2458. continue;
  2459. case SSL_ERROR_SYSCALL:
  2460. case SSL_ERROR_SSL:
  2461. case SSL_ERROR_ZERO_RETURN:
  2462. ret=1;
  2463. goto err;
  2464. /* break; */
  2465. }
  2466. SSL_renegotiate(con);
  2467. SSL_write(con,NULL,0);
  2468. }
  2469. i=BIO_gets(io,buf,bufsize-1);
  2470. if (i < 0) /* error */
  2471. {
  2472. if (!BIO_should_retry(io))
  2473. {
  2474. if (!s_quiet)
  2475. ERR_print_errors(bio_err);
  2476. goto err;
  2477. }
  2478. else
  2479. {
  2480. BIO_printf(bio_s_out,"read R BLOCK\n");
  2481. #if defined(OPENSSL_SYS_NETWARE)
  2482. delay(1000);
  2483. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2484. sleep(1);
  2485. #endif
  2486. continue;
  2487. }
  2488. }
  2489. else if (i == 0) /* end of input */
  2490. {
  2491. ret=1;
  2492. goto end;
  2493. }
  2494. /* else we have data */
  2495. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  2496. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  2497. {
  2498. char *p;
  2499. X509 *peer;
  2500. STACK_OF(SSL_CIPHER) *sk;
  2501. static const char *space=" ";
  2502. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2503. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2504. BIO_puts(io,"<pre>\n");
  2505. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2506. BIO_puts(io,"\n");
  2507. for (i=0; i<local_argc; i++)
  2508. {
  2509. BIO_puts(io,local_argv[i]);
  2510. BIO_write(io," ",1);
  2511. }
  2512. BIO_puts(io,"\n");
  2513. BIO_printf(io,
  2514. "Secure Renegotiation IS%s supported\n",
  2515. SSL_get_secure_renegotiation_support(con) ?
  2516. "" : " NOT");
  2517. /* The following is evil and should not really
  2518. * be done */
  2519. BIO_printf(io,"Ciphers supported in s_server binary\n");
  2520. sk=SSL_get_ciphers(con);
  2521. j=sk_SSL_CIPHER_num(sk);
  2522. for (i=0; i<j; i++)
  2523. {
  2524. c=sk_SSL_CIPHER_value(sk,i);
  2525. BIO_printf(io,"%-11s:%-25s",
  2526. SSL_CIPHER_get_version(c),
  2527. SSL_CIPHER_get_name(c));
  2528. if ((((i+1)%2) == 0) && (i+1 != j))
  2529. BIO_puts(io,"\n");
  2530. }
  2531. BIO_puts(io,"\n");
  2532. p=SSL_get_shared_ciphers(con,buf,bufsize);
  2533. if (p != NULL)
  2534. {
  2535. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  2536. j=i=0;
  2537. while (*p)
  2538. {
  2539. if (*p == ':')
  2540. {
  2541. BIO_write(io,space,26-j);
  2542. i++;
  2543. j=0;
  2544. BIO_write(io,((i%3)?" ":"\n"),1);
  2545. }
  2546. else
  2547. {
  2548. BIO_write(io,p,1);
  2549. j++;
  2550. }
  2551. p++;
  2552. }
  2553. BIO_puts(io,"\n");
  2554. }
  2555. BIO_printf(io,(SSL_cache_hit(con)
  2556. ?"---\nReused, "
  2557. :"---\nNew, "));
  2558. c=SSL_get_current_cipher(con);
  2559. BIO_printf(io,"%s, Cipher is %s\n",
  2560. SSL_CIPHER_get_version(c),
  2561. SSL_CIPHER_get_name(c));
  2562. SSL_SESSION_print(io,SSL_get_session(con));
  2563. BIO_printf(io,"---\n");
  2564. print_stats(io,SSL_get_SSL_CTX(con));
  2565. BIO_printf(io,"---\n");
  2566. peer=SSL_get_peer_certificate(con);
  2567. if (peer != NULL)
  2568. {
  2569. BIO_printf(io,"Client certificate\n");
  2570. X509_print(io,peer);
  2571. PEM_write_bio_X509(io,peer);
  2572. }
  2573. else
  2574. BIO_puts(io,"no client certificate available\n");
  2575. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  2576. break;
  2577. }
  2578. else if ((www == 2 || www == 3)
  2579. && (strncmp("GET /",buf,5) == 0))
  2580. {
  2581. BIO *file;
  2582. char *p,*e;
  2583. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2584. /* skip the '/' */
  2585. p= &(buf[5]);
  2586. dot = 1;
  2587. for (e=p; *e != '\0'; e++)
  2588. {
  2589. if (e[0] == ' ')
  2590. break;
  2591. switch (dot)
  2592. {
  2593. case 1:
  2594. dot = (e[0] == '.') ? 2 : 0;
  2595. break;
  2596. case 2:
  2597. dot = (e[0] == '.') ? 3 : 0;
  2598. break;
  2599. case 3:
  2600. dot = (e[0] == '/') ? -1 : 0;
  2601. break;
  2602. }
  2603. if (dot == 0)
  2604. dot = (e[0] == '/') ? 1 : 0;
  2605. }
  2606. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2607. if (*e == '\0')
  2608. {
  2609. BIO_puts(io,text);
  2610. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2611. break;
  2612. }
  2613. *e='\0';
  2614. if (dot)
  2615. {
  2616. BIO_puts(io,text);
  2617. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2618. break;
  2619. }
  2620. if (*p == '/')
  2621. {
  2622. BIO_puts(io,text);
  2623. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2624. break;
  2625. }
  2626. #if 0
  2627. /* append if a directory lookup */
  2628. if (e[-1] == '/')
  2629. strcat(p,"index.html");
  2630. #endif
  2631. /* if a directory, do the index thang */
  2632. if (app_isdir(p)>0)
  2633. {
  2634. #if 0 /* must check buffer size */
  2635. strcat(p,"/index.html");
  2636. #else
  2637. BIO_puts(io,text);
  2638. BIO_printf(io,"'%s' is a directory\r\n",p);
  2639. break;
  2640. #endif
  2641. }
  2642. if ((file=BIO_new_file(p,"r")) == NULL)
  2643. {
  2644. BIO_puts(io,text);
  2645. BIO_printf(io,"Error opening '%s'\r\n",p);
  2646. ERR_print_errors(io);
  2647. break;
  2648. }
  2649. if (!s_quiet)
  2650. BIO_printf(bio_err,"FILE:%s\n",p);
  2651. if (www == 2)
  2652. {
  2653. i=strlen(p);
  2654. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2655. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2656. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2657. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2658. else
  2659. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2660. }
  2661. /* send the file */
  2662. for (;;)
  2663. {
  2664. i=BIO_read(file,buf,bufsize);
  2665. if (i <= 0) break;
  2666. #ifdef RENEG
  2667. total_bytes+=i;
  2668. fprintf(stderr,"%d\n",i);
  2669. if (total_bytes > 3*1024)
  2670. {
  2671. total_bytes=0;
  2672. fprintf(stderr,"RENEGOTIATE\n");
  2673. SSL_renegotiate(con);
  2674. }
  2675. #endif
  2676. for (j=0; j<i; )
  2677. {
  2678. #ifdef RENEG
  2679. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2680. #endif
  2681. k=BIO_write(io,&(buf[j]),i-j);
  2682. if (k <= 0)
  2683. {
  2684. if (!BIO_should_retry(io))
  2685. goto write_error;
  2686. else
  2687. {
  2688. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2689. }
  2690. }
  2691. else
  2692. {
  2693. j+=k;
  2694. }
  2695. }
  2696. }
  2697. write_error:
  2698. BIO_free(file);
  2699. break;
  2700. }
  2701. }
  2702. for (;;)
  2703. {
  2704. i=(int)BIO_flush(io);
  2705. if (i <= 0)
  2706. {
  2707. if (!BIO_should_retry(io))
  2708. break;
  2709. }
  2710. else
  2711. break;
  2712. }
  2713. end:
  2714. #if 1
  2715. /* make sure we re-use sessions */
  2716. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2717. #else
  2718. /* This kills performance */
  2719. /* SSL_shutdown(con); A shutdown gets sent in the
  2720. * BIO_free_all(io) procession */
  2721. #endif
  2722. err:
  2723. if (ret >= 0)
  2724. BIO_printf(bio_s_out,"ACCEPT\n");
  2725. if (buf != NULL) OPENSSL_free(buf);
  2726. if (io != NULL) BIO_free_all(io);
  2727. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2728. return(ret);
  2729. }
  2730. #ifndef OPENSSL_NO_RSA
  2731. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2732. {
  2733. BIGNUM *bn = NULL;
  2734. static RSA *rsa_tmp=NULL;
  2735. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2736. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2737. if (!rsa_tmp && bn)
  2738. {
  2739. if (!s_quiet)
  2740. {
  2741. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2742. (void)BIO_flush(bio_err);
  2743. }
  2744. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2745. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2746. {
  2747. if(rsa_tmp) RSA_free(rsa_tmp);
  2748. rsa_tmp = NULL;
  2749. }
  2750. if (!s_quiet)
  2751. {
  2752. BIO_printf(bio_err,"\n");
  2753. (void)BIO_flush(bio_err);
  2754. }
  2755. BN_free(bn);
  2756. }
  2757. return(rsa_tmp);
  2758. }
  2759. #endif
  2760. #define MAX_SESSION_ID_ATTEMPTS 10
  2761. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2762. unsigned int *id_len)
  2763. {
  2764. unsigned int count = 0;
  2765. do {
  2766. RAND_pseudo_bytes(id, *id_len);
  2767. /* Prefix the session_id with the required prefix. NB: If our
  2768. * prefix is too long, clip it - but there will be worse effects
  2769. * anyway, eg. the server could only possibly create 1 session
  2770. * ID (ie. the prefix!) so all future session negotiations will
  2771. * fail due to conflicts. */
  2772. memcpy(id, session_id_prefix,
  2773. (strlen(session_id_prefix) < *id_len) ?
  2774. strlen(session_id_prefix) : *id_len);
  2775. }
  2776. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2777. (++count < MAX_SESSION_ID_ATTEMPTS));
  2778. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2779. return 0;
  2780. return 1;
  2781. }