s_server.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685
  1. /*
  2. * Copyright 1995-2022 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_is_dtls(ssl) && SSL_version(ssl) >= TLS1_3_VERSION) {
  118. /*
  119. * This callback is designed for use in (D)TLSv1.2 (or below). It is
  120. * possible to use a single callback for all protocol versions - but it
  121. * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
  122. * have 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 long int count_reads_callback(BIO *bio, int cmd, const char *argp,
  2056. int argi, long int argl, long int ret)
  2057. {
  2058. unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio);
  2059. switch (cmd) {
  2060. case BIO_CB_READ: /* No break here */
  2061. case BIO_CB_GETS:
  2062. if (p_counter != NULL)
  2063. ++*p_counter;
  2064. break;
  2065. default:
  2066. break;
  2067. }
  2068. if (s_debug) {
  2069. BIO_set_callback_arg(bio, (char *)bio_s_out);
  2070. ret = bio_dump_callback(bio, cmd, argp, argi, argl, ret);
  2071. BIO_set_callback_arg(bio, (char *)p_counter);
  2072. }
  2073. return ret;
  2074. }
  2075. static int sv_body(int s, int stype, int prot, unsigned char *context)
  2076. {
  2077. char *buf = NULL;
  2078. fd_set readfds;
  2079. int ret = 1, width;
  2080. int k, i;
  2081. unsigned long l;
  2082. SSL *con = NULL;
  2083. BIO *sbio;
  2084. struct timeval timeout;
  2085. #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
  2086. struct timeval *timeoutp;
  2087. #endif
  2088. #ifndef OPENSSL_NO_DTLS
  2089. # ifndef OPENSSL_NO_SCTP
  2090. int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP);
  2091. # else
  2092. int isdtls = (stype == SOCK_DGRAM);
  2093. # endif
  2094. #endif
  2095. buf = app_malloc(bufsize, "server buffer");
  2096. if (s_nbio) {
  2097. if (!BIO_socket_nbio(s, 1))
  2098. ERR_print_errors(bio_err);
  2099. else if (!s_quiet)
  2100. BIO_printf(bio_err, "Turned on non blocking io\n");
  2101. }
  2102. con = SSL_new(ctx);
  2103. if (con == NULL) {
  2104. ret = -1;
  2105. goto err;
  2106. }
  2107. if (s_tlsextdebug) {
  2108. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2109. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2110. }
  2111. if (context != NULL
  2112. && !SSL_set_session_id_context(con, context,
  2113. strlen((char *)context))) {
  2114. BIO_printf(bio_err, "Error setting session id context\n");
  2115. ret = -1;
  2116. goto err;
  2117. }
  2118. if (!SSL_clear(con)) {
  2119. BIO_printf(bio_err, "Error clearing SSL connection\n");
  2120. ret = -1;
  2121. goto err;
  2122. }
  2123. #ifndef OPENSSL_NO_DTLS
  2124. if (isdtls) {
  2125. # ifndef OPENSSL_NO_SCTP
  2126. if (prot == IPPROTO_SCTP)
  2127. sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE);
  2128. else
  2129. # endif
  2130. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  2131. if (enable_timeouts) {
  2132. timeout.tv_sec = 0;
  2133. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  2134. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  2135. timeout.tv_sec = 0;
  2136. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  2137. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  2138. }
  2139. if (socket_mtu) {
  2140. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  2141. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  2142. DTLS_get_link_min_mtu(con));
  2143. ret = -1;
  2144. BIO_free(sbio);
  2145. goto err;
  2146. }
  2147. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  2148. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  2149. BIO_printf(bio_err, "Failed to set MTU\n");
  2150. ret = -1;
  2151. BIO_free(sbio);
  2152. goto err;
  2153. }
  2154. } else
  2155. /* want to do MTU discovery */
  2156. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  2157. # ifndef OPENSSL_NO_SCTP
  2158. if (prot != IPPROTO_SCTP)
  2159. # endif
  2160. /* Turn on cookie exchange. Not necessary for SCTP */
  2161. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  2162. } else
  2163. #endif
  2164. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2165. if (sbio == NULL) {
  2166. BIO_printf(bio_err, "Unable to create BIO\n");
  2167. ERR_print_errors(bio_err);
  2168. goto err;
  2169. }
  2170. if (s_nbio_test) {
  2171. BIO *test;
  2172. test = BIO_new(BIO_f_nbio_test());
  2173. sbio = BIO_push(test, sbio);
  2174. }
  2175. SSL_set_bio(con, sbio, sbio);
  2176. SSL_set_accept_state(con);
  2177. /* SSL_set_fd(con,s); */
  2178. BIO_set_callback(SSL_get_rbio(con), count_reads_callback);
  2179. if (s_msg) {
  2180. #ifndef OPENSSL_NO_SSL_TRACE
  2181. if (s_msg == 2)
  2182. SSL_set_msg_callback(con, SSL_trace);
  2183. else
  2184. #endif
  2185. SSL_set_msg_callback(con, msg_cb);
  2186. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2187. }
  2188. if (s_tlsextdebug) {
  2189. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2190. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2191. }
  2192. if (early_data) {
  2193. int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR;
  2194. size_t readbytes;
  2195. while (edret != SSL_READ_EARLY_DATA_FINISH) {
  2196. for (;;) {
  2197. edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
  2198. if (edret != SSL_READ_EARLY_DATA_ERROR)
  2199. break;
  2200. switch (SSL_get_error(con, 0)) {
  2201. case SSL_ERROR_WANT_WRITE:
  2202. case SSL_ERROR_WANT_ASYNC:
  2203. case SSL_ERROR_WANT_READ:
  2204. /* Just keep trying - busy waiting */
  2205. continue;
  2206. default:
  2207. BIO_printf(bio_err, "Error reading early data\n");
  2208. ERR_print_errors(bio_err);
  2209. goto err;
  2210. }
  2211. }
  2212. if (readbytes > 0) {
  2213. if (write_header) {
  2214. BIO_printf(bio_s_out, "Early data received:\n");
  2215. write_header = 0;
  2216. }
  2217. raw_write_stdout(buf, (unsigned int)readbytes);
  2218. (void)BIO_flush(bio_s_out);
  2219. }
  2220. }
  2221. if (write_header) {
  2222. if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT)
  2223. BIO_printf(bio_s_out, "No early data received\n");
  2224. else
  2225. BIO_printf(bio_s_out, "Early data was rejected\n");
  2226. } else {
  2227. BIO_printf(bio_s_out, "\nEnd of early data\n");
  2228. }
  2229. if (SSL_is_init_finished(con))
  2230. print_connection_info(con);
  2231. }
  2232. if (fileno_stdin() > s)
  2233. width = fileno_stdin() + 1;
  2234. else
  2235. width = s + 1;
  2236. for (;;) {
  2237. int read_from_terminal;
  2238. int read_from_sslcon;
  2239. read_from_terminal = 0;
  2240. read_from_sslcon = SSL_has_pending(con)
  2241. || (async && SSL_waiting_for_async(con));
  2242. if (!read_from_sslcon) {
  2243. FD_ZERO(&readfds);
  2244. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
  2245. openssl_fdset(fileno_stdin(), &readfds);
  2246. #endif
  2247. openssl_fdset(s, &readfds);
  2248. /*
  2249. * Note: under VMS with SOCKETSHR the second parameter is
  2250. * currently of type (int *) whereas under other systems it is
  2251. * (void *) if you don't have a cast it will choke the compiler:
  2252. * if you do have a cast then you can either go for (int *) or
  2253. * (void *).
  2254. */
  2255. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  2256. /*
  2257. * Under DOS (non-djgpp) and Windows we can't select on stdin:
  2258. * only on sockets. As a workaround we timeout the select every
  2259. * second and check for any keypress. In a proper Windows
  2260. * application we wouldn't do this because it is inefficient.
  2261. */
  2262. timeout.tv_sec = 1;
  2263. timeout.tv_usec = 0;
  2264. i = select(width, (void *)&readfds, NULL, NULL, &timeout);
  2265. if (has_stdin_waiting())
  2266. read_from_terminal = 1;
  2267. if ((i < 0) || (!i && !read_from_terminal))
  2268. continue;
  2269. #else
  2270. if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
  2271. timeoutp = &timeout;
  2272. else
  2273. timeoutp = NULL;
  2274. i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
  2275. if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0)
  2276. BIO_printf(bio_err, "TIMEOUT occurred\n");
  2277. if (i <= 0)
  2278. continue;
  2279. if (FD_ISSET(fileno_stdin(), &readfds))
  2280. read_from_terminal = 1;
  2281. #endif
  2282. if (FD_ISSET(s, &readfds))
  2283. read_from_sslcon = 1;
  2284. }
  2285. if (read_from_terminal) {
  2286. if (s_crlf) {
  2287. int j, lf_num;
  2288. i = raw_read_stdin(buf, bufsize / 2);
  2289. lf_num = 0;
  2290. /* both loops are skipped when i <= 0 */
  2291. for (j = 0; j < i; j++)
  2292. if (buf[j] == '\n')
  2293. lf_num++;
  2294. for (j = i - 1; j >= 0; j--) {
  2295. buf[j + lf_num] = buf[j];
  2296. if (buf[j] == '\n') {
  2297. lf_num--;
  2298. i++;
  2299. buf[j + lf_num] = '\r';
  2300. }
  2301. }
  2302. assert(lf_num == 0);
  2303. } else {
  2304. i = raw_read_stdin(buf, bufsize);
  2305. }
  2306. if (!s_quiet && !s_brief) {
  2307. if ((i <= 0) || (buf[0] == 'Q')) {
  2308. BIO_printf(bio_s_out, "DONE\n");
  2309. (void)BIO_flush(bio_s_out);
  2310. BIO_closesocket(s);
  2311. close_accept_socket();
  2312. ret = -11;
  2313. goto err;
  2314. }
  2315. if ((i <= 0) || (buf[0] == 'q')) {
  2316. BIO_printf(bio_s_out, "DONE\n");
  2317. (void)BIO_flush(bio_s_out);
  2318. if (SSL_version(con) != DTLS1_VERSION)
  2319. BIO_closesocket(s);
  2320. /*
  2321. * close_accept_socket(); ret= -11;
  2322. */
  2323. goto err;
  2324. }
  2325. #ifndef OPENSSL_NO_HEARTBEATS
  2326. if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2327. BIO_printf(bio_err, "HEARTBEATING\n");
  2328. SSL_heartbeat(con);
  2329. i = 0;
  2330. continue;
  2331. }
  2332. #endif
  2333. if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2334. SSL_renegotiate(con);
  2335. i = SSL_do_handshake(con);
  2336. printf("SSL_do_handshake -> %d\n", i);
  2337. i = 0; /* 13; */
  2338. continue;
  2339. }
  2340. if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2341. SSL_set_verify(con,
  2342. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2343. NULL);
  2344. SSL_renegotiate(con);
  2345. i = SSL_do_handshake(con);
  2346. printf("SSL_do_handshake -> %d\n", i);
  2347. i = 0; /* 13; */
  2348. continue;
  2349. }
  2350. if ((buf[0] == 'K' || buf[0] == 'k')
  2351. && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2352. SSL_key_update(con, buf[0] == 'K' ?
  2353. SSL_KEY_UPDATE_REQUESTED
  2354. : SSL_KEY_UPDATE_NOT_REQUESTED);
  2355. i = SSL_do_handshake(con);
  2356. printf("SSL_do_handshake -> %d\n", i);
  2357. i = 0;
  2358. continue;
  2359. }
  2360. if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2361. SSL_set_verify(con, SSL_VERIFY_PEER, NULL);
  2362. i = SSL_verify_client_post_handshake(con);
  2363. if (i == 0) {
  2364. printf("Failed to initiate request\n");
  2365. ERR_print_errors(bio_err);
  2366. } else {
  2367. i = SSL_do_handshake(con);
  2368. printf("SSL_do_handshake -> %d\n", i);
  2369. i = 0;
  2370. }
  2371. continue;
  2372. }
  2373. if (buf[0] == 'P') {
  2374. static const char *str = "Lets print some clear text\n";
  2375. BIO_write(SSL_get_wbio(con), str, strlen(str));
  2376. }
  2377. if (buf[0] == 'S') {
  2378. print_stats(bio_s_out, SSL_get_SSL_CTX(con));
  2379. }
  2380. }
  2381. #ifdef CHARSET_EBCDIC
  2382. ebcdic2ascii(buf, buf, i);
  2383. #endif
  2384. l = k = 0;
  2385. for (;;) {
  2386. /* should do a select for the write */
  2387. #ifdef RENEG
  2388. static count = 0;
  2389. if (++count == 100) {
  2390. count = 0;
  2391. SSL_renegotiate(con);
  2392. }
  2393. #endif
  2394. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2395. #ifndef OPENSSL_NO_SRP
  2396. while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
  2397. BIO_printf(bio_s_out, "LOOKUP renego during write\n");
  2398. SRP_user_pwd_free(srp_callback_parm.user);
  2399. srp_callback_parm.user =
  2400. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2401. srp_callback_parm.login);
  2402. if (srp_callback_parm.user)
  2403. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2404. srp_callback_parm.user->info);
  2405. else
  2406. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2407. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2408. }
  2409. #endif
  2410. switch (SSL_get_error(con, k)) {
  2411. case SSL_ERROR_NONE:
  2412. break;
  2413. case SSL_ERROR_WANT_ASYNC:
  2414. BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
  2415. (void)BIO_flush(bio_s_out);
  2416. wait_for_async(con);
  2417. break;
  2418. case SSL_ERROR_WANT_WRITE:
  2419. case SSL_ERROR_WANT_READ:
  2420. case SSL_ERROR_WANT_X509_LOOKUP:
  2421. BIO_printf(bio_s_out, "Write BLOCK\n");
  2422. (void)BIO_flush(bio_s_out);
  2423. break;
  2424. case SSL_ERROR_WANT_ASYNC_JOB:
  2425. /*
  2426. * This shouldn't ever happen in s_server. Treat as an error
  2427. */
  2428. case SSL_ERROR_SYSCALL:
  2429. case SSL_ERROR_SSL:
  2430. BIO_printf(bio_s_out, "ERROR\n");
  2431. (void)BIO_flush(bio_s_out);
  2432. ERR_print_errors(bio_err);
  2433. ret = 1;
  2434. goto err;
  2435. /* break; */
  2436. case SSL_ERROR_ZERO_RETURN:
  2437. BIO_printf(bio_s_out, "DONE\n");
  2438. (void)BIO_flush(bio_s_out);
  2439. ret = 1;
  2440. goto err;
  2441. }
  2442. if (k > 0) {
  2443. l += k;
  2444. i -= k;
  2445. }
  2446. if (i <= 0)
  2447. break;
  2448. }
  2449. }
  2450. if (read_from_sslcon) {
  2451. /*
  2452. * init_ssl_connection handles all async events itself so if we're
  2453. * waiting for async then we shouldn't go back into
  2454. * init_ssl_connection
  2455. */
  2456. if ((!async || !SSL_waiting_for_async(con))
  2457. && !SSL_is_init_finished(con)) {
  2458. /*
  2459. * Count number of reads during init_ssl_connection.
  2460. * It helps us to distinguish configuration errors from errors
  2461. * caused by a client.
  2462. */
  2463. unsigned int read_counter = 0;
  2464. BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter);
  2465. i = init_ssl_connection(con);
  2466. BIO_set_callback_arg(SSL_get_rbio(con), NULL);
  2467. /*
  2468. * If initialization fails without reads, then
  2469. * there was a fatal error in configuration.
  2470. */
  2471. if (i <= 0 && read_counter == 0) {
  2472. ret = -1;
  2473. goto err;
  2474. }
  2475. if (i < 0) {
  2476. ret = 0;
  2477. goto err;
  2478. } else if (i == 0) {
  2479. ret = 1;
  2480. goto err;
  2481. }
  2482. } else {
  2483. again:
  2484. i = SSL_read(con, (char *)buf, bufsize);
  2485. #ifndef OPENSSL_NO_SRP
  2486. while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2487. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2488. SRP_user_pwd_free(srp_callback_parm.user);
  2489. srp_callback_parm.user =
  2490. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2491. srp_callback_parm.login);
  2492. if (srp_callback_parm.user)
  2493. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2494. srp_callback_parm.user->info);
  2495. else
  2496. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2497. i = SSL_read(con, (char *)buf, bufsize);
  2498. }
  2499. #endif
  2500. switch (SSL_get_error(con, i)) {
  2501. case SSL_ERROR_NONE:
  2502. #ifdef CHARSET_EBCDIC
  2503. ascii2ebcdic(buf, buf, i);
  2504. #endif
  2505. raw_write_stdout(buf, (unsigned int)i);
  2506. (void)BIO_flush(bio_s_out);
  2507. if (SSL_has_pending(con))
  2508. goto again;
  2509. break;
  2510. case SSL_ERROR_WANT_ASYNC:
  2511. BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
  2512. (void)BIO_flush(bio_s_out);
  2513. wait_for_async(con);
  2514. break;
  2515. case SSL_ERROR_WANT_WRITE:
  2516. case SSL_ERROR_WANT_READ:
  2517. BIO_printf(bio_s_out, "Read BLOCK\n");
  2518. (void)BIO_flush(bio_s_out);
  2519. break;
  2520. case SSL_ERROR_WANT_ASYNC_JOB:
  2521. /*
  2522. * This shouldn't ever happen in s_server. Treat as an error
  2523. */
  2524. case SSL_ERROR_SYSCALL:
  2525. case SSL_ERROR_SSL:
  2526. BIO_printf(bio_s_out, "ERROR\n");
  2527. (void)BIO_flush(bio_s_out);
  2528. ERR_print_errors(bio_err);
  2529. ret = 1;
  2530. goto err;
  2531. case SSL_ERROR_ZERO_RETURN:
  2532. BIO_printf(bio_s_out, "DONE\n");
  2533. (void)BIO_flush(bio_s_out);
  2534. ret = 1;
  2535. goto err;
  2536. }
  2537. }
  2538. }
  2539. }
  2540. err:
  2541. if (con != NULL) {
  2542. BIO_printf(bio_s_out, "shutting down SSL\n");
  2543. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2544. SSL_free(con);
  2545. }
  2546. BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
  2547. OPENSSL_clear_free(buf, bufsize);
  2548. return ret;
  2549. }
  2550. static void close_accept_socket(void)
  2551. {
  2552. BIO_printf(bio_err, "shutdown accept socket\n");
  2553. if (accept_socket >= 0) {
  2554. BIO_closesocket(accept_socket);
  2555. }
  2556. }
  2557. static int is_retryable(SSL *con, int i)
  2558. {
  2559. int err = SSL_get_error(con, i);
  2560. /* If it's not a fatal error, it must be retryable */
  2561. return (err != SSL_ERROR_SSL)
  2562. && (err != SSL_ERROR_SYSCALL)
  2563. && (err != SSL_ERROR_ZERO_RETURN);
  2564. }
  2565. static int init_ssl_connection(SSL *con)
  2566. {
  2567. int i;
  2568. long verify_err;
  2569. int retry = 0;
  2570. if (dtlslisten || stateless) {
  2571. BIO_ADDR *client = NULL;
  2572. if (dtlslisten) {
  2573. if ((client = BIO_ADDR_new()) == NULL) {
  2574. BIO_printf(bio_err, "ERROR - memory\n");
  2575. return 0;
  2576. }
  2577. i = DTLSv1_listen(con, client);
  2578. } else {
  2579. i = SSL_stateless(con);
  2580. }
  2581. if (i > 0) {
  2582. BIO *wbio;
  2583. int fd = -1;
  2584. if (dtlslisten) {
  2585. wbio = SSL_get_wbio(con);
  2586. if (wbio) {
  2587. BIO_get_fd(wbio, &fd);
  2588. }
  2589. if (!wbio || BIO_connect(fd, client, 0) == 0) {
  2590. BIO_printf(bio_err, "ERROR - unable to connect\n");
  2591. BIO_ADDR_free(client);
  2592. return 0;
  2593. }
  2594. (void)BIO_ctrl_set_connected(wbio, client);
  2595. BIO_ADDR_free(client);
  2596. dtlslisten = 0;
  2597. } else {
  2598. stateless = 0;
  2599. }
  2600. i = SSL_accept(con);
  2601. } else {
  2602. BIO_ADDR_free(client);
  2603. }
  2604. } else {
  2605. do {
  2606. i = SSL_accept(con);
  2607. if (i <= 0)
  2608. retry = is_retryable(con, i);
  2609. #ifdef CERT_CB_TEST_RETRY
  2610. {
  2611. while (i <= 0
  2612. && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
  2613. && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
  2614. BIO_printf(bio_err,
  2615. "LOOKUP from certificate callback during accept\n");
  2616. i = SSL_accept(con);
  2617. if (i <= 0)
  2618. retry = is_retryable(con, i);
  2619. }
  2620. }
  2621. #endif
  2622. #ifndef OPENSSL_NO_SRP
  2623. while (i <= 0
  2624. && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2625. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2626. srp_callback_parm.login);
  2627. SRP_user_pwd_free(srp_callback_parm.user);
  2628. srp_callback_parm.user =
  2629. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2630. srp_callback_parm.login);
  2631. if (srp_callback_parm.user)
  2632. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2633. srp_callback_parm.user->info);
  2634. else
  2635. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2636. i = SSL_accept(con);
  2637. if (i <= 0)
  2638. retry = is_retryable(con, i);
  2639. }
  2640. #endif
  2641. } while (i < 0 && SSL_waiting_for_async(con));
  2642. }
  2643. if (i <= 0) {
  2644. if (((dtlslisten || stateless) && i == 0)
  2645. || (!dtlslisten && !stateless && retry)) {
  2646. BIO_printf(bio_s_out, "DELAY\n");
  2647. return 1;
  2648. }
  2649. BIO_printf(bio_err, "ERROR\n");
  2650. verify_err = SSL_get_verify_result(con);
  2651. if (verify_err != X509_V_OK) {
  2652. BIO_printf(bio_err, "verify error:%s\n",
  2653. X509_verify_cert_error_string(verify_err));
  2654. }
  2655. /* Always print any error messages */
  2656. ERR_print_errors(bio_err);
  2657. return 0;
  2658. }
  2659. print_connection_info(con);
  2660. return 1;
  2661. }
  2662. static void print_connection_info(SSL *con)
  2663. {
  2664. const char *str;
  2665. X509 *peer;
  2666. char buf[BUFSIZ];
  2667. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2668. const unsigned char *next_proto_neg;
  2669. unsigned next_proto_neg_len;
  2670. #endif
  2671. unsigned char *exportedkeymat;
  2672. int i;
  2673. if (s_brief)
  2674. print_ssl_summary(con);
  2675. PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
  2676. peer = SSL_get_peer_certificate(con);
  2677. if (peer != NULL) {
  2678. BIO_printf(bio_s_out, "Client certificate\n");
  2679. PEM_write_bio_X509(bio_s_out, peer);
  2680. dump_cert_text(bio_s_out, peer);
  2681. X509_free(peer);
  2682. peer = NULL;
  2683. }
  2684. if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL)
  2685. BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
  2686. str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2687. ssl_print_sigalgs(bio_s_out, con);
  2688. #ifndef OPENSSL_NO_EC
  2689. ssl_print_point_formats(bio_s_out, con);
  2690. ssl_print_groups(bio_s_out, con, 0);
  2691. #endif
  2692. print_ca_names(bio_s_out, con);
  2693. BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
  2694. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2695. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2696. if (next_proto_neg) {
  2697. BIO_printf(bio_s_out, "NEXTPROTO is ");
  2698. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2699. BIO_printf(bio_s_out, "\n");
  2700. }
  2701. #endif
  2702. #ifndef OPENSSL_NO_SRTP
  2703. {
  2704. SRTP_PROTECTION_PROFILE *srtp_profile
  2705. = SSL_get_selected_srtp_profile(con);
  2706. if (srtp_profile)
  2707. BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
  2708. srtp_profile->name);
  2709. }
  2710. #endif
  2711. if (SSL_session_reused(con))
  2712. BIO_printf(bio_s_out, "Reused session-id\n");
  2713. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2714. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2715. if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION))
  2716. BIO_printf(bio_s_out, "Renegotiation is DISABLED\n");
  2717. if (keymatexportlabel != NULL) {
  2718. BIO_printf(bio_s_out, "Keying material exporter:\n");
  2719. BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
  2720. BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
  2721. exportedkeymat = app_malloc(keymatexportlen, "export key");
  2722. if (!SSL_export_keying_material(con, exportedkeymat,
  2723. keymatexportlen,
  2724. keymatexportlabel,
  2725. strlen(keymatexportlabel),
  2726. NULL, 0, 0)) {
  2727. BIO_printf(bio_s_out, " Error\n");
  2728. } else {
  2729. BIO_printf(bio_s_out, " Keying material: ");
  2730. for (i = 0; i < keymatexportlen; i++)
  2731. BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
  2732. BIO_printf(bio_s_out, "\n");
  2733. }
  2734. OPENSSL_free(exportedkeymat);
  2735. }
  2736. (void)BIO_flush(bio_s_out);
  2737. }
  2738. #ifndef OPENSSL_NO_DH
  2739. static DH *load_dh_param(const char *dhfile)
  2740. {
  2741. DH *ret = NULL;
  2742. BIO *bio;
  2743. if ((bio = BIO_new_file(dhfile, "r")) == NULL)
  2744. goto err;
  2745. ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2746. err:
  2747. BIO_free(bio);
  2748. return ret;
  2749. }
  2750. #endif
  2751. static int www_body(int s, int stype, int prot, unsigned char *context)
  2752. {
  2753. char *buf = NULL;
  2754. int ret = 1;
  2755. int i, j, k, dot;
  2756. SSL *con;
  2757. const SSL_CIPHER *c;
  2758. BIO *io, *ssl_bio, *sbio;
  2759. #ifdef RENEG
  2760. int total_bytes = 0;
  2761. #endif
  2762. int width;
  2763. fd_set readfds;
  2764. /* Set width for a select call if needed */
  2765. width = s + 1;
  2766. buf = app_malloc(bufsize, "server www buffer");
  2767. io = BIO_new(BIO_f_buffer());
  2768. ssl_bio = BIO_new(BIO_f_ssl());
  2769. if ((io == NULL) || (ssl_bio == NULL))
  2770. goto err;
  2771. if (s_nbio) {
  2772. if (!BIO_socket_nbio(s, 1))
  2773. ERR_print_errors(bio_err);
  2774. else if (!s_quiet)
  2775. BIO_printf(bio_err, "Turned on non blocking io\n");
  2776. }
  2777. /* lets make the output buffer a reasonable size */
  2778. if (!BIO_set_write_buffer_size(io, bufsize))
  2779. goto err;
  2780. if ((con = SSL_new(ctx)) == NULL)
  2781. goto err;
  2782. if (s_tlsextdebug) {
  2783. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2784. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2785. }
  2786. if (context != NULL
  2787. && !SSL_set_session_id_context(con, context,
  2788. strlen((char *)context))) {
  2789. SSL_free(con);
  2790. goto err;
  2791. }
  2792. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2793. if (s_nbio_test) {
  2794. BIO *test;
  2795. test = BIO_new(BIO_f_nbio_test());
  2796. sbio = BIO_push(test, sbio);
  2797. }
  2798. SSL_set_bio(con, sbio, sbio);
  2799. SSL_set_accept_state(con);
  2800. /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
  2801. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2802. BIO_push(io, ssl_bio);
  2803. #ifdef CHARSET_EBCDIC
  2804. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2805. #endif
  2806. if (s_debug) {
  2807. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2808. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2809. }
  2810. if (s_msg) {
  2811. #ifndef OPENSSL_NO_SSL_TRACE
  2812. if (s_msg == 2)
  2813. SSL_set_msg_callback(con, SSL_trace);
  2814. else
  2815. #endif
  2816. SSL_set_msg_callback(con, msg_cb);
  2817. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2818. }
  2819. for (;;) {
  2820. i = BIO_gets(io, buf, bufsize - 1);
  2821. if (i < 0) { /* error */
  2822. if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
  2823. if (!s_quiet)
  2824. ERR_print_errors(bio_err);
  2825. goto err;
  2826. } else {
  2827. BIO_printf(bio_s_out, "read R BLOCK\n");
  2828. #ifndef OPENSSL_NO_SRP
  2829. if (BIO_should_io_special(io)
  2830. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2831. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2832. SRP_user_pwd_free(srp_callback_parm.user);
  2833. srp_callback_parm.user =
  2834. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2835. srp_callback_parm.login);
  2836. if (srp_callback_parm.user)
  2837. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2838. srp_callback_parm.user->info);
  2839. else
  2840. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2841. continue;
  2842. }
  2843. #endif
  2844. #if !defined(OPENSSL_SYS_MSDOS)
  2845. sleep(1);
  2846. #endif
  2847. continue;
  2848. }
  2849. } else if (i == 0) { /* end of input */
  2850. ret = 1;
  2851. goto end;
  2852. }
  2853. /* else we have data */
  2854. if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
  2855. ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
  2856. char *p;
  2857. X509 *peer = NULL;
  2858. STACK_OF(SSL_CIPHER) *sk;
  2859. static const char *space = " ";
  2860. if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
  2861. if (strncmp("GET /renegcert", buf, 14) == 0)
  2862. SSL_set_verify(con,
  2863. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2864. NULL);
  2865. i = SSL_renegotiate(con);
  2866. BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
  2867. /* Send the HelloRequest */
  2868. i = SSL_do_handshake(con);
  2869. if (i <= 0) {
  2870. BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
  2871. SSL_get_error(con, i));
  2872. ERR_print_errors(bio_err);
  2873. goto err;
  2874. }
  2875. /* Wait for a ClientHello to come back */
  2876. FD_ZERO(&readfds);
  2877. openssl_fdset(s, &readfds);
  2878. i = select(width, (void *)&readfds, NULL, NULL, NULL);
  2879. if (i <= 0 || !FD_ISSET(s, &readfds)) {
  2880. BIO_printf(bio_s_out,
  2881. "Error waiting for client response\n");
  2882. ERR_print_errors(bio_err);
  2883. goto err;
  2884. }
  2885. /*
  2886. * We're not actually expecting any data here and we ignore
  2887. * any that is sent. This is just to force the handshake that
  2888. * we're expecting to come from the client. If they haven't
  2889. * sent one there's not much we can do.
  2890. */
  2891. BIO_gets(io, buf, bufsize - 1);
  2892. }
  2893. BIO_puts(io,
  2894. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2895. BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2896. BIO_puts(io, "<pre>\n");
  2897. /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
  2898. BIO_puts(io, "\n");
  2899. for (i = 0; i < local_argc; i++) {
  2900. const char *myp;
  2901. for (myp = local_argv[i]; *myp; myp++)
  2902. switch (*myp) {
  2903. case '<':
  2904. BIO_puts(io, "&lt;");
  2905. break;
  2906. case '>':
  2907. BIO_puts(io, "&gt;");
  2908. break;
  2909. case '&':
  2910. BIO_puts(io, "&amp;");
  2911. break;
  2912. default:
  2913. BIO_write(io, myp, 1);
  2914. break;
  2915. }
  2916. BIO_write(io, " ", 1);
  2917. }
  2918. BIO_puts(io, "\n");
  2919. BIO_printf(io,
  2920. "Secure Renegotiation IS%s supported\n",
  2921. SSL_get_secure_renegotiation_support(con) ?
  2922. "" : " NOT");
  2923. /*
  2924. * The following is evil and should not really be done
  2925. */
  2926. BIO_printf(io, "Ciphers supported in s_server binary\n");
  2927. sk = SSL_get_ciphers(con);
  2928. j = sk_SSL_CIPHER_num(sk);
  2929. for (i = 0; i < j; i++) {
  2930. c = sk_SSL_CIPHER_value(sk, i);
  2931. BIO_printf(io, "%-11s:%-25s ",
  2932. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2933. if ((((i + 1) % 2) == 0) && (i + 1 != j))
  2934. BIO_puts(io, "\n");
  2935. }
  2936. BIO_puts(io, "\n");
  2937. p = SSL_get_shared_ciphers(con, buf, bufsize);
  2938. if (p != NULL) {
  2939. BIO_printf(io,
  2940. "---\nCiphers common between both SSL end points:\n");
  2941. j = i = 0;
  2942. while (*p) {
  2943. if (*p == ':') {
  2944. BIO_write(io, space, 26 - j);
  2945. i++;
  2946. j = 0;
  2947. BIO_write(io, ((i % 3) ? " " : "\n"), 1);
  2948. } else {
  2949. BIO_write(io, p, 1);
  2950. j++;
  2951. }
  2952. p++;
  2953. }
  2954. BIO_puts(io, "\n");
  2955. }
  2956. ssl_print_sigalgs(io, con);
  2957. #ifndef OPENSSL_NO_EC
  2958. ssl_print_groups(io, con, 0);
  2959. #endif
  2960. print_ca_names(io, con);
  2961. BIO_printf(io, (SSL_session_reused(con)
  2962. ? "---\nReused, " : "---\nNew, "));
  2963. c = SSL_get_current_cipher(con);
  2964. BIO_printf(io, "%s, Cipher is %s\n",
  2965. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2966. SSL_SESSION_print(io, SSL_get_session(con));
  2967. BIO_printf(io, "---\n");
  2968. print_stats(io, SSL_get_SSL_CTX(con));
  2969. BIO_printf(io, "---\n");
  2970. peer = SSL_get_peer_certificate(con);
  2971. if (peer != NULL) {
  2972. BIO_printf(io, "Client certificate\n");
  2973. X509_print(io, peer);
  2974. PEM_write_bio_X509(io, peer);
  2975. X509_free(peer);
  2976. peer = NULL;
  2977. } else {
  2978. BIO_puts(io, "no client certificate available\n");
  2979. }
  2980. BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n");
  2981. break;
  2982. } else if ((www == 2 || www == 3)
  2983. && (strncmp("GET /", buf, 5) == 0)) {
  2984. BIO *file;
  2985. char *p, *e;
  2986. static const char *text =
  2987. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2988. /* skip the '/' */
  2989. p = &(buf[5]);
  2990. dot = 1;
  2991. for (e = p; *e != '\0'; e++) {
  2992. if (e[0] == ' ')
  2993. break;
  2994. if (e[0] == ':') {
  2995. /* Windows drive. We treat this the same way as ".." */
  2996. dot = -1;
  2997. break;
  2998. }
  2999. switch (dot) {
  3000. case 1:
  3001. dot = (e[0] == '.') ? 2 : 0;
  3002. break;
  3003. case 2:
  3004. dot = (e[0] == '.') ? 3 : 0;
  3005. break;
  3006. case 3:
  3007. dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
  3008. break;
  3009. }
  3010. if (dot == 0)
  3011. dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
  3012. }
  3013. dot = (dot == 3) || (dot == -1); /* filename contains ".."
  3014. * component */
  3015. if (*e == '\0') {
  3016. BIO_puts(io, text);
  3017. BIO_printf(io, "'%s' is an invalid file name\r\n", p);
  3018. break;
  3019. }
  3020. *e = '\0';
  3021. if (dot) {
  3022. BIO_puts(io, text);
  3023. BIO_printf(io, "'%s' contains '..' or ':'\r\n", p);
  3024. break;
  3025. }
  3026. if (*p == '/' || *p == '\\') {
  3027. BIO_puts(io, text);
  3028. BIO_printf(io, "'%s' is an invalid path\r\n", p);
  3029. break;
  3030. }
  3031. /* if a directory, do the index thang */
  3032. if (app_isdir(p) > 0) {
  3033. BIO_puts(io, text);
  3034. BIO_printf(io, "'%s' is a directory\r\n", p);
  3035. break;
  3036. }
  3037. if ((file = BIO_new_file(p, "r")) == NULL) {
  3038. BIO_puts(io, text);
  3039. BIO_printf(io, "Error opening '%s'\r\n", p);
  3040. ERR_print_errors(io);
  3041. break;
  3042. }
  3043. if (!s_quiet)
  3044. BIO_printf(bio_err, "FILE:%s\n", p);
  3045. if (www == 2) {
  3046. i = strlen(p);
  3047. if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
  3048. ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
  3049. ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
  3050. BIO_puts(io,
  3051. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  3052. else
  3053. BIO_puts(io,
  3054. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  3055. }
  3056. /* send the file */
  3057. for (;;) {
  3058. i = BIO_read(file, buf, bufsize);
  3059. if (i <= 0)
  3060. break;
  3061. #ifdef RENEG
  3062. total_bytes += i;
  3063. BIO_printf(bio_err, "%d\n", i);
  3064. if (total_bytes > 3 * 1024) {
  3065. total_bytes = 0;
  3066. BIO_printf(bio_err, "RENEGOTIATE\n");
  3067. SSL_renegotiate(con);
  3068. }
  3069. #endif
  3070. for (j = 0; j < i;) {
  3071. #ifdef RENEG
  3072. static count = 0;
  3073. if (++count == 13) {
  3074. SSL_renegotiate(con);
  3075. }
  3076. #endif
  3077. k = BIO_write(io, &(buf[j]), i - j);
  3078. if (k <= 0) {
  3079. if (!BIO_should_retry(io)
  3080. && !SSL_waiting_for_async(con))
  3081. goto write_error;
  3082. else {
  3083. BIO_printf(bio_s_out, "rwrite W BLOCK\n");
  3084. }
  3085. } else {
  3086. j += k;
  3087. }
  3088. }
  3089. }
  3090. write_error:
  3091. BIO_free(file);
  3092. break;
  3093. }
  3094. }
  3095. for (;;) {
  3096. i = (int)BIO_flush(io);
  3097. if (i <= 0) {
  3098. if (!BIO_should_retry(io))
  3099. break;
  3100. } else
  3101. break;
  3102. }
  3103. end:
  3104. /* make sure we re-use sessions */
  3105. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  3106. err:
  3107. OPENSSL_free(buf);
  3108. BIO_free_all(io);
  3109. return ret;
  3110. }
  3111. static int rev_body(int s, int stype, int prot, unsigned char *context)
  3112. {
  3113. char *buf = NULL;
  3114. int i;
  3115. int ret = 1;
  3116. SSL *con;
  3117. BIO *io, *ssl_bio, *sbio;
  3118. buf = app_malloc(bufsize, "server rev buffer");
  3119. io = BIO_new(BIO_f_buffer());
  3120. ssl_bio = BIO_new(BIO_f_ssl());
  3121. if ((io == NULL) || (ssl_bio == NULL))
  3122. goto err;
  3123. /* lets make the output buffer a reasonable size */
  3124. if (!BIO_set_write_buffer_size(io, bufsize))
  3125. goto err;
  3126. if ((con = SSL_new(ctx)) == NULL)
  3127. goto err;
  3128. if (s_tlsextdebug) {
  3129. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  3130. SSL_set_tlsext_debug_arg(con, bio_s_out);
  3131. }
  3132. if (context != NULL
  3133. && !SSL_set_session_id_context(con, context,
  3134. strlen((char *)context))) {
  3135. SSL_free(con);
  3136. ERR_print_errors(bio_err);
  3137. goto err;
  3138. }
  3139. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  3140. SSL_set_bio(con, sbio, sbio);
  3141. SSL_set_accept_state(con);
  3142. /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
  3143. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  3144. BIO_push(io, ssl_bio);
  3145. #ifdef CHARSET_EBCDIC
  3146. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  3147. #endif
  3148. if (s_debug) {
  3149. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  3150. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  3151. }
  3152. if (s_msg) {
  3153. #ifndef OPENSSL_NO_SSL_TRACE
  3154. if (s_msg == 2)
  3155. SSL_set_msg_callback(con, SSL_trace);
  3156. else
  3157. #endif
  3158. SSL_set_msg_callback(con, msg_cb);
  3159. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  3160. }
  3161. for (;;) {
  3162. i = BIO_do_handshake(io);
  3163. if (i > 0)
  3164. break;
  3165. if (!BIO_should_retry(io)) {
  3166. BIO_puts(bio_err, "CONNECTION FAILURE\n");
  3167. ERR_print_errors(bio_err);
  3168. goto end;
  3169. }
  3170. #ifndef OPENSSL_NO_SRP
  3171. if (BIO_should_io_special(io)
  3172. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3173. BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
  3174. SRP_user_pwd_free(srp_callback_parm.user);
  3175. srp_callback_parm.user =
  3176. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3177. srp_callback_parm.login);
  3178. if (srp_callback_parm.user)
  3179. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3180. srp_callback_parm.user->info);
  3181. else
  3182. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3183. continue;
  3184. }
  3185. #endif
  3186. }
  3187. BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
  3188. print_ssl_summary(con);
  3189. for (;;) {
  3190. i = BIO_gets(io, buf, bufsize - 1);
  3191. if (i < 0) { /* error */
  3192. if (!BIO_should_retry(io)) {
  3193. if (!s_quiet)
  3194. ERR_print_errors(bio_err);
  3195. goto err;
  3196. } else {
  3197. BIO_printf(bio_s_out, "read R BLOCK\n");
  3198. #ifndef OPENSSL_NO_SRP
  3199. if (BIO_should_io_special(io)
  3200. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3201. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  3202. SRP_user_pwd_free(srp_callback_parm.user);
  3203. srp_callback_parm.user =
  3204. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3205. srp_callback_parm.login);
  3206. if (srp_callback_parm.user)
  3207. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3208. srp_callback_parm.user->info);
  3209. else
  3210. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3211. continue;
  3212. }
  3213. #endif
  3214. #if !defined(OPENSSL_SYS_MSDOS)
  3215. sleep(1);
  3216. #endif
  3217. continue;
  3218. }
  3219. } else if (i == 0) { /* end of input */
  3220. ret = 1;
  3221. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3222. goto end;
  3223. } else {
  3224. char *p = buf + i - 1;
  3225. while (i && (*p == '\n' || *p == '\r')) {
  3226. p--;
  3227. i--;
  3228. }
  3229. if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {
  3230. ret = 1;
  3231. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3232. goto end;
  3233. }
  3234. BUF_reverse((unsigned char *)buf, NULL, i);
  3235. buf[i] = '\n';
  3236. BIO_write(io, buf, i + 1);
  3237. for (;;) {
  3238. i = BIO_flush(io);
  3239. if (i > 0)
  3240. break;
  3241. if (!BIO_should_retry(io))
  3242. goto end;
  3243. }
  3244. }
  3245. }
  3246. end:
  3247. /* make sure we re-use sessions */
  3248. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  3249. err:
  3250. OPENSSL_free(buf);
  3251. BIO_free_all(io);
  3252. return ret;
  3253. }
  3254. #define MAX_SESSION_ID_ATTEMPTS 10
  3255. static int generate_session_id(SSL *ssl, unsigned char *id,
  3256. unsigned int *id_len)
  3257. {
  3258. unsigned int count = 0;
  3259. do {
  3260. if (RAND_bytes(id, *id_len) <= 0)
  3261. return 0;
  3262. /*
  3263. * Prefix the session_id with the required prefix. NB: If our prefix
  3264. * is too long, clip it - but there will be worse effects anyway, eg.
  3265. * the server could only possibly create 1 session ID (ie. the
  3266. * prefix!) so all future session negotiations will fail due to
  3267. * conflicts.
  3268. */
  3269. memcpy(id, session_id_prefix,
  3270. (strlen(session_id_prefix) < *id_len) ?
  3271. strlen(session_id_prefix) : *id_len);
  3272. }
  3273. while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  3274. (++count < MAX_SESSION_ID_ATTEMPTS));
  3275. if (count >= MAX_SESSION_ID_ATTEMPTS)
  3276. return 0;
  3277. return 1;
  3278. }
  3279. /*
  3280. * By default s_server uses an in-memory cache which caches SSL_SESSION
  3281. * structures without any serialisation. This hides some bugs which only
  3282. * become apparent in deployed servers. By implementing a basic external
  3283. * session cache some issues can be debugged using s_server.
  3284. */
  3285. typedef struct simple_ssl_session_st {
  3286. unsigned char *id;
  3287. unsigned int idlen;
  3288. unsigned char *der;
  3289. int derlen;
  3290. struct simple_ssl_session_st *next;
  3291. } simple_ssl_session;
  3292. static simple_ssl_session *first = NULL;
  3293. static int add_session(SSL *ssl, SSL_SESSION *session)
  3294. {
  3295. simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
  3296. unsigned char *p;
  3297. SSL_SESSION_get_id(session, &sess->idlen);
  3298. sess->derlen = i2d_SSL_SESSION(session, NULL);
  3299. if (sess->derlen < 0) {
  3300. BIO_printf(bio_err, "Error encoding session\n");
  3301. OPENSSL_free(sess);
  3302. return 0;
  3303. }
  3304. sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
  3305. sess->der = app_malloc(sess->derlen, "get session buffer");
  3306. if (!sess->id) {
  3307. BIO_printf(bio_err, "Out of memory adding to external cache\n");
  3308. OPENSSL_free(sess->id);
  3309. OPENSSL_free(sess->der);
  3310. OPENSSL_free(sess);
  3311. return 0;
  3312. }
  3313. p = sess->der;
  3314. /* Assume it still works. */
  3315. if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
  3316. BIO_printf(bio_err, "Unexpected session encoding length\n");
  3317. OPENSSL_free(sess->id);
  3318. OPENSSL_free(sess->der);
  3319. OPENSSL_free(sess);
  3320. return 0;
  3321. }
  3322. sess->next = first;
  3323. first = sess;
  3324. BIO_printf(bio_err, "New session added to external cache\n");
  3325. return 0;
  3326. }
  3327. static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
  3328. int *do_copy)
  3329. {
  3330. simple_ssl_session *sess;
  3331. *do_copy = 0;
  3332. for (sess = first; sess; sess = sess->next) {
  3333. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
  3334. const unsigned char *p = sess->der;
  3335. BIO_printf(bio_err, "Lookup session: cache hit\n");
  3336. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  3337. }
  3338. }
  3339. BIO_printf(bio_err, "Lookup session: cache miss\n");
  3340. return NULL;
  3341. }
  3342. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  3343. {
  3344. simple_ssl_session *sess, *prev = NULL;
  3345. const unsigned char *id;
  3346. unsigned int idlen;
  3347. id = SSL_SESSION_get_id(session, &idlen);
  3348. for (sess = first; sess; sess = sess->next) {
  3349. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
  3350. if (prev)
  3351. prev->next = sess->next;
  3352. else
  3353. first = sess->next;
  3354. OPENSSL_free(sess->id);
  3355. OPENSSL_free(sess->der);
  3356. OPENSSL_free(sess);
  3357. return;
  3358. }
  3359. prev = sess;
  3360. }
  3361. }
  3362. static void init_session_cache_ctx(SSL_CTX *sctx)
  3363. {
  3364. SSL_CTX_set_session_cache_mode(sctx,
  3365. SSL_SESS_CACHE_NO_INTERNAL |
  3366. SSL_SESS_CACHE_SERVER);
  3367. SSL_CTX_sess_set_new_cb(sctx, add_session);
  3368. SSL_CTX_sess_set_get_cb(sctx, get_session);
  3369. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  3370. }
  3371. static void free_sessions(void)
  3372. {
  3373. simple_ssl_session *sess, *tsess;
  3374. for (sess = first; sess;) {
  3375. OPENSSL_free(sess->id);
  3376. OPENSSL_free(sess->der);
  3377. tsess = sess;
  3378. sess = sess->next;
  3379. OPENSSL_free(tsess);
  3380. }
  3381. first = NULL;
  3382. }
  3383. #endif /* OPENSSL_NO_SOCK */