client.c 149 KB

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