s_server.c 122 KB

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