s_server.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  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. #ifndef OPENSSL_NO_SRP
  184. #include <openssl/srp.h>
  185. #endif
  186. #include "s_apps.h"
  187. #include "timeouts.h"
  188. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  189. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  190. #undef FIONBIO
  191. #endif
  192. #if defined(OPENSSL_SYS_BEOS_R5)
  193. #include <fcntl.h>
  194. #endif
  195. #ifndef OPENSSL_NO_RSA
  196. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  197. #endif
  198. static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
  199. static int sv_body(char *hostname, int s, unsigned char *context);
  200. static int www_body(char *hostname, int s, unsigned char *context);
  201. static void close_accept_socket(void );
  202. static void sv_usage(void);
  203. static int init_ssl_connection(SSL *s);
  204. static void print_stats(BIO *bp,SSL_CTX *ctx);
  205. static int generate_session_id(const SSL *ssl, unsigned char *id,
  206. unsigned int *id_len);
  207. static void init_session_cache_ctx(SSL_CTX *sctx);
  208. static void free_sessions(void);
  209. #ifndef OPENSSL_NO_DH
  210. static DH *load_dh_param(const char *dhfile);
  211. static DH *get_dh512(void);
  212. #endif
  213. #ifdef MONOLITH
  214. static void s_server_init(void);
  215. #endif
  216. #ifndef OPENSSL_NO_DH
  217. static unsigned char dh512_p[]={
  218. 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
  219. 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
  220. 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
  221. 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
  222. 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
  223. 0x47,0x74,0xE8,0x33,
  224. };
  225. static unsigned char dh512_g[]={
  226. 0x02,
  227. };
  228. static DH *get_dh512(void)
  229. {
  230. DH *dh=NULL;
  231. if ((dh=DH_new()) == NULL) return(NULL);
  232. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  233. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  234. if ((dh->p == NULL) || (dh->g == NULL))
  235. return(NULL);
  236. return(dh);
  237. }
  238. #endif
  239. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  240. #undef BUFSIZZ
  241. #define BUFSIZZ 16*1024
  242. static int bufsize=BUFSIZZ;
  243. static int accept_socket= -1;
  244. #define TEST_CERT "server.pem"
  245. #ifndef OPENSSL_NO_TLSEXT
  246. #define TEST_CERT2 "server2.pem"
  247. #endif
  248. #undef PROG
  249. #define PROG s_server_main
  250. extern int verify_depth, verify_return_error;
  251. static char *cipher=NULL;
  252. static int s_server_verify=SSL_VERIFY_NONE;
  253. static int s_server_session_id_context = 1; /* anything will do */
  254. static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
  255. #ifndef OPENSSL_NO_TLSEXT
  256. static const char *s_cert_file2=TEST_CERT2,*s_key_file2=NULL;
  257. #endif
  258. static char *s_dcert_file=NULL,*s_dkey_file=NULL;
  259. #ifdef FIONBIO
  260. static int s_nbio=0;
  261. #endif
  262. static int s_nbio_test=0;
  263. int s_crlf=0;
  264. static SSL_CTX *ctx=NULL;
  265. #ifndef OPENSSL_NO_TLSEXT
  266. static SSL_CTX *ctx2=NULL;
  267. #endif
  268. static int www=0;
  269. static BIO *bio_s_out=NULL;
  270. static int s_debug=0;
  271. #ifndef OPENSSL_NO_TLSEXT
  272. static int s_tlsextdebug=0;
  273. static int s_tlsextstatus=0;
  274. static int cert_status_cb(SSL *s, void *arg);
  275. #endif
  276. static int no_resume_ephemeral = 0;
  277. static int s_msg=0;
  278. static int s_quiet=0;
  279. static int hack=0;
  280. #ifndef OPENSSL_NO_ENGINE
  281. static char *engine_id=NULL;
  282. #endif
  283. static const char *session_id_prefix=NULL;
  284. static int enable_timeouts = 0;
  285. static long socket_mtu;
  286. #ifndef OPENSSL_NO_DTLS1
  287. static int cert_chain = 0;
  288. #endif
  289. #ifndef OPENSSL_NO_PSK
  290. static char *psk_identity="Client_identity";
  291. char *psk_key=NULL; /* by default PSK is not used */
  292. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  293. unsigned char *psk, unsigned int max_psk_len)
  294. {
  295. unsigned int psk_len = 0;
  296. int ret;
  297. BIGNUM *bn = NULL;
  298. if (s_debug)
  299. BIO_printf(bio_s_out,"psk_server_cb\n");
  300. if (!identity)
  301. {
  302. BIO_printf(bio_err,"Error: client did not send PSK identity\n");
  303. goto out_err;
  304. }
  305. if (s_debug)
  306. BIO_printf(bio_s_out,"identity_len=%d identity=%s\n",
  307. identity ? (int)strlen(identity) : 0, identity);
  308. /* here we could lookup the given identity e.g. from a database */
  309. if (strcmp(identity, psk_identity) != 0)
  310. {
  311. BIO_printf(bio_s_out, "PSK error: client identity not found"
  312. " (got '%s' expected '%s')\n", identity,
  313. psk_identity);
  314. goto out_err;
  315. }
  316. if (s_debug)
  317. BIO_printf(bio_s_out, "PSK client identity found\n");
  318. /* convert the PSK key to binary */
  319. ret = BN_hex2bn(&bn, psk_key);
  320. if (!ret)
  321. {
  322. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  323. if (bn)
  324. BN_free(bn);
  325. return 0;
  326. }
  327. if (BN_num_bytes(bn) > (int)max_psk_len)
  328. {
  329. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  330. max_psk_len, BN_num_bytes(bn));
  331. BN_free(bn);
  332. return 0;
  333. }
  334. ret = BN_bn2bin(bn, psk);
  335. BN_free(bn);
  336. if (ret < 0)
  337. goto out_err;
  338. psk_len = (unsigned int)ret;
  339. if (s_debug)
  340. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  341. return psk_len;
  342. out_err:
  343. if (s_debug)
  344. BIO_printf(bio_err, "Error in PSK server callback\n");
  345. return 0;
  346. }
  347. #endif
  348. #ifndef OPENSSL_NO_SRP
  349. /* This is a context that we pass to callbacks */
  350. typedef struct srpsrvparm_st
  351. {
  352. int verbose;
  353. char *login;
  354. SRP_VBASE *vb;
  355. } srpsrvparm;
  356. static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  357. {
  358. srpsrvparm *p = arg;
  359. SRP_user_pwd *user;
  360. p->login = BUF_strdup(SSL_get_srp_username(s));
  361. BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
  362. user = SRP_VBASE_get_by_user(p->vb, p->login);
  363. if (user == NULL)
  364. {
  365. BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
  366. return SSL3_AL_FATAL;
  367. }
  368. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  369. user->info) < 0)
  370. {
  371. *ad = SSL_AD_INTERNAL_ERROR;
  372. return SSL3_AL_FATAL;
  373. }
  374. return SSL_ERROR_NONE;
  375. }
  376. #endif
  377. #ifdef MONOLITH
  378. static void s_server_init(void)
  379. {
  380. accept_socket=-1;
  381. cipher=NULL;
  382. s_server_verify=SSL_VERIFY_NONE;
  383. s_dcert_file=NULL;
  384. s_dkey_file=NULL;
  385. s_cert_file=TEST_CERT;
  386. s_key_file=NULL;
  387. #ifndef OPENSSL_NO_TLSEXT
  388. s_cert_file2=TEST_CERT2;
  389. s_key_file2=NULL;
  390. ctx2=NULL;
  391. #endif
  392. #ifdef FIONBIO
  393. s_nbio=0;
  394. #endif
  395. s_nbio_test=0;
  396. ctx=NULL;
  397. www=0;
  398. bio_s_out=NULL;
  399. s_debug=0;
  400. s_msg=0;
  401. s_quiet=0;
  402. hack=0;
  403. #ifndef OPENSSL_NO_ENGINE
  404. engine_id=NULL;
  405. #endif
  406. }
  407. #endif
  408. static void sv_usage(void)
  409. {
  410. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  411. BIO_printf(bio_err,"\n");
  412. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  413. BIO_printf(bio_err," -context arg - set session ID context\n");
  414. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  415. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  416. BIO_printf(bio_err," -cert arg - certificate file to use\n");
  417. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  418. BIO_printf(bio_err," -crl_check - check the peer certificate has not been revoked by its CA.\n" \
  419. " The CRL(s) are appended to the certificate file\n");
  420. BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
  421. " or any other CRL in the CA chain. CRL(s) are appened to the\n" \
  422. " the certificate file.\n");
  423. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  424. BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n");
  425. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  426. BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  427. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  428. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  429. BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n");
  430. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  431. BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  432. BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n");
  433. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  434. BIO_printf(bio_err," or a default set of parameters is used\n");
  435. #ifndef OPENSSL_NO_ECDH
  436. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  437. " Use \"openssl ecparam -list_curves\" for all names\n" \
  438. " (default is nistp256).\n");
  439. #endif
  440. #ifdef FIONBIO
  441. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  442. #endif
  443. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  444. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  445. BIO_printf(bio_err," -debug - Print more output\n");
  446. BIO_printf(bio_err," -msg - Show protocol messages\n");
  447. BIO_printf(bio_err," -state - Print the SSL states\n");
  448. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  449. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  450. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  451. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  452. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  453. BIO_printf(bio_err," -quiet - No server output\n");
  454. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  455. #ifndef OPENSSL_NO_PSK
  456. BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n");
  457. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  458. # ifndef OPENSSL_NO_JPAKE
  459. BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
  460. # endif
  461. #endif
  462. #ifndef OPENSSL_NO_SRP
  463. BIO_printf(bio_err," -srpvfile file - The verifier file for SRP\n");
  464. BIO_printf(bio_err," -srpuserseed string - A seed string for a default user salt.\n");
  465. #endif
  466. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  467. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  468. BIO_printf(bio_err," -tls1_2 - Just talk TLSv1.2\n");
  469. BIO_printf(bio_err," -tls1_1 - Just talk TLSv1.1\n");
  470. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  471. BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n");
  472. BIO_printf(bio_err," -timeout - Enable timeouts\n");
  473. BIO_printf(bio_err," -mtu - Set link layer MTU\n");
  474. BIO_printf(bio_err," -chain - Read a certificate chain\n");
  475. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  476. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  477. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  478. BIO_printf(bio_err," -no_tls1_1 - Just disable TLSv1.1\n");
  479. BIO_printf(bio_err," -no_tls1_2 - Just disable TLSv1.2\n");
  480. #ifndef OPENSSL_NO_DH
  481. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  482. #endif
  483. #ifndef OPENSSL_NO_ECDH
  484. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  485. #endif
  486. BIO_printf(bio_err, "-no_resume_ephemeral - Disable caching and tickets if ephemeral (EC)DH is used\n");
  487. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  488. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  489. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  490. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  491. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  492. #ifndef OPENSSL_NO_ENGINE
  493. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  494. #endif
  495. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  496. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  497. #ifndef OPENSSL_NO_TLSEXT
  498. BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n");
  499. BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  500. BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n");
  501. BIO_printf(bio_err," (default is %s)\n",TEST_CERT2);
  502. BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n");
  503. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2);
  504. BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
  505. BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
  506. BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
  507. # ifndef OPENSSL_NO_NEXTPROTONEG
  508. BIO_printf(bio_err," -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
  509. # endif
  510. #endif
  511. }
  512. static int local_argc=0;
  513. static char **local_argv;
  514. #ifdef CHARSET_EBCDIC
  515. static int ebcdic_new(BIO *bi);
  516. static int ebcdic_free(BIO *a);
  517. static int ebcdic_read(BIO *b, char *out, int outl);
  518. static int ebcdic_write(BIO *b, const char *in, int inl);
  519. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  520. static int ebcdic_gets(BIO *bp, char *buf, int size);
  521. static int ebcdic_puts(BIO *bp, const char *str);
  522. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  523. static BIO_METHOD methods_ebcdic=
  524. {
  525. BIO_TYPE_EBCDIC_FILTER,
  526. "EBCDIC/ASCII filter",
  527. ebcdic_write,
  528. ebcdic_read,
  529. ebcdic_puts,
  530. ebcdic_gets,
  531. ebcdic_ctrl,
  532. ebcdic_new,
  533. ebcdic_free,
  534. };
  535. typedef struct
  536. {
  537. size_t alloced;
  538. char buff[1];
  539. } EBCDIC_OUTBUFF;
  540. BIO_METHOD *BIO_f_ebcdic_filter()
  541. {
  542. return(&methods_ebcdic);
  543. }
  544. static int ebcdic_new(BIO *bi)
  545. {
  546. EBCDIC_OUTBUFF *wbuf;
  547. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  548. wbuf->alloced = 1024;
  549. wbuf->buff[0] = '\0';
  550. bi->ptr=(char *)wbuf;
  551. bi->init=1;
  552. bi->flags=0;
  553. return(1);
  554. }
  555. static int ebcdic_free(BIO *a)
  556. {
  557. if (a == NULL) return(0);
  558. if (a->ptr != NULL)
  559. OPENSSL_free(a->ptr);
  560. a->ptr=NULL;
  561. a->init=0;
  562. a->flags=0;
  563. return(1);
  564. }
  565. static int ebcdic_read(BIO *b, char *out, int outl)
  566. {
  567. int ret=0;
  568. if (out == NULL || outl == 0) return(0);
  569. if (b->next_bio == NULL) return(0);
  570. ret=BIO_read(b->next_bio,out,outl);
  571. if (ret > 0)
  572. ascii2ebcdic(out,out,ret);
  573. return(ret);
  574. }
  575. static int ebcdic_write(BIO *b, const char *in, int inl)
  576. {
  577. EBCDIC_OUTBUFF *wbuf;
  578. int ret=0;
  579. int num;
  580. unsigned char n;
  581. if ((in == NULL) || (inl <= 0)) return(0);
  582. if (b->next_bio == NULL) return(0);
  583. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  584. if (inl > (num = wbuf->alloced))
  585. {
  586. num = num + num; /* double the size */
  587. if (num < inl)
  588. num = inl;
  589. OPENSSL_free(wbuf);
  590. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  591. wbuf->alloced = num;
  592. wbuf->buff[0] = '\0';
  593. b->ptr=(char *)wbuf;
  594. }
  595. ebcdic2ascii(wbuf->buff, in, inl);
  596. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  597. return(ret);
  598. }
  599. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  600. {
  601. long ret;
  602. if (b->next_bio == NULL) return(0);
  603. switch (cmd)
  604. {
  605. case BIO_CTRL_DUP:
  606. ret=0L;
  607. break;
  608. default:
  609. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  610. break;
  611. }
  612. return(ret);
  613. }
  614. static int ebcdic_gets(BIO *bp, char *buf, int size)
  615. {
  616. int i, ret=0;
  617. if (bp->next_bio == NULL) return(0);
  618. /* return(BIO_gets(bp->next_bio,buf,size));*/
  619. for (i=0; i<size-1; ++i)
  620. {
  621. ret = ebcdic_read(bp,&buf[i],1);
  622. if (ret <= 0)
  623. break;
  624. else if (buf[i] == '\n')
  625. {
  626. ++i;
  627. break;
  628. }
  629. }
  630. if (i < size)
  631. buf[i] = '\0';
  632. return (ret < 0 && i == 0) ? ret : i;
  633. }
  634. static int ebcdic_puts(BIO *bp, const char *str)
  635. {
  636. if (bp->next_bio == NULL) return(0);
  637. return ebcdic_write(bp, str, strlen(str));
  638. }
  639. #endif
  640. #ifndef OPENSSL_NO_TLSEXT
  641. /* This is a context that we pass to callbacks */
  642. typedef struct tlsextctx_st {
  643. char * servername;
  644. BIO * biodebug;
  645. int extension_error;
  646. } tlsextctx;
  647. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  648. {
  649. tlsextctx * p = (tlsextctx *) arg;
  650. const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  651. if (servername && p->biodebug)
  652. BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername);
  653. if (!p->servername)
  654. return SSL_TLSEXT_ERR_NOACK;
  655. if (servername)
  656. {
  657. if (strcmp(servername,p->servername))
  658. return p->extension_error;
  659. if (ctx2)
  660. {
  661. BIO_printf(p->biodebug,"Switching server context.\n");
  662. SSL_set_SSL_CTX(s,ctx2);
  663. }
  664. }
  665. return SSL_TLSEXT_ERR_OK;
  666. }
  667. /* Structure passed to cert status callback */
  668. typedef struct tlsextstatusctx_st {
  669. /* Default responder to use */
  670. char *host, *path, *port;
  671. int use_ssl;
  672. int timeout;
  673. BIO *err;
  674. int verbose;
  675. } tlsextstatusctx;
  676. static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
  677. /* Certificate Status callback. This is called when a client includes a
  678. * certificate status request extension.
  679. *
  680. * This is a simplified version. It examines certificates each time and
  681. * makes one OCSP responder query for each request.
  682. *
  683. * A full version would store details such as the OCSP certificate IDs and
  684. * minimise the number of OCSP responses by caching them until they were
  685. * considered "expired".
  686. */
  687. static int cert_status_cb(SSL *s, void *arg)
  688. {
  689. tlsextstatusctx *srctx = arg;
  690. BIO *err = srctx->err;
  691. char *host, *port, *path;
  692. int use_ssl;
  693. unsigned char *rspder = NULL;
  694. int rspderlen;
  695. STACK_OF(OPENSSL_STRING) *aia = NULL;
  696. X509 *x = NULL;
  697. X509_STORE_CTX inctx;
  698. X509_OBJECT obj;
  699. OCSP_REQUEST *req = NULL;
  700. OCSP_RESPONSE *resp = NULL;
  701. OCSP_CERTID *id = NULL;
  702. STACK_OF(X509_EXTENSION) *exts;
  703. int ret = SSL_TLSEXT_ERR_NOACK;
  704. int i;
  705. #if 0
  706. STACK_OF(OCSP_RESPID) *ids;
  707. SSL_get_tlsext_status_ids(s, &ids);
  708. BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids));
  709. #endif
  710. if (srctx->verbose)
  711. BIO_puts(err, "cert_status: callback called\n");
  712. /* Build up OCSP query from server certificate */
  713. x = SSL_get_certificate(s);
  714. aia = X509_get1_ocsp(x);
  715. if (aia)
  716. {
  717. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  718. &host, &port, &path, &use_ssl))
  719. {
  720. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  721. goto err;
  722. }
  723. if (srctx->verbose)
  724. BIO_printf(err, "cert_status: AIA URL: %s\n",
  725. sk_OPENSSL_STRING_value(aia, 0));
  726. }
  727. else
  728. {
  729. if (!srctx->host)
  730. {
  731. BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
  732. goto done;
  733. }
  734. host = srctx->host;
  735. path = srctx->path;
  736. port = srctx->port;
  737. use_ssl = srctx->use_ssl;
  738. }
  739. if (!X509_STORE_CTX_init(&inctx,
  740. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  741. NULL, NULL))
  742. goto err;
  743. if (X509_STORE_get_by_subject(&inctx,X509_LU_X509,
  744. X509_get_issuer_name(x),&obj) <= 0)
  745. {
  746. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  747. X509_STORE_CTX_cleanup(&inctx);
  748. goto done;
  749. }
  750. req = OCSP_REQUEST_new();
  751. if (!req)
  752. goto err;
  753. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  754. X509_free(obj.data.x509);
  755. X509_STORE_CTX_cleanup(&inctx);
  756. if (!id)
  757. goto err;
  758. if (!OCSP_request_add0_id(req, id))
  759. goto err;
  760. id = NULL;
  761. /* Add any extensions to the request */
  762. SSL_get_tlsext_status_exts(s, &exts);
  763. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++)
  764. {
  765. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  766. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  767. goto err;
  768. }
  769. resp = process_responder(err, req, host, path, port, use_ssl, NULL,
  770. srctx->timeout);
  771. if (!resp)
  772. {
  773. BIO_puts(err, "cert_status: error querying responder\n");
  774. goto done;
  775. }
  776. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  777. if (rspderlen <= 0)
  778. goto err;
  779. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  780. if (srctx->verbose)
  781. {
  782. BIO_puts(err, "cert_status: ocsp response sent:\n");
  783. OCSP_RESPONSE_print(err, resp, 2);
  784. }
  785. ret = SSL_TLSEXT_ERR_OK;
  786. done:
  787. if (ret != SSL_TLSEXT_ERR_OK)
  788. ERR_print_errors(err);
  789. if (aia)
  790. {
  791. OPENSSL_free(host);
  792. OPENSSL_free(path);
  793. OPENSSL_free(port);
  794. X509_email_free(aia);
  795. }
  796. if (id)
  797. OCSP_CERTID_free(id);
  798. if (req)
  799. OCSP_REQUEST_free(req);
  800. if (resp)
  801. OCSP_RESPONSE_free(resp);
  802. return ret;
  803. err:
  804. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  805. goto done;
  806. }
  807. # ifndef OPENSSL_NO_NEXTPROTONEG
  808. /* This is the context that we pass to next_proto_cb */
  809. typedef struct tlsextnextprotoctx_st {
  810. unsigned char *data;
  811. unsigned int len;
  812. } tlsextnextprotoctx;
  813. static int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
  814. {
  815. tlsextnextprotoctx *next_proto = arg;
  816. *data = next_proto->data;
  817. *len = next_proto->len;
  818. return SSL_TLSEXT_ERR_OK;
  819. }
  820. # endif /* ndef OPENSSL_NO_NPN */
  821. #endif
  822. static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
  823. {
  824. /* disable resumption for sessions with forward secure ciphers */
  825. return is_forward_secure;
  826. }
  827. int MAIN(int, char **);
  828. #ifndef OPENSSL_NO_JPAKE
  829. static char *jpake_secret = NULL;
  830. #endif
  831. int MAIN(int argc, char *argv[])
  832. {
  833. X509_VERIFY_PARAM *vpm = NULL;
  834. int badarg = 0;
  835. short port=PORT;
  836. char *CApath=NULL,*CAfile=NULL;
  837. unsigned char *context = NULL;
  838. char *dhfile = NULL;
  839. #ifndef OPENSSL_NO_ECDH
  840. char *named_curve = NULL;
  841. #endif
  842. int badop=0,bugs=0;
  843. int ret=1;
  844. int off=0;
  845. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  846. int state=0;
  847. const SSL_METHOD *meth=NULL;
  848. int socket_type=SOCK_STREAM;
  849. ENGINE *e=NULL;
  850. char *inrand=NULL;
  851. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  852. char *passarg = NULL, *pass = NULL;
  853. char *dpassarg = NULL, *dpass = NULL;
  854. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  855. X509 *s_cert = NULL, *s_dcert = NULL;
  856. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  857. int no_cache = 0, ext_cache = 0;
  858. #ifndef OPENSSL_NO_TLSEXT
  859. EVP_PKEY *s_key2 = NULL;
  860. X509 *s_cert2 = NULL;
  861. #endif
  862. #ifndef OPENSSL_NO_TLSEXT
  863. tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
  864. # ifndef OPENSSL_NO_NEXTPROTONEG
  865. const char *next_proto_neg_in = NULL;
  866. tlsextnextprotoctx next_proto;
  867. # endif
  868. #endif
  869. #ifndef OPENSSL_NO_PSK
  870. /* by default do not send a PSK identity hint */
  871. static char *psk_identity_hint=NULL;
  872. #endif
  873. #ifndef OPENSSL_NO_SRP
  874. char *srpuserseed = NULL;
  875. char *srp_verifier_file = NULL;
  876. srpsrvparm p;
  877. #endif
  878. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  879. meth=SSLv23_server_method();
  880. #elif !defined(OPENSSL_NO_SSL3)
  881. meth=SSLv3_server_method();
  882. #elif !defined(OPENSSL_NO_SSL2)
  883. meth=SSLv2_server_method();
  884. #elif !defined(OPENSSL_NO_TLS1)
  885. meth=TLSv1_server_method();
  886. #else
  887. /* #error no SSL version enabled */
  888. #endif
  889. local_argc=argc;
  890. local_argv=argv;
  891. apps_startup();
  892. #ifdef MONOLITH
  893. s_server_init();
  894. #endif
  895. if (bio_err == NULL)
  896. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  897. if (!load_config(bio_err, NULL))
  898. goto end;
  899. verify_depth=0;
  900. #ifdef FIONBIO
  901. s_nbio=0;
  902. #endif
  903. s_nbio_test=0;
  904. argc--;
  905. argv++;
  906. while (argc >= 1)
  907. {
  908. if ((strcmp(*argv,"-port") == 0) ||
  909. (strcmp(*argv,"-accept") == 0))
  910. {
  911. if (--argc < 1) goto bad;
  912. if (!extract_port(*(++argv),&port))
  913. goto bad;
  914. }
  915. else if (strcmp(*argv,"-verify") == 0)
  916. {
  917. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  918. if (--argc < 1) goto bad;
  919. verify_depth=atoi(*(++argv));
  920. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  921. }
  922. else if (strcmp(*argv,"-Verify") == 0)
  923. {
  924. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  925. SSL_VERIFY_CLIENT_ONCE;
  926. if (--argc < 1) goto bad;
  927. verify_depth=atoi(*(++argv));
  928. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  929. }
  930. else if (strcmp(*argv,"-context") == 0)
  931. {
  932. if (--argc < 1) goto bad;
  933. context= (unsigned char *)*(++argv);
  934. }
  935. else if (strcmp(*argv,"-cert") == 0)
  936. {
  937. if (--argc < 1) goto bad;
  938. s_cert_file= *(++argv);
  939. }
  940. else if (strcmp(*argv,"-certform") == 0)
  941. {
  942. if (--argc < 1) goto bad;
  943. s_cert_format = str2fmt(*(++argv));
  944. }
  945. else if (strcmp(*argv,"-key") == 0)
  946. {
  947. if (--argc < 1) goto bad;
  948. s_key_file= *(++argv);
  949. }
  950. else if (strcmp(*argv,"-keyform") == 0)
  951. {
  952. if (--argc < 1) goto bad;
  953. s_key_format = str2fmt(*(++argv));
  954. }
  955. else if (strcmp(*argv,"-pass") == 0)
  956. {
  957. if (--argc < 1) goto bad;
  958. passarg = *(++argv);
  959. }
  960. else if (strcmp(*argv,"-dhparam") == 0)
  961. {
  962. if (--argc < 1) goto bad;
  963. dhfile = *(++argv);
  964. }
  965. #ifndef OPENSSL_NO_ECDH
  966. else if (strcmp(*argv,"-named_curve") == 0)
  967. {
  968. if (--argc < 1) goto bad;
  969. named_curve = *(++argv);
  970. }
  971. #endif
  972. else if (strcmp(*argv,"-dcertform") == 0)
  973. {
  974. if (--argc < 1) goto bad;
  975. s_dcert_format = str2fmt(*(++argv));
  976. }
  977. else if (strcmp(*argv,"-dcert") == 0)
  978. {
  979. if (--argc < 1) goto bad;
  980. s_dcert_file= *(++argv);
  981. }
  982. else if (strcmp(*argv,"-dkeyform") == 0)
  983. {
  984. if (--argc < 1) goto bad;
  985. s_dkey_format = str2fmt(*(++argv));
  986. }
  987. else if (strcmp(*argv,"-dpass") == 0)
  988. {
  989. if (--argc < 1) goto bad;
  990. dpassarg = *(++argv);
  991. }
  992. else if (strcmp(*argv,"-dkey") == 0)
  993. {
  994. if (--argc < 1) goto bad;
  995. s_dkey_file= *(++argv);
  996. }
  997. else if (strcmp(*argv,"-nocert") == 0)
  998. {
  999. nocert=1;
  1000. }
  1001. else if (strcmp(*argv,"-CApath") == 0)
  1002. {
  1003. if (--argc < 1) goto bad;
  1004. CApath= *(++argv);
  1005. }
  1006. else if (strcmp(*argv,"-no_cache") == 0)
  1007. no_cache = 1;
  1008. else if (strcmp(*argv,"-ext_cache") == 0)
  1009. ext_cache = 1;
  1010. else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
  1011. {
  1012. if (badarg)
  1013. goto bad;
  1014. continue;
  1015. }
  1016. else if (strcmp(*argv,"-verify_return_error") == 0)
  1017. verify_return_error = 1;
  1018. else if (strcmp(*argv,"-serverpref") == 0)
  1019. { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
  1020. else if (strcmp(*argv,"-legacy_renegotiation") == 0)
  1021. off|=SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  1022. else if (strcmp(*argv,"-cipher") == 0)
  1023. {
  1024. if (--argc < 1) goto bad;
  1025. cipher= *(++argv);
  1026. }
  1027. else if (strcmp(*argv,"-CAfile") == 0)
  1028. {
  1029. if (--argc < 1) goto bad;
  1030. CAfile= *(++argv);
  1031. }
  1032. #ifdef FIONBIO
  1033. else if (strcmp(*argv,"-nbio") == 0)
  1034. { s_nbio=1; }
  1035. #endif
  1036. else if (strcmp(*argv,"-nbio_test") == 0)
  1037. {
  1038. #ifdef FIONBIO
  1039. s_nbio=1;
  1040. #endif
  1041. s_nbio_test=1;
  1042. }
  1043. else if (strcmp(*argv,"-debug") == 0)
  1044. { s_debug=1; }
  1045. #ifndef OPENSSL_NO_TLSEXT
  1046. else if (strcmp(*argv,"-tlsextdebug") == 0)
  1047. s_tlsextdebug=1;
  1048. else if (strcmp(*argv,"-status") == 0)
  1049. s_tlsextstatus=1;
  1050. else if (strcmp(*argv,"-status_verbose") == 0)
  1051. {
  1052. s_tlsextstatus=1;
  1053. tlscstatp.verbose = 1;
  1054. }
  1055. else if (!strcmp(*argv, "-status_timeout"))
  1056. {
  1057. s_tlsextstatus=1;
  1058. if (--argc < 1) goto bad;
  1059. tlscstatp.timeout = atoi(*(++argv));
  1060. }
  1061. else if (!strcmp(*argv, "-status_url"))
  1062. {
  1063. s_tlsextstatus=1;
  1064. if (--argc < 1) goto bad;
  1065. if (!OCSP_parse_url(*(++argv),
  1066. &tlscstatp.host,
  1067. &tlscstatp.port,
  1068. &tlscstatp.path,
  1069. &tlscstatp.use_ssl))
  1070. {
  1071. BIO_printf(bio_err, "Error parsing URL\n");
  1072. goto bad;
  1073. }
  1074. }
  1075. #endif
  1076. else if (strcmp(*argv,"-msg") == 0)
  1077. { s_msg=1; }
  1078. else if (strcmp(*argv,"-hack") == 0)
  1079. { hack=1; }
  1080. else if (strcmp(*argv,"-state") == 0)
  1081. { state=1; }
  1082. else if (strcmp(*argv,"-crlf") == 0)
  1083. { s_crlf=1; }
  1084. else if (strcmp(*argv,"-quiet") == 0)
  1085. { s_quiet=1; }
  1086. else if (strcmp(*argv,"-bugs") == 0)
  1087. { bugs=1; }
  1088. else if (strcmp(*argv,"-no_tmp_rsa") == 0)
  1089. { no_tmp_rsa=1; }
  1090. else if (strcmp(*argv,"-no_dhe") == 0)
  1091. { no_dhe=1; }
  1092. else if (strcmp(*argv,"-no_ecdhe") == 0)
  1093. { no_ecdhe=1; }
  1094. else if (strcmp(*argv,"-no_resume_ephemeral") == 0)
  1095. { no_resume_ephemeral = 1; }
  1096. #ifndef OPENSSL_NO_PSK
  1097. else if (strcmp(*argv,"-psk_hint") == 0)
  1098. {
  1099. if (--argc < 1) goto bad;
  1100. psk_identity_hint= *(++argv);
  1101. }
  1102. else if (strcmp(*argv,"-psk") == 0)
  1103. {
  1104. size_t i;
  1105. if (--argc < 1) goto bad;
  1106. psk_key=*(++argv);
  1107. for (i=0; i<strlen(psk_key); i++)
  1108. {
  1109. if (isxdigit((int)psk_key[i]))
  1110. continue;
  1111. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  1112. goto bad;
  1113. }
  1114. }
  1115. #endif
  1116. #ifndef OPENSSL_NO_SRP
  1117. else if (strcmp(*argv, "-srpvfile") == 0)
  1118. {
  1119. if (--argc < 1) goto bad;
  1120. srp_verifier_file = *(++argv);
  1121. meth = TLSv1_server_method();
  1122. }
  1123. else if (strcmp(*argv, "-srpuserseed") == 0)
  1124. {
  1125. if (--argc < 1) goto bad;
  1126. srpuserseed = *(++argv);
  1127. meth = TLSv1_server_method();
  1128. }
  1129. #endif
  1130. else if (strcmp(*argv,"-www") == 0)
  1131. { www=1; }
  1132. else if (strcmp(*argv,"-WWW") == 0)
  1133. { www=2; }
  1134. else if (strcmp(*argv,"-HTTP") == 0)
  1135. { www=3; }
  1136. else if (strcmp(*argv,"-no_ssl2") == 0)
  1137. { off|=SSL_OP_NO_SSLv2; }
  1138. else if (strcmp(*argv,"-no_ssl3") == 0)
  1139. { off|=SSL_OP_NO_SSLv3; }
  1140. else if (strcmp(*argv,"-no_tls1_2") == 0)
  1141. { off|=SSL_OP_NO_TLSv1_2; }
  1142. else if (strcmp(*argv,"-no_tls1_1") == 0)
  1143. { off|=SSL_OP_NO_TLSv1_1; }
  1144. else if (strcmp(*argv,"-no_tls1") == 0)
  1145. { off|=SSL_OP_NO_TLSv1; }
  1146. else if (strcmp(*argv,"-no_comp") == 0)
  1147. { off|=SSL_OP_NO_COMPRESSION; }
  1148. #ifndef OPENSSL_NO_TLSEXT
  1149. else if (strcmp(*argv,"-no_ticket") == 0)
  1150. { off|=SSL_OP_NO_TICKET; }
  1151. #endif
  1152. #ifndef OPENSSL_NO_SSL2
  1153. else if (strcmp(*argv,"-ssl2") == 0)
  1154. { meth=SSLv2_server_method(); }
  1155. #endif
  1156. #ifndef OPENSSL_NO_SSL3
  1157. else if (strcmp(*argv,"-ssl3") == 0)
  1158. { meth=SSLv3_server_method(); }
  1159. #endif
  1160. #ifndef OPENSSL_NO_TLS1
  1161. else if (strcmp(*argv,"-tls1_2") == 0)
  1162. { meth=TLSv1_2_server_method(); }
  1163. else if (strcmp(*argv,"-tls1_1") == 0)
  1164. { meth=TLSv1_1_server_method(); }
  1165. else if (strcmp(*argv,"-tls1") == 0)
  1166. { meth=TLSv1_server_method(); }
  1167. else if (strcmp(*argv,"-tls1_1") == 0)
  1168. { meth=TLSv1_1_server_method(); }
  1169. #endif
  1170. #ifndef OPENSSL_NO_DTLS1
  1171. else if (strcmp(*argv,"-dtls1") == 0)
  1172. {
  1173. meth=DTLSv1_server_method();
  1174. socket_type = SOCK_DGRAM;
  1175. }
  1176. else if (strcmp(*argv,"-timeout") == 0)
  1177. enable_timeouts = 1;
  1178. else if (strcmp(*argv,"-mtu") == 0)
  1179. {
  1180. if (--argc < 1) goto bad;
  1181. socket_mtu = atol(*(++argv));
  1182. }
  1183. else if (strcmp(*argv, "-chain") == 0)
  1184. cert_chain = 1;
  1185. #endif
  1186. else if (strcmp(*argv, "-id_prefix") == 0)
  1187. {
  1188. if (--argc < 1) goto bad;
  1189. session_id_prefix = *(++argv);
  1190. }
  1191. #ifndef OPENSSL_NO_ENGINE
  1192. else if (strcmp(*argv,"-engine") == 0)
  1193. {
  1194. if (--argc < 1) goto bad;
  1195. engine_id= *(++argv);
  1196. }
  1197. #endif
  1198. else if (strcmp(*argv,"-rand") == 0)
  1199. {
  1200. if (--argc < 1) goto bad;
  1201. inrand= *(++argv);
  1202. }
  1203. #ifndef OPENSSL_NO_TLSEXT
  1204. else if (strcmp(*argv,"-servername") == 0)
  1205. {
  1206. if (--argc < 1) goto bad;
  1207. tlsextcbp.servername= *(++argv);
  1208. }
  1209. else if (strcmp(*argv,"-servername_fatal") == 0)
  1210. { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; }
  1211. else if (strcmp(*argv,"-cert2") == 0)
  1212. {
  1213. if (--argc < 1) goto bad;
  1214. s_cert_file2= *(++argv);
  1215. }
  1216. else if (strcmp(*argv,"-key2") == 0)
  1217. {
  1218. if (--argc < 1) goto bad;
  1219. s_key_file2= *(++argv);
  1220. }
  1221. # ifndef OPENSSL_NO_NEXTPROTONEG
  1222. else if (strcmp(*argv,"-nextprotoneg") == 0)
  1223. {
  1224. if (--argc < 1) goto bad;
  1225. next_proto_neg_in = *(++argv);
  1226. }
  1227. # endif
  1228. #endif
  1229. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1230. else if (strcmp(*argv,"-jpake") == 0)
  1231. {
  1232. if (--argc < 1) goto bad;
  1233. jpake_secret = *(++argv);
  1234. }
  1235. #endif
  1236. else
  1237. {
  1238. BIO_printf(bio_err,"unknown option %s\n",*argv);
  1239. badop=1;
  1240. break;
  1241. }
  1242. argc--;
  1243. argv++;
  1244. }
  1245. if (badop)
  1246. {
  1247. bad:
  1248. sv_usage();
  1249. goto end;
  1250. }
  1251. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1252. if (jpake_secret)
  1253. {
  1254. if (psk_key)
  1255. {
  1256. BIO_printf(bio_err,
  1257. "Can't use JPAKE and PSK together\n");
  1258. goto end;
  1259. }
  1260. psk_identity = "JPAKE";
  1261. if (cipher)
  1262. {
  1263. BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
  1264. goto end;
  1265. }
  1266. cipher = "PSK";
  1267. }
  1268. #endif
  1269. SSL_load_error_strings();
  1270. OpenSSL_add_ssl_algorithms();
  1271. #ifndef OPENSSL_NO_ENGINE
  1272. e = setup_engine(bio_err, engine_id, 1);
  1273. #endif
  1274. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
  1275. {
  1276. BIO_printf(bio_err, "Error getting password\n");
  1277. goto end;
  1278. }
  1279. if (s_key_file == NULL)
  1280. s_key_file = s_cert_file;
  1281. #ifndef OPENSSL_NO_TLSEXT
  1282. if (s_key_file2 == NULL)
  1283. s_key_file2 = s_cert_file2;
  1284. #endif
  1285. if (nocert == 0)
  1286. {
  1287. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  1288. "server certificate private key file");
  1289. if (!s_key)
  1290. {
  1291. ERR_print_errors(bio_err);
  1292. goto end;
  1293. }
  1294. s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
  1295. NULL, e, "server certificate file");
  1296. if (!s_cert)
  1297. {
  1298. ERR_print_errors(bio_err);
  1299. goto end;
  1300. }
  1301. #ifndef OPENSSL_NO_TLSEXT
  1302. if (tlsextcbp.servername)
  1303. {
  1304. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  1305. "second server certificate private key file");
  1306. if (!s_key2)
  1307. {
  1308. ERR_print_errors(bio_err);
  1309. goto end;
  1310. }
  1311. s_cert2 = load_cert(bio_err,s_cert_file2,s_cert_format,
  1312. NULL, e, "second server certificate file");
  1313. if (!s_cert2)
  1314. {
  1315. ERR_print_errors(bio_err);
  1316. goto end;
  1317. }
  1318. }
  1319. # ifndef OPENSSL_NO_NEXTPROTONEG
  1320. if (next_proto_neg_in)
  1321. {
  1322. unsigned short len;
  1323. next_proto.data = next_protos_parse(&len,
  1324. next_proto_neg_in);
  1325. if (next_proto.data == NULL)
  1326. goto end;
  1327. next_proto.len = len;
  1328. }
  1329. else
  1330. {
  1331. next_proto.data = NULL;
  1332. }
  1333. # endif
  1334. #endif
  1335. }
  1336. if (s_dcert_file)
  1337. {
  1338. if (s_dkey_file == NULL)
  1339. s_dkey_file = s_dcert_file;
  1340. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  1341. 0, dpass, e,
  1342. "second certificate private key file");
  1343. if (!s_dkey)
  1344. {
  1345. ERR_print_errors(bio_err);
  1346. goto end;
  1347. }
  1348. s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
  1349. NULL, e, "second server certificate file");
  1350. if (!s_dcert)
  1351. {
  1352. ERR_print_errors(bio_err);
  1353. goto end;
  1354. }
  1355. }
  1356. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1357. && !RAND_status())
  1358. {
  1359. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  1360. }
  1361. if (inrand != NULL)
  1362. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  1363. app_RAND_load_files(inrand));
  1364. if (bio_s_out == NULL)
  1365. {
  1366. if (s_quiet && !s_debug && !s_msg)
  1367. {
  1368. bio_s_out=BIO_new(BIO_s_null());
  1369. }
  1370. else
  1371. {
  1372. if (bio_s_out == NULL)
  1373. bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  1374. }
  1375. }
  1376. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1377. if (nocert)
  1378. #endif
  1379. {
  1380. s_cert_file=NULL;
  1381. s_key_file=NULL;
  1382. s_dcert_file=NULL;
  1383. s_dkey_file=NULL;
  1384. #ifndef OPENSSL_NO_TLSEXT
  1385. s_cert_file2=NULL;
  1386. s_key_file2=NULL;
  1387. #endif
  1388. }
  1389. ctx=SSL_CTX_new(meth);
  1390. if (ctx == NULL)
  1391. {
  1392. ERR_print_errors(bio_err);
  1393. goto end;
  1394. }
  1395. if (session_id_prefix)
  1396. {
  1397. if(strlen(session_id_prefix) >= 32)
  1398. BIO_printf(bio_err,
  1399. "warning: id_prefix is too long, only one new session will be possible\n");
  1400. else if(strlen(session_id_prefix) >= 16)
  1401. BIO_printf(bio_err,
  1402. "warning: id_prefix is too long if you use SSLv2\n");
  1403. if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
  1404. {
  1405. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1406. ERR_print_errors(bio_err);
  1407. goto end;
  1408. }
  1409. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1410. }
  1411. SSL_CTX_set_quiet_shutdown(ctx,1);
  1412. if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
  1413. if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1414. SSL_CTX_set_options(ctx,off);
  1415. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1416. * Setting read ahead solves this problem.
  1417. */
  1418. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  1419. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  1420. if (no_cache)
  1421. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  1422. else if (ext_cache)
  1423. init_session_cache_ctx(ctx);
  1424. else
  1425. SSL_CTX_sess_set_cache_size(ctx,128);
  1426. #if 0
  1427. if (cipher == NULL) cipher=getenv("SSL_CIPHER");
  1428. #endif
  1429. #if 0
  1430. if (s_cert_file == NULL)
  1431. {
  1432. BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
  1433. goto end;
  1434. }
  1435. #endif
  1436. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  1437. (!SSL_CTX_set_default_verify_paths(ctx)))
  1438. {
  1439. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1440. ERR_print_errors(bio_err);
  1441. /* goto end; */
  1442. }
  1443. if (vpm)
  1444. SSL_CTX_set1_param(ctx, vpm);
  1445. #ifndef OPENSSL_NO_TLSEXT
  1446. if (s_cert2)
  1447. {
  1448. ctx2=SSL_CTX_new(meth);
  1449. if (ctx2 == NULL)
  1450. {
  1451. ERR_print_errors(bio_err);
  1452. goto end;
  1453. }
  1454. }
  1455. if (ctx2)
  1456. {
  1457. BIO_printf(bio_s_out,"Setting secondary ctx parameters\n");
  1458. if (session_id_prefix)
  1459. {
  1460. if(strlen(session_id_prefix) >= 32)
  1461. BIO_printf(bio_err,
  1462. "warning: id_prefix is too long, only one new session will be possible\n");
  1463. else if(strlen(session_id_prefix) >= 16)
  1464. BIO_printf(bio_err,
  1465. "warning: id_prefix is too long if you use SSLv2\n");
  1466. if(!SSL_CTX_set_generate_session_id(ctx2, generate_session_id))
  1467. {
  1468. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1469. ERR_print_errors(bio_err);
  1470. goto end;
  1471. }
  1472. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1473. }
  1474. SSL_CTX_set_quiet_shutdown(ctx2,1);
  1475. if (bugs) SSL_CTX_set_options(ctx2,SSL_OP_ALL);
  1476. if (hack) SSL_CTX_set_options(ctx2,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1477. SSL_CTX_set_options(ctx2,off);
  1478. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1479. * Setting read ahead solves this problem.
  1480. */
  1481. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx2, 1);
  1482. if (state) SSL_CTX_set_info_callback(ctx2,apps_ssl_info_callback);
  1483. if (no_cache)
  1484. SSL_CTX_set_session_cache_mode(ctx2,SSL_SESS_CACHE_OFF);
  1485. else if (ext_cache)
  1486. init_session_cache_ctx(ctx2);
  1487. else
  1488. SSL_CTX_sess_set_cache_size(ctx2,128);
  1489. if ((!SSL_CTX_load_verify_locations(ctx2,CAfile,CApath)) ||
  1490. (!SSL_CTX_set_default_verify_paths(ctx2)))
  1491. {
  1492. ERR_print_errors(bio_err);
  1493. }
  1494. if (vpm)
  1495. SSL_CTX_set1_param(ctx2, vpm);
  1496. }
  1497. # ifndef OPENSSL_NO_NEXTPROTONEG
  1498. if (next_proto.data)
  1499. SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto);
  1500. # endif
  1501. #endif
  1502. #ifndef OPENSSL_NO_DH
  1503. if (!no_dhe)
  1504. {
  1505. DH *dh=NULL;
  1506. if (dhfile)
  1507. dh = load_dh_param(dhfile);
  1508. else if (s_cert_file)
  1509. dh = load_dh_param(s_cert_file);
  1510. if (dh != NULL)
  1511. {
  1512. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1513. }
  1514. else
  1515. {
  1516. BIO_printf(bio_s_out,"Using default temp DH parameters\n");
  1517. dh=get_dh512();
  1518. }
  1519. (void)BIO_flush(bio_s_out);
  1520. SSL_CTX_set_tmp_dh(ctx,dh);
  1521. #ifndef OPENSSL_NO_TLSEXT
  1522. if (ctx2)
  1523. {
  1524. if (!dhfile)
  1525. {
  1526. DH *dh2=load_dh_param(s_cert_file2);
  1527. if (dh2 != NULL)
  1528. {
  1529. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1530. (void)BIO_flush(bio_s_out);
  1531. DH_free(dh);
  1532. dh = dh2;
  1533. }
  1534. }
  1535. SSL_CTX_set_tmp_dh(ctx2,dh);
  1536. }
  1537. #endif
  1538. DH_free(dh);
  1539. }
  1540. #endif
  1541. #ifndef OPENSSL_NO_ECDH
  1542. if (!no_ecdhe)
  1543. {
  1544. EC_KEY *ecdh=NULL;
  1545. if (named_curve)
  1546. {
  1547. int nid = OBJ_sn2nid(named_curve);
  1548. if (nid == 0)
  1549. {
  1550. BIO_printf(bio_err, "unknown curve name (%s)\n",
  1551. named_curve);
  1552. goto end;
  1553. }
  1554. ecdh = EC_KEY_new_by_curve_name(nid);
  1555. if (ecdh == NULL)
  1556. {
  1557. BIO_printf(bio_err, "unable to create curve (%s)\n",
  1558. named_curve);
  1559. goto end;
  1560. }
  1561. }
  1562. if (ecdh != NULL)
  1563. {
  1564. BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
  1565. }
  1566. else
  1567. {
  1568. BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
  1569. ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  1570. if (ecdh == NULL)
  1571. {
  1572. BIO_printf(bio_err, "unable to create curve (nistp256)\n");
  1573. goto end;
  1574. }
  1575. }
  1576. (void)BIO_flush(bio_s_out);
  1577. SSL_CTX_set_tmp_ecdh(ctx,ecdh);
  1578. #ifndef OPENSSL_NO_TLSEXT
  1579. if (ctx2)
  1580. SSL_CTX_set_tmp_ecdh(ctx2,ecdh);
  1581. #endif
  1582. EC_KEY_free(ecdh);
  1583. }
  1584. #endif
  1585. if (!set_cert_key_stuff(ctx,s_cert,s_key))
  1586. goto end;
  1587. #ifndef OPENSSL_NO_TLSEXT
  1588. if (ctx2 && !set_cert_key_stuff(ctx2,s_cert2,s_key2))
  1589. goto end;
  1590. #endif
  1591. if (s_dcert != NULL)
  1592. {
  1593. if (!set_cert_key_stuff(ctx,s_dcert,s_dkey))
  1594. goto end;
  1595. }
  1596. #ifndef OPENSSL_NO_RSA
  1597. #if 1
  1598. if (!no_tmp_rsa)
  1599. {
  1600. SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
  1601. #ifndef OPENSSL_NO_TLSEXT
  1602. if (ctx2)
  1603. SSL_CTX_set_tmp_rsa_callback(ctx2,tmp_rsa_cb);
  1604. #endif
  1605. }
  1606. #else
  1607. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
  1608. {
  1609. RSA *rsa;
  1610. BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
  1611. BIO_flush(bio_s_out);
  1612. rsa=RSA_generate_key(512,RSA_F4,NULL);
  1613. if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
  1614. {
  1615. ERR_print_errors(bio_err);
  1616. goto end;
  1617. }
  1618. #ifndef OPENSSL_NO_TLSEXT
  1619. if (ctx2)
  1620. {
  1621. if (!SSL_CTX_set_tmp_rsa(ctx2,rsa))
  1622. {
  1623. ERR_print_errors(bio_err);
  1624. goto end;
  1625. }
  1626. }
  1627. #endif
  1628. RSA_free(rsa);
  1629. BIO_printf(bio_s_out,"\n");
  1630. }
  1631. #endif
  1632. #endif
  1633. if (no_resume_ephemeral)
  1634. {
  1635. SSL_CTX_set_not_resumable_session_callback(ctx, not_resumable_sess_cb);
  1636. #ifndef OPENSSL_NO_TLSEXT
  1637. if (ctx2)
  1638. SSL_CTX_set_not_resumable_session_callback(ctx2, not_resumable_sess_cb);
  1639. #endif
  1640. }
  1641. #ifndef OPENSSL_NO_PSK
  1642. #ifdef OPENSSL_NO_JPAKE
  1643. if (psk_key != NULL)
  1644. #else
  1645. if (psk_key != NULL || jpake_secret)
  1646. #endif
  1647. {
  1648. if (s_debug)
  1649. BIO_printf(bio_s_out, "PSK key given or JPAKE in use, setting server callback\n");
  1650. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1651. }
  1652. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint))
  1653. {
  1654. BIO_printf(bio_err,"error setting PSK identity hint to context\n");
  1655. ERR_print_errors(bio_err);
  1656. goto end;
  1657. }
  1658. #endif
  1659. if (cipher != NULL)
  1660. {
  1661. if(!SSL_CTX_set_cipher_list(ctx,cipher))
  1662. {
  1663. BIO_printf(bio_err,"error setting cipher list\n");
  1664. ERR_print_errors(bio_err);
  1665. goto end;
  1666. }
  1667. #ifndef OPENSSL_NO_TLSEXT
  1668. if (ctx2 && !SSL_CTX_set_cipher_list(ctx2,cipher))
  1669. {
  1670. BIO_printf(bio_err,"error setting cipher list\n");
  1671. ERR_print_errors(bio_err);
  1672. goto end;
  1673. }
  1674. #endif
  1675. }
  1676. SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
  1677. SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
  1678. sizeof s_server_session_id_context);
  1679. /* Set DTLS cookie generation and verification callbacks */
  1680. SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
  1681. SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
  1682. #ifndef OPENSSL_NO_TLSEXT
  1683. if (ctx2)
  1684. {
  1685. SSL_CTX_set_verify(ctx2,s_server_verify,verify_callback);
  1686. SSL_CTX_set_session_id_context(ctx2,(void*)&s_server_session_id_context,
  1687. sizeof s_server_session_id_context);
  1688. tlsextcbp.biodebug = bio_s_out;
  1689. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1690. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1691. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1692. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1693. }
  1694. #endif
  1695. #ifndef OPENSSL_NO_SRP
  1696. if (srp_verifier_file != NULL)
  1697. {
  1698. p.vb = SRP_VBASE_new(srpuserseed);
  1699. if ((ret = SRP_VBASE_init(p.vb, srp_verifier_file)) != SRP_NO_ERROR)
  1700. {
  1701. BIO_printf(bio_err,
  1702. "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
  1703. srp_verifier_file,ret);
  1704. goto end;
  1705. }
  1706. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE,verify_callback);
  1707. SSL_CTX_set_srp_cb_arg(ctx, &p);
  1708. SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
  1709. }
  1710. else
  1711. #endif
  1712. if (CAfile != NULL)
  1713. {
  1714. SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
  1715. #ifndef OPENSSL_NO_TLSEXT
  1716. if (ctx2)
  1717. SSL_CTX_set_client_CA_list(ctx2,SSL_load_client_CA_file(CAfile));
  1718. #endif
  1719. }
  1720. BIO_printf(bio_s_out,"ACCEPT\n");
  1721. (void)BIO_flush(bio_s_out);
  1722. if (www)
  1723. do_server(port,socket_type,&accept_socket,www_body, context);
  1724. else
  1725. do_server(port,socket_type,&accept_socket,sv_body, context);
  1726. print_stats(bio_s_out,ctx);
  1727. ret=0;
  1728. end:
  1729. if (ctx != NULL) SSL_CTX_free(ctx);
  1730. if (s_cert)
  1731. X509_free(s_cert);
  1732. if (s_dcert)
  1733. X509_free(s_dcert);
  1734. if (s_key)
  1735. EVP_PKEY_free(s_key);
  1736. if (s_dkey)
  1737. EVP_PKEY_free(s_dkey);
  1738. if (pass)
  1739. OPENSSL_free(pass);
  1740. if (dpass)
  1741. OPENSSL_free(dpass);
  1742. free_sessions();
  1743. #ifndef OPENSSL_NO_TLSEXT
  1744. if (ctx2 != NULL) SSL_CTX_free(ctx2);
  1745. if (s_cert2)
  1746. X509_free(s_cert2);
  1747. if (s_key2)
  1748. EVP_PKEY_free(s_key2);
  1749. #endif
  1750. if (bio_s_out != NULL)
  1751. {
  1752. BIO_free(bio_s_out);
  1753. bio_s_out=NULL;
  1754. }
  1755. apps_shutdown();
  1756. OPENSSL_EXIT(ret);
  1757. }
  1758. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1759. {
  1760. BIO_printf(bio,"%4ld items in the session cache\n",
  1761. SSL_CTX_sess_number(ssl_ctx));
  1762. BIO_printf(bio,"%4ld client connects (SSL_connect())\n",
  1763. SSL_CTX_sess_connect(ssl_ctx));
  1764. BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n",
  1765. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1766. BIO_printf(bio,"%4ld client connects that finished\n",
  1767. SSL_CTX_sess_connect_good(ssl_ctx));
  1768. BIO_printf(bio,"%4ld server accepts (SSL_accept())\n",
  1769. SSL_CTX_sess_accept(ssl_ctx));
  1770. BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n",
  1771. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1772. BIO_printf(bio,"%4ld server accepts that finished\n",
  1773. SSL_CTX_sess_accept_good(ssl_ctx));
  1774. BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
  1775. BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
  1776. BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
  1777. BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
  1778. BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n",
  1779. SSL_CTX_sess_cache_full(ssl_ctx),
  1780. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1781. }
  1782. static int sv_body(char *hostname, int s, unsigned char *context)
  1783. {
  1784. char *buf=NULL;
  1785. fd_set readfds;
  1786. int ret=1,width;
  1787. int k,i;
  1788. unsigned long l;
  1789. SSL *con=NULL;
  1790. BIO *sbio;
  1791. #ifndef OPENSSL_NO_KRB5
  1792. KSSL_CTX *kctx;
  1793. #endif
  1794. struct timeval timeout;
  1795. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1796. struct timeval tv;
  1797. #else
  1798. struct timeval *timeoutp;
  1799. #endif
  1800. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1801. {
  1802. BIO_printf(bio_err,"out of memory\n");
  1803. goto err;
  1804. }
  1805. #ifdef FIONBIO
  1806. if (s_nbio)
  1807. {
  1808. unsigned long sl=1;
  1809. if (!s_quiet)
  1810. BIO_printf(bio_err,"turning on non blocking io\n");
  1811. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1812. ERR_print_errors(bio_err);
  1813. }
  1814. #endif
  1815. if (con == NULL) {
  1816. con=SSL_new(ctx);
  1817. #ifndef OPENSSL_NO_TLSEXT
  1818. if (s_tlsextdebug)
  1819. {
  1820. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1821. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1822. }
  1823. if (s_tlsextstatus)
  1824. {
  1825. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1826. tlscstatp.err = bio_err;
  1827. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1828. }
  1829. #endif
  1830. #ifndef OPENSSL_NO_KRB5
  1831. if ((kctx = kssl_ctx_new()) != NULL)
  1832. {
  1833. SSL_set0_kssl_ctx(con, kctx);
  1834. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  1835. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  1836. }
  1837. #endif /* OPENSSL_NO_KRB5 */
  1838. if(context)
  1839. SSL_set_session_id_context(con, context,
  1840. strlen((char *)context));
  1841. }
  1842. SSL_clear(con);
  1843. #if 0
  1844. #ifdef TLSEXT_TYPE_opaque_prf_input
  1845. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  1846. #endif
  1847. #endif
  1848. if (SSL_version(con) == DTLS1_VERSION)
  1849. {
  1850. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1851. if (enable_timeouts)
  1852. {
  1853. timeout.tv_sec = 0;
  1854. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1855. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1856. timeout.tv_sec = 0;
  1857. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1858. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1859. }
  1860. if (socket_mtu > 28)
  1861. {
  1862. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1863. SSL_set_mtu(con, socket_mtu - 28);
  1864. }
  1865. else
  1866. /* want to do MTU discovery */
  1867. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1868. /* turn on cookie exchange */
  1869. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1870. }
  1871. else
  1872. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1873. if (s_nbio_test)
  1874. {
  1875. BIO *test;
  1876. test=BIO_new(BIO_f_nbio_test());
  1877. sbio=BIO_push(test,sbio);
  1878. }
  1879. #ifndef OPENSSL_NO_JPAKE
  1880. if(jpake_secret)
  1881. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  1882. #endif
  1883. SSL_set_bio(con,sbio,sbio);
  1884. SSL_set_accept_state(con);
  1885. /* SSL_set_fd(con,s); */
  1886. if (s_debug)
  1887. {
  1888. SSL_set_debug(con, 1);
  1889. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1890. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  1891. }
  1892. if (s_msg)
  1893. {
  1894. SSL_set_msg_callback(con, msg_cb);
  1895. SSL_set_msg_callback_arg(con, bio_s_out);
  1896. }
  1897. #ifndef OPENSSL_NO_TLSEXT
  1898. if (s_tlsextdebug)
  1899. {
  1900. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1901. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1902. }
  1903. #endif
  1904. width=s+1;
  1905. for (;;)
  1906. {
  1907. int read_from_terminal;
  1908. int read_from_sslcon;
  1909. read_from_terminal = 0;
  1910. read_from_sslcon = SSL_pending(con);
  1911. if (!read_from_sslcon)
  1912. {
  1913. FD_ZERO(&readfds);
  1914. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1915. openssl_fdset(fileno(stdin),&readfds);
  1916. #endif
  1917. openssl_fdset(s,&readfds);
  1918. /* Note: under VMS with SOCKETSHR the second parameter is
  1919. * currently of type (int *) whereas under other systems
  1920. * it is (void *) if you don't have a cast it will choke
  1921. * the compiler: if you do have a cast then you can either
  1922. * go for (int *) or (void *).
  1923. */
  1924. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1925. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1926. * on sockets. As a workaround we timeout the select every
  1927. * second and check for any keypress. In a proper Windows
  1928. * application we wouldn't do this because it is inefficient.
  1929. */
  1930. tv.tv_sec = 1;
  1931. tv.tv_usec = 0;
  1932. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1933. if((i < 0) || (!i && !_kbhit() ) )continue;
  1934. if(_kbhit())
  1935. read_from_terminal = 1;
  1936. #elif defined(OPENSSL_SYS_BEOS_R5)
  1937. /* Under BeOS-R5 the situation is similar to DOS */
  1938. tv.tv_sec = 1;
  1939. tv.tv_usec = 0;
  1940. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1941. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1942. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1943. continue;
  1944. if (read(fileno(stdin), buf, 0) >= 0)
  1945. read_from_terminal = 1;
  1946. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1947. #else
  1948. if ((SSL_version(con) == DTLS1_VERSION) &&
  1949. DTLSv1_get_timeout(con, &timeout))
  1950. timeoutp = &timeout;
  1951. else
  1952. timeoutp = NULL;
  1953. i=select(width,(void *)&readfds,NULL,NULL,timeoutp);
  1954. if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
  1955. {
  1956. BIO_printf(bio_err,"TIMEOUT occured\n");
  1957. }
  1958. if (i <= 0) continue;
  1959. if (FD_ISSET(fileno(stdin),&readfds))
  1960. read_from_terminal = 1;
  1961. #endif
  1962. if (FD_ISSET(s,&readfds))
  1963. read_from_sslcon = 1;
  1964. }
  1965. if (read_from_terminal)
  1966. {
  1967. if (s_crlf)
  1968. {
  1969. int j, lf_num;
  1970. i=raw_read_stdin(buf, bufsize/2);
  1971. lf_num = 0;
  1972. /* both loops are skipped when i <= 0 */
  1973. for (j = 0; j < i; j++)
  1974. if (buf[j] == '\n')
  1975. lf_num++;
  1976. for (j = i-1; j >= 0; j--)
  1977. {
  1978. buf[j+lf_num] = buf[j];
  1979. if (buf[j] == '\n')
  1980. {
  1981. lf_num--;
  1982. i++;
  1983. buf[j+lf_num] = '\r';
  1984. }
  1985. }
  1986. assert(lf_num == 0);
  1987. }
  1988. else
  1989. i=raw_read_stdin(buf,bufsize);
  1990. if (!s_quiet)
  1991. {
  1992. if ((i <= 0) || (buf[0] == 'Q'))
  1993. {
  1994. BIO_printf(bio_s_out,"DONE\n");
  1995. SHUTDOWN(s);
  1996. close_accept_socket();
  1997. ret= -11;
  1998. goto err;
  1999. }
  2000. if ((i <= 0) || (buf[0] == 'q'))
  2001. {
  2002. BIO_printf(bio_s_out,"DONE\n");
  2003. if (SSL_version(con) != DTLS1_VERSION)
  2004. SHUTDOWN(s);
  2005. /* close_accept_socket();
  2006. ret= -11;*/
  2007. goto err;
  2008. }
  2009. if ((buf[0] == 'r') &&
  2010. ((buf[1] == '\n') || (buf[1] == '\r')))
  2011. {
  2012. SSL_renegotiate(con);
  2013. i=SSL_do_handshake(con);
  2014. printf("SSL_do_handshake -> %d\n",i);
  2015. i=0; /*13; */
  2016. continue;
  2017. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  2018. }
  2019. if ((buf[0] == 'R') &&
  2020. ((buf[1] == '\n') || (buf[1] == '\r')))
  2021. {
  2022. SSL_set_verify(con,
  2023. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  2024. SSL_renegotiate(con);
  2025. i=SSL_do_handshake(con);
  2026. printf("SSL_do_handshake -> %d\n",i);
  2027. i=0; /* 13; */
  2028. continue;
  2029. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  2030. }
  2031. if (buf[0] == 'P')
  2032. {
  2033. static const char *str="Lets print some clear text\n";
  2034. BIO_write(SSL_get_wbio(con),str,strlen(str));
  2035. }
  2036. if (buf[0] == 'S')
  2037. {
  2038. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  2039. }
  2040. }
  2041. #ifdef CHARSET_EBCDIC
  2042. ebcdic2ascii(buf,buf,i);
  2043. #endif
  2044. l=k=0;
  2045. for (;;)
  2046. {
  2047. /* should do a select for the write */
  2048. #ifdef RENEG
  2049. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  2050. #endif
  2051. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  2052. switch (SSL_get_error(con,k))
  2053. {
  2054. case SSL_ERROR_NONE:
  2055. break;
  2056. case SSL_ERROR_WANT_WRITE:
  2057. case SSL_ERROR_WANT_READ:
  2058. case SSL_ERROR_WANT_X509_LOOKUP:
  2059. BIO_printf(bio_s_out,"Write BLOCK\n");
  2060. break;
  2061. case SSL_ERROR_SYSCALL:
  2062. case SSL_ERROR_SSL:
  2063. BIO_printf(bio_s_out,"ERROR\n");
  2064. ERR_print_errors(bio_err);
  2065. ret=1;
  2066. goto err;
  2067. /* break; */
  2068. case SSL_ERROR_ZERO_RETURN:
  2069. BIO_printf(bio_s_out,"DONE\n");
  2070. ret=1;
  2071. goto err;
  2072. }
  2073. l+=k;
  2074. i-=k;
  2075. if (i <= 0) break;
  2076. }
  2077. }
  2078. if (read_from_sslcon)
  2079. {
  2080. if (!SSL_is_init_finished(con))
  2081. {
  2082. i=init_ssl_connection(con);
  2083. if (i < 0)
  2084. {
  2085. ret=0;
  2086. goto err;
  2087. }
  2088. else if (i == 0)
  2089. {
  2090. ret=1;
  2091. goto err;
  2092. }
  2093. }
  2094. else
  2095. {
  2096. again:
  2097. i=SSL_read(con,(char *)buf,bufsize);
  2098. switch (SSL_get_error(con,i))
  2099. {
  2100. case SSL_ERROR_NONE:
  2101. #ifdef CHARSET_EBCDIC
  2102. ascii2ebcdic(buf,buf,i);
  2103. #endif
  2104. raw_write_stdout(buf,
  2105. (unsigned int)i);
  2106. if (SSL_pending(con)) goto again;
  2107. break;
  2108. case SSL_ERROR_WANT_WRITE:
  2109. case SSL_ERROR_WANT_READ:
  2110. case SSL_ERROR_WANT_X509_LOOKUP:
  2111. BIO_printf(bio_s_out,"Read BLOCK\n");
  2112. break;
  2113. case SSL_ERROR_SYSCALL:
  2114. case SSL_ERROR_SSL:
  2115. BIO_printf(bio_s_out,"ERROR\n");
  2116. ERR_print_errors(bio_err);
  2117. ret=1;
  2118. goto err;
  2119. case SSL_ERROR_ZERO_RETURN:
  2120. BIO_printf(bio_s_out,"DONE\n");
  2121. ret=1;
  2122. goto err;
  2123. }
  2124. }
  2125. }
  2126. }
  2127. err:
  2128. if (con != NULL)
  2129. {
  2130. BIO_printf(bio_s_out,"shutting down SSL\n");
  2131. #if 1
  2132. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2133. #else
  2134. SSL_shutdown(con);
  2135. #endif
  2136. SSL_free(con);
  2137. }
  2138. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  2139. if (buf != NULL)
  2140. {
  2141. OPENSSL_cleanse(buf,bufsize);
  2142. OPENSSL_free(buf);
  2143. }
  2144. if (ret >= 0)
  2145. BIO_printf(bio_s_out,"ACCEPT\n");
  2146. return(ret);
  2147. }
  2148. static void close_accept_socket(void)
  2149. {
  2150. BIO_printf(bio_err,"shutdown accept socket\n");
  2151. if (accept_socket >= 0)
  2152. {
  2153. SHUTDOWN2(accept_socket);
  2154. }
  2155. }
  2156. static int init_ssl_connection(SSL *con)
  2157. {
  2158. int i;
  2159. const char *str;
  2160. X509 *peer;
  2161. long verify_error;
  2162. MS_STATIC char buf[BUFSIZ];
  2163. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2164. const unsigned char *next_proto_neg;
  2165. unsigned next_proto_neg_len;
  2166. #endif
  2167. #ifndef OPENSSL_NO_KRB5
  2168. char *client_princ;
  2169. #endif
  2170. if ((i=SSL_accept(con)) <= 0)
  2171. {
  2172. if (BIO_sock_should_retry(i))
  2173. {
  2174. BIO_printf(bio_s_out,"DELAY\n");
  2175. return(1);
  2176. }
  2177. BIO_printf(bio_err,"ERROR\n");
  2178. verify_error=SSL_get_verify_result(con);
  2179. if (verify_error != X509_V_OK)
  2180. {
  2181. BIO_printf(bio_err,"verify error:%s\n",
  2182. X509_verify_cert_error_string(verify_error));
  2183. }
  2184. else
  2185. ERR_print_errors(bio_err);
  2186. return(0);
  2187. }
  2188. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  2189. peer=SSL_get_peer_certificate(con);
  2190. if (peer != NULL)
  2191. {
  2192. BIO_printf(bio_s_out,"Client certificate\n");
  2193. PEM_write_bio_X509(bio_s_out,peer);
  2194. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  2195. BIO_printf(bio_s_out,"subject=%s\n",buf);
  2196. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  2197. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  2198. X509_free(peer);
  2199. }
  2200. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  2201. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  2202. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2203. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  2204. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2205. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2206. if (next_proto_neg)
  2207. {
  2208. BIO_printf(bio_s_out,"NEXTPROTO is ");
  2209. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2210. BIO_printf(bio_s_out, "\n");
  2211. }
  2212. #endif
  2213. if (SSL_cache_hit(con)) BIO_printf(bio_s_out,"Reused session-id\n");
  2214. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  2215. TLS1_FLAGS_TLS_PADDING_BUG)
  2216. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  2217. #ifndef OPENSSL_NO_KRB5
  2218. client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con));
  2219. if (client_princ != NULL)
  2220. {
  2221. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  2222. client_princ);
  2223. }
  2224. #endif /* OPENSSL_NO_KRB5 */
  2225. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2226. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2227. return(1);
  2228. }
  2229. #ifndef OPENSSL_NO_DH
  2230. static DH *load_dh_param(const char *dhfile)
  2231. {
  2232. DH *ret=NULL;
  2233. BIO *bio;
  2234. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  2235. goto err;
  2236. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  2237. err:
  2238. if (bio != NULL) BIO_free(bio);
  2239. return(ret);
  2240. }
  2241. #endif
  2242. #if 0
  2243. static int load_CA(SSL_CTX *ctx, char *file)
  2244. {
  2245. FILE *in;
  2246. X509 *x=NULL;
  2247. if ((in=fopen(file,"r")) == NULL)
  2248. return(0);
  2249. for (;;)
  2250. {
  2251. if (PEM_read_X509(in,&x,NULL) == NULL)
  2252. break;
  2253. SSL_CTX_add_client_CA(ctx,x);
  2254. }
  2255. if (x != NULL) X509_free(x);
  2256. fclose(in);
  2257. return(1);
  2258. }
  2259. #endif
  2260. static int www_body(char *hostname, int s, unsigned char *context)
  2261. {
  2262. char *buf=NULL;
  2263. int ret=1;
  2264. int i,j,k,dot;
  2265. SSL *con;
  2266. const SSL_CIPHER *c;
  2267. BIO *io,*ssl_bio,*sbio;
  2268. #ifndef OPENSSL_NO_KRB5
  2269. KSSL_CTX *kctx;
  2270. #endif
  2271. buf=OPENSSL_malloc(bufsize);
  2272. if (buf == NULL) return(0);
  2273. io=BIO_new(BIO_f_buffer());
  2274. ssl_bio=BIO_new(BIO_f_ssl());
  2275. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  2276. #ifdef FIONBIO
  2277. if (s_nbio)
  2278. {
  2279. unsigned long sl=1;
  2280. if (!s_quiet)
  2281. BIO_printf(bio_err,"turning on non blocking io\n");
  2282. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  2283. ERR_print_errors(bio_err);
  2284. }
  2285. #endif
  2286. /* lets make the output buffer a reasonable size */
  2287. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  2288. if ((con=SSL_new(ctx)) == NULL) goto err;
  2289. #ifndef OPENSSL_NO_TLSEXT
  2290. if (s_tlsextdebug)
  2291. {
  2292. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2293. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2294. }
  2295. #endif
  2296. #ifndef OPENSSL_NO_KRB5
  2297. if ((kctx = kssl_ctx_new()) != NULL)
  2298. {
  2299. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2300. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2301. }
  2302. #endif /* OPENSSL_NO_KRB5 */
  2303. if(context) SSL_set_session_id_context(con, context,
  2304. strlen((char *)context));
  2305. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  2306. if (s_nbio_test)
  2307. {
  2308. BIO *test;
  2309. test=BIO_new(BIO_f_nbio_test());
  2310. sbio=BIO_push(test,sbio);
  2311. }
  2312. SSL_set_bio(con,sbio,sbio);
  2313. SSL_set_accept_state(con);
  2314. /* SSL_set_fd(con,s); */
  2315. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  2316. BIO_push(io,ssl_bio);
  2317. #ifdef CHARSET_EBCDIC
  2318. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  2319. #endif
  2320. if (s_debug)
  2321. {
  2322. SSL_set_debug(con, 1);
  2323. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  2324. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  2325. }
  2326. if (s_msg)
  2327. {
  2328. SSL_set_msg_callback(con, msg_cb);
  2329. SSL_set_msg_callback_arg(con, bio_s_out);
  2330. }
  2331. for (;;)
  2332. {
  2333. if (hack)
  2334. {
  2335. i=SSL_accept(con);
  2336. switch (SSL_get_error(con,i))
  2337. {
  2338. case SSL_ERROR_NONE:
  2339. break;
  2340. case SSL_ERROR_WANT_WRITE:
  2341. case SSL_ERROR_WANT_READ:
  2342. case SSL_ERROR_WANT_X509_LOOKUP:
  2343. continue;
  2344. case SSL_ERROR_SYSCALL:
  2345. case SSL_ERROR_SSL:
  2346. case SSL_ERROR_ZERO_RETURN:
  2347. ret=1;
  2348. goto err;
  2349. /* break; */
  2350. }
  2351. SSL_renegotiate(con);
  2352. SSL_write(con,NULL,0);
  2353. }
  2354. i=BIO_gets(io,buf,bufsize-1);
  2355. if (i < 0) /* error */
  2356. {
  2357. if (!BIO_should_retry(io))
  2358. {
  2359. if (!s_quiet)
  2360. ERR_print_errors(bio_err);
  2361. goto err;
  2362. }
  2363. else
  2364. {
  2365. BIO_printf(bio_s_out,"read R BLOCK\n");
  2366. #if defined(OPENSSL_SYS_NETWARE)
  2367. delay(1000);
  2368. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2369. sleep(1);
  2370. #endif
  2371. continue;
  2372. }
  2373. }
  2374. else if (i == 0) /* end of input */
  2375. {
  2376. ret=1;
  2377. goto end;
  2378. }
  2379. /* else we have data */
  2380. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  2381. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  2382. {
  2383. char *p;
  2384. X509 *peer;
  2385. STACK_OF(SSL_CIPHER) *sk;
  2386. static const char *space=" ";
  2387. if (www == 1 && strncmp("GET /reneg", buf, 10) == 0)
  2388. {
  2389. if (strncmp("GET /renegcert", buf, 14) == 0)
  2390. SSL_set_verify(con,
  2391. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  2392. i=SSL_renegotiate(con);
  2393. BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n",i);
  2394. i=SSL_do_handshake(con);
  2395. if (i <= 0)
  2396. {
  2397. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", SSL_get_error(con, i));
  2398. ERR_print_errors(bio_err);
  2399. goto err;
  2400. }
  2401. /* EVIL HACK! */
  2402. SSL_set_state(con, SSL_ST_ACCEPT);
  2403. i=SSL_do_handshake(con);
  2404. BIO_printf(bio_s_out, "SSL_do_handshake -> %d\n",i);
  2405. if (i <= 0)
  2406. {
  2407. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", SSL_get_error(con, i));
  2408. ERR_print_errors(bio_err);
  2409. goto err;
  2410. }
  2411. }
  2412. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2413. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2414. BIO_puts(io,"<pre>\n");
  2415. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2416. BIO_puts(io,"\n");
  2417. for (i=0; i<local_argc; i++)
  2418. {
  2419. BIO_puts(io,local_argv[i]);
  2420. BIO_write(io," ",1);
  2421. }
  2422. BIO_puts(io,"\n");
  2423. BIO_printf(io,
  2424. "Secure Renegotiation IS%s supported\n",
  2425. SSL_get_secure_renegotiation_support(con) ?
  2426. "" : " NOT");
  2427. /* The following is evil and should not really
  2428. * be done */
  2429. BIO_printf(io,"Ciphers supported in s_server binary\n");
  2430. sk=SSL_get_ciphers(con);
  2431. j=sk_SSL_CIPHER_num(sk);
  2432. for (i=0; i<j; i++)
  2433. {
  2434. c=sk_SSL_CIPHER_value(sk,i);
  2435. BIO_printf(io,"%-11s:%-25s",
  2436. SSL_CIPHER_get_version(c),
  2437. SSL_CIPHER_get_name(c));
  2438. if ((((i+1)%2) == 0) && (i+1 != j))
  2439. BIO_puts(io,"\n");
  2440. }
  2441. BIO_puts(io,"\n");
  2442. p=SSL_get_shared_ciphers(con,buf,bufsize);
  2443. if (p != NULL)
  2444. {
  2445. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  2446. j=i=0;
  2447. while (*p)
  2448. {
  2449. if (*p == ':')
  2450. {
  2451. BIO_write(io,space,26-j);
  2452. i++;
  2453. j=0;
  2454. BIO_write(io,((i%3)?" ":"\n"),1);
  2455. }
  2456. else
  2457. {
  2458. BIO_write(io,p,1);
  2459. j++;
  2460. }
  2461. p++;
  2462. }
  2463. BIO_puts(io,"\n");
  2464. }
  2465. BIO_printf(io,(SSL_cache_hit(con)
  2466. ?"---\nReused, "
  2467. :"---\nNew, "));
  2468. c=SSL_get_current_cipher(con);
  2469. BIO_printf(io,"%s, Cipher is %s\n",
  2470. SSL_CIPHER_get_version(c),
  2471. SSL_CIPHER_get_name(c));
  2472. SSL_SESSION_print(io,SSL_get_session(con));
  2473. BIO_printf(io,"---\n");
  2474. print_stats(io,SSL_get_SSL_CTX(con));
  2475. BIO_printf(io,"---\n");
  2476. peer=SSL_get_peer_certificate(con);
  2477. if (peer != NULL)
  2478. {
  2479. BIO_printf(io,"Client certificate\n");
  2480. X509_print(io,peer);
  2481. PEM_write_bio_X509(io,peer);
  2482. }
  2483. else
  2484. BIO_puts(io,"no client certificate available\n");
  2485. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  2486. break;
  2487. }
  2488. else if ((www == 2 || www == 3)
  2489. && (strncmp("GET /",buf,5) == 0))
  2490. {
  2491. BIO *file;
  2492. char *p,*e;
  2493. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2494. /* skip the '/' */
  2495. p= &(buf[5]);
  2496. dot = 1;
  2497. for (e=p; *e != '\0'; e++)
  2498. {
  2499. if (e[0] == ' ')
  2500. break;
  2501. switch (dot)
  2502. {
  2503. case 1:
  2504. dot = (e[0] == '.') ? 2 : 0;
  2505. break;
  2506. case 2:
  2507. dot = (e[0] == '.') ? 3 : 0;
  2508. break;
  2509. case 3:
  2510. dot = (e[0] == '/') ? -1 : 0;
  2511. break;
  2512. }
  2513. if (dot == 0)
  2514. dot = (e[0] == '/') ? 1 : 0;
  2515. }
  2516. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2517. if (*e == '\0')
  2518. {
  2519. BIO_puts(io,text);
  2520. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2521. break;
  2522. }
  2523. *e='\0';
  2524. if (dot)
  2525. {
  2526. BIO_puts(io,text);
  2527. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2528. break;
  2529. }
  2530. if (*p == '/')
  2531. {
  2532. BIO_puts(io,text);
  2533. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2534. break;
  2535. }
  2536. #if 0
  2537. /* append if a directory lookup */
  2538. if (e[-1] == '/')
  2539. strcat(p,"index.html");
  2540. #endif
  2541. /* if a directory, do the index thang */
  2542. if (app_isdir(p)>0)
  2543. {
  2544. #if 0 /* must check buffer size */
  2545. strcat(p,"/index.html");
  2546. #else
  2547. BIO_puts(io,text);
  2548. BIO_printf(io,"'%s' is a directory\r\n",p);
  2549. break;
  2550. #endif
  2551. }
  2552. if ((file=BIO_new_file(p,"r")) == NULL)
  2553. {
  2554. BIO_puts(io,text);
  2555. BIO_printf(io,"Error opening '%s'\r\n",p);
  2556. ERR_print_errors(io);
  2557. break;
  2558. }
  2559. if (!s_quiet)
  2560. BIO_printf(bio_err,"FILE:%s\n",p);
  2561. if (www == 2)
  2562. {
  2563. i=strlen(p);
  2564. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2565. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2566. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2567. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2568. else
  2569. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2570. }
  2571. /* send the file */
  2572. for (;;)
  2573. {
  2574. i=BIO_read(file,buf,bufsize);
  2575. if (i <= 0) break;
  2576. #ifdef RENEG
  2577. total_bytes+=i;
  2578. fprintf(stderr,"%d\n",i);
  2579. if (total_bytes > 3*1024)
  2580. {
  2581. total_bytes=0;
  2582. fprintf(stderr,"RENEGOTIATE\n");
  2583. SSL_renegotiate(con);
  2584. }
  2585. #endif
  2586. for (j=0; j<i; )
  2587. {
  2588. #ifdef RENEG
  2589. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2590. #endif
  2591. k=BIO_write(io,&(buf[j]),i-j);
  2592. if (k <= 0)
  2593. {
  2594. if (!BIO_should_retry(io))
  2595. goto write_error;
  2596. else
  2597. {
  2598. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2599. }
  2600. }
  2601. else
  2602. {
  2603. j+=k;
  2604. }
  2605. }
  2606. }
  2607. write_error:
  2608. BIO_free(file);
  2609. break;
  2610. }
  2611. }
  2612. for (;;)
  2613. {
  2614. i=(int)BIO_flush(io);
  2615. if (i <= 0)
  2616. {
  2617. if (!BIO_should_retry(io))
  2618. break;
  2619. }
  2620. else
  2621. break;
  2622. }
  2623. end:
  2624. #if 1
  2625. /* make sure we re-use sessions */
  2626. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2627. #else
  2628. /* This kills performance */
  2629. /* SSL_shutdown(con); A shutdown gets sent in the
  2630. * BIO_free_all(io) procession */
  2631. #endif
  2632. err:
  2633. if (ret >= 0)
  2634. BIO_printf(bio_s_out,"ACCEPT\n");
  2635. if (buf != NULL) OPENSSL_free(buf);
  2636. if (io != NULL) BIO_free_all(io);
  2637. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2638. return(ret);
  2639. }
  2640. #ifndef OPENSSL_NO_RSA
  2641. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2642. {
  2643. BIGNUM *bn = NULL;
  2644. static RSA *rsa_tmp=NULL;
  2645. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2646. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2647. if (!rsa_tmp && bn)
  2648. {
  2649. if (!s_quiet)
  2650. {
  2651. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2652. (void)BIO_flush(bio_err);
  2653. }
  2654. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2655. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2656. {
  2657. if(rsa_tmp) RSA_free(rsa_tmp);
  2658. rsa_tmp = NULL;
  2659. }
  2660. if (!s_quiet)
  2661. {
  2662. BIO_printf(bio_err,"\n");
  2663. (void)BIO_flush(bio_err);
  2664. }
  2665. BN_free(bn);
  2666. }
  2667. return(rsa_tmp);
  2668. }
  2669. #endif
  2670. #define MAX_SESSION_ID_ATTEMPTS 10
  2671. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2672. unsigned int *id_len)
  2673. {
  2674. unsigned int count = 0;
  2675. do {
  2676. RAND_pseudo_bytes(id, *id_len);
  2677. /* Prefix the session_id with the required prefix. NB: If our
  2678. * prefix is too long, clip it - but there will be worse effects
  2679. * anyway, eg. the server could only possibly create 1 session
  2680. * ID (ie. the prefix!) so all future session negotiations will
  2681. * fail due to conflicts. */
  2682. memcpy(id, session_id_prefix,
  2683. (strlen(session_id_prefix) < *id_len) ?
  2684. strlen(session_id_prefix) : *id_len);
  2685. }
  2686. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2687. (++count < MAX_SESSION_ID_ATTEMPTS));
  2688. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2689. return 0;
  2690. return 1;
  2691. }
  2692. /* By default s_server uses an in-memory cache which caches SSL_SESSION
  2693. * structures without any serialisation. This hides some bugs which only
  2694. * become apparent in deployed servers. By implementing a basic external
  2695. * session cache some issues can be debugged using s_server.
  2696. */
  2697. typedef struct simple_ssl_session_st
  2698. {
  2699. unsigned char *id;
  2700. unsigned int idlen;
  2701. unsigned char *der;
  2702. int derlen;
  2703. struct simple_ssl_session_st *next;
  2704. } simple_ssl_session;
  2705. static simple_ssl_session *first = NULL;
  2706. static int add_session(SSL *ssl, SSL_SESSION *session)
  2707. {
  2708. simple_ssl_session *sess;
  2709. unsigned char *p;
  2710. sess = OPENSSL_malloc(sizeof(simple_ssl_session));
  2711. sess->idlen = SSL_SESSION_get_id_len(session);
  2712. sess->derlen = i2d_SSL_SESSION(session, NULL);
  2713. sess->id = BUF_memdup(SSL_SESSION_get0_id(session), sess->idlen);
  2714. sess->der = OPENSSL_malloc(sess->derlen);
  2715. p = sess->der;
  2716. i2d_SSL_SESSION(session, &p);
  2717. sess->next = first;
  2718. first = sess;
  2719. BIO_printf(bio_err, "New session added to external cache\n");
  2720. return 0;
  2721. }
  2722. static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
  2723. int *do_copy)
  2724. {
  2725. simple_ssl_session *sess;
  2726. *do_copy = 0;
  2727. for (sess = first; sess; sess = sess->next)
  2728. {
  2729. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen))
  2730. {
  2731. const unsigned char *p = sess->der;
  2732. BIO_printf(bio_err, "Lookup session: cache hit\n");
  2733. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  2734. }
  2735. }
  2736. BIO_printf(bio_err, "Lookup session: cache miss\n");
  2737. return NULL;
  2738. }
  2739. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  2740. {
  2741. simple_ssl_session *sess, *prev = NULL;
  2742. const unsigned char *id = SSL_SESSION_get0_id(session);
  2743. unsigned int idlen = SSL_SESSION_get_id_len(session);
  2744. for (sess = first; sess; sess = sess->next)
  2745. {
  2746. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen))
  2747. {
  2748. if(prev)
  2749. prev->next = sess->next;
  2750. else
  2751. first = sess->next;
  2752. OPENSSL_free(sess->id);
  2753. OPENSSL_free(sess->der);
  2754. OPENSSL_free(sess);
  2755. return;
  2756. }
  2757. prev = sess;
  2758. }
  2759. }
  2760. static void init_session_cache_ctx(SSL_CTX *sctx)
  2761. {
  2762. SSL_CTX_set_session_cache_mode(sctx,
  2763. SSL_SESS_CACHE_NO_INTERNAL|SSL_SESS_CACHE_SERVER);
  2764. SSL_CTX_sess_set_new_cb(sctx, add_session);
  2765. SSL_CTX_sess_set_get_cb(sctx, get_session);
  2766. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  2767. }
  2768. static void free_sessions(void)
  2769. {
  2770. simple_ssl_session *sess, *tsess;
  2771. for (sess = first; sess;)
  2772. {
  2773. OPENSSL_free(sess->id);
  2774. OPENSSL_free(sess->der);
  2775. tsess = sess;
  2776. sess = sess->next;
  2777. OPENSSL_free(tsess);
  2778. }
  2779. first = NULL;
  2780. }