s_server.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  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. struct timeval timeout;
  1792. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1793. struct timeval tv;
  1794. #else
  1795. struct timeval *timeoutp;
  1796. #endif
  1797. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1798. {
  1799. BIO_printf(bio_err,"out of memory\n");
  1800. goto err;
  1801. }
  1802. #ifdef FIONBIO
  1803. if (s_nbio)
  1804. {
  1805. unsigned long sl=1;
  1806. if (!s_quiet)
  1807. BIO_printf(bio_err,"turning on non blocking io\n");
  1808. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1809. ERR_print_errors(bio_err);
  1810. }
  1811. #endif
  1812. if (con == NULL) {
  1813. con=SSL_new(ctx);
  1814. #ifndef OPENSSL_NO_TLSEXT
  1815. if (s_tlsextdebug)
  1816. {
  1817. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1818. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1819. }
  1820. if (s_tlsextstatus)
  1821. {
  1822. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1823. tlscstatp.err = bio_err;
  1824. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1825. }
  1826. #endif
  1827. #ifndef OPENSSL_NO_KRB5
  1828. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1829. {
  1830. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
  1831. KRB5SVC);
  1832. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
  1833. KRB5KEYTAB);
  1834. }
  1835. #endif /* OPENSSL_NO_KRB5 */
  1836. if(context)
  1837. SSL_set_session_id_context(con, context,
  1838. strlen((char *)context));
  1839. }
  1840. SSL_clear(con);
  1841. #if 0
  1842. #ifdef TLSEXT_TYPE_opaque_prf_input
  1843. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  1844. #endif
  1845. #endif
  1846. if (SSL_version(con) == DTLS1_VERSION)
  1847. {
  1848. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1849. if (enable_timeouts)
  1850. {
  1851. timeout.tv_sec = 0;
  1852. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1853. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1854. timeout.tv_sec = 0;
  1855. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1856. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1857. }
  1858. if (socket_mtu > 28)
  1859. {
  1860. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1861. SSL_set_mtu(con, socket_mtu - 28);
  1862. }
  1863. else
  1864. /* want to do MTU discovery */
  1865. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1866. /* turn on cookie exchange */
  1867. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1868. }
  1869. else
  1870. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1871. if (s_nbio_test)
  1872. {
  1873. BIO *test;
  1874. test=BIO_new(BIO_f_nbio_test());
  1875. sbio=BIO_push(test,sbio);
  1876. }
  1877. #ifndef OPENSSL_NO_JPAKE
  1878. if(jpake_secret)
  1879. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  1880. #endif
  1881. SSL_set_bio(con,sbio,sbio);
  1882. SSL_set_accept_state(con);
  1883. /* SSL_set_fd(con,s); */
  1884. if (s_debug)
  1885. {
  1886. SSL_set_debug(con, 1);
  1887. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1888. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  1889. }
  1890. if (s_msg)
  1891. {
  1892. SSL_set_msg_callback(con, msg_cb);
  1893. SSL_set_msg_callback_arg(con, bio_s_out);
  1894. }
  1895. #ifndef OPENSSL_NO_TLSEXT
  1896. if (s_tlsextdebug)
  1897. {
  1898. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1899. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1900. }
  1901. #endif
  1902. width=s+1;
  1903. for (;;)
  1904. {
  1905. int read_from_terminal;
  1906. int read_from_sslcon;
  1907. read_from_terminal = 0;
  1908. read_from_sslcon = SSL_pending(con);
  1909. if (!read_from_sslcon)
  1910. {
  1911. FD_ZERO(&readfds);
  1912. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1913. openssl_fdset(fileno(stdin),&readfds);
  1914. #endif
  1915. openssl_fdset(s,&readfds);
  1916. /* Note: under VMS with SOCKETSHR the second parameter is
  1917. * currently of type (int *) whereas under other systems
  1918. * it is (void *) if you don't have a cast it will choke
  1919. * the compiler: if you do have a cast then you can either
  1920. * go for (int *) or (void *).
  1921. */
  1922. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1923. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1924. * on sockets. As a workaround we timeout the select every
  1925. * second and check for any keypress. In a proper Windows
  1926. * application we wouldn't do this because it is inefficient.
  1927. */
  1928. tv.tv_sec = 1;
  1929. tv.tv_usec = 0;
  1930. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1931. if((i < 0) || (!i && !_kbhit() ) )continue;
  1932. if(_kbhit())
  1933. read_from_terminal = 1;
  1934. #elif defined(OPENSSL_SYS_BEOS_R5)
  1935. /* Under BeOS-R5 the situation is similar to DOS */
  1936. tv.tv_sec = 1;
  1937. tv.tv_usec = 0;
  1938. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1939. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1940. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1941. continue;
  1942. if (read(fileno(stdin), buf, 0) >= 0)
  1943. read_from_terminal = 1;
  1944. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1945. #else
  1946. if ((SSL_version(con) == DTLS1_VERSION) &&
  1947. DTLSv1_get_timeout(con, &timeout))
  1948. timeoutp = &timeout;
  1949. else
  1950. timeoutp = NULL;
  1951. i=select(width,(void *)&readfds,NULL,NULL,timeoutp);
  1952. if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
  1953. {
  1954. BIO_printf(bio_err,"TIMEOUT occured\n");
  1955. }
  1956. if (i <= 0) continue;
  1957. if (FD_ISSET(fileno(stdin),&readfds))
  1958. read_from_terminal = 1;
  1959. #endif
  1960. if (FD_ISSET(s,&readfds))
  1961. read_from_sslcon = 1;
  1962. }
  1963. if (read_from_terminal)
  1964. {
  1965. if (s_crlf)
  1966. {
  1967. int j, lf_num;
  1968. i=raw_read_stdin(buf, bufsize/2);
  1969. lf_num = 0;
  1970. /* both loops are skipped when i <= 0 */
  1971. for (j = 0; j < i; j++)
  1972. if (buf[j] == '\n')
  1973. lf_num++;
  1974. for (j = i-1; j >= 0; j--)
  1975. {
  1976. buf[j+lf_num] = buf[j];
  1977. if (buf[j] == '\n')
  1978. {
  1979. lf_num--;
  1980. i++;
  1981. buf[j+lf_num] = '\r';
  1982. }
  1983. }
  1984. assert(lf_num == 0);
  1985. }
  1986. else
  1987. i=raw_read_stdin(buf,bufsize);
  1988. if (!s_quiet)
  1989. {
  1990. if ((i <= 0) || (buf[0] == 'Q'))
  1991. {
  1992. BIO_printf(bio_s_out,"DONE\n");
  1993. SHUTDOWN(s);
  1994. close_accept_socket();
  1995. ret= -11;
  1996. goto err;
  1997. }
  1998. if ((i <= 0) || (buf[0] == 'q'))
  1999. {
  2000. BIO_printf(bio_s_out,"DONE\n");
  2001. if (SSL_version(con) != DTLS1_VERSION)
  2002. SHUTDOWN(s);
  2003. /* close_accept_socket();
  2004. ret= -11;*/
  2005. goto err;
  2006. }
  2007. if ((buf[0] == 'r') &&
  2008. ((buf[1] == '\n') || (buf[1] == '\r')))
  2009. {
  2010. SSL_renegotiate(con);
  2011. i=SSL_do_handshake(con);
  2012. printf("SSL_do_handshake -> %d\n",i);
  2013. i=0; /*13; */
  2014. continue;
  2015. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  2016. }
  2017. if ((buf[0] == 'R') &&
  2018. ((buf[1] == '\n') || (buf[1] == '\r')))
  2019. {
  2020. SSL_set_verify(con,
  2021. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  2022. SSL_renegotiate(con);
  2023. i=SSL_do_handshake(con);
  2024. printf("SSL_do_handshake -> %d\n",i);
  2025. i=0; /* 13; */
  2026. continue;
  2027. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  2028. }
  2029. if (buf[0] == 'P')
  2030. {
  2031. static const char *str="Lets print some clear text\n";
  2032. BIO_write(SSL_get_wbio(con),str,strlen(str));
  2033. }
  2034. if (buf[0] == 'S')
  2035. {
  2036. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  2037. }
  2038. }
  2039. #ifdef CHARSET_EBCDIC
  2040. ebcdic2ascii(buf,buf,i);
  2041. #endif
  2042. l=k=0;
  2043. for (;;)
  2044. {
  2045. /* should do a select for the write */
  2046. #ifdef RENEG
  2047. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  2048. #endif
  2049. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  2050. switch (SSL_get_error(con,k))
  2051. {
  2052. case SSL_ERROR_NONE:
  2053. break;
  2054. case SSL_ERROR_WANT_WRITE:
  2055. case SSL_ERROR_WANT_READ:
  2056. case SSL_ERROR_WANT_X509_LOOKUP:
  2057. BIO_printf(bio_s_out,"Write BLOCK\n");
  2058. break;
  2059. case SSL_ERROR_SYSCALL:
  2060. case SSL_ERROR_SSL:
  2061. BIO_printf(bio_s_out,"ERROR\n");
  2062. ERR_print_errors(bio_err);
  2063. ret=1;
  2064. goto err;
  2065. /* break; */
  2066. case SSL_ERROR_ZERO_RETURN:
  2067. BIO_printf(bio_s_out,"DONE\n");
  2068. ret=1;
  2069. goto err;
  2070. }
  2071. l+=k;
  2072. i-=k;
  2073. if (i <= 0) break;
  2074. }
  2075. }
  2076. if (read_from_sslcon)
  2077. {
  2078. if (!SSL_is_init_finished(con))
  2079. {
  2080. i=init_ssl_connection(con);
  2081. if (i < 0)
  2082. {
  2083. ret=0;
  2084. goto err;
  2085. }
  2086. else if (i == 0)
  2087. {
  2088. ret=1;
  2089. goto err;
  2090. }
  2091. }
  2092. else
  2093. {
  2094. again:
  2095. i=SSL_read(con,(char *)buf,bufsize);
  2096. switch (SSL_get_error(con,i))
  2097. {
  2098. case SSL_ERROR_NONE:
  2099. #ifdef CHARSET_EBCDIC
  2100. ascii2ebcdic(buf,buf,i);
  2101. #endif
  2102. raw_write_stdout(buf,
  2103. (unsigned int)i);
  2104. if (SSL_pending(con)) goto again;
  2105. break;
  2106. case SSL_ERROR_WANT_WRITE:
  2107. case SSL_ERROR_WANT_READ:
  2108. case SSL_ERROR_WANT_X509_LOOKUP:
  2109. BIO_printf(bio_s_out,"Read BLOCK\n");
  2110. break;
  2111. case SSL_ERROR_SYSCALL:
  2112. case SSL_ERROR_SSL:
  2113. BIO_printf(bio_s_out,"ERROR\n");
  2114. ERR_print_errors(bio_err);
  2115. ret=1;
  2116. goto err;
  2117. case SSL_ERROR_ZERO_RETURN:
  2118. BIO_printf(bio_s_out,"DONE\n");
  2119. ret=1;
  2120. goto err;
  2121. }
  2122. }
  2123. }
  2124. }
  2125. err:
  2126. if (con != NULL)
  2127. {
  2128. BIO_printf(bio_s_out,"shutting down SSL\n");
  2129. #if 1
  2130. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2131. #else
  2132. SSL_shutdown(con);
  2133. #endif
  2134. SSL_free(con);
  2135. }
  2136. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  2137. if (buf != NULL)
  2138. {
  2139. OPENSSL_cleanse(buf,bufsize);
  2140. OPENSSL_free(buf);
  2141. }
  2142. if (ret >= 0)
  2143. BIO_printf(bio_s_out,"ACCEPT\n");
  2144. return(ret);
  2145. }
  2146. static void close_accept_socket(void)
  2147. {
  2148. BIO_printf(bio_err,"shutdown accept socket\n");
  2149. if (accept_socket >= 0)
  2150. {
  2151. SHUTDOWN2(accept_socket);
  2152. }
  2153. }
  2154. static int init_ssl_connection(SSL *con)
  2155. {
  2156. int i;
  2157. const char *str;
  2158. X509 *peer;
  2159. long verify_error;
  2160. MS_STATIC char buf[BUFSIZ];
  2161. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2162. const unsigned char *next_proto_neg;
  2163. unsigned next_proto_neg_len;
  2164. #endif
  2165. if ((i=SSL_accept(con)) <= 0)
  2166. {
  2167. if (BIO_sock_should_retry(i))
  2168. {
  2169. BIO_printf(bio_s_out,"DELAY\n");
  2170. return(1);
  2171. }
  2172. BIO_printf(bio_err,"ERROR\n");
  2173. verify_error=SSL_get_verify_result(con);
  2174. if (verify_error != X509_V_OK)
  2175. {
  2176. BIO_printf(bio_err,"verify error:%s\n",
  2177. X509_verify_cert_error_string(verify_error));
  2178. }
  2179. else
  2180. ERR_print_errors(bio_err);
  2181. return(0);
  2182. }
  2183. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  2184. peer=SSL_get_peer_certificate(con);
  2185. if (peer != NULL)
  2186. {
  2187. BIO_printf(bio_s_out,"Client certificate\n");
  2188. PEM_write_bio_X509(bio_s_out,peer);
  2189. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  2190. BIO_printf(bio_s_out,"subject=%s\n",buf);
  2191. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  2192. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  2193. X509_free(peer);
  2194. }
  2195. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  2196. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  2197. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2198. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  2199. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2200. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2201. if (next_proto_neg)
  2202. {
  2203. BIO_printf(bio_s_out,"NEXTPROTO is ");
  2204. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2205. BIO_printf(bio_s_out, "\n");
  2206. }
  2207. #endif
  2208. if (SSL_cache_hit(con)) BIO_printf(bio_s_out,"Reused session-id\n");
  2209. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  2210. TLS1_FLAGS_TLS_PADDING_BUG)
  2211. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  2212. #ifndef OPENSSL_NO_KRB5
  2213. if (con->kssl_ctx->client_princ != NULL)
  2214. {
  2215. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  2216. con->kssl_ctx->client_princ);
  2217. }
  2218. #endif /* OPENSSL_NO_KRB5 */
  2219. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2220. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2221. return(1);
  2222. }
  2223. #ifndef OPENSSL_NO_DH
  2224. static DH *load_dh_param(const char *dhfile)
  2225. {
  2226. DH *ret=NULL;
  2227. BIO *bio;
  2228. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  2229. goto err;
  2230. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  2231. err:
  2232. if (bio != NULL) BIO_free(bio);
  2233. return(ret);
  2234. }
  2235. #endif
  2236. #if 0
  2237. static int load_CA(SSL_CTX *ctx, char *file)
  2238. {
  2239. FILE *in;
  2240. X509 *x=NULL;
  2241. if ((in=fopen(file,"r")) == NULL)
  2242. return(0);
  2243. for (;;)
  2244. {
  2245. if (PEM_read_X509(in,&x,NULL) == NULL)
  2246. break;
  2247. SSL_CTX_add_client_CA(ctx,x);
  2248. }
  2249. if (x != NULL) X509_free(x);
  2250. fclose(in);
  2251. return(1);
  2252. }
  2253. #endif
  2254. static int www_body(char *hostname, int s, unsigned char *context)
  2255. {
  2256. char *buf=NULL;
  2257. int ret=1;
  2258. int i,j,k,dot;
  2259. SSL *con;
  2260. const SSL_CIPHER *c;
  2261. BIO *io,*ssl_bio,*sbio;
  2262. buf=OPENSSL_malloc(bufsize);
  2263. if (buf == NULL) return(0);
  2264. io=BIO_new(BIO_f_buffer());
  2265. ssl_bio=BIO_new(BIO_f_ssl());
  2266. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  2267. #ifdef FIONBIO
  2268. if (s_nbio)
  2269. {
  2270. unsigned long sl=1;
  2271. if (!s_quiet)
  2272. BIO_printf(bio_err,"turning on non blocking io\n");
  2273. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  2274. ERR_print_errors(bio_err);
  2275. }
  2276. #endif
  2277. /* lets make the output buffer a reasonable size */
  2278. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  2279. if ((con=SSL_new(ctx)) == NULL) goto err;
  2280. #ifndef OPENSSL_NO_TLSEXT
  2281. if (s_tlsextdebug)
  2282. {
  2283. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2284. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2285. }
  2286. #endif
  2287. #ifndef OPENSSL_NO_KRB5
  2288. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  2289. {
  2290. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
  2291. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
  2292. }
  2293. #endif /* OPENSSL_NO_KRB5 */
  2294. if(context) SSL_set_session_id_context(con, context,
  2295. strlen((char *)context));
  2296. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  2297. if (s_nbio_test)
  2298. {
  2299. BIO *test;
  2300. test=BIO_new(BIO_f_nbio_test());
  2301. sbio=BIO_push(test,sbio);
  2302. }
  2303. SSL_set_bio(con,sbio,sbio);
  2304. SSL_set_accept_state(con);
  2305. /* SSL_set_fd(con,s); */
  2306. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  2307. BIO_push(io,ssl_bio);
  2308. #ifdef CHARSET_EBCDIC
  2309. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  2310. #endif
  2311. if (s_debug)
  2312. {
  2313. SSL_set_debug(con, 1);
  2314. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  2315. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  2316. }
  2317. if (s_msg)
  2318. {
  2319. SSL_set_msg_callback(con, msg_cb);
  2320. SSL_set_msg_callback_arg(con, bio_s_out);
  2321. }
  2322. for (;;)
  2323. {
  2324. if (hack)
  2325. {
  2326. i=SSL_accept(con);
  2327. switch (SSL_get_error(con,i))
  2328. {
  2329. case SSL_ERROR_NONE:
  2330. break;
  2331. case SSL_ERROR_WANT_WRITE:
  2332. case SSL_ERROR_WANT_READ:
  2333. case SSL_ERROR_WANT_X509_LOOKUP:
  2334. continue;
  2335. case SSL_ERROR_SYSCALL:
  2336. case SSL_ERROR_SSL:
  2337. case SSL_ERROR_ZERO_RETURN:
  2338. ret=1;
  2339. goto err;
  2340. /* break; */
  2341. }
  2342. SSL_renegotiate(con);
  2343. SSL_write(con,NULL,0);
  2344. }
  2345. i=BIO_gets(io,buf,bufsize-1);
  2346. if (i < 0) /* error */
  2347. {
  2348. if (!BIO_should_retry(io))
  2349. {
  2350. if (!s_quiet)
  2351. ERR_print_errors(bio_err);
  2352. goto err;
  2353. }
  2354. else
  2355. {
  2356. BIO_printf(bio_s_out,"read R BLOCK\n");
  2357. #if defined(OPENSSL_SYS_NETWARE)
  2358. delay(1000);
  2359. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2360. sleep(1);
  2361. #endif
  2362. continue;
  2363. }
  2364. }
  2365. else if (i == 0) /* end of input */
  2366. {
  2367. ret=1;
  2368. goto end;
  2369. }
  2370. /* else we have data */
  2371. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  2372. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  2373. {
  2374. char *p;
  2375. X509 *peer;
  2376. STACK_OF(SSL_CIPHER) *sk;
  2377. static const char *space=" ";
  2378. if (www == 1 && strncmp("GET /reneg", buf, 10) == 0)
  2379. {
  2380. if (strncmp("GET /renegcert", buf, 14) == 0)
  2381. SSL_set_verify(con,
  2382. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  2383. i=SSL_renegotiate(con);
  2384. BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n",i);
  2385. i=SSL_do_handshake(con);
  2386. if (i <= 0)
  2387. {
  2388. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", SSL_get_error(con, i));
  2389. ERR_print_errors(bio_err);
  2390. goto err;
  2391. }
  2392. /* EVIL HACK! */
  2393. SSL_set_state(con, SSL_ST_ACCEPT);
  2394. i=SSL_do_handshake(con);
  2395. BIO_printf(bio_s_out, "SSL_do_handshake -> %d\n",i);
  2396. if (i <= 0)
  2397. {
  2398. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", SSL_get_error(con, i));
  2399. ERR_print_errors(bio_err);
  2400. goto err;
  2401. }
  2402. }
  2403. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2404. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2405. BIO_puts(io,"<pre>\n");
  2406. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2407. BIO_puts(io,"\n");
  2408. for (i=0; i<local_argc; i++)
  2409. {
  2410. BIO_puts(io,local_argv[i]);
  2411. BIO_write(io," ",1);
  2412. }
  2413. BIO_puts(io,"\n");
  2414. BIO_printf(io,
  2415. "Secure Renegotiation IS%s supported\n",
  2416. SSL_get_secure_renegotiation_support(con) ?
  2417. "" : " NOT");
  2418. /* The following is evil and should not really
  2419. * be done */
  2420. BIO_printf(io,"Ciphers supported in s_server binary\n");
  2421. sk=SSL_get_ciphers(con);
  2422. j=sk_SSL_CIPHER_num(sk);
  2423. for (i=0; i<j; i++)
  2424. {
  2425. c=sk_SSL_CIPHER_value(sk,i);
  2426. BIO_printf(io,"%-11s:%-25s",
  2427. SSL_CIPHER_get_version(c),
  2428. SSL_CIPHER_get_name(c));
  2429. if ((((i+1)%2) == 0) && (i+1 != j))
  2430. BIO_puts(io,"\n");
  2431. }
  2432. BIO_puts(io,"\n");
  2433. p=SSL_get_shared_ciphers(con,buf,bufsize);
  2434. if (p != NULL)
  2435. {
  2436. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  2437. j=i=0;
  2438. while (*p)
  2439. {
  2440. if (*p == ':')
  2441. {
  2442. BIO_write(io,space,26-j);
  2443. i++;
  2444. j=0;
  2445. BIO_write(io,((i%3)?" ":"\n"),1);
  2446. }
  2447. else
  2448. {
  2449. BIO_write(io,p,1);
  2450. j++;
  2451. }
  2452. p++;
  2453. }
  2454. BIO_puts(io,"\n");
  2455. }
  2456. BIO_printf(io,(SSL_cache_hit(con)
  2457. ?"---\nReused, "
  2458. :"---\nNew, "));
  2459. c=SSL_get_current_cipher(con);
  2460. BIO_printf(io,"%s, Cipher is %s\n",
  2461. SSL_CIPHER_get_version(c),
  2462. SSL_CIPHER_get_name(c));
  2463. SSL_SESSION_print(io,SSL_get_session(con));
  2464. BIO_printf(io,"---\n");
  2465. print_stats(io,SSL_get_SSL_CTX(con));
  2466. BIO_printf(io,"---\n");
  2467. peer=SSL_get_peer_certificate(con);
  2468. if (peer != NULL)
  2469. {
  2470. BIO_printf(io,"Client certificate\n");
  2471. X509_print(io,peer);
  2472. PEM_write_bio_X509(io,peer);
  2473. }
  2474. else
  2475. BIO_puts(io,"no client certificate available\n");
  2476. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  2477. break;
  2478. }
  2479. else if ((www == 2 || www == 3)
  2480. && (strncmp("GET /",buf,5) == 0))
  2481. {
  2482. BIO *file;
  2483. char *p,*e;
  2484. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2485. /* skip the '/' */
  2486. p= &(buf[5]);
  2487. dot = 1;
  2488. for (e=p; *e != '\0'; e++)
  2489. {
  2490. if (e[0] == ' ')
  2491. break;
  2492. switch (dot)
  2493. {
  2494. case 1:
  2495. dot = (e[0] == '.') ? 2 : 0;
  2496. break;
  2497. case 2:
  2498. dot = (e[0] == '.') ? 3 : 0;
  2499. break;
  2500. case 3:
  2501. dot = (e[0] == '/') ? -1 : 0;
  2502. break;
  2503. }
  2504. if (dot == 0)
  2505. dot = (e[0] == '/') ? 1 : 0;
  2506. }
  2507. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2508. if (*e == '\0')
  2509. {
  2510. BIO_puts(io,text);
  2511. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2512. break;
  2513. }
  2514. *e='\0';
  2515. if (dot)
  2516. {
  2517. BIO_puts(io,text);
  2518. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2519. break;
  2520. }
  2521. if (*p == '/')
  2522. {
  2523. BIO_puts(io,text);
  2524. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2525. break;
  2526. }
  2527. #if 0
  2528. /* append if a directory lookup */
  2529. if (e[-1] == '/')
  2530. strcat(p,"index.html");
  2531. #endif
  2532. /* if a directory, do the index thang */
  2533. if (app_isdir(p)>0)
  2534. {
  2535. #if 0 /* must check buffer size */
  2536. strcat(p,"/index.html");
  2537. #else
  2538. BIO_puts(io,text);
  2539. BIO_printf(io,"'%s' is a directory\r\n",p);
  2540. break;
  2541. #endif
  2542. }
  2543. if ((file=BIO_new_file(p,"r")) == NULL)
  2544. {
  2545. BIO_puts(io,text);
  2546. BIO_printf(io,"Error opening '%s'\r\n",p);
  2547. ERR_print_errors(io);
  2548. break;
  2549. }
  2550. if (!s_quiet)
  2551. BIO_printf(bio_err,"FILE:%s\n",p);
  2552. if (www == 2)
  2553. {
  2554. i=strlen(p);
  2555. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2556. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2557. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2558. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2559. else
  2560. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2561. }
  2562. /* send the file */
  2563. for (;;)
  2564. {
  2565. i=BIO_read(file,buf,bufsize);
  2566. if (i <= 0) break;
  2567. #ifdef RENEG
  2568. total_bytes+=i;
  2569. fprintf(stderr,"%d\n",i);
  2570. if (total_bytes > 3*1024)
  2571. {
  2572. total_bytes=0;
  2573. fprintf(stderr,"RENEGOTIATE\n");
  2574. SSL_renegotiate(con);
  2575. }
  2576. #endif
  2577. for (j=0; j<i; )
  2578. {
  2579. #ifdef RENEG
  2580. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2581. #endif
  2582. k=BIO_write(io,&(buf[j]),i-j);
  2583. if (k <= 0)
  2584. {
  2585. if (!BIO_should_retry(io))
  2586. goto write_error;
  2587. else
  2588. {
  2589. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2590. }
  2591. }
  2592. else
  2593. {
  2594. j+=k;
  2595. }
  2596. }
  2597. }
  2598. write_error:
  2599. BIO_free(file);
  2600. break;
  2601. }
  2602. }
  2603. for (;;)
  2604. {
  2605. i=(int)BIO_flush(io);
  2606. if (i <= 0)
  2607. {
  2608. if (!BIO_should_retry(io))
  2609. break;
  2610. }
  2611. else
  2612. break;
  2613. }
  2614. end:
  2615. #if 1
  2616. /* make sure we re-use sessions */
  2617. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2618. #else
  2619. /* This kills performance */
  2620. /* SSL_shutdown(con); A shutdown gets sent in the
  2621. * BIO_free_all(io) procession */
  2622. #endif
  2623. err:
  2624. if (ret >= 0)
  2625. BIO_printf(bio_s_out,"ACCEPT\n");
  2626. if (buf != NULL) OPENSSL_free(buf);
  2627. if (io != NULL) BIO_free_all(io);
  2628. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2629. return(ret);
  2630. }
  2631. #ifndef OPENSSL_NO_RSA
  2632. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2633. {
  2634. BIGNUM *bn = NULL;
  2635. static RSA *rsa_tmp=NULL;
  2636. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2637. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2638. if (!rsa_tmp && bn)
  2639. {
  2640. if (!s_quiet)
  2641. {
  2642. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2643. (void)BIO_flush(bio_err);
  2644. }
  2645. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2646. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2647. {
  2648. if(rsa_tmp) RSA_free(rsa_tmp);
  2649. rsa_tmp = NULL;
  2650. }
  2651. if (!s_quiet)
  2652. {
  2653. BIO_printf(bio_err,"\n");
  2654. (void)BIO_flush(bio_err);
  2655. }
  2656. BN_free(bn);
  2657. }
  2658. return(rsa_tmp);
  2659. }
  2660. #endif
  2661. #define MAX_SESSION_ID_ATTEMPTS 10
  2662. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2663. unsigned int *id_len)
  2664. {
  2665. unsigned int count = 0;
  2666. do {
  2667. RAND_pseudo_bytes(id, *id_len);
  2668. /* Prefix the session_id with the required prefix. NB: If our
  2669. * prefix is too long, clip it - but there will be worse effects
  2670. * anyway, eg. the server could only possibly create 1 session
  2671. * ID (ie. the prefix!) so all future session negotiations will
  2672. * fail due to conflicts. */
  2673. memcpy(id, session_id_prefix,
  2674. (strlen(session_id_prefix) < *id_len) ?
  2675. strlen(session_id_prefix) : *id_len);
  2676. }
  2677. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2678. (++count < MAX_SESSION_ID_ATTEMPTS));
  2679. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2680. return 0;
  2681. return 1;
  2682. }
  2683. /* By default s_server uses an in-memory cache which caches SSL_SESSION
  2684. * structures without any serialisation. This hides some bugs which only
  2685. * become apparent in deployed servers. By implementing a basic external
  2686. * session cache some issues can be debugged using s_server.
  2687. */
  2688. typedef struct simple_ssl_session_st
  2689. {
  2690. unsigned char *id;
  2691. unsigned int idlen;
  2692. unsigned char *der;
  2693. int derlen;
  2694. struct simple_ssl_session_st *next;
  2695. } simple_ssl_session;
  2696. static simple_ssl_session *first = NULL;
  2697. static int add_session(SSL *ssl, SSL_SESSION *session)
  2698. {
  2699. simple_ssl_session *sess;
  2700. unsigned char *p;
  2701. sess = OPENSSL_malloc(sizeof(simple_ssl_session));
  2702. sess->idlen = SSL_SESSION_get_id_len(session);
  2703. sess->derlen = i2d_SSL_SESSION(session, NULL);
  2704. sess->id = BUF_memdup(SSL_SESSION_get0_id(session), sess->idlen);
  2705. sess->der = OPENSSL_malloc(sess->derlen);
  2706. p = sess->der;
  2707. i2d_SSL_SESSION(session, &p);
  2708. sess->next = first;
  2709. first = sess;
  2710. BIO_printf(bio_err, "New session added to external cache\n");
  2711. return 0;
  2712. }
  2713. static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
  2714. int *do_copy)
  2715. {
  2716. simple_ssl_session *sess;
  2717. *do_copy = 0;
  2718. for (sess = first; sess; sess = sess->next)
  2719. {
  2720. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen))
  2721. {
  2722. const unsigned char *p = sess->der;
  2723. BIO_printf(bio_err, "Lookup session: cache hit\n");
  2724. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  2725. }
  2726. }
  2727. BIO_printf(bio_err, "Lookup session: cache miss\n");
  2728. return NULL;
  2729. }
  2730. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  2731. {
  2732. simple_ssl_session *sess, *prev = NULL;
  2733. const unsigned char *id = SSL_SESSION_get0_id(session);
  2734. unsigned int idlen = SSL_SESSION_get_id_len(session);
  2735. for (sess = first; sess; sess = sess->next)
  2736. {
  2737. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen))
  2738. {
  2739. if(prev)
  2740. prev->next = sess->next;
  2741. else
  2742. first = sess->next;
  2743. OPENSSL_free(sess->id);
  2744. OPENSSL_free(sess->der);
  2745. OPENSSL_free(sess);
  2746. return;
  2747. }
  2748. prev = sess;
  2749. }
  2750. }
  2751. static void init_session_cache_ctx(SSL_CTX *sctx)
  2752. {
  2753. SSL_CTX_set_session_cache_mode(sctx,
  2754. SSL_SESS_CACHE_NO_INTERNAL|SSL_SESS_CACHE_SERVER);
  2755. SSL_CTX_sess_set_new_cb(sctx, add_session);
  2756. SSL_CTX_sess_set_get_cb(sctx, get_session);
  2757. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  2758. }
  2759. static void free_sessions(void)
  2760. {
  2761. simple_ssl_session *sess, *tsess;
  2762. for (sess = first; sess;)
  2763. {
  2764. OPENSSL_free(sess->id);
  2765. OPENSSL_free(sess->der);
  2766. tsess = sess;
  2767. sess = sess->next;
  2768. OPENSSL_free(tsess);
  2769. }
  2770. first = NULL;
  2771. }