server.c 121 KB

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