server.c 135 KB

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