s_server.c 110 KB

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