s_server.c 120 KB

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