s_server.c 120 KB

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