1
0

client.c 158 KB

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