s_server.c 124 KB

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