2
0

client.c 157 KB

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