server.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For simpler wolfSSL TLS server examples, visit
  22. * https://github.com/wolfSSL/wolfssl-examples/tree/master/tls
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifndef WOLFSSL_USER_SETTINGS
  28. #include <wolfssl/options.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  32. #include <wolfssl/ssl.h> /* name change portability layer */
  33. #ifdef HAVE_ECC
  34. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  35. #endif
  36. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  37. #include <wolfsentry/wolfsentry.h>
  38. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  39. static const char *wolfsentry_config_path = NULL;
  40. #endif
  41. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  42. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include "rl_fs.h"
  46. #include "rl_net.h"
  47. #endif
  48. #ifdef NO_FILESYSTEM
  49. #ifdef NO_RSA
  50. #error currently the example only tries to load in a RSA buffer
  51. #endif
  52. #undef USE_CERT_BUFFERS_2048
  53. #define USE_CERT_BUFFERS_2048
  54. #include <wolfssl/certs_test.h>
  55. #endif
  56. #include <wolfssl/test.h>
  57. #include <wolfssl/error-ssl.h>
  58. /* Force enable the compatibility macros for this example */
  59. #ifndef OPENSSL_EXTRA_X509_SMALL
  60. #define OPENSSL_EXTRA_X509_SMALL
  61. #endif
  62. #include <wolfssl/openssl/ssl.h>
  63. #include "examples/server/server.h"
  64. #ifndef NO_WOLFSSL_SERVER
  65. #if defined(WOLFSSL_TLS13) && ( \
  66. defined(HAVE_ECC) \
  67. || defined(HAVE_CURVE25519) \
  68. || defined(HAVE_CURVE448) \
  69. || defined(HAVE_FFDHE_2048))
  70. #define CAN_FORCE_CURVE
  71. #endif
  72. #if defined(CAN_FORCE_CURVE) && defined(HAVE_ECC)
  73. struct group_info {
  74. word16 group;
  75. const char *name;
  76. };
  77. static struct group_info group_id_to_text[] = {
  78. { WOLFSSL_ECC_SECP160K1, "SECP160K1" },
  79. { WOLFSSL_ECC_SECP160R1, "SECP160R1" },
  80. { WOLFSSL_ECC_SECP160R2, "SECP160R2" },
  81. { WOLFSSL_ECC_SECP192K1, "SECP192K1" },
  82. { WOLFSSL_ECC_SECP192R1, "SECP192R1" },
  83. { WOLFSSL_ECC_SECP224K1, "SECP224K1" },
  84. { WOLFSSL_ECC_SECP224R1, "SECP224R1" },
  85. { WOLFSSL_ECC_SECP256K1, "SECP256K1" },
  86. { WOLFSSL_ECC_SECP256R1, "SECP256R1" },
  87. { WOLFSSL_ECC_SECP384R1, "SECP384R1" },
  88. { WOLFSSL_ECC_SECP521R1, "SECP521R1" },
  89. { WOLFSSL_ECC_BRAINPOOLP256R1, "BRAINPOOLP256R1" },
  90. { WOLFSSL_ECC_BRAINPOOLP384R1, "BRAINPOOLP384R1" },
  91. { WOLFSSL_ECC_BRAINPOOLP512R1, "BRAINPOOLP512R1" },
  92. { 0, NULL }
  93. };
  94. #endif /* CAN_FORCE_CURVE && HAVE_ECC */
  95. #ifdef WOLFSSL_ASYNC_CRYPT
  96. static int devId = INVALID_DEVID;
  97. #endif
  98. #define DEFAULT_TIMEOUT_SEC 2
  99. /* Note on using port 0: if the server uses port 0 to bind an ephemeral port
  100. * number and is using the ready file for scripted testing, the code in
  101. * test.h will write the actual port number into the ready file for use
  102. * by the client. */
  103. #ifndef WOLFSSL_ALT_TEST_STRINGS
  104. static const char kReplyMsg[] = "I hear you fa shizzle!";
  105. #else
  106. static const char kReplyMsg[] = "I hear you fa shizzle!\n";
  107. #endif
  108. static const char kHttpServerMsg[] =
  109. "HTTP/1.1 200 OK\r\n"
  110. "Content-Type: text/html\r\n"
  111. "Connection: close\r\n"
  112. "Content-Length: 141\r\n"
  113. "\r\n"
  114. "<html>\r\n"
  115. "<head>\r\n"
  116. "<title>Welcome to wolfSSL!</title>\r\n"
  117. "</head>\r\n"
  118. "<body>\r\n"
  119. "<p>wolfSSL has successfully performed handshake!</p>\r\n"
  120. "</body>\r\n"
  121. "</html>\r\n";
  122. /* Read needs to be largest of the client.c message strings (29) */
  123. #define SRV_READ_SZ 32
  124. int runWithErrors = 0; /* Used with -x flag to run err_sys vs. print errors */
  125. int catastrophic = 0; /* Use with -x flag to still exit when an error is
  126. * considered catastrophic EG the servers own cert failing
  127. * to load would be catastrophic since there would be no
  128. * cert to send to clients attempting to connect. The
  129. * server should error out completely in that case
  130. */
  131. static int quieter = 0; /* Print fewer messages. This is helpful with overly
  132. * ambitious log parsers. */
  133. static int lng_index = 0;
  134. #define LOG_ERROR(...) \
  135. do { \
  136. if (!quieter) \
  137. fprintf(stderr, __VA_ARGS__); \
  138. } while(0)
  139. #ifdef WOLFSSL_CALLBACKS
  140. #if !defined(NO_OLD_TIMEVAL_NAME)
  141. Timeval srvTo;
  142. #else
  143. WOLFSSL_TIMEVAL srvTo;
  144. #endif
  145. static int srvHandShakeCB(HandShakeInfo* info)
  146. {
  147. (void)info;
  148. return 0;
  149. }
  150. static int srvTimeoutCB(TimeoutInfo* info)
  151. {
  152. (void)info;
  153. return 0;
  154. }
  155. #endif
  156. #ifndef NO_HANDSHAKE_DONE_CB
  157. static int myHsDoneCb(WOLFSSL* ssl, void* user_ctx)
  158. {
  159. (void)user_ctx;
  160. (void)ssl;
  161. /* printf("Notified HandShake done\n"); */
  162. /* return negative number to end TLS connection now */
  163. return 0;
  164. }
  165. #endif
  166. static void err_sys_ex(int out, const char* msg)
  167. {
  168. if (out == 1) { /* if server is running w/ -x flag, print error w/o exit */
  169. LOG_ERROR("wolfSSL error: %s\n", msg);
  170. LOG_ERROR("Continuing server execution...\n\n");
  171. } else {
  172. err_sys(msg);
  173. }
  174. }
  175. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  176. /* Translates return codes returned from
  177. * send() and recv() if need be.
  178. */
  179. static WC_INLINE int TranslateReturnCode(int old, int sd)
  180. {
  181. (void)sd;
  182. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  183. if (old == 0) {
  184. errno = SOCKET_EWOULDBLOCK;
  185. return -1; /* convert to BSD style wouldblock as error */
  186. }
  187. if (old < 0) {
  188. errno = RTCS_geterror(sd);
  189. if (errno == RTCSERR_TCP_CONN_CLOSING)
  190. return 0; /* convert to BSD style closing */
  191. if (errno == RTCSERR_TCP_CONN_RLSD)
  192. errno = SOCKET_ECONNRESET;
  193. if (errno == RTCSERR_TCP_TIMED_OUT)
  194. errno = SOCKET_EAGAIN;
  195. }
  196. #endif
  197. return old;
  198. }
  199. static WC_INLINE int wolfSSL_LastError(void)
  200. {
  201. #ifdef USE_WINDOWS_API
  202. return WSAGetLastError();
  203. #elif defined(EBSNET)
  204. return xn_getlasterror();
  205. #else
  206. return errno;
  207. #endif
  208. }
  209. /* wolfSSL Sock Addr */
  210. struct WOLFSSL_TEST_SOCKADDR {
  211. unsigned int sz; /* sockaddr size */
  212. SOCKADDR_IN_T sa; /* pointer to the sockaddr_in or sockaddr_in6 */
  213. };
  214. typedef struct WOLFSSL_TEST_DTLS_CTX {
  215. struct WOLFSSL_TEST_SOCKADDR peer;
  216. int rfd;
  217. int wfd;
  218. int failOnce;
  219. word32 blockSeq;
  220. } WOLFSSL_TEST_DTLS_CTX;
  221. static WC_INLINE int PeekSeq(const char* buf, word32* seq)
  222. {
  223. const char* c = buf + 3;
  224. if ((c[0] | c[1] | c[2] | c[3]) == 0) {
  225. *seq = ((word32)c[4] << 24) | ((word32)c[5] << 16) |
  226. ((word32)c[6] << 8) | (word32)c[7];
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. /* The send embedded callback
  232. * return : nb bytes sent, or error
  233. */
  234. static int TestEmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
  235. {
  236. WOLFSSL_TEST_DTLS_CTX* dtlsCtx = (WOLFSSL_TEST_DTLS_CTX*)ctx;
  237. int sd = dtlsCtx->wfd;
  238. int sent;
  239. (void)ssl;
  240. WOLFSSL_ENTER("TestEmbedSendTo");
  241. if (dtlsCtx->failOnce) {
  242. word32 seq = 0;
  243. if (PeekSeq(buf, &seq) && seq == dtlsCtx->blockSeq) {
  244. dtlsCtx->failOnce = 0;
  245. WOLFSSL_MSG("Forcing WANT_WRITE");
  246. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  247. }
  248. }
  249. sent = (int)sendto(sd, buf, (size_t)sz, 0,
  250. (const SOCKADDR*)&dtlsCtx->peer.sa, dtlsCtx->peer.sz);
  251. sent = TranslateReturnCode(sent, sd);
  252. if (sent < 0) {
  253. int err = wolfSSL_LastError();
  254. WOLFSSL_MSG("Embed Send To error");
  255. if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
  256. WOLFSSL_MSG("\tWould Block");
  257. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  258. }
  259. else if (err == SOCKET_ECONNRESET) {
  260. WOLFSSL_MSG("\tConnection reset");
  261. return WOLFSSL_CBIO_ERR_CONN_RST;
  262. }
  263. else if (err == SOCKET_EINTR) {
  264. WOLFSSL_MSG("\tSocket interrupted");
  265. return WOLFSSL_CBIO_ERR_ISR;
  266. }
  267. else if (err == SOCKET_EPIPE) {
  268. WOLFSSL_MSG("\tSocket EPIPE");
  269. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  270. }
  271. else {
  272. WOLFSSL_MSG("\tGeneral error");
  273. return WOLFSSL_CBIO_ERR_GENERAL;
  274. }
  275. }
  276. return sent;
  277. }
  278. #endif /* WOLFSSL_DTLS && USE_WOLFSSL_IO */
  279. static int NonBlockingSSL_Accept(SSL* ssl)
  280. {
  281. #ifndef WOLFSSL_CALLBACKS
  282. int ret = SSL_accept(ssl);
  283. #else
  284. int ret = wolfSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
  285. #endif
  286. int error = SSL_get_error(ssl, 0);
  287. SOCKET_T sockfd = (SOCKET_T)SSL_get_fd(ssl);
  288. int select_ret = 0;
  289. while (ret != WOLFSSL_SUCCESS &&
  290. (error == WOLFSSL_ERROR_WANT_READ || error == WOLFSSL_ERROR_WANT_WRITE
  291. #ifdef WOLFSSL_ASYNC_CRYPT
  292. || error == WC_NO_ERR_TRACE(WC_PENDING_E)
  293. #endif
  294. )) {
  295. if (error == WOLFSSL_ERROR_WANT_READ) {
  296. /* printf("... server would read block\n"); */
  297. }
  298. else if (error == WOLFSSL_ERROR_WANT_WRITE) {
  299. /* printf("... server would write block\n"); */
  300. }
  301. #ifdef WOLFSSL_ASYNC_CRYPT
  302. if (error == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  303. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  304. if (ret < 0) break;
  305. }
  306. else
  307. #endif
  308. {
  309. int currTimeout = 1;
  310. if (error == WOLFSSL_ERROR_WANT_WRITE)
  311. {
  312. select_ret = tcp_select_tx(sockfd, currTimeout);
  313. }
  314. else {
  315. #ifdef WOLFSSL_DTLS
  316. if (wolfSSL_dtls(ssl))
  317. currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
  318. #endif
  319. select_ret = tcp_select(sockfd, currTimeout);
  320. }
  321. }
  322. if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_SEND_READY)
  323. || (select_ret == TEST_ERROR_READY)
  324. #ifdef WOLFSSL_ASYNC_CRYPT
  325. || error == WC_NO_ERR_TRACE(WC_PENDING_E)
  326. #endif
  327. ) {
  328. #ifndef WOLFSSL_CALLBACKS
  329. ret = SSL_accept(ssl);
  330. #else
  331. ret = wolfSSL_accept_ex(ssl,
  332. srvHandShakeCB, srvTimeoutCB, srvTo);
  333. #endif
  334. error = SSL_get_error(ssl, 0);
  335. }
  336. else if (select_ret == TEST_TIMEOUT && !wolfSSL_dtls(ssl)) {
  337. error = WOLFSSL_ERROR_WANT_READ;
  338. }
  339. #ifdef WOLFSSL_DTLS
  340. else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl)) {
  341. ret = wolfSSL_dtls_got_timeout(ssl);
  342. if (ret != WOLFSSL_SUCCESS)
  343. error = wolfSSL_get_error(ssl, ret);
  344. else
  345. error = WOLFSSL_ERROR_WANT_READ;
  346. ret = WOLFSSL_FAILURE; /* Reset error so we loop */
  347. }
  348. #endif
  349. else {
  350. error = WOLFSSL_FATAL_ERROR;
  351. }
  352. }
  353. return ret;
  354. }
  355. /* Echo number of bytes specified by -B arg */
  356. int ServerEchoData(SSL* ssl, int clientfd, int echoData, int block,
  357. size_t throughput)
  358. {
  359. int ret = 0, err;
  360. double start = 0, rx_time = 0, tx_time = 0;
  361. int len, rx_pos;
  362. size_t xfer_bytes = 0;
  363. char* buffer;
  364. buffer = (char*)malloc((size_t)block);
  365. if (!buffer) {
  366. err_sys_ex(runWithErrors, "Server buffer malloc failed");
  367. }
  368. while ((echoData && throughput == 0) ||
  369. (!echoData && xfer_bytes < throughput))
  370. {
  371. int select_ret = tcp_select(clientfd, 1); /* Timeout=1 second */
  372. if (select_ret == TEST_RECV_READY) {
  373. if (throughput)
  374. len = (int)min((word32)block, (word32)(throughput - xfer_bytes));
  375. else
  376. len = block;
  377. rx_pos = 0;
  378. if (throughput) {
  379. start = current_time(1);
  380. }
  381. /* Read data */
  382. while (rx_pos < len) {
  383. ret = SSL_read(ssl, &buffer[rx_pos], len - rx_pos);
  384. if (ret <= 0) {
  385. err = SSL_get_error(ssl, 0);
  386. #ifdef WOLFSSL_ASYNC_CRYPT
  387. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  388. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  389. if (ret < 0) break;
  390. }
  391. else
  392. #endif
  393. if (err != WOLFSSL_ERROR_WANT_READ &&
  394. err != WOLFSSL_ERROR_WANT_WRITE &&
  395. err != WOLFSSL_ERROR_ZERO_RETURN &&
  396. err != WC_NO_ERR_TRACE(APP_DATA_READY))
  397. {
  398. LOG_ERROR("SSL_read echo error %d\n", err);
  399. err_sys_ex(runWithErrors, "SSL_read failed");
  400. break;
  401. }
  402. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  403. free(buffer);
  404. return WOLFSSL_ERROR_ZERO_RETURN;
  405. }
  406. }
  407. else {
  408. rx_pos += ret;
  409. if (!throughput)
  410. break;
  411. }
  412. }
  413. if (throughput) {
  414. rx_time += current_time(0) - start;
  415. start = current_time(1);
  416. }
  417. /* Write data */
  418. do {
  419. err = 0; /* reset error */
  420. ret = SSL_write(ssl, buffer, (int)min((word32)len, (word32)rx_pos));
  421. if (ret <= 0) {
  422. err = SSL_get_error(ssl, 0);
  423. #ifdef WOLFSSL_ASYNC_CRYPT
  424. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  425. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  426. if (ret < 0) break;
  427. }
  428. #endif
  429. }
  430. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E));
  431. if (ret != (int)min((word32)len, (word32)rx_pos)) {
  432. LOG_ERROR("SSL_write echo error %d\n", err);
  433. err_sys_ex(runWithErrors, "SSL_write failed");
  434. }
  435. if (throughput) {
  436. tx_time += current_time(0) - start;
  437. }
  438. xfer_bytes += (size_t)len;
  439. }
  440. }
  441. free(buffer);
  442. if (throughput) {
  443. #ifdef __MINGW32__
  444. #define SIZE_FMT "%d"
  445. #define SIZE_TYPE int
  446. #else
  447. #define SIZE_FMT "%zu"
  448. #define SIZE_TYPE size_t
  449. #endif
  450. if (rx_time > 0.0 && tx_time > 0.0) {
  451. printf(
  452. "wolfSSL Server Benchmark " SIZE_FMT " bytes\n"
  453. "\tRX %8.3f ms (%8.3f MBps)\n"
  454. "\tTX %8.3f ms (%8.3f MBps)\n",
  455. (SIZE_TYPE)throughput,
  456. (double)rx_time * 1000, (double)throughput / rx_time / 1024 / 1024,
  457. (double)tx_time * 1000, (double)throughput / tx_time / 1024 / 1024
  458. );
  459. }
  460. else {
  461. printf("Invalid rx_time: %f or tx_time: %f\n", rx_time, tx_time);
  462. }
  463. }
  464. return 0;
  465. }
  466. static void ServerRead(WOLFSSL* ssl, char* input, int inputLen)
  467. {
  468. int ret, err;
  469. char buffer[WOLFSSL_MAX_ERROR_SZ];
  470. /* Read data */
  471. do {
  472. err = 0; /* reset error */
  473. ret = SSL_read(ssl, input, inputLen);
  474. if (ret < 0) {
  475. err = SSL_get_error(ssl, ret);
  476. #ifdef HAVE_SECURE_RENEGOTIATION
  477. if (err == WC_NO_ERR_TRACE(APP_DATA_READY)) {
  478. /* If we receive a message during renegotiation
  479. * then just print it. We return the message sent
  480. * after the renegotiation. */
  481. ret = SSL_read(ssl, input, inputLen);
  482. if (ret >= 0) {
  483. /* null terminate message */
  484. input[ret] = '\0';
  485. printf("Client message received during "
  486. "secure renegotiation: %s\n", input);
  487. err = WOLFSSL_ERROR_WANT_READ;
  488. }
  489. else {
  490. err = SSL_get_error(ssl, ret);
  491. }
  492. }
  493. #endif
  494. #ifdef WOLFSSL_ASYNC_CRYPT
  495. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  496. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  497. if (ret < 0) break;
  498. }
  499. else
  500. #endif
  501. #ifdef WOLFSSL_DTLS
  502. if (wolfSSL_dtls(ssl) && err == WC_NO_ERR_TRACE(DECRYPT_ERROR)) {
  503. LOG_ERROR("Dropped client's message due to a bad MAC\n");
  504. }
  505. else
  506. #endif
  507. if (err != WOLFSSL_ERROR_WANT_READ
  508. && err != WOLFSSL_ERROR_WANT_WRITE /* Can happen during
  509. * handshake */
  510. #ifdef HAVE_SECURE_RENEGOTIATION
  511. && err != WC_NO_ERR_TRACE(APP_DATA_READY)
  512. #endif
  513. ) {
  514. LOG_ERROR("SSL_read input error %d, %s\n", err,
  515. ERR_error_string((unsigned long)err, buffer));
  516. err_sys_ex(runWithErrors, "SSL_read failed");
  517. }
  518. }
  519. else if (SSL_get_error(ssl, 0) == 0 &&
  520. tcp_select(SSL_get_fd(ssl), 0) == TEST_RECV_READY) {
  521. /* do a peek and check for "pending" */
  522. #ifdef WOLFSSL_ASYNC_CRYPT
  523. err = 0;
  524. #endif
  525. do {
  526. #ifdef WOLFSSL_ASYNC_CRYPT
  527. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  528. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  529. if (ret < 0) break;
  530. }
  531. #endif
  532. ret = wolfSSL_peek(ssl, buffer, 0);
  533. err = SSL_get_error(ssl, ret);
  534. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E)
  535. || err == WOLFSSL_ERROR_WANT_READ
  536. || err == WOLFSSL_ERROR_WANT_WRITE);
  537. if (err < 0) {
  538. err_sys_ex(runWithErrors, "wolfSSL_peek failed");
  539. }
  540. if (wolfSSL_pending(ssl))
  541. err = WOLFSSL_ERROR_WANT_READ;
  542. }
  543. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E)
  544. || err == WOLFSSL_ERROR_WANT_READ
  545. || err == WOLFSSL_ERROR_WANT_WRITE);
  546. if (ret > 0) {
  547. /* null terminate message */
  548. input[ret] = '\0';
  549. printf("Client message: %s\n", input);
  550. }
  551. }
  552. static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen)
  553. {
  554. int ret, err;
  555. int len;
  556. #ifdef OPENSSL_ALL
  557. /* Fuzz testing expects reply split over two msgs when TLSv1.0 or below */
  558. if (wolfSSL_GetVersion(ssl) <= WOLFSSL_TLSV1)
  559. len = outputLen / 2;
  560. else
  561. #endif
  562. len = outputLen;
  563. do {
  564. err = 0; /* reset error */
  565. ret = SSL_write(ssl, output, len);
  566. if (ret <= 0) {
  567. err = SSL_get_error(ssl, 0);
  568. #ifdef WOLFSSL_ASYNC_CRYPT
  569. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  570. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  571. if (ret < 0) break;
  572. }
  573. #endif
  574. }
  575. else if (ret != outputLen) {
  576. output += ret;
  577. len = (outputLen -= ret);
  578. err = WOLFSSL_ERROR_WANT_WRITE;
  579. }
  580. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E) ||
  581. err == WOLFSSL_ERROR_WANT_WRITE);
  582. if (ret != outputLen) {
  583. char buffer[WOLFSSL_MAX_ERROR_SZ];
  584. LOG_ERROR("SSL_write msg error %d, %s\n", err,
  585. ERR_error_string((unsigned long)err, buffer));
  586. err_sys_ex(runWithErrors, "SSL_write failed");
  587. }
  588. }
  589. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  590. #define MAX_GROUP_NUMBER 4
  591. static void SetKeyShare(WOLFSSL* ssl, int onlyKeyShare, int useX25519,
  592. int useX448, int usePqc, char* pqcAlg)
  593. {
  594. int ret;
  595. int groups[MAX_GROUP_NUMBER] = {0};
  596. int count = 0;
  597. (void)useX25519;
  598. (void)useX448;
  599. (void)usePqc;
  600. (void)pqcAlg;
  601. WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  602. if (onlyKeyShare == 2) {
  603. if (useX25519) {
  604. #ifdef HAVE_CURVE25519
  605. do {
  606. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519);
  607. if (ret == WOLFSSL_SUCCESS)
  608. groups[count++] = WOLFSSL_ECC_X25519;
  609. #ifdef WOLFSSL_ASYNC_CRYPT
  610. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
  611. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  612. #endif
  613. else
  614. err_sys("unable to use curve x25519");
  615. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  616. #endif
  617. }
  618. else if (useX448) {
  619. #ifdef HAVE_CURVE448
  620. do {
  621. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X448);
  622. if (ret == WOLFSSL_SUCCESS)
  623. groups[count++] = WOLFSSL_ECC_X448;
  624. #ifdef WOLFSSL_ASYNC_CRYPT
  625. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
  626. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  627. #endif
  628. else
  629. err_sys("unable to use curve x448");
  630. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  631. #endif
  632. }
  633. else if (usePqc == 1) {
  634. #ifdef HAVE_PQC
  635. groups[count] = 0;
  636. #ifndef WOLFSSL_NO_KYBER512
  637. if (XSTRCMP(pqcAlg, "KYBER_LEVEL1") == 0) {
  638. groups[count] = WOLFSSL_KYBER_LEVEL1;
  639. }
  640. else
  641. #endif
  642. #ifndef WOLFSSL_NO_KYBER768
  643. if (XSTRCMP(pqcAlg, "KYBER_LEVEL3") == 0) {
  644. groups[count] = WOLFSSL_KYBER_LEVEL3;
  645. }
  646. else
  647. #endif
  648. #ifndef WOLFSSL_NO_KYBER1024
  649. if (XSTRCMP(pqcAlg, "KYBER_LEVEL5") == 0) {
  650. groups[count] = WOLFSSL_KYBER_LEVEL5;
  651. }
  652. else
  653. #endif
  654. #ifndef WOLFSSL_NO_KYBER512
  655. if (XSTRCMP(pqcAlg, "P256_KYBER_LEVEL1") == 0) {
  656. groups[count] = WOLFSSL_P256_KYBER_LEVEL1;
  657. }
  658. else
  659. #endif
  660. #ifndef WOLFSSL_NO_KYBER768
  661. if (XSTRCMP(pqcAlg, "P384_KYBER_LEVEL3") == 0) {
  662. groups[count] = WOLFSSL_P384_KYBER_LEVEL3;
  663. }
  664. else
  665. #endif
  666. #ifndef WOLFSSL_NO_KYBER1024
  667. if (XSTRCMP(pqcAlg, "P521_KYBER_LEVEL5") == 0) {
  668. groups[count] = WOLFSSL_P521_KYBER_LEVEL5;
  669. }
  670. else
  671. #endif
  672. {
  673. err_sys("invalid post-quantum KEM specified");
  674. }
  675. if (groups[count] == 0) {
  676. err_sys("invalid post-quantum KEM specified");
  677. }
  678. else {
  679. if (wolfSSL_UseKeyShare(ssl, groups[count]) == WOLFSSL_SUCCESS) {
  680. printf("Using Post-Quantum KEM: %s\n", pqcAlg);
  681. count++;
  682. }
  683. else {
  684. groups[count] = 0;
  685. err_sys("unable to use post-quantum algorithm");
  686. }
  687. }
  688. #endif
  689. }
  690. else {
  691. #ifdef HAVE_ECC
  692. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  693. do {
  694. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1);
  695. if (ret == WOLFSSL_SUCCESS)
  696. groups[count++] = WOLFSSL_ECC_SECP256R1;
  697. #ifdef WOLFSSL_ASYNC_CRYPT
  698. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
  699. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  700. #endif
  701. else
  702. err_sys("unable to use curve secp256r1");
  703. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  704. #elif defined(WOLFSSL_SM2)
  705. do {
  706. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SM2P256V1);
  707. if (ret == WOLFSSL_SUCCESS)
  708. groups[count++] = WOLFSSL_ECC_SM2P256V1;
  709. #ifdef WOLFSSL_ASYNC_CRYPT
  710. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
  711. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  712. #endif
  713. else
  714. err_sys("unable to use curve sm2p256r1");
  715. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  716. #endif
  717. #endif
  718. }
  719. }
  720. if (onlyKeyShare == 1) {
  721. #ifdef HAVE_FFDHE_2048
  722. do {
  723. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048);
  724. if (ret == WOLFSSL_SUCCESS)
  725. groups[count++] = WOLFSSL_FFDHE_2048;
  726. #ifdef WOLFSSL_ASYNC_CRYPT
  727. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
  728. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  729. #endif
  730. else
  731. err_sys("unable to use DH 2048-bit parameters");
  732. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  733. #endif
  734. }
  735. if (count >= MAX_GROUP_NUMBER)
  736. err_sys("example group array size error");
  737. if (count > 0) {
  738. if (wolfSSL_set_groups(ssl, groups, count) != WOLFSSL_SUCCESS)
  739. err_sys("unable to set groups");
  740. }
  741. WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  742. }
  743. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  744. /* when adding new option, please follow the steps below: */
  745. /* 1. add new option message in English section */
  746. /* 2. increase the number of the second column */
  747. /* 3. increase the array dimension */
  748. /* 4. add the same message into Japanese section */
  749. /* (will be translated later) */
  750. /* 5. add printf() into suitable position of Usage() */
  751. static const char* server_usage_msg[][65] = {
  752. /* English */
  753. {
  754. " NOTE: All files relative to wolfSSL home dir\n", /* 0 */
  755. "-? <num> Help, print this usage\n"
  756. " 0: English, 1: Japanese\n"
  757. "--help Help, in English\n", /* 1 */
  758. "-p <num> Port to listen on, not 0, default", /* 2 */
  759. #ifndef WOLFSSL_TLS13
  760. "-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default", /* 3 */
  761. #else
  762. "-v <num> SSL version [0-4], SSLv3(0) - TLS1.3(4)), default", /* 3 */
  763. #endif
  764. "-l <str> Cipher suite list (: delimited)\n", /* 4 */
  765. "-c <file> Certificate file, default", /* 5 */
  766. "-k <file> Key file, default", /* 6 */
  767. "-A <file> Certificate Authority file, default", /* 7 */
  768. "-R <file> Create Ready file for external monitor"
  769. " default none\n", /* 8 */
  770. #ifndef NO_DH
  771. "-D <file> Diffie-Hellman Params file, default", /* 9 */
  772. "-Z <num> Minimum DH key bits, default", /* 10 */
  773. #endif
  774. #ifdef HAVE_ALPN
  775. "-L <str> Application-Layer Protocol Negotiation"
  776. " ({C,F}:<list>)\n", /* 11 */
  777. #endif
  778. "-d Disable client cert check\n", /* 12 */
  779. "-b Bind to any interface instead of localhost only\n",/* 13 */
  780. "-s Use pre Shared keys\n", /* 14 */
  781. #ifndef WOLFSSL_DTLS13
  782. "-u Use UDP DTLS, add -v 2 for DTLSv1, -v 3 for DTLSv1.2"
  783. " (default)\n", /* 15 */
  784. #else
  785. "-u Use UDP DTLS, add -v 2 for DTLSv1, -v 3 for DTLSv1.2"
  786. " (default), -v 4 for DTLSv1.3\n", /* 15 */
  787. #endif /* !WOLFSSL_DTLS13 */
  788. #ifdef WOLFSSL_SCTP
  789. "-G Use SCTP DTLS,"
  790. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 16 */
  791. #endif
  792. "-f Fewer packets/group messages\n", /* 17 */
  793. "-r Allow one client Resumption\n", /* 18 */
  794. "-N Use Non-blocking sockets\n", /* 19 */
  795. "-S <str> Use Host Name Indication\n", /* 20 */
  796. "-w Wait for bidirectional shutdown\n", /* 21 */
  797. #ifdef HAVE_OCSP
  798. "-o Perform OCSP lookup on peer certificate\n", /* 22 */
  799. "-O <url> Perform OCSP lookup using <url> as responder\n", /* 23 */
  800. #endif
  801. #ifdef HAVE_PK_CALLBACKS
  802. "-P Public Key Callbacks\n", /* 24 */
  803. #endif
  804. #ifdef HAVE_ANON
  805. "-a Anonymous server\n", /* 25 */
  806. #endif
  807. #ifndef NO_PSK
  808. "-I Do not send PSK identity hint\n", /* 26 */
  809. #endif
  810. "-x Print server errors but do not close connection\n",/* 27 */
  811. "-i Loop indefinitely (allow repeated connections)\n", /* 28 */
  812. "-e Echo data mode (return raw bytes received)\n", /* 29 */
  813. "-B <num> Benchmark throughput"
  814. " using <num> bytes and print stats\n", /* 31 */
  815. #ifdef HAVE_CRL
  816. "-V Disable CRL\n", /* 32 */
  817. #endif
  818. #ifdef WOLFSSL_TRUST_PEER_CERT
  819. "-E <file> Path to load trusted peer cert\n", /* 33 */
  820. #endif
  821. #ifdef HAVE_WNR
  822. "-q <file> Whitewood config file, default", /* 34 */
  823. #endif
  824. "-g Return basic HTML web page\n", /* 35 */
  825. "-C <num> The number of connections to accept, default: 1\n",/* 36 */
  826. "-H <arg> Internal tests"
  827. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  828. " loadSSL, disallowETM]\n", /* 38 */
  829. #ifdef WOLFSSL_TLS13
  830. "-U Update keys and IVs before sending\n", /* 39 */
  831. "-K Key Exchange for PSK not using (EC)DHE\n", /* 40 */
  832. #ifndef NO_DH
  833. "-y Pre-generate Key Share using FFDHE_2048 only\n", /* 41 */
  834. #endif
  835. #ifdef HAVE_ECC
  836. "-Y Pre-generate Key Share using P-256 only \n", /* 42 */
  837. #endif
  838. #ifdef HAVE_CURVE25519
  839. "-t Pre-generate Key share using Curve25519 only\n", /* 43 */
  840. #endif
  841. #endif /* WOLFSSL_TLS13 */
  842. #ifdef HAVE_SESSION_TICKET
  843. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS)
  844. "-T Do not generate session ticket\n", /* 44 */
  845. #else
  846. "-T [aon] Do not generate session ticket\n", /* 44 */
  847. " No option affects TLS 1.3 only, 'a' affects all"
  848. " protocol versions,\n", /* 45 */
  849. " 'o' affects TLS 1.2 and below only\n", /* 46 */
  850. " 'n' affects TLS 1.3 only\n", /* 47 */
  851. #endif
  852. #endif
  853. #ifdef WOLFSSL_TLS13
  854. "-F Send alert if no mutual authentication\n", /* 48 */
  855. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  856. "-Q Request certificate from client post-handshake\n", /* 49 */
  857. #endif
  858. #ifdef WOLFSSL_SEND_HRR_COOKIE
  859. "-J [n] Server sends Cookie Extension containing state (n to "
  860. "disable)\n", /* 50 */
  861. #endif
  862. #endif /* WOLFSSL_TLS13 */
  863. #ifdef WOLFSSL_EARLY_DATA
  864. "-0 Early data read from client (0-RTT handshake)\n", /* 51 */
  865. #endif
  866. #ifdef WOLFSSL_MULTICAST
  867. "-3 <grpid> Multicast, grpid < 256\n", /* 52 */
  868. #endif
  869. "-1 <num> Display a result by specified language."
  870. "\n 0: English, 1: Japanese\n", /* 53 */
  871. #ifdef HAVE_TRUSTED_CA
  872. "-5 Use Trusted CA Key Indication\n", /* 54 */
  873. #endif
  874. "-6 Simulate WANT_WRITE errors on every other IO send\n",
  875. /* 55 */
  876. #ifdef HAVE_CURVE448
  877. "-8 Pre-generate Key share using Curve448 only\n", /* 56 */
  878. #endif
  879. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  880. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  881. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  882. "-9 Use hash dir look up for certificate loading\n"
  883. " loading from <wolfSSL home>/certs folder\n"
  884. " files in the folder would have the form \"hash.N\" file name\n"
  885. " e.g symbolic link to the file at certs folder\n"
  886. " ln -s client-ca.pem `openssl x509 -in client-ca.pem -hash -noout`.0\n",
  887. /* 57 */
  888. #endif
  889. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  890. "--wolfsentry-config <file> Path for JSON wolfSentry config\n",
  891. /* 58 */
  892. #endif
  893. #ifndef WOLFSSL_TLS13
  894. "-7 Set minimum downgrade protocol version [0-3] "
  895. " SSLv3(0) - TLS1.2(3)\n",
  896. #else
  897. "-7 Set minimum downgrade protocol version [0-4] "
  898. " SSLv3(0) - TLS1.3(4)\n", /* 59 */
  899. #endif
  900. #ifdef HAVE_PQC
  901. "--pqc <alg> Key Share with specified post-quantum algorithm only [KYBER_LEVEL1, KYBER_LEVEL3,\n"
  902. " KYBER_LEVEL5, P256_KYBER_LEVEL1, P384_KYBER_LEVEL3, P521_KYBER_LEVEL5] \n", /* 60 */
  903. #endif
  904. #ifdef WOLFSSL_SRTP
  905. "--srtp <profile> (default is SRTP_AES128_CM_SHA1_80)\n", /* 61 */
  906. #endif
  907. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  908. "--send-ticket Send a new session ticket during application data\n",
  909. /* 62 */
  910. #endif
  911. #ifdef CAN_FORCE_CURVE
  912. "--force-curve [<curve>] Pre-generate a Key Share using <curve>.\n"
  913. " Leave <curve> blank to list all curves.\n"
  914. " Note: requires TLS1.3\n",
  915. /* 63 */
  916. #endif
  917. #ifdef HAVE_SUPPORTED_CURVES
  918. "--onlyPskDheKe Must use DHE key exchange with PSK\n", /* 64 */
  919. #endif
  920. #ifdef WOLFSSL_DUAL_ALG_CERTS
  921. "--altPrivKey <file> Generate alternative signature with this key.\n",
  922. /* 65 */
  923. #endif
  924. "\n"
  925. "For simpler wolfSSL TLS server examples, visit\n"
  926. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n",
  927. /* 66 */
  928. NULL,
  929. },
  930. #ifndef NO_MULTIBYTE_PRINT
  931. /* Japanese */
  932. {
  933. " 注意 : 全てのファイルは"
  934. " wolfSSL ホーム・ディレクトリからの相対です。\n", /* 0 */
  935. "-? <num> ヘルプ, 使い方を表示\n"
  936. " 0: 英語、 1: 日本語\n"
  937. "--ヘルプ 日本語で使い方を表示\n", /* 1 */
  938. "-p <num> 接続先ポート, 0は無効, 既定値", /* 2 */
  939. #ifndef WOLFSSL_TLS13
  940. "-v <num> SSL バージョン [0-3], SSLv3(0) - TLS1.2(3)),"
  941. " 既定値", /* 3 */
  942. #else
  943. "-v <num> SSL バージョン [0-4], SSLv3(0) - TLS1.3(4)),"
  944. " 既定値", /* 3 */
  945. #endif
  946. "-l <str> 暗号スイートリスト (区切り文字 :)\n", /* 4 */
  947. "-c <file> 証明書ファイル, 既定値", /* 5 */
  948. "-k <file> 鍵ファイル, 既定値", /* 6 */
  949. "-A <file> 認証局ファイル, 既定値", /* 7 */
  950. "-R <file> 外部モニタ用の準備完了ファイルを作成する。"
  951. "既定値 なし\n", /* 8 */
  952. #ifndef NO_DH
  953. "-D <file> ディフィー・ヘルマンのパラメータファイル,"
  954. " 既定値", /* 9 */
  955. "-Z <num> 最小 DH 鍵 ビット, 既定値", /* 10 */
  956. #endif
  957. #ifdef HAVE_ALPN
  958. "-L <str> アプリケーション層プロトコルネゴシエーションを行う"
  959. " ({C,F}:<list>)\n", /* 11 */
  960. #endif
  961. "-d クライアント認証を無効とする\n", /* 12 */
  962. "-b ローカルホスト以外のインターフェースへも"
  963. "バインドする\n", /* 13 */
  964. "-s 事前共有鍵を使用する\n", /* 14 */
  965. "-u UDP DTLSを使用する。\n"
  966. #ifndef WOLFSSL_DTLS13
  967. " -v 2 を追加指定するとDTLSv1, "
  968. "-v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 15 */
  969. #else
  970. " -v 2 を追加指定するとDTLSv1, "
  971. "-v 3 を追加指定すると DTLSv1.2 (既定値),\n"
  972. " -v 4 を追加指定すると DTLSv1.3\n", /* 15 */
  973. #endif /* !WOLFSSL_DTLS13 */
  974. #ifdef WOLFSSL_SCTP
  975. "-G SCTP DTLSを使用する。-v 2 を追加指定すると"
  976. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 16 */
  977. #endif
  978. "-f より少ないパケット/グループメッセージを使用する\n",/* 17 */
  979. "-r クライアントの再開を許可する\n", /* 18 */
  980. "-N ノンブロッキング・ソケットを使用する\n", /* 19 */
  981. "-S <str> ホスト名表示を使用する\n", /* 20 */
  982. "-w 双方向シャットダウンを待つ\n", /* 21 */
  983. #ifdef HAVE_OCSP
  984. "-o OCSPルックアップをピア証明書で実施する\n", /* 22 */
  985. "-O <url> OCSPルックアップを、"
  986. "<url>を使用し応答者として実施する\n", /* 23 */
  987. #endif
  988. #ifdef HAVE_PK_CALLBACKS
  989. "-P 公開鍵コールバック\n", /* 24 */
  990. #endif
  991. #ifdef HAVE_ANON
  992. "-a 匿名サーバー\n", /* 25 */
  993. #endif
  994. #ifndef NO_PSK
  995. "-I PSKアイデンティティのヒントを送信しない\n", /* 26 */
  996. #endif
  997. "-x サーバーエラーを出力するが接続を切断しない\n", /* 27 */
  998. "-i 無期限にループする(繰り返し接続を許可)\n", /* 28 */
  999. "-e エコー・データモード"
  1000. "(受け取ったバイトデータを返す)\n", /* 29 */
  1001. "-B <num> <num> バイトを用いてのベンチマーク・スループット"
  1002. "測定と結果を出力する\n", /* 31 */
  1003. #ifdef HAVE_CRL
  1004. "-V CRLを無効とする\n", /* 32 */
  1005. #endif
  1006. #ifdef WOLFSSL_TRUST_PEER_CERT
  1007. "-E <file> 信頼出来るピアの証明書ロードの為のパス\n\n", /* 33 */
  1008. #endif
  1009. #ifdef HAVE_WNR
  1010. "-q <file> Whitewood コンフィグファイル, 既定値", /* 34 */
  1011. #endif
  1012. "-g 基本的な Web ページを返す\n", /* 35 */
  1013. "-C <num> アクセプト可能な接続数を指定する。既定値: 1\n", /* 36 */
  1014. "-H <arg> 内部テスト"
  1015. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  1016. " loadSSL, disallowETM]\n", /* 38 */
  1017. #ifdef WOLFSSL_TLS13
  1018. "-U データ送信前に、鍵とIVを更新する\n", /* 39 */
  1019. "-K 鍵交換にPSKを使用、(EC)DHEは使用しない\n", /* 40 */
  1020. #ifndef NO_DH
  1021. "-y FFDHE_2048のみを使用して鍵共有を事前生成する\n", /* 41 */
  1022. #endif
  1023. #ifdef HAVE_ECC
  1024. "-Y P-256のみを使用したキー共有の事前生成\n", /* 42 */
  1025. #endif
  1026. #ifdef HAVE_CURVE25519
  1027. "-t Curve25519のみを使用して鍵共有を事前生成する\n", /* 43 */
  1028. #endif
  1029. #endif /* WOLFSSL_TLS13 */
  1030. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS)
  1031. "-T セッションチケットを生成しない\n", /* 44 */
  1032. #else
  1033. "-T [aon] セッションチケットを生成しない\n", /* 44 */
  1034. " オプション指定なしの場合、TLS 1.3 にだけ有効\n"
  1035. " 'a' を指定した場合、"
  1036. "全てのプロトコルバージョンに有効\n" /* 45 */
  1037. " 'o' を指定した場合、TLS 1.2 及び"
  1038. "それ以下のプロトコルバージョンに有効\n" /* 46 */
  1039. " 'n' を指定した場合、TLS 1.3 にのみ有効\n", /* 47 */
  1040. #endif
  1041. #ifdef WOLFSSL_TLS13
  1042. "-F 相互認証が無い場合にalert を送信\n", /* 48 */
  1043. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  1044. "-Q クライアントのポストハンドシェイクから"
  1045. "証明書を要求する\n", /* 49 */
  1046. #endif
  1047. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1048. "-J サーバーの状態を含むTLS Cookie 拡張を送信する\n", /* 50 */
  1049. #endif
  1050. #endif /* WOLFSSL_TLS13 */
  1051. #ifdef WOLFSSL_EARLY_DATA
  1052. "-0 クライアントからの Early Data 読み取り"
  1053. "(0-RTTハンドシェイク)\n", /* 51 */
  1054. #endif
  1055. #ifdef WOLFSSL_MULTICAST
  1056. "-3 <grpid> マルチキャスト, grpid < 256\n", /* 52 */
  1057. #endif
  1058. "-1 <num> 指定された言語で結果を表示します。"
  1059. "\n 0: 英語、 1: 日本語\n", /* 53 */
  1060. #ifdef HAVE_TRUSTED_CA
  1061. "-5 信頼できる認証局の鍵表示を使用する\n", /* 54 */
  1062. #endif
  1063. "-6 交互の IO 送信で WANT_WRITE エラー"
  1064. "をシュミレート\n",
  1065. /* 55 */
  1066. #ifdef HAVE_CURVE448
  1067. "-8 Curve448のみを使用して鍵共有を事前生成する\n", /* 56 */
  1068. #endif
  1069. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1070. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1071. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1072. "-9 証明書の読み込みに hash dir 機能を使用する\n"
  1073. " <wolfSSL home>/certs フォルダーからロードします\n"
  1074. " フォルダー中のファイルは、\"hash.N\"[N:0-9]名である必要があります\n"
  1075. " 以下の例ではca-cert.pemにシンボリックリンクを設定します\n"
  1076. " ln -s client-ca.pem `openssl x509 -in client-ca.pem -hash -noout`.0\n",
  1077. /* 57 */
  1078. #endif
  1079. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  1080. "--wolfsentry-config <file> wolfSentry コンフィグファイル\n",
  1081. /* 58 */
  1082. #endif
  1083. #ifndef WOLFSSL_TLS13
  1084. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-3] "
  1085. " SSLv3(0) - TLS1.2(3)\n",
  1086. #else
  1087. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-4] "
  1088. " SSLv3(0) - TLS1.3(4)\n", /* 59 */
  1089. #endif
  1090. #ifdef HAVE_PQC
  1091. "--pqc <alg> post-quantum 名前付きグループとの鍵共有のみ [KYBER_LEVEL1, KYBER_LEVEL3,\n"
  1092. " KYBER_LEVEL5, P256_KYBER_LEVEL1, P384_KYBER_LEVEL3, P521_KYBER_LEVEL5]\n", /* 60 */
  1093. #endif
  1094. #ifdef WOLFSSL_SRTP
  1095. "--srtp <profile> (デフォルトはSRTP_AES128_CM_SHA1_80)\n", /* 61 */
  1096. #endif
  1097. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1098. "--send-ticket Application data 中に新しい"
  1099. "セッションチケットを送信します\n",
  1100. /* 62 */
  1101. #endif
  1102. #ifdef CAN_FORCE_CURVE
  1103. /* TODO: Need Japanese translation */
  1104. "--force-curve [<curve>] Pre-generate a Key Share using <curve>.\n"
  1105. " Leave <curve> blank to list all curves.\n"
  1106. " Note: requires TLS1.3\n",
  1107. /* 63 */
  1108. #endif
  1109. #ifdef HAVE_SUPPORTED_CURVES
  1110. "--onlyPskDheKe Must use DHE key exchange with PSK\n", /* 64 */
  1111. #endif
  1112. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1113. "--altPrivKey <file> Generate alternative signature with this key.\n",
  1114. /* 65 */
  1115. #endif
  1116. "\n"
  1117. "より簡単なwolfSSL TSL クライアントの例については"
  1118. "下記にアクセスしてください\n"
  1119. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n",
  1120. /* 66 */
  1121. NULL,
  1122. },
  1123. #endif
  1124. };
  1125. static void Usage(void)
  1126. {
  1127. int msgId = 0;
  1128. const char** msg = server_usage_msg[lng_index];
  1129. printf("%s%s%s", "server ", LIBWOLFSSL_VERSION_STRING,
  1130. msg[msgId]);
  1131. printf("%s", msg[++msgId]); /* ? */
  1132. printf("%s %d\n", msg[++msgId], wolfSSLPort); /* -p */
  1133. #ifndef WOLFSSL_TLS13
  1134. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  1135. #else
  1136. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  1137. #endif
  1138. printf("%s", msg[++msgId]); /* -l */
  1139. printf("%s %s\n", msg[++msgId], svrCertFile); /* -c */
  1140. printf("%s %s\n", msg[++msgId], svrKeyFile); /* -k */
  1141. printf("%s %s\n", msg[++msgId], cliCertFile); /* -A */
  1142. printf("%s", msg[++msgId]); /* -R */
  1143. #ifndef NO_DH
  1144. printf("%s %s\n", msg[++msgId], dhParamFile); /* -D */
  1145. printf("%s %d\n", msg[++msgId], DEFAULT_MIN_DHKEY_BITS);/* -Z */
  1146. #endif
  1147. #ifdef HAVE_ALPN
  1148. printf("%s", msg[++msgId]); /* -L */
  1149. #endif
  1150. printf("%s", msg[++msgId]); /* -d */
  1151. printf("%s", msg[++msgId]); /* -b */
  1152. printf("%s", msg[++msgId]); /* -s */
  1153. printf("%s", msg[++msgId]); /* -u */
  1154. #ifdef WOLFSSL_SCTP
  1155. printf("%s", msg[++msgId]); /* -G */
  1156. #endif
  1157. printf("%s", msg[++msgId]); /* -f */
  1158. printf("%s", msg[++msgId]); /* -r */
  1159. printf("%s", msg[++msgId]); /* -N */
  1160. printf("%s", msg[++msgId]); /* -S */
  1161. printf("%s", msg[++msgId]); /* -w */
  1162. #ifdef HAVE_SECURE_RENEGOTIATION
  1163. printf("-M Allow Secure Renegotiation\n");
  1164. printf("-m Force Server Initiated Secure Renegotiation\n");
  1165. #endif /* HAVE_SECURE_RENEGOTIATION */
  1166. #ifdef HAVE_OCSP
  1167. printf("%s", msg[++msgId]); /* -o */
  1168. printf("%s", msg[++msgId]); /* -O */
  1169. #endif
  1170. #ifdef HAVE_PK_CALLBACKS
  1171. printf("%s", msg[++msgId]); /* -P */
  1172. #endif
  1173. #ifdef HAVE_ANON
  1174. printf("%s", msg[++msgId]); /* -a */
  1175. #endif
  1176. #ifndef NO_PSK
  1177. printf("%s", msg[++msgId]); /* -I */
  1178. #endif
  1179. printf("%s", msg[++msgId]); /* -x */
  1180. printf("%s", msg[++msgId]); /* -i */
  1181. printf("%s", msg[++msgId]); /* -e */
  1182. printf("%s", msg[++msgId]); /* -B */
  1183. #ifdef HAVE_CRL
  1184. printf("%s", msg[++msgId]); /* -V */
  1185. #endif
  1186. #ifdef WOLFSSL_TRUST_PEER_CERT
  1187. printf("%s", msg[++msgId]); /* -E */
  1188. #endif
  1189. #ifdef HAVE_WNR
  1190. printf("%s %s\n", msg[++msgId], wnrConfig); /* -q */
  1191. #endif
  1192. printf("%s", msg[++msgId]); /* -g */
  1193. printf("%s", msg[++msgId]); /* -C */
  1194. printf("%s", msg[++msgId]); /* -H */
  1195. printf("%s", msg[++msgId]); /* more -H options */
  1196. #ifdef WOLFSSL_TLS13
  1197. printf("%s", msg[++msgId]); /* -U */
  1198. printf("%s", msg[++msgId]); /* -K */
  1199. #ifndef NO_DH
  1200. printf("%s", msg[++msgId]); /* -y */
  1201. #endif
  1202. #ifdef HAVE_ECC
  1203. printf("%s", msg[++msgId]); /* -Y */
  1204. #endif
  1205. #ifdef HAVE_CURVE25519
  1206. printf("%s", msg[++msgId]); /* -t */
  1207. #endif
  1208. #endif /* WOLFSSL_TLS13 */
  1209. #ifdef HAVE_SESSION_TICKET
  1210. printf("%s", msg[++msgId]); /* -T */
  1211. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1212. printf("%s", msg[++msgId]); /* -T */
  1213. printf("%s", msg[++msgId]); /* -T */
  1214. printf("%s", msg[++msgId]); /* -T */
  1215. #endif
  1216. #endif
  1217. #ifdef WOLFSSL_TLS13
  1218. printf("%s", msg[++msgId]); /* -F */
  1219. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  1220. printf("%s", msg[++msgId]); /* -Q */
  1221. #endif
  1222. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1223. printf("%s", msg[++msgId]); /* -J */
  1224. #endif
  1225. #endif /* WOLFSSL_TLS13 */
  1226. #ifdef WOLFSSL_EARLY_DATA
  1227. printf("%s", msg[++msgId]); /* -0 */
  1228. #endif
  1229. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1230. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1231. printf("-2 Disable DH Prime check\n");
  1232. #endif
  1233. #ifdef WOLFSSL_DTLS
  1234. printf("-4 <seq> DTLS fake would-block for message seq\n");
  1235. #endif
  1236. #ifdef WOLFSSL_MULTICAST
  1237. printf("%s", msg[++msgId]); /* -3 */
  1238. #endif
  1239. printf("%s", msg[++msgId]); /* -1 */
  1240. #ifdef HAVE_TRUSTED_CA
  1241. printf("%s", msg[++msgId]); /* -5 */
  1242. #endif /* HAVE_TRUSTED_CA */
  1243. printf("%s", msg[++msgId]); /* -6 */
  1244. #ifdef HAVE_CURVE448
  1245. printf("%s", msg[++msgId]); /* -8 */
  1246. #endif
  1247. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1248. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1249. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1250. printf("%s", msg[++msgId]); /* -9 */
  1251. #endif
  1252. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1253. !defined(WOLFSENTRY_NO_JSON)
  1254. printf("%s", msg[++msgId]); /* --wolfsentry-config */
  1255. #endif
  1256. printf("%s", msg[++msgId]); /* -7 */
  1257. #ifdef HAVE_PQC
  1258. printf("%s", msg[++msgId]); /* --pqc */
  1259. printf("%s", msg[++msgId]); /* --pqc options */
  1260. printf("%s", msg[++msgId]); /* more --pqc options */
  1261. printf("%s", msg[++msgId]); /* more --pqc options */
  1262. #endif
  1263. #ifdef WOLFSSL_SRTP
  1264. printf("%s", msg[++msgId]); /* dtls-srtp */
  1265. #endif
  1266. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1267. printf("%s", msg[++msgId]); /* send-ticket */
  1268. #endif
  1269. #ifdef CAN_FORCE_CURVE
  1270. printf("%s", msg[++msgId]); /* force-curve */
  1271. #endif
  1272. #ifdef HAVE_SUPPORTED_CURVES
  1273. printf("%s", msg[++msgId]); /* --onlyPskDheKe */
  1274. #endif
  1275. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1276. printf("%s", msg[++msgId]); /* --altPrivKey */
  1277. #endif
  1278. printf("%s", msg[++msgId]); /* Examples repo link */
  1279. }
  1280. #ifdef WOLFSSL_SRTP
  1281. /**
  1282. * server_srtp_test() - print the ekm and share it with the client
  1283. * @ssl: ssl context
  1284. * @srtp_helper: srtp_test_helper shared struct with the client
  1285. *
  1286. * if @srtp_helper is NULL the ekm isn't shared, but it is still printed.
  1287. *
  1288. * calls srtp_helper_set_ekm() to wake the client and share the ekm with
  1289. * him. The client will check that the ekm matches the one computed by itself.
  1290. */
  1291. static int server_srtp_test(WOLFSSL *ssl, func_args *args)
  1292. {
  1293. size_t srtp_secret_length;
  1294. byte *srtp_secret, *p;
  1295. int ret;
  1296. #ifdef WOLFSSL_COND
  1297. srtp_test_helper *srtp_helper = args->srtp_helper;
  1298. #else
  1299. (void)args;
  1300. #endif
  1301. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, NULL,
  1302. &srtp_secret_length);
  1303. if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
  1304. LOG_ERROR("DTLS SRTP: Error getting key material length\n");
  1305. return ret;
  1306. }
  1307. srtp_secret = (byte*)XMALLOC(srtp_secret_length,
  1308. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1309. if (srtp_secret == NULL) {
  1310. err_sys("DTLS SRTP: Memory error");
  1311. }
  1312. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, srtp_secret,
  1313. &srtp_secret_length);
  1314. if (ret != WOLFSSL_SUCCESS) {
  1315. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1316. LOG_ERROR("DTLS SRTP: Error getting key material\n");
  1317. return ret;
  1318. }
  1319. printf("DTLS SRTP: Exported key material: ");
  1320. for (p = srtp_secret; p < srtp_secret + srtp_secret_length; p++)
  1321. printf("%02X", *p);
  1322. printf("\n");
  1323. #ifdef WOLFSSL_COND
  1324. if (srtp_helper != NULL) {
  1325. srtp_helper_set_ekm(srtp_helper, srtp_secret, srtp_secret_length);
  1326. /* client code will free srtp_secret buffer after checking for
  1327. correctness */
  1328. return 0;
  1329. }
  1330. #endif /* WOLFSSL_COND */
  1331. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1332. return 0;
  1333. }
  1334. #endif
  1335. THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
  1336. {
  1337. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  1338. SOCKET_T clientfd = WOLFSSL_SOCKET_INVALID;
  1339. SOCKADDR_IN_T client_addr;
  1340. socklen_t client_len;
  1341. wolfSSL_method_func method = NULL;
  1342. SSL_CTX* ctx = 0;
  1343. SSL* ssl = 0;
  1344. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  1345. wolfsentry_errcode_t wolfsentry_ret;
  1346. #endif
  1347. int minVersion = SERVER_INVALID_VERSION;
  1348. int useWebServerMsg = 0;
  1349. char input[SRV_READ_SZ];
  1350. #ifndef WOLFSSL_VXWORKS
  1351. int ch;
  1352. static const struct mygetopt_long_config long_options[] = {
  1353. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1354. !defined(WOLFSENTRY_NO_JSON)
  1355. { "wolfsentry-config", 1, 256 },
  1356. #endif
  1357. { "help", 0, 257 },
  1358. #ifndef NO_MULTIBYTE_PRINT
  1359. { "ヘルプ", 0, 258 },
  1360. #endif
  1361. #if defined(HAVE_PQC)
  1362. { "pqc", 1, 259 },
  1363. #endif
  1364. #ifdef WOLFSSL_SRTP
  1365. { "srtp", 2, 260 }, /* optional argument */
  1366. #endif
  1367. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1368. { "send-ticket", 0, 261 },
  1369. #endif
  1370. #ifdef CAN_FORCE_CURVE
  1371. { "force-curve", 2, 262},
  1372. #endif
  1373. #ifdef WOLFSSL_DTLS_CID
  1374. {"cid", 2, 263},
  1375. #endif /* WOLFSSL_DTLS_CID */
  1376. #ifdef HAVE_SUPPORTED_CURVES
  1377. {"onlyPskDheKe", 2, 264},
  1378. #endif /* HAVE_SUPPORTED_CURVES */
  1379. #ifdef HAVE_CRL
  1380. {"crl-dir", 1, 265},
  1381. #endif
  1382. {"quieter", 0, 266},
  1383. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1384. { "altPrivKey", 1, 267},
  1385. #endif
  1386. { 0, 0, 0 }
  1387. };
  1388. #endif
  1389. int version = SERVER_DEFAULT_VERSION;
  1390. #ifndef WOLFSSL_NO_CLIENT_AUTH
  1391. int doCliCertCheck = 1;
  1392. #else
  1393. int doCliCertCheck = 0;
  1394. #endif
  1395. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  1396. int disableCRL = 0;
  1397. #endif
  1398. int useAnyAddr = 0;
  1399. word16 port = wolfSSLPort;
  1400. int usePsk = 0;
  1401. int usePskPlus = 0;
  1402. int useAnon = 0;
  1403. int doDTLS = 0;
  1404. int dtlsUDP = 0;
  1405. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1406. defined(WOLFSSL_DTLS)
  1407. int dtlsMTU = 0;
  1408. #endif
  1409. int dtlsSCTP = 0;
  1410. int doMcast = 0;
  1411. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  1412. int doBlockSeq = 0;
  1413. WOLFSSL_TEST_DTLS_CTX dtlsCtx;
  1414. #endif
  1415. int needDH = 0;
  1416. int nonBlocking = 0;
  1417. int simulateWantWrite = 0;
  1418. int fewerPackets = 0;
  1419. #ifdef HAVE_PK_CALLBACKS
  1420. int pkCallbacks = 0;
  1421. PkCbInfo pkCbInfo;
  1422. #endif
  1423. int wc_shutdown = 0;
  1424. int resume = 0;
  1425. int resumeCount = 0;
  1426. int loops = 1;
  1427. int cnt = 0;
  1428. int echoData = 0;
  1429. int block = TEST_BUFFER_SIZE;
  1430. size_t throughput = 0;
  1431. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  1432. short minRsaKeyBits = DEFAULT_MIN_RSAKEY_BITS;
  1433. short minEccKeyBits = DEFAULT_MIN_ECCKEY_BITS;
  1434. int doListen = 1;
  1435. int crlFlags = 0;
  1436. int ret;
  1437. int err = 0;
  1438. char* serverReadyFile = NULL;
  1439. char* alpnList = NULL;
  1440. unsigned char alpn_opt = 0;
  1441. char* cipherList = NULL;
  1442. int useDefCipherList = 0;
  1443. const char* verifyCert;
  1444. const char* ourCert;
  1445. const char* ourKey;
  1446. const char* ourDhParam = dhParamFile;
  1447. tcp_ready* readySignal = NULL;
  1448. int argc = ((func_args*)args)->argc;
  1449. char** argv = ((func_args*)args)->argv;
  1450. #ifdef WOLFSSL_TRUST_PEER_CERT
  1451. const char* trustCert = NULL;
  1452. #endif
  1453. #ifndef NO_PSK
  1454. int sendPskIdentityHint = 1;
  1455. #endif
  1456. #ifdef HAVE_SNI
  1457. char* sniHostName = NULL;
  1458. #endif
  1459. #ifdef HAVE_TRUSTED_CA
  1460. int trustedCaKeyId = 0;
  1461. #endif /* HAVE_TRUSTED_CA */
  1462. #ifdef HAVE_OCSP
  1463. int useOcsp = 0;
  1464. char* ocspUrl = NULL;
  1465. #endif
  1466. #ifdef HAVE_WNR
  1467. const char* wnrConfigFile = wnrConfig;
  1468. #endif
  1469. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1470. #ifdef WOLFSSL_TLS13
  1471. int noPskDheKe = 0;
  1472. #ifdef HAVE_SUPPORTED_CURVES
  1473. int onlyPskDheKe = 0;
  1474. #endif
  1475. #endif
  1476. int updateKeysIVs = 0;
  1477. #ifndef NO_CERTS
  1478. int mutualAuth = 0;
  1479. #endif
  1480. int postHandAuth = 0;
  1481. int sendTicket = 0;
  1482. #ifdef WOLFSSL_EARLY_DATA
  1483. int earlyData = 0;
  1484. #endif
  1485. #ifdef HAVE_SECURE_RENEGOTIATION
  1486. int scr = 0;
  1487. int forceScr = 0;
  1488. #endif /* HAVE_SECURE_RENEGOTIATION */
  1489. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1490. int hrrCookie = 0;
  1491. #endif
  1492. byte mcastID = 0;
  1493. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1494. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1495. int doDhKeyCheck = 1;
  1496. #endif
  1497. #ifdef WOLFSSL_DTLS_CID
  1498. int useDtlsCID = 0;
  1499. char dtlsCID[DTLS_CID_BUFFER_SIZE] = { 0 };
  1500. #endif /* WOLFSSL_DTLS_CID */
  1501. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  1502. char* crlDir = NULL;
  1503. #endif
  1504. #ifdef WOLFSSL_STATIC_MEMORY
  1505. /* Note: Actual memory used is much less, this is the entire buffer buckets,
  1506. * which is partitioned into pools of common sizes. To adjust the buckets
  1507. * sizes see WOLFMEM_BUCKETS in memory.h */
  1508. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  1509. || defined(SESSION_CERTS)
  1510. /* big enough to handle most cases including session certs */
  1511. #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
  1512. ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
  1513. (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
  1514. /* increase is due to EdDSA_Update */
  1515. byte memory[440000];
  1516. #else
  1517. byte memory[320000];
  1518. #endif
  1519. #else
  1520. byte memory[80000];
  1521. #endif
  1522. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  1523. #if !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  1524. WOLFSSL_MEM_CONN_STATS ssl_stats;
  1525. #if defined(DEBUG_WOLFSSL)
  1526. WOLFSSL_MEM_STATS mem_stats;
  1527. #endif
  1528. #endif
  1529. #endif
  1530. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1531. int onlyKeyShare = 0;
  1532. #endif
  1533. #if defined(HAVE_SESSION_TICKET)
  1534. #ifdef WOLFSSL_TLS13
  1535. int noTicketTls13 = 0;
  1536. #endif
  1537. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1538. int noTicketTls12 = 0;
  1539. #endif
  1540. #endif
  1541. int useX25519 = 0;
  1542. int useX448 = 0;
  1543. int usePqc = 0;
  1544. char* pqcAlg = NULL;
  1545. char* altPrivKey = NULL;
  1546. int exitWithRet = 0;
  1547. int loadCertKeyIntoSSLObj = 0;
  1548. #ifdef HAVE_ENCRYPT_THEN_MAC
  1549. int disallowETM = 0;
  1550. #endif
  1551. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1552. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1553. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1554. int useCertFolder = 0;
  1555. #endif
  1556. #ifdef WOLFSSL_SRTP
  1557. const char* dtlsSrtpProfiles = NULL;
  1558. #endif
  1559. #ifdef HAVE_TEST_SESSION_TICKET
  1560. MyTicketCtx myTicketCtx;
  1561. #endif
  1562. #ifdef CAN_FORCE_CURVE
  1563. int force_curve_group_id = 0;
  1564. #endif
  1565. ((func_args*)args)->return_code = -1; /* error state */
  1566. #ifndef NO_RSA
  1567. verifyCert = cliCertFile;
  1568. ourCert = svrCertFile;
  1569. ourKey = svrKeyFile;
  1570. #else
  1571. #ifdef HAVE_ECC
  1572. verifyCert = cliEccCertFile;
  1573. ourCert = eccCertFile;
  1574. ourKey = eccKeyFile;
  1575. #elif defined(HAVE_ED25519)
  1576. verifyCert = cliEdCertFile;
  1577. ourCert = edCertFile;
  1578. ourKey = edKeyFile;
  1579. #elif defined(HAVE_ED448)
  1580. verifyCert = cliEd448CertFile;
  1581. ourCert = ed448CertFile;
  1582. ourKey = ed448KeyFile;
  1583. #else
  1584. verifyCert = NULL;
  1585. ourCert = NULL;
  1586. ourKey = NULL;
  1587. #endif
  1588. #endif
  1589. (void)needDH;
  1590. (void)ourKey;
  1591. (void)ourCert;
  1592. (void)ourDhParam;
  1593. (void)verifyCert;
  1594. (void)doCliCertCheck;
  1595. (void)minDhKeyBits;
  1596. (void)minRsaKeyBits;
  1597. (void)minEccKeyBits;
  1598. (void)alpnList;
  1599. (void)alpn_opt;
  1600. (void)crlFlags;
  1601. (void)readySignal;
  1602. (void)updateKeysIVs;
  1603. #ifndef NO_CERTS
  1604. (void)mutualAuth;
  1605. #endif
  1606. (void)postHandAuth;
  1607. (void)sendTicket;
  1608. (void)mcastID;
  1609. (void)loadCertKeyIntoSSLObj;
  1610. (void)nonBlocking;
  1611. (void)pqcAlg;
  1612. (void)usePqc;
  1613. (void)altPrivKey;
  1614. #ifdef WOLFSSL_TIRTOS
  1615. fdOpenSession(Task_self());
  1616. #endif
  1617. #ifdef WOLFSSL_VXWORKS
  1618. useAnyAddr = 1;
  1619. #else
  1620. /* Reinitialize the global myVerifyAction. */
  1621. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1622. /* Not Used: h, z, W, X */
  1623. while ((ch = mygetopt_long(argc, argv, "?:"
  1624. "abc:defgijk:l:mop:q:rstu;v:wxy"
  1625. "A:B:C:D:E:FGH:IJ;KL:MNO:PQR:S:T;UVYZ:"
  1626. "01:23:4:567:89"
  1627. "@#", long_options, 0)) != -1) {
  1628. switch (ch) {
  1629. case '?' :
  1630. if(myoptarg!=NULL) {
  1631. lng_index = atoi(myoptarg);
  1632. if(lng_index<0||lng_index>1){
  1633. lng_index = 0;
  1634. }
  1635. }
  1636. Usage();
  1637. XEXIT_T(EXIT_SUCCESS);
  1638. case 257 :
  1639. lng_index = 0;
  1640. Usage();
  1641. XEXIT_T(EXIT_SUCCESS);
  1642. case 258 :
  1643. lng_index = 1;
  1644. Usage();
  1645. XEXIT_T(EXIT_SUCCESS);
  1646. case 'x' :
  1647. runWithErrors = 1;
  1648. break;
  1649. case 'd' :
  1650. doCliCertCheck = 0;
  1651. break;
  1652. case 'V' :
  1653. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  1654. disableCRL = 1;
  1655. #endif
  1656. break;
  1657. case 'b' :
  1658. useAnyAddr = 1;
  1659. break;
  1660. case 's' :
  1661. usePsk = 1;
  1662. break;
  1663. case 'j' :
  1664. usePskPlus = 1;
  1665. break;
  1666. case 'u' :
  1667. doDTLS = 1;
  1668. dtlsUDP = 1;
  1669. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1670. defined(WOLFSSL_DTLS)
  1671. dtlsMTU = atoi(myoptarg);
  1672. #endif
  1673. break;
  1674. #ifdef WOLFSSL_SRTP
  1675. case 260:
  1676. doDTLS = 1;
  1677. dtlsUDP = 1;
  1678. dtlsSrtpProfiles = myoptarg != NULL ? myoptarg :
  1679. "SRTP_AES128_CM_SHA1_80";
  1680. printf("Using SRTP Profile(s): %s\n", dtlsSrtpProfiles);
  1681. break;
  1682. #endif
  1683. case 'G' :
  1684. #ifdef WOLFSSL_SCTP
  1685. doDTLS = 1;
  1686. dtlsUDP = 1;
  1687. dtlsSCTP = 1;
  1688. #endif
  1689. break;
  1690. case 'f' :
  1691. fewerPackets = 1;
  1692. break;
  1693. case 'R' :
  1694. serverReadyFile = myoptarg;
  1695. break;
  1696. case 'r' :
  1697. #ifndef NO_SESSION_CACHE
  1698. resume = 1;
  1699. #endif
  1700. break;
  1701. case 'P' :
  1702. #ifdef HAVE_PK_CALLBACKS
  1703. pkCallbacks = 1;
  1704. #endif
  1705. break;
  1706. case 'p' :
  1707. port = (word16)atoi(myoptarg);
  1708. break;
  1709. case 'w' :
  1710. wc_shutdown = 1;
  1711. break;
  1712. case 'v' :
  1713. if (myoptarg[0] == 'd') {
  1714. version = SERVER_DOWNGRADE_VERSION;
  1715. break;
  1716. }
  1717. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1718. else if (myoptarg[0] == 'e') {
  1719. version = EITHER_DOWNGRADE_VERSION;
  1720. #ifndef NO_CERTS
  1721. loadCertKeyIntoSSLObj = 1;
  1722. #endif
  1723. break;
  1724. }
  1725. #endif
  1726. version = atoi(myoptarg);
  1727. if (version < 0 || version > 4) {
  1728. Usage();
  1729. XEXIT_T(MY_EX_USAGE);
  1730. }
  1731. break;
  1732. case 'l' :
  1733. cipherList = myoptarg;
  1734. break;
  1735. case 'H' :
  1736. if (XSTRCMP(myoptarg, "defCipherList") == 0) {
  1737. printf("Using default cipher list for testing\n");
  1738. useDefCipherList = 1;
  1739. }
  1740. else if (XSTRCMP(myoptarg, "exitWithRet") == 0) {
  1741. printf("Skip exit() for testing\n");
  1742. exitWithRet = 1;
  1743. }
  1744. else if (XSTRCMP(myoptarg, "verifyFail") == 0) {
  1745. printf("Verify should fail\n");
  1746. myVerifyAction = VERIFY_FORCE_FAIL;
  1747. }
  1748. else if (XSTRCMP(myoptarg, "verifyInfo") == 0) {
  1749. printf("Verify should use preverify (just show info)\n");
  1750. myVerifyAction = VERIFY_USE_PREVERFIY;
  1751. }
  1752. else if (XSTRCMP(myoptarg, "loadSSL") == 0) {
  1753. printf("Also load cert/key into wolfSSL object\n");
  1754. #ifndef NO_CERTS
  1755. loadCertKeyIntoSSLObj = 2;
  1756. #endif
  1757. }
  1758. else if (XSTRCMP(myoptarg, "loadSSLOnly") == 0) {
  1759. printf("Only load cert/key into wolfSSL object\n");
  1760. #ifndef NO_CERTS
  1761. loadCertKeyIntoSSLObj = 1;
  1762. #endif
  1763. }
  1764. else if (XSTRCMP(myoptarg, "disallowETM") == 0) {
  1765. printf("Disallow Encrypt-Then-MAC\n");
  1766. #ifdef HAVE_ENCRYPT_THEN_MAC
  1767. disallowETM = 1;
  1768. #endif
  1769. }
  1770. else if (XSTRCMP(myoptarg, "overrideDateErr") == 0) {
  1771. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1772. myVerifyAction = VERIFY_OVERRIDE_DATE_ERR;
  1773. #endif
  1774. }
  1775. else {
  1776. Usage();
  1777. XEXIT_T(MY_EX_USAGE);
  1778. }
  1779. break;
  1780. case 'A' :
  1781. verifyCert = myoptarg;
  1782. break;
  1783. case 'c' :
  1784. ourCert = myoptarg;
  1785. break;
  1786. case 'k' :
  1787. ourKey = myoptarg;
  1788. break;
  1789. case 'D' :
  1790. #ifndef NO_DH
  1791. ourDhParam = myoptarg;
  1792. #endif
  1793. break;
  1794. case 'Z' :
  1795. #ifndef NO_DH
  1796. minDhKeyBits = atoi(myoptarg);
  1797. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  1798. Usage();
  1799. XEXIT_T(MY_EX_USAGE);
  1800. }
  1801. #endif
  1802. break;
  1803. case 'N':
  1804. nonBlocking = 1;
  1805. break;
  1806. case 'S' :
  1807. #ifdef HAVE_SNI
  1808. sniHostName = myoptarg;
  1809. #endif
  1810. break;
  1811. case 'o' :
  1812. #ifdef HAVE_OCSP
  1813. useOcsp = 1;
  1814. #endif
  1815. break;
  1816. case 'O' :
  1817. #ifdef HAVE_OCSP
  1818. useOcsp = 1;
  1819. ocspUrl = myoptarg;
  1820. #endif
  1821. break;
  1822. case 'a' :
  1823. #ifdef HAVE_ANON
  1824. useAnon = 1;
  1825. #endif
  1826. break;
  1827. case 'I':
  1828. #ifndef NO_PSK
  1829. sendPskIdentityHint = 0;
  1830. #endif
  1831. break;
  1832. case 'L' :
  1833. #ifdef HAVE_ALPN
  1834. alpnList = myoptarg;
  1835. if (alpnList[0] == 'C' && alpnList[1] == ':')
  1836. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  1837. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  1838. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  1839. else {
  1840. Usage();
  1841. XEXIT_T(MY_EX_USAGE);
  1842. }
  1843. alpnList += 2;
  1844. #endif
  1845. break;
  1846. case 'i' :
  1847. loops = -1;
  1848. break;
  1849. case 'C' :
  1850. loops = atoi(myoptarg);
  1851. if (loops <= 0) {
  1852. Usage();
  1853. XEXIT_T(MY_EX_USAGE);
  1854. }
  1855. break;
  1856. case 'e' :
  1857. echoData = 1;
  1858. break;
  1859. case 'B':
  1860. throughput = (size_t)atol(myoptarg);
  1861. for (; *myoptarg != '\0'; myoptarg++) {
  1862. if (*myoptarg == ',') {
  1863. block = atoi(myoptarg + 1);
  1864. break;
  1865. }
  1866. }
  1867. if (throughput == 0 || block <= 0) {
  1868. Usage();
  1869. XEXIT_T(MY_EX_USAGE);
  1870. }
  1871. break;
  1872. #ifdef WOLFSSL_TRUST_PEER_CERT
  1873. case 'E' :
  1874. trustCert = myoptarg;
  1875. break;
  1876. #endif
  1877. case 'q' :
  1878. #ifdef HAVE_WNR
  1879. wnrConfigFile = myoptarg;
  1880. #endif
  1881. break;
  1882. case 'g' :
  1883. useWebServerMsg = 1;
  1884. break;
  1885. case 'y' :
  1886. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) \
  1887. && !defined(NO_DH)
  1888. onlyKeyShare = 1;
  1889. #endif
  1890. break;
  1891. case 'Y' :
  1892. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) \
  1893. && defined(HAVE_ECC)
  1894. onlyKeyShare = 2;
  1895. #endif
  1896. break;
  1897. case 't' :
  1898. #ifdef HAVE_CURVE25519
  1899. useX25519 = 1;
  1900. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1901. onlyKeyShare = 2;
  1902. #endif
  1903. #endif
  1904. break;
  1905. case 'K' :
  1906. #ifdef WOLFSSL_TLS13
  1907. noPskDheKe = 1;
  1908. #endif
  1909. break;
  1910. case 'T' :
  1911. #if defined(HAVE_SESSION_TICKET)
  1912. if (XSTRLEN(myoptarg) == 0) {
  1913. #if defined(WOLFSSL_TLS13)
  1914. noTicketTls13 = 1;
  1915. #endif
  1916. }
  1917. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1918. else if (XSTRCMP(myoptarg, "a") == 0) {
  1919. noTicketTls12 = 1;
  1920. #if defined(WOLFSSL_TLS13)
  1921. noTicketTls13 = 1;
  1922. #endif
  1923. }
  1924. else if (XSTRCMP(myoptarg, "o") == 0) {
  1925. noTicketTls12 = 1;
  1926. }
  1927. else if (XSTRCMP(myoptarg, "n") == 0) {
  1928. #if defined(WOLFSSL_TLS13)
  1929. noTicketTls13 = 1;
  1930. #endif
  1931. }
  1932. #endif
  1933. else {
  1934. Usage();
  1935. XEXIT_T(MY_EX_USAGE);
  1936. }
  1937. #endif
  1938. break;
  1939. case 'U' :
  1940. #ifdef WOLFSSL_TLS13
  1941. updateKeysIVs = 1;
  1942. #endif
  1943. break;
  1944. #ifndef NO_CERTS
  1945. case 'F' :
  1946. mutualAuth = 1;
  1947. break;
  1948. #endif
  1949. case 'Q' :
  1950. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1951. postHandAuth = 1;
  1952. doCliCertCheck = 0;
  1953. #endif
  1954. break;
  1955. case 'J' :
  1956. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1957. hrrCookie = 1;
  1958. if (XSTRCMP(myoptarg, "n") == 0)
  1959. hrrCookie = -1;
  1960. #endif
  1961. break;
  1962. case 'M' :
  1963. #ifdef HAVE_SECURE_RENEGOTIATION
  1964. scr = 1;
  1965. #endif /* HAVE_SECURE_RENEGOTIATION */
  1966. break;
  1967. case 'm' :
  1968. #ifdef HAVE_SECURE_RENEGOTIATION
  1969. scr = 1;
  1970. forceScr = 1;
  1971. #endif /* HAVE_SECURE_RENEGOTIATION */
  1972. break;
  1973. case '0' :
  1974. #ifdef WOLFSSL_EARLY_DATA
  1975. earlyData = 1;
  1976. #endif
  1977. break;
  1978. case '1' :
  1979. lng_index = atoi(myoptarg);
  1980. if(lng_index<0||lng_index>1){
  1981. lng_index = 0;
  1982. }
  1983. break;
  1984. case '2' :
  1985. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1986. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1987. doDhKeyCheck = 0;
  1988. #endif
  1989. break;
  1990. case '3' :
  1991. #ifdef WOLFSSL_MULTICAST
  1992. doMcast = 1;
  1993. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  1994. #endif
  1995. break;
  1996. case '4' :
  1997. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  1998. XMEMSET(&dtlsCtx, 0, sizeof(dtlsCtx));
  1999. doBlockSeq = 1;
  2000. dtlsCtx.blockSeq = (word32)atoi(myoptarg);
  2001. #endif
  2002. break;
  2003. case '5' :
  2004. #ifdef HAVE_TRUSTED_CA
  2005. trustedCaKeyId = 1;
  2006. #endif /* HAVE_TRUSTED_CA */
  2007. break;
  2008. case '6' :
  2009. #ifdef WOLFSSL_ASYNC_IO
  2010. nonBlocking = 1;
  2011. simulateWantWrite = 1;
  2012. #else
  2013. LOG_ERROR("Ignoring -6 since async I/O support not "
  2014. "compiled in.\n");
  2015. #endif
  2016. break;
  2017. case '7' :
  2018. minVersion = atoi(myoptarg);
  2019. if (minVersion < 0 || minVersion > 4) {
  2020. Usage();
  2021. XEXIT_T(MY_EX_USAGE);
  2022. }
  2023. break;
  2024. case '8' :
  2025. #ifdef HAVE_CURVE448
  2026. useX448 = 1;
  2027. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2028. onlyKeyShare = 2;
  2029. #endif
  2030. #endif
  2031. break;
  2032. case '9' :
  2033. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2034. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2035. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2036. useCertFolder = 1;
  2037. break;
  2038. #endif
  2039. case '@' :
  2040. {
  2041. #ifdef HAVE_WC_INTROSPECTION
  2042. const char *conf_args = wolfSSL_configure_args();
  2043. if (conf_args) {
  2044. puts(conf_args);
  2045. XEXIT_T(EXIT_SUCCESS);
  2046. } else {
  2047. fputs("configure args not compiled in.\n",stderr);
  2048. XEXIT_T(MY_EX_USAGE);
  2049. }
  2050. #else
  2051. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2052. XEXIT_T(MY_EX_USAGE);
  2053. #endif
  2054. }
  2055. case '#' :
  2056. {
  2057. #ifdef HAVE_WC_INTROSPECTION
  2058. const char *cflags = wolfSSL_global_cflags();
  2059. if (cflags) {
  2060. puts(cflags);
  2061. XEXIT_T(EXIT_SUCCESS);
  2062. } else {
  2063. fputs("CFLAGS not compiled in.\n",stderr);
  2064. XEXIT_T(MY_EX_USAGE);
  2065. }
  2066. #else
  2067. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2068. XEXIT_T(MY_EX_USAGE);
  2069. #endif
  2070. }
  2071. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2072. case 256:
  2073. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  2074. wolfsentry_config_path = myoptarg;
  2075. #endif
  2076. break;
  2077. #endif
  2078. #ifdef HAVE_PQC
  2079. case 259:
  2080. usePqc = 1;
  2081. onlyKeyShare = 2;
  2082. pqcAlg = myoptarg;
  2083. break;
  2084. #endif
  2085. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  2086. case 261:
  2087. sendTicket = 1;
  2088. break;
  2089. #endif
  2090. #ifdef CAN_FORCE_CURVE
  2091. case 262: {
  2092. /* Note: this requires TSL1.3 (version >= 4) */
  2093. #ifdef HAVE_ECC
  2094. int j = 0; /* our group index */
  2095. #endif
  2096. if (NULL == myoptarg) {
  2097. #ifdef HAVE_ECC
  2098. int idx = 0; /* ecc curve index */
  2099. #endif
  2100. Usage();
  2101. if (lng_index == 1) {
  2102. /* TODO: Need Japanese translation */
  2103. printf("\nAvailable choices for --force-curve:\n");
  2104. } else {
  2105. printf("\nAvailable choices for --force-curve:\n");
  2106. }
  2107. #ifdef HAVE_ECC
  2108. for (idx=0; ; ++idx) {
  2109. int id = wc_ecc_get_curve_id(idx);
  2110. if (ECC_CURVE_INVALID == id) {
  2111. break;
  2112. }
  2113. for (j=0; group_id_to_text[j].group != 0; ++j) {
  2114. if (XSTRCMP(group_id_to_text[j].name,
  2115. wc_ecc_get_curve_name_from_id(id)) == 0) {
  2116. printf("\t%s\n", group_id_to_text[j].name);
  2117. }
  2118. }
  2119. }
  2120. #endif
  2121. #ifdef HAVE_CURVE25519
  2122. printf("\tCURVE25519\n");
  2123. #endif
  2124. #ifdef HAVE_CURVE448
  2125. printf("\tCURVE448\n");
  2126. #endif
  2127. printf("\n");
  2128. XEXIT_T(EXIT_SUCCESS);
  2129. }
  2130. #ifdef HAVE_ECC
  2131. for (j=0; group_id_to_text[j].group != 0; ++j) {
  2132. if (XSTRCMP(group_id_to_text[j].name, myoptarg) == 0) {
  2133. force_curve_group_id = group_id_to_text[j].group;
  2134. }
  2135. }
  2136. #endif
  2137. #ifdef HAVE_CURVE25519
  2138. if (force_curve_group_id <= 0) {
  2139. if (XSTRCMP(myoptarg, "CURVE25519") == 0) {
  2140. force_curve_group_id = WOLFSSL_ECC_X25519;
  2141. }
  2142. }
  2143. #endif
  2144. #ifdef HAVE_CURVE448
  2145. if (force_curve_group_id <= 0) {
  2146. if (XSTRCMP(myoptarg, "CURVE448") == 0) {
  2147. force_curve_group_id = WOLFSSL_ECC_X448;
  2148. }
  2149. }
  2150. #endif
  2151. if (force_curve_group_id <= 0) {
  2152. if (lng_index == 1) {
  2153. /* TODO: Need Japanese translation */
  2154. LOG_ERROR("Invalid curve '%s'\n", myoptarg);
  2155. } else {
  2156. LOG_ERROR("Invalid curve '%s'\n", myoptarg);
  2157. }
  2158. XEXIT_T(EXIT_FAILURE);
  2159. }
  2160. }
  2161. break;
  2162. #endif /* CAN_FORCE_CURVE */
  2163. #ifdef WOLFSSL_DTLS_CID
  2164. case 263:
  2165. useDtlsCID = 1;
  2166. if (myoptarg != NULL) {
  2167. if (XSTRLEN(myoptarg) >= DTLS_CID_BUFFER_SIZE) {
  2168. err_sys("provided connection ID is too big");
  2169. }
  2170. else {
  2171. strcpy(dtlsCID, myoptarg);
  2172. }
  2173. }
  2174. break;
  2175. #endif /* WOLFSSL_CID */
  2176. case 264:
  2177. #ifdef HAVE_SUPPORTED_CURVES
  2178. #ifdef WOLFSSL_TLS13
  2179. onlyPskDheKe = 1;
  2180. #endif
  2181. #endif
  2182. break;
  2183. case 265:
  2184. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  2185. crlDir = myoptarg;
  2186. #endif
  2187. break;
  2188. case 266:
  2189. quieter = 1;
  2190. break;
  2191. #ifdef WOLFSSL_DUAL_ALG_CERTS
  2192. case 267:
  2193. altPrivKey = myoptarg;
  2194. break;
  2195. #endif
  2196. case -1:
  2197. default:
  2198. Usage();
  2199. XEXIT_T(MY_EX_USAGE);
  2200. }
  2201. }
  2202. myoptind = 0; /* reset for test cases */
  2203. #endif /* !WOLFSSL_VXWORKS */
  2204. /* Can only use DTLS over UDP or SCTP, can't do both. */
  2205. if (dtlsUDP && dtlsSCTP) {
  2206. err_sys_ex(runWithErrors, "Cannot use DTLS with both UDP and SCTP.");
  2207. }
  2208. /* sort out DTLS versus TLS versions */
  2209. if (version == CLIENT_INVALID_VERSION) {
  2210. if (doDTLS)
  2211. version = CLIENT_DTLS_DEFAULT_VERSION;
  2212. else
  2213. version = CLIENT_DEFAULT_VERSION;
  2214. }
  2215. else {
  2216. if (doDTLS) {
  2217. if (version == 3) {
  2218. version = -2;
  2219. }
  2220. else if (version == 4) {
  2221. #ifdef WOLFSSL_DTLS13
  2222. version = -4;
  2223. #else
  2224. err_sys_ex(runWithErrors, "Bad DTLS version");
  2225. #endif /* WOLFSSL_DTLS13 */
  2226. }
  2227. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2228. else if (version == EITHER_DOWNGRADE_VERSION) {
  2229. version = -3;
  2230. }
  2231. #endif
  2232. else if (version == 2)
  2233. version = -1;
  2234. }
  2235. }
  2236. #ifndef HAVE_SESSION_TICKET
  2237. if ((version >= 4) && resume) {
  2238. LOG_ERROR("Can't do TLS 1.3 resumption; need session tickets!\n");
  2239. }
  2240. #endif
  2241. #ifdef HAVE_WNR
  2242. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  2243. err_sys_ex(runWithErrors, "can't load whitewood net random config "
  2244. "file");
  2245. #endif
  2246. #ifdef HAVE_PQC
  2247. if (usePqc) {
  2248. if (version == SERVER_DOWNGRADE_VERSION ||
  2249. version == EITHER_DOWNGRADE_VERSION) {
  2250. LOG_ERROR(
  2251. "WARNING: If a TLS 1.3 connection is not negotiated, you "
  2252. "will not be using a post-quantum group.\n");
  2253. } else if (version != 4 && version != -4) {
  2254. err_sys("can only use post-quantum groups with TLS 1.3 or DTLS 1.3");
  2255. }
  2256. }
  2257. #endif
  2258. switch (version) {
  2259. #ifndef NO_OLD_TLS
  2260. #ifdef WOLFSSL_ALLOW_SSLV3
  2261. case 0:
  2262. method = wolfSSLv3_server_method_ex;
  2263. break;
  2264. #endif
  2265. #ifndef NO_TLS
  2266. #ifdef WOLFSSL_ALLOW_TLSV10
  2267. case 1:
  2268. method = wolfTLSv1_server_method_ex;
  2269. break;
  2270. #endif
  2271. case 2:
  2272. method = wolfTLSv1_1_server_method_ex;
  2273. break;
  2274. #endif /* !NO_TLS */
  2275. #endif /* !NO_OLD_TLS */
  2276. #ifndef NO_TLS
  2277. #ifndef WOLFSSL_NO_TLS12
  2278. case 3:
  2279. method = wolfTLSv1_2_server_method_ex;
  2280. break;
  2281. #endif
  2282. #ifdef WOLFSSL_TLS13
  2283. case 4:
  2284. method = wolfTLSv1_3_server_method_ex;
  2285. break;
  2286. #endif
  2287. case SERVER_DOWNGRADE_VERSION:
  2288. if (!doDTLS) {
  2289. method = wolfSSLv23_server_method_ex;
  2290. }
  2291. else {
  2292. #ifdef WOLFSSL_DTLS
  2293. method = wolfDTLS_server_method_ex;
  2294. #else
  2295. err_sys_ex(runWithErrors, "version not supported");
  2296. #endif /* WOLFSSL_DTLS */
  2297. }
  2298. break;
  2299. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2300. case EITHER_DOWNGRADE_VERSION:
  2301. method = wolfSSLv23_method_ex;
  2302. break;
  2303. #endif
  2304. #endif /* NO_TLS */
  2305. #ifdef WOLFSSL_DTLS
  2306. #ifndef NO_OLD_TLS
  2307. case -1:
  2308. method = wolfDTLSv1_server_method_ex;
  2309. break;
  2310. #endif
  2311. #ifndef WOLFSSL_NO_TLS12
  2312. case -2:
  2313. method = wolfDTLSv1_2_server_method_ex;
  2314. break;
  2315. #endif
  2316. #ifdef WOLFSSL_DTLS13
  2317. case -4:
  2318. method = wolfDTLSv1_3_server_method_ex;
  2319. break;
  2320. #endif
  2321. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2322. case -3:
  2323. method = wolfDTLSv1_2_method_ex;
  2324. break;
  2325. #endif
  2326. #endif
  2327. default:
  2328. err_sys_ex(runWithErrors, "Bad SSL version");
  2329. }
  2330. if (method == NULL)
  2331. err_sys_ex(runWithErrors, "unable to get method");
  2332. #ifdef WOLFSSL_STATIC_MEMORY
  2333. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  2334. /* print off helper buffer sizes for use with static memory
  2335. * printing to stderr in case of debug mode turned on */
  2336. LOG_ERROR("static memory management size = %d\n",
  2337. wolfSSL_MemoryPaddingSz());
  2338. LOG_ERROR("calculated optimum general buffer size = %d\n",
  2339. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2340. LOG_ERROR("calculated optimum IO buffer size = %d\n",
  2341. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2342. WOLFMEM_IO_POOL_FIXED));
  2343. #endif /* DEBUG_WOLFSSL */
  2344. if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),0,1)
  2345. != WOLFSSL_SUCCESS)
  2346. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2347. /* load in a buffer for IO */
  2348. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2349. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)
  2350. != WOLFSSL_SUCCESS)
  2351. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2352. #else
  2353. if (method != NULL) {
  2354. ctx = SSL_CTX_new(method(NULL));
  2355. }
  2356. #ifdef WOLFSSL_CALLBACKS
  2357. wolfSSL_CTX_set_msg_callback(ctx, msgDebugCb);
  2358. #endif
  2359. #endif /* WOLFSSL_STATIC_MEMORY */
  2360. if (ctx == NULL)
  2361. err_sys_ex(catastrophic, "unable to get ctx");
  2362. if (minVersion != SERVER_INVALID_VERSION) {
  2363. #ifdef WOLFSSL_DTLS
  2364. if (doDTLS) {
  2365. switch (minVersion) {
  2366. #ifdef WOLFSSL_DTLS13
  2367. case 4:
  2368. minVersion = WOLFSSL_DTLSV1_3;
  2369. break;
  2370. #endif /* WOLFSSL_DTLS13 */
  2371. case 3:
  2372. minVersion = WOLFSSL_DTLSV1_2;
  2373. break;
  2374. case 2:
  2375. minVersion = WOLFSSL_DTLSV1;
  2376. break;
  2377. }
  2378. }
  2379. #endif /* WOLFSSL_DTLS13 */
  2380. if (wolfSSL_CTX_SetMinVersion(ctx, minVersion) != WOLFSSL_SUCCESS)
  2381. err_sys_ex(catastrophic, "can't set minimum downgrade version");
  2382. }
  2383. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2384. /* Restore wolfSSL verify defaults */
  2385. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL);
  2386. #endif
  2387. #ifdef WOLFSSL_SRTP
  2388. if (dtlsSrtpProfiles != NULL) {
  2389. if (wolfSSL_CTX_set_tlsext_use_srtp(ctx, dtlsSrtpProfiles)
  2390. != WOLFSSL_SUCCESS) {
  2391. err_sys_ex(catastrophic, "unable to set DTLS SRTP profile");
  2392. }
  2393. }
  2394. #endif
  2395. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2396. if (wolfsentry_setup(&wolfsentry, wolfsentry_config_path,
  2397. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN) < 0) {
  2398. err_sys("unable to initialize wolfSentry");
  2399. }
  2400. if (wolfSSL_CTX_set_AcceptFilter(
  2401. ctx,
  2402. (NetworkFilterCallback_t)wolfSentry_NetworkFilterCallback,
  2403. wolfsentry) < 0) {
  2404. err_sys_ex(catastrophic,
  2405. "unable to install wolfSentry_NetworkFilterCallback");
  2406. }
  2407. #endif
  2408. if (simulateWantWrite)
  2409. {
  2410. #ifdef USE_WOLFSSL_IO
  2411. wolfSSL_CTX_SetIOSend(ctx, SimulateWantWriteIOSendCb);
  2412. #endif
  2413. }
  2414. #ifdef HAVE_TEST_SESSION_TICKET
  2415. if (TicketInit() != 0)
  2416. err_sys_ex(catastrophic, "unable to setup Session Ticket Key context");
  2417. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  2418. XMEMSET(&myTicketCtx, 0, sizeof(myTicketCtx));
  2419. wolfSSL_CTX_set_TicketEncCtx(ctx, &myTicketCtx);
  2420. #endif
  2421. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_STATIC_EPHEMERAL)
  2422. /* used for testing only to set a static/fixed ephemeral key
  2423. for use with the sniffer */
  2424. #if defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  2425. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  2426. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_ECDH,
  2427. "./certs/statickeys/ecc-secp256r1.pem", 0, WOLFSSL_FILETYPE_PEM);
  2428. if (ret != 0) {
  2429. err_sys_ex(runWithErrors, "error loading static ECDH key");
  2430. }
  2431. {
  2432. const byte* key = NULL;
  2433. word32 keySz = 0;
  2434. /* example for getting pointer to loaded static ephemeral key */
  2435. wolfSSL_CTX_get_ephemeral_key(ctx, WC_PK_TYPE_ECDH, &key, &keySz);
  2436. (void)key;
  2437. (void)keySz;
  2438. }
  2439. #endif
  2440. #ifndef NO_DH
  2441. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_DH,
  2442. "./certs/statickeys/dh-ffdhe2048.pem", 0, WOLFSSL_FILETYPE_PEM);
  2443. if (ret != 0) {
  2444. err_sys_ex(runWithErrors, "error loading static DH key");
  2445. }
  2446. #endif
  2447. #ifdef HAVE_CURVE25519
  2448. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_CURVE25519,
  2449. "./certs/statickeys/x25519.pem", 0, WOLFSSL_FILETYPE_PEM);
  2450. if (ret != 0) {
  2451. err_sys_ex(runWithErrors, "error loading static X25519 key");
  2452. }
  2453. #endif
  2454. #endif /* WOLFSSL_SNIFFER && WOLFSSL_STATIC_EPHEMERAL */
  2455. if (cipherList && !useDefCipherList) {
  2456. if (SSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS)
  2457. err_sys_ex(runWithErrors, "server can't set custom cipher list");
  2458. }
  2459. #ifdef WOLFSSL_LEANPSK
  2460. if (!usePsk) {
  2461. usePsk = 1;
  2462. }
  2463. #endif
  2464. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2465. !defined(HAVE_ED448)
  2466. if (!usePsk) {
  2467. usePsk = 1;
  2468. }
  2469. #endif
  2470. if (fewerPackets)
  2471. wolfSSL_CTX_set_group_messages(ctx);
  2472. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2473. defined(WOLFSSL_DTLS)
  2474. if (dtlsMTU)
  2475. wolfSSL_CTX_dtls_set_mtu(ctx, (unsigned short)dtlsMTU);
  2476. #endif
  2477. #ifdef WOLFSSL_SCTP
  2478. if (dtlsSCTP)
  2479. wolfSSL_CTX_dtls_set_sctp(ctx);
  2480. #endif
  2481. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2482. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2483. #endif
  2484. #if !defined(NO_CERTS)
  2485. if ((!usePsk || usePskPlus) && !useAnon && !(loadCertKeyIntoSSLObj == 1)) {
  2486. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2487. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  2488. server_cert_der_2048, sizeof_server_cert_der_2048,
  2489. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2490. err_sys_ex(catastrophic, "can't load server cert buffer");
  2491. #elif !defined(TEST_LOAD_BUFFER)
  2492. if (SSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2493. != WOLFSSL_SUCCESS)
  2494. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2495. "and run from wolfSSL home dir");
  2496. #else
  2497. /* loads cert chain file using buffer API */
  2498. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2499. #endif
  2500. }
  2501. #endif
  2502. #ifndef NO_DH
  2503. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2504. != WOLFSSL_SUCCESS) {
  2505. err_sys_ex(runWithErrors, "Error setting minimum DH key size");
  2506. }
  2507. #endif
  2508. #ifndef NO_RSA
  2509. if (wolfSSL_CTX_SetMinRsaKey_Sz(ctx, minRsaKeyBits) != WOLFSSL_SUCCESS){
  2510. err_sys_ex(runWithErrors, "Error setting minimum RSA key size");
  2511. }
  2512. #endif
  2513. #ifdef HAVE_ECC
  2514. if (wolfSSL_CTX_SetMinEccKey_Sz(ctx, minEccKeyBits) != WOLFSSL_SUCCESS){
  2515. err_sys_ex(runWithErrors, "Error setting minimum ECC key size");
  2516. }
  2517. #endif
  2518. #if !defined(NO_CERTS)
  2519. #ifdef HAVE_PK_CALLBACKS
  2520. pkCbInfo.ourKey = ourKey;
  2521. #endif
  2522. if ((!usePsk || usePskPlus) && !useAnon
  2523. && !(loadCertKeyIntoSSLObj == 1)
  2524. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2525. && !pkCallbacks
  2526. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2527. ) {
  2528. #ifdef NO_FILESYSTEM
  2529. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2530. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2531. err_sys_ex(catastrophic, "can't load server private key buffer");
  2532. #elif !defined(TEST_LOAD_BUFFER)
  2533. if (SSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  2534. != WOLFSSL_SUCCESS)
  2535. err_sys_ex(catastrophic, "can't load server private key file, "
  2536. "check file and run from wolfSSL home dir");
  2537. #ifdef WOLFSSL_DUAL_ALG_CERTS
  2538. if ((altPrivKey != NULL) &&
  2539. wolfSSL_CTX_use_AltPrivateKey_file(ctx, altPrivKey,
  2540. WOLFSSL_FILETYPE_PEM)
  2541. != WOLFSSL_SUCCESS)
  2542. err_sys_ex(catastrophic, "can't load alt private key file, "
  2543. "check file and run from wolfSSL home dir");
  2544. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  2545. #else
  2546. /* loads private key file using buffer API */
  2547. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  2548. #endif
  2549. }
  2550. #endif
  2551. if (usePsk || usePskPlus) {
  2552. #ifndef NO_PSK
  2553. const char *defaultCipherList = cipherList;
  2554. SSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  2555. #ifdef WOLFSSL_TLS13
  2556. wolfSSL_CTX_set_psk_server_tls13_callback(ctx, my_psk_server_tls13_cb);
  2557. #endif
  2558. if (sendPskIdentityHint == 1)
  2559. SSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  2560. if (defaultCipherList == NULL && !usePskPlus) {
  2561. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2562. #ifdef WOLFSSL_TLS13
  2563. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2564. #ifndef WOLFSSL_NO_TLS12
  2565. ":DHE-PSK-AES128-GCM-SHA256"
  2566. #endif
  2567. ;
  2568. #else
  2569. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2570. #endif
  2571. needDH = 1;
  2572. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  2573. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2574. #ifndef WOLFSSL_NO_TLS12
  2575. ":PSK-AES128-GCM-SHA256"
  2576. #endif
  2577. ;
  2578. #elif defined(HAVE_NULL_CIPHER)
  2579. defaultCipherList = "PSK-NULL-SHA256";
  2580. #elif !defined(NO_AES_CBC)
  2581. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2582. #else
  2583. defaultCipherList = "PSK-AES128-GCM-SHA256";
  2584. #endif
  2585. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2586. != WOLFSSL_SUCCESS)
  2587. err_sys_ex(runWithErrors, "server can't set cipher list 2");
  2588. }
  2589. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2590. #endif /* !NO_PSK */
  2591. }
  2592. #ifndef NO_CERTS
  2593. if (mutualAuth)
  2594. wolfSSL_CTX_mutual_auth(ctx, 1);
  2595. #endif
  2596. #ifdef HAVE_ECC
  2597. /* Use ECDHE key size that matches long term key.
  2598. * Zero means use ctx->privateKeySz.
  2599. * Default ECDHE_SIZE is 32 bytes
  2600. */
  2601. if (wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 0) != WOLFSSL_SUCCESS){
  2602. err_sys_ex(runWithErrors, "Error setting ECDHE size");
  2603. }
  2604. #endif
  2605. if (useAnon) {
  2606. #ifdef HAVE_ANON
  2607. wolfSSL_CTX_allow_anon_cipher(ctx);
  2608. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2609. const char* defaultCipherList;
  2610. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2611. "ADH-AES128-SHA";
  2612. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2613. != WOLFSSL_SUCCESS)
  2614. err_sys_ex(runWithErrors, "server can't set cipher list 4");
  2615. }
  2616. #endif
  2617. }
  2618. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2619. /* if not using PSK, verify peer with certs
  2620. if using PSK Plus then verify peer certs except PSK suites */
  2621. if (doCliCertCheck && (usePsk == 0 || usePskPlus) && useAnon == 0) {
  2622. unsigned int verify_flags = 0;
  2623. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  2624. (usePskPlus ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  2625. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  2626. (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR ||
  2627. myVerifyAction == VERIFY_FORCE_FAIL) ? myVerify : NULL);
  2628. #ifdef TEST_BEFORE_DATE
  2629. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2630. #endif
  2631. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2632. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2633. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2634. if (useCertFolder) {
  2635. WOLFSSL_X509_STORE *store;
  2636. WOLFSSL_X509_LOOKUP *lookup;
  2637. store = wolfSSL_CTX_get_cert_store(ctx);
  2638. if (store == NULL) {
  2639. wolfSSL_CTX_free(ctx); ctx = NULL;
  2640. err_sys("can't get WOLFSSL_X509_STORE");
  2641. }
  2642. lookup = wolfSSL_X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  2643. if (lookup == NULL) {
  2644. wolfSSL_CTX_free(ctx); ctx = NULL;
  2645. err_sys("can't add lookup");
  2646. }
  2647. if (wolfSSL_X509_LOOKUP_ctrl(lookup, WOLFSSL_X509_L_ADD_DIR, caCertFolder,
  2648. X509_FILETYPE_PEM, NULL) != WOLFSSL_SUCCESS) {
  2649. err_sys("X509_LOOKUP_ctrl w/ L_ADD_DIR failed");
  2650. }
  2651. } else {
  2652. #endif
  2653. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2654. verify_flags) != WOLFSSL_SUCCESS) {
  2655. err_sys_ex(catastrophic,
  2656. "can't load ca file, Please run from wolfSSL home dir");
  2657. }
  2658. #ifdef WOLFSSL_TRUST_PEER_CERT
  2659. if (trustCert) {
  2660. if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  2661. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2662. err_sys_ex(runWithErrors, "can't load trusted peer cert file");
  2663. }
  2664. }
  2665. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2666. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2667. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2668. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2669. }
  2670. #endif
  2671. }
  2672. #endif
  2673. #ifdef WOLFSSL_SNIFFER
  2674. if (cipherList == NULL && version < 4) {
  2675. /* static RSA or static ECC cipher suites */
  2676. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2677. if (SSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2678. err_sys_ex(runWithErrors, "server can't set cipher list 3");
  2679. }
  2680. }
  2681. #endif
  2682. #ifdef HAVE_SNI
  2683. if (sniHostName)
  2684. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  2685. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS)
  2686. err_sys_ex(runWithErrors, "UseSNI failed");
  2687. #endif
  2688. #ifdef USE_WINDOWS_API
  2689. if (port == 0) {
  2690. /* Generate random port for testing */
  2691. port = GetRandomPort();
  2692. }
  2693. #endif /* USE_WINDOWS_API */
  2694. #ifdef WOLFSSL_ASYNC_CRYPT
  2695. ret = wolfAsync_DevOpen(&devId);
  2696. if (ret < 0) {
  2697. LOG_ERROR("Async device open failed\nRunning without async\n");
  2698. }
  2699. wolfSSL_CTX_SetDevId(ctx, devId);
  2700. #endif /* WOLFSSL_ASYNC_CRYPT */
  2701. #ifdef WOLFSSL_TLS13
  2702. if (noPskDheKe)
  2703. wolfSSL_CTX_no_dhe_psk(ctx);
  2704. #ifdef HAVE_SUPPORTED_CURVES
  2705. if (onlyPskDheKe)
  2706. wolfSSL_CTX_only_dhe_psk(ctx);
  2707. #endif
  2708. #endif
  2709. #ifdef HAVE_SESSION_TICKET
  2710. #ifdef WOLFSSL_TLS13
  2711. if (noTicketTls13)
  2712. wolfSSL_CTX_no_ticket_TLSv13(ctx);
  2713. #endif
  2714. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  2715. if (noTicketTls12)
  2716. wolfSSL_CTX_NoTicketTLSv12(ctx);
  2717. #endif
  2718. #endif
  2719. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  2720. if (!disableCRL) {
  2721. /* Need to load CA's to confirm CRL signatures */
  2722. unsigned int verify_flags = 0;
  2723. #ifdef TEST_BEFORE_DATE
  2724. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2725. #endif
  2726. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2727. verify_flags) != WOLFSSL_SUCCESS) {
  2728. err_sys_ex(catastrophic,
  2729. "can't load ca file, Please run from wolfSSL home dir");
  2730. }
  2731. #ifdef HAVE_CRL_MONITOR
  2732. crlFlags = WOLFSSL_CRL_MONITOR | WOLFSSL_CRL_START_MON;
  2733. #endif
  2734. if (wolfSSL_CTX_EnableCRL(ctx, 0) != WOLFSSL_SUCCESS)
  2735. err_sys_ex(runWithErrors, "unable to enable CRL");
  2736. if (wolfSSL_CTX_LoadCRL(ctx, crlDir != NULL ? crlDir : crlPemDir,
  2737. WOLFSSL_FILETYPE_PEM, crlFlags) != WOLFSSL_SUCCESS)
  2738. err_sys_ex(runWithErrors, "unable to load CRL");
  2739. if (wolfSSL_CTX_SetCRL_Cb(ctx, CRL_CallBack) != WOLFSSL_SUCCESS)
  2740. err_sys_ex(runWithErrors, "unable to set CRL callback url");
  2741. }
  2742. #endif
  2743. while (1) {
  2744. /* allow resume option */
  2745. if (resumeCount > 1) {
  2746. if (dtlsUDP == 0) {
  2747. client_len = sizeof client_addr;
  2748. clientfd = accept(sockfd, (struct sockaddr*)&client_addr,
  2749. (ACCEPT_THIRD_T)&client_len);
  2750. }
  2751. else {
  2752. tcp_listen(&sockfd, &port, useAnyAddr, dtlsUDP, dtlsSCTP);
  2753. clientfd = sockfd;
  2754. }
  2755. if (WOLFSSL_SOCKET_IS_INVALID(clientfd)) {
  2756. err_sys_ex(runWithErrors, "tcp accept failed");
  2757. }
  2758. }
  2759. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
  2760. !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  2761. LOG_ERROR("Before creating SSL\n");
  2762. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2763. err_sys_ex(runWithErrors, "ctx not using static memory");
  2764. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2765. err_sys_ex(runWithErrors, "error printing out memory stats");
  2766. #endif
  2767. if (doMcast) {
  2768. #ifdef WOLFSSL_MULTICAST
  2769. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  2770. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  2771. != WOLFSSL_SUCCESS)
  2772. err_sys("Couldn't set multicast cipher list.");
  2773. #endif
  2774. }
  2775. if (doDTLS && dtlsUDP) {
  2776. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  2777. if (doBlockSeq) {
  2778. wolfSSL_CTX_SetIOSend(ctx, TestEmbedSendTo);
  2779. }
  2780. #endif
  2781. }
  2782. #ifdef HAVE_PK_CALLBACKS
  2783. if (pkCallbacks)
  2784. SetupPkCallbacks(ctx);
  2785. #endif
  2786. ssl = SSL_new(ctx);
  2787. if (ssl == NULL)
  2788. err_sys_ex(catastrophic, "unable to create an SSL object");
  2789. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  2790. wolfSSL_KeepArrays(ssl);
  2791. #endif
  2792. /* Support for loading private key and cert using WOLFSSL object */
  2793. #if !defined(NO_CERTS)
  2794. if ((!usePsk || usePskPlus) && !useAnon && loadCertKeyIntoSSLObj) {
  2795. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2796. if (wolfSSL_use_certificate_chain_buffer_format(ssl,
  2797. server_cert_der_2048, sizeof_server_cert_der_2048,
  2798. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2799. err_sys_ex(catastrophic, "can't load server cert buffer");
  2800. #elif !defined(TEST_LOAD_BUFFER)
  2801. if (SSL_use_certificate_chain_file(ssl, ourCert)
  2802. != WOLFSSL_SUCCESS)
  2803. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2804. "and run from wolfSSL home dir");
  2805. #else
  2806. /* loads cert chain file using buffer API */
  2807. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  2808. #endif
  2809. }
  2810. if ((!usePsk || usePskPlus) && !useAnon &&
  2811. loadCertKeyIntoSSLObj
  2812. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2813. && !pkCallbacks
  2814. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2815. ) {
  2816. #if defined(NO_FILESYSTEM)
  2817. if (wolfSSL_use_PrivateKey_buffer(ssl, server_key_der_2048,
  2818. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2819. err_sys_ex(catastrophic, "can't load server private key buffer");
  2820. #elif !defined(TEST_LOAD_BUFFER)
  2821. if (SSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  2822. != WOLFSSL_SUCCESS)
  2823. err_sys_ex(catastrophic, "can't load server private key file, check"
  2824. "file and run from wolfSSL home dir");
  2825. #else
  2826. /* loads private key file using buffer API */
  2827. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  2828. #endif
  2829. }
  2830. #endif /* !NO_CERTS */
  2831. #ifdef WOLFSSL_SEND_HRR_COOKIE
  2832. if (hrrCookie == 1 && wolfSSL_send_hrr_cookie(ssl, NULL, 0)
  2833. != WOLFSSL_SUCCESS) {
  2834. err_sys("unable to set use of cookie with HRR msg");
  2835. }
  2836. else if (hrrCookie == -1) {
  2837. wolfSSL_disable_hrr_cookie(ssl);
  2838. }
  2839. #endif
  2840. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
  2841. !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  2842. LOG_ERROR("After creating SSL\n");
  2843. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2844. err_sys_ex(runWithErrors, "ctx not using static memory");
  2845. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2846. err_sys_ex(runWithErrors, "error printing out memory stats");
  2847. #endif
  2848. if (doMcast) {
  2849. #ifdef WOLFSSL_MULTICAST
  2850. /* DTLS multicast secret for testing only */
  2851. #define CLI_SRV_RANDOM_SZ 32 /* RAN_LEN (see internal.h) */
  2852. #define PMS_SZ 512 /* ENCRYPT_LEN (see internal.h) */
  2853. byte pms[PMS_SZ]; /* pre master secret */
  2854. byte cr[CLI_SRV_RANDOM_SZ]; /* client random */
  2855. byte sr[CLI_SRV_RANDOM_SZ]; /* server random */
  2856. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  2857. XMEMSET(pms, 0x23, sizeof(pms));
  2858. XMEMSET(cr, 0xA5, sizeof(cr));
  2859. XMEMSET(sr, 0x5A, sizeof(sr));
  2860. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  2861. != WOLFSSL_SUCCESS) {
  2862. err_sys("unable to set mcast secret");
  2863. }
  2864. #endif
  2865. }
  2866. #ifdef HAVE_SECURE_RENEGOTIATION
  2867. if (scr) {
  2868. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  2869. err_sys_ex(runWithErrors, "can't enable secure renegotiation");
  2870. }
  2871. }
  2872. #endif /* HAVE_SECURE_RENEGOTIATION */
  2873. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2874. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2875. if (postHandAuth) {
  2876. unsigned int verify_flags = 0;
  2877. #ifdef TEST_BEFORE_DATE
  2878. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2879. #endif
  2880. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2881. verify_flags)
  2882. != WOLFSSL_SUCCESS) {
  2883. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2884. "wolfSSL home dir");
  2885. }
  2886. #ifdef WOLFSSL_TRUST_PEER_CERT
  2887. if (trustCert) {
  2888. if (wolfSSL_trust_peer_cert(ssl, trustCert,
  2889. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2890. err_sys_ex(runWithErrors, "can't load trusted peer cert "
  2891. "file");
  2892. }
  2893. }
  2894. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2895. }
  2896. #endif
  2897. #endif
  2898. #ifndef NO_HANDSHAKE_DONE_CB
  2899. wolfSSL_SetHsDoneCb(ssl, myHsDoneCb, NULL);
  2900. #endif
  2901. #ifdef HAVE_OCSP
  2902. if (useOcsp) {
  2903. if (ocspUrl != NULL) {
  2904. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2905. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2906. | WOLFSSL_OCSP_URL_OVERRIDE);
  2907. }
  2908. else
  2909. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE);
  2910. }
  2911. #ifndef NO_RSA
  2912. /* All the OCSP Stapling test certs are RSA. */
  2913. #if !defined(NO_FILESYSTEM) && (\
  2914. defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2915. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
  2916. { /* scope start */
  2917. const char* ca1 = "certs/ocsp/intermediate1-ca-cert.pem";
  2918. const char* ca2 = "certs/ocsp/intermediate2-ca-cert.pem";
  2919. const char* ca3 = "certs/ocsp/intermediate3-ca-cert.pem";
  2920. int fails = 0;
  2921. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS) {
  2922. err_sys_ex(catastrophic, "can't enable OCSP Stapling "
  2923. "Certificate Manager");
  2924. }
  2925. if (SSL_CTX_load_verify_locations(ctx, ca1, 0) != WOLFSSL_SUCCESS) {
  2926. fails++;
  2927. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2928. "wolfSSL home dir");
  2929. }
  2930. if (SSL_CTX_load_verify_locations(ctx, ca2, 0) != WOLFSSL_SUCCESS) {
  2931. fails++;
  2932. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2933. "wolfSSL home dir");
  2934. }
  2935. if (SSL_CTX_load_verify_locations(ctx, ca3, 0) != WOLFSSL_SUCCESS) {
  2936. fails++;
  2937. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2938. "wolfSSL home dir");
  2939. }
  2940. if (fails > 2) {
  2941. err_sys_ex(catastrophic, "Failed to load any intermediates for "
  2942. "OCSP stapling test");
  2943. }
  2944. } /* scope end */
  2945. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  2946. #endif /* NO_RSA */
  2947. #endif /* HAVE_OCSP */
  2948. #ifdef HAVE_PK_CALLBACKS
  2949. /* This must be before SetKeyShare */
  2950. if (pkCallbacks) {
  2951. SetupPkCallbackContexts(ssl, &pkCbInfo);
  2952. }
  2953. #endif
  2954. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2955. if (version >= 4 || version == -4) {
  2956. #ifdef CAN_FORCE_CURVE
  2957. if (force_curve_group_id > 0) {
  2958. do {
  2959. ret = wolfSSL_UseKeyShare(ssl, (word16)force_curve_group_id);
  2960. if (ret == WOLFSSL_SUCCESS) {
  2961. }
  2962. #ifdef WOLFSSL_ASYNC_CRYPT
  2963. else if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  2964. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2965. }
  2966. #endif
  2967. else {
  2968. err_sys("Failed wolfSSL_UseKeyShare in force-curve");
  2969. }
  2970. } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
  2971. ret = wolfSSL_set_groups(ssl, &force_curve_group_id, 1);
  2972. if (WOLFSSL_SUCCESS != ret) {
  2973. err_sys("Failed wolfSSL_set_groups in force-curve");
  2974. }
  2975. }
  2976. else
  2977. #endif
  2978. SetKeyShare(ssl, onlyKeyShare, useX25519, useX448, usePqc,
  2979. pqcAlg);
  2980. }
  2981. #endif
  2982. #ifdef HAVE_ENCRYPT_THEN_MAC
  2983. if (disallowETM)
  2984. wolfSSL_AllowEncryptThenMac(ssl, 0);
  2985. #endif
  2986. /* do accept */
  2987. readySignal = ((func_args*)args)->signal;
  2988. if (readySignal) {
  2989. readySignal->srfName = serverReadyFile;
  2990. }
  2991. client_len = sizeof client_addr;
  2992. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr,
  2993. dtlsUDP, dtlsSCTP, serverReadyFile ? 1 : 0, doListen,
  2994. &client_addr, &client_len);
  2995. doListen = 0; /* Don't listen next time */
  2996. if (port == 0) {
  2997. port = readySignal->port;
  2998. }
  2999. if (SSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  3000. err_sys_ex(catastrophic, "error in setting fd");
  3001. }
  3002. #ifdef HAVE_TRUSTED_CA
  3003. if (trustedCaKeyId) {
  3004. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  3005. NULL, 0) != WOLFSSL_SUCCESS) {
  3006. err_sys_ex(runWithErrors, "UseTrustedCA failed");
  3007. }
  3008. }
  3009. #endif /* HAVE_TRUSTED_CA */
  3010. #ifdef HAVE_ALPN
  3011. if (alpnList != NULL) {
  3012. printf("ALPN accepted protocols list : %s\n", alpnList);
  3013. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  3014. }
  3015. #endif
  3016. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  3017. if (doDTLS && dtlsUDP) {
  3018. byte b[1500];
  3019. int isClientHello = 0;
  3020. while (!isClientHello) {
  3021. int n;
  3022. client_len = sizeof client_addr;
  3023. /* For DTLS, peek at the next datagram so we can get the
  3024. * client's address and set it into the ssl object later to
  3025. * generate the cookie. */
  3026. n = (int)recvfrom(clientfd, (char*)b, sizeof(b), MSG_PEEK,
  3027. (struct sockaddr*)&client_addr, &client_len);
  3028. if (n <= 0)
  3029. err_sys_ex(runWithErrors, "recvfrom failed");
  3030. /* when doing resumption, it may happen that we receive the
  3031. alert used to shutdown the first connection as the first
  3032. packet of the second accept:
  3033. Client | Server
  3034. | WolfSSL_Shutdown()
  3035. | <- Alert
  3036. | recvfrom(peek)
  3037. WolfSSL_Shutdown() |
  3038. Alert-> |
  3039. | wolfSSL_set_dtls_peer()
  3040. but this will set the wrong src port, making the test fail.
  3041. Discard not-handshake message to avoid this.
  3042. */
  3043. if (b[0] != 0x16) {
  3044. /* discard the packet */
  3045. n = (int)recvfrom(clientfd, (char *)b, sizeof(b), 0,
  3046. (struct sockaddr *)&client_addr, &client_len);
  3047. if (n <= 0)
  3048. err_sys_ex(runWithErrors, "recvfrom failed");
  3049. }
  3050. else {
  3051. isClientHello = 1;
  3052. }
  3053. }
  3054. if (doBlockSeq) {
  3055. XMEMCPY(&dtlsCtx.peer.sa, &client_addr, client_len);
  3056. dtlsCtx.peer.sz = client_len;
  3057. dtlsCtx.wfd = clientfd;
  3058. dtlsCtx.failOnce = 1;
  3059. wolfSSL_SetIOWriteCtx(ssl, &dtlsCtx);
  3060. }
  3061. else {
  3062. wolfSSL_dtls_set_peer(ssl, &client_addr, client_len);
  3063. }
  3064. if (simulateWantWrite) {
  3065. #ifdef USE_WOLFSSL_IO
  3066. /* connect on a udp to associate peer with this fd to make it
  3067. * simpler for SimulateWantWriteIOSendCb */
  3068. if (connect(clientfd, (struct sockaddr*)&client_addr,
  3069. client_len) != 0) {
  3070. err_sys_ex(catastrophic, "error in connecting to peer");
  3071. }
  3072. wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd);
  3073. #endif
  3074. }
  3075. }
  3076. #endif
  3077. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  3078. {
  3079. SOCKADDR_IN_T local_addr;
  3080. socklen_t local_len = sizeof(local_addr);
  3081. getsockname(clientfd, (struct sockaddr *)&local_addr,
  3082. (socklen_t *)&local_len);
  3083. if (((struct sockaddr *)&client_addr)->sa_family !=
  3084. ((struct sockaddr *)&local_addr)->sa_family)
  3085. err_sys_ex(catastrophic,
  3086. "client_addr.sa_family != local_addr.sa_family");
  3087. if (wolfsentry_store_endpoints(
  3088. ssl, &client_addr, &local_addr,
  3089. dtlsUDP ? IPPROTO_UDP : IPPROTO_TCP,
  3090. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN, NULL) != WOLFSSL_SUCCESS)
  3091. err_sys_ex(catastrophic,
  3092. "error in wolfsentry_store_endpoints()");
  3093. }
  3094. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  3095. if ((usePsk == 0 || usePskPlus) || useAnon == 1 || cipherList != NULL
  3096. || needDH == 1) {
  3097. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && !defined(NO_ASN)
  3098. wolfSSL_SetTmpDH_file(ssl, ourDhParam, WOLFSSL_FILETYPE_PEM);
  3099. #elif !defined(NO_DH)
  3100. SetDH(ssl); /* repick suites with DHE, higher priority than
  3101. * PSK */
  3102. #endif
  3103. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3104. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3105. if (!doDhKeyCheck)
  3106. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  3107. #endif
  3108. }
  3109. #ifdef WOLFSSL_DTLS_CID
  3110. if (useDtlsCID) {
  3111. ret = wolfSSL_dtls_cid_use(ssl);
  3112. if (ret != WOLFSSL_SUCCESS)
  3113. err_sys("Can't enable DTLS ConnectionID");
  3114. ret = wolfSSL_dtls_cid_set(ssl, (byte*)dtlsCID,
  3115. (word32)XSTRLEN(dtlsCID));
  3116. if (ret != WOLFSSL_SUCCESS)
  3117. err_sys("Can't set DTLS ConnectionID");
  3118. }
  3119. #endif /* WOLFSSL_DTLS_CID */
  3120. #ifdef WOLFSSL_DTLS_CH_FRAG
  3121. if (doDTLS)
  3122. wolfSSL_dtls13_allow_ch_frag(ssl, 1);
  3123. #endif
  3124. #ifndef WOLFSSL_CALLBACKS
  3125. if (nonBlocking) {
  3126. #ifdef WOLFSSL_DTLS
  3127. if (doDTLS) {
  3128. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3129. }
  3130. #endif
  3131. tcp_set_nonblocking(&clientfd);
  3132. ret = NonBlockingSSL_Accept(ssl);
  3133. }
  3134. else {
  3135. #ifdef WOLFSSL_EARLY_DATA
  3136. if (earlyData) {
  3137. do {
  3138. int len;
  3139. err = 0; /* reset error */
  3140. ret = wolfSSL_read_early_data(ssl, input, sizeof(input)-1,
  3141. &len);
  3142. if (ret <= 0) {
  3143. err = SSL_get_error(ssl, 0);
  3144. #ifdef WOLFSSL_ASYNC_CRYPT
  3145. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  3146. /* returns the number of polled items or <0 for
  3147. * error */
  3148. ret = wolfSSL_AsyncPoll(ssl,
  3149. WOLF_POLL_FLAG_CHECK_HW);
  3150. if (ret < 0) break;
  3151. }
  3152. #endif
  3153. }
  3154. else if (ret > 0) {
  3155. input[ret] = 0; /* null terminate message */
  3156. printf("Early Data Client message: %s\n", input);
  3157. }
  3158. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E) || ret > 0);
  3159. }
  3160. #endif
  3161. do {
  3162. err = 0; /* reset error */
  3163. ret = SSL_accept(ssl);
  3164. if (ret != WOLFSSL_SUCCESS) {
  3165. err = SSL_get_error(ssl, 0);
  3166. #ifdef WOLFSSL_ASYNC_CRYPT
  3167. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  3168. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3169. if (ret < 0) break;
  3170. }
  3171. #endif
  3172. }
  3173. } while (err == WC_NO_ERR_TRACE(WC_PENDING_E));
  3174. }
  3175. #else
  3176. if (nonBlocking) {
  3177. #ifdef WOLFSSL_DTLS
  3178. if (doDTLS) {
  3179. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3180. }
  3181. #endif
  3182. tcp_set_nonblocking(&clientfd);
  3183. }
  3184. ret = NonBlockingSSL_Accept(ssl);
  3185. #endif
  3186. #ifdef WOLFSSL_EARLY_DATA
  3187. EarlyDataStatus(ssl);
  3188. #endif
  3189. if (ret != WOLFSSL_SUCCESS) {
  3190. err = SSL_get_error(ssl, 0);
  3191. LOG_ERROR("SSL_accept error %d, %s\n", err,
  3192. ERR_error_string((unsigned long)err, buffer));
  3193. if (!exitWithRet) {
  3194. err_sys_ex(runWithErrors, "SSL_accept failed");
  3195. } else {
  3196. /* cleanup */
  3197. SSL_free(ssl); ssl = NULL;
  3198. SSL_CTX_free(ctx); ctx = NULL;
  3199. CloseSocket(clientfd);
  3200. CloseSocket(sockfd);
  3201. ((func_args*)args)->return_code = err;
  3202. goto exit;
  3203. }
  3204. }
  3205. showPeerEx(ssl, lng_index);
  3206. if (SSL_state(ssl) != 0) {
  3207. err_sys_ex(runWithErrors, "SSL in error state");
  3208. }
  3209. /* if the caller requested a particular cipher, check here that either
  3210. * a canonical name of the established cipher matches the requested
  3211. * cipher name, or the requested cipher name is marked as an alias
  3212. * that matches the established cipher.
  3213. */
  3214. if (cipherList && !useDefCipherList && (! XSTRSTR(cipherList, ":"))) {
  3215. WOLFSSL_CIPHER* established_cipher = wolfSSL_get_current_cipher(ssl);
  3216. byte requested_cipherSuite0, requested_cipherSuite;
  3217. int requested_cipherFlags;
  3218. if (established_cipher &&
  3219. /* don't test for pseudo-ciphers like "ALL" and "DEFAULT". */
  3220. (wolfSSL_get_cipher_suite_from_name(cipherList,
  3221. &requested_cipherSuite0,
  3222. &requested_cipherSuite,
  3223. &requested_cipherFlags) == 0)) {
  3224. word32 established_cipher_id = wolfSSL_CIPHER_get_id(established_cipher);
  3225. byte established_cipherSuite0 = (established_cipher_id >> 8) & 0xff;
  3226. byte established_cipherSuite = established_cipher_id & 0xff;
  3227. const char *established_cipher_name =
  3228. wolfSSL_get_cipher_name_from_suite(established_cipherSuite0,
  3229. established_cipherSuite);
  3230. const char *established_cipher_name_iana =
  3231. wolfSSL_get_cipher_name_iana_from_suite(established_cipherSuite0,
  3232. established_cipherSuite);
  3233. if (established_cipher_name == NULL)
  3234. err_sys_ex(catastrophic, "error looking up name of established cipher");
  3235. if (strcmp(cipherList, established_cipher_name) &&
  3236. ((established_cipher_name_iana == NULL) ||
  3237. strcmp(cipherList, established_cipher_name_iana))) {
  3238. if (! (requested_cipherFlags & WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS))
  3239. err_sys_ex(
  3240. catastrophic,
  3241. "Unexpected mismatch between names of requested and established ciphers.");
  3242. else if ((requested_cipherSuite0 != established_cipherSuite0) ||
  3243. (requested_cipherSuite != established_cipherSuite))
  3244. err_sys_ex(
  3245. catastrophic,
  3246. "Mismatch between IDs of requested and established ciphers.");
  3247. }
  3248. }
  3249. }
  3250. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  3251. {
  3252. byte* rnd = NULL;
  3253. size_t size;
  3254. /* get size of buffer then print */
  3255. size = wolfSSL_get_server_random(NULL, NULL, 0);
  3256. if (size == 0) {
  3257. err_sys_ex(runWithErrors, "error getting server random buffer "
  3258. "size");
  3259. }
  3260. else {
  3261. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3262. }
  3263. if (rnd == NULL) {
  3264. err_sys_ex(runWithErrors, "error creating server random buffer");
  3265. }
  3266. size = wolfSSL_get_server_random(ssl, rnd, size);
  3267. if (size == 0) {
  3268. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3269. rnd = NULL;
  3270. err_sys_ex(runWithErrors, "error getting server random buffer");
  3271. }
  3272. if (rnd) {
  3273. byte* pt;
  3274. printf("Server Random : ");
  3275. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  3276. printf("\n");
  3277. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3278. rnd = NULL;
  3279. }
  3280. }
  3281. #endif
  3282. #ifdef WOLFSSL_SRTP
  3283. if (dtlsSrtpProfiles != NULL) {
  3284. err = server_srtp_test(ssl, (func_args*)args);
  3285. if (err != 0) {
  3286. if (exitWithRet) {
  3287. ((func_args*)args)->return_code = err;
  3288. wolfSSL_free(ssl); ssl = NULL;
  3289. wolfSSL_CTX_free(ctx); ctx = NULL;
  3290. goto exit;
  3291. }
  3292. /* else */
  3293. err_sys("SRTP check failed");
  3294. }
  3295. }
  3296. #endif /* WOLFSSL_SRTP */
  3297. #ifdef WOLFSSL_DTLS_CID
  3298. if (useDtlsCID && wolfSSL_dtls_cid_is_enabled(ssl)) {
  3299. byte receivedCID[DTLS_CID_BUFFER_SIZE];
  3300. unsigned int receivedCIDSz;
  3301. printf("CID extension was negotiated\n");
  3302. ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
  3303. if (ret == WOLFSSL_SUCCESS && receivedCIDSz > 0) {
  3304. ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
  3305. DTLS_CID_BUFFER_SIZE - 1);
  3306. if (ret != WOLFSSL_SUCCESS)
  3307. err_sys("Can't get negotiated DTLS CID\n");
  3308. printf("Sending CID is ");
  3309. printBuffer(receivedCID, receivedCIDSz);
  3310. printf("\n");
  3311. }
  3312. else {
  3313. printf("other peer provided empty CID\n");
  3314. }
  3315. }
  3316. #endif
  3317. #ifdef HAVE_ALPN
  3318. if (alpnList != NULL) {
  3319. char *protocol_name = NULL, *list = NULL;
  3320. word16 protocol_nameSz = 0, listSz = 0;
  3321. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name,
  3322. &protocol_nameSz);
  3323. if (err == WOLFSSL_SUCCESS)
  3324. printf("Sent ALPN protocol : %s (%d)\n",
  3325. protocol_name, protocol_nameSz);
  3326. else if (err == WC_NO_ERR_TRACE(WOLFSSL_ALPN_NOT_FOUND))
  3327. printf("No ALPN response sent (no match)\n");
  3328. else
  3329. printf("Getting ALPN protocol name failed\n");
  3330. err = wolfSSL_ALPN_GetPeerProtocol(ssl, &list, &listSz);
  3331. if (err == WOLFSSL_SUCCESS)
  3332. printf("List of protocol names sent by Client: %s (%d)\n",
  3333. list, listSz);
  3334. else
  3335. printf("Get list of client's protocol name failed\n");
  3336. free(list);
  3337. }
  3338. #endif
  3339. if (echoData == 0 && throughput == 0) {
  3340. ServerRead(ssl, input, sizeof(input)-1);
  3341. err = SSL_get_error(ssl, 0);
  3342. }
  3343. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  3344. defined(HAVE_SERVER_RENEGOTIATION_INFO)
  3345. if (scr && forceScr) {
  3346. if (nonBlocking) {
  3347. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3348. err = wolfSSL_get_error(ssl, 0);
  3349. if (err == WOLFSSL_ERROR_WANT_READ ||
  3350. err == WOLFSSL_ERROR_WANT_WRITE) {
  3351. do {
  3352. #ifdef WOLFSSL_ASYNC_CRYPT
  3353. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  3354. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3355. if (ret < 0) break;
  3356. }
  3357. #endif
  3358. if (err == WC_NO_ERR_TRACE(APP_DATA_READY)) {
  3359. if (wolfSSL_read(ssl, input, sizeof(input)-1) < 0) {
  3360. err_sys("APP DATA should be present but error returned");
  3361. }
  3362. printf("Received message: %s\n", input);
  3363. }
  3364. err = 0;
  3365. if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
  3366. err = wolfSSL_get_error(ssl, ret);
  3367. }
  3368. } while (ret != WOLFSSL_SUCCESS &&
  3369. (err == WOLFSSL_ERROR_WANT_READ ||
  3370. err == WOLFSSL_ERROR_WANT_WRITE ||
  3371. err == WC_NO_ERR_TRACE(APP_DATA_READY) ||
  3372. err == WC_NO_ERR_TRACE(WC_PENDING_E)));
  3373. if (ret == WOLFSSL_SUCCESS) {
  3374. printf("NON-BLOCKING RENEGOTIATION SUCCESSFUL\n");
  3375. err = 0;
  3376. }
  3377. }
  3378. if (ret != WOLFSSL_SUCCESS) {
  3379. err = wolfSSL_get_error(ssl, 0);
  3380. LOG_ERROR(
  3381. "wolfSSL_Rehandshake error %d, %s\n", err,
  3382. wolfSSL_ERR_error_string(err, buffer));
  3383. wolfSSL_free(ssl); ssl = NULL;
  3384. wolfSSL_CTX_free(ctx); ctx = NULL;
  3385. err_sys("non-blocking wolfSSL_Rehandshake failed");
  3386. }
  3387. }
  3388. } else {
  3389. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3390. #ifdef WOLFSSL_ASYNC_CRYPT
  3391. err = wolfSSL_get_error(ssl, 0);
  3392. while (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  3393. err = 0;
  3394. ret = wolfSSL_negotiate(ssl);
  3395. if (ret != WOLFSSL_SUCCESS) {
  3396. err = wolfSSL_get_error(ssl, 0);
  3397. if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
  3398. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3399. if (ret < 0) break;
  3400. }
  3401. }
  3402. }
  3403. if (ret != WOLFSSL_SUCCESS)
  3404. #endif
  3405. printf("not doing secure renegotiation\n");
  3406. }
  3407. else {
  3408. printf("RENEGOTIATION SUCCESSFUL\n");
  3409. }
  3410. }
  3411. }
  3412. #endif /* HAVE_SECURE_RENEGOTIATION */
  3413. if (err == 0 && echoData == 0 && throughput == 0) {
  3414. const char* write_msg;
  3415. int write_msg_sz;
  3416. #ifdef WOLFSSL_TLS13
  3417. if (updateKeysIVs)
  3418. wolfSSL_update_keys(ssl);
  3419. #endif
  3420. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  3421. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3422. if (postHandAuth) {
  3423. SSL_set_verify(ssl, WOLFSSL_VERIFY_PEER |
  3424. ((usePskPlus) ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  3425. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT), 0);
  3426. if (wolfSSL_request_certificate(ssl) != WOLFSSL_SUCCESS) {
  3427. LOG_ERROR("Request for post-hs certificate failed\n");
  3428. }
  3429. else {
  3430. LOG_ERROR("Successfully requested post-hs certificate\n");
  3431. }
  3432. }
  3433. #endif
  3434. #endif
  3435. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  3436. if (sendTicket) {
  3437. if (wolfSSL_send_SessionTicket(ssl) != WOLFSSL_SUCCESS) {
  3438. LOG_ERROR("Sending new session ticket failed\n");
  3439. }
  3440. else {
  3441. LOG_ERROR("New session ticket sent\n");
  3442. }
  3443. }
  3444. #endif
  3445. /* Write data */
  3446. if (!useWebServerMsg) {
  3447. write_msg = kReplyMsg;
  3448. write_msg_sz = (int)XSTRLEN(kReplyMsg);
  3449. }
  3450. else {
  3451. write_msg = kHttpServerMsg;
  3452. write_msg_sz = (int)XSTRLEN(kHttpServerMsg);
  3453. }
  3454. ServerWrite(ssl, write_msg, write_msg_sz);
  3455. #ifdef WOLFSSL_TLS13
  3456. if (updateKeysIVs || postHandAuth)
  3457. ServerRead(ssl, input, sizeof(input)-1);
  3458. #endif
  3459. }
  3460. else if (err == 0 ||
  3461. err == WOLFSSL_ERROR_ZERO_RETURN)
  3462. {
  3463. err = ServerEchoData(ssl, clientfd, echoData, block, throughput);
  3464. /* Got close notify. Ignore it if not expecting a failure. */
  3465. if (err == WOLFSSL_ERROR_ZERO_RETURN &&
  3466. exitWithRet == 0)
  3467. {
  3468. err = 0;
  3469. }
  3470. if (err != 0) {
  3471. SSL_free(ssl); ssl = NULL;
  3472. SSL_CTX_free(ctx); ctx = NULL;
  3473. CloseSocket(clientfd);
  3474. CloseSocket(sockfd);
  3475. ((func_args*)args)->return_code = err;
  3476. goto exit;
  3477. }
  3478. }
  3479. #if defined(WOLFSSL_MDK_SHELL) && defined(HAVE_MDK_RTX)
  3480. os_dly_wait(500) ;
  3481. #elif defined (WOLFSSL_TIRTOS)
  3482. Task_yield();
  3483. #endif
  3484. #if defined(WOLFSSL_DTLS13)
  3485. if (wolfSSL_dtls(ssl) && version == -4) {
  3486. int zero_return = 0;
  3487. while (wolfSSL_dtls13_has_pending_msg(ssl)) {
  3488. err =
  3489. process_handshake_messages(ssl, !nonBlocking, &zero_return);
  3490. if (err < 0) {
  3491. /* other peer closes the connection, non fatal */
  3492. if (zero_return)
  3493. break;
  3494. err_sys("Error while processing pending DTLSv1.3 messages");
  3495. }
  3496. }
  3497. }
  3498. #endif /* WOLFSSL_DTLS13 */
  3499. ret = SSL_shutdown(ssl);
  3500. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  3501. while (tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC) ==
  3502. TEST_RECV_READY) {
  3503. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  3504. if (ret == WOLFSSL_SUCCESS) {
  3505. printf("Bidirectional shutdown complete\n");
  3506. break;
  3507. }
  3508. else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
  3509. LOG_ERROR("Bidirectional shutdown failed\n");
  3510. break;
  3511. }
  3512. }
  3513. if (ret != WOLFSSL_SUCCESS)
  3514. LOG_ERROR("Bidirectional shutdown failed\n");
  3515. }
  3516. /* display collected statistics */
  3517. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  3518. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  3519. err_sys_ex(runWithErrors, "static memory was not used with ssl");
  3520. LOG_ERROR("\nprint off SSL memory stats\n");
  3521. LOG_ERROR("*** This is memory state before wolfSSL_free is "
  3522. "called\n");
  3523. wolfSSL_PrintStatsConn(&ssl_stats);
  3524. #endif
  3525. SSL_free(ssl); ssl = NULL;
  3526. CloseSocket(clientfd);
  3527. if (resume == 1 && resumeCount == 0) {
  3528. resumeCount++; /* only do one resume for testing */
  3529. continue;
  3530. }
  3531. resumeCount = 0;
  3532. cnt++;
  3533. if (loops > 0) {
  3534. if (--loops == 0) {
  3535. break; /* out of while loop, done with normal and resume
  3536. * option
  3537. */
  3538. }
  3539. }
  3540. } /* while(1) */
  3541. WOLFSSL_TIME(cnt);
  3542. (void)cnt;
  3543. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  3544. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3545. wolfSSL_CTX_DisableOCSPStapling(ctx);
  3546. #endif
  3547. CloseSocket(sockfd);
  3548. SSL_CTX_free(ctx); ctx = NULL;
  3549. ((func_args*)args)->return_code = 0;
  3550. exit:
  3551. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  3552. wolfsentry_ret =
  3553. wolfsentry_shutdown(WOLFSENTRY_CONTEXT_ARGS_OUT_EX4(&wolfsentry, NULL));
  3554. if (wolfsentry_ret < 0) {
  3555. LOG_ERROR(
  3556. "wolfsentry_shutdown() returned " WOLFSENTRY_ERROR_FMT "\n",
  3557. WOLFSENTRY_ERROR_FMT_ARGS(wolfsentry_ret));
  3558. }
  3559. #endif
  3560. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  3561. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  3562. wc_ecc_fp_free(); /* free per thread cache */
  3563. #endif
  3564. #ifdef WOLFSSL_TIRTOS
  3565. fdCloseSession(Task_self());
  3566. #endif
  3567. #ifdef HAVE_TEST_SESSION_TICKET
  3568. TicketCleanup();
  3569. #endif
  3570. #ifdef WOLFSSL_ASYNC_CRYPT
  3571. wolfAsync_DevClose(&devId);
  3572. #endif
  3573. /* There are use cases when these assignments are not read. To avoid
  3574. * potential confusion those warnings have been handled here.
  3575. */
  3576. (void) ourKey;
  3577. (void) verifyCert;
  3578. (void) doCliCertCheck;
  3579. (void) ourDhParam;
  3580. (void) ourCert;
  3581. (void) useX25519;
  3582. (void) useX448;
  3583. #ifdef HAVE_SECURE_RENEGOTIATION
  3584. (void) forceScr;
  3585. #endif
  3586. #if defined(WOLFSSL_CALLBACKS) && defined(WOLFSSL_EARLY_DATA)
  3587. (void) earlyData;
  3588. #endif
  3589. WOLFSSL_RETURN_FROM_THREAD(0);
  3590. }
  3591. #endif /* !NO_WOLFSSL_SERVER */
  3592. /* so overall tests can pull in test function */
  3593. #ifndef NO_MAIN_DRIVER
  3594. int main(int argc, char** argv)
  3595. {
  3596. func_args args;
  3597. tcp_ready ready;
  3598. StartTCP();
  3599. args.argc = argc;
  3600. args.argv = argv;
  3601. args.signal = &ready;
  3602. args.return_code = 0;
  3603. #if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
  3604. args.srtp_helper = NULL;
  3605. #endif
  3606. InitTcpReady(&ready);
  3607. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL)
  3608. wolfSSL_Debugging_ON();
  3609. #endif
  3610. wolfSSL_Init();
  3611. #ifdef WC_RNG_SEED_CB
  3612. wc_SetSeed_Cb(wc_GenerateSeed);
  3613. #endif
  3614. ChangeToWolfRoot();
  3615. #ifndef NO_WOLFSSL_SERVER
  3616. #ifdef HAVE_STACK_SIZE
  3617. StackSizeCheck(&args, server_test);
  3618. #else
  3619. server_test(&args);
  3620. #endif
  3621. #else
  3622. fprintf(stderr, "Server not compiled in!\n");
  3623. #endif
  3624. wolfSSL_Cleanup();
  3625. FreeTcpReady(&ready);
  3626. #ifdef HAVE_WNR
  3627. if (wc_FreeNetRandom() < 0)
  3628. err_sys_ex(runWithErrors, "Failed to free netRandom context");
  3629. #endif /* HAVE_WNR */
  3630. return args.return_code;
  3631. }
  3632. int myoptind = 0;
  3633. char* myoptarg = NULL;
  3634. #endif /* NO_MAIN_DRIVER */