2
0

s_server.c 116 KB

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