server.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2020 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/ssl.h> /* name change portability layer */
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef HAVE_ECC
  27. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  28. #endif
  29. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "rl_fs.h"
  33. #include "rl_net.h"
  34. #endif
  35. #include <wolfssl/openssl/ssl.h>
  36. #include <wolfssl/test.h>
  37. #ifdef WOLFSSL_DTLS
  38. #include <wolfssl/error-ssl.h>
  39. #endif
  40. #include "examples/server/server.h"
  41. #ifndef NO_WOLFSSL_SERVER
  42. #ifdef WOLFSSL_ASYNC_CRYPT
  43. static int devId = INVALID_DEVID;
  44. #endif
  45. /* Note on using port 0: if the server uses port 0 to bind an ephemeral port
  46. * number and is using the ready file for scripted testing, the code in
  47. * test.h will write the actual port number into the ready file for use
  48. * by the client. */
  49. static const char webServerMsg[] =
  50. "HTTP/1.1 200 OK\r\n"
  51. "Content-Type: text/html\r\n"
  52. "Connection: close\r\n"
  53. "Content-Length: 141\r\n"
  54. "\r\n"
  55. "<html>\r\n"
  56. "<head>\r\n"
  57. "<title>Welcome to wolfSSL!</title>\r\n"
  58. "</head>\r\n"
  59. "<body>\r\n"
  60. "<p>wolfSSL has successfully performed handshake!</p>\r\n"
  61. "</body>\r\n"
  62. "</html>\r\n";
  63. int runWithErrors = 0; /* Used with -x flag to run err_sys vs. print errors */
  64. int catastrophic = 0; /* Use with -x flag to still exit when an error is
  65. * considered catastrophic EG the servers own cert failing
  66. * to load would be catastrophic since there would be no
  67. * cert to send to clients attempting to connect. The
  68. * server should error out completely in that case
  69. */
  70. static int lng_index = 0;
  71. #ifdef WOLFSSL_CALLBACKS
  72. Timeval srvTo;
  73. static int srvHandShakeCB(HandShakeInfo* info)
  74. {
  75. (void)info;
  76. return 0;
  77. }
  78. static int srvTimeoutCB(TimeoutInfo* info)
  79. {
  80. (void)info;
  81. return 0;
  82. }
  83. #endif
  84. #ifndef NO_HANDSHAKE_DONE_CB
  85. static int myHsDoneCb(WOLFSSL* ssl, void* user_ctx)
  86. {
  87. (void)user_ctx;
  88. (void)ssl;
  89. /* printf("Notified HandShake done\n"); */
  90. /* return negative number to end TLS connection now */
  91. return 0;
  92. }
  93. #endif
  94. static void err_sys_ex(int out, const char* msg)
  95. {
  96. if (out == 1) { /* if server is running w/ -x flag, print error w/o exit */
  97. printf("wolfSSL error: %s\n", msg);
  98. printf("Continuing server execution...\n\n");
  99. } else {
  100. err_sys(msg);
  101. }
  102. }
  103. #ifdef WOLFSSL_DTLS
  104. /* Translates return codes returned from
  105. * send() and recv() if need be.
  106. */
  107. static WC_INLINE int TranslateReturnCode(int old, int sd)
  108. {
  109. (void)sd;
  110. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  111. if (old == 0) {
  112. errno = SOCKET_EWOULDBLOCK;
  113. return -1; /* convert to BSD style wouldblock as error */
  114. }
  115. if (old < 0) {
  116. errno = RTCS_geterror(sd);
  117. if (errno == RTCSERR_TCP_CONN_CLOSING)
  118. return 0; /* convert to BSD style closing */
  119. if (errno == RTCSERR_TCP_CONN_RLSD)
  120. errno = SOCKET_ECONNRESET;
  121. if (errno == RTCSERR_TCP_TIMED_OUT)
  122. errno = SOCKET_EAGAIN;
  123. }
  124. #endif
  125. return old;
  126. }
  127. static WC_INLINE int wolfSSL_LastError(void)
  128. {
  129. #ifdef USE_WINDOWS_API
  130. return WSAGetLastError();
  131. #elif defined(EBSNET)
  132. return xn_getlasterror();
  133. #else
  134. return errno;
  135. #endif
  136. }
  137. /* wolfSSL Sock Addr */
  138. struct WOLFSSL_TEST_SOCKADDR {
  139. unsigned int sz; /* sockaddr size */
  140. SOCKADDR_IN_T sa; /* pointer to the sockaddr_in or sockaddr_in6 */
  141. };
  142. typedef struct WOLFSSL_TEST_DTLS_CTX {
  143. struct WOLFSSL_TEST_SOCKADDR peer;
  144. int rfd;
  145. int wfd;
  146. int failOnce;
  147. word32 blockSeq;
  148. } WOLFSSL_TEST_DTLS_CTX;
  149. static WC_INLINE int PeekSeq(const char* buf, word32* seq)
  150. {
  151. const char* c = buf + 3;
  152. if ((c[0] | c[1] | c[2] | c[3]) == 0) {
  153. *seq = (c[4] << 24) | (c[5] << 16) | (c[6] << 8) | c[7];
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. /* The send embedded callback
  159. * return : nb bytes sent, or error
  160. */
  161. static int TestEmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
  162. {
  163. WOLFSSL_TEST_DTLS_CTX* dtlsCtx = (WOLFSSL_TEST_DTLS_CTX*)ctx;
  164. int sd = dtlsCtx->wfd;
  165. int sent;
  166. int len = sz;
  167. int err;
  168. (void)ssl;
  169. WOLFSSL_ENTER("TestEmbedSendTo()");
  170. if (dtlsCtx->failOnce) {
  171. word32 seq = 0;
  172. if (PeekSeq(buf, &seq) && seq == dtlsCtx->blockSeq) {
  173. dtlsCtx->failOnce = 0;
  174. WOLFSSL_MSG("Forcing WANT_WRITE");
  175. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  176. }
  177. }
  178. sent = (int)sendto(sd, &buf[sz - len], len, 0,
  179. (const SOCKADDR*)&dtlsCtx->peer.sa,
  180. dtlsCtx->peer.sz);
  181. sent = TranslateReturnCode(sent, sd);
  182. if (sent < 0) {
  183. err = wolfSSL_LastError();
  184. WOLFSSL_MSG("Embed Send To error");
  185. if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
  186. WOLFSSL_MSG("\tWould Block");
  187. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  188. }
  189. else if (err == SOCKET_ECONNRESET) {
  190. WOLFSSL_MSG("\tConnection reset");
  191. return WOLFSSL_CBIO_ERR_CONN_RST;
  192. }
  193. else if (err == SOCKET_EINTR) {
  194. WOLFSSL_MSG("\tSocket interrupted");
  195. return WOLFSSL_CBIO_ERR_ISR;
  196. }
  197. else if (err == SOCKET_EPIPE) {
  198. WOLFSSL_MSG("\tSocket EPIPE");
  199. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  200. }
  201. else {
  202. WOLFSSL_MSG("\tGeneral error");
  203. return WOLFSSL_CBIO_ERR_GENERAL;
  204. }
  205. }
  206. return sent;
  207. }
  208. #endif /* WOLFSSL_DTLS */
  209. static int NonBlockingSSL_Accept(SSL* ssl)
  210. {
  211. #ifndef WOLFSSL_CALLBACKS
  212. int ret = SSL_accept(ssl);
  213. #else
  214. int ret = wolfSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
  215. #endif
  216. int error = SSL_get_error(ssl, 0);
  217. SOCKET_T sockfd = (SOCKET_T)SSL_get_fd(ssl);
  218. int select_ret = 0;
  219. while (ret != WOLFSSL_SUCCESS &&
  220. (error == WOLFSSL_ERROR_WANT_READ || error == WOLFSSL_ERROR_WANT_WRITE
  221. #ifdef WOLFSSL_ASYNC_CRYPT
  222. || error == WC_PENDING_E
  223. #endif
  224. )) {
  225. int currTimeout = 1;
  226. if (error == WOLFSSL_ERROR_WANT_READ) {
  227. /* printf("... server would read block\n"); */
  228. }
  229. else if (error == WOLFSSL_ERROR_WANT_WRITE) {
  230. /* printf("... server would write block\n"); */
  231. }
  232. #ifdef WOLFSSL_ASYNC_CRYPT
  233. if (error == WC_PENDING_E) {
  234. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  235. if (ret < 0) break;
  236. }
  237. else
  238. #endif
  239. {
  240. if (error != WOLFSSL_ERROR_WANT_WRITE) {
  241. #ifdef WOLFSSL_DTLS
  242. currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
  243. #endif
  244. select_ret = tcp_select(sockfd, currTimeout);
  245. }
  246. }
  247. if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_SEND_READY)
  248. || (select_ret == TEST_ERROR_READY)
  249. #ifdef WOLFSSL_ASYNC_CRYPT
  250. || error == WC_PENDING_E
  251. #endif
  252. ) {
  253. #ifndef WOLFSSL_CALLBACKS
  254. ret = SSL_accept(ssl);
  255. #else
  256. ret = wolfSSL_accept_ex(ssl,
  257. srvHandShakeCB, srvTimeoutCB, srvTo);
  258. #endif
  259. error = SSL_get_error(ssl, 0);
  260. if (error == WOLFSSL_ERROR_WANT_WRITE) {
  261. /* Do a select here. */
  262. select_ret = tcp_select_tx(sockfd, 1);
  263. if (select_ret == TEST_TIMEOUT)
  264. error = WOLFSSL_FATAL_ERROR;
  265. }
  266. }
  267. else if (select_ret == TEST_TIMEOUT && !wolfSSL_dtls(ssl)) {
  268. error = WOLFSSL_ERROR_WANT_READ;
  269. }
  270. #ifdef WOLFSSL_DTLS
  271. else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl) &&
  272. wolfSSL_dtls_got_timeout(ssl) >= 0) {
  273. error = WOLFSSL_ERROR_WANT_READ;
  274. }
  275. #endif
  276. else {
  277. error = WOLFSSL_FATAL_ERROR;
  278. }
  279. }
  280. return ret;
  281. }
  282. /* Echo number of bytes specified by -e arg */
  283. int ServerEchoData(SSL* ssl, int clientfd, int echoData, int block,
  284. size_t throughput)
  285. {
  286. int ret = 0, err;
  287. double start = 0, rx_time = 0, tx_time = 0;
  288. int select_ret, len, rx_pos;
  289. size_t xfer_bytes = 0;
  290. char* buffer;
  291. buffer = (char*)malloc(block);
  292. if (!buffer) {
  293. err_sys_ex(runWithErrors, "Server buffer malloc failed");
  294. }
  295. while ((echoData && throughput == 0) ||
  296. (!echoData && xfer_bytes < throughput))
  297. {
  298. select_ret = tcp_select(clientfd, 1); /* Timeout=1 second */
  299. if (select_ret == TEST_RECV_READY) {
  300. len = min(block, (int)(throughput - xfer_bytes));
  301. rx_pos = 0;
  302. if (throughput) {
  303. start = current_time(1);
  304. }
  305. /* Read data */
  306. while (rx_pos < len) {
  307. ret = SSL_read(ssl, &buffer[rx_pos], len - rx_pos);
  308. if (ret < 0) {
  309. err = SSL_get_error(ssl, 0);
  310. #ifdef WOLFSSL_ASYNC_CRYPT
  311. if (err == WC_PENDING_E) {
  312. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  313. if (ret < 0) break;
  314. }
  315. else
  316. #endif
  317. if (err != WOLFSSL_ERROR_WANT_READ &&
  318. err != WOLFSSL_ERROR_ZERO_RETURN) {
  319. printf("SSL_read echo error %d\n", err);
  320. err_sys_ex(runWithErrors, "SSL_read failed");
  321. break;
  322. }
  323. }
  324. else {
  325. rx_pos += ret;
  326. }
  327. }
  328. if (throughput) {
  329. rx_time += current_time(0) - start;
  330. start = current_time(1);
  331. }
  332. /* Write data */
  333. do {
  334. err = 0; /* reset error */
  335. ret = SSL_write(ssl, buffer, len);
  336. if (ret <= 0) {
  337. err = SSL_get_error(ssl, 0);
  338. #ifdef WOLFSSL_ASYNC_CRYPT
  339. if (err == WC_PENDING_E) {
  340. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  341. if (ret < 0) break;
  342. }
  343. #endif
  344. }
  345. } while (err == WC_PENDING_E);
  346. if (ret != len) {
  347. printf("SSL_write echo error %d\n", err);
  348. err_sys_ex(runWithErrors, "SSL_write failed");
  349. }
  350. if (throughput) {
  351. tx_time += current_time(0) - start;
  352. }
  353. xfer_bytes += len;
  354. }
  355. }
  356. free(buffer);
  357. if (throughput) {
  358. #if !defined(__MINGW32__)
  359. printf("wolfSSL Server Benchmark %zu bytes\n"
  360. #else
  361. printf("wolfSSL Server Benchmark %d bytes\n"
  362. #endif
  363. "\tRX %8.3f ms (%8.3f MBps)\n"
  364. "\tTX %8.3f ms (%8.3f MBps)\n",
  365. throughput,
  366. tx_time * 1000, throughput / tx_time / 1024 / 1024,
  367. rx_time * 1000, throughput / rx_time / 1024 / 1024
  368. );
  369. }
  370. return EXIT_SUCCESS;
  371. }
  372. static void ServerRead(WOLFSSL* ssl, char* input, int inputLen)
  373. {
  374. int ret, err;
  375. char buffer[WOLFSSL_MAX_ERROR_SZ];
  376. /* Read data */
  377. do {
  378. err = 0; /* reset error */
  379. ret = SSL_read(ssl, input, inputLen);
  380. if (ret < 0) {
  381. err = SSL_get_error(ssl, 0);
  382. #ifdef WOLFSSL_ASYNC_CRYPT
  383. if (err == WC_PENDING_E) {
  384. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  385. if (ret < 0) break;
  386. }
  387. else
  388. #endif
  389. #ifdef WOLFSSL_DTLS
  390. if (wolfSSL_dtls(ssl) && err == DECRYPT_ERROR) {
  391. printf("Dropped client's message due to a bad MAC\n");
  392. }
  393. else
  394. #endif
  395. if (err != WOLFSSL_ERROR_WANT_READ) {
  396. printf("SSL_read input error %d, %s\n", err,
  397. ERR_error_string(err, buffer));
  398. err_sys_ex(runWithErrors, "SSL_read failed");
  399. }
  400. }
  401. else if (SSL_get_error(ssl, 0) == 0 &&
  402. tcp_select(SSL_get_fd(ssl), 0) == TEST_RECV_READY) {
  403. err = WOLFSSL_ERROR_WANT_READ;
  404. }
  405. } while (err == WC_PENDING_E || err == WOLFSSL_ERROR_WANT_READ);
  406. if (ret > 0) {
  407. input[ret] = 0; /* null terminate message */
  408. printf("Client message: %s\n", input);
  409. }
  410. }
  411. static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen)
  412. {
  413. int ret, err;
  414. char buffer[WOLFSSL_MAX_ERROR_SZ];
  415. int len;
  416. #ifdef OPENSSL_ALL
  417. /* Fuzz testing expects reply split over two msgs when TLSv1.0 or below */
  418. if (wolfSSL_GetVersion(ssl) <= WOLFSSL_TLSV1)
  419. len = outputLen / 2;
  420. else
  421. #endif
  422. len = outputLen;
  423. do {
  424. err = 0; /* reset error */
  425. ret = SSL_write(ssl, output, len);
  426. if (ret <= 0) {
  427. err = SSL_get_error(ssl, 0);
  428. #ifdef WOLFSSL_ASYNC_CRYPT
  429. if (err == WC_PENDING_E) {
  430. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  431. if (ret < 0) break;
  432. }
  433. #endif
  434. }
  435. else if (ret != outputLen) {
  436. output += ret;
  437. len = (outputLen -= ret);
  438. err = WOLFSSL_ERROR_WANT_WRITE;
  439. }
  440. } while (err == WC_PENDING_E || err == WOLFSSL_ERROR_WANT_WRITE);
  441. if (ret != outputLen) {
  442. printf("SSL_write msg error %d, %s\n", err,
  443. ERR_error_string(err, buffer));
  444. err_sys_ex(runWithErrors, "SSL_write failed");
  445. }
  446. }
  447. /* when adding new option, please follow the steps below: */
  448. /* 1. add new option message in English section */
  449. /* 2. increase the number of the second column */
  450. /* 3. add the same message into Japanese section */
  451. /* (will be translated later) */
  452. /* 4. add printf() into suitable position of Usage() */
  453. static const char* server_usage_msg[][49] = {
  454. /* English */
  455. {
  456. " NOTE: All files relative to wolfSSL home dir\n", /* 0 */
  457. "-? <num> Help, print this usage\n"
  458. " 0: English, 1: Japanese\n", /* 1 */
  459. "-p <num> Port to listen on, not 0, default", /* 2 */
  460. #ifndef WOLFSSL_TLS13
  461. "-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default", /* 3 */
  462. #else
  463. "-v <num> SSL version [0-4], SSLv3(0) - TLS1.3(4)), default", /* 3 */
  464. #endif
  465. "-l <str> Cipher suite list (: delimited)\n", /* 4 */
  466. "-c <file> Certificate file, default", /* 5 */
  467. "-k <file> Key file, default", /* 6 */
  468. "-A <file> Certificate Authority file, default", /* 7 */
  469. "-R <file> Create Ready file for external monitor"
  470. " default none\n", /* 8 */
  471. #ifndef NO_DH
  472. "-D <file> Diffie-Hellman Params file, default", /* 9 */
  473. "-Z <num> Minimum DH key bits, default", /* 10 */
  474. #endif
  475. #ifdef HAVE_ALPN
  476. "-L <str> Application-Layer Protocol Negotiation"
  477. " ({C,F}:<list>)\n", /* 11 */
  478. #endif
  479. "-d Disable client cert check\n", /* 12 */
  480. "-b Bind to any interface instead of localhost only\n",/* 13 */
  481. "-s Use pre Shared keys\n", /* 14 */
  482. "-u Use UDP DTLS,"
  483. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 15 */
  484. #ifdef WOLFSSL_SCTP
  485. "-G Use SCTP DTLS,"
  486. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 16 */
  487. #endif
  488. "-f Fewer packets/group messages\n", /* 17 */
  489. "-r Allow one client Resumption\n", /* 18 */
  490. "-N Use Non-blocking sockets\n", /* 19 */
  491. "-S <str> Use Host Name Indication\n", /* 20 */
  492. "-w Wait for bidirectional shutdown\n", /* 21 */
  493. #ifdef HAVE_OCSP
  494. "-o Perform OCSP lookup on peer certificate\n", /* 22 */
  495. "-O <url> Perform OCSP lookup using <url> as responder\n", /* 23 */
  496. #endif
  497. #ifdef HAVE_PK_CALLBACKS
  498. "-P Public Key Callbacks\n", /* 24 */
  499. #endif
  500. #ifdef HAVE_ANON
  501. "-a Anonymous server\n", /* 25 */
  502. #endif
  503. #ifndef NO_PSK
  504. "-I Do not send PSK identity hint\n", /* 26 */
  505. #endif
  506. "-x Print server errors but do not close connection\n",/* 27 */
  507. "-i Loop indefinitely (allow repeated connections)\n", /* 28 */
  508. "-e Echo data mode (return raw bytes received)\n", /* 29 */
  509. #ifdef HAVE_NTRU
  510. "-n Use NTRU key (needed for NTRU suites)\n", /* 30 */
  511. #endif
  512. "-B <num> Benchmark throughput"
  513. " using <num> bytes and print stats\n", /* 31 */
  514. #ifdef HAVE_CRL
  515. "-V Disable CRL\n", /* 32 */
  516. #endif
  517. #ifdef WOLFSSL_TRUST_PEER_CERT
  518. "-E <file> Path to load trusted peer cert\n", /* 33 */
  519. #endif
  520. #ifdef HAVE_WNR
  521. "-q <file> Whitewood config file, default", /* 34 */
  522. #endif
  523. "-g Return basic HTML web page\n", /* 35 */
  524. "-C <num> The number of connections to accept, default: 1\n",/* 36 */
  525. "-H <arg> Internal tests"
  526. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  527. " loadSSL, disallowETM]\n", /* 38 */
  528. #ifdef WOLFSSL_TLS13
  529. "-U Update keys and IVs before sending\n", /* 39 */
  530. "-K Key Exchange for PSK not using (EC)DHE\n", /* 40 */
  531. #ifndef NO_DH
  532. "-y Pre-generate Key Share using FFDHE_2048 only\n", /* 41 */
  533. #endif
  534. #ifdef HAVE_ECC
  535. "-Y Pre-generate Key Share using P-256 only \n", /* 42 */
  536. #endif
  537. #ifdef HAVE_CURVE25519
  538. "-t Pre-generate Key share using Curve25519 only\n", /* 43 */
  539. #endif
  540. #ifdef HAVE_SESSION_TICKET
  541. "-T Do not generate session ticket\n", /* 44 */
  542. #endif
  543. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  544. "-Q Request certificate from client post-handshake\n", /* 45 */
  545. #endif
  546. #ifdef WOLFSSL_SEND_HRR_COOKIE
  547. "-J Server sends Cookie Extension containing state\n", /* 46 */
  548. #endif
  549. #endif /* WOLFSSL_TLS13 */
  550. #ifdef WOLFSSL_EARLY_DATA
  551. "-0 Early data read from client (0-RTT handshake)\n", /* 47 */
  552. #endif
  553. #ifdef WOLFSSL_MULTICAST
  554. "-3 <grpid> Multicast, grpid < 256\n", /* 48 */
  555. #endif
  556. "-1 <num> Display a result by specified language."
  557. "\n 0: English, 1: Japanese\n", /* 49 */
  558. #ifdef HAVE_TRUSTED_CA
  559. "-5 Use Trusted CA Key Indication\n", /* 52 */
  560. #endif
  561. NULL,
  562. },
  563. #ifndef NO_MULTIBYTE_PRINT
  564. /* Japanese */
  565. {
  566. " 注意 : 全てのファイルは"
  567. " wolfSSL ホーム・ディレクトリからの相対です。\n", /* 0 */
  568. "-? <num> ヘルプ, 使い方を表示\n"
  569. " 0: 英語、 1: 日本語\n", /* 1 */
  570. "-p <num> 接続先ポート, 0は無効, 既定値", /* 2 */
  571. #ifndef WOLFSSL_TLS13
  572. "-v <num> SSL バージョン [0-3], SSLv3(0) - TLS1.2(3)),"
  573. " 既定値", /* 3 */
  574. #else
  575. "-v <num> SSL バージョン [0-4], SSLv3(0) - TLS1.3(4)),"
  576. " 既定値", /* 3 */
  577. #endif
  578. "-l <str> 暗号スイートリスト (区切り文字 :)\n", /* 4 */
  579. "-c <file> 証明書ファイル, 既定値", /* 5 */
  580. "-k <file> 鍵ファイル, 既定値", /* 6 */
  581. "-A <file> 認証局ファイル, 既定値", /* 7 */
  582. "-R <file> 外部モニタ用の準備完了ファイルを作成する。"
  583. "既定値 なし\n", /* 8 */
  584. #ifndef NO_DH
  585. "-D <file> ディフィー・ヘルマンのパラメータファイル,"
  586. " 既定値", /* 9 */
  587. "-Z <num> 最小 DH 鍵 ビット, 既定値", /* 10 */
  588. #endif
  589. #ifdef HAVE_ALPN
  590. "-L <str> アプリケーション層プロトコルネゴシエーションを行う"
  591. " ({C,F}:<list>)\n", /* 11 */
  592. #endif
  593. "-d クライアント認証を無効とする\n", /* 12 */
  594. "-b ローカルホスト以外のインターフェースへも"
  595. "バインドする\n", /* 13 */
  596. "-s 事前共有鍵を使用する\n", /* 14 */
  597. "-u UDP DTLSを使用する。-v 2 を追加指定すると"
  598. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 15 */
  599. #ifdef WOLFSSL_SCTP
  600. "-G SCTP DTLSを使用する。-v 2 を追加指定すると"
  601. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 16 */
  602. #endif
  603. "-f より少ないパケット/グループメッセージを使用する\n",/* 17 */
  604. "-r クライアントの再開を許可する\n", /* 18 */
  605. "-N ノンブロッキング・ソケットを使用する\n", /* 19 */
  606. "-S <str> ホスト名表示を使用する\n", /* 20 */
  607. "-w 双方向シャットダウンを待つ\n", /* 21 */
  608. #ifdef HAVE_OCSP
  609. "-o OCSPルックアップをピア証明書で実施する\n", /* 22 */
  610. "-O <url> OCSPルックアップを、"
  611. "<url>を使用し応答者として実施する\n", /* 23 */
  612. #endif
  613. #ifdef HAVE_PK_CALLBACKS
  614. "-P 公開鍵コールバック\n", /* 24 */
  615. #endif
  616. #ifdef HAVE_ANON
  617. "-a 匿名サーバー\n", /* 25 */
  618. #endif
  619. #ifndef NO_PSK
  620. "-I PSKアイデンティティのヒントを送信しない\n", /* 26 */
  621. #endif
  622. "-x サーバーエラーを出力するが接続を切断しない\n", /* 27 */
  623. "-i 無期限にループする(繰り返し接続を許可)\n", /* 28 */
  624. "-e エコー・データモード"
  625. "(受け取ったバイトデータを返す)\n", /* 29 */
  626. #ifdef HAVE_NTRU
  627. "-n NTRU鍵を使用する(NTRUスイートに必要)\n", /* 30 */
  628. #endif
  629. "-B <num> <num> バイトを用いてのベンチマーク・スループット"
  630. "測定と結果を出力する\n", /* 31 */
  631. #ifdef HAVE_CRL
  632. "-V CRLを無効とする\n", /* 32 */
  633. #endif
  634. #ifdef WOLFSSL_TRUST_PEER_CERT
  635. "-E <file> 信頼出来るピアの証明書ロードの為のパス\n\n", /* 33 */
  636. #endif
  637. #ifdef HAVE_WNR
  638. "-q <file> Whitewood コンフィグファイル, 既定値", /* 34 */
  639. #endif
  640. "-g 基本的な Web ページを返す\n", /* 35 */
  641. "-C <num> アクセプト可能な接続数を指定する。既定値: 1\n", /* 36 */
  642. "-H <arg> 内部テスト"
  643. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  644. " loadSSL, disallowETM]\n", /* 38 */
  645. #ifdef WOLFSSL_TLS13
  646. "-U データ送信前に、鍵とIVを更新する\n", /* 39 */
  647. "-K 鍵交換にPSKを使用、(EC)DHEは使用しない\n", /* 40 */
  648. #ifndef NO_DH
  649. "-y FFDHE_2048のみを使用して鍵共有を事前生成する\n", /* 41 */
  650. #endif
  651. #ifdef HAVE_ECC
  652. "-Y P-256のみを使用したキー共有の事前生成\n", /* 42 */
  653. #endif
  654. #ifdef HAVE_CURVE25519
  655. "-t Curve25519のみを使用して鍵共有を事前生成する\n", /* 43 */
  656. #endif
  657. #ifdef HAVE_SESSION_TICKET
  658. "-T セッションチケットを生成しない\n", /* 44 */
  659. #endif
  660. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  661. "-Q クライアントのポストハンドシェイクから"
  662. "証明書を要求する\n", /* 45 */
  663. #endif
  664. #ifdef WOLFSSL_SEND_HRR_COOKIE
  665. "-J サーバーの状態を含むTLS Cookie 拡張を送信する\n", /* 46 */
  666. #endif
  667. #endif /* WOLFSSL_TLS13 */
  668. #ifdef WOLFSSL_EARLY_DATA
  669. "-0 クライアントからの Early Data 読み取り"
  670. "(0-RTTハンドシェイク)\n", /* 47 */
  671. #endif
  672. #ifdef WOLFSSL_MULTICAST
  673. "-3 <grpid> マルチキャスト, grpid < 256\n", /* 48 */
  674. #endif
  675. "-1 <num> 指定された言語で結果を表示します。"
  676. "\n 0: 英語、 1: 日本語\n", /* 49 */
  677. #ifdef HAVE_TRUSTED_CA
  678. "-5 信頼できる認証局の鍵表示を使用する\n", /* 52 */
  679. #endif
  680. NULL,
  681. },
  682. #endif
  683. };
  684. static void Usage(void)
  685. {
  686. int msgId = 0;
  687. const char** msg = server_usage_msg[lng_index];
  688. printf("%s%s%s", "server ", LIBWOLFSSL_VERSION_STRING,
  689. msg[msgId]);
  690. printf("%s", msg[++msgId]); /* ? */
  691. printf("%s %d\n", msg[++msgId], wolfSSLPort); /* -p */
  692. #ifndef WOLFSSL_TLS13
  693. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  694. #else
  695. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  696. #endif
  697. printf("%s", msg[++msgId]); /* -l */
  698. printf("%s %s\n", msg[++msgId], svrCertFile); /* -c */
  699. printf("%s %s\n", msg[++msgId], svrKeyFile); /* -k */
  700. printf("%s %s\n", msg[++msgId], cliCertFile); /* -A */
  701. printf("%s", msg[++msgId]); /* -R */
  702. #ifndef NO_DH
  703. printf("%s %s\n", msg[++msgId], dhParamFile); /* -D */
  704. printf("%s %d\n", msg[++msgId], DEFAULT_MIN_DHKEY_BITS);/* -Z */
  705. #endif
  706. #ifdef HAVE_ALPN
  707. printf("%s", msg[++msgId]); /* -L */
  708. #endif
  709. printf("%s", msg[++msgId]); /* -d */
  710. printf("%s", msg[++msgId]); /* -b */
  711. printf("%s", msg[++msgId]); /* -s */
  712. printf("%s", msg[++msgId]); /* -u */
  713. #ifdef WOLFSSL_SCTP
  714. printf("%s", msg[++msgId]); /* -G */
  715. #endif
  716. printf("%s", msg[++msgId]); /* -f */
  717. printf("%s", msg[++msgId]); /* -r */
  718. printf("%s", msg[++msgId]); /* -N */
  719. printf("%s", msg[++msgId]); /* -S */
  720. printf("%s", msg[++msgId]); /* -w */
  721. #ifdef HAVE_SECURE_RENEGOTIATION
  722. printf("-M Allow Secure Renegotiation\n");
  723. printf("-m Force Server Initiated Secure Renegotiation\n");
  724. #endif /* HAVE_SECURE_RENEGOTIATION */
  725. #ifdef HAVE_OCSP
  726. printf("%s", msg[++msgId]); /* -o */
  727. printf("%s", msg[++msgId]); /* -O */
  728. #endif
  729. #ifdef HAVE_PK_CALLBACKS
  730. printf("%s", msg[++msgId]); /* -P */
  731. #endif
  732. #ifdef HAVE_ANON
  733. printf("%s", msg[++msgId]); /* -a */
  734. #endif
  735. #ifndef NO_PSK
  736. printf("%s", msg[++msgId]); /* -I */
  737. #endif
  738. printf("%s", msg[++msgId]); /* -x */
  739. printf("%s", msg[++msgId]); /* -i */
  740. printf("%s", msg[++msgId]); /* -e */
  741. #ifdef HAVE_NTRU
  742. printf("%s", msg[++msgId]); /* -n */
  743. #endif
  744. printf("%s", msg[++msgId]); /* -B */
  745. #ifdef HAVE_CRL
  746. printf("%s", msg[++msgId]); /* -V */
  747. #endif
  748. #ifdef WOLFSSL_TRUST_PEER_CERT
  749. printf("%s", msg[++msgId]); /* -E */
  750. #endif
  751. #ifdef HAVE_WNR
  752. printf("%s %s\n", msg[++msgId], wnrConfig); /* -q */
  753. #endif
  754. printf("%s", msg[++msgId]); /* -g */
  755. printf("%s", msg[++msgId]); /* -C */
  756. printf("%s", msg[++msgId]); /* -H */
  757. printf("%s", msg[++msgId]); /* more -H options */
  758. #ifdef WOLFSSL_TLS13
  759. printf("%s", msg[++msgId]); /* -U */
  760. printf("%s", msg[++msgId]); /* -K */
  761. #ifndef NO_DH
  762. printf("%s", msg[++msgId]); /* -y */
  763. #endif
  764. #ifdef HAVE_ECC
  765. printf("%s", msg[++msgId]); /* -Y */
  766. #endif
  767. #ifdef HAVE_CURVE25519
  768. printf("%s", msg[++msgId]); /* -t */
  769. #endif
  770. #ifdef HAVE_SESSION_TICKET
  771. printf("%s", msg[++msgId]); /* -T */
  772. #endif
  773. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  774. printf("%s", msg[++msgId]); /* -Q */
  775. #endif
  776. #ifdef WOLFSSL_SEND_HRR_COOKIE
  777. printf("%s", msg[++msgId]); /* -J */
  778. #endif
  779. #endif /* WOLFSSL_TLS13 */
  780. #ifdef WOLFSSL_EARLY_DATA
  781. printf("%s", msg[++msgId]); /* -0 */
  782. #endif
  783. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  784. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  785. printf("-2 Disable DH Prime check\n");
  786. #endif
  787. #ifdef WOLFSSL_DTLS
  788. printf("-4 <seq> DTLS fake would-block for message seq\n");
  789. #endif
  790. #ifdef WOLFSSL_MULTICAST
  791. printf("%s", msg[++msgId]); /* -3 */
  792. #endif
  793. printf("%s", msg[++msgId]); /* -1 */
  794. #ifdef HAVE_TRUSTED_CA
  795. printf("%s", msg[++msgId]); /* -5 */
  796. #endif /* HAVE_TRUSTED_CA */
  797. }
  798. THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
  799. {
  800. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  801. SOCKET_T clientfd = WOLFSSL_SOCKET_INVALID;
  802. wolfSSL_method_func method = NULL;
  803. SSL_CTX* ctx = 0;
  804. SSL* ssl = 0;
  805. #ifndef WOLFSSL_ALT_TEST_STRINGS
  806. const char msg[] = "I hear you fa shizzle!";
  807. #else
  808. const char msg[] = "I hear you fa shizzle!\n";
  809. #endif
  810. int useWebServerMsg = 0;
  811. char input[80];
  812. int ch;
  813. int version = SERVER_DEFAULT_VERSION;
  814. #ifndef WOLFSSL_NO_CLIENT_AUTH
  815. int doCliCertCheck = 1;
  816. #else
  817. int doCliCertCheck = 0;
  818. #endif
  819. #ifdef HAVE_CRL
  820. int disableCRL = 0;
  821. #endif
  822. int useAnyAddr = 0;
  823. word16 port = wolfSSLPort;
  824. int usePsk = 0;
  825. int usePskPlus = 0;
  826. int useAnon = 0;
  827. int doDTLS = 0;
  828. int dtlsUDP = 0;
  829. int dtlsSCTP = 0;
  830. int doMcast = 0;
  831. #ifdef WOLFSSL_DTLS
  832. int doBlockSeq = 0;
  833. WOLFSSL_TEST_DTLS_CTX dtlsCtx;
  834. #endif
  835. int needDH = 0;
  836. int useNtruKey = 0;
  837. int nonBlocking = 0;
  838. int fewerPackets = 0;
  839. #ifdef HAVE_PK_CALLBACKS
  840. int pkCallbacks = 0;
  841. PkCbInfo pkCbInfo;
  842. #endif
  843. int wc_shutdown = 0;
  844. int resume = 0;
  845. int resumeCount = 0;
  846. int loops = 1;
  847. int cnt = 0;
  848. int echoData = 0;
  849. int block = TEST_BUFFER_SIZE;
  850. size_t throughput = 0;
  851. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  852. short minRsaKeyBits = DEFAULT_MIN_RSAKEY_BITS;
  853. short minEccKeyBits = DEFAULT_MIN_ECCKEY_BITS;
  854. int doListen = 1;
  855. int crlFlags = 0;
  856. int ret;
  857. int err = 0;
  858. char* serverReadyFile = NULL;
  859. char* alpnList = NULL;
  860. unsigned char alpn_opt = 0;
  861. char* cipherList = NULL;
  862. int useDefCipherList = 0;
  863. int overrideDateErrors = 0;
  864. const char* verifyCert;
  865. const char* ourCert;
  866. const char* ourKey;
  867. const char* ourDhParam = dhParamFile;
  868. tcp_ready* readySignal = NULL;
  869. int argc = ((func_args*)args)->argc;
  870. char** argv = ((func_args*)args)->argv;
  871. #ifdef WOLFSSL_TRUST_PEER_CERT
  872. const char* trustCert = NULL;
  873. #endif
  874. #ifndef NO_PSK
  875. int sendPskIdentityHint = 1;
  876. #endif
  877. #ifdef HAVE_SNI
  878. char* sniHostName = NULL;
  879. #endif
  880. #ifdef HAVE_TRUSTED_CA
  881. int trustedCaKeyId = 0;
  882. #endif /* HAVE_TRUSTED_CA */
  883. #ifdef HAVE_OCSP
  884. int useOcsp = 0;
  885. char* ocspUrl = NULL;
  886. #endif
  887. #ifdef HAVE_WNR
  888. const char* wnrConfigFile = wnrConfig;
  889. #endif
  890. char buffer[WOLFSSL_MAX_ERROR_SZ];
  891. #ifdef WOLFSSL_TLS13
  892. int noPskDheKe = 0;
  893. #endif
  894. int updateKeysIVs = 0;
  895. int postHandAuth = 0;
  896. #ifdef WOLFSSL_EARLY_DATA
  897. int earlyData = 0;
  898. #endif
  899. #ifdef HAVE_SECURE_RENEGOTIATION
  900. int scr = 0;
  901. int forceScr = 0;
  902. #endif /* HAVE_SECURE_RENEGOTIATION */
  903. #ifdef WOLFSSL_SEND_HRR_COOKIE
  904. int hrrCookie = 0;
  905. #endif
  906. byte mcastID = 0;
  907. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  908. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  909. int doDhKeyCheck = 1;
  910. #endif
  911. #ifdef WOLFSSL_STATIC_MEMORY
  912. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  913. || defined(SESSION_CERTS)
  914. /* big enough to handle most cases including session certs */
  915. byte memory[204000];
  916. #else
  917. byte memory[80000];
  918. #endif
  919. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  920. WOLFSSL_MEM_CONN_STATS ssl_stats;
  921. #ifdef DEBUG_WOLFSSL
  922. WOLFSSL_MEM_STATS mem_stats;
  923. #endif
  924. #endif
  925. #ifdef WOLFSSL_TLS13
  926. int onlyKeyShare = 0;
  927. int noTicket = 0;
  928. #endif
  929. int useX25519 = 0;
  930. int exitWithRet = 0;
  931. int loadCertKeyIntoSSLObj = 0;
  932. #ifdef HAVE_ENCRYPT_THEN_MAC
  933. int disallowETM = 0;
  934. #endif
  935. ((func_args*)args)->return_code = -1; /* error state */
  936. #ifndef NO_RSA
  937. verifyCert = cliCertFile;
  938. ourCert = svrCertFile;
  939. ourKey = svrKeyFile;
  940. #else
  941. #ifdef HAVE_ECC
  942. verifyCert = cliEccCertFile;
  943. ourCert = eccCertFile;
  944. ourKey = eccKeyFile;
  945. #elif defined(HAVE_ED25519)
  946. verifyCert = cliEdCertFile;
  947. ourCert = edCertFile;
  948. ourKey = edKeyFile;
  949. #else
  950. verifyCert = NULL;
  951. ourCert = NULL;
  952. ourKey = NULL;
  953. #endif
  954. #endif
  955. (void)needDH;
  956. (void)ourKey;
  957. (void)ourCert;
  958. (void)ourDhParam;
  959. (void)verifyCert;
  960. (void)useNtruKey;
  961. (void)doCliCertCheck;
  962. (void)minDhKeyBits;
  963. (void)minRsaKeyBits;
  964. (void)minEccKeyBits;
  965. (void)alpnList;
  966. (void)alpn_opt;
  967. (void)crlFlags;
  968. (void)readySignal;
  969. (void)updateKeysIVs;
  970. (void)postHandAuth;
  971. (void)mcastID;
  972. (void)loadCertKeyIntoSSLObj;
  973. (void)overrideDateErrors;
  974. #ifdef WOLFSSL_TIRTOS
  975. fdOpenSession(Task_self());
  976. #endif
  977. #ifdef WOLFSSL_VXWORKS
  978. useAnyAddr = 1;
  979. #else
  980. /* Not Used: h, z, F, T, V, W, X */
  981. while ((ch = mygetopt(argc, argv, "?:"
  982. "abc:defgijk:l:mnop:q:rstuv:wxy"
  983. "A:B:C:D:E:GH:IJKL:MNO:PQR:S:TUVYZ:"
  984. "01:23:4:5")) != -1) {
  985. switch (ch) {
  986. case '?' :
  987. if(myoptarg!=NULL) {
  988. lng_index = atoi(myoptarg);
  989. if(lng_index<0||lng_index>1){
  990. lng_index = 0;
  991. }
  992. }
  993. Usage();
  994. XEXIT_T(EXIT_SUCCESS);
  995. case 'x' :
  996. runWithErrors = 1;
  997. break;
  998. case 'd' :
  999. doCliCertCheck = 0;
  1000. break;
  1001. case 'V' :
  1002. #ifdef HAVE_CRL
  1003. disableCRL = 1;
  1004. #endif
  1005. break;
  1006. case 'b' :
  1007. useAnyAddr = 1;
  1008. break;
  1009. case 's' :
  1010. usePsk = 1;
  1011. break;
  1012. case 'j' :
  1013. usePskPlus = 1;
  1014. break;
  1015. case 'n' :
  1016. useNtruKey = 1;
  1017. break;
  1018. case 'u' :
  1019. doDTLS = 1;
  1020. dtlsUDP = 1;
  1021. break;
  1022. case 'G' :
  1023. #ifdef WOLFSSL_SCTP
  1024. doDTLS = 1;
  1025. dtlsSCTP = 1;
  1026. #endif
  1027. break;
  1028. case 'f' :
  1029. fewerPackets = 1;
  1030. break;
  1031. case 'R' :
  1032. serverReadyFile = myoptarg;
  1033. break;
  1034. case 'r' :
  1035. #ifndef NO_SESSION_CACHE
  1036. resume = 1;
  1037. #endif
  1038. break;
  1039. case 'P' :
  1040. #ifdef HAVE_PK_CALLBACKS
  1041. pkCallbacks = 1;
  1042. #endif
  1043. break;
  1044. case 'p' :
  1045. port = (word16)atoi(myoptarg);
  1046. break;
  1047. case 'w' :
  1048. wc_shutdown = 1;
  1049. break;
  1050. case 'v' :
  1051. if (myoptarg[0] == 'd') {
  1052. version = SERVER_DOWNGRADE_VERSION;
  1053. break;
  1054. }
  1055. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1056. else if (myoptarg[0] == 'e') {
  1057. version = EITHER_DOWNGRADE_VERSION;
  1058. #ifndef NO_CERTS
  1059. loadCertKeyIntoSSLObj = 1;
  1060. #endif
  1061. break;
  1062. }
  1063. #endif
  1064. version = atoi(myoptarg);
  1065. if (version < 0 || version > 4) {
  1066. Usage();
  1067. XEXIT_T(MY_EX_USAGE);
  1068. }
  1069. break;
  1070. case 'l' :
  1071. cipherList = myoptarg;
  1072. break;
  1073. case 'H' :
  1074. if (XSTRNCMP(myoptarg, "defCipherList", 13) == 0) {
  1075. printf("Using default cipher list for testing\n");
  1076. useDefCipherList = 1;
  1077. }
  1078. else if (XSTRNCMP(myoptarg, "exitWithRet", 11) == 0) {
  1079. printf("Skip exit() for testing\n");
  1080. exitWithRet = 1;
  1081. }
  1082. else if (XSTRNCMP(myoptarg, "verifyFail", 10) == 0) {
  1083. printf("Verify should fail\n");
  1084. myVerifyFail = 1;
  1085. }
  1086. else if (XSTRNCMP(myoptarg, "loadSSL", 7) == 0) {
  1087. printf("Also load cert/key into wolfSSL object\n");
  1088. #ifndef NO_CERTS
  1089. loadCertKeyIntoSSLObj = 2;
  1090. #endif
  1091. }
  1092. else if (XSTRNCMP(myoptarg, "loadSSLOnly", 11) == 0) {
  1093. printf("Only load cert/key into wolfSSL object\n");
  1094. #ifndef NO_CERTS
  1095. loadCertKeyIntoSSLObj = 1;
  1096. #endif
  1097. }
  1098. else if (XSTRNCMP(myoptarg, "disallowETM", 11) == 0) {
  1099. printf("Disallow Encrypt-Then-MAC\n");
  1100. #ifdef HAVE_ENCRYPT_THEN_MAC
  1101. disallowETM = 1;
  1102. #endif
  1103. }
  1104. else if (XSTRNCMP(myoptarg, "overrideDateErr", 15) == 0) {
  1105. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1106. overrideDateErrors = 1;
  1107. #endif
  1108. }
  1109. else {
  1110. Usage();
  1111. XEXIT_T(MY_EX_USAGE);
  1112. }
  1113. break;
  1114. case 'A' :
  1115. verifyCert = myoptarg;
  1116. break;
  1117. case 'c' :
  1118. ourCert = myoptarg;
  1119. break;
  1120. case 'k' :
  1121. ourKey = myoptarg;
  1122. break;
  1123. case 'D' :
  1124. #ifndef NO_DH
  1125. ourDhParam = myoptarg;
  1126. #endif
  1127. break;
  1128. case 'Z' :
  1129. #ifndef NO_DH
  1130. minDhKeyBits = atoi(myoptarg);
  1131. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  1132. Usage();
  1133. XEXIT_T(MY_EX_USAGE);
  1134. }
  1135. #endif
  1136. break;
  1137. case 'N':
  1138. nonBlocking = 1;
  1139. break;
  1140. case 'S' :
  1141. #ifdef HAVE_SNI
  1142. sniHostName = myoptarg;
  1143. #endif
  1144. break;
  1145. case 'o' :
  1146. #ifdef HAVE_OCSP
  1147. useOcsp = 1;
  1148. #endif
  1149. break;
  1150. case 'O' :
  1151. #ifdef HAVE_OCSP
  1152. useOcsp = 1;
  1153. ocspUrl = myoptarg;
  1154. #endif
  1155. break;
  1156. case 'a' :
  1157. #ifdef HAVE_ANON
  1158. useAnon = 1;
  1159. #endif
  1160. break;
  1161. case 'I':
  1162. #ifndef NO_PSK
  1163. sendPskIdentityHint = 0;
  1164. #endif
  1165. break;
  1166. case 'L' :
  1167. #ifdef HAVE_ALPN
  1168. alpnList = myoptarg;
  1169. if (alpnList[0] == 'C' && alpnList[1] == ':')
  1170. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  1171. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  1172. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  1173. else {
  1174. Usage();
  1175. XEXIT_T(MY_EX_USAGE);
  1176. }
  1177. alpnList += 2;
  1178. #endif
  1179. break;
  1180. case 'i' :
  1181. loops = -1;
  1182. break;
  1183. case 'C' :
  1184. loops = atoi(myoptarg);
  1185. if (loops <= 0) {
  1186. Usage();
  1187. XEXIT_T(MY_EX_USAGE);
  1188. }
  1189. break;
  1190. case 'e' :
  1191. echoData = 1;
  1192. break;
  1193. case 'B':
  1194. throughput = atol(myoptarg);
  1195. for (; *myoptarg != '\0'; myoptarg++) {
  1196. if (*myoptarg == ',') {
  1197. block = atoi(myoptarg + 1);
  1198. break;
  1199. }
  1200. }
  1201. if (throughput == 0 || block <= 0) {
  1202. Usage();
  1203. XEXIT_T(MY_EX_USAGE);
  1204. }
  1205. break;
  1206. #ifdef WOLFSSL_TRUST_PEER_CERT
  1207. case 'E' :
  1208. trustCert = myoptarg;
  1209. break;
  1210. #endif
  1211. case 'q' :
  1212. #ifdef HAVE_WNR
  1213. wnrConfigFile = myoptarg;
  1214. #endif
  1215. break;
  1216. case 'g' :
  1217. useWebServerMsg = 1;
  1218. break;
  1219. case 'y' :
  1220. #if defined(WOLFSSL_TLS13) && !defined(NO_DH)
  1221. onlyKeyShare = 1;
  1222. #endif
  1223. break;
  1224. case 'Y' :
  1225. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
  1226. onlyKeyShare = 2;
  1227. #endif
  1228. break;
  1229. case 't' :
  1230. #ifdef HAVE_CURVE25519
  1231. useX25519 = 1;
  1232. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
  1233. onlyKeyShare = 2;
  1234. #endif
  1235. #endif
  1236. break;
  1237. case 'K' :
  1238. #ifdef WOLFSSL_TLS13
  1239. noPskDheKe = 1;
  1240. #endif
  1241. break;
  1242. case 'T' :
  1243. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1244. noTicket = 1;
  1245. #endif
  1246. break;
  1247. case 'U' :
  1248. #ifdef WOLFSSL_TLS13
  1249. updateKeysIVs = 1;
  1250. #endif
  1251. break;
  1252. case 'Q' :
  1253. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1254. postHandAuth = 1;
  1255. doCliCertCheck = 0;
  1256. #endif
  1257. break;
  1258. case 'J' :
  1259. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1260. hrrCookie = 1;
  1261. #endif
  1262. break;
  1263. case 'M' :
  1264. #ifdef HAVE_SECURE_RENEGOTIATION
  1265. scr = 1;
  1266. #endif /* HAVE_SECURE_RENEGOTIATION */
  1267. break;
  1268. case 'm' :
  1269. #ifdef HAVE_SECURE_RENEGOTIATION
  1270. scr = 1;
  1271. forceScr = 1;
  1272. #endif /* HAVE_SECURE_RENEGOTIATION */
  1273. break;
  1274. case '0' :
  1275. #ifdef WOLFSSL_EARLY_DATA
  1276. earlyData = 1;
  1277. #endif
  1278. break;
  1279. case '1' :
  1280. lng_index = atoi(myoptarg);
  1281. if(lng_index<0||lng_index>1){
  1282. lng_index = 0;
  1283. }
  1284. break;
  1285. case '2' :
  1286. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1287. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1288. doDhKeyCheck = 0;
  1289. #endif
  1290. break;
  1291. case '3' :
  1292. #ifdef WOLFSSL_MULTICAST
  1293. doMcast = 1;
  1294. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  1295. #endif
  1296. break;
  1297. case '4' :
  1298. #ifdef WOLFSSL_DTLS
  1299. XMEMSET(&dtlsCtx, 0, sizeof(dtlsCtx));
  1300. doBlockSeq = 1;
  1301. dtlsCtx.blockSeq = atoi(myoptarg);
  1302. #endif
  1303. break;
  1304. case '5' :
  1305. #ifdef HAVE_TRUSTED_CA
  1306. trustedCaKeyId = 1;
  1307. #endif /* HAVE_TRUSTED_CA */
  1308. break;
  1309. default:
  1310. Usage();
  1311. XEXIT_T(MY_EX_USAGE);
  1312. }
  1313. }
  1314. myoptind = 0; /* reset for test cases */
  1315. #endif /* !WOLFSSL_VXWORKS */
  1316. /* Can only use DTLS over UDP or SCTP, can't do both. */
  1317. if (dtlsUDP && dtlsSCTP) {
  1318. err_sys_ex(runWithErrors, "Cannot use DTLS with both UDP and SCTP.");
  1319. }
  1320. /* sort out DTLS versus TLS versions */
  1321. if (version == CLIENT_INVALID_VERSION) {
  1322. if (doDTLS)
  1323. version = CLIENT_DTLS_DEFAULT_VERSION;
  1324. else
  1325. version = CLIENT_DEFAULT_VERSION;
  1326. }
  1327. else {
  1328. if (doDTLS) {
  1329. if (version == 3)
  1330. version = -2;
  1331. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1332. else if (version == EITHER_DOWNGRADE_VERSION)
  1333. version = -3;
  1334. #endif
  1335. else
  1336. version = -1;
  1337. }
  1338. }
  1339. #ifdef HAVE_WNR
  1340. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  1341. err_sys_ex(runWithErrors, "can't load whitewood net random config "
  1342. "file");
  1343. #endif
  1344. switch (version) {
  1345. #ifndef NO_OLD_TLS
  1346. #ifdef WOLFSSL_ALLOW_SSLV3
  1347. case 0:
  1348. method = wolfSSLv3_server_method_ex;
  1349. break;
  1350. #endif
  1351. #ifndef NO_TLS
  1352. #ifdef WOLFSSL_ALLOW_TLSV10
  1353. case 1:
  1354. method = wolfTLSv1_server_method_ex;
  1355. break;
  1356. #endif
  1357. case 2:
  1358. method = wolfTLSv1_1_server_method_ex;
  1359. break;
  1360. #endif /* !NO_TLS */
  1361. #endif /* !NO_OLD_TLS */
  1362. #ifndef NO_TLS
  1363. #ifndef WOLFSSL_NO_TLS12
  1364. case 3:
  1365. method = wolfTLSv1_2_server_method_ex;
  1366. break;
  1367. #endif
  1368. #ifdef WOLFSSL_TLS13
  1369. case 4:
  1370. method = wolfTLSv1_3_server_method_ex;
  1371. break;
  1372. #endif
  1373. case SERVER_DOWNGRADE_VERSION:
  1374. method = wolfSSLv23_server_method_ex;
  1375. break;
  1376. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1377. case EITHER_DOWNGRADE_VERSION:
  1378. method = wolfSSLv23_method_ex;
  1379. break;
  1380. #endif
  1381. #endif /* NO_TLS */
  1382. #ifdef WOLFSSL_DTLS
  1383. #ifndef NO_OLD_TLS
  1384. case -1:
  1385. method = wolfDTLSv1_server_method_ex;
  1386. break;
  1387. #endif
  1388. #ifndef WOLFSSL_NO_TLS12
  1389. case -2:
  1390. method = wolfDTLSv1_2_server_method_ex;
  1391. break;
  1392. #endif
  1393. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1394. case -3:
  1395. method = wolfDTLSv1_2_method_ex;
  1396. break;
  1397. #endif
  1398. #endif
  1399. default:
  1400. err_sys_ex(runWithErrors, "Bad SSL version");
  1401. }
  1402. if (method == NULL)
  1403. err_sys_ex(runWithErrors, "unable to get method");
  1404. #ifdef WOLFSSL_STATIC_MEMORY
  1405. #ifdef DEBUG_WOLFSSL
  1406. /* print off helper buffer sizes for use with static memory
  1407. * printing to stderr in case of debug mode turned on */
  1408. fprintf(stderr, "static memory management size = %d\n",
  1409. wolfSSL_MemoryPaddingSz());
  1410. fprintf(stderr, "calculated optimum general buffer size = %d\n",
  1411. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  1412. fprintf(stderr, "calculated optimum IO buffer size = %d\n",
  1413. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  1414. WOLFMEM_IO_POOL_FIXED));
  1415. #endif /* DEBUG_WOLFSSL */
  1416. if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),0,1)
  1417. != WOLFSSL_SUCCESS)
  1418. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  1419. /* load in a buffer for IO */
  1420. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  1421. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)
  1422. != WOLFSSL_SUCCESS)
  1423. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  1424. #else
  1425. ctx = SSL_CTX_new(method(NULL));
  1426. #endif /* WOLFSSL_STATIC_MEMORY */
  1427. if (ctx == NULL)
  1428. err_sys_ex(catastrophic, "unable to get ctx");
  1429. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  1430. defined(HAVE_POLY1305)
  1431. if (TicketInit() != 0)
  1432. err_sys_ex(catastrophic, "unable to setup Session Ticket Key context");
  1433. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  1434. #endif
  1435. if (cipherList && !useDefCipherList) {
  1436. if (SSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS)
  1437. err_sys_ex(runWithErrors, "server can't set custom cipher list");
  1438. }
  1439. #ifdef WOLFSSL_LEANPSK
  1440. if (!usePsk) {
  1441. usePsk = 1;
  1442. }
  1443. #endif
  1444. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519)
  1445. if (!usePsk) {
  1446. usePsk = 1;
  1447. }
  1448. #endif
  1449. if (fewerPackets)
  1450. wolfSSL_CTX_set_group_messages(ctx);
  1451. #ifdef WOLFSSL_SCTP
  1452. if (dtlsSCTP)
  1453. wolfSSL_CTX_dtls_set_sctp(ctx);
  1454. #endif
  1455. #ifdef WOLFSSL_ENCRYPTED_KEYS
  1456. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1457. #endif
  1458. #if !defined(NO_CERTS)
  1459. if ((!usePsk || usePskPlus) && !useAnon && !(loadCertKeyIntoSSLObj == 1)) {
  1460. #ifndef TEST_LOAD_BUFFER
  1461. if (SSL_CTX_use_certificate_chain_file(ctx, ourCert)
  1462. != WOLFSSL_SUCCESS)
  1463. err_sys_ex(catastrophic, "can't load server cert file, check file "
  1464. "and run from wolfSSL home dir");
  1465. #else
  1466. /* loads cert chain file using buffer API */
  1467. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  1468. #endif
  1469. }
  1470. #endif
  1471. #ifndef NO_DH
  1472. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  1473. != WOLFSSL_SUCCESS) {
  1474. err_sys_ex(runWithErrors, "Error setting minimum DH key size");
  1475. }
  1476. #endif
  1477. #ifndef NO_RSA
  1478. if (wolfSSL_CTX_SetMinRsaKey_Sz(ctx, minRsaKeyBits) != WOLFSSL_SUCCESS){
  1479. err_sys_ex(runWithErrors, "Error setting minimum RSA key size");
  1480. }
  1481. #endif
  1482. #ifdef HAVE_ECC
  1483. if (wolfSSL_CTX_SetMinEccKey_Sz(ctx, minEccKeyBits) != WOLFSSL_SUCCESS){
  1484. err_sys_ex(runWithErrors, "Error setting minimum ECC key size");
  1485. }
  1486. #endif
  1487. #ifdef HAVE_NTRU
  1488. if (useNtruKey) {
  1489. if (wolfSSL_CTX_use_NTRUPrivateKey_file(ctx, ourKey)
  1490. != WOLFSSL_SUCCESS)
  1491. err_sys_ex(catastrophic, "can't load ntru key file, "
  1492. "Please run from wolfSSL home dir");
  1493. }
  1494. #endif
  1495. #if !defined(NO_CERTS)
  1496. #ifdef HAVE_PK_CALLBACKS
  1497. pkCbInfo.ourKey = ourKey;
  1498. #endif
  1499. if (!useNtruKey && (!usePsk || usePskPlus) && !useAnon
  1500. && !(loadCertKeyIntoSSLObj == 1)
  1501. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  1502. && !pkCallbacks
  1503. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  1504. ) {
  1505. #ifndef TEST_LOAD_BUFFER
  1506. if (SSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  1507. != WOLFSSL_SUCCESS)
  1508. err_sys_ex(catastrophic, "can't load server private key file, "
  1509. "check file and run from wolfSSL home dir");
  1510. #else
  1511. /* loads private key file using buffer API */
  1512. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  1513. #endif
  1514. }
  1515. #endif
  1516. if (usePsk || usePskPlus) {
  1517. #ifndef NO_PSK
  1518. SSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  1519. #ifdef WOLFSSL_TLS13
  1520. wolfSSL_CTX_set_psk_server_tls13_callback(ctx, my_psk_server_tls13_cb);
  1521. #endif
  1522. if (sendPskIdentityHint == 1)
  1523. SSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  1524. if (cipherList == NULL && !usePskPlus) {
  1525. const char *defaultCipherList;
  1526. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  1527. #ifdef WOLFSSL_TLS13
  1528. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256:"
  1529. "TLS13-AES128-GCM-SHA256";
  1530. #else
  1531. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  1532. #endif
  1533. needDH = 1;
  1534. #elif defined(HAVE_NULL_CIPHER)
  1535. defaultCipherList = "PSK-NULL-SHA256";
  1536. #else
  1537. defaultCipherList = "PSK-AES128-CBC-SHA256";
  1538. #endif
  1539. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  1540. != WOLFSSL_SUCCESS)
  1541. err_sys_ex(runWithErrors, "server can't set cipher list 2");
  1542. }
  1543. #endif
  1544. }
  1545. #ifdef HAVE_ECC
  1546. /* Use ECDHE key size that matches long term key.
  1547. * Zero means use ctx->privateKeySz.
  1548. * Default ECDHE_SIZE is 32 bytes
  1549. */
  1550. if (wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 0) != WOLFSSL_SUCCESS){
  1551. err_sys_ex(runWithErrors, "Error setting ECDHE size");
  1552. }
  1553. #endif
  1554. if (useAnon) {
  1555. #ifdef HAVE_ANON
  1556. wolfSSL_CTX_allow_anon_cipher(ctx);
  1557. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  1558. const char* defaultCipherList;
  1559. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  1560. "ADH-AES128-SHA";
  1561. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  1562. != WOLFSSL_SUCCESS)
  1563. err_sys_ex(runWithErrors, "server can't set cipher list 4");
  1564. }
  1565. #endif
  1566. }
  1567. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1568. /* if not using PSK, verify peer with certs
  1569. if using PSK Plus then verify peer certs except PSK suites */
  1570. if (doCliCertCheck && (usePsk == 0 || usePskPlus) && useAnon == 0) {
  1571. unsigned int verify_flags = 0;
  1572. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  1573. (usePskPlus ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  1574. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  1575. overrideDateErrors == 1 ? myDateCb : NULL);
  1576. #ifdef TEST_BEFORE_DATE
  1577. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  1578. #endif
  1579. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  1580. verify_flags) != WOLFSSL_SUCCESS) {
  1581. err_sys_ex(catastrophic,
  1582. "can't load ca file, Please run from wolfSSL home dir");
  1583. }
  1584. #ifdef WOLFSSL_TRUST_PEER_CERT
  1585. if (trustCert) {
  1586. if ((ret = wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  1587. WOLFSSL_FILETYPE_PEM))
  1588. != WOLFSSL_SUCCESS) {
  1589. err_sys_ex(runWithErrors, "can't load trusted peer cert file");
  1590. }
  1591. }
  1592. #endif /* WOLFSSL_TRUST_PEER_CERT */
  1593. }
  1594. #endif
  1595. #if defined(WOLFSSL_SNIFFER)
  1596. /* don't use EDH, can't sniff tmp keys */
  1597. if (cipherList == NULL) {
  1598. if (SSL_CTX_set_cipher_list(ctx, "AES128-SHA") != WOLFSSL_SUCCESS)
  1599. err_sys_ex(runWithErrors, "server can't set cipher list 3");
  1600. }
  1601. #endif
  1602. #ifdef HAVE_SNI
  1603. if (sniHostName)
  1604. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  1605. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS)
  1606. err_sys_ex(runWithErrors, "UseSNI failed");
  1607. #endif
  1608. #ifdef USE_WINDOWS_API
  1609. if (port == 0) {
  1610. /* Generate random port for testing */
  1611. port = GetRandomPort();
  1612. }
  1613. #endif /* USE_WINDOWS_API */
  1614. #ifdef WOLFSSL_ASYNC_CRYPT
  1615. ret = wolfAsync_DevOpen(&devId);
  1616. if (ret < 0) {
  1617. printf("Async device open failed\nRunning without async\n");
  1618. }
  1619. wolfSSL_CTX_UseAsync(ctx, devId);
  1620. #endif /* WOLFSSL_ASYNC_CRYPT */
  1621. #ifdef WOLFSSL_TLS13
  1622. if (noPskDheKe)
  1623. wolfSSL_CTX_no_dhe_psk(ctx);
  1624. if (noTicket)
  1625. wolfSSL_CTX_no_ticket_TLSv13(ctx);
  1626. #endif
  1627. while (1) {
  1628. /* allow resume option */
  1629. if (resumeCount > 1) {
  1630. if (dtlsUDP == 0) {
  1631. SOCKADDR_IN_T client;
  1632. socklen_t client_len = sizeof(client);
  1633. clientfd = accept(sockfd, (struct sockaddr*)&client,
  1634. (ACCEPT_THIRD_T)&client_len);
  1635. }
  1636. else {
  1637. tcp_listen(&sockfd, &port, useAnyAddr, dtlsUDP, dtlsSCTP);
  1638. clientfd = sockfd;
  1639. }
  1640. if (WOLFSSL_SOCKET_IS_INVALID(clientfd)) {
  1641. err_sys_ex(runWithErrors, "tcp accept failed");
  1642. }
  1643. }
  1644. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  1645. fprintf(stderr, "Before creating SSL\n");
  1646. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  1647. err_sys_ex(runWithErrors, "ctx not using static memory");
  1648. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  1649. err_sys_ex(runWithErrors, "error printing out memory stats");
  1650. #endif
  1651. if (doMcast) {
  1652. #ifdef WOLFSSL_MULTICAST
  1653. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  1654. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  1655. != WOLFSSL_SUCCESS)
  1656. err_sys("Couldn't set multicast cipher list.");
  1657. #endif
  1658. }
  1659. if (doDTLS && dtlsUDP) {
  1660. #ifdef WOLFSSL_DTLS
  1661. if (doBlockSeq) {
  1662. wolfSSL_CTX_SetIOSend(ctx, TestEmbedSendTo);
  1663. }
  1664. #endif
  1665. }
  1666. #ifdef HAVE_PK_CALLBACKS
  1667. if (pkCallbacks)
  1668. SetupPkCallbacks(ctx);
  1669. #endif
  1670. ssl = SSL_new(ctx);
  1671. if (ssl == NULL)
  1672. err_sys_ex(catastrophic, "unable to create an SSL object");
  1673. #ifdef OPENSSL_EXTRA
  1674. wolfSSL_KeepArrays(ssl);
  1675. #endif
  1676. /* Support for loading private key and cert using WOLFSSL object */
  1677. #if !defined(NO_CERTS)
  1678. if ((!usePsk || usePskPlus) && !useAnon && loadCertKeyIntoSSLObj) {
  1679. #ifndef TEST_LOAD_BUFFER
  1680. if (SSL_use_certificate_chain_file(ssl, ourCert)
  1681. != WOLFSSL_SUCCESS)
  1682. err_sys_ex(catastrophic, "can't load server cert file, check file "
  1683. "and run from wolfSSL home dir");
  1684. #else
  1685. /* loads cert chain file using buffer API */
  1686. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  1687. #endif
  1688. }
  1689. if (!useNtruKey && (!usePsk || usePskPlus) && !useAnon &&
  1690. loadCertKeyIntoSSLObj
  1691. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  1692. && !pkCallbacks
  1693. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  1694. ) {
  1695. #ifndef TEST_LOAD_BUFFER
  1696. if (SSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  1697. != WOLFSSL_SUCCESS)
  1698. err_sys_ex(catastrophic, "can't load server private key file, check"
  1699. "file and run from wolfSSL home dir");
  1700. #else
  1701. /* loads private key file using buffer API */
  1702. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  1703. #endif
  1704. }
  1705. #endif /* !NO_CERTS */
  1706. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1707. if (hrrCookie && wolfSSL_send_hrr_cookie(ssl, NULL, 0)
  1708. != WOLFSSL_SUCCESS) {
  1709. err_sys("unable to set use of cookie with HRR msg");
  1710. }
  1711. #endif
  1712. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  1713. fprintf(stderr, "After creating SSL\n");
  1714. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  1715. err_sys_ex(runWithErrors, "ctx not using static memory");
  1716. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  1717. err_sys_ex(runWithErrors, "error printing out memory stats");
  1718. #endif
  1719. if (doMcast) {
  1720. #ifdef WOLFSSL_MULTICAST
  1721. byte pms[512];
  1722. byte cr[32];
  1723. byte sr[32];
  1724. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  1725. XMEMSET(pms, 0x23, sizeof(pms));
  1726. XMEMSET(cr, 0xA5, sizeof(cr));
  1727. XMEMSET(sr, 0x5A, sizeof(sr));
  1728. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  1729. != WOLFSSL_SUCCESS)
  1730. err_sys("unable to set mcast secret");
  1731. #endif
  1732. }
  1733. #ifdef HAVE_SECURE_RENEGOTIATION
  1734. if (scr) {
  1735. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  1736. err_sys_ex(runWithErrors, "can't enable secure renegotiation");
  1737. }
  1738. }
  1739. #endif /* HAVE_SECURE_RENEGOTIATION */
  1740. #ifndef NO_HANDSHAKE_DONE_CB
  1741. wolfSSL_SetHsDoneCb(ssl, myHsDoneCb, NULL);
  1742. #endif
  1743. #ifdef HAVE_CRL
  1744. if (!disableCRL) {
  1745. #ifdef HAVE_CRL_MONITOR
  1746. crlFlags = WOLFSSL_CRL_MONITOR | WOLFSSL_CRL_START_MON;
  1747. #endif
  1748. if (wolfSSL_EnableCRL(ssl, 0) != WOLFSSL_SUCCESS)
  1749. err_sys_ex(runWithErrors, "unable to enable CRL");
  1750. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, crlFlags)
  1751. != WOLFSSL_SUCCESS)
  1752. err_sys_ex(runWithErrors, "unable to load CRL");
  1753. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS)
  1754. err_sys_ex(runWithErrors, "unable to set CRL callback url");
  1755. }
  1756. #endif
  1757. #ifdef HAVE_OCSP
  1758. if (useOcsp) {
  1759. if (ocspUrl != NULL) {
  1760. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  1761. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  1762. | WOLFSSL_OCSP_URL_OVERRIDE);
  1763. }
  1764. else
  1765. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE);
  1766. }
  1767. #ifndef NO_RSA
  1768. /* All the OSCP Stapling test certs are RSA. */
  1769. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1770. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1771. { /* scope start */
  1772. const char* ca1 = "certs/ocsp/intermediate1-ca-cert.pem";
  1773. const char* ca2 = "certs/ocsp/intermediate2-ca-cert.pem";
  1774. const char* ca3 = "certs/ocsp/intermediate3-ca-cert.pem";
  1775. int fails = 0;
  1776. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS) {
  1777. err_sys_ex(catastrophic, "can't enable OCSP Stapling "
  1778. "Certificate Manager");
  1779. }
  1780. if (SSL_CTX_load_verify_locations(ctx, ca1, 0) != WOLFSSL_SUCCESS) {
  1781. fails++;
  1782. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  1783. "wolfSSL home dir");
  1784. }
  1785. if (SSL_CTX_load_verify_locations(ctx, ca2, 0) != WOLFSSL_SUCCESS) {
  1786. fails++;
  1787. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  1788. "wolfSSL home dir");
  1789. }
  1790. if (SSL_CTX_load_verify_locations(ctx, ca3, 0) != WOLFSSL_SUCCESS) {
  1791. fails++;
  1792. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  1793. "wolfSSL home dir");
  1794. }
  1795. if (fails > 2) {
  1796. err_sys_ex(catastrophic, "Failed to load any intermediates for "
  1797. "OCSP stapling test");
  1798. }
  1799. } /* scope end */
  1800. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  1801. #endif /* NO_RSA */
  1802. #endif /* HAVE_OCSP */
  1803. #ifdef HAVE_PK_CALLBACKS
  1804. if (pkCallbacks)
  1805. SetupPkCallbackContexts(ssl, &pkCbInfo);
  1806. #endif
  1807. #ifdef WOLFSSL_TLS13
  1808. if (version >= 4) {
  1809. WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
  1810. if (onlyKeyShare == 2) {
  1811. if (useX25519 == 1) {
  1812. #ifdef HAVE_CURVE25519
  1813. int groups[1] = { WOLFSSL_ECC_X25519 };
  1814. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  1815. != WOLFSSL_SUCCESS) {
  1816. err_sys("unable to use curve x25519");
  1817. }
  1818. if (wolfSSL_set_groups(ssl, groups, 1) != WOLFSSL_SUCCESS) {
  1819. err_sys("unable to set groups: x25519");
  1820. }
  1821. #endif
  1822. }
  1823. else
  1824. {
  1825. #ifdef HAVE_ECC
  1826. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  1827. int groups[1] = { WOLFSSL_ECC_SECP256R1 };
  1828. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1)
  1829. != WOLFSSL_SUCCESS) {
  1830. err_sys("unable to use curve secp256r1");
  1831. }
  1832. if (wolfSSL_set_groups(ssl, groups, 1) != WOLFSSL_SUCCESS) {
  1833. err_sys("unable to set groups: secp256r1");
  1834. }
  1835. #endif
  1836. #endif
  1837. }
  1838. }
  1839. else if (onlyKeyShare == 1) {
  1840. #ifdef HAVE_FFDHE_2048
  1841. int groups[1] = { WOLFSSL_FFDHE_2048 };
  1842. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048)
  1843. != WOLFSSL_SUCCESS) {
  1844. err_sys("unable to use DH 2048-bit parameters");
  1845. }
  1846. if (wolfSSL_set_groups(ssl, groups, 1) != WOLFSSL_SUCCESS) {
  1847. err_sys("unable to set groups: DH 2048-bit");
  1848. }
  1849. #endif
  1850. }
  1851. WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
  1852. }
  1853. #endif
  1854. #ifdef HAVE_ENCRYPT_THEN_MAC
  1855. if (disallowETM)
  1856. wolfSSL_AllowEncryptThenMac(ssl, 0);
  1857. #endif
  1858. /* do accept */
  1859. readySignal = ((func_args*)args)->signal;
  1860. if (readySignal) {
  1861. readySignal->srfName = serverReadyFile;
  1862. }
  1863. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr,
  1864. dtlsUDP, dtlsSCTP, serverReadyFile ? 1 : 0, doListen);
  1865. doListen = 0; /* Don't listen next time */
  1866. if (SSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  1867. err_sys_ex(catastrophic, "error in setting fd");
  1868. }
  1869. #ifdef HAVE_TRUSTED_CA
  1870. if (trustedCaKeyId) {
  1871. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  1872. NULL, 0) != WOLFSSL_SUCCESS) {
  1873. err_sys_ex(runWithErrors, "UseTrustedCA failed");
  1874. }
  1875. }
  1876. #endif /* HAVE_TRUSTED_CA */
  1877. #ifdef HAVE_ALPN
  1878. if (alpnList != NULL) {
  1879. printf("ALPN accepted protocols list : %s\n", alpnList);
  1880. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  1881. }
  1882. #endif
  1883. #ifdef WOLFSSL_DTLS
  1884. if (doDTLS && dtlsUDP) {
  1885. SOCKADDR_IN_T cliaddr;
  1886. byte b[1500];
  1887. int n;
  1888. socklen_t len = sizeof(cliaddr);
  1889. /* For DTLS, peek at the next datagram so we can get the client's
  1890. * address and set it into the ssl object later to generate the
  1891. * cookie. */
  1892. n = (int)recvfrom(clientfd, (char*)b, sizeof(b), MSG_PEEK,
  1893. (struct sockaddr*)&cliaddr, &len);
  1894. if (n <= 0)
  1895. err_sys_ex(runWithErrors, "recvfrom failed");
  1896. if (doBlockSeq) {
  1897. XMEMCPY(&dtlsCtx.peer.sa, &cliaddr, len);
  1898. dtlsCtx.peer.sz = len;
  1899. dtlsCtx.wfd = clientfd;
  1900. dtlsCtx.failOnce = 1;
  1901. wolfSSL_SetIOWriteCtx(ssl, &dtlsCtx);
  1902. }
  1903. else {
  1904. wolfSSL_dtls_set_peer(ssl, &cliaddr, len);
  1905. }
  1906. }
  1907. #endif
  1908. if ((usePsk == 0 || usePskPlus) || useAnon == 1 || cipherList != NULL
  1909. || needDH == 1) {
  1910. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && !defined(NO_ASN)
  1911. wolfSSL_SetTmpDH_file(ssl, ourDhParam, WOLFSSL_FILETYPE_PEM);
  1912. #elif !defined(NO_DH)
  1913. SetDH(ssl); /* repick suites with DHE, higher priority than
  1914. * PSK */
  1915. #endif
  1916. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  1917. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  1918. if (!doDhKeyCheck)
  1919. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  1920. #endif
  1921. }
  1922. #ifndef WOLFSSL_CALLBACKS
  1923. if (nonBlocking) {
  1924. #ifdef WOLFSSL_DTLS
  1925. if (doDTLS) {
  1926. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  1927. }
  1928. #endif
  1929. tcp_set_nonblocking(&clientfd);
  1930. }
  1931. #endif
  1932. #ifndef WOLFSSL_CALLBACKS
  1933. if (nonBlocking) {
  1934. ret = NonBlockingSSL_Accept(ssl);
  1935. }
  1936. else {
  1937. #ifdef WOLFSSL_EARLY_DATA
  1938. if (earlyData) {
  1939. do {
  1940. int len;
  1941. err = 0; /* reset error */
  1942. ret = wolfSSL_read_early_data(ssl, input, sizeof(input)-1,
  1943. &len);
  1944. if (ret != WOLFSSL_SUCCESS) {
  1945. err = SSL_get_error(ssl, 0);
  1946. #ifdef WOLFSSL_ASYNC_CRYPT
  1947. if (err == WC_PENDING_E) {
  1948. ret = wolfSSL_AsyncPoll(ssl,
  1949. WOLF_POLL_FLAG_CHECK_HW);
  1950. if (ret < 0) break;
  1951. }
  1952. #endif
  1953. }
  1954. if (ret > 0) {
  1955. input[ret] = 0; /* null terminate message */
  1956. printf("Early Data Client message: %s\n", input);
  1957. }
  1958. } while (err == WC_PENDING_E || ret > 0);
  1959. }
  1960. #endif
  1961. do {
  1962. err = 0; /* reset error */
  1963. ret = SSL_accept(ssl);
  1964. if (ret != WOLFSSL_SUCCESS) {
  1965. err = SSL_get_error(ssl, 0);
  1966. #ifdef WOLFSSL_ASYNC_CRYPT
  1967. if (err == WC_PENDING_E) {
  1968. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1969. if (ret < 0) break;
  1970. }
  1971. #endif
  1972. }
  1973. } while (err == WC_PENDING_E);
  1974. }
  1975. #else
  1976. ret = NonBlockingSSL_Accept(ssl);
  1977. #endif
  1978. if (ret != WOLFSSL_SUCCESS) {
  1979. err = SSL_get_error(ssl, 0);
  1980. printf("SSL_accept error %d, %s\n", err,
  1981. ERR_error_string(err, buffer));
  1982. if (!exitWithRet) {
  1983. err_sys_ex(runWithErrors, "SSL_accept failed");
  1984. } else {
  1985. /* cleanup */
  1986. SSL_free(ssl); ssl = NULL;
  1987. SSL_CTX_free(ctx); ctx = NULL;
  1988. CloseSocket(clientfd);
  1989. CloseSocket(sockfd);
  1990. ((func_args*)args)->return_code = err;
  1991. goto exit;
  1992. }
  1993. }
  1994. showPeerEx(ssl, lng_index);
  1995. if (SSL_state(ssl) != 0) {
  1996. err_sys_ex(runWithErrors, "SSL in error state");
  1997. }
  1998. #ifdef OPENSSL_EXTRA
  1999. {
  2000. byte* rnd;
  2001. byte* pt;
  2002. size_t size;
  2003. /* get size of buffer then print */
  2004. size = wolfSSL_get_server_random(NULL, NULL, 0);
  2005. if (size == 0) {
  2006. err_sys_ex(runWithErrors, "error getting server random buffer "
  2007. "size");
  2008. }
  2009. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2010. if (rnd == NULL) {
  2011. err_sys_ex(runWithErrors, "error creating server random buffer");
  2012. }
  2013. size = wolfSSL_get_server_random(ssl, rnd, size);
  2014. if (size == 0) {
  2015. if (rnd) {
  2016. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2017. rnd = NULL;
  2018. }
  2019. err_sys_ex(runWithErrors, "error getting server random buffer");
  2020. }
  2021. if (rnd) {
  2022. printf("Server Random : ");
  2023. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  2024. printf("\n");
  2025. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2026. rnd = NULL;
  2027. }
  2028. }
  2029. #endif
  2030. #ifdef HAVE_ALPN
  2031. if (alpnList != NULL) {
  2032. char *protocol_name = NULL, *list = NULL;
  2033. word16 protocol_nameSz = 0, listSz = 0;
  2034. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name,
  2035. &protocol_nameSz);
  2036. if (err == WOLFSSL_SUCCESS)
  2037. printf("Sent ALPN protocol : %s (%d)\n",
  2038. protocol_name, protocol_nameSz);
  2039. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  2040. printf("No ALPN response sent (no match)\n");
  2041. else
  2042. printf("Getting ALPN protocol name failed\n");
  2043. err = wolfSSL_ALPN_GetPeerProtocol(ssl, &list, &listSz);
  2044. if (err == WOLFSSL_SUCCESS)
  2045. printf("List of protocol names sent by Client: %s (%d)\n",
  2046. list, listSz);
  2047. else
  2048. printf("Get list of client's protocol name failed\n");
  2049. free(list);
  2050. }
  2051. #endif
  2052. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2053. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2054. if (postHandAuth) {
  2055. unsigned int verify_flags = 0;
  2056. SSL_set_verify(ssl, WOLFSSL_VERIFY_PEER |
  2057. ((usePskPlus) ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  2058. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT), 0);
  2059. #ifdef TEST_BEFORE_DATE
  2060. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2061. #endif
  2062. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2063. verify_flags)
  2064. != WOLFSSL_SUCCESS) {
  2065. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2066. "wolfSSL home dir");
  2067. }
  2068. #ifdef WOLFSSL_TRUST_PEER_CERT
  2069. if (trustCert) {
  2070. if ((ret = wolfSSL_trust_peer_cert(ssl, trustCert,
  2071. WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
  2072. err_sys_ex(runWithErrors, "can't load trusted peer cert "
  2073. "file");
  2074. }
  2075. }
  2076. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2077. }
  2078. #endif
  2079. #endif
  2080. if (echoData == 0 && throughput == 0) {
  2081. ServerRead(ssl, input, sizeof(input)-1);
  2082. err = SSL_get_error(ssl, 0);
  2083. }
  2084. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  2085. defined(HAVE_SERVER_RENEGOTIATION_INFO)
  2086. if (scr && forceScr) {
  2087. if (nonBlocking) {
  2088. printf("not doing secure renegotiation on example with"
  2089. " nonblocking yet\n");
  2090. } else {
  2091. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  2092. printf("not doing secure renegotiation\n");
  2093. }
  2094. else {
  2095. printf("RENEGOTIATION SUCCESSFUL\n");
  2096. }
  2097. }
  2098. }
  2099. #endif /* HAVE_SECURE_RENEGOTIATION */
  2100. if (err == 0 && echoData == 0 && throughput == 0) {
  2101. const char* write_msg;
  2102. int write_msg_sz;
  2103. #ifdef WOLFSSL_TLS13
  2104. if (updateKeysIVs)
  2105. wolfSSL_update_keys(ssl);
  2106. #endif
  2107. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2108. if (postHandAuth)
  2109. wolfSSL_request_certificate(ssl);
  2110. #endif
  2111. /* Write data */
  2112. if (!useWebServerMsg) {
  2113. write_msg = msg;
  2114. write_msg_sz = sizeof(msg);
  2115. }
  2116. else {
  2117. write_msg = webServerMsg;
  2118. write_msg_sz = sizeof(webServerMsg);
  2119. }
  2120. ServerWrite(ssl, write_msg, write_msg_sz);
  2121. #ifdef WOLFSSL_TLS13
  2122. if (updateKeysIVs || postHandAuth)
  2123. ServerRead(ssl, input, sizeof(input)-1);
  2124. #endif
  2125. }
  2126. else if (err == 0 || err == WOLFSSL_ERROR_ZERO_RETURN) {
  2127. ServerEchoData(ssl, clientfd, echoData, block, throughput);
  2128. }
  2129. #if defined(WOLFSSL_MDK_SHELL) && defined(HAVE_MDK_RTX)
  2130. os_dly_wait(500) ;
  2131. #elif defined (WOLFSSL_TIRTOS)
  2132. Task_yield();
  2133. #endif
  2134. if (dtlsUDP == 0) {
  2135. ret = SSL_shutdown(ssl);
  2136. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  2137. SSL_shutdown(ssl); /* bidirectional shutdown */
  2138. }
  2139. /* display collected statistics */
  2140. #ifdef WOLFSSL_STATIC_MEMORY
  2141. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  2142. err_sys_ex(runWithErrors, "static memory was not used with ssl");
  2143. fprintf(stderr, "\nprint off SSL memory stats\n");
  2144. fprintf(stderr, "*** This is memory state before wolfSSL_free is "
  2145. "called\n");
  2146. fprintf(stderr, "peak connection memory = %d\n", ssl_stats.peakMem);
  2147. fprintf(stderr, "current memory in use = %d\n", ssl_stats.curMem);
  2148. fprintf(stderr, "peak connection allocs = %d\n", ssl_stats.peakAlloc);
  2149. fprintf(stderr, "current connection allocs = %d\n",ssl_stats.curAlloc);
  2150. fprintf(stderr, "total connection allocs = %d\n",
  2151. ssl_stats.totalAlloc);
  2152. fprintf(stderr, "total connection frees = %d\n\n",
  2153. ssl_stats.totalFr);
  2154. #endif
  2155. SSL_free(ssl); ssl = NULL;
  2156. CloseSocket(clientfd);
  2157. if (resume == 1 && resumeCount == 0) {
  2158. resumeCount++; /* only do one resume for testing */
  2159. continue;
  2160. }
  2161. resumeCount = 0;
  2162. cnt++;
  2163. if (loops > 0 && --loops == 0) {
  2164. break; /* out of while loop, done with normal and resume option */
  2165. }
  2166. } /* while(1) */
  2167. WOLFSSL_TIME(cnt);
  2168. (void)cnt;
  2169. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2170. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2171. wolfSSL_CTX_DisableOCSPStapling(ctx);
  2172. #endif
  2173. CloseSocket(sockfd);
  2174. SSL_CTX_free(ctx); ctx = NULL;
  2175. ((func_args*)args)->return_code = 0;
  2176. exit:
  2177. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  2178. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  2179. wc_ecc_fp_free(); /* free per thread cache */
  2180. #endif
  2181. #ifdef WOLFSSL_TIRTOS
  2182. fdCloseSession(Task_self());
  2183. #endif
  2184. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  2185. defined(HAVE_POLY1305)
  2186. TicketCleanup();
  2187. #endif
  2188. #ifdef WOLFSSL_ASYNC_CRYPT
  2189. wolfAsync_DevClose(&devId);
  2190. #endif
  2191. /* There are use cases when these assignments are not read. To avoid
  2192. * potential confusion those warnings have been handled here.
  2193. */
  2194. (void) ourKey;
  2195. (void) verifyCert;
  2196. (void) doCliCertCheck;
  2197. (void) useNtruKey;
  2198. (void) ourDhParam;
  2199. (void) ourCert;
  2200. (void) useX25519;
  2201. #ifdef HAVE_SECURE_RENEGOTIATION
  2202. (void) forceScr;
  2203. #endif
  2204. #ifndef WOLFSSL_TIRTOS
  2205. return 0;
  2206. #endif
  2207. }
  2208. #endif /* !NO_WOLFSSL_SERVER */
  2209. /* so overall tests can pull in test function */
  2210. #ifndef NO_MAIN_DRIVER
  2211. int main(int argc, char** argv)
  2212. {
  2213. func_args args;
  2214. tcp_ready ready;
  2215. StartTCP();
  2216. args.argc = argc;
  2217. args.argv = argv;
  2218. args.signal = &ready;
  2219. args.return_code = 0;
  2220. InitTcpReady(&ready);
  2221. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL)
  2222. wolfSSL_Debugging_ON();
  2223. #endif
  2224. wolfSSL_Init();
  2225. ChangeToWolfRoot();
  2226. #ifndef NO_WOLFSSL_SERVER
  2227. #ifdef HAVE_STACK_SIZE
  2228. StackSizeCheck(&args, server_test);
  2229. #else
  2230. server_test(&args);
  2231. #endif
  2232. #else
  2233. printf("Server not compiled in!\n");
  2234. #endif
  2235. wolfSSL_Cleanup();
  2236. FreeTcpReady(&ready);
  2237. #ifdef HAVE_WNR
  2238. if (wc_FreeNetRandom() < 0)
  2239. err_sys_ex(runWithErrors, "Failed to free netRandom context");
  2240. #endif /* HAVE_WNR */
  2241. return args.return_code;
  2242. }
  2243. int myoptind = 0;
  2244. char* myoptarg = NULL;
  2245. #endif /* NO_MAIN_DRIVER */