2
0

s_server.c 117 KB

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