server.c 86 KB

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