1
0

server.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For simpler wolfSSL TLS server examples, visit
  22. * https://github.com/wolfSSL/wolfssl-examples/tree/master/tls
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifndef WOLFSSL_USER_SETTINGS
  28. #include <wolfssl/options.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  32. #include <wolfssl/ssl.h> /* name change portability layer */
  33. #ifdef HAVE_ECC
  34. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  35. #endif
  36. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  37. #include <wolfsentry/wolfsentry.h>
  38. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  39. static const char *wolfsentry_config_path = NULL;
  40. #endif
  41. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  42. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include "rl_fs.h"
  46. #include "rl_net.h"
  47. #endif
  48. #ifdef NO_FILESYSTEM
  49. #ifdef NO_RSA
  50. #error currently the example only tries to load in a RSA buffer
  51. #endif
  52. #undef USE_CERT_BUFFERS_2048
  53. #define USE_CERT_BUFFERS_2048
  54. #include <wolfssl/certs_test.h>
  55. #endif
  56. #include <wolfssl/test.h>
  57. #include <wolfssl/error-ssl.h>
  58. /* Force enable the compatibility macros for this example */
  59. #ifndef OPENSSL_EXTRA_X509_SMALL
  60. #define OPENSSL_EXTRA_X509_SMALL
  61. #endif
  62. #include <wolfssl/openssl/ssl.h>
  63. #include "examples/server/server.h"
  64. #ifndef NO_WOLFSSL_SERVER
  65. #if defined(WOLFSSL_TLS13) && ( \
  66. defined(HAVE_ECC) \
  67. || defined(HAVE_CURVE25519) \
  68. || defined(HAVE_CURVE448) \
  69. || defined(HAVE_FFDHE_2048))
  70. #define CAN_FORCE_CURVE
  71. #endif
  72. #if defined(CAN_FORCE_CURVE) && defined(HAVE_ECC)
  73. struct group_info {
  74. word16 group;
  75. const char *name;
  76. };
  77. static struct group_info group_id_to_text[] = {
  78. { WOLFSSL_ECC_SECP160K1, "SECP160K1" },
  79. { WOLFSSL_ECC_SECP160R1, "SECP160R1" },
  80. { WOLFSSL_ECC_SECP160R2, "SECP160R2" },
  81. { WOLFSSL_ECC_SECP192K1, "SECP192K1" },
  82. { WOLFSSL_ECC_SECP192R1, "SECP192R1" },
  83. { WOLFSSL_ECC_SECP224K1, "SECP224K1" },
  84. { WOLFSSL_ECC_SECP224R1, "SECP224R1" },
  85. { WOLFSSL_ECC_SECP256K1, "SECP256K1" },
  86. { WOLFSSL_ECC_SECP256R1, "SECP256R1" },
  87. { WOLFSSL_ECC_SECP384R1, "SECP384R1" },
  88. { WOLFSSL_ECC_SECP521R1, "SECP521R1" },
  89. { WOLFSSL_ECC_BRAINPOOLP256R1, "BRAINPOOLP256R1" },
  90. { WOLFSSL_ECC_BRAINPOOLP384R1, "BRAINPOOLP384R1" },
  91. { WOLFSSL_ECC_BRAINPOOLP512R1, "BRAINPOOLP512R1" },
  92. { 0, NULL }
  93. };
  94. #endif /* CAN_FORCE_CURVE && HAVE_ECC */
  95. #ifdef WOLFSSL_ASYNC_CRYPT
  96. static int devId = INVALID_DEVID;
  97. #endif
  98. #define DEFAULT_TIMEOUT_SEC 2
  99. /* Note on using port 0: if the server uses port 0 to bind an ephemeral port
  100. * number and is using the ready file for scripted testing, the code in
  101. * test.h will write the actual port number into the ready file for use
  102. * by the client. */
  103. #ifndef WOLFSSL_ALT_TEST_STRINGS
  104. static const char kReplyMsg[] = "I hear you fa shizzle!";
  105. #else
  106. static const char kReplyMsg[] = "I hear you fa shizzle!\n";
  107. #endif
  108. static const char kHttpServerMsg[] =
  109. "HTTP/1.1 200 OK\r\n"
  110. "Content-Type: text/html\r\n"
  111. "Connection: close\r\n"
  112. "Content-Length: 141\r\n"
  113. "\r\n"
  114. "<html>\r\n"
  115. "<head>\r\n"
  116. "<title>Welcome to wolfSSL!</title>\r\n"
  117. "</head>\r\n"
  118. "<body>\r\n"
  119. "<p>wolfSSL has successfully performed handshake!</p>\r\n"
  120. "</body>\r\n"
  121. "</html>\r\n";
  122. /* Read needs to be largest of the client.c message strings (29) */
  123. #define SRV_READ_SZ 32
  124. int runWithErrors = 0; /* Used with -x flag to run err_sys vs. print errors */
  125. int catastrophic = 0; /* Use with -x flag to still exit when an error is
  126. * considered catastrophic EG the servers own cert failing
  127. * to load would be catastrophic since there would be no
  128. * cert to send to clients attempting to connect. The
  129. * server should error out completely in that case
  130. */
  131. static int 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. #ifndef HAVE_SESSION_TICKET
  2125. if ((version >= 4) && resume) {
  2126. fprintf(stderr, "Can't do TLS 1.3 resumption; need session tickets!\n");
  2127. }
  2128. #endif
  2129. #ifdef HAVE_WNR
  2130. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  2131. err_sys_ex(runWithErrors, "can't load whitewood net random config "
  2132. "file");
  2133. #endif
  2134. #ifdef HAVE_PQC
  2135. if (usePqc) {
  2136. if (version == SERVER_DOWNGRADE_VERSION ||
  2137. version == EITHER_DOWNGRADE_VERSION) {
  2138. fprintf(stderr,
  2139. "WARNING: If a TLS 1.3 connection is not negotiated, you "
  2140. "will not be using a post-quantum group.\n");
  2141. } else if (version != 4 && version != -4) {
  2142. err_sys("can only use post-quantum groups with TLS 1.3 or DTLS 1.3");
  2143. }
  2144. }
  2145. #endif
  2146. switch (version) {
  2147. #ifndef NO_OLD_TLS
  2148. #ifdef WOLFSSL_ALLOW_SSLV3
  2149. case 0:
  2150. method = wolfSSLv3_server_method_ex;
  2151. break;
  2152. #endif
  2153. #ifndef NO_TLS
  2154. #ifdef WOLFSSL_ALLOW_TLSV10
  2155. case 1:
  2156. method = wolfTLSv1_server_method_ex;
  2157. break;
  2158. #endif
  2159. case 2:
  2160. method = wolfTLSv1_1_server_method_ex;
  2161. break;
  2162. #endif /* !NO_TLS */
  2163. #endif /* !NO_OLD_TLS */
  2164. #ifndef NO_TLS
  2165. #ifndef WOLFSSL_NO_TLS12
  2166. case 3:
  2167. method = wolfTLSv1_2_server_method_ex;
  2168. break;
  2169. #endif
  2170. #ifdef WOLFSSL_TLS13
  2171. case 4:
  2172. method = wolfTLSv1_3_server_method_ex;
  2173. break;
  2174. #endif
  2175. case SERVER_DOWNGRADE_VERSION:
  2176. if (!doDTLS) {
  2177. method = wolfSSLv23_server_method_ex;
  2178. }
  2179. else {
  2180. #ifdef WOLFSSL_DTLS
  2181. method = wolfDTLS_server_method_ex;
  2182. #else
  2183. err_sys_ex(runWithErrors, "version not supported");
  2184. #endif /* WOLFSSL_DTLS */
  2185. }
  2186. break;
  2187. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2188. case EITHER_DOWNGRADE_VERSION:
  2189. method = wolfSSLv23_method_ex;
  2190. break;
  2191. #endif
  2192. #endif /* NO_TLS */
  2193. #ifdef WOLFSSL_DTLS
  2194. #ifndef NO_OLD_TLS
  2195. case -1:
  2196. method = wolfDTLSv1_server_method_ex;
  2197. break;
  2198. #endif
  2199. #ifndef WOLFSSL_NO_TLS12
  2200. case -2:
  2201. method = wolfDTLSv1_2_server_method_ex;
  2202. break;
  2203. #endif
  2204. #ifdef WOLFSSL_DTLS13
  2205. case -4:
  2206. method = wolfDTLSv1_3_server_method_ex;
  2207. break;
  2208. #endif
  2209. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2210. case -3:
  2211. method = wolfDTLSv1_2_method_ex;
  2212. break;
  2213. #endif
  2214. #endif
  2215. default:
  2216. err_sys_ex(runWithErrors, "Bad SSL version");
  2217. }
  2218. if (method == NULL)
  2219. err_sys_ex(runWithErrors, "unable to get method");
  2220. #ifdef WOLFSSL_STATIC_MEMORY
  2221. #ifdef DEBUG_WOLFSSL
  2222. /* print off helper buffer sizes for use with static memory
  2223. * printing to stderr in case of debug mode turned on */
  2224. fprintf(stderr, "static memory management size = %d\n",
  2225. wolfSSL_MemoryPaddingSz());
  2226. fprintf(stderr, "calculated optimum general buffer size = %d\n",
  2227. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2228. fprintf(stderr, "calculated optimum IO buffer size = %d\n",
  2229. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2230. WOLFMEM_IO_POOL_FIXED));
  2231. #endif /* DEBUG_WOLFSSL */
  2232. if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),0,1)
  2233. != WOLFSSL_SUCCESS)
  2234. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2235. /* load in a buffer for IO */
  2236. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2237. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)
  2238. != WOLFSSL_SUCCESS)
  2239. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2240. #else
  2241. if (method != NULL) {
  2242. ctx = SSL_CTX_new(method(NULL));
  2243. }
  2244. #ifdef WOLFSSL_CALLBACKS
  2245. wolfSSL_CTX_set_msg_callback(ctx, msgDebugCb);
  2246. #endif
  2247. #endif /* WOLFSSL_STATIC_MEMORY */
  2248. if (ctx == NULL)
  2249. err_sys_ex(catastrophic, "unable to get ctx");
  2250. if (minVersion != SERVER_INVALID_VERSION) {
  2251. #ifdef WOLFSSL_DTLS
  2252. if (doDTLS) {
  2253. switch (minVersion) {
  2254. #ifdef WOLFSSL_DTLS13
  2255. case 4:
  2256. minVersion = WOLFSSL_DTLSV1_3;
  2257. break;
  2258. #endif /* WOLFSSL_DTLS13 */
  2259. case 3:
  2260. minVersion = WOLFSSL_DTLSV1_2;
  2261. break;
  2262. case 2:
  2263. minVersion = WOLFSSL_DTLSV1;
  2264. break;
  2265. }
  2266. }
  2267. #endif /* WOLFSSL_DTLS13 */
  2268. if (wolfSSL_CTX_SetMinVersion(ctx, minVersion) != WOLFSSL_SUCCESS)
  2269. err_sys_ex(catastrophic, "can't set minimum downgrade version");
  2270. }
  2271. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2272. /* Restore wolfSSL verify defaults */
  2273. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL);
  2274. #endif
  2275. #ifdef WOLFSSL_SRTP
  2276. if (dtlsSrtpProfiles != NULL) {
  2277. if (wolfSSL_CTX_set_tlsext_use_srtp(ctx, dtlsSrtpProfiles)
  2278. != WOLFSSL_SUCCESS) {
  2279. err_sys_ex(catastrophic, "unable to set DTLS SRTP profile");
  2280. }
  2281. }
  2282. #endif
  2283. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2284. if (wolfsentry_setup(&wolfsentry, wolfsentry_config_path,
  2285. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN) < 0) {
  2286. err_sys("unable to initialize wolfSentry");
  2287. }
  2288. if (wolfSSL_CTX_set_AcceptFilter(
  2289. ctx,
  2290. (NetworkFilterCallback_t)wolfSentry_NetworkFilterCallback,
  2291. wolfsentry) < 0) {
  2292. err_sys_ex(catastrophic,
  2293. "unable to install wolfSentry_NetworkFilterCallback");
  2294. }
  2295. #endif
  2296. if (simulateWantWrite)
  2297. {
  2298. #ifdef USE_WOLFSSL_IO
  2299. wolfSSL_CTX_SetIOSend(ctx, SimulateWantWriteIOSendCb);
  2300. #endif
  2301. }
  2302. #ifdef HAVE_TEST_SESSION_TICKET
  2303. if (TicketInit() != 0)
  2304. err_sys_ex(catastrophic, "unable to setup Session Ticket Key context");
  2305. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  2306. XMEMSET(&myTicketCtx, 0, sizeof(myTicketCtx));
  2307. wolfSSL_CTX_set_TicketEncCtx(ctx, &myTicketCtx);
  2308. #endif
  2309. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_STATIC_EPHEMERAL)
  2310. /* used for testing only to set a static/fixed ephemeral key
  2311. for use with the sniffer */
  2312. #if defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  2313. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  2314. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_ECDH,
  2315. "./certs/statickeys/ecc-secp256r1.pem", 0, WOLFSSL_FILETYPE_PEM);
  2316. if (ret != 0) {
  2317. err_sys_ex(runWithErrors, "error loading static ECDH key");
  2318. }
  2319. {
  2320. const byte* key = NULL;
  2321. word32 keySz = 0;
  2322. /* example for getting pointer to loaded static ephemeral key */
  2323. wolfSSL_CTX_get_ephemeral_key(ctx, WC_PK_TYPE_ECDH, &key, &keySz);
  2324. (void)key;
  2325. (void)keySz;
  2326. }
  2327. #endif
  2328. #ifndef NO_DH
  2329. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_DH,
  2330. "./certs/statickeys/dh-ffdhe2048.pem", 0, WOLFSSL_FILETYPE_PEM);
  2331. if (ret != 0) {
  2332. err_sys_ex(runWithErrors, "error loading static DH key");
  2333. }
  2334. #endif
  2335. #ifdef HAVE_CURVE25519
  2336. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_CURVE25519,
  2337. "./certs/statickeys/x25519.pem", 0, WOLFSSL_FILETYPE_PEM);
  2338. if (ret != 0) {
  2339. err_sys_ex(runWithErrors, "error loading static X25519 key");
  2340. }
  2341. #endif
  2342. #endif /* WOLFSSL_SNIFFER && WOLFSSL_STATIC_EPHEMERAL */
  2343. if (cipherList && !useDefCipherList) {
  2344. if (SSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS)
  2345. err_sys_ex(runWithErrors, "server can't set custom cipher list");
  2346. }
  2347. #ifdef WOLFSSL_LEANPSK
  2348. if (!usePsk) {
  2349. usePsk = 1;
  2350. }
  2351. #endif
  2352. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2353. !defined(HAVE_ED448)
  2354. if (!usePsk) {
  2355. usePsk = 1;
  2356. }
  2357. #endif
  2358. if (fewerPackets)
  2359. wolfSSL_CTX_set_group_messages(ctx);
  2360. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2361. defined(WOLFSSL_DTLS)
  2362. if (dtlsMTU)
  2363. wolfSSL_CTX_dtls_set_mtu(ctx, dtlsMTU);
  2364. #endif
  2365. #ifdef WOLFSSL_SCTP
  2366. if (dtlsSCTP)
  2367. wolfSSL_CTX_dtls_set_sctp(ctx);
  2368. #endif
  2369. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2370. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2371. #endif
  2372. #if !defined(NO_CERTS)
  2373. if ((!usePsk || usePskPlus) && !useAnon && !(loadCertKeyIntoSSLObj == 1)) {
  2374. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2375. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  2376. server_cert_der_2048, sizeof_server_cert_der_2048,
  2377. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2378. err_sys_ex(catastrophic, "can't load server cert buffer");
  2379. #elif !defined(TEST_LOAD_BUFFER)
  2380. if (SSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2381. != WOLFSSL_SUCCESS)
  2382. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2383. "and run from wolfSSL home dir");
  2384. #else
  2385. /* loads cert chain file using buffer API */
  2386. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2387. #endif
  2388. }
  2389. #endif
  2390. #ifndef NO_DH
  2391. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2392. != WOLFSSL_SUCCESS) {
  2393. err_sys_ex(runWithErrors, "Error setting minimum DH key size");
  2394. }
  2395. #endif
  2396. #ifndef NO_RSA
  2397. if (wolfSSL_CTX_SetMinRsaKey_Sz(ctx, minRsaKeyBits) != WOLFSSL_SUCCESS){
  2398. err_sys_ex(runWithErrors, "Error setting minimum RSA key size");
  2399. }
  2400. #endif
  2401. #ifdef HAVE_ECC
  2402. if (wolfSSL_CTX_SetMinEccKey_Sz(ctx, minEccKeyBits) != WOLFSSL_SUCCESS){
  2403. err_sys_ex(runWithErrors, "Error setting minimum ECC key size");
  2404. }
  2405. #endif
  2406. #if !defined(NO_CERTS)
  2407. #ifdef HAVE_PK_CALLBACKS
  2408. pkCbInfo.ourKey = ourKey;
  2409. #endif
  2410. if ((!usePsk || usePskPlus) && !useAnon
  2411. && !(loadCertKeyIntoSSLObj == 1)
  2412. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2413. && !pkCallbacks
  2414. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2415. ) {
  2416. #ifdef NO_FILESYSTEM
  2417. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2418. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2419. err_sys_ex(catastrophic, "can't load server private key buffer");
  2420. #elif !defined(TEST_LOAD_BUFFER)
  2421. if (SSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  2422. != WOLFSSL_SUCCESS)
  2423. err_sys_ex(catastrophic, "can't load server private key file, "
  2424. "check file and run from wolfSSL home dir");
  2425. #else
  2426. /* loads private key file using buffer API */
  2427. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  2428. #endif
  2429. }
  2430. #endif
  2431. if (usePsk || usePskPlus) {
  2432. #ifndef NO_PSK
  2433. const char *defaultCipherList = cipherList;
  2434. SSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  2435. #ifdef WOLFSSL_TLS13
  2436. wolfSSL_CTX_set_psk_server_tls13_callback(ctx, my_psk_server_tls13_cb);
  2437. #endif
  2438. if (sendPskIdentityHint == 1)
  2439. SSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  2440. if (defaultCipherList == NULL && !usePskPlus) {
  2441. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2442. #ifdef WOLFSSL_TLS13
  2443. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2444. #ifndef WOLFSSL_NO_TLS12
  2445. ":DHE-PSK-AES128-GCM-SHA256"
  2446. #endif
  2447. ;
  2448. #else
  2449. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2450. #endif
  2451. needDH = 1;
  2452. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  2453. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2454. #ifndef WOLFSSL_NO_TLS12
  2455. ":PSK-AES128-GCM-SHA256"
  2456. #endif
  2457. ;
  2458. #elif defined(HAVE_NULL_CIPHER)
  2459. defaultCipherList = "PSK-NULL-SHA256";
  2460. #elif !defined(NO_AES_CBC)
  2461. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2462. #else
  2463. defaultCipherList = "PSK-AES128-GCM-SHA256";
  2464. #endif
  2465. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2466. != WOLFSSL_SUCCESS)
  2467. err_sys_ex(runWithErrors, "server can't set cipher list 2");
  2468. }
  2469. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2470. #endif /* !NO_PSK */
  2471. }
  2472. #ifndef NO_CERTS
  2473. if (mutualAuth)
  2474. wolfSSL_CTX_mutual_auth(ctx, 1);
  2475. #endif
  2476. #ifdef HAVE_ECC
  2477. /* Use ECDHE key size that matches long term key.
  2478. * Zero means use ctx->privateKeySz.
  2479. * Default ECDHE_SIZE is 32 bytes
  2480. */
  2481. if (wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 0) != WOLFSSL_SUCCESS){
  2482. err_sys_ex(runWithErrors, "Error setting ECDHE size");
  2483. }
  2484. #endif
  2485. if (useAnon) {
  2486. #ifdef HAVE_ANON
  2487. wolfSSL_CTX_allow_anon_cipher(ctx);
  2488. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2489. const char* defaultCipherList;
  2490. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2491. "ADH-AES128-SHA";
  2492. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2493. != WOLFSSL_SUCCESS)
  2494. err_sys_ex(runWithErrors, "server can't set cipher list 4");
  2495. }
  2496. #endif
  2497. }
  2498. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2499. /* if not using PSK, verify peer with certs
  2500. if using PSK Plus then verify peer certs except PSK suites */
  2501. if (doCliCertCheck && (usePsk == 0 || usePskPlus) && useAnon == 0) {
  2502. unsigned int verify_flags = 0;
  2503. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  2504. (usePskPlus ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  2505. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  2506. (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR ||
  2507. myVerifyAction == VERIFY_FORCE_FAIL) ? myVerify : NULL);
  2508. #ifdef TEST_BEFORE_DATE
  2509. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2510. #endif
  2511. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2512. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2513. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2514. if (useCertFolder) {
  2515. WOLFSSL_X509_STORE *store;
  2516. WOLFSSL_X509_LOOKUP *lookup;
  2517. store = wolfSSL_CTX_get_cert_store(ctx);
  2518. if (store == NULL) {
  2519. wolfSSL_CTX_free(ctx); ctx = NULL;
  2520. err_sys("can't get WOLFSSL_X509_STORE");
  2521. }
  2522. lookup = wolfSSL_X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  2523. if (lookup == NULL) {
  2524. wolfSSL_CTX_free(ctx); ctx = NULL;
  2525. err_sys("can't add lookup");
  2526. }
  2527. if (wolfSSL_X509_LOOKUP_ctrl(lookup, WOLFSSL_X509_L_ADD_DIR, caCertFolder,
  2528. X509_FILETYPE_PEM, NULL) != WOLFSSL_SUCCESS) {
  2529. err_sys("X509_LOOKUP_ctrl w/ L_ADD_DIR failed");
  2530. }
  2531. } else {
  2532. #endif
  2533. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2534. verify_flags) != WOLFSSL_SUCCESS) {
  2535. err_sys_ex(catastrophic,
  2536. "can't load ca file, Please run from wolfSSL home dir");
  2537. }
  2538. #ifdef WOLFSSL_TRUST_PEER_CERT
  2539. if (trustCert) {
  2540. if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  2541. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2542. err_sys_ex(runWithErrors, "can't load trusted peer cert file");
  2543. }
  2544. }
  2545. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2546. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2547. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2548. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2549. }
  2550. #endif
  2551. }
  2552. #endif
  2553. #ifdef WOLFSSL_SNIFFER
  2554. if (cipherList == NULL && version < 4) {
  2555. /* static RSA or static ECC cipher suites */
  2556. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2557. if (SSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2558. err_sys_ex(runWithErrors, "server can't set cipher list 3");
  2559. }
  2560. }
  2561. #endif
  2562. #ifdef HAVE_SNI
  2563. if (sniHostName)
  2564. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  2565. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS)
  2566. err_sys_ex(runWithErrors, "UseSNI failed");
  2567. #endif
  2568. #ifdef USE_WINDOWS_API
  2569. if (port == 0) {
  2570. /* Generate random port for testing */
  2571. port = GetRandomPort();
  2572. }
  2573. #endif /* USE_WINDOWS_API */
  2574. #ifdef WOLFSSL_ASYNC_CRYPT
  2575. ret = wolfAsync_DevOpen(&devId);
  2576. if (ret < 0) {
  2577. fprintf(stderr, "Async device open failed\nRunning without async\n");
  2578. }
  2579. wolfSSL_CTX_SetDevId(ctx, devId);
  2580. #endif /* WOLFSSL_ASYNC_CRYPT */
  2581. #ifdef WOLFSSL_TLS13
  2582. if (noPskDheKe)
  2583. wolfSSL_CTX_no_dhe_psk(ctx);
  2584. #endif
  2585. #ifdef HAVE_SESSION_TICKET
  2586. #ifdef WOLFSSL_TLS13
  2587. if (noTicketTls13)
  2588. wolfSSL_CTX_no_ticket_TLSv13(ctx);
  2589. #endif
  2590. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  2591. if (noTicketTls12)
  2592. wolfSSL_CTX_NoTicketTLSv12(ctx);
  2593. #endif
  2594. #endif
  2595. while (1) {
  2596. /* allow resume option */
  2597. if (resumeCount > 1) {
  2598. if (dtlsUDP == 0) {
  2599. client_len = sizeof client_addr;
  2600. clientfd = accept(sockfd, (struct sockaddr*)&client_addr,
  2601. (ACCEPT_THIRD_T)&client_len);
  2602. }
  2603. else {
  2604. tcp_listen(&sockfd, &port, useAnyAddr, dtlsUDP, dtlsSCTP);
  2605. clientfd = sockfd;
  2606. }
  2607. if (WOLFSSL_SOCKET_IS_INVALID(clientfd)) {
  2608. err_sys_ex(runWithErrors, "tcp accept failed");
  2609. }
  2610. }
  2611. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2612. fprintf(stderr, "Before creating SSL\n");
  2613. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2614. err_sys_ex(runWithErrors, "ctx not using static memory");
  2615. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2616. err_sys_ex(runWithErrors, "error printing out memory stats");
  2617. #endif
  2618. if (doMcast) {
  2619. #ifdef WOLFSSL_MULTICAST
  2620. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  2621. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  2622. != WOLFSSL_SUCCESS)
  2623. err_sys("Couldn't set multicast cipher list.");
  2624. #endif
  2625. }
  2626. if (doDTLS && dtlsUDP) {
  2627. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  2628. if (doBlockSeq) {
  2629. wolfSSL_CTX_SetIOSend(ctx, TestEmbedSendTo);
  2630. }
  2631. #endif
  2632. }
  2633. #ifdef HAVE_PK_CALLBACKS
  2634. if (pkCallbacks)
  2635. SetupPkCallbacks(ctx);
  2636. #endif
  2637. ssl = SSL_new(ctx);
  2638. if (ssl == NULL)
  2639. err_sys_ex(catastrophic, "unable to create an SSL object");
  2640. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  2641. wolfSSL_KeepArrays(ssl);
  2642. #endif
  2643. /* Support for loading private key and cert using WOLFSSL object */
  2644. #if !defined(NO_CERTS)
  2645. if ((!usePsk || usePskPlus) && !useAnon && loadCertKeyIntoSSLObj) {
  2646. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2647. if (wolfSSL_use_certificate_chain_buffer_format(ssl,
  2648. server_cert_der_2048, sizeof_server_cert_der_2048,
  2649. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2650. err_sys_ex(catastrophic, "can't load server cert buffer");
  2651. #elif !defined(TEST_LOAD_BUFFER)
  2652. if (SSL_use_certificate_chain_file(ssl, ourCert)
  2653. != WOLFSSL_SUCCESS)
  2654. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2655. "and run from wolfSSL home dir");
  2656. #else
  2657. /* loads cert chain file using buffer API */
  2658. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  2659. #endif
  2660. }
  2661. if ((!usePsk || usePskPlus) && !useAnon &&
  2662. loadCertKeyIntoSSLObj
  2663. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2664. && !pkCallbacks
  2665. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2666. ) {
  2667. #if defined(NO_FILESYSTEM)
  2668. if (wolfSSL_use_PrivateKey_buffer(ssl, server_key_der_2048,
  2669. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2670. err_sys_ex(catastrophic, "can't load server private key buffer");
  2671. #elif !defined(TEST_LOAD_BUFFER)
  2672. if (SSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  2673. != WOLFSSL_SUCCESS)
  2674. err_sys_ex(catastrophic, "can't load server private key file, check"
  2675. "file and run from wolfSSL home dir");
  2676. #else
  2677. /* loads private key file using buffer API */
  2678. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  2679. #endif
  2680. }
  2681. #endif /* !NO_CERTS */
  2682. #ifdef WOLFSSL_SEND_HRR_COOKIE
  2683. if (hrrCookie == 1 && wolfSSL_send_hrr_cookie(ssl, NULL, 0)
  2684. != WOLFSSL_SUCCESS) {
  2685. err_sys("unable to set use of cookie with HRR msg");
  2686. }
  2687. else if (hrrCookie == -1) {
  2688. wolfSSL_disable_hrr_cookie(ssl);
  2689. }
  2690. #endif
  2691. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2692. fprintf(stderr, "After creating SSL\n");
  2693. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2694. err_sys_ex(runWithErrors, "ctx not using static memory");
  2695. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2696. err_sys_ex(runWithErrors, "error printing out memory stats");
  2697. #endif
  2698. if (doMcast) {
  2699. #ifdef WOLFSSL_MULTICAST
  2700. /* DTLS multicast secret for testing only */
  2701. #define CLI_SRV_RANDOM_SZ 32 /* RAN_LEN (see internal.h) */
  2702. #define PMS_SZ 512 /* ENCRYPT_LEN (see internal.h) */
  2703. byte pms[PMS_SZ]; /* pre master secret */
  2704. byte cr[CLI_SRV_RANDOM_SZ]; /* client random */
  2705. byte sr[CLI_SRV_RANDOM_SZ]; /* server random */
  2706. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  2707. XMEMSET(pms, 0x23, sizeof(pms));
  2708. XMEMSET(cr, 0xA5, sizeof(cr));
  2709. XMEMSET(sr, 0x5A, sizeof(sr));
  2710. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  2711. != WOLFSSL_SUCCESS) {
  2712. err_sys("unable to set mcast secret");
  2713. }
  2714. #endif
  2715. }
  2716. #ifdef HAVE_SECURE_RENEGOTIATION
  2717. if (scr) {
  2718. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  2719. err_sys_ex(runWithErrors, "can't enable secure renegotiation");
  2720. }
  2721. }
  2722. #endif /* HAVE_SECURE_RENEGOTIATION */
  2723. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2724. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2725. if (postHandAuth) {
  2726. unsigned int verify_flags = 0;
  2727. #ifdef TEST_BEFORE_DATE
  2728. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2729. #endif
  2730. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2731. verify_flags)
  2732. != WOLFSSL_SUCCESS) {
  2733. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2734. "wolfSSL home dir");
  2735. }
  2736. #ifdef WOLFSSL_TRUST_PEER_CERT
  2737. if (trustCert) {
  2738. if (wolfSSL_trust_peer_cert(ssl, trustCert,
  2739. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2740. err_sys_ex(runWithErrors, "can't load trusted peer cert "
  2741. "file");
  2742. }
  2743. }
  2744. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2745. }
  2746. #endif
  2747. #endif
  2748. #ifndef NO_HANDSHAKE_DONE_CB
  2749. wolfSSL_SetHsDoneCb(ssl, myHsDoneCb, NULL);
  2750. #endif
  2751. #ifdef HAVE_CRL
  2752. if (!disableCRL) {
  2753. #ifdef HAVE_CRL_MONITOR
  2754. crlFlags = WOLFSSL_CRL_MONITOR | WOLFSSL_CRL_START_MON;
  2755. #endif
  2756. if (wolfSSL_EnableCRL(ssl, 0) != WOLFSSL_SUCCESS)
  2757. err_sys_ex(runWithErrors, "unable to enable CRL");
  2758. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, crlFlags)
  2759. != WOLFSSL_SUCCESS)
  2760. err_sys_ex(runWithErrors, "unable to load CRL");
  2761. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS)
  2762. err_sys_ex(runWithErrors, "unable to set CRL callback url");
  2763. }
  2764. #endif
  2765. #ifdef HAVE_OCSP
  2766. if (useOcsp) {
  2767. if (ocspUrl != NULL) {
  2768. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2769. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2770. | WOLFSSL_OCSP_URL_OVERRIDE);
  2771. }
  2772. else
  2773. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE);
  2774. }
  2775. #ifndef NO_RSA
  2776. /* All the OCSP Stapling test certs are RSA. */
  2777. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2778. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2779. { /* scope start */
  2780. const char* ca1 = "certs/ocsp/intermediate1-ca-cert.pem";
  2781. const char* ca2 = "certs/ocsp/intermediate2-ca-cert.pem";
  2782. const char* ca3 = "certs/ocsp/intermediate3-ca-cert.pem";
  2783. int fails = 0;
  2784. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS) {
  2785. err_sys_ex(catastrophic, "can't enable OCSP Stapling "
  2786. "Certificate Manager");
  2787. }
  2788. if (SSL_CTX_load_verify_locations(ctx, ca1, 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, ca2, 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 (SSL_CTX_load_verify_locations(ctx, ca3, 0) != WOLFSSL_SUCCESS) {
  2799. fails++;
  2800. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2801. "wolfSSL home dir");
  2802. }
  2803. if (fails > 2) {
  2804. err_sys_ex(catastrophic, "Failed to load any intermediates for "
  2805. "OCSP stapling test");
  2806. }
  2807. } /* scope end */
  2808. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  2809. #endif /* NO_RSA */
  2810. #endif /* HAVE_OCSP */
  2811. #ifdef HAVE_PK_CALLBACKS
  2812. /* This must be before SetKeyShare */
  2813. if (pkCallbacks) {
  2814. SetupPkCallbackContexts(ssl, &pkCbInfo);
  2815. }
  2816. #endif
  2817. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2818. if (version >= 4 || version == -4) {
  2819. #ifdef CAN_FORCE_CURVE
  2820. if (force_curve_group_id > 0) {
  2821. do {
  2822. ret = wolfSSL_UseKeyShare(ssl, force_curve_group_id);
  2823. if (ret == WOLFSSL_SUCCESS) {
  2824. }
  2825. #ifdef WOLFSSL_ASYNC_CRYPT
  2826. else if (ret == WC_PENDING_E) {
  2827. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2828. }
  2829. #endif
  2830. else {
  2831. err_sys("Failed wolfSSL_UseKeyShare in force-curve");
  2832. }
  2833. } while (ret == WC_PENDING_E);
  2834. ret = wolfSSL_set_groups(ssl, &force_curve_group_id, 1);
  2835. if (WOLFSSL_SUCCESS != ret) {
  2836. err_sys("Failed wolfSSL_set_groups in force-curve");
  2837. }
  2838. }
  2839. else
  2840. #endif
  2841. SetKeyShare(ssl, onlyKeyShare, useX25519, useX448, usePqc,
  2842. pqcAlg);
  2843. }
  2844. #endif
  2845. #ifdef HAVE_ENCRYPT_THEN_MAC
  2846. if (disallowETM)
  2847. wolfSSL_AllowEncryptThenMac(ssl, 0);
  2848. #endif
  2849. /* do accept */
  2850. readySignal = ((func_args*)args)->signal;
  2851. if (readySignal) {
  2852. readySignal->srfName = serverReadyFile;
  2853. }
  2854. client_len = sizeof client_addr;
  2855. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr,
  2856. dtlsUDP, dtlsSCTP, serverReadyFile ? 1 : 0, doListen,
  2857. &client_addr, &client_len);
  2858. doListen = 0; /* Don't listen next time */
  2859. if (port == 0) {
  2860. port = readySignal->port;
  2861. }
  2862. if (SSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2863. err_sys_ex(catastrophic, "error in setting fd");
  2864. }
  2865. #ifdef HAVE_TRUSTED_CA
  2866. if (trustedCaKeyId) {
  2867. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  2868. NULL, 0) != WOLFSSL_SUCCESS) {
  2869. err_sys_ex(runWithErrors, "UseTrustedCA failed");
  2870. }
  2871. }
  2872. #endif /* HAVE_TRUSTED_CA */
  2873. #ifdef HAVE_ALPN
  2874. if (alpnList != NULL) {
  2875. printf("ALPN accepted protocols list : %s\n", alpnList);
  2876. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  2877. }
  2878. #endif
  2879. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  2880. if (doDTLS && dtlsUDP) {
  2881. byte b[1500];
  2882. int n;
  2883. int isClientHello = 0;
  2884. while (!isClientHello) {
  2885. client_len = sizeof client_addr;
  2886. /* For DTLS, peek at the next datagram so we can get the
  2887. * client's address and set it into the ssl object later to
  2888. * generate the cookie. */
  2889. n = (int)recvfrom(clientfd, (char*)b, sizeof(b), MSG_PEEK,
  2890. (struct sockaddr*)&client_addr, &client_len);
  2891. if (n <= 0)
  2892. err_sys_ex(runWithErrors, "recvfrom failed");
  2893. /* when doing resumption, it may happen that we receive the
  2894. alert used to shutdown the first connection as the first
  2895. packet of the second accept:
  2896. Client | Server
  2897. | WolfSSL_Shutdown()
  2898. | <- Alert
  2899. | recvfrom(peek)
  2900. WolfSSL_Shutdown() |
  2901. Alert-> |
  2902. | wolfSSL_set_dtls_peer()
  2903. but this will set the wrong src port, making the test fail.
  2904. Discard not-handshake message to avoid this.
  2905. */
  2906. if (b[0] != 0x16) {
  2907. /* discard the packet */
  2908. n = (int)recvfrom(clientfd, (char *)b, sizeof(b), 0,
  2909. (struct sockaddr *)&client_addr, &client_len);
  2910. if (n <= 0)
  2911. err_sys_ex(runWithErrors, "recvfrom failed");
  2912. }
  2913. else {
  2914. isClientHello = 1;
  2915. }
  2916. }
  2917. if (doBlockSeq) {
  2918. XMEMCPY(&dtlsCtx.peer.sa, &client_addr, client_len);
  2919. dtlsCtx.peer.sz = client_len;
  2920. dtlsCtx.wfd = clientfd;
  2921. dtlsCtx.failOnce = 1;
  2922. wolfSSL_SetIOWriteCtx(ssl, &dtlsCtx);
  2923. }
  2924. else {
  2925. wolfSSL_dtls_set_peer(ssl, &client_addr, client_len);
  2926. }
  2927. if (simulateWantWrite) {
  2928. #ifdef USE_WOLFSSL_IO
  2929. /* connect on a udp to associate peer with this fd to make it
  2930. * simpler for SimulateWantWriteIOSendCb */
  2931. if (connect(clientfd, (struct sockaddr*)&client_addr,
  2932. client_len) != 0) {
  2933. err_sys_ex(catastrophic, "error in connecting to peer");
  2934. }
  2935. wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd);
  2936. #endif
  2937. }
  2938. }
  2939. #endif
  2940. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2941. {
  2942. SOCKADDR_IN_T local_addr;
  2943. socklen_t local_len = sizeof(local_addr);
  2944. getsockname(clientfd, (struct sockaddr *)&local_addr,
  2945. (socklen_t *)&local_len);
  2946. if (((struct sockaddr *)&client_addr)->sa_family !=
  2947. ((struct sockaddr *)&local_addr)->sa_family)
  2948. err_sys_ex(catastrophic,
  2949. "client_addr.sa_family != local_addr.sa_family");
  2950. if (wolfsentry_store_endpoints(
  2951. ssl, &client_addr, &local_addr,
  2952. dtlsUDP ? IPPROTO_UDP : IPPROTO_TCP,
  2953. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN, NULL) != WOLFSSL_SUCCESS)
  2954. err_sys_ex(catastrophic,
  2955. "error in wolfsentry_store_endpoints()");
  2956. }
  2957. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  2958. if ((usePsk == 0 || usePskPlus) || useAnon == 1 || cipherList != NULL
  2959. || needDH == 1) {
  2960. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && !defined(NO_ASN)
  2961. wolfSSL_SetTmpDH_file(ssl, ourDhParam, WOLFSSL_FILETYPE_PEM);
  2962. #elif !defined(NO_DH)
  2963. SetDH(ssl); /* repick suites with DHE, higher priority than
  2964. * PSK */
  2965. #endif
  2966. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  2967. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  2968. if (!doDhKeyCheck)
  2969. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  2970. #endif
  2971. }
  2972. #ifdef WOLFSSL_DTLS_CID
  2973. if (useDtlsCID) {
  2974. ret = wolfSSL_dtls_cid_use(ssl);
  2975. if (ret != WOLFSSL_SUCCESS)
  2976. err_sys("Can't enable DTLS ConnectionID");
  2977. ret = wolfSSL_dtls_cid_set(ssl, (byte*)dtlsCID,
  2978. (word32)XSTRLEN(dtlsCID));
  2979. if (ret != WOLFSSL_SUCCESS)
  2980. err_sys("Can't set DTLS ConnectionID");
  2981. }
  2982. #endif /* WOLFSSL_DTLS_CID */
  2983. #ifndef WOLFSSL_CALLBACKS
  2984. if (nonBlocking) {
  2985. #ifdef WOLFSSL_DTLS
  2986. if (doDTLS) {
  2987. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  2988. }
  2989. #endif
  2990. tcp_set_nonblocking(&clientfd);
  2991. ret = NonBlockingSSL_Accept(ssl);
  2992. }
  2993. else {
  2994. #ifdef WOLFSSL_EARLY_DATA
  2995. if (earlyData) {
  2996. do {
  2997. int len;
  2998. err = 0; /* reset error */
  2999. ret = wolfSSL_read_early_data(ssl, input, sizeof(input)-1,
  3000. &len);
  3001. if (ret != WOLFSSL_SUCCESS) {
  3002. err = SSL_get_error(ssl, 0);
  3003. #ifdef WOLFSSL_ASYNC_CRYPT
  3004. if (err == WC_PENDING_E) {
  3005. /* returns the number of polled items or <0 for
  3006. * error */
  3007. ret = wolfSSL_AsyncPoll(ssl,
  3008. WOLF_POLL_FLAG_CHECK_HW);
  3009. if (ret < 0) break;
  3010. }
  3011. #endif
  3012. }
  3013. else if (ret > 0) {
  3014. input[ret] = 0; /* null terminate message */
  3015. printf("Early Data Client message: %s\n", input);
  3016. }
  3017. } while (err == WC_PENDING_E || ret > 0);
  3018. }
  3019. #endif
  3020. do {
  3021. err = 0; /* reset error */
  3022. ret = SSL_accept(ssl);
  3023. if (ret != WOLFSSL_SUCCESS) {
  3024. err = SSL_get_error(ssl, 0);
  3025. #ifdef WOLFSSL_ASYNC_CRYPT
  3026. if (err == WC_PENDING_E) {
  3027. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3028. if (ret < 0) break;
  3029. }
  3030. #endif
  3031. }
  3032. } while (err == WC_PENDING_E);
  3033. }
  3034. #else
  3035. (void)nonBlocking;
  3036. ret = NonBlockingSSL_Accept(ssl);
  3037. #endif
  3038. #ifdef WOLFSSL_EARLY_DATA
  3039. EarlyDataStatus(ssl);
  3040. #endif
  3041. if (ret != WOLFSSL_SUCCESS) {
  3042. err = SSL_get_error(ssl, 0);
  3043. fprintf(stderr, "SSL_accept error %d, %s\n", err,
  3044. ERR_error_string(err, buffer));
  3045. if (!exitWithRet) {
  3046. err_sys_ex(runWithErrors, "SSL_accept failed");
  3047. } else {
  3048. /* cleanup */
  3049. SSL_free(ssl); ssl = NULL;
  3050. SSL_CTX_free(ctx); ctx = NULL;
  3051. CloseSocket(clientfd);
  3052. CloseSocket(sockfd);
  3053. ((func_args*)args)->return_code = err;
  3054. goto exit;
  3055. }
  3056. }
  3057. showPeerEx(ssl, lng_index);
  3058. if (SSL_state(ssl) != 0) {
  3059. err_sys_ex(runWithErrors, "SSL in error state");
  3060. }
  3061. /* if the caller requested a particular cipher, check here that either
  3062. * a canonical name of the established cipher matches the requested
  3063. * cipher name, or the requested cipher name is marked as an alias
  3064. * that matches the established cipher.
  3065. */
  3066. if (cipherList && !useDefCipherList && (! XSTRSTR(cipherList, ":"))) {
  3067. WOLFSSL_CIPHER* established_cipher = wolfSSL_get_current_cipher(ssl);
  3068. byte requested_cipherSuite0, requested_cipherSuite;
  3069. int requested_cipherFlags;
  3070. if (established_cipher &&
  3071. /* don't test for pseudo-ciphers like "ALL" and "DEFAULT". */
  3072. (wolfSSL_get_cipher_suite_from_name(cipherList,
  3073. &requested_cipherSuite0,
  3074. &requested_cipherSuite,
  3075. &requested_cipherFlags) == 0)) {
  3076. word32 established_cipher_id = wolfSSL_CIPHER_get_id(established_cipher);
  3077. byte established_cipherSuite0 = (established_cipher_id >> 8) & 0xff;
  3078. byte established_cipherSuite = established_cipher_id & 0xff;
  3079. const char *established_cipher_name =
  3080. wolfSSL_get_cipher_name_from_suite(established_cipherSuite0,
  3081. established_cipherSuite);
  3082. const char *established_cipher_name_iana =
  3083. wolfSSL_get_cipher_name_iana_from_suite(established_cipherSuite0,
  3084. established_cipherSuite);
  3085. if (established_cipher_name == NULL)
  3086. err_sys_ex(catastrophic, "error looking up name of established cipher");
  3087. if (strcmp(cipherList, established_cipher_name) &&
  3088. ((established_cipher_name_iana == NULL) ||
  3089. strcmp(cipherList, established_cipher_name_iana))) {
  3090. if (! (requested_cipherFlags & WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS))
  3091. err_sys_ex(
  3092. catastrophic,
  3093. "Unexpected mismatch between names of requested and established ciphers.");
  3094. else if ((requested_cipherSuite0 != established_cipherSuite0) ||
  3095. (requested_cipherSuite != established_cipherSuite))
  3096. err_sys_ex(
  3097. catastrophic,
  3098. "Mismatch between IDs of requested and established ciphers.");
  3099. }
  3100. }
  3101. }
  3102. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  3103. {
  3104. byte* rnd = NULL;
  3105. byte* pt;
  3106. size_t size;
  3107. /* get size of buffer then print */
  3108. size = wolfSSL_get_server_random(NULL, NULL, 0);
  3109. if (size == 0) {
  3110. err_sys_ex(runWithErrors, "error getting server random buffer "
  3111. "size");
  3112. }
  3113. else {
  3114. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3115. }
  3116. if (rnd == NULL) {
  3117. err_sys_ex(runWithErrors, "error creating server random buffer");
  3118. }
  3119. size = wolfSSL_get_server_random(ssl, rnd, size);
  3120. if (size == 0) {
  3121. if (rnd) {
  3122. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3123. rnd = NULL;
  3124. }
  3125. err_sys_ex(runWithErrors, "error getting server random buffer");
  3126. }
  3127. if (rnd) {
  3128. printf("Server Random : ");
  3129. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  3130. printf("\n");
  3131. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3132. rnd = NULL;
  3133. }
  3134. }
  3135. #endif
  3136. #ifdef WOLFSSL_SRTP
  3137. if (dtlsSrtpProfiles != NULL) {
  3138. err = server_srtp_test(ssl, (func_args*)args);
  3139. if (err != 0) {
  3140. if (exitWithRet) {
  3141. ((func_args*)args)->return_code = err;
  3142. wolfSSL_free(ssl); ssl = NULL;
  3143. wolfSSL_CTX_free(ctx); ctx = NULL;
  3144. goto exit;
  3145. }
  3146. /* else */
  3147. err_sys("SRTP check failed");
  3148. }
  3149. }
  3150. #endif /* WOLFSSL_SRTP */
  3151. #ifdef WOLFSSL_DTLS_CID
  3152. if (useDtlsCID && wolfSSL_dtls_cid_is_enabled(ssl)) {
  3153. byte receivedCID[DTLS_CID_BUFFER_SIZE];
  3154. unsigned int receivedCIDSz;
  3155. printf("CID extension was negotiated\n");
  3156. ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
  3157. if (ret != WOLFSSL_SUCCESS)
  3158. err_sys("Can't get negotiated DTLS CID size\n");
  3159. if (receivedCIDSz > 0) {
  3160. ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
  3161. DTLS_CID_BUFFER_SIZE - 1);
  3162. if (ret != WOLFSSL_SUCCESS)
  3163. err_sys("Can't get negotiated DTLS CID\n");
  3164. printf("Sending CID is ");
  3165. printBuffer(receivedCID, receivedCIDSz);
  3166. printf("\n");
  3167. }
  3168. else {
  3169. printf("other peer provided empty CID\n");
  3170. }
  3171. }
  3172. #endif
  3173. #ifdef HAVE_ALPN
  3174. if (alpnList != NULL) {
  3175. char *protocol_name = NULL, *list = NULL;
  3176. word16 protocol_nameSz = 0, listSz = 0;
  3177. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name,
  3178. &protocol_nameSz);
  3179. if (err == WOLFSSL_SUCCESS)
  3180. printf("Sent ALPN protocol : %s (%d)\n",
  3181. protocol_name, protocol_nameSz);
  3182. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  3183. printf("No ALPN response sent (no match)\n");
  3184. else
  3185. printf("Getting ALPN protocol name failed\n");
  3186. err = wolfSSL_ALPN_GetPeerProtocol(ssl, &list, &listSz);
  3187. if (err == WOLFSSL_SUCCESS)
  3188. printf("List of protocol names sent by Client: %s (%d)\n",
  3189. list, listSz);
  3190. else
  3191. printf("Get list of client's protocol name failed\n");
  3192. free(list);
  3193. }
  3194. #endif
  3195. if (echoData == 0 && throughput == 0) {
  3196. ServerRead(ssl, input, sizeof(input)-1);
  3197. err = SSL_get_error(ssl, 0);
  3198. }
  3199. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  3200. defined(HAVE_SERVER_RENEGOTIATION_INFO)
  3201. if (scr && forceScr) {
  3202. if (nonBlocking) {
  3203. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3204. err = wolfSSL_get_error(ssl, 0);
  3205. if (err == WOLFSSL_ERROR_WANT_READ ||
  3206. err == WOLFSSL_ERROR_WANT_WRITE) {
  3207. do {
  3208. #ifdef WOLFSSL_ASYNC_CRYPT
  3209. if (err == WC_PENDING_E) {
  3210. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3211. if (ret < 0) break;
  3212. }
  3213. #endif
  3214. if (err == APP_DATA_READY) {
  3215. if (wolfSSL_read(ssl, input, sizeof(input)-1) < 0) {
  3216. err_sys("APP DATA should be present but error returned");
  3217. }
  3218. printf("Received message: %s\n", input);
  3219. }
  3220. err = 0;
  3221. if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
  3222. err = wolfSSL_get_error(ssl, ret);
  3223. }
  3224. } while (ret != WOLFSSL_SUCCESS &&
  3225. (err == WOLFSSL_ERROR_WANT_READ ||
  3226. err == WOLFSSL_ERROR_WANT_WRITE ||
  3227. err == APP_DATA_READY ||
  3228. err == WC_PENDING_E));
  3229. if (ret == WOLFSSL_SUCCESS) {
  3230. printf("NON-BLOCKING RENEGOTIATION SUCCESSFUL\n");
  3231. err = 0;
  3232. }
  3233. }
  3234. if (ret != WOLFSSL_SUCCESS) {
  3235. err = wolfSSL_get_error(ssl, 0);
  3236. fprintf(stderr,
  3237. "wolfSSL_Rehandshake error %d, %s\n", err,
  3238. wolfSSL_ERR_error_string(err, buffer));
  3239. wolfSSL_free(ssl); ssl = NULL;
  3240. wolfSSL_CTX_free(ctx); ctx = NULL;
  3241. err_sys("non-blocking wolfSSL_Rehandshake failed");
  3242. }
  3243. }
  3244. } else {
  3245. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3246. #ifdef WOLFSSL_ASYNC_CRYPT
  3247. err = wolfSSL_get_error(ssl, 0);
  3248. while (err == WC_PENDING_E) {
  3249. err = 0;
  3250. ret = wolfSSL_negotiate(ssl);
  3251. if (ret != WOLFSSL_SUCCESS) {
  3252. err = wolfSSL_get_error(ssl, 0);
  3253. if (err == WC_PENDING_E) {
  3254. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3255. if (ret < 0) break;
  3256. }
  3257. }
  3258. }
  3259. if (ret != WOLFSSL_SUCCESS)
  3260. #endif
  3261. printf("not doing secure renegotiation\n");
  3262. }
  3263. else {
  3264. printf("RENEGOTIATION SUCCESSFUL\n");
  3265. }
  3266. }
  3267. }
  3268. #endif /* HAVE_SECURE_RENEGOTIATION */
  3269. if (err == 0 && echoData == 0 && throughput == 0) {
  3270. const char* write_msg;
  3271. int write_msg_sz;
  3272. #ifdef WOLFSSL_TLS13
  3273. if (updateKeysIVs)
  3274. wolfSSL_update_keys(ssl);
  3275. #endif
  3276. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  3277. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3278. if (postHandAuth) {
  3279. SSL_set_verify(ssl, WOLFSSL_VERIFY_PEER |
  3280. ((usePskPlus) ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  3281. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT), 0);
  3282. wolfSSL_request_certificate(ssl);
  3283. }
  3284. #endif
  3285. #endif
  3286. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  3287. if (sendTicket) {
  3288. if (wolfSSL_send_SessionTicket(ssl) != WOLFSSL_SUCCESS) {
  3289. fprintf(stderr, "Sending new session ticket failed\n");
  3290. }
  3291. else {
  3292. fprintf(stderr, "New session ticket sent\n");
  3293. }
  3294. }
  3295. #endif
  3296. /* Write data */
  3297. if (!useWebServerMsg) {
  3298. write_msg = kReplyMsg;
  3299. write_msg_sz = (int)XSTRLEN(kReplyMsg);
  3300. }
  3301. else {
  3302. write_msg = kHttpServerMsg;
  3303. write_msg_sz = (int)XSTRLEN(kHttpServerMsg);
  3304. }
  3305. ServerWrite(ssl, write_msg, write_msg_sz);
  3306. #ifdef WOLFSSL_TLS13
  3307. if (updateKeysIVs || postHandAuth)
  3308. ServerRead(ssl, input, sizeof(input)-1);
  3309. #endif
  3310. }
  3311. else if (err == 0 || err == WOLFSSL_ERROR_ZERO_RETURN) {
  3312. err = ServerEchoData(ssl, clientfd, echoData, block, throughput);
  3313. /* Got close notify. Ignore it if not expecting a failure. */
  3314. if (err == WOLFSSL_ERROR_ZERO_RETURN && exitWithRet == 0)
  3315. err = 0;
  3316. if (err != 0) {
  3317. SSL_free(ssl); ssl = NULL;
  3318. SSL_CTX_free(ctx); ctx = NULL;
  3319. CloseSocket(clientfd);
  3320. CloseSocket(sockfd);
  3321. ((func_args*)args)->return_code = err;
  3322. goto exit;
  3323. }
  3324. }
  3325. #if defined(WOLFSSL_MDK_SHELL) && defined(HAVE_MDK_RTX)
  3326. os_dly_wait(500) ;
  3327. #elif defined (WOLFSSL_TIRTOS)
  3328. Task_yield();
  3329. #endif
  3330. #if defined(WOLFSSL_DTLS13)
  3331. if (wolfSSL_dtls(ssl) && version == -4) {
  3332. int zero_return = 0;
  3333. while (wolfSSL_dtls13_has_pending_msg(ssl)) {
  3334. err =
  3335. process_handshake_messages(ssl, !nonBlocking, &zero_return);
  3336. if (err < 0) {
  3337. /* other peer closes the connection, non fatal */
  3338. if (zero_return)
  3339. break;
  3340. err_sys("Error while processing pending DTLSv1.3 messages");
  3341. }
  3342. }
  3343. }
  3344. #endif /* WOLFSSL_DTLS13 */
  3345. ret = SSL_shutdown(ssl);
  3346. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  3347. while (tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC) ==
  3348. TEST_RECV_READY) {
  3349. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  3350. if (ret == WOLFSSL_SUCCESS) {
  3351. printf("Bidirectional shutdown complete\n");
  3352. break;
  3353. }
  3354. else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
  3355. fprintf(stderr, "Bidirectional shutdown failed\n");
  3356. break;
  3357. }
  3358. }
  3359. if (ret != WOLFSSL_SUCCESS)
  3360. fprintf(stderr, "Bidirectional shutdown failed\n");
  3361. }
  3362. /* display collected statistics */
  3363. #ifdef WOLFSSL_STATIC_MEMORY
  3364. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  3365. err_sys_ex(runWithErrors, "static memory was not used with ssl");
  3366. fprintf(stderr, "\nprint off SSL memory stats\n");
  3367. fprintf(stderr, "*** This is memory state before wolfSSL_free is "
  3368. "called\n");
  3369. wolfSSL_PrintStatsConn(&ssl_stats);
  3370. #endif
  3371. SSL_free(ssl); ssl = NULL;
  3372. CloseSocket(clientfd);
  3373. if (resume == 1 && resumeCount == 0) {
  3374. resumeCount++; /* only do one resume for testing */
  3375. continue;
  3376. }
  3377. resumeCount = 0;
  3378. cnt++;
  3379. if (loops > 0 && --loops == 0) {
  3380. break; /* out of while loop, done with normal and resume option */
  3381. }
  3382. } /* while(1) */
  3383. WOLFSSL_TIME(cnt);
  3384. (void)cnt;
  3385. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  3386. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3387. wolfSSL_CTX_DisableOCSPStapling(ctx);
  3388. #endif
  3389. CloseSocket(sockfd);
  3390. SSL_CTX_free(ctx); ctx = NULL;
  3391. ((func_args*)args)->return_code = 0;
  3392. exit:
  3393. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  3394. wolfsentry_ret = wolfsentry_shutdown(&wolfsentry);
  3395. if (wolfsentry_ret < 0) {
  3396. fprintf(stderr,
  3397. "wolfsentry_shutdown() returned " WOLFSENTRY_ERROR_FMT "\n",
  3398. WOLFSENTRY_ERROR_FMT_ARGS(wolfsentry_ret));
  3399. }
  3400. #endif
  3401. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  3402. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  3403. wc_ecc_fp_free(); /* free per thread cache */
  3404. #endif
  3405. #ifdef WOLFSSL_TIRTOS
  3406. fdCloseSession(Task_self());
  3407. #endif
  3408. #ifdef HAVE_TEST_SESSION_TICKET
  3409. TicketCleanup();
  3410. #endif
  3411. #ifdef WOLFSSL_ASYNC_CRYPT
  3412. wolfAsync_DevClose(&devId);
  3413. #endif
  3414. /* There are use cases when these assignments are not read. To avoid
  3415. * potential confusion those warnings have been handled here.
  3416. */
  3417. (void) ourKey;
  3418. (void) verifyCert;
  3419. (void) doCliCertCheck;
  3420. (void) ourDhParam;
  3421. (void) ourCert;
  3422. (void) useX25519;
  3423. (void) useX448;
  3424. #ifdef HAVE_SECURE_RENEGOTIATION
  3425. (void) forceScr;
  3426. #endif
  3427. #if defined(WOLFSSL_CALLBACKS) && defined(WOLFSSL_EARLY_DATA)
  3428. (void) earlyData;
  3429. #endif
  3430. #ifndef WOLFSSL_TIRTOS
  3431. return 0;
  3432. #endif
  3433. }
  3434. #endif /* !NO_WOLFSSL_SERVER */
  3435. /* so overall tests can pull in test function */
  3436. #ifndef NO_MAIN_DRIVER
  3437. int main(int argc, char** argv)
  3438. {
  3439. func_args args;
  3440. tcp_ready ready;
  3441. StartTCP();
  3442. args.argc = argc;
  3443. args.argv = argv;
  3444. args.signal = &ready;
  3445. args.return_code = 0;
  3446. #if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS)
  3447. args.srtp_helper = NULL;
  3448. #endif
  3449. InitTcpReady(&ready);
  3450. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL)
  3451. wolfSSL_Debugging_ON();
  3452. #endif
  3453. wolfSSL_Init();
  3454. #ifdef WC_RNG_SEED_CB
  3455. wc_SetSeed_Cb(wc_GenerateSeed);
  3456. #endif
  3457. ChangeToWolfRoot();
  3458. #ifndef NO_WOLFSSL_SERVER
  3459. #ifdef HAVE_STACK_SIZE
  3460. StackSizeCheck(&args, server_test);
  3461. #else
  3462. server_test(&args);
  3463. #endif
  3464. #else
  3465. fprintf(stderr, "Server not compiled in!\n");
  3466. #endif
  3467. wolfSSL_Cleanup();
  3468. FreeTcpReady(&ready);
  3469. #ifdef HAVE_WNR
  3470. if (wc_FreeNetRandom() < 0)
  3471. err_sys_ex(runWithErrors, "Failed to free netRandom context");
  3472. #endif /* HAVE_WNR */
  3473. return args.return_code;
  3474. }
  3475. int myoptind = 0;
  3476. char* myoptarg = NULL;
  3477. #endif /* NO_MAIN_DRIVER */