s_server.c 116 KB

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