s_server.c 114 KB

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