server.c 130 KB

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