s_server.c 117 KB

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