1
0

server.c 139 KB

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