server.c 134 KB

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