2
0

client.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. /* client.c
  2. *
  3. * Copyright (C) 2006-2017 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/wolfcrypt/settings.h>
  25. #include <wolfssl/ssl.h>
  26. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "cmsis_os.h"
  30. #include "rl_fs.h"
  31. #include "rl_net.h"
  32. #include "wolfssl_MDK_ARM.h"
  33. #endif
  34. #include <wolfssl/test.h>
  35. #include <examples/client/client.h>
  36. #ifndef NO_WOLFSSL_CLIENT
  37. #ifdef WOLFSSL_ASYNC_CRYPT
  38. static int devId = INVALID_DEVID;
  39. #endif
  40. #define DEFAULT_TIMEOUT_SEC 2
  41. /* Note on using port 0: the client standalone example doesn't utilize the
  42. * port 0 port sharing; that is used by (1) the server in external control
  43. * test mode and (2) the testsuite which uses this code and sets up the correct
  44. * port numbers when the internal thread using the server code using port 0. */
  45. #ifdef WOLFSSL_CALLBACKS
  46. Timeval timeout;
  47. static int handShakeCB(HandShakeInfo* info)
  48. {
  49. (void)info;
  50. return 0;
  51. }
  52. static int timeoutCB(TimeoutInfo* info)
  53. {
  54. (void)info;
  55. return 0;
  56. }
  57. #endif
  58. #ifdef HAVE_SESSION_TICKET
  59. static int sessionTicketCB(WOLFSSL* ssl,
  60. const unsigned char* ticket, int ticketSz,
  61. void* ctx)
  62. {
  63. (void)ssl;
  64. (void)ticket;
  65. printf("Session Ticket CB: ticketSz = %d, ctx = %s\n",
  66. ticketSz, (char*)ctx);
  67. return 0;
  68. }
  69. #endif
  70. static int NonBlockingSSL_Connect(WOLFSSL* ssl)
  71. {
  72. int ret;
  73. int error;
  74. SOCKET_T sockfd;
  75. int select_ret = 0;
  76. #ifndef WOLFSSL_CALLBACKS
  77. ret = wolfSSL_connect(ssl);
  78. #else
  79. ret = wolfSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
  80. #endif
  81. error = wolfSSL_get_error(ssl, 0);
  82. sockfd = (SOCKET_T)wolfSSL_get_fd(ssl);
  83. while (ret != WOLFSSL_SUCCESS && (error == WOLFSSL_ERROR_WANT_READ ||
  84. error == WOLFSSL_ERROR_WANT_WRITE ||
  85. error == WC_PENDING_E)) {
  86. int currTimeout = 1;
  87. if (error == WOLFSSL_ERROR_WANT_READ)
  88. printf("... client would read block\n");
  89. else if (error == WOLFSSL_ERROR_WANT_WRITE)
  90. printf("... client would write block\n");
  91. #ifdef WOLFSSL_ASYNC_CRYPT
  92. else if (error == WC_PENDING_E) {
  93. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  94. if (ret < 0) break;
  95. }
  96. #endif
  97. if (error != WC_PENDING_E) {
  98. #ifdef WOLFSSL_DTLS
  99. currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
  100. #endif
  101. select_ret = tcp_select(sockfd, currTimeout);
  102. }
  103. if ((select_ret == TEST_RECV_READY) ||
  104. (select_ret == TEST_ERROR_READY) || error == WC_PENDING_E) {
  105. #ifndef WOLFSSL_CALLBACKS
  106. ret = wolfSSL_connect(ssl);
  107. #else
  108. ret = wolfSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
  109. #endif
  110. error = wolfSSL_get_error(ssl, 0);
  111. }
  112. else if (select_ret == TEST_TIMEOUT && !wolfSSL_dtls(ssl)) {
  113. error = WOLFSSL_ERROR_WANT_READ;
  114. }
  115. #ifdef WOLFSSL_DTLS
  116. else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl) &&
  117. wolfSSL_dtls_got_timeout(ssl) >= 0) {
  118. error = WOLFSSL_ERROR_WANT_READ;
  119. }
  120. #endif
  121. else {
  122. error = WOLFSSL_FATAL_ERROR;
  123. }
  124. }
  125. return ret;
  126. }
  127. static void ShowCiphers(void)
  128. {
  129. char ciphers[4096];
  130. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  131. if (ret == WOLFSSL_SUCCESS)
  132. printf("%s\n", ciphers);
  133. }
  134. /* Shows which versions are valid */
  135. static void ShowVersions(void)
  136. {
  137. #ifndef NO_OLD_TLS
  138. #ifdef WOLFSSL_ALLOW_SSLV3
  139. printf("0:");
  140. #endif
  141. #ifdef WOLFSSL_ALLOW_TLSV10
  142. printf("1:");
  143. #endif
  144. printf("2:");
  145. #endif /* NO_OLD_TLS */
  146. printf("3:");
  147. #ifdef WOLFSSL_TLS13
  148. printf("4:");
  149. #endif
  150. printf("\n");
  151. }
  152. /* Measures average time to create, connect and disconnect a connection (TPS).
  153. Benchmark = number of connections. */
  154. static int ClientBenchmarkConnections(WOLFSSL_CTX* ctx, char* host, word16 port,
  155. int dtlsUDP, int dtlsSCTP, int benchmark, int resumeSession, int useX25519,
  156. int helloRetry)
  157. {
  158. /* time passed in number of connects give average */
  159. int times = benchmark;
  160. int loops = resumeSession ? 2 : 1;
  161. int i = 0, err, ret;
  162. #ifndef NO_SESSION_CACHE
  163. WOLFSSL_SESSION* benchSession = NULL;
  164. #endif
  165. #ifdef WOLFSSL_TLS13
  166. byte* reply[80];
  167. static const char msg[] = "hello wolfssl!";
  168. #endif
  169. (void)resumeSession;
  170. (void)useX25519;
  171. (void)helloRetry;
  172. while (loops--) {
  173. #ifndef NO_SESSION_CACHE
  174. int benchResume = resumeSession && loops == 0;
  175. #endif
  176. double start = current_time(1), avg;
  177. for (i = 0; i < times; i++) {
  178. SOCKET_T sockfd;
  179. WOLFSSL* ssl = wolfSSL_new(ctx);
  180. if (ssl == NULL)
  181. err_sys("unable to get SSL object");
  182. #ifdef WOLFSSL_TLS13
  183. if (helloRetry)
  184. wolfSSL_NoKeyShares(ssl);
  185. #endif
  186. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  187. #ifndef NO_SESSION_CACHE
  188. if (benchResume)
  189. wolfSSL_set_session(ssl, benchSession);
  190. #endif
  191. #ifdef WOLFSSL_TLS13
  192. #ifdef HAVE_CURVE25519
  193. #ifndef NO_SESSION_CACHE
  194. if (benchResume) {
  195. }
  196. else
  197. #endif
  198. if (useX25519) {
  199. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  200. != WOLFSSL_SUCCESS) {
  201. err_sys("unable to use curve x25519");
  202. }
  203. }
  204. #endif
  205. #endif
  206. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  207. err_sys("error in setting fd");
  208. }
  209. do {
  210. err = 0; /* reset error */
  211. ret = wolfSSL_connect(ssl);
  212. if (ret != WOLFSSL_SUCCESS) {
  213. err = wolfSSL_get_error(ssl, 0);
  214. #ifdef WOLFSSL_ASYNC_CRYPT
  215. if (err == WC_PENDING_E) {
  216. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  217. if (ret < 0) break;
  218. }
  219. #endif
  220. }
  221. } while (err == WC_PENDING_E);
  222. if (ret != WOLFSSL_SUCCESS) {
  223. err_sys("SSL_connect failed");
  224. }
  225. #ifdef WOLFSSL_TLS13
  226. if (resumeSession) {
  227. if (wolfSSL_write(ssl, msg, sizeof(msg)-1) <= 0)
  228. err_sys("SSL_write failed");
  229. if (wolfSSL_read(ssl, reply, sizeof(reply)-1) <= 0)
  230. err_sys("SSL_read failed");
  231. }
  232. #endif
  233. wolfSSL_shutdown(ssl);
  234. #ifndef NO_SESSION_CACHE
  235. if (i == (times-1) && resumeSession) {
  236. benchSession = wolfSSL_get_session(ssl);
  237. }
  238. #endif
  239. wolfSSL_free(ssl);
  240. CloseSocket(sockfd);
  241. }
  242. avg = current_time(0) - start;
  243. avg /= times;
  244. avg *= 1000; /* milliseconds */
  245. #ifndef NO_SESSION_CACHE
  246. if (benchResume)
  247. printf("wolfSSL_resume avg took: %8.3f milliseconds\n", avg);
  248. else
  249. #endif
  250. printf("wolfSSL_connect avg took: %8.3f milliseconds\n", avg);
  251. }
  252. return EXIT_SUCCESS;
  253. }
  254. /* Measures throughput in kbps. Throughput = number of bytes */
  255. static int ClientBenchmarkThroughput(WOLFSSL_CTX* ctx, char* host, word16 port,
  256. int dtlsUDP, int dtlsSCTP, int throughput, int useX25519)
  257. {
  258. double start, conn_time = 0, tx_time = 0, rx_time = 0;
  259. SOCKET_T sockfd;
  260. WOLFSSL* ssl;
  261. int ret = 0, err = 0;
  262. start = current_time(1);
  263. ssl = wolfSSL_new(ctx);
  264. if (ssl == NULL)
  265. err_sys("unable to get SSL object");
  266. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  267. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  268. err_sys("error in setting fd");
  269. }
  270. (void)useX25519;
  271. #ifdef WOLFSSL_TLS13
  272. #ifdef HAVE_CURVE25519
  273. if (useX25519) {
  274. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  275. != WOLFSSL_SUCCESS) {
  276. err_sys("unable to use curve x25519");
  277. }
  278. }
  279. #endif
  280. #endif
  281. do {
  282. err = 0; /* reset error */
  283. ret = wolfSSL_connect(ssl);
  284. if (ret != WOLFSSL_SUCCESS) {
  285. err = wolfSSL_get_error(ssl, 0);
  286. #ifdef WOLFSSL_ASYNC_CRYPT
  287. if (err == WC_PENDING_E) {
  288. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  289. if (ret < 0) break;
  290. }
  291. #endif
  292. }
  293. } while (err == WC_PENDING_E);
  294. if (ret == WOLFSSL_SUCCESS) {
  295. /* Perform throughput test */
  296. char *tx_buffer, *rx_buffer;
  297. /* Record connection time */
  298. conn_time = current_time(0) - start;
  299. /* Allocate TX/RX buffers */
  300. tx_buffer = (char*)XMALLOC(TEST_BUFFER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  301. rx_buffer = (char*)XMALLOC(TEST_BUFFER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  302. if (tx_buffer && rx_buffer) {
  303. WC_RNG rng;
  304. /* Startup the RNG */
  305. #if !defined(HAVE_FIPS) && defined(WOLFSSL_ASYNC_CRYPT)
  306. ret = wc_InitRng_ex(&rng, NULL, devId);
  307. #else
  308. ret = wc_InitRng(&rng);
  309. #endif
  310. if (ret == 0) {
  311. int xfer_bytes;
  312. /* Generate random data to send */
  313. ret = wc_RNG_GenerateBlock(&rng, (byte*)tx_buffer, TEST_BUFFER_SIZE);
  314. wc_FreeRng(&rng);
  315. if(ret != 0) {
  316. err_sys("wc_RNG_GenerateBlock failed");
  317. }
  318. /* Perform TX and RX of bytes */
  319. xfer_bytes = 0;
  320. while (throughput > xfer_bytes) {
  321. int len, rx_pos, select_ret;
  322. /* Determine packet size */
  323. len = min(TEST_BUFFER_SIZE, throughput - xfer_bytes);
  324. /* Perform TX */
  325. start = current_time(1);
  326. do {
  327. err = 0; /* reset error */
  328. ret = wolfSSL_write(ssl, tx_buffer, len);
  329. if (ret <= 0) {
  330. err = wolfSSL_get_error(ssl, 0);
  331. #ifdef WOLFSSL_ASYNC_CRYPT
  332. if (err == WC_PENDING_E) {
  333. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  334. if (ret < 0) break;
  335. }
  336. #endif
  337. }
  338. } while (err == WC_PENDING_E);
  339. if (ret != len) {
  340. printf("SSL_write bench error %d!\n", err);
  341. err_sys("SSL_write failed");
  342. }
  343. tx_time += current_time(0) - start;
  344. /* Perform RX */
  345. select_ret = tcp_select(sockfd, DEFAULT_TIMEOUT_SEC);
  346. if (select_ret == TEST_RECV_READY) {
  347. start = current_time(1);
  348. rx_pos = 0;
  349. while (rx_pos < len) {
  350. ret = wolfSSL_read(ssl, &rx_buffer[rx_pos],
  351. len - rx_pos);
  352. if (ret <= 0) {
  353. err = wolfSSL_get_error(ssl, 0);
  354. #ifdef WOLFSSL_ASYNC_CRYPT
  355. if (err == WC_PENDING_E) {
  356. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  357. if (ret < 0) break;
  358. }
  359. else
  360. #endif
  361. if (err != WOLFSSL_ERROR_WANT_READ) {
  362. printf("SSL_read bench error %d\n", err);
  363. err_sys("SSL_read failed");
  364. }
  365. }
  366. else {
  367. rx_pos += ret;
  368. }
  369. }
  370. rx_time += current_time(0) - start;
  371. }
  372. /* Compare TX and RX buffers */
  373. if(XMEMCMP(tx_buffer, rx_buffer, len) != 0) {
  374. free(tx_buffer);
  375. tx_buffer = NULL;
  376. free(rx_buffer);
  377. rx_buffer = NULL;
  378. err_sys("Compare TX and RX buffers failed");
  379. }
  380. /* Update overall position */
  381. xfer_bytes += len;
  382. }
  383. }
  384. else {
  385. err_sys("wc_InitRng failed");
  386. }
  387. }
  388. else {
  389. err_sys("Client buffer malloc failed");
  390. }
  391. if(tx_buffer) XFREE(tx_buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  392. if(rx_buffer) XFREE(rx_buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  393. }
  394. else {
  395. err_sys("wolfSSL_connect failed");
  396. }
  397. wolfSSL_shutdown(ssl);
  398. wolfSSL_free(ssl);
  399. CloseSocket(sockfd);
  400. printf("wolfSSL Client Benchmark %d bytes\n"
  401. "\tConnect %8.3f ms\n"
  402. "\tTX %8.3f ms (%8.3f MBps)\n"
  403. "\tRX %8.3f ms (%8.3f MBps)\n",
  404. throughput,
  405. conn_time * 1000,
  406. tx_time * 1000, throughput / tx_time / 1024 / 1024,
  407. rx_time * 1000, throughput / rx_time / 1024 / 1024
  408. );
  409. return EXIT_SUCCESS;
  410. }
  411. const char* starttlsCmd[6] = {
  412. "220",
  413. "EHLO mail.example.com\r\n",
  414. "250",
  415. "STARTTLS\r\n",
  416. "220",
  417. "QUIT\r\n",
  418. };
  419. /* Initiates the STARTTLS command sequence over TCP */
  420. static int StartTLS_Init(SOCKET_T* sockfd)
  421. {
  422. char tmpBuf[256];
  423. if (sockfd == NULL)
  424. return BAD_FUNC_ARG;
  425. /* S: 220 <host> SMTP service ready */
  426. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  427. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  428. err_sys("failed to read STARTTLS command\n");
  429. if (!XSTRNCMP(tmpBuf, starttlsCmd[0], XSTRLEN(starttlsCmd[0]))) {
  430. printf("%s\n", tmpBuf);
  431. } else {
  432. err_sys("incorrect STARTTLS command received");
  433. }
  434. /* C: EHLO mail.example.com */
  435. if (send(*sockfd, starttlsCmd[1], (int)XSTRLEN(starttlsCmd[1]), 0) !=
  436. (int)XSTRLEN(starttlsCmd[1]))
  437. err_sys("failed to send STARTTLS EHLO command\n");
  438. /* S: 250 <host> offers a warm hug of welcome */
  439. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  440. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  441. err_sys("failed to read STARTTLS command\n");
  442. if (!XSTRNCMP(tmpBuf, starttlsCmd[2], XSTRLEN(starttlsCmd[2]))) {
  443. printf("%s\n", tmpBuf);
  444. } else {
  445. err_sys("incorrect STARTTLS command received");
  446. }
  447. /* C: STARTTLS */
  448. if (send(*sockfd, starttlsCmd[3], (int)XSTRLEN(starttlsCmd[3]), 0) !=
  449. (int)XSTRLEN(starttlsCmd[3])) {
  450. err_sys("failed to send STARTTLS command\n");
  451. }
  452. /* S: 220 Go ahead */
  453. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  454. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  455. err_sys("failed to read STARTTLS command\n");
  456. if (!XSTRNCMP(tmpBuf, starttlsCmd[4], XSTRLEN(starttlsCmd[4]))) {
  457. printf("%s\n", tmpBuf);
  458. } else {
  459. err_sys("incorrect STARTTLS command received, expected 220");
  460. }
  461. return WOLFSSL_SUCCESS;
  462. }
  463. /* Closes down the SMTP connection */
  464. static int SMTP_Shutdown(WOLFSSL* ssl, int wc_shutdown)
  465. {
  466. int ret, err = 0;
  467. char tmpBuf[256];
  468. if (ssl == NULL)
  469. return BAD_FUNC_ARG;
  470. printf("\nwolfSSL client shutting down SMTP connection\n");
  471. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  472. /* C: QUIT */
  473. do {
  474. ret = wolfSSL_write(ssl, starttlsCmd[5], (int)XSTRLEN(starttlsCmd[5]));
  475. if (ret < 0) {
  476. err = wolfSSL_get_error(ssl, 0);
  477. #ifdef WOLFSSL_ASYNC_CRYPT
  478. if (err == WC_PENDING_E) {
  479. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  480. if (ret < 0) break;
  481. }
  482. #endif
  483. }
  484. } while (err == WC_PENDING_E);
  485. if (ret != (int)XSTRLEN(starttlsCmd[5])) {
  486. err_sys("failed to send SMTP QUIT command\n");
  487. }
  488. /* S: 221 2.0.0 Service closing transmission channel */
  489. do {
  490. ret = wolfSSL_read(ssl, tmpBuf, sizeof(tmpBuf));
  491. if (ret < 0) {
  492. err = wolfSSL_get_error(ssl, 0);
  493. #ifdef WOLFSSL_ASYNC_CRYPT
  494. if (err == WC_PENDING_E) {
  495. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  496. if (ret < 0) break;
  497. }
  498. #endif
  499. }
  500. } while (err == WC_PENDING_E);
  501. if (ret < 0) {
  502. err_sys("failed to read SMTP closing down response\n");
  503. }
  504. printf("%s\n", tmpBuf);
  505. ret = wolfSSL_shutdown(ssl);
  506. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  507. wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  508. return WOLFSSL_SUCCESS;
  509. }
  510. static void ClientWrite(WOLFSSL* ssl, char* msg, int msgSz)
  511. {
  512. int ret, err;
  513. char buffer[WOLFSSL_MAX_ERROR_SZ];
  514. do {
  515. err = 0; /* reset error */
  516. ret = wolfSSL_write(ssl, msg, msgSz);
  517. if (ret <= 0) {
  518. err = wolfSSL_get_error(ssl, 0);
  519. #ifdef WOLFSSL_ASYNC_CRYPT
  520. if (err == WC_PENDING_E) {
  521. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  522. if (ret < 0) break;
  523. }
  524. #endif
  525. }
  526. } while (err == WC_PENDING_E);
  527. if (ret != msgSz) {
  528. printf("SSL_write msg error %d, %s\n", err,
  529. wolfSSL_ERR_error_string(err, buffer));
  530. err_sys("SSL_write failed");
  531. }
  532. }
  533. static void ClientRead(WOLFSSL* ssl, char* reply, int replyLen, int mustRead)
  534. {
  535. int ret, err;
  536. char buffer[WOLFSSL_MAX_ERROR_SZ];
  537. do {
  538. err = 0; /* reset error */
  539. ret = wolfSSL_read(ssl, reply, replyLen);
  540. if (ret <= 0) {
  541. err = wolfSSL_get_error(ssl, 0);
  542. #ifdef WOLFSSL_ASYNC_CRYPT
  543. if (err == WC_PENDING_E) {
  544. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  545. if (ret < 0) break;
  546. }
  547. else
  548. #endif
  549. if (err != WOLFSSL_ERROR_WANT_READ) {
  550. printf("SSL_read reply error %d, %s\n", err,
  551. wolfSSL_ERR_error_string(err, buffer));
  552. err_sys("SSL_read failed");
  553. }
  554. }
  555. } while (err == WC_PENDING_E || (mustRead && err == WOLFSSL_ERROR_WANT_READ));
  556. if (ret > 0) {
  557. reply[ret] = 0;
  558. printf("%s\n", reply);
  559. }
  560. }
  561. static void Usage(void)
  562. {
  563. printf("client " LIBWOLFSSL_VERSION_STRING
  564. " NOTE: All files relative to wolfSSL home dir\n");
  565. printf("-? Help, print this usage\n");
  566. printf("-h <host> Host to connect to, default %s\n", wolfSSLIP);
  567. printf("-p <num> Port to connect on, not 0, default %d\n", wolfSSLPort);
  568. #ifndef WOLFSSL_TLS13
  569. printf("-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default %d\n",
  570. CLIENT_DEFAULT_VERSION);
  571. printf("-V Prints valid ssl version numbers, SSLv3(0) - TLS1.2(3)\n");
  572. #else
  573. printf("-v <num> SSL version [0-4], SSLv3(0) - TLS1.3(4)), default %d\n",
  574. CLIENT_DEFAULT_VERSION);
  575. printf("-V Prints valid ssl version numbers, SSLv3(0) - TLS1.3(4)\n");
  576. #endif
  577. printf("-l <str> Cipher suite list (: delimited)\n");
  578. printf("-c <file> Certificate file, default %s\n", cliCertFile);
  579. printf("-k <file> Key file, default %s\n", cliKeyFile);
  580. printf("-A <file> Certificate Authority file, default %s\n", caCertFile);
  581. #ifndef NO_DH
  582. printf("-Z <num> Minimum DH key bits, default %d\n",
  583. DEFAULT_MIN_DHKEY_BITS);
  584. #endif
  585. printf("-b <num> Benchmark <num> connections and print stats\n");
  586. #ifdef HAVE_ALPN
  587. printf("-L <str> Application-Layer Protocol Negotiation ({C,F}:<list>)\n");
  588. #endif
  589. printf("-B <num> Benchmark throughput using <num> bytes and print stats\n");
  590. printf("-s Use pre Shared keys\n");
  591. printf("-d Disable peer checks\n");
  592. printf("-D Override Date Errors example\n");
  593. printf("-e List Every cipher suite available, \n");
  594. printf("-g Send server HTTP GET\n");
  595. printf("-u Use UDP DTLS,"
  596. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n");
  597. #ifdef WOLFSSL_SCTP
  598. printf("-G Use SCTP DTLS,"
  599. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n");
  600. #endif
  601. printf("-m Match domain name in cert\n");
  602. printf("-N Use Non-blocking sockets\n");
  603. #ifndef NO_SESSION_CACHE
  604. printf("-r Resume session\n");
  605. #endif
  606. printf("-w Wait for bidirectional shutdown\n");
  607. printf("-M <prot> Use STARTTLS, using <prot> protocol (smtp)\n");
  608. #ifdef HAVE_SECURE_RENEGOTIATION
  609. printf("-R Allow Secure Renegotiation\n");
  610. printf("-i Force client Initiated Secure Renegotiation\n");
  611. #endif
  612. printf("-f Fewer packets/group messages\n");
  613. printf("-x Disable client cert/key loading\n");
  614. printf("-X Driven by eXternal test case\n");
  615. printf("-j Use verify callback override\n");
  616. #ifdef SHOW_SIZES
  617. printf("-z Print structure sizes\n");
  618. #endif
  619. #ifdef HAVE_SNI
  620. printf("-S <str> Use Host Name Indication\n");
  621. #endif
  622. #ifdef HAVE_MAX_FRAGMENT
  623. printf("-F <num> Use Maximum Fragment Length [1-5]\n");
  624. #endif
  625. #ifdef HAVE_TRUNCATED_HMAC
  626. printf("-T Use Truncated HMAC\n");
  627. #endif
  628. #ifdef HAVE_EXTENDED_MASTER
  629. printf("-n Disable Extended Master Secret\n");
  630. #endif
  631. #ifdef HAVE_OCSP
  632. printf("-o Perform OCSP lookup on peer certificate\n");
  633. printf("-O <url> Perform OCSP lookup using <url> as responder\n");
  634. #endif
  635. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  636. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  637. printf("-W Use OCSP Stapling\n");
  638. #endif
  639. #ifdef ATOMIC_USER
  640. printf("-U Atomic User Record Layer Callbacks\n");
  641. #endif
  642. #ifdef HAVE_PK_CALLBACKS
  643. printf("-P Public Key Callbacks\n");
  644. #endif
  645. #ifdef HAVE_ANON
  646. printf("-a Anonymous client\n");
  647. #endif
  648. #ifdef HAVE_CRL
  649. printf("-C Disable CRL\n");
  650. #endif
  651. #ifdef WOLFSSL_TRUST_PEER_CERT
  652. printf("-E <file> Path to load trusted peer cert\n");
  653. #endif
  654. #ifdef HAVE_WNR
  655. printf("-q <file> Whitewood config file, default %s\n", wnrConfig);
  656. #endif
  657. printf("-H <arg> Internal tests [defCipherList, badCert]\n");
  658. #ifdef WOLFSSL_TLS13
  659. printf("-J Use HelloRetryRequest to choose group for KE\n");
  660. printf("-K Key Exchange for PSK not using (EC)DHE\n");
  661. printf("-I Update keys and IVs before sending data\n");
  662. #ifndef NO_DH
  663. printf("-y Key Share with FFDHE named groups only\n");
  664. #endif
  665. #ifdef HAVE_ECC
  666. printf("-Y Key Share with ECC named groups only\n");
  667. #endif
  668. #endif /* WOLFSSL_TLS13 */
  669. #ifdef HAVE_CURVE25519
  670. printf("-t Use X25519 for key exchange\n");
  671. #endif
  672. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  673. printf("-Q Support requesting certificate post-handshake\n");
  674. #endif
  675. #ifdef WOLFSSL_EARLY_DATA
  676. printf("-0 Early data sent to server (0-RTT handshake)\n");
  677. #endif
  678. #ifdef WOLFSSL_MULTICAST
  679. printf("-3 <grpid> Multicast, grpid < 256\n");
  680. #endif
  681. }
  682. THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
  683. {
  684. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  685. wolfSSL_method_func method = NULL;
  686. WOLFSSL_CTX* ctx = 0;
  687. WOLFSSL* ssl = 0;
  688. WOLFSSL* sslResume = 0;
  689. WOLFSSL_SESSION* session = 0;
  690. #ifndef WOLFSSL_ALT_TEST_STRINGS
  691. char msg[32] = "hello wolfssl!"; /* GET may make bigger */
  692. char resumeMsg[32] = "resuming wolfssl!";
  693. #else
  694. char msg[32] = "hello wolfssl!\n";
  695. char resumeMsg[32] = "resuming wolfssl!\n";
  696. #endif
  697. char reply[80];
  698. int msgSz = (int)XSTRLEN(msg);
  699. int resumeSz = (int)XSTRLEN(resumeMsg);
  700. word16 port = wolfSSLPort;
  701. char* host = (char*)wolfSSLIP;
  702. const char* domain = "localhost"; /* can't default to www.wolfssl.com
  703. because can't tell if we're really
  704. going there to detect old chacha-poly
  705. */
  706. int ch;
  707. int version = CLIENT_INVALID_VERSION;
  708. int usePsk = 0;
  709. int useAnon = 0;
  710. int sendGET = 0;
  711. int benchmark = 0;
  712. int throughput = 0;
  713. int doDTLS = 0;
  714. int dtlsUDP = 0;
  715. int dtlsSCTP = 0;
  716. int doMcast = 0;
  717. int matchName = 0;
  718. int doPeerCheck = 1;
  719. int nonBlocking = 0;
  720. int resumeSession = 0;
  721. int wc_shutdown = 0;
  722. int disableCRL = 0;
  723. int externalTest = 0;
  724. int ret;
  725. int err = 0;
  726. int scr = 0; /* allow secure renegotiation */
  727. int forceScr = 0; /* force client initiaed scr */
  728. int useClientCert = 1;
  729. int fewerPackets = 0;
  730. int atomicUser = 0;
  731. int pkCallbacks = 0;
  732. int overrideDateErrors = 0;
  733. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  734. char* alpnList = NULL;
  735. unsigned char alpn_opt = 0;
  736. char* cipherList = NULL;
  737. int useDefCipherList = 0;
  738. int useBadCert = 0;
  739. const char* verifyCert = caCertFile;
  740. const char* ourCert = cliCertFile;
  741. const char* ourKey = cliKeyFile;
  742. int doSTARTTLS = 0;
  743. char* starttlsProt = NULL;
  744. int useVerifyCb = 0;
  745. #ifdef WOLFSSL_TRUST_PEER_CERT
  746. const char* trustCert = NULL;
  747. #endif
  748. #ifdef HAVE_SNI
  749. char* sniHostName = NULL;
  750. #endif
  751. #ifdef HAVE_MAX_FRAGMENT
  752. byte maxFragment = 0;
  753. #endif
  754. #ifdef HAVE_TRUNCATED_HMAC
  755. byte truncatedHMAC = 0;
  756. #endif
  757. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  758. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  759. byte statusRequest = 0;
  760. #endif
  761. #ifdef HAVE_EXTENDED_MASTER
  762. byte disableExtMasterSecret = 0;
  763. #endif
  764. int helloRetry = 0;
  765. #ifdef WOLFSSL_TLS13
  766. int onlyKeyShare = 0;
  767. int noPskDheKe = 0;
  768. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  769. int postHandAuth = 0;
  770. #endif
  771. #endif
  772. int updateKeysIVs = 0;
  773. #ifdef WOLFSSL_EARLY_DATA
  774. int earlyData = 0;
  775. #endif
  776. #ifdef WOLFSSL_MULTICAST
  777. byte mcastID = 0;
  778. #endif
  779. #ifdef HAVE_OCSP
  780. int useOcsp = 0;
  781. char* ocspUrl = NULL;
  782. #endif
  783. int useX25519 = 0;
  784. #ifdef HAVE_WNR
  785. const char* wnrConfigFile = wnrConfig;
  786. #endif
  787. char buffer[WOLFSSL_MAX_ERROR_SZ];
  788. int argc = ((func_args*)args)->argc;
  789. char** argv = ((func_args*)args)->argv;
  790. #ifdef WOLFSSL_STATIC_MEMORY
  791. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  792. || defined(SESSION_CERTS)
  793. /* big enough to handle most cases including session certs */
  794. byte memory[320000];
  795. #else
  796. byte memory[80000];
  797. #endif
  798. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  799. WOLFSSL_MEM_CONN_STATS ssl_stats;
  800. #ifdef DEBUG_WOLFSSL
  801. WOLFSSL_MEM_STATS mem_stats;
  802. #endif
  803. #endif
  804. ((func_args*)args)->return_code = -1; /* error state */
  805. #ifdef NO_RSA
  806. verifyCert = (char*)caEccCertFile;
  807. ourCert = (char*)cliEccCertFile;
  808. ourKey = (char*)cliEccKeyFile;
  809. #endif
  810. (void)resumeSz;
  811. (void)session;
  812. (void)sslResume;
  813. (void)atomicUser;
  814. (void)pkCallbacks;
  815. (void)scr;
  816. (void)forceScr;
  817. (void)ourKey;
  818. (void)ourCert;
  819. (void)verifyCert;
  820. (void)useClientCert;
  821. (void)overrideDateErrors;
  822. (void)disableCRL;
  823. (void)minDhKeyBits;
  824. (void)alpnList;
  825. (void)alpn_opt;
  826. (void)updateKeysIVs;
  827. (void)useX25519;
  828. (void)helloRetry;
  829. (void)useBadCert;
  830. StackTrap();
  831. #ifndef WOLFSSL_VXWORKS
  832. /* Not used: All used */
  833. while ((ch = mygetopt(argc, argv, "?"
  834. "ab:c:defgh:ijk:l:mnop:q:rstuv:wxyz"
  835. "A:B:CDE:F:GH:IJKL:M:NO:PQRS:TUVW:XYZ:"
  836. "03:")) != -1) {
  837. switch (ch) {
  838. case '?' :
  839. Usage();
  840. exit(EXIT_SUCCESS);
  841. case 'g' :
  842. sendGET = 1;
  843. break;
  844. case 'd' :
  845. doPeerCheck = 0;
  846. break;
  847. case 'e' :
  848. ShowCiphers();
  849. exit(EXIT_SUCCESS);
  850. case 'D' :
  851. overrideDateErrors = 1;
  852. break;
  853. case 'C' :
  854. #ifdef HAVE_CRL
  855. disableCRL = 1;
  856. #endif
  857. break;
  858. case 'u' :
  859. doDTLS = 1;
  860. dtlsUDP = 1;
  861. break;
  862. case 'G' :
  863. #ifdef WOLFSSL_SCTP
  864. doDTLS = 1;
  865. dtlsSCTP = 1;
  866. #endif
  867. break;
  868. case 's' :
  869. usePsk = 1;
  870. break;
  871. #ifdef WOLFSSL_TRUST_PEER_CERT
  872. case 'E' :
  873. trustCert = myoptarg;
  874. break;
  875. #endif
  876. case 'm' :
  877. matchName = 1;
  878. break;
  879. case 'x' :
  880. useClientCert = 0;
  881. break;
  882. case 'X' :
  883. externalTest = 1;
  884. break;
  885. case 'f' :
  886. fewerPackets = 1;
  887. break;
  888. case 'U' :
  889. #ifdef ATOMIC_USER
  890. atomicUser = 1;
  891. #endif
  892. break;
  893. case 'P' :
  894. #ifdef HAVE_PK_CALLBACKS
  895. pkCallbacks = 1;
  896. #endif
  897. break;
  898. case 'h' :
  899. host = myoptarg;
  900. domain = myoptarg;
  901. break;
  902. case 'p' :
  903. port = (word16)atoi(myoptarg);
  904. #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API)
  905. if (port == 0)
  906. err_sys("port number cannot be 0");
  907. #endif
  908. break;
  909. case 'v' :
  910. version = atoi(myoptarg);
  911. if (version < 0 || version > 4) {
  912. Usage();
  913. exit(MY_EX_USAGE);
  914. }
  915. break;
  916. case 'V' :
  917. ShowVersions();
  918. exit(EXIT_SUCCESS);
  919. case 'l' :
  920. cipherList = myoptarg;
  921. break;
  922. case 'H' :
  923. if (XSTRNCMP(myoptarg, "defCipherList", 13) == 0) {
  924. printf("Using default cipher list for testing\n");
  925. useDefCipherList = 1;
  926. }
  927. else if (XSTRNCMP(myoptarg, "badCert", 7) == 0) {
  928. printf("Using bad certificate for testing\n");
  929. useBadCert = 1;
  930. }
  931. else {
  932. Usage();
  933. exit(MY_EX_USAGE);
  934. }
  935. break;
  936. case 'A' :
  937. verifyCert = myoptarg;
  938. break;
  939. case 'c' :
  940. ourCert = myoptarg;
  941. break;
  942. case 'k' :
  943. ourKey = myoptarg;
  944. break;
  945. case 'Z' :
  946. #ifndef NO_DH
  947. minDhKeyBits = atoi(myoptarg);
  948. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  949. Usage();
  950. exit(MY_EX_USAGE);
  951. }
  952. #endif
  953. break;
  954. case 'b' :
  955. benchmark = atoi(myoptarg);
  956. if (benchmark < 0 || benchmark > 1000000) {
  957. Usage();
  958. exit(MY_EX_USAGE);
  959. }
  960. break;
  961. case 'B' :
  962. throughput = atoi(myoptarg);
  963. if (throughput <= 0) {
  964. Usage();
  965. exit(MY_EX_USAGE);
  966. }
  967. break;
  968. case 'N' :
  969. nonBlocking = 1;
  970. break;
  971. case 'r' :
  972. resumeSession = 1;
  973. break;
  974. case 'w' :
  975. wc_shutdown = 1;
  976. break;
  977. case 'R' :
  978. #ifdef HAVE_SECURE_RENEGOTIATION
  979. scr = 1;
  980. #endif
  981. break;
  982. case 'i' :
  983. #ifdef HAVE_SECURE_RENEGOTIATION
  984. scr = 1;
  985. forceScr = 1;
  986. #endif
  987. break;
  988. case 'z' :
  989. #ifndef WOLFSSL_LEANPSK
  990. wolfSSL_GetObjectSize();
  991. #endif
  992. break;
  993. case 'S' :
  994. #ifdef HAVE_SNI
  995. sniHostName = myoptarg;
  996. #endif
  997. break;
  998. case 'F' :
  999. #ifdef HAVE_MAX_FRAGMENT
  1000. maxFragment = atoi(myoptarg);
  1001. if (maxFragment < WOLFSSL_MFL_2_9 ||
  1002. maxFragment > WOLFSSL_MFL_2_13) {
  1003. Usage();
  1004. exit(MY_EX_USAGE);
  1005. }
  1006. #endif
  1007. break;
  1008. case 'T' :
  1009. #ifdef HAVE_TRUNCATED_HMAC
  1010. truncatedHMAC = 1;
  1011. #endif
  1012. break;
  1013. case 'n' :
  1014. #ifdef HAVE_EXTENDED_MASTER
  1015. disableExtMasterSecret = 1;
  1016. #endif
  1017. break;
  1018. case 'W' :
  1019. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1020. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1021. statusRequest = atoi(myoptarg);
  1022. #endif
  1023. break;
  1024. case 'o' :
  1025. #ifdef HAVE_OCSP
  1026. useOcsp = 1;
  1027. #endif
  1028. break;
  1029. case 'O' :
  1030. #ifdef HAVE_OCSP
  1031. useOcsp = 1;
  1032. ocspUrl = myoptarg;
  1033. #endif
  1034. break;
  1035. case 'a' :
  1036. #ifdef HAVE_ANON
  1037. useAnon = 1;
  1038. #endif
  1039. break;
  1040. case 'L' :
  1041. #ifdef HAVE_ALPN
  1042. alpnList = myoptarg;
  1043. if (alpnList[0] == 'C' && alpnList[1] == ':')
  1044. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  1045. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  1046. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  1047. else {
  1048. Usage();
  1049. exit(MY_EX_USAGE);
  1050. }
  1051. alpnList += 2;
  1052. #endif
  1053. break;
  1054. case 'M' :
  1055. doSTARTTLS = 1;
  1056. starttlsProt = myoptarg;
  1057. if (XSTRNCMP(starttlsProt, "smtp", 4) != 0) {
  1058. Usage();
  1059. exit(MY_EX_USAGE);
  1060. }
  1061. break;
  1062. case 'q' :
  1063. #ifdef HAVE_WNR
  1064. wnrConfigFile = myoptarg;
  1065. #endif
  1066. break;
  1067. case 'J' :
  1068. #ifdef WOLFSSL_TLS13
  1069. helloRetry = 1;
  1070. #endif
  1071. break;
  1072. case 'K' :
  1073. #ifdef WOLFSSL_TLS13
  1074. noPskDheKe = 1;
  1075. #endif
  1076. break;
  1077. case 'I' :
  1078. #ifdef WOLFSSL_TLS13
  1079. updateKeysIVs = 1;
  1080. #endif
  1081. break;
  1082. case 'y' :
  1083. #if defined(WOLFSSL_TLS13) && !defined(NO_DH)
  1084. onlyKeyShare = 1;
  1085. #endif
  1086. break;
  1087. case 'Y' :
  1088. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
  1089. onlyKeyShare = 2;
  1090. #endif
  1091. break;
  1092. case 'j' :
  1093. useVerifyCb = 1;
  1094. break;
  1095. case 't' :
  1096. #ifdef HAVE_CURVE25519
  1097. useX25519 = 1;
  1098. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
  1099. onlyKeyShare = 2;
  1100. #endif
  1101. #endif
  1102. break;
  1103. case 'Q' :
  1104. #if defined(WOLFSSL_TLS13) && \
  1105. defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1106. postHandAuth = 1;
  1107. #endif
  1108. break;
  1109. case '0' :
  1110. #ifdef WOLFSSL_EARLY_DATA
  1111. earlyData = 1;
  1112. #endif
  1113. break;
  1114. case '3' :
  1115. #ifdef WOLFSSL_MULTICAST
  1116. doMcast = 1;
  1117. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  1118. #endif
  1119. break;
  1120. default:
  1121. Usage();
  1122. exit(MY_EX_USAGE);
  1123. }
  1124. }
  1125. myoptind = 0; /* reset for test cases */
  1126. #endif /* !WOLFSSL_VXWORKS */
  1127. if (externalTest) {
  1128. /* detect build cases that wouldn't allow test against wolfssl.com */
  1129. int done = 0;
  1130. #ifdef NO_RSA
  1131. done += 1;
  1132. #endif
  1133. /* www.globalsign.com does not respond to ipv6 ocsp requests */
  1134. #if defined(TEST_IPV6) && defined(HAVE_OCSP)
  1135. done += 1;
  1136. #endif
  1137. /* www.globalsign.com has limited supported cipher suites */
  1138. #if defined(NO_AES) && defined(HAVE_OCSP)
  1139. done += 1;
  1140. #endif
  1141. /* www.globalsign.com only supports static RSA or ECDHE with AES */
  1142. /* We cannot expect users to have on static RSA so test for ECC only
  1143. * as some users will most likely be on 32-bit systems where ECC
  1144. * is not enabled by default */
  1145. #if defined(HAVE_OCSP) && !defined(HAVE_ECC)
  1146. done += 1;
  1147. #endif
  1148. #ifndef NO_PSK
  1149. done += 1;
  1150. #endif
  1151. #ifdef NO_SHA
  1152. done += 1; /* external cert chain most likely has SHA */
  1153. #endif
  1154. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  1155. || ( defined(HAVE_ECC) && !defined(HAVE_SUPPORTED_CURVES) \
  1156. && !defined(WOLFSSL_STATIC_RSA) )
  1157. /* google needs ECDHE+Supported Curves or static RSA */
  1158. if (!XSTRNCMP(domain, "www.google.com", 14))
  1159. done += 1;
  1160. #endif
  1161. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA)
  1162. /* wolfssl needs ECDHE or static RSA */
  1163. if (!XSTRNCMP(domain, "www.wolfssl.com", 15))
  1164. done += 1;
  1165. #endif
  1166. #if !defined(WOLFSSL_SHA384)
  1167. if (!XSTRNCMP(domain, "www.wolfssl.com", 15)) {
  1168. /* wolfssl need sha384 for cert chain verify */
  1169. done += 1;
  1170. }
  1171. #endif
  1172. #if !defined(HAVE_AESGCM) && defined(NO_AES) && \
  1173. !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  1174. /* need at least on of these for external tests */
  1175. done += 1;
  1176. #endif
  1177. #if defined(HAVE_QSH)
  1178. /*currently google server rejects client hello with QSH extension.*/
  1179. done += 1;
  1180. #endif
  1181. if (done) {
  1182. printf("external test can't be run in this mode");
  1183. ((func_args*)args)->return_code = 0;
  1184. exit(EXIT_SUCCESS);
  1185. }
  1186. }
  1187. /* sort out DTLS versus TLS versions */
  1188. if (version == CLIENT_INVALID_VERSION) {
  1189. if (doDTLS)
  1190. version = CLIENT_DTLS_DEFAULT_VERSION;
  1191. else
  1192. version = CLIENT_DEFAULT_VERSION;
  1193. }
  1194. else {
  1195. if (doDTLS) {
  1196. if (version == 3)
  1197. version = -2;
  1198. else
  1199. version = -1;
  1200. }
  1201. }
  1202. #ifdef HAVE_WNR
  1203. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  1204. err_sys("can't load whitewood net random config file");
  1205. #endif
  1206. switch (version) {
  1207. #ifndef NO_OLD_TLS
  1208. #ifdef WOLFSSL_ALLOW_SSLV3
  1209. case 0:
  1210. method = wolfSSLv3_client_method_ex;
  1211. break;
  1212. #endif
  1213. #ifndef NO_TLS
  1214. #ifdef WOLFSSL_ALLOW_TLSV10
  1215. case 1:
  1216. method = wolfTLSv1_client_method_ex;
  1217. break;
  1218. #endif
  1219. case 2:
  1220. method = wolfTLSv1_1_client_method_ex;
  1221. break;
  1222. #endif /* !NO_TLS */
  1223. #endif /* !NO_OLD_TLS */
  1224. #ifndef NO_TLS
  1225. case 3:
  1226. method = wolfTLSv1_2_client_method_ex;
  1227. break;
  1228. #ifdef WOLFSSL_TLS13
  1229. case 4:
  1230. method = wolfTLSv1_3_client_method_ex;
  1231. break;
  1232. #endif
  1233. #endif /* NO_TLS */
  1234. #ifdef WOLFSSL_DTLS
  1235. #ifndef NO_OLD_TLS
  1236. case -1:
  1237. method = wolfDTLSv1_client_method_ex;
  1238. break;
  1239. #endif
  1240. case -2:
  1241. method = wolfDTLSv1_2_client_method_ex;
  1242. break;
  1243. #endif
  1244. default:
  1245. err_sys("Bad SSL version");
  1246. break;
  1247. }
  1248. if (method == NULL)
  1249. err_sys("unable to get method");
  1250. #ifdef WOLFSSL_STATIC_MEMORY
  1251. #ifdef DEBUG_WOLFSSL
  1252. /* print off helper buffer sizes for use with static memory
  1253. * printing to stderr incase of debug mode turned on */
  1254. fprintf(stderr, "static memory management size = %d\n",
  1255. wolfSSL_MemoryPaddingSz());
  1256. fprintf(stderr, "calculated optimum general buffer size = %d\n",
  1257. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  1258. fprintf(stderr, "calculated optimum IO buffer size = %d\n",
  1259. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  1260. WOLFMEM_IO_POOL_FIXED));
  1261. #endif /* DEBUG_WOLFSSL */
  1262. if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),
  1263. 0, 1) != WOLFSSL_SUCCESS) {
  1264. err_sys("unable to load static memory");
  1265. }
  1266. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  1267. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) != WOLFSSL_SUCCESS) {
  1268. err_sys("unable to load static memory");
  1269. }
  1270. #else
  1271. ctx = wolfSSL_CTX_new(method(NULL));
  1272. #endif
  1273. if (ctx == NULL)
  1274. err_sys("unable to get ctx");
  1275. #ifdef SINGLE_THREADED
  1276. if (wolfSSL_CTX_new_rng(ctx) != WOLFSSL_SUCCESS) {
  1277. wolfSSL_CTX_free(ctx);
  1278. err_sys("Single Threaded new rng at CTX failed");
  1279. }
  1280. #endif
  1281. if (cipherList && !useDefCipherList) {
  1282. if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS) {
  1283. wolfSSL_CTX_free(ctx);
  1284. err_sys("client can't set cipher list 1");
  1285. }
  1286. }
  1287. #ifdef WOLFSSL_LEANPSK
  1288. if (!usePsk) {
  1289. usePsk = 1;
  1290. }
  1291. #endif
  1292. #if defined(NO_RSA) && !defined(HAVE_ECC)
  1293. if (!usePsk) {
  1294. usePsk = 1;
  1295. }
  1296. #endif
  1297. if (fewerPackets)
  1298. wolfSSL_CTX_set_group_messages(ctx);
  1299. #ifndef NO_DH
  1300. wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits);
  1301. #endif
  1302. if (usePsk) {
  1303. #ifndef NO_PSK
  1304. wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  1305. if (cipherList == NULL) {
  1306. const char *defaultCipherList;
  1307. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  1308. #ifdef WOLFSSL_TLS13
  1309. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256:"
  1310. "TLS13-AES128-GCM-SHA256";
  1311. #else
  1312. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  1313. #endif
  1314. #elif defined(HAVE_NULL_CIPHER)
  1315. defaultCipherList = "PSK-NULL-SHA256";
  1316. #else
  1317. defaultCipherList = "PSK-AES128-CBC-SHA256";
  1318. #endif
  1319. if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList)
  1320. !=WOLFSSL_SUCCESS) {
  1321. wolfSSL_CTX_free(ctx);
  1322. err_sys("client can't set cipher list 2");
  1323. }
  1324. }
  1325. #endif
  1326. if (useClientCert) {
  1327. useClientCert = 0;
  1328. }
  1329. }
  1330. if (useAnon) {
  1331. #ifdef HAVE_ANON
  1332. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  1333. wolfSSL_CTX_allow_anon_cipher(ctx);
  1334. if (wolfSSL_CTX_set_cipher_list(ctx,"ADH-AES128-SHA")
  1335. != WOLFSSL_SUCCESS) {
  1336. wolfSSL_CTX_free(ctx);
  1337. err_sys("client can't set cipher list 4");
  1338. }
  1339. }
  1340. #endif
  1341. if (useClientCert) {
  1342. useClientCert = 0;
  1343. }
  1344. }
  1345. #ifdef WOLFSSL_SCTP
  1346. if (dtlsSCTP)
  1347. wolfSSL_CTX_dtls_set_sctp(ctx);
  1348. #endif
  1349. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1350. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1351. #endif
  1352. #if defined(WOLFSSL_SNIFFER)
  1353. if (cipherList == NULL) {
  1354. /* don't use EDH, can't sniff tmp keys */
  1355. if (wolfSSL_CTX_set_cipher_list(ctx, "AES128-SHA") != WOLFSSL_SUCCESS) {
  1356. wolfSSL_CTX_free(ctx);
  1357. err_sys("client can't set cipher list 3");
  1358. }
  1359. }
  1360. #endif
  1361. #ifdef HAVE_OCSP
  1362. if (useOcsp) {
  1363. #ifdef HAVE_IO_TIMEOUT
  1364. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  1365. #endif
  1366. if (ocspUrl != NULL) {
  1367. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  1368. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  1369. | WOLFSSL_OCSP_URL_OVERRIDE);
  1370. }
  1371. else {
  1372. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL);
  1373. }
  1374. #ifdef WOLFSSL_NONBLOCK_OCSP
  1375. wolfSSL_CTX_SetOCSP_Cb(ctx, OCSPIOCb, OCSPRespFreeCb, NULL);
  1376. #endif
  1377. }
  1378. #endif
  1379. #ifdef USER_CA_CB
  1380. wolfSSL_CTX_SetCACb(ctx, CaCb);
  1381. #endif
  1382. #if !defined(NO_CERTS)
  1383. if (useClientCert){
  1384. #if !defined(NO_FILESYSTEM)
  1385. if (wolfSSL_CTX_use_certificate_chain_file(ctx, ourCert)
  1386. != WOLFSSL_SUCCESS) {
  1387. wolfSSL_CTX_free(ctx);
  1388. err_sys("can't load client cert file, check file and run from"
  1389. " wolfSSL home dir");
  1390. }
  1391. if (wolfSSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  1392. != WOLFSSL_SUCCESS) {
  1393. wolfSSL_CTX_free(ctx);
  1394. err_sys("can't load client private key file, check file and run "
  1395. "from wolfSSL home dir");
  1396. }
  1397. #else
  1398. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  1399. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  1400. #endif /* !defined(NO_FILESYSTEM) */
  1401. }
  1402. /* for testing only - use client cert as CA to force no signer error */
  1403. if (useBadCert) {
  1404. #if !defined(NO_RSA)
  1405. verifyCert = "./certs/client-cert.pem";
  1406. #elif defined(HAVE_ECC)
  1407. verifyCert = "./certs/client-ecc-cert.pem";
  1408. #endif
  1409. }
  1410. if (!usePsk && !useAnon && !useVerifyCb) {
  1411. #if !defined(NO_FILESYSTEM)
  1412. if (wolfSSL_CTX_load_verify_locations(ctx, verifyCert,0)
  1413. != WOLFSSL_SUCCESS) {
  1414. wolfSSL_CTX_free(ctx);
  1415. err_sys("can't load ca file, Please run from wolfSSL home dir");
  1416. }
  1417. #else
  1418. load_buffer(ctx, verifyCert, WOLFSSL_CA);
  1419. #endif /* !defined(NO_FILESYSTEM) */
  1420. #ifdef HAVE_ECC
  1421. /* load ecc verify too, echoserver uses it by default w/ ecc */
  1422. #if !defined(NO_FILESYSTEM)
  1423. if (wolfSSL_CTX_load_verify_locations(ctx, eccCertFile, 0)
  1424. != WOLFSSL_SUCCESS) {
  1425. wolfSSL_CTX_free(ctx);
  1426. err_sys("can't load ecc ca file, Please run from wolfSSL home dir");
  1427. }
  1428. #else
  1429. load_buffer(ctx, eccCertFile, WOLFSSL_CA);
  1430. #endif /* !defined(NO_FILESYSTEM) */
  1431. #endif /* HAVE_ECC */
  1432. #if defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_FILESYSTEM)
  1433. if (trustCert) {
  1434. if ((ret = wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  1435. WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
  1436. wolfSSL_CTX_free(ctx);
  1437. err_sys("can't load trusted peer cert file");
  1438. }
  1439. }
  1440. #endif /* WOLFSSL_TRUST_PEER_CERT && !NO_FILESYSTEM */
  1441. }
  1442. if (useVerifyCb)
  1443. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  1444. else if (!usePsk && !useAnon && doPeerCheck == 0)
  1445. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
  1446. else if (!usePsk && !useAnon && overrideDateErrors == 1)
  1447. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myDateCb);
  1448. #endif /* !defined(NO_CERTS) */
  1449. #ifdef WOLFSSL_ASYNC_CRYPT
  1450. ret = wolfAsync_DevOpen(&devId);
  1451. if (ret < 0) {
  1452. printf("Async device open failed\nRunning without async\n");
  1453. }
  1454. wolfSSL_CTX_UseAsync(ctx, devId);
  1455. #endif /* WOLFSSL_ASYNC_CRYPT */
  1456. #ifdef HAVE_SNI
  1457. if (sniHostName)
  1458. if (wolfSSL_CTX_UseSNI(ctx, 0, sniHostName,
  1459. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS) {
  1460. wolfSSL_CTX_free(ctx);
  1461. err_sys("UseSNI failed");
  1462. }
  1463. #endif
  1464. #ifdef HAVE_MAX_FRAGMENT
  1465. if (maxFragment)
  1466. if (wolfSSL_CTX_UseMaxFragment(ctx, maxFragment) != WOLFSSL_SUCCESS) {
  1467. wolfSSL_CTX_free(ctx);
  1468. err_sys("UseMaxFragment failed");
  1469. }
  1470. #endif
  1471. #ifdef HAVE_TRUNCATED_HMAC
  1472. if (truncatedHMAC)
  1473. if (wolfSSL_CTX_UseTruncatedHMAC(ctx) != WOLFSSL_SUCCESS) {
  1474. wolfSSL_CTX_free(ctx);
  1475. err_sys("UseTruncatedHMAC failed");
  1476. }
  1477. #endif
  1478. #ifdef HAVE_SESSION_TICKET
  1479. if (wolfSSL_CTX_UseSessionTicket(ctx) != WOLFSSL_SUCCESS) {
  1480. wolfSSL_CTX_free(ctx);
  1481. err_sys("UseSessionTicket failed");
  1482. }
  1483. #endif
  1484. #ifdef HAVE_EXTENDED_MASTER
  1485. if (disableExtMasterSecret)
  1486. if (wolfSSL_CTX_DisableExtendedMasterSecret(ctx) != WOLFSSL_SUCCESS) {
  1487. wolfSSL_CTX_free(ctx);
  1488. err_sys("DisableExtendedMasterSecret failed");
  1489. }
  1490. #endif
  1491. #if defined(HAVE_CURVE25519) && defined(HAVE_SUPPORTED_CURVES)
  1492. if (useX25519) {
  1493. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X25519)
  1494. != WOLFSSL_SUCCESS) {
  1495. err_sys("unable to support X25519");
  1496. }
  1497. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1)
  1498. != WOLFSSL_SUCCESS) {
  1499. err_sys("unable to support secp256r1");
  1500. }
  1501. }
  1502. #endif /* HAVE_CURVE25519 && HAVE_SUPPORTED_CURVES */
  1503. if (benchmark) {
  1504. ((func_args*)args)->return_code =
  1505. ClientBenchmarkConnections(ctx, host, port, dtlsUDP, dtlsSCTP,
  1506. benchmark, resumeSession, useX25519,
  1507. helloRetry);
  1508. wolfSSL_CTX_free(ctx);
  1509. exit(EXIT_SUCCESS);
  1510. }
  1511. if(throughput) {
  1512. ((func_args*)args)->return_code =
  1513. ClientBenchmarkThroughput(ctx, host, port, dtlsUDP, dtlsSCTP,
  1514. throughput, useX25519);
  1515. wolfSSL_CTX_free(ctx);
  1516. exit(EXIT_SUCCESS);
  1517. }
  1518. #if defined(WOLFSSL_MDK_ARM)
  1519. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
  1520. #endif
  1521. #if defined(OPENSSL_EXTRA)
  1522. if (wolfSSL_CTX_get_read_ahead(ctx) != 0) {
  1523. wolfSSL_CTX_free(ctx);
  1524. err_sys("bad read ahead default value");
  1525. }
  1526. if (wolfSSL_CTX_set_read_ahead(ctx, 1) != WOLFSSL_SUCCESS) {
  1527. wolfSSL_CTX_free(ctx);
  1528. err_sys("error setting read ahead value");
  1529. }
  1530. #endif
  1531. #ifdef WOLFSSL_TLS13
  1532. if (noPskDheKe)
  1533. wolfSSL_CTX_no_dhe_psk(ctx);
  1534. #endif
  1535. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1536. if (postHandAuth)
  1537. wolfSSL_CTX_allow_post_handshake_auth(ctx);
  1538. #endif
  1539. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  1540. fprintf(stderr, "Before creating SSL\n");
  1541. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  1542. err_sys("ctx not using static memory");
  1543. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  1544. err_sys("error printing out memory stats");
  1545. #endif
  1546. if (doMcast) {
  1547. #ifdef WOLFSSL_MULTICAST
  1548. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  1549. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  1550. != WOLFSSL_SUCCESS) {
  1551. wolfSSL_CTX_free(ctx);
  1552. err_sys("Couldn't set multicast cipher list.");
  1553. }
  1554. #endif
  1555. }
  1556. ssl = wolfSSL_new(ctx);
  1557. if (ssl == NULL) {
  1558. wolfSSL_CTX_free(ctx);
  1559. err_sys("unable to get SSL object");
  1560. }
  1561. #ifdef OPENSSL_EXTRA
  1562. wolfSSL_KeepArrays(ssl);
  1563. #endif
  1564. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  1565. fprintf(stderr, "After creating SSL\n");
  1566. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  1567. err_sys("ctx not using static memory");
  1568. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  1569. err_sys("error printing out memory stats");
  1570. #endif
  1571. #ifdef WOLFSSL_TLS13
  1572. if (!helloRetry) {
  1573. if (onlyKeyShare == 0 || onlyKeyShare == 2) {
  1574. #ifdef HAVE_CURVE25519
  1575. if (useX25519) {
  1576. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  1577. != WOLFSSL_SUCCESS) {
  1578. err_sys("unable to use curve x25519");
  1579. }
  1580. }
  1581. #endif
  1582. #ifdef HAVE_ECC
  1583. #if defined(HAVE_ECC256) || defined(HAVE_ALL_CURVES)
  1584. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1)
  1585. != WOLFSSL_SUCCESS) {
  1586. err_sys("unable to use curve secp256r1");
  1587. }
  1588. #endif
  1589. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  1590. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP384R1)
  1591. != WOLFSSL_SUCCESS) {
  1592. err_sys("unable to use curve secp384r1");
  1593. }
  1594. #endif
  1595. #endif
  1596. }
  1597. if (onlyKeyShare == 0 || onlyKeyShare == 1) {
  1598. #ifdef HAVE_FFDHE_2048
  1599. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048)
  1600. != WOLFSSL_SUCCESS) {
  1601. err_sys("unable to use DH 2048-bit parameters");
  1602. }
  1603. #endif
  1604. }
  1605. }
  1606. else {
  1607. wolfSSL_NoKeyShares(ssl);
  1608. }
  1609. #endif
  1610. if (doMcast) {
  1611. #ifdef WOLFSSL_MULTICAST
  1612. byte pms[512]; /* pre master secret */
  1613. byte cr[32]; /* client random */
  1614. byte sr[32]; /* server random */
  1615. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  1616. XMEMSET(pms, 0x23, sizeof(pms));
  1617. XMEMSET(cr, 0xA5, sizeof(cr));
  1618. XMEMSET(sr, 0x5A, sizeof(sr));
  1619. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  1620. != WOLFSSL_SUCCESS) {
  1621. wolfSSL_CTX_free(ctx);
  1622. err_sys("unable to set mcast secret");
  1623. }
  1624. #endif
  1625. }
  1626. #ifdef HAVE_SESSION_TICKET
  1627. wolfSSL_set_SessionTicket_cb(ssl, sessionTicketCB, (void*)"initial session");
  1628. #endif
  1629. #ifdef HAVE_ALPN
  1630. if (alpnList != NULL) {
  1631. printf("ALPN accepted protocols list : %s\n", alpnList);
  1632. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  1633. }
  1634. #endif
  1635. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  1636. if (statusRequest) {
  1637. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS)
  1638. err_sys("can't enable OCSP Stapling Certificate Manager");
  1639. switch (statusRequest) {
  1640. case WOLFSSL_CSR_OCSP:
  1641. if (wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR_OCSP,
  1642. WOLFSSL_CSR_OCSP_USE_NONCE) != WOLFSSL_SUCCESS) {
  1643. wolfSSL_free(ssl);
  1644. wolfSSL_CTX_free(ctx);
  1645. err_sys("UseCertificateStatusRequest failed");
  1646. }
  1647. break;
  1648. }
  1649. wolfSSL_CTX_EnableOCSP(ctx, 0);
  1650. }
  1651. #endif
  1652. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  1653. if (statusRequest) {
  1654. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS)
  1655. err_sys("can't enable OCSP Stapling Certificate Manager");
  1656. switch (statusRequest) {
  1657. case WOLFSSL_CSR2_OCSP:
  1658. if (wolfSSL_UseOCSPStaplingV2(ssl,
  1659. WOLFSSL_CSR2_OCSP, WOLFSSL_CSR2_OCSP_USE_NONCE)
  1660. != WOLFSSL_SUCCESS) {
  1661. wolfSSL_free(ssl);
  1662. wolfSSL_CTX_free(ctx);
  1663. err_sys("UseCertificateStatusRequest failed");
  1664. }
  1665. break;
  1666. case WOLFSSL_CSR2_OCSP_MULTI:
  1667. if (wolfSSL_UseOCSPStaplingV2(ssl,
  1668. WOLFSSL_CSR2_OCSP_MULTI, 0)
  1669. != WOLFSSL_SUCCESS) {
  1670. wolfSSL_free(ssl);
  1671. wolfSSL_CTX_free(ctx);
  1672. err_sys("UseCertificateStatusRequest failed");
  1673. }
  1674. break;
  1675. }
  1676. wolfSSL_CTX_EnableOCSP(ctx, 0);
  1677. }
  1678. #endif
  1679. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  1680. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  1681. wolfSSL_free(ssl);
  1682. wolfSSL_CTX_free(ctx);
  1683. err_sys("error in setting fd");
  1684. }
  1685. /* STARTTLS */
  1686. if (doSTARTTLS) {
  1687. if (StartTLS_Init(&sockfd) != WOLFSSL_SUCCESS) {
  1688. wolfSSL_free(ssl);
  1689. wolfSSL_CTX_free(ctx);
  1690. err_sys("error during STARTTLS protocol");
  1691. }
  1692. }
  1693. #ifdef HAVE_CRL
  1694. if (disableCRL == 0 && !useVerifyCb) {
  1695. #ifdef HAVE_IO_TIMEOUT
  1696. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  1697. #endif
  1698. if (wolfSSL_EnableCRL(ssl, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS) {
  1699. wolfSSL_free(ssl);
  1700. wolfSSL_CTX_free(ctx);
  1701. err_sys("can't enable crl check");
  1702. }
  1703. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, 0)
  1704. != WOLFSSL_SUCCESS) {
  1705. wolfSSL_free(ssl);
  1706. wolfSSL_CTX_free(ctx);
  1707. err_sys("can't load crl, check crlfile and date validity");
  1708. }
  1709. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS) {
  1710. wolfSSL_free(ssl);
  1711. wolfSSL_CTX_free(ctx);
  1712. err_sys("can't set crl callback");
  1713. }
  1714. }
  1715. #endif
  1716. #ifdef HAVE_SECURE_RENEGOTIATION
  1717. if (scr) {
  1718. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  1719. wolfSSL_free(ssl);
  1720. wolfSSL_CTX_free(ctx);
  1721. err_sys("can't enable secure renegotiation");
  1722. }
  1723. }
  1724. #endif
  1725. #ifdef ATOMIC_USER
  1726. if (atomicUser)
  1727. SetupAtomicUser(ctx, ssl);
  1728. #endif
  1729. #ifdef HAVE_PK_CALLBACKS
  1730. if (pkCallbacks)
  1731. SetupPkCallbacks(ctx, ssl);
  1732. #endif
  1733. if (matchName && doPeerCheck)
  1734. wolfSSL_check_domain_name(ssl, domain);
  1735. #ifndef WOLFSSL_CALLBACKS
  1736. if (nonBlocking) {
  1737. wolfSSL_set_using_nonblock(ssl, 1);
  1738. tcp_set_nonblocking(&sockfd);
  1739. ret = NonBlockingSSL_Connect(ssl);
  1740. }
  1741. else {
  1742. do {
  1743. err = 0; /* reset error */
  1744. ret = wolfSSL_connect(ssl);
  1745. if (ret != WOLFSSL_SUCCESS) {
  1746. err = wolfSSL_get_error(ssl, 0);
  1747. #ifdef WOLFSSL_ASYNC_CRYPT
  1748. if (err == WC_PENDING_E) {
  1749. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1750. if (ret < 0) break;
  1751. }
  1752. #endif
  1753. }
  1754. } while (err == WC_PENDING_E);
  1755. }
  1756. #else
  1757. timeout.tv_sec = DEFAULT_TIMEOUT_SEC;
  1758. timeout.tv_usec = 0;
  1759. ret = NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  1760. #endif
  1761. if (ret != WOLFSSL_SUCCESS) {
  1762. printf("wolfSSL_connect error %d, %s\n", err,
  1763. wolfSSL_ERR_error_string(err, buffer));
  1764. wolfSSL_free(ssl);
  1765. wolfSSL_CTX_free(ctx);
  1766. err_sys("wolfSSL_connect failed");
  1767. /* see note at top of README */
  1768. /* if you're getting an error here */
  1769. }
  1770. showPeer(ssl);
  1771. #ifdef OPENSSL_EXTRA
  1772. {
  1773. byte* rnd;
  1774. byte* pt;
  1775. size_t size;
  1776. /* get size of buffer then print */
  1777. size = wolfSSL_get_client_random(NULL, NULL, 0);
  1778. if (size == 0) {
  1779. wolfSSL_free(ssl);
  1780. wolfSSL_CTX_free(ctx);
  1781. err_sys("error getting client random buffer size");
  1782. }
  1783. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1784. if (rnd == NULL) {
  1785. wolfSSL_free(ssl);
  1786. wolfSSL_CTX_free(ctx);
  1787. err_sys("error creating client random buffer");
  1788. }
  1789. size = wolfSSL_get_client_random(ssl, rnd, size);
  1790. if (size == 0) {
  1791. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1792. wolfSSL_free(ssl);
  1793. wolfSSL_CTX_free(ctx);
  1794. err_sys("error getting client random buffer");
  1795. }
  1796. printf("Client Random : ");
  1797. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  1798. printf("\n");
  1799. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1800. }
  1801. #endif
  1802. if (doSTARTTLS) {
  1803. if (XSTRNCMP(starttlsProt, "smtp", 4) == 0) {
  1804. if (SMTP_Shutdown(ssl, wc_shutdown) != WOLFSSL_SUCCESS) {
  1805. wolfSSL_free(ssl);
  1806. wolfSSL_CTX_free(ctx);
  1807. err_sys("error closing STARTTLS connection");
  1808. }
  1809. }
  1810. wolfSSL_free(ssl);
  1811. CloseSocket(sockfd);
  1812. wolfSSL_CTX_free(ctx);
  1813. ((func_args*)args)->return_code = 0;
  1814. return 0;
  1815. }
  1816. #ifdef HAVE_ALPN
  1817. if (alpnList != NULL) {
  1818. char *protocol_name = NULL;
  1819. word16 protocol_nameSz = 0;
  1820. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name, &protocol_nameSz);
  1821. if (err == WOLFSSL_SUCCESS)
  1822. printf("Received ALPN protocol : %s (%d)\n",
  1823. protocol_name, protocol_nameSz);
  1824. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  1825. printf("No ALPN response received (no match with server)\n");
  1826. else
  1827. printf("Getting ALPN protocol name failed\n");
  1828. }
  1829. #endif
  1830. #ifdef HAVE_SECURE_RENEGOTIATION
  1831. if (scr && forceScr) {
  1832. if (nonBlocking) {
  1833. printf("not doing secure renegotiation on example with"
  1834. " nonblocking yet");
  1835. } else {
  1836. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  1837. err = wolfSSL_get_error(ssl, 0);
  1838. printf("err = %d, %s\n", err,
  1839. wolfSSL_ERR_error_string(err, buffer));
  1840. wolfSSL_free(ssl);
  1841. wolfSSL_CTX_free(ctx);
  1842. err_sys("wolfSSL_Rehandshake failed");
  1843. }
  1844. }
  1845. }
  1846. #endif /* HAVE_SECURE_RENEGOTIATION */
  1847. if (sendGET) {
  1848. printf("SSL connect ok, sending GET...\n");
  1849. msgSz = 28;
  1850. strncpy(msg, "GET /index.html HTTP/1.0\r\n\r\n", msgSz);
  1851. msg[msgSz] = '\0';
  1852. resumeSz = msgSz;
  1853. strncpy(resumeMsg, "GET /index.html HTTP/1.0\r\n\r\n", resumeSz);
  1854. resumeMsg[resumeSz] = '\0';
  1855. }
  1856. /* allow some time for exporting the session */
  1857. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  1858. #ifdef USE_WINDOWS_API
  1859. Sleep(500);
  1860. #elif defined(WOLFSSL_TIRTOS)
  1861. Task_sleep(1);
  1862. #else
  1863. sleep(1);
  1864. #endif
  1865. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  1866. #ifdef WOLFSSL_TLS13
  1867. if (updateKeysIVs)
  1868. wolfSSL_update_keys(ssl);
  1869. #endif
  1870. ClientWrite(ssl, msg, msgSz);
  1871. ClientRead(ssl, reply, sizeof(reply)-1, 1);
  1872. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1873. if (postHandAuth)
  1874. ClientWrite(ssl, msg, msgSz);
  1875. #endif
  1876. if (sendGET) { /* get html */
  1877. ClientRead(ssl, reply, sizeof(reply)-1, 0);
  1878. }
  1879. #ifndef NO_SESSION_CACHE
  1880. if (resumeSession) {
  1881. session = wolfSSL_get_session(ssl);
  1882. }
  1883. #endif
  1884. if (dtlsUDP == 0) { /* don't send alert after "break" command */
  1885. ret = wolfSSL_shutdown(ssl);
  1886. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  1887. wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  1888. }
  1889. #ifdef ATOMIC_USER
  1890. if (atomicUser)
  1891. FreeAtomicUser(ssl);
  1892. #endif
  1893. /* display collected statistics */
  1894. #ifdef WOLFSSL_STATIC_MEMORY
  1895. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  1896. err_sys("static memory was not used with ssl");
  1897. fprintf(stderr, "\nprint off SSL memory stats\n");
  1898. fprintf(stderr, "*** This is memory state before wolfSSL_free is called\n");
  1899. fprintf(stderr, "peak connection memory = %d\n", ssl_stats.peakMem);
  1900. fprintf(stderr, "current memory in use = %d\n", ssl_stats.curMem);
  1901. fprintf(stderr, "peak connection allocs = %d\n", ssl_stats.peakAlloc);
  1902. fprintf(stderr, "current connection allocs = %d\n", ssl_stats.curAlloc);
  1903. fprintf(stderr, "total connection allocs = %d\n", ssl_stats.totalAlloc);
  1904. fprintf(stderr, "total connection frees = %d\n\n", ssl_stats.totalFr);
  1905. #endif
  1906. wolfSSL_free(ssl);
  1907. CloseSocket(sockfd);
  1908. #ifndef NO_SESSION_CACHE
  1909. if (resumeSession) {
  1910. sslResume = wolfSSL_new(ctx);
  1911. if (sslResume == NULL) {
  1912. wolfSSL_CTX_free(ctx);
  1913. err_sys("unable to get SSL object");
  1914. }
  1915. if (dtlsUDP) {
  1916. #ifdef USE_WINDOWS_API
  1917. Sleep(500);
  1918. #elif defined(WOLFSSL_TIRTOS)
  1919. Task_sleep(1);
  1920. #else
  1921. sleep(1);
  1922. #endif
  1923. }
  1924. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, sslResume);
  1925. if (wolfSSL_set_fd(sslResume, sockfd) != WOLFSSL_SUCCESS) {
  1926. wolfSSL_free(sslResume);
  1927. wolfSSL_CTX_free(ctx);
  1928. err_sys("error in setting fd");
  1929. }
  1930. #ifdef HAVE_ALPN
  1931. if (alpnList != NULL) {
  1932. printf("ALPN accepted protocols list : %s\n", alpnList);
  1933. wolfSSL_UseALPN(sslResume, alpnList, (word32)XSTRLEN(alpnList),
  1934. alpn_opt);
  1935. }
  1936. #endif
  1937. #ifdef HAVE_SECURE_RENEGOTIATION
  1938. if (scr) {
  1939. if (wolfSSL_UseSecureRenegotiation(sslResume) != WOLFSSL_SUCCESS) {
  1940. wolfSSL_free(sslResume);
  1941. wolfSSL_CTX_free(ctx);
  1942. err_sys("can't enable secure renegotiation");
  1943. }
  1944. }
  1945. #endif
  1946. wolfSSL_set_session(sslResume, session);
  1947. #ifdef HAVE_SESSION_TICKET
  1948. wolfSSL_set_SessionTicket_cb(sslResume, sessionTicketCB,
  1949. (void*)"resumed session");
  1950. #endif
  1951. #ifdef WOLFSSL_TLS13
  1952. #ifdef HAVE_CURVE25519
  1953. if (useX25519) {
  1954. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519) != WOLFSSL_SUCCESS) {
  1955. err_sys("unable to use curve x25519");
  1956. }
  1957. }
  1958. #endif
  1959. #ifdef HAVE_ECC
  1960. if (wolfSSL_UseKeyShare(sslResume,
  1961. WOLFSSL_ECC_SECP256R1) != WOLFSSL_SUCCESS) {
  1962. err_sys("unable to use curve secp256r1");
  1963. }
  1964. if (wolfSSL_UseKeyShare(sslResume,
  1965. WOLFSSL_ECC_SECP384R1) != WOLFSSL_SUCCESS) {
  1966. err_sys("unable to use curve secp384r1");
  1967. }
  1968. #endif
  1969. #ifdef HAVE_FFDHE_2048
  1970. if (wolfSSL_UseKeyShare(sslResume, WOLFSSL_FFDHE_2048) != WOLFSSL_SUCCESS) {
  1971. err_sys("unable to use DH 2048-bit parameters");
  1972. }
  1973. #endif
  1974. #endif
  1975. #ifndef WOLFSSL_CALLBACKS
  1976. if (nonBlocking) {
  1977. wolfSSL_set_using_nonblock(sslResume, 1);
  1978. tcp_set_nonblocking(&sockfd);
  1979. ret = NonBlockingSSL_Connect(sslResume);
  1980. }
  1981. else {
  1982. #ifdef WOLFSSL_EARLY_DATA
  1983. #ifndef HAVE_SESSION_TICKET
  1984. if (!usePsk) {
  1985. }
  1986. else
  1987. #endif
  1988. if (earlyData) {
  1989. do {
  1990. err = 0; /* reset error */
  1991. ret = wolfSSL_write_early_data(sslResume, msg, msgSz,
  1992. &msgSz);
  1993. if (ret <= 0) {
  1994. err = wolfSSL_get_error(sslResume, 0);
  1995. #ifdef WOLFSSL_ASYNC_CRYPT
  1996. if (err == WC_PENDING_E) {
  1997. ret = wolfSSL_AsyncPoll(sslResume,
  1998. WOLF_POLL_FLAG_CHECK_HW);
  1999. if (ret < 0) break;
  2000. }
  2001. #endif
  2002. }
  2003. } while (err == WC_PENDING_E);
  2004. if (ret != msgSz) {
  2005. printf("SSL_write_early_data msg error %d, %s\n", err,
  2006. wolfSSL_ERR_error_string(err, buffer));
  2007. wolfSSL_free(sslResume);
  2008. wolfSSL_CTX_free(ctx);
  2009. err_sys("SSL_write_early_data failed");
  2010. }
  2011. do {
  2012. err = 0; /* reset error */
  2013. ret = wolfSSL_write_early_data(sslResume, msg, msgSz,
  2014. &msgSz);
  2015. if (ret <= 0) {
  2016. err = wolfSSL_get_error(sslResume, 0);
  2017. #ifdef WOLFSSL_ASYNC_CRYPT
  2018. if (err == WC_PENDING_E) {
  2019. ret = wolfSSL_AsyncPoll(sslResume,
  2020. WOLF_POLL_FLAG_CHECK_HW);
  2021. if (ret < 0) break;
  2022. }
  2023. #endif
  2024. }
  2025. } while (err == WC_PENDING_E);
  2026. if (ret != msgSz) {
  2027. printf("SSL_write_early_data msg error %d, %s\n", err,
  2028. wolfSSL_ERR_error_string(err, buffer));
  2029. wolfSSL_free(sslResume);
  2030. wolfSSL_CTX_free(ctx);
  2031. err_sys("SSL_write_early_data failed");
  2032. }
  2033. }
  2034. #endif
  2035. do {
  2036. err = 0; /* reset error */
  2037. ret = wolfSSL_connect(sslResume);
  2038. if (ret != WOLFSSL_SUCCESS) {
  2039. err = wolfSSL_get_error(sslResume, 0);
  2040. #ifdef WOLFSSL_ASYNC_CRYPT
  2041. if (err == WC_PENDING_E) {
  2042. ret = wolfSSL_AsyncPoll(sslResume,
  2043. WOLF_POLL_FLAG_CHECK_HW);
  2044. if (ret < 0) break;
  2045. }
  2046. #endif
  2047. }
  2048. } while (err == WC_PENDING_E);
  2049. }
  2050. #else
  2051. timeout.tv_sec = DEFAULT_TIMEOUT_SEC;
  2052. timeout.tv_usec = 0;
  2053. ret = NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  2054. #endif
  2055. if (ret != WOLFSSL_SUCCESS) {
  2056. printf("wolfSSL_connect resume error %d, %s\n", err,
  2057. wolfSSL_ERR_error_string(err, buffer));
  2058. wolfSSL_free(sslResume);
  2059. wolfSSL_CTX_free(ctx);
  2060. err_sys("wolfSSL_connect resume failed");
  2061. }
  2062. showPeer(sslResume);
  2063. if (wolfSSL_session_reused(sslResume))
  2064. printf("reused session id\n");
  2065. else
  2066. printf("didn't reuse session id!!!\n");
  2067. #ifdef HAVE_ALPN
  2068. if (alpnList != NULL) {
  2069. char *protocol_name = NULL;
  2070. word16 protocol_nameSz = 0;
  2071. printf("Sending ALPN accepted list : %s\n", alpnList);
  2072. err = wolfSSL_ALPN_GetProtocol(sslResume, &protocol_name,
  2073. &protocol_nameSz);
  2074. if (err == WOLFSSL_SUCCESS)
  2075. printf("Received ALPN protocol : %s (%d)\n",
  2076. protocol_name, protocol_nameSz);
  2077. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  2078. printf("Not received ALPN response (no match with server)\n");
  2079. else
  2080. printf("Getting ALPN protocol name failed\n");
  2081. }
  2082. #endif
  2083. /* allow some time for exporting the session */
  2084. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  2085. #ifdef USE_WINDOWS_API
  2086. Sleep(500);
  2087. #elif defined(WOLFSSL_TIRTOS)
  2088. Task_sleep(1);
  2089. #else
  2090. sleep(1);
  2091. #endif
  2092. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  2093. do {
  2094. err = 0; /* reset error */
  2095. ret = wolfSSL_write(sslResume, resumeMsg, resumeSz);
  2096. if (ret <= 0) {
  2097. err = wolfSSL_get_error(sslResume, 0);
  2098. #ifdef WOLFSSL_ASYNC_CRYPT
  2099. if (err == WC_PENDING_E) {
  2100. ret = wolfSSL_AsyncPoll(sslResume, WOLF_POLL_FLAG_CHECK_HW);
  2101. if (ret < 0) break;
  2102. }
  2103. #endif
  2104. }
  2105. } while (err == WC_PENDING_E);
  2106. if (ret != resumeSz) {
  2107. printf("SSL_write resume error %d, %s\n", err,
  2108. wolfSSL_ERR_error_string(err, buffer));
  2109. wolfSSL_free(sslResume);
  2110. wolfSSL_CTX_free(ctx);
  2111. err_sys("SSL_write failed");
  2112. }
  2113. if (nonBlocking) {
  2114. /* give server a chance to bounce a message back to client */
  2115. #ifdef USE_WINDOWS_API
  2116. Sleep(500);
  2117. #elif defined(WOLFSSL_TIRTOS)
  2118. Task_sleep(1);
  2119. #else
  2120. sleep(1);
  2121. #endif
  2122. }
  2123. do {
  2124. err = 0; /* reset error */
  2125. ret = wolfSSL_read(sslResume, reply, sizeof(reply)-1);
  2126. if (ret <= 0) {
  2127. err = wolfSSL_get_error(sslResume, 0);
  2128. #ifdef WOLFSSL_ASYNC_CRYPT
  2129. if (err == WC_PENDING_E) {
  2130. ret = wolfSSL_AsyncPoll(sslResume, WOLF_POLL_FLAG_CHECK_HW);
  2131. if (ret < 0) break;
  2132. }
  2133. #endif
  2134. }
  2135. } while (err == WC_PENDING_E);
  2136. if (ret > 0) {
  2137. reply[ret] = 0;
  2138. printf("Server resume response: %s\n", reply);
  2139. if (sendGET) { /* get html */
  2140. while (1) {
  2141. do {
  2142. err = 0; /* reset error */
  2143. ret = wolfSSL_read(sslResume, reply, sizeof(reply)-1);
  2144. if (ret <= 0) {
  2145. err = wolfSSL_get_error(sslResume, 0);
  2146. #ifdef WOLFSSL_ASYNC_CRYPT
  2147. if (err == WC_PENDING_E) {
  2148. ret = wolfSSL_AsyncPoll(sslResume,
  2149. WOLF_POLL_FLAG_CHECK_HW);
  2150. if (ret < 0) break;
  2151. }
  2152. #endif
  2153. }
  2154. } while (err == WC_PENDING_E);
  2155. if (ret > 0) {
  2156. reply[ret] = 0;
  2157. printf("%s\n", reply);
  2158. }
  2159. else
  2160. break;
  2161. }
  2162. }
  2163. }
  2164. if (ret < 0) {
  2165. if (err != WOLFSSL_ERROR_WANT_READ) {
  2166. printf("SSL_read resume error %d, %s\n", err,
  2167. wolfSSL_ERR_error_string(err, buffer));
  2168. wolfSSL_free(sslResume);
  2169. wolfSSL_CTX_free(ctx);
  2170. err_sys("SSL_read failed");
  2171. }
  2172. }
  2173. /* try to send session break */
  2174. do {
  2175. err = 0; /* reset error */
  2176. ret = wolfSSL_write(sslResume, msg, msgSz);
  2177. if (ret <= 0) {
  2178. err = wolfSSL_get_error(sslResume, 0);
  2179. #ifdef WOLFSSL_ASYNC_CRYPT
  2180. if (err == WC_PENDING_E) {
  2181. ret = wolfSSL_AsyncPoll(sslResume, WOLF_POLL_FLAG_CHECK_HW);
  2182. if (ret < 0) break;
  2183. }
  2184. #endif
  2185. }
  2186. } while (err == WC_PENDING_E);
  2187. ret = wolfSSL_shutdown(sslResume);
  2188. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  2189. wolfSSL_shutdown(sslResume); /* bidirectional shutdown */
  2190. /* display collected statistics */
  2191. #ifdef WOLFSSL_STATIC_MEMORY
  2192. if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
  2193. err_sys("static memory was not used with ssl");
  2194. fprintf(stderr, "\nprint off SSLresume memory stats\n");
  2195. fprintf(stderr, "*** This is memory state before wolfSSL_free is called\n");
  2196. fprintf(stderr, "peak connection memory = %d\n", ssl_stats.peakMem);
  2197. fprintf(stderr, "current memory in use = %d\n", ssl_stats.curMem);
  2198. fprintf(stderr, "peak connection allocs = %d\n", ssl_stats.peakAlloc);
  2199. fprintf(stderr, "current connection allocs = %d\n", ssl_stats.curAlloc);
  2200. fprintf(stderr, "total connection allocs = %d\n", ssl_stats.totalAlloc);
  2201. fprintf(stderr, "total connection frees = %d\n\n", ssl_stats.totalFr);
  2202. #endif
  2203. wolfSSL_free(sslResume);
  2204. CloseSocket(sockfd);
  2205. }
  2206. #endif /* NO_SESSION_CACHE */
  2207. wolfSSL_CTX_free(ctx);
  2208. ((func_args*)args)->return_code = 0;
  2209. #ifdef WOLFSSL_ASYNC_CRYPT
  2210. wolfAsync_DevClose(&devId);
  2211. #endif
  2212. /* There are use cases when these assignments are not read. To avoid
  2213. * potential confusion those warnings have been handled here.
  2214. */
  2215. (void) overrideDateErrors;
  2216. (void) useClientCert;
  2217. (void) verifyCert;
  2218. (void) ourCert;
  2219. (void) ourKey;
  2220. (void) useVerifyCb;
  2221. #if !defined(WOLFSSL_TIRTOS)
  2222. return 0;
  2223. #endif
  2224. }
  2225. #endif /* !NO_WOLFSSL_CLIENT */
  2226. /* so overall tests can pull in test function */
  2227. #ifndef NO_MAIN_DRIVER
  2228. int main(int argc, char** argv)
  2229. {
  2230. func_args args;
  2231. StartTCP();
  2232. args.argc = argc;
  2233. args.argv = argv;
  2234. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL) && !defined(STACK_TRAP)
  2235. wolfSSL_Debugging_ON();
  2236. #endif
  2237. wolfSSL_Init();
  2238. ChangeToWolfRoot();
  2239. #ifndef NO_WOLFSSL_CLIENT
  2240. #ifdef HAVE_STACK_SIZE
  2241. StackSizeCheck(&args, client_test);
  2242. #else
  2243. client_test(&args);
  2244. #endif
  2245. #endif
  2246. wolfSSL_Cleanup();
  2247. #ifdef HAVE_WNR
  2248. if (wc_FreeNetRandom() < 0)
  2249. err_sys("Failed to free netRandom context");
  2250. #endif /* HAVE_WNR */
  2251. return args.return_code;
  2252. }
  2253. int myoptind = 0;
  2254. char* myoptarg = NULL;
  2255. #endif /* NO_MAIN_DRIVER */