s_server.c 116 KB

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