s_server.c 51 KB

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