server.c 122 KB

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