client.c 131 KB

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