s_server.c 114 KB

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