client.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547
  1. /* client.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/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 "rl_fs.h"
  30. #include "rl_net.h"
  31. #endif
  32. #include <wolfssl/test.h>
  33. #include <examples/client/client.h>
  34. #include <wolfssl/error-ssl.h>
  35. #ifndef NO_WOLFSSL_CLIENT
  36. #ifdef USE_FAST_MATH
  37. /* included to inspect the size of FP_MAX_BITS */
  38. /* need integer.h header to make sure right math version used */
  39. #include <wolfssl/wolfcrypt/integer.h>
  40. #endif
  41. #ifdef HAVE_ECC
  42. #include <wolfssl/wolfcrypt/ecc.h>
  43. #endif
  44. #ifdef WOLFSSL_ASYNC_CRYPT
  45. static int devId = INVALID_DEVID;
  46. #endif
  47. #define DEFAULT_TIMEOUT_SEC 2
  48. #ifndef MAX_NON_BLOCK_SEC
  49. #define MAX_NON_BLOCK_SEC 10
  50. #endif
  51. #define OCSP_STAPLING 1
  52. #define OCSP_STAPLINGV2 2
  53. #define OCSP_STAPLINGV2_MULTI 3
  54. #define OCSP_STAPLING_OPT_MAX OCSP_STAPLINGV2_MULTI
  55. #ifdef WOLFSSL_ALT_TEST_STRINGS
  56. #define TEST_STR_TERM "\n"
  57. #else
  58. #define TEST_STR_TERM
  59. #endif
  60. static const char kHelloMsg[] = "hello wolfssl!" TEST_STR_TERM;
  61. #ifndef NO_SESSION_CACHE
  62. static const char kResumeMsg[] = "resuming wolfssl!" TEST_STR_TERM;
  63. #endif
  64. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_EARLY_DATA)
  65. static const char kEarlyMsg[] = "A drop of info" TEST_STR_TERM;
  66. #endif
  67. static const char kHttpGetMsg[] = "GET /index.html HTTP/1.0\r\n\r\n";
  68. /* Write needs to be largest of the above strings (29) */
  69. #define CLI_MSG_SZ 32
  70. /* Read needs to be at least sizeof server.c `webServerMsg` (226) */
  71. #define CLI_REPLY_SZ 256
  72. #if defined(XSLEEP_US) && defined(NO_MAIN_DRIVER)
  73. /* This is to force the server's thread to get a chance to
  74. * execute before continuing the resume in non-blocking
  75. * DTLS test cases. */
  76. #define TEST_DELAY() XSLEEP_US(10000)
  77. #else
  78. #define TEST_DELAY() XSLEEP_MS(1000)
  79. #endif
  80. /* Note on using port 0: the client standalone example doesn't utilize the
  81. * port 0 port sharing; that is used by (1) the server in external control
  82. * test mode and (2) the testsuite which uses this code and sets up the correct
  83. * port numbers when the internal thread using the server code using port 0. */
  84. static int lng_index = 0;
  85. #ifdef WOLFSSL_CALLBACKS
  86. WOLFSSL_TIMEVAL timeoutConnect;
  87. static int handShakeCB(HandShakeInfo* info)
  88. {
  89. (void)info;
  90. return 0;
  91. }
  92. static int timeoutCB(TimeoutInfo* info)
  93. {
  94. (void)info;
  95. return 0;
  96. }
  97. #endif
  98. #ifdef HAVE_SESSION_TICKET
  99. static int sessionTicketCB(WOLFSSL* ssl,
  100. const unsigned char* ticket, int ticketSz,
  101. void* ctx)
  102. {
  103. (void)ssl;
  104. (void)ticket;
  105. printf("Session Ticket CB: ticketSz = %d, ctx = %s\n",
  106. ticketSz, (char*)ctx);
  107. return 0;
  108. }
  109. #endif
  110. static int NonBlockingSSL_Connect(WOLFSSL* ssl)
  111. {
  112. int ret;
  113. int error;
  114. SOCKET_T sockfd;
  115. int select_ret = 0;
  116. int elapsedSec = 0;
  117. #ifndef WOLFSSL_CALLBACKS
  118. ret = wolfSSL_connect(ssl);
  119. #else
  120. ret = wolfSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeoutConnect);
  121. #endif
  122. error = wolfSSL_get_error(ssl, 0);
  123. sockfd = (SOCKET_T)wolfSSL_get_fd(ssl);
  124. while (ret != WOLFSSL_SUCCESS &&
  125. (error == WOLFSSL_ERROR_WANT_READ || error == WOLFSSL_ERROR_WANT_WRITE
  126. #ifdef WOLFSSL_ASYNC_CRYPT
  127. || error == WC_PENDING_E
  128. #endif
  129. #ifdef WOLFSSL_NONBLOCK_OCSP
  130. || error == OCSP_WANT_READ
  131. #endif
  132. )) {
  133. int currTimeout = 1;
  134. if (error == WOLFSSL_ERROR_WANT_READ)
  135. printf("... client would read block\n");
  136. else if (error == WOLFSSL_ERROR_WANT_WRITE)
  137. printf("... client would write block\n");
  138. #ifdef WOLFSSL_ASYNC_CRYPT
  139. if (error == WC_PENDING_E) {
  140. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  141. if (ret < 0) break;
  142. }
  143. else
  144. #endif
  145. {
  146. if (error != WOLFSSL_ERROR_WANT_WRITE) {
  147. #ifdef WOLFSSL_DTLS
  148. currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
  149. #endif
  150. select_ret = tcp_select(sockfd, currTimeout);
  151. }
  152. }
  153. if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_SEND_READY)
  154. || (select_ret == TEST_ERROR_READY)
  155. #ifdef WOLFSSL_ASYNC_CRYPT
  156. || error == WC_PENDING_E
  157. #endif
  158. ) {
  159. #ifndef WOLFSSL_CALLBACKS
  160. ret = wolfSSL_connect(ssl);
  161. #else
  162. ret = wolfSSL_connect_ex(ssl, handShakeCB, timeoutCB,
  163. timeoutConnect);
  164. #endif
  165. error = wolfSSL_get_error(ssl, 0);
  166. elapsedSec = 0; /* reset elapsed */
  167. if (error == WOLFSSL_ERROR_WANT_WRITE) {
  168. /* Do a send select here. */
  169. select_ret = tcp_select_tx(sockfd, 1);
  170. if (select_ret == TEST_TIMEOUT) {
  171. error = WOLFSSL_FATAL_ERROR;
  172. }
  173. }
  174. }
  175. else if (select_ret == TEST_TIMEOUT && !wolfSSL_dtls(ssl)) {
  176. error = WOLFSSL_ERROR_WANT_READ;
  177. elapsedSec += currTimeout;
  178. if (elapsedSec > MAX_NON_BLOCK_SEC) {
  179. printf("Nonblocking connect timeout\n");
  180. error = WOLFSSL_FATAL_ERROR;
  181. }
  182. }
  183. #ifdef WOLFSSL_DTLS
  184. else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl) &&
  185. wolfSSL_dtls_got_timeout(ssl) >= 0) {
  186. error = WOLFSSL_ERROR_WANT_READ;
  187. }
  188. #endif
  189. else {
  190. error = WOLFSSL_FATAL_ERROR;
  191. }
  192. }
  193. return ret;
  194. }
  195. static void ShowCiphers(void)
  196. {
  197. static char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  198. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  199. if (ret == WOLFSSL_SUCCESS)
  200. printf("%s\n", ciphers);
  201. }
  202. /* Shows which versions are valid */
  203. static void ShowVersions(void)
  204. {
  205. #ifndef NO_OLD_TLS
  206. #ifdef WOLFSSL_ALLOW_SSLV3
  207. printf("0:");
  208. #endif
  209. #ifdef WOLFSSL_ALLOW_TLSV10
  210. printf("1:");
  211. #endif
  212. printf("2:");
  213. #endif /* NO_OLD_TLS */
  214. #ifndef WOLFSSL_NO_TLS12
  215. printf("3:");
  216. #endif
  217. #ifdef WOLFSSL_TLS13
  218. printf("4:");
  219. #endif
  220. printf("d(downgrade):");
  221. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  222. printf("e(either):");
  223. #endif
  224. printf("\n");
  225. }
  226. #ifdef WOLFSSL_TLS13
  227. #define MAX_GROUP_NUMBER 4
  228. static void SetKeyShare(WOLFSSL* ssl, int onlyKeyShare, int useX25519,
  229. int useX448)
  230. {
  231. int groups[MAX_GROUP_NUMBER] = {0};
  232. int count = 0;
  233. (void)useX25519;
  234. (void)useX448;
  235. WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  236. if (onlyKeyShare == 0 || onlyKeyShare == 2) {
  237. #ifdef HAVE_CURVE25519
  238. if (useX25519) {
  239. groups[count++] = WOLFSSL_ECC_X25519;
  240. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519) != WOLFSSL_SUCCESS)
  241. err_sys("unable to use curve x25519");
  242. }
  243. else
  244. #endif
  245. #ifdef HAVE_CURVE448
  246. if (useX448) {
  247. groups[count++] = WOLFSSL_ECC_X448;
  248. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X448) != WOLFSSL_SUCCESS)
  249. err_sys("unable to use curve x448");
  250. }
  251. else
  252. #endif
  253. {
  254. #ifdef HAVE_ECC
  255. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  256. groups[count++] = WOLFSSL_ECC_SECP256R1;
  257. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1)
  258. != WOLFSSL_SUCCESS) {
  259. err_sys("unable to use curve secp256r1");
  260. }
  261. #endif
  262. #endif
  263. }
  264. }
  265. if (onlyKeyShare == 0 || onlyKeyShare == 1) {
  266. #ifdef HAVE_FFDHE_2048
  267. groups[count++] = WOLFSSL_FFDHE_2048;
  268. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048) != WOLFSSL_SUCCESS)
  269. err_sys("unable to use DH 2048-bit parameters");
  270. #endif
  271. }
  272. if (count >= MAX_GROUP_NUMBER)
  273. err_sys("example group array size error");
  274. if (wolfSSL_set_groups(ssl, groups, count) != WOLFSSL_SUCCESS)
  275. err_sys("unable to set groups");
  276. WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  277. }
  278. #endif
  279. #ifdef WOLFSSL_EARLY_DATA
  280. static void EarlyData(WOLFSSL_CTX* ctx, WOLFSSL* ssl, const char* msg,
  281. int msgSz, char* buffer)
  282. {
  283. int err;
  284. int ret;
  285. do {
  286. err = 0; /* reset error */
  287. ret = wolfSSL_write_early_data(ssl, msg, msgSz, &msgSz);
  288. if (ret <= 0) {
  289. err = wolfSSL_get_error(ssl, 0);
  290. #ifdef WOLFSSL_ASYNC_CRYPT
  291. if (err == WC_PENDING_E) {
  292. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  293. if (ret < 0) break;
  294. }
  295. #endif
  296. }
  297. } while (err == WC_PENDING_E);
  298. if (ret != msgSz) {
  299. printf("SSL_write_early_data msg error %d, %s\n", err,
  300. wolfSSL_ERR_error_string(err, buffer));
  301. wolfSSL_free(ssl); ssl = NULL;
  302. wolfSSL_CTX_free(ctx); ctx = NULL;
  303. err_sys("SSL_write_early_data failed");
  304. }
  305. do {
  306. err = 0; /* reset error */
  307. ret = wolfSSL_write_early_data(ssl, msg, msgSz, &msgSz);
  308. if (ret <= 0) {
  309. err = wolfSSL_get_error(ssl, 0);
  310. #ifdef WOLFSSL_ASYNC_CRYPT
  311. if (err == WC_PENDING_E) {
  312. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  313. if (ret < 0) break;
  314. }
  315. #endif
  316. }
  317. } while (err == WC_PENDING_E);
  318. if (ret != msgSz) {
  319. printf("SSL_write_early_data msg error %d, %s\n", err,
  320. wolfSSL_ERR_error_string(err, buffer));
  321. wolfSSL_free(ssl);
  322. wolfSSL_CTX_free(ctx);
  323. err_sys("SSL_write_early_data failed");
  324. }
  325. }
  326. #endif
  327. /* Measures average time to create, connect and disconnect a connection (TPS).
  328. Benchmark = number of connections. */
  329. static const char* client_bench_conmsg[][5] = {
  330. /* English */
  331. {
  332. "wolfSSL_resume avg took:", "milliseconds\n",
  333. "wolfSSL_connect avg took:", "milliseconds\n",
  334. NULL
  335. },
  336. #ifndef NO_MULTIBYTE_PRINT
  337. /* Japanese */
  338. {
  339. "wolfSSL_resume 平均時間:", "ミリ秒\n",
  340. "wolfSSL_connect 平均時間:", "ミリ秒\n",
  341. }
  342. #endif
  343. };
  344. static int ClientBenchmarkConnections(WOLFSSL_CTX* ctx, char* host, word16 port,
  345. int dtlsUDP, int dtlsSCTP, int benchmark, int resumeSession, int useX25519,
  346. int useX448, int helloRetry, int onlyKeyShare, int version, int earlyData)
  347. {
  348. /* time passed in number of connects give average */
  349. int times = benchmark, skip = times * 0.1;
  350. int loops = resumeSession ? 2 : 1;
  351. int i = 0, err, ret;
  352. #ifndef NO_SESSION_CACHE
  353. WOLFSSL_SESSION* benchSession = NULL;
  354. #endif
  355. #ifdef WOLFSSL_TLS13
  356. byte reply[CLI_REPLY_SZ];
  357. #endif
  358. const char** words = client_bench_conmsg[lng_index];
  359. (void)resumeSession;
  360. (void)useX25519;
  361. (void)useX448;
  362. (void)helloRetry;
  363. (void)onlyKeyShare;
  364. (void)version;
  365. (void)earlyData;
  366. while (loops--) {
  367. #ifndef NO_SESSION_CACHE
  368. int benchResume = resumeSession && loops == 0;
  369. #endif
  370. double start = current_time(1), avg;
  371. for (i = 0; i < times; i++) {
  372. SOCKET_T sockfd;
  373. WOLFSSL* ssl;
  374. if (i == skip)
  375. start = current_time(1);
  376. ssl = wolfSSL_new(ctx);
  377. if (ssl == NULL)
  378. err_sys("unable to get SSL object");
  379. #ifndef NO_SESSION_CACHE
  380. if (benchResume)
  381. wolfSSL_set_session(ssl, benchSession);
  382. #endif
  383. #ifdef WOLFSSL_TLS13
  384. else if (version >= 4) {
  385. if (!helloRetry)
  386. SetKeyShare(ssl, onlyKeyShare, useX25519, useX448);
  387. else
  388. wolfSSL_NoKeyShares(ssl);
  389. }
  390. #endif
  391. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  392. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  393. err_sys("error in setting fd");
  394. }
  395. #if defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  396. defined(WOLFSSL_EARLY_DATA)
  397. if (version >= 4 && benchResume && earlyData) {
  398. char buffer[WOLFSSL_MAX_ERROR_SZ];
  399. EarlyData(ctx, ssl, kEarlyMsg, sizeof(kEarlyMsg)-1, buffer);
  400. }
  401. #endif
  402. do {
  403. err = 0; /* reset error */
  404. ret = wolfSSL_connect(ssl);
  405. if (ret != WOLFSSL_SUCCESS) {
  406. err = wolfSSL_get_error(ssl, 0);
  407. #ifdef WOLFSSL_ASYNC_CRYPT
  408. if (err == WC_PENDING_E) {
  409. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  410. if (ret < 0) break;
  411. }
  412. #endif
  413. }
  414. } while (err == WC_PENDING_E);
  415. if (ret != WOLFSSL_SUCCESS) {
  416. err_sys("SSL_connect failed");
  417. }
  418. #ifdef WOLFSSL_TLS13
  419. #ifndef NO_SESSION_CACHE
  420. if (version >= 4 && resumeSession && !benchResume)
  421. #else
  422. if (version >= 4 && resumeSession)
  423. #endif
  424. {
  425. /* no null term */
  426. if (wolfSSL_write(ssl, kHttpGetMsg, sizeof(kHttpGetMsg)-1) <= 0)
  427. err_sys("SSL_write failed");
  428. if (wolfSSL_read(ssl, reply, sizeof(reply)-1) <= 0)
  429. err_sys("SSL_read failed");
  430. }
  431. #endif
  432. wolfSSL_shutdown(ssl);
  433. #ifndef NO_SESSION_CACHE
  434. if (i == (times-1) && resumeSession) {
  435. benchSession = wolfSSL_get_session(ssl);
  436. }
  437. #endif
  438. wolfSSL_free(ssl); ssl = NULL;
  439. CloseSocket(sockfd);
  440. }
  441. avg = current_time(0) - start;
  442. avg /= (times - skip);
  443. avg *= 1000; /* milliseconds */
  444. #ifndef NO_SESSION_CACHE
  445. if (benchResume)
  446. printf("%s %8.3f %s\n", words[0],avg, words[1]);
  447. else
  448. #endif
  449. printf("%s %8.3f %s\n", words[2],avg, words[3]);
  450. WOLFSSL_TIME(times);
  451. }
  452. return EXIT_SUCCESS;
  453. }
  454. /* Measures throughput in kbps. Throughput = number of bytes */
  455. static int ClientBenchmarkThroughput(WOLFSSL_CTX* ctx, char* host, word16 port,
  456. int dtlsUDP, int dtlsSCTP, int block, size_t throughput, int useX25519,
  457. int useX448, int exitWithRet)
  458. {
  459. double start, conn_time = 0, tx_time = 0, rx_time = 0;
  460. SOCKET_T sockfd;
  461. WOLFSSL* ssl;
  462. int ret = 0, err = 0;
  463. start = current_time(1);
  464. ssl = wolfSSL_new(ctx);
  465. if (ssl == NULL)
  466. err_sys("unable to get SSL object");
  467. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  468. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  469. err_sys("error in setting fd");
  470. }
  471. (void)useX25519;
  472. (void)useX448;
  473. #ifdef WOLFSSL_TLS13
  474. #ifdef HAVE_CURVE25519
  475. if (useX25519) {
  476. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  477. != WOLFSSL_SUCCESS) {
  478. err_sys("unable to use curve x25519");
  479. }
  480. }
  481. #endif
  482. #ifdef HAVE_CURVE448
  483. if (useX448) {
  484. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X448)
  485. != WOLFSSL_SUCCESS) {
  486. err_sys("unable to use curve x448");
  487. }
  488. }
  489. #endif
  490. #endif
  491. do {
  492. err = 0; /* reset error */
  493. ret = wolfSSL_connect(ssl);
  494. if (ret != WOLFSSL_SUCCESS) {
  495. err = wolfSSL_get_error(ssl, 0);
  496. #ifdef WOLFSSL_ASYNC_CRYPT
  497. if (err == WC_PENDING_E) {
  498. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  499. if (ret < 0) break;
  500. }
  501. #endif
  502. }
  503. } while (err == WC_PENDING_E);
  504. if (ret == WOLFSSL_SUCCESS) {
  505. /* Perform throughput test */
  506. char *tx_buffer, *rx_buffer;
  507. /* Record connection time */
  508. conn_time = current_time(0) - start;
  509. /* Allocate TX/RX buffers */
  510. tx_buffer = (char*)XMALLOC(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  511. rx_buffer = (char*)XMALLOC(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  512. if (tx_buffer && rx_buffer) {
  513. WC_RNG rng;
  514. /* Startup the RNG */
  515. #if !defined(HAVE_FIPS) && defined(WOLFSSL_ASYNC_CRYPT)
  516. ret = wc_InitRng_ex(&rng, NULL, devId);
  517. #else
  518. ret = wc_InitRng(&rng);
  519. #endif
  520. if (ret == 0) {
  521. size_t xfer_bytes;
  522. /* Generate random data to send */
  523. ret = wc_RNG_GenerateBlock(&rng, (byte*)tx_buffer, block);
  524. wc_FreeRng(&rng);
  525. if(ret != 0) {
  526. err_sys("wc_RNG_GenerateBlock failed");
  527. }
  528. /* Perform TX and RX of bytes */
  529. xfer_bytes = 0;
  530. while (throughput > xfer_bytes) {
  531. int len, rx_pos, select_ret;
  532. /* Determine packet size */
  533. len = min(block, (int)(throughput - xfer_bytes));
  534. /* Perform TX */
  535. start = current_time(1);
  536. do {
  537. err = 0; /* reset error */
  538. ret = wolfSSL_write(ssl, tx_buffer, len);
  539. if (ret <= 0) {
  540. err = wolfSSL_get_error(ssl, 0);
  541. #ifdef WOLFSSL_ASYNC_CRYPT
  542. if (err == WC_PENDING_E) {
  543. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  544. if (ret < 0) break;
  545. }
  546. #endif
  547. }
  548. } while (err == WC_PENDING_E);
  549. if (ret != len) {
  550. printf("SSL_write bench error %d!\n", err);
  551. if (!exitWithRet)
  552. err_sys("SSL_write failed");
  553. goto doExit;
  554. }
  555. tx_time += current_time(0) - start;
  556. /* Perform RX */
  557. select_ret = tcp_select(sockfd, DEFAULT_TIMEOUT_SEC);
  558. if (select_ret == TEST_RECV_READY) {
  559. start = current_time(1);
  560. rx_pos = 0;
  561. while (rx_pos < len) {
  562. ret = wolfSSL_read(ssl, &rx_buffer[rx_pos],
  563. len - rx_pos);
  564. if (ret <= 0) {
  565. err = wolfSSL_get_error(ssl, 0);
  566. #ifdef WOLFSSL_ASYNC_CRYPT
  567. if (err == WC_PENDING_E) {
  568. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  569. if (ret < 0) break;
  570. }
  571. else
  572. #endif
  573. if (err != WOLFSSL_ERROR_WANT_READ) {
  574. printf("SSL_read bench error %d\n", err);
  575. err_sys("SSL_read failed");
  576. }
  577. }
  578. else {
  579. rx_pos += ret;
  580. }
  581. }
  582. rx_time += current_time(0) - start;
  583. }
  584. /* Compare TX and RX buffers */
  585. if (XMEMCMP(tx_buffer, rx_buffer, len) != 0) {
  586. free(tx_buffer);
  587. tx_buffer = NULL;
  588. free(rx_buffer);
  589. rx_buffer = NULL;
  590. err_sys("Compare TX and RX buffers failed");
  591. }
  592. /* Update overall position */
  593. xfer_bytes += len;
  594. }
  595. }
  596. else {
  597. err_sys("wc_InitRng failed");
  598. }
  599. (void)rng; /* for WC_NO_RNG case */
  600. }
  601. else {
  602. err_sys("Client buffer malloc failed");
  603. }
  604. doExit:
  605. if(tx_buffer) XFREE(tx_buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  606. if(rx_buffer) XFREE(rx_buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  607. }
  608. else {
  609. err_sys("wolfSSL_connect failed");
  610. }
  611. wolfSSL_shutdown(ssl);
  612. wolfSSL_free(ssl); ssl = NULL;
  613. CloseSocket(sockfd);
  614. if (exitWithRet)
  615. return err;
  616. #if !defined(__MINGW32__)
  617. printf("wolfSSL Client Benchmark %zu bytes\n"
  618. #else
  619. printf("wolfSSL Client Benchmark %d bytes\n"
  620. #endif
  621. "\tConnect %8.3f ms\n"
  622. "\tTX %8.3f ms (%8.3f MBps)\n"
  623. "\tRX %8.3f ms (%8.3f MBps)\n",
  624. #if !defined(__MINGW32__)
  625. throughput,
  626. #else
  627. (int)throughput,
  628. #endif
  629. conn_time * 1000,
  630. tx_time * 1000, throughput / tx_time / 1024 / 1024,
  631. rx_time * 1000, throughput / rx_time / 1024 / 1024
  632. );
  633. return EXIT_SUCCESS;
  634. }
  635. const char* starttlsCmd[6] = {
  636. "220",
  637. "EHLO mail.example.com\r\n",
  638. "250",
  639. "STARTTLS\r\n",
  640. "220",
  641. "QUIT\r\n",
  642. };
  643. /* Initiates the STARTTLS command sequence over TCP */
  644. static int StartTLS_Init(SOCKET_T* sockfd)
  645. {
  646. char tmpBuf[256];
  647. if (sockfd == NULL)
  648. return BAD_FUNC_ARG;
  649. /* S: 220 <host> SMTP service ready */
  650. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  651. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  652. err_sys("failed to read STARTTLS command\n");
  653. if (!XSTRNCMP(tmpBuf, starttlsCmd[0], XSTRLEN(starttlsCmd[0]))) {
  654. printf("%s\n", tmpBuf);
  655. } else {
  656. err_sys("incorrect STARTTLS command received");
  657. }
  658. /* C: EHLO mail.example.com */
  659. if (send(*sockfd, starttlsCmd[1], (int)XSTRLEN(starttlsCmd[1]), 0) !=
  660. (int)XSTRLEN(starttlsCmd[1]))
  661. err_sys("failed to send STARTTLS EHLO command\n");
  662. /* S: 250 <host> offers a warm hug of welcome */
  663. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  664. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  665. err_sys("failed to read STARTTLS command\n");
  666. if (!XSTRNCMP(tmpBuf, starttlsCmd[2], XSTRLEN(starttlsCmd[2]))) {
  667. printf("%s\n", tmpBuf);
  668. } else {
  669. err_sys("incorrect STARTTLS command received");
  670. }
  671. /* C: STARTTLS */
  672. if (send(*sockfd, starttlsCmd[3], (int)XSTRLEN(starttlsCmd[3]), 0) !=
  673. (int)XSTRLEN(starttlsCmd[3])) {
  674. err_sys("failed to send STARTTLS command\n");
  675. }
  676. /* S: 220 Go ahead */
  677. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  678. if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
  679. err_sys("failed to read STARTTLS command\n");
  680. tmpBuf[sizeof(tmpBuf)-1] = '\0';
  681. if (!XSTRNCMP(tmpBuf, starttlsCmd[4], XSTRLEN(starttlsCmd[4]))) {
  682. printf("%s\n", tmpBuf);
  683. } else {
  684. err_sys("incorrect STARTTLS command received, expected 220");
  685. }
  686. return WOLFSSL_SUCCESS;
  687. }
  688. /* Closes down the SMTP connection */
  689. static int SMTP_Shutdown(WOLFSSL* ssl, int wc_shutdown)
  690. {
  691. int ret, err = 0;
  692. char tmpBuf[256];
  693. if (ssl == NULL)
  694. return BAD_FUNC_ARG;
  695. printf("\nwolfSSL client shutting down SMTP connection\n");
  696. XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
  697. /* C: QUIT */
  698. do {
  699. ret = wolfSSL_write(ssl, starttlsCmd[5], (int)XSTRLEN(starttlsCmd[5]));
  700. if (ret < 0) {
  701. err = wolfSSL_get_error(ssl, 0);
  702. #ifdef WOLFSSL_ASYNC_CRYPT
  703. if (err == WC_PENDING_E) {
  704. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  705. if (ret < 0) break;
  706. }
  707. #endif
  708. }
  709. } while (err == WC_PENDING_E);
  710. if (ret != (int)XSTRLEN(starttlsCmd[5])) {
  711. err_sys("failed to send SMTP QUIT command\n");
  712. }
  713. /* S: 221 2.0.0 Service closing transmission channel */
  714. do {
  715. ret = wolfSSL_read(ssl, tmpBuf, sizeof(tmpBuf)-1);
  716. if (ret < 0) {
  717. err = wolfSSL_get_error(ssl, 0);
  718. #ifdef WOLFSSL_ASYNC_CRYPT
  719. if (err == WC_PENDING_E) {
  720. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  721. if (ret < 0) break;
  722. }
  723. #endif
  724. }
  725. } while (err == WC_PENDING_E);
  726. if (ret < 0) {
  727. err_sys("failed to read SMTP closing down response\n");
  728. }
  729. tmpBuf[ret] = 0; /* null terminate message */
  730. printf("%s\n", tmpBuf);
  731. ret = wolfSSL_shutdown(ssl);
  732. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  733. if (tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC) ==
  734. TEST_RECV_READY) {
  735. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  736. if (ret == WOLFSSL_SUCCESS)
  737. printf("Bidirectional shutdown complete\n");
  738. }
  739. if (ret != WOLFSSL_SUCCESS)
  740. printf("Bidirectional shutdown failed\n");
  741. }
  742. return WOLFSSL_SUCCESS;
  743. }
  744. static int ClientWrite(WOLFSSL* ssl, const char* msg, int msgSz, const char* str,
  745. int exitWithRet)
  746. {
  747. int ret, err;
  748. char buffer[WOLFSSL_MAX_ERROR_SZ];
  749. do {
  750. err = 0; /* reset error */
  751. ret = wolfSSL_write(ssl, msg, msgSz);
  752. if (ret <= 0) {
  753. err = wolfSSL_get_error(ssl, 0);
  754. #ifdef WOLFSSL_ASYNC_CRYPT
  755. if (err == WC_PENDING_E) {
  756. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  757. if (ret < 0) break;
  758. }
  759. #endif
  760. }
  761. } while (err == WOLFSSL_ERROR_WANT_WRITE
  762. #ifdef WOLFSSL_ASYNC_CRYPT
  763. || err == WC_PENDING_E
  764. #endif
  765. );
  766. if (ret != msgSz) {
  767. printf("SSL_write%s msg error %d, %s\n", str, err,
  768. wolfSSL_ERR_error_string(err, buffer));
  769. if (!exitWithRet) {
  770. err_sys("SSL_write failed");
  771. }
  772. }
  773. return err;
  774. }
  775. static int ClientRead(WOLFSSL* ssl, char* reply, int replyLen, int mustRead,
  776. const char* str, int exitWithRet)
  777. {
  778. int ret, err;
  779. char buffer[WOLFSSL_MAX_ERROR_SZ];
  780. double start = current_time(1), elapsed;
  781. do {
  782. err = 0; /* reset error */
  783. ret = wolfSSL_read(ssl, reply, replyLen);
  784. if (ret <= 0) {
  785. err = wolfSSL_get_error(ssl, 0);
  786. #ifdef WOLFSSL_ASYNC_CRYPT
  787. if (err == WC_PENDING_E) {
  788. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  789. if (ret < 0) break;
  790. }
  791. else
  792. #endif
  793. if (err != WOLFSSL_ERROR_WANT_READ) {
  794. printf("SSL_read reply error %d, %s\n", err,
  795. wolfSSL_ERR_error_string(err, buffer));
  796. if (!exitWithRet) {
  797. err_sys("SSL_read failed");
  798. }
  799. else {
  800. break;
  801. }
  802. }
  803. }
  804. if (mustRead && err == WOLFSSL_ERROR_WANT_READ) {
  805. elapsed = current_time(0) - start;
  806. if (elapsed > MAX_NON_BLOCK_SEC) {
  807. printf("Nonblocking read timeout\n");
  808. ret = WOLFSSL_FATAL_ERROR;
  809. break;
  810. }
  811. }
  812. } while ((mustRead && err == WOLFSSL_ERROR_WANT_READ)
  813. #ifdef WOLFSSL_ASYNC_CRYPT
  814. || err == WC_PENDING_E
  815. #endif
  816. );
  817. if (ret > 0) {
  818. reply[ret] = 0; /* null terminate */
  819. printf("%s%s\n", str, reply);
  820. }
  821. return err;
  822. }
  823. /* when adding new option, please follow the steps below: */
  824. /* 1. add new option message in English section */
  825. /* 2. increase the number of the second column */
  826. /* 3. add the same message into Japanese section */
  827. /* (will be translated later) */
  828. /* 4. add printf() into suitable position of Usage() */
  829. static const char* client_usage_msg[][59] = {
  830. /* English */
  831. {
  832. " NOTE: All files relative to wolfSSL home dir\n", /* 0 */
  833. "Max RSA key size in bits for build is set at : ", /* 1 */
  834. #ifdef NO_RSA
  835. "RSA not supported\n", /* 2 */
  836. #elif defined(WOLFSSL_SP_MATH) /* case of SP math only */
  837. #ifndef WOLFSSL_SP_NO_3072
  838. "3072\n", /* 2 */
  839. #elif !defined(WOLFSSL_SP_NO_2048)
  840. "2048\n", /* 2 */
  841. #else
  842. "0\n", /* 2 */
  843. #endif
  844. #elif defined(USE_FAST_MATH)
  845. #else
  846. "INFINITE\n", /* 2 */
  847. #endif
  848. "-? <num> Help, print this usage\n"
  849. " 0: English, 1: Japanese\n", /* 3 */
  850. "-h <host> Host to connect to, default", /* 4 */
  851. "-p <num> Port to connect on, not 0, default", /* 5 */
  852. #ifndef WOLFSSL_TLS13
  853. "-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default", /* 6 */
  854. "-V Prints valid ssl version numbers"
  855. ", SSLv3(0) - TLS1.2(3)\n", /* 7 */
  856. #else
  857. "-v <num> SSL version [0-4], SSLv3(0) - TLS1.3(4)), default", /* 6 */
  858. "-V Prints valid ssl version numbers,"
  859. " SSLv3(0) - TLS1.3(4)\n", /* 7 */
  860. #endif
  861. "-l <str> Cipher suite list (: delimited)\n", /* 8 */
  862. "-c <file> Certificate file, default", /* 9 */
  863. "-k <file> Key file, default", /* 10 */
  864. "-A <file> Certificate Authority file, default", /* 11 */
  865. #ifndef NO_DH
  866. "-Z <num> Minimum DH key bits, default", /* 12 */
  867. #endif
  868. "-b <num> Benchmark <num> connections and print stats\n", /* 13 */
  869. #ifdef HAVE_ALPN
  870. "-L <str> Application-Layer Protocol"
  871. " Negotiation ({C,F}:<list>)\n", /* 14 */
  872. #endif
  873. "-B <num> Benchmark throughput"
  874. " using <num> bytes and print stats\n", /* 15 */
  875. "-s Use pre Shared keys\n", /* 16 */
  876. "-d Disable peer checks\n", /* 17 */
  877. "-D Override Date Errors example\n", /* 18 */
  878. "-e List Every cipher suite available, \n", /* 19 */
  879. "-g Send server HTTP GET\n", /* 20 */
  880. "-u Use UDP DTLS,"
  881. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 21 */
  882. #ifdef WOLFSSL_SCTP
  883. "-G Use SCTP DTLS,"
  884. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 22 */
  885. #endif
  886. "-m Match domain name in cert\n", /* 23 */
  887. "-N Use Non-blocking sockets\n", /* 24 */
  888. #ifndef NO_SESSION_CACHE
  889. "-r Resume session\n", /* 25 */
  890. #endif
  891. "-w Wait for bidirectional shutdown\n", /* 26 */
  892. "-M <prot> Use STARTTLS, using <prot> protocol (smtp)\n", /* 27 */
  893. #ifdef HAVE_SECURE_RENEGOTIATION
  894. "-R Allow Secure Renegotiation\n", /* 28 */
  895. "-i Force client Initiated Secure Renegotiation\n", /* 29 */
  896. #endif
  897. "-f Fewer packets/group messages\n", /* 30 */
  898. "-x Disable client cert/key loading\n", /* 31 */
  899. "-X Driven by eXternal test case\n", /* 32 */
  900. "-j Use verify callback override\n", /* 33 */
  901. #ifdef SHOW_SIZES
  902. "-z Print structure sizes\n", /* 34 */
  903. #endif
  904. #ifdef HAVE_SNI
  905. "-S <str> Use Host Name Indication\n", /* 35 */
  906. #endif
  907. #ifdef HAVE_MAX_FRAGMENT
  908. "-F <num> Use Maximum Fragment Length [1-6]\n", /* 36 */
  909. #endif
  910. #ifdef HAVE_TRUNCATED_HMAC
  911. "-T Use Truncated HMAC\n", /* 37 */
  912. #endif
  913. #ifdef HAVE_EXTENDED_MASTER
  914. "-n Disable Extended Master Secret\n", /* 38 */
  915. #endif
  916. #ifdef HAVE_OCSP
  917. "-o Perform OCSP lookup on peer certificate\n", /* 39 */
  918. "-O <url> Perform OCSP lookup using <url> as responder\n", /* 40 */
  919. #endif
  920. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  921. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  922. "-W <num> Use OCSP Stapling (1 v1, 2 v2, 3 v2 multi)\n", /* 41 */
  923. #endif
  924. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  925. "-U Atomic User Record Layer Callbacks\n", /* 42 */
  926. #endif
  927. #ifdef HAVE_PK_CALLBACKS
  928. "-P Public Key Callbacks\n", /* 43 */
  929. #endif
  930. #ifdef HAVE_ANON
  931. "-a Anonymous client\n", /* 44 */
  932. #endif
  933. #ifdef HAVE_CRL
  934. "-C Disable CRL\n", /* 45 */
  935. #endif
  936. #ifdef WOLFSSL_TRUST_PEER_CERT
  937. "-E <file> Path to load trusted peer cert\n", /* 46 */
  938. #endif
  939. #ifdef HAVE_WNR
  940. "-q <file> Whitewood config file, defaults\n", /* 47 */
  941. #endif
  942. "-H <arg> Internal tests"
  943. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 48 */
  944. " loadSSL, disallowETM]\n", /* 49 */
  945. #ifdef WOLFSSL_TLS13
  946. "-J Use HelloRetryRequest to choose group for KE\n", /* 50 */
  947. "-K Key Exchange for PSK not using (EC)DHE\n", /* 51 */
  948. "-I Update keys and IVs before sending data\n", /* 52 */
  949. #ifndef NO_DH
  950. "-y Key Share with FFDHE named groups only\n", /* 53 */
  951. #endif
  952. #ifdef HAVE_ECC
  953. "-Y Key Share with ECC named groups only\n", /* 54 */
  954. #endif
  955. #endif /* WOLFSSL_TLS13 */
  956. #ifdef HAVE_CURVE25519
  957. "-t Use X25519 for key exchange\n", /* 55 */
  958. #endif
  959. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  960. "-Q Support requesting certificate post-handshake\n", /* 56 */
  961. #endif
  962. #ifdef WOLFSSL_EARLY_DATA
  963. "-0 Early data sent to server (0-RTT handshake)\n", /* 57 */
  964. #endif
  965. #ifdef WOLFSSL_MULTICAST
  966. "-3 <grpid> Multicast, grpid < 256\n", /* 58 */
  967. #endif
  968. "-1 <num> Display a result by specified language.\n"
  969. " 0: English, 1: Japanese\n", /* 59 */
  970. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  971. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  972. "-2 Disable DH Prime check\n", /* 60 */
  973. #endif
  974. #ifdef HAVE_SECURE_RENEGOTIATION
  975. "-4 Use resumption for renegotiation\n", /* 61 */
  976. #endif
  977. #ifdef HAVE_TRUSTED_CA
  978. "-5 Use Trusted CA Key Indication\n", /* 62 */
  979. #endif
  980. #ifdef HAVE_CURVE448
  981. "-8 Use X448 for key exchange\n", /* 65 */
  982. #endif
  983. NULL,
  984. },
  985. #ifndef NO_MULTIBYTE_PRINT
  986. /* Japanese */
  987. {
  988. " 注意 : 全てのファイルは wolfSSL ホーム・ディレクトリからの相対です。"
  989. "\n", /* 0 */
  990. "RSAの最大ビットは次のように設定されています: ", /* 1 */
  991. #ifdef NO_RSA
  992. "RSAはサポートされていません。\n", /* 2 */
  993. #elif defined(WOLFSSL_SP_MATH) /* case of SP math only */
  994. #ifndef WOLFSSL_SP_NO_3072
  995. "3072\n", /* 2 */
  996. #elif !defined(WOLFSSL_SP_NO_2048)
  997. "2048\n", /* 2 */
  998. #else
  999. "0\n", /* 2 */
  1000. #endif
  1001. #elif defined(USE_FAST_MATH)
  1002. #else
  1003. "無限\n", /* 2 */
  1004. #endif
  1005. "-? <num> ヘルプ, 使い方を表示\n"
  1006. " 0: 英語、 1: 日本語\n", /* 3 */
  1007. "-h <host> 接続先ホスト, 既定値", /* 4 */
  1008. "-p <num> 接続先ポート, 0は無効, 既定値", /* 5 */
  1009. #ifndef WOLFSSL_TLS13
  1010. "-v <num> SSL バージョン [0-3], SSLv3(0) - TLS1.2(3)),"
  1011. " 既定値", /* 6 */
  1012. "-V 有効な ssl バージョン番号を出力, SSLv3(0) -"
  1013. " TLS1.2(3)\n", /* 7 */
  1014. #else
  1015. "-v <num> SSL バージョン [0-4], SSLv3(0) - TLS1.3(4)),"
  1016. " 既定値", /* 6 */
  1017. "-V 有効な ssl バージョン番号を出力, SSLv3(0) -"
  1018. " TLS1.3(4)\n", /* 7 */
  1019. #endif
  1020. "-l <str> 暗号スイートリスト (区切り文字 :)\n", /* 8 */
  1021. "-c <file> 証明書ファイル, 既定値", /* 9 */
  1022. "-k <file> 鍵ファイル, 既定値", /* 10 */
  1023. "-A <file> 認証局ファイル, 既定値", /* 11 */
  1024. #ifndef NO_DH
  1025. "-Z <num> 最小 DH 鍵 ビット, 既定値", /* 12 */
  1026. #endif
  1027. "-b <num> ベンチマーク <num> 接続及び結果出力する\n", /* 13 */
  1028. #ifdef HAVE_ALPN
  1029. "-L <str> アプリケーション層プロトコルネゴシエーションを行う"
  1030. " ({C,F}:<list>)\n", /* 14 */
  1031. #endif
  1032. "-B <num> <num> バイトを用いてのベンチマーク・スループット測定"
  1033. "と結果を出力する\n", /* 15 */
  1034. "-s 事前共有鍵を使用する\n", /* 16 */
  1035. "-d ピア確認を無効とする\n", /* 17 */
  1036. "-D 日付エラー用コールバック例の上書きを行う\n", /* 18 */
  1037. "-e 利用可能な全ての暗号スイートをリスト, \n", /* 19 */
  1038. "-g サーバーへ HTTP GET を送信\n", /* 20 */
  1039. "-u UDP DTLSを使用する。-v 2 を追加指定すると"
  1040. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 21 */
  1041. #ifdef WOLFSSL_SCTP
  1042. "-G SCTP DTLSを使用する。-v 2 を追加指定すると"
  1043. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 22 */
  1044. #endif
  1045. "-m 証明書内のドメイン名一致を確認する\n", /* 23 */
  1046. "-N ノンブロッキング・ソケットを使用する\n", /* 24 */
  1047. #ifndef NO_SESSION_CACHE
  1048. "-r セッションを継続する\n", /* 25 */
  1049. #endif
  1050. "-w 双方向シャットダウンを待つ\n", /* 26 */
  1051. "-M <prot> STARTTLSを使用する, <prot>プロトコル(smtp)を"
  1052. "使用する\n", /* 27 */
  1053. #ifdef HAVE_SECURE_RENEGOTIATION
  1054. "-R セキュアな再ネゴシエーションを許可する\n", /* 28 */
  1055. "-i クライアント主導のネゴシエーションを強制する\n", /* 29 */
  1056. #endif
  1057. "-f より少ないパケット/グループメッセージを使用する\n",/* 30 */
  1058. "-x クライアントの証明書/鍵のロードを無効する\n", /* 31 */
  1059. "-X 外部テスト・ケースにより動作する\n", /* 32 */
  1060. "-j コールバック・オーバーライドの検証を使用する\n", /* 33 */
  1061. #ifdef SHOW_SIZES
  1062. "-z 構造体のサイズを表示する\n", /* 34 */
  1063. #endif
  1064. #ifdef HAVE_SNI
  1065. "-S <str> ホスト名表示を使用する\n", /* 35 */
  1066. #endif
  1067. #ifdef HAVE_MAX_FRAGMENT
  1068. "-F <num> 最大フラグメント長[1-6]を設定する\n", /* 36 */
  1069. #endif
  1070. #ifdef HAVE_TRUNCATED_HMAC
  1071. "-T Truncated HMACを使用する\n", /* 37 */
  1072. #endif
  1073. #ifdef HAVE_EXTENDED_MASTER
  1074. "-n マスターシークレット拡張を無効にする\n", /* 38 */
  1075. #endif
  1076. #ifdef HAVE_OCSP
  1077. "-o OCSPルックアップをピア証明書で実施する\n", /* 39 */
  1078. "-O <url> OCSPルックアップを、<url>を使用し"
  1079. "応答者として実施する\n", /* 40 */
  1080. #endif
  1081. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1082. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1083. "-W <num> OCSP Staplingを使用する"
  1084. " (1 v1, 2 v2, 3 v2 multi)\n", /* 41 */
  1085. #endif
  1086. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1087. "-U アトミック・ユーザー記録の"
  1088. "コールバックを利用する\n", /* 42 */
  1089. #endif
  1090. #ifdef HAVE_PK_CALLBACKS
  1091. "-P 公開鍵コールバック\n", /* 43 */
  1092. #endif
  1093. #ifdef HAVE_ANON
  1094. "-a 匿名クライアント\n", /* 44 */
  1095. #endif
  1096. #ifdef HAVE_CRL
  1097. "-C CRLを無効\n", /* 45 */
  1098. #endif
  1099. #ifdef WOLFSSL_TRUST_PEER_CERT
  1100. "-E <file> 信頼出来るピアの証明書ロードの為のパス\n", /* 46 */
  1101. #endif
  1102. #ifdef HAVE_WNR
  1103. "-q <file> Whitewood コンフィグファイル, 既定値\n", /* 47 */
  1104. #endif
  1105. "-H <arg> 内部テスト"
  1106. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 48 */
  1107. " loadSSL, disallowETM]\n", /* 49 */
  1108. #ifdef WOLFSSL_TLS13
  1109. "-J HelloRetryRequestをKEのグループ選択に使用する\n", /* 50 */
  1110. "-K 鍵交換にPSKを使用、(EC)DHEは使用しない\n", /* 51 */
  1111. "-I データ送信前に、鍵とIVを更新する\n", /* 52 */
  1112. #ifndef NO_DH
  1113. "-y FFDHE名前付きグループとの鍵共有のみ\n", /* 53 */
  1114. #endif
  1115. #ifdef HAVE_ECC
  1116. "-Y ECC名前付きグループとの鍵共有のみ\n", /* 54 */
  1117. #endif
  1118. #endif /* WOLFSSL_TLS13 */
  1119. #ifdef HAVE_CURVE25519
  1120. "-t X25519を鍵交換に使用する\n", /* 55 */
  1121. #endif
  1122. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1123. "-Q ポストハンドシェークの証明要求をサポートする\n", /* 56 */
  1124. #endif
  1125. #ifdef WOLFSSL_EARLY_DATA
  1126. "-0 Early data をサーバーへ送信する"
  1127. "(0-RTTハンドシェイク)\n", /* 57 */
  1128. #endif
  1129. #ifdef WOLFSSL_MULTICAST
  1130. "-3 <grpid> マルチキャスト, grpid < 256\n", /* 58 */
  1131. #endif
  1132. "-1 <num> 指定された言語で結果を表示します。\n"
  1133. " 0: 英語、 1: 日本語\n", /* 59 */
  1134. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1135. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1136. "-2 DHプライム番号チェックを無効にする\n", /* 60 */
  1137. #endif
  1138. #ifdef HAVE_SECURE_RENEGOTIATION
  1139. "-4 再交渉に再開を使用\n", /* 61 */
  1140. #endif
  1141. #ifdef HAVE_TRUSTED_CA
  1142. "-5 信頼できる認証局の鍵表示を使用する\n", /* 62 */
  1143. #endif
  1144. #ifdef HAVE_CURVE448
  1145. "-8 Use X448 for key exchange\n", /* 65 */
  1146. #endif
  1147. NULL,
  1148. },
  1149. #endif
  1150. };
  1151. static void Usage(void)
  1152. {
  1153. int msgid = 0;
  1154. const char** msg = client_usage_msg[lng_index];
  1155. printf("%s%s%s", "wolfSSL client ", LIBWOLFSSL_VERSION_STRING,
  1156. msg[msgid]);
  1157. /* print out so that scripts can know what the max supported key size is */
  1158. printf("%s", msg[++msgid]);
  1159. #ifdef NO_RSA
  1160. printf("%s", msg[++msgid]);
  1161. #elif defined(WOLFSSL_SP_MATH) /* case of SP math only */
  1162. #ifndef WOLFSSL_SP_NO_3072
  1163. printf("%s", msg[++msgid]);
  1164. #elif !defined(WOLFSSL_SP_NO_2048)
  1165. printf("%s", msg[++msgid]);
  1166. #else
  1167. printf("%s", msg[++msgid]);
  1168. #endif
  1169. #elif defined(USE_FAST_MATH)
  1170. printf("%d\n", FP_MAX_BITS/2);
  1171. #else
  1172. /* normal math has unlimited max size */
  1173. printf("%s", msg[++msgid]);
  1174. #endif
  1175. printf("%s", msg[++msgid]); /* ? */
  1176. printf("%s %s\n", msg[++msgid], wolfSSLIP); /* -h */
  1177. printf("%s %d\n", msg[++msgid], wolfSSLPort); /* -p */
  1178. #ifndef WOLFSSL_TLS13
  1179. printf("%s %d\n", msg[++msgid], CLIENT_DEFAULT_VERSION); /* -v */
  1180. printf("%s", msg[++msgid]); /* -V */
  1181. #else
  1182. printf("%s %d\n", msg[++msgid], CLIENT_DEFAULT_VERSION); /* -v */
  1183. printf("%s", msg[++msgid]); /* -V */
  1184. #endif
  1185. printf("%s", msg[++msgid]); /* -l */
  1186. printf("%s %s\n", msg[++msgid], cliCertFile); /* -c */
  1187. printf("%s %s\n", msg[++msgid], cliKeyFile); /* -k */
  1188. printf("%s %s\n", msg[++msgid], caCertFile); /* -A */
  1189. #ifndef NO_DH
  1190. printf("%s %d\n", msg[++msgid], DEFAULT_MIN_DHKEY_BITS);
  1191. #endif
  1192. printf("%s", msg[++msgid]); /* -b */
  1193. #ifdef HAVE_ALPN
  1194. printf("%s", msg[++msgid]); /* -L <str> */
  1195. #endif
  1196. printf("%s", msg[++msgid]); /* -B <num> */
  1197. printf("%s", msg[++msgid]); /* -s */
  1198. printf("%s", msg[++msgid]); /* -d */
  1199. printf("%s", msg[++msgid]); /* -D */
  1200. printf("%s", msg[++msgid]); /* -e */
  1201. printf("%s", msg[++msgid]); /* -g */
  1202. printf("%s", msg[++msgid]); /* -u */
  1203. #ifdef WOLFSSL_SCTP
  1204. printf("%s", msg[++msgid]); /* -G */
  1205. #endif
  1206. printf("%s", msg[++msgid]); /* -m */
  1207. printf("%s", msg[++msgid]); /* -N */
  1208. #ifndef NO_SESSION_CACHE
  1209. printf("%s", msg[++msgid]); /* -r */
  1210. #endif
  1211. printf("%s", msg[++msgid]); /* -w */
  1212. printf("%s", msg[++msgid]); /* -M */
  1213. #ifdef HAVE_SECURE_RENEGOTIATION
  1214. printf("%s", msg[++msgid]); /* -R */
  1215. printf("%s", msg[++msgid]); /* -i */
  1216. #endif
  1217. printf("%s", msg[++msgid]); /* -f */
  1218. printf("%s", msg[++msgid]); /* -x */
  1219. printf("%s", msg[++msgid]); /* -X */
  1220. printf("%s", msg[++msgid]); /* -j */
  1221. #ifdef SHOW_SIZES
  1222. printf("%s", msg[++msgid]); /* -z */
  1223. #endif
  1224. #ifdef HAVE_SNI
  1225. printf("%s", msg[++msgid]); /* -S */
  1226. #endif
  1227. #ifdef HAVE_MAX_FRAGMENT
  1228. printf("%s", msg[++msgid]); /* -F */
  1229. #endif
  1230. #ifdef HAVE_TRUNCATED_HMAC
  1231. printf("%s", msg[++msgid]); /* -T */
  1232. #endif
  1233. #ifdef HAVE_EXTENDED_MASTER
  1234. printf("%s", msg[++msgid]); /* -n */
  1235. #endif
  1236. #ifdef HAVE_OCSP
  1237. printf("%s", msg[++msgid]); /* -o */
  1238. printf("%s", msg[++msgid]); /* -O */
  1239. #endif
  1240. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1241. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1242. printf("%s", msg[++msgid]); /* -W */
  1243. #endif
  1244. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1245. printf("%s", msg[++msgid]); /* -U */
  1246. #endif
  1247. #ifdef HAVE_PK_CALLBACKS
  1248. printf("%s", msg[++msgid]); /* -P */
  1249. #endif
  1250. #ifdef HAVE_ANON
  1251. printf("%s", msg[++msgid]); /* -a */
  1252. #endif
  1253. #ifdef HAVE_CRL
  1254. printf("%s", msg[++msgid]); /* -C */
  1255. #endif
  1256. #ifdef WOLFSSL_TRUST_PEER_CERT
  1257. printf("%s", msg[++msgid]); /* -E */
  1258. #endif
  1259. #ifdef HAVE_WNR
  1260. printf("%s %s\n", msg[++msgid], wnrConfig); /* -q */
  1261. #endif
  1262. printf("%s", msg[++msgid]); /* -H */
  1263. printf("%s", msg[++msgid]); /* more -H options */
  1264. #ifdef WOLFSSL_TLS13
  1265. printf("%s", msg[++msgid]); /* -J */
  1266. printf("%s", msg[++msgid]); /* -K */
  1267. printf("%s", msg[++msgid]); /* -I */
  1268. #ifndef NO_DH
  1269. printf("%s", msg[++msgid]); /* -y */
  1270. #endif
  1271. #ifdef HAVE_ECC
  1272. printf("%s", msg[++msgid]); /* -Y */
  1273. #endif
  1274. #endif /* WOLFSSL_TLS13 */
  1275. #ifdef HAVE_CURVE25519
  1276. printf("%s", msg[++msgid]); /* -t */
  1277. #endif
  1278. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1279. printf("%s", msg[++msgid]); /* -Q */
  1280. #endif
  1281. #ifdef WOLFSSL_EARLY_DATA
  1282. printf("%s", msg[++msgid]); /* -0 */
  1283. #endif
  1284. #ifdef WOLFSSL_MULTICAST
  1285. printf("%s", msg[++msgid]); /* -3 */
  1286. #endif
  1287. printf("%s", msg[++msgid]); /* -1 */
  1288. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1289. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1290. printf("%s", msg[++msgid]); /* -2 */
  1291. #endif
  1292. #ifdef HAVE_SECURE_RENEGOTIATION
  1293. printf("%s", msg[++msgid]); /* -4 */
  1294. #endif
  1295. #ifdef HAVE_TRUSTED_CA
  1296. printf("%s", msg[++msgid]); /* -5 */
  1297. #endif
  1298. #ifdef HAVE_CURVE448
  1299. printf("%s", msg[++msgid]); /* -8 */
  1300. #endif
  1301. }
  1302. THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
  1303. {
  1304. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  1305. wolfSSL_method_func method = NULL;
  1306. WOLFSSL_CTX* ctx = 0;
  1307. WOLFSSL* ssl = 0;
  1308. WOLFSSL* sslResume = 0;
  1309. WOLFSSL_SESSION* session = 0;
  1310. byte* flatSession = NULL;
  1311. int flatSessionSz = 0;
  1312. char msg[CLI_MSG_SZ];
  1313. int msgSz = 0;
  1314. char reply[CLI_REPLY_SZ];
  1315. word16 port = wolfSSLPort;
  1316. char* host = (char*)wolfSSLIP;
  1317. const char* domain = "localhost"; /* can't default to www.wolfssl.com
  1318. because can't tell if we're really
  1319. going there to detect old chacha-poly
  1320. */
  1321. #ifndef WOLFSSL_VXWORKS
  1322. int ch;
  1323. #endif
  1324. int version = CLIENT_INVALID_VERSION;
  1325. int usePsk = 0;
  1326. int useAnon = 0;
  1327. int sendGET = 0;
  1328. int benchmark = 0;
  1329. int block = TEST_BUFFER_SIZE;
  1330. size_t throughput = 0;
  1331. int doDTLS = 0;
  1332. int dtlsUDP = 0;
  1333. int dtlsSCTP = 0;
  1334. int doMcast = 0;
  1335. int matchName = 0;
  1336. int doPeerCheck = 1;
  1337. int nonBlocking = 0;
  1338. int resumeSession = 0;
  1339. int wc_shutdown = 0;
  1340. int disableCRL = 0;
  1341. int externalTest = 0;
  1342. int ret;
  1343. int err = 0;
  1344. int scr = 0; /* allow secure renegotiation */
  1345. int forceScr = 0; /* force client initiated scr */
  1346. int resumeScr = 0; /* use resumption for renegotiation */
  1347. #ifndef WOLFSSL_NO_CLIENT_AUTH
  1348. int useClientCert = 1;
  1349. #else
  1350. int useClientCert = 0;
  1351. #endif
  1352. int fewerPackets = 0;
  1353. int atomicUser = 0;
  1354. #ifdef HAVE_PK_CALLBACKS
  1355. int pkCallbacks = 0;
  1356. PkCbInfo pkCbInfo;
  1357. #endif
  1358. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  1359. char* alpnList = NULL;
  1360. unsigned char alpn_opt = 0;
  1361. char* cipherList = NULL;
  1362. int useDefCipherList = 0;
  1363. const char* verifyCert;
  1364. const char* ourCert;
  1365. const char* ourKey;
  1366. int doSTARTTLS = 0;
  1367. char* starttlsProt = NULL;
  1368. int useVerifyCb = 0;
  1369. int useSupCurve = 0;
  1370. #ifdef WOLFSSL_TRUST_PEER_CERT
  1371. const char* trustCert = NULL;
  1372. #endif
  1373. #ifdef HAVE_SNI
  1374. char* sniHostName = NULL;
  1375. #endif
  1376. #ifdef HAVE_TRUSTED_CA
  1377. int trustedCaKeyId = 0;
  1378. #endif
  1379. #ifdef HAVE_MAX_FRAGMENT
  1380. byte maxFragment = 0;
  1381. #endif
  1382. #ifdef HAVE_TRUNCATED_HMAC
  1383. byte truncatedHMAC = 0;
  1384. #endif
  1385. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1386. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1387. byte statusRequest = 0;
  1388. #endif
  1389. #ifdef HAVE_EXTENDED_MASTER
  1390. byte disableExtMasterSecret = 0;
  1391. #endif
  1392. int helloRetry = 0;
  1393. int onlyKeyShare = 0;
  1394. #ifdef WOLFSSL_TLS13
  1395. int noPskDheKe = 0;
  1396. int postHandAuth = 0;
  1397. #endif
  1398. int updateKeysIVs = 0;
  1399. int earlyData = 0;
  1400. #ifdef WOLFSSL_MULTICAST
  1401. byte mcastID = 0;
  1402. #endif
  1403. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1404. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1405. int doDhKeyCheck = 1;
  1406. #endif
  1407. #ifdef HAVE_OCSP
  1408. int useOcsp = 0;
  1409. char* ocspUrl = NULL;
  1410. #endif
  1411. int useX25519 = 0;
  1412. int useX448 = 0;
  1413. int exitWithRet = 0;
  1414. int loadCertKeyIntoSSLObj = 0;
  1415. #ifdef HAVE_ENCRYPT_THEN_MAC
  1416. int disallowETM = 0;
  1417. #endif
  1418. #ifdef HAVE_WNR
  1419. const char* wnrConfigFile = wnrConfig;
  1420. #endif
  1421. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1422. int argc = ((func_args*)args)->argc;
  1423. char** argv = ((func_args*)args)->argv;
  1424. #ifdef WOLFSSL_STATIC_MEMORY
  1425. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  1426. || defined(SESSION_CERTS)
  1427. /* big enough to handle most cases including session certs */
  1428. byte memory[320000];
  1429. #else
  1430. byte memory[80000];
  1431. #endif
  1432. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  1433. WOLFSSL_MEM_CONN_STATS ssl_stats;
  1434. #ifdef DEBUG_WOLFSSL
  1435. WOLFSSL_MEM_STATS mem_stats;
  1436. #endif
  1437. WOLFSSL_HEAP_HINT *heap = NULL;
  1438. #endif
  1439. ((func_args*)args)->return_code = -1; /* error state */
  1440. #ifndef NO_RSA
  1441. verifyCert = caCertFile;
  1442. ourCert = cliCertFile;
  1443. ourKey = cliKeyFile;
  1444. #else
  1445. #ifdef HAVE_ECC
  1446. verifyCert = caEccCertFile;
  1447. ourCert = cliEccCertFile;
  1448. ourKey = cliEccKeyFile;
  1449. #elif defined(HAVE_ED25519)
  1450. verifyCert = caEdCertFile;
  1451. ourCert = cliEdCertFile;
  1452. ourKey = cliEdKeyFile;
  1453. #elif defined(HAVE_ED448)
  1454. verifyCert = caEd448CertFile;
  1455. ourCert = cliEd448CertFile;
  1456. ourKey = cliEd448KeyFile;
  1457. #else
  1458. verifyCert = NULL;
  1459. ourCert = NULL;
  1460. ourKey = NULL;
  1461. #endif
  1462. #endif
  1463. (void)session;
  1464. (void)flatSession;
  1465. (void)flatSessionSz;
  1466. (void)sslResume;
  1467. (void)atomicUser;
  1468. (void)scr;
  1469. (void)forceScr;
  1470. (void)resumeScr;
  1471. (void)ourKey;
  1472. (void)ourCert;
  1473. (void)verifyCert;
  1474. (void)useClientCert;
  1475. (void)disableCRL;
  1476. (void)minDhKeyBits;
  1477. (void)alpnList;
  1478. (void)alpn_opt;
  1479. (void)updateKeysIVs;
  1480. (void)earlyData;
  1481. (void)useX25519;
  1482. (void)useX448;
  1483. (void)helloRetry;
  1484. (void)onlyKeyShare;
  1485. (void)useSupCurve;
  1486. (void)loadCertKeyIntoSSLObj;
  1487. StackTrap();
  1488. /* Reinitialize the global myVerifyAction. */
  1489. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1490. #ifndef WOLFSSL_VXWORKS
  1491. /* Not used: All used */
  1492. while ((ch = mygetopt(argc, argv, "?:"
  1493. "ab:c:defgh:ijk:l:mnop:q:rstuv:wxyz"
  1494. "A:B:CDE:F:GH:IJKL:M:NO:PQRS:TUVW:XYZ:"
  1495. "01:23:458")) != -1) {
  1496. switch (ch) {
  1497. case '?' :
  1498. if(myoptarg!=NULL) {
  1499. lng_index = atoi(myoptarg);
  1500. if(lng_index<0||lng_index>1){
  1501. lng_index = 0;
  1502. }
  1503. }
  1504. Usage();
  1505. XEXIT_T(EXIT_SUCCESS);
  1506. case 'g' :
  1507. sendGET = 1;
  1508. break;
  1509. case 'd' :
  1510. doPeerCheck = 0;
  1511. break;
  1512. case 'e' :
  1513. ShowCiphers();
  1514. XEXIT_T(EXIT_SUCCESS);
  1515. case 'D' :
  1516. myVerifyAction = VERIFY_OVERRIDE_DATE_ERR;
  1517. break;
  1518. case 'C' :
  1519. #ifdef HAVE_CRL
  1520. disableCRL = 1;
  1521. #endif
  1522. break;
  1523. case 'u' :
  1524. doDTLS = 1;
  1525. dtlsUDP = 1;
  1526. break;
  1527. case 'G' :
  1528. #ifdef WOLFSSL_SCTP
  1529. doDTLS = 1;
  1530. dtlsSCTP = 1;
  1531. #endif
  1532. break;
  1533. case 's' :
  1534. usePsk = 1;
  1535. break;
  1536. #ifdef WOLFSSL_TRUST_PEER_CERT
  1537. case 'E' :
  1538. trustCert = myoptarg;
  1539. break;
  1540. #endif
  1541. case 'm' :
  1542. matchName = 1;
  1543. break;
  1544. case 'x' :
  1545. useClientCert = 0;
  1546. break;
  1547. case 'X' :
  1548. externalTest = 1;
  1549. break;
  1550. case 'f' :
  1551. fewerPackets = 1;
  1552. break;
  1553. case 'U' :
  1554. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1555. atomicUser = 1;
  1556. #endif
  1557. break;
  1558. case 'P' :
  1559. #ifdef HAVE_PK_CALLBACKS
  1560. pkCallbacks = 1;
  1561. #endif
  1562. break;
  1563. case 'h' :
  1564. host = myoptarg;
  1565. domain = myoptarg;
  1566. break;
  1567. case 'p' :
  1568. port = (word16)atoi(myoptarg);
  1569. #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API)
  1570. if (port == 0)
  1571. err_sys("port number cannot be 0");
  1572. #endif
  1573. break;
  1574. case 'v' :
  1575. if (myoptarg[0] == 'd') {
  1576. version = CLIENT_DOWNGRADE_VERSION;
  1577. break;
  1578. }
  1579. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1580. else if (myoptarg[0] == 'e') {
  1581. version = EITHER_DOWNGRADE_VERSION;
  1582. #ifndef NO_CERTS
  1583. loadCertKeyIntoSSLObj = 1;
  1584. #endif
  1585. break;
  1586. }
  1587. #endif
  1588. version = atoi(myoptarg);
  1589. if (version < 0 || version > 4) {
  1590. Usage();
  1591. XEXIT_T(MY_EX_USAGE);
  1592. }
  1593. break;
  1594. case 'V' :
  1595. ShowVersions();
  1596. XEXIT_T(EXIT_SUCCESS);
  1597. case 'l' :
  1598. cipherList = myoptarg;
  1599. break;
  1600. case 'H' :
  1601. if (XSTRNCMP(myoptarg, "defCipherList", 13) == 0) {
  1602. printf("Using default cipher list for testing\n");
  1603. useDefCipherList = 1;
  1604. }
  1605. else if (XSTRNCMP(myoptarg, "exitWithRet", 11) == 0) {
  1606. printf("Skip exit() for testing\n");
  1607. exitWithRet = 1;
  1608. }
  1609. else if (XSTRNCMP(myoptarg, "verifyFail", 10) == 0) {
  1610. printf("Verify should fail\n");
  1611. myVerifyAction = VERIFY_FORCE_FAIL;
  1612. }
  1613. else if (XSTRNCMP(myoptarg, "verifyInfo", 10) == 0) {
  1614. printf("Verify should not override error\n");
  1615. myVerifyAction = VERIFY_USE_PREVERFIY;
  1616. }
  1617. else if (XSTRNCMP(myoptarg, "useSupCurve", 11) == 0) {
  1618. printf("Attempting to test use supported curve\n");
  1619. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  1620. useSupCurve = 1;
  1621. #else
  1622. printf("Supported curves not compiled in!\n");
  1623. #endif
  1624. }
  1625. else if (XSTRNCMP(myoptarg, "loadSSL", 7) == 0) {
  1626. printf("Load cert/key into wolfSSL object\n");
  1627. #ifndef NO_CERTS
  1628. loadCertKeyIntoSSLObj = 1;
  1629. #else
  1630. printf("Certs turned off with NO_CERTS!\n");
  1631. #endif
  1632. }
  1633. else if (XSTRNCMP(myoptarg, "disallowETM", 7) == 0) {
  1634. printf("Disallow Encrypt-Then-MAC\n");
  1635. #ifdef HAVE_ENCRYPT_THEN_MAC
  1636. disallowETM = 1;
  1637. #endif
  1638. }
  1639. else {
  1640. Usage();
  1641. XEXIT_T(MY_EX_USAGE);
  1642. }
  1643. break;
  1644. case 'A' :
  1645. verifyCert = myoptarg;
  1646. break;
  1647. case 'c' :
  1648. ourCert = myoptarg;
  1649. break;
  1650. case 'k' :
  1651. ourKey = myoptarg;
  1652. break;
  1653. case 'Z' :
  1654. #ifndef NO_DH
  1655. minDhKeyBits = atoi(myoptarg);
  1656. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  1657. Usage();
  1658. XEXIT_T(MY_EX_USAGE);
  1659. }
  1660. #endif
  1661. break;
  1662. case 'b' :
  1663. benchmark = atoi(myoptarg);
  1664. if (benchmark < 0 || benchmark > 1000000) {
  1665. Usage();
  1666. XEXIT_T(MY_EX_USAGE);
  1667. }
  1668. break;
  1669. case 'B' :
  1670. throughput = atol(myoptarg);
  1671. for (; *myoptarg != '\0'; myoptarg++) {
  1672. if (*myoptarg == ',') {
  1673. block = atoi(myoptarg + 1);
  1674. break;
  1675. }
  1676. }
  1677. if (throughput == 0 || block <= 0) {
  1678. Usage();
  1679. XEXIT_T(MY_EX_USAGE);
  1680. }
  1681. break;
  1682. case 'N' :
  1683. nonBlocking = 1;
  1684. break;
  1685. case 'r' :
  1686. resumeSession = 1;
  1687. break;
  1688. case 'w' :
  1689. wc_shutdown = 1;
  1690. break;
  1691. case 'R' :
  1692. #ifdef HAVE_SECURE_RENEGOTIATION
  1693. scr = 1;
  1694. #endif
  1695. break;
  1696. case 'i' :
  1697. #ifdef HAVE_SECURE_RENEGOTIATION
  1698. scr = 1;
  1699. forceScr = 1;
  1700. #endif
  1701. break;
  1702. case 'z' :
  1703. #ifndef WOLFSSL_LEANPSK
  1704. wolfSSL_GetObjectSize();
  1705. #endif
  1706. break;
  1707. case 'S' :
  1708. if (XSTRNCMP(myoptarg, "check", 5) == 0) {
  1709. #ifdef HAVE_SNI
  1710. printf("SNI is: ON\n");
  1711. #else
  1712. printf("SNI is: OFF\n");
  1713. #endif
  1714. XEXIT_T(EXIT_SUCCESS);
  1715. }
  1716. #ifdef HAVE_SNI
  1717. sniHostName = myoptarg;
  1718. #endif
  1719. break;
  1720. case 'F' :
  1721. #ifdef HAVE_MAX_FRAGMENT
  1722. maxFragment = atoi(myoptarg);
  1723. if (maxFragment < WOLFSSL_MFL_MIN ||
  1724. maxFragment > WOLFSSL_MFL_MAX) {
  1725. Usage();
  1726. XEXIT_T(MY_EX_USAGE);
  1727. }
  1728. #endif
  1729. break;
  1730. case 'T' :
  1731. #ifdef HAVE_TRUNCATED_HMAC
  1732. truncatedHMAC = 1;
  1733. #endif
  1734. break;
  1735. case 'n' :
  1736. #ifdef HAVE_EXTENDED_MASTER
  1737. disableExtMasterSecret = 1;
  1738. #endif
  1739. break;
  1740. case 'W' :
  1741. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1742. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1743. statusRequest = atoi(myoptarg);
  1744. if (statusRequest > OCSP_STAPLING_OPT_MAX) {
  1745. Usage();
  1746. XEXIT_T(MY_EX_USAGE);
  1747. }
  1748. #endif
  1749. break;
  1750. case 'o' :
  1751. #ifdef HAVE_OCSP
  1752. useOcsp = 1;
  1753. #endif
  1754. break;
  1755. case 'O' :
  1756. #ifdef HAVE_OCSP
  1757. useOcsp = 1;
  1758. ocspUrl = myoptarg;
  1759. #endif
  1760. break;
  1761. case 'a' :
  1762. #ifdef HAVE_ANON
  1763. useAnon = 1;
  1764. #endif
  1765. break;
  1766. case 'L' :
  1767. #ifdef HAVE_ALPN
  1768. alpnList = myoptarg;
  1769. if (alpnList[0] == 'C' && alpnList[1] == ':')
  1770. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  1771. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  1772. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  1773. else {
  1774. Usage();
  1775. XEXIT_T(MY_EX_USAGE);
  1776. }
  1777. alpnList += 2;
  1778. #endif
  1779. break;
  1780. case 'M' :
  1781. doSTARTTLS = 1;
  1782. starttlsProt = myoptarg;
  1783. if (XSTRNCMP(starttlsProt, "smtp", 4) != 0) {
  1784. Usage();
  1785. XEXIT_T(MY_EX_USAGE);
  1786. }
  1787. break;
  1788. case 'q' :
  1789. #ifdef HAVE_WNR
  1790. wnrConfigFile = myoptarg;
  1791. #endif
  1792. break;
  1793. case 'J' :
  1794. #ifdef WOLFSSL_TLS13
  1795. helloRetry = 1;
  1796. #endif
  1797. break;
  1798. case 'K' :
  1799. #ifdef WOLFSSL_TLS13
  1800. noPskDheKe = 1;
  1801. #endif
  1802. break;
  1803. case 'I' :
  1804. #ifdef WOLFSSL_TLS13
  1805. updateKeysIVs = 1;
  1806. #endif
  1807. break;
  1808. case 'y' :
  1809. #if defined(WOLFSSL_TLS13) && !defined(NO_DH)
  1810. onlyKeyShare = 1;
  1811. #endif
  1812. break;
  1813. case 'Y' :
  1814. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
  1815. onlyKeyShare = 2;
  1816. #endif
  1817. break;
  1818. case 'j' :
  1819. useVerifyCb = 1;
  1820. break;
  1821. case 't' :
  1822. #ifdef HAVE_CURVE25519
  1823. useX25519 = 1;
  1824. #ifdef HAVE_ECC
  1825. useSupCurve = 1;
  1826. #ifdef WOLFSSL_TLS13
  1827. onlyKeyShare = 2;
  1828. #endif
  1829. #endif
  1830. #endif
  1831. break;
  1832. case 'Q' :
  1833. #if defined(WOLFSSL_TLS13) && \
  1834. defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1835. postHandAuth = 1;
  1836. #endif
  1837. break;
  1838. case '0' :
  1839. #ifdef WOLFSSL_EARLY_DATA
  1840. earlyData = 1;
  1841. #endif
  1842. break;
  1843. case '1' :
  1844. lng_index = atoi(myoptarg);
  1845. if(lng_index<0||lng_index>1){
  1846. lng_index = 0;
  1847. }
  1848. break;
  1849. case '2' :
  1850. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1851. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1852. doDhKeyCheck = 0;
  1853. #endif
  1854. break;
  1855. case '3' :
  1856. #ifdef WOLFSSL_MULTICAST
  1857. doMcast = 1;
  1858. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  1859. #endif
  1860. break;
  1861. case '4' :
  1862. #ifdef HAVE_SECURE_RENEGOTIATION
  1863. scr = 1;
  1864. forceScr = 1;
  1865. resumeScr = 1;
  1866. #endif
  1867. break;
  1868. case '5' :
  1869. #ifdef HAVE_TRUSTED_CA
  1870. trustedCaKeyId = 1;
  1871. #endif /* HAVE_TRUSTED_CA */
  1872. break;
  1873. case '8' :
  1874. #ifdef HAVE_CURVE448
  1875. useX448 = 1;
  1876. #ifdef HAVE_ECC
  1877. useSupCurve = 1;
  1878. #ifdef WOLFSSL_TLS13
  1879. onlyKeyShare = 2;
  1880. #endif
  1881. #endif
  1882. #endif
  1883. break;
  1884. default:
  1885. Usage();
  1886. XEXIT_T(MY_EX_USAGE);
  1887. }
  1888. }
  1889. myoptind = 0; /* reset for test cases */
  1890. #endif /* !WOLFSSL_VXWORKS */
  1891. if (externalTest) {
  1892. /* detect build cases that wouldn't allow test against wolfssl.com */
  1893. int done = 0;
  1894. #ifdef NO_RSA
  1895. done += 1; /* require RSA for external tests */
  1896. #endif
  1897. if (!XSTRNCMP(domain, "www.globalsign.com", 14)) {
  1898. /* www.globalsign.com does not respond to ipv6 ocsp requests */
  1899. #if defined(TEST_IPV6) && defined(HAVE_OCSP)
  1900. done += 1;
  1901. #endif
  1902. /* www.globalsign.com has limited supported cipher suites */
  1903. #if defined(NO_AES) && defined(HAVE_OCSP)
  1904. done += 1;
  1905. #endif
  1906. /* www.globalsign.com only supports static RSA or ECDHE with AES */
  1907. /* We cannot expect users to have on static RSA so test for ECC only
  1908. * as some users will most likely be on 32-bit systems where ECC
  1909. * is not enabled by default */
  1910. #if defined(HAVE_OCSP) && !defined(HAVE_ECC)
  1911. done += 1;
  1912. #endif
  1913. }
  1914. #ifndef NO_PSK
  1915. if (usePsk) {
  1916. done += 1; /* don't perform external tests if PSK is enabled */
  1917. }
  1918. #endif
  1919. #ifdef NO_SHA
  1920. done += 1; /* external cert chain most likely has SHA */
  1921. #endif
  1922. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  1923. || ( defined(HAVE_ECC) && !defined(HAVE_SUPPORTED_CURVES) \
  1924. && !defined(WOLFSSL_STATIC_RSA) )
  1925. /* google needs ECDHE+Supported Curves or static RSA */
  1926. if (!XSTRNCMP(domain, "www.google.com", 14))
  1927. done += 1;
  1928. #endif
  1929. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA)
  1930. /* wolfssl needs ECDHE or static RSA */
  1931. if (!XSTRNCMP(domain, "www.wolfssl.com", 15))
  1932. done += 1;
  1933. #endif
  1934. #if !defined(WOLFSSL_SHA384)
  1935. if (!XSTRNCMP(domain, "www.wolfssl.com", 15)) {
  1936. /* wolfssl need sha384 for cert chain verify */
  1937. done += 1;
  1938. }
  1939. #endif
  1940. #if !defined(HAVE_AESGCM) && defined(NO_AES) && \
  1941. !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  1942. /* need at least one of these for external tests */
  1943. done += 1;
  1944. #endif
  1945. #if defined(HAVE_QSH)
  1946. /*currently google server rejects client hello with QSH extension.*/
  1947. done += 1;
  1948. #endif
  1949. /* For the external test, if we disable AES, GoDaddy will reject the
  1950. * connection. They only currently support AES suites, RC4 and 3DES
  1951. * suites. With AES disabled we only offer PolyChacha suites. */
  1952. #if defined(NO_AES) && !defined(HAVE_AESGCM)
  1953. if (!XSTRNCMP(domain, "www.wolfssl.com", 15)) {
  1954. done += 1;
  1955. }
  1956. #endif
  1957. if (done) {
  1958. printf("external test can't be run in this mode\n");
  1959. ((func_args*)args)->return_code = 0;
  1960. XEXIT_T(EXIT_SUCCESS);
  1961. }
  1962. }
  1963. /* sort out DTLS versus TLS versions */
  1964. if (version == CLIENT_INVALID_VERSION) {
  1965. if (doDTLS)
  1966. version = CLIENT_DTLS_DEFAULT_VERSION;
  1967. else
  1968. version = CLIENT_DEFAULT_VERSION;
  1969. }
  1970. else {
  1971. if (doDTLS) {
  1972. if (version == 3)
  1973. version = -2;
  1974. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1975. else if (version == EITHER_DOWNGRADE_VERSION)
  1976. version = -3;
  1977. #endif
  1978. else
  1979. version = -1;
  1980. }
  1981. }
  1982. #ifdef HAVE_WNR
  1983. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  1984. err_sys("can't load whitewood net random config file");
  1985. #endif
  1986. switch (version) {
  1987. #ifndef NO_OLD_TLS
  1988. #ifdef WOLFSSL_ALLOW_SSLV3
  1989. case 0:
  1990. method = wolfSSLv3_client_method_ex;
  1991. break;
  1992. #endif
  1993. #ifndef NO_TLS
  1994. #ifdef WOLFSSL_ALLOW_TLSV10
  1995. case 1:
  1996. method = wolfTLSv1_client_method_ex;
  1997. break;
  1998. #endif
  1999. case 2:
  2000. method = wolfTLSv1_1_client_method_ex;
  2001. break;
  2002. #endif /* !NO_TLS */
  2003. #endif /* !NO_OLD_TLS */
  2004. #ifndef NO_TLS
  2005. #ifndef WOLFSSL_NO_TLS12
  2006. case 3:
  2007. method = wolfTLSv1_2_client_method_ex;
  2008. break;
  2009. #endif
  2010. #ifdef WOLFSSL_TLS13
  2011. case 4:
  2012. method = wolfTLSv1_3_client_method_ex;
  2013. break;
  2014. #endif
  2015. case CLIENT_DOWNGRADE_VERSION:
  2016. method = wolfSSLv23_client_method_ex;
  2017. break;
  2018. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2019. case EITHER_DOWNGRADE_VERSION:
  2020. method = wolfSSLv23_method_ex;
  2021. break;
  2022. #endif
  2023. #endif /* NO_TLS */
  2024. #ifdef WOLFSSL_DTLS
  2025. #ifndef NO_OLD_TLS
  2026. case -1:
  2027. method = wolfDTLSv1_client_method_ex;
  2028. break;
  2029. #endif
  2030. #ifndef WOLFSSL_NO_TLS12
  2031. case -2:
  2032. method = wolfDTLSv1_2_client_method_ex;
  2033. break;
  2034. #endif
  2035. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2036. case -3:
  2037. method = wolfDTLSv1_2_method_ex;
  2038. break;
  2039. #endif
  2040. #endif
  2041. default:
  2042. err_sys("Bad SSL version");
  2043. break;
  2044. }
  2045. if (method == NULL)
  2046. err_sys("unable to get method");
  2047. #ifdef WOLFSSL_STATIC_MEMORY
  2048. #ifdef DEBUG_WOLFSSL
  2049. /* print off helper buffer sizes for use with static memory
  2050. * printing to stderr in case of debug mode turned on */
  2051. fprintf(stderr, "static memory management size = %d\n",
  2052. wolfSSL_MemoryPaddingSz());
  2053. fprintf(stderr, "calculated optimum general buffer size = %d\n",
  2054. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2055. fprintf(stderr, "calculated optimum IO buffer size = %d\n",
  2056. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2057. WOLFMEM_IO_POOL_FIXED));
  2058. #endif /* DEBUG_WOLFSSL */
  2059. if (wc_LoadStaticMemory(&heap, memory, sizeof(memory), WOLFMEM_GENERAL, 1)
  2060. != 0) {
  2061. err_sys("unable to load static memory");
  2062. }
  2063. ctx = wolfSSL_CTX_new_ex(method(heap), heap);
  2064. if (ctx == NULL)
  2065. err_sys("unable to get ctx");
  2066. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2067. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) != WOLFSSL_SUCCESS) {
  2068. err_sys("unable to load static memory");
  2069. }
  2070. #else
  2071. ctx = wolfSSL_CTX_new(method(NULL));
  2072. if (ctx == NULL)
  2073. err_sys("unable to get ctx");
  2074. #endif
  2075. #ifdef SINGLE_THREADED
  2076. if (wolfSSL_CTX_new_rng(ctx) != WOLFSSL_SUCCESS) {
  2077. wolfSSL_CTX_free(ctx); ctx = NULL;
  2078. err_sys("Single Threaded new rng at CTX failed");
  2079. }
  2080. #endif
  2081. if (cipherList && !useDefCipherList) {
  2082. if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS) {
  2083. wolfSSL_CTX_free(ctx); ctx = NULL;
  2084. err_sys("client can't set cipher list 1");
  2085. }
  2086. }
  2087. #ifdef WOLFSSL_LEANPSK
  2088. if (!usePsk) {
  2089. usePsk = 1;
  2090. }
  2091. #endif
  2092. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2093. !defined(HAVE_ED448)
  2094. if (!usePsk) {
  2095. usePsk = 1;
  2096. }
  2097. #endif
  2098. if (fewerPackets)
  2099. wolfSSL_CTX_set_group_messages(ctx);
  2100. #ifndef NO_DH
  2101. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2102. != WOLFSSL_SUCCESS) {
  2103. err_sys("Error setting minimum DH key size");
  2104. }
  2105. #endif
  2106. if (usePsk) {
  2107. #ifndef NO_PSK
  2108. const char *defaultCipherList = cipherList;
  2109. wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  2110. #ifdef WOLFSSL_TLS13
  2111. wolfSSL_CTX_set_psk_client_tls13_callback(ctx, my_psk_client_tls13_cb);
  2112. #endif
  2113. if (defaultCipherList == NULL) {
  2114. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2115. #ifdef WOLFSSL_TLS13
  2116. defaultCipherList = "TLS13-AES128-GCM-SHA256:"
  2117. "DHE-PSK-AES128-GCM-SHA256:";
  2118. #else
  2119. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2120. #endif
  2121. #elif defined(HAVE_NULL_CIPHER)
  2122. defaultCipherList = "PSK-NULL-SHA256";
  2123. #else
  2124. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2125. #endif
  2126. if (wolfSSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2127. !=WOLFSSL_SUCCESS) {
  2128. wolfSSL_CTX_free(ctx); ctx = NULL;
  2129. err_sys("client can't set cipher list 2");
  2130. }
  2131. }
  2132. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2133. #endif
  2134. if (useClientCert) {
  2135. useClientCert = 0;
  2136. }
  2137. }
  2138. if (useAnon) {
  2139. #ifdef HAVE_ANON
  2140. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2141. const char* defaultCipherList;
  2142. wolfSSL_CTX_allow_anon_cipher(ctx);
  2143. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2144. "ADH-AES128-SHA";
  2145. if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList)
  2146. != WOLFSSL_SUCCESS) {
  2147. wolfSSL_CTX_free(ctx); ctx = NULL;
  2148. err_sys("client can't set cipher list 4");
  2149. }
  2150. }
  2151. #endif
  2152. if (useClientCert) {
  2153. useClientCert = 0;
  2154. }
  2155. }
  2156. #ifdef WOLFSSL_SCTP
  2157. if (dtlsSCTP)
  2158. wolfSSL_CTX_dtls_set_sctp(ctx);
  2159. #endif
  2160. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2161. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2162. #endif
  2163. #ifdef WOLFSSL_SNIFFER
  2164. if (cipherList == NULL && version < 4) {
  2165. /* static RSA or ECC cipher suites */
  2166. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2167. if (wolfSSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2168. wolfSSL_CTX_free(ctx); ctx = NULL;
  2169. err_sys("client can't set cipher list 3");
  2170. }
  2171. }
  2172. #endif
  2173. #ifdef HAVE_OCSP
  2174. if (useOcsp) {
  2175. #ifdef HAVE_IO_TIMEOUT
  2176. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  2177. #endif
  2178. if (ocspUrl != NULL) {
  2179. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2180. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2181. | WOLFSSL_OCSP_URL_OVERRIDE);
  2182. }
  2183. else {
  2184. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL);
  2185. }
  2186. #ifdef WOLFSSL_NONBLOCK_OCSP
  2187. wolfSSL_CTX_SetOCSP_Cb(ctx, OCSPIOCb, OCSPRespFreeCb, NULL);
  2188. #endif
  2189. }
  2190. #endif
  2191. #ifdef USER_CA_CB
  2192. wolfSSL_CTX_SetCACb(ctx, CaCb);
  2193. #endif
  2194. #if defined(HAVE_EXT_CACHE) && !defined(NO_SESSION_CACHE)
  2195. wolfSSL_CTX_sess_set_get_cb(ctx, mySessGetCb);
  2196. wolfSSL_CTX_sess_set_new_cb(ctx, mySessNewCb);
  2197. wolfSSL_CTX_sess_set_remove_cb(ctx, mySessRemCb);
  2198. #endif
  2199. #ifndef NO_CERTS
  2200. if (useClientCert && !loadCertKeyIntoSSLObj){
  2201. #ifndef TEST_LOAD_BUFFER
  2202. if (wolfSSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2203. != WOLFSSL_SUCCESS) {
  2204. wolfSSL_CTX_free(ctx); ctx = NULL;
  2205. err_sys("can't load client cert file, check file and run from"
  2206. " wolfSSL home dir");
  2207. }
  2208. #else
  2209. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2210. #endif
  2211. }
  2212. #ifdef HAVE_PK_CALLBACKS
  2213. pkCbInfo.ourKey = ourKey;
  2214. #endif
  2215. if (useClientCert && !loadCertKeyIntoSSLObj
  2216. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2217. && !pkCallbacks
  2218. #endif
  2219. ) {
  2220. #ifndef TEST_LOAD_BUFFER
  2221. if (wolfSSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  2222. != WOLFSSL_SUCCESS) {
  2223. wolfSSL_CTX_free(ctx); ctx = NULL;
  2224. err_sys("can't load client private key file, check file and run "
  2225. "from wolfSSL home dir");
  2226. }
  2227. #else
  2228. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  2229. #endif
  2230. }
  2231. if (!usePsk && !useAnon && !useVerifyCb && myVerifyAction != VERIFY_FORCE_FAIL) {
  2232. #ifndef TEST_LOAD_BUFFER
  2233. unsigned int verify_flags = 0;
  2234. #ifdef TEST_BEFORE_DATE
  2235. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2236. #endif
  2237. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0, verify_flags)
  2238. != WOLFSSL_SUCCESS) {
  2239. wolfSSL_CTX_free(ctx); ctx = NULL;
  2240. err_sys("can't load ca file, Please run from wolfSSL home dir");
  2241. }
  2242. #else
  2243. load_buffer(ctx, verifyCert, WOLFSSL_CA);
  2244. #endif /* !NO_FILESYSTEM */
  2245. #ifdef HAVE_ECC
  2246. /* load ecc verify too, echoserver uses it by default w/ ecc */
  2247. #ifndef TEST_LOAD_BUFFER
  2248. if (wolfSSL_CTX_load_verify_locations_ex(ctx, eccCertFile, 0, verify_flags)
  2249. != WOLFSSL_SUCCESS) {
  2250. wolfSSL_CTX_free(ctx); ctx = NULL;
  2251. err_sys("can't load ecc ca file, Please run from wolfSSL home dir");
  2252. }
  2253. #else
  2254. load_buffer(ctx, eccCertFile, WOLFSSL_CA);
  2255. #endif /* !TEST_LOAD_BUFFER */
  2256. #endif /* HAVE_ECC */
  2257. #if defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_FILESYSTEM)
  2258. if (trustCert) {
  2259. if ((ret = wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  2260. WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
  2261. wolfSSL_CTX_free(ctx); ctx = NULL;
  2262. err_sys("can't load trusted peer cert file");
  2263. }
  2264. }
  2265. #endif /* WOLFSSL_TRUST_PEER_CERT && !NO_FILESYSTEM */
  2266. }
  2267. if (useVerifyCb || myVerifyAction == VERIFY_FORCE_FAIL ||
  2268. myVerifyAction == VERIFY_USE_PREVERFIY) {
  2269. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  2270. }
  2271. else if (!usePsk && !useAnon && doPeerCheck == 0) {
  2272. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
  2273. }
  2274. else if (!usePsk && !useAnon && myVerifyAction == VERIFY_OVERRIDE_DATE_ERR) {
  2275. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  2276. }
  2277. #endif /* !NO_CERTS */
  2278. #ifdef WOLFSSL_ASYNC_CRYPT
  2279. ret = wolfAsync_DevOpen(&devId);
  2280. if (ret < 0) {
  2281. printf("Async device open failed\nRunning without async\n");
  2282. }
  2283. wolfSSL_CTX_UseAsync(ctx, devId);
  2284. #endif /* WOLFSSL_ASYNC_CRYPT */
  2285. #ifdef HAVE_SNI
  2286. if (sniHostName) {
  2287. if (wolfSSL_CTX_UseSNI(ctx, 0, sniHostName,
  2288. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS) {
  2289. wolfSSL_CTX_free(ctx); ctx = NULL;
  2290. err_sys("UseSNI failed");
  2291. }
  2292. }
  2293. #endif
  2294. #ifdef HAVE_MAX_FRAGMENT
  2295. if (maxFragment)
  2296. if (wolfSSL_CTX_UseMaxFragment(ctx, maxFragment) != WOLFSSL_SUCCESS) {
  2297. wolfSSL_CTX_free(ctx); ctx = NULL;
  2298. err_sys("UseMaxFragment failed");
  2299. }
  2300. #endif
  2301. #ifdef HAVE_TRUNCATED_HMAC
  2302. if (truncatedHMAC)
  2303. if (wolfSSL_CTX_UseTruncatedHMAC(ctx) != WOLFSSL_SUCCESS) {
  2304. wolfSSL_CTX_free(ctx); ctx = NULL;
  2305. err_sys("UseTruncatedHMAC failed");
  2306. }
  2307. #endif
  2308. #ifdef HAVE_SESSION_TICKET
  2309. if (wolfSSL_CTX_UseSessionTicket(ctx) != WOLFSSL_SUCCESS) {
  2310. wolfSSL_CTX_free(ctx); ctx = NULL;
  2311. err_sys("UseSessionTicket failed");
  2312. }
  2313. #endif
  2314. #ifdef HAVE_EXTENDED_MASTER
  2315. if (disableExtMasterSecret)
  2316. if (wolfSSL_CTX_DisableExtendedMasterSecret(ctx) != WOLFSSL_SUCCESS) {
  2317. wolfSSL_CTX_free(ctx); ctx = NULL;
  2318. err_sys("DisableExtendedMasterSecret failed");
  2319. }
  2320. #endif
  2321. #if defined(HAVE_SUPPORTED_CURVES)
  2322. #if defined(HAVE_CURVE25519)
  2323. if (useX25519) {
  2324. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X25519)
  2325. != WOLFSSL_SUCCESS) {
  2326. err_sys("unable to support X25519");
  2327. }
  2328. }
  2329. #endif /* HAVE_CURVE25519 */
  2330. #if defined(HAVE_CURVE448)
  2331. if (useX448) {
  2332. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X448)
  2333. != WOLFSSL_SUCCESS) {
  2334. err_sys("unable to support X448");
  2335. }
  2336. }
  2337. #endif /* HAVE_CURVE448 */
  2338. #ifdef HAVE_ECC
  2339. if (useSupCurve) {
  2340. #if !defined(NO_ECC_SECP) && \
  2341. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  2342. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP384R1)
  2343. != WOLFSSL_SUCCESS) {
  2344. err_sys("unable to support secp384r1");
  2345. }
  2346. #endif
  2347. #if !defined(NO_ECC_SECP) && \
  2348. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  2349. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1)
  2350. != WOLFSSL_SUCCESS) {
  2351. err_sys("unable to support secp256r1");
  2352. }
  2353. #endif
  2354. }
  2355. #endif /* HAVE_ECC */
  2356. #ifdef HAVE_FFDHE_2048
  2357. if (useSupCurve) {
  2358. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_FFDHE_2048)
  2359. != WOLFSSL_SUCCESS) {
  2360. err_sys("unable to support FFDHE 2048");
  2361. }
  2362. }
  2363. #endif
  2364. #endif /* HAVE_SUPPORTED_CURVES */
  2365. #ifdef WOLFSSL_TLS13
  2366. if (noPskDheKe)
  2367. wolfSSL_CTX_no_dhe_psk(ctx);
  2368. #endif
  2369. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2370. if (postHandAuth)
  2371. wolfSSL_CTX_allow_post_handshake_auth(ctx);
  2372. #endif
  2373. if (benchmark) {
  2374. ((func_args*)args)->return_code =
  2375. ClientBenchmarkConnections(ctx, host, port, dtlsUDP, dtlsSCTP,
  2376. benchmark, resumeSession, useX25519,
  2377. useX448, helloRetry, onlyKeyShare,
  2378. version, earlyData);
  2379. wolfSSL_CTX_free(ctx); ctx = NULL;
  2380. XEXIT_T(EXIT_SUCCESS);
  2381. }
  2382. if (throughput) {
  2383. ((func_args*)args)->return_code =
  2384. ClientBenchmarkThroughput(ctx, host, port, dtlsUDP, dtlsSCTP,
  2385. block, throughput, useX25519, useX448,
  2386. exitWithRet);
  2387. wolfSSL_CTX_free(ctx); ctx = NULL;
  2388. if (!exitWithRet)
  2389. XEXIT_T(EXIT_SUCCESS);
  2390. else
  2391. goto exit;
  2392. }
  2393. #if defined(WOLFSSL_MDK_ARM)
  2394. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
  2395. #endif
  2396. #if defined(OPENSSL_EXTRA)
  2397. if (wolfSSL_CTX_get_read_ahead(ctx) != 0) {
  2398. wolfSSL_CTX_free(ctx); ctx = NULL;
  2399. err_sys("bad read ahead default value");
  2400. }
  2401. if (wolfSSL_CTX_set_read_ahead(ctx, 1) != WOLFSSL_SUCCESS) {
  2402. wolfSSL_CTX_free(ctx); ctx = NULL;
  2403. err_sys("error setting read ahead value");
  2404. }
  2405. #endif
  2406. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2407. fprintf(stderr, "Before creating SSL\n");
  2408. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2409. err_sys("ctx not using static memory");
  2410. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2411. err_sys("error printing out memory stats");
  2412. #endif
  2413. if (doMcast) {
  2414. #ifdef WOLFSSL_MULTICAST
  2415. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  2416. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  2417. != WOLFSSL_SUCCESS) {
  2418. wolfSSL_CTX_free(ctx); ctx = NULL;
  2419. err_sys("Couldn't set multicast cipher list.");
  2420. }
  2421. #endif
  2422. }
  2423. #ifdef HAVE_PK_CALLBACKS
  2424. if (pkCallbacks)
  2425. SetupPkCallbacks(ctx);
  2426. #endif
  2427. ssl = wolfSSL_new(ctx);
  2428. if (ssl == NULL) {
  2429. wolfSSL_CTX_free(ctx); ctx = NULL;
  2430. err_sys("unable to get SSL object");
  2431. }
  2432. #ifndef NO_CERTS
  2433. if (useClientCert && loadCertKeyIntoSSLObj){
  2434. #ifndef TEST_LOAD_BUFFER
  2435. if (wolfSSL_use_certificate_chain_file(ssl, ourCert)
  2436. != WOLFSSL_SUCCESS) {
  2437. wolfSSL_CTX_free(ctx); ctx = NULL;
  2438. err_sys("can't load client cert file, check file and run from"
  2439. " wolfSSL home dir");
  2440. }
  2441. #else
  2442. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  2443. #endif
  2444. }
  2445. if (loadCertKeyIntoSSLObj
  2446. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2447. && !pkCallbacks
  2448. #endif
  2449. ) {
  2450. #ifndef TEST_LOAD_BUFFER
  2451. if (wolfSSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  2452. != WOLFSSL_SUCCESS) {
  2453. wolfSSL_CTX_free(ctx); ctx = NULL;
  2454. err_sys("can't load client private key file, check file and run "
  2455. "from wolfSSL home dir");
  2456. }
  2457. #else
  2458. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  2459. #endif
  2460. }
  2461. #endif /* !NO_CERTS */
  2462. #ifdef OPENSSL_EXTRA
  2463. wolfSSL_KeepArrays(ssl);
  2464. #endif
  2465. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2466. fprintf(stderr, "After creating SSL\n");
  2467. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2468. err_sys("ctx not using static memory");
  2469. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2470. err_sys("error printing out memory stats");
  2471. #endif
  2472. #ifdef WOLFSSL_TLS13
  2473. if (!helloRetry) {
  2474. #if defined(WOLFSSL_TLS13) && (!defined(NO_DH) || defined(HAVE_ECC) || \
  2475. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448))
  2476. if (onlyKeyShare == 0 || onlyKeyShare == 2) {
  2477. #ifdef HAVE_CURVE25519
  2478. if (useX25519) {
  2479. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519)
  2480. != WOLFSSL_SUCCESS) {
  2481. err_sys("unable to use curve x25519");
  2482. }
  2483. }
  2484. #endif
  2485. #ifdef HAVE_CURVE448
  2486. if (useX448) {
  2487. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X448)
  2488. != WOLFSSL_SUCCESS) {
  2489. err_sys("unable to use curve x448");
  2490. }
  2491. }
  2492. #endif
  2493. #ifdef HAVE_ECC
  2494. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  2495. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1)
  2496. != WOLFSSL_SUCCESS) {
  2497. err_sys("unable to use curve secp256r1");
  2498. }
  2499. #endif
  2500. #endif
  2501. }
  2502. if (onlyKeyShare == 0 || onlyKeyShare == 1) {
  2503. #ifdef HAVE_FFDHE_2048
  2504. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048)
  2505. != WOLFSSL_SUCCESS) {
  2506. err_sys("unable to use DH 2048-bit parameters");
  2507. }
  2508. #endif
  2509. }
  2510. #endif
  2511. }
  2512. else {
  2513. wolfSSL_NoKeyShares(ssl);
  2514. }
  2515. #endif
  2516. if (doMcast) {
  2517. #ifdef WOLFSSL_MULTICAST
  2518. /* DTLS multicast secret for testing only */
  2519. #define CLI_SRV_RANDOM_SZ 32 /* RAN_LEN (see internal.h) */
  2520. #define PMS_SZ 512 /* ENCRYPT_LEN (see internal.h) */
  2521. byte pms[PMS_SZ]; /* pre master secret */
  2522. byte cr[CLI_SRV_RANDOM_SZ]; /* client random */
  2523. byte sr[CLI_SRV_RANDOM_SZ]; /* server random */
  2524. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  2525. XMEMSET(pms, 0x23, sizeof(pms));
  2526. XMEMSET(cr, 0xA5, sizeof(cr));
  2527. XMEMSET(sr, 0x5A, sizeof(sr));
  2528. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  2529. != WOLFSSL_SUCCESS) {
  2530. wolfSSL_CTX_free(ctx); ctx = NULL;
  2531. err_sys("unable to set mcast secret");
  2532. }
  2533. #endif
  2534. }
  2535. #ifdef HAVE_SESSION_TICKET
  2536. wolfSSL_set_SessionTicket_cb(ssl, sessionTicketCB, (void*)"initial session");
  2537. #endif
  2538. #ifdef HAVE_TRUSTED_CA
  2539. if (trustedCaKeyId) {
  2540. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  2541. NULL, 0) != WOLFSSL_SUCCESS) {
  2542. err_sys("UseTrustedCA failed");
  2543. }
  2544. }
  2545. #endif
  2546. #ifdef HAVE_ALPN
  2547. if (alpnList != NULL) {
  2548. printf("ALPN accepted protocols list : %s\n", alpnList);
  2549. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  2550. }
  2551. #endif
  2552. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2553. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2554. if (statusRequest) {
  2555. if (version == 4 &&
  2556. (statusRequest == OCSP_STAPLINGV2 || \
  2557. statusRequest == OCSP_STAPLINGV2_MULTI)) {
  2558. err_sys("Cannot use OCSP Stapling V2 with TLSv1.3");
  2559. }
  2560. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS)
  2561. err_sys("can't enable OCSP Stapling Certificate Manager");
  2562. switch (statusRequest) {
  2563. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2564. case OCSP_STAPLING:
  2565. if (wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR_OCSP,
  2566. WOLFSSL_CSR_OCSP_USE_NONCE) != WOLFSSL_SUCCESS) {
  2567. wolfSSL_free(ssl); ssl = NULL;
  2568. wolfSSL_CTX_free(ctx); ctx = NULL;
  2569. err_sys("UseCertificateStatusRequest failed");
  2570. }
  2571. break;
  2572. #endif
  2573. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2574. case OCSP_STAPLINGV2:
  2575. if (wolfSSL_UseOCSPStaplingV2(ssl,
  2576. WOLFSSL_CSR2_OCSP, WOLFSSL_CSR2_OCSP_USE_NONCE)
  2577. != WOLFSSL_SUCCESS) {
  2578. wolfSSL_free(ssl); ssl = NULL;
  2579. wolfSSL_CTX_free(ctx); ctx = NULL;
  2580. err_sys("UseCertificateStatusRequest failed");
  2581. }
  2582. break;
  2583. case OCSP_STAPLINGV2_MULTI:
  2584. if (wolfSSL_UseOCSPStaplingV2(ssl,
  2585. WOLFSSL_CSR2_OCSP_MULTI, 0)
  2586. != WOLFSSL_SUCCESS) {
  2587. wolfSSL_free(ssl); ssl = NULL;
  2588. wolfSSL_CTX_free(ctx); ctx = NULL;
  2589. err_sys("UseCertificateStatusRequest failed");
  2590. }
  2591. break;
  2592. #endif
  2593. default:
  2594. err_sys("Invalid OCSP Stapling option");
  2595. }
  2596. wolfSSL_CTX_EnableOCSP(ctx, 0);
  2597. }
  2598. #endif
  2599. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  2600. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  2601. if (!doDhKeyCheck)
  2602. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  2603. #endif
  2604. #ifdef HAVE_ENCRYPT_THEN_MAC
  2605. if (disallowETM)
  2606. wolfSSL_AllowEncryptThenMac(ssl, 0);
  2607. #endif
  2608. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  2609. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2610. wolfSSL_free(ssl); ssl = NULL;
  2611. wolfSSL_CTX_free(ctx); ctx = NULL;
  2612. err_sys("error in setting fd");
  2613. }
  2614. /* STARTTLS */
  2615. if (doSTARTTLS) {
  2616. if (StartTLS_Init(&sockfd) != WOLFSSL_SUCCESS) {
  2617. wolfSSL_free(ssl); ssl = NULL;
  2618. wolfSSL_CTX_free(ctx); ctx = NULL;
  2619. err_sys("error during STARTTLS protocol");
  2620. }
  2621. }
  2622. #ifdef HAVE_CRL
  2623. if (disableCRL == 0 && !useVerifyCb) {
  2624. #ifdef HAVE_IO_TIMEOUT
  2625. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  2626. #endif
  2627. if (wolfSSL_EnableCRL(ssl, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS) {
  2628. wolfSSL_free(ssl); ssl = NULL;
  2629. wolfSSL_CTX_free(ctx); ctx = NULL;
  2630. err_sys("can't enable crl check");
  2631. }
  2632. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, 0)
  2633. != WOLFSSL_SUCCESS) {
  2634. wolfSSL_free(ssl); ssl = NULL;
  2635. wolfSSL_CTX_free(ctx); ctx = NULL;
  2636. err_sys("can't load crl, check crlfile and date validity");
  2637. }
  2638. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS) {
  2639. wolfSSL_free(ssl); ssl = NULL;
  2640. wolfSSL_CTX_free(ctx); ctx = NULL;
  2641. err_sys("can't set crl callback");
  2642. }
  2643. }
  2644. #endif
  2645. #ifdef HAVE_SECURE_RENEGOTIATION
  2646. if (scr) {
  2647. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  2648. wolfSSL_free(ssl); ssl = NULL;
  2649. wolfSSL_CTX_free(ctx); ctx = NULL;
  2650. err_sys("can't enable secure renegotiation");
  2651. }
  2652. }
  2653. #endif
  2654. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  2655. if (atomicUser)
  2656. SetupAtomicUser(ctx, ssl);
  2657. #endif
  2658. #ifdef HAVE_PK_CALLBACKS
  2659. if (pkCallbacks)
  2660. SetupPkCallbackContexts(ssl, &pkCbInfo);
  2661. #endif
  2662. if (matchName && doPeerCheck)
  2663. wolfSSL_check_domain_name(ssl, domain);
  2664. #ifndef WOLFSSL_CALLBACKS
  2665. if (nonBlocking) {
  2666. #ifdef WOLFSSL_DTLS
  2667. if (doDTLS) {
  2668. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  2669. }
  2670. #endif
  2671. tcp_set_nonblocking(&sockfd);
  2672. ret = NonBlockingSSL_Connect(ssl);
  2673. }
  2674. else {
  2675. #ifdef WOLFSSL_EARLY_DATA
  2676. if (usePsk && earlyData)
  2677. EarlyData(ctx, ssl, kEarlyMsg, sizeof(kEarlyMsg)-1, buffer);
  2678. #endif
  2679. do {
  2680. err = 0; /* reset error */
  2681. ret = wolfSSL_connect(ssl);
  2682. if (ret != WOLFSSL_SUCCESS) {
  2683. err = wolfSSL_get_error(ssl, 0);
  2684. #ifdef WOLFSSL_ASYNC_CRYPT
  2685. if (err == WC_PENDING_E) {
  2686. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2687. if (ret < 0) break;
  2688. }
  2689. #endif
  2690. }
  2691. } while (err == WC_PENDING_E);
  2692. }
  2693. #else
  2694. timeoutConnect.tv_sec = DEFAULT_TIMEOUT_SEC;
  2695. timeoutConnect.tv_usec = 0;
  2696. ret = NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  2697. #endif
  2698. if (ret != WOLFSSL_SUCCESS) {
  2699. err = wolfSSL_get_error(ssl, 0);
  2700. printf("wolfSSL_connect error %d, %s\n", err,
  2701. wolfSSL_ERR_error_string(err, buffer));
  2702. /* cleanup */
  2703. wolfSSL_free(ssl); ssl = NULL;
  2704. wolfSSL_CTX_free(ctx); ctx = NULL;
  2705. CloseSocket(sockfd);
  2706. if (!exitWithRet)
  2707. err_sys("wolfSSL_connect failed");
  2708. /* see note at top of README */
  2709. /* if you're getting an error here */
  2710. ((func_args*)args)->return_code = err;
  2711. goto exit;
  2712. }
  2713. showPeerEx(ssl, lng_index);
  2714. #ifdef OPENSSL_EXTRA
  2715. printf("Session timeout set to %ld seconds\n", wolfSSL_get_timeout(ssl));
  2716. {
  2717. byte* rnd;
  2718. byte* pt;
  2719. size_t size;
  2720. /* get size of buffer then print */
  2721. size = wolfSSL_get_client_random(NULL, NULL, 0);
  2722. if (size == 0) {
  2723. wolfSSL_free(ssl); ssl = NULL;
  2724. wolfSSL_CTX_free(ctx); ctx = NULL;
  2725. err_sys("error getting client random buffer size");
  2726. }
  2727. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2728. if (rnd == NULL) {
  2729. wolfSSL_free(ssl); ssl = NULL;
  2730. wolfSSL_CTX_free(ctx); ctx = NULL;
  2731. err_sys("error creating client random buffer");
  2732. }
  2733. size = wolfSSL_get_client_random(ssl, rnd, size);
  2734. if (size == 0) {
  2735. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2736. wolfSSL_free(ssl); ssl = NULL;
  2737. wolfSSL_CTX_free(ctx); ctx = NULL;
  2738. err_sys("error getting client random buffer");
  2739. }
  2740. printf("Client Random : ");
  2741. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  2742. printf("\n");
  2743. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2744. }
  2745. #if defined(OPENSSL_ALL)
  2746. /* print out session to stdout */
  2747. {
  2748. WOLFSSL_BIO* bio = wolfSSL_BIO_new_fp(stdout, BIO_NOCLOSE);
  2749. if (bio != NULL) {
  2750. if (wolfSSL_SESSION_print(bio, wolfSSL_get_session(ssl)) !=
  2751. WOLFSSL_SUCCESS) {
  2752. wolfSSL_BIO_printf(bio, "ERROR: Unable to print out session\n");
  2753. }
  2754. }
  2755. wolfSSL_BIO_free(bio);
  2756. }
  2757. #endif
  2758. #endif
  2759. if (doSTARTTLS && starttlsProt != NULL) {
  2760. if (XSTRNCMP(starttlsProt, "smtp", 4) == 0) {
  2761. if (SMTP_Shutdown(ssl, wc_shutdown) != WOLFSSL_SUCCESS) {
  2762. wolfSSL_free(ssl); ssl = NULL;
  2763. wolfSSL_CTX_free(ctx); ctx = NULL;
  2764. err_sys("error closing STARTTLS connection");
  2765. }
  2766. }
  2767. wolfSSL_free(ssl); ssl = NULL;
  2768. CloseSocket(sockfd);
  2769. wolfSSL_CTX_free(ctx); ctx = NULL;
  2770. ((func_args*)args)->return_code = 0;
  2771. return 0;
  2772. }
  2773. #ifdef HAVE_ALPN
  2774. if (alpnList != NULL) {
  2775. char *protocol_name = NULL;
  2776. word16 protocol_nameSz = 0;
  2777. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name, &protocol_nameSz);
  2778. if (err == WOLFSSL_SUCCESS)
  2779. printf("Received ALPN protocol : %s (%d)\n",
  2780. protocol_name, protocol_nameSz);
  2781. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  2782. printf("No ALPN response received (no match with server)\n");
  2783. else
  2784. printf("Getting ALPN protocol name failed\n");
  2785. }
  2786. #endif
  2787. #ifdef HAVE_SECURE_RENEGOTIATION
  2788. if (scr && forceScr) {
  2789. if (nonBlocking) {
  2790. printf("not doing secure renegotiation on example with"
  2791. " nonblocking yet\n");
  2792. } else {
  2793. if (!resumeScr) {
  2794. printf("Beginning secure renegotiation.\n");
  2795. if ((ret = wolfSSL_Rehandshake(ssl)) != WOLFSSL_SUCCESS) {
  2796. err = wolfSSL_get_error(ssl, 0);
  2797. #ifdef WOLFSSL_ASYNC_CRYPT
  2798. while (err == WC_PENDING_E) {
  2799. err = 0;
  2800. ret = wolfSSL_negotiate(ssl);
  2801. if (ret != WOLFSSL_SUCCESS) {
  2802. err = wolfSSL_get_error(ssl, 0);
  2803. if (err == WC_PENDING_E) {
  2804. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2805. if (ret < 0) break;
  2806. }
  2807. }
  2808. }
  2809. #endif
  2810. if (ret != WOLFSSL_SUCCESS) {
  2811. printf("err = %d, %s\n", err,
  2812. wolfSSL_ERR_error_string(err, buffer));
  2813. wolfSSL_free(ssl); ssl = NULL;
  2814. wolfSSL_CTX_free(ctx); ctx = NULL;
  2815. err_sys("wolfSSL_Rehandshake failed");
  2816. }
  2817. }
  2818. else {
  2819. printf("RENEGOTIATION SUCCESSFUL\n");
  2820. }
  2821. }
  2822. else {
  2823. printf("Beginning secure resumption.\n");
  2824. if ((ret = wolfSSL_SecureResume(ssl)) != WOLFSSL_SUCCESS) {
  2825. err = wolfSSL_get_error(ssl, 0);
  2826. #ifdef WOLFSSL_ASYNC_CRYPT
  2827. while (err == WC_PENDING_E) {
  2828. err = 0;
  2829. ret = wolfSSL_negotiate(ssl);
  2830. if (ret != WOLFSSL_SUCCESS) {
  2831. err = wolfSSL_get_error(ssl, 0);
  2832. if (err == WC_PENDING_E) {
  2833. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2834. if (ret < 0) break;
  2835. }
  2836. }
  2837. }
  2838. #endif
  2839. if (ret != WOLFSSL_SUCCESS) {
  2840. printf("err = %d, %s\n", err,
  2841. wolfSSL_ERR_error_string(err, buffer));
  2842. wolfSSL_free(ssl); ssl = NULL;
  2843. wolfSSL_CTX_free(ctx); ctx = NULL;
  2844. err_sys("wolfSSL_SecureResume failed");
  2845. }
  2846. }
  2847. else {
  2848. printf("SECURE RESUMPTION SUCCESSFUL\n");
  2849. }
  2850. }
  2851. }
  2852. }
  2853. #endif /* HAVE_SECURE_RENEGOTIATION */
  2854. XMEMSET(msg, 0, sizeof(msg));
  2855. if (sendGET) {
  2856. printf("SSL connect ok, sending GET...\n");
  2857. msgSz = (int)XSTRLEN(kHttpGetMsg);
  2858. XMEMCPY(msg, kHttpGetMsg, msgSz);
  2859. }
  2860. else {
  2861. msgSz = (int)XSTRLEN(kHelloMsg);
  2862. XMEMCPY(msg, kHelloMsg, msgSz);
  2863. }
  2864. /* allow some time for exporting the session */
  2865. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  2866. TEST_DELAY();
  2867. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  2868. #ifdef WOLFSSL_TLS13
  2869. if (updateKeysIVs)
  2870. wolfSSL_update_keys(ssl);
  2871. #endif
  2872. err = ClientWrite(ssl, msg, msgSz, "", exitWithRet);
  2873. if (exitWithRet && (err != 0)) {
  2874. ((func_args*)args)->return_code = err;
  2875. wolfSSL_free(ssl); ssl = NULL;
  2876. wolfSSL_CTX_free(ctx); ctx = NULL;
  2877. goto exit;
  2878. }
  2879. err = ClientRead(ssl, reply, sizeof(reply)-1, 1, "", exitWithRet);
  2880. if (exitWithRet && (err != 0)) {
  2881. ((func_args*)args)->return_code = err;
  2882. wolfSSL_free(ssl); ssl = NULL;
  2883. wolfSSL_CTX_free(ctx); ctx = NULL;
  2884. goto exit;
  2885. }
  2886. #if defined(WOLFSSL_TLS13)
  2887. if (updateKeysIVs || postHandAuth)
  2888. (void)ClientWrite(ssl, msg, msgSz, "", 0);
  2889. #endif
  2890. #ifndef NO_SESSION_CACHE
  2891. if (resumeSession) {
  2892. session = wolfSSL_get_session(ssl);
  2893. }
  2894. #endif
  2895. #if !defined(NO_SESSION_CACHE) && (defined(OPENSSL_EXTRA) || \
  2896. defined(HAVE_EXT_CACHE))
  2897. if (session != NULL && resumeSession) {
  2898. flatSessionSz = wolfSSL_i2d_SSL_SESSION(session, NULL);
  2899. if (flatSessionSz != 0) {
  2900. int checkSz = wolfSSL_i2d_SSL_SESSION(session, &flatSession);
  2901. if (flatSession == NULL)
  2902. err_sys("error creating flattened session buffer");
  2903. if (checkSz != flatSessionSz) {
  2904. XFREE(flatSession, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2905. err_sys("flat session size check failure");
  2906. }
  2907. }
  2908. }
  2909. #endif
  2910. if (dtlsUDP == 0) { /* don't send alert after "break" command */
  2911. ret = wolfSSL_shutdown(ssl);
  2912. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  2913. if (tcp_select(sockfd, DEFAULT_TIMEOUT_SEC) == TEST_RECV_READY) {
  2914. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  2915. if (ret == WOLFSSL_SUCCESS)
  2916. printf("Bidirectional shutdown complete\n");
  2917. }
  2918. if (ret != WOLFSSL_SUCCESS)
  2919. printf("Bidirectional shutdown failed\n");
  2920. }
  2921. }
  2922. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  2923. if (atomicUser)
  2924. FreeAtomicUser(ssl);
  2925. #endif
  2926. /* display collected statistics */
  2927. #ifdef WOLFSSL_STATIC_MEMORY
  2928. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  2929. err_sys("static memory was not used with ssl");
  2930. fprintf(stderr, "\nprint off SSL memory stats\n");
  2931. fprintf(stderr, "*** This is memory state before wolfSSL_free is called\n");
  2932. fprintf(stderr, "peak connection memory = %d\n", ssl_stats.peakMem);
  2933. fprintf(stderr, "current memory in use = %d\n", ssl_stats.curMem);
  2934. fprintf(stderr, "peak connection allocs = %d\n", ssl_stats.peakAlloc);
  2935. fprintf(stderr, "current connection allocs = %d\n", ssl_stats.curAlloc);
  2936. fprintf(stderr, "total connection allocs = %d\n", ssl_stats.totalAlloc);
  2937. fprintf(stderr, "total connection frees = %d\n\n", ssl_stats.totalFr);
  2938. #endif
  2939. wolfSSL_free(ssl); ssl = NULL;
  2940. CloseSocket(sockfd);
  2941. #ifndef NO_SESSION_CACHE
  2942. if (resumeSession) {
  2943. sslResume = wolfSSL_new(ctx);
  2944. if (sslResume == NULL) {
  2945. wolfSSL_CTX_free(ctx); ctx = NULL;
  2946. err_sys("unable to get SSL object");
  2947. }
  2948. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  2949. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  2950. if (!doDhKeyCheck)
  2951. wolfSSL_SetEnableDhKeyTest(sslResume, 0);
  2952. #endif
  2953. if (dtlsUDP) {
  2954. TEST_DELAY();
  2955. }
  2956. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, sslResume);
  2957. if (wolfSSL_set_fd(sslResume, sockfd) != WOLFSSL_SUCCESS) {
  2958. wolfSSL_free(sslResume); sslResume = NULL;
  2959. wolfSSL_CTX_free(ctx); ctx = NULL;
  2960. err_sys("error in setting fd");
  2961. }
  2962. #ifdef HAVE_ALPN
  2963. if (alpnList != NULL) {
  2964. printf("ALPN accepted protocols list : %s\n", alpnList);
  2965. wolfSSL_UseALPN(sslResume, alpnList, (word32)XSTRLEN(alpnList),
  2966. alpn_opt);
  2967. }
  2968. #endif
  2969. #ifdef HAVE_SECURE_RENEGOTIATION
  2970. if (scr) {
  2971. if (wolfSSL_UseSecureRenegotiation(sslResume) != WOLFSSL_SUCCESS) {
  2972. wolfSSL_free(sslResume); sslResume = NULL;
  2973. wolfSSL_CTX_free(ctx); ctx = NULL;
  2974. err_sys("can't enable secure renegotiation");
  2975. }
  2976. }
  2977. #endif
  2978. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  2979. if (flatSession) {
  2980. const byte* constFlatSession = flatSession;
  2981. session = wolfSSL_d2i_SSL_SESSION(NULL,
  2982. &constFlatSession, flatSessionSz);
  2983. }
  2984. #endif
  2985. wolfSSL_set_session(sslResume, session);
  2986. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  2987. if (flatSession) {
  2988. XFREE(flatSession, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2989. wolfSSL_SESSION_free(session);
  2990. }
  2991. #endif
  2992. #ifdef HAVE_SESSION_TICKET
  2993. wolfSSL_set_SessionTicket_cb(sslResume, sessionTicketCB,
  2994. (void*)"resumed session");
  2995. #endif
  2996. #ifndef WOLFSSL_CALLBACKS
  2997. if (nonBlocking) {
  2998. #ifdef WOLFSSL_DTLS
  2999. if (doDTLS) {
  3000. wolfSSL_dtls_set_using_nonblock(sslResume, 1);
  3001. }
  3002. #endif
  3003. tcp_set_nonblocking(&sockfd);
  3004. ret = NonBlockingSSL_Connect(sslResume);
  3005. }
  3006. else {
  3007. #ifdef WOLFSSL_EARLY_DATA
  3008. #ifndef HAVE_SESSION_TICKET
  3009. if (!usePsk) {
  3010. }
  3011. else
  3012. #endif
  3013. if (earlyData) {
  3014. EarlyData(ctx, sslResume, kEarlyMsg, sizeof(kEarlyMsg)-1, buffer);
  3015. }
  3016. #endif
  3017. do {
  3018. err = 0; /* reset error */
  3019. ret = wolfSSL_connect(sslResume);
  3020. if (ret != WOLFSSL_SUCCESS) {
  3021. err = wolfSSL_get_error(sslResume, 0);
  3022. #ifdef WOLFSSL_ASYNC_CRYPT
  3023. if (err == WC_PENDING_E) {
  3024. ret = wolfSSL_AsyncPoll(sslResume,
  3025. WOLF_POLL_FLAG_CHECK_HW);
  3026. if (ret < 0) break;
  3027. }
  3028. #endif
  3029. }
  3030. } while (err == WC_PENDING_E);
  3031. }
  3032. #else
  3033. timeoutConnect.tv_sec = DEFAULT_TIMEOUT_SEC;
  3034. timeoutConnect.tv_usec = 0;
  3035. ret = NonBlockingSSL_Connect(sslResume); /* will keep retrying on timeout */
  3036. #endif
  3037. if (ret != WOLFSSL_SUCCESS) {
  3038. printf("wolfSSL_connect resume error %d, %s\n", err,
  3039. wolfSSL_ERR_error_string(err, buffer));
  3040. wolfSSL_free(sslResume); sslResume = NULL;
  3041. wolfSSL_CTX_free(ctx); ctx = NULL;
  3042. err_sys("wolfSSL_connect resume failed");
  3043. }
  3044. showPeerEx(sslResume, lng_index);
  3045. if (wolfSSL_session_reused(sslResume))
  3046. printf("reused session id\n");
  3047. else
  3048. printf("didn't reuse session id!!!\n");
  3049. #ifdef HAVE_ALPN
  3050. if (alpnList != NULL) {
  3051. char *protocol_name = NULL;
  3052. word16 protocol_nameSz = 0;
  3053. printf("Sending ALPN accepted list : %s\n", alpnList);
  3054. err = wolfSSL_ALPN_GetProtocol(sslResume, &protocol_name,
  3055. &protocol_nameSz);
  3056. if (err == WOLFSSL_SUCCESS)
  3057. printf("Received ALPN protocol : %s (%d)\n",
  3058. protocol_name, protocol_nameSz);
  3059. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  3060. printf("Not received ALPN response (no match with server)\n");
  3061. else
  3062. printf("Getting ALPN protocol name failed\n");
  3063. }
  3064. #endif
  3065. /* allow some time for exporting the session */
  3066. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  3067. TEST_DELAY();
  3068. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  3069. #ifdef HAVE_SECURE_RENEGOTIATION
  3070. if (scr && forceScr) {
  3071. if (nonBlocking) {
  3072. printf("not doing secure renegotiation on example with"
  3073. " nonblocking yet\n");
  3074. } else {
  3075. if (!resumeScr) {
  3076. printf("Beginning secure renegotiation.\n");
  3077. if (wolfSSL_Rehandshake(sslResume) != WOLFSSL_SUCCESS) {
  3078. err = wolfSSL_get_error(sslResume, 0);
  3079. printf("err = %d, %s\n", err,
  3080. wolfSSL_ERR_error_string(err, buffer));
  3081. wolfSSL_free(sslResume); sslResume = NULL;
  3082. wolfSSL_CTX_free(ctx); ctx = NULL;
  3083. err_sys("wolfSSL_Rehandshake failed");
  3084. }
  3085. else {
  3086. printf("RENEGOTIATION SUCCESSFUL\n");
  3087. }
  3088. }
  3089. else {
  3090. printf("Beginning secure resumption.\n");
  3091. if (wolfSSL_SecureResume(sslResume) != WOLFSSL_SUCCESS) {
  3092. err = wolfSSL_get_error(sslResume, 0);
  3093. printf("err = %d, %s\n", err,
  3094. wolfSSL_ERR_error_string(err, buffer));
  3095. wolfSSL_free(sslResume); sslResume = NULL;
  3096. wolfSSL_CTX_free(ctx); ctx = NULL;
  3097. err_sys("wolfSSL_SecureResume failed");
  3098. }
  3099. else {
  3100. printf("SECURE RESUMPTION SUCCESSFUL\n");
  3101. }
  3102. }
  3103. }
  3104. }
  3105. #endif /* HAVE_SECURE_RENEGOTIATION */
  3106. XMEMSET(msg, 0, sizeof(msg));
  3107. if (sendGET) {
  3108. msgSz = (int)XSTRLEN(kHttpGetMsg);
  3109. XMEMCPY(msg, kHttpGetMsg, msgSz);
  3110. }
  3111. else {
  3112. msgSz = (int)XSTRLEN(kResumeMsg);
  3113. XMEMCPY(msg, kResumeMsg, msgSz);
  3114. }
  3115. (void)ClientWrite(sslResume, msg, msgSz, " resume", 0);
  3116. (void)ClientRead(sslResume, reply, sizeof(reply)-1, sendGET,
  3117. "Server resume: ", 0);
  3118. ret = wolfSSL_shutdown(sslResume);
  3119. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  3120. wolfSSL_shutdown(sslResume); /* bidirectional shutdown */
  3121. /* display collected statistics */
  3122. #ifdef WOLFSSL_STATIC_MEMORY
  3123. if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
  3124. err_sys("static memory was not used with ssl");
  3125. fprintf(stderr, "\nprint off SSLresume memory stats\n");
  3126. fprintf(stderr, "*** This is memory state before wolfSSL_free is called\n");
  3127. fprintf(stderr, "peak connection memory = %d\n", ssl_stats.peakMem);
  3128. fprintf(stderr, "current memory in use = %d\n", ssl_stats.curMem);
  3129. fprintf(stderr, "peak connection allocs = %d\n", ssl_stats.peakAlloc);
  3130. fprintf(stderr, "current connection allocs = %d\n", ssl_stats.curAlloc);
  3131. fprintf(stderr, "total connection allocs = %d\n", ssl_stats.totalAlloc);
  3132. fprintf(stderr, "total connection frees = %d\n\n", ssl_stats.totalFr);
  3133. #endif
  3134. wolfSSL_free(sslResume); sslResume = NULL;
  3135. CloseSocket(sockfd);
  3136. }
  3137. #endif /* !NO_SESSION_CACHE */
  3138. wolfSSL_CTX_free(ctx); ctx = NULL;
  3139. ((func_args*)args)->return_code = 0;
  3140. exit:
  3141. #ifdef WOLFSSL_ASYNC_CRYPT
  3142. wolfAsync_DevClose(&devId);
  3143. #endif
  3144. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  3145. && defined(HAVE_STACK_SIZE)
  3146. wc_ecc_fp_free(); /* free per thread cache */
  3147. #endif
  3148. /* There are use cases when these assignments are not read. To avoid
  3149. * potential confusion those warnings have been handled here.
  3150. */
  3151. (void) useClientCert;
  3152. (void) verifyCert;
  3153. (void) ourCert;
  3154. (void) ourKey;
  3155. (void) useVerifyCb;
  3156. #if !defined(WOLFSSL_TIRTOS)
  3157. return 0;
  3158. #endif
  3159. }
  3160. #endif /* !NO_WOLFSSL_CLIENT */
  3161. /* so overall tests can pull in test function */
  3162. #ifndef NO_MAIN_DRIVER
  3163. int main(int argc, char** argv)
  3164. {
  3165. func_args args;
  3166. StartTCP();
  3167. args.argc = argc;
  3168. args.argv = argv;
  3169. args.return_code = 0;
  3170. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL) && !defined(STACK_TRAP)
  3171. wolfSSL_Debugging_ON();
  3172. #endif
  3173. wolfSSL_Init();
  3174. ChangeToWolfRoot();
  3175. #ifndef NO_WOLFSSL_CLIENT
  3176. #ifdef HAVE_STACK_SIZE
  3177. StackSizeCheck(&args, client_test);
  3178. #else
  3179. client_test(&args);
  3180. #endif
  3181. #else
  3182. printf("Client not compiled in!\n");
  3183. #endif
  3184. wolfSSL_Cleanup();
  3185. #ifdef HAVE_WNR
  3186. if (wc_FreeNetRandom() < 0)
  3187. err_sys("Failed to free netRandom context");
  3188. #endif /* HAVE_WNR */
  3189. return args.return_code;
  3190. }
  3191. int myoptind = 0;
  3192. char* myoptarg = NULL;
  3193. #endif /* NO_MAIN_DRIVER */