s_server.c 37 KB

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