s_server.c 126 KB

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