s_server.c 105 KB

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