s_server.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519
  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. /*
  143. * Until the key-gen callbacks are modified to use newer prototypes, we allow
  144. * deprecated functions for openssl-internal code
  145. */
  146. #ifdef OPENSSL_NO_DEPRECATED
  147. # undef OPENSSL_NO_DEPRECATED
  148. #endif
  149. #include <assert.h>
  150. #include <ctype.h>
  151. #include <stdio.h>
  152. #include <stdlib.h>
  153. #include <string.h>
  154. #include <openssl/e_os2.h>
  155. #ifdef OPENSSL_NO_STDIO
  156. # define APPS_WIN16
  157. #endif
  158. /* conflicts with winsock2 stuff on netware */
  159. #if !defined(OPENSSL_SYS_NETWARE)
  160. # include <sys/types.h>
  161. #endif
  162. /*
  163. * With IPv6, it looks like Digital has mixed up the proper order of
  164. * recursive header file inclusion, resulting in the compiler complaining
  165. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  166. * needed to have fileno() declared correctly... So let's define u_int
  167. */
  168. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  169. # define __U_INT
  170. typedef unsigned int u_int;
  171. #endif
  172. #include <openssl/lhash.h>
  173. #include <openssl/bn.h>
  174. #define USE_SOCKETS
  175. #include "apps.h"
  176. #include <openssl/err.h>
  177. #include <openssl/pem.h>
  178. #include <openssl/x509.h>
  179. #include <openssl/ssl.h>
  180. #include <openssl/rand.h>
  181. #include <openssl/ocsp.h>
  182. #ifndef OPENSSL_NO_DH
  183. # include <openssl/dh.h>
  184. #endif
  185. #ifndef OPENSSL_NO_RSA
  186. # include <openssl/rsa.h>
  187. #endif
  188. #ifndef OPENSSL_NO_SRP
  189. # include <openssl/srp.h>
  190. #endif
  191. #include "s_apps.h"
  192. #include "timeouts.h"
  193. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  194. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  195. # undef FIONBIO
  196. #endif
  197. #if defined(OPENSSL_SYS_BEOS_R5)
  198. # include <fcntl.h>
  199. #endif
  200. #ifndef OPENSSL_NO_RSA
  201. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  202. #endif
  203. static int sv_body(char *hostname, int s, int stype, unsigned char *context);
  204. static int www_body(char *hostname, int s, int stype, unsigned char *context);
  205. static int rev_body(char *hostname, int s, int stype, unsigned char *context);
  206. static void close_accept_socket(void);
  207. static void sv_usage(void);
  208. static int init_ssl_connection(SSL *s);
  209. static void print_stats(BIO *bp, SSL_CTX *ctx);
  210. static int generate_session_id(const SSL *ssl, unsigned char *id,
  211. unsigned int *id_len);
  212. static void init_session_cache_ctx(SSL_CTX *sctx);
  213. static void free_sessions(void);
  214. #ifndef OPENSSL_NO_DH
  215. static DH *load_dh_param(const char *dhfile);
  216. static DH *get_dh2048(void);
  217. #endif
  218. #ifdef MONOLITH
  219. static void s_server_init(void);
  220. #endif
  221. #ifndef OPENSSL_NO_DH
  222. static unsigned char dh2048_p[] = {
  223. 0xF6,0x42,0x57,0xB7,0x08,0x7F,0x08,0x17,0x72,0xA2,0xBA,0xD6,
  224. 0xA9,0x42,0xF3,0x05,0xE8,0xF9,0x53,0x11,0x39,0x4F,0xB6,0xF1,
  225. 0x6E,0xB9,0x4B,0x38,0x20,0xDA,0x01,0xA7,0x56,0xA3,0x14,0xE9,
  226. 0x8F,0x40,0x55,0xF3,0xD0,0x07,0xC6,0xCB,0x43,0xA9,0x94,0xAD,
  227. 0xF7,0x4C,0x64,0x86,0x49,0xF8,0x0C,0x83,0xBD,0x65,0xE9,0x17,
  228. 0xD4,0xA1,0xD3,0x50,0xF8,0xF5,0x59,0x5F,0xDC,0x76,0x52,0x4F,
  229. 0x3D,0x3D,0x8D,0xDB,0xCE,0x99,0xE1,0x57,0x92,0x59,0xCD,0xFD,
  230. 0xB8,0xAE,0x74,0x4F,0xC5,0xFC,0x76,0xBC,0x83,0xC5,0x47,0x30,
  231. 0x61,0xCE,0x7C,0xC9,0x66,0xFF,0x15,0xF9,0xBB,0xFD,0x91,0x5E,
  232. 0xC7,0x01,0xAA,0xD3,0x5B,0x9E,0x8D,0xA0,0xA5,0x72,0x3A,0xD4,
  233. 0x1A,0xF0,0xBF,0x46,0x00,0x58,0x2B,0xE5,0xF4,0x88,0xFD,0x58,
  234. 0x4E,0x49,0xDB,0xCD,0x20,0xB4,0x9D,0xE4,0x91,0x07,0x36,0x6B,
  235. 0x33,0x6C,0x38,0x0D,0x45,0x1D,0x0F,0x7C,0x88,0xB3,0x1C,0x7C,
  236. 0x5B,0x2D,0x8E,0xF6,0xF3,0xC9,0x23,0xC0,0x43,0xF0,0xA5,0x5B,
  237. 0x18,0x8D,0x8E,0xBB,0x55,0x8C,0xB8,0x5D,0x38,0xD3,0x34,0xFD,
  238. 0x7C,0x17,0x57,0x43,0xA3,0x1D,0x18,0x6C,0xDE,0x33,0x21,0x2C,
  239. 0xB5,0x2A,0xFF,0x3C,0xE1,0xB1,0x29,0x40,0x18,0x11,0x8D,0x7C,
  240. 0x84,0xA7,0x0A,0x72,0xD6,0x86,0xC4,0x03,0x19,0xC8,0x07,0x29,
  241. 0x7A,0xCA,0x95,0x0C,0xD9,0x96,0x9F,0xAB,0xD0,0x0A,0x50,0x9B,
  242. 0x02,0x46,0xD3,0x08,0x3D,0x66,0xA4,0x5D,0x41,0x9F,0x9C,0x7C,
  243. 0xBD,0x89,0x4B,0x22,0x19,0x26,0xBA,0xAB,0xA2,0x5E,0xC3,0x55,
  244. 0xE9,0x32,0x0B,0x3B,
  245. };
  246. static unsigned char dh2048_g[] = {
  247. 0x02,
  248. };
  249. DH *get_dh2048()
  250. {
  251. DH *dh;
  252. if ((dh = DH_new()) == NULL)
  253. return NULL;
  254. dh->p=BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
  255. dh->g=BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
  256. if (dh->p == NULL || dh->g == NULL) {
  257. DH_free(dh);
  258. return NULL;
  259. }
  260. return dh;
  261. }
  262. #endif
  263. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  264. #undef BUFSIZZ
  265. #define BUFSIZZ 16*1024
  266. static int bufsize = BUFSIZZ;
  267. static int accept_socket = -1;
  268. #define TEST_CERT "server.pem"
  269. #ifndef OPENSSL_NO_TLSEXT
  270. # define TEST_CERT2 "server2.pem"
  271. #endif
  272. #undef PROG
  273. #define PROG s_server_main
  274. extern int verify_depth, verify_return_error, verify_quiet;
  275. static int s_server_verify = SSL_VERIFY_NONE;
  276. static int s_server_session_id_context = 1; /* anything will do */
  277. static const char *s_cert_file = TEST_CERT, *s_key_file =
  278. NULL, *s_chain_file = NULL;
  279. #ifndef OPENSSL_NO_TLSEXT
  280. static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
  281. #endif
  282. static char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
  283. #ifdef FIONBIO
  284. static int s_nbio = 0;
  285. #endif
  286. static int s_nbio_test = 0;
  287. int s_crlf = 0;
  288. static SSL_CTX *ctx = NULL;
  289. #ifndef OPENSSL_NO_TLSEXT
  290. static SSL_CTX *ctx2 = NULL;
  291. #endif
  292. static int www = 0;
  293. static BIO *bio_s_out = NULL;
  294. static BIO *bio_s_msg = NULL;
  295. static int s_debug = 0;
  296. #ifndef OPENSSL_NO_TLSEXT
  297. static int s_tlsextdebug = 0;
  298. static int s_tlsextstatus = 0;
  299. static int cert_status_cb(SSL *s, void *arg);
  300. #endif
  301. static int no_resume_ephemeral = 0;
  302. static int s_msg = 0;
  303. static int s_quiet = 0;
  304. static int s_ign_eof = 0;
  305. static int s_brief = 0;
  306. static char *keymatexportlabel = NULL;
  307. static int keymatexportlen = 20;
  308. static int hack = 0;
  309. static char *engine_id = NULL;
  310. static const char *session_id_prefix = NULL;
  311. static int enable_timeouts = 0;
  312. static long socket_mtu;
  313. #ifndef OPENSSL_NO_DTLS1
  314. static int cert_chain = 0;
  315. #endif
  316. #ifndef OPENSSL_NO_TLSEXT
  317. static BIO *serverinfo_in = NULL;
  318. static const char *s_serverinfo_file = NULL;
  319. #endif
  320. #ifndef OPENSSL_NO_PSK
  321. static char *psk_identity = "Client_identity";
  322. char *psk_key = NULL; /* by default PSK is not used */
  323. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  324. unsigned char *psk,
  325. unsigned int max_psk_len)
  326. {
  327. long key_len = 0;
  328. unsigned char *key;
  329. if (s_debug)
  330. BIO_printf(bio_s_out, "psk_server_cb\n");
  331. if (!identity) {
  332. BIO_printf(bio_err, "Error: client did not send PSK identity\n");
  333. goto out_err;
  334. }
  335. if (s_debug)
  336. BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
  337. (int)strlen(identity), identity);
  338. /* here we could lookup the given identity e.g. from a database */
  339. if (strcmp(identity, psk_identity) != 0) {
  340. BIO_printf(bio_s_out, "PSK error: client identity not found"
  341. " (got '%s' expected '%s')\n", identity, psk_identity);
  342. goto out_err;
  343. }
  344. if (s_debug)
  345. BIO_printf(bio_s_out, "PSK client identity found\n");
  346. /* convert the PSK key to binary */
  347. key = string_to_hex(psk_key, &key_len);
  348. if (key == NULL) {
  349. BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
  350. psk_key);
  351. return 0;
  352. }
  353. if (key_len > (int)max_psk_len) {
  354. BIO_printf(bio_err,
  355. "psk buffer of callback is too small (%d) for key (%ld)\n",
  356. max_psk_len, key_len);
  357. OPENSSL_free(key);
  358. return 0;
  359. }
  360. memcpy(psk, key, key_len);
  361. OPENSSL_free(key);
  362. if (s_debug)
  363. BIO_printf(bio_s_out, "fetched PSK len=%ld\n", key_len);
  364. return key_len;
  365. out_err:
  366. if (s_debug)
  367. BIO_printf(bio_err, "Error in PSK server callback\n");
  368. return 0;
  369. }
  370. #endif
  371. #ifndef OPENSSL_NO_SRP
  372. /* This is a context that we pass to callbacks */
  373. typedef struct srpsrvparm_st {
  374. char *login;
  375. SRP_VBASE *vb;
  376. SRP_user_pwd *user;
  377. } srpsrvparm;
  378. /*
  379. * This callback pretends to require some asynchronous logic in order to
  380. * obtain a verifier. When the callback is called for a new connection we
  381. * return with a negative value. This will provoke the accept etc to return
  382. * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
  383. * (which would normally occur after a worker has finished) and we set the
  384. * user parameters.
  385. */
  386. static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  387. {
  388. srpsrvparm *p = (srpsrvparm *) arg;
  389. int ret = SSL3_AL_FATAL;
  390. if (p->login == NULL && p->user == NULL) {
  391. p->login = SSL_get_srp_username(s);
  392. BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
  393. return (-1);
  394. }
  395. if (p->user == NULL) {
  396. BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
  397. goto err;
  398. }
  399. if (SSL_set_srp_server_param
  400. (s, p->user->N, p->user->g, p->user->s, p->user->v,
  401. p->user->info) < 0) {
  402. *ad = SSL_AD_INTERNAL_ERROR;
  403. goto err;
  404. }
  405. BIO_printf(bio_err,
  406. "SRP parameters set: username = \"%s\" info=\"%s\" \n",
  407. p->login, p->user->info);
  408. ret = SSL_ERROR_NONE;
  409. err:
  410. SRP_user_pwd_free(p->user);
  411. p->user = NULL;
  412. p->login = NULL;
  413. return ret;
  414. }
  415. #endif
  416. #ifdef MONOLITH
  417. static void s_server_init(void)
  418. {
  419. accept_socket = -1;
  420. s_server_verify = SSL_VERIFY_NONE;
  421. s_dcert_file = NULL;
  422. s_dkey_file = NULL;
  423. s_dchain_file = NULL;
  424. s_cert_file = TEST_CERT;
  425. s_key_file = NULL;
  426. s_chain_file = NULL;
  427. # ifndef OPENSSL_NO_TLSEXT
  428. s_cert_file2 = TEST_CERT2;
  429. s_key_file2 = NULL;
  430. ctx2 = NULL;
  431. # endif
  432. # ifdef FIONBIO
  433. s_nbio = 0;
  434. # endif
  435. s_nbio_test = 0;
  436. ctx = NULL;
  437. www = 0;
  438. bio_s_out = NULL;
  439. s_debug = 0;
  440. s_msg = 0;
  441. s_quiet = 0;
  442. s_brief = 0;
  443. hack = 0;
  444. engine_id = NULL;
  445. }
  446. #endif
  447. static void sv_usage(void)
  448. {
  449. BIO_printf(bio_err, "usage: s_server [args ...]\n");
  450. BIO_printf(bio_err, "\n");
  451. BIO_printf(bio_err,
  452. " -accept arg - port to accept on (default is %d)\n", PORT);
  453. BIO_printf(bio_err,
  454. " -verify_hostname host - check peer certificate matches \"host\"\n");
  455. BIO_printf(bio_err,
  456. " -verify_email email - check peer certificate matches \"email\"\n");
  457. BIO_printf(bio_err,
  458. " -verify_ip ipaddr - check peer certificate matches \"ipaddr\"\n");
  459. BIO_printf(bio_err, " -context arg - set session ID context\n");
  460. BIO_printf(bio_err,
  461. " -verify arg - turn on peer certificate verification\n");
  462. BIO_printf(bio_err,
  463. " -Verify arg - turn on peer certificate verification, must have a cert.\n");
  464. BIO_printf(bio_err,
  465. " -verify_return_error - return verification errors\n");
  466. BIO_printf(bio_err, " -cert arg - certificate file to use\n");
  467. BIO_printf(bio_err, " (default is %s)\n", TEST_CERT);
  468. #ifndef OPENSSL_NO_TLSEXT
  469. BIO_printf(bio_err,
  470. " -serverinfo arg - PEM serverinfo file for certificate\n");
  471. BIO_printf(bio_err,
  472. " -auth - send and receive RFC 5878 TLS auth extensions and supplemental data\n");
  473. BIO_printf(bio_err,
  474. " -auth_require_reneg - Do not send TLS auth extensions until renegotiation\n");
  475. #endif
  476. BIO_printf(bio_err,
  477. " -no_resumption_on_reneg - set SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION flag\n");
  478. BIO_printf(bio_err,
  479. " -crl_check - check the peer certificate has not been revoked by its CA.\n"
  480. " The CRL(s) are appended to the certificate file\n");
  481. BIO_printf(bio_err,
  482. " -crl_check_all - check the peer certificate has not been revoked by its CA\n"
  483. " or any other CRL in the CA chain. CRL(s) are appened to the\n"
  484. " the certificate file.\n");
  485. BIO_printf(bio_err,
  486. " -certform arg - certificate format (PEM or DER) PEM default\n");
  487. BIO_printf(bio_err,
  488. " -key arg - Private Key file to use, in cert file if\n");
  489. BIO_printf(bio_err, " not specified (default is %s)\n",
  490. TEST_CERT);
  491. BIO_printf(bio_err,
  492. " -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  493. BIO_printf(bio_err,
  494. " -pass arg - private key file pass phrase source\n");
  495. BIO_printf(bio_err,
  496. " -dcert arg - second certificate file to use (usually for DSA)\n");
  497. BIO_printf(bio_err,
  498. " -dcertform x - second certificate format (PEM or DER) PEM default\n");
  499. BIO_printf(bio_err,
  500. " -dkey arg - second private key file to use (usually for DSA)\n");
  501. BIO_printf(bio_err,
  502. " -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  503. BIO_printf(bio_err,
  504. " -dpass arg - second private key file pass phrase source\n");
  505. BIO_printf(bio_err,
  506. " -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  507. BIO_printf(bio_err,
  508. " or a default set of parameters is used\n");
  509. #ifndef OPENSSL_NO_ECDH
  510. BIO_printf(bio_err,
  511. " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n"
  512. " Use \"openssl ecparam -list_curves\" for all names\n"
  513. " (default is nistp256).\n");
  514. #endif
  515. #ifdef FIONBIO
  516. BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
  517. #endif
  518. BIO_printf(bio_err,
  519. " -nbio_test - test with the non-blocking test bio\n");
  520. BIO_printf(bio_err,
  521. " -crlf - convert LF from terminal into CRLF\n");
  522. BIO_printf(bio_err, " -debug - Print more output\n");
  523. BIO_printf(bio_err, " -msg - Show protocol messages\n");
  524. BIO_printf(bio_err, " -state - Print the SSL states\n");
  525. BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
  526. BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
  527. BIO_printf(bio_err,
  528. " -no_alt_chains - only ever use the first certificate chain found\n");
  529. BIO_printf(bio_err,
  530. " -nocert - Don't use any certificates (Anon-DH)\n");
  531. BIO_printf(bio_err,
  532. " -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  533. BIO_printf(bio_err, " -serverpref - Use server's cipher preferences\n");
  534. BIO_printf(bio_err, " -quiet - No server output\n");
  535. BIO_printf(bio_err, " -no_tmp_rsa - Do not generate a tmp RSA key\n");
  536. #ifndef OPENSSL_NO_PSK
  537. BIO_printf(bio_err, " -psk_hint arg - PSK identity hint to use\n");
  538. BIO_printf(bio_err, " -psk arg - PSK in hex (without 0x)\n");
  539. # ifndef OPENSSL_NO_JPAKE
  540. BIO_printf(bio_err, " -jpake arg - JPAKE secret to use\n");
  541. # endif
  542. #endif
  543. #ifndef OPENSSL_NO_SRP
  544. BIO_printf(bio_err, " -srpvfile file - The verifier file for SRP\n");
  545. BIO_printf(bio_err,
  546. " -srpuserseed string - A seed string for a default user salt.\n");
  547. #endif
  548. BIO_printf(bio_err, " -ssl2 - Just talk SSLv2\n");
  549. #ifndef OPENSSL_NO_SSL3_METHOD
  550. BIO_printf(bio_err, " -ssl3 - Just talk SSLv3\n");
  551. #endif
  552. BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n");
  553. BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n");
  554. BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n");
  555. BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n");
  556. BIO_printf(bio_err, " -dtls1_2 - Just talk DTLSv1.2\n");
  557. BIO_printf(bio_err, " -timeout - Enable timeouts\n");
  558. BIO_printf(bio_err, " -mtu - Set link layer MTU\n");
  559. BIO_printf(bio_err, " -chain - Read a certificate chain\n");
  560. BIO_printf(bio_err, " -no_ssl2 - Just disable SSLv2\n");
  561. BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n");
  562. BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n");
  563. BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n");
  564. BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n");
  565. #ifndef OPENSSL_NO_DH
  566. BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n");
  567. #endif
  568. #ifndef OPENSSL_NO_ECDH
  569. BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n");
  570. #endif
  571. BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n");
  572. BIO_printf(bio_err,
  573. " -hack - workaround for early Netscape code\n");
  574. BIO_printf(bio_err,
  575. " -www - Respond to a 'GET /' with a status page\n");
  576. BIO_printf(bio_err,
  577. " -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  578. BIO_printf(bio_err,
  579. " -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  580. BIO_printf(bio_err,
  581. " with the assumption it contains a complete HTTP response.\n");
  582. #ifndef OPENSSL_NO_ENGINE
  583. BIO_printf(bio_err,
  584. " -engine id - Initialise and use the specified engine\n");
  585. #endif
  586. BIO_printf(bio_err,
  587. " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  588. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  589. LIST_SEPARATOR_CHAR);
  590. #ifndef OPENSSL_NO_TLSEXT
  591. BIO_printf(bio_err,
  592. " -servername host - servername for HostName TLS extension\n");
  593. BIO_printf(bio_err,
  594. " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  595. BIO_printf(bio_err,
  596. " -cert2 arg - certificate file to use for servername\n");
  597. BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2);
  598. BIO_printf(bio_err,
  599. " -key2 arg - Private Key file to use for servername, in cert file if\n");
  600. BIO_printf(bio_err, " not specified (default is %s)\n",
  601. TEST_CERT2);
  602. BIO_printf(bio_err,
  603. " -tlsextdebug - hex dump of all TLS extensions received\n");
  604. BIO_printf(bio_err,
  605. " -no_ticket - disable use of RFC4507bis session tickets\n");
  606. BIO_printf(bio_err,
  607. " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
  608. BIO_printf(bio_err,
  609. " -sigalgs arg - Signature algorithms to support (colon-separated list)\n");
  610. BIO_printf(bio_err,
  611. " -client_sigalgs arg - Signature algorithms to support for client \n");
  612. BIO_printf(bio_err,
  613. " certificate authentication (colon-separated list)\n");
  614. # ifndef OPENSSL_NO_NEXTPROTONEG
  615. BIO_printf(bio_err,
  616. " -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
  617. # endif
  618. # ifndef OPENSSL_NO_SRTP
  619. BIO_printf(bio_err,
  620. " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
  621. # endif
  622. BIO_printf(bio_err,
  623. " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n");
  624. #endif
  625. BIO_printf(bio_err,
  626. " -keymatexport label - Export keying material using label\n");
  627. BIO_printf(bio_err,
  628. " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
  629. BIO_printf(bio_err,
  630. " -status - respond to certificate status requests\n");
  631. BIO_printf(bio_err,
  632. " -status_verbose - enable status request verbose printout\n");
  633. BIO_printf(bio_err,
  634. " -status_timeout n - status request responder timeout\n");
  635. BIO_printf(bio_err, " -status_url URL - status request fallback URL\n");
  636. }
  637. static int local_argc = 0;
  638. static char **local_argv;
  639. #ifdef CHARSET_EBCDIC
  640. static int ebcdic_new(BIO *bi);
  641. static int ebcdic_free(BIO *a);
  642. static int ebcdic_read(BIO *b, char *out, int outl);
  643. static int ebcdic_write(BIO *b, const char *in, int inl);
  644. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  645. static int ebcdic_gets(BIO *bp, char *buf, int size);
  646. static int ebcdic_puts(BIO *bp, const char *str);
  647. # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  648. static BIO_METHOD methods_ebcdic = {
  649. BIO_TYPE_EBCDIC_FILTER,
  650. "EBCDIC/ASCII filter",
  651. ebcdic_write,
  652. ebcdic_read,
  653. ebcdic_puts,
  654. ebcdic_gets,
  655. ebcdic_ctrl,
  656. ebcdic_new,
  657. ebcdic_free,
  658. };
  659. typedef struct {
  660. size_t alloced;
  661. char buff[1];
  662. } EBCDIC_OUTBUFF;
  663. BIO_METHOD *BIO_f_ebcdic_filter()
  664. {
  665. return (&methods_ebcdic);
  666. }
  667. static int ebcdic_new(BIO *bi)
  668. {
  669. EBCDIC_OUTBUFF *wbuf;
  670. wbuf = (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  671. if (!wbuf)
  672. return 0;
  673. wbuf->alloced = 1024;
  674. wbuf->buff[0] = '\0';
  675. bi->ptr = (char *)wbuf;
  676. bi->init = 1;
  677. bi->flags = 0;
  678. return (1);
  679. }
  680. static int ebcdic_free(BIO *a)
  681. {
  682. if (a == NULL)
  683. return (0);
  684. if (a->ptr != NULL)
  685. OPENSSL_free(a->ptr);
  686. a->ptr = NULL;
  687. a->init = 0;
  688. a->flags = 0;
  689. return (1);
  690. }
  691. static int ebcdic_read(BIO *b, char *out, int outl)
  692. {
  693. int ret = 0;
  694. if (out == NULL || outl == 0)
  695. return (0);
  696. if (b->next_bio == NULL)
  697. return (0);
  698. ret = BIO_read(b->next_bio, out, outl);
  699. if (ret > 0)
  700. ascii2ebcdic(out, out, ret);
  701. return (ret);
  702. }
  703. static int ebcdic_write(BIO *b, const char *in, int inl)
  704. {
  705. EBCDIC_OUTBUFF *wbuf;
  706. int ret = 0;
  707. int num;
  708. unsigned char n;
  709. if ((in == NULL) || (inl <= 0))
  710. return (0);
  711. if (b->next_bio == NULL)
  712. return (0);
  713. wbuf = (EBCDIC_OUTBUFF *) b->ptr;
  714. if (inl > (num = wbuf->alloced)) {
  715. num = num + num; /* double the size */
  716. if (num < inl)
  717. num = inl;
  718. wbuf =
  719. (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  720. if (!wbuf)
  721. return 0;
  722. OPENSSL_free(b->ptr);
  723. wbuf->alloced = num;
  724. wbuf->buff[0] = '\0';
  725. b->ptr = (char *)wbuf;
  726. }
  727. ebcdic2ascii(wbuf->buff, in, inl);
  728. ret = BIO_write(b->next_bio, wbuf->buff, inl);
  729. return (ret);
  730. }
  731. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  732. {
  733. long ret;
  734. if (b->next_bio == NULL)
  735. return (0);
  736. switch (cmd) {
  737. case BIO_CTRL_DUP:
  738. ret = 0L;
  739. break;
  740. default:
  741. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  742. break;
  743. }
  744. return (ret);
  745. }
  746. static int ebcdic_gets(BIO *bp, char *buf, int size)
  747. {
  748. int i, ret = 0;
  749. if (bp->next_bio == NULL)
  750. return (0);
  751. /* return(BIO_gets(bp->next_bio,buf,size));*/
  752. for (i = 0; i < size - 1; ++i) {
  753. ret = ebcdic_read(bp, &buf[i], 1);
  754. if (ret <= 0)
  755. break;
  756. else if (buf[i] == '\n') {
  757. ++i;
  758. break;
  759. }
  760. }
  761. if (i < size)
  762. buf[i] = '\0';
  763. return (ret < 0 && i == 0) ? ret : i;
  764. }
  765. static int ebcdic_puts(BIO *bp, const char *str)
  766. {
  767. if (bp->next_bio == NULL)
  768. return (0);
  769. return ebcdic_write(bp, str, strlen(str));
  770. }
  771. #endif
  772. #ifndef OPENSSL_NO_TLSEXT
  773. /* This is a context that we pass to callbacks */
  774. typedef struct tlsextctx_st {
  775. char *servername;
  776. BIO *biodebug;
  777. int extension_error;
  778. } tlsextctx;
  779. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  780. {
  781. tlsextctx *p = (tlsextctx *) arg;
  782. const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  783. if (servername && p->biodebug)
  784. BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n",
  785. servername);
  786. if (!p->servername)
  787. return SSL_TLSEXT_ERR_NOACK;
  788. if (servername) {
  789. if (strcasecmp(servername, p->servername))
  790. return p->extension_error;
  791. if (ctx2) {
  792. BIO_printf(p->biodebug, "Switching server context.\n");
  793. SSL_set_SSL_CTX(s, ctx2);
  794. }
  795. }
  796. return SSL_TLSEXT_ERR_OK;
  797. }
  798. /* Structure passed to cert status callback */
  799. typedef struct tlsextstatusctx_st {
  800. /* Default responder to use */
  801. char *host, *path, *port;
  802. int use_ssl;
  803. int timeout;
  804. BIO *err;
  805. int verbose;
  806. } tlsextstatusctx;
  807. static tlsextstatusctx tlscstatp = { NULL, NULL, NULL, 0, -1, NULL, 0 };
  808. /*
  809. * Certificate Status callback. This is called when a client includes a
  810. * certificate status request extension. This is a simplified version. It
  811. * examines certificates each time and makes one OCSP responder query for
  812. * each request. A full version would store details such as the OCSP
  813. * certificate IDs and minimise the number of OCSP responses by caching them
  814. * until they were considered "expired".
  815. */
  816. static int cert_status_cb(SSL *s, void *arg)
  817. {
  818. tlsextstatusctx *srctx = arg;
  819. BIO *err = srctx->err;
  820. char *host, *port, *path;
  821. int use_ssl;
  822. unsigned char *rspder = NULL;
  823. int rspderlen;
  824. STACK_OF(OPENSSL_STRING) *aia = NULL;
  825. X509 *x = NULL;
  826. X509_STORE_CTX inctx;
  827. X509_OBJECT obj;
  828. OCSP_REQUEST *req = NULL;
  829. OCSP_RESPONSE *resp = NULL;
  830. OCSP_CERTID *id = NULL;
  831. STACK_OF(X509_EXTENSION) *exts;
  832. int ret = SSL_TLSEXT_ERR_NOACK;
  833. int i;
  834. # if 0
  835. STACK_OF(OCSP_RESPID) *ids;
  836. SSL_get_tlsext_status_ids(s, &ids);
  837. BIO_printf(err, "cert_status: received %d ids\n",
  838. sk_OCSP_RESPID_num(ids));
  839. # endif
  840. if (srctx->verbose)
  841. BIO_puts(err, "cert_status: callback called\n");
  842. /* Build up OCSP query from server certificate */
  843. x = SSL_get_certificate(s);
  844. aia = X509_get1_ocsp(x);
  845. if (aia) {
  846. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  847. &host, &port, &path, &use_ssl)) {
  848. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  849. goto err;
  850. }
  851. if (srctx->verbose)
  852. BIO_printf(err, "cert_status: AIA URL: %s\n",
  853. sk_OPENSSL_STRING_value(aia, 0));
  854. } else {
  855. if (!srctx->host) {
  856. BIO_puts(srctx->err,
  857. "cert_status: no AIA and no default responder URL\n");
  858. goto done;
  859. }
  860. host = srctx->host;
  861. path = srctx->path;
  862. port = srctx->port;
  863. use_ssl = srctx->use_ssl;
  864. }
  865. if (!X509_STORE_CTX_init(&inctx,
  866. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  867. NULL, NULL))
  868. goto err;
  869. if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
  870. X509_get_issuer_name(x), &obj) <= 0) {
  871. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  872. X509_STORE_CTX_cleanup(&inctx);
  873. goto done;
  874. }
  875. req = OCSP_REQUEST_new();
  876. if (!req)
  877. goto err;
  878. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  879. X509_free(obj.data.x509);
  880. X509_STORE_CTX_cleanup(&inctx);
  881. if (!id)
  882. goto err;
  883. if (!OCSP_request_add0_id(req, id))
  884. goto err;
  885. id = NULL;
  886. /* Add any extensions to the request */
  887. SSL_get_tlsext_status_exts(s, &exts);
  888. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  889. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  890. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  891. goto err;
  892. }
  893. resp = process_responder(err, req, host, path, port, use_ssl, NULL,
  894. srctx->timeout);
  895. if (!resp) {
  896. BIO_puts(err, "cert_status: error querying responder\n");
  897. goto done;
  898. }
  899. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  900. if (rspderlen <= 0)
  901. goto err;
  902. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  903. if (srctx->verbose) {
  904. BIO_puts(err, "cert_status: ocsp response sent:\n");
  905. OCSP_RESPONSE_print(err, resp, 2);
  906. }
  907. ret = SSL_TLSEXT_ERR_OK;
  908. done:
  909. if (ret != SSL_TLSEXT_ERR_OK)
  910. ERR_print_errors(err);
  911. if (aia) {
  912. OPENSSL_free(host);
  913. OPENSSL_free(path);
  914. OPENSSL_free(port);
  915. X509_email_free(aia);
  916. }
  917. if (id)
  918. OCSP_CERTID_free(id);
  919. if (req)
  920. OCSP_REQUEST_free(req);
  921. if (resp)
  922. OCSP_RESPONSE_free(resp);
  923. return ret;
  924. err:
  925. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  926. goto done;
  927. }
  928. # ifndef OPENSSL_NO_NEXTPROTONEG
  929. /* This is the context that we pass to next_proto_cb */
  930. typedef struct tlsextnextprotoctx_st {
  931. unsigned char *data;
  932. unsigned int len;
  933. } tlsextnextprotoctx;
  934. static int next_proto_cb(SSL *s, const unsigned char **data,
  935. unsigned int *len, void *arg)
  936. {
  937. tlsextnextprotoctx *next_proto = arg;
  938. *data = next_proto->data;
  939. *len = next_proto->len;
  940. return SSL_TLSEXT_ERR_OK;
  941. }
  942. # endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  943. /* This the context that we pass to alpn_cb */
  944. typedef struct tlsextalpnctx_st {
  945. unsigned char *data;
  946. unsigned short len;
  947. } tlsextalpnctx;
  948. static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
  949. const unsigned char *in, unsigned int inlen, void *arg)
  950. {
  951. tlsextalpnctx *alpn_ctx = arg;
  952. if (!s_quiet) {
  953. /* We can assume that |in| is syntactically valid. */
  954. unsigned i;
  955. BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
  956. for (i = 0; i < inlen;) {
  957. if (i)
  958. BIO_write(bio_s_out, ", ", 2);
  959. BIO_write(bio_s_out, &in[i + 1], in[i]);
  960. i += in[i] + 1;
  961. }
  962. BIO_write(bio_s_out, "\n", 1);
  963. }
  964. if (SSL_select_next_proto
  965. ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
  966. inlen) != OPENSSL_NPN_NEGOTIATED) {
  967. return SSL_TLSEXT_ERR_NOACK;
  968. }
  969. if (!s_quiet) {
  970. BIO_printf(bio_s_out, "ALPN protocols selected: ");
  971. BIO_write(bio_s_out, *out, *outlen);
  972. BIO_write(bio_s_out, "\n", 1);
  973. }
  974. return SSL_TLSEXT_ERR_OK;
  975. }
  976. #endif /* ndef OPENSSL_NO_TLSEXT */
  977. int MAIN(int, char **);
  978. #ifndef OPENSSL_NO_JPAKE
  979. static char *jpake_secret = NULL;
  980. # define no_jpake !jpake_secret
  981. #else
  982. # define no_jpake 1
  983. #endif
  984. #ifndef OPENSSL_NO_SRP
  985. static srpsrvparm srp_callback_parm;
  986. #endif
  987. #ifndef OPENSSL_NO_SRTP
  988. static char *srtp_profiles = NULL;
  989. #endif
  990. int MAIN(int argc, char *argv[])
  991. {
  992. X509_VERIFY_PARAM *vpm = NULL;
  993. int badarg = 0;
  994. short port = PORT;
  995. char *CApath = NULL, *CAfile = NULL;
  996. char *chCApath = NULL, *chCAfile = NULL;
  997. char *vfyCApath = NULL, *vfyCAfile = NULL;
  998. unsigned char *context = NULL;
  999. char *dhfile = NULL;
  1000. int badop = 0;
  1001. int ret = 1;
  1002. int build_chain = 0;
  1003. int no_tmp_rsa = 0, no_dhe = 0, no_ecdhe = 0, nocert = 0;
  1004. int state = 0;
  1005. const SSL_METHOD *meth = NULL;
  1006. int socket_type = SOCK_STREAM;
  1007. ENGINE *e = NULL;
  1008. char *inrand = NULL;
  1009. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  1010. char *passarg = NULL, *pass = NULL;
  1011. char *dpassarg = NULL, *dpass = NULL;
  1012. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  1013. X509 *s_cert = NULL, *s_dcert = NULL;
  1014. STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
  1015. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  1016. int no_cache = 0, ext_cache = 0;
  1017. int rev = 0, naccept = -1;
  1018. #ifndef OPENSSL_NO_TLSEXT
  1019. EVP_PKEY *s_key2 = NULL;
  1020. X509 *s_cert2 = NULL;
  1021. tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
  1022. # ifndef OPENSSL_NO_NEXTPROTONEG
  1023. const char *next_proto_neg_in = NULL;
  1024. tlsextnextprotoctx next_proto = { NULL, 0 };
  1025. # endif
  1026. const char *alpn_in = NULL;
  1027. tlsextalpnctx alpn_ctx = { NULL, 0 };
  1028. #endif
  1029. #ifndef OPENSSL_NO_PSK
  1030. /* by default do not send a PSK identity hint */
  1031. static char *psk_identity_hint = NULL;
  1032. #endif
  1033. #ifndef OPENSSL_NO_SRP
  1034. char *srpuserseed = NULL;
  1035. char *srp_verifier_file = NULL;
  1036. #endif
  1037. SSL_EXCERT *exc = NULL;
  1038. SSL_CONF_CTX *cctx = NULL;
  1039. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  1040. char *crl_file = NULL;
  1041. int crl_format = FORMAT_PEM;
  1042. int crl_download = 0;
  1043. STACK_OF(X509_CRL) *crls = NULL;
  1044. int prot_opt = 0, no_prot_opt = 0;
  1045. meth = SSLv23_server_method();
  1046. local_argc = argc;
  1047. local_argv = argv;
  1048. apps_startup();
  1049. #ifdef MONOLITH
  1050. s_server_init();
  1051. #endif
  1052. if (bio_err == NULL)
  1053. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  1054. if (!load_config(bio_err, NULL))
  1055. goto end;
  1056. cctx = SSL_CONF_CTX_new();
  1057. if (!cctx)
  1058. goto end;
  1059. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
  1060. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);
  1061. verify_depth = 0;
  1062. #ifdef FIONBIO
  1063. s_nbio = 0;
  1064. #endif
  1065. s_nbio_test = 0;
  1066. argc--;
  1067. argv++;
  1068. while (argc >= 1) {
  1069. if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) {
  1070. if (--argc < 1)
  1071. goto bad;
  1072. if (!extract_port(*(++argv), &port))
  1073. goto bad;
  1074. } else if (strcmp(*argv, "-naccept") == 0) {
  1075. if (--argc < 1)
  1076. goto bad;
  1077. naccept = atol(*(++argv));
  1078. if (naccept <= 0) {
  1079. BIO_printf(bio_err, "bad accept value %s\n", *argv);
  1080. goto bad;
  1081. }
  1082. } else if (strcmp(*argv, "-verify") == 0) {
  1083. s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
  1084. if (--argc < 1)
  1085. goto bad;
  1086. verify_depth = atoi(*(++argv));
  1087. if (!s_quiet)
  1088. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  1089. } else if (strcmp(*argv, "-Verify") == 0) {
  1090. s_server_verify =
  1091. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  1092. SSL_VERIFY_CLIENT_ONCE;
  1093. if (--argc < 1)
  1094. goto bad;
  1095. verify_depth = atoi(*(++argv));
  1096. if (!s_quiet)
  1097. BIO_printf(bio_err,
  1098. "verify depth is %d, must return a certificate\n",
  1099. verify_depth);
  1100. } else if (strcmp(*argv, "-context") == 0) {
  1101. if (--argc < 1)
  1102. goto bad;
  1103. context = (unsigned char *)*(++argv);
  1104. } else if (strcmp(*argv, "-cert") == 0) {
  1105. if (--argc < 1)
  1106. goto bad;
  1107. s_cert_file = *(++argv);
  1108. } else if (strcmp(*argv, "-CRL") == 0) {
  1109. if (--argc < 1)
  1110. goto bad;
  1111. crl_file = *(++argv);
  1112. } else if (strcmp(*argv, "-crl_download") == 0)
  1113. crl_download = 1;
  1114. #ifndef OPENSSL_NO_TLSEXT
  1115. else if (strcmp(*argv, "-serverinfo") == 0) {
  1116. if (--argc < 1)
  1117. goto bad;
  1118. s_serverinfo_file = *(++argv);
  1119. }
  1120. #endif
  1121. else if (strcmp(*argv, "-certform") == 0) {
  1122. if (--argc < 1)
  1123. goto bad;
  1124. s_cert_format = str2fmt(*(++argv));
  1125. } else if (strcmp(*argv, "-key") == 0) {
  1126. if (--argc < 1)
  1127. goto bad;
  1128. s_key_file = *(++argv);
  1129. } else if (strcmp(*argv, "-keyform") == 0) {
  1130. if (--argc < 1)
  1131. goto bad;
  1132. s_key_format = str2fmt(*(++argv));
  1133. } else if (strcmp(*argv, "-pass") == 0) {
  1134. if (--argc < 1)
  1135. goto bad;
  1136. passarg = *(++argv);
  1137. } else if (strcmp(*argv, "-cert_chain") == 0) {
  1138. if (--argc < 1)
  1139. goto bad;
  1140. s_chain_file = *(++argv);
  1141. } else if (strcmp(*argv, "-dhparam") == 0) {
  1142. if (--argc < 1)
  1143. goto bad;
  1144. dhfile = *(++argv);
  1145. } else if (strcmp(*argv, "-dcertform") == 0) {
  1146. if (--argc < 1)
  1147. goto bad;
  1148. s_dcert_format = str2fmt(*(++argv));
  1149. } else if (strcmp(*argv, "-dcert") == 0) {
  1150. if (--argc < 1)
  1151. goto bad;
  1152. s_dcert_file = *(++argv);
  1153. } else if (strcmp(*argv, "-dkeyform") == 0) {
  1154. if (--argc < 1)
  1155. goto bad;
  1156. s_dkey_format = str2fmt(*(++argv));
  1157. } else if (strcmp(*argv, "-dpass") == 0) {
  1158. if (--argc < 1)
  1159. goto bad;
  1160. dpassarg = *(++argv);
  1161. } else if (strcmp(*argv, "-dkey") == 0) {
  1162. if (--argc < 1)
  1163. goto bad;
  1164. s_dkey_file = *(++argv);
  1165. } else if (strcmp(*argv, "-dcert_chain") == 0) {
  1166. if (--argc < 1)
  1167. goto bad;
  1168. s_dchain_file = *(++argv);
  1169. } else if (strcmp(*argv, "-nocert") == 0) {
  1170. nocert = 1;
  1171. } else if (strcmp(*argv, "-CApath") == 0) {
  1172. if (--argc < 1)
  1173. goto bad;
  1174. CApath = *(++argv);
  1175. } else if (strcmp(*argv, "-chainCApath") == 0) {
  1176. if (--argc < 1)
  1177. goto bad;
  1178. chCApath = *(++argv);
  1179. } else if (strcmp(*argv, "-verifyCApath") == 0) {
  1180. if (--argc < 1)
  1181. goto bad;
  1182. vfyCApath = *(++argv);
  1183. } else if (strcmp(*argv, "-no_cache") == 0)
  1184. no_cache = 1;
  1185. else if (strcmp(*argv, "-ext_cache") == 0)
  1186. ext_cache = 1;
  1187. else if (strcmp(*argv, "-CRLform") == 0) {
  1188. if (--argc < 1)
  1189. goto bad;
  1190. crl_format = str2fmt(*(++argv));
  1191. } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
  1192. if (badarg)
  1193. goto bad;
  1194. continue;
  1195. } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) {
  1196. if (badarg)
  1197. goto bad;
  1198. continue;
  1199. } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args,
  1200. &no_prot_opt)) {
  1201. if (badarg)
  1202. goto bad;
  1203. continue;
  1204. } else if (strcmp(*argv, "-verify_return_error") == 0)
  1205. verify_return_error = 1;
  1206. else if (strcmp(*argv, "-verify_quiet") == 0)
  1207. verify_quiet = 1;
  1208. else if (strcmp(*argv, "-build_chain") == 0)
  1209. build_chain = 1;
  1210. else if (strcmp(*argv, "-CAfile") == 0) {
  1211. if (--argc < 1)
  1212. goto bad;
  1213. CAfile = *(++argv);
  1214. } else if (strcmp(*argv, "-chainCAfile") == 0) {
  1215. if (--argc < 1)
  1216. goto bad;
  1217. chCAfile = *(++argv);
  1218. } else if (strcmp(*argv, "-verifyCAfile") == 0) {
  1219. if (--argc < 1)
  1220. goto bad;
  1221. vfyCAfile = *(++argv);
  1222. }
  1223. #ifdef FIONBIO
  1224. else if (strcmp(*argv, "-nbio") == 0) {
  1225. s_nbio = 1;
  1226. }
  1227. #endif
  1228. else if (strcmp(*argv, "-nbio_test") == 0) {
  1229. #ifdef FIONBIO
  1230. s_nbio = 1;
  1231. #endif
  1232. s_nbio_test = 1;
  1233. } else if (strcmp(*argv, "-ign_eof") == 0)
  1234. s_ign_eof = 1;
  1235. else if (strcmp(*argv, "-no_ign_eof") == 0)
  1236. s_ign_eof = 0;
  1237. else if (strcmp(*argv, "-debug") == 0) {
  1238. s_debug = 1;
  1239. }
  1240. #ifndef OPENSSL_NO_TLSEXT
  1241. else if (strcmp(*argv, "-tlsextdebug") == 0)
  1242. s_tlsextdebug = 1;
  1243. else if (strcmp(*argv, "-status") == 0)
  1244. s_tlsextstatus = 1;
  1245. else if (strcmp(*argv, "-status_verbose") == 0) {
  1246. s_tlsextstatus = 1;
  1247. tlscstatp.verbose = 1;
  1248. } else if (!strcmp(*argv, "-status_timeout")) {
  1249. s_tlsextstatus = 1;
  1250. if (--argc < 1)
  1251. goto bad;
  1252. tlscstatp.timeout = atoi(*(++argv));
  1253. } else if (!strcmp(*argv, "-status_url")) {
  1254. s_tlsextstatus = 1;
  1255. if (--argc < 1)
  1256. goto bad;
  1257. if (!OCSP_parse_url(*(++argv),
  1258. &tlscstatp.host,
  1259. &tlscstatp.port,
  1260. &tlscstatp.path, &tlscstatp.use_ssl)) {
  1261. BIO_printf(bio_err, "Error parsing URL\n");
  1262. goto bad;
  1263. }
  1264. }
  1265. #endif
  1266. else if (strcmp(*argv, "-msg") == 0) {
  1267. s_msg = 1;
  1268. } else if (strcmp(*argv, "-msgfile") == 0) {
  1269. if (--argc < 1)
  1270. goto bad;
  1271. bio_s_msg = BIO_new_file(*(++argv), "w");
  1272. }
  1273. #ifndef OPENSSL_NO_SSL_TRACE
  1274. else if (strcmp(*argv, "-trace") == 0) {
  1275. s_msg = 2;
  1276. }
  1277. #endif
  1278. else if (strcmp(*argv, "-hack") == 0) {
  1279. hack = 1;
  1280. } else if (strcmp(*argv, "-state") == 0) {
  1281. state = 1;
  1282. } else if (strcmp(*argv, "-crlf") == 0) {
  1283. s_crlf = 1;
  1284. } else if (strcmp(*argv, "-quiet") == 0) {
  1285. s_quiet = 1;
  1286. } else if (strcmp(*argv, "-brief") == 0) {
  1287. s_quiet = 1;
  1288. s_brief = 1;
  1289. verify_quiet = 1;
  1290. } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
  1291. no_tmp_rsa = 1;
  1292. } else if (strcmp(*argv, "-no_dhe") == 0) {
  1293. no_dhe = 1;
  1294. } else if (strcmp(*argv, "-no_ecdhe") == 0) {
  1295. no_ecdhe = 1;
  1296. } else if (strcmp(*argv, "-no_resume_ephemeral") == 0) {
  1297. no_resume_ephemeral = 1;
  1298. }
  1299. #ifndef OPENSSL_NO_PSK
  1300. else if (strcmp(*argv, "-psk_hint") == 0) {
  1301. if (--argc < 1)
  1302. goto bad;
  1303. psk_identity_hint = *(++argv);
  1304. } else if (strcmp(*argv, "-psk") == 0) {
  1305. size_t i;
  1306. if (--argc < 1)
  1307. goto bad;
  1308. psk_key = *(++argv);
  1309. for (i = 0; i < strlen(psk_key); i++) {
  1310. if (isxdigit((unsigned char)psk_key[i]))
  1311. continue;
  1312. BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
  1313. goto bad;
  1314. }
  1315. }
  1316. #endif
  1317. #ifndef OPENSSL_NO_SRP
  1318. else if (strcmp(*argv, "-srpvfile") == 0) {
  1319. if (--argc < 1)
  1320. goto bad;
  1321. srp_verifier_file = *(++argv);
  1322. meth = TLSv1_server_method();
  1323. } else if (strcmp(*argv, "-srpuserseed") == 0) {
  1324. if (--argc < 1)
  1325. goto bad;
  1326. srpuserseed = *(++argv);
  1327. meth = TLSv1_server_method();
  1328. }
  1329. #endif
  1330. else if (strcmp(*argv, "-rev") == 0) {
  1331. rev = 1;
  1332. } else if (strcmp(*argv, "-www") == 0) {
  1333. www = 1;
  1334. } else if (strcmp(*argv, "-WWW") == 0) {
  1335. www = 2;
  1336. } else if (strcmp(*argv, "-HTTP") == 0) {
  1337. www = 3;
  1338. }
  1339. #ifndef OPENSSL_NO_SSL2
  1340. else if (strcmp(*argv, "-ssl2") == 0) {
  1341. no_ecdhe = 1;
  1342. meth = SSLv2_server_method();
  1343. prot_opt++;
  1344. }
  1345. #endif
  1346. #ifndef OPENSSL_NO_SSL3_METHOD
  1347. else if (strcmp(*argv, "-ssl3") == 0) {
  1348. meth = SSLv3_server_method();
  1349. prot_opt++;
  1350. }
  1351. #endif
  1352. #ifndef OPENSSL_NO_TLS1
  1353. else if (strcmp(*argv, "-tls1") == 0) {
  1354. meth = TLSv1_server_method();
  1355. prot_opt++;
  1356. } else if (strcmp(*argv, "-tls1_1") == 0) {
  1357. meth = TLSv1_1_server_method();
  1358. prot_opt++;
  1359. } else if (strcmp(*argv, "-tls1_2") == 0) {
  1360. meth = TLSv1_2_server_method();
  1361. prot_opt++;
  1362. }
  1363. #endif
  1364. #ifndef OPENSSL_NO_DTLS1
  1365. else if (strcmp(*argv, "-dtls") == 0) {
  1366. meth = DTLS_server_method();
  1367. socket_type = SOCK_DGRAM;
  1368. prot_opt++;
  1369. } else if (strcmp(*argv, "-dtls1") == 0) {
  1370. meth = DTLSv1_server_method();
  1371. socket_type = SOCK_DGRAM;
  1372. prot_opt++;
  1373. } else if (strcmp(*argv, "-dtls1_2") == 0) {
  1374. meth = DTLSv1_2_server_method();
  1375. socket_type = SOCK_DGRAM;
  1376. prot_opt++;
  1377. } else if (strcmp(*argv, "-timeout") == 0)
  1378. enable_timeouts = 1;
  1379. else if (strcmp(*argv, "-mtu") == 0) {
  1380. if (--argc < 1)
  1381. goto bad;
  1382. socket_mtu = atol(*(++argv));
  1383. } else if (strcmp(*argv, "-chain") == 0)
  1384. cert_chain = 1;
  1385. #endif
  1386. else if (strcmp(*argv, "-id_prefix") == 0) {
  1387. if (--argc < 1)
  1388. goto bad;
  1389. session_id_prefix = *(++argv);
  1390. }
  1391. #ifndef OPENSSL_NO_ENGINE
  1392. else if (strcmp(*argv, "-engine") == 0) {
  1393. if (--argc < 1)
  1394. goto bad;
  1395. engine_id = *(++argv);
  1396. }
  1397. #endif
  1398. else if (strcmp(*argv, "-rand") == 0) {
  1399. if (--argc < 1)
  1400. goto bad;
  1401. inrand = *(++argv);
  1402. }
  1403. #ifndef OPENSSL_NO_TLSEXT
  1404. else if (strcmp(*argv, "-servername") == 0) {
  1405. if (--argc < 1)
  1406. goto bad;
  1407. tlsextcbp.servername = *(++argv);
  1408. } else if (strcmp(*argv, "-servername_fatal") == 0) {
  1409. tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
  1410. } else if (strcmp(*argv, "-cert2") == 0) {
  1411. if (--argc < 1)
  1412. goto bad;
  1413. s_cert_file2 = *(++argv);
  1414. } else if (strcmp(*argv, "-key2") == 0) {
  1415. if (--argc < 1)
  1416. goto bad;
  1417. s_key_file2 = *(++argv);
  1418. }
  1419. # ifndef OPENSSL_NO_NEXTPROTONEG
  1420. else if (strcmp(*argv, "-nextprotoneg") == 0) {
  1421. if (--argc < 1)
  1422. goto bad;
  1423. next_proto_neg_in = *(++argv);
  1424. }
  1425. # endif
  1426. else if (strcmp(*argv, "-alpn") == 0) {
  1427. if (--argc < 1)
  1428. goto bad;
  1429. alpn_in = *(++argv);
  1430. }
  1431. #endif
  1432. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1433. else if (strcmp(*argv, "-jpake") == 0) {
  1434. if (--argc < 1)
  1435. goto bad;
  1436. jpake_secret = *(++argv);
  1437. }
  1438. #endif
  1439. #ifndef OPENSSL_NO_SRTP
  1440. else if (strcmp(*argv, "-use_srtp") == 0) {
  1441. if (--argc < 1)
  1442. goto bad;
  1443. srtp_profiles = *(++argv);
  1444. }
  1445. #endif
  1446. else if (strcmp(*argv, "-keymatexport") == 0) {
  1447. if (--argc < 1)
  1448. goto bad;
  1449. keymatexportlabel = *(++argv);
  1450. } else if (strcmp(*argv, "-keymatexportlen") == 0) {
  1451. if (--argc < 1)
  1452. goto bad;
  1453. keymatexportlen = atoi(*(++argv));
  1454. if (keymatexportlen == 0)
  1455. goto bad;
  1456. } else {
  1457. BIO_printf(bio_err, "unknown option %s\n", *argv);
  1458. badop = 1;
  1459. break;
  1460. }
  1461. argc--;
  1462. argv++;
  1463. }
  1464. if (badop) {
  1465. bad:
  1466. sv_usage();
  1467. goto end;
  1468. }
  1469. #ifndef OPENSSL_NO_DTLS1
  1470. if (www && socket_type == SOCK_DGRAM) {
  1471. BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
  1472. goto end;
  1473. }
  1474. #endif
  1475. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1476. if (jpake_secret) {
  1477. if (psk_key) {
  1478. BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
  1479. goto end;
  1480. }
  1481. psk_identity = "JPAKE";
  1482. }
  1483. #endif
  1484. if (prot_opt > 1) {
  1485. BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
  1486. goto end;
  1487. }
  1488. if (prot_opt == 1 && no_prot_opt) {
  1489. BIO_printf(bio_err, "Cannot supply both a protocol flag and "
  1490. "\"-no_<prot>\"\n");
  1491. goto end;
  1492. }
  1493. SSL_load_error_strings();
  1494. OpenSSL_add_ssl_algorithms();
  1495. e = setup_engine(bio_err, engine_id, 1);
  1496. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
  1497. BIO_printf(bio_err, "Error getting password\n");
  1498. goto end;
  1499. }
  1500. if (s_key_file == NULL)
  1501. s_key_file = s_cert_file;
  1502. #ifndef OPENSSL_NO_TLSEXT
  1503. if (s_key_file2 == NULL)
  1504. s_key_file2 = s_cert_file2;
  1505. #endif
  1506. if (!load_excert(&exc, bio_err))
  1507. goto end;
  1508. if (nocert == 0) {
  1509. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  1510. "server certificate private key file");
  1511. if (!s_key) {
  1512. ERR_print_errors(bio_err);
  1513. goto end;
  1514. }
  1515. s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
  1516. NULL, e, "server certificate file");
  1517. if (!s_cert) {
  1518. ERR_print_errors(bio_err);
  1519. goto end;
  1520. }
  1521. if (s_chain_file) {
  1522. s_chain = load_certs(bio_err, s_chain_file, FORMAT_PEM,
  1523. NULL, e, "server certificate chain");
  1524. if (!s_chain)
  1525. goto end;
  1526. }
  1527. #ifndef OPENSSL_NO_TLSEXT
  1528. if (tlsextcbp.servername) {
  1529. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  1530. "second server certificate private key file");
  1531. if (!s_key2) {
  1532. ERR_print_errors(bio_err);
  1533. goto end;
  1534. }
  1535. s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
  1536. NULL, e, "second server certificate file");
  1537. if (!s_cert2) {
  1538. ERR_print_errors(bio_err);
  1539. goto end;
  1540. }
  1541. }
  1542. #endif /* OPENSSL_NO_TLSEXT */
  1543. }
  1544. #if !defined(OPENSSL_NO_TLSEXT)
  1545. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  1546. if (next_proto_neg_in) {
  1547. unsigned short len;
  1548. next_proto.data = next_protos_parse(&len, next_proto_neg_in);
  1549. if (next_proto.data == NULL)
  1550. goto end;
  1551. next_proto.len = len;
  1552. } else {
  1553. next_proto.data = NULL;
  1554. }
  1555. # endif
  1556. alpn_ctx.data = NULL;
  1557. if (alpn_in) {
  1558. unsigned short len;
  1559. alpn_ctx.data = next_protos_parse(&len, alpn_in);
  1560. if (alpn_ctx.data == NULL)
  1561. goto end;
  1562. alpn_ctx.len = len;
  1563. }
  1564. #endif
  1565. if (crl_file) {
  1566. X509_CRL *crl;
  1567. crl = load_crl(crl_file, crl_format);
  1568. if (!crl) {
  1569. BIO_puts(bio_err, "Error loading CRL\n");
  1570. ERR_print_errors(bio_err);
  1571. goto end;
  1572. }
  1573. crls = sk_X509_CRL_new_null();
  1574. if (!crls || !sk_X509_CRL_push(crls, crl)) {
  1575. BIO_puts(bio_err, "Error adding CRL\n");
  1576. ERR_print_errors(bio_err);
  1577. X509_CRL_free(crl);
  1578. goto end;
  1579. }
  1580. }
  1581. if (s_dcert_file) {
  1582. if (s_dkey_file == NULL)
  1583. s_dkey_file = s_dcert_file;
  1584. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  1585. 0, dpass, e, "second certificate private key file");
  1586. if (!s_dkey) {
  1587. ERR_print_errors(bio_err);
  1588. goto end;
  1589. }
  1590. s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
  1591. NULL, e, "second server certificate file");
  1592. if (!s_dcert) {
  1593. ERR_print_errors(bio_err);
  1594. goto end;
  1595. }
  1596. if (s_dchain_file) {
  1597. s_dchain = load_certs(bio_err, s_dchain_file, FORMAT_PEM,
  1598. NULL, e, "second server certificate chain");
  1599. if (!s_dchain)
  1600. goto end;
  1601. }
  1602. }
  1603. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1604. && !RAND_status()) {
  1605. BIO_printf(bio_err,
  1606. "warning, not much extra random data, consider using the -rand option\n");
  1607. }
  1608. if (inrand != NULL)
  1609. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  1610. app_RAND_load_files(inrand));
  1611. if (bio_s_out == NULL) {
  1612. if (s_quiet && !s_debug) {
  1613. bio_s_out = BIO_new(BIO_s_null());
  1614. if (s_msg && !bio_s_msg)
  1615. bio_s_msg = BIO_new_fp(stdout, BIO_NOCLOSE);
  1616. } else {
  1617. if (bio_s_out == NULL)
  1618. bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  1619. }
  1620. }
  1621. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1622. if (nocert)
  1623. #endif
  1624. {
  1625. s_cert_file = NULL;
  1626. s_key_file = NULL;
  1627. s_dcert_file = NULL;
  1628. s_dkey_file = NULL;
  1629. #ifndef OPENSSL_NO_TLSEXT
  1630. s_cert_file2 = NULL;
  1631. s_key_file2 = NULL;
  1632. #endif
  1633. }
  1634. ctx = SSL_CTX_new(meth);
  1635. if (ctx == NULL) {
  1636. ERR_print_errors(bio_err);
  1637. goto end;
  1638. }
  1639. if (session_id_prefix) {
  1640. if (strlen(session_id_prefix) >= 32)
  1641. BIO_printf(bio_err,
  1642. "warning: id_prefix is too long, only one new session will be possible\n");
  1643. else if (strlen(session_id_prefix) >= 16)
  1644. BIO_printf(bio_err,
  1645. "warning: id_prefix is too long if you use SSLv2\n");
  1646. if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
  1647. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1648. ERR_print_errors(bio_err);
  1649. goto end;
  1650. }
  1651. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1652. }
  1653. SSL_CTX_set_quiet_shutdown(ctx, 1);
  1654. if (hack)
  1655. SSL_CTX_set_options(ctx, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1656. if (exc)
  1657. ssl_ctx_set_excert(ctx, exc);
  1658. if (state)
  1659. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1660. if (no_cache)
  1661. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  1662. else if (ext_cache)
  1663. init_session_cache_ctx(ctx);
  1664. else
  1665. SSL_CTX_sess_set_cache_size(ctx, 128);
  1666. #ifndef OPENSSL_NO_SRTP
  1667. if (srtp_profiles != NULL)
  1668. SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
  1669. #endif
  1670. #if 0
  1671. if (cipher == NULL)
  1672. cipher = getenv("SSL_CIPHER");
  1673. #endif
  1674. #if 0
  1675. if (s_cert_file == NULL) {
  1676. BIO_printf(bio_err,
  1677. "You must specify a certificate file for the server to use\n");
  1678. goto end;
  1679. }
  1680. #endif
  1681. if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
  1682. (!SSL_CTX_set_default_verify_paths(ctx))) {
  1683. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1684. ERR_print_errors(bio_err);
  1685. /* goto end; */
  1686. }
  1687. if (vpm)
  1688. SSL_CTX_set1_param(ctx, vpm);
  1689. ssl_ctx_add_crls(ctx, crls, 0);
  1690. if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
  1691. goto end;
  1692. if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
  1693. crls, crl_download)) {
  1694. BIO_printf(bio_err, "Error loading store locations\n");
  1695. ERR_print_errors(bio_err);
  1696. goto end;
  1697. }
  1698. #ifndef OPENSSL_NO_TLSEXT
  1699. if (s_cert2) {
  1700. ctx2 = SSL_CTX_new(meth);
  1701. if (ctx2 == NULL) {
  1702. ERR_print_errors(bio_err);
  1703. goto end;
  1704. }
  1705. }
  1706. if (ctx2) {
  1707. BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
  1708. if (session_id_prefix) {
  1709. if (strlen(session_id_prefix) >= 32)
  1710. BIO_printf(bio_err,
  1711. "warning: id_prefix is too long, only one new session will be possible\n");
  1712. else if (strlen(session_id_prefix) >= 16)
  1713. BIO_printf(bio_err,
  1714. "warning: id_prefix is too long if you use SSLv2\n");
  1715. if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
  1716. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1717. ERR_print_errors(bio_err);
  1718. goto end;
  1719. }
  1720. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1721. }
  1722. SSL_CTX_set_quiet_shutdown(ctx2, 1);
  1723. if (hack)
  1724. SSL_CTX_set_options(ctx2, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1725. if (exc)
  1726. ssl_ctx_set_excert(ctx2, exc);
  1727. if (state)
  1728. SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
  1729. if (no_cache)
  1730. SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
  1731. else if (ext_cache)
  1732. init_session_cache_ctx(ctx2);
  1733. else
  1734. SSL_CTX_sess_set_cache_size(ctx2, 128);
  1735. if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
  1736. (!SSL_CTX_set_default_verify_paths(ctx2))) {
  1737. ERR_print_errors(bio_err);
  1738. }
  1739. if (vpm)
  1740. SSL_CTX_set1_param(ctx2, vpm);
  1741. ssl_ctx_add_crls(ctx2, crls, 0);
  1742. if (!args_ssl_call(ctx2, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
  1743. goto end;
  1744. }
  1745. # ifndef OPENSSL_NO_NEXTPROTONEG
  1746. if (next_proto.data)
  1747. SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
  1748. &next_proto);
  1749. # endif
  1750. if (alpn_ctx.data)
  1751. SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
  1752. #endif
  1753. #ifndef OPENSSL_NO_DH
  1754. if (!no_dhe) {
  1755. DH *dh = NULL;
  1756. if (dhfile)
  1757. dh = load_dh_param(dhfile);
  1758. else if (s_cert_file)
  1759. dh = load_dh_param(s_cert_file);
  1760. if (dh != NULL) {
  1761. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1762. } else {
  1763. BIO_printf(bio_s_out, "Using default temp DH parameters\n");
  1764. dh = get_dh2048();
  1765. if (dh == NULL) {
  1766. ERR_print_errors(bio_err);
  1767. goto end;
  1768. }
  1769. }
  1770. (void)BIO_flush(bio_s_out);
  1771. SSL_CTX_set_tmp_dh(ctx, dh);
  1772. # ifndef OPENSSL_NO_TLSEXT
  1773. if (ctx2) {
  1774. if (!dhfile) {
  1775. DH *dh2 = load_dh_param(s_cert_file2);
  1776. if (dh2 != NULL) {
  1777. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1778. (void)BIO_flush(bio_s_out);
  1779. DH_free(dh);
  1780. dh = dh2;
  1781. }
  1782. }
  1783. SSL_CTX_set_tmp_dh(ctx2, dh);
  1784. }
  1785. # endif
  1786. DH_free(dh);
  1787. }
  1788. #endif
  1789. if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
  1790. goto end;
  1791. #ifndef OPENSSL_NO_TLSEXT
  1792. if (s_serverinfo_file != NULL
  1793. && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
  1794. ERR_print_errors(bio_err);
  1795. goto end;
  1796. }
  1797. #endif
  1798. #ifndef OPENSSL_NO_TLSEXT
  1799. if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
  1800. goto end;
  1801. #endif
  1802. if (s_dcert != NULL) {
  1803. if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
  1804. goto end;
  1805. }
  1806. #ifndef OPENSSL_NO_RSA
  1807. # if 1
  1808. if (!no_tmp_rsa) {
  1809. SSL_CTX_set_tmp_rsa_callback(ctx, tmp_rsa_cb);
  1810. # ifndef OPENSSL_NO_TLSEXT
  1811. if (ctx2)
  1812. SSL_CTX_set_tmp_rsa_callback(ctx2, tmp_rsa_cb);
  1813. # endif
  1814. }
  1815. # else
  1816. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) {
  1817. RSA *rsa;
  1818. BIO_printf(bio_s_out, "Generating temp (512 bit) RSA key...");
  1819. BIO_flush(bio_s_out);
  1820. rsa = RSA_generate_key(512, RSA_F4, NULL);
  1821. if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
  1822. ERR_print_errors(bio_err);
  1823. goto end;
  1824. }
  1825. # ifndef OPENSSL_NO_TLSEXT
  1826. if (ctx2) {
  1827. if (!SSL_CTX_set_tmp_rsa(ctx2, rsa)) {
  1828. ERR_print_errors(bio_err);
  1829. goto end;
  1830. }
  1831. }
  1832. # endif
  1833. RSA_free(rsa);
  1834. BIO_printf(bio_s_out, "\n");
  1835. }
  1836. # endif
  1837. #endif
  1838. #ifndef OPENSSL_NO_PSK
  1839. # ifdef OPENSSL_NO_JPAKE
  1840. if (psk_key != NULL)
  1841. # else
  1842. if (psk_key != NULL || jpake_secret)
  1843. # endif
  1844. {
  1845. if (s_debug)
  1846. BIO_printf(bio_s_out,
  1847. "PSK key given or JPAKE in use, setting server callback\n");
  1848. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1849. }
  1850. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
  1851. BIO_printf(bio_err, "error setting PSK identity hint to context\n");
  1852. ERR_print_errors(bio_err);
  1853. goto end;
  1854. }
  1855. #endif
  1856. SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
  1857. SSL_CTX_set_session_id_context(ctx, (void *)&s_server_session_id_context,
  1858. sizeof(s_server_session_id_context));
  1859. /* Set DTLS cookie generation and verification callbacks */
  1860. SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
  1861. SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
  1862. #ifndef OPENSSL_NO_TLSEXT
  1863. if (ctx2) {
  1864. SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
  1865. SSL_CTX_set_session_id_context(ctx2,
  1866. (void *)&s_server_session_id_context,
  1867. sizeof(s_server_session_id_context));
  1868. tlsextcbp.biodebug = bio_s_out;
  1869. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1870. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1871. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1872. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1873. }
  1874. #endif
  1875. #ifndef OPENSSL_NO_SRP
  1876. if (srp_verifier_file != NULL) {
  1877. srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
  1878. srp_callback_parm.user = NULL;
  1879. srp_callback_parm.login = NULL;
  1880. if ((ret =
  1881. SRP_VBASE_init(srp_callback_parm.vb,
  1882. srp_verifier_file)) != SRP_NO_ERROR) {
  1883. BIO_printf(bio_err,
  1884. "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
  1885. srp_verifier_file, ret);
  1886. goto end;
  1887. }
  1888. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
  1889. SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
  1890. SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
  1891. } else
  1892. #endif
  1893. if (CAfile != NULL) {
  1894. SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
  1895. #ifndef OPENSSL_NO_TLSEXT
  1896. if (ctx2)
  1897. SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
  1898. #endif
  1899. }
  1900. BIO_printf(bio_s_out, "ACCEPT\n");
  1901. (void)BIO_flush(bio_s_out);
  1902. if (rev)
  1903. do_server(port, socket_type, &accept_socket, rev_body, context,
  1904. naccept);
  1905. else if (www)
  1906. do_server(port, socket_type, &accept_socket, www_body, context,
  1907. naccept);
  1908. else
  1909. do_server(port, socket_type, &accept_socket, sv_body, context,
  1910. naccept);
  1911. print_stats(bio_s_out, ctx);
  1912. ret = 0;
  1913. end:
  1914. if (ctx != NULL)
  1915. SSL_CTX_free(ctx);
  1916. if (s_cert)
  1917. X509_free(s_cert);
  1918. if (crls)
  1919. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  1920. if (s_dcert)
  1921. X509_free(s_dcert);
  1922. if (s_key)
  1923. EVP_PKEY_free(s_key);
  1924. if (s_dkey)
  1925. EVP_PKEY_free(s_dkey);
  1926. if (s_chain)
  1927. sk_X509_pop_free(s_chain, X509_free);
  1928. if (s_dchain)
  1929. sk_X509_pop_free(s_dchain, X509_free);
  1930. if (pass)
  1931. OPENSSL_free(pass);
  1932. if (dpass)
  1933. OPENSSL_free(dpass);
  1934. if (vpm)
  1935. X509_VERIFY_PARAM_free(vpm);
  1936. free_sessions();
  1937. #ifndef OPENSSL_NO_TLSEXT
  1938. if (tlscstatp.host)
  1939. OPENSSL_free(tlscstatp.host);
  1940. if (tlscstatp.port)
  1941. OPENSSL_free(tlscstatp.port);
  1942. if (tlscstatp.path)
  1943. OPENSSL_free(tlscstatp.path);
  1944. if (ctx2 != NULL)
  1945. SSL_CTX_free(ctx2);
  1946. if (s_cert2)
  1947. X509_free(s_cert2);
  1948. if (s_key2)
  1949. EVP_PKEY_free(s_key2);
  1950. if (serverinfo_in != NULL)
  1951. BIO_free(serverinfo_in);
  1952. # ifndef OPENSSL_NO_NEXTPROTONEG
  1953. if (next_proto.data)
  1954. OPENSSL_free(next_proto.data);
  1955. # endif
  1956. if (alpn_ctx.data)
  1957. OPENSSL_free(alpn_ctx.data);
  1958. #endif
  1959. ssl_excert_free(exc);
  1960. if (ssl_args)
  1961. sk_OPENSSL_STRING_free(ssl_args);
  1962. if (cctx)
  1963. SSL_CONF_CTX_free(cctx);
  1964. #ifndef OPENSSL_NO_JPAKE
  1965. if (jpake_secret && psk_key)
  1966. OPENSSL_free(psk_key);
  1967. #endif
  1968. release_engine(e);
  1969. if (bio_s_out != NULL) {
  1970. BIO_free(bio_s_out);
  1971. bio_s_out = NULL;
  1972. }
  1973. if (bio_s_msg != NULL) {
  1974. BIO_free(bio_s_msg);
  1975. bio_s_msg = NULL;
  1976. }
  1977. SSL_COMP_free_compression_methods();
  1978. apps_shutdown();
  1979. OPENSSL_EXIT(ret);
  1980. }
  1981. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1982. {
  1983. BIO_printf(bio, "%4ld items in the session cache\n",
  1984. SSL_CTX_sess_number(ssl_ctx));
  1985. BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
  1986. SSL_CTX_sess_connect(ssl_ctx));
  1987. BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
  1988. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1989. BIO_printf(bio, "%4ld client connects that finished\n",
  1990. SSL_CTX_sess_connect_good(ssl_ctx));
  1991. BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
  1992. SSL_CTX_sess_accept(ssl_ctx));
  1993. BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
  1994. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1995. BIO_printf(bio, "%4ld server accepts that finished\n",
  1996. SSL_CTX_sess_accept_good(ssl_ctx));
  1997. BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
  1998. BIO_printf(bio, "%4ld session cache misses\n",
  1999. SSL_CTX_sess_misses(ssl_ctx));
  2000. BIO_printf(bio, "%4ld session cache timeouts\n",
  2001. SSL_CTX_sess_timeouts(ssl_ctx));
  2002. BIO_printf(bio, "%4ld callback cache hits\n",
  2003. SSL_CTX_sess_cb_hits(ssl_ctx));
  2004. BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
  2005. SSL_CTX_sess_cache_full(ssl_ctx),
  2006. SSL_CTX_sess_get_cache_size(ssl_ctx));
  2007. }
  2008. static int sv_body(char *hostname, int s, int stype, unsigned char *context)
  2009. {
  2010. char *buf = NULL;
  2011. fd_set readfds;
  2012. int ret = 1, width;
  2013. int k, i;
  2014. unsigned long l;
  2015. SSL *con = NULL;
  2016. BIO *sbio;
  2017. #ifndef OPENSSL_NO_KRB5
  2018. KSSL_CTX *kctx;
  2019. #endif
  2020. struct timeval timeout;
  2021. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  2022. struct timeval tv;
  2023. #else
  2024. struct timeval *timeoutp;
  2025. #endif
  2026. if ((buf = OPENSSL_malloc(bufsize)) == NULL) {
  2027. BIO_printf(bio_err, "out of memory\n");
  2028. goto err;
  2029. }
  2030. #ifdef FIONBIO
  2031. if (s_nbio) {
  2032. unsigned long sl = 1;
  2033. if (!s_quiet)
  2034. BIO_printf(bio_err, "turning on non blocking io\n");
  2035. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  2036. ERR_print_errors(bio_err);
  2037. }
  2038. #endif
  2039. if (con == NULL) {
  2040. con = SSL_new(ctx);
  2041. #ifndef OPENSSL_NO_TLSEXT
  2042. if (s_tlsextdebug) {
  2043. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2044. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2045. }
  2046. if (s_tlsextstatus) {
  2047. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  2048. tlscstatp.err = bio_err;
  2049. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  2050. }
  2051. #endif
  2052. #ifndef OPENSSL_NO_KRB5
  2053. if ((kctx = kssl_ctx_new()) != NULL) {
  2054. SSL_set0_kssl_ctx(con, kctx);
  2055. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2056. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2057. }
  2058. #endif /* OPENSSL_NO_KRB5 */
  2059. if (context)
  2060. SSL_set_session_id_context(con, context, strlen((char *)context));
  2061. }
  2062. SSL_clear(con);
  2063. #if 0
  2064. # ifdef TLSEXT_TYPE_opaque_prf_input
  2065. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  2066. # endif
  2067. #endif
  2068. if (stype == SOCK_DGRAM) {
  2069. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  2070. if (enable_timeouts) {
  2071. timeout.tv_sec = 0;
  2072. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  2073. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  2074. timeout.tv_sec = 0;
  2075. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  2076. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  2077. }
  2078. if (socket_mtu) {
  2079. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  2080. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  2081. DTLS_get_link_min_mtu(con));
  2082. ret = -1;
  2083. BIO_free(sbio);
  2084. goto err;
  2085. }
  2086. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  2087. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  2088. BIO_printf(bio_err, "Failed to set MTU\n");
  2089. ret = -1;
  2090. BIO_free(sbio);
  2091. goto err;
  2092. }
  2093. } else
  2094. /* want to do MTU discovery */
  2095. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  2096. /* turn on cookie exchange */
  2097. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  2098. } else
  2099. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2100. if (s_nbio_test) {
  2101. BIO *test;
  2102. test = BIO_new(BIO_f_nbio_test());
  2103. sbio = BIO_push(test, sbio);
  2104. }
  2105. #ifndef OPENSSL_NO_JPAKE
  2106. if (jpake_secret)
  2107. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  2108. #endif
  2109. SSL_set_bio(con, sbio, sbio);
  2110. SSL_set_accept_state(con);
  2111. /* SSL_set_fd(con,s); */
  2112. if (s_debug) {
  2113. SSL_set_debug(con, 1);
  2114. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2115. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2116. }
  2117. if (s_msg) {
  2118. #ifndef OPENSSL_NO_SSL_TRACE
  2119. if (s_msg == 2)
  2120. SSL_set_msg_callback(con, SSL_trace);
  2121. else
  2122. #endif
  2123. SSL_set_msg_callback(con, msg_cb);
  2124. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2125. }
  2126. #ifndef OPENSSL_NO_TLSEXT
  2127. if (s_tlsextdebug) {
  2128. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2129. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2130. }
  2131. #endif
  2132. if (fileno_stdin() > s)
  2133. width = fileno_stdin() + 1;
  2134. else
  2135. width = s + 1;
  2136. for (;;) {
  2137. int read_from_terminal;
  2138. int read_from_sslcon;
  2139. read_from_terminal = 0;
  2140. read_from_sslcon = SSL_pending(con);
  2141. if (!read_from_sslcon) {
  2142. FD_ZERO(&readfds);
  2143. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  2144. openssl_fdset(fileno_stdin(), &readfds);
  2145. #endif
  2146. openssl_fdset(s, &readfds);
  2147. /*
  2148. * Note: under VMS with SOCKETSHR the second parameter is
  2149. * currently of type (int *) whereas under other systems it is
  2150. * (void *) if you don't have a cast it will choke the compiler:
  2151. * if you do have a cast then you can either go for (int *) or
  2152. * (void *).
  2153. */
  2154. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  2155. /*
  2156. * Under DOS (non-djgpp) and Windows we can't select on stdin:
  2157. * only on sockets. As a workaround we timeout the select every
  2158. * second and check for any keypress. In a proper Windows
  2159. * application we wouldn't do this because it is inefficient.
  2160. */
  2161. tv.tv_sec = 1;
  2162. tv.tv_usec = 0;
  2163. i = select(width, (void *)&readfds, NULL, NULL, &tv);
  2164. if ((i < 0) || (!i && !_kbhit()))
  2165. continue;
  2166. if (_kbhit())
  2167. read_from_terminal = 1;
  2168. #elif defined(OPENSSL_SYS_BEOS_R5)
  2169. /* Under BeOS-R5 the situation is similar to DOS */
  2170. tv.tv_sec = 1;
  2171. tv.tv_usec = 0;
  2172. (void)fcntl(fileno_stdin(), F_SETFL, O_NONBLOCK);
  2173. i = select(width, (void *)&readfds, NULL, NULL, &tv);
  2174. if ((i < 0) || (!i && read(fileno_stdin(), buf, 0) < 0))
  2175. continue;
  2176. if (read(fileno_stdin(), buf, 0) >= 0)
  2177. read_from_terminal = 1;
  2178. (void)fcntl(fileno_stdin(), F_SETFL, 0);
  2179. #else
  2180. if ((SSL_version(con) == DTLS1_VERSION) &&
  2181. DTLSv1_get_timeout(con, &timeout))
  2182. timeoutp = &timeout;
  2183. else
  2184. timeoutp = NULL;
  2185. i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
  2186. if ((SSL_version(con) == DTLS1_VERSION)
  2187. && DTLSv1_handle_timeout(con) > 0) {
  2188. BIO_printf(bio_err, "TIMEOUT occured\n");
  2189. }
  2190. if (i <= 0)
  2191. continue;
  2192. if (FD_ISSET(fileno_stdin(), &readfds))
  2193. read_from_terminal = 1;
  2194. #endif
  2195. if (FD_ISSET(s, &readfds))
  2196. read_from_sslcon = 1;
  2197. }
  2198. if (read_from_terminal) {
  2199. if (s_crlf) {
  2200. int j, lf_num;
  2201. i = raw_read_stdin(buf, bufsize / 2);
  2202. lf_num = 0;
  2203. /* both loops are skipped when i <= 0 */
  2204. for (j = 0; j < i; j++)
  2205. if (buf[j] == '\n')
  2206. lf_num++;
  2207. for (j = i - 1; j >= 0; j--) {
  2208. buf[j + lf_num] = buf[j];
  2209. if (buf[j] == '\n') {
  2210. lf_num--;
  2211. i++;
  2212. buf[j + lf_num] = '\r';
  2213. }
  2214. }
  2215. assert(lf_num == 0);
  2216. } else
  2217. i = raw_read_stdin(buf, bufsize);
  2218. if (!s_quiet && !s_brief) {
  2219. if ((i <= 0) || (buf[0] == 'Q')) {
  2220. BIO_printf(bio_s_out, "DONE\n");
  2221. SHUTDOWN(s);
  2222. close_accept_socket();
  2223. ret = -11;
  2224. goto err;
  2225. }
  2226. if ((i <= 0) || (buf[0] == 'q')) {
  2227. BIO_printf(bio_s_out, "DONE\n");
  2228. if (SSL_version(con) != DTLS1_VERSION)
  2229. SHUTDOWN(s);
  2230. /*
  2231. * close_accept_socket(); ret= -11;
  2232. */
  2233. goto err;
  2234. }
  2235. #ifndef OPENSSL_NO_HEARTBEATS
  2236. if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2237. BIO_printf(bio_err, "HEARTBEATING\n");
  2238. SSL_heartbeat(con);
  2239. i = 0;
  2240. continue;
  2241. }
  2242. #endif
  2243. if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2244. SSL_renegotiate(con);
  2245. i = SSL_do_handshake(con);
  2246. printf("SSL_do_handshake -> %d\n", i);
  2247. i = 0; /* 13; */
  2248. continue;
  2249. /*
  2250. * strcpy(buf,"server side RE-NEGOTIATE\n");
  2251. */
  2252. }
  2253. if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2254. SSL_set_verify(con,
  2255. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2256. NULL);
  2257. SSL_renegotiate(con);
  2258. i = SSL_do_handshake(con);
  2259. printf("SSL_do_handshake -> %d\n", i);
  2260. i = 0; /* 13; */
  2261. continue;
  2262. /*
  2263. * strcpy(buf,"server side RE-NEGOTIATE asking for client
  2264. * cert\n");
  2265. */
  2266. }
  2267. if (buf[0] == 'P') {
  2268. static const char *str = "Lets print some clear text\n";
  2269. BIO_write(SSL_get_wbio(con), str, strlen(str));
  2270. }
  2271. if (buf[0] == 'S') {
  2272. print_stats(bio_s_out, SSL_get_SSL_CTX(con));
  2273. }
  2274. }
  2275. #ifdef CHARSET_EBCDIC
  2276. ebcdic2ascii(buf, buf, i);
  2277. #endif
  2278. l = k = 0;
  2279. for (;;) {
  2280. /* should do a select for the write */
  2281. #ifdef RENEG
  2282. {
  2283. static count = 0;
  2284. if (++count == 100) {
  2285. count = 0;
  2286. SSL_renegotiate(con);
  2287. }
  2288. }
  2289. #endif
  2290. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2291. #ifndef OPENSSL_NO_SRP
  2292. while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
  2293. BIO_printf(bio_s_out, "LOOKUP renego during write\n");
  2294. SRP_user_pwd_free(srp_callback_parm.user);
  2295. srp_callback_parm.user =
  2296. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2297. srp_callback_parm.login);
  2298. if (srp_callback_parm.user)
  2299. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2300. srp_callback_parm.user->info);
  2301. else
  2302. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2303. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2304. }
  2305. #endif
  2306. switch (SSL_get_error(con, k)) {
  2307. case SSL_ERROR_NONE:
  2308. break;
  2309. case SSL_ERROR_WANT_WRITE:
  2310. case SSL_ERROR_WANT_READ:
  2311. case SSL_ERROR_WANT_X509_LOOKUP:
  2312. BIO_printf(bio_s_out, "Write BLOCK\n");
  2313. break;
  2314. case SSL_ERROR_SYSCALL:
  2315. case SSL_ERROR_SSL:
  2316. BIO_printf(bio_s_out, "ERROR\n");
  2317. ERR_print_errors(bio_err);
  2318. ret = 1;
  2319. goto err;
  2320. /* break; */
  2321. case SSL_ERROR_ZERO_RETURN:
  2322. BIO_printf(bio_s_out, "DONE\n");
  2323. ret = 1;
  2324. goto err;
  2325. }
  2326. if (k > 0) {
  2327. l += k;
  2328. i -= k;
  2329. }
  2330. if (i <= 0)
  2331. break;
  2332. }
  2333. }
  2334. if (read_from_sslcon) {
  2335. if (!SSL_is_init_finished(con)) {
  2336. i = init_ssl_connection(con);
  2337. if (i < 0) {
  2338. ret = 0;
  2339. goto err;
  2340. } else if (i == 0) {
  2341. ret = 1;
  2342. goto err;
  2343. }
  2344. } else {
  2345. again:
  2346. i = SSL_read(con, (char *)buf, bufsize);
  2347. #ifndef OPENSSL_NO_SRP
  2348. while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2349. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2350. SRP_user_pwd_free(srp_callback_parm.user);
  2351. srp_callback_parm.user =
  2352. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2353. srp_callback_parm.login);
  2354. if (srp_callback_parm.user)
  2355. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2356. srp_callback_parm.user->info);
  2357. else
  2358. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2359. i = SSL_read(con, (char *)buf, bufsize);
  2360. }
  2361. #endif
  2362. switch (SSL_get_error(con, i)) {
  2363. case SSL_ERROR_NONE:
  2364. #ifdef CHARSET_EBCDIC
  2365. ascii2ebcdic(buf, buf, i);
  2366. #endif
  2367. raw_write_stdout(buf, (unsigned int)i);
  2368. if (SSL_pending(con))
  2369. goto again;
  2370. break;
  2371. case SSL_ERROR_WANT_WRITE:
  2372. case SSL_ERROR_WANT_READ:
  2373. BIO_printf(bio_s_out, "Read BLOCK\n");
  2374. break;
  2375. case SSL_ERROR_SYSCALL:
  2376. case SSL_ERROR_SSL:
  2377. BIO_printf(bio_s_out, "ERROR\n");
  2378. ERR_print_errors(bio_err);
  2379. ret = 1;
  2380. goto err;
  2381. case SSL_ERROR_ZERO_RETURN:
  2382. BIO_printf(bio_s_out, "DONE\n");
  2383. ret = 1;
  2384. goto err;
  2385. }
  2386. }
  2387. }
  2388. }
  2389. err:
  2390. if (con != NULL) {
  2391. BIO_printf(bio_s_out, "shutting down SSL\n");
  2392. #if 1
  2393. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2394. #else
  2395. SSL_shutdown(con);
  2396. #endif
  2397. SSL_free(con);
  2398. }
  2399. BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
  2400. if (buf != NULL) {
  2401. OPENSSL_cleanse(buf, bufsize);
  2402. OPENSSL_free(buf);
  2403. }
  2404. if (ret >= 0)
  2405. BIO_printf(bio_s_out, "ACCEPT\n");
  2406. return (ret);
  2407. }
  2408. static void close_accept_socket(void)
  2409. {
  2410. BIO_printf(bio_err, "shutdown accept socket\n");
  2411. if (accept_socket >= 0) {
  2412. SHUTDOWN2(accept_socket);
  2413. }
  2414. }
  2415. static int init_ssl_connection(SSL *con)
  2416. {
  2417. int i;
  2418. const char *str;
  2419. X509 *peer;
  2420. long verify_error;
  2421. MS_STATIC char buf[BUFSIZ];
  2422. #ifndef OPENSSL_NO_KRB5
  2423. char *client_princ;
  2424. #endif
  2425. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2426. const unsigned char *next_proto_neg;
  2427. unsigned next_proto_neg_len;
  2428. #endif
  2429. unsigned char *exportedkeymat;
  2430. i = SSL_accept(con);
  2431. #ifdef CERT_CB_TEST_RETRY
  2432. {
  2433. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
  2434. && SSL_state(con) == SSL3_ST_SR_CLNT_HELLO_C) {
  2435. fprintf(stderr,
  2436. "LOOKUP from certificate callback during accept\n");
  2437. i = SSL_accept(con);
  2438. }
  2439. }
  2440. #endif
  2441. #ifndef OPENSSL_NO_SRP
  2442. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2443. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2444. srp_callback_parm.login);
  2445. SRP_user_pwd_free(srp_callback_parm.user);
  2446. srp_callback_parm.user =
  2447. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2448. srp_callback_parm.login);
  2449. if (srp_callback_parm.user)
  2450. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2451. srp_callback_parm.user->info);
  2452. else
  2453. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2454. i = SSL_accept(con);
  2455. }
  2456. #endif
  2457. if (i <= 0) {
  2458. if (BIO_sock_should_retry(i)) {
  2459. BIO_printf(bio_s_out, "DELAY\n");
  2460. return (1);
  2461. }
  2462. BIO_printf(bio_err, "ERROR\n");
  2463. verify_error = SSL_get_verify_result(con);
  2464. if (verify_error != X509_V_OK) {
  2465. BIO_printf(bio_err, "verify error:%s\n",
  2466. X509_verify_cert_error_string(verify_error));
  2467. }
  2468. /* Always print any error messages */
  2469. ERR_print_errors(bio_err);
  2470. return (0);
  2471. }
  2472. if (s_brief)
  2473. print_ssl_summary(bio_err, con);
  2474. PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
  2475. peer = SSL_get_peer_certificate(con);
  2476. if (peer != NULL) {
  2477. BIO_printf(bio_s_out, "Client certificate\n");
  2478. PEM_write_bio_X509(bio_s_out, peer);
  2479. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof(buf));
  2480. BIO_printf(bio_s_out, "subject=%s\n", buf);
  2481. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof(buf));
  2482. BIO_printf(bio_s_out, "issuer=%s\n", buf);
  2483. X509_free(peer);
  2484. }
  2485. if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL)
  2486. BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
  2487. str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2488. ssl_print_sigalgs(bio_s_out, con);
  2489. #ifndef OPENSSL_NO_EC
  2490. ssl_print_point_formats(bio_s_out, con);
  2491. ssl_print_curves(bio_s_out, con, 0);
  2492. #endif
  2493. BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
  2494. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2495. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2496. if (next_proto_neg) {
  2497. BIO_printf(bio_s_out, "NEXTPROTO is ");
  2498. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2499. BIO_printf(bio_s_out, "\n");
  2500. }
  2501. #endif
  2502. #ifndef OPENSSL_NO_SRTP
  2503. {
  2504. SRTP_PROTECTION_PROFILE *srtp_profile
  2505. = SSL_get_selected_srtp_profile(con);
  2506. if (srtp_profile)
  2507. BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
  2508. srtp_profile->name);
  2509. }
  2510. #endif
  2511. if (SSL_cache_hit(con))
  2512. BIO_printf(bio_s_out, "Reused session-id\n");
  2513. if (SSL_ctrl(con, SSL_CTRL_GET_FLAGS, 0, NULL) &
  2514. TLS1_FLAGS_TLS_PADDING_BUG)
  2515. BIO_printf(bio_s_out, "Peer has incorrect TLSv1 block padding\n");
  2516. #ifndef OPENSSL_NO_KRB5
  2517. client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con));
  2518. if (client_princ != NULL) {
  2519. BIO_printf(bio_s_out, "Kerberos peer principal is %s\n",
  2520. client_princ);
  2521. }
  2522. #endif /* OPENSSL_NO_KRB5 */
  2523. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2524. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2525. if (keymatexportlabel != NULL) {
  2526. BIO_printf(bio_s_out, "Keying material exporter:\n");
  2527. BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
  2528. BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
  2529. exportedkeymat = OPENSSL_malloc(keymatexportlen);
  2530. if (exportedkeymat != NULL) {
  2531. if (!SSL_export_keying_material(con, exportedkeymat,
  2532. keymatexportlen,
  2533. keymatexportlabel,
  2534. strlen(keymatexportlabel),
  2535. NULL, 0, 0)) {
  2536. BIO_printf(bio_s_out, " Error\n");
  2537. } else {
  2538. BIO_printf(bio_s_out, " Keying material: ");
  2539. for (i = 0; i < keymatexportlen; i++)
  2540. BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
  2541. BIO_printf(bio_s_out, "\n");
  2542. }
  2543. OPENSSL_free(exportedkeymat);
  2544. }
  2545. }
  2546. return (1);
  2547. }
  2548. #ifndef OPENSSL_NO_DH
  2549. static DH *load_dh_param(const char *dhfile)
  2550. {
  2551. DH *ret = NULL;
  2552. BIO *bio;
  2553. if ((bio = BIO_new_file(dhfile, "r")) == NULL)
  2554. goto err;
  2555. ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2556. err:
  2557. if (bio != NULL)
  2558. BIO_free(bio);
  2559. return (ret);
  2560. }
  2561. #endif
  2562. #ifndef OPENSSL_NO_KRB5
  2563. char *client_princ;
  2564. #endif
  2565. #if 0
  2566. static int load_CA(SSL_CTX *ctx, char *file)
  2567. {
  2568. FILE *in;
  2569. X509 *x = NULL;
  2570. if ((in = fopen(file, "r")) == NULL)
  2571. return (0);
  2572. for (;;) {
  2573. if (PEM_read_X509(in, &x, NULL) == NULL)
  2574. break;
  2575. SSL_CTX_add_client_CA(ctx, x);
  2576. }
  2577. if (x != NULL)
  2578. X509_free(x);
  2579. fclose(in);
  2580. return (1);
  2581. }
  2582. #endif
  2583. static int www_body(char *hostname, int s, int stype, unsigned char *context)
  2584. {
  2585. char *buf = NULL;
  2586. int ret = 1;
  2587. int i, j, k, dot;
  2588. SSL *con;
  2589. const SSL_CIPHER *c;
  2590. BIO *io, *ssl_bio, *sbio;
  2591. #ifndef OPENSSL_NO_KRB5
  2592. KSSL_CTX *kctx;
  2593. #endif
  2594. buf = OPENSSL_malloc(bufsize);
  2595. if (buf == NULL)
  2596. return (0);
  2597. io = BIO_new(BIO_f_buffer());
  2598. ssl_bio = BIO_new(BIO_f_ssl());
  2599. if ((io == NULL) || (ssl_bio == NULL))
  2600. goto err;
  2601. #ifdef FIONBIO
  2602. if (s_nbio) {
  2603. unsigned long sl = 1;
  2604. if (!s_quiet)
  2605. BIO_printf(bio_err, "turning on non blocking io\n");
  2606. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  2607. ERR_print_errors(bio_err);
  2608. }
  2609. #endif
  2610. /* lets make the output buffer a reasonable size */
  2611. if (!BIO_set_write_buffer_size(io, bufsize))
  2612. goto err;
  2613. if ((con = SSL_new(ctx)) == NULL)
  2614. goto err;
  2615. #ifndef OPENSSL_NO_TLSEXT
  2616. if (s_tlsextdebug) {
  2617. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2618. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2619. }
  2620. #endif
  2621. #ifndef OPENSSL_NO_KRB5
  2622. if ((kctx = kssl_ctx_new()) != NULL) {
  2623. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2624. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2625. }
  2626. #endif /* OPENSSL_NO_KRB5 */
  2627. if (context)
  2628. SSL_set_session_id_context(con, context, strlen((char *)context));
  2629. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2630. if (s_nbio_test) {
  2631. BIO *test;
  2632. test = BIO_new(BIO_f_nbio_test());
  2633. sbio = BIO_push(test, sbio);
  2634. }
  2635. SSL_set_bio(con, sbio, sbio);
  2636. SSL_set_accept_state(con);
  2637. /* SSL_set_fd(con,s); */
  2638. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2639. BIO_push(io, ssl_bio);
  2640. #ifdef CHARSET_EBCDIC
  2641. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2642. #endif
  2643. if (s_debug) {
  2644. SSL_set_debug(con, 1);
  2645. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2646. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2647. }
  2648. if (s_msg) {
  2649. #ifndef OPENSSL_NO_SSL_TRACE
  2650. if (s_msg == 2)
  2651. SSL_set_msg_callback(con, SSL_trace);
  2652. else
  2653. #endif
  2654. SSL_set_msg_callback(con, msg_cb);
  2655. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2656. }
  2657. for (;;) {
  2658. if (hack) {
  2659. i = SSL_accept(con);
  2660. #ifndef OPENSSL_NO_SRP
  2661. while (i <= 0
  2662. && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2663. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2664. srp_callback_parm.login);
  2665. SRP_user_pwd_free(srp_callback_parm.user);
  2666. srp_callback_parm.user =
  2667. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2668. srp_callback_parm.login);
  2669. if (srp_callback_parm.user)
  2670. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2671. srp_callback_parm.user->info);
  2672. else
  2673. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2674. i = SSL_accept(con);
  2675. }
  2676. #endif
  2677. switch (SSL_get_error(con, i)) {
  2678. case SSL_ERROR_NONE:
  2679. break;
  2680. case SSL_ERROR_WANT_WRITE:
  2681. case SSL_ERROR_WANT_READ:
  2682. case SSL_ERROR_WANT_X509_LOOKUP:
  2683. continue;
  2684. case SSL_ERROR_SYSCALL:
  2685. case SSL_ERROR_SSL:
  2686. case SSL_ERROR_ZERO_RETURN:
  2687. ret = 1;
  2688. goto err;
  2689. /* break; */
  2690. }
  2691. SSL_renegotiate(con);
  2692. SSL_write(con, NULL, 0);
  2693. }
  2694. i = BIO_gets(io, buf, bufsize - 1);
  2695. if (i < 0) { /* error */
  2696. if (!BIO_should_retry(io)) {
  2697. if (!s_quiet)
  2698. ERR_print_errors(bio_err);
  2699. goto err;
  2700. } else {
  2701. BIO_printf(bio_s_out, "read R BLOCK\n");
  2702. #ifndef OPENSSL_NO_SRP
  2703. if (BIO_should_io_special(io)
  2704. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2705. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2706. SRP_user_pwd_free(srp_callback_parm.user);
  2707. srp_callback_parm.user =
  2708. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2709. srp_callback_parm.login);
  2710. if (srp_callback_parm.user)
  2711. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2712. srp_callback_parm.user->info);
  2713. else
  2714. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2715. continue;
  2716. }
  2717. #endif
  2718. #if defined(OPENSSL_SYS_NETWARE)
  2719. delay(1000);
  2720. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2721. sleep(1);
  2722. #endif
  2723. continue;
  2724. }
  2725. } else if (i == 0) { /* end of input */
  2726. ret = 1;
  2727. goto end;
  2728. }
  2729. /* else we have data */
  2730. if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
  2731. ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
  2732. char *p;
  2733. X509 *peer;
  2734. STACK_OF(SSL_CIPHER) *sk;
  2735. static const char *space = " ";
  2736. BIO_puts(io,
  2737. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2738. BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2739. BIO_puts(io, "<pre>\n");
  2740. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2741. BIO_puts(io, "\n");
  2742. for (i = 0; i < local_argc; i++) {
  2743. BIO_puts(io, local_argv[i]);
  2744. BIO_write(io, " ", 1);
  2745. }
  2746. BIO_puts(io, "\n");
  2747. BIO_printf(io,
  2748. "Secure Renegotiation IS%s supported\n",
  2749. SSL_get_secure_renegotiation_support(con) ?
  2750. "" : " NOT");
  2751. /*
  2752. * The following is evil and should not really be done
  2753. */
  2754. BIO_printf(io, "Ciphers supported in s_server binary\n");
  2755. sk = SSL_get_ciphers(con);
  2756. j = sk_SSL_CIPHER_num(sk);
  2757. for (i = 0; i < j; i++) {
  2758. c = sk_SSL_CIPHER_value(sk, i);
  2759. BIO_printf(io, "%-11s:%-25s",
  2760. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2761. if ((((i + 1) % 2) == 0) && (i + 1 != j))
  2762. BIO_puts(io, "\n");
  2763. }
  2764. BIO_puts(io, "\n");
  2765. p = SSL_get_shared_ciphers(con, buf, bufsize);
  2766. if (p != NULL) {
  2767. BIO_printf(io,
  2768. "---\nCiphers common between both SSL end points:\n");
  2769. j = i = 0;
  2770. while (*p) {
  2771. if (*p == ':') {
  2772. BIO_write(io, space, 26 - j);
  2773. i++;
  2774. j = 0;
  2775. BIO_write(io, ((i % 3) ? " " : "\n"), 1);
  2776. } else {
  2777. BIO_write(io, p, 1);
  2778. j++;
  2779. }
  2780. p++;
  2781. }
  2782. BIO_puts(io, "\n");
  2783. }
  2784. ssl_print_sigalgs(io, con);
  2785. #ifndef OPENSSL_NO_EC
  2786. ssl_print_curves(io, con, 0);
  2787. #endif
  2788. BIO_printf(io, (SSL_cache_hit(con)
  2789. ? "---\nReused, " : "---\nNew, "));
  2790. c = SSL_get_current_cipher(con);
  2791. BIO_printf(io, "%s, Cipher is %s\n",
  2792. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2793. SSL_SESSION_print(io, SSL_get_session(con));
  2794. BIO_printf(io, "---\n");
  2795. print_stats(io, SSL_get_SSL_CTX(con));
  2796. BIO_printf(io, "---\n");
  2797. peer = SSL_get_peer_certificate(con);
  2798. if (peer != NULL) {
  2799. BIO_printf(io, "Client certificate\n");
  2800. X509_print(io, peer);
  2801. PEM_write_bio_X509(io, peer);
  2802. } else
  2803. BIO_puts(io, "no client certificate available\n");
  2804. BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n");
  2805. break;
  2806. } else if ((www == 2 || www == 3)
  2807. && (strncmp("GET /", buf, 5) == 0)) {
  2808. BIO *file;
  2809. char *p, *e;
  2810. static const char *text =
  2811. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2812. /* skip the '/' */
  2813. p = &(buf[5]);
  2814. dot = 1;
  2815. for (e = p; *e != '\0'; e++) {
  2816. if (e[0] == ' ')
  2817. break;
  2818. switch (dot) {
  2819. case 1:
  2820. dot = (e[0] == '.') ? 2 : 0;
  2821. break;
  2822. case 2:
  2823. dot = (e[0] == '.') ? 3 : 0;
  2824. break;
  2825. case 3:
  2826. dot = (e[0] == '/') ? -1 : 0;
  2827. break;
  2828. }
  2829. if (dot == 0)
  2830. dot = (e[0] == '/') ? 1 : 0;
  2831. }
  2832. dot = (dot == 3) || (dot == -1); /* filename contains ".."
  2833. * component */
  2834. if (*e == '\0') {
  2835. BIO_puts(io, text);
  2836. BIO_printf(io, "'%s' is an invalid file name\r\n", p);
  2837. break;
  2838. }
  2839. *e = '\0';
  2840. if (dot) {
  2841. BIO_puts(io, text);
  2842. BIO_printf(io, "'%s' contains '..' reference\r\n", p);
  2843. break;
  2844. }
  2845. if (*p == '/') {
  2846. BIO_puts(io, text);
  2847. BIO_printf(io, "'%s' is an invalid path\r\n", p);
  2848. break;
  2849. }
  2850. #if 0
  2851. /* append if a directory lookup */
  2852. if (e[-1] == '/')
  2853. strcat(p, "index.html");
  2854. #endif
  2855. /* if a directory, do the index thang */
  2856. if (app_isdir(p) > 0) {
  2857. #if 0 /* must check buffer size */
  2858. strcat(p, "/index.html");
  2859. #else
  2860. BIO_puts(io, text);
  2861. BIO_printf(io, "'%s' is a directory\r\n", p);
  2862. break;
  2863. #endif
  2864. }
  2865. if ((file = BIO_new_file(p, "r")) == NULL) {
  2866. BIO_puts(io, text);
  2867. BIO_printf(io, "Error opening '%s'\r\n", p);
  2868. ERR_print_errors(io);
  2869. break;
  2870. }
  2871. if (!s_quiet)
  2872. BIO_printf(bio_err, "FILE:%s\n", p);
  2873. if (www == 2) {
  2874. i = strlen(p);
  2875. if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
  2876. ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
  2877. ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
  2878. BIO_puts(io,
  2879. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2880. else
  2881. BIO_puts(io,
  2882. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2883. }
  2884. /* send the file */
  2885. for (;;) {
  2886. i = BIO_read(file, buf, bufsize);
  2887. if (i <= 0)
  2888. break;
  2889. #ifdef RENEG
  2890. total_bytes += i;
  2891. fprintf(stderr, "%d\n", i);
  2892. if (total_bytes > 3 * 1024) {
  2893. total_bytes = 0;
  2894. fprintf(stderr, "RENEGOTIATE\n");
  2895. SSL_renegotiate(con);
  2896. }
  2897. #endif
  2898. for (j = 0; j < i;) {
  2899. #ifdef RENEG
  2900. {
  2901. static count = 0;
  2902. if (++count == 13) {
  2903. SSL_renegotiate(con);
  2904. }
  2905. }
  2906. #endif
  2907. k = BIO_write(io, &(buf[j]), i - j);
  2908. if (k <= 0) {
  2909. if (!BIO_should_retry(io))
  2910. goto write_error;
  2911. else {
  2912. BIO_printf(bio_s_out, "rwrite W BLOCK\n");
  2913. }
  2914. } else {
  2915. j += k;
  2916. }
  2917. }
  2918. }
  2919. write_error:
  2920. BIO_free(file);
  2921. break;
  2922. }
  2923. }
  2924. for (;;) {
  2925. i = (int)BIO_flush(io);
  2926. if (i <= 0) {
  2927. if (!BIO_should_retry(io))
  2928. break;
  2929. } else
  2930. break;
  2931. }
  2932. end:
  2933. #if 1
  2934. /* make sure we re-use sessions */
  2935. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2936. #else
  2937. /* This kills performance */
  2938. /*
  2939. * SSL_shutdown(con); A shutdown gets sent in the BIO_free_all(io)
  2940. * procession
  2941. */
  2942. #endif
  2943. err:
  2944. if (ret >= 0)
  2945. BIO_printf(bio_s_out, "ACCEPT\n");
  2946. if (buf != NULL)
  2947. OPENSSL_free(buf);
  2948. if (io != NULL)
  2949. BIO_free_all(io);
  2950. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2951. return (ret);
  2952. }
  2953. static int rev_body(char *hostname, int s, int stype, unsigned char *context)
  2954. {
  2955. char *buf = NULL;
  2956. int i;
  2957. int ret = 1;
  2958. SSL *con;
  2959. BIO *io, *ssl_bio, *sbio;
  2960. #ifndef OPENSSL_NO_KRB5
  2961. KSSL_CTX *kctx;
  2962. #endif
  2963. buf = OPENSSL_malloc(bufsize);
  2964. if (buf == NULL)
  2965. return (0);
  2966. io = BIO_new(BIO_f_buffer());
  2967. ssl_bio = BIO_new(BIO_f_ssl());
  2968. if ((io == NULL) || (ssl_bio == NULL))
  2969. goto err;
  2970. /* lets make the output buffer a reasonable size */
  2971. if (!BIO_set_write_buffer_size(io, bufsize))
  2972. goto err;
  2973. if ((con = SSL_new(ctx)) == NULL)
  2974. goto err;
  2975. #ifndef OPENSSL_NO_TLSEXT
  2976. if (s_tlsextdebug) {
  2977. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2978. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2979. }
  2980. #endif
  2981. #ifndef OPENSSL_NO_KRB5
  2982. if ((kctx = kssl_ctx_new()) != NULL) {
  2983. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2984. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2985. }
  2986. #endif /* OPENSSL_NO_KRB5 */
  2987. if (context)
  2988. SSL_set_session_id_context(con, context, strlen((char *)context));
  2989. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2990. SSL_set_bio(con, sbio, sbio);
  2991. SSL_set_accept_state(con);
  2992. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2993. BIO_push(io, ssl_bio);
  2994. #ifdef CHARSET_EBCDIC
  2995. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2996. #endif
  2997. if (s_debug) {
  2998. SSL_set_debug(con, 1);
  2999. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  3000. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  3001. }
  3002. if (s_msg) {
  3003. #ifndef OPENSSL_NO_SSL_TRACE
  3004. if (s_msg == 2)
  3005. SSL_set_msg_callback(con, SSL_trace);
  3006. else
  3007. #endif
  3008. SSL_set_msg_callback(con, msg_cb);
  3009. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  3010. }
  3011. for (;;) {
  3012. i = BIO_do_handshake(io);
  3013. if (i > 0)
  3014. break;
  3015. if (!BIO_should_retry(io)) {
  3016. BIO_puts(bio_err, "CONNECTION FAILURE\n");
  3017. ERR_print_errors(bio_err);
  3018. goto end;
  3019. }
  3020. #ifndef OPENSSL_NO_SRP
  3021. if (BIO_should_io_special(io)
  3022. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3023. BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
  3024. SRP_user_pwd_free(srp_callback_parm.user);
  3025. srp_callback_parm.user =
  3026. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3027. srp_callback_parm.login);
  3028. if (srp_callback_parm.user)
  3029. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3030. srp_callback_parm.user->info);
  3031. else
  3032. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3033. continue;
  3034. }
  3035. #endif
  3036. }
  3037. BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
  3038. print_ssl_summary(bio_err, con);
  3039. for (;;) {
  3040. i = BIO_gets(io, buf, bufsize - 1);
  3041. if (i < 0) { /* error */
  3042. if (!BIO_should_retry(io)) {
  3043. if (!s_quiet)
  3044. ERR_print_errors(bio_err);
  3045. goto err;
  3046. } else {
  3047. BIO_printf(bio_s_out, "read R BLOCK\n");
  3048. #ifndef OPENSSL_NO_SRP
  3049. if (BIO_should_io_special(io)
  3050. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3051. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  3052. SRP_user_pwd_free(srp_callback_parm.user);
  3053. srp_callback_parm.user =
  3054. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3055. srp_callback_parm.login);
  3056. if (srp_callback_parm.user)
  3057. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3058. srp_callback_parm.user->info);
  3059. else
  3060. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3061. continue;
  3062. }
  3063. #endif
  3064. #if defined(OPENSSL_SYS_NETWARE)
  3065. delay(1000);
  3066. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  3067. sleep(1);
  3068. #endif
  3069. continue;
  3070. }
  3071. } else if (i == 0) { /* end of input */
  3072. ret = 1;
  3073. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3074. goto end;
  3075. } else {
  3076. char *p = buf + i - 1;
  3077. while (i && (*p == '\n' || *p == '\r')) {
  3078. p--;
  3079. i--;
  3080. }
  3081. if (!s_ign_eof && i == 5 && !strncmp(buf, "CLOSE", 5)) {
  3082. ret = 1;
  3083. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3084. goto end;
  3085. }
  3086. BUF_reverse((unsigned char *)buf, NULL, i);
  3087. buf[i] = '\n';
  3088. BIO_write(io, buf, i + 1);
  3089. for (;;) {
  3090. i = BIO_flush(io);
  3091. if (i > 0)
  3092. break;
  3093. if (!BIO_should_retry(io))
  3094. goto end;
  3095. }
  3096. }
  3097. }
  3098. end:
  3099. /* make sure we re-use sessions */
  3100. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  3101. err:
  3102. if (buf != NULL)
  3103. OPENSSL_free(buf);
  3104. if (io != NULL)
  3105. BIO_free_all(io);
  3106. return (ret);
  3107. }
  3108. #ifndef OPENSSL_NO_RSA
  3109. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  3110. {
  3111. BIGNUM *bn = NULL;
  3112. static RSA *rsa_tmp = NULL;
  3113. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  3114. BIO_printf(bio_err, "Allocation error in generating RSA key\n");
  3115. if (!rsa_tmp && bn) {
  3116. if (!s_quiet) {
  3117. BIO_printf(bio_err, "Generating temp (%d bit) RSA key...",
  3118. keylength);
  3119. (void)BIO_flush(bio_err);
  3120. }
  3121. if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  3122. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
  3123. if (rsa_tmp)
  3124. RSA_free(rsa_tmp);
  3125. rsa_tmp = NULL;
  3126. }
  3127. if (!s_quiet) {
  3128. BIO_printf(bio_err, "\n");
  3129. (void)BIO_flush(bio_err);
  3130. }
  3131. BN_free(bn);
  3132. }
  3133. return (rsa_tmp);
  3134. }
  3135. #endif
  3136. #define MAX_SESSION_ID_ATTEMPTS 10
  3137. static int generate_session_id(const SSL *ssl, unsigned char *id,
  3138. unsigned int *id_len)
  3139. {
  3140. unsigned int count = 0;
  3141. do {
  3142. if (RAND_bytes(id, *id_len) <= 0)
  3143. return 0;
  3144. /*
  3145. * Prefix the session_id with the required prefix. NB: If our prefix
  3146. * is too long, clip it - but there will be worse effects anyway, eg.
  3147. * the server could only possibly create 1 session ID (ie. the
  3148. * prefix!) so all future session negotiations will fail due to
  3149. * conflicts.
  3150. */
  3151. memcpy(id, session_id_prefix,
  3152. (strlen(session_id_prefix) < *id_len) ?
  3153. strlen(session_id_prefix) : *id_len);
  3154. }
  3155. while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  3156. (++count < MAX_SESSION_ID_ATTEMPTS));
  3157. if (count >= MAX_SESSION_ID_ATTEMPTS)
  3158. return 0;
  3159. return 1;
  3160. }
  3161. /*
  3162. * By default s_server uses an in-memory cache which caches SSL_SESSION
  3163. * structures without any serialisation. This hides some bugs which only
  3164. * become apparent in deployed servers. By implementing a basic external
  3165. * session cache some issues can be debugged using s_server.
  3166. */
  3167. typedef struct simple_ssl_session_st {
  3168. unsigned char *id;
  3169. unsigned int idlen;
  3170. unsigned char *der;
  3171. int derlen;
  3172. struct simple_ssl_session_st *next;
  3173. } simple_ssl_session;
  3174. static simple_ssl_session *first = NULL;
  3175. static int add_session(SSL *ssl, SSL_SESSION *session)
  3176. {
  3177. simple_ssl_session *sess;
  3178. unsigned char *p;
  3179. sess = OPENSSL_malloc(sizeof(simple_ssl_session));
  3180. if (!sess) {
  3181. BIO_printf(bio_err, "Out of memory adding session to external cache\n");
  3182. return 0;
  3183. }
  3184. SSL_SESSION_get_id(session, &sess->idlen);
  3185. sess->derlen = i2d_SSL_SESSION(session, NULL);
  3186. sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
  3187. sess->der = OPENSSL_malloc(sess->derlen);
  3188. if (!sess->id || !sess->der) {
  3189. BIO_printf(bio_err, "Out of memory adding session to external cache\n");
  3190. if (sess->id)
  3191. OPENSSL_free(sess->id);
  3192. if (sess->der)
  3193. OPENSSL_free(sess->der);
  3194. OPENSSL_free(sess);
  3195. return 0;
  3196. }
  3197. p = sess->der;
  3198. i2d_SSL_SESSION(session, &p);
  3199. sess->next = first;
  3200. first = sess;
  3201. BIO_printf(bio_err, "New session added to external cache\n");
  3202. return 0;
  3203. }
  3204. static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
  3205. int *do_copy)
  3206. {
  3207. simple_ssl_session *sess;
  3208. *do_copy = 0;
  3209. for (sess = first; sess; sess = sess->next) {
  3210. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
  3211. const unsigned char *p = sess->der;
  3212. BIO_printf(bio_err, "Lookup session: cache hit\n");
  3213. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  3214. }
  3215. }
  3216. BIO_printf(bio_err, "Lookup session: cache miss\n");
  3217. return NULL;
  3218. }
  3219. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  3220. {
  3221. simple_ssl_session *sess, *prev = NULL;
  3222. const unsigned char *id;
  3223. unsigned int idlen;
  3224. id = SSL_SESSION_get_id(session, &idlen);
  3225. for (sess = first; sess; sess = sess->next) {
  3226. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
  3227. if (prev)
  3228. prev->next = sess->next;
  3229. else
  3230. first = sess->next;
  3231. OPENSSL_free(sess->id);
  3232. OPENSSL_free(sess->der);
  3233. OPENSSL_free(sess);
  3234. return;
  3235. }
  3236. prev = sess;
  3237. }
  3238. }
  3239. static void init_session_cache_ctx(SSL_CTX *sctx)
  3240. {
  3241. SSL_CTX_set_session_cache_mode(sctx,
  3242. SSL_SESS_CACHE_NO_INTERNAL |
  3243. SSL_SESS_CACHE_SERVER);
  3244. SSL_CTX_sess_set_new_cb(sctx, add_session);
  3245. SSL_CTX_sess_set_get_cb(sctx, get_session);
  3246. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  3247. }
  3248. static void free_sessions(void)
  3249. {
  3250. simple_ssl_session *sess, *tsess;
  3251. for (sess = first; sess;) {
  3252. OPENSSL_free(sess->id);
  3253. OPENSSL_free(sess->der);
  3254. tsess = sess;
  3255. sess = sess->next;
  3256. OPENSSL_free(tsess);
  3257. }
  3258. first = NULL;
  3259. }