s_server.c 120 KB

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