client.c 162 KB

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