2
0

s_server.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. /* apps/s_server.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. /* ====================================================================
  117. * Copyright 2005 Nokia. All rights reserved.
  118. *
  119. * The portions of the attached software ("Contribution") is developed by
  120. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  121. * license.
  122. *
  123. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  124. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  125. * support (see RFC 4279) to OpenSSL.
  126. *
  127. * No patent licenses or other rights except those expressly stated in
  128. * the OpenSSL open source license shall be deemed granted or received
  129. * expressly, by implication, estoppel, or otherwise.
  130. *
  131. * No assurances are provided by Nokia that the Contribution does not
  132. * infringe the patent or other intellectual property rights of any third
  133. * party or that the license provides you with all the necessary rights
  134. * to make use of the Contribution.
  135. *
  136. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  137. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  138. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  139. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  140. * OTHERWISE.
  141. */
  142. /* Until the key-gen callbacks are modified to use newer prototypes, we allow
  143. * deprecated functions for openssl-internal code */
  144. #ifdef OPENSSL_NO_DEPRECATED
  145. #undef OPENSSL_NO_DEPRECATED
  146. #endif
  147. #include <assert.h>
  148. #include <ctype.h>
  149. #include <stdio.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #include <openssl/e_os2.h>
  153. #ifdef OPENSSL_NO_STDIO
  154. #define APPS_WIN16
  155. #endif
  156. #if !defined(OPENSSL_SYS_NETWARE) /* conflicts with winsock2 stuff on netware */
  157. #include <sys/types.h>
  158. #endif
  159. /* With IPv6, it looks like Digital has mixed up the proper order of
  160. recursive header file inclusion, resulting in the compiler complaining
  161. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  162. is needed to have fileno() declared correctly... So let's define u_int */
  163. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  164. #define __U_INT
  165. typedef unsigned int u_int;
  166. #endif
  167. #include <openssl/lhash.h>
  168. #include <openssl/bn.h>
  169. #define USE_SOCKETS
  170. #include "apps.h"
  171. #include <openssl/err.h>
  172. #include <openssl/pem.h>
  173. #include <openssl/x509.h>
  174. #include <openssl/ssl.h>
  175. #include <openssl/rand.h>
  176. #ifndef OPENSSL_NO_DH
  177. #include <openssl/dh.h>
  178. #endif
  179. #ifndef OPENSSL_NO_RSA
  180. #include <openssl/rsa.h>
  181. #endif
  182. #include "s_apps.h"
  183. #include "timeouts.h"
  184. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  185. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  186. #undef FIONBIO
  187. #endif
  188. #if defined(OPENSSL_SYS_BEOS_R5)
  189. #include <fcntl.h>
  190. #endif
  191. #ifndef OPENSSL_NO_RSA
  192. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  193. #endif
  194. static int sv_body(char *hostname, int s, unsigned char *context);
  195. static int www_body(char *hostname, int s, unsigned char *context);
  196. static void close_accept_socket(void );
  197. static void sv_usage(void);
  198. static int init_ssl_connection(SSL *s);
  199. static void print_stats(BIO *bp,SSL_CTX *ctx);
  200. static int generate_session_id(const SSL *ssl, unsigned char *id,
  201. unsigned int *id_len);
  202. #ifndef OPENSSL_NO_DH
  203. static DH *load_dh_param(const char *dhfile);
  204. static DH *get_dh512(void);
  205. #endif
  206. #ifdef MONOLITH
  207. static void s_server_init(void);
  208. #endif
  209. #ifndef OPENSSL_NO_DH
  210. static unsigned char dh512_p[]={
  211. 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
  212. 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
  213. 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
  214. 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
  215. 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
  216. 0x47,0x74,0xE8,0x33,
  217. };
  218. static unsigned char dh512_g[]={
  219. 0x02,
  220. };
  221. static DH *get_dh512(void)
  222. {
  223. DH *dh=NULL;
  224. if ((dh=DH_new()) == NULL) return(NULL);
  225. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  226. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  227. if ((dh->p == NULL) || (dh->g == NULL))
  228. return(NULL);
  229. return(dh);
  230. }
  231. #endif
  232. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  233. #undef BUFSIZZ
  234. #define BUFSIZZ 16*1024
  235. static int bufsize=BUFSIZZ;
  236. static int accept_socket= -1;
  237. #define TEST_CERT "server.pem"
  238. #ifndef OPENSSL_NO_TLSEXT
  239. #define TEST_CERT2 "server2.pem"
  240. #endif
  241. #undef PROG
  242. #define PROG s_server_main
  243. extern int verify_depth;
  244. static char *cipher=NULL;
  245. static int s_server_verify=SSL_VERIFY_NONE;
  246. static int s_server_session_id_context = 1; /* anything will do */
  247. static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
  248. #ifndef OPENSSL_NO_TLSEXT
  249. static const char *s_cert_file2=TEST_CERT2,*s_key_file2=NULL;
  250. #endif
  251. static char *s_dcert_file=NULL,*s_dkey_file=NULL;
  252. #ifdef FIONBIO
  253. static int s_nbio=0;
  254. #endif
  255. static int s_nbio_test=0;
  256. int s_crlf=0;
  257. static SSL_CTX *ctx=NULL;
  258. #ifndef OPENSSL_NO_TLSEXT
  259. static SSL_CTX *ctx2=NULL;
  260. #endif
  261. static int www=0;
  262. static BIO *bio_s_out=NULL;
  263. static int s_debug=0;
  264. static int s_msg=0;
  265. static int s_quiet=0;
  266. static int hack=0;
  267. #ifndef OPENSSL_NO_ENGINE
  268. static char *engine_id=NULL;
  269. #endif
  270. static const char *session_id_prefix=NULL;
  271. static int enable_timeouts = 0;
  272. static long socket_mtu;
  273. static int cert_chain = 0;
  274. #ifndef OPENSSL_NO_PSK
  275. static char *psk_identity="Client_identity";
  276. static char *psk_key=NULL; /* by default PSK is not used */
  277. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  278. unsigned char *psk, unsigned int max_psk_len)
  279. {
  280. unsigned int psk_len = 0;
  281. int ret;
  282. BIGNUM *bn = NULL;
  283. if (s_debug)
  284. BIO_printf(bio_s_out,"psk_server_cb\n");
  285. if (!identity)
  286. {
  287. BIO_printf(bio_err,"Error: client did not send PSK identity\n");
  288. goto out_err;
  289. }
  290. if (s_debug)
  291. BIO_printf(bio_s_out,"identity_len=%d identity=%s\n",
  292. identity ? strlen(identity) : 0, identity);
  293. /* here we could lookup the given identity e.g. from a database */
  294. if (strcmp(identity, psk_identity) != 0)
  295. {
  296. BIO_printf(bio_s_out, "PSK error: client identity not found\n");
  297. goto out_err;
  298. }
  299. if (s_debug)
  300. BIO_printf(bio_s_out, "PSK client identity found\n");
  301. /* convert the PSK key to binary */
  302. ret = BN_hex2bn(&bn, psk_key);
  303. if (!ret)
  304. {
  305. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  306. if (bn)
  307. BN_free(bn);
  308. return 0;
  309. }
  310. if (BN_num_bytes(bn) > (int)max_psk_len)
  311. {
  312. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  313. max_psk_len, BN_num_bytes(bn));
  314. BN_free(bn);
  315. return 0;
  316. }
  317. ret = BN_bn2bin(bn, psk);
  318. BN_free(bn);
  319. if (ret < 0)
  320. goto out_err;
  321. psk_len = (unsigned int)ret;
  322. if (s_debug)
  323. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  324. return psk_len;
  325. out_err:
  326. if (s_debug)
  327. BIO_printf(bio_err, "Error in PSK server callback\n");
  328. return 0;
  329. }
  330. #endif
  331. #ifdef MONOLITH
  332. static void s_server_init(void)
  333. {
  334. accept_socket=-1;
  335. cipher=NULL;
  336. s_server_verify=SSL_VERIFY_NONE;
  337. s_dcert_file=NULL;
  338. s_dkey_file=NULL;
  339. s_cert_file=TEST_CERT;
  340. s_key_file=NULL;
  341. #ifndef OPENSSL_NO_TLSEXT
  342. s_cert_file2=TEST_CERT2;
  343. s_key_file2=NULL;
  344. ctx2=NULL;
  345. #endif
  346. #ifdef FIONBIO
  347. s_nbio=0;
  348. #endif
  349. s_nbio_test=0;
  350. ctx=NULL;
  351. www=0;
  352. bio_s_out=NULL;
  353. s_debug=0;
  354. s_msg=0;
  355. s_quiet=0;
  356. hack=0;
  357. #ifndef OPENSSL_NO_ENGINE
  358. engine_id=NULL;
  359. #endif
  360. }
  361. #endif
  362. static void sv_usage(void)
  363. {
  364. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  365. BIO_printf(bio_err,"\n");
  366. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  367. BIO_printf(bio_err," -context arg - set session ID context\n");
  368. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  369. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  370. BIO_printf(bio_err," -cert arg - certificate file to use\n");
  371. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  372. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  373. BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n");
  374. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  375. BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  376. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  377. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  378. BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n");
  379. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  380. BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  381. BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n");
  382. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  383. BIO_printf(bio_err," or a default set of parameters is used\n");
  384. #ifndef OPENSSL_NO_ECDH
  385. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  386. " Use \"openssl ecparam -list_curves\" for all names\n" \
  387. " (default is nistp256).\n");
  388. #endif
  389. #ifdef FIONBIO
  390. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  391. #endif
  392. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  393. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  394. BIO_printf(bio_err," -debug - Print more output\n");
  395. BIO_printf(bio_err," -msg - Show protocol messages\n");
  396. BIO_printf(bio_err," -state - Print the SSL states\n");
  397. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  398. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  399. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  400. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  401. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  402. BIO_printf(bio_err," -quiet - No server output\n");
  403. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  404. #ifndef OPENSSL_NO_PSK
  405. BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n");
  406. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  407. #endif
  408. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  409. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  410. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  411. BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n");
  412. BIO_printf(bio_err," -timeout - Enable timeouts\n");
  413. BIO_printf(bio_err," -mtu - Set MTU\n");
  414. BIO_printf(bio_err," -chain - Read a certificate chain\n");
  415. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  416. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  417. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  418. #ifndef OPENSSL_NO_DH
  419. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  420. #endif
  421. #ifndef OPENSSL_NO_ECDH
  422. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  423. #endif
  424. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  425. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  426. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  427. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  428. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  429. #ifndef OPENSSL_NO_ENGINE
  430. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  431. #endif
  432. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  433. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  434. #ifndef OPENSSL_NO_TLSEXT
  435. BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n");
  436. BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  437. BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n");
  438. BIO_printf(bio_err," (default is %s)\n",TEST_CERT2);
  439. BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n");
  440. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2);
  441. #endif
  442. }
  443. static int local_argc=0;
  444. static char **local_argv;
  445. #ifdef CHARSET_EBCDIC
  446. static int ebcdic_new(BIO *bi);
  447. static int ebcdic_free(BIO *a);
  448. static int ebcdic_read(BIO *b, char *out, int outl);
  449. static int ebcdic_write(BIO *b, const char *in, int inl);
  450. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  451. static int ebcdic_gets(BIO *bp, char *buf, int size);
  452. static int ebcdic_puts(BIO *bp, const char *str);
  453. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  454. static BIO_METHOD methods_ebcdic=
  455. {
  456. BIO_TYPE_EBCDIC_FILTER,
  457. "EBCDIC/ASCII filter",
  458. ebcdic_write,
  459. ebcdic_read,
  460. ebcdic_puts,
  461. ebcdic_gets,
  462. ebcdic_ctrl,
  463. ebcdic_new,
  464. ebcdic_free,
  465. };
  466. typedef struct
  467. {
  468. size_t alloced;
  469. char buff[1];
  470. } EBCDIC_OUTBUFF;
  471. BIO_METHOD *BIO_f_ebcdic_filter()
  472. {
  473. return(&methods_ebcdic);
  474. }
  475. static int ebcdic_new(BIO *bi)
  476. {
  477. EBCDIC_OUTBUFF *wbuf;
  478. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  479. wbuf->alloced = 1024;
  480. wbuf->buff[0] = '\0';
  481. bi->ptr=(char *)wbuf;
  482. bi->init=1;
  483. bi->flags=0;
  484. return(1);
  485. }
  486. static int ebcdic_free(BIO *a)
  487. {
  488. if (a == NULL) return(0);
  489. if (a->ptr != NULL)
  490. OPENSSL_free(a->ptr);
  491. a->ptr=NULL;
  492. a->init=0;
  493. a->flags=0;
  494. return(1);
  495. }
  496. static int ebcdic_read(BIO *b, char *out, int outl)
  497. {
  498. int ret=0;
  499. if (out == NULL || outl == 0) return(0);
  500. if (b->next_bio == NULL) return(0);
  501. ret=BIO_read(b->next_bio,out,outl);
  502. if (ret > 0)
  503. ascii2ebcdic(out,out,ret);
  504. return(ret);
  505. }
  506. static int ebcdic_write(BIO *b, const char *in, int inl)
  507. {
  508. EBCDIC_OUTBUFF *wbuf;
  509. int ret=0;
  510. int num;
  511. unsigned char n;
  512. if ((in == NULL) || (inl <= 0)) return(0);
  513. if (b->next_bio == NULL) return(0);
  514. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  515. if (inl > (num = wbuf->alloced))
  516. {
  517. num = num + num; /* double the size */
  518. if (num < inl)
  519. num = inl;
  520. OPENSSL_free(wbuf);
  521. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  522. wbuf->alloced = num;
  523. wbuf->buff[0] = '\0';
  524. b->ptr=(char *)wbuf;
  525. }
  526. ebcdic2ascii(wbuf->buff, in, inl);
  527. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  528. return(ret);
  529. }
  530. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  531. {
  532. long ret;
  533. if (b->next_bio == NULL) return(0);
  534. switch (cmd)
  535. {
  536. case BIO_CTRL_DUP:
  537. ret=0L;
  538. break;
  539. default:
  540. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  541. break;
  542. }
  543. return(ret);
  544. }
  545. static int ebcdic_gets(BIO *bp, char *buf, int size)
  546. {
  547. int i, ret=0;
  548. if (bp->next_bio == NULL) return(0);
  549. /* return(BIO_gets(bp->next_bio,buf,size));*/
  550. for (i=0; i<size-1; ++i)
  551. {
  552. ret = ebcdic_read(bp,&buf[i],1);
  553. if (ret <= 0)
  554. break;
  555. else if (buf[i] == '\n')
  556. {
  557. ++i;
  558. break;
  559. }
  560. }
  561. if (i < size)
  562. buf[i] = '\0';
  563. return (ret < 0 && i == 0) ? ret : i;
  564. }
  565. static int ebcdic_puts(BIO *bp, const char *str)
  566. {
  567. if (bp->next_bio == NULL) return(0);
  568. return ebcdic_write(bp, str, strlen(str));
  569. }
  570. #endif
  571. #ifndef OPENSSL_NO_TLSEXT
  572. /* This is a context that we pass to callbacks */
  573. typedef struct tlsextctx_st {
  574. char * servername;
  575. BIO * biodebug;
  576. int extension_error;
  577. } tlsextctx;
  578. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  579. {
  580. tlsextctx * p = (tlsextctx *) arg;
  581. const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  582. if (servername && p->biodebug)
  583. BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername);
  584. if (!p->servername)
  585. return SSL_TLSEXT_ERR_NOACK;
  586. if (servername)
  587. {
  588. if (strcmp(servername,p->servername))
  589. return p->extension_error;
  590. if (ctx2)
  591. {
  592. BIO_printf(p->biodebug,"Swiching server context.\n");
  593. SSL_set_SSL_CTX(s,ctx2);
  594. }
  595. }
  596. return SSL_TLSEXT_ERR_OK;
  597. }
  598. #endif
  599. int MAIN(int, char **);
  600. int MAIN(int argc, char *argv[])
  601. {
  602. X509_STORE *store = NULL;
  603. int vflags = 0;
  604. short port=PORT;
  605. char *CApath=NULL,*CAfile=NULL;
  606. unsigned char *context = NULL;
  607. char *dhfile = NULL;
  608. #ifndef OPENSSL_NO_ECDH
  609. char *named_curve = NULL;
  610. #endif
  611. int badop=0,bugs=0;
  612. int ret=1;
  613. int off=0;
  614. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  615. int state=0;
  616. const SSL_METHOD *meth=NULL;
  617. int socket_type=SOCK_STREAM;
  618. #ifndef OPENSSL_NO_ENGINE
  619. ENGINE *e=NULL;
  620. #endif
  621. char *inrand=NULL;
  622. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  623. char *passarg = NULL, *pass = NULL;
  624. char *dpassarg = NULL, *dpass = NULL;
  625. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  626. X509 *s_cert = NULL, *s_dcert = NULL;
  627. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  628. #ifndef OPENSSL_NO_TLSEXT
  629. EVP_PKEY *s_key2 = NULL;
  630. X509 *s_cert2 = NULL;
  631. #endif
  632. #ifndef OPENSSL_NO_TLSEXT
  633. tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
  634. #endif
  635. #ifndef OPENSSL_NO_PSK
  636. /* by default do not send a PSK identity hint */
  637. static char *psk_identity_hint=NULL;
  638. #endif
  639. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  640. meth=SSLv23_server_method();
  641. #elif !defined(OPENSSL_NO_SSL3)
  642. meth=SSLv3_server_method();
  643. #elif !defined(OPENSSL_NO_SSL2)
  644. meth=SSLv2_server_method();
  645. #endif
  646. local_argc=argc;
  647. local_argv=argv;
  648. apps_startup();
  649. #ifdef MONOLITH
  650. s_server_init();
  651. #endif
  652. if (bio_err == NULL)
  653. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  654. if (!load_config(bio_err, NULL))
  655. goto end;
  656. verify_depth=0;
  657. #ifdef FIONBIO
  658. s_nbio=0;
  659. #endif
  660. s_nbio_test=0;
  661. argc--;
  662. argv++;
  663. while (argc >= 1)
  664. {
  665. if ((strcmp(*argv,"-port") == 0) ||
  666. (strcmp(*argv,"-accept") == 0))
  667. {
  668. if (--argc < 1) goto bad;
  669. if (!extract_port(*(++argv),&port))
  670. goto bad;
  671. }
  672. else if (strcmp(*argv,"-verify") == 0)
  673. {
  674. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  675. if (--argc < 1) goto bad;
  676. verify_depth=atoi(*(++argv));
  677. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  678. }
  679. else if (strcmp(*argv,"-Verify") == 0)
  680. {
  681. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  682. SSL_VERIFY_CLIENT_ONCE;
  683. if (--argc < 1) goto bad;
  684. verify_depth=atoi(*(++argv));
  685. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  686. }
  687. else if (strcmp(*argv,"-context") == 0)
  688. {
  689. if (--argc < 1) goto bad;
  690. context= (unsigned char *)*(++argv);
  691. }
  692. else if (strcmp(*argv,"-cert") == 0)
  693. {
  694. if (--argc < 1) goto bad;
  695. s_cert_file= *(++argv);
  696. }
  697. else if (strcmp(*argv,"-certform") == 0)
  698. {
  699. if (--argc < 1) goto bad;
  700. s_cert_format = str2fmt(*(++argv));
  701. }
  702. else if (strcmp(*argv,"-key") == 0)
  703. {
  704. if (--argc < 1) goto bad;
  705. s_key_file= *(++argv);
  706. }
  707. else if (strcmp(*argv,"-keyform") == 0)
  708. {
  709. if (--argc < 1) goto bad;
  710. s_key_format = str2fmt(*(++argv));
  711. }
  712. else if (strcmp(*argv,"-pass") == 0)
  713. {
  714. if (--argc < 1) goto bad;
  715. passarg = *(++argv);
  716. }
  717. else if (strcmp(*argv,"-dhparam") == 0)
  718. {
  719. if (--argc < 1) goto bad;
  720. dhfile = *(++argv);
  721. }
  722. #ifndef OPENSSL_NO_ECDH
  723. else if (strcmp(*argv,"-named_curve") == 0)
  724. {
  725. if (--argc < 1) goto bad;
  726. named_curve = *(++argv);
  727. }
  728. #endif
  729. else if (strcmp(*argv,"-dcertform") == 0)
  730. {
  731. if (--argc < 1) goto bad;
  732. s_dcert_format = str2fmt(*(++argv));
  733. }
  734. else if (strcmp(*argv,"-dcert") == 0)
  735. {
  736. if (--argc < 1) goto bad;
  737. s_dcert_file= *(++argv);
  738. }
  739. else if (strcmp(*argv,"-dkeyform") == 0)
  740. {
  741. if (--argc < 1) goto bad;
  742. s_dkey_format = str2fmt(*(++argv));
  743. }
  744. else if (strcmp(*argv,"-dpass") == 0)
  745. {
  746. if (--argc < 1) goto bad;
  747. dpassarg = *(++argv);
  748. }
  749. else if (strcmp(*argv,"-dkey") == 0)
  750. {
  751. if (--argc < 1) goto bad;
  752. s_dkey_file= *(++argv);
  753. }
  754. else if (strcmp(*argv,"-nocert") == 0)
  755. {
  756. nocert=1;
  757. }
  758. else if (strcmp(*argv,"-CApath") == 0)
  759. {
  760. if (--argc < 1) goto bad;
  761. CApath= *(++argv);
  762. }
  763. else if (strcmp(*argv,"-crl_check") == 0)
  764. {
  765. vflags |= X509_V_FLAG_CRL_CHECK;
  766. }
  767. else if (strcmp(*argv,"-crl_check") == 0)
  768. {
  769. vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  770. }
  771. else if (strcmp(*argv,"-serverpref") == 0)
  772. { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
  773. else if (strcmp(*argv,"-cipher") == 0)
  774. {
  775. if (--argc < 1) goto bad;
  776. cipher= *(++argv);
  777. }
  778. else if (strcmp(*argv,"-CAfile") == 0)
  779. {
  780. if (--argc < 1) goto bad;
  781. CAfile= *(++argv);
  782. }
  783. #ifdef FIONBIO
  784. else if (strcmp(*argv,"-nbio") == 0)
  785. { s_nbio=1; }
  786. #endif
  787. else if (strcmp(*argv,"-nbio_test") == 0)
  788. {
  789. #ifdef FIONBIO
  790. s_nbio=1;
  791. #endif
  792. s_nbio_test=1;
  793. }
  794. else if (strcmp(*argv,"-debug") == 0)
  795. { s_debug=1; }
  796. else if (strcmp(*argv,"-msg") == 0)
  797. { s_msg=1; }
  798. else if (strcmp(*argv,"-hack") == 0)
  799. { hack=1; }
  800. else if (strcmp(*argv,"-state") == 0)
  801. { state=1; }
  802. else if (strcmp(*argv,"-crlf") == 0)
  803. { s_crlf=1; }
  804. else if (strcmp(*argv,"-quiet") == 0)
  805. { s_quiet=1; }
  806. else if (strcmp(*argv,"-bugs") == 0)
  807. { bugs=1; }
  808. else if (strcmp(*argv,"-no_tmp_rsa") == 0)
  809. { no_tmp_rsa=1; }
  810. else if (strcmp(*argv,"-no_dhe") == 0)
  811. { no_dhe=1; }
  812. else if (strcmp(*argv,"-no_ecdhe") == 0)
  813. { no_ecdhe=1; }
  814. #ifndef OPENSSL_NO_PSK
  815. else if (strcmp(*argv,"-psk_hint") == 0)
  816. {
  817. if (--argc < 1) goto bad;
  818. psk_identity_hint= *(++argv);
  819. }
  820. else if (strcmp(*argv,"-psk") == 0)
  821. {
  822. size_t i;
  823. if (--argc < 1) goto bad;
  824. psk_key=*(++argv);
  825. for (i=0; i<strlen(psk_key); i++)
  826. {
  827. if (isxdigit((int)psk_key[i]))
  828. continue;
  829. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  830. goto bad;
  831. }
  832. }
  833. #endif
  834. else if (strcmp(*argv,"-www") == 0)
  835. { www=1; }
  836. else if (strcmp(*argv,"-WWW") == 0)
  837. { www=2; }
  838. else if (strcmp(*argv,"-HTTP") == 0)
  839. { www=3; }
  840. else if (strcmp(*argv,"-no_ssl2") == 0)
  841. { off|=SSL_OP_NO_SSLv2; }
  842. else if (strcmp(*argv,"-no_ssl3") == 0)
  843. { off|=SSL_OP_NO_SSLv3; }
  844. else if (strcmp(*argv,"-no_tls1") == 0)
  845. { off|=SSL_OP_NO_TLSv1; }
  846. else if (strcmp(*argv,"-no_comp") == 0)
  847. { off|=SSL_OP_NO_COMPRESSION; }
  848. #ifndef OPENSSL_NO_SSL2
  849. else if (strcmp(*argv,"-ssl2") == 0)
  850. { meth=SSLv2_server_method(); }
  851. #endif
  852. #ifndef OPENSSL_NO_SSL3
  853. else if (strcmp(*argv,"-ssl3") == 0)
  854. { meth=SSLv3_server_method(); }
  855. #endif
  856. #ifndef OPENSSL_NO_TLS1
  857. else if (strcmp(*argv,"-tls1") == 0)
  858. { meth=TLSv1_server_method(); }
  859. #endif
  860. #ifndef OPENSSL_NO_DTLS1
  861. else if (strcmp(*argv,"-dtls1") == 0)
  862. {
  863. meth=DTLSv1_server_method();
  864. socket_type = SOCK_DGRAM;
  865. }
  866. else if (strcmp(*argv,"-timeout") == 0)
  867. enable_timeouts = 1;
  868. else if (strcmp(*argv,"-mtu") == 0)
  869. {
  870. if (--argc < 1) goto bad;
  871. socket_mtu = atol(*(++argv));
  872. }
  873. else if (strcmp(*argv, "-chain") == 0)
  874. cert_chain = 1;
  875. #endif
  876. else if (strcmp(*argv, "-id_prefix") == 0)
  877. {
  878. if (--argc < 1) goto bad;
  879. session_id_prefix = *(++argv);
  880. }
  881. #ifndef OPENSSL_NO_ENGINE
  882. else if (strcmp(*argv,"-engine") == 0)
  883. {
  884. if (--argc < 1) goto bad;
  885. engine_id= *(++argv);
  886. }
  887. #endif
  888. else if (strcmp(*argv,"-rand") == 0)
  889. {
  890. if (--argc < 1) goto bad;
  891. inrand= *(++argv);
  892. }
  893. #ifndef OPENSSL_NO_TLSEXT
  894. else if (strcmp(*argv,"-servername") == 0)
  895. {
  896. if (--argc < 1) goto bad;
  897. tlsextcbp.servername= *(++argv);
  898. }
  899. else if (strcmp(*argv,"-servername_fatal") == 0)
  900. { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; }
  901. else if (strcmp(*argv,"-cert2") == 0)
  902. {
  903. if (--argc < 1) goto bad;
  904. s_cert_file2= *(++argv);
  905. }
  906. else if (strcmp(*argv,"-key2") == 0)
  907. {
  908. if (--argc < 1) goto bad;
  909. s_key_file2= *(++argv);
  910. }
  911. #endif
  912. else
  913. {
  914. BIO_printf(bio_err,"unknown option %s\n",*argv);
  915. badop=1;
  916. break;
  917. }
  918. argc--;
  919. argv++;
  920. }
  921. if (badop)
  922. {
  923. bad:
  924. sv_usage();
  925. goto end;
  926. }
  927. SSL_load_error_strings();
  928. OpenSSL_add_ssl_algorithms();
  929. #ifndef OPENSSL_NO_ENGINE
  930. e = setup_engine(bio_err, engine_id, 1);
  931. #endif
  932. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
  933. {
  934. BIO_printf(bio_err, "Error getting password\n");
  935. goto end;
  936. }
  937. if (s_key_file == NULL)
  938. s_key_file = s_cert_file;
  939. #ifndef OPENSSL_NO_TLSEXT
  940. if (s_key_file2 == NULL)
  941. s_key_file2 = s_cert_file2;
  942. #endif
  943. if (nocert == 0)
  944. {
  945. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  946. "server certificate private key file");
  947. if (!s_key)
  948. {
  949. ERR_print_errors(bio_err);
  950. goto end;
  951. }
  952. s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
  953. NULL, e, "server certificate file");
  954. if (!s_cert)
  955. {
  956. ERR_print_errors(bio_err);
  957. goto end;
  958. }
  959. #ifndef OPENSSL_NO_TLSEXT
  960. if (tlsextcbp.servername)
  961. {
  962. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  963. "second server certificate private key file");
  964. if (!s_key2)
  965. {
  966. ERR_print_errors(bio_err);
  967. goto end;
  968. }
  969. s_cert2 = load_cert(bio_err,s_cert_file2,s_cert_format,
  970. NULL, e, "second server certificate file");
  971. if (!s_cert2)
  972. {
  973. ERR_print_errors(bio_err);
  974. goto end;
  975. }
  976. }
  977. #endif
  978. }
  979. if (s_dcert_file)
  980. {
  981. if (s_dkey_file == NULL)
  982. s_dkey_file = s_dcert_file;
  983. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  984. 0, dpass, e,
  985. "second certificate private key file");
  986. if (!s_dkey)
  987. {
  988. ERR_print_errors(bio_err);
  989. goto end;
  990. }
  991. s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
  992. NULL, e, "second server certificate file");
  993. if (!s_dcert)
  994. {
  995. ERR_print_errors(bio_err);
  996. goto end;
  997. }
  998. }
  999. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1000. && !RAND_status())
  1001. {
  1002. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  1003. }
  1004. if (inrand != NULL)
  1005. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  1006. app_RAND_load_files(inrand));
  1007. if (bio_s_out == NULL)
  1008. {
  1009. if (s_quiet && !s_debug && !s_msg)
  1010. {
  1011. bio_s_out=BIO_new(BIO_s_null());
  1012. }
  1013. else
  1014. {
  1015. if (bio_s_out == NULL)
  1016. bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  1017. }
  1018. }
  1019. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1020. if (nocert)
  1021. #endif
  1022. {
  1023. s_cert_file=NULL;
  1024. s_key_file=NULL;
  1025. s_dcert_file=NULL;
  1026. s_dkey_file=NULL;
  1027. #ifndef OPENSSL_NO_TLSEXT
  1028. s_cert_file2=NULL;
  1029. s_key_file2=NULL;
  1030. #endif
  1031. }
  1032. ctx=SSL_CTX_new(meth);
  1033. if (ctx == NULL)
  1034. {
  1035. ERR_print_errors(bio_err);
  1036. goto end;
  1037. }
  1038. if (session_id_prefix)
  1039. {
  1040. if(strlen(session_id_prefix) >= 32)
  1041. BIO_printf(bio_err,
  1042. "warning: id_prefix is too long, only one new session will be possible\n");
  1043. else if(strlen(session_id_prefix) >= 16)
  1044. BIO_printf(bio_err,
  1045. "warning: id_prefix is too long if you use SSLv2\n");
  1046. if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
  1047. {
  1048. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1049. ERR_print_errors(bio_err);
  1050. goto end;
  1051. }
  1052. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1053. }
  1054. SSL_CTX_set_quiet_shutdown(ctx,1);
  1055. if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
  1056. if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1057. SSL_CTX_set_options(ctx,off);
  1058. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1059. * Setting read ahead solves this problem.
  1060. */
  1061. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  1062. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  1063. SSL_CTX_sess_set_cache_size(ctx,128);
  1064. #if 0
  1065. if (cipher == NULL) cipher=getenv("SSL_CIPHER");
  1066. #endif
  1067. #if 0
  1068. if (s_cert_file == NULL)
  1069. {
  1070. BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
  1071. goto end;
  1072. }
  1073. #endif
  1074. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  1075. (!SSL_CTX_set_default_verify_paths(ctx)))
  1076. {
  1077. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1078. ERR_print_errors(bio_err);
  1079. /* goto end; */
  1080. }
  1081. store = SSL_CTX_get_cert_store(ctx);
  1082. X509_STORE_set_flags(store, vflags);
  1083. #ifndef OPENSSL_NO_TLSEXT
  1084. if (s_cert2)
  1085. {
  1086. ctx2=SSL_CTX_new(meth);
  1087. if (ctx2 == NULL)
  1088. {
  1089. ERR_print_errors(bio_err);
  1090. goto end;
  1091. }
  1092. }
  1093. if (ctx2)
  1094. {
  1095. BIO_printf(bio_s_out,"Setting secondary ctx parameters\n");
  1096. if (session_id_prefix)
  1097. {
  1098. if(strlen(session_id_prefix) >= 32)
  1099. BIO_printf(bio_err,
  1100. "warning: id_prefix is too long, only one new session will be possible\n");
  1101. else if(strlen(session_id_prefix) >= 16)
  1102. BIO_printf(bio_err,
  1103. "warning: id_prefix is too long if you use SSLv2\n");
  1104. if(!SSL_CTX_set_generate_session_id(ctx2, generate_session_id))
  1105. {
  1106. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1107. ERR_print_errors(bio_err);
  1108. goto end;
  1109. }
  1110. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1111. }
  1112. SSL_CTX_set_quiet_shutdown(ctx2,1);
  1113. if (bugs) SSL_CTX_set_options(ctx2,SSL_OP_ALL);
  1114. if (hack) SSL_CTX_set_options(ctx2,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1115. SSL_CTX_set_options(ctx2,off);
  1116. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1117. * Setting read ahead solves this problem.
  1118. */
  1119. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx2, 1);
  1120. if (state) SSL_CTX_set_info_callback(ctx2,apps_ssl_info_callback);
  1121. SSL_CTX_sess_set_cache_size(ctx2,128);
  1122. if ((!SSL_CTX_load_verify_locations(ctx2,CAfile,CApath)) ||
  1123. (!SSL_CTX_set_default_verify_paths(ctx2)))
  1124. {
  1125. ERR_print_errors(bio_err);
  1126. }
  1127. store = SSL_CTX_get_cert_store(ctx2);
  1128. X509_STORE_set_flags(store, vflags);
  1129. }
  1130. #endif
  1131. #ifndef OPENSSL_NO_DH
  1132. if (!no_dhe)
  1133. {
  1134. DH *dh=NULL;
  1135. if (dhfile)
  1136. dh = load_dh_param(dhfile);
  1137. else if (s_cert_file)
  1138. dh = load_dh_param(s_cert_file);
  1139. if (dh != NULL)
  1140. {
  1141. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1142. }
  1143. else
  1144. {
  1145. BIO_printf(bio_s_out,"Using default temp DH parameters\n");
  1146. dh=get_dh512();
  1147. }
  1148. (void)BIO_flush(bio_s_out);
  1149. SSL_CTX_set_tmp_dh(ctx,dh);
  1150. #ifndef OPENSSL_NO_TLSEXT
  1151. if (ctx2)
  1152. {
  1153. if (!dhfile)
  1154. {
  1155. DH *dh2=load_dh_param(s_cert_file2);
  1156. if (dh2 != NULL)
  1157. {
  1158. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1159. (void)BIO_flush(bio_s_out);
  1160. DH_free(dh);
  1161. dh = dh2;
  1162. }
  1163. }
  1164. SSL_CTX_set_tmp_dh(ctx2,dh);
  1165. }
  1166. #endif
  1167. DH_free(dh);
  1168. }
  1169. #endif
  1170. #ifndef OPENSSL_NO_ECDH
  1171. if (!no_ecdhe)
  1172. {
  1173. EC_KEY *ecdh=NULL;
  1174. if (named_curve)
  1175. {
  1176. int nid = OBJ_sn2nid(named_curve);
  1177. if (nid == 0)
  1178. {
  1179. BIO_printf(bio_err, "unknown curve name (%s)\n",
  1180. named_curve);
  1181. goto end;
  1182. }
  1183. ecdh = EC_KEY_new_by_curve_name(nid);
  1184. if (ecdh == NULL)
  1185. {
  1186. BIO_printf(bio_err, "unable to create curve (%s)\n",
  1187. named_curve);
  1188. goto end;
  1189. }
  1190. }
  1191. if (ecdh != NULL)
  1192. {
  1193. BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
  1194. }
  1195. else
  1196. {
  1197. BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
  1198. ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  1199. if (ecdh == NULL)
  1200. {
  1201. BIO_printf(bio_err, "unable to create curve (nistp256)\n");
  1202. goto end;
  1203. }
  1204. }
  1205. (void)BIO_flush(bio_s_out);
  1206. SSL_CTX_set_tmp_ecdh(ctx,ecdh);
  1207. #ifndef OPENSSL_NO_TLSEXT
  1208. if (ctx2)
  1209. SSL_CTX_set_tmp_ecdh(ctx2,ecdh);
  1210. #endif
  1211. EC_KEY_free(ecdh);
  1212. }
  1213. #endif
  1214. if (!set_cert_key_stuff(ctx,s_cert,s_key))
  1215. goto end;
  1216. #ifndef OPENSSL_NO_TLSEXT
  1217. if (ctx2 && !set_cert_key_stuff(ctx2,s_cert2,s_key2))
  1218. goto end;
  1219. #endif
  1220. if (s_dcert != NULL)
  1221. {
  1222. if (!set_cert_key_stuff(ctx,s_dcert,s_dkey))
  1223. goto end;
  1224. }
  1225. #ifndef OPENSSL_NO_RSA
  1226. #if 1
  1227. if (!no_tmp_rsa)
  1228. {
  1229. SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
  1230. #ifndef OPENSSL_NO_TLSEXT
  1231. if (ctx2)
  1232. SSL_CTX_set_tmp_rsa_callback(ctx2,tmp_rsa_cb);
  1233. #endif
  1234. }
  1235. #else
  1236. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
  1237. {
  1238. RSA *rsa;
  1239. BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
  1240. BIO_flush(bio_s_out);
  1241. rsa=RSA_generate_key(512,RSA_F4,NULL);
  1242. if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
  1243. {
  1244. ERR_print_errors(bio_err);
  1245. goto end;
  1246. }
  1247. #ifndef OPENSSL_NO_TLSEXT
  1248. if (ctx2)
  1249. {
  1250. if (!SSL_CTX_set_tmp_rsa(ctx2,rsa))
  1251. {
  1252. ERR_print_errors(bio_err);
  1253. goto end;
  1254. }
  1255. }
  1256. #endif
  1257. RSA_free(rsa);
  1258. BIO_printf(bio_s_out,"\n");
  1259. }
  1260. #endif
  1261. #endif
  1262. #ifndef OPENSSL_NO_PSK
  1263. if (psk_key != NULL)
  1264. {
  1265. if (s_debug)
  1266. BIO_printf(bio_s_out, "PSK key given, setting server callback\n");
  1267. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1268. }
  1269. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint))
  1270. {
  1271. BIO_printf(bio_err,"error setting PSK identity hint to context\n");
  1272. ERR_print_errors(bio_err);
  1273. goto end;
  1274. }
  1275. #endif
  1276. if (cipher != NULL)
  1277. {
  1278. if(!SSL_CTX_set_cipher_list(ctx,cipher))
  1279. {
  1280. BIO_printf(bio_err,"error setting cipher list\n");
  1281. ERR_print_errors(bio_err);
  1282. goto end;
  1283. }
  1284. #ifndef OPENSSL_NO_TLSEXT
  1285. if (ctx2 && !SSL_CTX_set_cipher_list(ctx2,cipher))
  1286. {
  1287. BIO_printf(bio_err,"error setting cipher list\n");
  1288. ERR_print_errors(bio_err);
  1289. goto end;
  1290. }
  1291. #endif
  1292. }
  1293. SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
  1294. SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
  1295. sizeof s_server_session_id_context);
  1296. #ifndef OPENSSL_NO_TLSEXT
  1297. if (ctx2)
  1298. {
  1299. SSL_CTX_set_verify(ctx2,s_server_verify,verify_callback);
  1300. SSL_CTX_set_session_id_context(ctx2,(void*)&s_server_session_id_context,
  1301. sizeof s_server_session_id_context);
  1302. tlsextcbp.biodebug = bio_s_out;
  1303. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1304. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1305. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1306. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1307. }
  1308. #endif
  1309. if (CAfile != NULL)
  1310. {
  1311. SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
  1312. #ifndef OPENSSL_NO_TLSEXT
  1313. if (ctx2)
  1314. SSL_CTX_set_client_CA_list(ctx2,SSL_load_client_CA_file(CAfile));
  1315. #endif
  1316. }
  1317. BIO_printf(bio_s_out,"ACCEPT\n");
  1318. if (www)
  1319. do_server(port,socket_type,&accept_socket,www_body, context);
  1320. else
  1321. do_server(port,socket_type,&accept_socket,sv_body, context);
  1322. print_stats(bio_s_out,ctx);
  1323. ret=0;
  1324. end:
  1325. if (ctx != NULL) SSL_CTX_free(ctx);
  1326. if (s_cert)
  1327. X509_free(s_cert);
  1328. if (s_dcert)
  1329. X509_free(s_dcert);
  1330. if (s_key)
  1331. EVP_PKEY_free(s_key);
  1332. if (s_dkey)
  1333. EVP_PKEY_free(s_dkey);
  1334. if (pass)
  1335. OPENSSL_free(pass);
  1336. if (dpass)
  1337. OPENSSL_free(dpass);
  1338. #ifndef OPENSSL_NO_TLSEXT
  1339. if (ctx2 != NULL) SSL_CTX_free(ctx2);
  1340. if (s_cert2)
  1341. X509_free(s_cert2);
  1342. if (s_key2)
  1343. EVP_PKEY_free(s_key2);
  1344. #endif
  1345. if (bio_s_out != NULL)
  1346. {
  1347. BIO_free(bio_s_out);
  1348. bio_s_out=NULL;
  1349. }
  1350. apps_shutdown();
  1351. OPENSSL_EXIT(ret);
  1352. }
  1353. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1354. {
  1355. BIO_printf(bio,"%4ld items in the session cache\n",
  1356. SSL_CTX_sess_number(ssl_ctx));
  1357. BIO_printf(bio,"%4ld client connects (SSL_connect())\n",
  1358. SSL_CTX_sess_connect(ssl_ctx));
  1359. BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n",
  1360. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1361. BIO_printf(bio,"%4ld client connects that finished\n",
  1362. SSL_CTX_sess_connect_good(ssl_ctx));
  1363. BIO_printf(bio,"%4ld server accepts (SSL_accept())\n",
  1364. SSL_CTX_sess_accept(ssl_ctx));
  1365. BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n",
  1366. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1367. BIO_printf(bio,"%4ld server accepts that finished\n",
  1368. SSL_CTX_sess_accept_good(ssl_ctx));
  1369. BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
  1370. BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
  1371. BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
  1372. BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
  1373. BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n",
  1374. SSL_CTX_sess_cache_full(ssl_ctx),
  1375. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1376. }
  1377. static int sv_body(char *hostname, int s, unsigned char *context)
  1378. {
  1379. char *buf=NULL;
  1380. fd_set readfds;
  1381. int ret=1,width;
  1382. int k,i;
  1383. unsigned long l;
  1384. SSL *con=NULL;
  1385. BIO *sbio;
  1386. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1387. struct timeval tv;
  1388. #endif
  1389. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1390. {
  1391. BIO_printf(bio_err,"out of memory\n");
  1392. goto err;
  1393. }
  1394. #ifdef FIONBIO
  1395. if (s_nbio)
  1396. {
  1397. unsigned long sl=1;
  1398. if (!s_quiet)
  1399. BIO_printf(bio_err,"turning on non blocking io\n");
  1400. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1401. ERR_print_errors(bio_err);
  1402. }
  1403. #endif
  1404. if (con == NULL) {
  1405. con=SSL_new(ctx);
  1406. #ifndef OPENSSL_NO_KRB5
  1407. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1408. {
  1409. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
  1410. KRB5SVC);
  1411. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
  1412. KRB5KEYTAB);
  1413. }
  1414. #endif /* OPENSSL_NO_KRB5 */
  1415. if(context)
  1416. SSL_set_session_id_context(con, context,
  1417. strlen((char *)context));
  1418. }
  1419. SSL_clear(con);
  1420. if (SSL_version(con) == DTLS1_VERSION)
  1421. {
  1422. struct timeval timeout;
  1423. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1424. if (enable_timeouts)
  1425. {
  1426. timeout.tv_sec = 0;
  1427. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1428. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1429. timeout.tv_sec = 0;
  1430. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1431. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1432. }
  1433. if (socket_mtu > 0)
  1434. {
  1435. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1436. SSL_set_mtu(con, socket_mtu);
  1437. }
  1438. else
  1439. /* want to do MTU discovery */
  1440. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1441. /* turn on cookie exchange */
  1442. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1443. }
  1444. else
  1445. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1446. if (s_nbio_test)
  1447. {
  1448. BIO *test;
  1449. test=BIO_new(BIO_f_nbio_test());
  1450. sbio=BIO_push(test,sbio);
  1451. }
  1452. SSL_set_bio(con,sbio,sbio);
  1453. SSL_set_accept_state(con);
  1454. /* SSL_set_fd(con,s); */
  1455. if (s_debug)
  1456. {
  1457. con->debug=1;
  1458. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1459. BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
  1460. }
  1461. if (s_msg)
  1462. {
  1463. SSL_set_msg_callback(con, msg_cb);
  1464. SSL_set_msg_callback_arg(con, bio_s_out);
  1465. }
  1466. width=s+1;
  1467. for (;;)
  1468. {
  1469. int read_from_terminal;
  1470. int read_from_sslcon;
  1471. read_from_terminal = 0;
  1472. read_from_sslcon = SSL_pending(con);
  1473. if (!read_from_sslcon)
  1474. {
  1475. FD_ZERO(&readfds);
  1476. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1477. openssl_fdset(fileno(stdin),&readfds);
  1478. #endif
  1479. openssl_fdset(s,&readfds);
  1480. /* Note: under VMS with SOCKETSHR the second parameter is
  1481. * currently of type (int *) whereas under other systems
  1482. * it is (void *) if you don't have a cast it will choke
  1483. * the compiler: if you do have a cast then you can either
  1484. * go for (int *) or (void *).
  1485. */
  1486. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1487. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1488. * on sockets. As a workaround we timeout the select every
  1489. * second and check for any keypress. In a proper Windows
  1490. * application we wouldn't do this because it is inefficient.
  1491. */
  1492. tv.tv_sec = 1;
  1493. tv.tv_usec = 0;
  1494. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1495. if((i < 0) || (!i && !_kbhit() ) )continue;
  1496. if(_kbhit())
  1497. read_from_terminal = 1;
  1498. #elif defined(OPENSSL_SYS_BEOS_R5)
  1499. /* Under BeOS-R5 the situation is similar to DOS */
  1500. tv.tv_sec = 1;
  1501. tv.tv_usec = 0;
  1502. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1503. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1504. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1505. continue;
  1506. if (read(fileno(stdin), buf, 0) >= 0)
  1507. read_from_terminal = 1;
  1508. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1509. #else
  1510. i=select(width,(void *)&readfds,NULL,NULL,NULL);
  1511. if (i <= 0) continue;
  1512. if (FD_ISSET(fileno(stdin),&readfds))
  1513. read_from_terminal = 1;
  1514. #endif
  1515. if (FD_ISSET(s,&readfds))
  1516. read_from_sslcon = 1;
  1517. }
  1518. if (read_from_terminal)
  1519. {
  1520. if (s_crlf)
  1521. {
  1522. int j, lf_num;
  1523. i=raw_read_stdin(buf, bufsize/2);
  1524. lf_num = 0;
  1525. /* both loops are skipped when i <= 0 */
  1526. for (j = 0; j < i; j++)
  1527. if (buf[j] == '\n')
  1528. lf_num++;
  1529. for (j = i-1; j >= 0; j--)
  1530. {
  1531. buf[j+lf_num] = buf[j];
  1532. if (buf[j] == '\n')
  1533. {
  1534. lf_num--;
  1535. i++;
  1536. buf[j+lf_num] = '\r';
  1537. }
  1538. }
  1539. assert(lf_num == 0);
  1540. }
  1541. else
  1542. i=raw_read_stdin(buf,bufsize);
  1543. if (!s_quiet)
  1544. {
  1545. if ((i <= 0) || (buf[0] == 'Q'))
  1546. {
  1547. BIO_printf(bio_s_out,"DONE\n");
  1548. SHUTDOWN(s);
  1549. close_accept_socket();
  1550. ret= -11;
  1551. goto err;
  1552. }
  1553. if ((i <= 0) || (buf[0] == 'q'))
  1554. {
  1555. BIO_printf(bio_s_out,"DONE\n");
  1556. if (SSL_version(con) != DTLS1_VERSION)
  1557. SHUTDOWN(s);
  1558. /* close_accept_socket();
  1559. ret= -11;*/
  1560. goto err;
  1561. }
  1562. if ((buf[0] == 'r') &&
  1563. ((buf[1] == '\n') || (buf[1] == '\r')))
  1564. {
  1565. SSL_renegotiate(con);
  1566. i=SSL_do_handshake(con);
  1567. printf("SSL_do_handshake -> %d\n",i);
  1568. i=0; /*13; */
  1569. continue;
  1570. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  1571. }
  1572. if ((buf[0] == 'R') &&
  1573. ((buf[1] == '\n') || (buf[1] == '\r')))
  1574. {
  1575. SSL_set_verify(con,
  1576. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  1577. SSL_renegotiate(con);
  1578. i=SSL_do_handshake(con);
  1579. printf("SSL_do_handshake -> %d\n",i);
  1580. i=0; /* 13; */
  1581. continue;
  1582. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  1583. }
  1584. if (buf[0] == 'P')
  1585. {
  1586. static const char *str="Lets print some clear text\n";
  1587. BIO_write(SSL_get_wbio(con),str,strlen(str));
  1588. }
  1589. if (buf[0] == 'S')
  1590. {
  1591. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  1592. }
  1593. }
  1594. #ifdef CHARSET_EBCDIC
  1595. ebcdic2ascii(buf,buf,i);
  1596. #endif
  1597. l=k=0;
  1598. for (;;)
  1599. {
  1600. /* should do a select for the write */
  1601. #ifdef RENEG
  1602. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  1603. #endif
  1604. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  1605. switch (SSL_get_error(con,k))
  1606. {
  1607. case SSL_ERROR_NONE:
  1608. break;
  1609. case SSL_ERROR_WANT_WRITE:
  1610. case SSL_ERROR_WANT_READ:
  1611. case SSL_ERROR_WANT_X509_LOOKUP:
  1612. BIO_printf(bio_s_out,"Write BLOCK\n");
  1613. break;
  1614. case SSL_ERROR_SYSCALL:
  1615. case SSL_ERROR_SSL:
  1616. BIO_printf(bio_s_out,"ERROR\n");
  1617. ERR_print_errors(bio_err);
  1618. ret=1;
  1619. goto err;
  1620. /* break; */
  1621. case SSL_ERROR_ZERO_RETURN:
  1622. BIO_printf(bio_s_out,"DONE\n");
  1623. ret=1;
  1624. goto err;
  1625. }
  1626. l+=k;
  1627. i-=k;
  1628. if (i <= 0) break;
  1629. }
  1630. }
  1631. if (read_from_sslcon)
  1632. {
  1633. if (!SSL_is_init_finished(con))
  1634. {
  1635. i=init_ssl_connection(con);
  1636. if (i < 0)
  1637. {
  1638. ret=0;
  1639. goto err;
  1640. }
  1641. else if (i == 0)
  1642. {
  1643. ret=1;
  1644. goto err;
  1645. }
  1646. }
  1647. else
  1648. {
  1649. again:
  1650. i=SSL_read(con,(char *)buf,bufsize);
  1651. switch (SSL_get_error(con,i))
  1652. {
  1653. case SSL_ERROR_NONE:
  1654. #ifdef CHARSET_EBCDIC
  1655. ascii2ebcdic(buf,buf,i);
  1656. #endif
  1657. raw_write_stdout(buf,
  1658. (unsigned int)i);
  1659. if (SSL_pending(con)) goto again;
  1660. break;
  1661. case SSL_ERROR_WANT_WRITE:
  1662. case SSL_ERROR_WANT_READ:
  1663. case SSL_ERROR_WANT_X509_LOOKUP:
  1664. BIO_printf(bio_s_out,"Read BLOCK\n");
  1665. break;
  1666. case SSL_ERROR_SYSCALL:
  1667. case SSL_ERROR_SSL:
  1668. BIO_printf(bio_s_out,"ERROR\n");
  1669. ERR_print_errors(bio_err);
  1670. ret=1;
  1671. goto err;
  1672. case SSL_ERROR_ZERO_RETURN:
  1673. BIO_printf(bio_s_out,"DONE\n");
  1674. ret=1;
  1675. goto err;
  1676. }
  1677. }
  1678. }
  1679. }
  1680. err:
  1681. if (con != NULL)
  1682. {
  1683. BIO_printf(bio_s_out,"shutting down SSL\n");
  1684. #if 1
  1685. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  1686. #else
  1687. SSL_shutdown(con);
  1688. #endif
  1689. SSL_free(con);
  1690. }
  1691. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  1692. if (buf != NULL)
  1693. {
  1694. OPENSSL_cleanse(buf,bufsize);
  1695. OPENSSL_free(buf);
  1696. }
  1697. if (ret >= 0)
  1698. BIO_printf(bio_s_out,"ACCEPT\n");
  1699. return(ret);
  1700. }
  1701. static void close_accept_socket(void)
  1702. {
  1703. BIO_printf(bio_err,"shutdown accept socket\n");
  1704. if (accept_socket >= 0)
  1705. {
  1706. SHUTDOWN2(accept_socket);
  1707. }
  1708. }
  1709. static int init_ssl_connection(SSL *con)
  1710. {
  1711. int i;
  1712. const char *str;
  1713. X509 *peer;
  1714. long verify_error;
  1715. MS_STATIC char buf[BUFSIZ];
  1716. if ((i=SSL_accept(con)) <= 0)
  1717. {
  1718. if (BIO_sock_should_retry(i))
  1719. {
  1720. BIO_printf(bio_s_out,"DELAY\n");
  1721. return(1);
  1722. }
  1723. BIO_printf(bio_err,"ERROR\n");
  1724. verify_error=SSL_get_verify_result(con);
  1725. if (verify_error != X509_V_OK)
  1726. {
  1727. BIO_printf(bio_err,"verify error:%s\n",
  1728. X509_verify_cert_error_string(verify_error));
  1729. }
  1730. else
  1731. ERR_print_errors(bio_err);
  1732. return(0);
  1733. }
  1734. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  1735. peer=SSL_get_peer_certificate(con);
  1736. if (peer != NULL)
  1737. {
  1738. BIO_printf(bio_s_out,"Client certificate\n");
  1739. PEM_write_bio_X509(bio_s_out,peer);
  1740. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  1741. BIO_printf(bio_s_out,"subject=%s\n",buf);
  1742. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  1743. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  1744. X509_free(peer);
  1745. }
  1746. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  1747. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  1748. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  1749. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  1750. if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
  1751. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  1752. TLS1_FLAGS_TLS_PADDING_BUG)
  1753. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  1754. #ifndef OPENSSL_NO_KRB5
  1755. if (con->kssl_ctx->client_princ != NULL)
  1756. {
  1757. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  1758. con->kssl_ctx->client_princ);
  1759. }
  1760. #endif /* OPENSSL_NO_KRB5 */
  1761. return(1);
  1762. }
  1763. #ifndef OPENSSL_NO_DH
  1764. static DH *load_dh_param(const char *dhfile)
  1765. {
  1766. DH *ret=NULL;
  1767. BIO *bio;
  1768. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  1769. goto err;
  1770. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  1771. err:
  1772. if (bio != NULL) BIO_free(bio);
  1773. return(ret);
  1774. }
  1775. #endif
  1776. #if 0
  1777. static int load_CA(SSL_CTX *ctx, char *file)
  1778. {
  1779. FILE *in;
  1780. X509 *x=NULL;
  1781. if ((in=fopen(file,"r")) == NULL)
  1782. return(0);
  1783. for (;;)
  1784. {
  1785. if (PEM_read_X509(in,&x,NULL) == NULL)
  1786. break;
  1787. SSL_CTX_add_client_CA(ctx,x);
  1788. }
  1789. if (x != NULL) X509_free(x);
  1790. fclose(in);
  1791. return(1);
  1792. }
  1793. #endif
  1794. static int www_body(char *hostname, int s, unsigned char *context)
  1795. {
  1796. char *buf=NULL;
  1797. int ret=1;
  1798. int i,j,k,blank,dot;
  1799. SSL *con;
  1800. SSL_CIPHER *c;
  1801. BIO *io,*ssl_bio,*sbio;
  1802. long total_bytes;
  1803. buf=OPENSSL_malloc(bufsize);
  1804. if (buf == NULL) return(0);
  1805. io=BIO_new(BIO_f_buffer());
  1806. ssl_bio=BIO_new(BIO_f_ssl());
  1807. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  1808. #ifdef FIONBIO
  1809. if (s_nbio)
  1810. {
  1811. unsigned long sl=1;
  1812. if (!s_quiet)
  1813. BIO_printf(bio_err,"turning on non blocking io\n");
  1814. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1815. ERR_print_errors(bio_err);
  1816. }
  1817. #endif
  1818. /* lets make the output buffer a reasonable size */
  1819. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  1820. if ((con=SSL_new(ctx)) == NULL) goto err;
  1821. #ifndef OPENSSL_NO_KRB5
  1822. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1823. {
  1824. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
  1825. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
  1826. }
  1827. #endif /* OPENSSL_NO_KRB5 */
  1828. if(context) SSL_set_session_id_context(con, context,
  1829. strlen((char *)context));
  1830. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1831. if (s_nbio_test)
  1832. {
  1833. BIO *test;
  1834. test=BIO_new(BIO_f_nbio_test());
  1835. sbio=BIO_push(test,sbio);
  1836. }
  1837. SSL_set_bio(con,sbio,sbio);
  1838. SSL_set_accept_state(con);
  1839. /* SSL_set_fd(con,s); */
  1840. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  1841. BIO_push(io,ssl_bio);
  1842. #ifdef CHARSET_EBCDIC
  1843. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  1844. #endif
  1845. if (s_debug)
  1846. {
  1847. con->debug=1;
  1848. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1849. BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
  1850. }
  1851. if (s_msg)
  1852. {
  1853. SSL_set_msg_callback(con, msg_cb);
  1854. SSL_set_msg_callback_arg(con, bio_s_out);
  1855. }
  1856. blank=0;
  1857. for (;;)
  1858. {
  1859. if (hack)
  1860. {
  1861. i=SSL_accept(con);
  1862. switch (SSL_get_error(con,i))
  1863. {
  1864. case SSL_ERROR_NONE:
  1865. break;
  1866. case SSL_ERROR_WANT_WRITE:
  1867. case SSL_ERROR_WANT_READ:
  1868. case SSL_ERROR_WANT_X509_LOOKUP:
  1869. continue;
  1870. case SSL_ERROR_SYSCALL:
  1871. case SSL_ERROR_SSL:
  1872. case SSL_ERROR_ZERO_RETURN:
  1873. ret=1;
  1874. goto err;
  1875. /* break; */
  1876. }
  1877. SSL_renegotiate(con);
  1878. SSL_write(con,NULL,0);
  1879. }
  1880. i=BIO_gets(io,buf,bufsize-1);
  1881. if (i < 0) /* error */
  1882. {
  1883. if (!BIO_should_retry(io))
  1884. {
  1885. if (!s_quiet)
  1886. ERR_print_errors(bio_err);
  1887. goto err;
  1888. }
  1889. else
  1890. {
  1891. BIO_printf(bio_s_out,"read R BLOCK\n");
  1892. #if defined(OPENSSL_SYS_NETWARE)
  1893. delay(1000);
  1894. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  1895. sleep(1);
  1896. #endif
  1897. continue;
  1898. }
  1899. }
  1900. else if (i == 0) /* end of input */
  1901. {
  1902. ret=1;
  1903. goto end;
  1904. }
  1905. /* else we have data */
  1906. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  1907. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  1908. {
  1909. char *p;
  1910. X509 *peer;
  1911. STACK_OF(SSL_CIPHER) *sk;
  1912. static const char *space=" ";
  1913. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  1914. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  1915. BIO_puts(io,"<pre>\n");
  1916. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  1917. BIO_puts(io,"\n");
  1918. for (i=0; i<local_argc; i++)
  1919. {
  1920. BIO_puts(io,local_argv[i]);
  1921. BIO_write(io," ",1);
  1922. }
  1923. BIO_puts(io,"\n");
  1924. /* The following is evil and should not really
  1925. * be done */
  1926. BIO_printf(io,"Ciphers supported in s_server binary\n");
  1927. sk=SSL_get_ciphers(con);
  1928. j=sk_SSL_CIPHER_num(sk);
  1929. for (i=0; i<j; i++)
  1930. {
  1931. c=sk_SSL_CIPHER_value(sk,i);
  1932. BIO_printf(io,"%-11s:%-25s",
  1933. SSL_CIPHER_get_version(c),
  1934. SSL_CIPHER_get_name(c));
  1935. if ((((i+1)%2) == 0) && (i+1 != j))
  1936. BIO_puts(io,"\n");
  1937. }
  1938. BIO_puts(io,"\n");
  1939. p=SSL_get_shared_ciphers(con,buf,bufsize);
  1940. if (p != NULL)
  1941. {
  1942. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  1943. j=i=0;
  1944. while (*p)
  1945. {
  1946. if (*p == ':')
  1947. {
  1948. BIO_write(io,space,26-j);
  1949. i++;
  1950. j=0;
  1951. BIO_write(io,((i%3)?" ":"\n"),1);
  1952. }
  1953. else
  1954. {
  1955. BIO_write(io,p,1);
  1956. j++;
  1957. }
  1958. p++;
  1959. }
  1960. BIO_puts(io,"\n");
  1961. }
  1962. BIO_printf(io,((con->hit)
  1963. ?"---\nReused, "
  1964. :"---\nNew, "));
  1965. c=SSL_get_current_cipher(con);
  1966. BIO_printf(io,"%s, Cipher is %s\n",
  1967. SSL_CIPHER_get_version(c),
  1968. SSL_CIPHER_get_name(c));
  1969. SSL_SESSION_print(io,SSL_get_session(con));
  1970. BIO_printf(io,"---\n");
  1971. print_stats(io,SSL_get_SSL_CTX(con));
  1972. BIO_printf(io,"---\n");
  1973. peer=SSL_get_peer_certificate(con);
  1974. if (peer != NULL)
  1975. {
  1976. BIO_printf(io,"Client certificate\n");
  1977. X509_print(io,peer);
  1978. PEM_write_bio_X509(io,peer);
  1979. }
  1980. else
  1981. BIO_puts(io,"no client certificate available\n");
  1982. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  1983. break;
  1984. }
  1985. else if ((www == 2 || www == 3)
  1986. && (strncmp("GET /",buf,5) == 0))
  1987. {
  1988. BIO *file;
  1989. char *p,*e;
  1990. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  1991. /* skip the '/' */
  1992. p= &(buf[5]);
  1993. dot = 1;
  1994. for (e=p; *e != '\0'; e++)
  1995. {
  1996. if (e[0] == ' ')
  1997. break;
  1998. switch (dot)
  1999. {
  2000. case 1:
  2001. dot = (e[0] == '.') ? 2 : 0;
  2002. break;
  2003. case 2:
  2004. dot = (e[0] == '.') ? 3 : 0;
  2005. break;
  2006. case 3:
  2007. dot = (e[0] == '/') ? -1 : 0;
  2008. break;
  2009. }
  2010. if (dot == 0)
  2011. dot = (e[0] == '/') ? 1 : 0;
  2012. }
  2013. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2014. if (*e == '\0')
  2015. {
  2016. BIO_puts(io,text);
  2017. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2018. break;
  2019. }
  2020. *e='\0';
  2021. if (dot)
  2022. {
  2023. BIO_puts(io,text);
  2024. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2025. break;
  2026. }
  2027. if (*p == '/')
  2028. {
  2029. BIO_puts(io,text);
  2030. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2031. break;
  2032. }
  2033. #if 0
  2034. /* append if a directory lookup */
  2035. if (e[-1] == '/')
  2036. strcat(p,"index.html");
  2037. #endif
  2038. /* if a directory, do the index thang */
  2039. if (app_isdir(p)>0)
  2040. {
  2041. #if 0 /* must check buffer size */
  2042. strcat(p,"/index.html");
  2043. #else
  2044. BIO_puts(io,text);
  2045. BIO_printf(io,"'%s' is a directory\r\n",p);
  2046. break;
  2047. #endif
  2048. }
  2049. if ((file=BIO_new_file(p,"r")) == NULL)
  2050. {
  2051. BIO_puts(io,text);
  2052. BIO_printf(io,"Error opening '%s'\r\n",p);
  2053. ERR_print_errors(io);
  2054. break;
  2055. }
  2056. if (!s_quiet)
  2057. BIO_printf(bio_err,"FILE:%s\n",p);
  2058. if (www == 2)
  2059. {
  2060. i=strlen(p);
  2061. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2062. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2063. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2064. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2065. else
  2066. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2067. }
  2068. /* send the file */
  2069. total_bytes=0;
  2070. for (;;)
  2071. {
  2072. i=BIO_read(file,buf,bufsize);
  2073. if (i <= 0) break;
  2074. #ifdef RENEG
  2075. total_bytes+=i;
  2076. fprintf(stderr,"%d\n",i);
  2077. if (total_bytes > 3*1024)
  2078. {
  2079. total_bytes=0;
  2080. fprintf(stderr,"RENEGOTIATE\n");
  2081. SSL_renegotiate(con);
  2082. }
  2083. #endif
  2084. for (j=0; j<i; )
  2085. {
  2086. #ifdef RENEG
  2087. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2088. #endif
  2089. k=BIO_write(io,&(buf[j]),i-j);
  2090. if (k <= 0)
  2091. {
  2092. if (!BIO_should_retry(io))
  2093. goto write_error;
  2094. else
  2095. {
  2096. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2097. }
  2098. }
  2099. else
  2100. {
  2101. j+=k;
  2102. }
  2103. }
  2104. }
  2105. write_error:
  2106. BIO_free(file);
  2107. break;
  2108. }
  2109. }
  2110. for (;;)
  2111. {
  2112. i=(int)BIO_flush(io);
  2113. if (i <= 0)
  2114. {
  2115. if (!BIO_should_retry(io))
  2116. break;
  2117. }
  2118. else
  2119. break;
  2120. }
  2121. end:
  2122. #if 1
  2123. /* make sure we re-use sessions */
  2124. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2125. #else
  2126. /* This kills performance */
  2127. /* SSL_shutdown(con); A shutdown gets sent in the
  2128. * BIO_free_all(io) procession */
  2129. #endif
  2130. err:
  2131. if (ret >= 0)
  2132. BIO_printf(bio_s_out,"ACCEPT\n");
  2133. if (buf != NULL) OPENSSL_free(buf);
  2134. if (io != NULL) BIO_free_all(io);
  2135. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2136. return(ret);
  2137. }
  2138. #ifndef OPENSSL_NO_RSA
  2139. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2140. {
  2141. BIGNUM *bn = NULL;
  2142. static RSA *rsa_tmp=NULL;
  2143. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2144. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2145. if (!rsa_tmp && bn)
  2146. {
  2147. if (!s_quiet)
  2148. {
  2149. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2150. (void)BIO_flush(bio_err);
  2151. }
  2152. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2153. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2154. {
  2155. if(rsa_tmp) RSA_free(rsa_tmp);
  2156. rsa_tmp = NULL;
  2157. }
  2158. if (!s_quiet)
  2159. {
  2160. BIO_printf(bio_err,"\n");
  2161. (void)BIO_flush(bio_err);
  2162. }
  2163. BN_free(bn);
  2164. }
  2165. return(rsa_tmp);
  2166. }
  2167. #endif
  2168. #define MAX_SESSION_ID_ATTEMPTS 10
  2169. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2170. unsigned int *id_len)
  2171. {
  2172. unsigned int count = 0;
  2173. do {
  2174. RAND_pseudo_bytes(id, *id_len);
  2175. /* Prefix the session_id with the required prefix. NB: If our
  2176. * prefix is too long, clip it - but there will be worse effects
  2177. * anyway, eg. the server could only possibly create 1 session
  2178. * ID (ie. the prefix!) so all future session negotiations will
  2179. * fail due to conflicts. */
  2180. memcpy(id, session_id_prefix,
  2181. (strlen(session_id_prefix) < *id_len) ?
  2182. strlen(session_id_prefix) : *id_len);
  2183. }
  2184. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2185. (++count < MAX_SESSION_ID_ATTEMPTS));
  2186. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2187. return 0;
  2188. return 1;
  2189. }