1
0

server.c 133 KB

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