server.c 84 KB

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