client.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675
  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[][75] = {
  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. "\n"
  1205. "For simpler wolfSSL TLS client examples, visit\n"
  1206. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n", /* 75 */
  1207. NULL,
  1208. },
  1209. #ifndef NO_MULTIBYTE_PRINT
  1210. /* Japanese */
  1211. {
  1212. " 注意 : 全てのファイルは wolfSSL ホーム・ディレクトリからの相対です。"
  1213. "\n", /* 0 */
  1214. "RSAの最大ビットは次のように設定されています: ", /* 1 */
  1215. #ifdef NO_RSA
  1216. "RSAはサポートされていません。\n", /* 2 */
  1217. #elif defined(WOLFSSL_SP_MATH) /* case of SP math only */
  1218. #ifndef WOLFSSL_SP_NO_3072
  1219. "3072\n", /* 2 */
  1220. #elif !defined(WOLFSSL_SP_NO_2048)
  1221. "2048\n", /* 2 */
  1222. #else
  1223. "0\n", /* 2 */
  1224. #endif
  1225. #elif defined(USE_FAST_MATH)
  1226. #else
  1227. "無限\n", /* 2 */
  1228. #endif
  1229. "-? <num> ヘルプ, 使い方を表示\n"
  1230. " 0: 英語、 1: 日本語\n"
  1231. "--ヘルプ 日本語で使い方を表示\n", /* 3 */
  1232. "-h <host> 接続先ホスト, 既定値", /* 4 */
  1233. "-p <num> 接続先ポート, 0は無効, 既定値", /* 5 */
  1234. #ifndef WOLFSSL_TLS13
  1235. "-v <num> SSL バージョン [0-3], SSLv3(0) - TLS1.2(3)),"
  1236. " 既定値", /* 6 */
  1237. "-V 有効な ssl バージョン番号を出力, SSLv3(0) -"
  1238. " TLS1.2(3)\n", /* 7 */
  1239. #else
  1240. "-v <num> SSL バージョン [0-4], SSLv3(0) - TLS1.3(4)),"
  1241. " 既定値", /* 6 */
  1242. "-V 有効な ssl バージョン番号を出力, SSLv3(0) -"
  1243. " TLS1.3(4)\n", /* 7 */
  1244. #endif
  1245. "-l <str> 暗号スイートリスト (区切り文字 :)\n", /* 8 */
  1246. #ifndef NO_CERTS
  1247. "-c <file> 証明書ファイル, 既定値", /* 9 */
  1248. "-k <file> 鍵ファイル, 既定値", /* 10 */
  1249. "-A <file> 認証局ファイル, 既定値", /* 11 */
  1250. #endif
  1251. #ifndef NO_DH
  1252. "-Z <num> 最小 DH 鍵 ビット, 既定値", /* 12 */
  1253. #endif
  1254. "-b <num> ベンチマーク <num> 接続及び結果出力する\n", /* 13 */
  1255. #ifdef HAVE_ALPN
  1256. "-L <str> アプリケーション層プロトコルネゴシエーションを行う"
  1257. " ({C,F}:<list>)\n", /* 14 */
  1258. #endif
  1259. "-B <num> <num> バイトを用いてのベンチマーク・スループット測定"
  1260. "と結果を出力する\n", /* 15 */
  1261. #ifndef NO_PSK
  1262. "-s 事前共有鍵を使用する\n", /* 16 */
  1263. #endif
  1264. "-d ピア確認を無効とする\n", /* 17 */
  1265. "-D 日付エラー用コールバック例の上書きを行う\n", /* 18 */
  1266. "-e 利用可能な全ての暗号スイートをリスト, \n", /* 19 */
  1267. "-g サーバーへ HTTP GET を送信\n", /* 20 */
  1268. "-u UDP DTLSを使用する。\n"
  1269. #ifndef WOLFSSL_DTLS13
  1270. " -v 2 を追加指定するとDTLSv1, "
  1271. "-v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 21 */
  1272. #else
  1273. " -v 2 を追加指定するとDTLSv1, "
  1274. "-v 3 を追加指定すると DTLSv1.2 (既定値),\n"
  1275. " -v 4 を追加指定すると DTLSv1.3\n", /* 21 */
  1276. #endif /* !WOLFSSL_DTLS13 */
  1277. #ifdef WOLFSSL_SCTP
  1278. "-G SCTP DTLSを使用する。-v 2 を追加指定すると"
  1279. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 22 */
  1280. #endif
  1281. #ifndef NO_CERTS
  1282. "-m 証明書内のドメイン名一致を確認する\n", /* 23 */
  1283. #endif
  1284. "-N ノンブロッキング・ソケットを使用する\n", /* 24 */
  1285. #ifndef NO_SESSION_CACHE
  1286. "-r セッションを継続する\n", /* 25 */
  1287. #endif
  1288. "-w 双方向シャットダウンを待つ\n", /* 26 */
  1289. "-M <prot> STARTTLSを使用する, <prot>プロトコル(smtp)を"
  1290. "使用する\n", /* 27 */
  1291. #ifdef HAVE_SECURE_RENEGOTIATION
  1292. "-R セキュアな再ネゴシエーションを許可する\n", /* 28 */
  1293. "-i <str> クライアント主導のネゴシエーションを強制する\n", /* 29 */
  1294. #endif
  1295. "-f より少ないパケット/グループメッセージを使用する\n",/* 30 */
  1296. #ifndef NO_CERTS
  1297. "-x クライアントの証明書/鍵のロードを無効する\n", /* 31 */
  1298. #endif
  1299. "-X 外部テスト・ケースにより動作する\n", /* 32 */
  1300. "-j コールバック・オーバーライドの検証を使用する\n", /* 33 */
  1301. #ifdef SHOW_SIZES
  1302. "-z 構造体のサイズを表示する\n", /* 34 */
  1303. #endif
  1304. #ifdef HAVE_SNI
  1305. "-S <str> ホスト名表示を使用する\n", /* 35 */
  1306. #endif
  1307. #ifdef HAVE_MAX_FRAGMENT
  1308. "-F <num> 最大フラグメント長[1-6]を設定する\n", /* 36 */
  1309. #endif
  1310. #ifdef HAVE_TRUNCATED_HMAC
  1311. "-T Truncated HMACを使用する\n", /* 37 */
  1312. #endif
  1313. #ifdef HAVE_EXTENDED_MASTER
  1314. "-n マスターシークレット拡張を無効にする\n", /* 38 */
  1315. #endif
  1316. #ifdef HAVE_OCSP
  1317. "-o OCSPルックアップをピア証明書で実施する\n", /* 39 */
  1318. "-O <url> OCSPルックアップを、<url>を使用し"
  1319. "応答者として実施する\n", /* 40 */
  1320. #endif
  1321. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1322. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1323. "-W <num> OCSP Staplingを使用する"
  1324. " (1 v1, 2 v2, 3 v2 multi)\n", /* 41 */
  1325. " 'm' を最後に指定すると必ず staple を使用する\n" /* 42 */
  1326. #endif
  1327. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1328. "-U アトミック・ユーザー記録の"
  1329. "コールバックを利用する\n", /* 43 */
  1330. #endif
  1331. #ifdef HAVE_PK_CALLBACKS
  1332. "-P 公開鍵コールバック\n", /* 44 */
  1333. #endif
  1334. #ifdef HAVE_ANON
  1335. "-a 匿名クライアント\n", /* 45 */
  1336. #endif
  1337. #ifdef HAVE_CRL
  1338. "-C CRLを無効\n", /* 46 */
  1339. #endif
  1340. #ifdef WOLFSSL_TRUST_PEER_CERT
  1341. "-E <file> 信頼出来るピアの証明書ロードの為のパス\n", /* 47 */
  1342. #endif
  1343. #ifdef HAVE_WNR
  1344. "-q <file> Whitewood コンフィグファイル, 既定値\n", /* 48 */
  1345. #endif
  1346. "-H <arg> 内部テスト"
  1347. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 49 */
  1348. " loadSSL, disallowETM]\n", /* 50 */
  1349. #ifdef WOLFSSL_TLS13
  1350. "-J HelloRetryRequestをKEのグループ選択に使用する\n", /* 51 */
  1351. "-K 鍵交換にPSKを使用、(EC)DHEは使用しない\n", /* 52 */
  1352. "-I データ送信前に、鍵とIVを更新する\n", /* 53 */
  1353. #ifndef NO_DH
  1354. "-y FFDHE名前付きグループとの鍵共有のみ\n", /* 54 */
  1355. #endif
  1356. #ifdef HAVE_ECC
  1357. "-Y ECC名前付きグループとの鍵共有のみ\n", /* 55 */
  1358. #endif
  1359. #endif /* WOLFSSL_TLS13 */
  1360. #ifdef HAVE_CURVE25519
  1361. "-t X25519を鍵交換に使用する\n", /* 56 */
  1362. #endif
  1363. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1364. "-Q ポストハンドシェークの証明要求をサポートする\n", /* 57 */
  1365. #endif
  1366. #ifdef WOLFSSL_EARLY_DATA
  1367. "-0 Early data をサーバーへ送信する"
  1368. "(0-RTTハンドシェイク)\n", /* 58 */
  1369. #endif
  1370. #ifdef WOLFSSL_MULTICAST
  1371. "-3 <grpid> マルチキャスト, grpid < 256\n", /* 59 */
  1372. #endif
  1373. "-1 <num> 指定された言語で結果を表示します。\n"
  1374. " 0: 英語、 1: 日本語\n", /* 60 */
  1375. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1376. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1377. "-2 DHプライム番号チェックを無効にする\n", /* 61 */
  1378. #endif
  1379. #ifdef HAVE_SECURE_RENEGOTIATION
  1380. "-4 再交渉に再開を使用\n", /* 62 */
  1381. #endif
  1382. #ifdef HAVE_TRUSTED_CA
  1383. "-5 信頼できる認証局の鍵表示を使用する\n", /* 63 */
  1384. #endif
  1385. "-6 WANT_WRITE エラーを全てのIO 送信でシミュレートします\n",
  1386. #ifdef HAVE_CURVE448
  1387. "-8 鍵交換に X448 を使用する\n", /* 66 */
  1388. #endif
  1389. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1390. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1391. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1392. "-9 証明書の読み込みに hash dir 機能を使用する\n"
  1393. " <wolfSSL home>/certs フォルダーからロードします\n"
  1394. " フォルダー中のファイルは、\"hash.N\"[N:0-9]名である必要があります\n"
  1395. " 以下の例ではca-cert.pemにシンボリックリンクを設定します\n"
  1396. " ln -s ca-cert.pem `openssl x509 -in ca-cert.pem -hash -noout`.0\n",
  1397. /* 67 */
  1398. #endif
  1399. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1400. !defined(WOLFSENTRY_NO_JSON)
  1401. "--wolfsentry-config <file> wolfSentry コンフィグファイル\n",
  1402. /* 68 */
  1403. #endif
  1404. #ifndef WOLFSSL_TLS13
  1405. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-3] "
  1406. " SSLv3(0) - TLS1.2(3)\n",
  1407. #else
  1408. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-4] "
  1409. " SSLv3(0) - TLS1.3(4)\n", /* 69 */
  1410. #endif
  1411. #ifdef HAVE_PQC
  1412. "--pqc <alg> post-quantum 名前付きグループとの鍵共有のみ [KYBER_LEVEL1, KYBER_LEVEL3,\n"
  1413. " KYBER_LEVEL5, P256_KYBER_LEVEL1, P384_KYBER_LEVEL3, P521_KYBER_LEVEL5]\n", /* 70 */
  1414. #endif
  1415. #ifdef WOLFSSL_SRTP
  1416. "--srtp <profile> (デフォルトは SRTP_AES128_CM_SHA1_80)\n", /* 71 */
  1417. #endif
  1418. #ifdef WOLFSSL_SYS_CA_CERTS
  1419. "--sys-ca-certs Load system CA certs for server cert verification\n", /* 72 */
  1420. #endif
  1421. #ifdef HAVE_SUPPORTED_CURVES
  1422. "--onlyPskDheKe Must use DHE key exchange with PSK\n", /* 73 */
  1423. #endif
  1424. #ifndef NO_PSK
  1425. "--openssl-psk Use TLS 1.3 PSK callback compatible with OpenSSL\n", /* 74 */
  1426. #endif
  1427. "\n"
  1428. "より簡単なwolfSSL TLS クライアントの例については"
  1429. "下記にアクセスしてください\n"
  1430. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n", /* 75 */
  1431. NULL,
  1432. },
  1433. #endif
  1434. };
  1435. static void showPeerPEM(WOLFSSL* ssl)
  1436. {
  1437. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && defined(WOLFSSL_CERT_GEN)
  1438. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  1439. if (peer) {
  1440. WOLFSSL_BIO* bioOut = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  1441. if (bioOut == NULL) {
  1442. LOG_ERROR("failed to get bio on stdout\n");
  1443. }
  1444. else {
  1445. if (wolfSSL_BIO_set_fp(bioOut, stdout, BIO_NOCLOSE)
  1446. != WOLFSSL_SUCCESS) {
  1447. LOG_ERROR("failed to set stdout to bio output\n");
  1448. wolfSSL_BIO_free(bioOut);
  1449. bioOut = NULL;
  1450. }
  1451. }
  1452. if (bioOut) {
  1453. wolfSSL_BIO_write(bioOut, "---\nServer certificate\n",
  1454. XSTRLEN("---\nServer certificate\n"));
  1455. wolfSSL_PEM_write_bio_X509(bioOut, peer);
  1456. }
  1457. wolfSSL_BIO_free(bioOut);
  1458. }
  1459. wolfSSL_FreeX509(peer);
  1460. #endif /* OPENSSL_ALL && WOLFSSL_CERT_GEN && !NO_BIO */
  1461. (void)ssl;
  1462. }
  1463. static void Usage(void)
  1464. {
  1465. int msgid = 0;
  1466. const char** msg = client_usage_msg[lng_index];
  1467. printf("%s%s%s", "wolfSSL client ", LIBWOLFSSL_VERSION_STRING,
  1468. msg[msgid]);
  1469. /* print out so that scripts can know what the max supported key size is */
  1470. printf("%s", msg[++msgid]);
  1471. #ifdef NO_RSA
  1472. printf("%s", msg[++msgid]);
  1473. #elif defined(WOLFSSL_SP_MATH) /* case of SP math only */
  1474. #ifndef WOLFSSL_SP_NO_3072
  1475. printf("%s", msg[++msgid]);
  1476. #elif !defined(WOLFSSL_SP_NO_2048)
  1477. printf("%s", msg[++msgid]);
  1478. #else
  1479. printf("%s", msg[++msgid]);
  1480. #endif
  1481. #elif defined(USE_FAST_MATH)
  1482. #if !defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_SP_MATH)
  1483. printf("%d\n", FP_MAX_BITS/2);
  1484. #else
  1485. printf("%d\n", SP_INT_MAX_BITS/2);
  1486. #endif
  1487. #else
  1488. /* normal math has unlimited max size */
  1489. printf("%s", msg[++msgid]);
  1490. #endif
  1491. printf("%s", msg[++msgid]); /* ? */
  1492. printf("%s %s\n", msg[++msgid], wolfSSLIP); /* -h */
  1493. printf("%s %d\n", msg[++msgid], wolfSSLPort); /* -p */
  1494. #ifndef WOLFSSL_TLS13
  1495. printf("%s %d\n", msg[++msgid], CLIENT_DEFAULT_VERSION); /* -v */
  1496. printf("%s", msg[++msgid]); /* -V */
  1497. #else
  1498. printf("%s %d\n", msg[++msgid], CLIENT_DEFAULT_VERSION); /* -v */
  1499. printf("%s", msg[++msgid]); /* -V */
  1500. #endif
  1501. printf("%s", msg[++msgid]); /* -l */
  1502. #ifndef NO_CERTS
  1503. printf("%s %s\n", msg[++msgid], cliCertFile); /* -c */
  1504. printf("%s %s\n", msg[++msgid], cliKeyFile); /* -k */
  1505. printf("%s %s\n", msg[++msgid], caCertFile); /* -A */
  1506. #endif
  1507. #ifndef NO_DH
  1508. printf("%s %d\n", msg[++msgid], DEFAULT_MIN_DHKEY_BITS);
  1509. #endif
  1510. printf("%s", msg[++msgid]); /* -b */
  1511. #ifdef HAVE_ALPN
  1512. printf("%s", msg[++msgid]); /* -L <str> */
  1513. #endif
  1514. printf("%s", msg[++msgid]); /* -B <num> */
  1515. #ifndef NO_PSK
  1516. printf("%s", msg[++msgid]); /* -s */
  1517. #endif
  1518. printf("%s", msg[++msgid]); /* -d */
  1519. printf("%s", msg[++msgid]); /* -D */
  1520. printf("%s", msg[++msgid]); /* -e */
  1521. printf("%s", msg[++msgid]); /* -g */
  1522. printf("%s", msg[++msgid]); /* -u */
  1523. #ifdef WOLFSSL_SCTP
  1524. printf("%s", msg[++msgid]); /* -G */
  1525. #endif
  1526. #ifndef NO_CERTS
  1527. printf("%s", msg[++msgid]); /* -m */
  1528. #endif
  1529. printf("%s", msg[++msgid]); /* -N */
  1530. #ifndef NO_SESSION_CACHE
  1531. printf("%s", msg[++msgid]); /* -r */
  1532. #endif
  1533. printf("%s", msg[++msgid]); /* -w */
  1534. printf("%s", msg[++msgid]); /* -M */
  1535. #ifdef HAVE_SECURE_RENEGOTIATION
  1536. printf("%s", msg[++msgid]); /* -R */
  1537. printf("%s", msg[++msgid]); /* -i */
  1538. #endif
  1539. printf("%s", msg[++msgid]); /* -f */
  1540. #ifndef NO_CERTS
  1541. printf("%s", msg[++msgid]); /* -x */
  1542. #endif
  1543. printf("%s", msg[++msgid]); /* -X */
  1544. printf("%s", msg[++msgid]); /* -j */
  1545. #ifdef SHOW_SIZES
  1546. printf("%s", msg[++msgid]); /* -z */
  1547. #endif
  1548. #ifdef HAVE_SNI
  1549. printf("%s", msg[++msgid]); /* -S */
  1550. #endif
  1551. #ifdef HAVE_MAX_FRAGMENT
  1552. printf("%s", msg[++msgid]); /* -F */
  1553. #endif
  1554. #ifdef HAVE_TRUNCATED_HMAC
  1555. printf("%s", msg[++msgid]); /* -T */
  1556. #endif
  1557. #ifdef HAVE_EXTENDED_MASTER
  1558. printf("%s", msg[++msgid]); /* -n */
  1559. #endif
  1560. #ifdef HAVE_OCSP
  1561. printf("%s", msg[++msgid]); /* -o */
  1562. printf("%s", msg[++msgid]); /* -O */
  1563. #endif
  1564. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1565. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1566. printf("%s", msg[++msgid]); /* -W */
  1567. printf("%s", msg[++msgid]); /* note for -W */
  1568. #endif
  1569. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1570. printf("%s", msg[++msgid]); /* -U */
  1571. #endif
  1572. #ifdef HAVE_PK_CALLBACKS
  1573. printf("%s", msg[++msgid]); /* -P */
  1574. #endif
  1575. #ifdef HAVE_ANON
  1576. printf("%s", msg[++msgid]); /* -a */
  1577. #endif
  1578. #ifdef HAVE_CRL
  1579. printf("%s", msg[++msgid]); /* -C */
  1580. #endif
  1581. #ifdef WOLFSSL_TRUST_PEER_CERT
  1582. printf("%s", msg[++msgid]); /* -E */
  1583. #endif
  1584. #ifdef HAVE_WNR
  1585. printf("%s %s\n", msg[++msgid], wnrConfig); /* -q */
  1586. #endif
  1587. printf("%s", msg[++msgid]); /* -H */
  1588. printf("%s", msg[++msgid]); /* more -H options */
  1589. #ifdef WOLFSSL_TLS13
  1590. printf("%s", msg[++msgid]); /* -J */
  1591. printf("%s", msg[++msgid]); /* -K */
  1592. printf("%s", msg[++msgid]); /* -I */
  1593. #ifndef NO_DH
  1594. printf("%s", msg[++msgid]); /* -y */
  1595. #endif
  1596. #ifdef HAVE_ECC
  1597. printf("%s", msg[++msgid]); /* -Y */
  1598. #endif
  1599. #endif /* WOLFSSL_TLS13 */
  1600. #ifdef HAVE_CURVE25519
  1601. printf("%s", msg[++msgid]); /* -t */
  1602. #endif
  1603. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1604. printf("%s", msg[++msgid]); /* -Q */
  1605. #endif
  1606. #ifdef WOLFSSL_EARLY_DATA
  1607. printf("%s", msg[++msgid]); /* -0 */
  1608. #endif
  1609. #ifdef WOLFSSL_MULTICAST
  1610. printf("%s", msg[++msgid]); /* -3 */
  1611. #endif
  1612. printf("%s", msg[++msgid]); /* -1 */
  1613. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1614. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1615. printf("%s", msg[++msgid]); /* -2 */
  1616. #endif
  1617. #ifdef HAVE_SECURE_RENEGOTIATION
  1618. printf("%s", msg[++msgid]); /* -4 */
  1619. #endif
  1620. #ifdef HAVE_TRUSTED_CA
  1621. printf("%s", msg[++msgid]); /* -5 */
  1622. #endif
  1623. printf("%s", msg[++msgid]); /* -6 */
  1624. #ifdef HAVE_CURVE448
  1625. printf("%s", msg[++msgid]); /* -8 */
  1626. #endif
  1627. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1628. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1629. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1630. printf("%s", msg[++msgid]); /* -9 */
  1631. #endif
  1632. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1633. !defined(WOLFSENTRY_NO_JSON)
  1634. printf("%s", msg[++msgid]); /* --wolfsentry-config */
  1635. #endif
  1636. printf("%s", msg[++msgid]); /* -7 */
  1637. printf("%s", msg[++msgid]); /* Examples repo link */
  1638. #ifdef HAVE_PQC
  1639. printf("%s", msg[++msgid]); /* --pqc */
  1640. printf("%s", msg[++msgid]); /* --pqc options */
  1641. printf("%s", msg[++msgid]); /* more --pqc options */
  1642. printf("%s", msg[++msgid]); /* more --pqc options */
  1643. #endif
  1644. #ifdef WOLFSSL_SYS_CA_CERTS
  1645. printf("%s", msg[++msgid]); /* --sys-ca-certs */
  1646. #endif
  1647. #ifdef HAVE_SUPPORTED_CURVES
  1648. printf("%s", msg[++msgid]); /* --onlyPskDheKe */
  1649. #endif
  1650. #ifdef WOLFSSL_SRTP
  1651. printf("%s", msg[++msgid]); /* dtls-srtp */
  1652. #endif
  1653. }
  1654. #ifdef WOLFSSL_SRTP
  1655. /**
  1656. * client_srtp_test() - test that the computed ekm matches with the server one
  1657. * @ssl: ssl context
  1658. * @srtp_helper: srtp_test_helper struct shared with the server
  1659. *
  1660. * if @srtp_helper is NULL no check is made, but the ekm is printed.
  1661. *
  1662. * calls srtp_helper_get_ekm() to wait and then get the ekm computed by the
  1663. * server, then check if it matches the one computed by itself.
  1664. */
  1665. static int client_srtp_test(WOLFSSL *ssl, func_args *args)
  1666. {
  1667. size_t srtp_secret_length;
  1668. byte *srtp_secret, *p;
  1669. int ret;
  1670. #ifdef WOLFSSL_COND
  1671. srtp_test_helper *srtp_helper = args->srtp_helper;
  1672. byte *other_secret = NULL;
  1673. size_t other_size = 0;
  1674. #else
  1675. (void)args;
  1676. #endif
  1677. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, NULL,
  1678. &srtp_secret_length);
  1679. if (ret != LENGTH_ONLY_E) {
  1680. LOG_ERROR("DTLS SRTP: Error getting keying material length\n");
  1681. return ret;
  1682. }
  1683. srtp_secret = (byte*)XMALLOC(srtp_secret_length,
  1684. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1685. if (srtp_secret == NULL) {
  1686. err_sys("DTLS SRTP: Low memory");
  1687. }
  1688. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, srtp_secret,
  1689. &srtp_secret_length);
  1690. if (ret != WOLFSSL_SUCCESS) {
  1691. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1692. LOG_ERROR("DTLS SRTP: Error getting keying material\n");
  1693. return ret;
  1694. }
  1695. printf("DTLS SRTP: Exported key material: ");
  1696. for (p = srtp_secret; p < srtp_secret + srtp_secret_length; p++)
  1697. printf("%02X", *p);
  1698. printf("\n");
  1699. #ifdef WOLFSSL_COND
  1700. if (srtp_helper != NULL) {
  1701. srtp_helper_get_ekm(srtp_helper, &other_secret, &other_size);
  1702. if (other_size != srtp_secret_length ||
  1703. (XMEMCMP(other_secret, srtp_secret, srtp_secret_length) != 0)) {
  1704. /* we are delegated from server to free this buffer */
  1705. XFREE(other_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1706. printf("DTLS SRTP: Exported Keying Material mismatch\n");
  1707. return WOLFSSL_UNKNOWN;
  1708. }
  1709. /* we are delegated from server to free this buffer */
  1710. XFREE(other_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1711. }
  1712. #endif /* WOLFSSL_COND */
  1713. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1714. return 0;
  1715. }
  1716. #endif /* WOLFSSL_SRTP */
  1717. THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
  1718. {
  1719. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  1720. wolfSSL_method_func method = NULL;
  1721. WOLFSSL_CTX* ctx = NULL;
  1722. WOLFSSL* ssl = NULL;
  1723. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  1724. wolfsentry_errcode_t wolfsentry_ret;
  1725. #endif
  1726. WOLFSSL* sslResume = NULL;
  1727. WOLFSSL_SESSION* session = NULL;
  1728. #if !defined(NO_SESSION_CACHE) && (defined(OPENSSL_EXTRA) || \
  1729. defined(HAVE_EXT_CACHE))
  1730. byte* flatSession = NULL;
  1731. int flatSessionSz = 0;
  1732. #endif
  1733. char msg[CLI_MSG_SZ];
  1734. int msgSz = 0;
  1735. char reply[CLI_REPLY_SZ];
  1736. word16 port = wolfSSLPort;
  1737. char* host = (char*)wolfSSLIP;
  1738. const char* domain = "localhost"; /* can't default to www.wolfssl.com
  1739. because can't tell if we're really
  1740. going there to detect old chacha-poly
  1741. */
  1742. #ifndef WOLFSSL_VXWORKS
  1743. int ch;
  1744. static const struct mygetopt_long_config long_options[] = {
  1745. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1746. !defined(WOLFSENTRY_NO_JSON)
  1747. { "wolfsentry-config", 1, 256 },
  1748. #endif
  1749. { "help", 0, 257 },
  1750. #ifndef NO_MULTIBYTE_PRINT
  1751. { "ヘルプ", 0, 258 },
  1752. #endif
  1753. #if defined(HAVE_PQC)
  1754. { "pqc", 1, 259 },
  1755. #endif
  1756. #ifdef WOLFSSL_SRTP
  1757. { "srtp", 2, 260 }, /* optional argument */
  1758. #endif
  1759. #ifdef WOLFSSL_DTLS13
  1760. /* allow waitTicket option even when HAVE_SESSION_TICKET is 0. Otherwise
  1761. * tests that use this option will ignore the options following
  1762. * --waitTicket in the command line and fail */
  1763. {"waitTicket", 0, 261},
  1764. #endif /* WOLFSSL_DTLS13 */
  1765. #ifdef WOLFSSL_DTLS_CID
  1766. {"cid", 2, 262},
  1767. #endif /* WOLFSSL_DTLS_CID */
  1768. #ifdef WOLFSSL_SYS_CA_CERTS
  1769. { "sys-ca-certs", 0, 263 },
  1770. #endif
  1771. #ifdef HAVE_SUPPORTED_CURVES
  1772. { "onlyPskDheKe", 0, 264 },
  1773. #endif
  1774. #ifndef NO_PSK
  1775. { "openssl-psk", 0, 265 },
  1776. #endif
  1777. { "quieter", 0, 266 },
  1778. { 0, 0, 0 }
  1779. };
  1780. #endif
  1781. int version = CLIENT_INVALID_VERSION;
  1782. int minVersion = CLIENT_INVALID_VERSION;
  1783. int usePsk = 0;
  1784. int opensslPsk = 0;
  1785. int useAnon = 0;
  1786. int sendGET = 0;
  1787. int benchmark = 0;
  1788. int block = TEST_BUFFER_SIZE;
  1789. size_t throughput = 0;
  1790. int doDTLS = 0;
  1791. int dtlsUDP = 0;
  1792. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1793. defined(WOLFSSL_DTLS)
  1794. int dtlsMTU = 0;
  1795. #endif
  1796. int dtlsSCTP = 0;
  1797. int doMcast = 0;
  1798. int matchName = 0;
  1799. int doPeerCheck = 1;
  1800. int nonBlocking = 0;
  1801. int simulateWantWrite = 0;
  1802. int resumeSession = 0;
  1803. int wc_shutdown = 0;
  1804. int disableCRL = 0;
  1805. int externalTest = 0;
  1806. int ret;
  1807. int err = 0;
  1808. int scr = 0; /* allow secure renegotiation */
  1809. int forceScr = 0; /* force client initiated scr */
  1810. int scrAppData = 0;
  1811. int resumeScr = 0; /* use resumption for renegotiation */
  1812. #ifndef WOLFSSL_NO_CLIENT_AUTH
  1813. int useClientCert = 1;
  1814. #else
  1815. int useClientCert = 0;
  1816. #endif
  1817. int fewerPackets = 0;
  1818. int atomicUser = 0;
  1819. #ifdef HAVE_PK_CALLBACKS
  1820. int pkCallbacks = 0;
  1821. PkCbInfo pkCbInfo;
  1822. #endif
  1823. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  1824. char* alpnList = NULL;
  1825. unsigned char alpn_opt = 0;
  1826. char* cipherList = NULL;
  1827. int useDefCipherList = 0;
  1828. int customVerifyCert = 0;
  1829. const char* verifyCert;
  1830. const char* ourCert;
  1831. const char* ourKey;
  1832. int doSTARTTLS = 0;
  1833. char* starttlsProt = NULL;
  1834. int useVerifyCb = 0;
  1835. int useSupCurve = 0;
  1836. #ifdef WOLFSSL_TRUST_PEER_CERT
  1837. const char* trustCert = NULL;
  1838. #endif
  1839. #ifdef HAVE_SNI
  1840. char* sniHostName = NULL;
  1841. #endif
  1842. #ifdef HAVE_TRUSTED_CA
  1843. int trustedCaKeyId = 0;
  1844. #endif
  1845. #ifdef HAVE_MAX_FRAGMENT
  1846. byte maxFragment = 0;
  1847. #endif
  1848. #ifdef HAVE_TRUNCATED_HMAC
  1849. byte truncatedHMAC = 0;
  1850. #endif
  1851. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  1852. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1853. byte statusRequest = 0;
  1854. byte mustStaple = 0;
  1855. #endif
  1856. #ifdef HAVE_EXTENDED_MASTER
  1857. byte disableExtMasterSecret = 0;
  1858. #endif
  1859. int helloRetry = 0;
  1860. int onlyKeyShare = 0;
  1861. #ifdef WOLFSSL_TLS13
  1862. int noPskDheKe = 0;
  1863. #ifdef HAVE_SUPPORTED_CURVES
  1864. int onlyPskDheKe = 0;
  1865. #endif
  1866. int postHandAuth = 0;
  1867. #endif
  1868. int updateKeysIVs = 0;
  1869. int earlyData = 0;
  1870. #ifdef WOLFSSL_MULTICAST
  1871. byte mcastID = 0;
  1872. #endif
  1873. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1874. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1875. int doDhKeyCheck = 1;
  1876. #endif
  1877. #ifdef HAVE_OCSP
  1878. int useOcsp = 0;
  1879. char* ocspUrl = NULL;
  1880. #endif
  1881. int useX25519 = 0;
  1882. int useX448 = 0;
  1883. int usePqc = 0;
  1884. char* pqcAlg = NULL;
  1885. int exitWithRet = 0;
  1886. int loadCertKeyIntoSSLObj = 0;
  1887. #ifdef WOLFSSL_SYS_CA_CERTS
  1888. byte loadSysCaCerts = 0;
  1889. #endif
  1890. #ifdef HAVE_ENCRYPT_THEN_MAC
  1891. int disallowETM = 0;
  1892. #endif
  1893. #ifdef HAVE_WNR
  1894. const char* wnrConfigFile = wnrConfig;
  1895. #endif
  1896. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1897. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1898. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1899. int useCertFolder = 0;
  1900. #endif
  1901. #ifdef WOLFSSL_SRTP
  1902. const char* dtlsSrtpProfiles = NULL;
  1903. #endif
  1904. #ifdef HAVE_SESSION_TICKET
  1905. int waitTicket = 0;
  1906. #endif /* HAVE_SESSION_TICKET */
  1907. #ifdef WOLFSSL_DTLS_CID
  1908. int useDtlsCID = 0;
  1909. char dtlsCID[DTLS_CID_BUFFER_SIZE] = { 0 };
  1910. #endif /* WOLFSSL_DTLS_CID */
  1911. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1912. int argc = ((func_args*)args)->argc;
  1913. char** argv = ((func_args*)args)->argv;
  1914. #ifdef WOLFSSL_STATIC_MEMORY
  1915. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  1916. || defined(SESSION_CERTS)
  1917. /* big enough to handle most cases including session certs */
  1918. byte memory[320000];
  1919. #else
  1920. byte memory[80000];
  1921. #endif
  1922. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  1923. WOLFSSL_MEM_CONN_STATS ssl_stats;
  1924. #ifdef DEBUG_WOLFSSL
  1925. WOLFSSL_MEM_STATS mem_stats;
  1926. #endif
  1927. WOLFSSL_HEAP_HINT *heap = NULL;
  1928. #endif
  1929. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1930. /* Set our preference for verfication to be for both the native and
  1931. * alternative chains. Ultimately, its the server's choice. This will be
  1932. * used in the call to wolfSSL_UseCKS(). */
  1933. byte cks_order[3] = {
  1934. WOLFSSL_CKS_SIGSPEC_BOTH,
  1935. WOLFSSL_CKS_SIGSPEC_ALTERNATIVE,
  1936. WOLFSSL_CKS_SIGSPEC_NATIVE,
  1937. };
  1938. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  1939. ((func_args*)args)->return_code = -1; /* error state */
  1940. #ifndef NO_RSA
  1941. verifyCert = caCertFile;
  1942. ourCert = cliCertFile;
  1943. ourKey = cliKeyFile;
  1944. #else
  1945. #ifdef HAVE_ECC
  1946. verifyCert = caEccCertFile;
  1947. ourCert = cliEccCertFile;
  1948. ourKey = cliEccKeyFile;
  1949. #elif defined(HAVE_ED25519)
  1950. verifyCert = caEdCertFile;
  1951. ourCert = cliEdCertFile;
  1952. ourKey = cliEdKeyFile;
  1953. #elif defined(HAVE_ED448)
  1954. verifyCert = caEd448CertFile;
  1955. ourCert = cliEd448CertFile;
  1956. ourKey = cliEd448KeyFile;
  1957. #else
  1958. verifyCert = NULL;
  1959. ourCert = NULL;
  1960. ourKey = NULL;
  1961. #endif
  1962. #endif
  1963. (void)session;
  1964. (void)sslResume;
  1965. (void)atomicUser;
  1966. (void)scr;
  1967. (void)forceScr;
  1968. (void)scrAppData;
  1969. (void)resumeScr;
  1970. (void)ourKey;
  1971. (void)ourCert;
  1972. (void)verifyCert;
  1973. (void)useClientCert;
  1974. (void)disableCRL;
  1975. (void)minDhKeyBits;
  1976. (void)alpnList;
  1977. (void)alpn_opt;
  1978. (void)updateKeysIVs;
  1979. (void)earlyData;
  1980. (void)useX25519;
  1981. (void)useX448;
  1982. (void)helloRetry;
  1983. (void)onlyKeyShare;
  1984. (void)useSupCurve;
  1985. (void)loadCertKeyIntoSSLObj;
  1986. (void)usePqc;
  1987. (void)pqcAlg;
  1988. (void)opensslPsk;
  1989. StackTrap();
  1990. /* Reinitialize the global myVerifyAction. */
  1991. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1992. #ifndef WOLFSSL_VXWORKS
  1993. /* Not used: All used */
  1994. while ((ch = mygetopt_long(argc, argv, "?:"
  1995. "ab:c:defgh:i;jk:l:mnop:q:rstu;v:wxyz"
  1996. "A:B:CDE:F:GH:IJKL:M:NO:PQRS:TUVW:XYZ:"
  1997. "01:23:4567:89"
  1998. "@#", long_options, 0)) != -1) {
  1999. switch (ch) {
  2000. case '?' :
  2001. if(myoptarg!=NULL) {
  2002. lng_index = atoi(myoptarg);
  2003. if(lng_index<0||lng_index>1){
  2004. lng_index = 0;
  2005. }
  2006. }
  2007. Usage();
  2008. XEXIT_T(EXIT_SUCCESS);
  2009. case 257 :
  2010. lng_index = 0;
  2011. Usage();
  2012. XEXIT_T(EXIT_SUCCESS);
  2013. case 258 :
  2014. lng_index = 1;
  2015. Usage();
  2016. XEXIT_T(EXIT_SUCCESS);
  2017. case 'g' :
  2018. sendGET = 1;
  2019. break;
  2020. case 'd' :
  2021. doPeerCheck = 0;
  2022. break;
  2023. case 'e' :
  2024. ShowCiphers();
  2025. XEXIT_T(EXIT_SUCCESS);
  2026. case 'D' :
  2027. myVerifyAction = VERIFY_OVERRIDE_DATE_ERR;
  2028. break;
  2029. case 'C' :
  2030. #ifdef HAVE_CRL
  2031. disableCRL = 1;
  2032. #endif
  2033. break;
  2034. case 'u' :
  2035. doDTLS = 1;
  2036. dtlsUDP = 1;
  2037. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2038. defined(WOLFSSL_DTLS)
  2039. dtlsMTU = atoi(myoptarg);
  2040. #endif
  2041. break;
  2042. #ifdef WOLFSSL_SRTP
  2043. case 260:
  2044. doDTLS = 1;
  2045. dtlsUDP = 1;
  2046. dtlsSrtpProfiles = myoptarg != NULL ? myoptarg :
  2047. "SRTP_AES128_CM_SHA1_80";
  2048. printf("Using SRTP Profile(s): %s\n", dtlsSrtpProfiles);
  2049. break;
  2050. #endif
  2051. #ifdef WOLFSSL_DTLS13
  2052. case 261:
  2053. #ifdef HAVE_SESSION_TICKET
  2054. waitTicket = 1;
  2055. #endif /* HAVE_SESSION_TICKET */
  2056. break;
  2057. #endif /* WOLFSSL_DTLS13 */
  2058. #ifdef WOLFSSL_DTLS_CID
  2059. case 262:
  2060. useDtlsCID = 1;
  2061. if (myoptarg != NULL) {
  2062. if (XSTRLEN(myoptarg) >= DTLS_CID_BUFFER_SIZE) {
  2063. err_sys("provided connection ID is too big");
  2064. }
  2065. else {
  2066. strcpy(dtlsCID, myoptarg);
  2067. }
  2068. }
  2069. break;
  2070. #endif /* WOLFSSL_CID */
  2071. case 'G' :
  2072. #ifdef WOLFSSL_SCTP
  2073. doDTLS = 1;
  2074. dtlsUDP = 1;
  2075. dtlsSCTP = 1;
  2076. #endif
  2077. break;
  2078. case 's' :
  2079. usePsk = 1;
  2080. break;
  2081. #ifdef WOLFSSL_TRUST_PEER_CERT
  2082. case 'E' :
  2083. trustCert = myoptarg;
  2084. break;
  2085. #endif
  2086. case 'm' :
  2087. matchName = 1;
  2088. break;
  2089. case 'x' :
  2090. useClientCert = 0;
  2091. break;
  2092. case 'X' :
  2093. externalTest = 1;
  2094. break;
  2095. case 'f' :
  2096. fewerPackets = 1;
  2097. break;
  2098. case 'U' :
  2099. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  2100. atomicUser = 1;
  2101. #endif
  2102. break;
  2103. case 'P' :
  2104. #ifdef HAVE_PK_CALLBACKS
  2105. pkCallbacks = 1;
  2106. #endif
  2107. break;
  2108. case 'h' :
  2109. host = myoptarg;
  2110. domain = myoptarg;
  2111. break;
  2112. case 'p' :
  2113. port = (word16)atoi(myoptarg);
  2114. #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API)
  2115. if (port == 0)
  2116. err_sys("port number cannot be 0");
  2117. #endif
  2118. break;
  2119. case 'v' :
  2120. if (myoptarg[0] == 'd') {
  2121. version = CLIENT_DOWNGRADE_VERSION;
  2122. break;
  2123. }
  2124. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2125. else if (myoptarg[0] == 'e') {
  2126. version = EITHER_DOWNGRADE_VERSION;
  2127. #ifndef NO_CERTS
  2128. loadCertKeyIntoSSLObj = 1;
  2129. #endif
  2130. break;
  2131. }
  2132. #endif
  2133. version = atoi(myoptarg);
  2134. if (version < 0 || version > 4) {
  2135. Usage();
  2136. XEXIT_T(MY_EX_USAGE);
  2137. }
  2138. break;
  2139. case 'V' :
  2140. ShowVersions();
  2141. XEXIT_T(EXIT_SUCCESS);
  2142. case 'l' :
  2143. cipherList = myoptarg;
  2144. break;
  2145. case 'H' :
  2146. if (XSTRCMP(myoptarg, "defCipherList") == 0) {
  2147. printf("Using default cipher list for testing\n");
  2148. useDefCipherList = 1;
  2149. }
  2150. else if (XSTRCMP(myoptarg, "exitWithRet") == 0) {
  2151. printf("Skip exit() for testing\n");
  2152. exitWithRet = 1;
  2153. }
  2154. else if (XSTRCMP(myoptarg, "verifyFail") == 0) {
  2155. printf("Verify should fail\n");
  2156. myVerifyAction = VERIFY_FORCE_FAIL;
  2157. }
  2158. else if (XSTRCMP(myoptarg, "verifyInfo") == 0) {
  2159. printf("Verify should not override error\n");
  2160. myVerifyAction = VERIFY_USE_PREVERFIY;
  2161. }
  2162. else if (XSTRCMP(myoptarg, "useSupCurve") == 0) {
  2163. printf("Attempting to test use supported curve\n");
  2164. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  2165. useSupCurve = 1;
  2166. #else
  2167. printf("Supported curves not compiled in!\n");
  2168. #endif
  2169. }
  2170. else if (XSTRCMP(myoptarg, "loadSSL") == 0) {
  2171. printf("Load cert/key into wolfSSL object\n");
  2172. #ifndef NO_CERTS
  2173. loadCertKeyIntoSSLObj = 1;
  2174. #else
  2175. printf("Certs turned off with NO_CERTS!\n");
  2176. #endif
  2177. }
  2178. else if (XSTRCMP(myoptarg, "disallowETM") == 0) {
  2179. printf("Disallow Encrypt-Then-MAC\n");
  2180. #ifdef HAVE_ENCRYPT_THEN_MAC
  2181. disallowETM = 1;
  2182. #endif
  2183. }
  2184. else {
  2185. Usage();
  2186. XEXIT_T(MY_EX_USAGE);
  2187. }
  2188. break;
  2189. case 'A' :
  2190. customVerifyCert = 1;
  2191. verifyCert = myoptarg;
  2192. break;
  2193. case 'c' :
  2194. ourCert = myoptarg;
  2195. break;
  2196. case 'k' :
  2197. ourKey = myoptarg;
  2198. break;
  2199. case 'Z' :
  2200. #ifndef NO_DH
  2201. minDhKeyBits = atoi(myoptarg);
  2202. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  2203. Usage();
  2204. XEXIT_T(MY_EX_USAGE);
  2205. }
  2206. #endif
  2207. break;
  2208. case 'b' :
  2209. benchmark = atoi(myoptarg);
  2210. if (benchmark < 0 || benchmark > 1000000) {
  2211. Usage();
  2212. XEXIT_T(MY_EX_USAGE);
  2213. }
  2214. break;
  2215. case 'B' :
  2216. throughput = atol(myoptarg);
  2217. for (; *myoptarg != '\0'; myoptarg++) {
  2218. if (*myoptarg == ',') {
  2219. block = atoi(myoptarg + 1);
  2220. break;
  2221. }
  2222. }
  2223. if (throughput == 0 || block <= 0) {
  2224. Usage();
  2225. XEXIT_T(MY_EX_USAGE);
  2226. }
  2227. break;
  2228. case 'N' :
  2229. nonBlocking = 1;
  2230. break;
  2231. case 'r' :
  2232. resumeSession = 1;
  2233. break;
  2234. case 'w' :
  2235. wc_shutdown = 1;
  2236. break;
  2237. case 'R' :
  2238. #ifdef HAVE_SECURE_RENEGOTIATION
  2239. scr = 1;
  2240. #endif
  2241. break;
  2242. case 'i' :
  2243. #ifdef HAVE_SECURE_RENEGOTIATION
  2244. scr = 1;
  2245. forceScr = 1;
  2246. if (XSTRCMP(myoptarg, "scr-app-data") == 0) {
  2247. scrAppData = 1;
  2248. }
  2249. #endif
  2250. break;
  2251. case 'z' :
  2252. #ifndef WOLFSSL_LEANPSK
  2253. wolfSSL_GetObjectSize();
  2254. #endif
  2255. break;
  2256. case 'S' :
  2257. if (XSTRCMP(myoptarg, "check") == 0) {
  2258. #ifdef HAVE_SNI
  2259. printf("SNI is: ON\n");
  2260. #else
  2261. printf("SNI is: OFF\n");
  2262. #endif
  2263. XEXIT_T(EXIT_SUCCESS);
  2264. }
  2265. #ifdef HAVE_SNI
  2266. sniHostName = myoptarg;
  2267. #endif
  2268. break;
  2269. case 'F' :
  2270. #ifdef HAVE_MAX_FRAGMENT
  2271. maxFragment = atoi(myoptarg);
  2272. if (maxFragment < WOLFSSL_MFL_MIN ||
  2273. maxFragment > WOLFSSL_MFL_MAX) {
  2274. Usage();
  2275. XEXIT_T(MY_EX_USAGE);
  2276. }
  2277. #endif
  2278. break;
  2279. case 'T' :
  2280. #ifdef HAVE_TRUNCATED_HMAC
  2281. truncatedHMAC = 1;
  2282. #endif
  2283. break;
  2284. case 'n' :
  2285. #ifdef HAVE_EXTENDED_MASTER
  2286. disableExtMasterSecret = 1;
  2287. #endif
  2288. break;
  2289. case 'W' :
  2290. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2291. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2292. {
  2293. word32 myoptargSz;
  2294. statusRequest = atoi(myoptarg);
  2295. if (statusRequest > OCSP_STAPLING_OPT_MAX) {
  2296. Usage();
  2297. XEXIT_T(MY_EX_USAGE);
  2298. }
  2299. myoptargSz = (word32)XSTRLEN(myoptarg);
  2300. if (myoptargSz > 0 &&
  2301. XTOUPPER((unsigned char)myoptarg[myoptargSz-1]) == 'M') {
  2302. mustStaple = 1;
  2303. }
  2304. }
  2305. #endif
  2306. break;
  2307. case 'o' :
  2308. #ifdef HAVE_OCSP
  2309. useOcsp = 1;
  2310. #endif
  2311. break;
  2312. case 'O' :
  2313. #ifdef HAVE_OCSP
  2314. useOcsp = 1;
  2315. ocspUrl = myoptarg;
  2316. #endif
  2317. break;
  2318. case 'a' :
  2319. #ifdef HAVE_ANON
  2320. useAnon = 1;
  2321. #endif
  2322. break;
  2323. case 'L' :
  2324. #ifdef HAVE_ALPN
  2325. alpnList = myoptarg;
  2326. if (alpnList[0] == 'C' && alpnList[1] == ':')
  2327. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  2328. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  2329. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  2330. else {
  2331. Usage();
  2332. XEXIT_T(MY_EX_USAGE);
  2333. }
  2334. alpnList += 2;
  2335. #endif
  2336. break;
  2337. case 'M' :
  2338. doSTARTTLS = 1;
  2339. starttlsProt = myoptarg;
  2340. if (XSTRCMP(starttlsProt, "smtp") != 0) {
  2341. Usage();
  2342. XEXIT_T(MY_EX_USAGE);
  2343. }
  2344. break;
  2345. case 'q' :
  2346. #ifdef HAVE_WNR
  2347. wnrConfigFile = myoptarg;
  2348. #endif
  2349. break;
  2350. case 'J' :
  2351. #ifdef WOLFSSL_TLS13
  2352. helloRetry = 1;
  2353. #endif
  2354. break;
  2355. case 'K' :
  2356. #ifdef WOLFSSL_TLS13
  2357. noPskDheKe = 1;
  2358. #endif
  2359. break;
  2360. case 'I' :
  2361. #ifdef WOLFSSL_TLS13
  2362. updateKeysIVs = 1;
  2363. #endif
  2364. break;
  2365. case 'y' :
  2366. #if defined(WOLFSSL_TLS13) && \
  2367. defined(HAVE_SUPPORTED_CURVES) && !defined(NO_DH)
  2368. onlyKeyShare = 1;
  2369. #endif
  2370. break;
  2371. case 'Y' :
  2372. #if defined(WOLFSSL_TLS13) && \
  2373. defined(HAVE_SUPPORTED_CURVES) && defined(HAVE_ECC)
  2374. onlyKeyShare = 2;
  2375. #endif
  2376. break;
  2377. case 'j' :
  2378. useVerifyCb = 1;
  2379. break;
  2380. case 't' :
  2381. #ifdef HAVE_CURVE25519
  2382. useX25519 = 1;
  2383. #ifdef HAVE_ECC
  2384. useSupCurve = 1;
  2385. #if defined(WOLFSSL_TLS13) && \
  2386. defined(HAVE_SUPPORTED_CURVES)
  2387. onlyKeyShare = 2;
  2388. #endif
  2389. #endif
  2390. #endif
  2391. break;
  2392. case 'Q' :
  2393. #if defined(WOLFSSL_TLS13) && \
  2394. defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2395. postHandAuth = 1;
  2396. #endif
  2397. break;
  2398. case '0' :
  2399. #ifdef WOLFSSL_EARLY_DATA
  2400. earlyData = 1;
  2401. #endif
  2402. break;
  2403. case '1' :
  2404. lng_index = atoi(myoptarg);
  2405. if(lng_index<0||lng_index>1){
  2406. lng_index = 0;
  2407. }
  2408. break;
  2409. case '2' :
  2410. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  2411. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  2412. doDhKeyCheck = 0;
  2413. #endif
  2414. break;
  2415. case '3' :
  2416. #ifdef WOLFSSL_MULTICAST
  2417. doMcast = 1;
  2418. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  2419. #endif
  2420. break;
  2421. case '4' :
  2422. #ifdef HAVE_SECURE_RENEGOTIATION
  2423. scr = 1;
  2424. forceScr = 1;
  2425. resumeScr = 1;
  2426. #endif
  2427. break;
  2428. case '5' :
  2429. #ifdef HAVE_TRUSTED_CA
  2430. trustedCaKeyId = 1;
  2431. #endif /* HAVE_TRUSTED_CA */
  2432. break;
  2433. case '6' :
  2434. #ifdef WOLFSSL_ASYNC_IO
  2435. nonBlocking = 1;
  2436. simulateWantWrite = 1;
  2437. #else
  2438. LOG_ERROR("Ignoring -6 since async I/O support not "
  2439. "compiled in.\n");
  2440. #endif
  2441. break;
  2442. case '7' :
  2443. minVersion = atoi(myoptarg);
  2444. if (minVersion < 0 || minVersion > 4) {
  2445. Usage();
  2446. XEXIT_T(MY_EX_USAGE);
  2447. }
  2448. break;
  2449. case '8' :
  2450. #ifdef HAVE_CURVE448
  2451. useX448 = 1;
  2452. #ifdef HAVE_ECC
  2453. useSupCurve = 1;
  2454. #if defined(WOLFSSL_TLS13) && \
  2455. defined(HAVE_SUPPORTED_CURVES)
  2456. onlyKeyShare = 2;
  2457. #endif
  2458. #endif
  2459. #endif
  2460. break;
  2461. case '9' :
  2462. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2463. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2464. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2465. useCertFolder = 1;
  2466. #endif
  2467. break;
  2468. case '@' :
  2469. {
  2470. #ifdef HAVE_WC_INTROSPECTION
  2471. const char *conf_args = wolfSSL_configure_args();
  2472. if (conf_args) {
  2473. puts(conf_args);
  2474. XEXIT_T(EXIT_SUCCESS);
  2475. } else {
  2476. fputs("configure args not compiled in.\n",stderr);
  2477. XEXIT_T(MY_EX_USAGE);
  2478. }
  2479. #else
  2480. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2481. XEXIT_T(MY_EX_USAGE);
  2482. #endif
  2483. }
  2484. case '#' :
  2485. {
  2486. #ifdef HAVE_WC_INTROSPECTION
  2487. const char *cflags = wolfSSL_global_cflags();
  2488. if (cflags) {
  2489. puts(cflags);
  2490. XEXIT_T(EXIT_SUCCESS);
  2491. } else {
  2492. fputs("CFLAGS not compiled in.\n",stderr);
  2493. XEXIT_T(MY_EX_USAGE);
  2494. }
  2495. #else
  2496. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2497. XEXIT_T(MY_EX_USAGE);
  2498. #endif
  2499. }
  2500. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2501. case 256:
  2502. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  2503. wolfsentry_config_path = myoptarg;
  2504. #endif
  2505. break;
  2506. #endif
  2507. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) && \
  2508. defined(HAVE_PQC)
  2509. case 259:
  2510. usePqc = 1;
  2511. onlyKeyShare = 3;
  2512. pqcAlg = myoptarg;
  2513. break;
  2514. #endif
  2515. #ifdef WOLFSSL_SYS_CA_CERTS
  2516. case 263:
  2517. loadSysCaCerts = 1;
  2518. break;
  2519. #endif
  2520. case 264:
  2521. #ifdef HAVE_SUPPORTED_CURVES
  2522. #ifdef WOLFSSL_TLS13
  2523. onlyPskDheKe = 1;
  2524. #endif
  2525. #endif
  2526. break;
  2527. case 265:
  2528. #ifndef NO_PSK
  2529. opensslPsk = 1;
  2530. #endif
  2531. break;
  2532. case 266:
  2533. quieter = 1;
  2534. break;
  2535. default:
  2536. Usage();
  2537. XEXIT_T(MY_EX_USAGE);
  2538. }
  2539. }
  2540. myoptind = 0; /* reset for test cases */
  2541. #endif /* !WOLFSSL_VXWORKS */
  2542. if (externalTest) {
  2543. /* detect build cases that wouldn't allow test against wolfssl.com */
  2544. int done = 0;
  2545. #ifdef NO_RSA
  2546. done += 1; /* require RSA for external tests */
  2547. #endif
  2548. if (!XSTRCMP(domain, "www.globalsign.com")) {
  2549. /* www.globalsign.com does not respond to ipv6 ocsp requests */
  2550. #if defined(TEST_IPV6) && defined(HAVE_OCSP)
  2551. done += 1;
  2552. #endif
  2553. /* www.globalsign.com has limited supported cipher suites */
  2554. #if defined(NO_AES) && defined(HAVE_OCSP)
  2555. done += 1;
  2556. #endif
  2557. /* www.globalsign.com only supports static RSA or ECDHE with AES */
  2558. /* We cannot expect users to have on static RSA so test for ECC only
  2559. * as some users will most likely be on 32-bit systems where ECC
  2560. * is not enabled by default */
  2561. #if defined(HAVE_OCSP) && !defined(HAVE_ECC)
  2562. done += 1;
  2563. #endif
  2564. }
  2565. #ifndef NO_PSK
  2566. if (usePsk) {
  2567. done += 1; /* don't perform external tests if PSK is enabled */
  2568. }
  2569. #endif
  2570. #ifdef NO_SHA
  2571. done += 1; /* external cert chain most likely has SHA */
  2572. #endif
  2573. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  2574. || ( defined(HAVE_ECC) && !defined(HAVE_SUPPORTED_CURVES) \
  2575. && !defined(WOLFSSL_STATIC_RSA) )
  2576. /* google needs ECDHE+Supported Curves or static RSA */
  2577. if (!XSTRCASECMP(domain, "www.google.com"))
  2578. done += 1;
  2579. #endif
  2580. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA)
  2581. /* wolfssl needs ECDHE or static RSA */
  2582. if (!XSTRCASECMP(domain, "www.wolfssl.com"))
  2583. done += 1;
  2584. #endif
  2585. #if !defined(WOLFSSL_SHA384)
  2586. if (!XSTRCASECMP(domain, "www.wolfssl.com")) {
  2587. /* wolfssl need sha384 for cert chain verify */
  2588. done += 1;
  2589. }
  2590. #endif
  2591. #if !defined(HAVE_AESGCM) && defined(NO_AES) && \
  2592. !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  2593. /* need at least one of these for external tests */
  2594. done += 1;
  2595. #endif
  2596. /* For the external test, if we disable AES, GoDaddy will reject the
  2597. * connection. They only currently support AES suites, RC4 and 3DES
  2598. * suites. With AES disabled we only offer PolyChacha suites. */
  2599. #if defined(NO_AES) && !defined(HAVE_AESGCM)
  2600. if (!XSTRCASECMP(domain, "www.wolfssl.com")) {
  2601. done += 1;
  2602. }
  2603. #endif
  2604. if (done) {
  2605. LOG_ERROR("external test can't be run in this mode\n");
  2606. ((func_args*)args)->return_code = 0;
  2607. XEXIT_T(EXIT_SUCCESS);
  2608. }
  2609. }
  2610. /* sort out DTLS versus TLS versions */
  2611. if (version == CLIENT_INVALID_VERSION) {
  2612. if (doDTLS)
  2613. version = CLIENT_DTLS_DEFAULT_VERSION;
  2614. else
  2615. version = CLIENT_DEFAULT_VERSION;
  2616. }
  2617. else {
  2618. if (doDTLS) {
  2619. if (version == 3) {
  2620. version = -2;
  2621. }
  2622. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2623. else if (version == EITHER_DOWNGRADE_VERSION) {
  2624. version = -3;
  2625. }
  2626. #endif
  2627. else if (version == 4) {
  2628. #ifdef WOLFSSL_DTLS13
  2629. version = -4;
  2630. #else
  2631. err_sys("Bad DTLS version");
  2632. #endif /* WOLFSSL_DTLS13 */
  2633. }
  2634. else if (version == 2)
  2635. version = -1;
  2636. }
  2637. }
  2638. #ifndef HAVE_SESSION_TICKET
  2639. if ((version >= 4) && resumeSession) {
  2640. LOG_ERROR("Can't do TLS 1.3 resumption; need session tickets!\n");
  2641. }
  2642. #endif
  2643. #ifdef HAVE_WNR
  2644. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  2645. err_sys("can't load whitewood net random config file");
  2646. #endif
  2647. #ifdef HAVE_PQC
  2648. if (usePqc) {
  2649. if (version == CLIENT_DOWNGRADE_VERSION ||
  2650. version == EITHER_DOWNGRADE_VERSION)
  2651. LOG_ERROR(
  2652. "WARNING: If a TLS 1.3 connection is not negotiated, you "
  2653. "will not be using a post-quantum group.\n");
  2654. else if (version != 4 && version != -4)
  2655. err_sys("can only use post-quantum groups with TLS 1.3 or DTLS 1.3");
  2656. }
  2657. #endif
  2658. switch (version) {
  2659. #ifndef NO_OLD_TLS
  2660. #ifdef WOLFSSL_ALLOW_SSLV3
  2661. case 0:
  2662. method = wolfSSLv3_client_method_ex;
  2663. break;
  2664. #endif
  2665. #ifndef NO_TLS
  2666. #ifdef WOLFSSL_ALLOW_TLSV10
  2667. case 1:
  2668. method = wolfTLSv1_client_method_ex;
  2669. break;
  2670. #endif
  2671. case 2:
  2672. method = wolfTLSv1_1_client_method_ex;
  2673. break;
  2674. #endif /* !NO_TLS */
  2675. #endif /* !NO_OLD_TLS */
  2676. #ifndef NO_TLS
  2677. #ifndef WOLFSSL_NO_TLS12
  2678. case 3:
  2679. method = wolfTLSv1_2_client_method_ex;
  2680. break;
  2681. #endif
  2682. #ifdef WOLFSSL_TLS13
  2683. case 4:
  2684. method = wolfTLSv1_3_client_method_ex;
  2685. break;
  2686. #endif
  2687. case CLIENT_DOWNGRADE_VERSION:
  2688. if (!doDTLS) {
  2689. method = wolfSSLv23_client_method_ex;
  2690. }
  2691. else {
  2692. #ifdef WOLFSSL_DTLS
  2693. method = wolfDTLS_client_method_ex;
  2694. #else
  2695. err_sys("version not supported");
  2696. #endif /* WOLFSSL_DTLS */
  2697. }
  2698. break;
  2699. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2700. case EITHER_DOWNGRADE_VERSION:
  2701. method = wolfSSLv23_method_ex;
  2702. break;
  2703. #endif
  2704. #endif /* NO_TLS */
  2705. #ifdef WOLFSSL_DTLS
  2706. #ifndef NO_OLD_TLS
  2707. case -1:
  2708. method = wolfDTLSv1_client_method_ex;
  2709. break;
  2710. #endif
  2711. #ifndef WOLFSSL_NO_TLS12
  2712. case -2:
  2713. method = wolfDTLSv1_2_client_method_ex;
  2714. break;
  2715. #endif
  2716. #ifdef WOLFSSL_DTLS13
  2717. case -4:
  2718. method = wolfDTLSv1_3_client_method_ex;
  2719. break;
  2720. #endif /* WOLFSSL_DTLS13 */
  2721. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2722. case -3:
  2723. method = wolfDTLSv1_2_method_ex;
  2724. break;
  2725. #endif
  2726. #endif
  2727. default:
  2728. err_sys("Bad SSL version");
  2729. }
  2730. if (method == NULL)
  2731. err_sys("unable to get method");
  2732. #ifdef WOLFSSL_STATIC_MEMORY
  2733. #ifdef DEBUG_WOLFSSL
  2734. /* print off helper buffer sizes for use with static memory
  2735. * printing to stderr in case of debug mode turned on */
  2736. LOG_ERROR("static memory management size = %d\n",
  2737. wolfSSL_MemoryPaddingSz());
  2738. LOG_ERROR("calculated optimum general buffer size = %d\n",
  2739. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2740. LOG_ERROR("calculated optimum IO buffer size = %d\n",
  2741. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2742. WOLFMEM_IO_POOL_FIXED));
  2743. #endif /* DEBUG_WOLFSSL */
  2744. if (wc_LoadStaticMemory(&heap, memory, sizeof(memory), WOLFMEM_GENERAL, 1)
  2745. != 0) {
  2746. err_sys("unable to load static memory");
  2747. }
  2748. ctx = wolfSSL_CTX_new_ex(method(heap), heap);
  2749. if (ctx == NULL)
  2750. err_sys("unable to get ctx");
  2751. #ifdef WOLFSSL_CALLBACKS
  2752. wolfSSL_CTX_set_msg_callback(ctx, msgDebugCb);
  2753. #endif
  2754. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2755. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) != WOLFSSL_SUCCESS) {
  2756. err_sys("unable to load static memory");
  2757. }
  2758. #else
  2759. if (method != NULL) {
  2760. ctx = wolfSSL_CTX_new(method(NULL));
  2761. if (ctx == NULL)
  2762. err_sys("unable to get ctx");
  2763. }
  2764. #endif
  2765. #ifdef WOLFSSL_SYS_CA_CERTS
  2766. if (loadSysCaCerts &&
  2767. wolfSSL_CTX_load_system_CA_certs(ctx) != WOLFSSL_SUCCESS) {
  2768. err_sys("wolfSSL_CTX_load_system_CA_certs failed");
  2769. }
  2770. #endif /* WOLFSSL_SYS_CA_CERTS */
  2771. if (minVersion != CLIENT_INVALID_VERSION) {
  2772. #ifdef WOLFSSL_DTLS
  2773. if (doDTLS) {
  2774. switch (minVersion) {
  2775. case 4:
  2776. #ifdef WOLFSSL_DTLS13
  2777. minVersion = WOLFSSL_DTLSV1_3;
  2778. break;
  2779. #else
  2780. err_sys("invalid minimum downgrade version");
  2781. #endif /* WOLFSSL_DTLS13 */
  2782. case 3:
  2783. minVersion = WOLFSSL_DTLSV1_2;
  2784. break;
  2785. case 2:
  2786. minVersion = WOLFSSL_DTLSV1;
  2787. break;
  2788. }
  2789. }
  2790. #endif /* WOLFSSL_DTLS */
  2791. if (wolfSSL_CTX_SetMinVersion(ctx, minVersion) != WOLFSSL_SUCCESS)
  2792. err_sys("can't set minimum downgrade version");
  2793. }
  2794. if (simulateWantWrite) {
  2795. #ifdef USE_WOLFSSL_IO
  2796. wolfSSL_CTX_SetIOSend(ctx, SimulateWantWriteIOSendCb);
  2797. #endif
  2798. }
  2799. #ifdef SINGLE_THREADED
  2800. if (wolfSSL_CTX_new_rng(ctx) != WOLFSSL_SUCCESS) {
  2801. wolfSSL_CTX_free(ctx); ctx = NULL;
  2802. err_sys("Single Threaded new rng at CTX failed");
  2803. }
  2804. #endif
  2805. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2806. /* Restore wolfSSL verify defaults */
  2807. if (ctx) {
  2808. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL);
  2809. }
  2810. #endif
  2811. #ifdef WOLFSSL_SRTP
  2812. if (dtlsSrtpProfiles != NULL) {
  2813. if (wolfSSL_CTX_set_tlsext_use_srtp(ctx, dtlsSrtpProfiles)
  2814. != WOLFSSL_SUCCESS) {
  2815. err_sys("unable to set DTLS SRTP profile");
  2816. }
  2817. }
  2818. #endif
  2819. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2820. if (wolfsentry_setup(&wolfsentry, wolfsentry_config_path,
  2821. WOLFSENTRY_ROUTE_FLAG_DIRECTION_OUT) < 0) {
  2822. err_sys("unable to initialize wolfSentry");
  2823. }
  2824. if (wolfSSL_CTX_set_ConnectFilter(
  2825. ctx,
  2826. (NetworkFilterCallback_t)wolfSentry_NetworkFilterCallback,
  2827. wolfsentry) < 0) {
  2828. err_sys("unable to install wolfSentry_NetworkFilterCallback");
  2829. }
  2830. #endif
  2831. if (cipherList && !useDefCipherList) {
  2832. if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS) {
  2833. wolfSSL_CTX_free(ctx); ctx = NULL;
  2834. err_sys("client can't set cipher list 1");
  2835. }
  2836. }
  2837. #ifdef WOLFSSL_LEANPSK
  2838. if (!usePsk) {
  2839. usePsk = 1;
  2840. }
  2841. #endif
  2842. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2843. !defined(HAVE_ED448)
  2844. if (!usePsk) {
  2845. usePsk = 1;
  2846. }
  2847. #endif
  2848. if (fewerPackets)
  2849. wolfSSL_CTX_set_group_messages(ctx);
  2850. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2851. defined(WOLFSSL_DTLS)
  2852. if (dtlsMTU)
  2853. wolfSSL_CTX_dtls_set_mtu(ctx, dtlsMTU);
  2854. #endif
  2855. #ifndef NO_DH
  2856. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2857. != WOLFSSL_SUCCESS) {
  2858. err_sys("Error setting minimum DH key size");
  2859. }
  2860. #endif
  2861. if (usePsk) {
  2862. #ifndef NO_PSK
  2863. const char *defaultCipherList = cipherList;
  2864. wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  2865. #ifdef WOLFSSL_TLS13
  2866. #if !defined(WOLFSSL_PSK_TLS13_CB) && !defined(WOLFSSL_PSK_ONE_ID)
  2867. if (!opensslPsk) {
  2868. wolfSSL_CTX_set_psk_client_cs_callback(ctx, my_psk_client_cs_cb);
  2869. }
  2870. else
  2871. #endif
  2872. {
  2873. wolfSSL_CTX_set_psk_client_tls13_callback(ctx,
  2874. my_psk_client_tls13_cb);
  2875. }
  2876. #endif
  2877. if (defaultCipherList == NULL) {
  2878. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2879. #ifdef WOLFSSL_TLS13
  2880. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2881. #ifndef WOLFSSL_NO_TLS12
  2882. ":DHE-PSK-AES128-GCM-SHA256"
  2883. #endif
  2884. ;
  2885. #else
  2886. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2887. #endif
  2888. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  2889. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2890. #ifndef WOLFSSL_NO_TLS12
  2891. ":PSK-AES128-GCM-SHA256"
  2892. #endif
  2893. ;
  2894. #elif defined(HAVE_NULL_CIPHER)
  2895. defaultCipherList = "PSK-NULL-SHA256";
  2896. #elif !defined(NO_AES_CBC)
  2897. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2898. #else
  2899. defaultCipherList = "PSK-AES128-GCM-SHA256";
  2900. #endif
  2901. if (wolfSSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2902. !=WOLFSSL_SUCCESS) {
  2903. wolfSSL_CTX_free(ctx); ctx = NULL;
  2904. err_sys("client can't set cipher list 2");
  2905. }
  2906. }
  2907. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2908. #endif
  2909. if (useClientCert) {
  2910. useClientCert = 0;
  2911. }
  2912. }
  2913. if (useAnon) {
  2914. #ifdef HAVE_ANON
  2915. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2916. const char* defaultCipherList;
  2917. wolfSSL_CTX_allow_anon_cipher(ctx);
  2918. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2919. "ADH-AES128-SHA";
  2920. if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList)
  2921. != WOLFSSL_SUCCESS) {
  2922. wolfSSL_CTX_free(ctx); ctx = NULL;
  2923. err_sys("client can't set cipher list 4");
  2924. }
  2925. }
  2926. #endif
  2927. if (useClientCert) {
  2928. useClientCert = 0;
  2929. }
  2930. }
  2931. #ifdef WOLFSSL_SCTP
  2932. if (dtlsSCTP)
  2933. wolfSSL_CTX_dtls_set_sctp(ctx);
  2934. #endif
  2935. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2936. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2937. #endif
  2938. #ifdef WOLFSSL_SNIFFER
  2939. if (cipherList == NULL && version < 4) {
  2940. /* static RSA or ECC cipher suites */
  2941. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2942. if (wolfSSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2943. wolfSSL_CTX_free(ctx); ctx = NULL;
  2944. err_sys("client can't set cipher list 3");
  2945. }
  2946. }
  2947. #endif
  2948. #ifdef HAVE_OCSP
  2949. if (useOcsp) {
  2950. #if defined(HAVE_IO_TIMEOUT) && defined(HAVE_HTTP_CLIENT)
  2951. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  2952. #endif
  2953. if (ocspUrl != NULL) {
  2954. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2955. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2956. | WOLFSSL_OCSP_URL_OVERRIDE);
  2957. }
  2958. else {
  2959. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL);
  2960. }
  2961. #ifdef WOLFSSL_NONBLOCK_OCSP
  2962. wolfSSL_CTX_SetOCSP_Cb(ctx, OCSPIOCb, OCSPRespFreeCb, NULL);
  2963. #endif
  2964. }
  2965. #endif
  2966. #ifdef USER_CA_CB
  2967. wolfSSL_CTX_SetCACb(ctx, CaCb);
  2968. #endif
  2969. #if defined(HAVE_EXT_CACHE) && !defined(NO_SESSION_CACHE)
  2970. wolfSSL_CTX_sess_set_get_cb(ctx, mySessGetCb);
  2971. wolfSSL_CTX_sess_set_new_cb(ctx, mySessNewCb);
  2972. wolfSSL_CTX_sess_set_remove_cb(ctx, mySessRemCb);
  2973. #endif
  2974. #ifndef NO_CERTS
  2975. if (useClientCert && !loadCertKeyIntoSSLObj){
  2976. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2977. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  2978. client_cert_der_2048, sizeof_client_cert_der_2048,
  2979. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2980. err_sys("can't load client cert buffer");
  2981. #elif !defined(TEST_LOAD_BUFFER)
  2982. if (wolfSSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2983. != WOLFSSL_SUCCESS) {
  2984. wolfSSL_CTX_free(ctx); ctx = NULL;
  2985. err_sys("can't load client cert file, check file and run from"
  2986. " wolfSSL home dir");
  2987. }
  2988. #else
  2989. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2990. #endif
  2991. }
  2992. #ifdef HAVE_PK_CALLBACKS
  2993. pkCbInfo.ourKey = ourKey;
  2994. #endif
  2995. if (useClientCert && !loadCertKeyIntoSSLObj
  2996. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2997. && !pkCallbacks
  2998. #endif
  2999. ) {
  3000. #ifdef NO_FILESYSTEM
  3001. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, client_key_der_2048,
  3002. sizeof_client_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  3003. err_sys("can't load client private key buffer");
  3004. #elif !defined(TEST_LOAD_BUFFER)
  3005. if (wolfSSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  3006. != WOLFSSL_SUCCESS) {
  3007. wolfSSL_CTX_free(ctx); ctx = NULL;
  3008. err_sys("can't load client private key file, check file and run "
  3009. "from wolfSSL home dir");
  3010. }
  3011. #else
  3012. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  3013. #endif
  3014. }
  3015. if (!usePsk && !useAnon && !useVerifyCb && myVerifyAction != VERIFY_FORCE_FAIL) {
  3016. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  3017. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  3018. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  3019. if (useCertFolder) {
  3020. WOLFSSL_X509_STORE *store;
  3021. WOLFSSL_X509_LOOKUP *lookup;
  3022. store = wolfSSL_CTX_get_cert_store(ctx);
  3023. if (store == NULL) {
  3024. wolfSSL_CTX_free(ctx); ctx = NULL;
  3025. err_sys("can't get WOLFSSL_X509_STORE");
  3026. }
  3027. lookup = wolfSSL_X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  3028. if (lookup == NULL) {
  3029. wolfSSL_CTX_free(ctx); ctx = NULL;
  3030. err_sys("can't add lookup");
  3031. }
  3032. if (wolfSSL_X509_LOOKUP_ctrl(lookup, WOLFSSL_X509_L_ADD_DIR, caCertFolder,
  3033. X509_FILETYPE_PEM, NULL) != WOLFSSL_SUCCESS) {
  3034. err_sys("X509_LOOKUP_ctrl w/ L_ADD_DIR failed");
  3035. }
  3036. } else {
  3037. #endif
  3038. #ifdef NO_FILESYSTEM
  3039. if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,
  3040. sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  3041. wolfSSL_CTX_free(ctx); ctx = NULL;
  3042. err_sys("can't load ca buffer, Please run from wolfSSL home dir");
  3043. }
  3044. #elif !defined(TEST_LOAD_BUFFER)
  3045. unsigned int verify_flags = 0;
  3046. #ifdef TEST_BEFORE_DATE
  3047. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  3048. #endif
  3049. if (doPeerCheck != 0 &&
  3050. wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0, verify_flags)
  3051. != WOLFSSL_SUCCESS) {
  3052. wolfSSL_CTX_free(ctx); ctx = NULL;
  3053. err_sys("can't load ca file, Please run from wolfSSL home dir");
  3054. }
  3055. #else
  3056. load_buffer(ctx, verifyCert, WOLFSSL_CA);
  3057. #endif /* !NO_FILESYSTEM */
  3058. #ifdef HAVE_ECC
  3059. /* load ecc verify too, echoserver uses it by default w/ ecc */
  3060. #ifdef NO_FILESYSTEM
  3061. if (doPeerCheck != 0 &&
  3062. wolfSSL_CTX_load_verify_buffer(ctx, ca_ecc_cert_der_256,
  3063. sizeof_ca_ecc_cert_der_256, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  3064. wolfSSL_CTX_free(ctx); ctx = NULL;
  3065. err_sys("can't load ecc ca buffer");
  3066. }
  3067. #elif !defined(TEST_LOAD_BUFFER)
  3068. if (doPeerCheck != 0 && !customVerifyCert &&
  3069. wolfSSL_CTX_load_verify_locations_ex(ctx, eccCertFile, 0, verify_flags)
  3070. != WOLFSSL_SUCCESS) {
  3071. wolfSSL_CTX_free(ctx); ctx = NULL;
  3072. err_sys("can't load ecc ca file, Please run from wolfSSL home dir");
  3073. }
  3074. #else
  3075. load_buffer(ctx, eccCertFile, WOLFSSL_CA);
  3076. #endif /* !TEST_LOAD_BUFFER */
  3077. #endif /* HAVE_ECC */
  3078. #if defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_FILESYSTEM)
  3079. if (trustCert) {
  3080. if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  3081. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3082. wolfSSL_CTX_free(ctx); ctx = NULL;
  3083. err_sys("can't load trusted peer cert file");
  3084. }
  3085. }
  3086. #endif /* WOLFSSL_TRUST_PEER_CERT && !NO_FILESYSTEM */
  3087. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  3088. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  3089. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  3090. }
  3091. #endif
  3092. }
  3093. if (useVerifyCb || myVerifyAction == VERIFY_FORCE_FAIL ||
  3094. myVerifyAction == VERIFY_USE_PREVERFIY) {
  3095. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  3096. }
  3097. else if (!usePsk && !useAnon && doPeerCheck == 0) {
  3098. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  3099. }
  3100. else if (!usePsk && !useAnon && myVerifyAction == VERIFY_OVERRIDE_DATE_ERR) {
  3101. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  3102. }
  3103. #endif /* !NO_CERTS */
  3104. #ifdef WOLFSSL_ASYNC_CRYPT
  3105. ret = wolfAsync_DevOpen(&devId);
  3106. if (ret < 0) {
  3107. LOG_ERROR("Async device open failed\nRunning without async\n");
  3108. }
  3109. wolfSSL_CTX_SetDevId(ctx, devId);
  3110. #endif /* WOLFSSL_ASYNC_CRYPT */
  3111. #ifdef HAVE_SNI
  3112. if (sniHostName) {
  3113. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  3114. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS) {
  3115. wolfSSL_CTX_free(ctx); ctx = NULL;
  3116. err_sys("UseSNI failed");
  3117. }
  3118. }
  3119. #endif
  3120. #ifdef HAVE_MAX_FRAGMENT
  3121. if (maxFragment)
  3122. if (wolfSSL_CTX_UseMaxFragment(ctx, maxFragment) != WOLFSSL_SUCCESS) {
  3123. wolfSSL_CTX_free(ctx); ctx = NULL;
  3124. err_sys("UseMaxFragment failed");
  3125. }
  3126. #endif
  3127. #ifdef HAVE_TRUNCATED_HMAC
  3128. if (truncatedHMAC)
  3129. if (wolfSSL_CTX_UseTruncatedHMAC(ctx) != WOLFSSL_SUCCESS) {
  3130. wolfSSL_CTX_free(ctx); ctx = NULL;
  3131. err_sys("UseTruncatedHMAC failed");
  3132. }
  3133. #endif
  3134. #ifdef HAVE_SESSION_TICKET
  3135. if (wolfSSL_CTX_UseSessionTicket(ctx) != WOLFSSL_SUCCESS) {
  3136. wolfSSL_CTX_free(ctx); ctx = NULL;
  3137. err_sys("UseSessionTicket failed");
  3138. }
  3139. #endif
  3140. #ifdef HAVE_EXTENDED_MASTER
  3141. if (disableExtMasterSecret)
  3142. if (wolfSSL_CTX_DisableExtendedMasterSecret(ctx) != WOLFSSL_SUCCESS) {
  3143. wolfSSL_CTX_free(ctx); ctx = NULL;
  3144. err_sys("DisableExtendedMasterSecret failed");
  3145. }
  3146. #endif
  3147. #if defined(HAVE_SUPPORTED_CURVES)
  3148. #if defined(HAVE_CURVE25519)
  3149. if (useX25519) {
  3150. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X25519)
  3151. != WOLFSSL_SUCCESS) {
  3152. err_sys("unable to support X25519");
  3153. }
  3154. }
  3155. #endif /* HAVE_CURVE25519 */
  3156. #if defined(HAVE_CURVE448)
  3157. if (useX448) {
  3158. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X448)
  3159. != WOLFSSL_SUCCESS) {
  3160. err_sys("unable to support X448");
  3161. }
  3162. }
  3163. #endif /* HAVE_CURVE448 */
  3164. #ifdef HAVE_ECC
  3165. if (useSupCurve) {
  3166. #if !defined(NO_ECC_SECP) && \
  3167. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  3168. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP384R1)
  3169. != WOLFSSL_SUCCESS) {
  3170. err_sys("unable to support secp384r1");
  3171. }
  3172. #endif
  3173. #if !defined(NO_ECC_SECP) && \
  3174. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  3175. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1)
  3176. != WOLFSSL_SUCCESS) {
  3177. err_sys("unable to support secp256r1");
  3178. }
  3179. #endif
  3180. }
  3181. #endif /* HAVE_ECC */
  3182. #ifdef HAVE_FFDHE_2048
  3183. if (useSupCurve) {
  3184. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_FFDHE_2048)
  3185. != WOLFSSL_SUCCESS) {
  3186. err_sys("unable to support FFDHE 2048");
  3187. }
  3188. }
  3189. #endif
  3190. #endif /* HAVE_SUPPORTED_CURVES */
  3191. #ifdef WOLFSSL_TLS13
  3192. if (noPskDheKe)
  3193. wolfSSL_CTX_no_dhe_psk(ctx);
  3194. #ifdef HAVE_SUPPORTED_CURVES
  3195. if (onlyPskDheKe)
  3196. wolfSSL_CTX_only_dhe_psk(ctx);
  3197. #endif
  3198. #endif
  3199. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3200. if (postHandAuth)
  3201. wolfSSL_CTX_allow_post_handshake_auth(ctx);
  3202. #endif
  3203. if (benchmark) {
  3204. ((func_args*)args)->return_code =
  3205. ClientBenchmarkConnections(ctx, host, port, dtlsUDP, dtlsSCTP,
  3206. benchmark, resumeSession, useX25519,
  3207. useX448, usePqc, pqcAlg, helloRetry,
  3208. onlyKeyShare, version, earlyData);
  3209. wolfSSL_CTX_free(ctx); ctx = NULL;
  3210. XEXIT_T(EXIT_SUCCESS);
  3211. }
  3212. if (throughput) {
  3213. ((func_args*)args)->return_code =
  3214. ClientBenchmarkThroughput(ctx, host, port, dtlsUDP, dtlsSCTP,
  3215. block, throughput, useX25519, useX448,
  3216. usePqc, pqcAlg, exitWithRet, version,
  3217. onlyKeyShare);
  3218. wolfSSL_CTX_free(ctx); ctx = NULL;
  3219. if (((func_args*)args)->return_code != EXIT_SUCCESS && !exitWithRet)
  3220. XEXIT_T(EXIT_SUCCESS);
  3221. else
  3222. goto exit;
  3223. }
  3224. #if defined(WOLFSSL_MDK_ARM)
  3225. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  3226. #endif
  3227. #if defined(OPENSSL_EXTRA)
  3228. if (wolfSSL_CTX_get_read_ahead(ctx) != 0) {
  3229. wolfSSL_CTX_free(ctx); ctx = NULL;
  3230. err_sys("bad read ahead default value");
  3231. }
  3232. if (wolfSSL_CTX_set_read_ahead(ctx, 1) != WOLFSSL_SUCCESS) {
  3233. wolfSSL_CTX_free(ctx); ctx = NULL;
  3234. err_sys("error setting read ahead value");
  3235. }
  3236. #endif
  3237. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  3238. LOG_ERROR("Before creating SSL\n");
  3239. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  3240. err_sys("ctx not using static memory");
  3241. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  3242. err_sys("error printing out memory stats");
  3243. #endif
  3244. if (doMcast) {
  3245. #ifdef WOLFSSL_MULTICAST
  3246. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  3247. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  3248. != WOLFSSL_SUCCESS) {
  3249. wolfSSL_CTX_free(ctx); ctx = NULL;
  3250. err_sys("Couldn't set multicast cipher list.");
  3251. }
  3252. #endif
  3253. }
  3254. #ifdef HAVE_PK_CALLBACKS
  3255. if (pkCallbacks)
  3256. SetupPkCallbacks(ctx);
  3257. #endif
  3258. ssl = wolfSSL_new(ctx);
  3259. if (ssl == NULL) {
  3260. wolfSSL_CTX_free(ctx); ctx = NULL;
  3261. err_sys("unable to get SSL object");
  3262. }
  3263. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3264. if (!wolfSSL_UseCKS(ssl, cks_order, sizeof(cks_order))) {
  3265. wolfSSL_CTX_free(ctx); ctx = NULL;
  3266. err_sys("unable to set the CKS order.");
  3267. }
  3268. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  3269. #ifndef NO_PSK
  3270. if (usePsk) {
  3271. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(TEST_PSK_USE_SESSION)
  3272. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  3273. #endif
  3274. }
  3275. #endif
  3276. #ifndef NO_CERTS
  3277. if (useClientCert && loadCertKeyIntoSSLObj){
  3278. #ifdef NO_FILESYSTEM
  3279. if (wolfSSL_use_certificate_buffer(ssl, client_cert_der_2048,
  3280. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  3281. wolfSSL_CTX_free(ctx); ctx = NULL;
  3282. err_sys("can't load client cert buffer");
  3283. }
  3284. #elif !defined(TEST_LOAD_BUFFER)
  3285. if (wolfSSL_use_certificate_chain_file(ssl, ourCert)
  3286. != WOLFSSL_SUCCESS) {
  3287. wolfSSL_CTX_free(ctx); ctx = NULL;
  3288. err_sys("can't load client cert file, check file and run from"
  3289. " wolfSSL home dir");
  3290. }
  3291. #else
  3292. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  3293. #endif
  3294. }
  3295. if (loadCertKeyIntoSSLObj
  3296. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  3297. && !pkCallbacks
  3298. #endif
  3299. ) {
  3300. #ifdef NO_FILESYSTEM
  3301. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, client_key_der_2048,
  3302. sizeof_client_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  3303. err_sys("can't load client private key buffer");
  3304. #elif !defined(TEST_LOAD_BUFFER)
  3305. if (wolfSSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  3306. != WOLFSSL_SUCCESS) {
  3307. wolfSSL_CTX_free(ctx); ctx = NULL;
  3308. err_sys("can't load client private key file, check file and run "
  3309. "from wolfSSL home dir");
  3310. }
  3311. #else
  3312. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  3313. #endif
  3314. }
  3315. #endif /* !NO_CERTS */
  3316. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  3317. wolfSSL_KeepArrays(ssl);
  3318. #endif
  3319. #ifdef HAVE_PK_CALLBACKS
  3320. /* This must be before SetKeyShare */
  3321. if (pkCallbacks) {
  3322. SetupPkCallbackContexts(ssl, &pkCbInfo);
  3323. }
  3324. #endif
  3325. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  3326. LOG_ERROR("After creating SSL\n");
  3327. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  3328. err_sys("ctx not using static memory");
  3329. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  3330. err_sys("error printing out memory stats");
  3331. #endif
  3332. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  3333. if (!helloRetry && (version >= 4 || version <= -4)) {
  3334. SetKeyShare(ssl, onlyKeyShare, useX25519, useX448, usePqc,
  3335. pqcAlg, 0);
  3336. }
  3337. else {
  3338. wolfSSL_NoKeyShares(ssl);
  3339. }
  3340. #endif
  3341. if (doMcast) {
  3342. #ifdef WOLFSSL_MULTICAST
  3343. /* DTLS multicast secret for testing only */
  3344. #define CLI_SRV_RANDOM_SZ 32 /* RAN_LEN (see internal.h) */
  3345. #define PMS_SZ 512 /* ENCRYPT_LEN (see internal.h) */
  3346. byte pms[PMS_SZ]; /* pre master secret */
  3347. byte cr[CLI_SRV_RANDOM_SZ]; /* client random */
  3348. byte sr[CLI_SRV_RANDOM_SZ]; /* server random */
  3349. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  3350. XMEMSET(pms, 0x23, sizeof(pms));
  3351. XMEMSET(cr, 0xA5, sizeof(cr));
  3352. XMEMSET(sr, 0x5A, sizeof(sr));
  3353. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  3354. != WOLFSSL_SUCCESS) {
  3355. wolfSSL_CTX_free(ctx); ctx = NULL;
  3356. err_sys("unable to set mcast secret");
  3357. }
  3358. #endif
  3359. }
  3360. #ifdef HAVE_SESSION_TICKET
  3361. wolfSSL_set_SessionTicket_cb(ssl, sessionTicketCB, (void*)"initial session");
  3362. #endif
  3363. #ifdef HAVE_TRUSTED_CA
  3364. if (trustedCaKeyId) {
  3365. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  3366. NULL, 0) != WOLFSSL_SUCCESS) {
  3367. err_sys("UseTrustedCA failed");
  3368. }
  3369. }
  3370. #endif
  3371. #ifdef HAVE_ALPN
  3372. if (alpnList != NULL) {
  3373. printf("ALPN accepted protocols list : %s\n", alpnList);
  3374. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  3375. }
  3376. #endif
  3377. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3378. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3379. if (statusRequest) {
  3380. if (version == 4 &&
  3381. (statusRequest == OCSP_STAPLINGV2 || \
  3382. statusRequest == OCSP_STAPLINGV2_MULTI)) {
  3383. err_sys("Cannot use OCSP Stapling V2 with TLSv1.3");
  3384. }
  3385. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS)
  3386. err_sys("can't enable OCSP Stapling Certificate Manager");
  3387. if (mustStaple) {
  3388. if (wolfSSL_CTX_EnableOCSPMustStaple(ctx) != WOLFSSL_SUCCESS)
  3389. err_sys("can't enable OCSP Must Staple");
  3390. }
  3391. switch (statusRequest) {
  3392. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  3393. case OCSP_STAPLING:
  3394. if (wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR_OCSP,
  3395. WOLFSSL_CSR_OCSP_USE_NONCE) != WOLFSSL_SUCCESS) {
  3396. wolfSSL_free(ssl); ssl = NULL;
  3397. CloseSocket(sockfd);
  3398. wolfSSL_CTX_free(ctx); ctx = NULL;
  3399. err_sys("UseCertificateStatusRequest failed");
  3400. }
  3401. break;
  3402. #endif
  3403. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  3404. case OCSP_STAPLINGV2:
  3405. if (wolfSSL_UseOCSPStaplingV2(ssl,
  3406. WOLFSSL_CSR2_OCSP, WOLFSSL_CSR2_OCSP_USE_NONCE)
  3407. != WOLFSSL_SUCCESS) {
  3408. wolfSSL_free(ssl); ssl = NULL;
  3409. CloseSocket(sockfd);
  3410. wolfSSL_CTX_free(ctx); ctx = NULL;
  3411. err_sys("UseCertificateStatusRequest failed");
  3412. }
  3413. break;
  3414. case OCSP_STAPLINGV2_MULTI:
  3415. if (wolfSSL_UseOCSPStaplingV2(ssl,
  3416. WOLFSSL_CSR2_OCSP_MULTI, 0)
  3417. != WOLFSSL_SUCCESS) {
  3418. wolfSSL_free(ssl); ssl = NULL;
  3419. CloseSocket(sockfd);
  3420. wolfSSL_CTX_free(ctx); ctx = NULL;
  3421. err_sys("UseCertificateStatusRequest failed");
  3422. }
  3423. break;
  3424. #endif
  3425. default:
  3426. err_sys("Invalid OCSP Stapling option");
  3427. }
  3428. wolfSSL_CTX_EnableOCSP(ctx, 0);
  3429. }
  3430. #endif
  3431. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3432. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3433. if (!doDhKeyCheck)
  3434. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  3435. #endif
  3436. #ifdef HAVE_ENCRYPT_THEN_MAC
  3437. if (disallowETM)
  3438. wolfSSL_AllowEncryptThenMac(ssl, 0);
  3439. #endif
  3440. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
  3441. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  3442. wolfSSL_free(ssl); ssl = NULL;
  3443. CloseSocket(sockfd);
  3444. wolfSSL_CTX_free(ctx); ctx = NULL;
  3445. err_sys("error in setting fd");
  3446. }
  3447. if (simulateWantWrite) {
  3448. if (dtlsUDP) {
  3449. wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd);
  3450. udp_connect(&sockfd, host, port);
  3451. }
  3452. }
  3453. /* STARTTLS */
  3454. if (doSTARTTLS) {
  3455. if (StartTLS_Init(&sockfd) != WOLFSSL_SUCCESS) {
  3456. wolfSSL_free(ssl); ssl = NULL;
  3457. CloseSocket(sockfd);
  3458. wolfSSL_CTX_free(ctx); ctx = NULL;
  3459. err_sys("error during STARTTLS protocol");
  3460. }
  3461. }
  3462. #if defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
  3463. if (disableCRL == 0 && !useVerifyCb) {
  3464. #if defined(HAVE_IO_TIMEOUT) && defined(HAVE_HTTP_CLIENT)
  3465. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  3466. #endif
  3467. if (wolfSSL_EnableCRL(ssl, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS) {
  3468. wolfSSL_free(ssl); ssl = NULL;
  3469. CloseSocket(sockfd);
  3470. wolfSSL_CTX_free(ctx); ctx = NULL;
  3471. err_sys("can't enable crl check");
  3472. }
  3473. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, 0)
  3474. != WOLFSSL_SUCCESS) {
  3475. wolfSSL_free(ssl); ssl = NULL;
  3476. CloseSocket(sockfd);
  3477. wolfSSL_CTX_free(ctx); ctx = NULL;
  3478. err_sys("can't load crl, check crlfile and date validity");
  3479. }
  3480. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS) {
  3481. wolfSSL_free(ssl); ssl = NULL;
  3482. CloseSocket(sockfd);
  3483. wolfSSL_CTX_free(ctx); ctx = NULL;
  3484. err_sys("can't set crl callback");
  3485. }
  3486. }
  3487. #endif
  3488. #ifdef HAVE_SECURE_RENEGOTIATION
  3489. if (scr) {
  3490. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  3491. wolfSSL_free(ssl); ssl = NULL;
  3492. CloseSocket(sockfd);
  3493. wolfSSL_CTX_free(ctx); ctx = NULL;
  3494. err_sys("can't enable secure renegotiation");
  3495. }
  3496. }
  3497. #endif
  3498. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  3499. if (atomicUser)
  3500. SetupAtomicUser(ctx, ssl);
  3501. #endif
  3502. #ifdef WOLFSSL_DTLS_CID
  3503. if (useDtlsCID) {
  3504. ret = wolfSSL_dtls_cid_use(ssl);
  3505. if (ret != WOLFSSL_SUCCESS)
  3506. err_sys("Can't enable DTLS ConnectionID");
  3507. ret = wolfSSL_dtls_cid_set(ssl, (unsigned char*)dtlsCID,
  3508. (word32)XSTRLEN(dtlsCID));
  3509. if (ret != WOLFSSL_SUCCESS)
  3510. err_sys("Can't set DTLS ConnectionID");
  3511. }
  3512. #endif /* WOLFSSL_DTLS_CID */
  3513. if (matchName && doPeerCheck)
  3514. wolfSSL_check_domain_name(ssl, domain);
  3515. #ifndef WOLFSSL_CALLBACKS
  3516. if (nonBlocking) {
  3517. #ifdef WOLFSSL_DTLS
  3518. if (doDTLS) {
  3519. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3520. }
  3521. #endif
  3522. tcp_set_nonblocking(&sockfd);
  3523. ret = NonBlockingSSL_Connect(ssl);
  3524. }
  3525. else {
  3526. #ifdef WOLFSSL_EARLY_DATA
  3527. if (usePsk && earlyData)
  3528. EarlyData(ctx, ssl, kEarlyMsg, sizeof(kEarlyMsg)-1, buffer);
  3529. #endif
  3530. do {
  3531. err = 0; /* reset error */
  3532. ret = wolfSSL_connect(ssl);
  3533. if (ret != WOLFSSL_SUCCESS) {
  3534. err = wolfSSL_get_error(ssl, 0);
  3535. #ifdef WOLFSSL_ASYNC_CRYPT
  3536. if (err == WC_PENDING_E) {
  3537. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3538. if (ret < 0) break;
  3539. }
  3540. #endif
  3541. }
  3542. } while (err == WC_PENDING_E);
  3543. }
  3544. #else
  3545. timeoutConnect.tv_sec = DEFAULT_TIMEOUT_SEC;
  3546. timeoutConnect.tv_usec = 0;
  3547. ret = NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  3548. #endif
  3549. if (ret != WOLFSSL_SUCCESS) {
  3550. err = wolfSSL_get_error(ssl, 0);
  3551. LOG_ERROR("wolfSSL_connect error %d, %s\n", err,
  3552. wolfSSL_ERR_error_string(err, buffer));
  3553. /* cleanup */
  3554. wolfSSL_free(ssl); ssl = NULL;
  3555. wolfSSL_CTX_free(ctx); ctx = NULL;
  3556. CloseSocket(sockfd);
  3557. if (!exitWithRet)
  3558. err_sys("wolfSSL_connect failed");
  3559. /* see note at top of README */
  3560. /* if you're getting an error here */
  3561. ((func_args*)args)->return_code = err;
  3562. goto exit;
  3563. }
  3564. showPeerEx(ssl, lng_index);
  3565. showPeerPEM(ssl);
  3566. /* if the caller requested a particular cipher, check here that either
  3567. * a canonical name of the established cipher matches the requested
  3568. * cipher name, or the requested cipher name is marked as an alias
  3569. * that matches the established cipher.
  3570. */
  3571. if (cipherList && !useDefCipherList && (! XSTRSTR(cipherList, ":"))) {
  3572. WOLFSSL_CIPHER* established_cipher = wolfSSL_get_current_cipher(ssl);
  3573. byte requested_cipherSuite0, requested_cipherSuite;
  3574. int requested_cipherFlags;
  3575. if (established_cipher &&
  3576. /* don't test for pseudo-ciphers like "ALL" and "DEFAULT". */
  3577. (wolfSSL_get_cipher_suite_from_name(cipherList,
  3578. &requested_cipherSuite0,
  3579. &requested_cipherSuite,
  3580. &requested_cipherFlags) == 0)) {
  3581. word32 established_cipher_id =
  3582. wolfSSL_CIPHER_get_id(established_cipher);
  3583. byte established_cipherSuite0 = (established_cipher_id >> 8) & 0xff;
  3584. byte established_cipherSuite = established_cipher_id & 0xff;
  3585. const char *established_cipher_name =
  3586. wolfSSL_get_cipher_name_from_suite(established_cipherSuite0,
  3587. established_cipherSuite);
  3588. const char *established_cipher_name_iana =
  3589. wolfSSL_get_cipher_name_iana_from_suite(established_cipherSuite0,
  3590. established_cipherSuite);
  3591. if (established_cipher_name == NULL)
  3592. err_sys("error looking up name of established cipher");
  3593. if (strcmp(cipherList, established_cipher_name) &&
  3594. ((established_cipher_name_iana == NULL) ||
  3595. strcmp(cipherList, established_cipher_name_iana))) {
  3596. if (! (requested_cipherFlags & WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS))
  3597. err_sys("Unexpected mismatch between names of requested and established ciphers.");
  3598. else if ((requested_cipherSuite0 != established_cipherSuite0) ||
  3599. (requested_cipherSuite != established_cipherSuite))
  3600. err_sys("Mismatch between IDs of requested and established ciphers.");
  3601. }
  3602. }
  3603. }
  3604. #if defined(HAVE_OCSP) && !defined(NO_ASN_TIME)
  3605. #ifdef HAVE_STRFTIME
  3606. {
  3607. struct tm tm;
  3608. char date[32];
  3609. ret = wolfSSL_get_ocsp_producedDate_tm(ssl, &tm);
  3610. if ((ret == 0) && (strftime(date, sizeof date, "%Y-%m-%d %H:%M:%S %z", &tm) > 0))
  3611. printf("OCSP response timestamp: %s\n", date);
  3612. }
  3613. #else
  3614. {
  3615. byte date[MAX_DATE_SIZE];
  3616. int asn_date_format;
  3617. ret = wolfSSL_get_ocsp_producedDate(ssl, date, sizeof date, &asn_date_format);
  3618. if (ret == 0)
  3619. printf("OCSP response timestamp: %s (ASN.1 type %d)\n", (char *)date, asn_date_format);
  3620. }
  3621. #endif
  3622. #endif
  3623. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  3624. printf("Session timeout set to %ld seconds\n", wolfSSL_get_timeout(ssl));
  3625. {
  3626. byte* rnd;
  3627. byte* pt;
  3628. size_t size;
  3629. /* get size of buffer then print */
  3630. size = wolfSSL_get_client_random(NULL, NULL, 0);
  3631. if (size == 0) {
  3632. wolfSSL_free(ssl); ssl = NULL;
  3633. CloseSocket(sockfd);
  3634. wolfSSL_CTX_free(ctx); ctx = NULL;
  3635. err_sys("error getting client random buffer size");
  3636. }
  3637. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3638. if (rnd == NULL) {
  3639. wolfSSL_free(ssl); ssl = NULL;
  3640. CloseSocket(sockfd);
  3641. wolfSSL_CTX_free(ctx); ctx = NULL;
  3642. err_sys("error creating client random buffer");
  3643. }
  3644. size = wolfSSL_get_client_random(ssl, rnd, size);
  3645. if (size == 0) {
  3646. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3647. wolfSSL_free(ssl); ssl = NULL;
  3648. CloseSocket(sockfd);
  3649. wolfSSL_CTX_free(ctx); ctx = NULL;
  3650. err_sys("error getting client random buffer");
  3651. }
  3652. printf("Client Random : ");
  3653. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  3654. printf("\n");
  3655. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3656. }
  3657. #endif
  3658. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && (defined(HAVE_STUNNEL) || \
  3659. defined(WOLFSSL_NGINX) || defined(HAVE_LIGHTY) || \
  3660. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_OPENSSH)))
  3661. #if !defined(NO_SESSION_CACHE) && \
  3662. (defined(HAVE_SESSION_TICKET) || defined(SESSION_CERTS)) && \
  3663. !defined(NO_FILESYSTEM)
  3664. #ifndef NO_BIO
  3665. /* print out session to stdout */
  3666. {
  3667. WOLFSSL_BIO* bio = wolfSSL_BIO_new_fp(stdout, BIO_NOCLOSE);
  3668. if (bio != NULL) {
  3669. if (wolfSSL_SESSION_print(bio, wolfSSL_get_session(ssl)) !=
  3670. WOLFSSL_SUCCESS) {
  3671. wolfSSL_BIO_printf(bio, "BIO error printing session\n");
  3672. }
  3673. }
  3674. wolfSSL_BIO_free(bio);
  3675. }
  3676. #endif /* !NO_BIO */
  3677. #endif
  3678. #endif
  3679. if (doSTARTTLS && starttlsProt != NULL) {
  3680. if (XSTRCMP(starttlsProt, "smtp") == 0) {
  3681. if (SMTP_Shutdown(ssl, wc_shutdown) != WOLFSSL_SUCCESS) {
  3682. wolfSSL_free(ssl); ssl = NULL;
  3683. CloseSocket(sockfd);
  3684. wolfSSL_CTX_free(ctx); ctx = NULL;
  3685. err_sys("error closing STARTTLS connection");
  3686. }
  3687. }
  3688. wolfSSL_free(ssl); ssl = NULL;
  3689. CloseSocket(sockfd);
  3690. wolfSSL_CTX_free(ctx); ctx = NULL;
  3691. ((func_args*)args)->return_code = 0;
  3692. WOLFSSL_RETURN_FROM_THREAD(0);
  3693. }
  3694. #ifdef HAVE_ALPN
  3695. if (alpnList != NULL) {
  3696. char *protocol_name = NULL;
  3697. word16 protocol_nameSz = 0;
  3698. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name, &protocol_nameSz);
  3699. if (err == WOLFSSL_SUCCESS)
  3700. printf("Received ALPN protocol : %s (%d)\n",
  3701. protocol_name, protocol_nameSz);
  3702. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  3703. printf("No ALPN response received (no match with server)\n");
  3704. else
  3705. printf("Getting ALPN protocol name failed\n");
  3706. }
  3707. #endif
  3708. #ifdef WOLFSSL_DTLS_CID
  3709. if (useDtlsCID && wolfSSL_dtls_cid_is_enabled(ssl)) {
  3710. unsigned char receivedCID[DTLS_CID_BUFFER_SIZE];
  3711. unsigned int receivedCIDSz;
  3712. printf("CID extension was negotiated\n");
  3713. ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
  3714. if (ret != WOLFSSL_SUCCESS)
  3715. err_sys("Can't get negotiated DTLS CID size\n");
  3716. if (receivedCIDSz > 0) {
  3717. ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
  3718. DTLS_CID_BUFFER_SIZE - 1);
  3719. if (ret != WOLFSSL_SUCCESS)
  3720. err_sys("Can't get negotiated DTLS CID\n");
  3721. printf("Sending CID is ");
  3722. printBuffer(receivedCID, receivedCIDSz);
  3723. printf("\n");
  3724. }
  3725. else {
  3726. printf("other peer provided empty CID\n");
  3727. }
  3728. }
  3729. #endif /* WOLFSSL_DTLS_CID */
  3730. #ifdef HAVE_SECURE_RENEGOTIATION
  3731. if (scr && forceScr) {
  3732. if (nonBlocking) {
  3733. if (!resumeScr) {
  3734. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3735. err = wolfSSL_get_error(ssl, 0);
  3736. if (err == WOLFSSL_ERROR_WANT_READ ||
  3737. err == WOLFSSL_ERROR_WANT_WRITE) {
  3738. if (scrAppData) {
  3739. ret = ClientWrite(ssl,
  3740. "msg sent during renegotiation",
  3741. sizeof("msg sent during renegotiation") - 1,
  3742. "", 1);
  3743. }
  3744. else {
  3745. ret = 0;
  3746. }
  3747. if (ret != 0) {
  3748. ret = WOLFSSL_FAILURE;
  3749. }
  3750. else {
  3751. do {
  3752. #ifdef WOLFSSL_ASYNC_CRYPT
  3753. if (err == WC_PENDING_E) {
  3754. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3755. if (ret < 0) break;
  3756. }
  3757. #endif
  3758. if (err == APP_DATA_READY) {
  3759. if (wolfSSL_read(ssl, reply,
  3760. sizeof(reply)-1) < 0) {
  3761. err_sys("APP DATA should be present "
  3762. "but error returned");
  3763. }
  3764. printf("Received message during "
  3765. "renegotiation: %s\n", reply);
  3766. }
  3767. err = 0;
  3768. if ((ret = wolfSSL_connect(ssl))
  3769. != WOLFSSL_SUCCESS) {
  3770. err = wolfSSL_get_error(ssl, ret);
  3771. }
  3772. } while (ret != WOLFSSL_SUCCESS &&
  3773. (err == WOLFSSL_ERROR_WANT_READ ||
  3774. err == WOLFSSL_ERROR_WANT_WRITE ||
  3775. err == APP_DATA_READY ||
  3776. err == WC_PENDING_E));
  3777. }
  3778. if (ret == WOLFSSL_SUCCESS) {
  3779. printf("NON-BLOCKING RENEGOTIATION SUCCESSFUL\n");
  3780. }
  3781. }
  3782. if (ret != WOLFSSL_SUCCESS) {
  3783. err = wolfSSL_get_error(ssl, 0);
  3784. LOG_ERROR("wolfSSL_Rehandshake error %d, %s\n", err,
  3785. wolfSSL_ERR_error_string(err, buffer));
  3786. wolfSSL_free(ssl); ssl = NULL;
  3787. CloseSocket(sockfd);
  3788. wolfSSL_CTX_free(ctx); ctx = NULL;
  3789. err_sys("non-blocking wolfSSL_Rehandshake failed");
  3790. }
  3791. }
  3792. }
  3793. else {
  3794. LOG_ERROR("not doing secure resumption with non-blocking");
  3795. }
  3796. } else {
  3797. if (!resumeScr) {
  3798. printf("Beginning secure renegotiation.\n");
  3799. if ((ret = wolfSSL_Rehandshake(ssl)) != WOLFSSL_SUCCESS) {
  3800. err = wolfSSL_get_error(ssl, 0);
  3801. #ifdef WOLFSSL_ASYNC_CRYPT
  3802. while (err == WC_PENDING_E) {
  3803. err = 0;
  3804. ret = wolfSSL_negotiate(ssl);
  3805. if (ret != WOLFSSL_SUCCESS) {
  3806. err = wolfSSL_get_error(ssl, 0);
  3807. if (err == WC_PENDING_E) {
  3808. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3809. if (ret < 0) break;
  3810. }
  3811. }
  3812. }
  3813. #endif
  3814. if (ret != WOLFSSL_SUCCESS) {
  3815. printf("err = %d, %s\n", err,
  3816. wolfSSL_ERR_error_string(err, buffer));
  3817. wolfSSL_free(ssl); ssl = NULL;
  3818. CloseSocket(sockfd);
  3819. wolfSSL_CTX_free(ctx); ctx = NULL;
  3820. err_sys("wolfSSL_Rehandshake failed");
  3821. }
  3822. }
  3823. else {
  3824. printf("RENEGOTIATION SUCCESSFUL\n");
  3825. }
  3826. }
  3827. else {
  3828. printf("Beginning secure resumption.\n");
  3829. if ((ret = wolfSSL_SecureResume(ssl)) != WOLFSSL_SUCCESS) {
  3830. err = wolfSSL_get_error(ssl, 0);
  3831. #ifdef WOLFSSL_ASYNC_CRYPT
  3832. while (err == WC_PENDING_E) {
  3833. err = 0;
  3834. ret = wolfSSL_negotiate(ssl);
  3835. if (ret != WOLFSSL_SUCCESS) {
  3836. err = wolfSSL_get_error(ssl, 0);
  3837. if (err == WC_PENDING_E) {
  3838. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3839. if (ret < 0) break;
  3840. }
  3841. }
  3842. }
  3843. #endif
  3844. if (ret != WOLFSSL_SUCCESS) {
  3845. printf("err = %d, %s\n", err,
  3846. wolfSSL_ERR_error_string(err, buffer));
  3847. wolfSSL_free(ssl); ssl = NULL;
  3848. CloseSocket(sockfd);
  3849. wolfSSL_CTX_free(ctx); ctx = NULL;
  3850. err_sys("wolfSSL_SecureResume failed");
  3851. }
  3852. }
  3853. else {
  3854. printf("SECURE RESUMPTION SUCCESSFUL\n");
  3855. }
  3856. }
  3857. }
  3858. }
  3859. #endif /* HAVE_SECURE_RENEGOTIATION */
  3860. XMEMSET(msg, 0, sizeof(msg));
  3861. if (sendGET) {
  3862. printf("SSL connect ok, sending GET...\n");
  3863. msgSz = (int)XSTRLEN(kHttpGetMsg);
  3864. XMEMCPY(msg, kHttpGetMsg, msgSz);
  3865. }
  3866. else {
  3867. msgSz = (int)XSTRLEN(kHelloMsg);
  3868. XMEMCPY(msg, kHelloMsg, msgSz);
  3869. }
  3870. /* allow some time for exporting the session */
  3871. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  3872. TEST_DELAY();
  3873. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  3874. #ifdef WOLFSSL_SRTP
  3875. if (dtlsSrtpProfiles != NULL) {
  3876. err = client_srtp_test(ssl, (func_args*)args);
  3877. if (err != 0) {
  3878. if (exitWithRet) {
  3879. ((func_args*)args)->return_code = err;
  3880. wolfSSL_free(ssl); ssl = NULL;
  3881. CloseSocket(sockfd);
  3882. wolfSSL_CTX_free(ctx); ctx = NULL;
  3883. goto exit;
  3884. }
  3885. /* else */
  3886. err_sys("SRTP check failed");
  3887. }
  3888. }
  3889. #endif /* WOLFSSL_SRTP */
  3890. #ifdef WOLFSSL_TLS13
  3891. if (updateKeysIVs)
  3892. wolfSSL_update_keys(ssl);
  3893. #endif
  3894. err = ClientWriteRead(ssl, msg, msgSz, reply, sizeof(reply)-1, 1, "",
  3895. exitWithRet);
  3896. if (exitWithRet && (err != 0)) {
  3897. ((func_args*)args)->return_code = err;
  3898. wolfSSL_free(ssl); ssl = NULL;
  3899. CloseSocket(sockfd);
  3900. wolfSSL_CTX_free(ctx); ctx = NULL;
  3901. goto exit;
  3902. }
  3903. #if defined(WOLFSSL_TLS13)
  3904. if (updateKeysIVs || postHandAuth)
  3905. (void)ClientWrite(ssl, msg, msgSz, "", 0);
  3906. #endif
  3907. #if defined(HAVE_SESSION_TICKET)
  3908. while (waitTicket == 1) {
  3909. unsigned char ticketBuf[SESSION_TICKET_LEN];
  3910. int zeroReturn = 0;
  3911. word32 size;
  3912. (void)zeroReturn;
  3913. size = sizeof(ticketBuf);
  3914. err = wolfSSL_get_SessionTicket(ssl, ticketBuf, &size);
  3915. if (err < 0)
  3916. err_sys("wolfSSL_get_SessionTicket failed");
  3917. if (size == 0) {
  3918. err = process_handshake_messages(ssl, !nonBlocking, &zeroReturn);
  3919. if (err < 0)
  3920. err_sys("error waiting for session ticket ");
  3921. }
  3922. else {
  3923. waitTicket = 0;
  3924. }
  3925. }
  3926. #endif
  3927. #ifndef NO_SESSION_CACHE
  3928. if (resumeSession) {
  3929. session = wolfSSL_get1_session(ssl);
  3930. }
  3931. #endif
  3932. #if !defined(NO_SESSION_CACHE) && (defined(OPENSSL_EXTRA) || \
  3933. defined(HAVE_EXT_CACHE))
  3934. if (session != NULL && resumeSession) {
  3935. flatSessionSz = wolfSSL_i2d_SSL_SESSION(session, NULL);
  3936. if (flatSessionSz != 0) {
  3937. int checkSz = wolfSSL_i2d_SSL_SESSION(session, &flatSession);
  3938. if (flatSession == NULL)
  3939. err_sys("error creating flattened session buffer");
  3940. if (checkSz != flatSessionSz) {
  3941. XFREE(flatSession, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3942. err_sys("flat session size check failure");
  3943. }
  3944. /* using heap based flat session, free original session */
  3945. wolfSSL_SESSION_free(session);
  3946. session = NULL;
  3947. }
  3948. }
  3949. #endif
  3950. ret = wolfSSL_shutdown(ssl);
  3951. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  3952. while (tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC) ==
  3953. TEST_RECV_READY) {
  3954. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  3955. if (ret == WOLFSSL_SUCCESS) {
  3956. printf("Bidirectional shutdown complete\n");
  3957. break;
  3958. }
  3959. else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
  3960. LOG_ERROR("Bidirectional shutdown failed\n");
  3961. break;
  3962. }
  3963. }
  3964. if (ret != WOLFSSL_SUCCESS)
  3965. LOG_ERROR("Bidirectional shutdown failed\n");
  3966. }
  3967. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  3968. if (atomicUser)
  3969. FreeAtomicUser(ssl);
  3970. #endif
  3971. /* display collected statistics */
  3972. #ifdef WOLFSSL_STATIC_MEMORY
  3973. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  3974. err_sys("static memory was not used with ssl");
  3975. LOG_ERROR("\nprint off SSL memory stats\n");
  3976. LOG_ERROR("*** This is memory state before wolfSSL_free is called\n");
  3977. wolfSSL_PrintStatsConn(&ssl_stats);
  3978. #endif
  3979. wolfSSL_free(ssl); ssl = NULL;
  3980. CloseSocket(sockfd);
  3981. #ifndef NO_SESSION_CACHE
  3982. if (resumeSession) {
  3983. sslResume = wolfSSL_new(ctx);
  3984. if (sslResume == NULL) {
  3985. wolfSSL_CTX_free(ctx); ctx = NULL;
  3986. err_sys("unable to get SSL object");
  3987. }
  3988. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3989. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3990. if (!doDhKeyCheck)
  3991. wolfSSL_SetEnableDhKeyTest(sslResume, 0);
  3992. #endif
  3993. #ifdef HAVE_PK_CALLBACKS
  3994. if (pkCallbacks) {
  3995. SetupPkCallbackContexts(sslResume, &pkCbInfo);
  3996. }
  3997. #endif
  3998. if (dtlsUDP) {
  3999. TEST_DELAY();
  4000. }
  4001. tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, sslResume);
  4002. if (wolfSSL_set_fd(sslResume, sockfd) != WOLFSSL_SUCCESS) {
  4003. wolfSSL_free(sslResume); sslResume = NULL;
  4004. CloseSocket(sockfd);
  4005. wolfSSL_CTX_free(ctx); ctx = NULL;
  4006. err_sys("error in setting fd");
  4007. }
  4008. if (simulateWantWrite) {
  4009. if (dtlsUDP) {
  4010. wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd);
  4011. udp_connect(&sockfd, host, port);
  4012. }
  4013. }
  4014. #ifdef HAVE_ALPN
  4015. if (alpnList != NULL) {
  4016. printf("ALPN accepted protocols list : %s\n", alpnList);
  4017. wolfSSL_UseALPN(sslResume, alpnList, (word32)XSTRLEN(alpnList),
  4018. alpn_opt);
  4019. }
  4020. #endif
  4021. #ifdef HAVE_SECURE_RENEGOTIATION
  4022. if (scr) {
  4023. if (wolfSSL_UseSecureRenegotiation(sslResume) != WOLFSSL_SUCCESS) {
  4024. wolfSSL_free(sslResume); sslResume = NULL;
  4025. CloseSocket(sockfd);
  4026. wolfSSL_CTX_free(ctx); ctx = NULL;
  4027. err_sys("can't enable secure renegotiation");
  4028. }
  4029. }
  4030. #endif
  4031. #if !defined(NO_SESSION_CACHE) && (defined(OPENSSL_EXTRA) || \
  4032. defined(HAVE_EXT_CACHE))
  4033. if (flatSession) {
  4034. const byte* constFlatSession = flatSession;
  4035. session = wolfSSL_d2i_SSL_SESSION(NULL,
  4036. &constFlatSession, flatSessionSz);
  4037. }
  4038. #endif
  4039. wolfSSL_set_session(sslResume, session);
  4040. #if !defined(NO_SESSION_CACHE) && (defined(OPENSSL_EXTRA) || \
  4041. defined(HAVE_EXT_CACHE))
  4042. XFREE(flatSession, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4043. #endif
  4044. wolfSSL_SESSION_free(session);
  4045. session = NULL;
  4046. #ifdef HAVE_SESSION_TICKET
  4047. wolfSSL_set_SessionTicket_cb(sslResume, sessionTicketCB,
  4048. (void*)"resumed session");
  4049. #endif
  4050. #ifndef WOLFSSL_CALLBACKS
  4051. if (nonBlocking) {
  4052. #ifdef WOLFSSL_DTLS
  4053. if (doDTLS) {
  4054. wolfSSL_dtls_set_using_nonblock(sslResume, 1);
  4055. }
  4056. #endif
  4057. tcp_set_nonblocking(&sockfd);
  4058. ret = NonBlockingSSL_Connect(sslResume);
  4059. }
  4060. else {
  4061. #ifdef WOLFSSL_EARLY_DATA
  4062. #ifndef HAVE_SESSION_TICKET
  4063. if (!usePsk) {
  4064. }
  4065. else
  4066. #endif
  4067. if (earlyData) {
  4068. EarlyData(ctx, sslResume, kEarlyMsg, sizeof(kEarlyMsg)-1, buffer);
  4069. }
  4070. #endif
  4071. do {
  4072. err = 0; /* reset error */
  4073. ret = wolfSSL_connect(sslResume);
  4074. if (ret != WOLFSSL_SUCCESS) {
  4075. err = wolfSSL_get_error(sslResume, 0);
  4076. #ifdef WOLFSSL_ASYNC_CRYPT
  4077. if (err == WC_PENDING_E) {
  4078. ret = wolfSSL_AsyncPoll(sslResume,
  4079. WOLF_POLL_FLAG_CHECK_HW);
  4080. if (ret < 0) break;
  4081. }
  4082. #endif
  4083. }
  4084. } while (err == WC_PENDING_E);
  4085. }
  4086. #else
  4087. timeoutConnect.tv_sec = DEFAULT_TIMEOUT_SEC;
  4088. timeoutConnect.tv_usec = 0;
  4089. ret = NonBlockingSSL_Connect(sslResume); /* will keep retrying on timeout */
  4090. #endif
  4091. if (ret != WOLFSSL_SUCCESS) {
  4092. LOG_ERROR("wolfSSL_connect resume error %d, %s\n", err,
  4093. wolfSSL_ERR_error_string(err, buffer));
  4094. wolfSSL_free(sslResume); sslResume = NULL;
  4095. CloseSocket(sockfd);
  4096. wolfSSL_CTX_free(ctx); ctx = NULL;
  4097. err_sys("wolfSSL_connect resume failed");
  4098. }
  4099. showPeerEx(sslResume, lng_index);
  4100. showPeerPEM(sslResume);
  4101. if (wolfSSL_session_reused(sslResume))
  4102. printf("reused session id\n");
  4103. else
  4104. LOG_ERROR("didn't reuse session id!!!\n");
  4105. #ifdef HAVE_ALPN
  4106. if (alpnList != NULL) {
  4107. char *protocol_name = NULL;
  4108. word16 protocol_nameSz = 0;
  4109. printf("Sending ALPN accepted list : %s\n", alpnList);
  4110. err = wolfSSL_ALPN_GetProtocol(sslResume, &protocol_name,
  4111. &protocol_nameSz);
  4112. if (err == WOLFSSL_SUCCESS)
  4113. printf("Received ALPN protocol : %s (%d)\n",
  4114. protocol_name, protocol_nameSz);
  4115. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  4116. printf("Not received ALPN response (no match with server)\n");
  4117. else
  4118. printf("Getting ALPN protocol name failed\n");
  4119. }
  4120. #endif
  4121. /* allow some time for exporting the session */
  4122. #ifdef WOLFSSL_SESSION_EXPORT_DEBUG
  4123. TEST_DELAY();
  4124. #endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
  4125. #ifdef HAVE_SECURE_RENEGOTIATION
  4126. if (scr && forceScr) {
  4127. if (nonBlocking) {
  4128. printf("not doing secure renegotiation on example with"
  4129. " nonblocking yet\n");
  4130. } else {
  4131. if (!resumeScr) {
  4132. printf("Beginning secure renegotiation.\n");
  4133. if (wolfSSL_Rehandshake(sslResume) != WOLFSSL_SUCCESS) {
  4134. err = wolfSSL_get_error(sslResume, 0);
  4135. LOG_ERROR("err = %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_Rehandshake failed");
  4141. }
  4142. else {
  4143. printf("RENEGOTIATION SUCCESSFUL\n");
  4144. }
  4145. }
  4146. else {
  4147. printf("Beginning secure resumption.\n");
  4148. if (wolfSSL_SecureResume(sslResume) != WOLFSSL_SUCCESS) {
  4149. err = wolfSSL_get_error(sslResume, 0);
  4150. LOG_ERROR("err = %d, %s\n", err,
  4151. wolfSSL_ERR_error_string(err, buffer));
  4152. wolfSSL_free(sslResume); sslResume = NULL;
  4153. CloseSocket(sockfd);
  4154. wolfSSL_CTX_free(ctx); ctx = NULL;
  4155. err_sys("wolfSSL_SecureResume failed");
  4156. }
  4157. else {
  4158. printf("SECURE RESUMPTION SUCCESSFUL\n");
  4159. }
  4160. }
  4161. }
  4162. }
  4163. #endif /* HAVE_SECURE_RENEGOTIATION */
  4164. XMEMSET(msg, 0, sizeof(msg));
  4165. if (sendGET) {
  4166. msgSz = (int)XSTRLEN(kHttpGetMsg);
  4167. XMEMCPY(msg, kHttpGetMsg, msgSz);
  4168. }
  4169. else {
  4170. msgSz = (int)XSTRLEN(kResumeMsg);
  4171. XMEMCPY(msg, kResumeMsg, msgSz);
  4172. }
  4173. (void)ClientWriteRead(sslResume, msg, msgSz, reply, sizeof(reply)-1,
  4174. sendGET, " resume", 0);
  4175. ret = wolfSSL_shutdown(sslResume);
  4176. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
  4177. wolfSSL_shutdown(sslResume); /* bidirectional shutdown */
  4178. /* display collected statistics */
  4179. #ifdef WOLFSSL_STATIC_MEMORY
  4180. if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
  4181. err_sys("static memory was not used with ssl");
  4182. LOG_ERROR("\nprint off SSLresume memory stats\n");
  4183. LOG_ERROR("*** This is memory state before wolfSSL_free is called\n");
  4184. wolfSSL_PrintStatsConn(&ssl_stats);
  4185. #endif
  4186. wolfSSL_free(sslResume); sslResume = NULL;
  4187. CloseSocket(sockfd);
  4188. }
  4189. #endif /* !NO_SESSION_CACHE */
  4190. wolfSSL_CTX_free(ctx); ctx = NULL;
  4191. ((func_args*)args)->return_code = 0;
  4192. exit:
  4193. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  4194. wolfsentry_ret =
  4195. wolfsentry_shutdown(WOLFSENTRY_CONTEXT_ARGS_OUT_EX4(&wolfsentry, NULL));
  4196. if (wolfsentry_ret < 0) {
  4197. LOG_ERROR(
  4198. "wolfsentry_shutdown() returned " WOLFSENTRY_ERROR_FMT "\n",
  4199. WOLFSENTRY_ERROR_FMT_ARGS(wolfsentry_ret));
  4200. }
  4201. #endif
  4202. #ifdef WOLFSSL_ASYNC_CRYPT
  4203. wolfAsync_DevClose(&devId);
  4204. #endif
  4205. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  4206. && defined(HAVE_STACK_SIZE)
  4207. wc_ecc_fp_free(); /* free per thread cache */
  4208. #endif
  4209. /* There are use cases when these assignments are not read. To avoid
  4210. * potential confusion those warnings have been handled here.
  4211. */
  4212. (void) useClientCert;
  4213. (void) verifyCert;
  4214. (void) ourCert;
  4215. (void) ourKey;
  4216. (void) useVerifyCb;
  4217. (void) customVerifyCert;
  4218. WOLFSSL_RETURN_FROM_THREAD(0);
  4219. }
  4220. #endif /* !NO_WOLFSSL_CLIENT */
  4221. /* so overall tests can pull in test function */
  4222. #ifndef NO_MAIN_DRIVER
  4223. int main(int argc, char** argv)
  4224. {
  4225. func_args args;
  4226. StartTCP();
  4227. #if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
  4228. args.srtp_helper = NULL;
  4229. #endif
  4230. args.argc = argc;
  4231. args.argv = argv;
  4232. args.return_code = 0;
  4233. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL) && !defined(STACK_TRAP)
  4234. wolfSSL_Debugging_ON();
  4235. #endif
  4236. wolfSSL_Init();
  4237. ChangeToWolfRoot();
  4238. #ifndef NO_WOLFSSL_CLIENT
  4239. #ifdef HAVE_STACK_SIZE
  4240. StackSizeCheck(&args, client_test);
  4241. #else
  4242. client_test(&args);
  4243. #endif
  4244. #else
  4245. fprintf(stderr, "Client not compiled in!\n");
  4246. #endif
  4247. wolfSSL_Cleanup();
  4248. #ifdef HAVE_WNR
  4249. if (wc_FreeNetRandom() < 0)
  4250. err_sys("Failed to free netRandom context");
  4251. #endif /* HAVE_WNR */
  4252. return args.return_code;
  4253. }
  4254. int myoptind = 0;
  4255. char* myoptarg = NULL;
  4256. #endif /* NO_MAIN_DRIVER */