s_server.c 67 KB

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