s_server.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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-2001 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. #include <assert.h>
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include <sys/types.h>
  121. #include <sys/stat.h>
  122. #include <openssl/e_os2.h>
  123. #ifdef OPENSSL_NO_STDIO
  124. #define APPS_WIN16
  125. #endif
  126. /* With IPv6, it looks like Digital has mixed up the proper order of
  127. recursive header file inclusion, resulting in the compiler complaining
  128. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  129. is needed to have fileno() declared correctly... So let's define u_int */
  130. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  131. #define __U_INT
  132. typedef unsigned int u_int;
  133. #endif
  134. #include <openssl/lhash.h>
  135. #include <openssl/bn.h>
  136. #define USE_SOCKETS
  137. #include "apps.h"
  138. #include <openssl/err.h>
  139. #include <openssl/pem.h>
  140. #include <openssl/x509.h>
  141. #include <openssl/ssl.h>
  142. #include <openssl/rand.h>
  143. #include "s_apps.h"
  144. #ifdef OPENSSL_SYS_WINDOWS
  145. #include <conio.h>
  146. #endif
  147. #ifdef OPENSSL_SYS_WINCE
  148. /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
  149. #ifdef fileno
  150. #undef fileno
  151. #endif
  152. #define fileno(a) (int)_fileno(a)
  153. #endif
  154. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  155. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  156. #undef FIONBIO
  157. #endif
  158. #ifndef OPENSSL_NO_RSA
  159. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  160. #endif
  161. static int sv_body(char *hostname, int s, unsigned char *context);
  162. static int www_body(char *hostname, int s, unsigned char *context);
  163. static void close_accept_socket(void );
  164. static void sv_usage(void);
  165. static int init_ssl_connection(SSL *s);
  166. static void print_stats(BIO *bp,SSL_CTX *ctx);
  167. static int generate_session_id(const SSL *ssl, unsigned char *id,
  168. unsigned int *id_len);
  169. #ifndef OPENSSL_NO_DH
  170. static DH *load_dh_param(char *dhfile);
  171. static DH *get_dh512(void);
  172. #endif
  173. #ifdef MONOLITH
  174. static void s_server_init(void);
  175. #endif
  176. #ifndef S_ISDIR
  177. # if defined(_S_IFMT) && defined(_S_IFDIR)
  178. # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
  179. # else
  180. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  181. # endif
  182. #endif
  183. #ifndef OPENSSL_NO_DH
  184. static unsigned char dh512_p[]={
  185. 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
  186. 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
  187. 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
  188. 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
  189. 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
  190. 0x47,0x74,0xE8,0x33,
  191. };
  192. static unsigned char dh512_g[]={
  193. 0x02,
  194. };
  195. static DH *get_dh512(void)
  196. {
  197. DH *dh=NULL;
  198. if ((dh=DH_new()) == NULL) return(NULL);
  199. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  200. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  201. if ((dh->p == NULL) || (dh->g == NULL))
  202. return(NULL);
  203. return(dh);
  204. }
  205. #endif
  206. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  207. #undef BUFSIZZ
  208. #define BUFSIZZ 16*1024
  209. static int bufsize=BUFSIZZ;
  210. static int accept_socket= -1;
  211. #define TEST_CERT "server.pem"
  212. #undef PROG
  213. #define PROG s_server_main
  214. extern int verify_depth;
  215. static char *cipher=NULL;
  216. static int s_server_verify=SSL_VERIFY_NONE;
  217. static int s_server_session_id_context = 1; /* anything will do */
  218. static char *s_cert_file=TEST_CERT,*s_key_file=NULL;
  219. static char *s_dcert_file=NULL,*s_dkey_file=NULL;
  220. #ifdef FIONBIO
  221. static int s_nbio=0;
  222. #endif
  223. static int s_nbio_test=0;
  224. int s_crlf=0;
  225. static SSL_CTX *ctx=NULL;
  226. static int www=0;
  227. static BIO *bio_s_out=NULL;
  228. static int s_debug=0;
  229. static int s_msg=0;
  230. static int s_quiet=0;
  231. static int hack=0;
  232. static char *engine_id=NULL;
  233. static const char *session_id_prefix=NULL;
  234. #ifdef MONOLITH
  235. static void s_server_init(void)
  236. {
  237. accept_socket=-1;
  238. cipher=NULL;
  239. s_server_verify=SSL_VERIFY_NONE;
  240. s_dcert_file=NULL;
  241. s_dkey_file=NULL;
  242. s_cert_file=TEST_CERT;
  243. s_key_file=NULL;
  244. #ifdef FIONBIO
  245. s_nbio=0;
  246. #endif
  247. s_nbio_test=0;
  248. ctx=NULL;
  249. www=0;
  250. bio_s_out=NULL;
  251. s_debug=0;
  252. s_msg=0;
  253. s_quiet=0;
  254. hack=0;
  255. engine_id=NULL;
  256. }
  257. #endif
  258. static void sv_usage(void)
  259. {
  260. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  261. BIO_printf(bio_err,"\n");
  262. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  263. BIO_printf(bio_err," -context arg - set session ID context\n");
  264. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  265. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  266. BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
  267. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  268. BIO_printf(bio_err," -key arg - Private Key file to use, PEM format assumed, in cert file if\n");
  269. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  270. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  271. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  272. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  273. BIO_printf(bio_err," or a default set of parameters is used\n");
  274. #ifndef OPENSSL_NO_ECDH
  275. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  276. " Use \"openssl ecparam -list_curves\" for all names\n" \
  277. " (default is sect163r2).\n");
  278. #endif
  279. #ifdef FIONBIO
  280. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  281. #endif
  282. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  283. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  284. BIO_printf(bio_err," -debug - Print more output\n");
  285. BIO_printf(bio_err," -msg - Show protocol messages\n");
  286. BIO_printf(bio_err," -state - Print the SSL states\n");
  287. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  288. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  289. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  290. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  291. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  292. BIO_printf(bio_err," -quiet - No server output\n");
  293. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  294. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  295. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  296. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  297. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  298. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  299. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  300. #ifndef OPENSSL_NO_DH
  301. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  302. #endif
  303. #ifndef OPENSSL_NO_ECDH
  304. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  305. #endif
  306. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  307. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  308. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  309. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  310. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  311. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  312. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  313. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  314. }
  315. static int local_argc=0;
  316. static char **local_argv;
  317. #ifdef CHARSET_EBCDIC
  318. static int ebcdic_new(BIO *bi);
  319. static int ebcdic_free(BIO *a);
  320. static int ebcdic_read(BIO *b, char *out, int outl);
  321. static int ebcdic_write(BIO *b, const char *in, int inl);
  322. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  323. static int ebcdic_gets(BIO *bp, char *buf, int size);
  324. static int ebcdic_puts(BIO *bp, const char *str);
  325. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  326. static BIO_METHOD methods_ebcdic=
  327. {
  328. BIO_TYPE_EBCDIC_FILTER,
  329. "EBCDIC/ASCII filter",
  330. ebcdic_write,
  331. ebcdic_read,
  332. ebcdic_puts,
  333. ebcdic_gets,
  334. ebcdic_ctrl,
  335. ebcdic_new,
  336. ebcdic_free,
  337. };
  338. typedef struct
  339. {
  340. size_t alloced;
  341. char buff[1];
  342. } EBCDIC_OUTBUFF;
  343. BIO_METHOD *BIO_f_ebcdic_filter()
  344. {
  345. return(&methods_ebcdic);
  346. }
  347. static int ebcdic_new(BIO *bi)
  348. {
  349. EBCDIC_OUTBUFF *wbuf;
  350. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  351. wbuf->alloced = 1024;
  352. wbuf->buff[0] = '\0';
  353. bi->ptr=(char *)wbuf;
  354. bi->init=1;
  355. bi->flags=0;
  356. return(1);
  357. }
  358. static int ebcdic_free(BIO *a)
  359. {
  360. if (a == NULL) return(0);
  361. if (a->ptr != NULL)
  362. OPENSSL_free(a->ptr);
  363. a->ptr=NULL;
  364. a->init=0;
  365. a->flags=0;
  366. return(1);
  367. }
  368. static int ebcdic_read(BIO *b, char *out, int outl)
  369. {
  370. int ret=0;
  371. if (out == NULL || outl == 0) return(0);
  372. if (b->next_bio == NULL) return(0);
  373. ret=BIO_read(b->next_bio,out,outl);
  374. if (ret > 0)
  375. ascii2ebcdic(out,out,ret);
  376. return(ret);
  377. }
  378. static int ebcdic_write(BIO *b, const char *in, int inl)
  379. {
  380. EBCDIC_OUTBUFF *wbuf;
  381. int ret=0;
  382. int num;
  383. unsigned char n;
  384. if ((in == NULL) || (inl <= 0)) return(0);
  385. if (b->next_bio == NULL) return(0);
  386. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  387. if (inl > (num = wbuf->alloced))
  388. {
  389. num = num + num; /* double the size */
  390. if (num < inl)
  391. num = inl;
  392. OPENSSL_free(wbuf);
  393. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  394. wbuf->alloced = num;
  395. wbuf->buff[0] = '\0';
  396. b->ptr=(char *)wbuf;
  397. }
  398. ebcdic2ascii(wbuf->buff, in, inl);
  399. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  400. return(ret);
  401. }
  402. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  403. {
  404. long ret;
  405. if (b->next_bio == NULL) return(0);
  406. switch (cmd)
  407. {
  408. case BIO_CTRL_DUP:
  409. ret=0L;
  410. break;
  411. default:
  412. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  413. break;
  414. }
  415. return(ret);
  416. }
  417. static int ebcdic_gets(BIO *bp, char *buf, int size)
  418. {
  419. int i, ret=0;
  420. if (bp->next_bio == NULL) return(0);
  421. /* return(BIO_gets(bp->next_bio,buf,size));*/
  422. for (i=0; i<size-1; ++i)
  423. {
  424. ret = ebcdic_read(bp,&buf[i],1);
  425. if (ret <= 0)
  426. break;
  427. else if (buf[i] == '\n')
  428. {
  429. ++i;
  430. break;
  431. }
  432. }
  433. if (i < size)
  434. buf[i] = '\0';
  435. return (ret < 0 && i == 0) ? ret : i;
  436. }
  437. static int ebcdic_puts(BIO *bp, const char *str)
  438. {
  439. if (bp->next_bio == NULL) return(0);
  440. return ebcdic_write(bp, str, strlen(str));
  441. }
  442. #endif
  443. int MAIN(int, char **);
  444. int MAIN(int argc, char *argv[])
  445. {
  446. X509_STORE *store = NULL;
  447. int vflags = 0;
  448. short port=PORT;
  449. char *CApath=NULL,*CAfile=NULL;
  450. char *context = NULL;
  451. char *dhfile = NULL;
  452. char *named_curve = NULL;
  453. int badop=0,bugs=0;
  454. int ret=1;
  455. int off=0;
  456. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  457. int state=0;
  458. SSL_METHOD *meth=NULL;
  459. ENGINE *e=NULL;
  460. char *inrand=NULL;
  461. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  462. meth=SSLv23_server_method();
  463. #elif !defined(OPENSSL_NO_SSL3)
  464. meth=SSLv3_server_method();
  465. #elif !defined(OPENSSL_NO_SSL2)
  466. meth=SSLv2_server_method();
  467. #endif
  468. local_argc=argc;
  469. local_argv=argv;
  470. apps_startup();
  471. #ifdef MONOLITH
  472. s_server_init();
  473. #endif
  474. if (bio_err == NULL)
  475. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  476. if (!load_config(bio_err, NULL))
  477. goto end;
  478. verify_depth=0;
  479. #ifdef FIONBIO
  480. s_nbio=0;
  481. #endif
  482. s_nbio_test=0;
  483. argc--;
  484. argv++;
  485. while (argc >= 1)
  486. {
  487. if ((strcmp(*argv,"-port") == 0) ||
  488. (strcmp(*argv,"-accept") == 0))
  489. {
  490. if (--argc < 1) goto bad;
  491. if (!extract_port(*(++argv),&port))
  492. goto bad;
  493. }
  494. else if (strcmp(*argv,"-verify") == 0)
  495. {
  496. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  497. if (--argc < 1) goto bad;
  498. verify_depth=atoi(*(++argv));
  499. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  500. }
  501. else if (strcmp(*argv,"-Verify") == 0)
  502. {
  503. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  504. SSL_VERIFY_CLIENT_ONCE;
  505. if (--argc < 1) goto bad;
  506. verify_depth=atoi(*(++argv));
  507. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  508. }
  509. else if (strcmp(*argv,"-context") == 0)
  510. {
  511. if (--argc < 1) goto bad;
  512. context= *(++argv);
  513. }
  514. else if (strcmp(*argv,"-cert") == 0)
  515. {
  516. if (--argc < 1) goto bad;
  517. s_cert_file= *(++argv);
  518. }
  519. else if (strcmp(*argv,"-key") == 0)
  520. {
  521. if (--argc < 1) goto bad;
  522. s_key_file= *(++argv);
  523. }
  524. else if (strcmp(*argv,"-dhparam") == 0)
  525. {
  526. if (--argc < 1) goto bad;
  527. dhfile = *(++argv);
  528. }
  529. #ifndef OPENSSL_NO_ECDH
  530. else if (strcmp(*argv,"-named_curve") == 0)
  531. {
  532. if (--argc < 1) goto bad;
  533. named_curve = *(++argv);
  534. }
  535. #endif
  536. else if (strcmp(*argv,"-dcert") == 0)
  537. {
  538. if (--argc < 1) goto bad;
  539. s_dcert_file= *(++argv);
  540. }
  541. else if (strcmp(*argv,"-dkey") == 0)
  542. {
  543. if (--argc < 1) goto bad;
  544. s_dkey_file= *(++argv);
  545. }
  546. else if (strcmp(*argv,"-nocert") == 0)
  547. {
  548. nocert=1;
  549. }
  550. else if (strcmp(*argv,"-CApath") == 0)
  551. {
  552. if (--argc < 1) goto bad;
  553. CApath= *(++argv);
  554. }
  555. else if (strcmp(*argv,"-crl_check") == 0)
  556. {
  557. vflags |= X509_V_FLAG_CRL_CHECK;
  558. }
  559. else if (strcmp(*argv,"-crl_check") == 0)
  560. {
  561. vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  562. }
  563. else if (strcmp(*argv,"-serverpref") == 0)
  564. { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
  565. else if (strcmp(*argv,"-cipher") == 0)
  566. {
  567. if (--argc < 1) goto bad;
  568. cipher= *(++argv);
  569. }
  570. else if (strcmp(*argv,"-CAfile") == 0)
  571. {
  572. if (--argc < 1) goto bad;
  573. CAfile= *(++argv);
  574. }
  575. #ifdef FIONBIO
  576. else if (strcmp(*argv,"-nbio") == 0)
  577. { s_nbio=1; }
  578. #endif
  579. else if (strcmp(*argv,"-nbio_test") == 0)
  580. {
  581. #ifdef FIONBIO
  582. s_nbio=1;
  583. #endif
  584. s_nbio_test=1;
  585. }
  586. else if (strcmp(*argv,"-debug") == 0)
  587. { s_debug=1; }
  588. else if (strcmp(*argv,"-msg") == 0)
  589. { s_msg=1; }
  590. else if (strcmp(*argv,"-hack") == 0)
  591. { hack=1; }
  592. else if (strcmp(*argv,"-state") == 0)
  593. { state=1; }
  594. else if (strcmp(*argv,"-crlf") == 0)
  595. { s_crlf=1; }
  596. else if (strcmp(*argv,"-quiet") == 0)
  597. { s_quiet=1; }
  598. else if (strcmp(*argv,"-bugs") == 0)
  599. { bugs=1; }
  600. else if (strcmp(*argv,"-no_tmp_rsa") == 0)
  601. { no_tmp_rsa=1; }
  602. else if (strcmp(*argv,"-no_dhe") == 0)
  603. { no_dhe=1; }
  604. else if (strcmp(*argv,"-no_ecdhe") == 0)
  605. { no_ecdhe=1; }
  606. else if (strcmp(*argv,"-www") == 0)
  607. { www=1; }
  608. else if (strcmp(*argv,"-WWW") == 0)
  609. { www=2; }
  610. else if (strcmp(*argv,"-HTTP") == 0)
  611. { www=3; }
  612. else if (strcmp(*argv,"-no_ssl2") == 0)
  613. { off|=SSL_OP_NO_SSLv2; }
  614. else if (strcmp(*argv,"-no_ssl3") == 0)
  615. { off|=SSL_OP_NO_SSLv3; }
  616. else if (strcmp(*argv,"-no_tls1") == 0)
  617. { off|=SSL_OP_NO_TLSv1; }
  618. #ifndef OPENSSL_NO_SSL2
  619. else if (strcmp(*argv,"-ssl2") == 0)
  620. { meth=SSLv2_server_method(); }
  621. #endif
  622. #ifndef OPENSSL_NO_SSL3
  623. else if (strcmp(*argv,"-ssl3") == 0)
  624. { meth=SSLv3_server_method(); }
  625. #endif
  626. #ifndef OPENSSL_NO_TLS1
  627. else if (strcmp(*argv,"-tls1") == 0)
  628. { meth=TLSv1_server_method(); }
  629. #endif
  630. else if (strcmp(*argv, "-id_prefix") == 0)
  631. {
  632. if (--argc < 1) goto bad;
  633. session_id_prefix = *(++argv);
  634. }
  635. else if (strcmp(*argv,"-engine") == 0)
  636. {
  637. if (--argc < 1) goto bad;
  638. engine_id= *(++argv);
  639. }
  640. else if (strcmp(*argv,"-rand") == 0)
  641. {
  642. if (--argc < 1) goto bad;
  643. inrand= *(++argv);
  644. }
  645. else
  646. {
  647. BIO_printf(bio_err,"unknown option %s\n",*argv);
  648. badop=1;
  649. break;
  650. }
  651. argc--;
  652. argv++;
  653. }
  654. if (badop)
  655. {
  656. bad:
  657. sv_usage();
  658. goto end;
  659. }
  660. SSL_load_error_strings();
  661. OpenSSL_add_ssl_algorithms();
  662. e = setup_engine(bio_err, engine_id, 1);
  663. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  664. && !RAND_status())
  665. {
  666. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  667. }
  668. if (inrand != NULL)
  669. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  670. app_RAND_load_files(inrand));
  671. if (bio_s_out == NULL)
  672. {
  673. if (s_quiet && !s_debug && !s_msg)
  674. {
  675. bio_s_out=BIO_new(BIO_s_null());
  676. }
  677. else
  678. {
  679. if (bio_s_out == NULL)
  680. bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  681. }
  682. }
  683. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  684. if (nocert)
  685. #endif
  686. {
  687. s_cert_file=NULL;
  688. s_key_file=NULL;
  689. s_dcert_file=NULL;
  690. s_dkey_file=NULL;
  691. }
  692. ctx=SSL_CTX_new(meth);
  693. if (ctx == NULL)
  694. {
  695. ERR_print_errors(bio_err);
  696. goto end;
  697. }
  698. if (session_id_prefix)
  699. {
  700. if(strlen(session_id_prefix) >= 32)
  701. BIO_printf(bio_err,
  702. "warning: id_prefix is too long, only one new session will be possible\n");
  703. else if(strlen(session_id_prefix) >= 16)
  704. BIO_printf(bio_err,
  705. "warning: id_prefix is too long if you use SSLv2\n");
  706. if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
  707. {
  708. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  709. ERR_print_errors(bio_err);
  710. goto end;
  711. }
  712. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  713. }
  714. SSL_CTX_set_quiet_shutdown(ctx,1);
  715. if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
  716. if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  717. SSL_CTX_set_options(ctx,off);
  718. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  719. SSL_CTX_sess_set_cache_size(ctx,128);
  720. #if 0
  721. if (cipher == NULL) cipher=getenv("SSL_CIPHER");
  722. #endif
  723. #if 0
  724. if (s_cert_file == NULL)
  725. {
  726. BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
  727. goto end;
  728. }
  729. #endif
  730. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  731. (!SSL_CTX_set_default_verify_paths(ctx)))
  732. {
  733. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  734. ERR_print_errors(bio_err);
  735. /* goto end; */
  736. }
  737. store = SSL_CTX_get_cert_store(ctx);
  738. X509_STORE_set_flags(store, vflags);
  739. #ifndef OPENSSL_NO_DH
  740. if (!no_dhe)
  741. {
  742. DH *dh=NULL;
  743. if (dhfile)
  744. dh = load_dh_param(dhfile);
  745. else if (s_cert_file)
  746. dh = load_dh_param(s_cert_file);
  747. if (dh != NULL)
  748. {
  749. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  750. }
  751. else
  752. {
  753. BIO_printf(bio_s_out,"Using default temp DH parameters\n");
  754. dh=get_dh512();
  755. }
  756. (void)BIO_flush(bio_s_out);
  757. SSL_CTX_set_tmp_dh(ctx,dh);
  758. DH_free(dh);
  759. }
  760. #endif
  761. #ifndef OPENSSL_NO_ECDH
  762. if (!no_ecdhe)
  763. {
  764. EC_KEY *ecdh=NULL;
  765. ecdh = EC_KEY_new();
  766. if (ecdh == NULL)
  767. {
  768. BIO_printf(bio_err,"Could not create ECDH struct.\n");
  769. goto end;
  770. }
  771. if (named_curve)
  772. {
  773. int nid = OBJ_sn2nid(named_curve);
  774. if (nid == 0)
  775. {
  776. BIO_printf(bio_err, "unknown curve name (%s)\n",
  777. named_curve);
  778. goto end;
  779. }
  780. ecdh->group = EC_GROUP_new_by_nid(nid);
  781. if (ecdh->group == NULL)
  782. {
  783. BIO_printf(bio_err, "unable to create curve (%s)\n",
  784. named_curve);
  785. goto end;
  786. }
  787. }
  788. if (ecdh->group != NULL)
  789. {
  790. BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
  791. }
  792. else
  793. {
  794. BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
  795. ecdh->group=EC_GROUP_new_by_nid(NID_sect163r2);
  796. if (ecdh->group == NULL)
  797. {
  798. BIO_printf(bio_err, "unable to create curve (sect163r2)\n");
  799. goto end;
  800. }
  801. }
  802. (void)BIO_flush(bio_s_out);
  803. SSL_CTX_set_tmp_ecdh(ctx,ecdh);
  804. EC_KEY_free(ecdh);
  805. }
  806. #endif
  807. if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
  808. goto end;
  809. if (s_dcert_file != NULL)
  810. {
  811. if (!set_cert_stuff(ctx,s_dcert_file,s_dkey_file))
  812. goto end;
  813. }
  814. #ifndef OPENSSL_NO_RSA
  815. #if 1
  816. if (!no_tmp_rsa)
  817. SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
  818. #else
  819. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
  820. {
  821. RSA *rsa;
  822. BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
  823. BIO_flush(bio_s_out);
  824. rsa=RSA_generate_key(512,RSA_F4,NULL);
  825. if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
  826. {
  827. ERR_print_errors(bio_err);
  828. goto end;
  829. }
  830. RSA_free(rsa);
  831. BIO_printf(bio_s_out,"\n");
  832. }
  833. #endif
  834. #endif
  835. if (cipher != NULL)
  836. if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
  837. BIO_printf(bio_err,"error setting cipher list\n");
  838. ERR_print_errors(bio_err);
  839. goto end;
  840. }
  841. SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
  842. SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
  843. sizeof s_server_session_id_context);
  844. if (CAfile != NULL)
  845. SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
  846. BIO_printf(bio_s_out,"ACCEPT\n");
  847. if (www)
  848. do_server(port,&accept_socket,www_body, context);
  849. else
  850. do_server(port,&accept_socket,sv_body, context);
  851. print_stats(bio_s_out,ctx);
  852. ret=0;
  853. end:
  854. if (ctx != NULL) SSL_CTX_free(ctx);
  855. if (bio_s_out != NULL)
  856. {
  857. BIO_free(bio_s_out);
  858. bio_s_out=NULL;
  859. }
  860. apps_shutdown();
  861. EXIT(ret);
  862. }
  863. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  864. {
  865. BIO_printf(bio,"%4ld items in the session cache\n",
  866. SSL_CTX_sess_number(ssl_ctx));
  867. BIO_printf(bio,"%4d client connects (SSL_connect())\n",
  868. SSL_CTX_sess_connect(ssl_ctx));
  869. BIO_printf(bio,"%4d client renegotiates (SSL_connect())\n",
  870. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  871. BIO_printf(bio,"%4d client connects that finished\n",
  872. SSL_CTX_sess_connect_good(ssl_ctx));
  873. BIO_printf(bio,"%4d server accepts (SSL_accept())\n",
  874. SSL_CTX_sess_accept(ssl_ctx));
  875. BIO_printf(bio,"%4d server renegotiates (SSL_accept())\n",
  876. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  877. BIO_printf(bio,"%4d server accepts that finished\n",
  878. SSL_CTX_sess_accept_good(ssl_ctx));
  879. BIO_printf(bio,"%4d session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
  880. BIO_printf(bio,"%4d session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
  881. BIO_printf(bio,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
  882. BIO_printf(bio,"%4d callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
  883. BIO_printf(bio,"%4d cache full overflows (%d allowed)\n",
  884. SSL_CTX_sess_cache_full(ssl_ctx),
  885. SSL_CTX_sess_get_cache_size(ssl_ctx));
  886. }
  887. static int sv_body(char *hostname, int s, unsigned char *context)
  888. {
  889. char *buf=NULL;
  890. fd_set readfds;
  891. int ret=1,width;
  892. int k,i;
  893. unsigned long l;
  894. SSL *con=NULL;
  895. BIO *sbio;
  896. #ifdef OPENSSL_SYS_WINDOWS
  897. struct timeval tv;
  898. #endif
  899. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  900. {
  901. BIO_printf(bio_err,"out of memory\n");
  902. goto err;
  903. }
  904. #ifdef FIONBIO
  905. if (s_nbio)
  906. {
  907. unsigned long sl=1;
  908. if (!s_quiet)
  909. BIO_printf(bio_err,"turning on non blocking io\n");
  910. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  911. ERR_print_errors(bio_err);
  912. }
  913. #endif
  914. if (con == NULL) {
  915. con=SSL_new(ctx);
  916. #ifndef OPENSSL_NO_KRB5
  917. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  918. {
  919. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
  920. KRB5SVC);
  921. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
  922. KRB5KEYTAB);
  923. }
  924. #endif /* OPENSSL_NO_KRB5 */
  925. if(context)
  926. SSL_set_session_id_context(con, context,
  927. strlen((char *)context));
  928. }
  929. SSL_clear(con);
  930. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  931. if (s_nbio_test)
  932. {
  933. BIO *test;
  934. test=BIO_new(BIO_f_nbio_test());
  935. sbio=BIO_push(test,sbio);
  936. }
  937. SSL_set_bio(con,sbio,sbio);
  938. SSL_set_accept_state(con);
  939. /* SSL_set_fd(con,s); */
  940. if (s_debug)
  941. {
  942. con->debug=1;
  943. BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
  944. BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
  945. }
  946. if (s_msg)
  947. {
  948. SSL_set_msg_callback(con, msg_cb);
  949. SSL_set_msg_callback_arg(con, bio_s_out);
  950. }
  951. width=s+1;
  952. for (;;)
  953. {
  954. int read_from_terminal;
  955. int read_from_sslcon;
  956. read_from_terminal = 0;
  957. read_from_sslcon = SSL_pending(con);
  958. if (!read_from_sslcon)
  959. {
  960. FD_ZERO(&readfds);
  961. #ifndef OPENSSL_SYS_WINDOWS
  962. FD_SET(fileno(stdin),&readfds);
  963. #endif
  964. FD_SET(s,&readfds);
  965. /* Note: under VMS with SOCKETSHR the second parameter is
  966. * currently of type (int *) whereas under other systems
  967. * it is (void *) if you don't have a cast it will choke
  968. * the compiler: if you do have a cast then you can either
  969. * go for (int *) or (void *).
  970. */
  971. #ifdef OPENSSL_SYS_WINDOWS
  972. /* Under Windows we can't select on stdin: only
  973. * on sockets. As a workaround we timeout the select every
  974. * second and check for any keypress. In a proper Windows
  975. * application we wouldn't do this because it is inefficient.
  976. */
  977. tv.tv_sec = 1;
  978. tv.tv_usec = 0;
  979. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  980. if((i < 0) || (!i && !_kbhit() ) )continue;
  981. if(_kbhit())
  982. read_from_terminal = 1;
  983. #else
  984. i=select(width,(void *)&readfds,NULL,NULL,NULL);
  985. if (i <= 0) continue;
  986. if (FD_ISSET(fileno(stdin),&readfds))
  987. read_from_terminal = 1;
  988. #endif
  989. if (FD_ISSET(s,&readfds))
  990. read_from_sslcon = 1;
  991. }
  992. if (read_from_terminal)
  993. {
  994. if (s_crlf)
  995. {
  996. int j, lf_num;
  997. i=read(fileno(stdin), buf, bufsize/2);
  998. lf_num = 0;
  999. /* both loops are skipped when i <= 0 */
  1000. for (j = 0; j < i; j++)
  1001. if (buf[j] == '\n')
  1002. lf_num++;
  1003. for (j = i-1; j >= 0; j--)
  1004. {
  1005. buf[j+lf_num] = buf[j];
  1006. if (buf[j] == '\n')
  1007. {
  1008. lf_num--;
  1009. i++;
  1010. buf[j+lf_num] = '\r';
  1011. }
  1012. }
  1013. assert(lf_num == 0);
  1014. }
  1015. else
  1016. i=read(fileno(stdin),buf,bufsize);
  1017. if (!s_quiet)
  1018. {
  1019. if ((i <= 0) || (buf[0] == 'Q'))
  1020. {
  1021. BIO_printf(bio_s_out,"DONE\n");
  1022. SHUTDOWN(s);
  1023. close_accept_socket();
  1024. ret= -11;
  1025. goto err;
  1026. }
  1027. if ((i <= 0) || (buf[0] == 'q'))
  1028. {
  1029. BIO_printf(bio_s_out,"DONE\n");
  1030. SHUTDOWN(s);
  1031. /* close_accept_socket();
  1032. ret= -11;*/
  1033. goto err;
  1034. }
  1035. if ((buf[0] == 'r') &&
  1036. ((buf[1] == '\n') || (buf[1] == '\r')))
  1037. {
  1038. SSL_renegotiate(con);
  1039. i=SSL_do_handshake(con);
  1040. printf("SSL_do_handshake -> %d\n",i);
  1041. i=0; /*13; */
  1042. continue;
  1043. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  1044. }
  1045. if ((buf[0] == 'R') &&
  1046. ((buf[1] == '\n') || (buf[1] == '\r')))
  1047. {
  1048. SSL_set_verify(con,
  1049. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  1050. SSL_renegotiate(con);
  1051. i=SSL_do_handshake(con);
  1052. printf("SSL_do_handshake -> %d\n",i);
  1053. i=0; /* 13; */
  1054. continue;
  1055. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  1056. }
  1057. if (buf[0] == 'P')
  1058. {
  1059. static char *str="Lets print some clear text\n";
  1060. BIO_write(SSL_get_wbio(con),str,strlen(str));
  1061. }
  1062. if (buf[0] == 'S')
  1063. {
  1064. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  1065. }
  1066. }
  1067. #ifdef CHARSET_EBCDIC
  1068. ebcdic2ascii(buf,buf,i);
  1069. #endif
  1070. l=k=0;
  1071. for (;;)
  1072. {
  1073. /* should do a select for the write */
  1074. #ifdef RENEG
  1075. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  1076. #endif
  1077. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  1078. switch (SSL_get_error(con,k))
  1079. {
  1080. case SSL_ERROR_NONE:
  1081. break;
  1082. case SSL_ERROR_WANT_WRITE:
  1083. case SSL_ERROR_WANT_READ:
  1084. case SSL_ERROR_WANT_X509_LOOKUP:
  1085. BIO_printf(bio_s_out,"Write BLOCK\n");
  1086. break;
  1087. case SSL_ERROR_SYSCALL:
  1088. case SSL_ERROR_SSL:
  1089. BIO_printf(bio_s_out,"ERROR\n");
  1090. ERR_print_errors(bio_err);
  1091. ret=1;
  1092. goto err;
  1093. /* break; */
  1094. case SSL_ERROR_ZERO_RETURN:
  1095. BIO_printf(bio_s_out,"DONE\n");
  1096. ret=1;
  1097. goto err;
  1098. }
  1099. l+=k;
  1100. i-=k;
  1101. if (i <= 0) break;
  1102. }
  1103. }
  1104. if (read_from_sslcon)
  1105. {
  1106. if (!SSL_is_init_finished(con))
  1107. {
  1108. i=init_ssl_connection(con);
  1109. if (i < 0)
  1110. {
  1111. ret=0;
  1112. goto err;
  1113. }
  1114. else if (i == 0)
  1115. {
  1116. ret=1;
  1117. goto err;
  1118. }
  1119. }
  1120. else
  1121. {
  1122. again:
  1123. i=SSL_read(con,(char *)buf,bufsize);
  1124. switch (SSL_get_error(con,i))
  1125. {
  1126. case SSL_ERROR_NONE:
  1127. #ifdef CHARSET_EBCDIC
  1128. ascii2ebcdic(buf,buf,i);
  1129. #endif
  1130. write(fileno(stdout),buf,
  1131. (unsigned int)i);
  1132. if (SSL_pending(con)) goto again;
  1133. break;
  1134. case SSL_ERROR_WANT_WRITE:
  1135. case SSL_ERROR_WANT_READ:
  1136. case SSL_ERROR_WANT_X509_LOOKUP:
  1137. BIO_printf(bio_s_out,"Read BLOCK\n");
  1138. break;
  1139. case SSL_ERROR_SYSCALL:
  1140. case SSL_ERROR_SSL:
  1141. BIO_printf(bio_s_out,"ERROR\n");
  1142. ERR_print_errors(bio_err);
  1143. ret=1;
  1144. goto err;
  1145. case SSL_ERROR_ZERO_RETURN:
  1146. BIO_printf(bio_s_out,"DONE\n");
  1147. ret=1;
  1148. goto err;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. err:
  1154. BIO_printf(bio_s_out,"shutting down SSL\n");
  1155. #if 1
  1156. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  1157. #else
  1158. SSL_shutdown(con);
  1159. #endif
  1160. if (con != NULL) SSL_free(con);
  1161. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  1162. if (buf != NULL)
  1163. {
  1164. OPENSSL_cleanse(buf,bufsize);
  1165. OPENSSL_free(buf);
  1166. }
  1167. if (ret >= 0)
  1168. BIO_printf(bio_s_out,"ACCEPT\n");
  1169. return(ret);
  1170. }
  1171. static void close_accept_socket(void)
  1172. {
  1173. BIO_printf(bio_err,"shutdown accept socket\n");
  1174. if (accept_socket >= 0)
  1175. {
  1176. SHUTDOWN2(accept_socket);
  1177. }
  1178. }
  1179. static int init_ssl_connection(SSL *con)
  1180. {
  1181. int i;
  1182. const char *str;
  1183. X509 *peer;
  1184. long verify_error;
  1185. MS_STATIC char buf[BUFSIZ];
  1186. if ((i=SSL_accept(con)) <= 0)
  1187. {
  1188. if (BIO_sock_should_retry(i))
  1189. {
  1190. BIO_printf(bio_s_out,"DELAY\n");
  1191. return(1);
  1192. }
  1193. BIO_printf(bio_err,"ERROR\n");
  1194. verify_error=SSL_get_verify_result(con);
  1195. if (verify_error != X509_V_OK)
  1196. {
  1197. BIO_printf(bio_err,"verify error:%s\n",
  1198. X509_verify_cert_error_string(verify_error));
  1199. }
  1200. else
  1201. ERR_print_errors(bio_err);
  1202. return(0);
  1203. }
  1204. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  1205. peer=SSL_get_peer_certificate(con);
  1206. if (peer != NULL)
  1207. {
  1208. BIO_printf(bio_s_out,"Client certificate\n");
  1209. PEM_write_bio_X509(bio_s_out,peer);
  1210. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  1211. BIO_printf(bio_s_out,"subject=%s\n",buf);
  1212. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  1213. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  1214. X509_free(peer);
  1215. }
  1216. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  1217. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  1218. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  1219. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  1220. if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
  1221. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  1222. TLS1_FLAGS_TLS_PADDING_BUG)
  1223. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  1224. return(1);
  1225. }
  1226. #ifndef OPENSSL_NO_DH
  1227. static DH *load_dh_param(char *dhfile)
  1228. {
  1229. DH *ret=NULL;
  1230. BIO *bio;
  1231. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  1232. goto err;
  1233. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  1234. err:
  1235. if (bio != NULL) BIO_free(bio);
  1236. return(ret);
  1237. }
  1238. #endif
  1239. #if 0
  1240. static int load_CA(SSL_CTX *ctx, char *file)
  1241. {
  1242. FILE *in;
  1243. X509 *x=NULL;
  1244. if ((in=fopen(file,"r")) == NULL)
  1245. return(0);
  1246. for (;;)
  1247. {
  1248. if (PEM_read_X509(in,&x,NULL) == NULL)
  1249. break;
  1250. SSL_CTX_add_client_CA(ctx,x);
  1251. }
  1252. if (x != NULL) X509_free(x);
  1253. fclose(in);
  1254. return(1);
  1255. }
  1256. #endif
  1257. static int www_body(char *hostname, int s, unsigned char *context)
  1258. {
  1259. char *buf=NULL;
  1260. int ret=1;
  1261. int i,j,k,blank,dot;
  1262. struct stat st_buf;
  1263. SSL *con;
  1264. SSL_CIPHER *c;
  1265. BIO *io,*ssl_bio,*sbio;
  1266. long total_bytes;
  1267. buf=OPENSSL_malloc(bufsize);
  1268. if (buf == NULL) return(0);
  1269. io=BIO_new(BIO_f_buffer());
  1270. ssl_bio=BIO_new(BIO_f_ssl());
  1271. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  1272. #ifdef FIONBIO
  1273. if (s_nbio)
  1274. {
  1275. unsigned long sl=1;
  1276. if (!s_quiet)
  1277. BIO_printf(bio_err,"turning on non blocking io\n");
  1278. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1279. ERR_print_errors(bio_err);
  1280. }
  1281. #endif
  1282. /* lets make the output buffer a reasonable size */
  1283. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  1284. if ((con=SSL_new(ctx)) == NULL) goto err;
  1285. #ifndef OPENSSL_NO_KRB5
  1286. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1287. {
  1288. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
  1289. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
  1290. }
  1291. #endif /* OPENSSL_NO_KRB5 */
  1292. if(context) SSL_set_session_id_context(con, context,
  1293. strlen((char *)context));
  1294. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1295. if (s_nbio_test)
  1296. {
  1297. BIO *test;
  1298. test=BIO_new(BIO_f_nbio_test());
  1299. sbio=BIO_push(test,sbio);
  1300. }
  1301. SSL_set_bio(con,sbio,sbio);
  1302. SSL_set_accept_state(con);
  1303. /* SSL_set_fd(con,s); */
  1304. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  1305. BIO_push(io,ssl_bio);
  1306. #ifdef CHARSET_EBCDIC
  1307. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  1308. #endif
  1309. if (s_debug)
  1310. {
  1311. con->debug=1;
  1312. BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
  1313. BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
  1314. }
  1315. if (s_msg)
  1316. {
  1317. SSL_set_msg_callback(con, msg_cb);
  1318. SSL_set_msg_callback_arg(con, bio_s_out);
  1319. }
  1320. blank=0;
  1321. for (;;)
  1322. {
  1323. if (hack)
  1324. {
  1325. i=SSL_accept(con);
  1326. switch (SSL_get_error(con,i))
  1327. {
  1328. case SSL_ERROR_NONE:
  1329. break;
  1330. case SSL_ERROR_WANT_WRITE:
  1331. case SSL_ERROR_WANT_READ:
  1332. case SSL_ERROR_WANT_X509_LOOKUP:
  1333. continue;
  1334. case SSL_ERROR_SYSCALL:
  1335. case SSL_ERROR_SSL:
  1336. case SSL_ERROR_ZERO_RETURN:
  1337. ret=1;
  1338. goto err;
  1339. /* break; */
  1340. }
  1341. SSL_renegotiate(con);
  1342. SSL_write(con,NULL,0);
  1343. }
  1344. i=BIO_gets(io,buf,bufsize-1);
  1345. if (i < 0) /* error */
  1346. {
  1347. if (!BIO_should_retry(io))
  1348. {
  1349. if (!s_quiet)
  1350. ERR_print_errors(bio_err);
  1351. goto err;
  1352. }
  1353. else
  1354. {
  1355. BIO_printf(bio_s_out,"read R BLOCK\n");
  1356. #if !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  1357. sleep(1);
  1358. #endif
  1359. continue;
  1360. }
  1361. }
  1362. else if (i == 0) /* end of input */
  1363. {
  1364. ret=1;
  1365. goto end;
  1366. }
  1367. /* else we have data */
  1368. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  1369. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  1370. {
  1371. char *p;
  1372. X509 *peer;
  1373. STACK_OF(SSL_CIPHER) *sk;
  1374. static char *space=" ";
  1375. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  1376. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  1377. BIO_puts(io,"<pre>\n");
  1378. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  1379. BIO_puts(io,"\n");
  1380. for (i=0; i<local_argc; i++)
  1381. {
  1382. BIO_puts(io,local_argv[i]);
  1383. BIO_write(io," ",1);
  1384. }
  1385. BIO_puts(io,"\n");
  1386. /* The following is evil and should not really
  1387. * be done */
  1388. BIO_printf(io,"Ciphers supported in s_server binary\n");
  1389. sk=SSL_get_ciphers(con);
  1390. j=sk_SSL_CIPHER_num(sk);
  1391. for (i=0; i<j; i++)
  1392. {
  1393. c=sk_SSL_CIPHER_value(sk,i);
  1394. BIO_printf(io,"%-11s:%-25s",
  1395. SSL_CIPHER_get_version(c),
  1396. SSL_CIPHER_get_name(c));
  1397. if ((((i+1)%2) == 0) && (i+1 != j))
  1398. BIO_puts(io,"\n");
  1399. }
  1400. BIO_puts(io,"\n");
  1401. p=SSL_get_shared_ciphers(con,buf,bufsize);
  1402. if (p != NULL)
  1403. {
  1404. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  1405. j=i=0;
  1406. while (*p)
  1407. {
  1408. if (*p == ':')
  1409. {
  1410. BIO_write(io,space,26-j);
  1411. i++;
  1412. j=0;
  1413. BIO_write(io,((i%3)?" ":"\n"),1);
  1414. }
  1415. else
  1416. {
  1417. BIO_write(io,p,1);
  1418. j++;
  1419. }
  1420. p++;
  1421. }
  1422. BIO_puts(io,"\n");
  1423. }
  1424. BIO_printf(io,((con->hit)
  1425. ?"---\nReused, "
  1426. :"---\nNew, "));
  1427. c=SSL_get_current_cipher(con);
  1428. BIO_printf(io,"%s, Cipher is %s\n",
  1429. SSL_CIPHER_get_version(c),
  1430. SSL_CIPHER_get_name(c));
  1431. SSL_SESSION_print(io,SSL_get_session(con));
  1432. BIO_printf(io,"---\n");
  1433. print_stats(io,SSL_get_SSL_CTX(con));
  1434. BIO_printf(io,"---\n");
  1435. peer=SSL_get_peer_certificate(con);
  1436. if (peer != NULL)
  1437. {
  1438. BIO_printf(io,"Client certificate\n");
  1439. X509_print(io,peer);
  1440. PEM_write_bio_X509(io,peer);
  1441. }
  1442. else
  1443. BIO_puts(io,"no client certificate available\n");
  1444. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  1445. break;
  1446. }
  1447. else if ((www == 2 || www == 3)
  1448. && (strncmp("GET /",buf,5) == 0))
  1449. {
  1450. BIO *file;
  1451. char *p,*e;
  1452. static char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  1453. /* skip the '/' */
  1454. p= &(buf[5]);
  1455. dot = 1;
  1456. for (e=p; *e != '\0'; e++)
  1457. {
  1458. if (e[0] == ' ')
  1459. break;
  1460. switch (dot)
  1461. {
  1462. case 1:
  1463. dot = (e[0] == '.') ? 2 : 0;
  1464. break;
  1465. case 2:
  1466. dot = (e[0] == '.') ? 3 : 0;
  1467. break;
  1468. case 3:
  1469. dot = (e[0] == '/') ? -1 : 0;
  1470. break;
  1471. }
  1472. if (dot == 0)
  1473. dot = (e[0] == '/') ? 1 : 0;
  1474. }
  1475. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  1476. if (*e == '\0')
  1477. {
  1478. BIO_puts(io,text);
  1479. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  1480. break;
  1481. }
  1482. *e='\0';
  1483. if (dot)
  1484. {
  1485. BIO_puts(io,text);
  1486. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  1487. break;
  1488. }
  1489. if (*p == '/')
  1490. {
  1491. BIO_puts(io,text);
  1492. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  1493. break;
  1494. }
  1495. #if 0
  1496. /* append if a directory lookup */
  1497. if (e[-1] == '/')
  1498. strcat(p,"index.html");
  1499. #endif
  1500. /* if a directory, do the index thang */
  1501. if (stat(p,&st_buf) < 0)
  1502. {
  1503. BIO_puts(io,text);
  1504. BIO_printf(io,"Error accessing '%s'\r\n",p);
  1505. ERR_print_errors(io);
  1506. break;
  1507. }
  1508. if (S_ISDIR(st_buf.st_mode))
  1509. {
  1510. #if 0 /* must check buffer size */
  1511. strcat(p,"/index.html");
  1512. #else
  1513. BIO_puts(io,text);
  1514. BIO_printf(io,"'%s' is a directory\r\n",p);
  1515. break;
  1516. #endif
  1517. }
  1518. if ((file=BIO_new_file(p,"r")) == NULL)
  1519. {
  1520. BIO_puts(io,text);
  1521. BIO_printf(io,"Error opening '%s'\r\n",p);
  1522. ERR_print_errors(io);
  1523. break;
  1524. }
  1525. if (!s_quiet)
  1526. BIO_printf(bio_err,"FILE:%s\n",p);
  1527. if (www == 2)
  1528. {
  1529. i=strlen(p);
  1530. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  1531. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  1532. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  1533. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  1534. else
  1535. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  1536. }
  1537. /* send the file */
  1538. total_bytes=0;
  1539. for (;;)
  1540. {
  1541. i=BIO_read(file,buf,bufsize);
  1542. if (i <= 0) break;
  1543. #ifdef RENEG
  1544. total_bytes+=i;
  1545. fprintf(stderr,"%d\n",i);
  1546. if (total_bytes > 3*1024)
  1547. {
  1548. total_bytes=0;
  1549. fprintf(stderr,"RENEGOTIATE\n");
  1550. SSL_renegotiate(con);
  1551. }
  1552. #endif
  1553. for (j=0; j<i; )
  1554. {
  1555. #ifdef RENEG
  1556. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  1557. #endif
  1558. k=BIO_write(io,&(buf[j]),i-j);
  1559. if (k <= 0)
  1560. {
  1561. if (!BIO_should_retry(io))
  1562. goto write_error;
  1563. else
  1564. {
  1565. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  1566. }
  1567. }
  1568. else
  1569. {
  1570. j+=k;
  1571. }
  1572. }
  1573. }
  1574. write_error:
  1575. BIO_free(file);
  1576. break;
  1577. }
  1578. }
  1579. for (;;)
  1580. {
  1581. i=(int)BIO_flush(io);
  1582. if (i <= 0)
  1583. {
  1584. if (!BIO_should_retry(io))
  1585. break;
  1586. }
  1587. else
  1588. break;
  1589. }
  1590. end:
  1591. #if 1
  1592. /* make sure we re-use sessions */
  1593. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  1594. #else
  1595. /* This kills performance */
  1596. /* SSL_shutdown(con); A shutdown gets sent in the
  1597. * BIO_free_all(io) procession */
  1598. #endif
  1599. err:
  1600. if (ret >= 0)
  1601. BIO_printf(bio_s_out,"ACCEPT\n");
  1602. if (buf != NULL) OPENSSL_free(buf);
  1603. if (io != NULL) BIO_free_all(io);
  1604. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  1605. return(ret);
  1606. }
  1607. #ifndef OPENSSL_NO_RSA
  1608. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  1609. {
  1610. static RSA *rsa_tmp=NULL;
  1611. if (rsa_tmp == NULL)
  1612. {
  1613. if (!s_quiet)
  1614. {
  1615. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  1616. (void)BIO_flush(bio_err);
  1617. }
  1618. rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
  1619. if (!s_quiet)
  1620. {
  1621. BIO_printf(bio_err,"\n");
  1622. (void)BIO_flush(bio_err);
  1623. }
  1624. }
  1625. return(rsa_tmp);
  1626. }
  1627. #endif
  1628. #define MAX_SESSION_ID_ATTEMPTS 10
  1629. static int generate_session_id(const SSL *ssl, unsigned char *id,
  1630. unsigned int *id_len)
  1631. {
  1632. unsigned int count = 0;
  1633. do {
  1634. RAND_pseudo_bytes(id, *id_len);
  1635. /* Prefix the session_id with the required prefix. NB: If our
  1636. * prefix is too long, clip it - but there will be worse effects
  1637. * anyway, eg. the server could only possibly create 1 session
  1638. * ID (ie. the prefix!) so all future session negotiations will
  1639. * fail due to conflicts. */
  1640. memcpy(id, session_id_prefix,
  1641. (strlen(session_id_prefix) < *id_len) ?
  1642. strlen(session_id_prefix) : *id_len);
  1643. }
  1644. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  1645. (++count < MAX_SESSION_ID_ATTEMPTS));
  1646. if(count >= MAX_SESSION_ID_ATTEMPTS)
  1647. return 0;
  1648. return 1;
  1649. }