server.c 112 KB

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