s_server.c 116 KB

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