s_server.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE.
  140. */
  141. #include <ctype.h>
  142. #include <stdio.h>
  143. #include <stdlib.h>
  144. #include <string.h>
  145. #include <openssl/async.h>
  146. #include <openssl/e_os2.h>
  147. /* conflicts with winsock2 stuff on netware */
  148. #if !defined(OPENSSL_SYS_NETWARE)
  149. # include <sys/types.h>
  150. #endif
  151. /*
  152. * With IPv6, it looks like Digital has mixed up the proper order of
  153. * recursive header file inclusion, resulting in the compiler complaining
  154. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  155. * needed to have fileno() declared correctly... So let's define u_int
  156. */
  157. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  158. # define __U_INT
  159. typedef unsigned int u_int;
  160. #endif
  161. #include <openssl/lhash.h>
  162. #include <openssl/bn.h>
  163. #define USE_SOCKETS
  164. #include "apps.h"
  165. #include <openssl/err.h>
  166. #include <openssl/pem.h>
  167. #include <openssl/x509.h>
  168. #include <openssl/ssl.h>
  169. #include <openssl/rand.h>
  170. #include <openssl/ocsp.h>
  171. #ifndef OPENSSL_NO_DH
  172. # include <openssl/dh.h>
  173. #endif
  174. #ifndef OPENSSL_NO_RSA
  175. # include <openssl/rsa.h>
  176. #endif
  177. #ifndef OPENSSL_NO_SRP
  178. # include <openssl/srp.h>
  179. #endif
  180. #include "s_apps.h"
  181. #include "timeouts.h"
  182. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  183. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  184. # undef FIONBIO
  185. #endif
  186. static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
  187. static int sv_body(const char *hostname, int s, int stype,
  188. unsigned char *context);
  189. static int www_body(const char *hostname, int s, int stype,
  190. unsigned char *context);
  191. static int rev_body(const char *hostname, int s, int stype,
  192. unsigned char *context);
  193. static void close_accept_socket(void);
  194. static int init_ssl_connection(SSL *s);
  195. static void print_stats(BIO *bp, SSL_CTX *ctx);
  196. static int generate_session_id(const SSL *ssl, unsigned char *id,
  197. unsigned int *id_len);
  198. static void init_session_cache_ctx(SSL_CTX *sctx);
  199. static void free_sessions(void);
  200. #ifndef OPENSSL_NO_DH
  201. static DH *load_dh_param(const char *dhfile);
  202. #endif
  203. static void s_server_init(void);
  204. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  205. #undef BUFSIZZ
  206. #define BUFSIZZ 16*1024
  207. static int bufsize = BUFSIZZ;
  208. static int accept_socket = -1;
  209. #define TEST_CERT "server.pem"
  210. #define TEST_CERT2 "server2.pem"
  211. extern int verify_depth, verify_return_error, verify_quiet;
  212. static int s_server_verify = SSL_VERIFY_NONE;
  213. static int s_server_session_id_context = 1; /* anything will do */
  214. static const char *s_cert_file = TEST_CERT, *s_key_file =
  215. NULL, *s_chain_file = NULL;
  216. static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
  217. static char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
  218. #ifdef FIONBIO
  219. static int s_nbio = 0;
  220. #endif
  221. static int s_nbio_test = 0;
  222. static int s_crlf = 0;
  223. static SSL_CTX *ctx = NULL;
  224. static SSL_CTX *ctx2 = NULL;
  225. static int www = 0;
  226. static BIO *bio_s_out = NULL;
  227. static BIO *bio_s_msg = NULL;
  228. static int s_debug = 0;
  229. static int s_tlsextdebug = 0;
  230. static int s_tlsextstatus = 0;
  231. static int cert_status_cb(SSL *s, void *arg);
  232. static int no_resume_ephemeral = 0;
  233. static int s_msg = 0;
  234. static int s_quiet = 0;
  235. static int s_ign_eof = 0;
  236. static int s_brief = 0;
  237. static char *keymatexportlabel = NULL;
  238. static int keymatexportlen = 20;
  239. static int async = 0;
  240. #ifndef OPENSSL_NO_ENGINE
  241. static char *engine_id = NULL;
  242. #endif
  243. static const char *session_id_prefix = NULL;
  244. #ifndef OPENSSL_NO_DTLS
  245. static int enable_timeouts = 0;
  246. static long socket_mtu;
  247. static int cert_chain = 0;
  248. #endif
  249. static int dtlslisten = 0;
  250. static BIO *serverinfo_in = NULL;
  251. static const char *s_serverinfo_file = NULL;
  252. #ifndef OPENSSL_NO_PSK
  253. static char *psk_identity = "Client_identity";
  254. char *psk_key = NULL; /* by default PSK is not used */
  255. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  256. unsigned char *psk,
  257. unsigned int max_psk_len)
  258. {
  259. unsigned int psk_len = 0;
  260. int ret;
  261. BIGNUM *bn = NULL;
  262. if (s_debug)
  263. BIO_printf(bio_s_out, "psk_server_cb\n");
  264. if (!identity) {
  265. BIO_printf(bio_err, "Error: client did not send PSK identity\n");
  266. goto out_err;
  267. }
  268. if (s_debug)
  269. BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
  270. (int)strlen(identity), identity);
  271. /* here we could lookup the given identity e.g. from a database */
  272. if (strcmp(identity, psk_identity) != 0) {
  273. BIO_printf(bio_s_out, "PSK error: client identity not found"
  274. " (got '%s' expected '%s')\n", identity, psk_identity);
  275. goto out_err;
  276. }
  277. if (s_debug)
  278. BIO_printf(bio_s_out, "PSK client identity found\n");
  279. /* convert the PSK key to binary */
  280. ret = BN_hex2bn(&bn, psk_key);
  281. if (!ret) {
  282. BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
  283. psk_key);
  284. BN_free(bn);
  285. return 0;
  286. }
  287. if (BN_num_bytes(bn) > (int)max_psk_len) {
  288. BIO_printf(bio_err,
  289. "psk buffer of callback is too small (%d) for key (%d)\n",
  290. max_psk_len, BN_num_bytes(bn));
  291. BN_free(bn);
  292. return 0;
  293. }
  294. ret = BN_bn2bin(bn, psk);
  295. BN_free(bn);
  296. if (ret < 0)
  297. goto out_err;
  298. psk_len = (unsigned int)ret;
  299. if (s_debug)
  300. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  301. return psk_len;
  302. out_err:
  303. if (s_debug)
  304. BIO_printf(bio_err, "Error in PSK server callback\n");
  305. (void)BIO_flush(bio_err);
  306. (void)BIO_flush(bio_s_out);
  307. return 0;
  308. }
  309. #endif
  310. #ifndef OPENSSL_NO_SRP
  311. /* This is a context that we pass to callbacks */
  312. typedef struct srpsrvparm_st {
  313. char *login;
  314. SRP_VBASE *vb;
  315. SRP_user_pwd *user;
  316. } srpsrvparm;
  317. /*
  318. * This callback pretends to require some asynchronous logic in order to
  319. * obtain a verifier. When the callback is called for a new connection we
  320. * return with a negative value. This will provoke the accept etc to return
  321. * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
  322. * (which would normally occur after a worker has finished) and we set the
  323. * user parameters.
  324. */
  325. static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  326. {
  327. srpsrvparm *p = (srpsrvparm *) arg;
  328. if (p->login == NULL && p->user == NULL) {
  329. p->login = SSL_get_srp_username(s);
  330. BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
  331. return (-1);
  332. }
  333. if (p->user == NULL) {
  334. BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
  335. return SSL3_AL_FATAL;
  336. }
  337. if (SSL_set_srp_server_param
  338. (s, p->user->N, p->user->g, p->user->s, p->user->v,
  339. p->user->info) < 0) {
  340. *ad = SSL_AD_INTERNAL_ERROR;
  341. return SSL3_AL_FATAL;
  342. }
  343. BIO_printf(bio_err,
  344. "SRP parameters set: username = \"%s\" info=\"%s\" \n",
  345. p->login, p->user->info);
  346. /* need to check whether there are memory leaks */
  347. p->user = NULL;
  348. p->login = NULL;
  349. return SSL_ERROR_NONE;
  350. }
  351. #endif
  352. static void s_server_init(void)
  353. {
  354. accept_socket = -1;
  355. verify_depth = 0;
  356. s_server_verify = SSL_VERIFY_NONE;
  357. s_dcert_file = NULL;
  358. s_dkey_file = NULL;
  359. s_dchain_file = NULL;
  360. s_cert_file = TEST_CERT;
  361. s_key_file = NULL;
  362. s_chain_file = NULL;
  363. s_cert_file2 = TEST_CERT2;
  364. s_key_file2 = NULL;
  365. ctx2 = NULL;
  366. s_nbio = 0;
  367. s_nbio_test = 0;
  368. ctx = NULL;
  369. www = 0;
  370. bio_s_out = NULL;
  371. s_debug = 0;
  372. s_msg = 0;
  373. s_quiet = 0;
  374. s_brief = 0;
  375. async = 0;
  376. #ifndef OPENSSL_NO_ENGINE
  377. engine_id = NULL;
  378. #endif
  379. }
  380. static int local_argc = 0;
  381. static char **local_argv;
  382. #ifdef CHARSET_EBCDIC
  383. static int ebcdic_new(BIO *bi);
  384. static int ebcdic_free(BIO *a);
  385. static int ebcdic_read(BIO *b, char *out, int outl);
  386. static int ebcdic_write(BIO *b, const char *in, int inl);
  387. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  388. static int ebcdic_gets(BIO *bp, char *buf, int size);
  389. static int ebcdic_puts(BIO *bp, const char *str);
  390. # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  391. static BIO_METHOD methods_ebcdic = {
  392. BIO_TYPE_EBCDIC_FILTER,
  393. "EBCDIC/ASCII filter",
  394. ebcdic_write,
  395. ebcdic_read,
  396. ebcdic_puts,
  397. ebcdic_gets,
  398. ebcdic_ctrl,
  399. ebcdic_new,
  400. ebcdic_free,
  401. };
  402. /* This struct is "unwarranted chumminess with the compiler." */
  403. typedef struct {
  404. size_t alloced;
  405. char buff[1];
  406. } EBCDIC_OUTBUFF;
  407. BIO_METHOD *BIO_f_ebcdic_filter()
  408. {
  409. return (&methods_ebcdic);
  410. }
  411. static int ebcdic_new(BIO *bi)
  412. {
  413. EBCDIC_OUTBUFF *wbuf;
  414. wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf");
  415. wbuf->alloced = 1024;
  416. wbuf->buff[0] = '\0';
  417. bi->ptr = (char *)wbuf;
  418. bi->init = 1;
  419. bi->flags = 0;
  420. return (1);
  421. }
  422. static int ebcdic_free(BIO *a)
  423. {
  424. if (a == NULL)
  425. return (0);
  426. OPENSSL_free(a->ptr);
  427. a->ptr = NULL;
  428. a->init = 0;
  429. a->flags = 0;
  430. return (1);
  431. }
  432. static int ebcdic_read(BIO *b, char *out, int outl)
  433. {
  434. int ret = 0;
  435. if (out == NULL || outl == 0)
  436. return (0);
  437. if (b->next_bio == NULL)
  438. return (0);
  439. ret = BIO_read(b->next_bio, out, outl);
  440. if (ret > 0)
  441. ascii2ebcdic(out, out, ret);
  442. return (ret);
  443. }
  444. static int ebcdic_write(BIO *b, const char *in, int inl)
  445. {
  446. EBCDIC_OUTBUFF *wbuf;
  447. int ret = 0;
  448. int num;
  449. unsigned char n;
  450. if ((in == NULL) || (inl <= 0))
  451. return (0);
  452. if (b->next_bio == NULL)
  453. return (0);
  454. wbuf = (EBCDIC_OUTBUFF *) b->ptr;
  455. if (inl > (num = wbuf->alloced)) {
  456. num = num + num; /* double the size */
  457. if (num < inl)
  458. num = inl;
  459. wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf");
  460. OPENSSL_free(b->ptr);
  461. wbuf->alloced = num;
  462. wbuf->buff[0] = '\0';
  463. b->ptr = (char *)wbuf;
  464. }
  465. ebcdic2ascii(wbuf->buff, in, inl);
  466. ret = BIO_write(b->next_bio, wbuf->buff, inl);
  467. return (ret);
  468. }
  469. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  470. {
  471. long ret;
  472. if (b->next_bio == NULL)
  473. return (0);
  474. switch (cmd) {
  475. case BIO_CTRL_DUP:
  476. ret = 0L;
  477. break;
  478. default:
  479. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  480. break;
  481. }
  482. return (ret);
  483. }
  484. static int ebcdic_gets(BIO *bp, char *buf, int size)
  485. {
  486. int i, ret = 0;
  487. if (bp->next_bio == NULL)
  488. return (0);
  489. /* return(BIO_gets(bp->next_bio,buf,size));*/
  490. for (i = 0; i < size - 1; ++i) {
  491. ret = ebcdic_read(bp, &buf[i], 1);
  492. if (ret <= 0)
  493. break;
  494. else if (buf[i] == '\n') {
  495. ++i;
  496. break;
  497. }
  498. }
  499. if (i < size)
  500. buf[i] = '\0';
  501. return (ret < 0 && i == 0) ? ret : i;
  502. }
  503. static int ebcdic_puts(BIO *bp, const char *str)
  504. {
  505. if (bp->next_bio == NULL)
  506. return (0);
  507. return ebcdic_write(bp, str, strlen(str));
  508. }
  509. #endif
  510. /* This is a context that we pass to callbacks */
  511. typedef struct tlsextctx_st {
  512. char *servername;
  513. BIO *biodebug;
  514. int extension_error;
  515. } tlsextctx;
  516. static int ssl_servername_cb(SSL *s, int *ad, void *arg)
  517. {
  518. tlsextctx *p = (tlsextctx *) arg;
  519. const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  520. if (servername && p->biodebug)
  521. BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n",
  522. servername);
  523. if (!p->servername)
  524. return SSL_TLSEXT_ERR_NOACK;
  525. if (servername) {
  526. if (strcasecmp(servername, p->servername))
  527. return p->extension_error;
  528. if (ctx2) {
  529. BIO_printf(p->biodebug, "Switching server context.\n");
  530. SSL_set_SSL_CTX(s, ctx2);
  531. }
  532. }
  533. return SSL_TLSEXT_ERR_OK;
  534. }
  535. /* Structure passed to cert status callback */
  536. typedef struct tlsextstatusctx_st {
  537. /* Default responder to use */
  538. char *host, *path, *port;
  539. int use_ssl;
  540. int timeout;
  541. int verbose;
  542. } tlsextstatusctx;
  543. static tlsextstatusctx tlscstatp = { NULL, NULL, NULL, 0, -1, 0 };
  544. /*
  545. * Certificate Status callback. This is called when a client includes a
  546. * certificate status request extension. This is a simplified version. It
  547. * examines certificates each time and makes one OCSP responder query for
  548. * each request. A full version would store details such as the OCSP
  549. * certificate IDs and minimise the number of OCSP responses by caching them
  550. * until they were considered "expired".
  551. */
  552. static int cert_status_cb(SSL *s, void *arg)
  553. {
  554. tlsextstatusctx *srctx = arg;
  555. char *host = NULL, *port = NULL, *path = NULL;
  556. int use_ssl;
  557. unsigned char *rspder = NULL;
  558. int rspderlen;
  559. STACK_OF(OPENSSL_STRING) *aia = NULL;
  560. X509 *x = NULL;
  561. X509_STORE_CTX inctx;
  562. X509_OBJECT obj;
  563. OCSP_REQUEST *req = NULL;
  564. OCSP_RESPONSE *resp = NULL;
  565. OCSP_CERTID *id = NULL;
  566. STACK_OF(X509_EXTENSION) *exts;
  567. int ret = SSL_TLSEXT_ERR_NOACK;
  568. int i;
  569. if (srctx->verbose)
  570. BIO_puts(bio_err, "cert_status: callback called\n");
  571. /* Build up OCSP query from server certificate */
  572. x = SSL_get_certificate(s);
  573. aia = X509_get1_ocsp(x);
  574. if (aia) {
  575. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  576. &host, &port, &path, &use_ssl)) {
  577. BIO_puts(bio_err, "cert_status: can't parse AIA URL\n");
  578. goto err;
  579. }
  580. if (srctx->verbose)
  581. BIO_printf(bio_err, "cert_status: AIA URL: %s\n",
  582. sk_OPENSSL_STRING_value(aia, 0));
  583. } else {
  584. if (!srctx->host) {
  585. BIO_puts(bio_err,
  586. "cert_status: no AIA and no default responder URL\n");
  587. goto done;
  588. }
  589. host = srctx->host;
  590. path = srctx->path;
  591. port = srctx->port;
  592. use_ssl = srctx->use_ssl;
  593. }
  594. if (!X509_STORE_CTX_init(&inctx,
  595. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  596. NULL, NULL))
  597. goto err;
  598. if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
  599. X509_get_issuer_name(x), &obj) <= 0) {
  600. BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n");
  601. X509_STORE_CTX_cleanup(&inctx);
  602. goto done;
  603. }
  604. req = OCSP_REQUEST_new();
  605. if (req == NULL)
  606. goto err;
  607. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  608. X509_free(obj.data.x509);
  609. X509_STORE_CTX_cleanup(&inctx);
  610. if (!id)
  611. goto err;
  612. if (!OCSP_request_add0_id(req, id))
  613. goto err;
  614. id = NULL;
  615. /* Add any extensions to the request */
  616. SSL_get_tlsext_status_exts(s, &exts);
  617. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  618. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  619. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  620. goto err;
  621. }
  622. resp = process_responder(req, host, path, port, use_ssl, NULL,
  623. srctx->timeout);
  624. if (!resp) {
  625. BIO_puts(bio_err, "cert_status: error querying responder\n");
  626. goto done;
  627. }
  628. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  629. if (rspderlen <= 0)
  630. goto err;
  631. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  632. if (srctx->verbose) {
  633. BIO_puts(bio_err, "cert_status: ocsp response sent:\n");
  634. OCSP_RESPONSE_print(bio_err, resp, 2);
  635. }
  636. ret = SSL_TLSEXT_ERR_OK;
  637. done:
  638. if (ret != SSL_TLSEXT_ERR_OK)
  639. ERR_print_errors(bio_err);
  640. if (aia) {
  641. OPENSSL_free(host);
  642. OPENSSL_free(path);
  643. OPENSSL_free(port);
  644. X509_email_free(aia);
  645. }
  646. OCSP_CERTID_free(id);
  647. OCSP_REQUEST_free(req);
  648. OCSP_RESPONSE_free(resp);
  649. return ret;
  650. err:
  651. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  652. goto done;
  653. }
  654. #ifndef OPENSSL_NO_NEXTPROTONEG
  655. /* This is the context that we pass to next_proto_cb */
  656. typedef struct tlsextnextprotoctx_st {
  657. unsigned char *data;
  658. unsigned int len;
  659. } tlsextnextprotoctx;
  660. static int next_proto_cb(SSL *s, const unsigned char **data,
  661. unsigned int *len, void *arg)
  662. {
  663. tlsextnextprotoctx *next_proto = arg;
  664. *data = next_proto->data;
  665. *len = next_proto->len;
  666. return SSL_TLSEXT_ERR_OK;
  667. }
  668. #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  669. /* This the context that we pass to alpn_cb */
  670. typedef struct tlsextalpnctx_st {
  671. unsigned char *data;
  672. unsigned short len;
  673. } tlsextalpnctx;
  674. static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
  675. const unsigned char *in, unsigned int inlen, void *arg)
  676. {
  677. tlsextalpnctx *alpn_ctx = arg;
  678. if (!s_quiet) {
  679. /* We can assume that |in| is syntactically valid. */
  680. unsigned i;
  681. BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
  682. for (i = 0; i < inlen;) {
  683. if (i)
  684. BIO_write(bio_s_out, ", ", 2);
  685. BIO_write(bio_s_out, &in[i + 1], in[i]);
  686. i += in[i] + 1;
  687. }
  688. BIO_write(bio_s_out, "\n", 1);
  689. }
  690. if (SSL_select_next_proto
  691. ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
  692. inlen) != OPENSSL_NPN_NEGOTIATED) {
  693. return SSL_TLSEXT_ERR_NOACK;
  694. }
  695. if (!s_quiet) {
  696. BIO_printf(bio_s_out, "ALPN protocols selected: ");
  697. BIO_write(bio_s_out, *out, *outlen);
  698. BIO_write(bio_s_out, "\n", 1);
  699. }
  700. return SSL_TLSEXT_ERR_OK;
  701. }
  702. static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
  703. {
  704. /* disable resumption for sessions with forward secure ciphers */
  705. return is_forward_secure;
  706. }
  707. static char *jpake_secret = NULL;
  708. #ifndef OPENSSL_NO_SRP
  709. static srpsrvparm srp_callback_parm;
  710. #endif
  711. #ifndef OPENSSL_NO_SRTP
  712. static char *srtp_profiles = NULL;
  713. #endif
  714. typedef enum OPTION_choice {
  715. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE,
  716. OPT_4, OPT_6, OPT_ACCEPT, OPT_PORT, OPT_UNIX, OPT_UNLINK, OPT_NACCEPT,
  717. OPT_VERIFY, OPT_UPPER_V_VERIFY, OPT_CONTEXT, OPT_CERT, OPT_CRL,
  718. OPT_CRL_DOWNLOAD, OPT_SERVERINFO, OPT_CERTFORM, OPT_KEY, OPT_KEYFORM,
  719. OPT_PASS, OPT_CERT_CHAIN, OPT_DHPARAM, OPT_DCERTFORM, OPT_DCERT,
  720. OPT_DKEYFORM, OPT_DPASS, OPT_DKEY, OPT_DCERT_CHAIN, OPT_NOCERT,
  721. OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH, OPT_NO_CACHE,
  722. OPT_EXT_CACHE, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
  723. OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE,
  724. OPT_VERIFYCAFILE, OPT_NBIO, OPT_NBIO_TEST, OPT_IGN_EOF, OPT_NO_IGN_EOF,
  725. OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_STATUS_VERBOSE,
  726. OPT_STATUS_TIMEOUT, OPT_STATUS_URL, OPT_MSG, OPT_MSGFILE, OPT_TRACE,
  727. OPT_SECURITY_DEBUG, OPT_SECURITY_DEBUG_VERBOSE, OPT_STATE, OPT_CRLF,
  728. OPT_QUIET, OPT_BRIEF, OPT_NO_DHE,
  729. OPT_NO_RESUME_EPHEMERAL, OPT_PSK_HINT, OPT_PSK, OPT_SRPVFILE,
  730. OPT_SRPUSERSEED, OPT_REV, OPT_WWW, OPT_UPPER_WWW, OPT_HTTP, OPT_ASYNC,
  731. OPT_SSL_CONFIG, OPT_SSL3,
  732. OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
  733. OPT_DTLS1_2, OPT_TIMEOUT, OPT_MTU, OPT_CHAIN, OPT_LISTEN,
  734. OPT_ID_PREFIX, OPT_RAND, OPT_SERVERNAME, OPT_SERVERNAME_FATAL,
  735. OPT_CERT2, OPT_KEY2, OPT_NEXTPROTONEG, OPT_ALPN, OPT_JPAKE,
  736. OPT_SRTP_PROFILES, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN,
  737. OPT_S_ENUM,
  738. OPT_V_ENUM,
  739. OPT_X_ENUM
  740. } OPTION_CHOICE;
  741. OPTIONS s_server_options[] = {
  742. {"help", OPT_HELP, '-', "Display this summary"},
  743. {"port", OPT_PORT, 'p',
  744. "TCP/IP port to listen on for connections (default is " PORT ")"},
  745. {"accept", OPT_ACCEPT, 's',
  746. "TCP/IP optional host and port to accept on (default is " PORT ")"},
  747. #ifdef AF_UNIX
  748. {"unix", OPT_UNIX, 's', "Unix domain socket to accept on"},
  749. #endif
  750. {"4", OPT_4, '-', "Use IPv4 only"},
  751. {"6", OPT_6, '-', "Use IPv6 only"},
  752. #ifdef AF_UNIX
  753. {"unlink", OPT_UNLINK, '-', "For -unix, unlink existing socket first"},
  754. #endif
  755. {"context", OPT_CONTEXT, 's', "Set session ID context"},
  756. {"verify", OPT_VERIFY, 'n', "Turn on peer certificate verification"},
  757. {"Verify", OPT_UPPER_V_VERIFY, 'n',
  758. "Turn on peer certificate verification, must have a cert"},
  759. {"cert", OPT_CERT, '<', "Certificate file to use; default is " TEST_CERT},
  760. {"naccept", OPT_NACCEPT, 'p', "Terminate after pnum connections"},
  761. {"serverinfo", OPT_SERVERINFO, 's',
  762. "PEM serverinfo file for certificate"},
  763. {"certform", OPT_CERTFORM, 'F',
  764. "Certificate format (PEM or DER) PEM default"},
  765. {"key", OPT_KEY, '<',
  766. "Private Key if not in -cert; default is " TEST_CERT},
  767. {"keyform", OPT_KEYFORM, 'f',
  768. "Key format (PEM, DER or ENGINE) PEM default"},
  769. {"pass", OPT_PASS, 's', "Private key file pass phrase source"},
  770. {"dcert", OPT_DCERT, '<',
  771. "Second certificate file to use (usually for DSA)"},
  772. {"dcertform", OPT_DCERTFORM, 'F',
  773. "Second certificate format (PEM or DER) PEM default"},
  774. {"dkey", OPT_DKEY, '<',
  775. "Second private key file to use (usually for DSA)"},
  776. {"dkeyform", OPT_DKEYFORM, 'F',
  777. "Second key format (PEM, DER or ENGINE) PEM default"},
  778. {"dpass", OPT_DPASS, 's', "Second private key file pass phrase source"},
  779. {"nbio_test", OPT_NBIO_TEST, '-', "Test with the non-blocking test bio"},
  780. {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
  781. {"debug", OPT_DEBUG, '-', "Print more output"},
  782. {"msg", OPT_MSG, '-', "Show protocol messages"},
  783. {"msgfile", OPT_MSGFILE, '>',
  784. "File to send output of -msg or -trace, instead of stdout"},
  785. {"state", OPT_STATE, '-', "Print the SSL states"},
  786. {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
  787. {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
  788. {"no-CAfile", OPT_NOCAFILE, '-',
  789. "Do not load the default certificates file"},
  790. {"no-CApath", OPT_NOCAPATH, '-',
  791. "Do not load certificates from the default certificates directory"},
  792. {"nocert", OPT_NOCERT, '-', "Don't use any certificates (Anon-DH)"},
  793. {"quiet", OPT_QUIET, '-', "No server output"},
  794. {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL, '-',
  795. "Disable caching and tickets if ephemeral (EC)DH is used"},
  796. {"www", OPT_WWW, '-', "Respond to a 'GET /' with a status page"},
  797. {"WWW", OPT_UPPER_WWW, '-', "Respond to a 'GET with the file ./path"},
  798. {"servername", OPT_SERVERNAME, 's',
  799. "Servername for HostName TLS extension"},
  800. {"servername_fatal", OPT_SERVERNAME_FATAL, '-',
  801. "mismatch send fatal alert (default warning alert)"},
  802. {"cert2", OPT_CERT2, '<',
  803. "Certificate file to use for servername; default is" TEST_CERT2},
  804. {"key2", OPT_KEY2, '<',
  805. "-Private Key file to use for servername if not in -cert2"},
  806. {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
  807. "Hex dump of all TLS extensions received"},
  808. {"HTTP", OPT_HTTP, '-', "Like -WWW but ./path incluedes HTTP headers"},
  809. {"id_prefix", OPT_ID_PREFIX, 's',
  810. "Generate SSL/TLS session IDs prefixed by arg"},
  811. {"rand", OPT_RAND, 's',
  812. "Load the file(s) into the random number generator"},
  813. {"keymatexport", OPT_KEYMATEXPORT, 's',
  814. "Export keying material using label"},
  815. {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
  816. "Export len bytes of keying material (default 20)"},
  817. {"CRL", OPT_CRL, '<', "CRL file to use"},
  818. {"crl_download", OPT_CRL_DOWNLOAD, '-',
  819. "Download CRL from distribution points"},
  820. {"cert_chain", OPT_CERT_CHAIN, '<',
  821. "certificate chain file in PEM format"},
  822. {"dcert_chain", OPT_DCERT_CHAIN, '<',
  823. "second certificate chain file in PEM format"},
  824. {"chainCApath", OPT_CHAINCAPATH, '/',
  825. "use dir as certificate store path to build CA certificate chain"},
  826. {"verifyCApath", OPT_VERIFYCAPATH, '/',
  827. "use dir as certificate store path to verify CA certificate"},
  828. {"no_cache", OPT_NO_CACHE, '-', "Disable session cache"},
  829. {"ext_cache", OPT_EXT_CACHE, '-',
  830. "Disable internal cache, setup and use external cache"},
  831. {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER) PEM is default" },
  832. {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
  833. "Close connection on verification error"},
  834. {"verify_quiet", OPT_VERIFY_QUIET, '-',
  835. "No verify output except verify errors"},
  836. {"build_chain", OPT_BUILD_CHAIN, '-', "Build certificate chain"},
  837. {"chainCAfile", OPT_CHAINCAFILE, '<',
  838. "CA file for certificate chain (PEM format)"},
  839. {"verifyCAfile", OPT_VERIFYCAFILE, '<',
  840. "CA file for certificate verification (PEM format)"},
  841. {"ign_eof", OPT_IGN_EOF, '-', "ignore input eof (default when -quiet)"},
  842. {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Do not ignore input eof"},
  843. {"status", OPT_STATUS, '-', "Request certificate status from server"},
  844. {"status_verbose", OPT_STATUS_VERBOSE, '-',
  845. "Print more output in certificate status callback"},
  846. {"status_timeout", OPT_STATUS_TIMEOUT, 'n',
  847. "Status request responder timeout"},
  848. {"status_url", OPT_STATUS_URL, 's', "Status request fallback URL"},
  849. #ifndef OPENSSL_NO_SSL_TRACE
  850. {"trace", OPT_TRACE, '-', "trace protocol messages"},
  851. #endif
  852. {"security_debug", OPT_SECURITY_DEBUG, '-',
  853. "Print output from SSL/TLS security framework"},
  854. {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
  855. "Print more output from SSL/TLS security framework"},
  856. {"brief", OPT_BRIEF, '-', \
  857. "Restrict output to brief summary of connection parameters"},
  858. {"rev", OPT_REV, '-',
  859. "act as a simple test server which just sends back with the received text reversed"},
  860. {"async", OPT_ASYNC, '-', "Operate in asynchronous mode"},
  861. {"ssl_config", OPT_SSL_CONFIG, 's', \
  862. "Configure SSL_CTX using the configuration 'val'"},
  863. OPT_S_OPTIONS,
  864. OPT_V_OPTIONS,
  865. OPT_X_OPTIONS,
  866. #ifdef FIONBIO
  867. {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
  868. #endif
  869. #ifndef OPENSSL_NO_PSK
  870. {"psk_hint", OPT_PSK_HINT, 's', "PSK identity hint to use"},
  871. {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
  872. # ifndef OPENSSL_NO_JPAKE
  873. {"jpake", OPT_JPAKE, 's', "JPAKE secret to use"},
  874. # endif
  875. #endif
  876. #ifndef OPENSSL_NO_SRP
  877. {"srpvfile", OPT_SRPVFILE, '<', "The verifier file for SRP"},
  878. {"srpuserseed", OPT_SRPUSERSEED, 's',
  879. "A seed string for a default user salt"},
  880. #endif
  881. #ifndef OPENSSL_NO_SSL3
  882. {"ssl3", OPT_SSL3, '-', "Just talk SSLv3"},
  883. #endif
  884. #ifndef OPENSSL_NO_TLS1
  885. {"tls1", OPT_TLS1, '-', "Just talk TLSv1"},
  886. #endif
  887. #ifndef OPENSSL_NO_TLS1_1
  888. {"tls1_1", OPT_TLS1_1, '-', "Just talk TLSv1.1"},
  889. #endif
  890. #ifndef OPENSSL_NO_TLS1_2
  891. {"tls1_2", OPT_TLS1_2, '-', "just talk TLSv1.2"},
  892. #endif
  893. #ifndef OPENSSL_NO_DTLS
  894. {"dtls", OPT_DTLS, '-', "Use any DTLS version"},
  895. {"timeout", OPT_TIMEOUT, '-', "Enable timeouts"},
  896. {"mtu", OPT_MTU, 'p', "Set link layer MTU"},
  897. {"chain", OPT_CHAIN, '-', "Read a certificate chain"},
  898. {"listen", OPT_LISTEN, '-',
  899. "Listen for a DTLS ClientHello with a cookie and then connect"},
  900. #endif
  901. #ifndef OPENSSL_NO_DTLS1
  902. {"dtls1", OPT_DTLS1, '-', "Just talk DTLSv1"},
  903. #endif
  904. #ifndef OPENSSL_NO_DTLS1_2
  905. {"dtls1_2", OPT_DTLS1_2, '-', "Just talk DTLSv1.2"},
  906. #endif
  907. #ifndef OPENSSL_NO_DH
  908. {"no_dhe", OPT_NO_DHE, '-', "Disable ephemeral DH"},
  909. #endif
  910. #ifndef OPENSSL_NO_NEXTPROTONEG
  911. {"nextprotoneg", OPT_NEXTPROTONEG, 's',
  912. "Set the advertised protocols for the NPN extension (comma-separated list)"},
  913. #endif
  914. #ifndef OPENSSL_NO_SRTP
  915. {"use_srtp", OPT_SRTP_PROFILES, 's',
  916. "Offer SRTP key management with a colon-separated profile list"},
  917. {"alpn", OPT_ALPN, 's',
  918. "Set the advertised protocols for the ALPN extension (comma-separated list)"},
  919. #endif
  920. #ifndef OPENSSL_NO_ENGINE
  921. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  922. #endif
  923. {NULL}
  924. };
  925. int s_server_main(int argc, char *argv[])
  926. {
  927. ENGINE *e = NULL;
  928. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  929. SSL_CONF_CTX *cctx = NULL;
  930. const SSL_METHOD *meth = TLS_server_method();
  931. SSL_EXCERT *exc = NULL;
  932. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  933. STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
  934. STACK_OF(X509_CRL) *crls = NULL;
  935. X509 *s_cert = NULL, *s_dcert = NULL;
  936. X509_VERIFY_PARAM *vpm = NULL;
  937. char *CApath = NULL, *CAfile = NULL, *chCApath = NULL, *chCAfile = NULL;
  938. #ifndef OPENSSL_NO_DH
  939. char *dhfile = NULL;
  940. #endif
  941. char *dpassarg = NULL, *dpass = NULL, *inrand = NULL;
  942. char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;
  943. char *crl_file = NULL, *prog;
  944. #ifndef OPENSSL_NO_PSK
  945. char *p;
  946. #endif
  947. #ifdef AF_UNIX
  948. int unlink_unix_path = 0;
  949. #endif
  950. int (*server_cb) (const char *hostname, int s, int stype,
  951. unsigned char *context);
  952. int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0;
  953. #ifndef OPENSSL_NO_DH
  954. int no_dhe = 0;
  955. #endif
  956. int nocert = 0, ret = 1;
  957. int noCApath = 0, noCAfile = 0;
  958. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  959. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  960. int rev = 0, naccept = -1, sdebug = 0;
  961. int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM;
  962. int state = 0, crl_format = FORMAT_PEM, crl_download = 0;
  963. char *host = NULL;
  964. char *port = BUF_strdup(PORT);
  965. unsigned char *context = NULL;
  966. OPTION_CHOICE o;
  967. EVP_PKEY *s_key2 = NULL;
  968. X509 *s_cert2 = NULL;
  969. tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
  970. const char *ssl_config = NULL;
  971. #ifndef OPENSSL_NO_NEXTPROTONEG
  972. const char *next_proto_neg_in = NULL;
  973. tlsextnextprotoctx next_proto = { NULL, 0 };
  974. #endif
  975. const char *alpn_in = NULL;
  976. tlsextalpnctx alpn_ctx = { NULL, 0 };
  977. #ifndef OPENSSL_NO_PSK
  978. /* by default do not send a PSK identity hint */
  979. static char *psk_identity_hint = NULL;
  980. #endif
  981. #ifndef OPENSSL_NO_SRP
  982. char *srpuserseed = NULL;
  983. char *srp_verifier_file = NULL;
  984. #endif
  985. local_argc = argc;
  986. local_argv = argv;
  987. s_server_init();
  988. cctx = SSL_CONF_CTX_new();
  989. vpm = X509_VERIFY_PARAM_new();
  990. if (cctx == NULL || vpm == NULL)
  991. goto end;
  992. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE);
  993. prog = opt_init(argc, argv, s_server_options);
  994. while ((o = opt_next()) != OPT_EOF) {
  995. switch (o) {
  996. case OPT_EOF:
  997. case OPT_ERR:
  998. opthelp:
  999. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  1000. goto end;
  1001. case OPT_HELP:
  1002. opt_help(s_server_options);
  1003. ret = 0;
  1004. goto end;
  1005. case OPT_4:
  1006. #ifdef AF_UNIX
  1007. if (socket_family == AF_UNIX) {
  1008. OPENSSL_free(host); host = NULL;
  1009. OPENSSL_free(port); port = NULL;
  1010. }
  1011. #endif
  1012. socket_family = AF_INET;
  1013. break;
  1014. case OPT_6:
  1015. if (1) {
  1016. #ifdef AF_INET6
  1017. #ifdef AF_UNIX
  1018. if (socket_family == AF_UNIX) {
  1019. OPENSSL_free(host); host = NULL;
  1020. OPENSSL_free(port); port = NULL;
  1021. }
  1022. #endif
  1023. socket_family = AF_INET6;
  1024. } else {
  1025. #endif
  1026. BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog);
  1027. goto end;
  1028. }
  1029. break;
  1030. case OPT_PORT:
  1031. #ifdef AF_UNIX
  1032. if (socket_family == AF_UNIX) {
  1033. socket_family = AF_UNSPEC;
  1034. }
  1035. #endif
  1036. OPENSSL_free(port); port = NULL;
  1037. OPENSSL_free(host); host = NULL;
  1038. if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) {
  1039. BIO_printf(bio_err,
  1040. "%s: -port argument malformed or ambiguous\n",
  1041. port);
  1042. goto end;
  1043. }
  1044. break;
  1045. case OPT_ACCEPT:
  1046. #ifdef AF_UNIX
  1047. if (socket_family == AF_UNIX) {
  1048. socket_family = AF_UNSPEC;
  1049. }
  1050. #endif
  1051. OPENSSL_free(port); port = NULL;
  1052. OPENSSL_free(host); host = NULL;
  1053. if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) {
  1054. BIO_printf(bio_err,
  1055. "%s: -accept argument malformed or ambiguous\n",
  1056. port);
  1057. goto end;
  1058. }
  1059. break;
  1060. #ifdef AF_UNIX
  1061. case OPT_UNIX:
  1062. socket_family = AF_UNIX;
  1063. OPENSSL_free(host); host = BUF_strdup(opt_arg());
  1064. OPENSSL_free(port); port = NULL;
  1065. break;
  1066. case OPT_UNLINK:
  1067. unlink_unix_path = 1;
  1068. break;
  1069. #endif
  1070. case OPT_NACCEPT:
  1071. naccept = atol(opt_arg());
  1072. break;
  1073. case OPT_VERIFY:
  1074. s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
  1075. verify_depth = atoi(opt_arg());
  1076. if (!s_quiet)
  1077. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  1078. break;
  1079. case OPT_UPPER_V_VERIFY:
  1080. s_server_verify =
  1081. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  1082. SSL_VERIFY_CLIENT_ONCE;
  1083. verify_depth = atoi(opt_arg());
  1084. if (!s_quiet)
  1085. BIO_printf(bio_err,
  1086. "verify depth is %d, must return a certificate\n",
  1087. verify_depth);
  1088. break;
  1089. case OPT_CONTEXT:
  1090. context = (unsigned char *)opt_arg();
  1091. break;
  1092. case OPT_CERT:
  1093. s_cert_file = opt_arg();
  1094. break;
  1095. case OPT_CRL:
  1096. crl_file = opt_arg();
  1097. break;
  1098. case OPT_CRL_DOWNLOAD:
  1099. crl_download = 1;
  1100. break;
  1101. case OPT_SERVERINFO:
  1102. s_serverinfo_file = opt_arg();
  1103. break;
  1104. case OPT_CERTFORM:
  1105. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_cert_format))
  1106. goto opthelp;
  1107. break;
  1108. case OPT_KEY:
  1109. s_key_file = opt_arg();
  1110. break;
  1111. case OPT_KEYFORM:
  1112. if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format))
  1113. goto opthelp;
  1114. break;
  1115. case OPT_PASS:
  1116. passarg = opt_arg();
  1117. break;
  1118. case OPT_CERT_CHAIN:
  1119. s_chain_file = opt_arg();
  1120. break;
  1121. case OPT_DHPARAM:
  1122. #ifndef OPENSSL_NO_DH
  1123. dhfile = opt_arg();
  1124. #endif
  1125. break;
  1126. case OPT_DCERTFORM:
  1127. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dcert_format))
  1128. goto opthelp;
  1129. break;
  1130. case OPT_DCERT:
  1131. s_dcert_file = opt_arg();
  1132. break;
  1133. case OPT_DKEYFORM:
  1134. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dkey_format))
  1135. goto opthelp;
  1136. break;
  1137. case OPT_DPASS:
  1138. dpassarg = opt_arg();
  1139. break;
  1140. case OPT_DKEY:
  1141. s_dkey_file = opt_arg();
  1142. break;
  1143. case OPT_DCERT_CHAIN:
  1144. s_dchain_file = opt_arg();
  1145. break;
  1146. case OPT_NOCERT:
  1147. nocert = 1;
  1148. break;
  1149. case OPT_CAPATH:
  1150. CApath = opt_arg();
  1151. break;
  1152. case OPT_NOCAPATH:
  1153. noCApath = 1;
  1154. break;
  1155. case OPT_CHAINCAPATH:
  1156. chCApath = opt_arg();
  1157. break;
  1158. case OPT_VERIFYCAPATH:
  1159. vfyCApath = opt_arg();
  1160. break;
  1161. case OPT_NO_CACHE:
  1162. no_cache = 1;
  1163. break;
  1164. case OPT_EXT_CACHE:
  1165. ext_cache = 1;
  1166. break;
  1167. case OPT_CRLFORM:
  1168. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
  1169. goto opthelp;
  1170. break;
  1171. case OPT_S_CASES:
  1172. if (ssl_args == NULL)
  1173. ssl_args = sk_OPENSSL_STRING_new_null();
  1174. if (ssl_args == NULL
  1175. || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
  1176. || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
  1177. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  1178. goto end;
  1179. }
  1180. break;
  1181. case OPT_V_CASES:
  1182. if (!opt_verify(o, vpm))
  1183. goto end;
  1184. vpmtouched++;
  1185. break;
  1186. case OPT_X_CASES:
  1187. if (!args_excert(o, &exc))
  1188. goto end;
  1189. break;
  1190. case OPT_VERIFY_RET_ERROR:
  1191. verify_return_error = 1;
  1192. break;
  1193. case OPT_VERIFY_QUIET:
  1194. verify_quiet = 1;
  1195. break;
  1196. case OPT_BUILD_CHAIN:
  1197. build_chain = 1;
  1198. break;
  1199. case OPT_CAFILE:
  1200. CAfile = opt_arg();
  1201. break;
  1202. case OPT_NOCAFILE:
  1203. noCAfile = 1;
  1204. break;
  1205. case OPT_CHAINCAFILE:
  1206. chCAfile = opt_arg();
  1207. break;
  1208. case OPT_VERIFYCAFILE:
  1209. vfyCAfile = opt_arg();
  1210. break;
  1211. case OPT_NBIO:
  1212. s_nbio = 1;
  1213. break;
  1214. case OPT_NBIO_TEST:
  1215. s_nbio = s_nbio_test = 1;
  1216. break;
  1217. case OPT_IGN_EOF:
  1218. s_ign_eof = 1;
  1219. break;
  1220. case OPT_NO_IGN_EOF:
  1221. s_ign_eof = 0;
  1222. break;
  1223. case OPT_DEBUG:
  1224. s_debug = 1;
  1225. break;
  1226. case OPT_TLSEXTDEBUG:
  1227. s_tlsextdebug = 1;
  1228. break;
  1229. case OPT_STATUS:
  1230. s_tlsextstatus = 1;
  1231. break;
  1232. case OPT_STATUS_VERBOSE:
  1233. s_tlsextstatus = tlscstatp.verbose = 1;
  1234. break;
  1235. case OPT_STATUS_TIMEOUT:
  1236. s_tlsextstatus = 1;
  1237. tlscstatp.timeout = atoi(opt_arg());
  1238. break;
  1239. case OPT_STATUS_URL:
  1240. s_tlsextstatus = 1;
  1241. if (!OCSP_parse_url(opt_arg(),
  1242. &tlscstatp.host,
  1243. &tlscstatp.port,
  1244. &tlscstatp.path, &tlscstatp.use_ssl)) {
  1245. BIO_printf(bio_err, "Error parsing URL\n");
  1246. goto end;
  1247. }
  1248. break;
  1249. case OPT_MSG:
  1250. s_msg = 1;
  1251. break;
  1252. case OPT_MSGFILE:
  1253. bio_s_msg = BIO_new_file(opt_arg(), "w");
  1254. break;
  1255. case OPT_TRACE:
  1256. #ifndef OPENSSL_NO_SSL_TRACE
  1257. s_msg = 2;
  1258. #else
  1259. break;
  1260. #endif
  1261. case OPT_SECURITY_DEBUG:
  1262. sdebug = 1;
  1263. break;
  1264. case OPT_SECURITY_DEBUG_VERBOSE:
  1265. sdebug = 2;
  1266. break;
  1267. case OPT_STATE:
  1268. state = 1;
  1269. break;
  1270. case OPT_CRLF:
  1271. s_crlf = 1;
  1272. break;
  1273. case OPT_QUIET:
  1274. s_quiet = 1;
  1275. break;
  1276. case OPT_BRIEF:
  1277. s_quiet = s_brief = verify_quiet = 1;
  1278. break;
  1279. case OPT_NO_DHE:
  1280. #ifndef OPENSSL_NO_DH
  1281. no_dhe = 1;
  1282. #endif
  1283. break;
  1284. case OPT_NO_RESUME_EPHEMERAL:
  1285. no_resume_ephemeral = 1;
  1286. break;
  1287. case OPT_PSK_HINT:
  1288. #ifndef OPENSSL_NO_PSK
  1289. psk_identity_hint = opt_arg();
  1290. #endif
  1291. break;
  1292. case OPT_PSK:
  1293. #ifndef OPENSSL_NO_PSK
  1294. for (p = psk_key = opt_arg(); *p; p++) {
  1295. if (isxdigit(*p))
  1296. continue;
  1297. BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
  1298. goto end;
  1299. }
  1300. #endif
  1301. break;
  1302. case OPT_SRPVFILE:
  1303. #ifndef OPENSSL_NO_SRP
  1304. srp_verifier_file = opt_arg();
  1305. meth = TLSv1_server_method();
  1306. #endif
  1307. break;
  1308. case OPT_SRPUSERSEED:
  1309. #ifndef OPENSSL_NO_SRP
  1310. srpuserseed = opt_arg();
  1311. meth = TLSv1_server_method();
  1312. #endif
  1313. break;
  1314. case OPT_REV:
  1315. rev = 1;
  1316. break;
  1317. case OPT_WWW:
  1318. www = 1;
  1319. break;
  1320. case OPT_UPPER_WWW:
  1321. www = 2;
  1322. break;
  1323. case OPT_HTTP:
  1324. www = 3;
  1325. break;
  1326. case OPT_SSL_CONFIG:
  1327. ssl_config = opt_arg();
  1328. break;
  1329. case OPT_SSL3:
  1330. #ifndef OPENSSL_NO_SSL3
  1331. meth = SSLv3_server_method();
  1332. #endif
  1333. break;
  1334. case OPT_TLS1_2:
  1335. #ifndef OPENSSL_NO_TLS1_2
  1336. meth = TLSv1_2_server_method();
  1337. #endif
  1338. break;
  1339. case OPT_TLS1_1:
  1340. #ifndef OPENSSL_NO_TLS1_1
  1341. meth = TLSv1_1_server_method();
  1342. #endif
  1343. break;
  1344. case OPT_TLS1:
  1345. #ifndef OPENSSL_NO_TLS1
  1346. meth = TLSv1_server_method();
  1347. #endif
  1348. break;
  1349. case OPT_DTLS:
  1350. #ifndef OPENSSL_NO_DTLS
  1351. meth = DTLS_server_method();
  1352. socket_type = SOCK_DGRAM;
  1353. #endif
  1354. break;
  1355. case OPT_DTLS1:
  1356. #ifndef OPENSSL_NO_DTLS1
  1357. meth = DTLSv1_server_method();
  1358. socket_type = SOCK_DGRAM;
  1359. #endif
  1360. break;
  1361. case OPT_DTLS1_2:
  1362. #ifndef OPENSSL_NO_DTLS1_2
  1363. meth = DTLSv1_2_server_method();
  1364. socket_type = SOCK_DGRAM;
  1365. #endif
  1366. break;
  1367. case OPT_TIMEOUT:
  1368. #ifndef OPENSSL_NO_DTLS
  1369. enable_timeouts = 1;
  1370. #endif
  1371. break;
  1372. case OPT_MTU:
  1373. #ifndef OPENSSL_NO_DTLS
  1374. socket_mtu = atol(opt_arg());
  1375. #endif
  1376. break;
  1377. case OPT_CHAIN:
  1378. #ifndef OPENSSL_NO_DTLS
  1379. cert_chain = 1;
  1380. #endif
  1381. break;
  1382. case OPT_LISTEN:
  1383. #ifndef OPENSSL_NO_DTLS
  1384. dtlslisten = 1;
  1385. #endif
  1386. break;
  1387. case OPT_ID_PREFIX:
  1388. session_id_prefix = opt_arg();
  1389. break;
  1390. case OPT_ENGINE:
  1391. e = setup_engine(opt_arg(), 1);
  1392. break;
  1393. case OPT_RAND:
  1394. inrand = opt_arg();
  1395. break;
  1396. case OPT_SERVERNAME:
  1397. tlsextcbp.servername = opt_arg();
  1398. break;
  1399. case OPT_SERVERNAME_FATAL:
  1400. tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
  1401. break;
  1402. case OPT_CERT2:
  1403. s_cert_file2 = opt_arg();
  1404. break;
  1405. case OPT_KEY2:
  1406. s_key_file2 = opt_arg();
  1407. break;
  1408. case OPT_NEXTPROTONEG:
  1409. # ifndef OPENSSL_NO_NEXTPROTONEG
  1410. next_proto_neg_in = opt_arg();
  1411. #endif
  1412. break;
  1413. case OPT_ALPN:
  1414. alpn_in = opt_arg();
  1415. break;
  1416. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1417. case OPT_JPAKE:
  1418. jpake_secret = opt_arg();
  1419. break;
  1420. #else
  1421. case OPT_JPAKE:
  1422. goto opthelp;
  1423. #endif
  1424. case OPT_SRTP_PROFILES:
  1425. srtp_profiles = opt_arg();
  1426. break;
  1427. case OPT_KEYMATEXPORT:
  1428. keymatexportlabel = opt_arg();
  1429. break;
  1430. case OPT_KEYMATEXPORTLEN:
  1431. keymatexportlen = atoi(opt_arg());
  1432. break;
  1433. case OPT_ASYNC:
  1434. async = 1;
  1435. break;
  1436. }
  1437. }
  1438. argc = opt_num_rest();
  1439. argv = opt_rest();
  1440. #ifndef OPENSSL_NO_DTLS
  1441. if (www && socket_type == SOCK_DGRAM) {
  1442. BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
  1443. goto end;
  1444. }
  1445. if (dtlslisten && socket_type != SOCK_DGRAM) {
  1446. BIO_printf(bio_err, "Can only use -listen with DTLS\n");
  1447. goto end;
  1448. }
  1449. #endif
  1450. #ifdef AF_UNIX
  1451. if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
  1452. BIO_printf(bio_err,
  1453. "Can't use unix sockets and datagrams together\n");
  1454. goto end;
  1455. }
  1456. #endif
  1457. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1458. if (jpake_secret) {
  1459. if (psk_key) {
  1460. BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
  1461. goto end;
  1462. }
  1463. psk_identity = "JPAKE";
  1464. }
  1465. #endif
  1466. if (!app_passwd(passarg, dpassarg, &pass, &dpass)) {
  1467. BIO_printf(bio_err, "Error getting password\n");
  1468. goto end;
  1469. }
  1470. if (s_key_file == NULL)
  1471. s_key_file = s_cert_file;
  1472. if (s_key_file2 == NULL)
  1473. s_key_file2 = s_cert_file2;
  1474. if (!load_excert(&exc))
  1475. goto end;
  1476. if (nocert == 0) {
  1477. s_key = load_key(s_key_file, s_key_format, 0, pass, e,
  1478. "server certificate private key file");
  1479. if (!s_key) {
  1480. ERR_print_errors(bio_err);
  1481. goto end;
  1482. }
  1483. s_cert = load_cert(s_cert_file, s_cert_format,
  1484. NULL, e, "server certificate file");
  1485. if (!s_cert) {
  1486. ERR_print_errors(bio_err);
  1487. goto end;
  1488. }
  1489. if (s_chain_file) {
  1490. if (!load_certs(s_chain_file, &s_chain, FORMAT_PEM, NULL, e,
  1491. "server certificate chain"))
  1492. goto end;
  1493. }
  1494. if (tlsextcbp.servername) {
  1495. s_key2 = load_key(s_key_file2, s_key_format, 0, pass, e,
  1496. "second server certificate private key file");
  1497. if (!s_key2) {
  1498. ERR_print_errors(bio_err);
  1499. goto end;
  1500. }
  1501. s_cert2 = load_cert(s_cert_file2, s_cert_format,
  1502. NULL, e, "second server certificate file");
  1503. if (!s_cert2) {
  1504. ERR_print_errors(bio_err);
  1505. goto end;
  1506. }
  1507. }
  1508. }
  1509. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1510. if (next_proto_neg_in) {
  1511. unsigned short len;
  1512. next_proto.data = next_protos_parse(&len, next_proto_neg_in);
  1513. if (next_proto.data == NULL)
  1514. goto end;
  1515. next_proto.len = len;
  1516. } else {
  1517. next_proto.data = NULL;
  1518. }
  1519. #endif
  1520. alpn_ctx.data = NULL;
  1521. if (alpn_in) {
  1522. unsigned short len;
  1523. alpn_ctx.data = next_protos_parse(&len, alpn_in);
  1524. if (alpn_ctx.data == NULL)
  1525. goto end;
  1526. alpn_ctx.len = len;
  1527. }
  1528. if (crl_file) {
  1529. X509_CRL *crl;
  1530. crl = load_crl(crl_file, crl_format);
  1531. if (!crl) {
  1532. BIO_puts(bio_err, "Error loading CRL\n");
  1533. ERR_print_errors(bio_err);
  1534. goto end;
  1535. }
  1536. crls = sk_X509_CRL_new_null();
  1537. if (!crls || !sk_X509_CRL_push(crls, crl)) {
  1538. BIO_puts(bio_err, "Error adding CRL\n");
  1539. ERR_print_errors(bio_err);
  1540. X509_CRL_free(crl);
  1541. goto end;
  1542. }
  1543. }
  1544. if (s_dcert_file) {
  1545. if (s_dkey_file == NULL)
  1546. s_dkey_file = s_dcert_file;
  1547. s_dkey = load_key(s_dkey_file, s_dkey_format,
  1548. 0, dpass, e, "second certificate private key file");
  1549. if (!s_dkey) {
  1550. ERR_print_errors(bio_err);
  1551. goto end;
  1552. }
  1553. s_dcert = load_cert(s_dcert_file, s_dcert_format,
  1554. NULL, e, "second server certificate file");
  1555. if (!s_dcert) {
  1556. ERR_print_errors(bio_err);
  1557. goto end;
  1558. }
  1559. if (s_dchain_file) {
  1560. if (!load_certs(s_dchain_file, &s_dchain, FORMAT_PEM, NULL, e,
  1561. "second server certificate chain"))
  1562. goto end;
  1563. }
  1564. }
  1565. if (!app_RAND_load_file(NULL, 1) && inrand == NULL
  1566. && !RAND_status()) {
  1567. BIO_printf(bio_err,
  1568. "warning, not much extra random data, consider using the -rand option\n");
  1569. }
  1570. if (inrand != NULL)
  1571. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  1572. app_RAND_load_files(inrand));
  1573. if (bio_s_out == NULL) {
  1574. if (s_quiet && !s_debug) {
  1575. bio_s_out = BIO_new(BIO_s_null());
  1576. if (s_msg && !bio_s_msg)
  1577. bio_s_msg = dup_bio_out(FORMAT_TEXT);
  1578. } else {
  1579. if (bio_s_out == NULL)
  1580. bio_s_out = dup_bio_out(FORMAT_TEXT);
  1581. }
  1582. }
  1583. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  1584. if (nocert)
  1585. #endif
  1586. {
  1587. s_cert_file = NULL;
  1588. s_key_file = NULL;
  1589. s_dcert_file = NULL;
  1590. s_dkey_file = NULL;
  1591. s_cert_file2 = NULL;
  1592. s_key_file2 = NULL;
  1593. }
  1594. ctx = SSL_CTX_new(meth);
  1595. if (ctx == NULL) {
  1596. ERR_print_errors(bio_err);
  1597. goto end;
  1598. }
  1599. if (sdebug)
  1600. ssl_ctx_security_debug(ctx, sdebug);
  1601. if (ssl_config) {
  1602. if (SSL_CTX_config(ctx, ssl_config) == 0) {
  1603. BIO_printf(bio_err, "Error using configuration \"%s\"\n",
  1604. ssl_config);
  1605. ERR_print_errors(bio_err);
  1606. goto end;
  1607. }
  1608. }
  1609. if (session_id_prefix) {
  1610. if (strlen(session_id_prefix) >= 32)
  1611. BIO_printf(bio_err,
  1612. "warning: id_prefix is too long, only one new session will be possible\n");
  1613. if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
  1614. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1615. ERR_print_errors(bio_err);
  1616. goto end;
  1617. }
  1618. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1619. }
  1620. SSL_CTX_set_quiet_shutdown(ctx, 1);
  1621. if (exc)
  1622. ssl_ctx_set_excert(ctx, exc);
  1623. if (state)
  1624. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1625. if (no_cache)
  1626. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  1627. else if (ext_cache)
  1628. init_session_cache_ctx(ctx);
  1629. else
  1630. SSL_CTX_sess_set_cache_size(ctx, 128);
  1631. if (async) {
  1632. SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
  1633. }
  1634. #ifndef OPENSSL_NO_SRTP
  1635. if (srtp_profiles != NULL) {
  1636. /* Returns 0 on success! */
  1637. if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
  1638. BIO_printf(bio_err, "Error setting SRTP profile\n");
  1639. ERR_print_errors(bio_err);
  1640. goto end;
  1641. }
  1642. }
  1643. #endif
  1644. if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
  1645. ERR_print_errors(bio_err);
  1646. goto end;
  1647. }
  1648. if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
  1649. BIO_printf(bio_err, "Error setting verify params\n");
  1650. ERR_print_errors(bio_err);
  1651. goto end;
  1652. }
  1653. ssl_ctx_add_crls(ctx, crls, 0);
  1654. if (!config_ctx(cctx, ssl_args, ctx, jpake_secret == NULL))
  1655. goto end;
  1656. if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
  1657. crls, crl_download)) {
  1658. BIO_printf(bio_err, "Error loading store locations\n");
  1659. ERR_print_errors(bio_err);
  1660. goto end;
  1661. }
  1662. if (s_cert2) {
  1663. ctx2 = SSL_CTX_new(meth);
  1664. if (ctx2 == NULL) {
  1665. ERR_print_errors(bio_err);
  1666. goto end;
  1667. }
  1668. }
  1669. if (ctx2) {
  1670. BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
  1671. if (sdebug)
  1672. ssl_ctx_security_debug(ctx, sdebug);
  1673. if (session_id_prefix) {
  1674. if (strlen(session_id_prefix) >= 32)
  1675. BIO_printf(bio_err,
  1676. "warning: id_prefix is too long, only one new session will be possible\n");
  1677. if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
  1678. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1679. ERR_print_errors(bio_err);
  1680. goto end;
  1681. }
  1682. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1683. }
  1684. SSL_CTX_set_quiet_shutdown(ctx2, 1);
  1685. if (exc)
  1686. ssl_ctx_set_excert(ctx2, exc);
  1687. if (state)
  1688. SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
  1689. if (no_cache)
  1690. SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
  1691. else if (ext_cache)
  1692. init_session_cache_ctx(ctx2);
  1693. else
  1694. SSL_CTX_sess_set_cache_size(ctx2, 128);
  1695. if (async)
  1696. SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC);
  1697. if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
  1698. (!SSL_CTX_set_default_verify_paths(ctx2))) {
  1699. ERR_print_errors(bio_err);
  1700. }
  1701. if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) {
  1702. BIO_printf(bio_err, "Error setting verify params\n");
  1703. ERR_print_errors(bio_err);
  1704. goto end;
  1705. }
  1706. ssl_ctx_add_crls(ctx2, crls, 0);
  1707. if (!config_ctx(cctx, ssl_args, ctx2, jpake_secret == NULL))
  1708. goto end;
  1709. }
  1710. #ifndef OPENSSL_NO_NEXTPROTONEG
  1711. if (next_proto.data)
  1712. SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
  1713. &next_proto);
  1714. #endif
  1715. if (alpn_ctx.data)
  1716. SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
  1717. #ifndef OPENSSL_NO_DH
  1718. if (!no_dhe) {
  1719. DH *dh = NULL;
  1720. if (dhfile)
  1721. dh = load_dh_param(dhfile);
  1722. else if (s_cert_file)
  1723. dh = load_dh_param(s_cert_file);
  1724. if (dh != NULL) {
  1725. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1726. } else {
  1727. BIO_printf(bio_s_out, "Using default temp DH parameters\n");
  1728. }
  1729. (void)BIO_flush(bio_s_out);
  1730. if (dh == NULL)
  1731. SSL_CTX_set_dh_auto(ctx, 1);
  1732. else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
  1733. BIO_puts(bio_err, "Error setting temp DH parameters\n");
  1734. ERR_print_errors(bio_err);
  1735. DH_free(dh);
  1736. goto end;
  1737. }
  1738. if (ctx2) {
  1739. if (!dhfile) {
  1740. DH *dh2 = load_dh_param(s_cert_file2);
  1741. if (dh2 != NULL) {
  1742. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1743. (void)BIO_flush(bio_s_out);
  1744. DH_free(dh);
  1745. dh = dh2;
  1746. }
  1747. }
  1748. if (dh == NULL)
  1749. SSL_CTX_set_dh_auto(ctx2, 1);
  1750. else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
  1751. BIO_puts(bio_err, "Error setting temp DH parameters\n");
  1752. ERR_print_errors(bio_err);
  1753. DH_free(dh);
  1754. goto end;
  1755. }
  1756. }
  1757. DH_free(dh);
  1758. }
  1759. #endif
  1760. if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
  1761. goto end;
  1762. if (s_serverinfo_file != NULL
  1763. && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
  1764. ERR_print_errors(bio_err);
  1765. goto end;
  1766. }
  1767. if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
  1768. goto end;
  1769. if (s_dcert != NULL) {
  1770. if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
  1771. goto end;
  1772. }
  1773. if (no_resume_ephemeral) {
  1774. SSL_CTX_set_not_resumable_session_callback(ctx,
  1775. not_resumable_sess_cb);
  1776. if (ctx2)
  1777. SSL_CTX_set_not_resumable_session_callback(ctx2,
  1778. not_resumable_sess_cb);
  1779. }
  1780. #ifndef OPENSSL_NO_PSK
  1781. # ifdef OPENSSL_NO_JPAKE
  1782. if (psk_key != NULL)
  1783. # else
  1784. if (psk_key != NULL || jpake_secret)
  1785. # endif
  1786. {
  1787. if (s_debug)
  1788. BIO_printf(bio_s_out,
  1789. "PSK key given or JPAKE in use, setting server callback\n");
  1790. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1791. }
  1792. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
  1793. BIO_printf(bio_err, "error setting PSK identity hint to context\n");
  1794. ERR_print_errors(bio_err);
  1795. goto end;
  1796. }
  1797. #endif
  1798. SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
  1799. if (!SSL_CTX_set_session_id_context(ctx,
  1800. (void *)&s_server_session_id_context,
  1801. sizeof s_server_session_id_context)) {
  1802. BIO_printf(bio_err, "error setting session id context\n");
  1803. ERR_print_errors(bio_err);
  1804. goto end;
  1805. }
  1806. /* Set DTLS cookie generation and verification callbacks */
  1807. SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
  1808. SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
  1809. if (ctx2) {
  1810. SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
  1811. if (!SSL_CTX_set_session_id_context(ctx2,
  1812. (void *)&s_server_session_id_context,
  1813. sizeof s_server_session_id_context)) {
  1814. BIO_printf(bio_err, "error setting session id context\n");
  1815. ERR_print_errors(bio_err);
  1816. goto end;
  1817. }
  1818. tlsextcbp.biodebug = bio_s_out;
  1819. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1820. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1821. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1822. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1823. }
  1824. #ifndef OPENSSL_NO_SRP
  1825. if (srp_verifier_file != NULL) {
  1826. srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
  1827. srp_callback_parm.user = NULL;
  1828. srp_callback_parm.login = NULL;
  1829. if ((ret =
  1830. SRP_VBASE_init(srp_callback_parm.vb,
  1831. srp_verifier_file)) != SRP_NO_ERROR) {
  1832. BIO_printf(bio_err,
  1833. "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
  1834. srp_verifier_file, ret);
  1835. goto end;
  1836. }
  1837. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
  1838. SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
  1839. SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
  1840. } else
  1841. #endif
  1842. if (CAfile != NULL) {
  1843. SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
  1844. if (ctx2)
  1845. SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
  1846. }
  1847. if (s_tlsextstatus) {
  1848. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1849. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1850. if (ctx2) {
  1851. SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);
  1852. SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);
  1853. }
  1854. }
  1855. BIO_printf(bio_s_out, "ACCEPT\n");
  1856. (void)BIO_flush(bio_s_out);
  1857. if (rev)
  1858. server_cb = rev_body;
  1859. else if (www)
  1860. server_cb = www_body;
  1861. else
  1862. server_cb = sv_body;
  1863. #ifdef AF_UNIX
  1864. if (socket_family == AF_UNIX
  1865. && unlink_unix_path)
  1866. unlink(host);
  1867. #endif
  1868. do_server(&accept_socket, host, port, socket_family, socket_type,
  1869. server_cb, context, naccept);
  1870. print_stats(bio_s_out, ctx);
  1871. ret = 0;
  1872. end:
  1873. SSL_CTX_free(ctx);
  1874. X509_free(s_cert);
  1875. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  1876. X509_free(s_dcert);
  1877. EVP_PKEY_free(s_key);
  1878. EVP_PKEY_free(s_dkey);
  1879. sk_X509_pop_free(s_chain, X509_free);
  1880. sk_X509_pop_free(s_dchain, X509_free);
  1881. OPENSSL_free(pass);
  1882. OPENSSL_free(dpass);
  1883. OPENSSL_free(host);
  1884. OPENSSL_free(port);
  1885. X509_VERIFY_PARAM_free(vpm);
  1886. free_sessions();
  1887. OPENSSL_free(tlscstatp.host);
  1888. OPENSSL_free(tlscstatp.port);
  1889. OPENSSL_free(tlscstatp.path);
  1890. SSL_CTX_free(ctx2);
  1891. X509_free(s_cert2);
  1892. EVP_PKEY_free(s_key2);
  1893. BIO_free(serverinfo_in);
  1894. #ifndef OPENSSL_NO_NEXTPROTONEG
  1895. OPENSSL_free(next_proto.data);
  1896. #endif
  1897. OPENSSL_free(alpn_ctx.data);
  1898. ssl_excert_free(exc);
  1899. sk_OPENSSL_STRING_free(ssl_args);
  1900. SSL_CONF_CTX_free(cctx);
  1901. BIO_free(bio_s_out);
  1902. bio_s_out = NULL;
  1903. BIO_free(bio_s_msg);
  1904. bio_s_msg = NULL;
  1905. return (ret);
  1906. }
  1907. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1908. {
  1909. BIO_printf(bio, "%4ld items in the session cache\n",
  1910. SSL_CTX_sess_number(ssl_ctx));
  1911. BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
  1912. SSL_CTX_sess_connect(ssl_ctx));
  1913. BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
  1914. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1915. BIO_printf(bio, "%4ld client connects that finished\n",
  1916. SSL_CTX_sess_connect_good(ssl_ctx));
  1917. BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
  1918. SSL_CTX_sess_accept(ssl_ctx));
  1919. BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
  1920. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1921. BIO_printf(bio, "%4ld server accepts that finished\n",
  1922. SSL_CTX_sess_accept_good(ssl_ctx));
  1923. BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
  1924. BIO_printf(bio, "%4ld session cache misses\n",
  1925. SSL_CTX_sess_misses(ssl_ctx));
  1926. BIO_printf(bio, "%4ld session cache timeouts\n",
  1927. SSL_CTX_sess_timeouts(ssl_ctx));
  1928. BIO_printf(bio, "%4ld callback cache hits\n",
  1929. SSL_CTX_sess_cb_hits(ssl_ctx));
  1930. BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
  1931. SSL_CTX_sess_cache_full(ssl_ctx),
  1932. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1933. }
  1934. static int sv_body(const char *hostname, int s, int stype,
  1935. unsigned char *context)
  1936. {
  1937. char *buf = NULL;
  1938. fd_set readfds;
  1939. int ret = 1, width;
  1940. int k, i;
  1941. unsigned long l;
  1942. SSL *con = NULL;
  1943. BIO *sbio;
  1944. struct timeval timeout;
  1945. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1946. struct timeval tv;
  1947. #else
  1948. struct timeval *timeoutp;
  1949. #endif
  1950. buf = app_malloc(bufsize, "server buffer");
  1951. #ifdef FIONBIO
  1952. if (s_nbio) {
  1953. unsigned long sl = 1;
  1954. if (!s_quiet)
  1955. BIO_printf(bio_err, "turning on non blocking io\n");
  1956. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  1957. ERR_print_errors(bio_err);
  1958. }
  1959. #endif
  1960. if (con == NULL) {
  1961. con = SSL_new(ctx);
  1962. if (s_tlsextdebug) {
  1963. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1964. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1965. }
  1966. if (context
  1967. && !SSL_set_session_id_context(con,
  1968. context, strlen((char *)context))) {
  1969. BIO_printf(bio_err, "Error setting session id context\n");
  1970. ret = -1;
  1971. goto err;
  1972. }
  1973. }
  1974. if (!SSL_clear(con)) {
  1975. BIO_printf(bio_err, "Error clearing SSL connection\n");
  1976. ret = -1;
  1977. goto err;
  1978. }
  1979. #ifndef OPENSSL_NO_DTLS
  1980. if (stype == SOCK_DGRAM) {
  1981. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  1982. if (enable_timeouts) {
  1983. timeout.tv_sec = 0;
  1984. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1985. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1986. timeout.tv_sec = 0;
  1987. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1988. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1989. }
  1990. if (socket_mtu) {
  1991. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  1992. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  1993. DTLS_get_link_min_mtu(con));
  1994. ret = -1;
  1995. BIO_free(sbio);
  1996. goto err;
  1997. }
  1998. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1999. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  2000. BIO_printf(bio_err, "Failed to set MTU\n");
  2001. ret = -1;
  2002. BIO_free(sbio);
  2003. goto err;
  2004. }
  2005. } else
  2006. /* want to do MTU discovery */
  2007. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  2008. /* turn on cookie exchange */
  2009. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  2010. } else
  2011. #endif
  2012. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2013. if (s_nbio_test) {
  2014. BIO *test;
  2015. test = BIO_new(BIO_f_nbio_test());
  2016. sbio = BIO_push(test, sbio);
  2017. }
  2018. #ifndef OPENSSL_NO_JPAKE
  2019. if (jpake_secret)
  2020. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  2021. #endif
  2022. SSL_set_bio(con, sbio, sbio);
  2023. SSL_set_accept_state(con);
  2024. /* SSL_set_fd(con,s); */
  2025. if (s_debug) {
  2026. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2027. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2028. }
  2029. if (s_msg) {
  2030. #ifndef OPENSSL_NO_SSL_TRACE
  2031. if (s_msg == 2)
  2032. SSL_set_msg_callback(con, SSL_trace);
  2033. else
  2034. #endif
  2035. SSL_set_msg_callback(con, msg_cb);
  2036. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2037. }
  2038. if (s_tlsextdebug) {
  2039. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2040. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2041. }
  2042. width = s + 1;
  2043. for (;;) {
  2044. int read_from_terminal;
  2045. int read_from_sslcon;
  2046. read_from_terminal = 0;
  2047. read_from_sslcon = SSL_pending(con)
  2048. || (async && SSL_waiting_for_async(con));
  2049. if (!read_from_sslcon) {
  2050. FD_ZERO(&readfds);
  2051. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
  2052. openssl_fdset(fileno(stdin), &readfds);
  2053. #endif
  2054. openssl_fdset(s, &readfds);
  2055. /*
  2056. * Note: under VMS with SOCKETSHR the second parameter is
  2057. * currently of type (int *) whereas under other systems it is
  2058. * (void *) if you don't have a cast it will choke the compiler:
  2059. * if you do have a cast then you can either go for (int *) or
  2060. * (void *).
  2061. */
  2062. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  2063. /*
  2064. * Under DOS (non-djgpp) and Windows we can't select on stdin:
  2065. * only on sockets. As a workaround we timeout the select every
  2066. * second and check for any keypress. In a proper Windows
  2067. * application we wouldn't do this because it is inefficient.
  2068. */
  2069. tv.tv_sec = 1;
  2070. tv.tv_usec = 0;
  2071. i = select(width, (void *)&readfds, NULL, NULL, &tv);
  2072. if ((i < 0) || (!i && !_kbhit()))
  2073. continue;
  2074. if (_kbhit())
  2075. read_from_terminal = 1;
  2076. #else
  2077. if ((SSL_version(con) == DTLS1_VERSION) &&
  2078. DTLSv1_get_timeout(con, &timeout))
  2079. timeoutp = &timeout;
  2080. else
  2081. timeoutp = NULL;
  2082. i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
  2083. if ((SSL_version(con) == DTLS1_VERSION)
  2084. && DTLSv1_handle_timeout(con) > 0) {
  2085. BIO_printf(bio_err, "TIMEOUT occurred\n");
  2086. }
  2087. if (i <= 0)
  2088. continue;
  2089. if (FD_ISSET(fileno(stdin), &readfds))
  2090. read_from_terminal = 1;
  2091. #endif
  2092. if (FD_ISSET(s, &readfds))
  2093. read_from_sslcon = 1;
  2094. }
  2095. if (read_from_terminal) {
  2096. if (s_crlf) {
  2097. int j, lf_num;
  2098. i = raw_read_stdin(buf, bufsize / 2);
  2099. lf_num = 0;
  2100. /* both loops are skipped when i <= 0 */
  2101. for (j = 0; j < i; j++)
  2102. if (buf[j] == '\n')
  2103. lf_num++;
  2104. for (j = i - 1; j >= 0; j--) {
  2105. buf[j + lf_num] = buf[j];
  2106. if (buf[j] == '\n') {
  2107. lf_num--;
  2108. i++;
  2109. buf[j + lf_num] = '\r';
  2110. }
  2111. }
  2112. assert(lf_num == 0);
  2113. } else
  2114. i = raw_read_stdin(buf, bufsize);
  2115. if (!s_quiet && !s_brief) {
  2116. if ((i <= 0) || (buf[0] == 'Q')) {
  2117. BIO_printf(bio_s_out, "DONE\n");
  2118. (void)BIO_flush(bio_s_out);
  2119. SHUTDOWN(s);
  2120. close_accept_socket();
  2121. ret = -11;
  2122. goto err;
  2123. }
  2124. if ((i <= 0) || (buf[0] == 'q')) {
  2125. BIO_printf(bio_s_out, "DONE\n");
  2126. (void)BIO_flush(bio_s_out);
  2127. if (SSL_version(con) != DTLS1_VERSION)
  2128. SHUTDOWN(s);
  2129. /*
  2130. * close_accept_socket(); ret= -11;
  2131. */
  2132. goto err;
  2133. }
  2134. #ifndef OPENSSL_NO_HEARTBEATS
  2135. if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2136. BIO_printf(bio_err, "HEARTBEATING\n");
  2137. SSL_heartbeat(con);
  2138. i = 0;
  2139. continue;
  2140. }
  2141. #endif
  2142. if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2143. SSL_renegotiate(con);
  2144. i = SSL_do_handshake(con);
  2145. printf("SSL_do_handshake -> %d\n", i);
  2146. i = 0; /* 13; */
  2147. continue;
  2148. /*
  2149. * strcpy(buf,"server side RE-NEGOTIATE\n");
  2150. */
  2151. }
  2152. if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2153. SSL_set_verify(con,
  2154. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2155. NULL);
  2156. SSL_renegotiate(con);
  2157. i = SSL_do_handshake(con);
  2158. printf("SSL_do_handshake -> %d\n", i);
  2159. i = 0; /* 13; */
  2160. continue;
  2161. /*
  2162. * strcpy(buf,"server side RE-NEGOTIATE asking for client
  2163. * cert\n");
  2164. */
  2165. }
  2166. if (buf[0] == 'P') {
  2167. static const char *str = "Lets print some clear text\n";
  2168. BIO_write(SSL_get_wbio(con), str, strlen(str));
  2169. }
  2170. if (buf[0] == 'S') {
  2171. print_stats(bio_s_out, SSL_get_SSL_CTX(con));
  2172. }
  2173. }
  2174. #ifdef CHARSET_EBCDIC
  2175. ebcdic2ascii(buf, buf, i);
  2176. #endif
  2177. l = k = 0;
  2178. for (;;) {
  2179. /* should do a select for the write */
  2180. #ifdef RENEG
  2181. {
  2182. static count = 0;
  2183. if (++count == 100) {
  2184. count = 0;
  2185. SSL_renegotiate(con);
  2186. }
  2187. }
  2188. #endif
  2189. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2190. #ifndef OPENSSL_NO_SRP
  2191. while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
  2192. BIO_printf(bio_s_out, "LOOKUP renego during write\n");
  2193. srp_callback_parm.user =
  2194. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2195. srp_callback_parm.login);
  2196. if (srp_callback_parm.user)
  2197. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2198. srp_callback_parm.user->info);
  2199. else
  2200. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2201. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2202. }
  2203. #endif
  2204. switch (SSL_get_error(con, k)) {
  2205. case SSL_ERROR_NONE:
  2206. break;
  2207. case SSL_ERROR_WANT_ASYNC:
  2208. BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
  2209. wait_for_async(con);
  2210. break;
  2211. case SSL_ERROR_WANT_WRITE:
  2212. case SSL_ERROR_WANT_READ:
  2213. case SSL_ERROR_WANT_X509_LOOKUP:
  2214. BIO_printf(bio_s_out, "Write BLOCK\n");
  2215. break;
  2216. case SSL_ERROR_SYSCALL:
  2217. case SSL_ERROR_SSL:
  2218. BIO_printf(bio_s_out, "ERROR\n");
  2219. (void)BIO_flush(bio_s_out);
  2220. ERR_print_errors(bio_err);
  2221. ret = 1;
  2222. goto err;
  2223. /* break; */
  2224. case SSL_ERROR_ZERO_RETURN:
  2225. BIO_printf(bio_s_out, "DONE\n");
  2226. (void)BIO_flush(bio_s_out);
  2227. ret = 1;
  2228. goto err;
  2229. }
  2230. if (k > 0) {
  2231. l += k;
  2232. i -= k;
  2233. }
  2234. if (i <= 0)
  2235. break;
  2236. }
  2237. }
  2238. if (read_from_sslcon) {
  2239. /*
  2240. * init_ssl_connection handles all async events itself so if we're
  2241. * waiting for async then we shouldn't go back into
  2242. * init_ssl_connection
  2243. */
  2244. if ((!async || !SSL_waiting_for_async(con))
  2245. && !SSL_is_init_finished(con)) {
  2246. i = init_ssl_connection(con);
  2247. if (i < 0) {
  2248. ret = 0;
  2249. goto err;
  2250. } else if (i == 0) {
  2251. ret = 1;
  2252. goto err;
  2253. }
  2254. } else {
  2255. again:
  2256. i = SSL_read(con, (char *)buf, bufsize);
  2257. #ifndef OPENSSL_NO_SRP
  2258. while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2259. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2260. srp_callback_parm.user =
  2261. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2262. srp_callback_parm.login);
  2263. if (srp_callback_parm.user)
  2264. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2265. srp_callback_parm.user->info);
  2266. else
  2267. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2268. i = SSL_read(con, (char *)buf, bufsize);
  2269. }
  2270. #endif
  2271. switch (SSL_get_error(con, i)) {
  2272. case SSL_ERROR_NONE:
  2273. #ifdef CHARSET_EBCDIC
  2274. ascii2ebcdic(buf, buf, i);
  2275. #endif
  2276. raw_write_stdout(buf, (unsigned int)i);
  2277. if (SSL_pending(con))
  2278. goto again;
  2279. break;
  2280. case SSL_ERROR_WANT_ASYNC:
  2281. BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
  2282. wait_for_async(con);
  2283. break;
  2284. case SSL_ERROR_WANT_WRITE:
  2285. case SSL_ERROR_WANT_READ:
  2286. BIO_printf(bio_s_out, "Read BLOCK\n");
  2287. break;
  2288. case SSL_ERROR_SYSCALL:
  2289. case SSL_ERROR_SSL:
  2290. BIO_printf(bio_s_out, "ERROR\n");
  2291. (void)BIO_flush(bio_s_out);
  2292. ERR_print_errors(bio_err);
  2293. ret = 1;
  2294. goto err;
  2295. case SSL_ERROR_ZERO_RETURN:
  2296. BIO_printf(bio_s_out, "DONE\n");
  2297. (void)BIO_flush(bio_s_out);
  2298. ret = 1;
  2299. goto err;
  2300. }
  2301. }
  2302. }
  2303. }
  2304. err:
  2305. if (con != NULL) {
  2306. BIO_printf(bio_s_out, "shutting down SSL\n");
  2307. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2308. SSL_free(con);
  2309. }
  2310. BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
  2311. OPENSSL_clear_free(buf, bufsize);
  2312. if (ret >= 0)
  2313. BIO_printf(bio_s_out, "ACCEPT\n");
  2314. (void)BIO_flush(bio_s_out);
  2315. return (ret);
  2316. }
  2317. static void close_accept_socket(void)
  2318. {
  2319. BIO_printf(bio_err, "shutdown accept socket\n");
  2320. if (accept_socket >= 0) {
  2321. SHUTDOWN2(accept_socket);
  2322. }
  2323. }
  2324. static int init_ssl_connection(SSL *con)
  2325. {
  2326. int i;
  2327. const char *str;
  2328. X509 *peer;
  2329. long verify_err;
  2330. char buf[BUFSIZ];
  2331. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2332. const unsigned char *next_proto_neg;
  2333. unsigned next_proto_neg_len;
  2334. #endif
  2335. unsigned char *exportedkeymat;
  2336. #ifndef OPENSSL_NO_DTLS
  2337. if(dtlslisten) {
  2338. BIO_ADDR *client = NULL;
  2339. if ((client = BIO_ADDR_new()) == NULL) {
  2340. BIO_printf(bio_err, "ERROR - memory\n");
  2341. return 0;
  2342. }
  2343. i = DTLSv1_listen(con, client);
  2344. if (i > 0) {
  2345. BIO *wbio;
  2346. int fd = -1;
  2347. wbio = SSL_get_wbio(con);
  2348. if(wbio) {
  2349. BIO_get_fd(wbio, &fd);
  2350. }
  2351. if(!wbio || BIO_connect(fd, client, 0) == 0) {
  2352. BIO_printf(bio_err, "ERROR - unable to connect\n");
  2353. BIO_ADDR_free(client);
  2354. return 0;
  2355. }
  2356. BIO_ADDR_free(client);
  2357. dtlslisten = 0;
  2358. i = SSL_accept(con);
  2359. }
  2360. } else
  2361. #endif
  2362. do {
  2363. i = SSL_accept(con);
  2364. #ifdef CERT_CB_TEST_RETRY
  2365. {
  2366. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
  2367. && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
  2368. BIO_printf(bio_err,
  2369. "LOOKUP from certificate callback during accept\n");
  2370. i = SSL_accept(con);
  2371. }
  2372. }
  2373. #endif
  2374. #ifndef OPENSSL_NO_SRP
  2375. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2376. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2377. srp_callback_parm.login);
  2378. srp_callback_parm.user =
  2379. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2380. srp_callback_parm.login);
  2381. if (srp_callback_parm.user)
  2382. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2383. srp_callback_parm.user->info);
  2384. else
  2385. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2386. i = SSL_accept(con);
  2387. }
  2388. #endif
  2389. } while (i < 0 && SSL_waiting_for_async(con));
  2390. if (i <= 0) {
  2391. if ((dtlslisten && i == 0)
  2392. || (!dtlslisten && BIO_sock_should_retry(i))) {
  2393. BIO_printf(bio_s_out, "DELAY\n");
  2394. return (1);
  2395. }
  2396. BIO_printf(bio_err, "ERROR\n");
  2397. verify_err = SSL_get_verify_result(con);
  2398. if (verify_err != X509_V_OK) {
  2399. BIO_printf(bio_err, "verify error:%s\n",
  2400. X509_verify_cert_error_string(verify_err));
  2401. }
  2402. /* Always print any error messages */
  2403. ERR_print_errors(bio_err);
  2404. return (0);
  2405. }
  2406. if (s_brief)
  2407. print_ssl_summary(con);
  2408. PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
  2409. peer = SSL_get_peer_certificate(con);
  2410. if (peer != NULL) {
  2411. BIO_printf(bio_s_out, "Client certificate\n");
  2412. PEM_write_bio_X509(bio_s_out, peer);
  2413. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
  2414. BIO_printf(bio_s_out, "subject=%s\n", buf);
  2415. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
  2416. BIO_printf(bio_s_out, "issuer=%s\n", buf);
  2417. X509_free(peer);
  2418. }
  2419. if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
  2420. BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
  2421. str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2422. ssl_print_sigalgs(bio_s_out, con);
  2423. #ifndef OPENSSL_NO_EC
  2424. ssl_print_point_formats(bio_s_out, con);
  2425. ssl_print_curves(bio_s_out, con, 0);
  2426. #endif
  2427. BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
  2428. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2429. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2430. if (next_proto_neg) {
  2431. BIO_printf(bio_s_out, "NEXTPROTO is ");
  2432. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2433. BIO_printf(bio_s_out, "\n");
  2434. }
  2435. #endif
  2436. #ifndef OPENSSL_NO_SRTP
  2437. {
  2438. SRTP_PROTECTION_PROFILE *srtp_profile
  2439. = SSL_get_selected_srtp_profile(con);
  2440. if (srtp_profile)
  2441. BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
  2442. srtp_profile->name);
  2443. }
  2444. #endif
  2445. if (SSL_session_reused(con))
  2446. BIO_printf(bio_s_out, "Reused session-id\n");
  2447. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2448. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2449. if (keymatexportlabel != NULL) {
  2450. BIO_printf(bio_s_out, "Keying material exporter:\n");
  2451. BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
  2452. BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
  2453. exportedkeymat = app_malloc(keymatexportlen, "export key");
  2454. if (!SSL_export_keying_material(con, exportedkeymat,
  2455. keymatexportlen,
  2456. keymatexportlabel,
  2457. strlen(keymatexportlabel),
  2458. NULL, 0, 0)) {
  2459. BIO_printf(bio_s_out, " Error\n");
  2460. } else {
  2461. BIO_printf(bio_s_out, " Keying material: ");
  2462. for (i = 0; i < keymatexportlen; i++)
  2463. BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
  2464. BIO_printf(bio_s_out, "\n");
  2465. }
  2466. OPENSSL_free(exportedkeymat);
  2467. }
  2468. return (1);
  2469. }
  2470. #ifndef OPENSSL_NO_DH
  2471. static DH *load_dh_param(const char *dhfile)
  2472. {
  2473. DH *ret = NULL;
  2474. BIO *bio;
  2475. if ((bio = BIO_new_file(dhfile, "r")) == NULL)
  2476. goto err;
  2477. ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2478. err:
  2479. BIO_free(bio);
  2480. return (ret);
  2481. }
  2482. #endif
  2483. static int www_body(const char *hostname, int s, int stype,
  2484. unsigned char *context)
  2485. {
  2486. char *buf = NULL;
  2487. int ret = 1;
  2488. int i, j, k, dot;
  2489. SSL *con;
  2490. const SSL_CIPHER *c;
  2491. BIO *io, *ssl_bio, *sbio;
  2492. #ifdef RENEG
  2493. int total_bytes = 0;
  2494. #endif
  2495. int width;
  2496. fd_set readfds;
  2497. /* Set width for a select call if needed */
  2498. width = s + 1;
  2499. buf = app_malloc(bufsize, "server www buffer");
  2500. io = BIO_new(BIO_f_buffer());
  2501. ssl_bio = BIO_new(BIO_f_ssl());
  2502. if ((io == NULL) || (ssl_bio == NULL))
  2503. goto err;
  2504. #ifdef FIONBIO
  2505. if (s_nbio) {
  2506. unsigned long sl = 1;
  2507. if (!s_quiet)
  2508. BIO_printf(bio_err, "turning on non blocking io\n");
  2509. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  2510. ERR_print_errors(bio_err);
  2511. }
  2512. #endif
  2513. /* lets make the output buffer a reasonable size */
  2514. if (!BIO_set_write_buffer_size(io, bufsize))
  2515. goto err;
  2516. if ((con = SSL_new(ctx)) == NULL)
  2517. goto err;
  2518. if (s_tlsextdebug) {
  2519. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2520. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2521. }
  2522. if (context && !SSL_set_session_id_context(con, context,
  2523. strlen((char *)context)))
  2524. goto err;
  2525. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2526. if (s_nbio_test) {
  2527. BIO *test;
  2528. test = BIO_new(BIO_f_nbio_test());
  2529. sbio = BIO_push(test, sbio);
  2530. }
  2531. SSL_set_bio(con, sbio, sbio);
  2532. SSL_set_accept_state(con);
  2533. /* SSL_set_fd(con,s); */
  2534. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2535. BIO_push(io, ssl_bio);
  2536. #ifdef CHARSET_EBCDIC
  2537. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2538. #endif
  2539. if (s_debug) {
  2540. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2541. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2542. }
  2543. if (s_msg) {
  2544. #ifndef OPENSSL_NO_SSL_TRACE
  2545. if (s_msg == 2)
  2546. SSL_set_msg_callback(con, SSL_trace);
  2547. else
  2548. #endif
  2549. SSL_set_msg_callback(con, msg_cb);
  2550. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2551. }
  2552. for (;;) {
  2553. i = BIO_gets(io, buf, bufsize - 1);
  2554. if (i < 0) { /* error */
  2555. if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
  2556. if (!s_quiet)
  2557. ERR_print_errors(bio_err);
  2558. goto err;
  2559. } else {
  2560. BIO_printf(bio_s_out, "read R BLOCK\n");
  2561. #ifndef OPENSSL_NO_SRP
  2562. if (BIO_should_io_special(io)
  2563. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2564. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2565. srp_callback_parm.user =
  2566. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2567. srp_callback_parm.login);
  2568. if (srp_callback_parm.user)
  2569. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2570. srp_callback_parm.user->info);
  2571. else
  2572. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2573. continue;
  2574. }
  2575. #endif
  2576. #if defined(OPENSSL_SYS_NETWARE)
  2577. delay(1000);
  2578. #elif !defined(OPENSSL_SYS_MSDOS)
  2579. sleep(1);
  2580. #endif
  2581. continue;
  2582. }
  2583. } else if (i == 0) { /* end of input */
  2584. ret = 1;
  2585. goto end;
  2586. }
  2587. /* else we have data */
  2588. if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
  2589. ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
  2590. char *p;
  2591. X509 *peer;
  2592. STACK_OF(SSL_CIPHER) *sk;
  2593. static const char *space = " ";
  2594. if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
  2595. if (strncmp("GET /renegcert", buf, 14) == 0)
  2596. SSL_set_verify(con,
  2597. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2598. NULL);
  2599. i = SSL_renegotiate(con);
  2600. BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
  2601. /* Send the HelloRequest */
  2602. i = SSL_do_handshake(con);
  2603. if (i <= 0) {
  2604. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
  2605. SSL_get_error(con, i));
  2606. ERR_print_errors(bio_err);
  2607. goto err;
  2608. }
  2609. /* Wait for a ClientHello to come back */
  2610. FD_ZERO(&readfds);
  2611. openssl_fdset(s, &readfds);
  2612. i = select(width, (void *)&readfds, NULL, NULL, NULL);
  2613. if (i <= 0 || !FD_ISSET(s, &readfds)) {
  2614. BIO_printf(bio_s_out, "Error waiting for client response\n");
  2615. ERR_print_errors(bio_err);
  2616. goto err;
  2617. }
  2618. /*
  2619. * We're not acutally expecting any data here and we ignore
  2620. * any that is sent. This is just to force the handshake that
  2621. * we're expecting to come from the client. If they haven't
  2622. * sent one there's not much we can do.
  2623. */
  2624. BIO_gets(io, buf, bufsize - 1);
  2625. }
  2626. BIO_puts(io,
  2627. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2628. BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2629. BIO_puts(io, "<pre>\n");
  2630. /* BIO_puts(io,OpenSSL_version(OPENSSL_VERSION));*/
  2631. BIO_puts(io, "\n");
  2632. for (i = 0; i < local_argc; i++) {
  2633. const char *myp;
  2634. for (myp = local_argv[i]; *myp; myp++)
  2635. switch (*myp) {
  2636. case '<':
  2637. BIO_puts(io, "&lt;");
  2638. break;
  2639. case '>':
  2640. BIO_puts(io, "&gt;");
  2641. break;
  2642. case '&':
  2643. BIO_puts(io, "&amp;");
  2644. break;
  2645. default:
  2646. BIO_write(io, myp, 1);
  2647. break;
  2648. }
  2649. BIO_write(io, " ", 1);
  2650. }
  2651. BIO_puts(io, "\n");
  2652. BIO_printf(io,
  2653. "Secure Renegotiation IS%s supported\n",
  2654. SSL_get_secure_renegotiation_support(con) ?
  2655. "" : " NOT");
  2656. /*
  2657. * The following is evil and should not really be done
  2658. */
  2659. BIO_printf(io, "Ciphers supported in s_server binary\n");
  2660. sk = SSL_get_ciphers(con);
  2661. j = sk_SSL_CIPHER_num(sk);
  2662. for (i = 0; i < j; i++) {
  2663. c = sk_SSL_CIPHER_value(sk, i);
  2664. BIO_printf(io, "%-11s:%-25s ",
  2665. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2666. if ((((i + 1) % 2) == 0) && (i + 1 != j))
  2667. BIO_puts(io, "\n");
  2668. }
  2669. BIO_puts(io, "\n");
  2670. p = SSL_get_shared_ciphers(con, buf, bufsize);
  2671. if (p != NULL) {
  2672. BIO_printf(io,
  2673. "---\nCiphers common between both SSL end points:\n");
  2674. j = i = 0;
  2675. while (*p) {
  2676. if (*p == ':') {
  2677. BIO_write(io, space, 26 - j);
  2678. i++;
  2679. j = 0;
  2680. BIO_write(io, ((i % 3) ? " " : "\n"), 1);
  2681. } else {
  2682. BIO_write(io, p, 1);
  2683. j++;
  2684. }
  2685. p++;
  2686. }
  2687. BIO_puts(io, "\n");
  2688. }
  2689. ssl_print_sigalgs(io, con);
  2690. #ifndef OPENSSL_NO_EC
  2691. ssl_print_curves(io, con, 0);
  2692. #endif
  2693. BIO_printf(io, (SSL_session_reused(con)
  2694. ? "---\nReused, " : "---\nNew, "));
  2695. c = SSL_get_current_cipher(con);
  2696. BIO_printf(io, "%s, Cipher is %s\n",
  2697. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2698. SSL_SESSION_print(io, SSL_get_session(con));
  2699. BIO_printf(io, "---\n");
  2700. print_stats(io, SSL_get_SSL_CTX(con));
  2701. BIO_printf(io, "---\n");
  2702. peer = SSL_get_peer_certificate(con);
  2703. if (peer != NULL) {
  2704. BIO_printf(io, "Client certificate\n");
  2705. X509_print(io, peer);
  2706. PEM_write_bio_X509(io, peer);
  2707. } else
  2708. BIO_puts(io, "no client certificate available\n");
  2709. BIO_puts(io, "</BODY></HTML>\r\n\r\n");
  2710. break;
  2711. } else if ((www == 2 || www == 3)
  2712. && (strncmp("GET /", buf, 5) == 0)) {
  2713. BIO *file;
  2714. char *p, *e;
  2715. static const char *text =
  2716. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2717. /* skip the '/' */
  2718. p = &(buf[5]);
  2719. dot = 1;
  2720. for (e = p; *e != '\0'; e++) {
  2721. if (e[0] == ' ')
  2722. break;
  2723. switch (dot) {
  2724. case 1:
  2725. dot = (e[0] == '.') ? 2 : 0;
  2726. break;
  2727. case 2:
  2728. dot = (e[0] == '.') ? 3 : 0;
  2729. break;
  2730. case 3:
  2731. dot = (e[0] == '/') ? -1 : 0;
  2732. break;
  2733. }
  2734. if (dot == 0)
  2735. dot = (e[0] == '/') ? 1 : 0;
  2736. }
  2737. dot = (dot == 3) || (dot == -1); /* filename contains ".."
  2738. * component */
  2739. if (*e == '\0') {
  2740. BIO_puts(io, text);
  2741. BIO_printf(io, "'%s' is an invalid file name\r\n", p);
  2742. break;
  2743. }
  2744. *e = '\0';
  2745. if (dot) {
  2746. BIO_puts(io, text);
  2747. BIO_printf(io, "'%s' contains '..' reference\r\n", p);
  2748. break;
  2749. }
  2750. if (*p == '/') {
  2751. BIO_puts(io, text);
  2752. BIO_printf(io, "'%s' is an invalid path\r\n", p);
  2753. break;
  2754. }
  2755. /* if a directory, do the index thang */
  2756. if (app_isdir(p) > 0) {
  2757. BIO_puts(io, text);
  2758. BIO_printf(io, "'%s' is a directory\r\n", p);
  2759. break;
  2760. }
  2761. if ((file = BIO_new_file(p, "r")) == NULL) {
  2762. BIO_puts(io, text);
  2763. BIO_printf(io, "Error opening '%s'\r\n", p);
  2764. ERR_print_errors(io);
  2765. break;
  2766. }
  2767. if (!s_quiet)
  2768. BIO_printf(bio_err, "FILE:%s\n", p);
  2769. if (www == 2) {
  2770. i = strlen(p);
  2771. if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
  2772. ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
  2773. ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
  2774. BIO_puts(io,
  2775. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2776. else
  2777. BIO_puts(io,
  2778. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2779. }
  2780. /* send the file */
  2781. for (;;) {
  2782. i = BIO_read(file, buf, bufsize);
  2783. if (i <= 0)
  2784. break;
  2785. #ifdef RENEG
  2786. total_bytes += i;
  2787. BIO_printf(bio_err, "%d\n", i);
  2788. if (total_bytes > 3 * 1024) {
  2789. total_bytes = 0;
  2790. BIO_printf(bio_err, "RENEGOTIATE\n");
  2791. SSL_renegotiate(con);
  2792. }
  2793. #endif
  2794. for (j = 0; j < i;) {
  2795. #ifdef RENEG
  2796. {
  2797. static count = 0;
  2798. if (++count == 13) {
  2799. SSL_renegotiate(con);
  2800. }
  2801. }
  2802. #endif
  2803. k = BIO_write(io, &(buf[j]), i - j);
  2804. if (k <= 0) {
  2805. if (!BIO_should_retry(io) && !SSL_waiting_for_async(con))
  2806. goto write_error;
  2807. else {
  2808. BIO_printf(bio_s_out, "rwrite W BLOCK\n");
  2809. }
  2810. } else {
  2811. j += k;
  2812. }
  2813. }
  2814. }
  2815. write_error:
  2816. BIO_free(file);
  2817. break;
  2818. }
  2819. }
  2820. for (;;) {
  2821. i = (int)BIO_flush(io);
  2822. if (i <= 0) {
  2823. if (!BIO_should_retry(io))
  2824. break;
  2825. } else
  2826. break;
  2827. }
  2828. end:
  2829. /* make sure we re-use sessions */
  2830. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2831. err:
  2832. if (ret >= 0)
  2833. BIO_printf(bio_s_out, "ACCEPT\n");
  2834. OPENSSL_free(buf);
  2835. BIO_free_all(io);
  2836. return (ret);
  2837. }
  2838. static int rev_body(const char *hostname, int s, int stype,
  2839. unsigned char *context)
  2840. {
  2841. char *buf = NULL;
  2842. int i;
  2843. int ret = 1;
  2844. SSL *con;
  2845. BIO *io, *ssl_bio, *sbio;
  2846. buf = app_malloc(bufsize, "server rev buffer");
  2847. io = BIO_new(BIO_f_buffer());
  2848. ssl_bio = BIO_new(BIO_f_ssl());
  2849. if ((io == NULL) || (ssl_bio == NULL))
  2850. goto err;
  2851. /* lets make the output buffer a reasonable size */
  2852. if (!BIO_set_write_buffer_size(io, bufsize))
  2853. goto err;
  2854. if ((con = SSL_new(ctx)) == NULL)
  2855. goto err;
  2856. if (s_tlsextdebug) {
  2857. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2858. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2859. }
  2860. if (context && !SSL_set_session_id_context(con, context,
  2861. strlen((char *)context))) {
  2862. ERR_print_errors(bio_err);
  2863. goto err;
  2864. }
  2865. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2866. SSL_set_bio(con, sbio, sbio);
  2867. SSL_set_accept_state(con);
  2868. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2869. BIO_push(io, ssl_bio);
  2870. #ifdef CHARSET_EBCDIC
  2871. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2872. #endif
  2873. if (s_debug) {
  2874. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2875. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2876. }
  2877. if (s_msg) {
  2878. #ifndef OPENSSL_NO_SSL_TRACE
  2879. if (s_msg == 2)
  2880. SSL_set_msg_callback(con, SSL_trace);
  2881. else
  2882. #endif
  2883. SSL_set_msg_callback(con, msg_cb);
  2884. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2885. }
  2886. for (;;) {
  2887. i = BIO_do_handshake(io);
  2888. if (i > 0)
  2889. break;
  2890. if (!BIO_should_retry(io)) {
  2891. BIO_puts(bio_err, "CONNECTION FAILURE\n");
  2892. ERR_print_errors(bio_err);
  2893. goto end;
  2894. }
  2895. #ifndef OPENSSL_NO_SRP
  2896. if (BIO_should_io_special(io)
  2897. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2898. BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
  2899. srp_callback_parm.user =
  2900. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2901. srp_callback_parm.login);
  2902. if (srp_callback_parm.user)
  2903. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2904. srp_callback_parm.user->info);
  2905. else
  2906. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2907. continue;
  2908. }
  2909. #endif
  2910. }
  2911. BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
  2912. print_ssl_summary(con);
  2913. for (;;) {
  2914. i = BIO_gets(io, buf, bufsize - 1);
  2915. if (i < 0) { /* error */
  2916. if (!BIO_should_retry(io)) {
  2917. if (!s_quiet)
  2918. ERR_print_errors(bio_err);
  2919. goto err;
  2920. } else {
  2921. BIO_printf(bio_s_out, "read R BLOCK\n");
  2922. #ifndef OPENSSL_NO_SRP
  2923. if (BIO_should_io_special(io)
  2924. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2925. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2926. srp_callback_parm.user =
  2927. SRP_VBASE_get_by_user(srp_callback_parm.vb,
  2928. srp_callback_parm.login);
  2929. if (srp_callback_parm.user)
  2930. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2931. srp_callback_parm.user->info);
  2932. else
  2933. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2934. continue;
  2935. }
  2936. #endif
  2937. #if defined(OPENSSL_SYS_NETWARE)
  2938. delay(1000);
  2939. #elif !defined(OPENSSL_SYS_MSDOS)
  2940. sleep(1);
  2941. #endif
  2942. continue;
  2943. }
  2944. } else if (i == 0) { /* end of input */
  2945. ret = 1;
  2946. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  2947. goto end;
  2948. } else {
  2949. char *p = buf + i - 1;
  2950. while (i && (*p == '\n' || *p == '\r')) {
  2951. p--;
  2952. i--;
  2953. }
  2954. if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {
  2955. ret = 1;
  2956. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  2957. goto end;
  2958. }
  2959. BUF_reverse((unsigned char *)buf, NULL, i);
  2960. buf[i] = '\n';
  2961. BIO_write(io, buf, i + 1);
  2962. for (;;) {
  2963. i = BIO_flush(io);
  2964. if (i > 0)
  2965. break;
  2966. if (!BIO_should_retry(io))
  2967. goto end;
  2968. }
  2969. }
  2970. }
  2971. end:
  2972. /* make sure we re-use sessions */
  2973. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2974. err:
  2975. OPENSSL_free(buf);
  2976. BIO_free_all(io);
  2977. return (ret);
  2978. }
  2979. #define MAX_SESSION_ID_ATTEMPTS 10
  2980. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2981. unsigned int *id_len)
  2982. {
  2983. unsigned int count = 0;
  2984. do {
  2985. if (RAND_bytes(id, *id_len) <= 0)
  2986. return 0;
  2987. /*
  2988. * Prefix the session_id with the required prefix. NB: If our prefix
  2989. * is too long, clip it - but there will be worse effects anyway, eg.
  2990. * the server could only possibly create 1 session ID (ie. the
  2991. * prefix!) so all future session negotiations will fail due to
  2992. * conflicts.
  2993. */
  2994. memcpy(id, session_id_prefix,
  2995. (strlen(session_id_prefix) < *id_len) ?
  2996. strlen(session_id_prefix) : *id_len);
  2997. }
  2998. while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  2999. (++count < MAX_SESSION_ID_ATTEMPTS));
  3000. if (count >= MAX_SESSION_ID_ATTEMPTS)
  3001. return 0;
  3002. return 1;
  3003. }
  3004. /*
  3005. * By default s_server uses an in-memory cache which caches SSL_SESSION
  3006. * structures without any serialisation. This hides some bugs which only
  3007. * become apparent in deployed servers. By implementing a basic external
  3008. * session cache some issues can be debugged using s_server.
  3009. */
  3010. typedef struct simple_ssl_session_st {
  3011. unsigned char *id;
  3012. unsigned int idlen;
  3013. unsigned char *der;
  3014. int derlen;
  3015. struct simple_ssl_session_st *next;
  3016. } simple_ssl_session;
  3017. static simple_ssl_session *first = NULL;
  3018. static int add_session(SSL *ssl, SSL_SESSION *session)
  3019. {
  3020. simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
  3021. unsigned char *p;
  3022. SSL_SESSION_get_id(session, &sess->idlen);
  3023. sess->derlen = i2d_SSL_SESSION(session, NULL);
  3024. if (sess->derlen < 0) {
  3025. BIO_printf(bio_err, "Error encoding session\n");
  3026. OPENSSL_free(sess);
  3027. return 0;
  3028. }
  3029. sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
  3030. sess->der = app_malloc(sess->derlen, "get session buffer");
  3031. if (!sess->id) {
  3032. BIO_printf(bio_err, "Out of memory adding to external cache\n");
  3033. OPENSSL_free(sess->id);
  3034. OPENSSL_free(sess->der);
  3035. OPENSSL_free(sess);
  3036. return 0;
  3037. }
  3038. p = sess->der;
  3039. /* Assume it still works. */
  3040. if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
  3041. BIO_printf(bio_err, "Unexpected session encoding length\n");
  3042. OPENSSL_free(sess->id);
  3043. OPENSSL_free(sess->der);
  3044. OPENSSL_free(sess);
  3045. return 0;
  3046. }
  3047. sess->next = first;
  3048. first = sess;
  3049. BIO_printf(bio_err, "New session added to external cache\n");
  3050. return 0;
  3051. }
  3052. static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
  3053. int *do_copy)
  3054. {
  3055. simple_ssl_session *sess;
  3056. *do_copy = 0;
  3057. for (sess = first; sess; sess = sess->next) {
  3058. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
  3059. const unsigned char *p = sess->der;
  3060. BIO_printf(bio_err, "Lookup session: cache hit\n");
  3061. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  3062. }
  3063. }
  3064. BIO_printf(bio_err, "Lookup session: cache miss\n");
  3065. return NULL;
  3066. }
  3067. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  3068. {
  3069. simple_ssl_session *sess, *prev = NULL;
  3070. const unsigned char *id;
  3071. unsigned int idlen;
  3072. id = SSL_SESSION_get_id(session, &idlen);
  3073. for (sess = first; sess; sess = sess->next) {
  3074. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
  3075. if (prev)
  3076. prev->next = sess->next;
  3077. else
  3078. first = sess->next;
  3079. OPENSSL_free(sess->id);
  3080. OPENSSL_free(sess->der);
  3081. OPENSSL_free(sess);
  3082. return;
  3083. }
  3084. prev = sess;
  3085. }
  3086. }
  3087. static void init_session_cache_ctx(SSL_CTX *sctx)
  3088. {
  3089. SSL_CTX_set_session_cache_mode(sctx,
  3090. SSL_SESS_CACHE_NO_INTERNAL |
  3091. SSL_SESS_CACHE_SERVER);
  3092. SSL_CTX_sess_set_new_cb(sctx, add_session);
  3093. SSL_CTX_sess_set_get_cb(sctx, get_session);
  3094. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  3095. }
  3096. static void free_sessions(void)
  3097. {
  3098. simple_ssl_session *sess, *tsess;
  3099. for (sess = first; sess;) {
  3100. OPENSSL_free(sess->id);
  3101. OPENSSL_free(sess->der);
  3102. tsess = sess;
  3103. sess = sess->next;
  3104. OPENSSL_free(tsess);
  3105. }
  3106. first = NULL;
  3107. }